From 9df375e5eae726c5a90ada70f9535a5e22e90214 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 3 Jan 2025 11:53:21 +0900 Subject: [PATCH] [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 --- lld/test/wasm/dylink-non-pie.s | 38 ++++++++++++++++++++++++++++++++++ lld/wasm/Relocations.cpp | 2 +- 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100755 lld/test/wasm/dylink-non-pie.s diff --git a/lld/test/wasm/dylink-non-pie.s b/lld/test/wasm/dylink-non-pie.s new file mode 100755 index 000000000000..3157b8c32120 --- /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 745dfde76ab7..52888ad25034 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) {