LLVM  5.0.0
WebAssemblyWasmObjectWriter.cpp
Go to the documentation of this file.
1 //===-- WebAssemblyWasmObjectWriter.cpp - WebAssembly Wasm Writer ---------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief This file handles Wasm-specific object emission, converting LLVM's
12 /// internal fixups into the appropriate relocations.
13 ///
14 //===----------------------------------------------------------------------===//
15 
18 #include "llvm/BinaryFormat/Wasm.h"
19 #include "llvm/MC/MCAsmBackend.h"
20 #include "llvm/MC/MCFixup.h"
22 #include "llvm/MC/MCSymbolWasm.h"
24 #include "llvm/MC/MCValue.h"
25 #include "llvm/Support/Casting.h"
27 
28 using namespace llvm;
29 
30 namespace {
31 class WebAssemblyWasmObjectWriter final : public MCWasmObjectTargetWriter {
32 public:
33  explicit WebAssemblyWasmObjectWriter(bool Is64Bit);
34 
35 private:
36  unsigned getRelocType(const MCValue &Target,
37  const MCFixup &Fixup) const override;
38 };
39 } // end anonymous namespace
40 
41 WebAssemblyWasmObjectWriter::WebAssemblyWasmObjectWriter(bool Is64Bit)
42  : MCWasmObjectTargetWriter(Is64Bit) {}
43 
44 // Test whether the given expression computes a function address.
45 static bool IsFunctionExpr(const MCExpr *Expr) {
46  if (auto SyExp = dyn_cast<MCSymbolRefExpr>(Expr))
47  return cast<MCSymbolWasm>(SyExp->getSymbol()).isFunction();
48 
49  if (auto BinOp = dyn_cast<MCBinaryExpr>(Expr))
50  return IsFunctionExpr(BinOp->getLHS()) != IsFunctionExpr(BinOp->getRHS());
51 
52  if (auto UnOp = dyn_cast<MCUnaryExpr>(Expr))
53  return IsFunctionExpr(UnOp->getSubExpr());
54 
55  return false;
56 }
57 
58 static bool IsFunctionType(const MCValue &Target) {
59  const MCSymbolRefExpr *RefA = Target.getSymA();
60  return RefA && RefA->getKind() == MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX;
61 }
62 
63 unsigned
65  const MCFixup &Fixup) const {
66  // WebAssembly functions are not allocated in the data address space. To
67  // resolve a pointer to a function, we must use a special relocation type.
68  bool IsFunction = IsFunctionExpr(Fixup.getValue());
69 
70  switch (unsigned(Fixup.getKind())) {
72  return wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB;
74  if (IsFunction)
75  return wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB;
76  return wasm::R_WEBASSEMBLY_GLOBAL_ADDR_SLEB;
78  llvm_unreachable("fixup_sleb128_i64 not implemented yet");
80  if (IsFunctionType(Target))
81  return wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB;
82  if (IsFunction)
83  return wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB;
84  return wasm::R_WEBASSEMBLY_GLOBAL_ADDR_LEB;
85  case FK_Data_4:
86  if (IsFunction)
87  return wasm::R_WEBASSEMBLY_TABLE_INDEX_I32;
88  return wasm::R_WEBASSEMBLY_GLOBAL_ADDR_I32;
89  case FK_Data_8:
90  llvm_unreachable("FK_Data_8 not implemented yet");
91  default:
92  llvm_unreachable("unimplemented fixup kind");
93  }
94 }
95 
97  bool Is64Bit) {
98  MCWasmObjectTargetWriter *MOTW = new WebAssemblyWasmObjectWriter(Is64Bit);
99  return createWasmObjectWriter(MOTW, OS);
100 }
MCObjectWriter * createWasmObjectWriter(MCWasmObjectTargetWriter *MOTW, raw_pwrite_stream &OS)
Construct a new Wasm writer instance.
MCObjectWriter * createWebAssemblyWasmObjectWriter(raw_pwrite_stream &OS, bool Is64Bit)
This represents an "assembler immediate".
Definition: MCValue.h:40
Defines the object file and target independent interfaces used by the assembler backend to write nati...
static bool IsFunctionType(const MCValue &Target)
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:66
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
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.
A four-byte fixup.
Definition: MCFixup.h:26
const MCExpr * getValue() const
Definition: MCFixup.h:98
This file provides WebAssembly-specific target descriptions.
MCFixupKind getKind() const
Definition: MCFixup.h:93
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:47
Target - Wrapper for Target specific information.
llvm_unreachable("Unknown RegBank!")
A eight-byte fixup.
Definition: MCFixup.h:27
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:333
VariantKind getKind() const
Definition: MCExpr.h:320
static bool IsFunctionExpr(const MCExpr *Expr)