[ELF] Avoid promoting an undefined weak entry symbol to global.

Without this patch, lld emits "error: undefined symbol: _start"
if it encountered only weak references to that symbol.

llvm-svn: 314790
This commit is contained in:
Igor Kudrin 2017-10-03 12:23:46 +00:00
parent 66b0ad91f6
commit fb7f8befec
4 changed files with 33 additions and 10 deletions

View File

@ -1034,8 +1034,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
// 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<ELFT>(Config->Entry);
Symtab->fetchIfLazy<ELFT>(Config->Entry);
// Return if there were name resolution errors.
if (ErrorCount)

View File

@ -581,17 +581,22 @@ void SymbolTable::addLazyObject(StringRef Name, LazyObjFile &Obj) {
addFile<ELFT>(F);
}
// If we already saw this symbol, force loading its file.
template <class ELFT> 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<Lazy>(B))
if (InputFile *File = L->fetch())
addFile<ELFT>(File);
}
}
// Process undefined (-u) flags by loading lazy symbols named by those flags.
template <class ELFT> 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<Lazy>(B))
if (InputFile *File = L->fetch())
addFile<ELFT>(File);
}
fetchIfLazy<ELFT>(S);
}
// This function takes care of the case in which shared libraries depend on
@ -874,6 +879,11 @@ template void SymbolTable::addShared<ELF64BE>(StringRef, SharedFile<ELF64BE> *,
const typename ELF64BE::Sym &,
const typename ELF64BE::Verdef *);
template void SymbolTable::fetchIfLazy<ELF32LE>(StringRef);
template void SymbolTable::fetchIfLazy<ELF32BE>(StringRef);
template void SymbolTable::fetchIfLazy<ELF64LE>(StringRef);
template void SymbolTable::fetchIfLazy<ELF64BE>(StringRef);
template void SymbolTable::scanUndefinedFlags<ELF32LE>();
template void SymbolTable::scanUndefinedFlags<ELF32BE>();
template void SymbolTable::scanUndefinedFlags<ELF64LE>();

View File

@ -84,6 +84,7 @@ public:
uint8_t Visibility, bool CanOmitFromDynSym,
InputFile *File);
template <class ELFT> void fetchIfLazy(StringRef Name);
template <class ELFT> void scanUndefinedFlags();
template <class ELFT> void scanShlibUndefined();
void scanVersionScript();

13
lld/test/ELF/weak-entry.s Normal file
View File

@ -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