Skip to content

Commit 1c28f31

Browse files
committed
[ELF] Pass Ctx &
1 parent 24ac6cf commit 1c28f31

File tree

10 files changed

+44
-33
lines changed

10 files changed

+44
-33
lines changed

lld/ELF/Arch/AArch64.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ addTaggedSymbolReferences(InputSectionBase &sec,
11501150
// symbols should also be built with tagging. But, to handle these cases, we
11511151
// demote the symbol to be untagged.
11521152
void elf::createTaggedSymbols(Ctx &ctx) {
1153-
assert(hasMemtag());
1153+
assert(hasMemtag(ctx));
11541154

11551155
// First, collect all symbols that are marked as tagged, and count how many
11561156
// times they're marked as tagged.

lld/ELF/CallGraphSort.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,12 @@ struct Cluster {
8888
/// * Sort non-empty clusters by density
8989
class CallGraphSort {
9090
public:
91-
CallGraphSort();
91+
CallGraphSort(Ctx &);
9292

9393
DenseMap<const InputSectionBase *, int> run();
9494

9595
private:
96+
Ctx &ctx;
9697
std::vector<Cluster> clusters;
9798
std::vector<const InputSectionBase *> sections;
9899
};
@@ -111,7 +112,7 @@ using SectionPair =
111112
// Take the edge list in ctx.arg.callGraphProfile, resolve symbol names to
112113
// Symbols, and generate a graph between InputSections with the provided
113114
// weights.
114-
CallGraphSort::CallGraphSort() {
115+
CallGraphSort::CallGraphSort(Ctx &ctx) : ctx(ctx) {
115116
MapVector<SectionPair, uint64_t> &profile = ctx.arg.callGraphProfile;
116117
DenseMap<const InputSectionBase *, int> secToCluster;
117118

@@ -274,7 +275,8 @@ DenseMap<const InputSectionBase *, int> CallGraphSort::run() {
274275
// Sort sections by the profile data using the Cache-Directed Sort algorithm.
275276
// The placement is done by optimizing the locality by co-locating frequently
276277
// executed code sections together.
277-
DenseMap<const InputSectionBase *, int> elf::computeCacheDirectedSortOrder() {
278+
DenseMap<const InputSectionBase *, int>
279+
elf::computeCacheDirectedSortOrder(Ctx &ctx) {
278280
SmallVector<uint64_t, 0> funcSizes;
279281
SmallVector<uint64_t, 0> funcCounts;
280282
SmallVector<codelayout::EdgeCount, 0> callCounts;
@@ -336,8 +338,9 @@ DenseMap<const InputSectionBase *, int> elf::computeCacheDirectedSortOrder() {
336338
//
337339
// This first builds a call graph based on the profile data then merges sections
338340
// according either to the C³ or Cache-Directed-Sort ordering algorithm.
339-
DenseMap<const InputSectionBase *, int> elf::computeCallGraphProfileOrder() {
341+
DenseMap<const InputSectionBase *, int>
342+
elf::computeCallGraphProfileOrder(Ctx &ctx) {
340343
if (ctx.arg.callGraphProfileSort == CGProfileSortKind::Cdsort)
341-
return computeCacheDirectedSortOrder();
342-
return CallGraphSort().run();
344+
return computeCacheDirectedSortOrder(ctx);
345+
return CallGraphSort(ctx).run();
343346
}

lld/ELF/CallGraphSort.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
#include "llvm/ADT/DenseMap.h"
1313

1414
namespace lld::elf {
15+
struct Ctx;
1516
class InputSectionBase;
1617

17-
llvm::DenseMap<const InputSectionBase *, int> computeCacheDirectedSortOrder();
18+
llvm::DenseMap<const InputSectionBase *, int>
19+
computeCacheDirectedSortOrder(Ctx &);
1820

19-
llvm::DenseMap<const InputSectionBase *, int> computeCallGraphProfileOrder();
21+
llvm::DenseMap<const InputSectionBase *, int>
22+
computeCallGraphProfileOrder(Ctx &);
2023
} // namespace lld::elf
2124

2225
#endif

lld/ELF/Driver.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,9 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
155155

156156
context->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
157157
context->e.cleanupCallback = []() {
158-
elf::ctx.reset();
159-
elf::ctx.partitions.emplace_back();
158+
Ctx &ctx = elf::ctx;
159+
ctx.reset();
160+
ctx.partitions.emplace_back(ctx);
160161

161162
SharedFile::vernauxNum = 0;
162163
};
@@ -165,13 +166,14 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
165166
"too many errors emitted, stopping now (use "
166167
"--error-limit=0 to see all errors)";
167168

169+
Ctx &ctx = elf::ctx;
168170
LinkerScript script(ctx);
169171
ctx.script = &script;
170172
ctx.symAux.emplace_back();
171173
ctx.symtab = std::make_unique<SymbolTable>(ctx);
172174

173175
ctx.partitions.clear();
174-
ctx.partitions.emplace_back();
176+
ctx.partitions.emplace_back(ctx);
175177

176178
ctx.arg.progName = args[0];
177179

@@ -2495,7 +2497,7 @@ static void readSymbolPartitionSection(Ctx &ctx, InputSectionBase *s) {
24952497
if (ctx.partitions.size() == 254)
24962498
fatal("may not have more than 254 partitions");
24972499

2498-
ctx.partitions.emplace_back();
2500+
ctx.partitions.emplace_back(ctx);
24992501
Partition &newPart = ctx.partitions.back();
25002502
newPart.name = partName;
25012503
sym->partition = newPart.getNumber();
@@ -3157,7 +3159,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
31573159
// partition.
31583160
copySectionsIntoPartitions(ctx);
31593161

3160-
if (canHaveMemtagGlobals()) {
3162+
if (canHaveMemtagGlobals(ctx)) {
31613163
llvm::TimeTraceScope timeScope("Process memory tagged symbols");
31623164
createTaggedSymbols(ctx);
31633165
}

lld/ELF/InputFiles.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ template <class ELFT> void ObjFile<ELFT>::parse(bool ignoreComdats) {
649649
// medatada, and we don't want them to end up in the output file for static
650650
// executables.
651651
if (sec.sh_type == SHT_AARCH64_MEMTAG_GLOBALS_STATIC &&
652-
!canHaveMemtagGlobals()) {
652+
!canHaveMemtagGlobals(ctx)) {
653653
this->sections[i] = &InputSection::discarded;
654654
continue;
655655
}

lld/ELF/InputSection.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ static void decompressAux(const InputSectionBase &sec, uint8_t *out,
119119
}
120120

121121
void InputSectionBase::decompress() const {
122+
Ctx &ctx = getCtx();
122123
uint8_t *uncompressedBuf;
123124
{
124125
static std::mutex mu;

lld/ELF/MapFile.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static void writeHeader(Ctx &ctx, raw_ostream &os, uint64_t vma, uint64_t lma,
5353
}
5454

5555
// Returns a list of all symbols that we want to print out.
56-
static std::vector<Defined *> getSymbols() {
56+
static std::vector<Defined *> getSymbols(Ctx &ctx) {
5757
std::vector<Defined *> v;
5858
for (ELFFileBase *file : ctx.objectFiles)
5959
for (Symbol *b : file->getSymbols())
@@ -148,7 +148,7 @@ static void printEhFrame(Ctx &ctx, raw_ostream &os, const EhFrameSection *sec) {
148148

149149
static void writeMapFile(Ctx &ctx, raw_fd_ostream &os) {
150150
// Collect symbol info that we want to print out.
151-
std::vector<Defined *> syms = getSymbols();
151+
std::vector<Defined *> syms = getSymbols(ctx);
152152
SymbolMapTy sectionSyms = getSectionSyms(syms);
153153
DenseMap<Symbol *, std::string> symStr = getSymbolStrings(ctx, syms);
154154

lld/ELF/SyntheticSections.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ SmallVector<EhFrameSection::FdeData, 0> EhFrameSection::getFdeData() const {
592592
return ret;
593593
}
594594

595-
static uint64_t readFdeAddr(uint8_t *buf, int size) {
595+
static uint64_t readFdeAddr(Ctx &ctx, uint8_t *buf, int size) {
596596
switch (size) {
597597
case DW_EH_PE_udata2:
598598
return read16(buf);
@@ -619,7 +619,7 @@ uint64_t EhFrameSection::getFdePc(uint8_t *buf, size_t fdeOff,
619619
// stored at FDE + 8 byte. And this offset is within
620620
// the .eh_frame section.
621621
size_t off = fdeOff + 8;
622-
uint64_t addr = readFdeAddr(buf + off, enc & 0xf);
622+
uint64_t addr = readFdeAddr(ctx, buf + off, enc & 0xf);
623623
if ((enc & 0x70) == DW_EH_PE_absptr)
624624
return ctx.arg.is64 ? addr : uint32_t(addr);
625625
if ((enc & 0x70) == DW_EH_PE_pcrel)
@@ -1478,7 +1478,7 @@ DynamicSection<ELFT>::computeContents() {
14781478
if (ctx.arg.zPacPlt)
14791479
addInt(DT_AARCH64_PAC_PLT, 0);
14801480

1481-
if (hasMemtag()) {
1481+
if (hasMemtag(ctx)) {
14821482
addInt(DT_AARCH64_MEMTAG_MODE, ctx.arg.androidMemtagMode == NT_MEMTAG_LEVEL_ASYNC);
14831483
addInt(DT_AARCH64_MEMTAG_HEAP, ctx.arg.androidMemtagHeap);
14841484
addInt(DT_AARCH64_MEMTAG_STACK, ctx.arg.androidMemtagStack);
@@ -4359,7 +4359,7 @@ bool PPC64LongBranchTargetSection::isNeeded() const {
43594359
return !finalized || !entries.empty();
43604360
}
43614361

4362-
static uint8_t getAbiVersion() {
4362+
static uint8_t getAbiVersion(Ctx &ctx) {
43634363
// MIPS non-PIC executable gets ABI version 1.
43644364
if (ctx.arg.emachine == EM_MIPS) {
43654365
if (!ctx.arg.isPic && !ctx.arg.relocatable &&
@@ -4388,7 +4388,7 @@ template <typename ELFT> void elf::writeEhdr(uint8_t *buf, Partition &part) {
43884388
ELFT::Endianness == endianness::little ? ELFDATA2LSB : ELFDATA2MSB;
43894389
eHdr->e_ident[EI_VERSION] = EV_CURRENT;
43904390
eHdr->e_ident[EI_OSABI] = ctx.arg.osabi;
4391-
eHdr->e_ident[EI_ABIVERSION] = getAbiVersion();
4391+
eHdr->e_ident[EI_ABIVERSION] = getAbiVersion(ctx);
43924392
eHdr->e_machine = ctx.arg.emachine;
43934393
eHdr->e_version = EV_CURRENT;
43944394
eHdr->e_flags = ctx.arg.eflags;
@@ -4516,7 +4516,7 @@ static bool needsInterpSection(Ctx &ctx) {
45164516
!ctx.arg.dynamicLinker.empty() && ctx.script->needsInterpSection();
45174517
}
45184518

4519-
bool elf::hasMemtag() {
4519+
bool elf::hasMemtag(Ctx &ctx) {
45204520
return ctx.arg.emachine == EM_AARCH64 &&
45214521
ctx.arg.androidMemtagMode != ELF::NT_MEMTAG_LEVEL_NONE;
45224522
}
@@ -4527,8 +4527,8 @@ bool elf::hasMemtag() {
45274527
// - Dynamic entries.
45284528
// This restriction could be removed in future by re-using some of the ideas
45294529
// that ifuncs use in fully static executables.
4530-
bool elf::canHaveMemtagGlobals() {
4531-
return hasMemtag() &&
4530+
bool elf::canHaveMemtagGlobals(Ctx &ctx) {
4531+
return hasMemtag(ctx) &&
45324532
(ctx.arg.relocatable || ctx.arg.shared || needsInterpSection(ctx));
45334533
}
45344534

@@ -4656,7 +4656,7 @@ static OutputSection *findSection(StringRef name) {
46564656
return nullptr;
46574657
}
46584658

4659-
static Defined *addOptionalRegular(StringRef name, SectionBase *sec,
4659+
static Defined *addOptionalRegular(Ctx &ctx, StringRef name, SectionBase *sec,
46604660
uint64_t val, uint8_t stOther = STV_HIDDEN) {
46614661
Symbol *s = ctx.symtab->find(name);
46624662
if (!s || s->isDefined() || s->isCommon())
@@ -4757,10 +4757,10 @@ template <class ELFT> void elf::createSyntheticSections(Ctx &ctx) {
47574757
continue;
47584758
part.dynamic = std::make_unique<DynamicSection<ELFT>>(ctx);
47594759

4760-
if (hasMemtag()) {
4760+
if (hasMemtag(ctx)) {
47614761
part.memtagAndroidNote = std::make_unique<MemtagAndroidNote>(ctx);
47624762
add(*part.memtagAndroidNote);
4763-
if (canHaveMemtagGlobals()) {
4763+
if (canHaveMemtagGlobals(ctx)) {
47644764
part.memtagGlobalDescriptors =
47654765
std::make_unique<MemtagGlobalDescriptors>(ctx);
47664766
add(*part.memtagGlobalDescriptors);
@@ -4840,8 +4840,8 @@ template <class ELFT> void elf::createSyntheticSections(Ctx &ctx) {
48404840
add(*ctx.in.partEnd);
48414841

48424842
ctx.in.partIndex = std::make_unique<PartitionIndexSection>(ctx);
4843-
addOptionalRegular("__part_index_begin", ctx.in.partIndex.get(), 0);
4844-
addOptionalRegular("__part_index_end", ctx.in.partIndex.get(),
4843+
addOptionalRegular(ctx, "__part_index_begin", ctx.in.partIndex.get(), 0);
4844+
addOptionalRegular(ctx, "__part_index_end", ctx.in.partIndex.get(),
48454845
ctx.in.partIndex->getSize());
48464846
add(*ctx.in.partIndex);
48474847
}

lld/ELF/SyntheticSections.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,8 +1433,8 @@ MergeInputSection *createCommentSection();
14331433
template <class ELFT> void splitSections(Ctx &);
14341434
void combineEhSections(Ctx &);
14351435

1436-
bool hasMemtag();
1437-
bool canHaveMemtagGlobals();
1436+
bool hasMemtag(Ctx &);
1437+
bool canHaveMemtagGlobals(Ctx &);
14381438

14391439
template <typename ELFT> void writeEhdr(uint8_t *buf, Partition &part);
14401440
template <typename ELFT> void writePhdrs(uint8_t *buf, Partition &part);
@@ -1446,6 +1446,7 @@ void addVerneed(Symbol *ss);
14461446

14471447
// Linker generated per-partition sections.
14481448
struct Partition {
1449+
Ctx &ctx;
14491450
StringRef name;
14501451
uint64_t nameStrTab;
14511452

@@ -1472,6 +1473,7 @@ struct Partition {
14721473
std::unique_ptr<SyntheticSection> verNeed;
14731474
std::unique_ptr<VersionTableSection> verSym;
14741475

1476+
Partition(Ctx &ctx) : ctx(ctx) {}
14751477
unsigned getNumber() const { return this - &ctx.partitions[0] + 1; }
14761478
};
14771479

lld/ELF/Writer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ static DenseMap<const InputSectionBase *, int> buildSectionOrder(Ctx &ctx) {
10721072
DenseMap<const InputSectionBase *, int> sectionOrder;
10731073
// Use the rarely used option --call-graph-ordering-file to sort sections.
10741074
if (!ctx.arg.callGraphProfile.empty())
1075-
return computeCallGraphProfileOrder();
1075+
return computeCallGraphProfileOrder(ctx);
10761076

10771077
if (ctx.arg.symbolOrderingFile.empty())
10781078
return sectionOrder;

0 commit comments

Comments
 (0)