[lld] 9df375e - [lld][WebAssembly] Fix non-pie dynamic-linking executable (#108146)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 2 18:53:25 PST 2025
Author: YAMAMOTO Takashi
Date: 2025-01-02T18:53:21-08:00
New Revision: 9df375e5eae726c5a90ada70f9535a5e22e90214
URL: https://github.com/llvm/llvm-project/commit/9df375e5eae726c5a90ada70f9535a5e22e90214
DIFF: https://github.com/llvm/llvm-project/commit/9df375e5eae726c5a90ada70f9535a5e22e90214.diff
LOG: [lld][WebAssembly] Fix non-pie dynamic-linking executable (#108146)
The commit 22b7b84860d39da71964c9b329937f2ee1d875ba
made the symbols provided by shared libraries "defined",
and thus effectively made it impossible to generate non-pie
dynamically linked executables using
--unresolved-symbols=import-dynamic.
This commit, based on https://github.com/llvm/llvm-project/pull/109249,
fixes it by checking sym->isShared() explictly.
(as a bonus, you don't need to rely on
--unresolved-symbols=import-dynamic
anymore.)
Fixes https://github.com/llvm/llvm-project/issues/107387
Added:
lld/test/wasm/dylink-non-pie.s
Modified:
lld/wasm/Relocations.cpp
Removed:
################################################################################
diff --git a/lld/test/wasm/dylink-non-pie.s b/lld/test/wasm/dylink-non-pie.s
new file mode 100755
index 00000000000000..3157b8c32120f7
--- /dev/null
+++ b/lld/test/wasm/dylink-non-pie.s
@@ -0,0 +1,38 @@
+# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.lib.o %p/Inputs/ret32.s
+# RUN: wasm-ld -m wasm32 --experimental-pic -shared --no-entry %t.lib.o -o %t.lib.so
+# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %s
+# RUN: wasm-ld -m wasm32 -Bdynamic %t.o %t.lib.so -o %t.wasm
+# RUN: obj2yaml %t.wasm | FileCheck %s
+# RUN: llvm-objdump -d --no-show-raw-insn --no-leading-addr %t.wasm | FileCheck %s --check-prefixes DIS
+
+ .functype ret32 (f32) -> (i32)
+ .globl _start
+_start:
+ .functype _start () -> ()
+ i32.const f_p
+ drop
+ end_function
+
+ .section .data.f_p,"",@
+f_p:
+ .int32 ret32
+ .size f_p, 4
+
+# CHECK: Sections:
+# CHECK-NEXT: - Type: CUSTOM
+# CHECK-NEXT: Name: dylink.0
+
+# non-pie executable doesn't import __memory_base
+# CHECK: - Type: IMPORT
+# CHECK-NOT: Field: __memory_base
+
+# CHECK: - Type: EXPORT
+# CHECK: - Name: __wasm_apply_data_relocs
+# CHECK-NEXT: Kind: FUNCTION
+
+# DIS: <__wasm_apply_data_relocs>:
+# DIS-EMPTY:
+# DIS-NEXT: i32.const 1024
+# DIS-NEXT: global.get 0
+# DIS-NEXT: i32.store 0
+# DIS-NEXT: end
diff --git a/lld/wasm/Relocations.cpp b/lld/wasm/Relocations.cpp
index 745dfde76ab70e..52888ad25034e8 100644
--- a/lld/wasm/Relocations.cpp
+++ b/lld/wasm/Relocations.cpp
@@ -144,7 +144,7 @@ void scanRelocations(InputChunk *chunk) {
break;
}
- if (ctx.isPic ||
+ if (ctx.isPic || sym->isShared() ||
(sym->isUndefined() &&
ctx.arg.unresolvedSymbols == UnresolvedPolicy::ImportDynamic)) {
switch (reloc.Type) {
More information about the llvm-commits
mailing list