[lld] 6ce1409 - [lld][WebAssembly] Report unsupported PIC relocations as errors (#104926)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 18 08:40:33 PDT 2024
Author: Luc Blaeser
Date: 2024-09-18T08:40:30-07:00
New Revision: 6ce14099ffa1194a5ed1f1ae6c35a4e811706fae
URL: https://github.com/llvm/llvm-project/commit/6ce14099ffa1194a5ed1f1ae6c35a4e811706fae
DIFF: https://github.com/llvm/llvm-project/commit/6ce14099ffa1194a5ed1f1ae6c35a4e811706fae.diff
LOG: [lld][WebAssembly] Report unsupported PIC relocations as errors (#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`.
Added:
lld/test/wasm/unsupported-pic-relocations.s
lld/test/wasm/unsupported-pic-relocations64.s
Modified:
lld/wasm/Relocations.cpp
Removed:
################################################################################
diff --git a/lld/test/wasm/unsupported-pic-relocations.s b/lld/test/wasm/unsupported-pic-relocations.s
new file mode 100644
index 00000000000000..ea32e8468cdb4d
--- /dev/null
+++ b/lld/test/wasm/unsupported-pic-relocations.s
@@ -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 at 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 at 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
diff --git a/lld/test/wasm/unsupported-pic-relocations64.s b/lld/test/wasm/unsupported-pic-relocations64.s
new file mode 100644
index 00000000000000..db9707b7fbac5e
--- /dev/null
+++ b/lld/test/wasm/unsupported-pic-relocations64.s
@@ -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 at 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 at 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
diff --git a/lld/wasm/Relocations.cpp b/lld/wasm/Relocations.cpp
index 6f33a4f28a9d09..2dbfe335494711 100644
--- a/lld/wasm/Relocations.cpp
+++ b/lld/wasm/Relocations.cpp
@@ -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);
More information about the llvm-commits
mailing list