[lld] [lld][WebAssembly] Fix non-pie dynamic-linking executable (PR #108146)
YAMAMOTO Takashi via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 31 20:10:37 PDT 2024
https://github.com/yamt updated https://github.com/llvm/llvm-project/pull/108146
>From 82a12e6dc052221019e9bf01227d754d340a1874 Mon Sep 17 00:00:00 2001
From: YAMAMOTO Takashi <yamamoto at midokura.com>
Date: Thu, 19 Sep 2024 15:05:05 +0900
Subject: [PATCH 1/8] [lld][WebAssembly] Fix non-pie dynamic-linking executable
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/wasm/Relocations.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/lld/wasm/Relocations.cpp b/lld/wasm/Relocations.cpp
index 45ad32701616a1..4beb1e6caafc86 100644
--- a/lld/wasm/Relocations.cpp
+++ b/lld/wasm/Relocations.cpp
@@ -146,7 +146,8 @@ void scanRelocations(InputChunk *chunk) {
if (ctx.isPic ||
(sym->isUndefined() &&
- config->unresolvedSymbols == UnresolvedPolicy::ImportDynamic)) {
+ config->unresolvedSymbols == UnresolvedPolicy::ImportDynamic) ||
+ sym->isShared()) {
switch (reloc.Type) {
case R_WASM_TABLE_INDEX_SLEB:
case R_WASM_TABLE_INDEX_SLEB64:
>From 06482d7c3799c86d142e6a03f1a1563f1530e358 Mon Sep 17 00:00:00 2001
From: YAMAMOTO Takashi <yamamoto at midokura.com>
Date: Tue, 1 Oct 2024 14:14:59 +0900
Subject: [PATCH 2/8] add a test
---
lld/test/wasm/Inputs/lib.s | 6 ++++++
lld/test/wasm/dylink-non-pie.s | 35 ++++++++++++++++++++++++++++++++++
2 files changed, 41 insertions(+)
create mode 100644 lld/test/wasm/Inputs/lib.s
create mode 100755 lld/test/wasm/dylink-non-pie.s
diff --git a/lld/test/wasm/Inputs/lib.s b/lld/test/wasm/Inputs/lib.s
new file mode 100644
index 00000000000000..c5e561e7e893dd
--- /dev/null
+++ b/lld/test/wasm/Inputs/lib.s
@@ -0,0 +1,6 @@
+ .functype f () -> ()
+ .globl f
+ .type f, at function
+f:
+ .functype f () -> ()
+ end_function
diff --git a/lld/test/wasm/dylink-non-pie.s b/lld/test/wasm/dylink-non-pie.s
new file mode 100755
index 00000000000000..17c04ffa59b7ba
--- /dev/null
+++ b/lld/test/wasm/dylink-non-pie.s
@@ -0,0 +1,35 @@
+# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.lib.o %p/Inputs/lib.s
+# RUN: wasm-ld -m wasm32 --experimental-pic -shared --no-entry %t.lib.o -o %t.lib.so
+# RUN: llvm-mc -filetype=obj -mattr=+reference-types -triple=wasm32-unknown-unknown -o %t.o %s
+# RUN: wasm-ld -m wasm32 --features=mutable-globals -Bdynamic --export-table --growable-table --export-memory --export=__stack_pointer --export=__heap_base --export=__heap_end --entry=_start %t.o %t.lib.so -o %t.wasm
+# RUN: obj2yaml %t.wasm | FileCheck %s
+
+ .tabletype __indirect_function_table, funcref
+ .globaltype __memory_base, i32, immutable
+
+ .functype f () -> ()
+ .functype _start () -> ()
+ .globl _start
+ .type _start, at function
+_start:
+ .functype _start () -> ()
+ global.get __memory_base
+ i32.const f_p at MBREL
+ i32.add
+ i32.load 0
+ call_indirect __indirect_function_table, () -> ()
+ end_function
+
+ .section .data.f_p,"",@
+ .globl f_p
+f_p:
+ .int32 f
+ .size f_p, 4
+
+# CHECK: Sections:
+# CHECK-NEXT: - Type: CUSTOM
+# CHECK-NEXT: Name: dylink.0
+
+# CHECK: - Type: EXPORT
+# CHECK: - Name: __wasm_apply_data_relocs
+# CHECK-NEXT: Kind: FUNCTION
>From fb9a6abb766f92abdd16c8f4ad270b8f6b546171 Mon Sep 17 00:00:00 2001
From: YAMAMOTO Takashi <yamamoto at midokura.com>
Date: Tue, 1 Oct 2024 17:13:40 +0900
Subject: [PATCH 3/8] dylink-non-pie.s: use the existing file
---
lld/test/wasm/Inputs/lib.s | 6 ------
lld/test/wasm/dylink-non-pie.s | 10 ++++++----
2 files changed, 6 insertions(+), 10 deletions(-)
delete mode 100644 lld/test/wasm/Inputs/lib.s
diff --git a/lld/test/wasm/Inputs/lib.s b/lld/test/wasm/Inputs/lib.s
deleted file mode 100644
index c5e561e7e893dd..00000000000000
--- a/lld/test/wasm/Inputs/lib.s
+++ /dev/null
@@ -1,6 +0,0 @@
- .functype f () -> ()
- .globl f
- .type f, at function
-f:
- .functype f () -> ()
- end_function
diff --git a/lld/test/wasm/dylink-non-pie.s b/lld/test/wasm/dylink-non-pie.s
index 17c04ffa59b7ba..a2da383e5766c7 100755
--- a/lld/test/wasm/dylink-non-pie.s
+++ b/lld/test/wasm/dylink-non-pie.s
@@ -1,4 +1,4 @@
-# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.lib.o %p/Inputs/lib.s
+# 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 -mattr=+reference-types -triple=wasm32-unknown-unknown -o %t.o %s
# RUN: wasm-ld -m wasm32 --features=mutable-globals -Bdynamic --export-table --growable-table --export-memory --export=__stack_pointer --export=__heap_base --export=__heap_end --entry=_start %t.o %t.lib.so -o %t.wasm
@@ -7,23 +7,25 @@
.tabletype __indirect_function_table, funcref
.globaltype __memory_base, i32, immutable
- .functype f () -> ()
+ .functype ret32 (f32) -> (i32)
.functype _start () -> ()
.globl _start
.type _start, at function
_start:
.functype _start () -> ()
+ f32.const 0.0
global.get __memory_base
i32.const f_p at MBREL
i32.add
i32.load 0
- call_indirect __indirect_function_table, () -> ()
+ call_indirect __indirect_function_table, (f32) -> (i32)
+ drop
end_function
.section .data.f_p,"",@
.globl f_p
f_p:
- .int32 f
+ .int32 ret32
.size f_p, 4
# CHECK: Sections:
>From 9e234c1184c236507e94ac3e599028782deee7f9 Mon Sep 17 00:00:00 2001
From: YAMAMOTO Takashi <yamamoto at midokura.com>
Date: Tue, 1 Oct 2024 17:16:41 +0900
Subject: [PATCH 4/8] tweak the order of conditions
no functional changes are intended
---
lld/wasm/Relocations.cpp | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/lld/wasm/Relocations.cpp b/lld/wasm/Relocations.cpp
index 4beb1e6caafc86..f21df01d8180d0 100644
--- a/lld/wasm/Relocations.cpp
+++ b/lld/wasm/Relocations.cpp
@@ -144,10 +144,9 @@ void scanRelocations(InputChunk *chunk) {
break;
}
- if (ctx.isPic ||
+ if (ctx.isPic || sym->isShared() ||
(sym->isUndefined() &&
- config->unresolvedSymbols == UnresolvedPolicy::ImportDynamic) ||
- sym->isShared()) {
+ config->unresolvedSymbols == UnresolvedPolicy::ImportDynamic)) {
switch (reloc.Type) {
case R_WASM_TABLE_INDEX_SLEB:
case R_WASM_TABLE_INDEX_SLEB64:
>From 972b54029b839c2746a699cff723cc926ee3b02a Mon Sep 17 00:00:00 2001
From: YAMAMOTO Takashi <yamamoto at midokura.com>
Date: Tue, 1 Oct 2024 18:29:42 +0900
Subject: [PATCH 5/8] lld/test/wasm/dylink-non-pie.s: simplify
---
lld/test/wasm/dylink-non-pie.s | 14 +++-----------
1 file changed, 3 insertions(+), 11 deletions(-)
diff --git a/lld/test/wasm/dylink-non-pie.s b/lld/test/wasm/dylink-non-pie.s
index a2da383e5766c7..07de7ffeae6b05 100755
--- a/lld/test/wasm/dylink-non-pie.s
+++ b/lld/test/wasm/dylink-non-pie.s
@@ -1,24 +1,16 @@
# 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 -mattr=+reference-types -triple=wasm32-unknown-unknown -o %t.o %s
-# RUN: wasm-ld -m wasm32 --features=mutable-globals -Bdynamic --export-table --growable-table --export-memory --export=__stack_pointer --export=__heap_base --export=__heap_end --entry=_start %t.o %t.lib.so -o %t.wasm
+# 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
- .tabletype __indirect_function_table, funcref
- .globaltype __memory_base, i32, immutable
-
.functype ret32 (f32) -> (i32)
.functype _start () -> ()
.globl _start
.type _start, at function
_start:
.functype _start () -> ()
- f32.const 0.0
- global.get __memory_base
- i32.const f_p at MBREL
- i32.add
- i32.load 0
- call_indirect __indirect_function_table, (f32) -> (i32)
+ i32.const f_p
drop
end_function
>From 9c486a420d977fcf808422484c930f52d29f2177 Mon Sep 17 00:00:00 2001
From: YAMAMOTO Takashi <yamamoto at midokura.com>
Date: Wed, 2 Oct 2024 09:26:35 +0900
Subject: [PATCH 6/8] lld/test/wasm/dylink-non-pie.s: check the contents of
__wasm_apply_data_relocs
---
lld/test/wasm/dylink-non-pie.s | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/lld/test/wasm/dylink-non-pie.s b/lld/test/wasm/dylink-non-pie.s
index 07de7ffeae6b05..d38b0f646e52d2 100755
--- a/lld/test/wasm/dylink-non-pie.s
+++ b/lld/test/wasm/dylink-non-pie.s
@@ -3,6 +3,7 @@
# 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)
.functype _start () -> ()
@@ -27,3 +28,10 @@ f_p:
# 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
>From 35ecf2469843b43a63fa70d5ff9801d036bdb54a Mon Sep 17 00:00:00 2001
From: YAMAMOTO Takashi <yamamoto at midokura.com>
Date: Wed, 2 Oct 2024 09:35:17 +0900
Subject: [PATCH 7/8] lld/test/wasm/dylink-non-pie.s: ensure it doesn't import
__memory_base
---
lld/test/wasm/dylink-non-pie.s | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/lld/test/wasm/dylink-non-pie.s b/lld/test/wasm/dylink-non-pie.s
index d38b0f646e52d2..a7725afa9dce69 100755
--- a/lld/test/wasm/dylink-non-pie.s
+++ b/lld/test/wasm/dylink-non-pie.s
@@ -25,6 +25,10 @@ f_p:
# 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
>From 156e6f2327cdac1a4dbeb0c1b6508a6119bdf053 Mon Sep 17 00:00:00 2001
From: YAMAMOTO Takashi <yamamoto at midokura.com>
Date: Fri, 1 Nov 2024 12:09:31 +0900
Subject: [PATCH 8/8] lld/test/wasm/dylink-non-pie.s: remove a few unnecessary
things
---
lld/test/wasm/dylink-non-pie.s | 3 ---
1 file changed, 3 deletions(-)
diff --git a/lld/test/wasm/dylink-non-pie.s b/lld/test/wasm/dylink-non-pie.s
index a7725afa9dce69..3157b8c32120f7 100755
--- a/lld/test/wasm/dylink-non-pie.s
+++ b/lld/test/wasm/dylink-non-pie.s
@@ -6,9 +6,7 @@
# RUN: llvm-objdump -d --no-show-raw-insn --no-leading-addr %t.wasm | FileCheck %s --check-prefixes DIS
.functype ret32 (f32) -> (i32)
- .functype _start () -> ()
.globl _start
- .type _start, at function
_start:
.functype _start () -> ()
i32.const f_p
@@ -16,7 +14,6 @@ _start:
end_function
.section .data.f_p,"",@
- .globl f_p
f_p:
.int32 ret32
.size f_p, 4
More information about the llvm-commits
mailing list