Skip to content

Commit 9a57216

Browse files
committed
[ELF] Move InputFiles global variables (memoryBuffers, objectFiles, etc) into Ctx. NFC
1 parent 404479b commit 9a57216

19 files changed

+94
-100
lines changed

lld/ELF/AArch64ErrataFix.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ void AArch64Err843419Patcher::init() {
439439
};
440440

441441
// Collect mapping symbols for every executable InputSection.
442-
for (ELFFileBase *file : objectFiles) {
442+
for (ELFFileBase *file : ctx->objectFiles) {
443443
for (Symbol *b : file->getLocalSymbols()) {
444444
auto *def = dyn_cast<Defined>(b);
445445
if (!def)

lld/ELF/ARMErrataFix.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ void ARMErr657417Patcher::init() {
327327
};
328328

329329
// Collect mapping symbols for every executable InputSection.
330-
for (ELFFileBase *file : objectFiles) {
330+
for (ELFFileBase *file : ctx->objectFiles) {
331331
for (Symbol *s : file->getLocalSymbols()) {
332332
auto *def = dyn_cast<Defined>(s);
333333
if (!def)

lld/ELF/Arch/AMDGPU.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ static uint32_t getEFlags(InputFile *file) {
4848
}
4949

5050
uint32_t AMDGPU::calcEFlagsV3() const {
51-
uint32_t ret = getEFlags(objectFiles[0]);
51+
uint32_t ret = getEFlags(ctx->objectFiles[0]);
5252

5353
// Verify that all input files have the same e_flags.
54-
for (InputFile *f : makeArrayRef(objectFiles).slice(1)) {
54+
for (InputFile *f : makeArrayRef(ctx->objectFiles).slice(1)) {
5555
if (ret == getEFlags(f))
5656
continue;
5757
error("incompatible e_flags: " + toString(f));
@@ -61,14 +61,15 @@ uint32_t AMDGPU::calcEFlagsV3() const {
6161
}
6262

6363
uint32_t AMDGPU::calcEFlagsV4() const {
64-
uint32_t retMach = getEFlags(objectFiles[0]) & EF_AMDGPU_MACH;
65-
uint32_t retXnack = getEFlags(objectFiles[0]) & EF_AMDGPU_FEATURE_XNACK_V4;
64+
uint32_t retMach = getEFlags(ctx->objectFiles[0]) & EF_AMDGPU_MACH;
65+
uint32_t retXnack =
66+
getEFlags(ctx->objectFiles[0]) & EF_AMDGPU_FEATURE_XNACK_V4;
6667
uint32_t retSramEcc =
67-
getEFlags(objectFiles[0]) & EF_AMDGPU_FEATURE_SRAMECC_V4;
68+
getEFlags(ctx->objectFiles[0]) & EF_AMDGPU_FEATURE_SRAMECC_V4;
6869

6970
// Verify that all input files have compatible e_flags (same mach, all
7071
// features in the same category are either ANY, ANY and ON, or ANY and OFF).
71-
for (InputFile *f : makeArrayRef(objectFiles).slice(1)) {
72+
for (InputFile *f : makeArrayRef(ctx->objectFiles).slice(1)) {
7273
if (retMach != (getEFlags(f) & EF_AMDGPU_MACH)) {
7374
error("incompatible mach: " + toString(f));
7475
return 0;
@@ -105,11 +106,13 @@ uint32_t AMDGPU::calcEFlagsV4() const {
105106
}
106107

107108
uint32_t AMDGPU::calcEFlags() const {
108-
if (objectFiles.empty())
109+
if (ctx->objectFiles.empty())
109110
return 0;
110111

111-
uint8_t abiVersion = cast<ObjFile<ELF64LE>>(objectFiles[0])->getObj()
112-
.getHeader().e_ident[EI_ABIVERSION];
112+
uint8_t abiVersion = cast<ObjFile<ELF64LE>>(ctx->objectFiles[0])
113+
->getObj()
114+
.getHeader()
115+
.e_ident[EI_ABIVERSION];
113116
switch (abiVersion) {
114117
case ELFABIVERSION_AMDGPU_HSA_V2:
115118
case ELFABIVERSION_AMDGPU_HSA_V3:

lld/ELF/Arch/AVR.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,12 @@ static uint32_t getEFlags(InputFile *file) {
226226
}
227227

228228
uint32_t AVR::calcEFlags() const {
229-
assert(!objectFiles.empty());
229+
assert(!ctx->objectFiles.empty());
230230

231-
uint32_t flags = getEFlags(objectFiles[0]);
231+
uint32_t flags = getEFlags(ctx->objectFiles[0]);
232232
bool hasLinkRelaxFlag = flags & EF_AVR_LINKRELAX_PREPARED;
233233

234-
for (InputFile *f : makeArrayRef(objectFiles).slice(1)) {
234+
for (InputFile *f : makeArrayRef(ctx->objectFiles).slice(1)) {
235235
uint32_t objFlags = getEFlags(f);
236236
if ((objFlags & EF_AVR_ARCH_MASK) != (flags & EF_AVR_ARCH_MASK))
237237
error(toString(f) +

lld/ELF/Arch/Hexagon.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ Hexagon::Hexagon() {
5959
}
6060

6161
uint32_t Hexagon::calcEFlags() const {
62-
assert(!objectFiles.empty());
62+
assert(!ctx->objectFiles.empty());
6363

6464
// The architecture revision must always be equal to or greater than
6565
// greatest revision in the list of inputs.
6666
uint32_t ret = 0;
67-
for (InputFile *f : objectFiles) {
67+
for (InputFile *f : ctx->objectFiles) {
6868
uint32_t eflags = cast<ObjFile<ELF32LE>>(f)->getObj().getHeader().e_flags;
6969
if (eflags > ret)
7070
ret = eflags;

lld/ELF/Arch/MipsArchTree.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ static uint32_t getArchFlags(ArrayRef<FileFlags> files) {
295295

296296
template <class ELFT> uint32_t elf::calcMipsEFlags() {
297297
std::vector<FileFlags> v;
298-
for (InputFile *f : objectFiles)
298+
for (InputFile *f : ctx->objectFiles)
299299
v.push_back({f, cast<ObjFile<ELFT>>(f)->getObj().getHeader().e_flags});
300300
if (v.empty()) {
301301
// If we don't have any input files, we'll have to rely on the information

lld/ELF/Arch/PPC64.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ static uint32_t getEFlags(InputFile *file) {
622622
// This file implements v2 ABI. This function makes sure that all
623623
// object files have v2 or an unspecified version as an ABI version.
624624
uint32_t PPC64::calcEFlags() const {
625-
for (InputFile *f : objectFiles) {
625+
for (InputFile *f : ctx->objectFiles) {
626626
uint32_t flag = getEFlags(f);
627627
if (flag == 1)
628628
error(toString(f) + ": ABI version 1 is not supported");

lld/ELF/Arch/RISCV.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ static uint32_t getEFlags(InputFile *f) {
111111
uint32_t RISCV::calcEFlags() const {
112112
// If there are only binary input files (from -b binary), use a
113113
// value of 0 for the ELF header flags.
114-
if (objectFiles.empty())
114+
if (ctx->objectFiles.empty())
115115
return 0;
116116

117-
uint32_t target = getEFlags(objectFiles.front());
117+
uint32_t target = getEFlags(ctx->objectFiles.front());
118118

119-
for (InputFile *f : objectFiles) {
119+
for (InputFile *f : ctx->objectFiles) {
120120
uint32_t eflags = getEFlags(f);
121121
if (eflags & EF_RISCV_RVC)
122122
target |= EF_RISCV_RVC;

lld/ELF/Config.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ namespace lld {
2929
namespace elf {
3030

3131
class InputFile;
32+
class BinaryFile;
33+
class BitcodeFile;
34+
class ELFFileBase;
35+
class SharedFile;
3236
class InputSectionBase;
3337
class Symbol;
3438

@@ -373,6 +377,12 @@ struct DuplicateSymbol {
373377
};
374378

375379
struct Ctx {
380+
SmallVector<std::unique_ptr<MemoryBuffer>> memoryBuffers;
381+
SmallVector<ELFFileBase *, 0> objectFiles;
382+
SmallVector<SharedFile *, 0> sharedFiles;
383+
SmallVector<BinaryFile *, 0> binaryFiles;
384+
SmallVector<BitcodeFile *, 0> bitcodeFiles;
385+
SmallVector<BitcodeFile *, 0> lazyBitcodeFiles;
376386
// Duplicate symbol candidates.
377387
SmallVector<DuplicateSymbol, 0> duplicates;
378388
// Symbols in a non-prevailing COMDAT group which should be changed to an

lld/ELF/Driver.cpp

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,6 @@ bool elf::link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
9797
ctx->e.cleanupCallback = []() {
9898
inputSections.clear();
9999
outputSections.clear();
100-
memoryBuffers.clear();
101-
binaryFiles.clear();
102-
bitcodeFiles.clear();
103-
lazyBitcodeFiles.clear();
104-
objectFiles.clear();
105-
sharedFiles.clear();
106100
symAux.clear();
107101

108102
tar = nullptr;
@@ -198,7 +192,7 @@ std::vector<std::pair<MemoryBufferRef, uint64_t>> static getArchiveMembers(
198192

199193
// Take ownership of memory buffers created for members of thin archives.
200194
std::vector<std::unique_ptr<MemoryBuffer>> mbs = file->takeThinBuffers();
201-
std::move(mbs.begin(), mbs.end(), std::back_inserter(memoryBuffers));
195+
std::move(mbs.begin(), mbs.end(), std::back_inserter(ctx->memoryBuffers));
202196

203197
return v;
204198
}
@@ -843,7 +837,7 @@ static std::pair<bool, bool> getPackDynRelocs(opt::InputArgList &args) {
843837
static void readCallGraph(MemoryBufferRef mb) {
844838
// Build a map from symbol name to section
845839
DenseMap<StringRef, Symbol *> map;
846-
for (ELFFileBase *file : objectFiles)
840+
for (ELFFileBase *file : ctx->objectFiles)
847841
for (Symbol *sym : file->getSymbols())
848842
map[sym->getName()] = sym;
849843

@@ -922,7 +916,7 @@ processCallGraphRelocations(SmallVector<uint32_t, 32> &symbolIndices,
922916
template <class ELFT> static void readCallGraphsFromObjectFiles() {
923917
SmallVector<uint32_t, 32> symbolIndices;
924918
ArrayRef<typename ELFT::CGProfile> cgProfile;
925-
for (auto file : objectFiles) {
919+
for (auto file : ctx->objectFiles) {
926920
auto *obj = cast<ObjFile<ELFT>>(file);
927921
if (!processCallGraphRelocations(symbolIndices, cgProfile, obj))
928922
continue;
@@ -1757,10 +1751,10 @@ static void excludeLibs(opt::InputArgList &args) {
17571751
sym->versionId = VER_NDX_LOCAL;
17581752
};
17591753

1760-
for (ELFFileBase *file : objectFiles)
1754+
for (ELFFileBase *file : ctx->objectFiles)
17611755
visit(file);
17621756

1763-
for (BitcodeFile *file : bitcodeFiles)
1757+
for (BitcodeFile *file : ctx->bitcodeFiles)
17641758
visit(file);
17651759
}
17661760

@@ -1826,10 +1820,10 @@ static void writeArchiveStats() {
18261820

18271821
SmallVector<StringRef, 0> archives;
18281822
DenseMap<CachedHashStringRef, unsigned> all, extracted;
1829-
for (ELFFileBase *file : objectFiles)
1823+
for (ELFFileBase *file : ctx->objectFiles)
18301824
if (file->archiveName.size())
18311825
++extracted[CachedHashStringRef(file->archiveName)];
1832-
for (BitcodeFile *file : bitcodeFiles)
1826+
for (BitcodeFile *file : ctx->bitcodeFiles)
18331827
if (file->archiveName.size())
18341828
++extracted[CachedHashStringRef(file->archiveName)];
18351829
for (std::pair<StringRef, unsigned> f : driver->archiveFiles) {
@@ -1953,7 +1947,7 @@ static void writeDependencyFile() {
19531947
// symbols of type CommonSymbol.
19541948
static void replaceCommonSymbols() {
19551949
llvm::TimeTraceScope timeScope("Replace common symbols");
1956-
for (ELFFileBase *file : objectFiles) {
1950+
for (ELFFileBase *file : ctx->objectFiles) {
19571951
if (!file->hasCommonSyms)
19581952
continue;
19591953
for (Symbol *sym : file->getGlobalSymbols()) {
@@ -2029,7 +2023,7 @@ static void findKeepUniqueSections(opt::InputArgList &args) {
20292023

20302024
// Visit the address-significance table in each object file and mark each
20312025
// referenced symbol as address-significant.
2032-
for (InputFile *f : objectFiles) {
2026+
for (InputFile *f : ctx->objectFiles) {
20332027
auto *obj = cast<ObjFile<ELFT>>(f);
20342028
ArrayRef<Symbol *> syms = obj->getSymbols();
20352029
if (obj->addrsigSec) {
@@ -2115,18 +2109,18 @@ static void markBuffersAsDontNeed(bool skipLinkedOutput) {
21152109
// buffers as MADV_DONTNEED so that these pages can be reused by the expensive
21162110
// thin link, saving memory.
21172111
if (skipLinkedOutput) {
2118-
for (MemoryBuffer &mb : llvm::make_pointee_range(memoryBuffers))
2112+
for (MemoryBuffer &mb : llvm::make_pointee_range(ctx->memoryBuffers))
21192113
mb.dontNeedIfMmap();
21202114
return;
21212115
}
21222116

21232117
// Otherwise, just mark MemoryBuffers backing BitcodeFiles.
21242118
DenseSet<const char *> bufs;
2125-
for (BitcodeFile *file : bitcodeFiles)
2119+
for (BitcodeFile *file : ctx->bitcodeFiles)
21262120
bufs.insert(file->mb.getBufferStart());
2127-
for (BitcodeFile *file : lazyBitcodeFiles)
2121+
for (BitcodeFile *file : ctx->lazyBitcodeFiles)
21282122
bufs.insert(file->mb.getBufferStart());
2129-
for (MemoryBuffer &mb : llvm::make_pointee_range(memoryBuffers))
2123+
for (MemoryBuffer &mb : llvm::make_pointee_range(ctx->memoryBuffers))
21302124
if (bufs.count(mb.getBufferStart()))
21312125
mb.dontNeedIfMmap();
21322126
}
@@ -2143,10 +2137,10 @@ void LinkerDriver::compileBitcodeFiles(bool skipLinkedOutput) {
21432137
llvm::TimeTraceScope timeScope("LTO");
21442138
// Compile bitcode files and replace bitcode symbols.
21452139
lto.reset(new BitcodeCompiler);
2146-
for (BitcodeFile *file : bitcodeFiles)
2140+
for (BitcodeFile *file : ctx->bitcodeFiles)
21472141
lto->add(*file);
21482142

2149-
if (!bitcodeFiles.empty())
2143+
if (!ctx->bitcodeFiles.empty())
21502144
markBuffersAsDontNeed(skipLinkedOutput);
21512145

21522146
for (InputFile *file : lto->compile()) {
@@ -2158,7 +2152,7 @@ void LinkerDriver::compileBitcodeFiles(bool skipLinkedOutput) {
21582152
for (Symbol *sym : obj->getGlobalSymbols())
21592153
if (sym->hasVersionSuffix)
21602154
sym->parseSymbolVersion();
2161-
objectFiles.push_back(obj);
2155+
ctx->objectFiles.push_back(obj);
21622156
}
21632157
}
21642158

@@ -2283,7 +2277,7 @@ static void redirectSymbols(ArrayRef<WrappedSymbol> wrapped) {
22832277
return;
22842278

22852279
// Update pointers in input files.
2286-
parallelForEach(objectFiles, [&](ELFFileBase *file) {
2280+
parallelForEach(ctx->objectFiles, [&](ELFFileBase *file) {
22872281
for (Symbol *&sym : file->getMutableGlobalSymbols())
22882282
if (Symbol *s = map.lookup(sym))
22892283
sym = s;
@@ -2318,7 +2312,7 @@ static uint32_t getAndFeatures() {
23182312
return 0;
23192313

23202314
uint32_t ret = -1;
2321-
for (ELFFileBase *f : objectFiles) {
2315+
for (ELFFileBase *f : ctx->objectFiles) {
23222316
uint32_t features = f->andFeatures;
23232317

23242318
checkAndReportMissingFeature(
@@ -2471,7 +2465,7 @@ void LinkerDriver::link(opt::InputArgList &args) {
24712465
// We also need one if any shared libraries are used and for pie executables
24722466
// (probably because the dynamic linker needs it).
24732467
config->hasDynSymTab =
2474-
!sharedFiles.empty() || config->isPic || config->exportDynamic;
2468+
!ctx->sharedFiles.empty() || config->isPic || config->exportDynamic;
24752469

24762470
// Some symbols (such as __ehdr_start) are defined lazily only when there
24772471
// are undefined symbols for them, so we add these to trigger that logic.
@@ -2517,7 +2511,7 @@ void LinkerDriver::link(opt::InputArgList &args) {
25172511
// to, i.e. if the symbol's definition is in bitcode. Any other required
25182512
// libcall symbols will be added to the link after LTO when we add the LTO
25192513
// object file to the link.
2520-
if (!bitcodeFiles.empty())
2514+
if (!ctx->bitcodeFiles.empty())
25212515
for (auto *s : lto::LTO::getRuntimeLibcallSymbols())
25222516
handleLibcall(s);
25232517

@@ -2526,9 +2520,10 @@ void LinkerDriver::link(opt::InputArgList &args) {
25262520

25272521
// No more lazy bitcode can be extracted at this point. Do post parse work
25282522
// like checking duplicate symbols.
2529-
parallelForEach(objectFiles, initializeLocalSymbols);
2530-
parallelForEach(objectFiles, postParseObjectFile);
2531-
parallelForEach(bitcodeFiles, [](BitcodeFile *file) { file->postParse(); });
2523+
parallelForEach(ctx->objectFiles, initializeLocalSymbols);
2524+
parallelForEach(ctx->objectFiles, postParseObjectFile);
2525+
parallelForEach(ctx->bitcodeFiles,
2526+
[](BitcodeFile *file) { file->postParse(); });
25322527
for (auto &it : ctx->nonPrevailingSyms) {
25332528
Symbol &sym = *it.first;
25342529
sym.replace(Undefined{sym.file, sym.getName(), sym.binding, sym.stOther,
@@ -2591,7 +2586,7 @@ void LinkerDriver::link(opt::InputArgList &args) {
25912586
//
25922587
// With this the symbol table should be complete. After this, no new names
25932588
// except a few linker-synthesized ones will be added to the symbol table.
2594-
const size_t numObjsBeforeLTO = objectFiles.size();
2589+
const size_t numObjsBeforeLTO = ctx->objectFiles.size();
25952590
invokeELFT(compileBitcodeFiles, skipLinkedOutput);
25962591

25972592
// Symbol resolution finished. Report backward reference problems,
@@ -2608,7 +2603,7 @@ void LinkerDriver::link(opt::InputArgList &args) {
26082603

26092604
// compileBitcodeFiles may have produced lto.tmp object files. After this, no
26102605
// more file will be added.
2611-
auto newObjectFiles = makeArrayRef(objectFiles).slice(numObjsBeforeLTO);
2606+
auto newObjectFiles = makeArrayRef(ctx->objectFiles).slice(numObjsBeforeLTO);
26122607
parallelForEach(newObjectFiles, initializeLocalSymbols);
26132608
parallelForEach(newObjectFiles, postParseObjectFile);
26142609
for (const DuplicateSymbol &d : ctx->duplicates)
@@ -2631,11 +2626,11 @@ void LinkerDriver::link(opt::InputArgList &args) {
26312626
// Now that we have a complete list of input files.
26322627
// Beyond this point, no new files are added.
26332628
// Aggregate all input sections into one place.
2634-
for (InputFile *f : objectFiles)
2629+
for (InputFile *f : ctx->objectFiles)
26352630
for (InputSectionBase *s : f->getSections())
26362631
if (s && s != &InputSection::discarded)
26372632
inputSections.push_back(s);
2638-
for (BinaryFile *f : binaryFiles)
2633+
for (BinaryFile *f : ctx->binaryFiles)
26392634
for (InputSectionBase *s : f->getSections())
26402635
inputSections.push_back(cast<InputSection>(s));
26412636
}

lld/ELF/ICF.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ template <class ELFT> void ICF<ELFT>::run() {
560560
};
561561
for (Symbol *sym : symtab->symbols())
562562
fold(sym);
563-
parallelForEach(objectFiles, [&](ELFFileBase *file) {
563+
parallelForEach(ctx->objectFiles, [&](ELFFileBase *file) {
564564
for (Symbol *sym : file->getLocalSymbols())
565565
fold(sym);
566566
});

0 commit comments

Comments
 (0)