Skip to content

Commit e2f0ec3

Browse files
committed
[ELF] Pass Ctx & to SyntheticSection::getSize
1 parent ec06471 commit e2f0ec3

File tree

10 files changed

+102
-97
lines changed

10 files changed

+102
-97
lines changed

lld/ELF/AArch64ErrataFix.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ class elf::Patch843419Section final : public SyntheticSection {
376376

377377
void writeTo(Ctx &, uint8_t *buf) override;
378378

379-
size_t getSize() const override { return 8; }
379+
size_t getSize(Ctx &) const override { return 8; }
380380

381381
uint64_t getLDSTAddr() const;
382382

@@ -399,7 +399,7 @@ Patch843419Section::Patch843419Section(InputSection *p, uint64_t off)
399399
this->parent = p->getParent();
400400
patchSym = addSyntheticLocal(
401401
saver().save("__CortexA53843419_" + utohexstr(getLDSTAddr())), STT_FUNC,
402-
0, getSize(), *this);
402+
0, getSize(ctx), *this);
403403
addSyntheticLocal(saver().save("$x"), STT_NOTYPE, 0, 0, *this);
404404
}
405405

lld/ELF/ARMErrataFix.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class elf::Patch657417Section final : public SyntheticSection {
7474

7575
void writeTo(Ctx &, uint8_t *buf) override;
7676

77-
size_t getSize() const override { return 4; }
77+
size_t getSize(Ctx &) const override { return 4; }
7878

7979
// Get the virtual address of the branch instruction at patcheeOffset.
8080
uint64_t getBranchAddr() const;
@@ -141,7 +141,7 @@ Patch657417Section::Patch657417Section(InputSection *p, uint64_t off,
141141
parent = p->getParent();
142142
patchSym = addSyntheticLocal(
143143
saver().save("__CortexA8657417_" + utohexstr(getBranchAddr())), STT_FUNC,
144-
isARM ? 0 : 1, getSize(), *this);
144+
isARM ? 0 : 1, getSize(ctx), *this);
145145
addSyntheticLocal(saver().save(isARM ? "$a" : "$t"), STT_NOTYPE, 0, 0, *this);
146146
}
147147

lld/ELF/Arch/ARM.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,7 @@ void ArmCmseSGSection::addMappingSymbol() {
13971397
addSyntheticLocal("$t", STT_NOTYPE, /*off=*/0, /*size=*/0, *this);
13981398
}
13991399

