[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
This commit is contained in:
YAMAMOTO Takashi 2025-01-03 11:53:21 +09:00 committed by GitHub
parent 510a5c7fc2
commit 9df375e5ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 1 deletions

38
lld/test/wasm/dylink-non-pie.s Executable file
View File

@ -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

View File

@ -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) {