Path: blob/main/contrib/llvm-project/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp
35293 views
//===-- WebAssemblyWasmObjectWriter.cpp - WebAssembly Wasm Writer ---------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7///8/// \file9/// This file handles Wasm-specific object emission, converting LLVM's10/// internal fixups into the appropriate relocations.11///12//===----------------------------------------------------------------------===//1314#include "MCTargetDesc/WebAssemblyFixupKinds.h"15#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"16#include "llvm/BinaryFormat/Wasm.h"17#include "llvm/MC/MCAsmBackend.h"18#include "llvm/MC/MCFixup.h"19#include "llvm/MC/MCFixupKindInfo.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"2728using namespace llvm;2930namespace {31class WebAssemblyWasmObjectWriter final : public MCWasmObjectTargetWriter {32public:33explicit WebAssemblyWasmObjectWriter(bool Is64Bit, bool IsEmscripten);3435private:36unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup,37const MCSectionWasm &FixupSection,38bool IsLocRel) const override;39};40} // end anonymous namespace4142WebAssemblyWasmObjectWriter::WebAssemblyWasmObjectWriter(bool Is64Bit,43bool IsEmscripten)44: MCWasmObjectTargetWriter(Is64Bit, IsEmscripten) {}4546static const MCSection *getTargetSection(const MCExpr *Expr) {47if (auto SyExp = dyn_cast<MCSymbolRefExpr>(Expr)) {48if (SyExp->getSymbol().isInSection())49return &SyExp->getSymbol().getSection();50return nullptr;51}5253if (auto BinOp = dyn_cast<MCBinaryExpr>(Expr)) {54auto SectionLHS = getTargetSection(BinOp->getLHS());55auto SectionRHS = getTargetSection(BinOp->getRHS());56return SectionLHS == SectionRHS ? nullptr : SectionLHS;57}5859if (auto UnOp = dyn_cast<MCUnaryExpr>(Expr))60return getTargetSection(UnOp->getSubExpr());6162return nullptr;63}6465unsigned WebAssemblyWasmObjectWriter::getRelocType(66const MCValue &Target, const MCFixup &Fixup,67const MCSectionWasm &FixupSection, bool IsLocRel) const {68const MCSymbolRefExpr *RefA = Target.getSymA();69assert(RefA);70auto& SymA = cast<MCSymbolWasm>(RefA->getSymbol());7172MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();7374switch (Modifier) {75case MCSymbolRefExpr::VK_GOT:76case MCSymbolRefExpr::VK_WASM_GOT_TLS:77return wasm::R_WASM_GLOBAL_INDEX_LEB;78case MCSymbolRefExpr::VK_WASM_TBREL:79assert(SymA.isFunction());80return is64Bit() ? wasm::R_WASM_TABLE_INDEX_REL_SLEB6481: wasm::R_WASM_TABLE_INDEX_REL_SLEB;82case MCSymbolRefExpr::VK_WASM_TLSREL:83return is64Bit() ? wasm::R_WASM_MEMORY_ADDR_TLS_SLEB6484: wasm::R_WASM_MEMORY_ADDR_TLS_SLEB;85case MCSymbolRefExpr::VK_WASM_MBREL:86assert(SymA.isData());87return is64Bit() ? wasm::R_WASM_MEMORY_ADDR_REL_SLEB6488: wasm::R_WASM_MEMORY_ADDR_REL_SLEB;89case MCSymbolRefExpr::VK_WASM_TYPEINDEX:90return wasm::R_WASM_TYPE_INDEX_LEB;91case MCSymbolRefExpr::VK_None:92break;93case MCSymbolRefExpr::VK_WASM_FUNCINDEX:94return wasm::R_WASM_FUNCTION_INDEX_I32;95default:96report_fatal_error("unknown VariantKind");97break;98}99100switch (unsigned(Fixup.getKind())) {101case WebAssembly::fixup_sleb128_i32:102if (SymA.isFunction())103return wasm::R_WASM_TABLE_INDEX_SLEB;104return wasm::R_WASM_MEMORY_ADDR_SLEB;105case WebAssembly::fixup_sleb128_i64:106if (SymA.isFunction())107return wasm::R_WASM_TABLE_INDEX_SLEB64;108return wasm::R_WASM_MEMORY_ADDR_SLEB64;109case WebAssembly::fixup_uleb128_i32:110if (SymA.isGlobal())111return wasm::R_WASM_GLOBAL_INDEX_LEB;112if (SymA.isFunction())113return wasm::R_WASM_FUNCTION_INDEX_LEB;114if (SymA.isTag())115return wasm::R_WASM_TAG_INDEX_LEB;116if (SymA.isTable())117return wasm::R_WASM_TABLE_NUMBER_LEB;118return wasm::R_WASM_MEMORY_ADDR_LEB;119case WebAssembly::fixup_uleb128_i64:120assert(SymA.isData());121return wasm::R_WASM_MEMORY_ADDR_LEB64;122case FK_Data_4:123if (SymA.isFunction()) {124if (FixupSection.isMetadata())125return wasm::R_WASM_FUNCTION_OFFSET_I32;126assert(FixupSection.isWasmData());127return wasm::R_WASM_TABLE_INDEX_I32;128}129if (SymA.isGlobal())130return wasm::R_WASM_GLOBAL_INDEX_I32;131if (auto Section = static_cast<const MCSectionWasm *>(132getTargetSection(Fixup.getValue()))) {133if (Section->isText())134return wasm::R_WASM_FUNCTION_OFFSET_I32;135else if (!Section->isWasmData())136return wasm::R_WASM_SECTION_OFFSET_I32;137}138return IsLocRel ? wasm::R_WASM_MEMORY_ADDR_LOCREL_I32139: wasm::R_WASM_MEMORY_ADDR_I32;140case FK_Data_8:141if (SymA.isFunction()) {142if (FixupSection.isMetadata())143return wasm::R_WASM_FUNCTION_OFFSET_I64;144return wasm::R_WASM_TABLE_INDEX_I64;145}146if (SymA.isGlobal())147llvm_unreachable("unimplemented R_WASM_GLOBAL_INDEX_I64");148if (auto Section = static_cast<const MCSectionWasm *>(149getTargetSection(Fixup.getValue()))) {150if (Section->isText())151return wasm::R_WASM_FUNCTION_OFFSET_I64;152else if (!Section->isWasmData())153llvm_unreachable("unimplemented R_WASM_SECTION_OFFSET_I64");154}155assert(SymA.isData());156return wasm::R_WASM_MEMORY_ADDR_I64;157default:158llvm_unreachable("unimplemented fixup kind");159}160}161162std::unique_ptr<MCObjectTargetWriter>163llvm::createWebAssemblyWasmObjectWriter(bool Is64Bit, bool IsEmscripten) {164return std::make_unique<WebAssemblyWasmObjectWriter>(Is64Bit, IsEmscripten);165}166167168