Skip to content

Commit acb3166

Browse files
Implement the .reloc directive for WASM
1 parent f44dcf6 commit acb3166

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

llvm/lib/MC/WasmObjectWriter.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,11 @@ void WasmObjectWriter::recordRelocation(const MCFragment &F,
530530
// be negative and don't wrap.
531531
FixedValue = 0;
532532

533-
unsigned Type =
534-
TargetObjectWriter->getRelocType(Target, Fixup, FixupSection, IsLocRel);
533+
unsigned Type;
534+
if (mc::isRelocRelocation(Fixup.getKind()))
535+
Type = Fixup.getKind() - FirstLiteralRelocationKind;
536+
else
537+
Type = TargetObjectWriter->getRelocType(Target, Fixup, FixupSection, IsLocRel);
535538

536539
// Absolute offset within a section or a function.
537540
// Currently only supported for metadata sections.

llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyAsmBackend.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "MCTargetDesc/WebAssemblyFixupKinds.h"
1515
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
16+
#include "llvm/ADT/StringSwitch.h"
1617
#include "llvm/MC/MCAsmBackend.h"
1718
#include "llvm/MC/MCAssembler.h"
1819
#include "llvm/MC/MCExpr.h"
@@ -36,6 +37,7 @@ class WebAssemblyAsmBackend final : public MCAsmBackend {
3637
: MCAsmBackend(llvm::endianness::little), Is64Bit(Is64Bit),
3738
IsEmscripten(IsEmscripten) {}
3839

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

4143
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
@@ -48,6 +50,17 @@ class WebAssemblyAsmBackend final : public MCAsmBackend {
4850
const MCSubtargetInfo *STI) const override;
4951
};
5052

53+
std::optional<MCFixupKind> WebAssemblyAsmBackend::getFixupKind(StringRef Name) const {
54+
unsigned Type = llvm::StringSwitch<unsigned>(Name)
55+
#define WASM_RELOC(NAME, ID) .Case(#NAME, ID)
56+
#include "llvm/BinaryFormat/WasmRelocs.def"
57+
#undef WASM_RELOC
58+
.Default(-1u);
59+
if (Type != -1u)
60+
return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
61+
return std::nullopt;
62+
}
63+
5164
MCFixupKindInfo
5265
WebAssemblyAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
5366
const static MCFixupKindInfo Infos[WebAssembly::NumTargetFixupKinds] = {
@@ -61,6 +74,11 @@ WebAssemblyAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
6174
{"fixup_uleb128_i64", 0, 10 * 8, 0},
6275
};
6376

77+
// Fixup kinds from raw relocation types and .reloc directives force
78+
// relocations and do not use these fields.
79+
if (mc::isRelocation(Kind))
80+
return MCAsmBackend::getFixupKindInfo(FK_NONE);
81+
6482
if (Kind < FirstTargetFixupKind)
6583
return MCAsmBackend::getFixupKindInfo(Kind);
6684

0 commit comments

Comments
 (0)