Set entry address to 0x0 if no _start symbol
Differential revision: https://reviews.llvm.org/D23925
llvm-svn: 280912
diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index f06fe1b..4d7cf3e2 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -127,7 +127,7 @@
ELFKind EKind = ELFNoneKind;
uint16_t DefaultSymbolVersion = llvm::ELF::VER_NDX_GLOBAL;
uint16_t EMachine = llvm::ELF::EM_NONE;
- uint64_t EntryAddr = -1;
+ uint64_t EntryAddr = 0;
uint64_t ImageBase;
uint64_t ZStackSize = -1;
unsigned LtoJobs;
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 22482eb..9e5d77a 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -575,11 +575,6 @@
Config->Mips64EL =
(Config->EMachine == EM_MIPS && Config->EKind == ELF64LEKind);
- // Add entry symbol. Note that AMDGPU binaries have no entry points.
- if (Config->Entry.empty() && !Config->Shared && !Config->Relocatable &&
- Config->EMachine != EM_AMDGPU)
- Config->Entry = (Config->EMachine == EM_MIPS) ? "__start" : "_start";
-
// Default output filename is "a.out" by the Unix tradition.
if (Config->OutputFile.empty())
Config->OutputFile = "a.out";
@@ -588,13 +583,6 @@
for (auto *Arg : Args.filtered(OPT_trace_symbol))
Symtab.trace(Arg->getValue());
- // Set either EntryAddr (if S is a number) or EntrySym (otherwise).
- if (!Config->Entry.empty()) {
- StringRef S = Config->Entry;
- if (S.getAsInteger(0, Config->EntryAddr))
- Config->EntrySym = Symtab.addUndefined(S);
- }
-
// Initialize Config->ImageBase.
if (auto *Arg = Args.getLastArg(OPT_image_base)) {
StringRef S = Arg->getValue();
@@ -606,8 +594,27 @@
Config->ImageBase = Config->Pic ? 0 : Target->DefaultImageBase;
}
+ // Add all files to the symbol table. After this, the symbol table
+ // contains all known names except a few linker-synthesized symbols.
for (std::unique_ptr<InputFile> &F : Files)
Symtab.addFile(std::move(F));
+
+ // Add the start symbol.
+ // It initializes either Config->Entry or Config->EntryAddr.
+ // Note that AMDGPU binaries have no entries.
+ if (!Config->Entry.empty()) {
+ // It is either "-e <addr>" or "-e <symbol>".
+ if (Config->Entry.getAsInteger(0, Config->EntryAddr))
+ Config->EntrySym = Symtab.addUndefined(Config->Entry);
+ } else if (!Config->Shared && !Config->Relocatable &&
+ Config->EMachine != EM_AMDGPU) {
+ // -e was not specified. Use the default start symbol name
+ // if it is resolvable.
+ Config->Entry = (Config->EMachine == EM_MIPS) ? "__start" : "_start";
+ if (Symtab.find(Config->Entry))
+ Config->EntrySym = Symtab.addUndefined(Config->Entry);
+ }
+
if (HasError)
return; // There were duplicate symbols or incompatible files
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index b1c08d1..ba58330 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -1186,9 +1186,7 @@
template <class ELFT> static typename ELFT::uint getEntryAddr() {
if (Symbol *S = Config->EntrySym)
return S->body()->getVA<ELFT>();
- if (Config->EntryAddr != uint64_t(-1))
- return Config->EntryAddr;
- return 0;
+ return Config->EntryAddr;
}
template <class ELFT> static uint8_t getELFEncoding() {