1400-
size_t ArmCmseSGSection::getSize() const {
1400+
size_t ArmCmseSGSection::getSize(Ctx &) const {
14011401
if (sgVeneers.empty())
14021402
return (impLibMaxAddr ? impLibMaxAddr - getVA() : 0) + newEntries * entsize;
14031403

@@ -1471,7 +1471,7 @@ template <typename ELFT> void elf::writeARMCmseImportLib() {
14711471
osec->recordSection(isec);
14721472
osec->finalizeInputSections(ctx);
14731473
osec->shName = shstrtab->addString(osec->name);
1474-
osec->size = isec->getSize();
1474+
osec->size = isec->getSize(ctx);
14751475
isec->finalizeContents(ctx);
14761476
osec->offset = alignToPowerOf2(off, osec->addralign);
14771477
off = osec->offset + osec->size;

lld/ELF/Arch/RISCV.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ class RISCVAttributesSection final : public SyntheticSection {
10471047
RISCVAttributesSection()
10481048
: SyntheticSection(0, SHT_RISCV_ATTRIBUTES, 1, ".riscv.attributes") {}
10491049

1050-
size_t getSize() const override { return size; }
1050+
size_t getSize(Ctx &) const override { return size; }
10511051
void writeTo(Ctx &, uint8_t *buf) override;
10521052

10531053
static constexpr StringRef vendor = "riscv";
@@ -1277,7 +1277,7 @@ mergeAttributesSection(const SmallVector<InputSectionBase *, 0> &sections) {
12771277
}
12781278

12791279
void RISCVAttributesSection::writeTo(Ctx &ctx, uint8_t *buf) {
1280-
const size_t size = getSize();
1280+
const size_t size = getSize(ctx);
12811281
uint8_t *const end = buf + size;
12821282
*buf = ELFAttrs::Format_Version;
12831283
write32(buf + 1, size - 1);

lld/ELF/InputSection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ InputSectionBase::InputSectionBase(ObjFile<ELFT> &file,
101101

102102
size_t InputSectionBase::getSize() const {
103103
if (auto *s = dyn_cast<SyntheticSection>(this))
104-
return s->getSize();
104+
return s->getSize(ctx);
105105
return size - bytesDropped;
106106
}
107107

lld/ELF/InputSection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ class SyntheticSection : public InputSection {
478478
InputSectionBase::Synthetic) {}
479479

480480
virtual ~SyntheticSection() = default;
481-
virtual size_t getSize() const = 0;
481+
virtual size_t getSize(Ctx &) const = 0;
482482
virtual bool updateAllocSize() { return false; }
483483
// If the section has the SHF_ALLOC flag and the size may be changed if
484484
// thunks are added, update the section size.

lld/ELF/Relocations.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,14 +2019,15 @@ static void forEachInputSectionDescription(
20192019
// This may invalidate any output section offsets stored outside of InputSection
20202020
void ThunkCreator::mergeThunks(ArrayRef<OutputSection *> outputSections) {
20212021
forEachInputSectionDescription(
2022-
outputSections, [&](OutputSection *os, InputSectionDescription *isd) {
2022+
outputSections,
2023+
[&, &ctx = ctx](OutputSection *os, InputSectionDescription *isd) {
20232024
if (isd->thunkSections.empty())
20242025
return;
20252026

20262027
// Remove any zero sized precreated Thunks.
20272028
llvm::erase_if(isd->thunkSections,
2028-
[](const std::pair<ThunkSection *, uint32_t> &ts) {
2029-
return ts.first->getSize() == 0;
2029+
[&ctx](const std::pair<ThunkSection *, uint32_t> &ts) {
2030+
return ts.first->getSize(ctx) == 0;
20302031
});
20312032

20322033
// ISD->ThunkSections contains all created ThunkSections, including
@@ -2079,7 +2080,7 @@ ThunkSection *ThunkCreator::getISDThunkSec(OutputSection *os,
20792080
for (std::pair<ThunkSection *, uint32_t> tp : isd->thunkSections) {
20802081
ThunkSection *ts = tp.first;
20812082
uint64_t tsBase = os->addr + ts->outSecOff - pcBias;
2082-
uint64_t tsLimit = tsBase + ts->getSize();
2083+
uint64_t tsLimit = tsBase + ts->getSize(ctx);
20832084
if (ctx.target->inBranchRange(rel.type, src,
20842085
(src > tsLimit) ? tsBase : tsLimit))
20852086
return ts;

lld/ELF/SyntheticSections.cpp

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ template <class ELFT>
167167
void MipsOptionsSection<ELFT>::writeTo(Ctx &ctx, uint8_t *buf) {
168168
auto *options = reinterpret_cast<Elf_Mips_Options *>(buf);
169169
options->kind = ODK_REGINFO;
170-
options->size = getSize();
170+
options->size = getSize(ctx);
171171

172172
if (!ctx.arg.relocatable)
173173
reginfo.ri_gp_value = ctx.in.mipsGot->getGp();
@@ -322,7 +322,7 @@ GnuPropertySection::GnuPropertySection()
322322

323323
void GnuPropertySection::writeTo(Ctx &ctx, uint8_t *buf) {
324324
write32(buf, 4); // Name size
325-
write32(buf + 4, getSize() - 16); // Content size
325+
write32(buf + 4, getSize(ctx) - 16); // Content size
326326
write32(buf + 8, NT_GNU_PROPERTY_TYPE_0); // Type
327327
memcpy(buf + 12, "GNU", 4); // Name string
328328

@@ -348,7 +348,7 @@ void GnuPropertySection::writeTo(Ctx &ctx, uint8_t *buf) {
348348
}
349349
}
350350

351-
size_t GnuPropertySection::getSize() const {
351+
size_t GnuPropertySection::getSize(Ctx &ctx) const {
352352
uint32_t contentSize = 0;
353353
if (ctx.arg.andFeatures != 0)
354354
contentSize += ctx.arg.is64 ? 16 : 12;
@@ -1183,7 +1183,7 @@ void GotPltSection::addEntry(Symbol &sym) {
11831183
entries.push_back(&sym);
11841184
}
11851185

1186-
size_t GotPltSection::getSize() const {
1186+
size_t GotPltSection::getSize(Ctx &ctx) const {
11871187
return (ctx.target->gotPltHeaderEntriesNum + entries.size()) *
11881188
ctx.target->gotEntrySize;
11891189
}
@@ -1228,7 +1228,7 @@ void IgotPltSection::addEntry(Symbol &sym) {
12281228
entries.push_back(&sym);
12291229
}
12301230

1231-
size_t IgotPltSection::getSize() const {
1231+
size_t IgotPltSection::getSize(Ctx &ctx) const {
12321232
return entries.size() * ctx.target->gotEntrySize;
12331233
}
12341234

@@ -1303,17 +1303,17 @@ DynamicSection<ELFT>::DynamicSection()
13031303
//
13041304
// DT_RELASZ is the total size of the included sections.
13051305
static uint64_t addRelaSz(const RelocationBaseSection &relaDyn) {
1306-
size_t size = relaDyn.getSize();
1306+
size_t size = relaDyn.getSize(ctx);
13071307
if (ctx.in.relaPlt->getParent() == relaDyn.getParent())
1308-
size += ctx.in.relaPlt->getSize();
1308+
size += ctx.in.relaPlt->getSize(ctx);
13091309
return size;
13101310
}
13111311

13121312
// A Linker script may assign the RELA relocation sections to the same
13131313
// output section. When this occurs we cannot just use the OutputSection
13141314
// Size. Moreover the [DT_JMPREL, DT_JMPREL + DT_PLTRELSZ) is permitted to
13151315
// overlap with the [DT_RELA, DT_RELA + DT_RELASZ).
1316-
static uint64_t addPltRelSz() { return ctx.in.relaPlt->getSize(); }
1316+
static uint64_t addPltRelSz() { return ctx.in.relaPlt->getSize(ctx); }
13171317

13181318
// Add remaining entries to complete .dynamic contents.
13191319
template <class ELFT>
@@ -1482,15 +1482,15 @@ DynamicSection<ELFT>::computeContents() {
14821482
addInSec(DT_AARCH64_MEMTAG_GLOBALS,
14831483
*ctx.mainPart->memtagGlobalDescriptors);
14841484
addInt(DT_AARCH64_MEMTAG_GLOBALSSZ,
1485-
ctx.mainPart->memtagGlobalDescriptors->getSize());
1485+
ctx.mainPart->memtagGlobalDescriptors->getSize(ctx));
14861486
}
14871487
}
14881488
}
14891489

14901490
addInSec(DT_SYMTAB, *part.dynSymTab);
14911491
addInt(DT_SYMENT, sizeof(Elf_Sym));
14921492
addInSec(DT_STRTAB, *part.dynStrTab);
1493-
addInt(DT_STRSZ, part.dynStrTab->getSize());
1493+
addInt(DT_STRSZ, part.dynStrTab->getSize(ctx));
14941494
if (!ctx.arg.zText)
14951495
addInt(DT_TEXTREL, 0);
14961496
if (part.gnuHashTab && part.gnuHashTab->getParent())
@@ -2361,7 +2361,7 @@ void SymtabShndxSection::finalizeContents(Ctx &) {
23612361
getParent()->link = ctx.in.symTab->getParent()->sectionIndex;
23622362
}
23632363

2364-
size_t SymtabShndxSection::getSize() const {
2364+
size_t SymtabShndxSection::getSize(Ctx &ctx) const {
23652365
return ctx.in.symTab->getNumSymbols() * 4;
23662366
}
23672367

@@ -2583,7 +2583,7 @@ void PltSection::addEntry(Symbol &sym) {
25832583
entries.push_back(&sym);
25842584
}
25852585

2586-
size_t PltSection::getSize() const {
2586+
size_t PltSection::getSize(Ctx &ctx) const {
25872587
return headerSize + entries.size() * ctx.target->pltEntrySize;
25882588
}
25892589

@@ -2620,7 +2620,7 @@ void IpltSection::writeTo(Ctx &ctx, uint8_t *buf) {
26202620
}
26212621
}
26222622

2623-
size_t IpltSection::getSize() const {
2623+
size_t IpltSection::getSize(Ctx &ctx) const {
26242624
return entries.size() * ctx.target->ipltEntrySize;
26252625
}
26262626

@@ -2648,7 +2648,7 @@ void PPC32GlinkSection::writeTo(Ctx &ctx, uint8_t *buf) {
26482648
writePPC32GlinkSection(buf, entries.size());
26492649
}
26502650

2651-
size_t PPC32GlinkSection::getSize() const {
2651+
size_t PPC32GlinkSection::getSize(Ctx &ctx) const {
26522652
return headerSize + entries.size() * ctx.target->pltEntrySize + footerSize;
26532653
}
26542654

@@ -2717,7 +2717,7 @@ void IBTPltSection::writeTo(Ctx &ctx, uint8_t *buf) {
27172717
ctx.target->writeIBTPlt(buf, ctx.in.plt->getNumEntries());
27182718
}
27192719

2720-
size_t IBTPltSection::getSize() const {
2720+
size_t IBTPltSection::getSize(Ctx &ctx) const {
27212721
// 16 is the header size of .plt.
27222722
return 16 + ctx.in.plt->getNumEntries() * ctx.target->pltEntrySize;
27232723
}
@@ -3662,7 +3662,7 @@ void EhFrameHeader::write() {
36623662
}
36633663
}
36643664

3665-
size_t EhFrameHeader::getSize() const {
3665+
size_t EhFrameHeader::getSize(Ctx &ctx) const {
36663666
// .eh_frame_hdr has a 12 bytes header followed by an array of FDEs.
36673667
return 12 + getPartition().ehFrame->numFdes * 8;
36683668
}
@@ -3728,7 +3728,7 @@ void VersionDefinitionSection::writeTo(Ctx &ctx, uint8_t *buf) {
37283728
write32(buf + 16, 0); // vd_next
37293729
}
37303730

3731-
size_t VersionDefinitionSection::getSize() const {
3731+
size_t VersionDefinitionSection::getSize(Ctx &ctx) const {
37323732
return EntrySize * getVerDefNum();
37333733
}
37343734

@@ -3745,7 +3745,7 @@ void VersionTableSection::finalizeContents(Ctx &) {
37453745
getParent()->link = getPartition().dynSymTab->getParent()->sectionIndex;
37463746
}
37473747

3748-
size_t VersionTableSection::getSize() const {
3748+
size_t VersionTableSection::getSize(Ctx &ctx) const {
37493749
return (getPartition().dynSymTab->getSymbols().size() + 1) * 2;
37503750
}
37513751

@@ -3852,7 +3852,7 @@ void VersionNeedSection<ELFT>::writeTo(Ctx &ctx, uint8_t *buf) {
38523852
verneed[-1].vn_next = 0;
38533853
}
38543854

3855-
template <class ELFT> size_t VersionNeedSection<ELFT>::getSize() const {
3855+
template <class ELFT> size_t VersionNeedSection<ELFT>::getSize(Ctx &ctx) const {
38563856
return verneeds.size() * sizeof(Elf_Verneed) +
38573857
SharedFile::vernauxNum * sizeof(Elf_Vernaux);
38583858
}
@@ -3873,9 +3873,9 @@ MergeTailSection::MergeTailSection(StringRef name, uint32_t type,
38733873
: MergeSyntheticSection(name, type, flags, alignment),
38743874
builder(StringTableBuilder::RAW, llvm::Align(alignment)) {}
38753875

3876-
size_t MergeTailSection::getSize() const { return builder.getSize(); }
3876+
size_t MergeTailSection::getSize(Ctx &) const { return builder.getSize(); }
38773877

3878-
void MergeTailSection::writeTo(Ctx &ctx, uint8_t *buf) { builder.write(buf); }
3878+
void MergeTailSection::writeTo(Ctx &, uint8_t *buf) { builder.write(buf); }
38793879

38803880
void MergeTailSection::finalizeContents(Ctx &) {
38813881
// Add all string pieces to the string table builder to create section
@@ -4228,7 +4228,7 @@ ThunkSection::ThunkSection(OutputSection *os, uint64_t off)
42284228
this->outSecOff = off;
42294229
}
42304230

4231-
size_t ThunkSection::getSize() const {
4231+
size_t ThunkSection::getSize(Ctx &) const {
42324232
if (roundUpSizeForErrata)
42334233
return alignTo(size, 4096);
42344234
return size;
@@ -4318,7 +4318,7 @@ PPC64LongBranchTargetSection::addEntry(const Symbol *sym, int64_t addend) {
43184318
return res.first->second;
43194319
}
43204320

4321-
size_t PPC64LongBranchTargetSection::getSize() const {
4321+
size_t PPC64LongBranchTargetSection::getSize(Ctx &ctx) const {
43224322
return entries.size() * 8;
43234323
}
43244324

@@ -4415,7 +4415,7 @@ PartitionElfHeaderSection<ELFT>::PartitionElfHeaderSection()
44154415
: SyntheticSection(SHF_ALLOC, SHT_LLVM_PART_EHDR, 1, "") {}
44164416

44174417
template <typename ELFT>
4418-
size_t PartitionElfHeaderSection<ELFT>::getSize() const {
4418+
size_t PartitionElfHeaderSection<ELFT>::getSize(Ctx &ctx) const {
44194419
return sizeof(typename ELFT::Ehdr);
44204420
}
44214421

@@ -4433,7 +4433,7 @@ PartitionProgramHeadersSection<ELFT>::PartitionProgramHeadersSection()
44334433
: SyntheticSection(SHF_ALLOC, SHT_LLVM_PART_PHDR, 1, ".phdrs") {}
44344434

44354435
template <typename ELFT>
4436-
size_t PartitionProgramHeadersSection<ELFT>::getSize() const {
4436+
size_t PartitionProgramHeadersSection<ELFT>::getSize(Ctx &ctx) const {
44374437
return sizeof(typename ELFT::Phdr) * getPartition().phdrs.size();
44384438
}
44394439

@@ -4445,7 +4445,7 @@ void PartitionProgramHeadersSection<ELFT>::writeTo(Ctx &ctx, uint8_t *buf) {
44454445
PartitionIndexSection::PartitionIndexSection()
44464446
: SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 4, ".rodata") {}
44474447

4448-
size_t PartitionIndexSection::getSize() const {
4448+
size_t PartitionIndexSection::getSize(Ctx &ctx) const {
44494449
return 12 * (ctx.partitions.size() - 1);
44504450
}
44514451

@@ -4548,7 +4548,7 @@ void MemtagAndroidNote::writeTo(Ctx &ctx, uint8_t *buf) {
45484548
write32(buf, value); // note value
45494549
}
45504550

4551-
size_t MemtagAndroidNote::getSize() const {
4551+
size_t MemtagAndroidNote::getSize(Ctx &ctx) const {
45524552
return sizeof(llvm::ELF::Elf64_Nhdr) +
45534553
/*namesz=*/alignTo(sizeof(kMemtagAndroidNoteName), 4) +
45544554
/*descsz=*/sizeof(uint32_t);
@@ -4563,7 +4563,7 @@ void PackageMetadataNote::writeTo(Ctx &ctx, uint8_t *buf) {
45634563
ctx.arg.packageMetadata.size());
45644564
}
45654565

4566-
size_t PackageMetadataNote::getSize() const {
4566+
size_t PackageMetadataNote::getSize(Ctx &ctx) const {
45674567
return sizeof(llvm::ELF::Elf64_Nhdr) + 4 +
45684568
alignTo(ctx.arg.packageMetadata.size() + 1, 4);
45694569
}
@@ -4623,19 +4623,19 @@ createMemtagGlobalDescriptors(const SmallVector<const Symbol *, 0> &symbols,
46234623
}
46244624

46254625
bool MemtagGlobalDescriptors::updateAllocSize() {
4626-
size_t oldSize = getSize();
4626+
size_t oldSize = getSize(ctx);
46274627
std::stable_sort(symbols.begin(), symbols.end(),
46284628
[](const Symbol *s1, const Symbol *s2) {
46294629
return s1->getVA() < s2->getVA();
46304630
});
4631-
return oldSize != getSize();
4631+
return oldSize != getSize(ctx);
46324632
}
46334633

46344634
void MemtagGlobalDescriptors::writeTo(Ctx &ctx, uint8_t *buf) {
46354635
createMemtagGlobalDescriptors(symbols, buf);
46364636
}
46374637

4638-
size_t MemtagGlobalDescriptors::getSize() const {
4638+
size_t MemtagGlobalDescriptors::getSize(Ctx &ctx) const {
46394639
return createMemtagGlobalDescriptors(symbols);
46404640
}
46414641

@@ -4831,7 +4831,7 @@ template <class ELFT> void elf::createSyntheticSections(Ctx &ctx) {
48314831
ctx.in.partIndex = std::make_unique<PartitionIndexSection>();
48324832
addOptionalRegular("__part_index_begin", ctx.in.partIndex.get(), 0);
48334833
addOptionalRegular("__part_index_end", ctx.in.partIndex.get(),
4834-
ctx.in.partIndex->getSize());
4834+
ctx.in.partIndex->getSize(ctx));
48354835
add(*ctx.in.partIndex);
48364836
}
48374837

0 commit comments

Comments
 (0)