Skip to content

Commit 40e8e4d

Browse files
committed
[ELF] Move partitions into ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such singletons.
1 parent 0098cee commit 40e8e4d

File tree

10 files changed

+50
-53
lines changed

10 files changed

+50
-53
lines changed

lld/ELF/Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ struct Ctx {
502502
};
503503
OutSections out;
504504
SmallVector<OutputSection *, 0> outputSections;
505+
std::vector<Partition> partitions;
505506

506507
// Some linker-generated symbols need to be created as
507508
// Defined symbols.

lld/ELF/Driver.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ void Ctx::reset() {
101101
tlsPhdr = nullptr;
102102
out = OutSections{};
103103
outputSections.clear();
104+
partitions.clear();
104105

105106
sym = ElfSym{};
106107

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

153155
in.reset();
154156

155-
partitions.clear();
156-
partitions.emplace_back();
157-
158157
SharedFile::vernauxNum = 0;
159158
};
160159
ctx->e.logName = args::getFilenameWithoutExe(args[0]);
@@ -167,8 +166,8 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
167166
elf::ctx.script = &script;
168167
elf::ctx.symAux.emplace_back();
169168

170-
partitions.clear();
171-
partitions.emplace_back();
169+
elf::ctx.partitions.clear();
170+
elf::ctx.partitions.emplace_back();
172171

173172
config->progName = args[0];
174173

@@ -2448,7 +2447,7 @@ static void readSymbolPartitionSection(InputSectionBase *s) {
24482447
return;
24492448

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

2479-
partitions.emplace_back();
2480-
Partition &newPart = partitions.back();
2478+
ctx.partitions.emplace_back();
2479+
Partition &newPart = ctx.partitions.back();
24812480
newPart.name = partName;
24822481
sym->partition = newPart.getNumber();
24832482
}
@@ -3097,7 +3096,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
30973096

30983097
// Now that the number of partitions is fixed, save a pointer to the main
30993098
// partition.
3100-
ctx.mainPart = &partitions[0];
3099+
ctx.mainPart = &ctx.partitions[0];
31013100

31023101
// Read .note.gnu.property sections from input object files which
31033102
// contain a hint to tweak linker's and loader's behaviors.

