Skip to content

Commit 1a4d6de

Browse files
committed
[ELF] Remove redundant isExported computation
Commit 2a26292 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.
1 parent 2a26292 commit 1a4d6de

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lld/ELF/Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ struct Ctx : CommonLinkerContext {
619619
};
620620
ElfSym sym{};
621621
std::unique_ptr<SymbolTable> symtab;
622+
SmallVector<Symbol *, 0> synthesizedSymbols;
622623

623624
SmallVector<std::unique_ptr<MemoryBuffer>> memoryBuffers;
624625
SmallVector<ELFFileBase *, 0> objectFiles;

lld/ELF/Writer.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ static Defined *addOptionalRegular(Ctx &ctx, StringRef name, SectionBase *sec,
149149
if (!s || s->isDefined() || s->isCommon())
150150
return nullptr;
151151

152+
ctx.synthesizedSymbols.push_back(s);
152153
s->resolve(ctx, Defined{ctx, ctx.internalFile, StringRef(), STB_GLOBAL,
153154
stOther, STT_NOTYPE, val,
154155
/*size=*/0, sec});
@@ -282,6 +283,7 @@ static void demoteDefined(Defined &sym, DenseMap<SectionBase *, size_t> &map) {
282283
static void demoteSymbolsAndComputeIsPreemptible(Ctx &ctx) {
283284
llvm::TimeTraceScope timeScope("Demote symbols");
284285
DenseMap<InputFile *, DenseMap<SectionBase *, size_t>> sectionIndexMap;
286+
bool hasDynSymTab = ctx.arg.hasDynSymTab;
285287
for (Symbol *sym : ctx.symtab->getSymbols()) {
286288
if (auto *d = dyn_cast<Defined>(sym)) {
287289
if (d->section && !d->section->isLive())
@@ -294,11 +296,12 @@ static void demoteSymbolsAndComputeIsPreemptible(Ctx &ctx) {
294296
sym->type)
295297
.overwrite(*sym);
296298
sym->versionId = VER_NDX_GLOBAL;
299+
if (sym->includeInDynsym(ctx))
300+
sym->isExported = true;
297301
}
298302
}
299303

300-
sym->isExported = sym->includeInDynsym(ctx);
301-
if (ctx.arg.hasDynSymTab)
304+
if (hasDynSymTab)
302305
sym->isPreemptible = sym->isExported && computeIsPreemptible(ctx, *sym);
303306
}
304307
}
@@ -1846,6 +1849,11 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
18461849
}
18471850
}
18481851

1852+
// If the previous code block defines any non-hidden symbols (e.g.
1853+
// __global_pointer$), they may be exported.
1854+
for (Symbol *sym : ctx.synthesizedSymbols)
1855+
sym->isExported = sym->includeInDynsym(ctx);
1856+
18491857
demoteSymbolsAndComputeIsPreemptible(ctx);
18501858

18511859
if (ctx.arg.copyRelocs && ctx.arg.discard != DiscardPolicy::None)

0 commit comments

Comments
 (0)