Set default entry point to .text if no entry point is found.

Previously, if a symbol specified by -e or ENTRY() is not found,
we didn't set entry point address. That is incompatible with GNU
because GNU linkers set the first address of .text to entry.
This patch implement that behavior.

llvm-svn: 287836
This commit is contained in:
Rui Ueyama 2016-11-23 22:41:00 +00:00
parent 3ce6a545c7
commit 2eda6d1633
2 changed files with 52 additions and 16 deletions

View File

@ -82,6 +82,7 @@ private:
void addRelIpltSymbols();
void addStartEndSymbols();
void addStartStopSymbols(OutputSectionBase *Sec);
uintX_t getEntryAddr();
OutputSectionBase *findSection(StringRef Name);
std::vector<Phdr> Phdrs;
@ -1360,12 +1361,30 @@ template <class ELFT> void Writer<ELFT>::setPhdrs() {
}
}
template <class ELFT> static typename ELFT::uint getEntryAddr() {
// The entry point address is chosen in the following ways.
//
// 1. the '-e' entry command-line option;
// 2. the ENTRY(symbol) command in a linker control script;
// 3. the value of the symbol start, if present;
// 4. the address of the first byte of the .text section, if present;
// 5. the address 0.
template <class ELFT> typename ELFT::uint Writer<ELFT>::getEntryAddr() {
// Case 1, 2 or 3
if (Config->Entry.empty())
return Config->EntryAddr;
if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Entry))
return B->getVA<ELFT>();
warn("entry symbol " + Config->Entry + " not found, assuming 0");
// Case 4
if (OutputSectionBase *Sec = findSection(".text")) {
warn("cannot find entry symbol " + Config->Entry + "; defaulting to 0x" +
utohexstr(Sec->Addr));
return Sec->Addr;
}
// Case 5
warn("cannot find entry symbol " + Config->Entry +
"; not setting start address");
return 0;
}
@ -1438,7 +1457,7 @@ template <class ELFT> void Writer<ELFT>::writeHeader() {
EHdr->e_type = getELFType();
EHdr->e_machine = Config->EMachine;
EHdr->e_version = EV_CURRENT;
EHdr->e_entry = getEntryAddr<ELFT>();
EHdr->e_entry = getEntryAddr();
EHdr->e_shoff = SectionHeaderOff;
EHdr->e_ehsize = sizeof(Elf_Ehdr);
EHdr->e_phnum = Phdrs.size();

View File

@ -1,33 +1,50 @@
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1
# RUN: ld.lld -e foobar %t1 -o %t2 2>&1 | FileCheck -check-prefix=WARN %s
# RUN: llvm-readobj -file-headers %t2 | FileCheck -check-prefix=NOENTRY %s
# RUN: ld.lld %t1 -o %t2 2>&1 | FileCheck -check-prefix=WARN2 %s
# RUN: ld.lld -e foobar %t1 -o %t2 2>&1 | FileCheck -check-prefix=WARN1 %s
# RUN: llvm-readobj -file-headers %t2 | FileCheck -check-prefix=TEXT %s
# WARN1: warning: cannot find entry symbol foobar; defaulting to 0x201000
# TEXT: Entry: 0x201000
# RUN: ld.lld %t1 -o %t2 2>&1 | FileCheck -check-prefix=WARN2 %s
# WARN2: warning: cannot find entry symbol _start; defaulting to 0x201000
# RUN: ld.lld -shared -e foobar %t1 -o %t2 2>&1 | FileCheck -check-prefix=WARN3 %s
# WARN3: warning: cannot find entry symbol foobar; defaulting to 0x1000
# RUN: ld.lld -shared -e foobar %t1 -o %t2 2>&1 | FileCheck -check-prefix=WARN %s
# RUN: ld.lld -shared --fatal-warnings -e entry %t1 -o %t2
# RUN: ld.lld -shared --fatal-warnings %t1 -o %t2
# RUN: echo .data > %t.s
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux -n %t.s -o %t3
# RUN: ld.lld %t3 -o %t4 2>&1 | FileCheck -check-prefix=WARN4 %s
# RUN: llvm-readobj -file-headers %t4 | FileCheck -check-prefix=NOENTRY %s
# WARN4: cannot find entry symbol _start; not setting start address
# NOENTRY: Entry: 0x0
# RUN: ld.lld %t1 -o %t2 -e entry
# RUN: llvm-readobj -file-headers %t2 | FileCheck -check-prefix=SYM %s
# SYM: Entry: 0x201008
# RUN: ld.lld %t1 --fatal-warnings -shared -o %t2 -e entry
# RUN: llvm-readobj -file-headers %t2 | FileCheck -check-prefix=DSO %s
# DSO: Entry: 0x1008
# RUN: ld.lld %t1 -o %t2 --entry=4096
# RUN: llvm-readobj -file-headers %t2 | FileCheck -check-prefix=DEC %s
# DEC: Entry: 0x1000
# RUN: ld.lld %t1 -o %t2 --entry 0xcafe
# RUN: llvm-readobj -file-headers %t2 | FileCheck -check-prefix=HEX %s
# HEX: Entry: 0xCAFE
# RUN: ld.lld %t1 -o %t2 -e 0777
# RUN: llvm-readobj -file-headers %t2 | FileCheck -check-prefix=OCT %s
# WARN: warning: entry symbol foobar not found, assuming 0
# WARN2: warning: entry symbol _start not found, assuming 0
# NOENTRY: Entry: 0x0
# SYM: Entry: 0x201000
# DSO: Entry: 0x1000
# DEC: Entry: 0x1000
# HEX: Entry: 0xCAFE
# OCT: Entry: 0x1FF
.globl entry
.text
.quad 0
entry:
ret