Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -1034,8 +1034,7 @@ // If an entry symbol is in a static archive, pull out that file now // to complete the symbol table. After this, no new names except a // few linker-synthesized ones will be added to the symbol table. - if (Symtab->find(Config->Entry)) - Symtab->addUndefined(Config->Entry); + Symtab->fetchIfLazy(Config->Entry); // Return if there were name resolution errors. if (ErrorCount) Index: ELF/SymbolTable.h =================================================================== --- ELF/SymbolTable.h +++ ELF/SymbolTable.h @@ -82,6 +82,7 @@ uint8_t Visibility, bool CanOmitFromDynSym, InputFile *File); + template void fetchIfLazy(StringRef Name); template void scanUndefinedFlags(); template void scanShlibUndefined(); void scanVersionScript(); Index: ELF/SymbolTable.cpp =================================================================== --- ELF/SymbolTable.cpp +++ ELF/SymbolTable.cpp @@ -582,17 +582,22 @@ addFile(F); } +// If we already saw this symbol, force loading its file. +template void SymbolTable::fetchIfLazy(StringRef Name) { + if (SymbolBody *B = find(Name)) { + // Mark the symbol not to be eliminated by LTO + // even if it is a bitcode symbol. + B->symbol()->IsUsedInRegularObj = true; + if (auto *L = dyn_cast_or_null(B)) + if (InputFile *File = L->fetch()) + addFile(File); + } +} + // Process undefined (-u) flags by loading lazy symbols named by those flags. template void SymbolTable::scanUndefinedFlags() { for (StringRef S : Config->Undefined) - if (SymbolBody *B = find(S)) { - // Mark the symbol not to be eliminated by LTO - // even if it is a bitcode symbol. - B->symbol()->IsUsedInRegularObj = true; - if (auto *L = dyn_cast_or_null(B)) - if (InputFile *File = L->fetch()) - addFile(File); - } + fetchIfLazy(S); } // This function takes care of the case in which shared libraries depend on @@ -875,6 +880,11 @@ const typename ELF64BE::Sym &, const typename ELF64BE::Verdef *); +template void SymbolTable::fetchIfLazy(StringRef); +template void SymbolTable::fetchIfLazy(StringRef); +template void SymbolTable::fetchIfLazy(StringRef); +template void SymbolTable::fetchIfLazy(StringRef); + template void SymbolTable::scanUndefinedFlags(); template void SymbolTable::scanUndefinedFlags(); template void SymbolTable::scanUndefinedFlags(); Index: test/ELF/weak-entry.s =================================================================== --- /dev/null +++ test/ELF/weak-entry.s @@ -0,0 +1,13 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t +# RUN: ld.lld %t -o %tout +# RUN: llvm-nm %tout | FileCheck %s + +# CHECK: w _start +# CHECK-NEXT: T foo + +.global foo +.weak _start +.text +foo: + .dc.a _start