Index: lld/ELF/InputFiles.h =================================================================== --- lld/ELF/InputFiles.h +++ lld/ELF/InputFiles.h @@ -320,9 +320,7 @@ std::map VerdefMap; // Used for --as-needed - bool AsNeeded = false; - bool IsUsed = false; - bool isNeeded() const { return !AsNeeded || IsUsed; } + bool IsNeeded; }; class BinaryFile : public InputFile { Index: lld/ELF/InputFiles.cpp =================================================================== --- lld/ELF/InputFiles.cpp +++ lld/ELF/InputFiles.cpp @@ -655,7 +655,7 @@ template SharedFile::SharedFile(MemoryBufferRef M, StringRef DefaultSoName) : ELFFileBase(Base::SharedKind, M), SoName(DefaultSoName), - AsNeeded(Config->AsNeeded) {} + IsNeeded(!Config->AsNeeded) {} // Partially parse the shared object file so that we can call // getSoName on this object. Index: lld/ELF/MarkLive.cpp =================================================================== --- lld/ELF/MarkLive.cpp +++ lld/ELF/MarkLive.cpp @@ -64,6 +64,10 @@ std::function Fn) { Symbol &B = Sec.getFile()->getRelocTargetSym(Rel); + if (auto *Sym = dyn_cast(&B)) + if (!Sym->isWeak()) + cast>(Sym->File)->IsNeeded = true; + if (auto *D = dyn_cast(&B)) { if (!D->Section) return; @@ -212,6 +216,10 @@ }; auto MarkSymbol = [&](Symbol *Sym) { + if (auto *S = dyn_cast_or_null(Sym)) + if (!S->isWeak()) + cast>(S->File)->IsNeeded = true; + if (auto *D = dyn_cast_or_null(Sym)) if (auto *IS = cast_or_null(D->Section)) Enqueue(IS, D->Value); Index: lld/ELF/SymbolTable.cpp =================================================================== --- lld/ELF/SymbolTable.cpp +++ lld/ELF/SymbolTable.cpp @@ -303,8 +303,9 @@ if (Binding != STB_WEAK) { if (!S->isDefined()) S->Binding = Binding; - if (auto *SS = dyn_cast(S)) - SS->getFile()->IsUsed = true; + if (!Config->GcSections) + if (auto *SS = dyn_cast(S)) + SS->getFile()->IsNeeded = true; } if (auto *L = dyn_cast(S)) { // An undefined weak will not fetch archive members. See comment on Lazy in @@ -500,8 +501,8 @@ Sym.st_value, Sym.st_size, Alignment, Verdef); if (!WasInserted) { S->Binding = Binding; - if (!S->isWeak()) - File->IsUsed = true; + if (!Config->GcSections && !S->isWeak()) + File->IsNeeded = true; } } } Index: lld/ELF/SyntheticSections.cpp =================================================================== --- lld/ELF/SyntheticSections.cpp +++ lld/ELF/SyntheticSections.cpp @@ -997,7 +997,7 @@ InX::DynStrTab->addString(Config->Rpath)}); for (InputFile *File : SharedFiles) { SharedFile *F = cast>(File); - if (F->isNeeded()) + if (F->IsNeeded) add({DT_NEEDED, InX::DynStrTab->addString(F->SoName)}); } if (!Config->SoName.empty()) Index: lld/ELF/Writer.cpp =================================================================== --- lld/ELF/Writer.cpp +++ lld/ELF/Writer.cpp @@ -1275,7 +1275,7 @@ if (InX::DynSymTab && Sym->includeInDynsym()) { InX::DynSymTab->addSymbol(Sym); if (auto *SS = dyn_cast(Sym)) - if (cast>(Sym->File)->isNeeded()) + if (cast>(Sym->File)->IsNeeded) In::VerNeed->addSymbol(SS); } }