Skip to content

Commit 16f0243

Browse files
committed
[WebAssembly] Added R_WASM_FUNCTION_OFFSET_I64 for use with DWARF DW_AT_low_pc
Needed for wasm64, see discussion in https://reviews.llvm.org/D91203 Differential Revision: https://reviews.llvm.org/D91395
1 parent e11195d commit 16f0243

File tree

12 files changed

+330
-28
lines changed

12 files changed

+330
-28
lines changed

lld/wasm/InputChunks.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ void InputChunk::verifyRelocTargets() const {
9797
break;
9898
case R_WASM_TABLE_INDEX_I64:
9999
case R_WASM_MEMORY_ADDR_I64:
100+
case R_WASM_FUNCTION_OFFSET_I64:
100101
existingValue = read64le(loc);
101102
break;
102103
default:
@@ -176,6 +177,7 @@ void InputChunk::writeTo(uint8_t *buf) const {
176177
break;
177178
case R_WASM_TABLE_INDEX_I64:
178179
case R_WASM_MEMORY_ADDR_I64:
180+
case R_WASM_FUNCTION_OFFSET_I64:
179181
write64le(loc, value);
180182
break;
181183
default:

lld/wasm/InputFiles.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ uint64_t ObjFile::calcNewAddend(const WasmRelocation &reloc) const {
124124
case R_WASM_MEMORY_ADDR_I32:
125125
case R_WASM_MEMORY_ADDR_I64:
126126
case R_WASM_FUNCTION_OFFSET_I32:
127+
case R_WASM_FUNCTION_OFFSET_I64:
127128
return reloc.Addend;
128129
case R_WASM_SECTION_OFFSET_I32:
129130
return getSectionSymbol(reloc.Index)->section->outputOffset + reloc.Addend;
@@ -171,7 +172,8 @@ uint64_t ObjFile::calcExpectedValue(const WasmRelocation &reloc) const {
171172
else
172173
llvm_unreachable("unknown init expr opcode");
173174
}
174-
case R_WASM_FUNCTION_OFFSET_I32: {
175+
case R_WASM_FUNCTION_OFFSET_I32:
176+
case R_WASM_FUNCTION_OFFSET_I64: {
175177
const WasmSymbol &sym = wasmObj->syms()[reloc.Index];
176178
InputFunction *f =
177179
functions[sym.Info.ElementIndex - wasmObj->getNumImportedFunctions()];
@@ -258,7 +260,8 @@ uint64_t ObjFile::calcNewValue(const WasmRelocation &reloc) const {
258260
return sym->getGOTIndex();
259261
case R_WASM_EVENT_INDEX_LEB:
260262
return getEventSymbol(reloc.Index)->getEventIndex();
261-
case R_WASM_FUNCTION_OFFSET_I32: {
263+
case R_WASM_FUNCTION_OFFSET_I32:
264+
case R_WASM_FUNCTION_OFFSET_I64: {
262265
auto *f = cast<DefinedFunction>(sym);
263266
return f->function->outputOffset +
264267
(f->function->getFunctionCodeOffset() + reloc.Addend);

llvm/include/llvm/BinaryFormat/WasmRelocs.def

+1
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ WASM_RELOC(R_WASM_TABLE_INDEX_SLEB64, 18)
2424
WASM_RELOC(R_WASM_TABLE_INDEX_I64, 19)
2525
WASM_RELOC(R_WASM_TABLE_NUMBER_LEB, 20)
2626
WASM_RELOC(R_WASM_MEMORY_ADDR_TLS_SLEB, 21)
27+
WASM_RELOC(R_WASM_FUNCTION_OFFSET_I64, 22)

llvm/lib/BinaryFormat/Wasm.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ bool llvm::wasm::relocTypeHasAddend(uint32_t Type) {
5050
case R_WASM_MEMORY_ADDR_I64:
5151
case R_WASM_MEMORY_ADDR_TLS_SLEB:
5252
case R_WASM_FUNCTION_OFFSET_I32:
53+
case R_WASM_FUNCTION_OFFSET_I64:
5354
case R_WASM_SECTION_OFFSET_I32:
5455
return true;
5556
default:

llvm/lib/MC/WasmObjectWriter.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
478478
// Currently only supported for for metadata sections.
479479
// See: test/MC/WebAssembly/blockaddress.ll
480480
if (Type == wasm::R_WASM_FUNCTION_OFFSET_I32 ||
481+
Type == wasm::R_WASM_FUNCTION_OFFSET_I64 ||
481482
Type == wasm::R_WASM_SECTION_OFFSET_I32) {
482483
if (!FixupSection.getKind().isMetadata())
483484
report_fatal_error("relocations for function or section offsets are "
@@ -564,6 +565,7 @@ WasmObjectWriter::getProvisionalValue(const WasmRelocationEntry &RelEntry,
564565
assert(WasmIndices.count(RelEntry.Symbol) > 0 && "symbol not found in wasm index space");
565566
return WasmIndices[RelEntry.Symbol];
566567
case wasm::R_WASM_FUNCTION_OFFSET_I32:
568+
case wasm::R_WASM_FUNCTION_OFFSET_I64:
567569
case wasm::R_WASM_SECTION_OFFSET_I32: {
568570
const auto &Section =
569571
static_cast<const MCSectionWasm &>(RelEntry.Symbol->getSection());
@@ -680,6 +682,7 @@ void WasmObjectWriter::applyRelocations(
680682
break;
681683
case wasm::R_WASM_TABLE_INDEX_I64:
682684
case wasm::R_WASM_MEMORY_ADDR_I64:
685+
case wasm::R_WASM_FUNCTION_OFFSET_I64:
683686
patchI64(Stream, Value, Offset);
684687
break;
685688
case wasm::R_WASM_TABLE_INDEX_SLEB:

llvm/lib/Object/RelocationResolver.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ static bool supportsWasm64(uint64_t Type) {
566566
case wasm::R_WASM_MEMORY_ADDR_I64:
567567
case wasm::R_WASM_TABLE_INDEX_SLEB64:
568568
case wasm::R_WASM_TABLE_INDEX_I64:
569+
case wasm::R_WASM_FUNCTION_OFFSET_I64:
569570
return true;
570571
default:
571572
return supportsWasm32(Type);
@@ -601,6 +602,7 @@ static uint64_t resolveWasm64(RelocationRef R, uint64_t S, uint64_t A) {
601602
case wasm::R_WASM_MEMORY_ADDR_I64:
602603
case wasm::R_WASM_TABLE_INDEX_SLEB64:
603604
case wasm::R_WASM_TABLE_INDEX_I64:
605+
case wasm::R_WASM_FUNCTION_OFFSET_I64:
604606
// For wasm section, its offset at 0 -- ignoring Value
605607
return A;
606608
default:

llvm/lib/Object/WasmObjectFile.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,12 @@ Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) {
876876
object_error::parse_failed);
877877
Reloc.Addend = readVarint32(Ctx);
878878
break;
879+
case wasm::R_WASM_FUNCTION_OFFSET_I64:
880+
if (!isValidFunctionSymbol(Reloc.Index))
881+
return make_error<GenericBinaryError>("Bad relocation function index",
882+
object_error::parse_failed);
883+
Reloc.Addend = readVarint64(Ctx);
884+
break;
879885
case wasm::R_WASM_SECTION_OFFSET_I32:
880886
if (!isValidSectionSymbol(Reloc.Index))
881887
return make_error<GenericBinaryError>("Bad relocation section index",
@@ -903,7 +909,8 @@ Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) {
903909
Reloc.Type == wasm::R_WASM_GLOBAL_INDEX_I32)
904910
Size = 4;
905911
if (Reloc.Type == wasm::R_WASM_TABLE_INDEX_I64 ||
906-
Reloc.Type == wasm::R_WASM_MEMORY_ADDR_I64)
912+
Reloc.Type == wasm::R_WASM_MEMORY_ADDR_I64 ||
913+
Reloc.Type == wasm::R_WASM_FUNCTION_OFFSET_I64)
907914
Size = 8;
908915
if (Reloc.Offset + Size > EndOffset)
909916
return make_error<GenericBinaryError>("Bad relocation offset",

llvm/lib/ObjectYAML/WasmEmitter.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ void WasmWriter::writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec,
547547
case wasm::R_WASM_MEMORY_ADDR_I32:
548548
case wasm::R_WASM_MEMORY_ADDR_I64:
549549
case wasm::R_WASM_FUNCTION_OFFSET_I32:
550+
case wasm::R_WASM_FUNCTION_OFFSET_I64:
550551
case wasm::R_WASM_SECTION_OFFSET_I32:
551552
encodeULEB128(Reloc.Addend, OS);
552553
}

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ WebAssemblyMCAsmInfo::~WebAssemblyMCAsmInfo() = default; // anchor.
2323

2424
WebAssemblyMCAsmInfo::WebAssemblyMCAsmInfo(const Triple &T,
2525
const MCTargetOptions &Options) {
26-
CalleeSaveStackSlotSize = T.isArch64Bit() ? 8 : 4;
27-
// So far this is used for DWARF DW_AT_low_pc which is always 32-bit in Wasm.
28-
CodePointerSize = 4;
26+
CodePointerSize = CalleeSaveStackSlotSize = T.isArch64Bit() ? 8 : 4;
2927

3028
// TODO: What should MaxInstLength be?
3129

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ unsigned WebAssemblyWasmObjectWriter::getRelocType(const MCValue &Target,
131131
if (auto Section = static_cast<const MCSectionWasm *>(
132132
getFixupSection(Fixup.getValue()))) {
133133
if (Section->getKind().isText())
134-
llvm_unreachable("unimplemented R_WASM_FUNCTION_OFFSET_I64");
134+
return wasm::R_WASM_FUNCTION_OFFSET_I64;
135135
else if (!Section->isWasmData())
136136
llvm_unreachable("unimplemented R_WASM_SECTION_OFFSET_I64");
137137
}

0 commit comments

Comments
 (0)