[lld] [lld][RISCV] More fine-grained __global_pointer$ placement (PR #182029)

via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 18 11:09:14 PST 2026


================
@@ -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
----------------
LukeZhuang wrote:

Yeah I understood, defining it explicitly in the link script is always a hammer. I guess the original purpose of this piece of code is providing a “default behavior” or a backup plan, when the users are not able to (or do not want to) do this. May I ask what do you guys think?

https://github.com/llvm/llvm-project/pull/182029


More information about the llvm-commits mailing list