xref: /llvm-project/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp (revision 43570a2841e2a8f1efd00503beee751cc1e72513)
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/MCObjectWriter.h"
21 #include "llvm/MC/MCSectionWasm.h"
22 #include "llvm/MC/MCSymbolWasm.h"
23 #include "llvm/MC/MCValue.h"
24 #include "llvm/MC/MCWasmObjectWriter.h"
25 #include "llvm/Support/Casting.h"
26 #include "llvm/Support/ErrorHandling.h"
27 
28 using namespace llvm;
29 
30 namespace {
31 class WebAssemblyWasmObjectWriter final : public MCWasmObjectTargetWriter {
32 public:
33   explicit WebAssemblyWasmObjectWriter(bool Is64Bit, bool IsEmscripten);
34 
35 private:
36   unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup,
37                         const MCSectionWasm &FixupSection,
38                         bool IsLocRel) 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 *getTargetSection(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 = getTargetSection(BinOp->getLHS());
55     auto SectionRHS = getTargetSection(BinOp->getRHS());
56     return SectionLHS == SectionRHS ? nullptr : SectionLHS;
57   }
58 
59   if (auto UnOp = dyn_cast<MCUnaryExpr>(Expr))
60     return getTargetSection(UnOp->getSubExpr());
61 
62   return nullptr;
63 }
64 
65 unsigned WebAssemblyWasmObjectWriter::getRelocType(
66     const MCValue &Target, const MCFixup &Fixup,
67     const MCSectionWasm &FixupSection, bool IsLocRel) const {
68   const MCSymbolRefExpr *RefA = Target.getSymA();
69   assert(RefA);
70   auto& SymA = cast<MCSymbolWasm>(RefA->getSymbol());
71 
72   MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();
73 
74   switch (Modifier) {
75     case MCSymbolRefExpr::VK_GOT:
76     case MCSymbolRefExpr::VK_WASM_GOT_TLS:
77       return wasm::R_WASM_GLOBAL_INDEX_LEB;
78     case MCSymbolRefExpr::VK_WASM_TBREL:
79       assert(SymA.isFunction());
80       return is64Bit() ? wasm::R_WASM_TABLE_INDEX_REL_SLEB64
81                        : wasm::R_WASM_TABLE_INDEX_REL_SLEB;
82     case MCSymbolRefExpr::VK_WASM_TLSREL:
83       return is64Bit() ? wasm::R_WASM_MEMORY_ADDR_TLS_SLEB64
84                        : wasm::R_WASM_MEMORY_ADDR_TLS_SLEB;
85     case MCSymbolRefExpr::VK_WASM_MBREL:
86       assert(SymA.isData());
87       return is64Bit() ? wasm::R_WASM_MEMORY_ADDR_REL_SLEB64
88                        : wasm::R_WASM_MEMORY_ADDR_REL_SLEB;
89     case MCSymbolRefExpr::VK_WASM_TYPEINDEX:
90       return wasm::R_WASM_TYPE_INDEX_LEB;
91     case MCSymbolRefExpr::VK_None:
92       break;
93     case MCSymbolRefExpr::VK_WASM_FUNCINDEX:
94       return wasm::R_WASM_FUNCTION_INDEX_I32;
95     default:
96       report_fatal_error("unknown VariantKind");
97       break;
98   }
99 
100   switch (unsigned(Fixup.getKind())) {
101   case WebAssembly::fixup_sleb128_i32:
102     if (SymA.isFunction())
103       return wasm::R_WASM_TABLE_INDEX_SLEB;
104     return wasm::R_WASM_MEMORY_ADDR_SLEB;
105   case WebAssembly::fixup_sleb128_i64:
106     if (SymA.isFunction())
107       return wasm::R_WASM_TABLE_INDEX_SLEB64;
108     return wasm::R_WASM_MEMORY_ADDR_SLEB64;
109   case WebAssembly::fixup_uleb128_i32:
110     if (SymA.isGlobal())
111       return wasm::R_WASM_GLOBAL_INDEX_LEB;
112     if (SymA.isFunction())
113       return wasm::R_WASM_FUNCTION_INDEX_LEB;
114     if (SymA.isTag())
115       return wasm::R_WASM_TAG_INDEX_LEB;
116     if (SymA.isTable())
117       return wasm::R_WASM_TABLE_NUMBER_LEB;
118     return wasm::R_WASM_MEMORY_ADDR_LEB;
119   case WebAssembly::fixup_uleb128_i64:
120     assert(SymA.isData());
121     return wasm::R_WASM_MEMORY_ADDR_LEB64;
122   case FK_Data_4:
123     if (SymA.isFunction()) {
124       if (FixupSection.isMetadata())
125         return wasm::R_WASM_FUNCTION_OFFSET_I32;
126       assert(FixupSection.isWasmData());
127       return wasm::R_WASM_TABLE_INDEX_I32;
128     }
129     if (SymA.isGlobal())
130       return wasm::R_WASM_GLOBAL_INDEX_I32;
131     if (auto Section = static_cast<const MCSectionWasm *>(
132             getTargetSection(Fixup.getValue()))) {
133       if (Section->isText())
134         return wasm::R_WASM_FUNCTION_OFFSET_I32;
135       else if (!Section->isWasmData())
136         return wasm::R_WASM_SECTION_OFFSET_I32;
137     }
138     return IsLocRel ? wasm::R_WASM_MEMORY_ADDR_LOCREL_I32
139                     : wasm::R_WASM_MEMORY_ADDR_I32;
140   case FK_Data_8:
141     if (SymA.isFunction()) {
142       if (FixupSection.isMetadata())
143         return wasm::R_WASM_FUNCTION_OFFSET_I64;
144       return wasm::R_WASM_TABLE_INDEX_I64;
145     }
146     if (SymA.isGlobal())
147       llvm_unreachable("unimplemented R_WASM_GLOBAL_INDEX_I64");
148     if (auto Section = static_cast<const MCSectionWasm *>(
149             getTargetSection(Fixup.getValue()))) {
150       if (Section->isText())
151         return wasm::R_WASM_FUNCTION_OFFSET_I64;
152       else if (!Section->isWasmData())
153         llvm_unreachable("unimplemented R_WASM_SECTION_OFFSET_I64");
154     }
155     assert(SymA.isData());
156     return wasm::R_WASM_MEMORY_ADDR_I64;
157   default:
158     llvm_unreachable("unimplemented fixup kind");
159   }
160 }
161 
162 std::unique_ptr<MCObjectTargetWriter>
163 llvm::createWebAssemblyWasmObjectWriter(bool Is64Bit, bool IsEmscripten) {
164   return std::make_unique<WebAssemblyWasmObjectWriter>(Is64Bit, IsEmscripten);
165 }
166