[lld] 4986510 - [ELF] Pass Ctx & to InputFiles

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 6 11:27:36 PDT 2024


Author: Fangrui Song
Date: 2024-10-06T11:27:24-07:00
New Revision: 49865107d49b2be86d9ff5474d081c49d1556213

URL: https://github.com/llvm/llvm-project/commit/49865107d49b2be86d9ff5474d081c49d1556213
DIFF: https://github.com/llvm/llvm-project/commit/49865107d49b2be86d9ff5474d081c49d1556213.diff

LOG: [ELF] Pass Ctx & to InputFiles

Added: 
    

Modified: 
    lld/ELF/Driver.cpp
    lld/ELF/Driver.h
    lld/ELF/DriverUtils.cpp
    lld/ELF/EhFrame.cpp
    lld/ELF/InputFiles.cpp
    lld/ELF/InputFiles.h
    lld/ELF/ScriptParser.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 72dcf1d46bb74a..b6aae97dbc249f 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -281,7 +281,7 @@ bool LinkerDriver::tryAddFatLTOFile(MemoryBufferRef mb, StringRef archiveName,
 void LinkerDriver::addFile(StringRef path, bool withLOption) {
   using namespace sys::fs;
 
-  std::optional<MemoryBufferRef> buffer = readFile(path);
+  std::optional<MemoryBufferRef> buffer = readFile(ctx, path);
   if (!buffer)
     return;
   MemoryBufferRef mbref = *buffer;
@@ -352,7 +352,7 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
     // cannot be stored into SharedFile::soName.
     path = mbref.getBufferIdentifier();
     auto *f =
-        make<SharedFile>(mbref, withLOption ? path::filename(path) : path);
+        make<SharedFile>(ctx, mbref, withLOption ? path::filename(path) : path);
     f->init();
     files.push_back(f);
     return;
@@ -371,7 +371,7 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
 
 // Add a given library by searching it from input search paths.
 void LinkerDriver::addLibrary(StringRef name) {
-  if (std::optional<std::string> path = searchLibrary(name))
+  if (std::optional<std::string> path = searchLibrary(ctx, name))
     addFile(saver().save(*path), /*withLOption=*/true);
   else
     error("unable to find library -l" + name, ErrorTag::LibNotFound, {name});
@@ -665,7 +665,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
       ctx.tar->append("version.txt", getLLDVersion() + "\n");
       StringRef ltoSampleProfile = args.getLastArgValue(OPT_lto_sample_profile);
       if (!ltoSampleProfile.empty())
-        readFile(ltoSampleProfile);
+        readFile(ctx, ltoSampleProfile);
     } else {
       error("--reproduce: " + toString(errOrWriter.takeError()));
     }
@@ -1542,7 +1542,7 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
   }
   for (opt::Arg *arg : args.filtered(OPT_remap_inputs_file)) {
     StringRef filename(arg->getValue());
-    std::optional<MemoryBufferRef> buffer = readFile(filename);
+    std::optional<MemoryBufferRef> buffer = readFile(ctx, filename);
     if (!buffer)
       continue;
     // Parse 'from-glob=to-file' lines, ignoring #-led comments.
@@ -1761,7 +1761,8 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
     if (args.hasArg(OPT_call_graph_ordering_file))
       error("--symbol-ordering-file and --call-graph-order-file "
             "may not be used together");
-    if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue())) {
+    if (std::optional<MemoryBufferRef> buffer =
+            readFile(ctx, arg->getValue())) {
       ctx.arg.symbolOrderingFile = getSymbolOrderingFile(ctx, *buffer);
       // Also need to disable CallGraphProfileSort to prevent
       // LLD order symbols with CGProfile
@@ -1780,7 +1781,7 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
   if (auto *arg = args.getLastArg(OPT_retain_symbols_file)) {
     ctx.arg.versionDefinitions[VER_NDX_LOCAL].nonLocalPatterns.push_back(
         {"*", /*isExternCpp=*/false, /*hasWildcard=*/true});
-    if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue()))
+    if (std::optional<MemoryBufferRef> buffer = readFile(ctx, arg->getValue()))
       for (StringRef s : args::getLines(*buffer))
         ctx.arg.versionDefinitions[VER_NDX_GLOBAL].nonLocalPatterns.push_back(
             {s, /*isExternCpp=*/false, /*hasWildcard=*/false});
@@ -1812,12 +1813,12 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
       ctx.arg.bsymbolic == BsymbolicKind::All || args.hasArg(OPT_dynamic_list);
   for (auto *arg :
        args.filtered(OPT_dynamic_list, OPT_export_dynamic_symbol_list))
-    if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue()))
+    if (std::optional<MemoryBufferRef> buffer = readFile(ctx, arg->getValue()))
       readDynamicList(ctx, *buffer);
 
   for (auto *arg : args.filtered(OPT_version_script))
