Index: ELF/Config.h =================================================================== --- ELF/Config.h +++ 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; Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ 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 &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 " or "-e ". + 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. + StringRef Start = (Config->EMachine == EM_MIPS) ? "__start" : "_start"; + if (Symtab.find(Start)) + Config->EntrySym = Symtab.addUndefined(Start); + } + if (HasError) return; // There were duplicate symbols or incompatible files Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -1186,9 +1186,7 @@ template static typename ELFT::uint getEntryAddr() { if (Symbol *S = Config->EntrySym) return S->body()->getVA(); - if (Config->EntryAddr != uint64_t(-1)) - return Config->EntryAddr; - return 0; + return Config->EntryAddr; } template static uint8_t getELFEncoding() {