Skip to content

Commit 4986510

Browse files
committed
[ELF] Pass Ctx & to InputFiles
1 parent 26ca8ef commit 4986510

File tree

7 files changed

+44
-38
lines changed

7 files changed

+44
-38
lines changed

lld/ELF/Driver.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ bool LinkerDriver::tryAddFatLTOFile(MemoryBufferRef mb, StringRef archiveName,
281281
void LinkerDriver::addFile(StringRef path, bool withLOption) {
282282
using namespace sys::fs;
283283

284-
std::optional<MemoryBufferRef> buffer = readFile(path);
284+
std::optional<MemoryBufferRef> buffer = readFile(ctx, path);
285285
if (!buffer)
286286
return;
287287
MemoryBufferRef mbref = *buffer;
@@ -352,7 +352,7 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
352352
// cannot be stored into SharedFile::soName.
353353
path = mbref.getBufferIdentifier();
354354
auto *f =
355-
make<SharedFile>(mbref, withLOption ? path::filename(path) : path);
355+
make<SharedFile>(ctx, mbref, withLOption ? path::filename(path) : path);
356356
f->init();
357357
files.push_back(f);
358358
return;
@@ -371,7 +371,7 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
371371

372372
// Add a given library by searching it from input search paths.
373373
void LinkerDriver::addLibrary(StringRef name) {
374-
if (std::optional<std::string> path = searchLibrary(name))
374+
if (std::optional<std::string> path = searchLibrary(ctx, name))
375375
addFile(saver().save(*path), /*withLOption=*/true);
376376
else
377377
error("unable to find library -l" + name, ErrorTag::LibNotFound, {name});
@@ -665,7 +665,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
665665
ctx.tar->append("version.txt", getLLDVersion() + "\n");
666666
StringRef ltoSampleProfile = args.getLastArgValue(OPT_lto_sample_profile);
667667
if (!ltoSampleProfile.empty())
668-
readFile(ltoSampleProfile);
668+
readFile(ctx, ltoSampleProfile);
669669
} else {
670670
error("--reproduce: " + toString(errOrWriter.takeError()));
671671
}
@@ -1542,7 +1542,7 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
15421542
}
15431543
for (opt::Arg *arg : args.filtered(OPT_remap_inputs_file)) {
15441544
StringRef filename(arg->getValue());
1545-
std::optional<MemoryBufferRef> buffer = readFile(filename);
1545+
std::optional<MemoryBufferRef> buffer = readFile(ctx, filename);
15461546
if (!buffer)
15471547
continue;
15481548
// Parse 'from-glob=to-file' lines, ignoring #-led comments.
@@ -1761,7 +1761,8 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
17611761
if (args.hasArg(OPT_call_graph_ordering_file))
17621762
error("--symbol-ordering-file and --call-graph-order-file "
17631763
"may not be used together");
1764-
if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue())) {
1764+
if (std::optional<MemoryBufferRef> buffer =
1765+
readFile(ctx, arg->getValue())) {
17651766
ctx.arg.symbolOrderingFile = getSymbolOrderingFile(ctx, *buffer);
17661767
// Also need to disable CallGraphProfileSort to prevent
17671768
// LLD order symbols with CGProfile
@@ -1780,7 +1781,7 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
17801781
if (auto *arg = args.getLastArg(OPT_retain_symbols_file)) {
17811782
ctx.arg.versionDefinitions[VER_NDX_LOCAL].nonLocalPatterns.push_back(
17821783
{"*", /*isExternCpp=*/false, /*hasWildcard=*/true});
1783-
if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue()))
1784+
if (std::optional<MemoryBufferRef> buffer = readFile(ctx, arg->getValue()))
17841785
for (StringRef s : args::getLines(*buffer))
17851786
ctx.arg.versionDefinitions[VER_NDX_GLOBAL].nonLocalPatterns.push_back(
17861787
{s, /*isExternCpp=*/false, /*hasWildcard=*/false});
@@ -1812,12 +1813,12 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
18121813
ctx.arg.bsymbolic == BsymbolicKind::All || args.hasArg(OPT_dynamic_list);
18131814
for (auto *arg :
18141815
args.filtered(OPT_dynamic_list, OPT_export_dynamic_symbol_list))
1815-
if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue()))
1816+
if (std::optional<MemoryBufferRef> buffer = readFile(ctx, arg->getValue()))
18161817
readDynamicList(ctx, *buffer);
18171818

