[llvm] df3e903 - [llvm-readobj/libObject] - Get rid of `FirstSym` argument. NFCI.

Georgii Rymar via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 21 06:08:19 PDT 2020


Author: Georgii Rymar
Date: 2020-09-21T16:07:50+03:00
New Revision: df3e903655e2499968fc7af64fb5fa52b2ee79bb

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

LOG: [llvm-readobj/libObject] - Get rid of `FirstSym` argument. NFCI.

We use `FirstSym` argument in `getExtendedSymbolTableIndex` to calculate
a symbol index:

```
&Sym - &FirstSym
```

Instead, we could pass the symbol index directly.
This is what this patch does, it allows to simplify another llvm-readobj API.

Differential revision: https://reviews.llvm.org/D88016

Added: 
    

Modified: 
    llvm/include/llvm/Object/ELF.h
    llvm/tools/llvm-readobj/ELFDumper.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Object/ELF.h b/llvm/include/llvm/Object/ELF.h
index f4ba2cf66d9f..7ebe6869c7b5 100644
--- a/llvm/include/llvm/Object/ELF.h
+++ b/llvm/include/llvm/Object/ELF.h
@@ -337,19 +337,17 @@ getSection(typename ELFT::ShdrRange Sections, uint32_t Index) {
 
 template <class ELFT>
 inline Expected<uint32_t>
-getExtendedSymbolTableIndex(const typename ELFT::Sym &Sym,
-                            const typename ELFT::Sym &FirstSym,
+getExtendedSymbolTableIndex(const typename ELFT::Sym &Sym, unsigned SymIndex,
                             ArrayRef<typename ELFT::Word> ShndxTable) {
   assert(Sym.st_shndx == ELF::SHN_XINDEX);
-  unsigned Index = &Sym - &FirstSym;
-  if (Index >= ShndxTable.size())
+  if (SymIndex >= ShndxTable.size())
     return createError(
-        "extended symbol index (" + Twine(Index) +
+        "extended symbol index (" + Twine(SymIndex) +
         ") is past the end of the SHT_SYMTAB_SHNDX section of size " +
         Twine(ShndxTable.size()));
 
   // The size of the table was checked in getSHNDXTable.
-  return ShndxTable[Index];
+  return ShndxTable[SymIndex];
 }
 
 template <class ELFT>
@@ -359,7 +357,7 @@ ELFFile<ELFT>::getSectionIndex(const Elf_Sym &Sym, Elf_Sym_Range Syms,
   uint32_t Index = Sym.st_shndx;
   if (Index == ELF::SHN_XINDEX) {
     Expected<uint32_t> ErrorOrIndex =
-        getExtendedSymbolTableIndex<ELFT>(Sym, *Syms.begin(), ShndxTable);
+        getExtendedSymbolTableIndex<ELFT>(Sym, &Sym - Syms.begin(), ShndxTable);
     if (!ErrorOrIndex)
       return ErrorOrIndex.takeError();
     return *ErrorOrIndex;

diff  --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp
index f9da512e3a91..2b256e9219b8 100644
--- a/llvm/tools/llvm-readobj/ELFDumper.cpp
+++ b/llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -359,7 +359,7 @@ template <typename ELFT> class ELFDumper : public ObjDumper {
                                 Optional<StringRef> StrTable,
                                 bool IsDynamic) const;
   Expected<unsigned> getSymbolSectionIndex(const Elf_Sym *Symbol,
-                                           const Elf_Sym *FirstSym) const;
+                                           unsigned SymIndex) const;
   Expected<StringRef> getSymbolSectionName(const Elf_Sym *Symbol,
                                            unsigned SectionIndex) const;
   std::string getStaticSymbolName(uint32_t Index) const;
@@ -711,7 +711,7 @@ void ELFDumper<ELFT>::printSymbolsHelper(bool IsDynamic) const {
 
   ELFDumperStyle->printSymtabMessage(SymtabSec, Entries, NonVisibilityBitsUsed);
   for (const auto &Sym : Syms)
-    ELFDumperStyle->printSymbol(&Sym, Syms.begin(), StrTable, IsDynamic,
+    ELFDumperStyle->printSymbol(&Sym, &Sym - Syms.begin(), StrTable, IsDynamic,
                                 NonVisibilityBitsUsed);
 }
 
@@ -740,7 +740,7 @@ template <typename ELFT> class DumpStyle {
   virtual void printDynamicRelocations() = 0;
   virtual void printSymtabMessage(const Elf_Shdr *Symtab, size_t Offset,
                                   bool NonVisibilityBitsUsed) {}
-  virtual void printSymbol(const Elf_Sym *Symbol, const Elf_Sym *FirstSym,
+  virtual void printSymbol(const Elf_Sym *Symbol, unsigned SymIndex,
                            Optional<StringRef> StrTable, bool IsDynamic,
                            bool NonVisibilityBitsUsed) = 0;
   virtual void printProgramHeaders(bool PrintProgramHeaders,
@@ -891,7 +891,7 @@ template <typename ELFT> class GNUStyle : public DumpStyle<ELFT> {
     OS.flush();
     return OS;
   }
-  void printHashedSymbol(const Elf_Sym *FirstSym, uint32_t Sym,
+  void printHashedSymbol(const Elf_Sym *Sym, unsigned SymIndex,
                          StringRef StrTable, uint32_t Bucket);
   void printReloc(const Relocation<ELFT> &R, unsigned RelIndex,
                   const Elf_Shdr &Sec, const Elf_Shdr *SymTab) override;
@@ -899,15 +899,14 @@ template <typename ELFT> class GNUStyle : public DumpStyle<ELFT> {
 
   void printRelRelaReloc(const Relocation<ELFT> &R,
                          const RelSymbol<ELFT> &RelSym);
-  void printSymbol(const Elf_Sym *Symbol, const Elf_Sym *First,
+  void printSymbol(const Elf_Sym *Symbol, unsigned SymIndex,
                    Optional<StringRef> StrTable, bool IsDynamic,
                    bool NonVisibilityBitsUsed) override;
   void printDynamicRelocHeader(unsigned Type, StringRef Name,
                                const DynRegionInfo &Reg) override;
   void printDynamicReloc(const Relocation<ELFT> &R) override;
 
-  std::string getSymbolSectionNdx(const Elf_Sym *Symbol,
-                                  const Elf_Sym *FirstSym);
+  std::string getSymbolSectionNdx(const Elf_Sym *Symbol, unsigned SymIndex);
   void printProgramHeaders();
   void printSectionMapping();
   void printGNUVersionSectionProlog(const typename ELFT::Shdr *Sec,
@@ -967,8 +966,8 @@ template <typename ELFT> class LLVMStyle : public DumpStyle<ELFT> {
   void printRelRelaReloc(const Relocation<ELFT> &R, StringRef SymbolName);
   void printSymbols();
   void printDynamicSymbols();
-  void printSymbolSection(const Elf_Sym *Symbol, const Elf_Sym *First);
-  void printSymbol(const Elf_Sym *Symbol, const Elf_Sym *First,
+  void printSymbolSection(const Elf_Sym *Symbol, unsigned SymIndex);
+  void printSymbol(const Elf_Sym *Symbol, unsigned SymIndex,
                    Optional<StringRef> StrTable, bool IsDynamic,
                    bool /*NonVisibilityBitsUsed*/) override;
   void printProgramHeaders();
@@ -1191,7 +1190,7 @@ std::string ELFDumper<ELFT>::getFullSymbolName(const Elf_Sym *Symbol,
     Elf_Sym_Range Syms = unwrapOrError(
         ObjF->getFileName(), ObjF->getELFFile()->symbols(DotSymtabSec));
     Expected<unsigned> SectionIndex =
-        getSymbolSectionIndex(Symbol, Syms.begin());
+        getSymbolSectionIndex(Symbol, Symbol - Syms.begin());
     if (!SectionIndex) {
       reportUniqueWarning(SectionIndex.takeError());
       return "<?>";
@@ -1224,10 +1223,10 @@ std::string ELFDumper<ELFT>::getFullSymbolName(const Elf_Sym *Symbol,
 template <typename ELFT>
 Expected<unsigned>
 ELFDumper<ELFT>::getSymbolSectionIndex(const Elf_Sym *Symbol,
-                                       const Elf_Sym *FirstSym) const {
+                                       unsigned SymIndex) const {
   unsigned Ndx = Symbol->st_shndx;
   if (Ndx == SHN_XINDEX)
-    return object::getExtendedSymbolTableIndex<ELFT>(*Symbol, *FirstSym,
+    return object::getExtendedSymbolTableIndex<ELFT>(*Symbol, SymIndex,
                                                      ShndxTable);
   if (Ndx != SHN_UNDEF && Ndx < SHN_LORESERVE)
     return Ndx;
@@ -3925,7 +3924,7 @@ void GNUStyle<ELFT>::printSymtabMessage(const Elf_Shdr *Symtab, size_t Entries,
 
 template <class ELFT>
 std::string GNUStyle<ELFT>::getSymbolSectionNdx(const Elf_Sym *Symbol,
-                                                const Elf_Sym *FirstSym) {
+                                                unsigned SymIndex) {
   unsigned SectionIndex = Symbol->st_shndx;
   switch (SectionIndex) {
   case ELF::SHN_UNDEF:
@@ -3936,7 +3935,7 @@ std::string GNUStyle<ELFT>::getSymbolSectionNdx(const Elf_Sym *Symbol,
     return "COM";
   case ELF::SHN_XINDEX: {
     Expected<uint32_t> IndexOrErr = object::getExtendedSymbolTableIndex<ELFT>(
-        *Symbol, *FirstSym, this->dumper()->getShndxTable());
+        *Symbol, SymIndex, this->dumper()->getShndxTable());
     if (!IndexOrErr) {
       assert(Symbol->st_shndx == SHN_XINDEX &&
              "getExtendedSymbolTableIndex should only fail due to an invalid "
@@ -3967,13 +3966,13 @@ std::string GNUStyle<ELFT>::getSymbolSectionNdx(const Elf_Sym *Symbol,
 }
 
 template <class ELFT>
-void GNUStyle<ELFT>::printSymbol(const Elf_Sym *Symbol, const Elf_Sym *FirstSym,
+void GNUStyle<ELFT>::printSymbol(const Elf_Sym *Symbol, unsigned SymIndex,
                                  Optional<StringRef> StrTable, bool IsDynamic,
                                  bool NonVisibilityBitsUsed) {
   unsigned Bias = ELFT::Is64Bits ? 8 : 0;
   Field Fields[8] = {0,         8,         17 + Bias, 23 + Bias,
                      31 + Bias, 38 + Bias, 48 + Bias, 51 + Bias};
-  Fields[0].Str = to_string(format_decimal(Symbol - FirstSym, 6)) + ":";
+  Fields[0].Str = to_string(format_decimal(SymIndex, 6)) + ":";
   Fields[1].Str = to_string(
       format_hex_no_prefix(Symbol->st_value, ELFT::Is64Bits ? 16 : 8));
   Fields[2].Str = to_string(format_decimal(Symbol->st_size, 5));
@@ -3994,7 +3993,7 @@ void GNUStyle<ELFT>::printSymbol(const Elf_Sym *Symbol, const Elf_Sym *FirstSym,
         " [<other: " + to_string(format_hex(Symbol->st_other, 2)) + ">]";
 
   Fields[6].Column += NonVisibilityBitsUsed ? 13 : 0;
-  Fields[6].Str = getSymbolSectionNdx(Symbol, FirstSym);
+  Fields[6].Str = getSymbolSectionNdx(Symbol, SymIndex);
 
   Fields[7].Str =
       this->dumper()->getFullSymbolName(Symbol, StrTable, IsDynamic);
@@ -4004,15 +4003,14 @@ void GNUStyle<ELFT>::printSymbol(const Elf_Sym *Symbol, const Elf_Sym *FirstSym,
 }
 
 template <class ELFT>
-void GNUStyle<ELFT>::printHashedSymbol(const Elf_Sym *FirstSym, uint32_t Sym,
+void GNUStyle<ELFT>::printHashedSymbol(const Elf_Sym *Symbol, unsigned SymIndex,
                                        StringRef StrTable, uint32_t Bucket) {
   unsigned Bias = ELFT::Is64Bits ? 8 : 0;
   Field Fields[9] = {0,         6,         11,        20 + Bias, 25 + Bias,
                      34 + Bias, 41 + Bias, 49 + Bias, 53 + Bias};
-  Fields[0].Str = to_string(format_decimal(Sym, 5));
+  Fields[0].Str = to_string(format_decimal(SymIndex, 5));
   Fields[1].Str = to_string(format_decimal(Bucket, 3)) + ":";
 
-  const auto Symbol = FirstSym + Sym;
   Fields[2].Str = to_string(
       format_hex_no_prefix(Symbol->st_value, ELFT::Is64Bits ? 16 : 8));
   Fields[3].Str = to_string(format_decimal(Symbol->st_size, 5));
@@ -4028,7 +4026,7 @@ void GNUStyle<ELFT>::printHashedSymbol(const Elf_Sym *FirstSym, uint32_t Sym,
       printEnum(Symbol->getBinding(), makeArrayRef(ElfSymbolBindings));
   Fields[6].Str =
       printEnum(Symbol->getVisibility(), makeArrayRef(ElfSymbolVisibilities));
-  Fields[7].Str = getSymbolSectionNdx(Symbol, FirstSym);
+  Fields[7].Str = getSymbolSectionNdx(Symbol, SymIndex);
   Fields[8].Str = this->dumper()->getFullSymbolName(Symbol, StrTable, true);
 
   for (auto &Entry : Fields)
@@ -4087,7 +4085,7 @@ void GNUStyle<ELFT>::printHashTableSymbols(const Elf_Hash &SysVHash) {
         break;
       }
 
-      printHashedSymbol(FirstSym, Ch, StringTable, Buc);
+      printHashedSymbol(FirstSym + Ch, Ch, StringTable, Buc);
       Visited[Ch] = true;
     }
   }
@@ -4118,7 +4116,8 @@ void GNUStyle<ELFT>::printGnuHashTableSymbols(const Elf_GnuHash &GnuHash) {
     uint32_t GnuHashable = Index - GnuHash.symndx;
     // Print whole chain
     while (true) {
-      printHashedSymbol(FirstSym, Index++, StringTable, Buc);
+      uint32_t SymIndex = Index++;
+      printHashedSymbol(FirstSym + SymIndex, SymIndex, StringTable, Buc);
       // Chain ends at symbol with stopper bit
       if ((GnuHash.values(DynSyms.size())[GnuHashable++] & 1) == 1)
         break;
@@ -5854,7 +5853,8 @@ void GNUStyle<ELFT>::printMipsGOT(const MipsGOTParser<ELFT> &Parser) {
       OS.PadToColumn(40 + 3 * Bias);
       OS << printEnum(Sym->getType(), makeArrayRef(ElfSymbolTypes));
       OS.PadToColumn(48 + 3 * Bias);
-      OS << getSymbolSectionNdx(Sym, this->dumper()->dynamic_symbols().begin());
+      OS << getSymbolSectionNdx(
+          Sym, Sym - this->dumper()->dynamic_symbols().begin());
       OS.PadToColumn(52 + 3 * Bias);
       OS << SymName << "\n";
     }
@@ -5903,7 +5903,8 @@ void GNUStyle<ELFT>::printMipsPLT(const MipsGOTParser<ELFT> &Parser) {
       OS.PadToColumn(29 + 3 * Bias);
       OS << printEnum(Sym->getType(), makeArrayRef(ElfSymbolTypes));
       OS.PadToColumn(37 + 3 * Bias);
-      OS << getSymbolSectionNdx(Sym, this->dumper()->dynamic_symbols().begin());
+      OS << getSymbolSectionNdx(
+          Sym, Sym - this->dumper()->dynamic_symbols().begin());
       OS.PadToColumn(41 + 3 * Bias);
       OS << SymName << "\n";
     }
@@ -6146,17 +6147,15 @@ template <class ELFT> void LLVMStyle<ELFT>::printSectionHeaders() {
         StringRef StrTable = unwrapOrError(
             this->FileName, this->Obj.getStringTableForSymtab(*Symtab));
 
-        for (const Elf_Sym &Sym :
-             unwrapOrError(this->FileName, this->Obj.symbols(Symtab))) {
+        typename ELFT::SymRange Symbols =
+            unwrapOrError(this->FileName, this->Obj.symbols(Symtab));
+        for (const Elf_Sym &Sym : Symbols) {
           const Elf_Shdr *SymSec =
               unwrapOrError(this->FileName,
                             this->Obj.getSection(
                                 Sym, Symtab, this->dumper()->getShndxTable()));
           if (SymSec == &Sec)
-            printSymbol(&Sym,
-                        unwrapOrError(this->FileName, this->Obj.symbols(Symtab))
-                            .begin(),
-                        StrTable, false, false);
+            printSymbol(&Sym, &Sym - &Symbols[0], StrTable, false, false);
         }
       }
     }
@@ -6173,7 +6172,7 @@ template <class ELFT> void LLVMStyle<ELFT>::printSectionHeaders() {
 
 template <class ELFT>
 void LLVMStyle<ELFT>::printSymbolSection(const Elf_Sym *Symbol,
-                                         const Elf_Sym *First) {
+                                         unsigned SymIndex) {
   auto GetSectionSpecialType = [&]() -> Optional<StringRef> {
     if (Symbol->isUndefined())
       return StringRef("Undefined");
@@ -6196,7 +6195,7 @@ void LLVMStyle<ELFT>::printSymbolSection(const Elf_Sym *Symbol,
   }
 
   Expected<unsigned> SectionIndex =
-      this->dumper()->getSymbolSectionIndex(Symbol, First);
+      this->dumper()->getSymbolSectionIndex(Symbol, SymIndex);
   if (!SectionIndex) {
     assert(Symbol->st_shndx == SHN_XINDEX &&
            "getSymbolSectionIndex should only fail due to an invalid "
@@ -6222,7 +6221,7 @@ void LLVMStyle<ELFT>::printSymbolSection(const Elf_Sym *Symbol,
 }
 
 template <class ELFT>
-void LLVMStyle<ELFT>::printSymbol(const Elf_Sym *Symbol, const Elf_Sym *First,
+void LLVMStyle<ELFT>::printSymbol(const Elf_Sym *Symbol, unsigned SymIndex,
                                   Optional<StringRef> StrTable, bool IsDynamic,
                                   bool /*NonVisibilityBitsUsed*/) {
   std::string FullSymbolName =
@@ -6261,7 +6260,7 @@ void LLVMStyle<ELFT>::printSymbol(const Elf_Sym *Symbol, const Elf_Sym *First,
     }
     W.printFlags("Other", Symbol->st_other, makeArrayRef(SymOtherFlags), 0x3u);
   }
-  printSymbolSection(Symbol, First);
+  printSymbolSection(Symbol, SymIndex);
 }
 
 template <class ELFT>
@@ -6765,7 +6764,7 @@ void LLVMStyle<ELFT>::printMipsGOT(const MipsGOTParser<ELFT> &Parser) {
       const Elf_Sym *Sym = Parser.getGotSym(&E);
       W.printHex("Value", Sym->st_value);
       W.printEnum("Type", Sym->getType(), makeArrayRef(ElfSymbolTypes));
-      printSymbolSection(Sym, this->dumper()->dynamic_symbols().begin());
+      printSymbolSection(Sym, Sym - this->dumper()->dynamic_symbols().begin());
 
       std::string SymName = this->dumper()->getFullSymbolName(
           Sym, this->dumper()->getDynamicStringTable(), true);
@@ -6809,7 +6808,7 @@ void LLVMStyle<ELFT>::printMipsPLT(const MipsGOTParser<ELFT> &Parser) {
       const Elf_Sym *Sym = Parser.getPltSym(&E);
       W.printHex("Value", Sym->st_value);
       W.printEnum("Type", Sym->getType(), makeArrayRef(ElfSymbolTypes));
-      printSymbolSection(Sym, this->dumper()->dynamic_symbols().begin());
+      printSymbolSection(Sym, Sym - this->dumper()->dynamic_symbols().begin());
 
       std::string SymName =
           this->dumper()->getFullSymbolName(Sym, Parser.getPltStrTable(), true);


        


More information about the llvm-commits mailing list