diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index d082463d34e576..5f88389a584082 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -2248,24 +2248,6 @@ static void replaceCommonSymbols() { } } -// If all references to a DSO happen to be weak, the DSO is not added to -// DT_NEEDED. If that happens, replace ShardSymbol with Undefined to avoid -// dangling references to an unneeded DSO. Use a weak binding to avoid -// --no-allow-shlib-undefined diagnostics. Similarly, demote lazy symbols. -static void demoteSharedAndLazySymbols() { - llvm::TimeTraceScope timeScope("Demote shared and lazy symbols"); - for (Symbol *sym : symtab.getSymbols()) { - auto *s = dyn_cast(sym); - if (!(s && !cast(s->file)->isNeeded) && !sym->isLazy()) - continue; - - uint8_t binding = sym->isLazy() ? sym->binding : uint8_t(STB_WEAK); - Undefined(nullptr, sym->getName(), binding, sym->stOther, sym->type) - .overwrite(*sym); - sym->versionId = VER_NDX_GLOBAL; - } -} - // The section referred to by `s` is considered address-significant. Set the // keepUnique flag on the section if appropriate. static void markAddrsig(Symbol *s) { @@ -3023,7 +3005,6 @@ void LinkerDriver::link(opt::InputArgList &args) { // Garbage collection and removal of shared symbols from unused shared objects. invokeELFT(markLive,); - demoteSharedAndLazySymbols(); // Make copies of any input sections that need to be copied into each // partition. diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 5077c972658a14..5fc4412aa49f13 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -251,6 +251,23 @@ void elf::addReservedSymbols() { ElfSym::edata2 = add("_edata", -1); } +// If all references to a DSO happen to be weak, the DSO is not added to +// DT_NEEDED. If that happens, replace ShardSymbol with Undefined to avoid +// dangling references to an unneeded DSO. Use a weak binding to avoid +// --no-allow-shlib-undefined diagnostics. Similarly, demote lazy symbols. +static void demoteSymbols() { + llvm::TimeTraceScope timeScope("Demote symbols"); + for (Symbol *sym : symtab.getSymbols()) { + auto *s = dyn_cast(sym); + if (!(s && !cast(s->file)->isNeeded) && !sym->isLazy()) + continue; + uint8_t binding = sym->isLazy() ? sym->binding : uint8_t(STB_WEAK); + Undefined(nullptr, sym->getName(), binding, sym->stOther, sym->type) + .overwrite(*sym); + sym->versionId = VER_NDX_GLOBAL; + } +} + // Fully static executables don't support MTE globals at this point in time, as // we currently rely on: // - A dynamic loader to process relocations, and @@ -1935,12 +1952,13 @@ template void Writer::finalizeSections() { for (Partition &part : partitions) finalizeSynthetic(part.ehFrame.get()); } + } - if (config->hasDynSymTab) { - parallelForEach(symtab.getSymbols(), [](Symbol *sym) { - sym->isPreemptible = computeIsPreemptible(*sym); - }); - } + demoteSymbols(); + if (config->hasDynSymTab) { + parallelForEach(symtab.getSymbols(), [](Symbol *sym) { + sym->isPreemptible = computeIsPreemptible(*sym); + }); } // Change values of linker-script-defined symbols from placeholders (assigned