[ELF] Define __ehdr_start symbol

This symbol can be used by the program to examine its own headers.

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

llvm-svn: 279452
This commit is contained in:
Petr Hosek 2016-08-22 18:44:04 +00:00
parent 9cf5e4fc00
commit 4f7a5c3bb4
4 changed files with 32 additions and 0 deletions

View 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;

View 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;

11
lld/test/ELF/ehdr_start.s Normal file
View File

@ -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

View File

@ -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