[lld] b672071 - [ELF] Pass Ctx & to InputFile

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 6 18:09:57 PDT 2024


Author: Fangrui Song
Date: 2024-10-06T18:09:52-07:00
New Revision: b672071ba51ef6b64651a62bcfaf78bdfdb7d3d4

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

LOG: [ELF] Pass Ctx & to InputFile

Added: 
    

Modified: 
    lld/ELF/Driver.cpp
    lld/ELF/InputFiles.cpp
    lld/ELF/InputFiles.h
    lld/ELF/LTO.cpp
    lld/ELF/LinkerScript.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 4206a7fcc65926..cde65de4142df9 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -288,7 +288,7 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
   MemoryBufferRef mbref = *buffer;
 
   if (ctx.arg.formatBinary) {
-    files.push_back(make<BinaryFile>(mbref));
+    files.push_back(make<BinaryFile>(ctx, mbref));
     return;
   }
 
@@ -304,7 +304,7 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
           files.push_back(
               make<BitcodeFile>(ctx, p.first, path, p.second, false));
         else if (!tryAddFatLTOFile(p.first, path, p.second, false))
-          files.push_back(createObjFile(p.first, path));
+          files.push_back(createObjFile(ctx, p.first, path));
       }
       return;
     }
@@ -328,7 +328,7 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
       auto magic = identify_magic(p.first.getBuffer());
       if (magic == file_magic::elf_relocatable) {
         if (!tryAddFatLTOFile(p.first, path, p.second, true))
-          files.push_back(createObjFile(p.first, path, true));
+          files.push_back(createObjFile(ctx, p.first, path, true));
       } else if (magic == file_magic::bitcode)
         files.push_back(make<BitcodeFile>(ctx, p.first, path, p.second, true));
       else
@@ -361,7 +361,7 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
     break;
   case file_magic::elf_relocatable:
     if (!tryAddFatLTOFile(mbref, "", 0, inLib))
-      files.push_back(createObjFile(mbref, "", inLib));
+      files.push_back(createObjFile(ctx, mbref, "", inLib));
     break;
   default:
     error(path + ": unknown file type");
@@ -1992,7 +1992,7 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
       break;
     case OPT_just_symbols:
       if (std::optional<MemoryBufferRef> mb = readFile(ctx, arg->getValue())) {
-        files.push_back(createObjFile(*mb));
+        files.push_back(createObjFile(ctx, *mb));
         files.back()->justSymbols = true;
       }
       break;
@@ -2001,7 +2001,7 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
         error("multiple CMSE import libraries not supported");
       else if (std::optional<MemoryBufferRef> mb =
                    readFile(ctx, arg->getValue()))
-        armCmseImpLib = createObjFile(*mb);
+        armCmseImpLib = createObjFile(ctx, *mb);
       break;
     case OPT_start_group:
       if (isInGroup)
@@ -2872,7 +2872,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
   for (auto *arg : args.filtered(OPT_trace_symbol))
     ctx.symtab->insert(arg->getValue())->traced = true;
 
-  ctx.internalFile = createInternalFile("<internal>");
+  ctx.internalFile = createInternalFile(ctx, "<internal>");
 
   // Handle -u/--undefined before input files. If both a.a and b.so define foo,
   // -u foo a.a b.so will extract a.a.

diff  --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index d2ba5b2e4f8f8f..39a78a65bd7cd6 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -202,8 +202,8 @@ static void updateSupportedARMFeatures(Ctx &ctx,
   ctx.arg.armHasThumb2ISA |= thumb && *thumb >= ARMBuildAttrs::AllowThumb32;
 }
 
