Skip to content

Commit 2531b46

Browse files
committed
[ELF] Move InStruct into Ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such singletons. `#define in ctx.sec` is used for now to avoid migrating `in.xxx`.
1 parent 1cd0752 commit 2531b46

File tree

5 files changed

+67
-47
lines changed

5 files changed

+67
-47
lines changed

lld/ELF/Config.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@ class TargetInfo;
5151
struct Partition;
5252
struct PhdrEntry;
5353

54+
class BssSection;
55+
class GdbIndexSection;
56+
class GotPltSection;
57+
class GotSection;
58+
class IBTPltSection;
59+
class IgotPltSection;
60+
class InputSection;
61+
class IpltSection;
62+
class MipsGotSection;
63+
class MipsRldMapSection;
64+
class PPC32Got2Section;
65+
class PPC64LongBranchTargetSection;
66+
class PltSection;
67+
class RelocationBaseSection;
68+
class RelroPaddingSection;
69+
class StringTableSection;
70+
class SymbolTableBaseSection;
71+
class SymtabShndxSection;
72+
class SyntheticSection;
73+
5474
enum ELFKind : uint8_t {
5575
ELFNoneKind,
5676
ELF32LEKind,
@@ -483,6 +503,42 @@ struct DuplicateSymbol {
483503
uint64_t value;
484504
};
485505

506+
// Linker generated sections which can be used as inputs and are not specific to
507+
// a partition.
508+
struct InStruct {
509+
std::unique_ptr<InputSection> attributes;
510+
std::unique_ptr<SyntheticSection> riscvAttributes;
511+
std::unique_ptr<BssSection> bss;
512+
std::unique_ptr<BssSection> bssRelRo;
513+
std::unique_ptr<GotSection> got;
514+
std::unique_ptr<GotPltSection> gotPlt;
515+
std::unique_ptr<IgotPltSection> igotPlt;
516+
std::unique_ptr<RelroPaddingSection> relroPadding;
517+
std::unique_ptr<SyntheticSection> armCmseSGSection;
518+
std::unique_ptr<PPC64LongBranchTargetSection> ppc64LongBranchTarget;
519+
std::unique_ptr<SyntheticSection> mipsAbiFlags;
520+
std::unique_ptr<MipsGotSection> mipsGot;
521+
std::unique_ptr<SyntheticSection> mipsOptions;
522+
std::unique_ptr<SyntheticSection> mipsReginfo;
523+
std::unique_ptr<MipsRldMapSection> mipsRldMap;
524+
std::unique_ptr<SyntheticSection> partEnd;
525+
std::unique_ptr<SyntheticSection> partIndex;
526+
std::unique_ptr<PltSection> plt;
527+
std::unique_ptr<IpltSection> iplt;
528+
std::unique_ptr<PPC32Got2Section> ppc32Got2;
529+
std::unique_ptr<IBTPltSection> ibtPlt;
530+
std::unique_ptr<RelocationBaseSection> relaPlt;
531+
// Non-SHF_ALLOC sections
532+
std::unique_ptr<SyntheticSection> debugNames;
533+
std::unique_ptr<GdbIndexSection> gdbIndex;
534+
std::unique_ptr<StringTableSection> shStrTab;
535+
std::unique_ptr<StringTableSection> strTab;
536+
std::unique_ptr<SymbolTableBaseSection> symTab;
537+
std::unique_ptr<SymtabShndxSection> symTabShndx;
538+
539+
void reset();
540+
};
541+
486542
struct Ctx {
487543
LinkerDriver driver;
488544
LinkerScript *script;
@@ -504,6 +560,8 @@ struct Ctx {
504560
SmallVector<OutputSection *, 0> outputSections;
505561
std::vector<Partition> partitions;
506562

563+
InStruct sec;
564+
507565
// Some linker-generated symbols need to be created as
508566
// Defined symbols.
509567
struct ElfSym {

lld/ELF/Driver.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ void Ctx::reset() {
103103
outputSections.clear();
104104
partitions.clear();
105105

106+
in.reset();
106107
sym = ElfSym{};
107108

108109
memoryBuffers.clear();
@@ -152,8 +153,6 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
152153
elf::ctx.partitions.emplace_back();
153154
symtab = SymbolTable();
154155

155-
in.reset();
156-
157156
SharedFile::vernauxNum = 0;
158157
};
159158
ctx->e.logName = args::getFilenameWithoutExe(args[0]);

lld/ELF/OutputSections.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/Support/Parallel.h"
2323
#include "llvm/Support/Path.h"
2424
#include "llvm/Support/TimeProfiler.h"
25+
#undef in
2526
#if LLVM_ENABLE_ZLIB
2627
// Avoid introducing max as a macro from Windows headers.
2728
#define NOMINMAX
@@ -584,15 +585,15 @@ void OutputSection::writeTo(uint8_t *buf, parallel::TaskGroup &tg) {
584585
static void finalizeShtGroup(OutputSection *os, InputSection *section) {
585586
// sh_link field for SHT_GROUP sections should contain the section index of
586587
// the symbol table.
587-
os->link = in.symTab->getParent()->sectionIndex;
588+
os->link = ctx.sec.symTab->getParent()->sectionIndex;
588589

589590
if (!section)
590591
return;
591592

592593
// sh_info then contain index of an entry in symbol table section which
593594
// provides signature of the section group.
594595
ArrayRef<Symbol *> symbols = section->file->getSymbols();
595-
os->info = in.symTab->getSymbolIndex(*symbols[section->info]);
596+
os->info = ctx.sec.symTab->getSymbolIndex(*symbols[section->info]);
596597

597598
// Some group members may be combined or discarded, so we need to compute the
598599
// new size. The content will be rewritten in InputSection::copyShtGroup.
@@ -610,7 +611,7 @@ encodeOneCrel(raw_svector_ostream &os, Elf_Crel<sizeof(uint) == 8> &out,
610611
uint offset, const Symbol &sym, uint32_t type, uint addend) {
611612
const auto deltaOffset = static_cast<uint64_t>(offset - out.r_offset);
612613
out.r_offset = offset;
613-
int64_t symidx = in.symTab->getSymbolIndex(sym);
614+
int64_t symidx = ctx.sec.symTab->getSymbolIndex(sym);
614615
if (sym.type == STT_SECTION) {
615616
auto *d = dyn_cast<Defined>(&sym);
616617
if (d) {
@@ -731,7 +732,7 @@ void OutputSection::finalize() {
731732
if (!first || isa<SyntheticSection>(first))
732733
return;
733734

734-
link = in.symTab->getParent()->sectionIndex;
735+
link = ctx.sec.symTab->getParent()->sectionIndex;
735736
// sh_info for SHT_REL[A] sections should contain the section header index of
736737
// the section to which the relocation applies.
737738
InputSectionBase *s = first->getRelocatedSection();
@@ -881,8 +882,8 @@ void OutputSection::checkDynRelAddends(const uint8_t *bufStart) {
881882
// Some targets have NOBITS synthetic sections with dynamic relocations
882883
// with non-zero addends. Skip such sections.
883884
if (is_contained({EM_PPC, EM_PPC64}, config->emachine) &&
884-
(rel.inputSec == in.ppc64LongBranchTarget.get() ||
885-
rel.inputSec == in.igotPlt.get()))
885+
(rel.inputSec == ctx.sec.ppc64LongBranchTarget.get() ||
886+
rel.inputSec == ctx.sec.igotPlt.get()))
886887
continue;
887888
const uint8_t *relocTarget =
888889
bufStart + relOsec->offset + rel.inputSec->getOffset(rel.offsetInSec);

lld/ELF/SyntheticSections.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4926,8 +4926,6 @@ template <class ELFT> void elf::createSyntheticSections() {
49264926
add(*in.strTab);
49274927
}
49284928

4929-
InStruct elf::in;
4930-
49314929
template void elf::splitSections<ELF32LE>();
49324930
template void elf::splitSections<ELF32BE>();
49334931
template void elf::splitSections<ELF64LE>();

lld/ELF/SyntheticSections.h

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,43 +1479,7 @@ inline Partition &SectionBase::getPartition() const {
14791479
return ctx.partitions[partition - 1];
14801480
}
14811481

1482-
// Linker generated sections which can be used as inputs and are not specific to
1483-
// a partition.
1484-
struct InStruct {
1485-
std::unique_ptr<InputSection> attributes;
1486-
std::unique_ptr<SyntheticSection> riscvAttributes;
1487-
std::unique_ptr<BssSection> bss;
1488-
std::unique_ptr<BssSection> bssRelRo;
1489-
std::unique_ptr<GotSection> got;
1490-
std::unique_ptr<GotPltSection> gotPlt;
1491-
std::unique_ptr<IgotPltSection> igotPlt;
1492-
std::unique_ptr<RelroPaddingSection> relroPadding;
1493-
std::unique_ptr<SyntheticSection> armCmseSGSection;
1494-
std::unique_ptr<PPC64LongBranchTargetSection> ppc64LongBranchTarget;
1495-
std::unique_ptr<SyntheticSection> mipsAbiFlags;
1496-
std::unique_ptr<MipsGotSection> mipsGot;
1497-
std::unique_ptr<SyntheticSection> mipsOptions;
1498-
std::unique_ptr<SyntheticSection> mipsReginfo;
1499-
std::unique_ptr<MipsRldMapSection> mipsRldMap;
1500-
std::unique_ptr<SyntheticSection> partEnd;
1501-
std::unique_ptr<SyntheticSection> partIndex;
1502-
std::unique_ptr<PltSection> plt;
1503-
std::unique_ptr<IpltSection> iplt;
1504-
std::unique_ptr<PPC32Got2Section> ppc32Got2;
1505-
std::unique_ptr<IBTPltSection> ibtPlt;
1506-
std::unique_ptr<RelocationBaseSection> relaPlt;
1507-
// Non-SHF_ALLOC sections
1508-
std::unique_ptr<SyntheticSection> debugNames;
1509-
std::unique_ptr<GdbIndexSection> gdbIndex;
1510-
std::unique_ptr<StringTableSection> shStrTab;
1511-
std::unique_ptr<StringTableSection> strTab;
1512-
std::unique_ptr<SymbolTableBaseSection> symTab;
1513-
std::unique_ptr<SymtabShndxSection> symTabShndx;
1514-
1515-
void reset();
1516-
};
1517-
1518-
LLVM_LIBRARY_VISIBILITY extern InStruct in;
1482+
#define in ctx.sec
15191483

15201484
} // namespace lld::elf
15211485

0 commit comments

Comments
 (0)