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

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 18 09:23:49 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
+        // sections, by the order of `secAnchors` below.
+        bool foundSecAnchor = false;
+        std::vector<StringRef> secAnchors = {".sdata", ".data", ".sbss",
----------------
topperc wrote:

std::array or a plain C array? No need to make a vector of fixed size.

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


More information about the llvm-commits mailing list