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, const MCFixup &Fixup,
38                         const MCSectionWasm &FixupSection,
39                         bool IsLocRel) const override;
40 };
41 } // end anonymous namespace
42 
43 WebAssemblyWasmObjectWriter::WebAssemblyWasmObjectWriter(bool Is64Bit,
44                                                          bool IsEmscripten)
45     : MCWasmObjectTargetWriter(Is64Bit, IsEmscripten) {}
46 
47 static const MCSection *getTargetSection(const MCExpr *Expr) {
48   if (auto SyExp = dyn_cast<MCSymbolRefExpr>(Expr)) {
49     if (SyExp->getSymbol().isInSection())
50       return &SyExp->getSymbol().getSection();
51     return nullptr;
52   }
53 
54   if (auto BinOp = dyn_cast<MCBinaryExpr>(Expr)) {
55     auto SectionLHS = getTargetSection(BinOp->getLHS());
56     auto SectionRHS = getTargetSection(BinOp->getRHS());
57     return SectionLHS == SectionRHS ? nullptr : SectionLHS;
58   }
59 
60   if (auto UnOp = dyn_cast<MCUnaryExpr>(Expr))
61     return getTargetSection(UnOp->getSubExpr());
62 
63   return nullptr;
64 }
65 
66 unsigned WebAssemblyWasmObjectWriter::getRelocType(
67     const MCValue &Target, const MCFixup &Fixup,
68     const MCSectionWasm &FixupSection, bool IsLocRel) const {
69   const MCSymbolRefExpr *RefA = Target.getSymA();
70   assert(RefA);
71   auto& SymA = cast<MCSymbolWasm>(RefA->getSymbol());
72 
73   MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();
74 
75   switch (Modifier) {
76     case MCSymbolRefExpr::VK_GOT:
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     default:
92       break;
93   }
94 
95   switch (unsigned(Fixup.getKind())) {
96   case WebAssembly::fixup_sleb128_i32:
97     if (SymA.isFunction())
98       return wasm::R_WASM_TABLE_INDEX_SLEB;
99     return wasm::R_WASM_MEMORY_ADDR_SLEB;
100   case WebAssembly::fixup_sleb128_i64:
101     if (SymA.isFunction())
102       return wasm::R_WASM_TABLE_INDEX_SLEB64;
103     return wasm::R_WASM_MEMORY_ADDR_SLEB64;
104   case WebAssembly::fixup_uleb128_i32:
105     if (SymA.isGlobal())
106       return wasm::R_WASM_GLOBAL_INDEX_LEB;
107     if (SymA.isFunction())
108       return wasm::R_WASM_FUNCTION_INDEX_LEB;
109     if (SymA.isTag())
110       return wasm::R_WASM_TAG_INDEX_LEB;
111     if (SymA.isTable())
112       return wasm::R_WASM_TABLE_NUMBER_LEB;
113     return wasm::R_WASM_MEMORY_ADDR_LEB;
114   case WebAssembly::fixup_uleb128_i64:
115     assert(SymA.isData());
116     return wasm::R_WASM_MEMORY_ADDR_LEB64;
117   case FK_Data_4:
118     if (SymA.isFunction()) {
119       if (FixupSection.getKind().isMetadata())
120         return wasm::R_WASM_FUNCTION_OFFSET_I32;
121       assert(FixupSection.isWasmData());
122       return wasm::R_WASM_TABLE_INDEX_I32;
123     }
124     if (SymA.isGlobal())
125       return wasm::R_WASM_GLOBAL_INDEX_I32;
126     if (auto Section = static_cast<const MCSectionWasm *>(
127             getTargetSection(Fixup.getValue()))) {
128       if (Section->getKind().isText())
129         return wasm::R_WASM_FUNCTION_OFFSET_I32;
130       else if (!Section->isWasmData())
131         return wasm::R_WASM_SECTION_OFFSET_I32;
132     }
133     return IsLocRel ? wasm::R_WASM_MEMORY_ADDR_LOCREL_I32
134                     : wasm::R_WASM_MEMORY_ADDR_I32;
135   case FK_Data_8:
136     if (SymA.isFunction()) {
137       if (FixupSection.getKind().isMetadata())
138         return wasm::R_WASM_FUNCTION_OFFSET_I64;
139       return wasm::R_WASM_TABLE_INDEX_I64;
140     }
141     if (SymA.isGlobal())
142       llvm_unreachable("unimplemented R_WASM_GLOBAL_INDEX_I64");
143     if (auto Section = static_cast<const MCSectionWasm *>(
144             getTargetSection(Fixup.getValue()))) {
145       if (Section->getKind().isText())
146         return wasm::R_WASM_FUNCTION_OFFSET_I64;
147       else if (!Section->isWasmData())
148         llvm_unreachable("unimplemented R_WASM_SECTION_OFFSET_I64");
149     }
150     assert(SymA.isData());
151     return wasm::R_WASM_MEMORY_ADDR_I64;
152   default:
153     llvm_unreachable("unimplemented fixup kind");
154   }
155 }
156 
157 std::unique_ptr<MCObjectTargetWriter>
158 llvm::createWebAssemblyWasmObjectWriter(bool Is64Bit, bool IsEmscripten) {
159   return std::make_unique<WebAssemblyWasmObjectWriter>(Is64Bit, IsEmscripten);
160 }
161