Skip to content

Commit

Permalink
[ELF] Move partitions into ctx. NFC
Browse files Browse the repository at this point in the history
Ctx was introduced in March 2022 as a more suitable place for such
singletons.
  • Loading branch information
MaskRay committed Sep 15, 2024
1 parent 0098cee commit 40e8e4d
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 53 deletions.
1 change: 1 addition & 0 deletions lld/ELF/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ struct Ctx {
};
OutSections out;
SmallVector<OutputSection *, 0> outputSections;
std::vector<Partition> partitions;

// Some linker-generated symbols need to be created as
// Defined symbols.
Expand Down
19 changes: 9 additions & 10 deletions lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ void Ctx::reset() {
tlsPhdr = nullptr;
out = OutSections{};
outputSections.clear();
partitions.clear();

sym = ElfSym{};

Expand Down Expand Up @@ -148,13 +149,11 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
ctx->e.cleanupCallback = []() {
elf::ctx.reset();
elf::ctx.partitions.emplace_back();
symtab = SymbolTable();

in.reset();

partitions.clear();
partitions.emplace_back();

SharedFile::vernauxNum = 0;
};
ctx->e.logName = args::getFilenameWithoutExe(args[0]);
Expand All @@ -167,8 +166,8 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
elf::ctx.script = &script;
elf::ctx.symAux.emplace_back();

partitions.clear();
partitions.emplace_back();
elf::ctx.partitions.clear();
elf::ctx.partitions.emplace_back();

config->progName = args[0];

Expand Down Expand Up @@ -2448,7 +2447,7 @@ static void readSymbolPartitionSection(InputSectionBase *s) {
return;

StringRef partName = reinterpret_cast<const char *>(s->content().data());
for (Partition &part : partitions) {
for (Partition &part : ctx.partitions) {
if (part.name == partName) {
sym->partition = part.getNumber();
return;
Expand All @@ -2473,11 +2472,11 @@ static void readSymbolPartitionSection(InputSectionBase *s) {
// Impose a limit of no more than 254 partitions. This limit comes from the
// sizes of the Partition fields in InputSectionBase and Symbol, as well as
// the amount of space devoted to the partition number in RankFlags.
if (partitions.size() == 254)
if (ctx.partitions.size() == 254)
fatal("may not have more than 254 partitions");

partitions.emplace_back();
Partition &newPart = partitions.back();
ctx.partitions.emplace_back();
Partition &newPart = ctx.partitions.back();
newPart.name = partName;
sym->partition = newPart.getNumber();
}
Expand Down Expand Up @@ -3097,7 +3096,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {

// Now that the number of partitions is fixed, save a pointer to the main
// partition.
ctx.mainPart = &partitions[0];
ctx.mainPart = &ctx.partitions[0];

// Read .note.gnu.property sections from input object files which
// contain a hint to tweak linker's and loader's behaviors.
Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/ICF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ template <class ELFT> void ICF<ELFT>::run() {
// If two .gcc_except_table have identical semantics (usually identical
// content with PC-relative encoding), we will lose folding opportunity.
uint32_t uniqueId = 0;
for (Partition &part : partitions)
for (Partition &part : ctx.partitions)
part.ehFrame->iterateFDEWithLSDA<ELFT>(
[&](InputSection &s) { s.eqClass[0] = s.eqClass[1] = ++uniqueId; });

Expand Down
2 changes: 0 additions & 2 deletions lld/ELF/InputSection.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ class SyntheticSection;
template <class ELFT> class ObjFile;
class OutputSection;

LLVM_LIBRARY_VISIBILITY extern std::vector<Partition> partitions;

// Returned by InputSectionBase::relsOrRelas. At most one member is empty.
template <class ELFT> struct RelsOrRelas {
Relocs<typename ELFT::Rel> rels;
Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/LinkerScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ void LinkerScript::discard(InputSectionBase &s) {
}

void LinkerScript::discardSynthetic(OutputSection &outCmd) {
for (Partition &part : partitions) {
for (Partition &part : ctx.partitions) {
if (!part.armExidx || !part.armExidx->isLive())
continue;
SmallVector<InputSectionBase *, 0> secs(
Expand Down
6 changes: 3 additions & 3 deletions lld/ELF/MarkLive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,13 @@ template <class ELFT> void elf::markLive() {
sec->markDead();

// Follow the graph to mark all live sections.
for (unsigned curPart = 1; curPart <= partitions.size(); ++curPart)
MarkLive<ELFT>(curPart).run();
for (unsigned i = 1, e = ctx.partitions.size(); i <= e; ++i)
MarkLive<ELFT>(i).run();

// If we have multiple partitions, some sections need to live in the main
// partition even if they were allocated to a loadable partition. Move them
// there now.
if (partitions.size() != 1)
if (ctx.partitions.size() != 1)
MarkLive<ELFT>(1).moveToMain();

// Report garbage-collected sections.
Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/Relocations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1662,7 +1662,7 @@ template <class ELFT> void elf::scanRelocations() {

tg.spawn([] {
RelocationScanner scanner;
for (Partition &part : partitions) {
for (Partition &part : ctx.partitions) {
for (EhInputSection *sec : part.ehFrame->sections)
scanner.template scanSection<ELFT>(*sec, /*isEH=*/true);
if (part.armExidx && part.armExidx->isLive())
Expand Down
30 changes: 14 additions & 16 deletions lld/ELF/SyntheticSections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4438,26 +4438,26 @@ PartitionIndexSection::PartitionIndexSection()
: SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 4, ".rodata") {}

size_t PartitionIndexSection::getSize() const {
return 12 * (partitions.size() - 1);
return 12 * (ctx.partitions.size() - 1);
}

void PartitionIndexSection::finalizeContents() {
for (size_t i = 1; i != partitions.size(); ++i)
partitions[i].nameStrTab =
ctx.mainPart->dynStrTab->addString(partitions[i].name);
for (size_t i = 1; i != ctx.partitions.size(); ++i)
ctx.partitions[i].nameStrTab =
ctx.mainPart->dynStrTab->addString(ctx.partitions[i].name);
}

void PartitionIndexSection::writeTo(uint8_t *buf) {
uint64_t va = getVA();
for (size_t i = 1; i != partitions.size(); ++i) {
write32(buf,
ctx.mainPart->dynStrTab->getVA() + partitions[i].nameStrTab - va);
write32(buf + 4, partitions[i].elfHeader->getVA() - (va + 4));
for (size_t i = 1; i != ctx.partitions.size(); ++i) {
write32(buf, ctx.mainPart->dynStrTab->getVA() +
ctx.partitions[i].nameStrTab - va);
write32(buf + 4, ctx.partitions[i].elfHeader->getVA() - (va + 4));

SyntheticSection *next = i == partitions.size() - 1
SyntheticSection *next = i == ctx.partitions.size() - 1
? in.partEnd.get()
: partitions[i + 1].elfHeader.get();
write32(buf + 8, next->getVA() - partitions[i].elfHeader->getVA());
: ctx.partitions[i + 1].elfHeader.get();
write32(buf + 8, next->getVA() - ctx.partitions[i].elfHeader->getVA());

va += 12;
buf += 12;
Expand Down Expand Up @@ -4657,7 +4657,7 @@ template <class ELFT> void elf::createSyntheticSections() {
// The removeUnusedSyntheticSections() function relies on the
// SyntheticSections coming last.
if (needsInterpSection()) {
for (size_t i = 1; i <= partitions.size(); ++i) {
for (size_t i = 1; i <= ctx.partitions.size(); ++i) {
InputSection *sec = createInterpSection();
sec->partition = i;
ctx.inputSections.push_back(sec);
Expand Down Expand Up @@ -4707,7 +4707,7 @@ template <class ELFT> void elf::createSyntheticSections() {
StringRef relaDynName = config->isRela ? ".rela.dyn" : ".rel.dyn";

const unsigned threadCount = config->threadCount;
for (Partition &part : partitions) {
for (Partition &part : ctx.partitions) {
auto add = [&](SyntheticSection &sec) {
sec.partition = part.getNumber();
ctx.inputSections.push_back(&sec);
Expand Down Expand Up @@ -4811,7 +4811,7 @@ template <class ELFT> void elf::createSyntheticSections() {
}
}

if (partitions.size() != 1) {
if (ctx.partitions.size() != 1) {
// Create the partition end marker. This needs to be in partition number 255
// so that it is sorted after all other partitions. It also has other
// special handling (see createPhdrs() and combineEhSections()).
Expand Down Expand Up @@ -4928,8 +4928,6 @@ template <class ELFT> void elf::createSyntheticSections() {

InStruct elf::in;

std::vector<Partition> elf::partitions;

template void elf::splitSections<ELF32LE>();
template void elf::splitSections<ELF32BE>();
template void elf::splitSections<ELF64LE>();
Expand Down
4 changes: 2 additions & 2 deletions lld/ELF/SyntheticSections.h
Original file line number Diff line number Diff line change
Expand Up @@ -1471,12 +1471,12 @@ struct Partition {
std::unique_ptr<SyntheticSection> verNeed;
std::unique_ptr<VersionTableSection> verSym;

unsigned getNumber() const { return this - &partitions[0] + 1; }
unsigned getNumber() const { return this - &ctx.partitions[0] + 1; }
};

inline Partition &SectionBase::getPartition() const {
assert(isLive());
return partitions[partition - 1];
return ctx.partitions[partition - 1];
}

// Linker generated sections which can be used as inputs and are not specific to
Expand Down
35 changes: 18 additions & 17 deletions lld/ELF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ static void removeEmptyPTLoad(SmallVector<PhdrEntry *, 0> &phdrs) {
void elf::copySectionsIntoPartitions() {
SmallVector<InputSectionBase *, 0> newSections;
const size_t ehSize = ctx.ehInputSections.size();
for (unsigned part = 2; part != partitions.size() + 1; ++part) {
for (unsigned part = 2; part != ctx.partitions.size() + 1; ++part) {
for (InputSectionBase *s : ctx.inputSections) {
if (!(s->flags & SHF_ALLOC) || !s->isLive() || s->type != SHT_NOTE)
continue;
Expand Down Expand Up @@ -320,15 +320,15 @@ template <class ELFT> void Writer<ELFT>::run() {
// Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
// 0 sized region. This has to be done late since only after assignAddresses
// we know the size of the sections.
for (Partition &part : partitions)
for (Partition &part : ctx.partitions)
removeEmptyPTLoad(part.phdrs);

if (!config->oFormatBinary)
assignFileOffsets();
else
assignFileOffsetsBinary();

for (Partition &part : partitions)
for (Partition &part : ctx.partitions)
setPhdrs(part);

// Handle --print-map(-M)/--Map and --cref. Dump them before checkSections()
Expand Down Expand Up @@ -844,7 +844,7 @@ template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
auto isLarge = [](OutputSection *osec) {
return config->emachine == EM_X86_64 && osec->flags & SHF_X86_64_LARGE;
};
for (Partition &part : partitions) {
for (Partition &part : ctx.partitions) {
for (PhdrEntry *p : part.phdrs) {
if (p->p_type != PT_LOAD)
continue;
Expand Down Expand Up @@ -1443,7 +1443,7 @@ template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
// increasing. Anything here must be repeatable, since spilling may change
// section order.
const auto finalizeOrderDependentContent = [this] {
for (Partition &part : partitions)
for (Partition &part : ctx.partitions)
finalizeSynthetic(part.armExidx.get());
resolveShfLinkOrder();
};
Expand Down Expand Up @@ -1485,7 +1485,7 @@ template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
if (in.mipsGot)
in.mipsGot->updateAllocSize();

for (Partition &part : partitions) {
for (Partition &part : ctx.partitions) {
// The R_AARCH64_AUTH_RELATIVE has a smaller addend field as bits [63:32]
// encode the signing schema. We've put relocations in .relr.auth.dyn
// during RelocationScanner::processAux, but the target VA for some of
Expand Down Expand Up @@ -1777,7 +1777,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
// earlier.
{
llvm::TimeTraceScope timeScope("Finalize .eh_frame");
for (Partition &part : partitions)
for (Partition &part : ctx.partitions)
finalizeSynthetic(part.ehFrame.get());
}
}
Expand Down Expand Up @@ -1865,7 +1865,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
in.symTab->addSymbol(sym);

if (sym->includeInDynsym()) {
partitions[sym->partition - 1].dynSymTab->addSymbol(sym);
ctx.partitions[sym->partition - 1].dynSymTab->addSymbol(sym);
if (auto *file = dyn_cast_or_null<SharedFile>(sym->file))
if (file->isNeeded && !sym->isUndefined())
addVerneed(sym);
Expand All @@ -1874,7 +1874,8 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {

// We also need to scan the dynamic relocation tables of the other
// partitions and add any referenced symbols to the partition's dynsym.
for (Partition &part : MutableArrayRef<Partition>(partitions).slice(1)) {
for (Partition &part :
MutableArrayRef<Partition>(ctx.partitions).slice(1)) {
DenseSet<Symbol *> syms;
for (const SymbolTableEntry &e : part.dynSymTab->getSymbols())
syms.insert(e.sym);
Expand Down Expand Up @@ -1922,7 +1923,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
symtab.addSymbol(Undefined{ctx.internalFile, "__tls_get_addr",
STB_GLOBAL, STV_DEFAULT, STT_NOTYPE});
sym->isPreemptible = true;
partitions[0].dynSymTab->addSymbol(sym);
ctx.partitions[0].dynSymTab->addSymbol(sym);
}

// This is a bit of a hack. A value of 0 means undef, so we set it
Expand All @@ -1935,7 +1936,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
// The headers have to be created before finalize as that can influence the
// image base and the dynamic section on mips includes the image base.
if (!config->relocatable && !config->oFormatBinary) {
for (Partition &part : partitions) {
for (Partition &part : ctx.partitions) {
part.phdrs = ctx.script->hasPhdrsCommands() ? ctx.script->createPhdrs()
: createPhdrs(part);
if (config->emachine == EM_ARM) {
Expand Down Expand Up @@ -1993,7 +1994,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {

// Dynamic section must be the last one in this list and dynamic
// symbol table section (dynSymTab) must be the first one.
for (Partition &part : partitions) {
for (Partition &part : ctx.partitions) {
if (part.relaDyn) {
part.relaDyn->mergeRels();
// Compute DT_RELACOUNT to be used by part.dynamic.
Expand Down Expand Up @@ -2437,7 +2438,7 @@ template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {
}
};

for (Partition &part : partitions) {
for (Partition &part : ctx.partitions) {
prev = nullptr;
for (const PhdrEntry *p : part.phdrs)
if (p->p_type == PT_LOAD && p->firstSec) {
Expand Down Expand Up @@ -2503,7 +2504,7 @@ template <class ELFT> void Writer<ELFT>::assignFileOffsets() {
uint64_t off = ctx.out.elfHeader->size + ctx.out.programHeaders->size;

PhdrEntry *lastRX = nullptr;
for (Partition &part : partitions)
for (Partition &part : ctx.partitions)
for (PhdrEntry *p : part.phdrs)
if (p->p_type == PT_LOAD && (p->p_flags & PF_X))
lastRX = p;
Expand Down Expand Up @@ -2813,7 +2814,7 @@ static void fillTrap(uint8_t *i, uint8_t *end) {
// We'll leave other pages in segments as-is because the rest will be
// overwritten by output sections.
template <class ELFT> void Writer<ELFT>::writeTrapInstr() {
for (Partition &part : partitions) {
for (Partition &part : ctx.partitions) {
// Fill the last page.
for (PhdrEntry *p : part.phdrs)
if (p->p_type == PT_LOAD && (p->p_flags & PF_X))
Expand Down Expand Up @@ -2890,7 +2891,7 @@ template <class ELFT> void Writer<ELFT>::writeBuildId() {
return;

if (config->buildId == BuildIdKind::Hexstring) {
for (Partition &part : partitions)
for (Partition &part : ctx.partitions)
part.buildId->writeBuildId(config->buildIdVector);
return;
}
Expand Down Expand Up @@ -2930,7 +2931,7 @@ template <class ELFT> void Writer<ELFT>::writeBuildId() {
default:
llvm_unreachable("unknown BuildIdKind");
}
for (Partition &part : partitions)
for (Partition &part : ctx.partitions)
part.buildId->writeBuildId(output);
}

Expand Down

0 comments on commit 40e8e4d

Please sign in to comment.