Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -1231,33 +1231,19 @@ Symtab->fetchLazy(Sym); } -template static bool shouldDemote(Symbol &Sym) { - // If all references to a DSO happen to be weak, the DSO is not added to - // DT_NEEDED. If that happens, we need to eliminate shared symbols created - // from the DSO. Otherwise, they become dangling references that point to a - // non-existent DSO. - if (auto *S = dyn_cast(&Sym)) - return !S->getFile().IsNeeded; - - // We are done processing archives, so lazy symbols that were used but not - // found can be converted to undefined. We could also just delete the other - // lazy symbols, but that seems to be more work than it is worth. - return Sym.isLazy() && Sym.IsUsedInRegularObj; -} - -// Some files, such as .so or files between -{start,end}-lib may be removed -// after their symbols are added to the symbol table. If that happens, we -// need to remove symbols that refer files that no longer exist, so that -// they won't appear in the symbol table of the output file. -// -// We remove symbols by demoting them to undefined symbol. -template static void demoteSymbols() { +// If all references to a DSO happen to be weak, the DSO is not added +// to DT_NEEDED. If that happens, we need to eliminate shared symbols +// created from the DSO. Otherwise, they become dangling references +// that point to a non-existent DSO. +template static void demoteSharedSymbols() { for (Symbol *Sym : Symtab->getSymbols()) { - if (shouldDemote(*Sym)) { - bool Used = Sym->Used; - replaceSymbol(Sym, nullptr, Sym->getName(), Sym->Binding, - Sym->StOther, Sym->Type); - Sym->Used = Used; + if (auto *S = dyn_cast(Sym)) { + if (!S->getFile().IsNeeded) { + bool Used = S->Used; + replaceSymbol(S, nullptr, S->getName(), STB_WEAK, S->StOther, + S->Type); + S->Used = Used; + } } } } @@ -1592,7 +1578,7 @@ decompressSections(); splitSections(); markLive(); - demoteSymbols(); + demoteSharedSymbols(); mergeSections(); if (Config->ICF != ICFLevel::None) { findKeepUniqueSections(Args); Index: ELF/Symbols.h =================================================================== --- ELF/Symbols.h +++ ELF/Symbols.h @@ -128,8 +128,12 @@ return SymbolKind == LazyArchiveKind || SymbolKind == LazyObjectKind; } - // True if this is an undefined weak symbol. - bool isUndefWeak() const { return isWeak() && isUndefined(); } + // True if this is an undefined weak symbol. This only works once + // all input files have been added. + bool isUndefWeak() const { + // See comment on lazy symbols for details. + return isWeak() && (isUndefined() || isLazy()); + } StringRef getName() const { if (NameSize == (uint32_t)-1) Index: ELF/Symbols.cpp =================================================================== --- ELF/Symbols.cpp +++ ELF/Symbols.cpp @@ -103,7 +103,8 @@ return 0; case Symbol::LazyArchiveKind: case Symbol::LazyObjectKind: - llvm_unreachable("lazy symbol reached writer"); + assert(Sym.IsUsedInRegularObj && "lazy symbol reached writer"); + return 0; } llvm_unreachable("invalid symbol kind"); }