[lld] d9dbf9e - [ELF] Move init from ELFFileBase constructor to a separate function. NFC

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 2 21:10:36 PDT 2022


Author: Fangrui Song
Date: 2022-10-02T21:10:28-07:00
New Revision: d9dbf9e30a581fcadd667b6d8e5827a4003b85a2

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

LOG: [ELF] Move init from ELFFileBase constructor to a separate function. NFC

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 2e71282f0f15..47878a0e29ec 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -281,7 +281,7 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
       ++InputFile::nextGroupId;
     return;
   }
-  case file_magic::elf_shared_object:
+  case file_magic::elf_shared_object: {
     if (config->isStatic || config->relocatable) {
       error("attempted static link of dynamic object " + path);
       return;
@@ -292,9 +292,12 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
     // the directory part is ignored. Note that path may be a temporary and
     // cannot be stored into SharedFile::soName.
     path = mbref.getBufferIdentifier();
-    files.push_back(
-        make<SharedFile>(mbref, withLOption ? path::filename(path) : path));
+    auto *f =
+        make<SharedFile>(mbref, withLOption ? path::filename(path) : path);
+    f->init();
+    files.push_back(f);
     return;
+  }
   case file_magic::bitcode:
     files.push_back(make<BitcodeFile>(mbref, "", 0, inLib));
     break;

diff  --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 3ca9c0a52833..98f974f6520f 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -449,32 +449,35 @@ Optional<DILineInfo> ObjFile<ELFT>::getDILineInfo(InputSectionBase *s,
 ELFFileBase::ELFFileBase(Kind k, ELFKind ekind, MemoryBufferRef mb)
     : InputFile(k, mb) {
   this->ekind = ekind;
+}
+
+template <typename Elf_Shdr>
+static const Elf_Shdr *findSection(ArrayRef<Elf_Shdr> sections, uint32_t type) {
+  for (const Elf_Shdr &sec : sections)
+    if (sec.sh_type == type)
+      return &sec;
+  return nullptr;
+}
+
+void ELFFileBase::init() {
   switch (ekind) {
   case ELF32LEKind:
-    init<ELF32LE>(k);
+    init<ELF32LE>(fileKind);
     break;
   case ELF32BEKind:
-    init<ELF32BE>(k);
+    init<ELF32BE>(fileKind);
     break;
   case ELF64LEKind:
-    init<ELF64LE>(k);
+    init<ELF64LE>(fileKind);
     break;
   case ELF64BEKind:
-    init<ELF64BE>(k);
+    init<ELF64BE>(fileKind);
     break;
   default:
     llvm_unreachable("getELFKind");
   }
 }
 
-template <typename Elf_Shdr>
-static const Elf_Shdr *findSection(ArrayRef<Elf_Shdr> sections, uint32_t type) {
-  for (const Elf_Shdr &sec : sections)
-    if (sec.sh_type == type)
-      return &sec;
-  return nullptr;
-}
-
 template <class ELFT> void ELFFileBase::init(InputFile::Kind k) {
   using Elf_Shdr = typename ELFT::Shdr;
   using Elf_Sym = typename ELFT::Sym;
@@ -1233,6 +1236,7 @@ template <class ELFT>
 static bool isNonCommonDef(ELFKind ekind, MemoryBufferRef mb, StringRef symName,
                            StringRef archiveName) {
   ObjFile<ELFT> *obj = make<ObjFile<ELFT>>(ekind, mb, archiveName);
+  obj->init();
   StringRef stringtable = obj->getStringTable();
 
   for (auto sym : obj->template getGlobalELFSyms<ELFT>()) {
@@ -1754,6 +1758,7 @@ ELFFileBase *elf::createObjFile(MemoryBufferRef mb, StringRef archiveName,
   default:
     llvm_unreachable("getELFKind");
   }
+  f->init();
   f->lazy = lazy;
   return f;
 }

diff  --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h
index 9ef086f5a5fc..e318d9bf5de9 100644
--- a/lld/ELF/InputFiles.h
+++ b/lld/ELF/InputFiles.h
@@ -163,6 +163,7 @@ class ELFFileBase : public InputFile {
   ELFFileBase(Kind k, ELFKind ekind, MemoryBufferRef m);
   static bool classof(const InputFile *f) { return f->isElf(); }
 
+  void init();
   template <typename ELFT> llvm::object::ELFFile<ELFT> getObj() const {
     return check(llvm::object::ELFFile<ELFT>::create(mb.getBuffer()));
   }


        


More information about the llvm-commits mailing list