[ELF] Remove redundant isExported computation

Commit 2a26292388fcab0c857c91b2d08074c33abd37e8 made `isExported`
accurate except a few linker-synthesized symbols in finalizeSections.
We can collect these linker-synthesized symbols into a vector
and avoid recomputation for other symbols.

This is reland of 1a4d6de1b532149b10522eae5dabce39e5f7c687 after
`isExported` has been made accurate by f10441ad003236ef3b9e5415a571d2be0c0ce5ce
This commit is contained in:
Fangrui Song 2025-01-27 22:39:40 -08:00
parent 5e43dd5bde
commit 085f7fb560
2 changed files with 11 additions and 3 deletions

View File

@ -619,6 +619,7 @@ struct Ctx : CommonLinkerContext {
};
ElfSym sym{};
std::unique_ptr<SymbolTable> symtab;
SmallVector<Symbol *, 0> synthesizedSymbols;
SmallVector<std::unique_ptr<MemoryBuffer>> memoryBuffers;
SmallVector<ELFFileBase *, 0> objectFiles;

View File

@ -149,6 +149,7 @@ static Defined *addOptionalRegular(Ctx &ctx, StringRef name, SectionBase *sec,
if (!s || s->isDefined() || s->isCommon())
return nullptr;
ctx.synthesizedSymbols.push_back(s);
s->resolve(ctx, Defined{ctx, ctx.internalFile, StringRef(), STB_GLOBAL,
stOther, STT_NOTYPE, val,
/*size=*/0, sec});
@ -295,13 +296,13 @@ static void demoteSymbolsAndComputeIsPreemptible(Ctx &ctx) {
sym->type)
.overwrite(*sym);
sym->versionId = VER_NDX_GLOBAL;
if (hasDynsym && sym->includeInDynsym(ctx))
sym->isExported = true;
}
}
if (hasDynsym) {
sym->isExported = sym->includeInDynsym(ctx);
if (hasDynsym)
sym->isPreemptible = sym->isExported && computeIsPreemptible(ctx, *sym);
}
}
}
@ -1838,6 +1839,12 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
}
}
// If the previous code block defines any non-hidden symbols (e.g.
// __global_pointer$), they may be exported.
if (ctx.hasDynsym)
for (Symbol *sym : ctx.synthesizedSymbols)
sym->isExported = sym->includeInDynsym(ctx);
demoteSymbolsAndComputeIsPreemptible(ctx);
if (ctx.arg.copyRelocs && ctx.arg.discard != DiscardPolicy::None)