[llvm] r366464 - [NFC][llvm-readobj] Refactor dynamic string table indexing into a function.

Yuanfang Chen via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 18 10:04:28 PDT 2019


Author: yuanfang
Date: Thu Jul 18 10:04:28 2019
New Revision: 366464

URL: http://llvm.org/viewvc/llvm-project?rev=366464&view=rev
Log:
[NFC][llvm-readobj] Refactor dynamic string table indexing into a function.

Restore printDynamicString removed in rL363868. It provides better
error handling whenever indexing dynamic string table is needed.

Reviewers: jhenderson, MaskRay, grimar

Reviewed by: jhenderson, MaskRay, grimar

Differential Revision: https://reviews.llvm.org/D64674

Modified:
    llvm/trunk/test/tools/llvm-readobj/elf-dynamic-malformed.test
    llvm/trunk/tools/llvm-readobj/ELFDumper.cpp

Modified: llvm/trunk/test/tools/llvm-readobj/elf-dynamic-malformed.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-readobj/elf-dynamic-malformed.test?rev=366464&r1=366463&r2=366464&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-readobj/elf-dynamic-malformed.test (original)
+++ llvm/trunk/test/tools/llvm-readobj/elf-dynamic-malformed.test Thu Jul 18 10:04:28 2019
@@ -98,22 +98,22 @@ ProgramHeaders:
 # RUN: llvm-readelf --dynamic-table %t.bad-string | FileCheck %s --check-prefix BAD-STRING-GNU
 
 # BAD-STRING-LLVM: 0x000000000000000A STRSZ         1 (bytes)
-# BAD-STRING-LLVM: 0x0000000000000001 NEEDED        Shared library: <Invalid offset 0x1>
-# BAD-STRING-LLVM: 0x000000007FFFFFFF FILTER        Filter library: <Invalid offset 0x1>
-# BAD-STRING-LLVM: 0x000000007FFFFFFD AUXILIARY     Auxiliary library: <Invalid offset 0x1>
-# BAD-STRING-LLVM: 0x000000007FFFFFFE USED          Not needed object: <Invalid offset 0x1>
-# BAD-STRING-LLVM: 0x000000000000000E SONAME        Library soname: <Invalid offset 0x1>
-# BAD-STRING-LLVM: 0x000000000000000F RPATH         Library rpath: <Invalid offset 0x1>
-# BAD-STRING-LLVM: 0x000000000000001D RUNPATH       Library runpath: <Invalid offset 0x1>
+# BAD-STRING-LLVM: 0x0000000000000001 NEEDED        Shared library: [<Invalid offset 0x1>]
+# BAD-STRING-LLVM: 0x000000007FFFFFFF FILTER        Filter library: [<Invalid offset 0x1>]
+# BAD-STRING-LLVM: 0x000000007FFFFFFD AUXILIARY     Auxiliary library: [<Invalid offset 0x1>]
+# BAD-STRING-LLVM: 0x000000007FFFFFFE USED          Not needed object: [<Invalid offset 0x1>]
+# BAD-STRING-LLVM: 0x000000000000000E SONAME        Library soname: [<Invalid offset 0x1>]
+# BAD-STRING-LLVM: 0x000000000000000F RPATH         Library rpath: [<Invalid offset 0x1>]
+# BAD-STRING-LLVM: 0x000000000000001D RUNPATH       Library runpath: [<Invalid offset 0x1>]
 
 # BAD-STRING-GNU:  0x000000000000000a (STRSZ)       1 (bytes)
-# BAD-STRING-GNU:  0x0000000000000001 (NEEDED)      Shared library: <Invalid offset 0x1>
-# BAD-STRING-GNU:  0x000000007fffffff (FILTER)      Filter library: <Invalid offset 0x1>
-# BAD-STRING-GNU:  0x000000007ffffffd (AUXILIARY)   Auxiliary library: <Invalid offset 0x1>
-# BAD-STRING-GNU:  0x000000007ffffffe (USED)        Not needed object: <Invalid offset 0x1>
-# BAD-STRING-GNU:  0x000000000000000e (SONAME)      Library soname: <Invalid offset 0x1>
-# BAD-STRING-GNU:  0x000000000000000f (RPATH)       Library rpath: <Invalid offset 0x1>
-# BAD-STRING-GNU:  0x000000000000001d (RUNPATH)     Library runpath: <Invalid offset 0x1>
+# BAD-STRING-GNU:  0x0000000000000001 (NEEDED)      Shared library: [<Invalid offset 0x1>]
+# BAD-STRING-GNU:  0x000000007fffffff (FILTER)      Filter library: [<Invalid offset 0x1>]
+# BAD-STRING-GNU:  0x000000007ffffffd (AUXILIARY)   Auxiliary library: [<Invalid offset 0x1>]
+# BAD-STRING-GNU:  0x000000007ffffffe (USED)        Not needed object: [<Invalid offset 0x1>]
+# BAD-STRING-GNU:  0x000000000000000e (SONAME)      Library soname: [<Invalid offset 0x1>]
+# BAD-STRING-GNU:  0x000000000000000f (RPATH)       Library rpath: [<Invalid offset 0x1>]
+# BAD-STRING-GNU:  0x000000000000001d (RUNPATH)     Library runpath: [<Invalid offset 0x1>]
 
 --- !ELF
 FileHeader:
