Skip to content

Commit 682925e

Browse files
committed
[ELF] Pass Ctx & to Partition
1 parent 00cd1a0 commit 682925e

File tree

6 files changed

+48
-48
lines changed

6 files changed

+48
-48
lines changed

lld/ELF/Driver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2471,7 +2471,7 @@ static void readSymbolPartitionSection(Ctx &ctx, InputSectionBase *s) {
24712471
StringRef partName = reinterpret_cast<const char *>(s->content().data());
24722472
for (Partition &part : ctx.partitions) {
24732473
if (part.name == partName) {
2474-
sym->partition = part.getNumber();
2474+
sym->partition = part.getNumber(ctx);
24752475
return;
24762476
}
24772477
}
@@ -2500,7 +2500,7 @@ static void readSymbolPartitionSection(Ctx &ctx, InputSectionBase *s) {
25002500
ctx.partitions.emplace_back(ctx);
25012501
Partition &newPart = ctx.partitions.back();
25022502
newPart.name = partName;
2503-
sym->partition = newPart.getNumber();
2503+
sym->partition = newPart.getNumber(ctx);
25042504
}
25052505

25062506
static void markBuffersAsDontNeed(Ctx &ctx, bool skipLinkedOutput) {

lld/ELF/InputSection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class SectionBase {
8989
// The 1-indexed partition that this section is assigned to by the garbage
9090
// collector, or 0 if this section is dead. Normally there is only one
9191
// partition, so this will either be 0 or 1.
92-
elf::Partition &getPartition() const;
92+
elf::Partition &getPartition(Ctx &) const;
9393

9494
// These corresponds to the fields in Elf_Shdr.
9595
uint64_t flags;

lld/ELF/Relocations.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ template <bool shard = false>
878878
static void addRelativeReloc(Ctx &ctx, InputSectionBase &isec,
879879
uint64_t offsetInSec, Symbol &sym, int64_t addend,
880880
RelExpr expr, RelType type) {
881-
Partition &part = isec.getPartition();
881+
Partition &part = isec.getPartition(ctx);
882882

883883
if (sym.isTagged()) {
884884
std::lock_guard<std::mutex> lock(relocMutex);
@@ -1159,7 +1159,7 @@ void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
11591159
if (ctx.arg.emachine == EM_MIPS && rel == ctx.target->symbolicRel)
11601160
rel = ctx.target->relativeRel;
11611161
std::lock_guard<std::mutex> lock(relocMutex);
1162-
Partition &part = sec->getPartition();
1162+
Partition &part = sec->getPartition(ctx);
11631163
if (ctx.arg.emachine == EM_AARCH64 && type == R_AARCH64_AUTH_ABS64) {
11641164
// For a preemptible symbol, we can't use a relative relocation. For an
11651165
// undefined symbol, we can't compute offset at link-time and use a

lld/ELF/SyntheticSections.cpp

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ SmallVector<EhFrameSection::FdeData, 0> EhFrameSection::getFdeData() const {
563563
uint8_t *buf = ctx.bufferStart + getParent()->offset + outSecOff;
564564
SmallVector<FdeData, 0> ret;
565565

566-
uint64_t va = getPartition().ehFrameHdr->getVA();
566+
uint64_t va = getPartition(ctx).ehFrameHdr->getVA();
567567
for (CieRecord *rec : cieRecords) {
568568
uint8_t enc = getFdeEncoding(rec->cie);
569569
for (EhSectionPiece *fde : rec->fdes) {
@@ -650,8 +650,8 @@ void EhFrameSection::writeTo(uint8_t *buf) {
650650
for (EhInputSection *s : sections)
651651
ctx.target->relocateAlloc(*s, buf);
652652

653-
if (getPartition().ehFrameHdr && getPartition().ehFrameHdr->getParent())
654-
getPartition().ehFrameHdr->write();
653+
if (getPartition(ctx).ehFrameHdr && getPartition(ctx).ehFrameHdr->getParent())
654+
getPartition(ctx).ehFrameHdr->write();
655655
}
656656

657657
GotSection::GotSection(Ctx &ctx)
@@ -1325,7 +1325,7 @@ static uint64_t addPltRelSz(Ctx &ctx) { return ctx.in.relaPlt->getSize(); }
13251325
template <class ELFT>
13261326
std::vector<std::pair<int32_t, uint64_t>>
13271327
DynamicSection<ELFT>::computeContents() {
1328-
elf::Partition &part = getPartition();
1328+
elf::Partition &part = getPartition(ctx);
13291329
bool isMain = part.name.empty();
13301330
std::vector<std::pair<int32_t, uint64_t>> entries;
13311331

@@ -1586,7 +1586,7 @@ DynamicSection<ELFT>::computeContents() {
15861586
}
15871587

15881588
template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1589-
if (OutputSection *sec = getPartition().dynStrTab->getParent())
1589+
if (OutputSection *sec = getPartition(ctx).dynStrTab->getParent())
15901590
getParent()->link = sec->sectionIndex;
15911591
this->size = computeContents().size() * this->entsize;
15921592
}
@@ -1688,7 +1688,7 @@ void RelocationBaseSection::partitionRels() {
16881688
}
16891689

16901690
void RelocationBaseSection::finalizeContents() {
1691-
SymbolTableBaseSection *symTab = getPartition().dynSymTab.get();
1691+
SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
16921692

16931693
// When linking glibc statically, .rel{,a}.plt contains R_*_IRELATIVE
16941694
// relocations due to IFUNC (e.g. strcpy). sh_link will be set to 0 in that
@@ -1712,7 +1712,7 @@ void DynamicReloc::computeRaw(Ctx &ctx, SymbolTableBaseSection *symt) {
17121712
}
17131713

17141714
void RelocationBaseSection::computeRels() {
1715-
SymbolTableBaseSection *symTab = getPartition().dynSymTab.get();
1715+
SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
17161716
parallelForEach(relocs, [&ctx = ctx, symTab](DynamicReloc &rel) {
17171717
rel.computeRaw(ctx, symTab);
17181718
});
@@ -1852,7 +1852,7 @@ bool AndroidPackedRelocationSection<ELFT>::updateAllocSize(Ctx &ctx) {
18521852
for (const DynamicReloc &rel : relocs) {
18531853
Elf_Rela r;
18541854
r.r_offset = rel.getOffset();
1855-
r.setSymbolAndType(rel.getSymIndex(getPartition().dynSymTab.get()),
1855+
r.setSymbolAndType(rel.getSymIndex(getPartition(ctx).dynSymTab.get()),
18561856
rel.type, false);
18571857
r.r_addend = ctx.arg.isRela ? rel.computeAddend(ctx) : 0;
18581858

@@ -2162,9 +2162,9 @@ void SymbolTableBaseSection::finalizeContents() {
21622162
// Because the first symbol entry is a null entry, 1 is the first.
21632163
getParent()->info = 1;
21642164

2165-
if (getPartition().gnuHashTab) {
2165+
if (getPartition(ctx).gnuHashTab) {
21662166
// NB: It also sorts Symbols to meet the GNU hash table requirements.
2167-
getPartition().gnuHashTab->addSymbols(symbols);
2167+
getPartition(ctx).gnuHashTab->addSymbols(symbols);
21682168
} else if (ctx.arg.emachine == EM_MIPS) {
21692169
sortMipsSymbols(ctx, symbols);
21702170
}
@@ -2416,7 +2416,7 @@ GnuHashTableSection::GnuHashTableSection(Ctx &ctx)
24162416
".gnu.hash") {}
24172417

24182418
void GnuHashTableSection::finalizeContents() {
2419-
if (OutputSection *sec = getPartition().dynSymTab->getParent())
2419+
if (OutputSection *sec = getPartition(ctx).dynSymTab->getParent())
24202420
getParent()->link = sec->sectionIndex;
24212421

24222422
// Computes bloom filter size in word size. We want to allocate 12
@@ -2438,7 +2438,7 @@ void GnuHashTableSection::writeTo(uint8_t *buf) {
24382438
// Write a header.
24392439
write32(ctx, buf, nBuckets);
24402440
write32(ctx, buf + 4,
2441-
getPartition().dynSymTab->getNumSymbols() - symbols.size());
2441+
getPartition(ctx).dynSymTab->getNumSymbols() - symbols.size());
24422442
write32(ctx, buf + 8, maskWords);
24432443
write32(ctx, buf + 12, Shift2);
24442444
buf += 16;
@@ -2474,7 +2474,7 @@ void GnuHashTableSection::writeTo(uint8_t *buf) {
24742474
// Write a hash bucket. Hash buckets contain indices in the following hash
24752475
// value table.
24762476
write32(ctx, buckets + i->bucketIdx,
2477-
getPartition().dynSymTab->getSymbolIndex(*i->sym));
2477+
getPartition(ctx).dynSymTab->getSymbolIndex(*i->sym));
24782478
oldBucket = i->bucketIdx;
24792479
}
24802480
}
@@ -2527,7 +2527,7 @@ HashTableSection::HashTableSection(Ctx &ctx)
25272527
}
25282528

25292529
void HashTableSection::finalizeContents() {
2530-
SymbolTableBaseSection *symTab = getPartition().dynSymTab.get();
2530+
SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
25312531

25322532
if (OutputSection *sec = symTab->getParent())
25332533
getParent()->link = sec->sectionIndex;
@@ -2541,7 +2541,7 @@ void HashTableSection::finalizeContents() {
25412541
}
25422542

25432543
void HashTableSection::writeTo(uint8_t *buf) {
2544-
SymbolTableBaseSection *symTab = getPartition().dynSymTab.get();
2544+
SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
25452545
unsigned numSymbols = symTab->getNumSymbols();
25462546

25472547
uint32_t *p = reinterpret_cast<uint32_t *>(buf);
@@ -3667,14 +3667,14 @@ void EhFrameHeader::writeTo(uint8_t *buf) {
36673667
void EhFrameHeader::write() {
36683668
uint8_t *buf = ctx.bufferStart + getParent()->offset + outSecOff;
36693669
using FdeData = EhFrameSection::FdeData;
3670-
SmallVector<FdeData, 0> fdes = getPartition().ehFrame->getFdeData();
3670+
SmallVector<FdeData, 0> fdes = getPartition(ctx).ehFrame->getFdeData();
36713671

36723672
buf[0] = 1;
36733673
buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;
36743674
buf[2] = DW_EH_PE_udata4;
36753675
buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
36763676
write32(ctx, buf + 4,
3677-
getPartition().ehFrame->getParent()->addr - this->getVA() - 4);
3677+
getPartition(ctx).ehFrame->getParent()->addr - this->getVA() - 4);
36783678
write32(ctx, buf + 8, fdes.size());
36793679
buf += 12;
36803680

@@ -3687,31 +3687,31 @@ void EhFrameHeader::write() {
36873687

36883688
size_t EhFrameHeader::getSize() const {
36893689
// .eh_frame_hdr has a 12 bytes header followed by an array of FDEs.
3690-
return 12 + getPartition().ehFrame->numFdes * 8;
3690+
return 12 + getPartition(ctx).ehFrame->numFdes * 8;
36913691
}
36923692

36933693
bool EhFrameHeader::isNeeded() const {
3694-
return isLive() && getPartition().ehFrame->isNeeded();
3694+
return isLive() && getPartition(ctx).ehFrame->isNeeded();
36953695
}
36963696

36973697
VersionDefinitionSection::VersionDefinitionSection(Ctx &ctx)
36983698
: SyntheticSection(ctx, SHF_ALLOC, SHT_GNU_verdef, sizeof(uint32_t),
36993699
".gnu.version_d") {}
37003700

37013701
StringRef VersionDefinitionSection::getFileDefName() {
3702-
if (!getPartition().name.empty())
3703-
return getPartition().name;
3702+
if (!getPartition(ctx).name.empty())
3703+
return getPartition(ctx).name;
37043704
if (!ctx.arg.soName.empty())
37053705
return ctx.arg.soName;
37063706
return ctx.arg.outputFile;
37073707
}
37083708

37093709
void VersionDefinitionSection::finalizeContents() {
3710-
fileDefNameOff = getPartition().dynStrTab->addString(getFileDefName());
3710+
fileDefNameOff = getPartition(ctx).dynStrTab->addString(getFileDefName());
37113711
for (const VersionDefinition &v : namedVersionDefs(ctx))
3712-
verDefNameOffs.push_back(getPartition().dynStrTab->addString(v.name));
3712+
verDefNameOffs.push_back(getPartition(ctx).dynStrTab->addString(v.name));
37133713

3714-
if (OutputSection *sec = getPartition().dynStrTab->getParent())
3714+
if (OutputSection *sec = getPartition(ctx).dynStrTab->getParent())
37153715
getParent()->link = sec->sectionIndex;
37163716

37173717
// sh_info should be set to the number of definitions. This fact is missed in
@@ -3765,16 +3765,16 @@ VersionTableSection::VersionTableSection(Ctx &ctx)
37653765
void VersionTableSection::finalizeContents() {
37663766
// At the moment of june 2016 GNU docs does not mention that sh_link field
37673767
// should be set, but Sun docs do. Also readelf relies on this field.
3768-
getParent()->link = getPartition().dynSymTab->getParent()->sectionIndex;
3768+
getParent()->link = getPartition(ctx).dynSymTab->getParent()->sectionIndex;
37693769
}
37703770

37713771
size_t VersionTableSection::getSize() const {
3772-
return (getPartition().dynSymTab->getSymbols().size() + 1) * 2;
3772+
return (getPartition(ctx).dynSymTab->getSymbols().size() + 1) * 2;
37733773
}
37743774

37753775
void VersionTableSection::writeTo(uint8_t *buf) {
37763776
buf += 2;
3777-
for (const SymbolTableEntry &s : getPartition().dynSymTab->getSymbols()) {
3777+
for (const SymbolTableEntry &s : getPartition(ctx).dynSymTab->getSymbols()) {
37783778
// For an unextracted lazy symbol (undefined weak), it must have been
37793779
// converted to Undefined and have VER_NDX_GLOBAL version here.
37803780
assert(!s.sym->isLazy());
@@ -3785,7 +3785,7 @@ void VersionTableSection::writeTo(uint8_t *buf) {
37853785

37863786
bool VersionTableSection::isNeeded() const {
37873787
return isLive() &&
3788-
(getPartition().verDef || getPartition().verNeed->isNeeded());
3788+
(getPartition(ctx).verDef || getPartition(ctx).verNeed->isNeeded());
37893789
}
37903790

37913791
void elf::addVerneed(Ctx &ctx, Symbol &ss) {
@@ -3817,7 +3817,7 @@ template <class ELFT> void VersionNeedSection<ELFT>::finalizeContents() {
38173817
continue;
38183818
verneeds.emplace_back();
38193819
Verneed &vn = verneeds.back();
3820-
vn.nameStrTab = getPartition().dynStrTab->addString(f->soName);
3820+
vn.nameStrTab = getPartition(ctx).dynStrTab->addString(f->soName);
38213821
bool isLibc = ctx.arg.relrGlibc && f->soName.starts_with("libc.so.");
38223822
bool isGlibc2 = false;
38233823
for (unsigned i = 0; i != f->vernauxs.size(); ++i) {
@@ -3829,17 +3829,17 @@ template <class ELFT> void VersionNeedSection<ELFT>::finalizeContents() {
38293829
if (isLibc && ver.starts_with("GLIBC_2."))
38303830
isGlibc2 = true;
38313831
vn.vernauxs.push_back({verdef->vd_hash, f->vernauxs[i],
3832-
getPartition().dynStrTab->addString(ver)});
3832+
getPartition(ctx).dynStrTab->addString(ver)});
38333833
}
38343834
if (isGlibc2) {
38353835
const char *ver = "GLIBC_ABI_DT_RELR";
38363836
vn.vernauxs.push_back({hashSysV(ver),
38373837
++SharedFile::vernauxNum + getVerDefNum(ctx),
3838-
getPartition().dynStrTab->addString(ver)});
3838+
getPartition(ctx).dynStrTab->addString(ver)});
38393839
}
38403840
}
38413841

3842-
if (OutputSection *sec = getPartition().dynStrTab->getParent())
3842+
if (OutputSection *sec = getPartition(ctx).dynStrTab->getParent())
38433843
getParent()->link = sec->sectionIndex;
38443844
getParent()->info = verneeds.size();
38453845
}
@@ -3995,7 +3995,7 @@ template <class ELFT> void elf::splitSections(Ctx &ctx) {
39953995
void elf::combineEhSections(Ctx &ctx) {
39963996
llvm::TimeTraceScope timeScope("Combine EH sections");
39973997
for (EhInputSection *sec : ctx.ehInputSections) {
3998-
EhFrameSection &eh = *sec->getPartition().ehFrame;
3998+
EhFrameSection &eh = *sec->getPartition(ctx).ehFrame;
39993999
sec->parent = &eh;
40004000
eh.addralign = std::max(eh.addralign, sec->addralign);
40014001
eh.sections.push_back(sec);
@@ -4004,12 +4004,12 @@ void elf::combineEhSections(Ctx &ctx) {
40044004

40054005
if (!ctx.mainPart->armExidx)
40064006
return;
4007-
llvm::erase_if(ctx.inputSections, [](InputSectionBase *s) {
4007+
llvm::erase_if(ctx.inputSections, [&](InputSectionBase *s) {
40084008
// Ignore dead sections and the partition end marker (.part.end),
40094009
// whose partition number is out of bounds.
40104010
if (!s->isLive() || s->partition == 255)
40114011
return false;
4012-
Partition &part = s->getPartition();
4012+
Partition &part = s->getPartition(ctx);
40134013
return s->kind() == SectionBase::Regular && part.armExidx &&
40144014
part.armExidx->addSection(cast<InputSection>(s));
40154015
});
@@ -4447,7 +4447,7 @@ size_t PartitionElfHeaderSection<ELFT>::getSize() const {
44474447

44484448
template <typename ELFT>
44494449
void PartitionElfHeaderSection<ELFT>::writeTo(uint8_t *buf) {
4450-
writeEhdr<ELFT>(ctx, buf, getPartition());
4450+
writeEhdr<ELFT>(ctx, buf, getPartition(ctx));
44514451

44524452
// Loadable partitions are always ET_DYN.
44534453
auto *eHdr = reinterpret_cast<typename ELFT::Ehdr *>(buf);
@@ -4460,12 +4460,12 @@ PartitionProgramHeadersSection<ELFT>::PartitionProgramHeadersSection(Ctx &ctx)
44604460

44614461
template <typename ELFT>
44624462
size_t PartitionProgramHeadersSection<ELFT>::getSize() const {
4463-
return sizeof(typename ELFT::Phdr) * getPartition().phdrs.size();
4463+
return sizeof(typename ELFT::Phdr) * getPartition(ctx).phdrs.size();
44644464
}
44654465

44664466
template <typename ELFT>
44674467
void PartitionProgramHeadersSection<ELFT>::writeTo(uint8_t *buf) {
4468-
writePhdrs<ELFT>(buf, getPartition());
4468+
writePhdrs<ELFT>(buf, getPartition(ctx));
44694469
}
44704470

44714471
PartitionIndexSection::PartitionIndexSection(Ctx &ctx)
@@ -4747,7 +4747,7 @@ template <class ELFT> void elf::createSyntheticSections(Ctx &ctx) {
47474747
const unsigned threadCount = ctx.arg.threadCount;
47484748
for (Partition &part : ctx.partitions) {
47494749
auto add = [&](SyntheticSection &sec) {
4750-
sec.partition = part.getNumber();
4750+
sec.partition = part.getNumber(ctx);
47514751
ctx.inputSections.push_back(&sec);
47524752
};
47534753

lld/ELF/SyntheticSections.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,10 +1475,10 @@ struct Partition {
14751475
std::unique_ptr<VersionTableSection> verSym;
14761476

14771477
Partition(Ctx &ctx) : ctx(ctx) {}
1478-
unsigned getNumber() const { return this - &ctx.partitions[0] + 1; }
1478+
unsigned getNumber(Ctx &ctx) const { return this - &ctx.partitions[0] + 1; }
14791479
};
14801480

1481-
inline Partition &SectionBase::getPartition() const {
1481+
inline Partition &SectionBase::getPartition(Ctx &ctx) const {
14821482
assert(isLive());
14831483
return ctx.partitions[partition - 1];
14841484
}

lld/ELF/Writer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,7 +2191,7 @@ SmallVector<PhdrEntry *, 0> Writer<ELFT>::createPhdrs(Partition &part) {
21912191
return ret.back();
21922192
};
21932193

2194-
unsigned partNo = part.getNumber();
2194+
unsigned partNo = part.getNumber(ctx);
21952195
bool isMain = partNo == 1;
21962196

21972197
// Add the first PT_LOAD segment for regular output sections.
@@ -2381,7 +2381,7 @@ SmallVector<PhdrEntry *, 0> Writer<ELFT>::createPhdrs(Partition &part) {
23812381
template <class ELFT>
23822382
void Writer<ELFT>::addPhdrForSection(Partition &part, unsigned shType,
23832383
unsigned pType, unsigned pFlags) {
2384-
unsigned partNo = part.getNumber();
2384+
unsigned partNo = part.getNumber(ctx);
23852385
auto i = llvm::find_if(ctx.outputSections, [=](OutputSection *cmd) {
23862386
return cmd->partition == partNo && cmd->type == shType;
23872387
});

0 commit comments

Comments
 (0)