[lld] r249077 - Still define __init_array_(start|end) if there is no .init_array.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 1 14:22:26 PDT 2015


Author: rafael
Date: Thu Oct  1 16:22:26 2015
New Revision: 249077

URL: http://llvm.org/viewvc/llvm-project?rev=249077&view=rev
Log:
Still define __init_array_(start|end) if there is no .init_array.

This matches the behavior of other linkers and seems necessary to link
in some older systems.

Added:
    lld/trunk/test/elf2/init_array_missing.s
Modified:
    lld/trunk/ELF/SymbolTable.cpp
    lld/trunk/ELF/SymbolTable.h
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/SymbolTable.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SymbolTable.cpp?rev=249077&r1=249076&r2=249077&view=diff
==============================================================================
--- lld/trunk/ELF/SymbolTable.cpp (original)
+++ lld/trunk/ELF/SymbolTable.cpp Thu Oct  1 16:22:26 2015
@@ -81,6 +81,13 @@ void SymbolTable::addSyntheticSym(String
   resolve<ELFT>(Sym);
 }
 
+template <class ELFT> void SymbolTable::addIgnoredSym(StringRef Name) {
+  DefinedAbsolute<ELFT>::IgnoreUndef.setVisibility(STV_HIDDEN);
+  auto Sym = new (Alloc)
+      DefinedAbsolute<ELFT>(Name, DefinedAbsolute<ELFT>::IgnoreUndef);
+  resolve<ELFT>(Sym);
+}
+
 template <class ELFT> void SymbolTable::init(uint16_t EMachine) {
   Target.reset(createTarget(EMachine));
   if (Config->Shared)
@@ -102,10 +109,7 @@ template <class ELFT> void SymbolTable::
   // 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.
-  DefinedAbsolute<ELFT>::IgnoreUndef.setVisibility(STV_HIDDEN);
-  auto Got = new (Alloc) DefinedAbsolute<ELFT>(
-      "_GLOBAL_OFFSET_TABLE_", DefinedAbsolute<ELFT>::IgnoreUndef);
-  resolve<ELFT>(Got);
+  addIgnoredSym<ELFT>("_GLOBAL_OFFSET_TABLE_");
 }
 
 template <class ELFT> void SymbolTable::addELFFile(ELFFileBase *File) {
@@ -249,5 +253,10 @@ template void SymbolTable::addSyntheticS
                                            ELFFile<ELF64LE>::uintX_t);
 template void SymbolTable::addSyntheticSym(StringRef, OutputSection<ELF64BE> &,
                                            ELFFile<ELF64BE>::uintX_t);
+
+template void SymbolTable::addIgnoredSym<ELF32LE>(StringRef);
+template void SymbolTable::addIgnoredSym<ELF32BE>(StringRef);
+template void SymbolTable::addIgnoredSym<ELF64LE>(StringRef);
+template void SymbolTable::addIgnoredSym<ELF64BE>(StringRef);
 }
 }

Modified: lld/trunk/ELF/SymbolTable.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SymbolTable.h?rev=249077&r1=249076&r2=249077&view=diff
==============================================================================
--- lld/trunk/ELF/SymbolTable.h (original)
+++ lld/trunk/ELF/SymbolTable.h Thu Oct  1 16:22:26 2015
@@ -65,6 +65,8 @@ public:
   void addSyntheticSym(StringRef Name, OutputSection<ELFT> &Section,
                        typename llvm::object::ELFFile<ELFT>::uintX_t Value);
 
+  template <class ELFT> void addIgnoredSym(StringRef Name);
+
 private:
   Symbol *insert(SymbolBody *New);
   template <class ELFT> void addELFFile(ELFFileBase *File);

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=249077&r1=249076&r2=249077&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Thu Oct  1 16:22:26 2015
@@ -332,6 +332,9 @@ template <class ELFT> void Writer<ELFT>:
           Map.lookup({".init_array", SHT_INIT_ARRAY, SHF_WRITE | SHF_ALLOC})) {
     Symtab.addSyntheticSym<ELFT>("__init_array_start", *OS, 0);
     Symtab.addSyntheticSym<ELFT>("__init_array_end", *OS, OS->getSize());
+  } else {
+    Symtab.addIgnoredSym<ELFT>("__init_array_start");
+    Symtab.addIgnoredSym<ELFT>("__init_array_end");
   }
 
   // FIXME: Try to avoid the extra walk over all global symbols.

Added: lld/trunk/test/elf2/init_array_missing.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/init_array_missing.s?rev=249077&view=auto
==============================================================================
--- lld/trunk/test/elf2/init_array_missing.s (added)
+++ lld/trunk/test/elf2/init_array_missing.s Thu Oct  1 16:22:26 2015
@@ -0,0 +1,18 @@
+// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+// RUN: lld -flavor gnu2 %t -o %t2
+// RUN: llvm-objdump -d %t2 | FileCheck %s
+// REQUIRES: x86
+
+.globl _start
+_start:
+  call __init_array_start
+  call __init_array_end
+
+// With no .init_array section the symbols resolve to 0
+// 0 - (0x11000 + 5) = -69637
+// 0 - (0x11005 + 5) = -69642
+
+// CHECK: Disassembly of section .text:
+// CHECK-NEXT:  _start:
+// CHECK-NEXT:   11000:	e8 fb ef fe ff 	callq	-69637
+// CHECK-NEXT:   11005:	e8 f6 ef fe ff 	callq	-69642




More information about the llvm-commits mailing list