LLVM 20.0.0git
WebAssemblyWasmObjectWriter.cpp
Go to the documentation of this file.
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
19#include "llvm/MC/MCFixup.h"
23#include "llvm/MC/MCValue.h"
27
28using namespace llvm;
29
30namespace {
31class WebAssemblyWasmObjectWriter final : public MCWasmObjectTargetWriter {
32public:
33 explicit WebAssemblyWasmObjectWriter(bool Is64Bit, bool IsEmscripten);
34
35private:
36 unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup,
37 const MCSectionWasm &FixupSection,
38 bool IsLocRel) const override;
39};
40} // end anonymous namespace
41
42WebAssemblyWasmObjectWriter::WebAssemblyWasmObjectWriter(bool Is64Bit,
43 bool IsEmscripten)
44 : MCWasmObjectTargetWriter(Is64Bit, IsEmscripten) {}
45
46static 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
65unsigned 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) {
77 return wasm::R_WASM_GLOBAL_INDEX_LEB;
79 assert(SymA.isFunction());
80 return is64Bit() ? wasm::R_WASM_TABLE_INDEX_REL_SLEB64
81 : wasm::R_WASM_TABLE_INDEX_REL_SLEB;
83 return is64Bit() ? wasm::R_WASM_MEMORY_ADDR_TLS_SLEB64
84 : wasm::R_WASM_MEMORY_ADDR_TLS_SLEB;
86 assert(SymA.isData());
87 return is64Bit() ? wasm::R_WASM_MEMORY_ADDR_REL_SLEB64
88 : wasm::R_WASM_MEMORY_ADDR_REL_SLEB;
90 return wasm::R_WASM_TYPE_INDEX_LEB;
92 break;
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())) {
102 if (SymA.isFunction())
103 return wasm::R_WASM_TABLE_INDEX_SLEB;
104 return wasm::R_WASM_MEMORY_ADDR_SLEB;
106 if (SymA.isFunction())
107 return wasm::R_WASM_TABLE_INDEX_SLEB64;
108 return wasm::R_WASM_MEMORY_ADDR_SLEB64;
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;
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
162std::unique_ptr<MCObjectTargetWriter>
163llvm::createWebAssemblyWasmObjectWriter(bool Is64Bit, bool IsEmscripten) {
164 return std::make_unique<WebAssemblyWasmObjectWriter>(Is64Bit, IsEmscripten);
165}
PowerPC TLS Dynamic Call Fixup
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file provides WebAssembly-specific target descriptions.
static const MCSection * getTargetSection(const MCExpr *Expr)
static bool is64Bit(const char *name)
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
This represents a section on wasm.
Definition: MCSectionWasm.h:26
bool isWasmData() const
Definition: MCSectionWasm.h:72
bool isMetadata() const
Definition: MCSectionWasm.h:73
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:36
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
const MCSymbol & getSymbol() const
Definition: MCExpr.h:411
This represents an "assembler immediate".
Definition: MCValue.h:36
virtual unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup, const MCSectionWasm &FixupSection, bool IsLocRel) const =0
Target - Wrapper for Target specific information.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::unique_ptr< MCObjectTargetWriter > createWebAssemblyWasmObjectWriter(bool Is64Bit, bool IsEmscripten)
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
@ FK_Data_8
A eight-byte fixup.
Definition: MCFixup.h:26
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:25