[llvm-branch-commits] [llvm] release/23.x: [ARM] Use .reloc for dso_local weak symbols in PIC mode instead of GOT indirection (#209660) (PR #211727)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Jul 23 22:39:21 PDT 2026


https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/211727

Backport 755f3e41de69bcf901f531b086bba6a0a58e5adc

Requested by: @petrhosek

>From 2edb230ebc57576f0a48c72c7a1dd04060e9bec3 Mon Sep 17 00:00:00 2001
From: dong jianqiang <dongjianqiang2 at huawei.com>
Date: Fri, 24 Jul 2026 09:39:26 +0800
Subject: [PATCH] [ARM] Use .reloc for dso_local weak symbols in PIC mode
 instead of GOT indirection (#209660)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

In ARM ELF PIC mode, weak symbols referenced via the constant pool use a
PC-relative expression like `.long sym-(.LPC+8)`. The assembler eagerly
resolves this when the symbol and reference are in the same section,
which prevents the linker from overriding a weak definition with a
non-weak one from another object file.

The previous approach forced *all* weak symbols through GOT indirection.
That works, but `dso_local` weak symbols with hidden/protected
visibility are non-preemptible — they don't need GOT indirection, and
the extra load is wasteful. This patch emits a `.reloc` directive for
such symbols instead, forcing the assembler to produce an `R_ARM_REL32`
relocation while keeping a direct PC-relative reference from the
constant pool.

Default-visibility weak symbols (even `dso_local`) remain routed through
the GOT because they stay exported and preemptible in shared objects,
and `R_ARM_REL32` against an external symbol cannot be used when making
a shared object.

Fix #209244

(cherry picked from commit 755f3e41de69bcf901f531b086bba6a0a58e5adc)
---
 llvm/lib/Target/ARM/ARMAsmPrinter.cpp    |  34 +++++++
 llvm/lib/Target/ARM/ARMFastISel.cpp      |   3 +-
 llvm/lib/Target/ARM/ARMISelLowering.cpp  |  11 +--
 llvm/lib/Target/ARM/ARMSubtarget.cpp     |   3 +-
 llvm/lib/Target/ARM/ARMTargetMachine.h   |   9 --
 llvm/test/CodeGen/ARM/elf-preemption.ll  | 107 ++++++++++++++++++++++-
 llvm/test/CodeGen/ARM/weak-hidden-pic.ll |  14 +--
 7 files changed, 150 insertions(+), 31 deletions(-)

diff --git a/llvm/lib/Target/ARM/ARMAsmPrinter.cpp b/llvm/lib/Target/ARM/ARMAsmPrinter.cpp
index c25a2cfeff8d6..49c5c983e2934 100644
--- a/llvm/lib/Target/ARM/ARMAsmPrinter.cpp
+++ b/llvm/lib/Target/ARM/ARMAsmPrinter.cpp
@@ -1016,6 +1016,40 @@ void ARMAsmPrinter::emitMachineConstantPoolValue(
     unsigned char TF =
         TM.getTargetTriple().isOSBinFormatMachO() ? ARMII::MO_NONLAZY : 0;
     MCSym = GetARMGVSymbol(GV, TF);
+
+    // For dso_local weak symbols in ELF PIC mode, the assembler would eagerly
+    // resolve a PC-relative expression like sym-(LPC+8) when the symbol and
+    // reference are in the same section, preventing the linker from overriding
+    // a weak definition with a non-weak definition from another section. Use a
+    // .reloc directive rather than a fixup to force the generation of a
+    // relocation (R_ARM_REL32) so the linker can perform the override. This is
+    // restricted to dso_local symbols: a preemptible/external weak symbol
+    // (e.g. an extern_weak reference) must use the GOT, as R_ARM_REL32 against
+    // an external symbol cannot be used when making a shared object.
+    if (GV->isWeakForLinker() && GV->isDSOLocal() &&
+        TM.getTargetTriple().isOSBinFormatELF() && TM.isPositionIndependent() &&
+        ACPV->getPCAdjustment() != 0) {
+      MCSymbol *CPILabel = OutContext.createTempSymbol();
+      OutStreamer->emitLabel(CPILabel);
+      // Emit local-only expression: CPILabel - (LPC+PCAdj)
+      const MCExpr *LocalExpr = MCSymbolRefExpr::create(CPILabel, OutContext);
+      MCSymbol *PCLabel =
+          getPICLabel(DL.getInternalSymbolPrefix(), getFunctionNumber(),
+                      ACPV->getLabelId(), OutContext);
+      const MCExpr *PCRelExpr = MCSymbolRefExpr::create(PCLabel, OutContext);
+      PCRelExpr = MCBinaryExpr::createAdd(
+          PCRelExpr,
+          MCConstantExpr::create(ACPV->getPCAdjustment(), OutContext),
+          OutContext);
+      LocalExpr = MCBinaryExpr::createSub(LocalExpr, PCRelExpr, OutContext);
+      OutStreamer->emitValue(LocalExpr, Size);
+      // Emit .reloc to force linker resolution of the weak symbol.
+      const MCExpr *CPIExpr = MCSymbolRefExpr::create(CPILabel, OutContext);
+      const MCExpr *SymExpr = MCSymbolRefExpr::create(MCSym, OutContext);
+      OutStreamer->emitRelocDirective(*CPIExpr, "R_ARM_REL32", SymExpr,
+                                      SMLoc());
+      return;
+    }
   } else if (ACPV->isMachineBasicBlock()) {
     const MachineBasicBlock *MBB = cast<ARMConstantPoolMBB>(ACPV)->getMBB();
     MCSym = MBB->getSymbol();
diff --git a/llvm/lib/Target/ARM/ARMFastISel.cpp b/llvm/lib/Target/ARM/ARMFastISel.cpp
index 1cf0d98f690aa..2c5d286e11c4f 100644
--- a/llvm/lib/Target/ARM/ARMFastISel.cpp
+++ b/llvm/lib/Target/ARM/ARMFastISel.cpp
@@ -3033,8 +3033,7 @@ bool ARMFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
 }
 
 Register ARMFastISel::ARMLowerPICELF(const GlobalValue *GV, MVT VT) {
-  // Weak symbols need GOT indirection even when hidden/DSO-local.
-  bool UseGOT_PREL = !GV->isDSOLocal() || GV->isWeakForLinker();
+  bool UseGOT_PREL = !GV->isDSOLocal();
   LLVMContext *Context = &MF->getFunction().getContext();
   unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
   unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
diff --git a/llvm/lib/Target/ARM/ARMISelLowering.cpp b/llvm/lib/Target/ARM/ARMISelLowering.cpp
index 81cca300389e7..998e09384dab6 100644
--- a/llvm/lib/Target/ARM/ARMISelLowering.cpp
+++ b/llvm/lib/Target/ARM/ARMISelLowering.cpp
@@ -3651,15 +3651,10 @@ SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
       return V;
 
   if (isPositionIndependent()) {
-    // Weak symbols need GOT indirection even when hidden/DSO-local.
-    // The assembler eagerly resolves PC-relative expressions when the
-    // symbol and reference are in the same section, which prevents the
-    // linker from overriding a weak definition with a non-weak one.
-    bool UseGOT = !GV->isDSOLocal() || GV->isWeakForLinker();
-    SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
-                                           UseGOT ? ARMII::MO_GOT : 0);
+    SDValue G = DAG.getTargetGlobalAddress(
+        GV, dl, PtrVT, 0, GV->isDSOLocal() ? 0 : ARMII::MO_GOT);
     SDValue Result = DAG.getNode(ARMISD::WrapperPIC, dl, PtrVT, G);
-    if (UseGOT)
+    if (!GV->isDSOLocal())
       Result =
           DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
                       MachinePointerInfo::getGOT(DAG.getMachineFunction()));
diff --git a/llvm/lib/Target/ARM/ARMSubtarget.cpp b/llvm/lib/Target/ARM/ARMSubtarget.cpp
index 0f324a6a31757..4893d8d3a9ef1 100644
--- a/llvm/lib/Target/ARM/ARMSubtarget.cpp
+++ b/llvm/lib/Target/ARM/ARMSubtarget.cpp
@@ -397,8 +397,7 @@ bool ARMSubtarget::isGVIndirectSymbol(const GlobalValue *GV) const {
 }
 
 bool ARMSubtarget::isGVInGOT(const GlobalValue *GV) const {
-  return isTargetELF() && TM.isPositionIndependent() &&
-         (!GV->isDSOLocal() || GV->isWeakForLinker());
+  return isTargetELF() && TM.isPositionIndependent() && !GV->isDSOLocal();
 }
 
 unsigned ARMSubtarget::getMispredictionPenalty() const {
diff --git a/llvm/lib/Target/ARM/ARMTargetMachine.h b/llvm/lib/Target/ARM/ARMTargetMachine.h
index 109d6fd5e6671..215b541620acb 100644
--- a/llvm/lib/Target/ARM/ARMTargetMachine.h
+++ b/llvm/lib/Target/ARM/ARMTargetMachine.h
@@ -111,15 +111,6 @@ class ARMBaseTargetMachine : public CodeGenTargetMachineImpl {
         (GV->isDeclarationForLinker() || GV->hasCommonLinkage()))
       return true;
 
-    // In ELF PIC mode, weak symbols referenced via the constant pool use a
-    // PC-relative expression (e.g. .long xxx-(.LPC+8)) that the assembler
-    // eagerly resolves when both the symbol and label are in the same section.
-    // This prevents the linker from overriding a weak definition with a
-    // non-weak one. Use GOT indirection for weak symbols to avoid this.
-    if (getTargetTriple().isOSBinFormatELF() && isPositionIndependent() &&
-        GV->isWeakForLinker())
-      return true;
-
     return false;
   }
 
diff --git a/llvm/test/CodeGen/ARM/elf-preemption.ll b/llvm/test/CodeGen/ARM/elf-preemption.ll
index 88b35683b97c0..599a59520e5e3 100644
--- a/llvm/test/CodeGen/ARM/elf-preemption.ll
+++ b/llvm/test/CodeGen/ARM/elf-preemption.ll
@@ -59,13 +59,14 @@ define ptr @get_weak_dsolocal_var() nounwind {
 ; PIC:       @ %bb.0:
 ; PIC-NEXT:    ldr r0, .LCPI2_0
 ; PIC-NEXT:  .LPC2_0:
-; PIC-NEXT:    ldr r0, [pc, r0]
+; PIC-NEXT:    add r0, pc, r0
 ; PIC-NEXT:    bx lr
 ; PIC-NEXT:    .p2align 2
 ; PIC-NEXT:  @ %bb.1:
 ; PIC-NEXT:  .LCPI2_0:
 ; PIC-NEXT:  .Ltmp1:
-; PIC-NEXT:    .long weak_dsolocal_var(GOT_PREL)-(.LPC2_0+8-.Ltmp1)
+; PIC-NEXT:    .long .Ltmp1-(.LPC2_0+8)
+; PIC-NEXT:    .reloc .Ltmp1, R_ARM_REL32, weak_dsolocal_var
   ret ptr @weak_dsolocal_var
 }
 
@@ -163,13 +164,14 @@ define weak dso_local ptr @weak_dsolocal_func() nounwind {
 ; PIC:       @ %bb.0:
 ; PIC-NEXT:    ldr r0, .LCPI7_0
 ; PIC-NEXT:  .LPC7_0:
-; PIC-NEXT:    ldr r0, [pc, r0]
+; PIC-NEXT:    add r0, pc, r0
 ; PIC-NEXT:    bx lr
 ; PIC-NEXT:    .p2align 2
 ; PIC-NEXT:  @ %bb.1:
 ; PIC-NEXT:  .LCPI7_0:
 ; PIC-NEXT:  .Ltmp3:
-; PIC-NEXT:    .long weak_dsolocal_func(GOT_PREL)-(.LPC7_0+8-.Ltmp3)
+; PIC-NEXT:    .long .Ltmp3-(.LPC7_0+8)
+; PIC-NEXT:    .reloc .Ltmp3, R_ARM_REL32, weak_dsolocal_func
   ret ptr @weak_dsolocal_func
 }
 
@@ -192,3 +194,100 @@ define dso_local void @call_dsolocal_func() nounwind {
   call ptr @dsolocal_func()
   ret void
 }
+
+;; extern_weak (undefined) symbols are external/preemptible and must go through
+;; the GOT in PIC mode. R_ARM_REL32 against an external or undefined symbol
+;; cannot be used when making a shared object, so the .reloc path must not
+;; apply to them.
+ at extern_weak_var = extern_weak global i32
+define ptr @get_extern_weak_var() nounwind {
+; STATIC-LABEL: get_extern_weak_var:
+; STATIC:       @ %bb.0:
+; STATIC-NEXT:    movw r0, :lower16:extern_weak_var
+; STATIC-NEXT:    movt r0, :upper16:extern_weak_var
+; STATIC-NEXT:    bx lr
+;
+; PIC-LABEL: get_extern_weak_var:
+; PIC:       @ %bb.0:
+; PIC-NEXT:    ldr r0, .LCPI9_0
+; PIC-NEXT:  .LPC9_0:
+; PIC-NEXT:    ldr r0, [pc, r0]
+; PIC-NEXT:    bx lr
+; PIC-NEXT:    .p2align 2
+; PIC-NEXT:  @ %bb.1:
+; PIC-NEXT:  .LCPI9_0:
+; PIC-NEXT:  .Ltmp4:
+; PIC-NEXT:    .long extern_weak_var(GOT_PREL)-(.LPC9_0+8-.Ltmp4)
+  ret ptr @extern_weak_var
+}
+
+declare extern_weak ptr @extern_weak_func()
+define ptr @get_extern_weak_func() nounwind {
+; STATIC-LABEL: get_extern_weak_func:
+; STATIC:       @ %bb.0:
+; STATIC-NEXT:    movw r0, :lower16:extern_weak_func
+; STATIC-NEXT:    movt r0, :upper16:extern_weak_func
+; STATIC-NEXT:    bx lr
+;
+; PIC-LABEL: get_extern_weak_func:
+; PIC:       @ %bb.0:
+; PIC-NEXT:    ldr r0, .LCPI10_0
+; PIC-NEXT:  .LPC10_0:
+; PIC-NEXT:    ldr r0, [pc, r0]
+; PIC-NEXT:    bx lr
+; PIC-NEXT:    .p2align 2
+; PIC-NEXT:  @ %bb.1:
+; PIC-NEXT:  .LCPI10_0:
+; PIC-NEXT:  .Ltmp5:
+; PIC-NEXT:    .long extern_weak_func(GOT_PREL)-(.LPC10_0+8-.Ltmp5)
+  ret ptr @extern_weak_func
+}
+
+;; An undefined weak reference with hidden visibility (e.g. from
+;; `[[gnu::weak, gnu::visibility("hidden")]] extern int foo;`) is still not
+;; dso_local, so it must go through the GOT: the compiler cannot know whether
+;; it will be defined or undefined at link time, and an undefined weak has to
+;; materialize as 0, which only the GOT can produce (R_ARM_REL32 cannot).
+ at extern_weak_hidden_var = extern_weak hidden global i32
+define ptr @get_extern_weak_hidden_var() nounwind {
+; STATIC-LABEL: get_extern_weak_hidden_var:
+; STATIC:       @ %bb.0:
+; STATIC-NEXT:    movw r0, :lower16:extern_weak_hidden_var
+; STATIC-NEXT:    movt r0, :upper16:extern_weak_hidden_var
+; STATIC-NEXT:    bx lr
+;
+; PIC-LABEL: get_extern_weak_hidden_var:
+; PIC:       @ %bb.0:
+; PIC-NEXT:    ldr r0, .LCPI11_0
+; PIC-NEXT:  .LPC11_0:
+; PIC-NEXT:    ldr r0, [pc, r0]
+; PIC-NEXT:    bx lr
+; PIC-NEXT:    .p2align 2
+; PIC-NEXT:  @ %bb.1:
+; PIC-NEXT:  .LCPI11_0:
+; PIC-NEXT:  .Ltmp6:
+; PIC-NEXT:    .long extern_weak_hidden_var(GOT_PREL)-(.LPC11_0+8-.Ltmp6)
+  ret ptr @extern_weak_hidden_var
+}
+
+declare extern_weak hidden ptr @extern_weak_hidden_func()
+define ptr @get_extern_weak_hidden_func() nounwind {
+; STATIC-LABEL: get_extern_weak_hidden_func:
+; STATIC:       @ %bb.0:
+; STATIC-NEXT:    movw r0, :lower16:extern_weak_hidden_func
+; STATIC-NEXT:    movt r0, :upper16:extern_weak_hidden_func
+; STATIC-NEXT:    bx lr
+;
+; PIC-LABEL: get_extern_weak_hidden_func:
+; PIC:       @ %bb.0:
+; PIC-NEXT:    ldr r0, .LCPI12_0
+; PIC-NEXT:  .LPC12_0:
+; PIC-NEXT:    ldr r0, [pc, r0]
+; PIC-NEXT:    bx lr
+; PIC-NEXT:    .p2align 2
+; PIC-NEXT:  @ %bb.1:
+; PIC-NEXT:  .LCPI12_0:
+; PIC-NEXT:  .Ltmp7:
+; PIC-NEXT:    .long extern_weak_hidden_func(GOT_PREL)-(.LPC12_0+8-.Ltmp7)
+  ret ptr @extern_weak_hidden_func
+}
diff --git a/llvm/test/CodeGen/ARM/weak-hidden-pic.ll b/llvm/test/CodeGen/ARM/weak-hidden-pic.ll
index 4d6eca77af855..26a779f2dc9f9 100644
--- a/llvm/test/CodeGen/ARM/weak-hidden-pic.ll
+++ b/llvm/test/CodeGen/ARM/weak-hidden-pic.ll
@@ -2,18 +2,20 @@
 ; RUN: llc < %s -mtriple=thumbv7-linux-gnueabi -relocation-model=pic | FileCheck %s
 ; RUN: llc < %s -O0 -fast-isel-abort=2 -mtriple=armv7-linux-gnueabi -relocation-model=pic | FileCheck %s
 
-; Hidden weak function with dso_local must still use GOT indirection
-; in PIC mode on ARM. The assembler eagerly resolves PC-relative
-; expressions like .long xxx-(.LPC+8) when both are in the same section,
-; which prevents the linker from overriding the weak definition with
-; a non-weak one from another object file.
+; Weak dso_local hidden functions must be overridable at link time
+; (a non-weak definition in another object should override the weak one).
+; Instead of using GOT indirection, we use a PC-relative constant pool
+; entry with a .reloc directive to force the assembler to emit a proper
+; relocation (R_ARM_REL32), preventing eager resolution when the symbol
+; and reference are in the same section.
 
 define weak dso_local hidden void @weak_hidden_func() {
   ret void
 }
 
 ; CHECK-LABEL: weak_hidden_func_addr:
-; CHECK:       .long weak_hidden_func(GOT_PREL)
+; CHECK:       .long .Ltmp{{[0-9]+}}-(.LPC{{[0-9]+}}_0+{{[48]}})
+; CHECK:       .reloc .Ltmp{{[0-9]+}}, R_ARM_REL32, weak_hidden_func
 define i8* @weak_hidden_func_addr() {
   ret i8* bitcast (void()* @weak_hidden_func to i8*)
 }



More information about the llvm-branch-commits mailing list