[lld] [lld][WebAssembly] Reject shared libraries when `-static`/`-Bstatic` is used (PR #108263)

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 11 11:15:21 PDT 2024


https://github.com/sbc100 updated https://github.com/llvm/llvm-project/pull/108263

>From a81c1e04822a7a3569bbb344b79d8d807a959357 Mon Sep 17 00:00:00 2001
From: Sam Clegg <sbc at chromium.org>
Date: Wed, 11 Sep 2024 11:09:00 -0700
Subject: [PATCH] [lld][WebAssembly] Reject shared libraries when
 `-static`/`-Bstatic` is used

This matches the behaviour of GNU ld and the ELF version of lld.
---
 lld/test/wasm/static-error.s | 12 ++++++++++++
 lld/wasm/Driver.cpp          | 10 ++++++++--
 2 files changed, 20 insertions(+), 2 deletions(-)
 create mode 100644 lld/test/wasm/static-error.s

diff --git a/lld/test/wasm/static-error.s b/lld/test/wasm/static-error.s
new file mode 100644
index 00000000000000..3557506a5f07a2
--- /dev/null
+++ b/lld/test/wasm/static-error.s
@@ -0,0 +1,12 @@
+// RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %s -o %t.o
+// RUN: wasm-ld --experimental-pic -shared -o %t.so %t.o
+
+// RUN: wasm-ld --experimental-pic -pie -o /dev/null %t.o %t.so
+// RUN: not wasm-ld -o /dev/null -static %t.o %t.so 2>&1 | FileCheck %s
+
+// CHECK: attempted static link of dynamic object
+
+.global _start
+_start:
+  .functype _start () -> ()
+  end_function
diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index cb8fe2534f5fe7..2de7dcaeb43d47 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -333,9 +333,15 @@ void LinkerDriver::addFile(StringRef path) {
     return;
   }
   case file_magic::bitcode:
-  case file_magic::wasm_object:
-    files.push_back(createObjectFile(mbref, "", 0, inLib));
+  case file_magic::wasm_object: {
+    auto obj = createObjectFile(mbref, "", 0, inLib);
+    if (config->isStatic && isa<SharedFile>(obj)) {
+      error("attempted static link of dynamic object " + path);
+      break;
+    }
+    files.push_back(obj);
     break;
+  }
   case file_magic::unknown:
     if (mbref.getBuffer().starts_with("#STUB")) {
       files.push_back(make<StubFile>(mbref));



More information about the llvm-commits mailing list