Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Sep 11, 2024

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Sep 11, 2024

This matches the behaviour of GNU ld and the ELF version of lld.

@llvmbot
Copy link
Member

llvmbot commented Sep 11, 2024

@llvm/pr-subscribers-lld-wasm

@llvm/pr-subscribers-lld

Author: Sam Clegg (sbc100)

Changes

This matches the behaviour of GNU ld and the ELF version of lld.


Full diff: https://github.com/llvm/llvm-project/pull/108263.diff

2 Files Affected:

  • (added) lld/test/wasm/static-error.s (+12)
  • (modified) lld/wasm/Driver.cpp (+10-3)
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..1480cc4151d7db 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));
@@ -395,7 +401,8 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
       config->isStatic = true;
       break;
     case OPT_Bdynamic:
-      config->isStatic = false;
+      if (!config->relocatable)
+        config->isStatic = false;
       break;
     case OPT_whole_archive:
       inWholeArchive = true;

…is used

This matches the behaviour of GNU ld and the ELF version of lld.
@sbc100 sbc100 force-pushed the reject_dylink_in_static_mode branch from c8e5a1c to a81c1e0 Compare September 11, 2024 18:15
@sbc100 sbc100 merged commit be770ed into llvm:main Sep 11, 2024
5 of 7 checks passed
@sbc100 sbc100 deleted the reject_dylink_in_static_mode branch September 11, 2024 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants