[lld] d9ef557 - [ELF] -r: don't define __global_pointer$

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 22 12:37:56 PST 2022


Author: Fangrui Song
Date: 2022-11-22T12:37:51-08:00
New Revision: d9ef5574d4e37af394d7bd53ab7e2534949c5c63

URL: https://github.com/llvm/llvm-project/commit/d9ef5574d4e37af394d7bd53ab7e2534949c5c63
DIFF: https://github.com/llvm/llvm-project/commit/d9ef5574d4e37af394d7bd53ab7e2534949c5c63.diff

LOG: [ELF] -r: don't define __global_pointer$

This symbol is supposed to be defined by the final executable link. The new
behavor matches GNU ld.

Added: 
    

Modified: 
    lld/ELF/Symbols.cpp
    lld/ELF/Symbols.h
    lld/ELF/Writer.cpp
    lld/test/ELF/riscv-gp.s

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp
index 248255248c1ee..c2375e07f248c 100644
--- a/lld/ELF/Symbols.cpp
+++ b/lld/ELF/Symbols.cpp
@@ -73,7 +73,6 @@ Defined *ElfSym::mipsGpDisp;
 Defined *ElfSym::mipsLocalGp;
 Defined *ElfSym::relaIpltStart;
 Defined *ElfSym::relaIpltEnd;
-Defined *ElfSym::riscvGlobalPointer;
 Defined *ElfSym::tlsModuleBase;
 SmallVector<SymbolAux, 0> elf::symAux;
 

diff  --git a/lld/ELF/Symbols.h b/lld/ELF/Symbols.h
index 8fa384428da97..5e3d52d4e5530 100644
--- a/lld/ELF/Symbols.h
+++ b/lld/ELF/Symbols.h
@@ -516,9 +516,6 @@ struct ElfSym {
   static Defined *relaIpltStart;
   static Defined *relaIpltEnd;
 
-  // __global_pointer$ for RISC-V.
-  static Defined *riscvGlobalPointer;
-
   // _TLS_MODULE_BASE_ on targets that support TLSDESC.
   static Defined *tlsModuleBase;
 };

diff  --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 51e4483a75575..1098813a1114d 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -1852,17 +1852,16 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
 
     // Define __rel[a]_iplt_{start,end} symbols if needed.
     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
-  // value/section does not matter but it has to be relative, so set its
-  // st_shndx arbitrarily to 1 (Out::elfHeader).
-  if (config->emachine == EM_RISCV && !config->shared) {
-    OutputSection *sec = findSection(".sdata");
-    ElfSym::riscvGlobalPointer =
-        addOptionalRegular("__global_pointer$", sec ? sec : Out::elfHeader,
-                           0x800, STV_DEFAULT);
+    // 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
+    // value/section does not matter but it has to be relative, so set its
+    // st_shndx arbitrarily to 1 (Out::elfHeader).
+    if (config->emachine == EM_RISCV && !config->shared) {
+      OutputSection *sec = findSection(".sdata");
+      addOptionalRegular("__global_pointer$", sec ? sec : Out::elfHeader, 0x800,
+                         STV_DEFAULT);
+    }
   }
 
   if (config->emachine == EM_386 || config->emachine == EM_X86_64) {

diff  --git a/lld/test/ELF/riscv-gp.s b/lld/test/ELF/riscv-gp.s
index 3ca29b05b2a66..fcc59ffa94813 100644
--- a/lld/test/ELF/riscv-gp.s
+++ b/lld/test/ELF/riscv-gp.s
@@ -24,6 +24,12 @@
 
 # ERR: error: relocation R_RISCV_PCREL_HI20 cannot be used against symbol '__global_pointer$'; recompile with -fPIC
 
+## -r mode does not define __global_pointer$.
+# RUN: ld.lld -r %t.64.o -o %t.64.ro
+# RUN: llvm-readelf -s %t.64.ro | FileCheck --check-prefix=RELOCATABLE %s
+
+# RELOCATABLE: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND __global_pointer$
+
 lla gp, __global_pointer$
 
 .section .sdata,"aw"


        


More information about the llvm-commits mailing list