[ELF] - Do not create reserved symbols in case of relocatable output.
authorGeorge Rimar <grimar@accesssoftek.com>
Tue, 15 Nov 2016 08:19:02 +0000 (08:19 +0000)
committerGeorge Rimar <grimar@accesssoftek.com>
Tue, 15 Nov 2016 08:19:02 +0000 (08:19 +0000)
This patch stops creating symbols like __ehdr_start,
_end/_etext_edata,__tls_get_addr when using -r.

This fixes PR30984.

Differential revision: https://reviews.llvm.org/D26600

llvm-svn: 286941

lld/ELF/Writer.cpp
lld/test/ELF/Inputs/relocatable-tls.s [new file with mode: 0644]
lld/test/ELF/edata-etext.s
lld/test/ELF/ehdr_start.s
lld/test/ELF/end.s
lld/test/ELF/relocatable-tls.s [new file with mode: 0644]

index 04548c1..15b4b32 100644 (file)
@@ -134,7 +134,9 @@ template <class ELFT> void elf::writeResult() { Writer<ELFT>().run(); }
 // The main function of the writer.
 template <class ELFT> void Writer<ELFT>::run() {
   createSyntheticSections();
-  addReservedSymbols();
+
+  if (!Config->Relocatable)
+    addReservedSymbols();
 
   if (Target->NeedsThunks)
     forEachRelSec(createThunks<ELFT>);
@@ -604,7 +606,7 @@ template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
 // The linker is expected to define some symbols depending on
 // the linking result. This function defines such symbols.
 template <class ELFT> void Writer<ELFT>::addReservedSymbols() {
-  if (Config->EMachine == EM_MIPS && !Config->Relocatable) {
+  if (Config->EMachine == EM_MIPS) {
     // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
     // so that it points to an absolute address which is relative to GOT.
     // See "Global Data Symbols" in Chapter 6 in the following document:
@@ -636,8 +638,7 @@ template <class ELFT> void Writer<ELFT>::addReservedSymbols() {
   // an undefined symbol in the .o files.
   // Given that the symbol is effectively unused, we just create a dummy
   // hidden one to avoid the undefined symbol error.
-  if (!Config->Relocatable)
-    Symtab<ELFT>::X->addIgnored("_GLOBAL_OFFSET_TABLE_");
+  Symtab<ELFT>::X->addIgnored("_GLOBAL_OFFSET_TABLE_");
 
   // __tls_get_addr is defined by the dynamic linker for dynamic ELFs. For
   // static linking the linker is required to optimize away any references to
diff --git a/lld/test/ELF/Inputs/relocatable-tls.s b/lld/test/ELF/Inputs/relocatable-tls.s
new file mode 100644 (file)
index 0000000..296beb4
--- /dev/null
@@ -0,0 +1 @@
+callq __tls_get_addr@PLT
index f00f3c3..3deda94 100644 (file)
 # CHECK-NEXT:  0000000000011001         *ABS* 00000000 _etext
 # CHECK-NEXT:  0000000000011000         .text 00000000 _start
 
+# RUN: ld.lld -r %t.o -o %t2
+# RUN: llvm-objdump -t %t2 | FileCheck %s --check-prefix=RELOCATABLE
+# RELOCATABLE:       0000000000000000 *UND* 00000000 _edata
+# RELOCATABLE-NEXT:  0000000000000000 *UND* 00000000 _end
+# RELOCATABLE-NEXT:  0000000000000000 *UND* 00000000 _etext
+
 .global _start,_end,_etext,_edata
 .text
 _start:
index a3ace7d..404c9a4 100644 (file)
@@ -9,3 +9,8 @@
 .global _start, __ehdr_start
 _start:
   .quad __ehdr_start
+
+# RUN: ld.lld -r %t.o -o %t.r
+# RUN: llvm-objdump -t %t.r | FileCheck %s --check-prefix=RELOCATABLE
+
+# RELOCATABLE: 0000000000000000 *UND* 00000000 __ehdr_start
index 18c77a3..0e32abd 100644 (file)
 // DEFAULT-NEXT:     Value: 0x12008
 // DEFAULT: ]
 
+// RUN: ld.lld -r %t.o -o %t2
+// RUN: llvm-objdump -t %t2 | FileCheck %s --check-prefix=RELOCATABLE
+// RELOCATABLE: 0000000000000000 *UND* 00000000 _end
+
 .global _start,_end
 .text
 _start:
diff --git a/lld/test/ELF/relocatable-tls.s b/lld/test/ELF/relocatable-tls.s
new file mode 100644 (file)
index 0000000..88d38c2
--- /dev/null
@@ -0,0 +1,15 @@
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \
+# RUN:   %S/Inputs/relocatable-tls.s -o %t2.o
+
+# RUN: ld.lld -r %t2.o -o %t3.r
+# RUN: llvm-objdump -t %t3.r | FileCheck --check-prefix=RELOCATABLE %s
+# RELOCATABLE: SYMBOL TABLE:
+# RELOCATABLE: 0000000000000000 *UND* 00000000 __tls_get_addr
+
+# RUN: ld.lld -shared %t2.o %t3.r -o %t4.out
+# RUN: llvm-objdump -t %t4.out | FileCheck --check-prefix=DSO %s
+# DSO: SYMBOL TABLE:
+# DSO: 0000000000000000 *UND* 00000000 __tls_get_addr
+
+callq __tls_get_addr@PLT