-    if (std::optional<std::string> path = searchScript(arg->getValue())) {
-      if (std::optional<MemoryBufferRef> buffer = readFile(*path))
+    if (std::optional<std::string> path = searchScript(ctx, arg->getValue())) {
+      if (std::optional<MemoryBufferRef> buffer = readFile(ctx, *path))
         readVersionScript(ctx, *buffer);
     } else {
       error(Twine("cannot find version script ") + arg->getValue());
@@ -1951,8 +1952,9 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
     }
     case OPT_script:
     case OPT_default_script:
-      if (std::optional<std::string> path = searchScript(arg->getValue())) {
-        if (std::optional<MemoryBufferRef> mb = readFile(*path)) {
+      if (std::optional<std::string> path =
+              searchScript(ctx, arg->getValue())) {
+        if (std::optional<MemoryBufferRef> mb = readFile(ctx, *path)) {
           if (arg->getOption().matches(OPT_default_script)) {
             defaultScript = mb;
           } else {
@@ -1989,7 +1991,7 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
       inWholeArchive = false;
       break;
     case OPT_just_symbols:
-      if (std::optional<MemoryBufferRef> mb = readFile(arg->getValue())) {
+      if (std::optional<MemoryBufferRef> mb = readFile(ctx, arg->getValue())) {
         files.push_back(createObjFile(*mb));
         files.back()->justSymbols = true;
       }
@@ -1997,7 +1999,8 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
     case OPT_in_implib:
       if (armCmseImpLib)
         error("multiple CMSE import libraries not supported");
-      else if (std::optional<MemoryBufferRef> mb = readFile(arg->getValue()))
+      else if (std::optional<MemoryBufferRef> mb =
+                   readFile(ctx, arg->getValue()))
         armCmseImpLib = createObjFile(*mb);
       break;
     case OPT_start_group:
@@ -3208,7 +3211,8 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
   // Read the callgraph now that we know what was gced or icfed
   if (ctx.arg.callGraphProfileSort != CGProfileSortKind::None) {
     if (auto *arg = args.getLastArg(OPT_call_graph_ordering_file))
-      if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue()))
+      if (std::optional<MemoryBufferRef> buffer =
+              readFile(ctx, arg->getValue()))
         readCallGraph(ctx, *buffer);
     readCallGraphsFromObjectFiles<ELFT>(ctx);
   }

diff  --git a/lld/ELF/Driver.h b/lld/ELF/Driver.h
index f555e5267e32e5..95cc437d351f76 100644
--- a/lld/ELF/Driver.h
+++ b/lld/ELF/Driver.h
@@ -35,10 +35,10 @@ enum {
 void printHelp(Ctx &ctx);
 std::string createResponseFile(const llvm::opt::InputArgList &args);
 
-std::optional<std::string> findFromSearchPaths(StringRef path);
-std::optional<std::string> searchScript(StringRef path);
-std::optional<std::string> searchLibraryBaseName(StringRef path);
-std::optional<std::string> searchLibrary(StringRef path);
+std::optional<std::string> findFromSearchPaths(Ctx &, StringRef path);
+std::optional<std::string> searchScript(Ctx &, StringRef path);
+std::optional<std::string> searchLibraryBaseName(Ctx &, StringRef path);
+std::optional<std::string> searchLibrary(Ctx &, StringRef path);
 
 } // namespace lld::elf
 

diff  --git a/lld/ELF/DriverUtils.cpp b/lld/ELF/DriverUtils.cpp
index d87f2d85e5a523..8b93068f2f9526 100644
--- a/lld/ELF/DriverUtils.cpp
+++ b/lld/ELF/DriverUtils.cpp
@@ -223,7 +223,7 @@ static std::optional<std::string> findFile(StringRef path1,
   return std::nullopt;
 }
 
-std::optional<std::string> elf::findFromSearchPaths(StringRef path) {
+std::optional<std::string> elf::findFromSearchPaths(Ctx &ctx, StringRef path) {
   for (StringRef dir : ctx.arg.searchPaths)
     if (std::optional<std::string> s = findFile(dir, path))
       return s;
@@ -232,7 +232,8 @@ std::optional<std::string> elf::findFromSearchPaths(StringRef path) {
 
 // This is for -l<basename>. We'll look for lib<basename>.so or lib<basename>.a from
 // search paths.
-std::optional<std::string> elf::searchLibraryBaseName(StringRef name) {
+std::optional<std::string> elf::searchLibraryBaseName(Ctx &ctx,
+                                                      StringRef name) {
   for (StringRef dir : ctx.arg.searchPaths) {
     if (!ctx.arg.isStatic)
       if (std::optional<std::string> s = findFile(dir, "lib" + name + ".so"))
@@ -244,18 +245,18 @@ std::optional<std::string> elf::searchLibraryBaseName(StringRef name) {
 }
 
 // This is for -l<namespec>.
-std::optional<std::string> elf::searchLibrary(StringRef name) {
+std::optional<std::string> elf::searchLibrary(Ctx &ctx, StringRef name) {
   llvm::TimeTraceScope timeScope("Locate library", name);
   if (name.starts_with(":"))
-    return findFromSearchPaths(name.substr(1));
-  return searchLibraryBaseName(name);
+    return findFromSearchPaths(ctx, name.substr(1));
+  return searchLibraryBaseName(ctx, name);
 }
 
 // If a linker/version script doesn't exist in the current directory, we also
 // look for the script in the '-L' search paths. This matches the behaviour of
 // '-T', --version-script=, and linker script INPUT() command in ld.bfd.
-std::optional<std::string> elf::searchScript(StringRef name) {
+std::optional<std::string> elf::searchScript(Ctx &ctx, StringRef name) {
   if (fs::exists(name))
     return name.str();
-  return findFromSearchPaths(name);
+  return findFromSearchPaths(ctx, name);
 }

diff  --git a/lld/ELF/EhFrame.cpp b/lld/ELF/EhFrame.cpp
index efedbd35d47ccf..d2d0e62e97ec45 100644
--- a/lld/ELF/EhFrame.cpp
+++ b/lld/ELF/EhFrame.cpp
@@ -97,7 +97,7 @@ void EhReader::skipLeb128() {
   failOn(errPos, "corrupted CIE (failed to read LEB128)");
 }
 
-static size_t getAugPSize(unsigned enc) {
+static size_t getAugPSize(Ctx &ctx, unsigned enc) {
   switch (enc & 0x0f) {
   case DW_EH_PE_absptr:
   case DW_EH_PE_signed:
@@ -119,7 +119,7 @@ void EhReader::skipAugP() {
   uint8_t enc = readByte();
   if ((enc & 0xf0) == DW_EH_PE_aligned)
     failOn(d.data() - 1, "DW_EH_PE_aligned encoding is not supported");
-  size_t size = getAugPSize(enc);
+  size_t size = getAugPSize(ctx, enc);
   if (size == 0)
     failOn(d.data() - 1, "unknown FDE encoding");
   if (size >= d.size())

diff  --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 651d32b4039bf9..7a451f38ba794c 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -213,7 +213,7 @@ InputFile::InputFile(Kind k, MemoryBufferRef m)
     ++nextGroupId;
 }
 
-std::optional<MemoryBufferRef> elf::readFile(StringRef path) {
+std::optional<MemoryBufferRef> elf::readFile(Ctx &ctx, StringRef path) {
   llvm::TimeTraceScope timeScope("Load input files", path);
 
   // The --chroot option changes our virtual root directory.
@@ -423,9 +423,9 @@ static void addDependentLibrary(Ctx &ctx, StringRef specifier,
                                 const InputFile *f) {
   if (!ctx.arg.dependentLibraries)
     return;
-  if (std::optional<std::string> s = searchLibraryBaseName(specifier))
+  if (std::optional<std::string> s = searchLibraryBaseName(ctx, specifier))
     ctx.driver.addFile(saver().save(*s), /*withLOption=*/true);
-  else if (std::optional<std::string> s = findFromSearchPaths(specifier))
+  else if (std::optional<std::string> s = findFromSearchPaths(ctx, specifier))
     ctx.driver.addFile(saver().save(*s), /*withLOption=*/true);
   else if (fs::exists(specifier))
     ctx.driver.addFile(specifier, /*withLOption=*/false);
@@ -1359,7 +1359,7 @@ static bool isNonCommonDef(MemoryBufferRef mb, StringRef symName,
 
 unsigned SharedFile::vernauxNum;
 
-SharedFile::SharedFile(MemoryBufferRef m, StringRef defaultSoName)
+SharedFile::SharedFile(Ctx &ctx, MemoryBufferRef m, StringRef defaultSoName)
     : ELFFileBase(SharedKind, getELFKind(m, ""), m), soName(defaultSoName),
       isNeeded(!ctx.arg.asNeeded) {}
 
@@ -1903,10 +1903,11 @@ template <class ELFT> void ObjFile<ELFT>::parseLazy() {
   // resolve() may trigger this->extract() if an existing symbol is an undefined
   // symbol. If that happens, this function has served its purpose, and we can
   // exit from the loop early.
+  auto *symtab = ctx.symtab.get();
   for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) {
     if (eSyms[i].st_shndx == SHN_UNDEF)
       continue;
-    symbols[i] = ctx.symtab->insert(CHECK(eSyms[i].getName(stringTable), this));
+    symbols[i] = symtab->insert(CHECK(eSyms[i].getName(stringTable), this));
     symbols[i]->resolve(LazySymbol{*this});
     if (!lazy)
       break;

diff  --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h
index e93539b8c590e5..4f78cdb60d9762 100644
--- a/lld/ELF/InputFiles.h
+++ b/lld/ELF/InputFiles.h
@@ -40,7 +40,7 @@ class InputSection;
 class Symbol;
 
 // Opens a given file.
-std::optional<MemoryBufferRef> readFile(StringRef path);
+std::optional<MemoryBufferRef> readFile(Ctx &, StringRef path);
 
 // Add symbols in File to the symbol table.
 void parseFile(Ctx &, InputFile *file);
@@ -339,7 +339,7 @@ class BitcodeFile : public InputFile {
 // .so file.
 class SharedFile : public ELFFileBase {
 public:
-  SharedFile(MemoryBufferRef m, StringRef defaultSoName);
+  SharedFile(Ctx &, MemoryBufferRef m, StringRef defaultSoName);
 
   // This is actually a vector of Elf_Verdef pointers.
   SmallVector<const void *, 0> verdefs;

diff  --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index 3282551c441d12..cc0e8cef9763b0 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -352,7 +352,7 @@ void ScriptParser::addFile(StringRef s) {
       ctx.driver.addFile(s, /*withLOption=*/false);
     } else {
       // Finally, search in the list of library paths.
-      if (std::optional<std::string> path = findFromSearchPaths(s))
+      if (std::optional<std::string> path = findFromSearchPaths(ctx, s))
         ctx.driver.addFile(saver().save(*path), /*withLOption=*/true);
       else
         setError("unable to find " + s);
@@ -400,8 +400,8 @@ void ScriptParser::readInclude() {
     return;
   }
 
-  if (std::optional<std::string> path = searchScript(name)) {
-    if (std::optional<MemoryBufferRef> mb = readFile(*path)) {
+  if (std::optional<std::string> path = searchScript(ctx, name)) {
+    if (std::optional<MemoryBufferRef> mb = readFile(ctx, *path)) {
       buffers.push_back(curBuf);
       curBuf = Buffer(ctx, *mb);
       mbs.push_back(*mb);


        


More information about the llvm-commits mailing list