[lld] [lld][WebAssembly]: Restore non-pie dynamic-linking executable (PR #108146)

YAMAMOTO Takashi via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 30 22:19:30 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/2] [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/2] 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



More information about the llvm-commits mailing list