LLVM  12.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,
38  const MCFixup &Fixup) const override;
39 };
40 } // end anonymous namespace
41 
42 WebAssemblyWasmObjectWriter::WebAssemblyWasmObjectWriter(bool Is64Bit,
43  bool IsEmscripten)
44  : MCWasmObjectTargetWriter(Is64Bit, IsEmscripten) {}
45 
46 static const MCSection *getFixupSection(const MCExpr *Expr) {
47  if (auto SyExp = dyn_cast<MCSymbolRefExpr>(Expr)) {
48  if (SyExp->getSymbol().isInSection())
49  return &SyExp->getSymbol().getSection();
50  return nullptr;
51  }
52 
53  if (auto BinOp = dyn_cast<MCBinaryExpr>(Expr)) {
54  auto SectionLHS = getFixupSection(BinOp->getLHS());
55  auto SectionRHS = getFixupSection(BinOp->getRHS());
56  return SectionLHS == SectionRHS ? nullptr : SectionLHS;
57  }
58 
59  if (auto UnOp = dyn_cast<MCUnaryExpr>(Expr))
60  return getFixupSection(UnOp->getSubExpr());
61 
62  return nullptr;
63 }
64 
65 unsigned WebAssemblyWasmObjectWriter::getRelocType(const MCValue &Target,
66  const MCFixup &Fixup) const {
67  const MCSymbolRefExpr *RefA = Target.getSymA();
68  assert(RefA);
69  auto& SymA = cast<MCSymbolWasm>(RefA->getSymbol());
70 
72 
73  switch (Modifier) {
75  return wasm::R_WASM_GLOBAL_INDEX_LEB;
77  assert(SymA.isFunction());
78  return wasm::R_WASM_TABLE_INDEX_REL_SLEB;
80  assert(SymA.isData());
81  return is64Bit() ? wasm::R_WASM_MEMORY_ADDR_REL_SLEB64
82  : wasm::R_WASM_MEMORY_ADDR_REL_SLEB;
84  return wasm::R_WASM_TYPE_INDEX_LEB;
85  default:
86  break;
87  }
88 
89  switch (unsigned(Fixup.getKind())) {
91  if (SymA.isFunction())
92  return wasm::R_WASM_TABLE_INDEX_SLEB;
93  return wasm::R_WASM_MEMORY_ADDR_SLEB;
95  assert(SymA.isData());
96  return wasm::R_WASM_MEMORY_ADDR_SLEB64;
98  if (SymA.isGlobal())
99  return wasm::R_WASM_GLOBAL_INDEX_LEB;
100  if (SymA.isFunction())
101  return wasm::R_WASM_FUNCTION_INDEX_LEB;
102  if (SymA.isEvent())
103  return wasm::R_WASM_EVENT_INDEX_LEB;
104  return wasm::R_WASM_MEMORY_ADDR_LEB;
106  assert(SymA.isData());
107  return wasm::R_WASM_MEMORY_ADDR_LEB64;
108  case FK_Data_4:
109  if (SymA.isFunction())
110  return wasm::R_WASM_TABLE_INDEX_I32;
111  if (SymA.isGlobal())
112  return wasm::R_WASM_GLOBAL_INDEX_I32;
113  if (auto Section = static_cast<const MCSectionWasm *>(
114  getFixupSection(Fixup.getValue()))) {
115  if (Section->getKind().isText())
116  return wasm::R_WASM_FUNCTION_OFFSET_I32;
117  else if (!Section->isWasmData())
118  return wasm::R_WASM_SECTION_OFFSET_I32;
119  }
120  return wasm::R_WASM_MEMORY_ADDR_I32;
121  case FK_Data_8:
122  assert(SymA.isData());
123  return wasm::R_WASM_MEMORY_ADDR_I64;
124  default:
125  llvm_unreachable("unimplemented fixup kind");
126  }
127 }
128 
129 std::unique_ptr<MCObjectTargetWriter>
130 llvm::createWebAssemblyWasmObjectWriter(bool Is64Bit, bool IsEmscripten) {
131  return std::make_unique<WebAssemblyWasmObjectWriter>(Is64Bit, IsEmscripten);
132 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This represents an "assembler immediate".
Definition: MCValue.h:37
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:82
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:192
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:45
#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:397
Target - Wrapper for Target specific information.
A eight-byte fixup.
Definition: MCFixup.h:27
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::unique_ptr< MCObjectTargetWriter > createWebAssemblyWasmObjectWriter(bool Is64Bit, bool IsEmscripten)
const MCExpr * getValue() const
Definition: MCFixup.h:138
static bool is64Bit(const char *name)
MCFixupKind getKind() const
Definition: MCFixup.h:131