Skip to content

Commit

Permalink
[ELF] Move demoteSymbols to Writer.cpp. NFC
Browse files Browse the repository at this point in the history
History of demoteSharedSymbols:

* https://reviews.llvm.org/D45536 demotes SharedSymbol
* https://reviews.llvm.org/D111365 demotes lazy symbols
* The pending llvm#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.
  • Loading branch information
MaskRay committed Oct 17, 2023
1 parent d4088e7 commit e9b9a1d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
19 changes: 0 additions & 19 deletions lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<SharedSymbol>(sym);
if (!(s && !cast<SharedFile>(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) {
Expand Down Expand Up @@ -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.
Expand Down
28 changes: 23 additions & 5 deletions lld/ELF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<SharedSymbol>(sym);
if (!(s && !cast<SharedFile>(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
Expand Down Expand Up @@ -1935,12 +1952,13 @@ template <class ELFT> void Writer<ELFT>::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
Expand Down

0 comments on commit e9b9a1d

Please sign in to comment.