[llvm] e8299a8 - [llvm-readobj] - Split the printGnuHashTable(). NFCI.

Georgii Rymar via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 17 04:14:14 PDT 2020


Author: Georgii Rymar
Date: 2020-06-17T14:13:51+03:00
New Revision: e8299a806ad197d64a627c1868b842256f464035

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

LOG: [llvm-readobj] - Split the printGnuHashTable(). NFCI.

`printGnuHashTable` contains the code to check the GNU hash table.
This patch splits it to `getGnuHashTableChains` helper
(and reorders slightly to reduce).

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

Added: 
    

Modified: 
    llvm/tools/llvm-readobj/ELFDumper.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp
index fa4aaa02c526..6b3c92df9b91 100644
--- a/llvm/tools/llvm-readobj/ELFDumper.cpp
+++ b/llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -2716,6 +2716,35 @@ template <typename ELFT> void ELFDumper<ELFT>::printHashTable() {
   W.printList("Chains", HashTable->chains());
 }
 
+template <class ELFT>
+static Expected<ArrayRef<typename ELFT::Word>>
+getGnuHashTableChains(const typename ELFT::SymRange DynSymTable,
+                      const typename ELFT::GnuHash *GnuHashTable) {
+  size_t NumSyms = DynSymTable.size();
+  if (!NumSyms)
+    return createError("unable to dump 'Values' for the SHT_GNU_HASH "
+                       "section: the dynamic symbol table is empty");
+
+  if (GnuHashTable->symndx < NumSyms)
+    return GnuHashTable->values(NumSyms);
+
+  // A normal empty GNU hash table section produced by linker might have
+  // symndx set to the number of dynamic symbols + 1 (for the zero symbol)
+  // and have dummy null values in the Bloom filter and in the buckets
+  // vector. It happens because the value of symndx is not important for
+  // dynamic loaders when the GNU hash table is empty. They just skip the
+  // whole object during symbol lookup. In such cases, the symndx value is
+  // irrelevant and we should not report a warning.
+  ArrayRef<typename ELFT::Word> Buckets = GnuHashTable->buckets();
+  if (!llvm::all_of(Buckets, [](typename ELFT::Word V) { return V == 0; }))
+    return createError("the first hashed symbol index (" +
+                       Twine(GnuHashTable->symndx) +
+                       ") is larger than the number of dynamic symbols (" +
+                       Twine(NumSyms) + ")");
+
+  return GnuHashTable->values(NumSyms);
+}
+
 template <typename ELFT>
 void ELFDumper<ELFT>::printGnuHashTable(const object::ObjectFile *Obj) {
   DictScope D(W, "GnuHashTable");
@@ -2750,37 +2779,14 @@ void ELFDumper<ELFT>::printGnuHashTable(const object::ObjectFile *Obj) {
     return;
   }
 
-  size_t NumSyms = dynamic_symbols().size();
-  if (!NumSyms) {
-    reportWarning(createError("unable to dump 'Values' for the SHT_GNU_HASH "
-                              "section: the dynamic symbol table is empty"),
-                  ObjF->getFileName());
+  Expected<ArrayRef<Elf_Word>> Chains =
+      getGnuHashTableChains<ELFT>(dynamic_symbols(), GnuHashTable);
+  if (!Chains) {
+    reportUniqueWarning(Chains.takeError());
     return;
   }
 
-  if (GnuHashTable->symndx >= NumSyms) {
-    // A normal empty GNU hash table section produced by linker might have
-    // symndx set to the number of dynamic symbols + 1 (for the zero symbol)
-    // and have dummy null values in the Bloom filter and in the buckets
-    // vector. It happens because the value of symndx is not important for
-    // dynamic loaders when the GNU hash table is empty. They just skip the
-    // whole object during symbol lookup. In such cases, the symndx value is
-    // irrelevant and we should not report a warning.
-    bool IsEmptyHashTable =
-        llvm::all_of(Buckets, [](Elf_Word V) { return V == 0; });
-
-    if (!IsEmptyHashTable) {
-      reportWarning(
-          createError("the first hashed symbol index (" +
-                      Twine(GnuHashTable->symndx) +
-                      ") is larger than the number of dynamic symbols (" +
-                      Twine(NumSyms) + ")"),
-          ObjF->getFileName());
-      return;
-    }
-  }
-
-  W.printHexList("Values", GnuHashTable->values(NumSyms));
+  W.printHexList("Values", *Chains);
 }
 
 template <typename ELFT> void ELFDumper<ELFT>::printLoadName() {


        


More information about the llvm-commits mailing list