[lld] r287836 - Set default entry point to .text if no entry point is found.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 23 14:41:00 PST 2016


Author: ruiu
Date: Wed Nov 23 16:41:00 2016
New Revision: 287836

URL: http://llvm.org/viewvc/llvm-project?rev=287836&view=rev
Log:
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.

Modified:
    lld/trunk/ELF/Writer.cpp
    lld/trunk/test/ELF/entry.s

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=287836&r1=287835&r2=287836&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Wed Nov 23 16:41:00 2016
@@ -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>:
   }
 }
 
-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>:
   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();

Modified: lld/trunk/test/ELF/entry.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/entry.s?rev=287836&r1=287835&r2=287836&view=diff
==============================================================================
--- lld/trunk/test/ELF/entry.s (original)
+++ lld/trunk/test/ELF/entry.s Wed Nov 23 16:41:00 2016
@@ -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 -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




More information about the llvm-commits mailing list