Skip to content

Commit e9b9a1d

Browse files
committed
[ELF] Move demoteSymbols to Writer.cpp. NFC
History of demoteSharedSymbols: * https://reviews.llvm.org/D45536 demotes SharedSymbol * https://reviews.llvm.org/D111365 demotes lazy symbols * The pending #69295 will demote symbols defined in discarded sections The pass is placed after markLive just to be clear that it needs `isNeeded` information computed by markLive. The remaining passes in Driver.cpp do not use symbol information. Move the pass to Writer.cpp to be closer to other symbol-related passes.
1 parent d4088e7 commit e9b9a1d

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

lld/ELF/Driver.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2248,24 +2248,6 @@ static void replaceCommonSymbols() {
22482248
}
22492249
}
22502250

2251-
// If all references to a DSO happen to be weak, the DSO is not added to
2252-
// DT_NEEDED. If that happens, replace ShardSymbol with Undefined to avoid
2253-
// dangling references to an unneeded DSO. Use a weak binding to avoid
2254-
// --no-allow-shlib-undefined diagnostics. Similarly, demote lazy symbols.
2255-
static void demoteSharedAndLazySymbols() {
2256-
llvm::TimeTraceScope timeScope("Demote shared and lazy symbols");
2257-
for (Symbol *sym : symtab.getSymbols()) {
2258-
auto *s = dyn_cast<SharedSymbol>(sym);
2259-
if (!(s && !cast<SharedFile>(s->file)->isNeeded) && !sym->isLazy())
2260-
continue;
2261-
2262-
uint8_t binding = sym->isLazy() ? sym->binding : uint8_t(STB_WEAK);
2263-
Undefined(nullptr, sym->getName(), binding, sym->stOther, sym->type)
2264-
.overwrite(*sym);
2265-
sym->versionId = VER_NDX_GLOBAL;
2266-
}
2267-
}
2268-
22692251
// The section referred to by `s` is considered address-significant. Set the
22702252
// keepUnique flag on the section if appropriate.
22712253
static void markAddrsig(Symbol *s) {
@@ -3023,7 +3005,6 @@ void LinkerDriver::link(opt::InputArgList &args) {
30233005

30243006
// Garbage collection and removal of shared symbols from unused shared objects.
30253007
invokeELFT(markLive,);
3026-
demoteSharedAndLazySymbols();
30273008

30283009
// Make copies of any input sections that need to be copied into each
30293010
// partition.

lld/ELF/Writer.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,23 @@ void elf::addReservedSymbols() {
251251
ElfSym::edata2 = add("_edata", -1);
252252
}
253253

254+
// If all references to a DSO happen to be weak, the DSO is not added to
255+
// DT_NEEDED. If that happens, replace ShardSymbol with Undefined to avoid
256+
// dangling references to an unneeded DSO. Use a weak binding to avoid
257+
// --no-allow-shlib-undefined diagnostics. Similarly, demote lazy symbols.
258+
static void demoteSymbols() {
259+
llvm::TimeTraceScope timeScope("Demote symbols");
260+
for (Symbol *sym : symtab.getSymbols()) {
261+
auto *s = dyn_cast<SharedSymbol>(sym);
262+
if (!(s && !cast<SharedFile>(s->file)->isNeeded) && !sym->isLazy())
263+
continue;
264+
uint8_t binding = sym->isLazy() ? sym->binding : uint8_t(STB_WEAK);
265+
Undefined(nullptr, sym->getName(), binding, sym->stOther, sym->type)
266+
.overwrite(*sym);
267+
sym->versionId = VER_NDX_GLOBAL;
268+
}
269+
}
270+
254271
// Fully static executables don't support MTE globals at this point in time, as
255272
// we currently rely on:
256273
// - A dynamic loader to process relocations, and
@@ -1935,12 +1952,13 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
19351952
for (Partition &part : partitions)
19361953
finalizeSynthetic(part.ehFrame.get());
19371954
}
1955+
}
19381956

1939-
if (config->hasDynSymTab) {
1940-
parallelForEach(symtab.getSymbols(), [](Symbol *sym) {
1941-
sym->isPreemptible = computeIsPreemptible(*sym);
1942-
});
1943-
}
1957+
demoteSymbols();
1958+
if (config->hasDynSymTab) {
1959+
parallelForEach(symtab.getSymbols(), [](Symbol *sym) {
1960+
sym->isPreemptible = computeIsPreemptible(*sym);
1961+
});
19441962
}
19451963

19461964
// Change values of linker-script-defined symbols from placeholders (assigned

0 commit comments

Comments
 (0)