Skip to content

Commit 5f63461

Browse files
committed
[ELF] Pass Ctx & to SyntheticSections
1 parent f1dccda commit 5f63461

File tree

5 files changed

+83
-79
lines changed

5 files changed

+83
-79
lines changed

lld/ELF/InputSection.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,10 +477,10 @@ class SyntheticSection : public InputSection {
477477

478478
virtual ~SyntheticSection() = default;
479479
virtual size_t getSize(Ctx &) const = 0;
480-
virtual bool updateAllocSize() { return false; }
480+
virtual bool updateAllocSize(Ctx &) { return false; }
481481
// If the section has the SHF_ALLOC flag and the size may be changed if
482482
// thunks are added, update the section size.
483-
virtual bool isNeeded() const { return true; }
483+
virtual bool isNeeded(Ctx &) const { return true; }
484484
virtual void finalizeContents(Ctx &) {}
485485
virtual void writeTo(Ctx &, uint8_t *buf) = 0;
486486

lld/ELF/LinkerScript.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ void LinkerScript::diagnoseOrphanHandling() const {
10551055
}
10561056

10571057
void LinkerScript::diagnoseMissingSGSectionAddress() const {
1058-
if (!ctx.arg.cmseImplib || !ctx.in.armCmseSGSection->isNeeded())
1058+
if (!ctx.arg.cmseImplib || !ctx.in.armCmseSGSection->isNeeded(ctx))
10591059
return;
10601060

10611061
OutputSection *sec = findByName(sectionCommands, ".gnu.sgstubs");

lld/ELF/SyntheticSections.cpp

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ void MipsAbiFlagsSection<ELFT>::writeTo(Ctx &ctx, uint8_t *buf) {
108108
}
109109

110110
template <class ELFT>
111-
std::unique_ptr<MipsAbiFlagsSection<ELFT>> MipsAbiFlagsSection<ELFT>::create() {
111+
std::unique_ptr<MipsAbiFlagsSection<ELFT>>
112+
MipsAbiFlagsSection<ELFT>::create(Ctx &ctx) {
112113
Elf_Mips_ABIFlags flags = {};
113114
bool create = false;
114115

@@ -175,7 +176,8 @@ void MipsOptionsSection<ELFT>::writeTo(Ctx &ctx, uint8_t *buf) {
175176
}
176177

177178
template <class ELFT>
178-
std::unique_ptr<MipsOptionsSection<ELFT>> MipsOptionsSection<ELFT>::create() {
179+
std::unique_ptr<MipsOptionsSection<ELFT>>
180+
MipsOptionsSection<ELFT>::create(Ctx &ctx) {
179181
// N64 ABI only.
180182
if (!ELFT::Is64Bits)
181183
return nullptr;
@@ -233,7 +235,8 @@ void MipsReginfoSection<ELFT>::writeTo(Ctx &ctx, uint8_t *buf) {
233235
}
234236

235237
template <class ELFT>
236-
std::unique_ptr<MipsReginfoSection<ELFT>> MipsReginfoSection<ELFT>::create() {
238+
std::unique_ptr<MipsReginfoSection<ELFT>>
239+
MipsReginfoSection<ELFT>::create(Ctx &ctx) {
237240
// Section should be alive for O32 and N32 ABIs only.
238241
if (ELFT::Is64Bits)
239242
return nullptr;
@@ -712,7 +715,7 @@ void GotSection::finalizeContents(Ctx &) {
712715
size = numEntries * ctx.arg.wordsize;
713716
}
714717

715-
bool GotSection::isNeeded() const {
718+
bool GotSection::isNeeded(Ctx &ctx) const {
716719
// Needed if the GOT symbol is used or the number of entries is more than just
717720
// the header. A GOT with just the header may not be needed.
718721
return hasGotOffRel || numEntries > ctx.target->gotHeaderEntriesNum;
@@ -872,9 +875,9 @@ bool MipsGotSection::tryMergeGots(FileGot &dst, FileGot &src, bool isPrimary) {
872875
return true;
873876
}
874877

875-
void MipsGotSection::finalizeContents(Ctx &) { updateAllocSize(); }
878+
void MipsGotSection::finalizeContents(Ctx &) { updateAllocSize(ctx); }
876879

877-
bool MipsGotSection::updateAllocSize() {
880+
bool MipsGotSection::updateAllocSize(Ctx &ctx) {
878881
size = headerEntriesNum * ctx.arg.wordsize;
879882
for (const FileGot &g : gots)
880883
size += g.getEntriesNum() * ctx.arg.wordsize;
@@ -1084,7 +1087,7 @@ void MipsGotSection::build() {
10841087
}
10851088
}
10861089

1087-
bool MipsGotSection::isNeeded() const {
1090+
bool MipsGotSection::isNeeded(Ctx &ctx) const {
10881091
// We add the .got section to the result for dynamic MIPS target because
10891092
// its address and properties are mentioned in the .dynamic section.
10901093
return !ctx.arg.relocatable;
@@ -1197,7 +1200,7 @@ void GotPltSection::writeTo(Ctx &ctx, uint8_t *buf) {
11971200
}
11981201
}
11991202

1200-
bool GotPltSection::isNeeded() const {
1203+
bool GotPltSection::isNeeded(Ctx &) const {
12011204
// We need to emit GOTPLT even if it's empty if there's a relocation relative
12021205
// to it.
12031206
return !entries.empty() || hasGotPltOffRel;
@@ -1400,7 +1403,7 @@ DynamicSection<ELFT>::computeContents() {
14001403
if (!ctx.arg.shared && !ctx.arg.relocatable && !ctx.arg.zRodynamic)
14011404
addInt(DT_DEBUG, 0);
14021405

1403-
if (part.relaDyn->isNeeded()) {
1406+
if (part.relaDyn->isNeeded(ctx)) {
14041407
addInSec(part.relaDyn->dynamicTag, *part.relaDyn);
14051408
entries.emplace_back(part.relaDyn->sizeDynamicTag,
14061409
addRelaSz(*part.relaDyn));
@@ -1433,7 +1436,7 @@ DynamicSection<ELFT>::computeContents() {
14331436
addInt(DT_AARCH64_AUTH_RELRSZ, part.relrAuthDyn->getParent()->size);
14341437
addInt(DT_AARCH64_AUTH_RELRENT, sizeof(Elf_Relr));
14351438
}
1436-
if (isMain && ctx.in.relaPlt->isNeeded()) {
1439+
if (isMain && ctx.in.relaPlt->isNeeded(ctx)) {
14371440
addInSec(DT_JMPREL, *ctx.in.relaPlt);
14381441
entries.emplace_back(DT_PLTRELSZ, addPltRelSz());
14391442
switch (ctx.arg.emachine) {
@@ -1478,7 +1481,7 @@ DynamicSection<ELFT>::computeContents() {
14781481
addInt(DT_AARCH64_MEMTAG_MODE, ctx.arg.androidMemtagMode == NT_MEMTAG_LEVEL_ASYNC);
14791482
addInt(DT_AARCH64_MEMTAG_HEAP, ctx.arg.androidMemtagHeap);
14801483
addInt(DT_AARCH64_MEMTAG_STACK, ctx.arg.androidMemtagStack);
1481-
if (ctx.mainPart->memtagGlobalDescriptors->isNeeded()) {
1484+
if (ctx.mainPart->memtagGlobalDescriptors->isNeeded(ctx)) {
14821485
addInSec(DT_AARCH64_MEMTAG_GLOBALS,
14831486
*ctx.mainPart->memtagGlobalDescriptors);
14841487
addInt(DT_AARCH64_MEMTAG_GLOBALSSZ,
@@ -1520,13 +1523,13 @@ DynamicSection<ELFT>::computeContents() {
15201523
addInt(DT_FINI, b->getVA());
15211524
}
15221525

1523-
if (part.verSym && part.verSym->isNeeded())
1526+
if (part.verSym && part.verSym->isNeeded(ctx))
15241527
addInSec(DT_VERSYM, *part.verSym);
15251528
if (part.verDef && part.verDef->isLive()) {
15261529
addInSec(DT_VERDEF, *part.verDef);
15271530
addInt(DT_VERDEFNUM, getVerDefNum());
15281531
}
1529-
if (part.verNeed && part.verNeed->isNeeded()) {
1532+
if (part.verNeed && part.verNeed->isNeeded(ctx)) {
15301533
addInSec(DT_VERNEED, *part.verNeed);
15311534
unsigned needNum = 0;
15321535
for (SharedFile *f : ctx.sharedFiles)
@@ -1563,7 +1566,7 @@ DynamicSection<ELFT>::computeContents() {
15631566
addInSec(DT_PPC_GOT, *ctx.in.got);
15641567

15651568
// Glink dynamic tag is required by the V2 abi if the plt section isn't empty.
1566-
if (ctx.arg.emachine == EM_PPC64 && ctx.in.plt->isNeeded()) {
1569+
if (ctx.arg.emachine == EM_PPC64 && ctx.in.plt->isNeeded(ctx)) {
15671570
// The Glink tag points to 32 bytes before the first lazy symbol resolution
15681571
// stub, which starts directly after the header.
15691572
addInt(DT_PPC64_GLINK,
@@ -1780,7 +1783,7 @@ AndroidPackedRelocationSection<ELFT>::AndroidPackedRelocationSection(
17801783
}
17811784

17821785
template <class ELFT>
1783-
bool AndroidPackedRelocationSection<ELFT>::updateAllocSize() {
1786+
bool AndroidPackedRelocationSection<ELFT>::updateAllocSize(Ctx &ctx) {
17841787
// This function computes the contents of an Android-format packed relocation
17851788
// section.
17861789
//
@@ -2028,7 +2031,7 @@ RelrSection<ELFT>::RelrSection(unsigned concurrency, bool isAArch64Auth)
20282031
this->entsize = ctx.arg.wordsize;
20292032
}
20302033

2031-
template <class ELFT> bool RelrSection<ELFT>::updateAllocSize() {
2034+
template <class ELFT> bool RelrSection<ELFT>::updateAllocSize(Ctx &ctx) {
20322035
// This function computes the contents of an SHT_RELR packed relocation
20332036
// section.
20342037
//
@@ -2344,7 +2347,7 @@ void SymtabShndxSection::writeTo(Ctx &ctx, uint8_t *buf) {
23442347
}
23452348
}
23462349

2347-
bool SymtabShndxSection::isNeeded() const {
2350+
bool SymtabShndxSection::isNeeded(Ctx &ctx) const {
23482351
// SHT_SYMTAB can hold symbols with section indices values up to
23492352
// SHN_LORESERVE. If we need more, we want to use extension SHT_SYMTAB_SHNDX
23502353
// section. Problem is that we reveal the final section indices a bit too
@@ -2587,9 +2590,10 @@ size_t PltSection::getSize(Ctx &ctx) const {
25872590
return headerSize + entries.size() * ctx.target->pltEntrySize;
25882591
}
25892592

2590-
bool PltSection::isNeeded() const {
2593+
bool PltSection::isNeeded(Ctx &ctx) const {
25912594
// For -z retpolineplt, .iplt needs the .plt header.
2592-
return !entries.empty() || (ctx.arg.zRetpolineplt && ctx.in.iplt->isNeeded());
2595+
return !entries.empty() ||
2596+
(ctx.arg.zRetpolineplt && ctx.in.iplt->isNeeded(ctx));
25932597
}
25942598

25952599
// Used by ARM to add mapping symbols in the PLT section, which aid
@@ -2722,7 +2726,9 @@ size_t IBTPltSection::getSize(Ctx &ctx) const {
27222726
return 16 + ctx.in.plt->getNumEntries() * ctx.target->pltEntrySize;
27232727
}
27242728

2725-
bool IBTPltSection::isNeeded() const { return ctx.in.plt->getNumEntries() > 0; }
2729+
bool IBTPltSection::isNeeded(Ctx &ctx) const {
2730+
return ctx.in.plt->getNumEntries() > 0;
2731+
}
27262732

27272733
RelroPaddingSection::RelroPaddingSection()
27282734
: SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, 1, ".relro_padding") {
@@ -3624,7 +3630,7 @@ void GdbIndexSection::writeTo(Ctx &ctx, uint8_t *buf) {
36243630
}
36253631
}
36263632

3627-
bool GdbIndexSection::isNeeded() const { return !chunks.empty(); }
3633+
bool GdbIndexSection::isNeeded(Ctx &) const { return !chunks.empty(); }
36283634

36293635
EhFrameHeader::EhFrameHeader()
36303636
: SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 4, ".eh_frame_hdr") {}
@@ -3667,8 +3673,8 @@ size_t EhFrameHeader::getSize(Ctx &ctx) const {
36673673
return 12 + getPartition().ehFrame->numFdes * 8;
36683674
}
36693675

3670-
bool EhFrameHeader::isNeeded() const {
3671-
return isLive() && getPartition().ehFrame->isNeeded();
3676+
bool EhFrameHeader::isNeeded(Ctx &ctx) const {
3677+
return isLive() && getPartition().ehFrame->isNeeded(ctx);
36723678
}
36733679

36743680
VersionDefinitionSection::VersionDefinitionSection()
@@ -3760,9 +3766,9 @@ void VersionTableSection::writeTo(Ctx &ctx, uint8_t *buf) {
37603766
}
37613767
}
37623768

3763-
bool VersionTableSection::isNeeded() const {
3769+
bool VersionTableSection::isNeeded(Ctx &ctx) const {
37643770
return isLive() &&
3765-
(getPartition().verDef || getPartition().verNeed->isNeeded());
3771+
(getPartition().verDef || getPartition().verNeed->isNeeded(ctx));
37663772
}
37673773

37683774
void elf::addVerneed(Symbol *ss) {
@@ -3857,7 +3863,7 @@ template <class ELFT> size_t VersionNeedSection<ELFT>::getSize(Ctx &ctx) const {
38573863
SharedFile::vernauxNum * sizeof(Elf_Vernaux);
38583864
}
38593865

3860-
template <class ELFT> bool VersionNeedSection<ELFT>::isNeeded() const {
3866+
template <class ELFT> bool VersionNeedSection<ELFT>::isNeeded(Ctx &) const {
38613867
return isLive() && SharedFile::vernauxNum != 0;
38623868
}
38633869

@@ -4216,7 +4222,7 @@ void ARMExidxSyntheticSection::writeTo(Ctx &ctx, uint8_t *buf) {
42164222
assert(size == offset + 8);
42174223
}
42184224

4219-
bool ARMExidxSyntheticSection::isNeeded() const {
4225+
bool ARMExidxSyntheticSection::isNeeded(Ctx &) const {
42204226
return llvm::any_of(exidxSections,
42214227
[](InputSection *isec) { return isec->isLive(); });
42224228
}
@@ -4268,7 +4274,7 @@ bool ThunkSection::assignOffsets() {
42684274
PPC32Got2Section::PPC32Got2Section()
42694275
: SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 4, ".got2") {}
42704276

4271-
bool PPC32Got2Section::isNeeded() const {
4277+
bool PPC32Got2Section::isNeeded(Ctx &) const {
42724278
// See the comment below. This is not needed if there is no other
42734279
// InputSection.
42744280
for (SectionCommand *cmd : getParent()->commands)
@@ -4341,7 +4347,7 @@ void PPC64LongBranchTargetSection::writeTo(Ctx &ctx, uint8_t *buf) {
43414347
}
43424348
}
43434349

4344-
bool PPC64LongBranchTargetSection::isNeeded() const {
4350+
bool PPC64LongBranchTargetSection::isNeeded(Ctx &) const {
43454351
// `removeUnusedSyntheticSections()` is called before thunk allocation which
43464352
// is too early to determine if this section will be empty or not. We need
43474353
// Finalized to keep the section alive until after thunk creation. Finalized
@@ -4622,7 +4628,7 @@ createMemtagGlobalDescriptors(const SmallVector<const Symbol *, 0> &symbols,
46224628
return sectionSize;
46234629
}
46244630

4625-
bool MemtagGlobalDescriptors::updateAllocSize() {
4631+
bool MemtagGlobalDescriptors::updateAllocSize(Ctx &ctx) {
46264632
size_t oldSize = getSize(ctx);
46274633
std::stable_sort(symbols.begin(), symbols.end(),
46284634
[](const Symbol *s1, const Symbol *s2) {
@@ -4704,11 +4710,11 @@ template <class ELFT> void elf::createSyntheticSections(Ctx &ctx) {
47044710
ctx.in.mipsRldMap = std::make_unique<MipsRldMapSection>();
47054711
add(*ctx.in.mipsRldMap);
47064712
}
4707-
if ((ctx.in.mipsAbiFlags = MipsAbiFlagsSection<ELFT>::create()))
4713+
if ((ctx.in.mipsAbiFlags = MipsAbiFlagsSection<ELFT>::create(ctx)))
47084714
add(*ctx.in.mipsAbiFlags);
4709-
if ((ctx.in.mipsOptions = MipsOptionsSection<ELFT>::create()))
4715+
if ((ctx.in.mipsOptions = MipsOptionsSection<ELFT>::create(ctx)))
47104716
add(*ctx.in.mipsOptions);
4711-
if ((ctx.in.mipsReginfo = MipsReginfoSection<ELFT>::create()))
4717+
if ((ctx.in.mipsReginfo = MipsReginfoSection<ELFT>::create(ctx)))
47124718
add(*ctx.in.mipsReginfo);
47134719
}
47144720

0 commit comments

Comments
 (0)