1 //===-- WebAssemblyWasmObjectWriter.cpp - WebAssembly Wasm Writer ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file handles Wasm-specific object emission, converting LLVM's
12 /// internal fixups into the appropriate relocations.
14 //===----------------------------------------------------------------------===//
16 #include "MCTargetDesc/WebAssemblyFixupKinds.h"
17 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
18 #include "llvm/BinaryFormat/Wasm.h"
19 #include "llvm/MC/MCAsmBackend.h"
20 #include "llvm/MC/MCFixup.h"
21 #include "llvm/MC/MCFixupKindInfo.h"
22 #include "llvm/MC/MCObjectWriter.h"
23 #include "llvm/MC/MCSymbolWasm.h"
24 #include "llvm/MC/MCWasmObjectWriter.h"
25 #include "llvm/MC/MCValue.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/ErrorHandling.h"
32 class WebAssemblyWasmObjectWriter final : public MCWasmObjectTargetWriter {
34 explicit WebAssemblyWasmObjectWriter(bool Is64Bit);
37 unsigned getRelocType(const MCValue &Target,
38 const MCFixup &Fixup) const override;
40 } // end anonymous namespace
42 WebAssemblyWasmObjectWriter::WebAssemblyWasmObjectWriter(bool Is64Bit)
43 : MCWasmObjectTargetWriter(Is64Bit) {}
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();
50 if (auto BinOp = dyn_cast<MCBinaryExpr>(Expr))
51 return IsFunctionExpr(BinOp->getLHS()) != IsFunctionExpr(BinOp->getRHS());
53 if (auto UnOp = dyn_cast<MCUnaryExpr>(Expr))
54 return IsFunctionExpr(UnOp->getSubExpr());
59 static bool IsFunctionType(const MCValue &Target) {
60 const MCSymbolRefExpr *RefA = Target.getSymA();
61 return RefA && RefA->getKind() == MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX;
65 WebAssemblyWasmObjectWriter::getRelocType(const MCValue &Target,
66 const MCFixup &Fixup) const {
67 // WebAssembly functions are not allocated in the data address space. To
68 // resolve a pointer to a function, we must use a special relocation type.
69 bool IsFunction = IsFunctionExpr(Fixup.getValue());
71 switch (unsigned(Fixup.getKind())) {
72 case WebAssembly::fixup_code_global_index:
73 return wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB;
74 case WebAssembly::fixup_code_sleb128_i32:
76 return wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB;
77 return wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB;
78 case WebAssembly::fixup_code_sleb128_i64:
79 llvm_unreachable("fixup_sleb128_i64 not implemented yet");
80 case WebAssembly::fixup_code_uleb128_i32:
81 if (IsFunctionType(Target))
82 return wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB;
84 return wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB;
85 return wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB;
88 return wasm::R_WEBASSEMBLY_TABLE_INDEX_I32;
89 return wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32;
91 llvm_unreachable("FK_Data_8 not implemented yet");
93 llvm_unreachable("unimplemented fixup kind");
97 std::unique_ptr<MCObjectWriter>
98 llvm::createWebAssemblyWasmObjectWriter(raw_pwrite_stream &OS,
100 auto MOTW = llvm::make_unique<WebAssemblyWasmObjectWriter>(Is64Bit);
101 return createWasmObjectWriter(std::move(MOTW), OS);