Skip to content

Conversation

@sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Oct 28, 2025

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Oct 28, 2025

@llvm/pr-subscribers-lld-wasm

@llvm/pr-subscribers-lld

Author: Sam Clegg (sbc100)

Changes

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

2 Files Affected:

  • (modified) lld/test/wasm/lto/relocation-model.ll (+5)
  • (modified) lld/wasm/LTO.cpp (+6)
diff --git a/lld/test/wasm/lto/relocation-model.ll b/lld/test/wasm/lto/relocation-model.ll
index 8fe198d0c64e6..a042615b8fe1c 100644
--- a/lld/test/wasm/lto/relocation-model.ll
+++ b/lld/test/wasm/lto/relocation-model.ll
@@ -8,6 +8,11 @@
 ; RUN: wasm-ld %t.o -o %t_static.wasm -save-temps -r -mllvm -relocation-model=static
 ; RUN: llvm-readobj -r %t_static.wasm.lto.o | FileCheck %s --check-prefix=STATIC
 
+;; Linking with --unresolved-symbols=import-dynamic should also generate PIC
+;; code for external references.
+; RUN: wasm-ld %t.o -o %t_import.wasm -save-temps --experimental-pic --unresolved-symbols=import-dynamic
+; RUN: llvm-readobj -r %t_import.wasm.lto.o | FileCheck %s --check-prefix=PIC
+
 ; PIC: R_WASM_GLOBAL_INDEX_LEB foo
 ; STATIC: R_WASM_MEMORY_ADDR_LEB foo
 
diff --git a/lld/wasm/LTO.cpp b/lld/wasm/LTO.cpp
index ae85f4693214b..668cdf21ea3ed 100644
--- a/lld/wasm/LTO.cpp
+++ b/lld/wasm/LTO.cpp
@@ -63,6 +63,12 @@ static lto::Config createConfig() {
     c.RelocModel = std::nullopt;
   else if (ctx.isPic)
     c.RelocModel = Reloc::PIC_;
+  else if (ctx.arg.unresolvedSymbols == UnresolvedPolicy::ImportDynamic)
+    // With ImportDynamic we also need to use the PIC relocation model so that
+    // external symbols are references via the GOT.
+    // TODO(sbc): This should probably be Reloc::DynamicNoPIC, but the backend
+    // doesn't currently support that.
+    c.RelocModel = Reloc::PIC_;
   else
     c.RelocModel = Reloc::Static;
 

@sbc100 sbc100 force-pushed the import_dynamic_pic branch from 4e4528b to fcdee61 Compare October 28, 2025 15:25
@sbc100 sbc100 requested a review from aheejin October 28, 2025 15:25
@sbc100 sbc100 enabled auto-merge (squash) October 28, 2025 15:27
@sbc100 sbc100 disabled auto-merge October 28, 2025 15:27
// external symbols are references via the GOT.
// TODO(sbc): This should probably be Reloc::DynamicNoPIC, but the backend
// doesn't currently support that.
c.RelocModel = Reloc::PIC_;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the docs for import-dynamic need to be updated?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentatio already talks about how the input file need to be PIC.. so this is really just a bugfix I think. The code better matches the docs now.

@sbc100 sbc100 requested a review from kripken October 28, 2025 21:46
@sbc100 sbc100 merged commit 6d51c31 into llvm:main Oct 28, 2025
10 checks passed
@sbc100 sbc100 deleted the import_dynamic_pic branch October 28, 2025 21:46
aokblast pushed a commit to aokblast/llvm-project that referenced this pull request Oct 30, 2025
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