Skip to content

Commit 9df375e

Browse files
authored
[lld][WebAssembly] Fix non-pie dynamic-linking executable (#108146)
The commit 22b7b84 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 #109249, fixes it by checking sym->isShared() explictly. (as a bonus, you don't need to rely on --unresolved-symbols=import-dynamic anymore.) Fixes #107387
1 parent 510a5c7 commit 9df375e

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

lld/test/wasm/dylink-non-pie.s

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.lib.o %p/Inputs/ret32.s
2+
# RUN: wasm-ld -m wasm32 --experimental-pic -shared --no-entry %t.lib.o -o %t.lib.so
3+
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %s
4+
# RUN: wasm-ld -m wasm32 -Bdynamic %t.o %t.lib.so -o %t.wasm
5+
# RUN: obj2yaml %t.wasm | FileCheck %s
6+
# RUN: llvm-objdump -d --no-show-raw-insn --no-leading-addr %t.wasm | FileCheck %s --check-prefixes DIS
7+
8+
.functype ret32 (f32) -> (i32)
9+
.globl _start
10+
_start:
11+
.functype _start () -> ()
12+
i32.const f_p
13+
drop
14+
end_function
15+
16+
.section .data.f_p,"",@
17+
f_p:
18+
.int32 ret32
19+
.size f_p, 4
20+
21+
# CHECK: Sections:
22+
# CHECK-NEXT: - Type: CUSTOM
23+
# CHECK-NEXT: Name: dylink.0
24+
25+
# non-pie executable doesn't import __memory_base
26+
# CHECK: - Type: IMPORT
27+
# CHECK-NOT: Field: __memory_base
28+
29+
# CHECK: - Type: EXPORT
30+
# CHECK: - Name: __wasm_apply_data_relocs
31+
# CHECK-NEXT: Kind: FUNCTION
32+
33+
# DIS: <__wasm_apply_data_relocs>:
34+
# DIS-EMPTY:
35+
# DIS-NEXT: i32.const 1024
36+
# DIS-NEXT: global.get 0
37+
# DIS-NEXT: i32.store 0
38+
# DIS-NEXT: end

lld/wasm/Relocations.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ void scanRelocations(InputChunk *chunk) {
144144
break;
145145
}
146146

147-
if (ctx.isPic ||
147+
if (ctx.isPic || sym->isShared() ||
148148
(sym->isUndefined() &&
149149
ctx.arg.unresolvedSymbols == UnresolvedPolicy::ImportDynamic)) {
150150
switch (reloc.Type) {

0 commit comments

Comments
 (0)