Skip to content

Commit 0206181

Browse files
committed
[ELF] Pass Ctx & to Driver and Writer
1 parent f5eaadc commit 0206181

File tree

3 files changed

+47
-44
lines changed

3 files changed

+47
-44
lines changed

lld/ELF/Driver.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2726,7 +2726,7 @@ static void checkAndReportMissingFeature(StringRef config, uint32_t features,
27262726
// For AArch64 PAuth-enabled object files, the core info of all of them must
27272727
// match. Missing info for some object files with matching info for remaining
27282728
// ones can be allowed (see -z pauth-report).
2729-
static void readSecurityNotes() {
2729+
static void readSecurityNotes(Ctx &ctx) {
27302730
if (ctx.arg.emachine != EM_386 && ctx.arg.emachine != EM_X86_64 &&
27312731
ctx.arg.emachine != EM_AARCH64)
27322732
return;
@@ -2969,7 +2969,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
29692969

29702970
// We need to create some reserved symbols such as _end. Create them.
29712971
if (!ctx.arg.relocatable)
2972-
addReservedSymbols();
2972+
addReservedSymbols(ctx);
29732973

29742974
// Apply version scripts.
29752975
//
@@ -3113,7 +3113,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
31133113

31143114
// Read .note.gnu.property sections from input object files which
31153115
// contain a hint to tweak linker's and loader's behaviors.
3116-
readSecurityNotes();
3116+
readSecurityNotes(ctx);
31173117

31183118
// The Target instance handles target-specific stuff, such as applying
31193119
// relocations or writing a PLT section. It also contains target-dependent
@@ -3147,7 +3147,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
31473147

31483148
// Make copies of any input sections that need to be copied into each
31493149
// partition.
3150-
copySectionsIntoPartitions();
3150+
copySectionsIntoPartitions(ctx);
31513151

31523152
if (canHaveMemtagGlobals()) {
31533153
llvm::TimeTraceScope timeScope("Process memory tagged symbols");

lld/ELF/Writer.cpp

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ template <class ELFT> void elf::writeResult(Ctx &ctx) {
9696
Writer<ELFT>(ctx).run();
9797
}
9898

99-
static void removeEmptyPTLoad(SmallVector<PhdrEntry *, 0> &phdrs) {
99+
static void removeEmptyPTLoad(Ctx &ctx, SmallVector<PhdrEntry *, 0> &phdrs) {
100100
auto it = std::stable_partition(
101101
phdrs.begin(), phdrs.end(), [&](const PhdrEntry *p) {
102102
if (p->p_type != PT_LOAD)
@@ -116,7 +116,7 @@ static void removeEmptyPTLoad(SmallVector<PhdrEntry *, 0> &phdrs) {
116116
phdrs.erase(it, phdrs.end());
117117
}
118118

119-
void elf::copySectionsIntoPartitions() {
119+
void elf::copySectionsIntoPartitions(Ctx &ctx) {
120120
SmallVector<InputSectionBase *, 0> newSections;
121121
const size_t ehSize = ctx.ehInputSections.size();
122122
for (unsigned part = 2; part != ctx.partitions.size() + 1; ++part) {
@@ -139,7 +139,7 @@ void elf::copySectionsIntoPartitions() {
139139
newSections.end());
140140
}
141141

142-
static Defined *addOptionalRegular(StringRef name, SectionBase *sec,
142+
static Defined *addOptionalRegular(Ctx &ctx, StringRef name, SectionBase *sec,
143143
uint64_t val, uint8_t stOther = STV_HIDDEN) {
144144
Symbol *s = ctx.symtab->find(name);
145145
if (!s || s->isDefined() || s->isCommon())
@@ -154,9 +154,9 @@ static Defined *addOptionalRegular(StringRef name, SectionBase *sec,
154154

155155
// The linker is expected to define some symbols depending on
156156
// the linking result. This function defines such symbols.
157-
void elf::addReservedSymbols() {
157+
void elf::addReservedSymbols(Ctx &ctx) {
158158
if (ctx.arg.emachine == EM_MIPS) {
159-
auto addAbsolute = [](StringRef name) {
159+
auto addAbsolute = [&](StringRef name) {
160160
Symbol *sym =
161161
ctx.symtab->addSymbol(Defined{ctx.internalFile, name, STB_GLOBAL,
162162
STV_HIDDEN, STT_NOTYPE, 0, 0, nullptr});
@@ -184,7 +184,7 @@ void elf::addReservedSymbols() {
184184
} else if (ctx.arg.emachine == EM_PPC) {
185185
// glibc *crt1.o has a undefined reference to _SDA_BASE_. Since we don't
186186
// support Small Data Area, define it arbitrarily as 0.
187-
addOptionalRegular("_SDA_BASE_", nullptr, 0, STV_HIDDEN);
187+
addOptionalRegular(ctx, "_SDA_BASE_", nullptr, 0, STV_HIDDEN);
188188
} else if (ctx.arg.emachine == EM_PPC64) {
189189
addPPC64SaveRestore();
190190
}
@@ -220,23 +220,24 @@ void elf::addReservedSymbols() {
220220
// this symbol unconditionally even when using a linker script, which
221221
// differs from the behavior implemented by GNU linker which only define
222222
// this symbol if ELF headers are in the memory mapped segment.
223-
addOptionalRegular("__ehdr_start", ctx.out.elfHeader, 0, STV_HIDDEN);
223+
addOptionalRegular(ctx, "__ehdr_start", ctx.out.elfHeader, 0, STV_HIDDEN);
224224

225225
// __executable_start is not documented, but the expectation of at
226226
// least the Android libc is that it points to the ELF header.
227-
addOptionalRegular("__executable_start", ctx.out.elfHeader, 0, STV_HIDDEN);
227+
addOptionalRegular(ctx, "__executable_start", ctx.out.elfHeader, 0,
228+
STV_HIDDEN);
228229

229230
// __dso_handle symbol is passed to cxa_finalize as a marker to identify
230231
// each DSO. The address of the symbol doesn't matter as long as they are
231232
// different in different DSOs, so we chose the start address of the DSO.
232-
addOptionalRegular("__dso_handle", ctx.out.elfHeader, 0, STV_HIDDEN);
233+
addOptionalRegular(ctx, "__dso_handle", ctx.out.elfHeader, 0, STV_HIDDEN);
233234

234235
// If linker script do layout we do not need to create any standard symbols.
235236
if (ctx.script->hasSectionsCommand)
236237
return;
237238

238-
auto add = [](StringRef s, int64_t pos) {
239-
return addOptionalRegular(s, ctx.out.elfHeader, pos, STV_DEFAULT);
239+
auto add = [&](StringRef s, int64_t pos) {
240+
return addOptionalRegular(ctx, s, ctx.out.elfHeader, pos, STV_DEFAULT);
240241
};
241242

242243
ctx.sym.bss = add("__bss_start", 0);
@@ -270,7 +271,7 @@ static void demoteDefined(Defined &sym, DenseMap<SectionBase *, size_t> &map) {
270271
//
271272
// In addition, demote symbols defined in discarded sections, so that
272273
// references to /DISCARD/ discarded symbols will lead to errors.
273-
static void demoteSymbolsAndComputeIsPreemptible() {
274+
static void demoteSymbolsAndComputeIsPreemptible(Ctx &ctx) {
274275
llvm::TimeTraceScope timeScope("Demote symbols");
275276
DenseMap<InputFile *, DenseMap<SectionBase *, size_t>> sectionIndexMap;
276277
for (Symbol *sym : ctx.symtab->getSymbols()) {
@@ -322,7 +323,7 @@ template <class ELFT> void Writer<ELFT>::run() {
322323
// 0 sized region. This has to be done late since only after assignAddresses
323324
// we know the size of the sections.
324325
for (Partition &part : ctx.partitions)
325-
removeEmptyPTLoad(part.phdrs);
326+
removeEmptyPTLoad(ctx, part.phdrs);
326327

327328
if (!ctx.arg.oFormatBinary)
328329
assignFileOffsets();
@@ -391,7 +392,7 @@ static void markUsedLocalSymbolsImpl(ObjFile<ELFT> *file,
391392

392393
// The function ensures that the "used" field of local symbols reflects the fact
393394
// that the symbol is used in a relocation from a live section.
394-
template <class ELFT> static void markUsedLocalSymbols() {
395+
template <class ELFT> static void markUsedLocalSymbols(Ctx &ctx) {
395396
// With --gc-sections, the field is already filled.
396397
// See MarkLive<ELFT>::resolveReloc().
397398
if (ctx.arg.gcSections)
@@ -419,7 +420,7 @@ template <class ELFT> static void markUsedLocalSymbols() {
419420
}
420421
}
421422

422-
static bool shouldKeepInSymtab(const Defined &sym) {
423+
static bool shouldKeepInSymtab(Ctx &ctx, const Defined &sym) {
423424
if (sym.isSection())
424425
return false;
425426

@@ -474,7 +475,7 @@ bool lld::elf::includeInSymtab(const Symbol &b) {
474475
// - demote symbols defined relative to /DISCARD/ discarded input sections so
475476
// that relocations referencing them will lead to errors.
476477
// - copy eligible symbols to .symTab
477-
static void demoteAndCopyLocalSymbols() {
478+
static void demoteAndCopyLocalSymbols(Ctx &ctx) {
478479
llvm::TimeTraceScope timeScope("Add local symbols");
479480
for (ELFFileBase *file : ctx.objectFiles) {
480481
DenseMap<SectionBase *, size_t> sectionIndexMap;
@@ -486,7 +487,8 @@ static void demoteAndCopyLocalSymbols() {
486487

487488
if (dr->section && !dr->section->isLive())
488489
demoteDefined(*dr, sectionIndexMap);
489-
else if (ctx.in.symTab && includeInSymtab(*b) && shouldKeepInSymtab(*dr))
490+
else if (ctx.in.symTab && includeInSymtab(*b) &&
491+
shouldKeepInSymtab(ctx, *dr))
490492
ctx.in.symTab->addSymbol(b);
491493
}
492494
}
@@ -811,10 +813,10 @@ template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
811813
// .rela.dyn will be present in the output.
812814
std::string name = ctx.arg.isRela ? "__rela_iplt_start" : "__rel_iplt_start";
813815
ctx.sym.relaIpltStart =
814-
addOptionalRegular(name, ctx.out.elfHeader, 0, STV_HIDDEN);
816+
addOptionalRegular(ctx, name, ctx.out.elfHeader, 0, STV_HIDDEN);
815817
name.replace(name.size() - 5, 5, "end");
816818
ctx.sym.relaIpltEnd =
817-
addOptionalRegular(name, ctx.out.elfHeader, 0, STV_HIDDEN);
819+
addOptionalRegular(ctx, name, ctx.out.elfHeader, 0, STV_HIDDEN);
818820
}
819821

820822
// This function generates assignments for predefined symbols (e.g. _end or
@@ -1661,7 +1663,7 @@ template <class ELFT> void Writer<ELFT>::optimizeBasicBlockJumps() {
16611663
// To deal with the above problem, this function is called after
16621664
// scanRelocations is called to remove synthetic sections that turn
16631665
// out to be empty.
1664-
static void removeUnusedSyntheticSections() {
1666+
static void removeUnusedSyntheticSections(Ctx &ctx) {
16651667
// All input synthetic sections that can be empty are placed after
16661668
// all regular ones. Reverse iterate to find the first synthetic section
16671669
// after a non-synthetic one which will be our starting point.
@@ -1740,8 +1742,8 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
17401742
if (ctx.arg.emachine == EM_RISCV) {
17411743
if (!ctx.arg.shared) {
17421744
OutputSection *sec = findSection(".sdata");
1743-
addOptionalRegular("__global_pointer$", sec ? sec : ctx.out.elfHeader,
1744-
0x800, STV_DEFAULT);
1745+
addOptionalRegular(ctx, "__global_pointer$",
1746+
sec ? sec : ctx.out.elfHeader, 0x800, STV_DEFAULT);
17451747
// Set riscvGlobalPointer to be used by the optional global pointer
17461748
// relaxation.
17471749
if (ctx.arg.relaxGP) {
@@ -1783,11 +1785,11 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
17831785
}
17841786
}
17851787

1786-
demoteSymbolsAndComputeIsPreemptible();
1788+
demoteSymbolsAndComputeIsPreemptible(ctx);
17871789

17881790
if (ctx.arg.copyRelocs && ctx.arg.discard != DiscardPolicy::None)
1789-
markUsedLocalSymbols<ELFT>();
1790-
demoteAndCopyLocalSymbols();
1791+
markUsedLocalSymbols<ELFT>(ctx);
1792+
demoteAndCopyLocalSymbols(ctx);
17911793

17921794
if (ctx.arg.copyRelocs)
17931795
addSectionSymbols();
@@ -1890,7 +1892,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
18901892
if (ctx.in.mipsGot)
18911893
ctx.in.mipsGot->build();
18921894

1893-
removeUnusedSyntheticSections();
1895+
removeUnusedSyntheticSections(ctx);
18941896
ctx.script->diagnoseOrphanHandling();
18951897
ctx.script->diagnoseMissingSGSectionAddress();
18961898

@@ -2111,13 +2113,13 @@ template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
21112113
// correct.
21122114
auto define = [=](StringRef start, StringRef end, OutputSection *os) {
21132115
if (os) {
2114-
Defined *startSym = addOptionalRegular(start, os, 0);
2115-
Defined *stopSym = addOptionalRegular(end, os, -1);
2116+
Defined *startSym = addOptionalRegular(ctx, start, os, 0);
2117+
Defined *stopSym = addOptionalRegular(ctx, end, os, -1);
21162118
if (startSym || stopSym)
21172119
os->usedInExpression = true;
21182120
} else {
2119-
addOptionalRegular(start, ctx.out.elfHeader, 0);
2120-
addOptionalRegular(end, ctx.out.elfHeader, 0);
2121+
addOptionalRegular(ctx, start, ctx.out.elfHeader, 0);
2122+
addOptionalRegular(ctx, end, ctx.out.elfHeader, 0);
21212123
}
21222124
};
21232125

@@ -2141,10 +2143,11 @@ void Writer<ELFT>::addStartStopSymbols(OutputSection &osec) {
21412143
StringRef s = osec.name;
21422144
if (!isValidCIdentifier(s))
21432145
return;
2144-
Defined *startSym = addOptionalRegular(saver().save("__start_" + s), &osec, 0,
2145-
ctx.arg.zStartStopVisibility);
2146-
Defined *stopSym = addOptionalRegular(saver().save("__stop_" + s), &osec, -1,
2147-
ctx.arg.zStartStopVisibility);
2146+
Defined *startSym =
2147+
addOptionalRegular(ctx, saver().save("__start_" + s), &osec, 0,
2148+
ctx.arg.zStartStopVisibility);
2149+
Defined *stopSym = addOptionalRegular(ctx, saver().save("__stop_" + s), &osec,
2150+
-1, ctx.arg.zStartStopVisibility);
21482151
if (startSym || stopSym)
21492152
osec.usedInExpression = true;
21502153
}
@@ -2162,7 +2165,7 @@ static bool needsPtLoad(OutputSection *sec) {
21622165
}
21632166

21642167
// Adjust phdr flags according to certain options.
2165-
static uint64_t computeFlags(uint64_t flags) {
2168+
static uint64_t computeFlags(Ctx &ctx, uint64_t flags) {
21662169
if (ctx.arg.omagic)
21672170
return PF_R | PF_W | PF_X;
21682171
if (ctx.arg.executeOnly && (flags & PF_X))
@@ -2184,7 +2187,7 @@ SmallVector<PhdrEntry *, 0> Writer<ELFT>::createPhdrs(Partition &part) {
21842187
bool isMain = partNo == 1;
21852188

21862189
// Add the first PT_LOAD segment for regular output sections.
2187-
uint64_t flags = computeFlags(PF_R);
2190+
uint64_t flags = computeFlags(ctx, PF_R);
21882191
PhdrEntry *load = nullptr;
21892192

21902193
// nmagic or omagic output does not have PT_PHDR, PT_INTERP, or the readonly
@@ -2247,7 +2250,7 @@ SmallVector<PhdrEntry *, 0> Writer<ELFT>::createPhdrs(Partition &part) {
22472250
// partitions.
22482251
if (sec->partition != partNo) {
22492252
if (isMain && sec->partition == 255)
2250-
addHdr(PT_LOAD, computeFlags(sec->getPhdrFlags()))->add(sec);
2253+
addHdr(PT_LOAD, computeFlags(ctx, sec->getPhdrFlags()))->add(sec);
22512254
continue;
22522255
}
22532256

@@ -2267,7 +2270,7 @@ SmallVector<PhdrEntry *, 0> Writer<ELFT>::createPhdrs(Partition &part) {
22672270
// supposed-to-be-NOBITS section to the output file. (However, we cannot do
22682271
// so when hasSectionsCommand, since we cannot introduce the extra alignment
22692272
// needed to create a new LOAD)
2270-
uint64_t newFlags = computeFlags(sec->getPhdrFlags());
2273+
uint64_t newFlags = computeFlags(ctx, sec->getPhdrFlags());
22712274
// When --no-rosegment is specified, RO and RX sections are compatible.
22722275
uint32_t incompatible = flags ^ newFlags;
22732276
if (ctx.arg.singleRoRx && !(newFlags & PF_W))

lld/ELF/Writer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace lld::elf {
1717
class InputFile;
1818
class OutputSection;
19-
void copySectionsIntoPartitions();
19+
void copySectionsIntoPartitions(Ctx &ctx);
2020
template <class ELFT> void writeResult(Ctx &ctx);
2121

2222
// This describes a program header entry.
@@ -44,7 +44,7 @@ struct PhdrEntry {
4444
uint64_t lmaOffset = 0;
4545
};
4646

47-
void addReservedSymbols();
47+
void addReservedSymbols(Ctx &ctx);
4848
bool includeInSymtab(const Symbol &b);
4949
unsigned getSectionRank(OutputSection &osec);
5050

0 commit comments

Comments
 (0)