[lld] [lld][WebAssembly] Honor command line reloc model during LTO (PR #164838)
    Sam Clegg via llvm-commits 
    llvm-commits at lists.llvm.org
       
    Thu Oct 23 08:59:34 PDT 2025
    
    
  
https://github.com/sbc100 created https://github.com/llvm/llvm-project/pull/164838
This code matches what the ELF linker already does.  See ae4c30a4bed from back in 2019.
>From cf00ee9e15cdfc8a5b2592ad0cc50f7152be5d4c Mon Sep 17 00:00:00 2001
From: Sam Clegg <sbc at chromium.org>
Date: Thu, 23 Oct 2025 08:16:31 -0700
Subject: [PATCH] [lld][WebAssembly] Honor command line reloc model during LTO
This code matches what the ELF linker already does.  See ae4c30a4bed
from back in 2019.
---
 lld/test/wasm/lto/relocation-model.ll | 21 +++++++++++++++++++++
 lld/wasm/LTO.cpp                      |  4 +++-
 2 files changed, 24 insertions(+), 1 deletion(-)
 create mode 100644 lld/test/wasm/lto/relocation-model.ll
diff --git a/lld/test/wasm/lto/relocation-model.ll b/lld/test/wasm/lto/relocation-model.ll
new file mode 100644
index 0000000000000..8fe198d0c64e6
--- /dev/null
+++ b/lld/test/wasm/lto/relocation-model.ll
@@ -0,0 +1,21 @@
+;; The explicit relocation model flag.
+
+; RUN: llvm-as %s -o %t.o
+
+; RUN: wasm-ld %t.o -o %t.wasm -save-temps -r -mllvm -relocation-model=pic
+; RUN: llvm-readobj -r %t.wasm.lto.o | FileCheck %s --check-prefix=PIC
+
+; 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
+
+; PIC: R_WASM_GLOBAL_INDEX_LEB foo
+; STATIC: R_WASM_MEMORY_ADDR_LEB foo
+
+target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20"
+target triple = "wasm32-unknown-unknown"
+
+ at foo = external global i32
+define i32 @_start() {
+  %t = load i32, i32* @foo
+  ret i32 %t
+}
diff --git a/lld/wasm/LTO.cpp b/lld/wasm/LTO.cpp
index 71f18aa25a35c..ae85f4693214b 100644
--- a/lld/wasm/LTO.cpp
+++ b/lld/wasm/LTO.cpp
@@ -57,7 +57,9 @@ static lto::Config createConfig() {
   c.DebugPassManager = ctx.arg.ltoDebugPassManager;
   c.AlwaysEmitRegularLTOObj = !ctx.arg.ltoObjPath.empty();
 
-  if (ctx.arg.relocatable)
+  if (auto relocModel = getRelocModelFromCMModel())
+    c.RelocModel = *relocModel;
+  else if (ctx.arg.relocatable)
     c.RelocModel = std::nullopt;
   else if (ctx.isPic)
     c.RelocModel = Reloc::PIC_;
    
    
More information about the llvm-commits
mailing list