[lld] [lld][RISCV] More fine-grained __global_pointer$ placement (PR #182029)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 19 05:23:32 PST 2026
https://github.com/LukeZhuang updated https://github.com/llvm/llvm-project/pull/182029
>From 9d287eae2365b6cbe5e455bc6e8c43dd12f55e52 Mon Sep 17 00:00:00 2001
From: Zhi Zhuang <zhuangzhi.zz at alibaba-inc.com>
Date: Wed, 18 Feb 2026 21:31:22 +0800
Subject: [PATCH] [lld][RISCV] More fine-grained __global_pointer$ placement
(#182029)
Placing __global_pointer$ more wisely by looking at other meaningful
section anchors, which can help us do gp-relax even if .sdata section
does not exist. This is more similar to the behaviour of GNU-LD.
---
lld/ELF/Writer.cpp | 24 +++++++++++++++++-----
lld/test/ELF/riscv-relax-hi20-lo12.s | 30 ++++++++++++++++++++++++++++
lld/test/ELF/riscv-section-layout.s | 6 ++----
3 files changed, 51 insertions(+), 9 deletions(-)
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index c84e4df767452..8e1ce80237354 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -1842,15 +1842,29 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
addRelIpltSymbols();
// RISC-V's gp can address +/- 2 KiB, set it to .sdata + 0x800. This symbol
- // should only be defined in an executable. If .sdata does not exist, its
+ // should only be defined in an executable. If .sdata does not exist, look
+ // for other section anchors. If none of them exists (very unusual), its
// value/section does not matter but it has to be relative, so set its
// st_shndx arbitrarily to 1 (ctx.out.elfHeader).
if (ctx.arg.emachine == EM_RISCV) {
if (!ctx.arg.shared) {
- OutputSection *sec = findSection(ctx, ".sdata");
- addOptionalRegular(ctx, "__global_pointer$",
- sec ? sec : ctx.out.elfHeader.get(), 0x800,
- STV_DEFAULT);
+ // Try to put the global pointer at the beginning of some anchor
+ // sections, by the order of `secAnchors` below.
+ bool foundSecAnchor = false;
+ std::array<StringRef, 4> secAnchors = {".sdata", ".data", ".sbss",
+ ".bss"};
+ for (auto secName : secAnchors) {
+ OutputSection *sec = findSection(ctx, secName);
+ if (sec) {
+ addOptionalRegular(ctx, "__global_pointer$", sec, 0x800,
+ STV_DEFAULT);
+ foundSecAnchor = true;
+ break;
+ }
+ }
+ if (!foundSecAnchor)
+ addOptionalRegular(ctx, "__global_pointer$", ctx.out.elfHeader.get(),
+ 0x800, STV_DEFAULT);
// Set riscvGlobalPointer to be used by the optional global pointer
// relaxation.
if (ctx.arg.relaxGP) {
diff --git a/lld/test/ELF/riscv-relax-hi20-lo12.s b/lld/test/ELF/riscv-relax-hi20-lo12.s
index aa10e957cc60e..0fb1d344b5b2b 100644
--- a/lld/test/ELF/riscv-relax-hi20-lo12.s
+++ b/lld/test/ELF/riscv-relax-hi20-lo12.s
@@ -9,6 +9,36 @@
# RUN: llvm-objdump -td -M no-aliases --no-show-raw-insn rv32 | FileCheck %s
# RUN: llvm-objdump -td -M no-aliases --no-show-raw-insn rv64 | FileCheck %s
+
+# RUN: sed 's/\.sdata/\.data/g' a.s > a2.s && sed 's/\.sdata/\.data/g' lds > lds2
+# RUN: llvm-mc -filetype=obj -triple=riscv32-unknown-elf -mattr=+relax a2.s -o rv32.o
+# RUN: llvm-mc -filetype=obj -triple=riscv64-unknown-elf -mattr=+relax a2.s -o rv64.o
+
+# RUN: ld.lld --relax-gp --undefined=__global_pointer$ --defsym baz=420 rv32.o lds2 -o rv32
+# RUN: ld.lld --relax-gp --undefined=__global_pointer$ --defsym baz=420 rv64.o lds2 -o rv64
+# RUN: llvm-objdump -td -M no-aliases --no-show-raw-insn rv32 | FileCheck %s
+# RUN: llvm-objdump -td -M no-aliases --no-show-raw-insn rv64 | FileCheck %s
+
+
+# RUN: sed 's/\.sdata/\.sbss/g' a.s > a3.s && sed 's/\.sdata/\.sbss/g' lds > lds3
+# RUN: llvm-mc -filetype=obj -triple=riscv32-unknown-elf -mattr=+relax a3.s -o rv32.o
+# RUN: llvm-mc -filetype=obj -triple=riscv64-unknown-elf -mattr=+relax a3.s -o rv64.o
+
+# RUN: ld.lld --relax-gp --undefined=__global_pointer$ --defsym baz=420 rv32.o lds3 -o rv32
+# RUN: ld.lld --relax-gp --undefined=__global_pointer$ --defsym baz=420 rv64.o lds3 -o rv64
+# RUN: llvm-objdump -td -M no-aliases --no-show-raw-insn rv32 | FileCheck %s
+# RUN: llvm-objdump -td -M no-aliases --no-show-raw-insn rv64 | FileCheck %s
+
+
+# RUN: sed 's/\.sdata/\.bss/g' a.s > a4.s && sed 's/\.sdata/\.bss/g' lds > lds4
+# RUN: llvm-mc -filetype=obj -triple=riscv32-unknown-elf -mattr=+relax a4.s -o rv32.o
+# RUN: llvm-mc -filetype=obj -triple=riscv64-unknown-elf -mattr=+relax a4.s -o rv64.o
+
+# RUN: ld.lld --relax-gp --undefined=__global_pointer$ --defsym baz=420 rv32.o lds4 -o rv32
+# RUN: ld.lld --relax-gp --undefined=__global_pointer$ --defsym baz=420 rv64.o lds4 -o rv64
+# RUN: llvm-objdump -td -M no-aliases --no-show-raw-insn rv32 | FileCheck %s
+# RUN: llvm-objdump -td -M no-aliases --no-show-raw-insn rv64 | FileCheck %s
+
# CHECK: 00000040 l .text {{0*}}0 a
# CHECK-NOT: lui
diff --git a/lld/test/ELF/riscv-section-layout.s b/lld/test/ELF/riscv-section-layout.s
index 123b5f056ad66..fe2cf306204ae 100644
--- a/lld/test/ELF/riscv-section-layout.s
+++ b/lld/test/ELF/riscv-section-layout.s
@@ -25,14 +25,12 @@
# NOSDATA-NEXT: .data PROGBITS [[#%x,DATA:]]
# NOSDATA-NEXT: .bss NOBITS [[#%x,BSS:]]
-## If there is an undefined reference to __global_pointer$ but .sdata doesn't
-## exist, define __global_pointer$ and set its st_shndx arbitrarily to 1.
-## The symbol value should not be used by the program.
+## If .sdata doesn't exist but .data exists, define __global_pointer$ at DATA_BEGIN+0x800
# NOSDATA-DAG: [[#]]: {{.*}} 0 NOTYPE GLOBAL DEFAULT [[#]] (.text) _etext
# NOSDATA-DAG: [[#]]: {{0*}}[[#BSS]] 0 NOTYPE GLOBAL DEFAULT [[#]] (.data) _edata
# NOSDATA-DAG: [[#]]: {{0*}}[[#BSS]] 0 NOTYPE GLOBAL DEFAULT [[#]] (.bss) __bss_start
-# NOSDATA-DAG: [[#]]: {{0*}}800 0 NOTYPE GLOBAL DEFAULT 1 (.dynsym) __global_pointer$
+# NOSDATA-DAG: [[#]]: {{0*}}[[#DATA+0x800]] 0 NOTYPE GLOBAL DEFAULT [[#]] (.data) __global_pointer$
# CHECK: .rodata PROGBITS
# CHECK-NEXT: .srodata PROGBITS
More information about the llvm-commits
mailing list