Skip to content

Commit 4d17190

Browse files
luc-blaesertmsri
authored andcommitted
[lld][WebAssembly] Report unsupported PIC relocations as errors (llvm#104926)
`WASM_MEMORY_ADDR_REL_` and `WASM_TABLE_INDEX_REL_` relocations against **undefined symbols** are not supported and, except for `UnresolvedPolicy::ReportError`, lead to incorrect Wasm code, such as invalid data address or invalid table index that cannot be patched during later dynamic Wasm linking with modules declaring those symbols. This is different to other relocations that support undefined symbols by declaring correspond Wasm imports. For more robust behavior, `wasm-ld` should probably report an error for such unsupported PIC relocations, independent of the `UnresolvedPolicy`.
1 parent 31c55d9 commit 4d17190

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %s
2+
3+
# RUN: not wasm-ld --experimental-pic -shared %t.o -o /dev/null 2>&1 | \
4+
# RUN: FileCheck %s
5+
6+
# RUN: not wasm-ld --experimental-pic -shared %t.o -o /dev/null --unresolved-symbols=report-all 2>&1 | \
7+
# RUN: FileCheck %s
8+
9+
# RUN: not wasm-ld --experimental-pic -shared %t.o -o /dev/null --warn-unresolved-symbols 2>&1 | \
10+
# RUN: FileCheck %s
11+
12+
# RUN: not wasm-ld --experimental-pic -shared %t.o -o /dev/null --unresolved-symbols=ignore-all 2>&1 | \
13+
# RUN: FileCheck %s
14+
15+
# RUN: not wasm-ld --experimental-pic -shared %t.o -o /dev/null --unresolved-symbols=import-dynamic 2>&1 | \
16+
# RUN: FileCheck %s
17+
18+
.functype external_func () -> ()
19+
20+
use_undefined_function:
21+
.functype use_undefined_function () -> ()
22+
i32.const external_func@TBREL
23+
# CHECK: error: {{.*}}.o: relocation R_WASM_TABLE_INDEX_REL_SLEB is not supported against an undefined symbol `external_func`
24+
drop
25+
end_function
26+
27+
use_undefined_data:
28+
.functype use_undefined_data () -> ()
29+
i32.const external_data@MBREL
30+
# CHECK: error: {{.*}}.o: relocation R_WASM_MEMORY_ADDR_REL_SLEB is not supported against an undefined symbol `external_data`
31+
drop
32+
end_function
33+
34+
.globl _start
35+
_start:
36+
.functype _start () -> ()
37+
call use_undefined_function
38+
call use_undefined_data
39+
end_function
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# RUN: llvm-mc -filetype=obj -triple=wasm64-unknown-unknown -o %t.o %s
2+
3+
# RUN: not wasm-ld -mwasm64 --experimental-pic -shared %t.o -o /dev/null 2>&1 | \
4+
# RUN: FileCheck %s
5+
6+
# RUN: not wasm-ld -mwasm64 --experimental-pic -shared %t.o -o /dev/null --unresolved-symbols=report-all 2>&1 | \
7+
# RUN: FileCheck %s
8+
9+
# RUN: not wasm-ld -mwasm64 --experimental-pic -shared %t.o -o /dev/null --warn-unresolved-symbols 2>&1 | \
10+
# RUN: FileCheck %s
11+
12+
# RUN: not wasm-ld -mwasm64 --experimental-pic -shared %t.o -o /dev/null --unresolved-symbols=ignore-all 2>&1 | \
13+
# RUN: FileCheck %s
14+
15+
# RUN: not wasm-ld -mwasm64 --experimental-pic -shared %t.o -o /dev/null --unresolved-symbols=import-dynamic 2>&1 | \
16+
# RUN: FileCheck %s
17+
18+
.functype external_func () -> ()
19+
20+
use_undefined_function:
21+
.functype use_undefined_function () -> ()
22+
i64.const external_func@TBREL
23+
# CHECK: error: {{.*}}.o: relocation R_WASM_TABLE_INDEX_REL_SLEB64 is not supported against an undefined symbol `external_func`
24+
drop
25+
end_function
26+
27+
use_undefined_data:
28+
.functype use_undefined_data () -> ()
29+
i64.const external_data@MBREL
30+
# CHECK: error: {{.*}}.o: relocation R_WASM_MEMORY_ADDR_REL_SLEB64 is not supported against an undefined symbol `external_data`
31+
drop
32+
end_function
33+
34+
.globl _start
35+
_start:
36+
.functype _start () -> ()
37+
call use_undefined_function
38+
call use_undefined_data
39+
end_function

lld/wasm/Relocations.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,22 @@ void scanRelocations(InputChunk *chunk) {
173173
}
174174
}
175175

176+
if (sym->isUndefined()) {
177+
switch (reloc.Type) {
178+
case R_WASM_TABLE_INDEX_REL_SLEB:
179+
case R_WASM_TABLE_INDEX_REL_SLEB64:
180+
case R_WASM_MEMORY_ADDR_REL_SLEB:
181+
case R_WASM_MEMORY_ADDR_REL_SLEB64:
182+
// These relocation types are for symbols that exists relative to
183+
// `__memory_base` or `__table_base` and as such only make sense for
184+
// defined symbols.
185+
error(toString(file) + ": relocation " + relocTypeToString(reloc.Type) +
186+
" is not supported against an undefined symbol `" +
187+
toString(*sym) + "`");
188+
break;
189+
}
190+
}
191+
176192
if (sym->isUndefined() && !config->relocatable && !sym->isWeak()) {
177193
// Report undefined symbols
178194
reportUndefined(file, sym);

0 commit comments

Comments
 (0)