Skip to content

Commit 079b832

Browse files
committed
[ELF] Pass Ctx & to InputFiles and SyntheticSections
1 parent cc6c059 commit 079b832

10 files changed

+42
-43
lines changed

lld/ELF/Arch/ARM.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,7 @@ static std::string checkCmseSymAttributes(Symbol *acleSeSym, Symbol *sym) {
12611261
// name with __acle_se_.
12621262
// Both these symbols are Thumb function symbols with external linkage.
12631263
// <sym> may be redefined in .gnu.sgstubs.
1264-
void elf::processArmCmseSymbols() {
1264+
void elf::processArmCmseSymbols(Ctx &ctx) {
12651265
if (!ctx.arg.cmseImplib)
12661266
return;
12671267
// Only symbols with external linkage end up in ctx.symtab, so no need to do

lld/ELF/Arch/RISCV.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,7 @@ void RISCVAttributesSection::writeTo(uint8_t *buf) {
13051305
}
13061306
}
13071307

1308-
void elf::mergeRISCVAttributesSections() {
1308+
void elf::mergeRISCVAttributesSections(Ctx &) {
13091309
// Find the first input SHT_RISCV_ATTRIBUTES; return if not found.
13101310
size_t place =
13111311
llvm::find_if(ctx.inputSections,

lld/ELF/Config.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ class LinkerDriver {
173173

174174
std::unique_ptr<BitcodeCompiler> lto;
175175
std::vector<InputFile *> files;
176-
InputFile *armCmseImpLib = nullptr;
177176

178177
public:
178+
InputFile *armCmseImpLib = nullptr;
179179
SmallVector<std::pair<StringRef, unsigned>, 0> archiveFiles;
180180
};
181181

lld/ELF/Driver.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -2875,7 +2875,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
28752875
for (StringRef name : ctx.arg.undefined)
28762876
ctx.symtab->addUnusedUndefined(name)->referenced = true;
28772877

2878-
parseFiles(files, armCmseImpLib);
2878+
parseFiles(ctx, files);
28792879

28802880
// Create dynamic sections for dynamic linking and static PIE.
28812881
ctx.arg.hasDynSymTab = !ctx.sharedFiles.empty() || ctx.arg.isPic;
@@ -3052,7 +3052,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
30523052
excludeLibs(ctx, args);
30533053

30543054
// Record [__acle_se_<sym>, <sym>] pairs for later processing.
3055-
processArmCmseSymbols();
3055+
processArmCmseSymbols(ctx);
30563056

30573057
// Apply symbol renames for --wrap and combine foo@v1 and foo@@v1.
30583058
redirectSymbols(ctx, wrapped);
@@ -3144,7 +3144,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
31443144
ctx.inputSections.push_back(createCommentSection());
31453145

31463146
// Split SHF_MERGE and .eh_frame sections into pieces in preparation for garbage collection.
3147-
splitSections<ELFT>();
3147+
splitSections<ELFT>(ctx);
31483148

31493149
// Garbage collection and removal of shared symbols from unused shared objects.
31503150
markLive<ELFT>(ctx);
@@ -3160,17 +3160,17 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
31603160

31613161
// Create synthesized sections such as .got and .plt. This is called before
31623162
// processSectionCommands() so that they can be placed by SECTIONS commands.
3163-
createSyntheticSections<ELFT>();
3163+
createSyntheticSections<ELFT>(ctx);
31643164

31653165
// Some input sections that are used for exception handling need to be moved
31663166
// into synthetic sections. Do that now so that they aren't assigned to
31673167
// output sections in the usual way.
31683168
if (!ctx.arg.relocatable)
3169-
combineEhSections();
3169+
combineEhSections(ctx);
31703170

31713171
// Merge .riscv.attributes sections.
31723172
if (ctx.arg.emachine == EM_RISCV)
3173-
mergeRISCVAttributesSections();
3173+
mergeRISCVAttributesSections(ctx);
31743174

31753175
{
31763176
llvm::TimeTraceScope timeScope("Assign sections");

lld/ELF/InputFiles.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ static bool isCompatible(InputFile *file) {
296296
return false;
297297
}
298298

299-
template <class ELFT> static void doParseFile(InputFile *file) {
299+
template <class ELFT> static void doParseFile(Ctx &ctx, InputFile *file) {
300300
if (!isCompatible(file))
301301
return;
302302

@@ -329,7 +329,9 @@ template <class ELFT> static void doParseFile(InputFile *file) {
329329
}
330330

331331
// Add symbols in File to the symbol table.
332-
void elf::parseFile(InputFile *file) { invokeELFT(doParseFile, file); }
332+
void elf::parseFile(Ctx &ctx, InputFile *file) {
333+
invokeELFT(doParseFile, ctx, file);
334+
}
333335

334336
// This function is explicitly instantiated in ARM.cpp. Mark it extern here,
335337
// to avoid warnings when building with MSVC.
@@ -339,23 +341,21 @@ extern template void ObjFile<ELF64LE>::importCmseSymbols();
339341
extern template void ObjFile<ELF64BE>::importCmseSymbols();
340342

341343
template <class ELFT>
342-
static void doParseFiles(const std::vector<InputFile *> &files,
343-
InputFile *armCmseImpLib) {
344+
static void doParseFiles(Ctx &ctx, const std::vector<InputFile *> &files) {
344345
// Add all files to the symbol table. This will add almost all symbols that we
345346
// need to the symbol table. This process might add files to the link due to
346347
// addDependentLibrary.
347348
for (size_t i = 0; i < files.size(); ++i) {
348349
llvm::TimeTraceScope timeScope("Parse input files", files[i]->getName());
349-
doParseFile<ELFT>(files[i]);
350+
doParseFile<ELFT>(ctx, files[i]);
350351
}
351-
if (armCmseImpLib)
352-
cast<ObjFile<ELFT>>(*armCmseImpLib).importCmseSymbols();
352+
if (ctx.driver.armCmseImpLib)
353+
cast<ObjFile<ELFT>>(*ctx.driver.armCmseImpLib).importCmseSymbols();
353354
}
354355

355-
void elf::parseFiles(const std::vector<InputFile *> &files,
356-
InputFile *armCmseImpLib) {
356+
void elf::parseFiles(Ctx &ctx, const std::vector<InputFile *> &files) {
357357
llvm::TimeTraceScope timeScope("Parse input files");
358-
invokeELFT(doParseFiles, files, armCmseImpLib);
358+
invokeELFT(doParseFiles, ctx, files);
359359
}
360360

361361
// Concatenates arguments to construct a string representing an error location.

lld/ELF/InputFiles.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ class Symbol;
4343
std::optional<MemoryBufferRef> readFile(StringRef path);
4444

4545
// Add symbols in File to the symbol table.
46-
void parseFile(InputFile *file);
47-
void parseFiles(const std::vector<InputFile *> &files,
48-
InputFile *armCmseImpLib);
46+
void parseFile(Ctx &, InputFile *file);
47+
void parseFiles(Ctx &, const std::vector<InputFile *> &files);
4948

5049
// The root class of input files.
5150
class InputFile {

lld/ELF/Symbols.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ void Symbol::parseSymbolVersion() {
250250
void Symbol::extract() const {
251251
if (file->lazy) {
252252
file->lazy = false;
253-
parseFile(file);
253+
parseFile(ctx, file);
254254
}
255255
}
256256

lld/ELF/SyntheticSections.cpp

+15-15
Original file line numberDiff line numberDiff line change
@@ -3946,7 +3946,7 @@ void MergeNoTailSection::finalizeContents() {
39463946
});
39473947
}
39483948

3949-
template <class ELFT> void elf::splitSections() {
3949+
template <class ELFT> void elf::splitSections(Ctx &ctx) {
39503950
llvm::TimeTraceScope timeScope("Split sections");
39513951
// splitIntoPieces needs to be called on each MergeInputSection
39523952
// before calling finalizeContents().
@@ -3962,7 +3962,7 @@ template <class ELFT> void elf::splitSections() {
39623962
});
39633963
}
39643964

3965-
void elf::combineEhSections() {
3965+
void elf::combineEhSections(Ctx &ctx) {
39663966
llvm::TimeTraceScope timeScope("Combine EH sections");
39673967
for (EhInputSection *sec : ctx.ehInputSections) {
39683968
EhFrameSection &eh = *sec->getPartition().ehFrame;
@@ -4495,7 +4495,7 @@ void InStruct::reset() {
44954495
symTabShndx.reset();
44964496
}
44974497

4498-
static bool needsInterpSection() {
4498+
static bool needsInterpSection(Ctx &ctx) {
44994499
return !ctx.arg.relocatable && !ctx.arg.shared &&
45004500
!ctx.arg.dynamicLinker.empty() && ctx.script->needsInterpSection();
45014501
}
@@ -4513,7 +4513,7 @@ bool elf::hasMemtag() {
45134513
// that ifuncs use in fully static executables.
45144514
bool elf::canHaveMemtagGlobals() {
45154515
return hasMemtag() &&
4516-
(ctx.arg.relocatable || ctx.arg.shared || needsInterpSection());
4516+
(ctx.arg.relocatable || ctx.arg.shared || needsInterpSection(ctx));
45174517
}
45184518

45194519
constexpr char kMemtagAndroidNoteName[] = "Android";
@@ -4652,19 +4652,19 @@ static Defined *addOptionalRegular(StringRef name, SectionBase *sec,
46524652
return cast<Defined>(s);
46534653
}
46544654

4655-
template <class ELFT> void elf::createSyntheticSections() {
4655+
template <class ELFT> void elf::createSyntheticSections(Ctx &ctx) {
46564656
// Add the .interp section first because it is not a SyntheticSection.
46574657
// The removeUnusedSyntheticSections() function relies on the
46584658
// SyntheticSections coming last.
4659-
if (needsInterpSection()) {
4659+
if (needsInterpSection(ctx)) {
46604660
for (size_t i = 1; i <= ctx.partitions.size(); ++i) {
46614661
InputSection *sec = createInterpSection();
46624662
sec->partition = i;
46634663
ctx.inputSections.push_back(sec);
46644664
}
46654665
}
46664666

4667-
auto add = [](SyntheticSection &sec) { ctx.inputSections.push_back(&sec); };
4667+
auto add = [&](SyntheticSection &sec) { ctx.inputSections.push_back(&sec); };
46684668

46694669
if (ctx.arg.zSectionHeader)
46704670
ctx.in.shStrTab = std::make_unique<StringTableSection>(".shstrtab", false);
@@ -4927,10 +4927,10 @@ template <class ELFT> void elf::createSyntheticSections() {
49274927
add(*ctx.in.strTab);
49284928
}
49294929

4930-
template void elf::splitSections<ELF32LE>();
4931-
template void elf::splitSections<ELF32BE>();
4932-
template void elf::splitSections<ELF64LE>();
4933-
template void elf::splitSections<ELF64BE>();
4930+
template void elf::splitSections<ELF32LE>(Ctx &);
4931+
template void elf::splitSections<ELF32BE>(Ctx &);
4932+
template void elf::splitSections<ELF64LE>(Ctx &);
4933+
template void elf::splitSections<ELF64BE>(Ctx &);
49344934

49354935
template void EhFrameSection::iterateFDEWithLSDA<ELF32LE>(
49364936
function_ref<void(InputSection &)>);
@@ -4956,7 +4956,7 @@ template void elf::writePhdrs<ELF32BE>(uint8_t *Buf, Partition &Part);
49564956
template void elf::writePhdrs<ELF64LE>(uint8_t *Buf, Partition &Part);
49574957
template void elf::writePhdrs<ELF64BE>(uint8_t *Buf, Partition &Part);
49584958

4959-
template void elf::createSyntheticSections<ELF32LE>();
4960-
template void elf::createSyntheticSections<ELF32BE>();
4961-
template void elf::createSyntheticSections<ELF64LE>();
4962-
template void elf::createSyntheticSections<ELF64BE>();
4959+
template void elf::createSyntheticSections<ELF32LE>(Ctx &);
4960+
template void elf::createSyntheticSections<ELF32BE>(Ctx &);
4961+
template void elf::createSyntheticSections<ELF64LE>(Ctx &);
4962+
template void elf::createSyntheticSections<ELF64BE>(Ctx &);

lld/ELF/SyntheticSections.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1426,11 +1426,11 @@ class MemtagGlobalDescriptors final : public SyntheticSection {
14261426
SmallVector<const Symbol *, 0> symbols;
14271427
};
14281428

1429-
template <class ELFT> void createSyntheticSections();
1429+
template <class ELFT> void createSyntheticSections(Ctx &);
14301430
InputSection *createInterpSection();
14311431
MergeInputSection *createCommentSection();
1432-
template <class ELFT> void splitSections();
1433-
void combineEhSections();
1432+
template <class ELFT> void splitSections(Ctx &);
1433+
void combineEhSections(Ctx &);
14341434

14351435
bool hasMemtag();
14361436
bool canHaveMemtagGlobals();

lld/ELF/Target.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ static inline std::string getErrorLocation(const uint8_t *loc) {
208208
return getErrorPlace(ctx, loc).loc;
209209
}
210210

211-
void processArmCmseSymbols();
211+
void processArmCmseSymbols(Ctx &);
212212

213213
void writePPC32GlinkSection(uint8_t *buf, size_t numEntries);
214214

@@ -235,7 +235,7 @@ uint64_t getAArch64Page(uint64_t expr);
235235
template <typename ELFT> void writeARMCmseImportLib();
236236
uint64_t getLoongArchPageDelta(uint64_t dest, uint64_t pc, RelType type);
237237
void riscvFinalizeRelax(int passes);
238-
void mergeRISCVAttributesSections();
238+
void mergeRISCVAttributesSections(Ctx &);
239239
void addArmInputSectionMappingSymbols();
240240
void addArmSyntheticSectionMappingSymbol(Defined *);
241241
void sortArmMappingSymbols();

0 commit comments

Comments
 (0)