-InputFile::InputFile(Kind k, MemoryBufferRef m)
-    : mb(m), groupId(ctx.driver.nextGroupId), fileKind(k) {
+InputFile::InputFile(Ctx &ctx, Kind k, MemoryBufferRef m)
+    : ctx(ctx), mb(m), groupId(ctx.driver.nextGroupId), fileKind(k) {
   // All files within the same --{start,end}-group get the same group ID.
   // Otherwise, a new file will get a new group ID.
   if (!ctx.driver.isInGroup)
@@ -509,8 +509,8 @@ ObjFile<ELFT>::getDILineInfo(const InputSectionBase *s, uint64_t offset) {
   return getDwarf()->getDILineInfo(offset, sectionIndex);
 }
 
-ELFFileBase::ELFFileBase(Kind k, ELFKind ekind, MemoryBufferRef mb)
-    : InputFile(k, mb) {
+ELFFileBase::ELFFileBase(Ctx &ctx, Kind k, ELFKind ekind, MemoryBufferRef mb)
+    : InputFile(ctx, k, mb) {
   this->ekind = ekind;
 }
 
@@ -950,7 +950,8 @@ void ObjFile<ELFT>::initializeSections(bool ignoreComdats,
 //   hardware-assisted call flow control;
 // - AArch64 PAuth ABI core info (16 bytes).
 template <class ELFT>
-void readGnuProperty(const InputSection &sec, ObjFile<ELFT> &f) {
+static void readGnuProperty(Ctx &ctx, const InputSection &sec,
+                            ObjFile<ELFT> &f) {
   using Elf_Nhdr = typename ELFT::Nhdr;
   using Elf_Note = typename ELFT::Note;
 
@@ -1070,7 +1071,7 @@ InputSectionBase *ObjFile<ELFT>::createInputSection(uint32_t idx,
     // .note.gnu.property containing a single AND'ed bitmap, we discard an input
     // file's .note.gnu.property section.
     if (name == ".note.gnu.property") {
-      readGnuProperty<ELFT>(InputSection(*this, sec, name), *this);
+      readGnuProperty<ELFT>(ctx, InputSection(*this, sec, name), *this);
       return &InputSection::discarded;
     }
 
@@ -1127,10 +1128,10 @@ void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) {
   }
 
   // Some entries have been filled by LazyObjFile.
+  auto *symtab = ctx.symtab.get();
   for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i)
     if (!symbols[i])
-      symbols[i] =
-          ctx.symtab->insert(CHECK(eSyms[i].getName(stringTable), this));
+      symbols[i] = symtab->insert(CHECK(eSyms[i].getName(stringTable), this));
 
   // Perform symbol resolution on non-local symbols.
   SmallVector<unsigned, 32> undefineds;
@@ -1325,7 +1326,7 @@ static bool isBitcodeNonCommonDef(MemoryBufferRef mb, StringRef symName,
 template <class ELFT>
 static bool isNonCommonDef(ELFKind ekind, MemoryBufferRef mb, StringRef symName,
                            StringRef archiveName) {
-  ObjFile<ELFT> *obj = make<ObjFile<ELFT>>(ekind, mb, archiveName);
+  ObjFile<ELFT> *obj = make<ObjFile<ELFT>>(ctx, ekind, mb, archiveName);
   obj->init();
   StringRef stringtable = obj->getStringTable();
 
@@ -1357,7 +1358,7 @@ static bool isNonCommonDef(MemoryBufferRef mb, StringRef symName,
 unsigned SharedFile::vernauxNum;
 
 SharedFile::SharedFile(Ctx &ctx, MemoryBufferRef m, StringRef defaultSoName)
-    : ELFFileBase(SharedKind, getELFKind(m, ""), m), soName(defaultSoName),
+    : ELFFileBase(ctx, SharedKind, getELFKind(m, ""), m), soName(defaultSoName),
       isNeeded(!ctx.arg.asNeeded) {}
 
 // Parse the version definitions in the object file if present, and return a
@@ -1697,7 +1698,7 @@ static uint8_t getOsAbi(const Triple &t) {
 
 BitcodeFile::BitcodeFile(Ctx &ctx, MemoryBufferRef mb, StringRef archiveName,
                          uint64_t offsetInArchive, bool lazy)
-    : InputFile(BitcodeKind, mb) {
+    : InputFile(ctx, BitcodeKind, mb) {
   this->archiveName = archiveName;
   this->lazy = lazy;
 
@@ -1859,30 +1860,30 @@ void BinaryFile::parse() {
                                            data.size(), 0, nullptr});
 }
 
-InputFile *elf::createInternalFile(StringRef name) {
+InputFile *elf::createInternalFile(Ctx &ctx, StringRef name) {
   auto *file =
-      make<InputFile>(InputFile::InternalKind, MemoryBufferRef("", name));
+      make<InputFile>(ctx, InputFile::InternalKind, MemoryBufferRef("", name));
   // References from an internal file do not lead to --warn-backrefs
   // diagnostics.
   file->groupId = 0;
   return file;
 }
 
-ELFFileBase *elf::createObjFile(MemoryBufferRef mb, StringRef archiveName,
-                                bool lazy) {
+ELFFileBase *elf::createObjFile(Ctx &ctx, MemoryBufferRef mb,
+                                StringRef archiveName, bool lazy) {
   ELFFileBase *f;
   switch (getELFKind(mb, archiveName)) {
   case ELF32LEKind:
-    f = make<ObjFile<ELF32LE>>(ELF32LEKind, mb, archiveName);
+    f = make<ObjFile<ELF32LE>>(ctx, ELF32LEKind, mb, archiveName);
     break;
   case ELF32BEKind:
-    f = make<ObjFile<ELF32BE>>(ELF32BEKind, mb, archiveName);
+    f = make<ObjFile<ELF32BE>>(ctx, ELF32BEKind, mb, archiveName);
     break;
   case ELF64LEKind:
-    f = make<ObjFile<ELF64LE>>(ELF64LEKind, mb, archiveName);
+    f = make<ObjFile<ELF64LE>>(ctx, ELF64LEKind, mb, archiveName);
     break;
   case ELF64BEKind:
-    f = make<ObjFile<ELF64BE>>(ELF64BEKind, mb, archiveName);
+    f = make<ObjFile<ELF64BE>>(ctx, ELF64BEKind, mb, archiveName);
     break;
   default:
     llvm_unreachable("getELFKind");

diff  --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h
index 50fa882922c767..0b54f92d1a2669 100644
--- a/lld/ELF/InputFiles.h
+++ b/lld/ELF/InputFiles.h
@@ -49,6 +49,7 @@ void parseFiles(Ctx &, const std::vector<InputFile *> &files);
 // The root class of input files.
 class InputFile {
 protected:
+  Ctx &ctx;
   std::unique_ptr<Symbol *[]> symbols;
   uint32_t numSymbols = 0;
   SmallVector<InputSectionBase *, 0> sections;
@@ -62,7 +63,7 @@ class InputFile {
     InternalKind,
   };
 
-  InputFile(Kind k, MemoryBufferRef m);
+  InputFile(Ctx &, Kind k, MemoryBufferRef m);
   Kind kind() const { return fileKind; }
 
   bool isElf() const {
@@ -175,7 +176,7 @@ class InputFile {
 
 class ELFFileBase : public InputFile {
 public:
-  ELFFileBase(Kind k, ELFKind ekind, MemoryBufferRef m);
+  ELFFileBase(Ctx &ctx, Kind k, ELFKind ekind, MemoryBufferRef m);
   static bool classof(const InputFile *f) { return f->isElf(); }
 
   void init();
@@ -239,8 +240,8 @@ template <class ELFT> class ObjFile : public ELFFileBase {
     return this->ELFFileBase::getObj<ELFT>();
   }
 
-  ObjFile(ELFKind ekind, MemoryBufferRef m, StringRef archiveName)
-      : ELFFileBase(ObjKind, ekind, m) {
+  ObjFile(Ctx &ctx, ELFKind ekind, MemoryBufferRef m, StringRef archiveName)
+      : ELFFileBase(ctx, ObjKind, ekind, m) {
     this->archiveName = archiveName;
   }
 
@@ -371,14 +372,15 @@ class SharedFile : public ELFFileBase {
 
 class BinaryFile : public InputFile {
 public:
-  explicit BinaryFile(MemoryBufferRef m) : InputFile(BinaryKind, m) {}
+  explicit BinaryFile(Ctx &ctx, MemoryBufferRef m)
+      : InputFile(ctx, BinaryKind, m) {}
   static bool classof(const InputFile *f) { return f->kind() == BinaryKind; }
   void parse();
 };
 
-InputFile *createInternalFile(StringRef name);
-ELFFileBase *createObjFile(MemoryBufferRef mb, StringRef archiveName = "",
-                           bool lazy = false);
+InputFile *createInternalFile(Ctx &, StringRef name);
+ELFFileBase *createObjFile(Ctx &, MemoryBufferRef mb,
+                           StringRef archiveName = "", bool lazy = false);
 
 std::string replaceThinLTOSuffix(Ctx &, StringRef path);
 

diff  --git a/lld/ELF/LTO.cpp b/lld/ELF/LTO.cpp
index 4df1b0c289eef8..02b5c09150c65c 100644
--- a/lld/ELF/LTO.cpp
+++ b/lld/ELF/LTO.cpp
@@ -413,7 +413,7 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
     if (savePrelink || ctx.arg.ltoEmitAsm)
       saveBuffer(buf[i].second, ltoObjName);
     if (!ctx.arg.ltoEmitAsm)
-      ret.push_back(createObjFile(MemoryBufferRef(objBuf, ltoObjName)));
+      ret.push_back(createObjFile(ctx, MemoryBufferRef(objBuf, ltoObjName)));
   }
   return ret;
 }

diff  --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 0ebc3b24d972da..a8b24256fe0c43 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -227,7 +227,7 @@ void LinkerScript::addSymbol(SymbolAssignment *cmd) {
   // write expressions like this: `alignment = 16; . = ALIGN(., alignment)`.
   uint64_t symValue = value.sec ? 0 : value.getValue();
 
-  Defined newSym(createInternalFile(cmd->location), cmd->name, STB_GLOBAL,
+  Defined newSym(createInternalFile(ctx, cmd->location), cmd->name, STB_GLOBAL,
                  visibility, value.type, symValue, 0, sec);
 
   Symbol *sym = ctx.symtab->insert(cmd->name);


        


More information about the llvm-commits mailing list