LLVM  14.0.0git
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, bool IsEmscripten);
35 
36 private:
37  unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup,
38  const MCSectionWasm &FixupSection,
39  bool IsLocRel) const override;
40 };
41 } // end anonymous namespace
42 
43 WebAssemblyWasmObjectWriter::WebAssemblyWasmObjectWriter(bool Is64Bit,
44  bool IsEmscripten)
45  : MCWasmObjectTargetWriter(Is64Bit, IsEmscripten) {}
46 
47 static const MCSection *getTargetSection(const MCExpr *Expr) {
48  if (auto SyExp = dyn_cast<MCSymbolRefExpr>(Expr)) {
49  if (SyExp->getSymbol().isInSection())
50  return &SyExp->getSymbol().getSection();
51  return nullptr;
52  }
53 
54  if (auto BinOp = dyn_cast<MCBinaryExpr>(Expr)) {
55  auto SectionLHS = getTargetSection(BinOp->getLHS());
56  auto SectionRHS = getTargetSection(BinOp->getRHS());
57  return SectionLHS == SectionRHS ? nullptr : SectionLHS;
58  }
59 
60  if (auto UnOp = dyn_cast<MCUnaryExpr>(Expr))
61  return getTargetSection(UnOp->getSubExpr());
62 
63  return nullptr;
64 }
65 
66 unsigned WebAssemblyWasmObjectWriter::getRelocType(
67  const MCValue &Target, const MCFixup &Fixup,
68  const MCSectionWasm &FixupSection, bool IsLocRel) const {
69  const MCSymbolRefExpr *RefA = Target.getSymA();
70  assert(RefA);
71  auto& SymA = cast<MCSymbolWasm>(RefA->getSymbol());
72 
73  MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();
74 
75  switch (Modifier) {
78  return wasm::R_WASM_GLOBAL_INDEX_LEB;
80  assert(SymA.isFunction());
81  return is64Bit() ? wasm::R_WASM_TABLE_INDEX_REL_SLEB64
82  : wasm::R_WASM_TABLE_INDEX_REL_SLEB;
84  return is64Bit() ? wasm::R_WASM_MEMORY_ADDR_TLS_SLEB64
85  : wasm::R_WASM_MEMORY_ADDR_TLS_SLEB;
87  assert(SymA.isData());
88  return is64Bit() ? wasm::R_WASM_MEMORY_ADDR_REL_SLEB64
89  : wasm::R_WASM_MEMORY_ADDR_REL_SLEB;
91  return wasm::R_WASM_TYPE_INDEX_LEB;
93  break;
94  default:
95  report_fatal_error("unknown VariantKind");
96  break;
97  }
98 
99  switch (unsigned(Fixup.getKind())) {
101  if (SymA.isFunction())
102  return wasm::R_WASM_TABLE_INDEX_SLEB;
103  return wasm::R_WASM_MEMORY_ADDR_SLEB;
105  if (SymA.isFunction())
106  return wasm::R_WASM_TABLE_INDEX_SLEB64;
107  return wasm::R_WASM_MEMORY_ADDR_SLEB64;
109  if (SymA.isGlobal())
110  return wasm::R_WASM_GLOBAL_INDEX_LEB;
111  if (SymA.isFunction())
112  return wasm::R_WASM_FUNCTION_INDEX_LEB;
113  if (SymA.isTag())
114  return wasm::R_WASM_TAG_INDEX_LEB;
115  if (SymA.isTable())
116  return wasm::R_WASM_TABLE_NUMBER_LEB;
117  return wasm::R_WASM_MEMORY_ADDR_LEB;
119  assert(SymA.isData());
120  return wasm::R_WASM_MEMORY_ADDR_LEB64;
121  case FK_Data_4:
122  if (SymA.isFunction()) {
123  if (FixupSection.getKind().isMetadata())
124  return wasm::R_WASM_FUNCTION_OFFSET_I32;
125  assert(FixupSection.isWasmData());
126  return wasm::R_WASM_TABLE_INDEX_I32;
127  }
128  if (SymA.isGlobal())
129  return wasm::R_WASM_GLOBAL_INDEX_I32;
130  if (auto Section = static_cast<const MCSectionWasm *>(
131  getTargetSection(Fixup.getValue()))) {
132  if (Section->getKind().isText())
133  return wasm::R_WASM_FUNCTION_OFFSET_I32;
134  else if (!Section->isWasmData())
135  return wasm::R_WASM_SECTION_OFFSET_I32;
136  }
137  return IsLocRel ? wasm::R_WASM_MEMORY_ADDR_LOCREL_I32
138  : wasm::R_WASM_MEMORY_ADDR_I32;
139  case FK_Data_8:
140  if (SymA.isFunction()) {
141  if (FixupSection.getKind().isMetadata())
142  return wasm::R_WASM_FUNCTION_OFFSET_I64;
143  return wasm::R_WASM_TABLE_INDEX_I64;
144  }
145  if (SymA.isGlobal())
146  llvm_unreachable("unimplemented R_WASM_GLOBAL_INDEX_I64");
147  if (auto Section = static_cast<const MCSectionWasm *>(
148  getTargetSection(Fixup.getValue()))) {
149  if (Section->getKind().isText())
150  return wasm::R_WASM_FUNCTION_OFFSET_I64;
151  else if (!Section->isWasmData())
152  llvm_unreachable("unimplemented R_WASM_SECTION_OFFSET_I64");
153  }
154  assert(SymA.isData());
155  return wasm::R_WASM_MEMORY_ADDR_I64;
156  default:
157  llvm_unreachable("unimplemented fixup kind");
158  }
159 }
160 
161 std::unique_ptr<MCObjectTargetWriter>
162 llvm::createWebAssemblyWasmObjectWriter(bool Is64Bit, bool IsEmscripten) {
163  return std::make_unique<WebAssemblyWasmObjectWriter>(Is64Bit, IsEmscripten);
164 }
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::MCSymbolRefExpr::VK_GOT
@ VK_GOT
Definition: MCExpr.h:198
is64Bit
static bool is64Bit(const char *name)
Definition: X86Disassembler.cpp:1023
llvm::WebAssembly::fixup_uleb128_i64
@ fixup_uleb128_i64
Definition: WebAssemblyFixupKinds.h:20
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:137
ErrorHandling.h
MCWasmObjectWriter.h
MCFixupKindInfo.h
llvm::WebAssembly::fixup_uleb128_i32
@ fixup_uleb128_i32
Definition: WebAssemblyFixupKinds.h:19
llvm::createWebAssemblyWasmObjectWriter
std::unique_ptr< MCObjectTargetWriter > createWebAssemblyWasmObjectWriter(bool Is64Bit, bool IsEmscripten)
Definition: WebAssemblyWasmObjectWriter.cpp:162
llvm::MCSymbolRefExpr::VK_WASM_MBREL
@ VK_WASM_MBREL
Definition: MCExpr.h:329
llvm::ARMBuildAttrs::Section
@ Section
Legacy Tags.
Definition: ARMBuildAttributes.h:78
llvm::WebAssembly::fixup_sleb128_i64
@ fixup_sleb128_i64
Definition: WebAssemblyFixupKinds.h:18
llvm::FK_Data_4
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:25
llvm::MCSymbolRefExpr::VK_WASM_TBREL
@ VK_WASM_TBREL
Definition: MCExpr.h:330
llvm::MCWasmObjectTargetWriter
Definition: MCWasmObjectWriter.h:22
MCAsmBackend.h
llvm::MCSectionWasm
This represents a section on wasm.
Definition: MCSectionWasm.h:26
MCSymbolWasm.h
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
llvm::MCSymbolRefExpr::getSymbol
const MCSymbol & getSymbol() const
Definition: MCExpr.h:398
llvm::MCSectionWasm::isWasmData
bool isWasmData() const
Definition: MCSectionWasm.h:67
WebAssemblyMCTargetDesc.h
llvm::MCSymbolRefExpr::VK_WASM_GOT_TLS
@ VK_WASM_GOT_TLS
Definition: MCExpr.h:331
llvm::MCSymbolRefExpr::VariantKind
VariantKind
Definition: MCExpr.h:194
llvm::SectionKind::isMetadata
bool isMetadata() const
Definition: SectionKind.h:119
llvm::MCSymbolRefExpr::VK_WASM_TLSREL
@ VK_WASM_TLSREL
Definition: MCExpr.h:328
getTargetSection
static const MCSection * getTargetSection(const MCExpr *Expr)
Definition: WebAssemblyWasmObjectWriter.cpp:47
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MCSymbolRefExpr
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
llvm::WebAssembly::fixup_sleb128_i32
@ fixup_sleb128_i32
Definition: WebAssemblyFixupKinds.h:17
Fixup
PowerPC TLS Dynamic Call Fixup
Definition: PPCTLSDynamicCall.cpp:235
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
MCFixup.h
llvm::MCSection
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
MCObjectWriter.h
Casting.h
llvm::MCSymbolRefExpr::VK_WASM_TYPEINDEX
@ VK_WASM_TYPEINDEX
Definition: MCExpr.h:327
Wasm.h
MCValue.h
llvm::MCSection::getKind
SectionKind getKind() const
Definition: MCSection.h:123
llvm::FK_Data_8
@ FK_Data_8
A eight-byte fixup.
Definition: MCFixup.h:26
llvm::MCValue
This represents an "assembler immediate".
Definition: MCValue.h:37
llvm::MCSymbolRefExpr::VK_None
@ VK_None
Definition: MCExpr.h:195
llvm::MCFixup
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
WebAssemblyFixupKinds.h
MCSectionWasm.h