18181819
for (auto *arg : args.filtered(OPT_version_script))
1819-
if (std::optional<std::string> path = searchScript(arg->getValue())) {
1820-
if (std::optional<MemoryBufferRef> buffer = readFile(*path))
1820+
if (std::optional<std::string> path = searchScript(ctx, arg->getValue())) {
1821+
if (std::optional<MemoryBufferRef> buffer = readFile(ctx, *path))
18211822
readVersionScript(ctx, *buffer);
18221823
} else {
18231824
error(Twine("cannot find version script ") + arg->getValue());
@@ -1951,8 +1952,9 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
19511952
}
19521953
case OPT_script:
19531954
case OPT_default_script:
1954-
if (std::optional<std::string> path = searchScript(arg->getValue())) {
1955-
if (std::optional<MemoryBufferRef> mb = readFile(*path)) {
1955+
if (std::optional<std::string> path =
1956+
searchScript(ctx, arg->getValue())) {
1957+
if (std::optional<MemoryBufferRef> mb = readFile(ctx, *path)) {
19561958
if (arg->getOption().matches(OPT_default_script)) {
19571959
defaultScript = mb;
19581960
} else {
@@ -1989,15 +1991,16 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
19891991
inWholeArchive = false;
19901992
break;
19911993
case OPT_just_symbols:
1992-
if (std::optional<MemoryBufferRef> mb = readFile(arg->getValue())) {
1994+
if (std::optional<MemoryBufferRef> mb = readFile(ctx, arg->getValue())) {
19931995
files.push_back(createObjFile(*mb));
19941996
files.back()->justSymbols = true;
19951997
}
19961998
break;
19971999
case OPT_in_implib:
19982000
if (armCmseImpLib)
19992001
error("multiple CMSE import libraries not supported");
2000-
else if (std::optional<MemoryBufferRef> mb = readFile(arg->getValue()))
2002+
else if (std::optional<MemoryBufferRef> mb =
2003+
readFile(ctx, arg->getValue()))
20012004
armCmseImpLib = createObjFile(*mb);
20022005
break;
20032006
case OPT_start_group:
@@ -3208,7 +3211,8 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
32083211
// Read the callgraph now that we know what was gced or icfed
32093212
if (ctx.arg.callGraphProfileSort != CGProfileSortKind::None) {
32103213
if (auto *arg = args.getLastArg(OPT_call_graph_ordering_file))
3211-
if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue()))
3214+
if (std::optional<MemoryBufferRef> buffer =
3215+
readFile(ctx, arg->getValue()))
32123216
readCallGraph(ctx, *buffer);
32133217
readCallGraphsFromObjectFiles<ELFT>(ctx);
32143218
}

lld/ELF/Driver.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ enum {
3535
void printHelp(Ctx &ctx);
3636
std::string createResponseFile(const llvm::opt::InputArgList &args);
3737

38-
std::optional<std::string> findFromSearchPaths(StringRef path);
39-
std::optional<std::string> searchScript(StringRef path);
40-
std::optional<std::string> searchLibraryBaseName(StringRef path);
41-
std::optional<std::string> searchLibrary(StringRef path);
38+
std::optional<std::string> findFromSearchPaths(Ctx &, StringRef path);
39+
std::optional<std::string> searchScript(Ctx &, StringRef path);
40+
std::optional<std::string> searchLibraryBaseName(Ctx &, StringRef path);
41+
std::optional<std::string> searchLibrary(Ctx &, StringRef path);
4242

4343
} // namespace lld::elf
4444

lld/ELF/DriverUtils.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ static std::optional<std::string> findFile(StringRef path1,
223223
return std::nullopt;
224224
}
225225

226-
std::optional<std::string> elf::findFromSearchPaths(StringRef path) {
226+
std::optional<std::string> elf::findFromSearchPaths(Ctx &ctx, StringRef path) {
227227
for (StringRef dir : ctx.arg.searchPaths)
228228
if (std::optional<std::string> s = findFile(dir, path))
229229
return s;
@@ -232,7 +232,8 @@ std::optional<std::string> elf::findFromSearchPaths(StringRef path) {
232232

233233
// This is for -l<basename>. We'll look for lib<basename>.so or lib<basename>.a from
234234
// search paths.
235-
std::optional<std::string> elf::searchLibraryBaseName(StringRef name) {
235+
std::optional<std::string> elf::searchLibraryBaseName(Ctx &ctx,
236+
StringRef name) {
236237
for (StringRef dir : ctx.arg.searchPaths) {
237238
if (!ctx.arg.isStatic)
238239
if (std::optional<std::string> s = findFile(dir, "lib" + name + ".so"))
@@ -244,18 +245,18 @@ std::optional<std::string> elf::searchLibraryBaseName(StringRef name) {
244245
}
245246

246247
// This is for -l<namespec>.
247-
std::optional<std::string> elf::searchLibrary(StringRef name) {
248+
std::optional<std::string> elf::searchLibrary(Ctx &ctx, StringRef name) {
248249
llvm::TimeTraceScope timeScope("Locate library", name);
249250
if (name.starts_with(":"))
250-
return findFromSearchPaths(name.substr(1));
251-
return searchLibraryBaseName(name);
251+
return findFromSearchPaths(ctx, name.substr(1));
252+
return searchLibraryBaseName(ctx, name);
252253
}
253254

254255
// If a linker/version script doesn't exist in the current directory, we also
255256
// look for the script in the '-L' search paths. This matches the behaviour of
256257
// '-T', --version-script=, and linker script INPUT() command in ld.bfd.
257-
std::optional<std::string> elf::searchScript(StringRef name) {
258+
std::optional<std::string> elf::searchScript(Ctx &ctx, StringRef name) {
258259
if (fs::exists(name))
259260
return name.str();
260-
return findFromSearchPaths(name);
261+
return findFromSearchPaths(ctx, name);
261262
}

lld/ELF/EhFrame.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void EhReader::skipLeb128() {
9797
failOn(errPos, "corrupted CIE (failed to read LEB128)");
9898
}
9999

100-
static size_t getAugPSize(unsigned enc) {
100+
static size_t getAugPSize(Ctx &ctx, unsigned enc) {
101101
switch (enc & 0x0f) {
102102
case DW_EH_PE_absptr:
103103
case DW_EH_PE_signed:
@@ -119,7 +119,7 @@ void EhReader::skipAugP() {
119119
uint8_t enc = readByte();
120120
if ((enc & 0xf0) == DW_EH_PE_aligned)
121121
failOn(d.data() - 1, "DW_EH_PE_aligned encoding is not supported");
122-
size_t size = getAugPSize(enc);
122+
size_t size = getAugPSize(ctx, enc);
123123
if (size == 0)
124124
failOn(d.data() - 1, "unknown FDE encoding");
125125
if (size >= d.size())

lld/ELF/InputFiles.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ InputFile::InputFile(Kind k, MemoryBufferRef m)
213213
++nextGroupId;
214214
}
215215

216-
std::optional<MemoryBufferRef> elf::readFile(StringRef path) {
216+
std::optional<MemoryBufferRef> elf::readFile(Ctx &ctx, StringRef path) {
217217
llvm::TimeTraceScope timeScope("Load input files", path);
218218

219219
// The --chroot option changes our virtual root directory.
@@ -423,9 +423,9 @@ static void addDependentLibrary(Ctx &ctx, StringRef specifier,
423423
const InputFile *f) {
424424
if (!ctx.arg.dependentLibraries)
425425
return;
426-
if (std::optional<std::string> s = searchLibraryBaseName(specifier))
426+
if (std::optional<std::string> s = searchLibraryBaseName(ctx, specifier))
427427
ctx.driver.addFile(saver().save(*s), /*withLOption=*/true);
428-
else if (std::optional<std::string> s = findFromSearchPaths(specifier))
428+
else if (std::optional<std::string> s = findFromSearchPaths(ctx, specifier))
429429
ctx.driver.addFile(saver().save(*s), /*withLOption=*/true);
430430
else if (fs::exists(specifier))
431431
ctx.driver.addFile(specifier, /*withLOption=*/false);
@@ -1359,7 +1359,7 @@ static bool isNonCommonDef(MemoryBufferRef mb, StringRef symName,
13591359

13601360
unsigned SharedFile::vernauxNum;
13611361

1362-
SharedFile::SharedFile(MemoryBufferRef m, StringRef defaultSoName)
1362+
SharedFile::SharedFile(Ctx &ctx, MemoryBufferRef m, StringRef defaultSoName)
13631363
: ELFFileBase(SharedKind, getELFKind(m, ""), m), soName(defaultSoName),
13641364
isNeeded(!ctx.arg.asNeeded) {}
13651365

@@ -1903,10 +1903,11 @@ template <class ELFT> void ObjFile<ELFT>::parseLazy() {
19031903
// resolve() may trigger this->extract() if an existing symbol is an undefined
19041904
// symbol. If that happens, this function has served its purpose, and we can
19051905
// exit from the loop early.
1906+
auto *symtab = ctx.symtab.get();
19061907
for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) {
19071908
if (eSyms[i].st_shndx == SHN_UNDEF)
19081909
continue;
1909-
symbols[i] = ctx.symtab->insert(CHECK(eSyms[i].getName(stringTable), this));
1910+
symbols[i] = symtab->insert(CHECK(eSyms[i].getName(stringTable), this));
19101911
symbols[i]->resolve(LazySymbol{*this});
19111912
if (!lazy)
19121913
break;

lld/ELF/InputFiles.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class InputSection;
4040
class Symbol;
4141

4242
// Opens a given file.
43-
std::optional<MemoryBufferRef> readFile(StringRef path);
43+
std::optional<MemoryBufferRef> readFile(Ctx &, StringRef path);
4444

4545
// Add symbols in File to the symbol table.
4646
void parseFile(Ctx &, InputFile *file);
@@ -339,7 +339,7 @@ class BitcodeFile : public InputFile {
339339
// .so file.
340340
class SharedFile : public ELFFileBase {
341341
public:
342-
SharedFile(MemoryBufferRef m, StringRef defaultSoName);
342+
SharedFile(Ctx &, MemoryBufferRef m, StringRef defaultSoName);
343343

344344
// This is actually a vector of Elf_Verdef pointers.
345345
SmallVector<const void *, 0> verdefs;

lld/ELF/ScriptParser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ void ScriptParser::addFile(StringRef s) {
352352
ctx.driver.addFile(s, /*withLOption=*/false);
353353
} else {
354354
// Finally, search in the list of library paths.
355-
if (std::optional<std::string> path = findFromSearchPaths(s))
355+
if (std::optional<std::string> path = findFromSearchPaths(ctx, s))
356356
ctx.driver.addFile(saver().save(*path), /*withLOption=*/true);
357357
else
358358
setError("unable to find " + s);
@@ -400,8 +400,8 @@ void ScriptParser::readInclude() {
400400
return;
401401
}
402402

403-
if (std::optional<std::string> path = searchScript(name)) {
404-
if (std::optional<MemoryBufferRef> mb = readFile(*path)) {
403+
if (std::optional<std::string> path = searchScript(ctx, name)) {
404+
if (std::optional<MemoryBufferRef> mb = readFile(ctx, *path)) {
405405
buffers.push_back(curBuf);
406406
curBuf = Buffer(ctx, *mb);
407407
mbs.push_back(*mb);

0 commit comments

Comments
 (0)