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