lld/ELF/ICF.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ template <class ELFT> void ICF<ELFT>::run() {
480480
// If two .gcc_except_table have identical semantics (usually identical
481481
// content with PC-relative encoding), we will lose folding opportunity.
482482
uint32_t uniqueId = 0;
483-
for (Partition &part : partitions)
483+
for (Partition &part : ctx.partitions)
484484
part.ehFrame->iterateFDEWithLSDA<ELFT>(
485485
[&](InputSection &s) { s.eqClass[0] = s.eqClass[1] = ++uniqueId; });
486486

lld/ELF/InputSection.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ class SyntheticSection;
3333
template <class ELFT> class ObjFile;
3434
class OutputSection;
3535

36-
LLVM_LIBRARY_VISIBILITY extern std::vector<Partition> partitions;
37-
3836
// Returned by InputSectionBase::relsOrRelas. At most one member is empty.
3937
template <class ELFT> struct RelsOrRelas {
4038
Relocs<typename ELFT::Rel> rels;

lld/ELF/LinkerScript.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ void LinkerScript::discard(InputSectionBase &s) {
659659
}
660660

661661
void LinkerScript::discardSynthetic(OutputSection &outCmd) {
662-
for (Partition &part : partitions) {
662+
for (Partition &part : ctx.partitions) {
663663
if (!part.armExidx || !part.armExidx->isLive())
664664
continue;
665665
SmallVector<InputSectionBase *, 0> secs(

lld/ELF/MarkLive.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,13 @@ template <class ELFT> void elf::markLive() {
377377
sec->markDead();
378378

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

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

389389
// Report garbage-collected sections.

lld/ELF/Relocations.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1662,7 +1662,7 @@ template <class ELFT> void elf::scanRelocations() {
16621662

16631663
tg.spawn([] {
16641664
RelocationScanner scanner;
1665-
for (Partition &part : partitions) {
1665+
for (Partition &part : ctx.partitions) {
16661666
for (EhInputSection *sec : part.ehFrame->sections)
16671667
scanner.template scanSection<ELFT>(*sec, /*isEH=*/true);
16681668
if (part.armExidx && part.armExidx->isLive())

lld/ELF/SyntheticSections.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4438,26 +4438,26 @@ PartitionIndexSection::PartitionIndexSection()
44384438
: SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 4, ".rodata") {}
44394439

44404440
size_t PartitionIndexSection::getSize() const {
4441-
return 12 * (partitions.size() - 1);
4441+
return 12 * (ctx.partitions.size() - 1);
44424442
}
44434443

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

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

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

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

47094709
const unsigned threadCount = config->threadCount;
4710-
for (Partition &part : partitions) {
4710+
for (Partition &part : ctx.partitions) {
47114711
auto add = [&](SyntheticSection &sec) {
47124712
sec.partition = part.getNumber();
47134713
ctx.inputSections.push_back(&sec);
@@ -4811,7 +4811,7 @@ template <class ELFT> void elf::createSyntheticSections() {
48114811
}
48124812
}
48134813

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

49294929
InStruct elf::in;
49304930

4931-
std::vector<Partition> elf::partitions;
4932-
49334931
template void elf::splitSections<ELF32LE>();
49344932
template void elf::splitSections<ELF32BE>();
49354933
template void elf::splitSections<ELF64LE>();

lld/ELF/SyntheticSections.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,12 +1471,12 @@ struct Partition {
14711471
std::unique_ptr<SyntheticSection> verNeed;
14721472
std::unique_ptr<VersionTableSection> verSym;
14731473

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

14771477
inline Partition &SectionBase::getPartition() const {
14781478
assert(isLive());
1479-
return partitions[partition - 1];
1479+
return ctx.partitions[partition - 1];
14801480
}
14811481

14821482
// Linker generated sections which can be used as inputs and are not specific to

lld/ELF/Writer.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ static void removeEmptyPTLoad(SmallVector<PhdrEntry *, 0> &phdrs) {
118118
void elf::copySectionsIntoPartitions() {
119119
SmallVector<InputSectionBase *, 0> newSections;
120120
const size_t ehSize = ctx.ehInputSections.size();
121-
for (unsigned part = 2; part != partitions.size() + 1; ++part) {
121+
for (unsigned part = 2; part != ctx.partitions.size() + 1; ++part) {
122122
for (InputSectionBase *s : ctx.inputSections) {
123123
if (!(s->flags & SHF_ALLOC) || !s->isLive() || s->type != SHT_NOTE)
124124
continue;
@@ -320,15 +320,15 @@ template <class ELFT> void Writer<ELFT>::run() {
320320
// Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
321321
// 0 sized region. This has to be done late since only after assignAddresses
322322
// we know the size of the sections.
323-
for (Partition &part : partitions)
323+
for (Partition &part : ctx.partitions)
324324
removeEmptyPTLoad(part.phdrs);
325325

326326
if (!config->oFormatBinary)
327327
assignFileOffsets();
328328
else
329329
assignFileOffsetsBinary();
330330

331-
for (Partition &part : partitions)
331+
for (Partition &part : ctx.partitions)
332332
setPhdrs(part);
333333

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

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

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

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

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

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

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

25052506
PhdrEntry *lastRX = nullptr;
2506-
for (Partition &part : partitions)
2507+
for (Partition &part : ctx.partitions)
25072508
for (PhdrEntry *p : part.phdrs)
25082509
if (p->p_type == PT_LOAD && (p->p_flags & PF_X))
25092510
lastRX = p;
@@ -2813,7 +2814,7 @@ static void fillTrap(uint8_t *i, uint8_t *end) {
28132814
// We'll leave other pages in segments as-is because the rest will be
28142815
// overwritten by output sections.
28152816
template <class ELFT> void Writer<ELFT>::writeTrapInstr() {
2816-
for (Partition &part : partitions) {
2817+
for (Partition &part : ctx.partitions) {
28172818
// Fill the last page.
28182819
for (PhdrEntry *p : part.phdrs)
28192820
if (p->p_type == PT_LOAD && (p->p_flags & PF_X))
@@ -2890,7 +2891,7 @@ template <class ELFT> void Writer<ELFT>::writeBuildId() {
28902891
return;
28912892

28922893
if (config->buildId == BuildIdKind::Hexstring) {
2893-
for (Partition &part : partitions)
2894+
for (Partition &part : ctx.partitions)
28942895
part.buildId->writeBuildId(config->buildIdVector);
28952896
return;
28962897
}
@@ -2930,7 +2931,7 @@ template <class ELFT> void Writer<ELFT>::writeBuildId() {
29302931
default:
29312932
llvm_unreachable("unknown BuildIdKind");
29322933
}
2933-
for (Partition &part : partitions)
2934+
for (Partition &part : ctx.partitions)
29342935
part.buildId->writeBuildId(output);
29352936
}
29362937

0 commit comments

Comments
 (0)