Skip to content

Commit 709980e

Browse files
authored
[lld][WebAssembly] Error on unexpected relocation types in -pie/-shared data sections (#162117)
Most likely we do want to support R_WASM_FUNCTION_INDEX_I32 at some point but this relocation types (along with many others) is not currently supported by `InputChunk::generateRelocationCode`. See #146923
1 parent 76e71e0 commit 709980e

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

lld/test/wasm/bad-data-relocs.s

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Certain relocations types are not supported by runtime relocation code
2+
## generated in `-shared/`-pie` binaries.
3+
4+
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %s
5+
# RUN: not wasm-ld -pie --experimental-pic %t.o -o %t.wasm 2>&1 | FileCheck %s
6+
7+
# CHECK: wasm-ld: error: invalid runtime relocation type in data section: R_WASM_FUNCTION_INDEX_I32
8+
9+
foo:
10+
.functype foo (i32) -> ()
11+
end_function
12+
13+
.globl _start
14+
_start:
15+
.functype _start () -> ()
16+
i32.const bar@GOT
17+
call foo@GOT
18+
end_function
19+
20+
# data section containing relocation type that is not valid in a data section
21+
.section .data,"",@
22+
.globl bar
23+
bar:
24+
.int32 0
25+
.size bar, 4
26+
27+
.reloc bar, R_WASM_FUNCTION_INDEX_I32, foo

lld/wasm/InputChunks.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,14 @@ uint64_t InputChunk::getVA(uint64_t offset) const {
406406
return (outputSeg ? outputSeg->startVA : 0) + getChunkOffset(offset);
407407
}
408408

409+
bool isValidRuntimeRelocation(WasmRelocType type) {
410+
// TODO(https://github.com/llvm/llvm-project/issues/146923): Currently
411+
// this means that R_WASM_FUNCTION_INDEX_I32 is not valid in `-pie` data
412+
// sections.
413+
return type == R_WASM_TABLE_INDEX_I32 || type == R_WASM_TABLE_INDEX_I64 ||
414+
type == R_WASM_MEMORY_ADDR_I32 || type == R_WASM_MEMORY_ADDR_I64;
415+
}
416+
409417
// Generate code to apply relocations to the data section at runtime.
410418
// This is only called when generating shared libraries (PIC) where address are
411419
// not known at static link time.
@@ -424,15 +432,18 @@ bool InputChunk::generateRelocationCode(raw_ostream &os) const {
424432
// TODO(sbc): Encode the relocations in the data section and write a loop
425433
// here to apply them.
426434
for (const WasmRelocation &rel : relocations) {
427-
uint64_t offset = getVA(rel.Offset) - getInputSectionOffset();
428-
429435
Symbol *sym = file->getSymbol(rel);
430436
// Runtime relocations are needed when we don't know the address of
431437
// a symbol statically.
432438
bool requiresRuntimeReloc = ctx.isPic || sym->hasGOTIndex();
433439
if (!requiresRuntimeReloc)
434440
continue;
435441

442+
if (!isValidRuntimeRelocation(rel.getType()))
443+
error("invalid runtime relocation type in data section: " +
444+
relocTypetoString(rel.Type));
445+
446+
uint64_t offset = getVA(rel.Offset) - getInputSectionOffset();
436447
LLVM_DEBUG(dbgs() << "gen reloc: type=" << relocTypeToString(rel.Type)
437448
<< " addend=" << rel.Addend << " index=" << rel.Index
438449
<< " output offset=" << offset << "\n");

0 commit comments

Comments
 (0)