Skip to content

[WebAssembly] Implement the .reloc directive for WASM #146952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions llvm/lib/MC/WasmObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,12 @@ void WasmObjectWriter::recordRelocation(const MCFragment &F,
// be negative and don't wrap.
FixedValue = 0;

unsigned Type =
TargetObjectWriter->getRelocType(Target, Fixup, FixupSection, IsLocRel);
unsigned Type;
if (mc::isRelocRelocation(Fixup.getKind()))
Type = Fixup.getKind() - FirstLiteralRelocationKind;
else
Type =
TargetObjectWriter->getRelocType(Target, Fixup, FixupSection, IsLocRel);

// Absolute offset within a section or a function.
// Currently only supported for metadata sections.
Expand Down
19 changes: 19 additions & 0 deletions llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "MCTargetDesc/WebAssemblyFixupKinds.h"
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/MC/MCAsmBackend.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCExpr.h"
Expand All @@ -36,6 +37,7 @@ class WebAssemblyAsmBackend final : public MCAsmBackend {
: MCAsmBackend(llvm::endianness::little), Is64Bit(Is64Bit),
IsEmscripten(IsEmscripten) {}

std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;

void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
Expand All @@ -48,6 +50,18 @@ class WebAssemblyAsmBackend final : public MCAsmBackend {
const MCSubtargetInfo *STI) const override;
};

std::optional<MCFixupKind>
WebAssemblyAsmBackend::getFixupKind(StringRef Name) const {
unsigned Type = llvm::StringSwitch<unsigned>(Name)
#define WASM_RELOC(NAME, ID) .Case(#NAME, ID)
#include "llvm/BinaryFormat/WasmRelocs.def"
#undef WASM_RELOC
.Default(-1u);
if (Type != -1u)
return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
return std::nullopt;
}

MCFixupKindInfo
WebAssemblyAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
const static MCFixupKindInfo Infos[WebAssembly::NumTargetFixupKinds] = {
Expand All @@ -61,6 +75,11 @@ WebAssemblyAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
{"fixup_uleb128_i64", 0, 10 * 8, 0},
};

// Fixup kinds from raw relocation types and .reloc directives force
// relocations and do not use these fields.
if (mc::isRelocation(Kind))
return MCAsmBackend::getFixupKindInfo(FK_NONE);

if (Kind < FirstTargetFixupKind)
return MCAsmBackend::getFixupKindInfo(Kind);

Expand Down
29 changes: 29 additions & 0 deletions llvm/test/MC/WebAssembly/reloc-directive.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# RUN: llvm-mc -triple=wasm32 %s | FileCheck --check-prefix=PRINT %s
# RUN: llvm-mc -filetype=obj -triple=wasm32 %s | llvm-readobj -r - | FileCheck %s

load_function_index_func:
.functype load_function_index_func () -> (i32)
i32.const 0
nop
nop
nop
nop
i32.load 0
end_function

# PRINT: .reloc load_function_index_func+2, R_WASM_MEMORY_ADDR_SLEB, function_index_data+1
# CHECK: Section ({{.*}}) CODE {
# CHECK-NEXT: 0x4 R_WASM_MEMORY_ADDR_SLEB function_index_data 1
# CHECK-NEXT: }
.reloc load_function_index_func + 2, R_WASM_MEMORY_ADDR_SLEB, function_index_data + 1

.section .data,"",@
function_index_data:
.int32 0
.size function_index_data, 4

# PRINT: .reloc function_index_data, R_WASM_FUNCTION_INDEX_I32, load_function_index_func
# CHECK: Section ({{.*}}) DATA {
# CHECK-NEXT: 0x6 R_WASM_FUNCTION_INDEX_I32 load_function_index_func
# CHECK-NEXT: }
.reloc function_index_data, R_WASM_FUNCTION_INDEX_I32, load_function_index_func
Loading