Skip to content

Commit 6d51c31

Browse files
authored
[lld][WebAssembly] LTO: Use PIC reloc model with dynamic imports (llvm#165342)
1 parent c25a4a9 commit 6d51c31

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

lld/test/wasm/lto/relocation-model.ll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
; RUN: wasm-ld %t.o -o %t_static.wasm -save-temps -r -mllvm -relocation-model=static
99
; RUN: llvm-readobj -r %t_static.wasm.lto.o | FileCheck %s --check-prefix=STATIC
1010

11+
;; Linking with --unresolved-symbols=import-dynamic should also generate PIC
12+
;; code for external references.
13+
; RUN: wasm-ld %t.o -o %t_import.wasm -save-temps --experimental-pic --unresolved-symbols=import-dynamic
14+
; RUN: llvm-readobj -r %t_import.wasm.lto.o | FileCheck %s --check-prefix=PIC
15+
1116
; PIC: R_WASM_GLOBAL_INDEX_LEB foo
1217
; STATIC: R_WASM_MEMORY_ADDR_LEB foo
1318

lld/wasm/LTO.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ static lto::Config createConfig() {
6363
c.RelocModel = std::nullopt;
6464
else if (ctx.isPic)
6565
c.RelocModel = Reloc::PIC_;
66+
else if (ctx.arg.unresolvedSymbols == UnresolvedPolicy::ImportDynamic)
67+
// With ImportDynamic we also need to use the PIC relocation model so that
68+
// external symbols are references via the GOT.
69+
// TODO(sbc): This should probably be Reloc::DynamicNoPIC, but the backend
70+
// doesn't currently support that.
71+
c.RelocModel = Reloc::PIC_;
6672
else
6773
c.RelocModel = Reloc::Static;
6874

0 commit comments

Comments
 (0)