[ELF] Define __ehdr_start symbol
authorPetr Hosek <phosek@chromium.org>
Mon, 22 Aug 2016 18:44:04 +0000 (18:44 +0000)
committerPetr Hosek <phosek@chromium.org>
Mon, 22 Aug 2016 18:44:04 +0000 (18:44 +0000)
This symbol can be used by the program to examine its own headers.

Differential Revision: https://reviews.llvm.org/D23750

llvm-svn: 279452

lld/ELF/Symbols.h
lld/ELF/Writer.cpp
lld/test/ELF/ehdr_start.s [new file with mode: 0644]
lld/test/ELF/linkerscript/linkerscript-ehdr_start.s [new file with mode: 0644]

index a1170f4..dfdcbe4 100644 (file)
@@ -366,6 +366,9 @@ public:
 // Some linker-generated symbols need to be created as
 // DefinedRegular symbols.
 template <class ELFT> struct ElfSym {
+  // The content for __ehdr_start symbol.
+  static DefinedRegular<ELFT> *EhdrStart;
+
   // The content for _etext and etext symbols.
   static DefinedRegular<ELFT> *Etext;
   static DefinedRegular<ELFT> *Etext2;
@@ -382,6 +385,7 @@ template <class ELFT> struct ElfSym {
   static SymbolBody *MipsGpDisp;
 };
 
+template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::EhdrStart;
 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext;
 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Etext2;
 template <class ELFT> DefinedRegular<ELFT> *ElfSym<ELFT>::Edata;
index 3b20bbe..a8c6f82 100644 (file)
@@ -598,6 +598,8 @@ template <class ELFT> void Writer<ELFT>::addReservedSymbols() {
   if (ScriptConfig->HasContents)
     return;
 
+  ElfSym<ELFT>::EhdrStart = Symtab<ELFT>::X->addIgnored("__ehdr_start");
+
   auto Define = [this](StringRef S, DefinedRegular<ELFT> *&Sym1,
                        DefinedRegular<ELFT> *&Sym2) {
     Sym1 = Symtab<ELFT>::X->addIgnored(S, STV_DEFAULT);
@@ -1180,6 +1182,10 @@ static uint16_t getELFType() {
 // to each section. This function fixes some predefined absolute
 // symbol values that depend on section address and size.
 template <class ELFT> void Writer<ELFT>::fixAbsoluteSymbols() {
+  // __ehdr_start is the location of program headers.
+  if (ElfSym<ELFT>::EhdrStart)
+    ElfSym<ELFT>::EhdrStart->Value = Out<ELFT>::ProgramHeaders->getVA();
+
   auto Set = [](DefinedRegular<ELFT> *S1, DefinedRegular<ELFT> *S2, uintX_t V) {
     if (S1)
       S1->Value = V;
diff --git a/lld/test/ELF/ehdr_start.s b/lld/test/ELF/ehdr_start.s
new file mode 100644 (file)
index 0000000..e605df5
--- /dev/null
@@ -0,0 +1,11 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: ld.lld %t.o -o %t
+# RUN: llvm-objdump -t %t | FileCheck %s
+# CHECK: 0000000000010040 *ABS* 00000000 .hidden __ehdr_start
+
+.text
+.global _start, __ehdr_start
+_start:
+       .quad __ehdr_start
diff --git a/lld/test/ELF/linkerscript/linkerscript-ehdr_start.s b/lld/test/ELF/linkerscript/linkerscript-ehdr_start.s
new file mode 100644 (file)
index 0000000..ef3dc59
--- /dev/null
@@ -0,0 +1,11 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: echo "SECTIONS { }" > %t.script
+# RUN: not ld.lld %t.o -script %t.script -o %t 2>&1 | FileCheck %s
+# CHECK: undefined symbol: __ehdr_start
+
+.text
+.global _start, __ehdr_start
+_start:
+       .quad __ehdr_start