@@ -169,11 +169,11 @@ ProgramHeaders:
 
 # RUN: llvm-readobj --dynamic-table --needed-libs %t.bad-strtab | FileCheck %s --check-prefixes=BAD-STRTAB,BAD-STRTAB-LLVM
 # RUN: llvm-readelf --dynamic-table --needed-libs %t.bad-strtab | FileCheck %s --check-prefixes=BAD-STRTAB,BAD-STRTAB-GNU
-# BAD-STRTAB-LLVM: LoadName: <Not found>
-# BAD-STRTAB-LLVM: 0x0000000000000001  NEEDED   Shared library: <String table is empty or was not found>
-# BAD-STRTAB-GNU:  0x0000000000000001 (NEEDED)  Shared library: <String table is empty or was not found>
+# BAD-STRTAB-LLVM: LoadName: <String table is empty or was not found>
+# BAD-STRTAB-LLVM: 0x0000000000000001  NEEDED   Shared library: [<String table is empty or was not found>]
+# BAD-STRTAB-GNU:  0x0000000000000001 (NEEDED)  Shared library: [<String table is empty or was not found>]
 # BAD-STRTAB:      NeededLibraries [
-# BAD-STRTAB:        <Library name index out of range>
+# BAD-STRTAB:        <String table is empty or was not found>
 # BAD-STRTAB:      ]
 
 --- !ELF

Modified: llvm/trunk/tools/llvm-readobj/ELFDumper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/ELFDumper.cpp?rev=366464&r1=366463&r2=366464&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/ELFDumper.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/ELFDumper.cpp Thu Jul 18 10:04:28 2019
@@ -226,7 +226,7 @@ private:
   DynRegionInfo DynSymRegion;
   DynRegionInfo DynamicTable;
   StringRef DynamicStringTable;
-  StringRef SOName = "<Not found>";
+  std::string SOName = "<Not found>";
   const Elf_Hash *HashTable = nullptr;
   const Elf_GnuHash *GnuHashTable = nullptr;
   const Elf_Shdr *DotSymtabSec = nullptr;
@@ -292,6 +292,7 @@ public:
                            StringRef &SectionName,
                            unsigned &SectionIndex) const;
   std::string getStaticSymbolName(uint32_t Index) const;
+  std::string getDynamicString(uint64_t Value) const;
   StringRef getSymbolVersionByIndex(StringRef StrTab,
                                     uint32_t VersionSymbolIndex,
                                     bool &IsDefault) const;
@@ -1632,8 +1633,7 @@ template <typename ELFT> void ELFDumper<
   }
   if (StringTableBegin)
     DynamicStringTable = StringRef(StringTableBegin, StringTableSize);
-  if (SONameOffset && SONameOffset < DynamicStringTable.size())
-    SOName = DynamicStringTable.data() + SONameOffset;
+  SOName = getDynamicString(SONameOffset);
 }
 
 template <typename ELFT>
@@ -1953,13 +1953,7 @@ void ELFDumper<ELFT>::printDynamicEntry(
       {DT_RPATH,     "Library rpath"},
       {DT_RUNPATH,   "Library runpath"},
     };
-    OS << TagNames.at(Type) << ": ";
-    if (DynamicStringTable.empty())
-      OS << "<String table is empty or was not found> ";
-    else if (Value < DynamicStringTable.size())
-      OS << "[" << StringRef(DynamicStringTable.data() + Value) << "]";
-    else
-      OS << "<Invalid offset 0x" << utohexstr(Value) << ">";
+    OS << TagNames.at(Type) << ": [" << getDynamicString(Value) << "]";
     break;
   }
   case DT_FLAGS:
@@ -1974,6 +1968,15 @@ void ELFDumper<ELFT>::printDynamicEntry(
   }
 }
 
+template <class ELFT>
+std::string ELFDumper<ELFT>::getDynamicString(uint64_t Value) const {
+  if (DynamicStringTable.empty())
+    return "<String table is empty or was not found>";
+  if (Value < DynamicStringTable.size())
+    return DynamicStringTable.data() + Value;
+  return Twine("<Invalid offset 0x" + utohexstr(Value) + ">").str();
+}
+
 template <class ELFT> void ELFDumper<ELFT>::printUnwindInfo() {
   DwarfCFIEH::PrinterContext<ELFT> Ctx(W, ObjF);
   Ctx.printUnwindInformation();
@@ -2001,17 +2004,10 @@ template <class ELFT> void ELFDumper<ELF
 template <class ELFT> void ELFDumper<ELFT>::printNeededLibraries() {
   ListScope D(W, "NeededLibraries");
 
-  using LibsTy = std::vector<StringRef>;
-  LibsTy Libs;
-
+  std::vector<std::string> Libs;
   for (const auto &Entry : dynamic_table())
-    if (Entry.d_tag == ELF::DT_NEEDED) {
-      uint64_t Value = Entry.d_un.d_val;
-      if (Value < DynamicStringTable.size())
-        Libs.push_back(StringRef(DynamicStringTable.data() + Value));
-      else
-        Libs.push_back("<Library name index out of range>");
-    }
+    if (Entry.d_tag == ELF::DT_NEEDED)
+      Libs.push_back(getDynamicString(Entry.d_un.d_val));
 
   llvm::stable_sort(Libs);
 




More information about the llvm-commits mailing list