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, 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 
71   MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();
72 
73   switch (Modifier) {
74     case MCSymbolRefExpr::VK_GOT:
75       return wasm::R_WASM_GLOBAL_INDEX_LEB;
76     case MCSymbolRefExpr::VK_WASM_TBREL:
77       assert(SymA.isFunction());
78       return wasm::R_WASM_TABLE_INDEX_REL_SLEB;
79     case MCSymbolRefExpr::VK_WASM_TLSREL:
80       return wasm::R_WASM_MEMORY_ADDR_TLS_SLEB;
81     case MCSymbolRefExpr::VK_WASM_MBREL:
82       assert(SymA.isData());
83       return is64Bit() ? wasm::R_WASM_MEMORY_ADDR_REL_SLEB64
84                        : wasm::R_WASM_MEMORY_ADDR_REL_SLEB;
85     case MCSymbolRefExpr::VK_WASM_TYPEINDEX:
86       return wasm::R_WASM_TYPE_INDEX_LEB;
87     default:
88       break;
89   }
90 
91   switch (unsigned(Fixup.getKind())) {
92   case WebAssembly::fixup_sleb128_i32:
93     if (SymA.isFunction())
94       return wasm::R_WASM_TABLE_INDEX_SLEB;
95     return wasm::R_WASM_MEMORY_ADDR_SLEB;
96   case WebAssembly::fixup_sleb128_i64:
97     if (SymA.isFunction())
98       return wasm::R_WASM_TABLE_INDEX_SLEB64;
99     return wasm::R_WASM_MEMORY_ADDR_SLEB64;
100   case WebAssembly::fixup_uleb128_i32:
101     if (SymA.isGlobal())
102       return wasm::R_WASM_GLOBAL_INDEX_LEB;
103     if (SymA.isFunction())
104       return wasm::R_WASM_FUNCTION_INDEX_LEB;
105     if (SymA.isEvent())
106       return wasm::R_WASM_EVENT_INDEX_LEB;
107     if (SymA.isTable())
108       return wasm::R_WASM_TABLE_NUMBER_LEB;
109     return wasm::R_WASM_MEMORY_ADDR_LEB;
110   case WebAssembly::fixup_uleb128_i64:
111     assert(SymA.isData());
112     return wasm::R_WASM_MEMORY_ADDR_LEB64;
113   case FK_Data_4:
114     if (SymA.isFunction())
115       return wasm::R_WASM_TABLE_INDEX_I32;
116     if (SymA.isGlobal())
117       return wasm::R_WASM_GLOBAL_INDEX_I32;
118     if (auto Section = static_cast<const MCSectionWasm *>(
119             getFixupSection(Fixup.getValue()))) {
120       if (Section->getKind().isText())
121         return wasm::R_WASM_FUNCTION_OFFSET_I32;
122       else if (!Section->isWasmData())
123         return wasm::R_WASM_SECTION_OFFSET_I32;
124     }
125     return wasm::R_WASM_MEMORY_ADDR_I32;
126   case FK_Data_8:
127     if (SymA.isFunction())
128       return wasm::R_WASM_TABLE_INDEX_I64;
129     if (SymA.isGlobal())
130       llvm_unreachable("unimplemented R_WASM_GLOBAL_INDEX_I64");
131     if (auto Section = static_cast<const MCSectionWasm *>(
132             getFixupSection(Fixup.getValue()))) {
133       if (Section->getKind().isText())
134         return wasm::R_WASM_FUNCTION_OFFSET_I64;
135       else if (!Section->isWasmData())
136         llvm_unreachable("unimplemented R_WASM_SECTION_OFFSET_I64");
137     }
138     assert(SymA.isData());
139     return wasm::R_WASM_MEMORY_ADDR_I64;
140   default:
141     llvm_unreachable("unimplemented fixup kind");
142   }
143 }
144 
145 std::unique_ptr<MCObjectTargetWriter>
146 llvm::createWebAssemblyWasmObjectWriter(bool Is64Bit, bool IsEmscripten) {
147   return std::make_unique<WebAssemblyWasmObjectWriter>(Is64Bit, IsEmscripten);
148 }
149