Skip to content

Commit 0198e8f

Browse files
authored
[lld][WebAssembly] Honor command line reloc model during LTO (#164838)
This code matches what the ELF linker already does. See ae4c30a from back in 2019.
1 parent 3f27f57 commit 0198e8f

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
;; The explicit relocation model flag.
2+
3+
; RUN: llvm-as %s -o %t.o
4+
5+
; RUN: wasm-ld %t.o -o %t.wasm -save-temps -r -mllvm -relocation-model=pic
6+
; RUN: llvm-readobj -r %t.wasm.lto.o | FileCheck %s --check-prefix=PIC
7+
8+
; RUN: wasm-ld %t.o -o %t_static.wasm -save-temps -r -mllvm -relocation-model=static
9+
; RUN: llvm-readobj -r %t_static.wasm.lto.o | FileCheck %s --check-prefix=STATIC
10+
11+
; PIC: R_WASM_GLOBAL_INDEX_LEB foo
12+
; STATIC: R_WASM_MEMORY_ADDR_LEB foo
13+
14+
target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20"
15+
target triple = "wasm32-unknown-unknown"
16+
17+
@foo = external global i32
18+
define i32 @_start() {
19+
%t = load i32, i32* @foo
20+
ret i32 %t
21+
}

lld/wasm/LTO.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ static lto::Config createConfig() {
5757
c.DebugPassManager = ctx.arg.ltoDebugPassManager;
5858
c.AlwaysEmitRegularLTOObj = !ctx.arg.ltoObjPath.empty();
5959

60-
if (ctx.arg.relocatable)
60+
if (auto relocModel = getRelocModelFromCMModel())
61+
c.RelocModel = *relocModel;
62+
else if (ctx.arg.relocatable)
6163
c.RelocModel = std::nullopt;
6264
else if (ctx.isPic)
6365
c.RelocModel = Reloc::PIC_;

0 commit comments

Comments
 (0)