Skip to content

Commit c62fa63

Browse files
committed
[ELF] Move mainPart to Ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such singletons.
1 parent bf88db7 commit c62fa63

File tree

12 files changed

+72
-64
lines changed

12 files changed

+72
-64
lines changed

lld/ELF/Arch/PPC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ void PPC::writeGotHeader(uint8_t *buf) const {
187187
// _GLOBAL_OFFSET_TABLE_[0] = _DYNAMIC
188188
// glibc stores _dl_runtime_resolve in _GLOBAL_OFFSET_TABLE_[1],
189189
// link_map in _GLOBAL_OFFSET_TABLE_[2].
190-
write32(buf, mainPart->dynamic->getVA());
190+
write32(buf, ctx.mainPart->dynamic->getVA());
191191
}
192192

193193
void PPC::writeGotPlt(uint8_t *buf, const Symbol &s) const {

lld/ELF/Arch/RISCV.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,9 @@ int64_t RISCV::getImplicitAddend(const uint8_t *buf, RelType type) const {
200200

201201
void RISCV::writeGotHeader(uint8_t *buf) const {
202202
if (config->is64)
203-
write64le(buf, mainPart->dynamic->getVA());
203+
write64le(buf, ctx.mainPart->dynamic->getVA());
204204
else
205-
write32le(buf, mainPart->dynamic->getVA());
205+
write32le(buf, ctx.mainPart->dynamic->getVA());
206206
}
207207

208208
void RISCV::writeGotPlt(uint8_t *buf, const Symbol &s) const {

lld/ELF/Arch/SystemZ.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ RelExpr SystemZ::getRelExpr(RelType type, const Symbol &s,
179179
void SystemZ::writeGotHeader(uint8_t *buf) const {
180180
// _GLOBAL_OFFSET_TABLE_[0] holds the value of _DYNAMIC.
181181
// _GLOBAL_OFFSET_TABLE_[1] and [2] are reserved.
182-
write64be(buf, mainPart->dynamic->getVA());
182+
write64be(buf, ctx.mainPart->dynamic->getVA());
183183
}
184184

185185
void SystemZ::writeGotPlt(uint8_t *buf, const Symbol &s) const {

lld/ELF/Arch/X86.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ RelExpr X86::adjustTlsExpr(RelType type, RelExpr expr) const {
164164
}
165165

166166
void X86::writeGotPltHeader(uint8_t *buf) const {
167-
write32le(buf, mainPart->dynamic->getVA());
167+
write32le(buf, ctx.mainPart->dynamic->getVA());
168168
}
169169

170170
void X86::writeGotPlt(uint8_t *buf, const Symbol &s) const {

lld/ELF/Arch/X86_64.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ void X86_64::writeGotPltHeader(uint8_t *buf) const {
411411
// in the psABI and glibc before Aug 2021 used the entry to compute run-time
412412
// load address of the shared object (note that this is relevant for linking
413413
// ld.so, not any other program).
414-
write64le(buf, mainPart->dynamic->getVA());
414+
write64le(buf, ctx.mainPart->dynamic->getVA());
415415
}
416416

417417
void X86_64::writeGotPlt(uint8_t *buf, const Symbol &s) const {

lld/ELF/Config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class Defined;
4646
class Symbol;
4747
class BitcodeCompiler;
4848
class OutputSection;
49+
struct Partition;
4950
struct PhdrEntry;
5051

5152
enum ELFKind : uint8_t {
@@ -486,6 +487,7 @@ struct Ctx {
486487
// These variables are initialized by Writer and should not be used before
487488
// Writer is initialized.
488489
uint8_t *bufferStart;
490+
Partition *mainPart;
489491
PhdrEntry *tlsPhdr;
490492
struct OutSections {
491493
OutputSection *elfHeader;

lld/ELF/Driver.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ void Ctx::reset() {
9595
driver = LinkerDriver();
9696

9797
bufferStart = nullptr;
98+
mainPart = nullptr;
9899
tlsPhdr = nullptr;
99100
out = OutSections{};
100101
outputSections.clear();
@@ -3094,7 +3095,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
30943095

30953096
// Now that the number of partitions is fixed, save a pointer to the main
30963097
// partition.
3097-
mainPart = &partitions[0];
3098+
ctx.mainPart = &partitions[0];
30983099

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

lld/ELF/Relocations.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ template <class ELFT> static void addCopyRelSymbol(SharedSymbol &ss) {
399399
for (SharedSymbol *sym : getSymbolsAt<ELFT>(ss))
400400
replaceWithDefined(*sym, *sec, 0, sym->size);
401401

402-
mainPart->relaDyn->addSymbolReloc(target->copyRel, *sec, 0, ss);
402+
ctx.mainPart->relaDyn->addSymbolReloc(target->copyRel, *sec, 0, ss);
403403
}
404404

405405
// .eh_frame sections are mergeable input sections, so their input
@@ -927,8 +927,9 @@ void elf::addGotEntry(Symbol &sym) {
927927

928928
// If preemptible, emit a GLOB_DAT relocation.
929929
if (sym.isPreemptible) {
930-
mainPart->relaDyn->addReloc({target->gotRel, in.got.get(), off,
931-
DynamicReloc::AgainstSymbol, sym, 0, R_ABS});
930+
ctx.mainPart->relaDyn->addReloc({target->gotRel, in.got.get(), off,
931+
DynamicReloc::AgainstSymbol, sym, 0,
932+
R_ABS});
932933
return;
933934
}
934935

@@ -947,7 +948,7 @@ static void addTpOffsetGotEntry(Symbol &sym) {
947948
in.got->addConstant({R_TPREL, target->symbolicRel, off, 0, &sym});
948949
return;
949950
}
950-
mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible(
951+
ctx.mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible(
951952
target->tlsGotRel, *in.got, off, sym, target->symbolicRel);
952953
}
953954

@@ -1085,7 +1086,8 @@ void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
10851086
if (LLVM_UNLIKELY(isIfunc) && config->zIfuncNoplt) {
10861087
std::lock_guard<std::mutex> lock(relocMutex);
10871088
sym.exportDynamic = true;
1088-
mainPart->relaDyn->addSymbolReloc(type, *sec, offset, sym, addend, type);
1089+
ctx.mainPart->relaDyn->addSymbolReloc(type, *sec, offset, sym, addend,
1090+
type);
10891091
return;
10901092
}
10911093

@@ -1727,7 +1729,8 @@ static bool handleNonPreemptibleIfunc(Symbol &sym, uint16_t flags) {
17271729
// IRELATIVE in .rela.plt.
17281730
auto *directSym = makeDefined(cast<Defined>(sym));
17291731
directSym->allocateAux();
1730-
auto &dyn = config->androidPackDynRelocs ? *in.relaPlt : *mainPart->relaDyn;
1732+
auto &dyn =
1733+
config->androidPackDynRelocs ? *in.relaPlt : *ctx.mainPart->relaDyn;
17311734
addPltEntry(*in.iplt, *in.igotPlt, dyn, target->iRelativeRel, *directSym);
17321735
sym.allocateAux();
17331736
ctx.symAux.back().pltIdx = ctx.symAux[directSym->auxIdx].pltIdx;
@@ -1758,7 +1761,7 @@ void elf::postScanRelocations() {
17581761
return;
17591762

17601763
if (sym.isTagged() && sym.isDefined())
1761-
mainPart->memtagGlobalDescriptors->addSymbol(sym);
1764+
ctx.mainPart->memtagGlobalDescriptors->addSymbol(sym);
17621765

17631766
if (!sym.needsDynReloc())
17641767
return;
@@ -1799,7 +1802,7 @@ void elf::postScanRelocations() {
17991802

18001803
if (flags & NEEDS_TLSDESC) {
18011804
got->addTlsDescEntry(sym);
1802-
mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible(
1805+
ctx.mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible(
18031806
target->tlsDescRel, *got, got->getTlsDescOffset(sym), sym,
18041807
target->tlsDescRel);
18051808
}
@@ -1810,22 +1813,22 @@ void elf::postScanRelocations() {
18101813
// Write one to the GOT slot.
18111814
got->addConstant({R_ADDEND, target->symbolicRel, off, 1, &sym});
18121815
else
1813-
mainPart->relaDyn->addSymbolReloc(target->tlsModuleIndexRel, *got, off,
1814-
sym);
1816+
ctx.mainPart->relaDyn->addSymbolReloc(target->tlsModuleIndexRel, *got,
1817+
off, sym);
18151818

18161819
// If the symbol is preemptible we need the dynamic linker to write
18171820
// the offset too.
18181821
uint64_t offsetOff = off + config->wordsize;
18191822
if (sym.isPreemptible)
1820-
mainPart->relaDyn->addSymbolReloc(target->tlsOffsetRel, *got, offsetOff,
1821-
sym);
1823+
ctx.mainPart->relaDyn->addSymbolReloc(target->tlsOffsetRel, *got,
1824+
offsetOff, sym);
18221825
else
18231826
got->addConstant({R_ABS, target->tlsOffsetRel, offsetOff, 0, &sym});
18241827
}
18251828
if (flags & NEEDS_TLSGD_TO_IE) {
18261829
got->addEntry(sym);
1827-
mainPart->relaDyn->addSymbolReloc(target->tlsGotRel, *got,
1828-
sym.getGotOffset(), sym);
1830+
ctx.mainPart->relaDyn->addSymbolReloc(target->tlsGotRel, *got,
1831+
sym.getGotOffset(), sym);
18291832
}
18301833
if (flags & NEEDS_GOT_DTPREL) {
18311834
got->addEntry(sym);
@@ -1841,7 +1844,7 @@ void elf::postScanRelocations() {
18411844
if (ctx.needsTlsLd.load(std::memory_order_relaxed) && got->addTlsIndex()) {
18421845
static Undefined dummy(ctx.internalFile, "", STB_LOCAL, 0, 0);
18431846
if (config->shared)
1844-
mainPart->relaDyn->addReloc(
1847+
ctx.mainPart->relaDyn->addReloc(
18451848
{target->tlsModuleIndexRel, got, got->getTlsIndexOff()});
18461849
else
18471850
got->addConstant(

lld/ELF/SyntheticSections.cpp

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,33 +1017,34 @@ void MipsGotSection::build() {
10171017
// for the TP-relative offset as we don't know how much other data will
10181018
// be allocated before us in the static TLS block.
10191019
if (s->isPreemptible || config->shared)
1020-
mainPart->relaDyn->addReloc({target->tlsGotRel, this, offset,
1021-
DynamicReloc::AgainstSymbolWithTargetVA,
1022-
*s, 0, R_ABS});
1020+
ctx.mainPart->relaDyn->addReloc(
1021+
{target->tlsGotRel, this, offset,
1022+
DynamicReloc::AgainstSymbolWithTargetVA, *s, 0, R_ABS});
10231023
}
10241024
for (std::pair<Symbol *, size_t> &p : got.dynTlsSymbols) {
10251025
Symbol *s = p.first;
10261026
uint64_t offset = p.second * config->wordsize;
10271027
if (s == nullptr) {
10281028
if (!config->shared)
10291029
continue;
1030-
mainPart->relaDyn->addReloc({target->tlsModuleIndexRel, this, offset});
1030+
ctx.mainPart->relaDyn->addReloc(
1031+
{target->tlsModuleIndexRel, this, offset});
10311032
} else {
10321033
// When building a shared library we still need a dynamic relocation
10331034
// for the module index. Therefore only checking for
10341035
// S->isPreemptible is not sufficient (this happens e.g. for
10351036
// thread-locals that have been marked as local through a linker script)
10361037
if (!s->isPreemptible && !config->shared)
10371038
continue;
1038-
mainPart->relaDyn->addSymbolReloc(target->tlsModuleIndexRel, *this,
1039-
offset, *s);
1039+
ctx.mainPart->relaDyn->addSymbolReloc(target->tlsModuleIndexRel, *this,
1040+
offset, *s);
10401041
// However, we can skip writing the TLS offset reloc for non-preemptible
10411042
// symbols since it is known even in shared libraries
10421043
if (!s->isPreemptible)
10431044
continue;
10441045
offset += config->wordsize;
1045-
mainPart->relaDyn->addSymbolReloc(target->tlsOffsetRel, *this, offset,
1046-
*s);
1046+
ctx.mainPart->relaDyn->addSymbolReloc(target->tlsOffsetRel, *this,
1047+
offset, *s);
10471048
}
10481049
}
10491050

@@ -1055,8 +1056,8 @@ void MipsGotSection::build() {
10551056
// Dynamic relocations for "global" entries.
10561057
for (const std::pair<Symbol *, size_t> &p : got.global) {
10571058
uint64_t offset = p.second * config->wordsize;
1058-
mainPart->relaDyn->addSymbolReloc(target->relativeRel, *this, offset,
1059-
*p.first);
1059+
ctx.mainPart->relaDyn->addSymbolReloc(target->relativeRel, *this, offset,
1060+
*p.first);
10601061
}
10611062
if (!config->isPic)
10621063
continue;
@@ -1066,15 +1067,15 @@ void MipsGotSection::build() {
10661067
size_t pageCount = l.second.count;
10671068
for (size_t pi = 0; pi < pageCount; ++pi) {
10681069
uint64_t offset = (l.second.firstIndex + pi) * config->wordsize;
1069-
mainPart->relaDyn->addReloc({target->relativeRel, this, offset, l.first,
1070-
int64_t(pi * 0x10000)});
1070+
ctx.mainPart->relaDyn->addReloc({target->relativeRel, this, offset,
1071+
l.first, int64_t(pi * 0x10000)});
10711072
}
10721073
}
10731074
for (const std::pair<GotEntry, size_t> &p : got.local16) {
10741075
uint64_t offset = p.second * config->wordsize;
1075-
mainPart->relaDyn->addReloc({target->relativeRel, this, offset,
1076-
DynamicReloc::AddendOnlyWithTargetVA,
1077-
*p.first.first, p.first.second, R_ABS});
1076+
ctx.mainPart->relaDyn->addReloc({target->relativeRel, this, offset,
1077+
DynamicReloc::AddendOnlyWithTargetVA,
1078+
*p.first.first, p.first.second, R_ABS});
10781079
}
10791080
}
10801081
}
@@ -1473,10 +1474,11 @@ DynamicSection<ELFT>::computeContents() {
14731474
addInt(DT_AARCH64_MEMTAG_MODE, config->androidMemtagMode == NT_MEMTAG_LEVEL_ASYNC);
14741475
addInt(DT_AARCH64_MEMTAG_HEAP, config->androidMemtagHeap);
14751476
addInt(DT_AARCH64_MEMTAG_STACK, config->androidMemtagStack);
1476-
if (mainPart->memtagGlobalDescriptors->isNeeded()) {
1477-
addInSec(DT_AARCH64_MEMTAG_GLOBALS, *mainPart->memtagGlobalDescriptors);
1477+
if (ctx.mainPart->memtagGlobalDescriptors->isNeeded()) {
1478+
addInSec(DT_AARCH64_MEMTAG_GLOBALS,
1479+
*ctx.mainPart->memtagGlobalDescriptors);
14781480
addInt(DT_AARCH64_MEMTAG_GLOBALSSZ,
1479-
mainPart->memtagGlobalDescriptors->getSize());
1481+
ctx.mainPart->memtagGlobalDescriptors->getSize());
14801482
}
14811483
}
14821484
}
@@ -1617,7 +1619,7 @@ uint32_t DynamicReloc::getSymIndex(SymbolTableBaseSection *symTab) const {
16171619

16181620
size_t index = symTab->getSymbolIndex(*sym);
16191621
assert((index != 0 || (type != target->gotRel && type != target->pltRel) ||
1620-
!mainPart->dynSymTab->getParent()) &&
1622+
!ctx.mainPart->dynSymTab->getParent()) &&
16211623
"GOT or PLT relocation must refer to symbol in dynamic symbol table");
16221624
return index;
16231625
}
@@ -2149,7 +2151,7 @@ void SymbolTableBaseSection::finalizeContents() {
21492151

21502152
// Only the main partition's dynsym indexes are stored in the symbols
21512153
// themselves. All other partitions use a lookup table.
2152-
if (this == mainPart->dynSymTab.get()) {
2154+
if (this == ctx.mainPart->dynSymTab.get()) {
21532155
size_t i = 0;
21542156
for (const SymbolTableEntry &s : symbols)
21552157
s.sym->dynsymIndex = ++i;
@@ -2193,7 +2195,7 @@ void SymbolTableBaseSection::addSymbol(Symbol *b) {
21932195
}
21942196

21952197
size_t SymbolTableBaseSection::getSymbolIndex(const Symbol &sym) {
2196-
if (this == mainPart->dynSymTab.get())
2198+
if (this == ctx.mainPart->dynSymTab.get())
21972199
return sym.dynsymIndex;
21982200

21992201
// Initializes symbol lookup tables lazily. This is used only for -r,
@@ -3968,7 +3970,7 @@ void elf::combineEhSections() {
39683970
llvm::append_range(eh.dependentSections, sec->dependentSections);
39693971
}
39703972

3971-
if (!mainPart->armExidx)
3973+
if (!ctx.mainPart->armExidx)
39723974
return;
39733975
llvm::erase_if(ctx.inputSections, [](InputSectionBase *s) {
39743976
// Ignore dead sections and the partition end marker (.part.end),
@@ -4439,13 +4441,15 @@ size_t PartitionIndexSection::getSize() const {
44394441

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

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

44514455
SyntheticSection *next = i == partitions.size() - 1
@@ -4922,7 +4926,6 @@ template <class ELFT> void elf::createSyntheticSections() {
49224926
InStruct elf::in;
49234927

49244928
std::vector<Partition> elf::partitions;
4925-
Partition *elf::mainPart;
49264929

49274930
template void elf::splitSections<ELF32LE>();
49284931
template void elf::splitSections<ELF32BE>();

lld/ELF/SyntheticSections.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,8 +1474,6 @@ struct Partition {
14741474
unsigned getNumber() const { return this - &partitions[0] + 1; }
14751475
};
14761476

1477-
LLVM_LIBRARY_VISIBILITY extern Partition *mainPart;
1478-
14791477
inline Partition &SectionBase::getPartition() const {
14801478
assert(isLive());
14811479
return partitions[partition - 1];

lld/ELF/Thunks.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ class PPC64PILongBranchThunk final : public PPC64LongBranchThunk {
474474
assert(!dest.isPreemptible);
475475
if (std::optional<uint32_t> index =
476476
in.ppc64LongBranchTarget->addEntry(&dest, addend)) {
477-
mainPart->relaDyn->addRelativeReloc(
477+
ctx.mainPart->relaDyn->addRelativeReloc(
478478
target->relativeRel, *in.ppc64LongBranchTarget, *index * UINT64_C(8),
479479
dest, addend + getPPC64GlobalEntryToLocalEntryOffset(dest.stOther),
480480
target->symbolicRel, R_ABS);

0 commit comments

Comments
 (0)