Skip to content

[lld][WebAssembly] Report unsupported PIC relocations as errors #104926

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lld/test/wasm/unsupported-pic-relocations.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %s

# RUN: not wasm-ld --experimental-pic -shared %t.o -o /dev/null 2>&1 | \
# RUN: FileCheck %s

# RUN: not wasm-ld --experimental-pic -shared %t.o -o /dev/null --unresolved-symbols=report-all 2>&1 | \
# RUN: FileCheck %s

# RUN: not wasm-ld --experimental-pic -shared %t.o -o /dev/null --warn-unresolved-symbols 2>&1 | \
# RUN: FileCheck %s

# RUN: not wasm-ld --experimental-pic -shared %t.o -o /dev/null --unresolved-symbols=ignore-all 2>&1 | \
# RUN: FileCheck %s

# RUN: not wasm-ld --experimental-pic -shared %t.o -o /dev/null --unresolved-symbols=import-dynamic 2>&1 | \
# RUN: FileCheck %s

.functype external_func () -> ()

use_undefined_function:
.functype use_undefined_function () -> ()
i32.const external_func@TBREL
# CHECK: error: {{.*}}.o: relocation R_WASM_TABLE_INDEX_REL_SLEB is not supported against an undefined symbol `external_func`
drop
end_function

use_undefined_data:
.functype use_undefined_data () -> ()
i32.const external_data@MBREL
# CHECK: error: {{.*}}.o: relocation R_WASM_MEMORY_ADDR_REL_SLEB is not supported against an undefined symbol `external_data`
drop
end_function

.globl _start
_start:
.functype _start () -> ()
call use_undefined_function
call use_undefined_data
end_function
39 changes: 39 additions & 0 deletions lld/test/wasm/unsupported-pic-relocations64.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# RUN: llvm-mc -filetype=obj -triple=wasm64-unknown-unknown -o %t.o %s

# RUN: not wasm-ld -mwasm64 --experimental-pic -shared %t.o -o /dev/null 2>&1 | \
# RUN: FileCheck %s

# RUN: not wasm-ld -mwasm64 --experimental-pic -shared %t.o -o /dev/null --unresolved-symbols=report-all 2>&1 | \
# RUN: FileCheck %s

# RUN: not wasm-ld -mwasm64 --experimental-pic -shared %t.o -o /dev/null --warn-unresolved-symbols 2>&1 | \
# RUN: FileCheck %s

# RUN: not wasm-ld -mwasm64 --experimental-pic -shared %t.o -o /dev/null --unresolved-symbols=ignore-all 2>&1 | \
# RUN: FileCheck %s

# RUN: not wasm-ld -mwasm64 --experimental-pic -shared %t.o -o /dev/null --unresolved-symbols=import-dynamic 2>&1 | \
# RUN: FileCheck %s

.functype external_func () -> ()

use_undefined_function:
.functype use_undefined_function () -> ()
i64.const external_func@TBREL
# CHECK: error: {{.*}}.o: relocation R_WASM_TABLE_INDEX_REL_SLEB64 is not supported against an undefined symbol `external_func`
drop
end_function

use_undefined_data:
.functype use_undefined_data () -> ()
i64.const external_data@MBREL
# CHECK: error: {{.*}}.o: relocation R_WASM_MEMORY_ADDR_REL_SLEB64 is not supported against an undefined symbol `external_data`
drop
end_function

.globl _start
_start:
.functype _start () -> ()
call use_undefined_function
call use_undefined_data
end_function
16 changes: 16 additions & 0 deletions lld/wasm/Relocations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,22 @@ void scanRelocations(InputChunk *chunk) {
}
}

if (sym->isUndefined()) {
switch (reloc.Type) {
case R_WASM_TABLE_INDEX_REL_SLEB:
case R_WASM_TABLE_INDEX_REL_SLEB64:
case R_WASM_MEMORY_ADDR_REL_SLEB:
case R_WASM_MEMORY_ADDR_REL_SLEB64:
// These relocation types are for symbols that exists relative to
// `__memory_base` or `__table_base` and as such only make sense for
// defined symbols.
error(toString(file) + ": relocation " + relocTypeToString(reloc.Type) +
" is not supported against an undefined symbol `" +
toString(*sym) + "`");
break;
}
}

if (sym->isUndefined() && !config->relocatable && !sym->isWeak()) {
// Report undefined symbols
reportUndefined(file, sym);
Expand Down
Loading