[lld] ee7720a - [ELF] Avoid repeated getObj construction in getSectionIndex. NFC

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 29 16:51:06 PST 2022


Author: Fangrui Song
Date: 2022-01-29T16:51:00-08:00
New Revision: ee7720acd602046e316b4f8e605eefca0081c0a0

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

LOG: [ELF] Avoid repeated getObj construction in getSectionIndex. NFC

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 825f29a48e2e..0930df886f06 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -401,14 +401,15 @@ uint32_t ObjFile<ELFT>::getSectionIndex(const Elf_Sym &sym) const {
 }
 
 template <class ELFT> void ObjFile<ELFT>::parse(bool ignoreComdats) {
+  object::ELFFile<ELFT> obj = this->getObj();
   // Read a section table. justSymbols is usually false.
   if (this->justSymbols)
     initializeJustSymbols();
   else
-    initializeSections(ignoreComdats);
+    initializeSections(ignoreComdats, obj);
 
   // Read a symbol table.
-  initializeSymbols();
+  initializeSymbols(obj);
 }
 
 // Sections with SHT_GROUP and comdat bits define comdat section groups.
@@ -541,9 +542,8 @@ static void handleSectionGroup(ArrayRef<InputSectionBase *> sections,
 }
 
 template <class ELFT>
-void ObjFile<ELFT>::initializeSections(bool ignoreComdats) {
-  const ELFFile<ELFT> &obj = this->getObj();
-
+void ObjFile<ELFT>::initializeSections(bool ignoreComdats,
+                                       const llvm::object::ELFFile<ELFT> &obj) {
   ArrayRef<Elf_Shdr> objSections = getELFShdrs<ELFT>();
   StringRef shstrtab = CHECK(obj.getSectionStringTable(objSections), this);
   uint64_t size = objSections.size();
@@ -602,7 +602,8 @@ void ObjFile<ELFT>::initializeSections(bool ignoreComdats) {
               .second;
       if (keepGroup) {
         if (config->relocatable)
-          this->sections[i] = createInputSection(i, sec, shstrtab);
+          this->sections[i] = createInputSection(
+              i, sec, check(obj.getSectionName(sec, shstrtab)), shstrtab);
         selectedGroups.push_back(entries);
         continue;
       }
@@ -626,7 +627,8 @@ void ObjFile<ELFT>::initializeSections(bool ignoreComdats) {
     case SHT_NULL:
       break;
     default:
-      this->sections[i] = createInputSection(i, sec, shstrtab);
+      this->sections[i] = createInputSection(
+          i, sec, check(obj.getSectionName(sec, shstrtab)), shstrtab);
     }
   }
 
@@ -889,12 +891,10 @@ InputSectionBase *ObjFile<ELFT>::getRelocTarget(uint32_t idx,
 }
 
 template <class ELFT>
-InputSectionBase *ObjFile<ELFT>::createInputSection(uint32_t idx,
-                                                    const Elf_Shdr &sec,
-                                                    StringRef shstrtab) {
-  StringRef name = CHECK(getObj().getSectionName(sec, shstrtab), this);
-
-  if (config->emachine == EM_ARM && sec.sh_type == SHT_ARM_ATTRIBUTES) {
+InputSectionBase *
+ObjFile<ELFT>::createInputSection(uint32_t idx, const Elf_Shdr &sec,
+                                  StringRef name, StringRef shstrtab) {
+  if (sec.sh_type == SHT_ARM_ATTRIBUTES && config->emachine == EM_ARM) {
     ARMAttributeParser attributes;
     ArrayRef<uint8_t> contents = check(this->getObj().getSectionContents(sec));
     if (Error e = attributes.parse(contents, config->ekind == ELF32LEKind
@@ -918,7 +918,7 @@ InputSectionBase *ObjFile<ELFT>::createInputSection(uint32_t idx,
     }
   }
 
-  if (config->emachine == EM_RISCV && sec.sh_type == SHT_RISCV_ATTRIBUTES) {
+  if (sec.sh_type == SHT_RISCV_ATTRIBUTES && config->emachine == EM_RISCV) {
     RISCVAttributeParser attributes;
     ArrayRef<uint8_t> contents = check(this->getObj().getSectionContents(sec));
     if (Error e = attributes.parse(contents, support::little)) {
@@ -1040,7 +1040,8 @@ InputSectionBase *ObjFile<ELFT>::createInputSection(uint32_t idx,
 
 // Initialize this->Symbols. this->Symbols is a parallel array as
 // its corresponding ELF symbol table.
-template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
+template <class ELFT>
+void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) {
   ArrayRef<InputSectionBase *> sections(this->sections);
   SymbolTable &symtab = *elf::symtab;
 
@@ -1053,7 +1054,9 @@ template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
 
   for (size_t i = 0, end = firstGlobal; i != end; ++i) {
     const Elf_Sym &eSym = eSyms[i];
-    uint32_t secIdx = getSectionIndex(eSym);
+    const uint32_t secIdx =
+        check(obj.getSectionIndex(eSym, getELFSyms<ELFT>(), shndxTable));
+
     if (LLVM_UNLIKELY(secIdx >= sections.size()))
       fatal(toString(this) + ": invalid section index: " + Twine(secIdx));
     if (LLVM_UNLIKELY(eSym.getBinding() != STB_LOCAL))
@@ -1093,7 +1096,8 @@ template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
                   Twine(firstGlobal) + ")");
       continue;
     }
-    uint32_t secIdx = getSectionIndex(eSym);
+    const uint32_t secIdx =
+        check(obj.getSectionIndex(eSym, getELFSyms<ELFT>(), shndxTable));
     if (LLVM_UNLIKELY(secIdx >= sections.size()))
       fatal(toString(this) + ": invalid section index: " + Twine(secIdx));
     InputSectionBase *sec = sections[secIdx];

diff  --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h
index 44442ffe76ed..38e148fe52e1 100644
--- a/lld/ELF/InputFiles.h
+++ b/lld/ELF/InputFiles.h
@@ -281,14 +281,15 @@ template <class ELFT> class ObjFile : public ELFFileBase {
   DWARFCache *getDwarf();
 
 private:
-  void initializeSections(bool ignoreComdats);
-  void initializeSymbols();
+  void initializeSections(bool ignoreComdats,
+                          const llvm::object::ELFFile<ELFT> &obj);
+  void initializeSymbols(const llvm::object::ELFFile<ELFT> &obj);
   void initializeJustSymbols();
 
   InputSectionBase *getRelocTarget(uint32_t idx, const Elf_Shdr &sec,
                                    uint32_t info);
   InputSectionBase *createInputSection(uint32_t idx, const Elf_Shdr &sec,
-                                       StringRef shstrtab);
+                                       StringRef name, StringRef shstrtab);
 
   bool shouldMerge(const Elf_Shdr &sec, StringRef name);
 


        


More information about the llvm-commits mailing list