Skip to content

Commit 09dd0fe

Browse files
committed
[ELF] Move Out into Ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such singletons. ctx's hidden visibility optimizes generated instructions. bufferStart and tlsPhdr, which are not OutputSection, can now be moved outside of `Out`.
1 parent 534a873 commit 09dd0fe

File tree

10 files changed

+97
-105
lines changed

10 files changed

+97
-105
lines changed

lld/ELF/Config.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class InputSectionBase;
4444
class EhInputSection;
4545
class Symbol;
4646
class BitcodeCompiler;
47+
class OutputSection;
48+
struct PhdrEntry;
4749

4850
enum ELFKind : uint8_t {
4951
ELFNoneKind,
@@ -478,6 +480,20 @@ struct DuplicateSymbol {
478480

479481
struct Ctx {
480482
LinkerDriver driver;
483+
484+
// These variables are initialized by Writer and should not be used before
485+
// Writer is initialized.
486+
uint8_t *bufferStart;
487+
PhdrEntry *tlsPhdr;
488+
struct OutSections {
489+
OutputSection *elfHeader;
490+
OutputSection *programHeaders;
491+
OutputSection *preinitArray;
492+
OutputSection *initArray;
493+
OutputSection *finiArray;
494+
};
495+
OutSections out;
496+
481497
SmallVector<std::unique_ptr<MemoryBuffer>> memoryBuffers;
482498
SmallVector<ELFFileBase *, 0> objectFiles;
483499
SmallVector<SharedFile *, 0> sharedFiles;

lld/ELF/Driver.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ void elf::errorOrWarn(const Twine &msg) {
9393

9494
void Ctx::reset() {
9595
driver = LinkerDriver();
96+
97+
bufferStart = nullptr;
98+
tlsPhdr = nullptr;
99+
out = OutSections{};
100+
96101
memoryBuffers.clear();
97102
objectFiles.clear();
98103
sharedFiles.clear();
@@ -2934,7 +2939,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
29342939

29352940
// Create elfHeader early. We need a dummy section in
29362941
// addReservedSymbols to mark the created symbols as not absolute.
2937-
Out::elfHeader = make<OutputSection>("", 0, SHF_ALLOC);
2942+
ctx.out.elfHeader = make<OutputSection>("", 0, SHF_ALLOC);
29382943

29392944
// We need to create some reserved symbols such as _end. Create them.
29402945
if (!config->relocatable)

lld/ELF/InputSection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ static int64_t getTlsTpOffset(const Symbol &s) {
679679
// Variant 2. Static TLS blocks, followed by alignment padding are placed
680680
// before TP. The alignment padding is added so that (TP - padding -
681681
// p_memsz) is congruent to p_vaddr modulo p_align.
682-
PhdrEntry *tls = Out::tlsPhdr;
682+
PhdrEntry *tls = ctx.tlsPhdr;
683683
switch (config->emachine) {
684684
// Variant 1.
685685
case EM_ARM:

lld/ELF/LinkerScript.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,17 +1359,17 @@ void LinkerScript::allocateHeaders(SmallVector<PhdrEntry *, 0> &phdrs) {
13591359
if ((paged || hasExplicitHeaders) &&
13601360
headerSize <= min - computeBase(min, hasExplicitHeaders)) {
13611361
min = alignDown(min - headerSize, config->maxPageSize);
1362-
Out::elfHeader->addr = min;
1363-
Out::programHeaders->addr = min + Out::elfHeader->size;
1362+
ctx.out.elfHeader->addr = min;
1363+
ctx.out.programHeaders->addr = min + ctx.out.elfHeader->size;
13641364
return;
13651365
}
13661366

13671367
// Error if we were explicitly asked to allocate headers.
13681368
if (hasExplicitHeaders)
13691369
error("could not allocate headers");
13701370

1371-
Out::elfHeader->ptLoad = nullptr;
1372-
Out::programHeaders->ptLoad = nullptr;
1371+
ctx.out.elfHeader->ptLoad = nullptr;
1372+
ctx.out.programHeaders->ptLoad = nullptr;
13731373
firstPTLoad->firstSec = findFirstSection(firstPTLoad);
13741374

13751375
llvm::erase_if(phdrs,
@@ -1397,8 +1397,8 @@ LinkerScript::assignAddresses() {
13971397
} else {
13981398
// Assign addresses to headers right now.
13991399
dot = target->getImageBase();
1400-
Out::elfHeader->addr = dot;
1401-
Out::programHeaders->addr = dot + Out::elfHeader->size;
1400+
ctx.out.elfHeader->addr = dot;
1401+
ctx.out.programHeaders->addr = dot + ctx.out.elfHeader->size;
14021402
dot += getHeaderSize();
14031403
}
14041404

@@ -1543,9 +1543,9 @@ SmallVector<PhdrEntry *, 0> LinkerScript::createPhdrs() {
15431543
PhdrEntry *phdr = make<PhdrEntry>(cmd.type, cmd.flags.value_or(PF_R));
15441544

15451545
if (cmd.hasFilehdr)
1546-
phdr->add(Out::elfHeader);
1546+
phdr->add(ctx.out.elfHeader);
15471547
if (cmd.hasPhdrs)
1548-
phdr->add(Out::programHeaders);
1548+
phdr->add(ctx.out.programHeaders);
15491549

15501550
if (cmd.lmaExpr) {
15511551
phdr->p_paddr = cmd.lmaExpr().getValue();

lld/ELF/OutputSections.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,6 @@ using namespace llvm::ELF;
3939
using namespace lld;
4040
using namespace lld::elf;
4141

42-
uint8_t *Out::bufferStart;
43-
PhdrEntry *Out::tlsPhdr;
44-
OutputSection *Out::elfHeader;
45-
OutputSection *Out::programHeaders;
46-
OutputSection *Out::preinitArray;
47-
OutputSection *Out::initArray;
48-
OutputSection *Out::finiArray;
49-
5042
SmallVector<OutputSection *, 0> elf::outputSections;
5143

5244
uint32_t OutputSection::getPhdrFlags() const {
@@ -272,7 +264,7 @@ static void sortByOrder(MutableArrayRef<InputSection *> in,
272264
uint64_t elf::getHeaderSize() {
273265
if (config->oFormatBinary)
274266
return 0;
275-
return Out::elfHeader->size + Out::programHeaders->size;
267+
return ctx.out.elfHeader->size + ctx.out.programHeaders->size;
276268
}
277269

278270
void OutputSection::sort(llvm::function_ref<int(InputSectionBase *s)> order) {

lld/ELF/OutputSections.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,19 +150,6 @@ llvm::ArrayRef<InputSection *>
150150
getInputSections(const OutputSection &os,
151151
SmallVector<InputSection *, 0> &storage);
152152

153-
// All output sections that are handled by the linker specially are
154-
// globally accessible. Writer initializes them, so don't use them
155-
// until Writer is initialized.
156-
struct Out {
157-
static uint8_t *bufferStart;
158-
static PhdrEntry *tlsPhdr;
159-
static OutputSection *elfHeader;
160-
static OutputSection *programHeaders;
161-
static OutputSection *preinitArray;
162-
static OutputSection *initArray;
163-
static OutputSection *finiArray;
164-
};
165-
166153
uint64_t getHeaderSize();
167154

168155
LLVM_LIBRARY_VISIBILITY extern llvm::SmallVector<OutputSection *, 0>

lld/ELF/Symbols.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ static uint64_t getSymVA(const Symbol &sym, int64_t addend) {
135135
// after sections are finalized. (e.g. Measuring the size of .rela.dyn
136136
// for Android relocation packing requires knowing TLS symbol addresses
137137
// during section finalization.)
138-
if (!Out::tlsPhdr || !Out::tlsPhdr->firstSec)
138+
if (!ctx.tlsPhdr || !ctx.tlsPhdr->firstSec)
139139
fatal(toString(d.file) +
140140
" has an STT_TLS symbol but doesn't have an SHF_TLS section");
141-
return va - Out::tlsPhdr->firstSec->addr;
141+
return va - ctx.tlsPhdr->firstSec->addr;
142142
}
143143
return va;
144144
}

lld/ELF/SyntheticSections.cpp

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ void EhFrameSection::finalizeContents() {
553553
// to get an FDE from an address to which FDE is applied. This function
554554
// returns a list of such pairs.
555555
SmallVector<EhFrameSection::FdeData, 0> EhFrameSection::getFdeData() const {
556-
uint8_t *buf = Out::bufferStart + getParent()->offset + outSecOff;
556+
uint8_t *buf = ctx.bufferStart + getParent()->offset + outSecOff;
557557
SmallVector<FdeData, 0> ret;
558558

559559
uint64_t va = getPartition().ehFrameHdr->getVA();
@@ -1493,17 +1493,17 @@ DynamicSection<ELFT>::computeContents() {
14931493
addInSec(DT_HASH, *part.hashTab);
14941494

14951495
if (isMain) {
1496-
if (Out::preinitArray) {
1497-
addInt(DT_PREINIT_ARRAY, Out::preinitArray->addr);
1498-
addInt(DT_PREINIT_ARRAYSZ, Out::preinitArray->size);
1496+
if (ctx.out.preinitArray) {
1497+
addInt(DT_PREINIT_ARRAY, ctx.out.preinitArray->addr);
1498+
addInt(DT_PREINIT_ARRAYSZ, ctx.out.preinitArray->size);
14991499
}
1500-
if (Out::initArray) {
1501-
addInt(DT_INIT_ARRAY, Out::initArray->addr);
1502-
addInt(DT_INIT_ARRAYSZ, Out::initArray->size);
1500+
if (ctx.out.initArray) {
1501+
addInt(DT_INIT_ARRAY, ctx.out.initArray->addr);
1502+
addInt(DT_INIT_ARRAYSZ, ctx.out.initArray->size);
15031503
}
1504-
if (Out::finiArray) {
1505-
addInt(DT_FINI_ARRAY, Out::finiArray->addr);
1506-
addInt(DT_FINI_ARRAYSZ, Out::finiArray->size);
1504+
if (ctx.out.finiArray) {
1505+
addInt(DT_FINI_ARRAY, ctx.out.finiArray->addr);
1506+
addInt(DT_FINI_ARRAYSZ, ctx.out.finiArray->size);
15071507
}
15081508

15091509
if (Symbol *b = symtab.find(config->init))
@@ -3631,7 +3631,7 @@ void EhFrameHeader::writeTo(uint8_t *buf) {
36313631
// the starting PC from where FDEs covers, and the FDE's address.
36323632
// It is sorted by PC.
36333633
void EhFrameHeader::write() {
3634-
uint8_t *buf = Out::bufferStart + getParent()->offset + outSecOff;
3634+
uint8_t *buf = ctx.bufferStart + getParent()->offset + outSecOff;
36353635
using FdeData = EhFrameSection::FdeData;
36363636
SmallVector<FdeData, 0> fdes = getPartition().ehFrame->getFdeData();
36373637

@@ -4647,13 +4647,6 @@ static Defined *addOptionalRegular(StringRef name, SectionBase *sec,
46474647
}
46484648

46494649
template <class ELFT> void elf::createSyntheticSections() {
4650-
// Initialize all pointers with NULL. This is needed because
4651-
// you can call lld::elf::main more than once as a library.
4652-
Out::tlsPhdr = nullptr;
4653-
Out::preinitArray = nullptr;
4654-
Out::initArray = nullptr;
4655-
Out::finiArray = nullptr;
4656-
46574650
// Add the .interp section first because it is not a SyntheticSection.
46584651
// The removeUnusedSyntheticSections() function relies on the
46594652
// SyntheticSections coming last.
@@ -4670,8 +4663,8 @@ template <class ELFT> void elf::createSyntheticSections() {
46704663
if (config->zSectionHeader)
46714664
in.shStrTab = std::make_unique<StringTableSection>(".shstrtab", false);
46724665

4673-
Out::programHeaders = make<OutputSection>("", 0, SHF_ALLOC);
4674-
Out::programHeaders->addralign = config->wordsize;
4666+
ctx.out.programHeaders = make<OutputSection>("", 0, SHF_ALLOC);
4667+
ctx.out.programHeaders->addralign = config->wordsize;
46754668

46764669
if (config->strip != StripPolicy::All) {
46774670
in.strTab = std::make_unique<StringTableSection>(".strtab", false);

lld/ELF/Target.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ ErrorPlace elf::getErrorPlace(const uint8_t *loc) {
104104
continue;
105105

106106
const uint8_t *isecLoc =
107-
Out::bufferStart
108-
? (Out::bufferStart + isec->getParent()->offset + isec->outSecOff)
107+
ctx.bufferStart
108+
? (ctx.bufferStart + isec->getParent()->offset + isec->outSecOff)
109109
: isec->contentMaybeDecompress().data();
110110
if (isecLoc == nullptr) {
111111
assert(isa<SyntheticSection>(isec) && "No data but not synthetic?");

0 commit comments

Comments
 (0)