From 2eda6d16330ed1d27783544c7ae7595f65cc4189 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Wed, 23 Nov 2016 22:41:00 +0000 Subject: [PATCH] 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 --- lld/ELF/Writer.cpp | 25 ++++++++++++++++++++++--- lld/test/ELF/entry.s | 43 ++++++++++++++++++++++++++++++------------- 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 39ece191d8c9..5329514cdef9 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -82,6 +82,7 @@ private: void addRelIpltSymbols(); void addStartEndSymbols(); void addStartStopSymbols(OutputSectionBase *Sec); + uintX_t getEntryAddr(); OutputSectionBase *findSection(StringRef Name); std::vector Phdrs; @@ -1360,12 +1361,30 @@ template void Writer::setPhdrs() { } } -template 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 typename ELFT::uint Writer::getEntryAddr() { + // Case 1, 2 or 3 if (Config->Entry.empty()) return Config->EntryAddr; if (SymbolBody *B = Symtab::X->find(Config->Entry)) return B->getVA(); - 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 void Writer::writeHeader() { EHdr->e_type = getELFType(); EHdr->e_machine = Config->EMachine; EHdr->e_version = EV_CURRENT; - EHdr->e_entry = getEntryAddr(); + EHdr->e_entry = getEntryAddr(); EHdr->e_shoff = SectionHeaderOff; EHdr->e_ehsize = sizeof(Elf_Ehdr); EHdr->e_phnum = Phdrs.size(); diff --git a/lld/test/ELF/entry.s b/lld/test/ELF/entry.s index 34a55ababffc..cf08073025cb 100644 --- a/lld/test/ELF/entry.s +++ b/lld/test/ELF/entry.s @@ -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