Skip to content

Commit 1e75b9b

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

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

llvm/lib/MC/WasmObjectWriter.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,12 @@ 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 =
538+
TargetObjectWriter->getRelocType(Target, Fixup, FixupSection, IsLocRel);
535539

536540
// Absolute offset within a section or a function.
537541
// Currently only supported for metadata sections.

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

Lines changed: 19 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,18 @@ class WebAssemblyAsmBackend final : public MCAsmBackend {
4850
const MCSubtargetInfo *STI) const override;
4951
};
5052

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

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

0 commit comments

Comments
 (0)