LLVM  10.0.0svn
WebAssemblyWasmObjectWriter.cpp
Go to the documentation of this file.
1 //===-- WebAssemblyWasmObjectWriter.cpp - WebAssembly Wasm Writer ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file handles Wasm-specific object emission, converting LLVM's
11 /// internal fixups into the appropriate relocations.
12 ///
13 //===----------------------------------------------------------------------===//
14 
17 #include "llvm/BinaryFormat/Wasm.h"
18 #include "llvm/MC/MCAsmBackend.h"
19 #include "llvm/MC/MCFixup.h"
21 #include "llvm/MC/MCObjectWriter.h"
22 #include "llvm/MC/MCSectionWasm.h"
23 #include "llvm/MC/MCSymbolWasm.h"
24 #include "llvm/MC/MCValue.h"
26 #include "llvm/Support/Casting.h"
28 
29 using namespace llvm;
30 
31 namespace {
32 class WebAssemblyWasmObjectWriter final : public MCWasmObjectTargetWriter {
33 public:
34  explicit WebAssemblyWasmObjectWriter(bool Is64Bit);
35 
36 private:
37  unsigned getRelocType(const MCValue &Target,
38  const MCFixup &Fixup) const override;
39 };
40 } // end anonymous namespace
41 
42 WebAssemblyWasmObjectWriter::WebAssemblyWasmObjectWriter(bool Is64Bit)
43  : MCWasmObjectTargetWriter(Is64Bit) {}
44 
45 static const MCSection *getFixupSection(const MCExpr *Expr) {
46  if (auto SyExp = dyn_cast<MCSymbolRefExpr>(Expr)) {
47  if (SyExp->getSymbol().isInSection())
48  return &SyExp->getSymbol().getSection();
49  return nullptr;
50  }
51 
52  if (auto BinOp = dyn_cast<MCBinaryExpr>(Expr)) {
53  auto SectionLHS = getFixupSection(BinOp->getLHS());
54  auto SectionRHS = getFixupSection(BinOp->getRHS());
55  return SectionLHS == SectionRHS ? nullptr : SectionLHS;
56  }
57 
58  if (auto UnOp = dyn_cast<MCUnaryExpr>(Expr))
59  return getFixupSection(UnOp->getSubExpr());
60 
61  return nullptr;
62 }
63 
65  const MCFixup &Fixup) const {
66  const MCSymbolRefExpr *RefA = Target.getSymA();
67  assert(RefA);
68  auto& SymA = cast<MCSymbolWasm>(RefA->getSymbol());
69 
71 
72  switch (Modifier) {
74  return wasm::R_WASM_GLOBAL_INDEX_LEB;
76  assert(SymA.isFunction());
77  return wasm::R_WASM_TABLE_INDEX_REL_SLEB;
79  assert(SymA.isData());
80  return wasm::R_WASM_MEMORY_ADDR_REL_SLEB;
82  return wasm::R_WASM_TYPE_INDEX_LEB;
83  default:
84  break;
85  }
86 
87  switch (unsigned(Fixup.getKind())) {
89  if (SymA.isFunction())
90  return wasm::R_WASM_TABLE_INDEX_SLEB;
91  return wasm::R_WASM_MEMORY_ADDR_SLEB;
93  llvm_unreachable("fixup_sleb128_i64 not implemented yet");
95  if (SymA.isGlobal())
96  return wasm::R_WASM_GLOBAL_INDEX_LEB;
97  if (SymA.isFunction())
98  return wasm::R_WASM_FUNCTION_INDEX_LEB;
99  if (SymA.isEvent())
100  return wasm::R_WASM_EVENT_INDEX_LEB;
101  return wasm::R_WASM_MEMORY_ADDR_LEB;
102  case FK_Data_4:
103  if (SymA.isFunction())
104  return wasm::R_WASM_TABLE_INDEX_I32;
105  if (auto Section = static_cast<const MCSectionWasm *>(
106  getFixupSection(Fixup.getValue()))) {
107  if (Section->getKind().isText())
108  return wasm::R_WASM_FUNCTION_OFFSET_I32;
109  else if (!Section->isWasmData())
110  return wasm::R_WASM_SECTION_OFFSET_I32;
111  }
112  return wasm::R_WASM_MEMORY_ADDR_I32;
113  default:
114  llvm_unreachable("unimplemented fixup kind");
115  }
116 }
117 
118 std::unique_ptr<MCObjectTargetWriter>
120  return std::make_unique<WebAssemblyWasmObjectWriter>(Is64Bit);
121 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:38
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This represents an "assembler immediate".
Definition: MCValue.h:39
std::unique_ptr< MCObjectTargetWriter > createWebAssemblyWasmObjectWriter(bool Is64Bit)
MCSymbolRefExpr::VariantKind getAccessVariant() const
Definition: MCValue.cpp:46
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:77
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:169
static unsigned getRelocType(const MCValue &Target, const MCFixupKind FixupKind, const bool IsPCRel)
Translates generic PPC fixup kind to Mach-O/PPC relocation type enum.
static const MCSection * getFixupSection(const MCExpr *Expr)
A four-byte fixup.
Definition: MCFixup.h:26
This file provides WebAssembly-specific target descriptions.
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:47
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
PowerPC TLS Dynamic Call Fixup
const MCSymbol & getSymbol() const
Definition: MCExpr.h:340
Target - Wrapper for Target specific information.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MCExpr * getValue() const
Definition: MCFixup.h:131
MCFixupKind getKind() const
Definition: MCFixup.h:126