-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
[lld][WebAssembly] Report unsupported PIC relocations as errors #104926
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-lld-wasm Author: Luc Blaeser (luc-blaeser) Changes
For more robust behavior, Full diff: https://github.com/llvm/llvm-project/pull/104926.diff 3 Files Affected:
diff --git a/lld/test/wasm/unsupported-pic-relocations.s b/lld/test/wasm/unsupported-pic-relocations.s
new file mode 100644
index 00000000000000..81b9fdde83e677
--- /dev/null
+++ b/lld/test/wasm/unsupported-pic-relocations.s
@@ -0,0 +1,47 @@
+# 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
+
+.globaltype __memory_base, i32, immutable
+.globaltype __table_base, i32, immutable
+
+.functype external_func () -> ()
+
+call_undefined_function:
+ .functype call_undefined_function () -> ()
+ global.get __table_base
+ i32.const external_func@TBREL
+ # CHECK: error: {{.*}}.o: relocation R_WASM_TABLE_INDEX_REL_SLEB is not supported against an undefined symbol `external_func`
+ i32.add
+ call_indirect () -> ()
+ end_function
+
+access_undefined_data:
+ .functype access_undefined_data () -> ()
+ global.get __memory_base
+ i32.const external_data@MBREL
+ # CHECK: error: {{.*}}.o: relocation R_WASM_MEMORY_ADDR_REL_SLEB is not supported against an undefined symbol `external_data`
+ i32.add
+ i32.load 0
+ drop
+ end_function
+
+.globl _start
+_start:
+ .functype _start () -> ()
+ call call_undefined_function
+ call access_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..cae78d95862b48
--- /dev/null
+++ b/lld/test/wasm/unsupported-pic-relocations64.s
@@ -0,0 +1,48 @@
+# 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
+
+.globaltype __memory_base, i64, immutable
+.globaltype __table_base, i64, immutable
+
+.functype external_func () -> ()
+
+call_undefined_function:
+ .functype call_undefined_function () -> ()
+ global.get __table_base
+ i64.const external_func@TBREL
+ # CHECK: error: {{.*}}.o: relocation R_WASM_TABLE_INDEX_REL_SLEB64 is not supported against an undefined symbol `external_func`
+ i64.add
+ i32.wrap_i64 # Remove when table64 is supported
+ call_indirect () -> ()
+ end_function
+
+access_undefined_data:
+ .functype access_undefined_data () -> ()
+ global.get __memory_base
+ i64.const external_data@MBREL
+ # CHECK: error: {{.*}}.o: relocation R_WASM_MEMORY_ADDR_REL_SLEB64 is not supported against an undefined symbol `external_data`
+ i64.add
+ i64.load 0
+ drop
+ end_function
+
+.globl _start
+_start:
+ .functype _start () -> ()
+ call call_undefined_function
+ call access_undefined_data
+ end_function
diff --git a/lld/wasm/Relocations.cpp b/lld/wasm/Relocations.cpp
index 6f33a4f28a9d09..6b177f4f34bb6f 100644
--- a/lld/wasm/Relocations.cpp
+++ b/lld/wasm/Relocations.cpp
@@ -170,6 +170,19 @@ void scanRelocations(InputChunk *chunk) {
if (requiresGOTAccess(sym))
addGOTEntry(sym);
break;
+ 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 only present in the code section and
+ // are not supported. It would require replacing the constant by using
+ // a GOT global.
+ if (sym->isUndefined())
+ error(toString(file) + ": relocation " +
+ relocTypeToString(reloc.Type) +
+ " is not supported against an undefined symbol `" +
+ toString(*sym) + "`");
+ break;
}
}
|
@llvm/pr-subscribers-lld Author: Luc Blaeser (luc-blaeser) Changes
For more robust behavior, Full diff: https://github.com/llvm/llvm-project/pull/104926.diff 3 Files Affected:
diff --git a/lld/test/wasm/unsupported-pic-relocations.s b/lld/test/wasm/unsupported-pic-relocations.s
new file mode 100644
index 00000000000000..81b9fdde83e677
--- /dev/null
+++ b/lld/test/wasm/unsupported-pic-relocations.s
@@ -0,0 +1,47 @@
+# 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
+
+.globaltype __memory_base, i32, immutable
+.globaltype __table_base, i32, immutable
+
+.functype external_func () -> ()
+
+call_undefined_function:
+ .functype call_undefined_function () -> ()
+ global.get __table_base
+ i32.const external_func@TBREL
+ # CHECK: error: {{.*}}.o: relocation R_WASM_TABLE_INDEX_REL_SLEB is not supported against an undefined symbol `external_func`
+ i32.add
+ call_indirect () -> ()
+ end_function
+
+access_undefined_data:
+ .functype access_undefined_data () -> ()
+ global.get __memory_base
+ i32.const external_data@MBREL
+ # CHECK: error: {{.*}}.o: relocation R_WASM_MEMORY_ADDR_REL_SLEB is not supported against an undefined symbol `external_data`
+ i32.add
+ i32.load 0
+ drop
+ end_function
+
+.globl _start
+_start:
+ .functype _start () -> ()
+ call call_undefined_function
+ call access_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..cae78d95862b48
--- /dev/null
+++ b/lld/test/wasm/unsupported-pic-relocations64.s
@@ -0,0 +1,48 @@
+# 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
+
+.globaltype __memory_base, i64, immutable
+.globaltype __table_base, i64, immutable
+
+.functype external_func () -> ()
+
+call_undefined_function:
+ .functype call_undefined_function () -> ()
+ global.get __table_base
+ i64.const external_func@TBREL
+ # CHECK: error: {{.*}}.o: relocation R_WASM_TABLE_INDEX_REL_SLEB64 is not supported against an undefined symbol `external_func`
+ i64.add
+ i32.wrap_i64 # Remove when table64 is supported
+ call_indirect () -> ()
+ end_function
+
+access_undefined_data:
+ .functype access_undefined_data () -> ()
+ global.get __memory_base
+ i64.const external_data@MBREL
+ # CHECK: error: {{.*}}.o: relocation R_WASM_MEMORY_ADDR_REL_SLEB64 is not supported against an undefined symbol `external_data`
+ i64.add
+ i64.load 0
+ drop
+ end_function
+
+.globl _start
+_start:
+ .functype _start () -> ()
+ call call_undefined_function
+ call access_undefined_data
+ end_function
diff --git a/lld/wasm/Relocations.cpp b/lld/wasm/Relocations.cpp
index 6f33a4f28a9d09..6b177f4f34bb6f 100644
--- a/lld/wasm/Relocations.cpp
+++ b/lld/wasm/Relocations.cpp
@@ -170,6 +170,19 @@ void scanRelocations(InputChunk *chunk) {
if (requiresGOTAccess(sym))
addGOTEntry(sym);
break;
+ 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 only present in the code section and
+ // are not supported. It would require replacing the constant by using
+ // a GOT global.
+ if (sym->isUndefined())
+ error(toString(file) + ": relocation " +
+ relocTypeToString(reloc.Type) +
+ " is not supported against an undefined symbol `" +
+ toString(*sym) + "`");
+ break;
}
}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm % some test cleanups. I think we can maybe make the tests much smaller.
Ping |
I don't see another revision after you responded to the comments. Did you forget to push it? |
Sorry, my mistake, I forgot to push. |
Thanks! Do need me to commit this? |
Thank you also for your time! Please feel free to merge when you think it is okay. I believe I have no right to merge. |
@luc-blaeser Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Thanks for the contribution! |
…#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`.
This seems to have broken the Emscripten benchmark test The command line is It looks like this command is trying to build a DSO but I also see it's using There are also warnings like which also seems maybe wrong. |
Interesting.. all those errors are about I think the issue here is that we probably don't want to be reporting these errors in |
Fix is in #109822 |
…/--relocatable Followup to llvm#104926. We ran into issues on the emscripten waterfall where relocation against `__dso_handle` were being reported as errors even though `-r/--relocatable` was being used to generate object file output rather than executable output.
WASM_MEMORY_ADDR_REL_
andWASM_TABLE_INDEX_REL_
relocations against undefined symbols are not supported and, except forUnresolvedPolicy::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 theUnresolvedPolicy
.