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 15 #include "MCTargetDesc/WebAssemblyFixupKinds.h" 16 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" 17 #include "llvm/BinaryFormat/Wasm.h" 18 #include "llvm/MC/MCAsmBackend.h" 19 #include "llvm/MC/MCFixup.h" 20 #include "llvm/MC/MCFixupKindInfo.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" 25 #include "llvm/MC/MCWasmObjectWriter.h" 26 #include "llvm/Support/Casting.h" 27 #include "llvm/Support/ErrorHandling.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 static bool isFunctionSignatureRef(const MCSymbolRefExpr *Ref) { 46 return Ref->getKind() == MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX; 47 } 48 49 static bool isGOTRef(const MCSymbolRefExpr *Ref) { 50 return Ref->getKind() == MCSymbolRefExpr::VK_GOT; 51 } 52 53 static const MCSection *getFixupSection(const MCExpr *Expr) { 54 if (auto SyExp = dyn_cast<MCSymbolRefExpr>(Expr)) { 55 if (SyExp->getSymbol().isInSection()) 56 return &SyExp->getSymbol().getSection(); 57 return nullptr; 58 } 59 60 if (auto BinOp = dyn_cast<MCBinaryExpr>(Expr)) { 61 auto SectionLHS = getFixupSection(BinOp->getLHS()); 62 auto SectionRHS = getFixupSection(BinOp->getRHS()); 63 return SectionLHS == SectionRHS ? nullptr : SectionLHS; 64 } 65 66 if (auto UnOp = dyn_cast<MCUnaryExpr>(Expr)) 67 return getFixupSection(UnOp->getSubExpr()); 68 69 return nullptr; 70 } 71 72 unsigned WebAssemblyWasmObjectWriter::getRelocType(const MCValue &Target, 73 const MCFixup &Fixup) const { 74 const MCSymbolRefExpr *RefA = Target.getSymA(); 75 assert(RefA); 76 auto& SymA = cast<MCSymbolWasm>(RefA->getSymbol()); 77 78 switch (unsigned(Fixup.getKind())) { 79 case WebAssembly::fixup_sleb128_i32: 80 if (SymA.isFunction()) 81 return wasm::R_WASM_TABLE_INDEX_SLEB; 82 return wasm::R_WASM_MEMORY_ADDR_SLEB; 83 case WebAssembly::fixup_sleb128_i64: 84 llvm_unreachable("fixup_sleb128_i64 not implemented yet"); 85 case WebAssembly::fixup_uleb128_i32: 86 if (SymA.isGlobal() || isGOTRef(RefA)) 87 return wasm::R_WASM_GLOBAL_INDEX_LEB; 88 if (SymA.isFunction()) { 89 if (isFunctionSignatureRef(RefA)) 90 return wasm::R_WASM_TYPE_INDEX_LEB; 91 else 92 return wasm::R_WASM_FUNCTION_INDEX_LEB; 93 } 94 if (SymA.isEvent()) 95 return wasm::R_WASM_EVENT_INDEX_LEB; 96 return wasm::R_WASM_MEMORY_ADDR_LEB; 97 case FK_Data_4: 98 if (SymA.isFunction()) 99 return wasm::R_WASM_TABLE_INDEX_I32; 100 if (auto Section = static_cast<const MCSectionWasm *>( 101 getFixupSection(Fixup.getValue()))) { 102 if (Section->getKind().isText()) 103 return wasm::R_WASM_FUNCTION_OFFSET_I32; 104 else if (!Section->isWasmData()) 105 return wasm::R_WASM_SECTION_OFFSET_I32; 106 } 107 return wasm::R_WASM_MEMORY_ADDR_I32; 108 default: 109 llvm_unreachable("unimplemented fixup kind"); 110 } 111 } 112 113 std::unique_ptr<MCObjectTargetWriter> 114 llvm::createWebAssemblyWasmObjectWriter(bool Is64Bit) { 115 return llvm::make_unique<WebAssemblyWasmObjectWriter>(Is64Bit); 116 } 117