LLVM  9.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 // Test whether the given expression computes a function address.
46 static bool isFunctionExpr(const MCExpr *Expr) {
47  if (auto SyExp = dyn_cast<MCSymbolRefExpr>(Expr))
48  return cast<MCSymbolWasm>(SyExp->getSymbol()).isFunction();
49 
50  if (auto BinOp = dyn_cast<MCBinaryExpr>(Expr))
51  return isFunctionExpr(BinOp->getLHS()) != isFunctionExpr(BinOp->getRHS());
52 
53  if (auto UnOp = dyn_cast<MCUnaryExpr>(Expr))
54  return isFunctionExpr(UnOp->getSubExpr());
55 
56  return false;
57 }
58 
59 static bool isFunctionType(const MCValue &Target) {
60  const MCSymbolRefExpr *RefA = Target.getSymA();
61  return RefA && RefA->getKind() == MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX;
62 }
63 
64 static const MCSection *getFixupSection(const MCExpr *Expr) {
65  if (auto SyExp = dyn_cast<MCSymbolRefExpr>(Expr)) {
66  if (SyExp->getSymbol().isInSection())
67  return &SyExp->getSymbol().getSection();
68  return nullptr;
69  }
70 
71  if (auto BinOp = dyn_cast<MCBinaryExpr>(Expr)) {
72  auto SectionLHS = getFixupSection(BinOp->getLHS());
73  auto SectionRHS = getFixupSection(BinOp->getRHS());
74  return SectionLHS == SectionRHS ? nullptr : SectionLHS;
75  }
76 
77  if (auto UnOp = dyn_cast<MCUnaryExpr>(Expr))
78  return getFixupSection(UnOp->getSubExpr());
79 
80  return nullptr;
81 }
82 
83 static bool isGlobalType(const MCValue &Target) {
84  const MCSymbolRefExpr *RefA = Target.getSymA();
85  return RefA && RefA->getKind() == MCSymbolRefExpr::VK_WebAssembly_GLOBAL;
86 }
87 
88 static bool isEventType(const MCValue &Target) {
89  const MCSymbolRefExpr *RefA = Target.getSymA();
90  return RefA && RefA->getKind() == MCSymbolRefExpr::VK_WebAssembly_EVENT;
91 }
92 
94  const MCFixup &Fixup) const {
95  // WebAssembly functions are not allocated in the data address space. To
96  // resolve a pointer to a function, we must use a special relocation type.
97  bool IsFunction = isFunctionExpr(Fixup.getValue());
98 
99  switch (unsigned(Fixup.getKind())) {
101  if (IsFunction)
102  return wasm::R_WASM_TABLE_INDEX_SLEB;
103  return wasm::R_WASM_MEMORY_ADDR_SLEB;
105  llvm_unreachable("fixup_sleb128_i64 not implemented yet");
107  if (isGlobalType(Target))
108  return wasm::R_WASM_GLOBAL_INDEX_LEB;
109  if (isFunctionType(Target))
110  return wasm::R_WASM_TYPE_INDEX_LEB;
111  if (IsFunction)
112  return wasm::R_WASM_FUNCTION_INDEX_LEB;
113  if (isEventType(Target))
114  return wasm::R_WASM_EVENT_INDEX_LEB;
115  return wasm::R_WASM_MEMORY_ADDR_LEB;
116  case FK_Data_4:
117  if (IsFunction)
118  return wasm::R_WASM_TABLE_INDEX_I32;
119  if (auto Section = static_cast<const MCSectionWasm *>(
120  getFixupSection(Fixup.getValue()))) {
121  if (Section->getKind().isText())
122  return wasm::R_WASM_FUNCTION_OFFSET_I32;
123  else if (!Section->isWasmData())
124  return wasm::R_WASM_SECTION_OFFSET_I32;
125  }
126  return wasm::R_WASM_MEMORY_ADDR_I32;
127  case FK_Data_8:
128  llvm_unreachable("FK_Data_8 not implemented yet");
129  default:
130  llvm_unreachable("unimplemented fixup kind");
131  }
132 }
133 
134 std::unique_ptr<MCObjectTargetWriter>
136  return llvm::make_unique<WebAssemblyWasmObjectWriter>(Is64Bit);
137 }
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)
VariantKind getKind() const
Definition: MCExpr.h:337
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:73
static bool isFunctionType(const MCValue &Target)
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:165
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 bool isFunctionExpr(const MCExpr *Expr)
static const MCSection * getFixupSection(const MCExpr *Expr)
A four-byte fixup.
Definition: MCFixup.h:25
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
Target - Wrapper for Target specific information.
A eight-byte fixup.
Definition: MCFixup.h:26
static bool isEventType(const MCValue &Target)
const MCExpr * getValue() const
Definition: MCFixup.h:127
static bool isGlobalType(const MCValue &Target)
MCFixupKind getKind() const
Definition: MCFixup.h:122