[Object][ELF] Support dumping hash-tables from files with no section table.
authorMichael J. Spencer <bigcheesegs@gmail.com>
Thu, 9 Jul 2015 22:32:24 +0000 (22:32 +0000)
committerMichael J. Spencer <bigcheesegs@gmail.com>
Thu, 9 Jul 2015 22:32:24 +0000 (22:32 +0000)
This time without breaking the bots.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241869 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Object/ELF.h
include/llvm/Object/ELFTypes.h
test/Object/Inputs/no-section-table.so [new file with mode: 0644]
test/Object/no-section-table.test [new file with mode: 0644]
tools/llvm-readobj/ELFDumper.cpp
tools/llvm-readobj/ObjDumper.h
tools/llvm-readobj/StreamWriter.h
tools/llvm-readobj/llvm-readobj.cpp

index 3b0c548ffe157b01dd6b656302ba92d26bd18734..068fc32f3a2de5fafd26f0f1903bc9c9974fe921 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/IntervalMap.h"
 #include "llvm/ADT/PointerIntPair.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringSwitch.h"
@@ -139,6 +140,7 @@ public:
   typedef Elf_Verneed_Impl<ELFT> Elf_Verneed;
   typedef Elf_Vernaux_Impl<ELFT> Elf_Vernaux;
   typedef Elf_Versym_Impl<ELFT> Elf_Versym;
+  typedef Elf_Hash<ELFT> Elf_Hash;
   typedef ELFEntityIterator<const Elf_Dyn> Elf_Dyn_Iter;
   typedef iterator_range<Elf_Dyn_Iter> Elf_Dyn_Range;
   typedef ELFEntityIterator<const Elf_Rela> Elf_Rela_Iter;
@@ -176,6 +178,7 @@ private:
   const Elf_Shdr *dot_symtab_sec = nullptr; // Symbol table section.
   StringRef DynSymStrTab;                   // Dynnamic symbol string table.
   const Elf_Shdr *DotDynSymSec = nullptr;   // Dynamic symbol table section.
+  const Elf_Hash *HashTable = nullptr;
 
   const Elf_Shdr *SymbolTableSectionHeaderIndex = nullptr;
   DenseMap<const Elf_Sym *, ELF::Elf64_Word> ExtendedSymbolTable;
@@ -229,6 +232,8 @@ private:
   void LoadVersionNeeds(const Elf_Shdr *ec) const;
   void LoadVersionMap() const;
 
+  void scanDynamicTable();
+
 public:
   template<typename T>
   const T        *getEntry(uint32_t Section, uint32_t Entry) const;
@@ -237,6 +242,7 @@ public:
 
   const Elf_Shdr *getDotSymtabSec() const { return dot_symtab_sec; }
   const Elf_Shdr *getDotDynSymSec() const { return DotDynSymSec; }
+  const Elf_Hash *getHashTable() const { return HashTable; }
 
   ErrorOr<StringRef> getStringTable(const Elf_Shdr *Section) const;
   const char *getDynamicString(uintX_t Offset) const;
@@ -578,8 +584,10 @@ ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
 
   Header = reinterpret_cast<const Elf_Ehdr *>(base());
 
-  if (Header->e_shoff == 0)
+  if (Header->e_shoff == 0) {
+    scanDynamicTable();
     return;
+  }
 
   const uint64_t SectionTableOffset = Header->e_shoff;
 
@@ -604,6 +612,13 @@ ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
 
   for (const Elf_Shdr &Sec : sections()) {
     switch (Sec.sh_type) {
+    case ELF::SHT_HASH:
+      if (HashTable) {
+        EC = object_error::parse_failed;
+        return;
+      }
+      HashTable = reinterpret_cast<const Elf_Hash *>(base() + Sec.sh_offset);
+      break;
     case ELF::SHT_SYMTAB_SHNDX:
       if (SymbolTableSectionHeaderIndex) {
         // More than one .symtab_shndx!
@@ -701,7 +716,21 @@ ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
     }
   }
 
-  // Scan program headers.
+  scanDynamicTable();
+
+  EC = std::error_code();
+}
+
+template <class ELFT>
+void ELFFile<ELFT>::scanDynamicTable() {
+  // Build load-address to file-offset map.
+  typedef IntervalMap<
+      uintX_t, uintptr_t,
+      IntervalMapImpl::NodeSizer<uintX_t, uintptr_t>::LeafSize,
+      IntervalMapHalfOpenInfo<uintX_t>> LoadMapT;
+  typename LoadMapT::Allocator Alloc;
+  LoadMapT LoadMap(Alloc);
+
   for (Elf_Phdr_Iter PhdrI = program_header_begin(),
                      PhdrE = program_header_end();
        PhdrI != PhdrE; ++PhdrI) {
@@ -709,34 +738,36 @@ ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
       DynamicRegion.Addr = base() + PhdrI->p_offset;
       DynamicRegion.Size = PhdrI->p_filesz;
       DynamicRegion.EntSize = sizeof(Elf_Dyn);
-      break;
+      continue;
     }
+    if (PhdrI->p_type != ELF::PT_LOAD)
+      continue;
+    if (PhdrI->p_filesz == 0)
+      continue;
+    LoadMap.insert(PhdrI->p_vaddr, PhdrI->p_vaddr + PhdrI->p_filesz,
+                   PhdrI->p_offset);
   }
 
-  // Scan dynamic table.
+  auto toMappedAddr = [&](uint64_t VAddr) -> const uint8_t * {
+    auto I = LoadMap.find(VAddr);
+    if (I == LoadMap.end())
+      return nullptr;
+    return base() + I.value() + (VAddr - I.start());
+  };
+
   for (Elf_Dyn_Iter DynI = dynamic_table_begin(), DynE = dynamic_table_end();
        DynI != DynE; ++DynI) {
     switch (DynI->d_tag) {
-    case ELF::DT_RELA: {
-      uint64_t VBase = 0;
-      const uint8_t *FBase = nullptr;
-      for (Elf_Phdr_Iter PhdrI = program_header_begin(),
-                         PhdrE = program_header_end();
-           PhdrI != PhdrE; ++PhdrI) {
-        if (PhdrI->p_type != ELF::PT_LOAD)
-          continue;
-        if (DynI->getPtr() >= PhdrI->p_vaddr &&
-            DynI->getPtr() < PhdrI->p_vaddr + PhdrI->p_memsz) {
-          VBase = PhdrI->p_vaddr;
-          FBase = base() + PhdrI->p_offset;
-          break;
-        }
-      }
-      if (!VBase)
-        return;
-      DynRelaRegion.Addr = FBase + DynI->getPtr() - VBase;
+    case ELF::DT_HASH:
+      if (HashTable)
+        continue;
+      HashTable =
+          reinterpret_cast<const Elf_Hash *>(toMappedAddr(DynI->getPtr()));
+      break;
+    case ELF::DT_RELA:
+      if (!DynRelaRegion.Addr)
+        DynRelaRegion.Addr = toMappedAddr(DynI->getPtr());
       break;
-    }
     case ELF::DT_RELASZ:
       DynRelaRegion.Size = DynI->getVal();
       break;
@@ -744,8 +775,6 @@ ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
       DynRelaRegion.EntSize = DynI->getVal();
     }
   }
-
-  EC = std::error_code();
 }
 
 template <class ELFT>
index 63e13909ae5c9481d0dc48a87a5141701b9a2ca9..1e6e20e0013e5932edd6067c4d6ee7c9144cd47b 100644 (file)
@@ -10,6 +10,7 @@
 #ifndef LLVM_OBJECT_ELFTYPES_H
 #define LLVM_OBJECT_ELFTYPES_H
 
+#include "llvm/ADT/ArrayRef.h"
 #include "llvm/Object/Error.h"
 #include "llvm/Support/DataTypes.h"
 #include "llvm/Support/ELF.h"
@@ -463,6 +464,23 @@ struct Elf_Phdr_Impl<ELFType<TargetEndianness, true>> {
   Elf_Xword p_align;  // Segment alignment constraint
 };
 
+// ELFT needed for endianess.
+template <class ELFT>
+struct Elf_Hash {
+  LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
+  Elf_Word nbucket;
+  Elf_Word nchain;
+
+  ArrayRef<Elf_Word> buckets() const {
+    return ArrayRef<Elf_Word>(&nbucket + 2, &nbucket + 2 + nbucket);
+  }
+
+  ArrayRef<Elf_Word> chains() const {
+    return ArrayRef<Elf_Word>(&nbucket + 2 + nbucket,
+                              &nbucket + 2 + nbucket + nchain);
+  }
+};
+
 // MIPS .reginfo section
 template <class ELFT>
 struct Elf_Mips_RegInfo;
diff --git a/test/Object/Inputs/no-section-table.so b/test/Object/Inputs/no-section-table.so
new file mode 100644 (file)
index 0000000..fd176eb
Binary files /dev/null and b/test/Object/Inputs/no-section-table.so differ
diff --git a/test/Object/no-section-table.test b/test/Object/no-section-table.test
new file mode 100644 (file)
index 0000000..1049390
--- /dev/null
@@ -0,0 +1,8 @@
+RUN: llvm-readobj %p/Inputs/no-section-table.so -hash-table | FileCheck %s
+
+CHECK: HashTable {
+CHECK:   Num Buckets: 3
+CHECK:   Num Chains: 13
+CHECK:   Buckets: [12, 10, 11]
+CHECK:   Chains: [0, 0, 0, 0, 2, 3, 4, 0, 7, 5, 6, 8, 9]
+CHECK: }
index 967e8aa55c91de2f087e8fcea95656ffa022cff9..1cdf5529c0809a18e9b6491dd875a97f568e894a 100644 (file)
@@ -56,6 +56,7 @@ public:
   void printDynamicTable() override;
   void printNeededLibraries() override;
   void printProgramHeaders() override;
+  void printHashTable() override;
 
   void printAttributes() override;
   void printMipsPLTGOT() override;
@@ -1119,6 +1120,18 @@ void ELFDumper<ELFT>::printProgramHeaders() {
   }
 }
 
+template <typename ELFT>
+void ELFDumper<ELFT>::printHashTable() {
+  DictScope D(W, "HashTable");
+  auto HT = Obj->getHashTable();
+  if (!HT)
+    return;
+  W.printNumber("Num Buckets", HT->nbucket);
+  W.printNumber("Num Chains", HT->nchain);
+  W.printList("Buckets", HT->buckets());
+  W.printList("Chains", HT->chains());
+}
+
 template <class ELFT>
 void ELFDumper<ELFT>::printAttributes() {
   W.startLine() << "Attributes not implemented.\n";
index 27e15b256cc5776f5d78fc500f28ca647795b0f0..5ecf0ec3d6fa0ab6a0eb3afd64ae87fca11b305d 100644 (file)
@@ -37,6 +37,7 @@ public:
   virtual void printDynamicTable() { }
   virtual void printNeededLibraries() { }
   virtual void printProgramHeaders() { }
+  virtual void printHashTable() { }
 
   // Only implemented for ARM ELF at this time.
   virtual void printAttributes() { }
index 245588ba06006c2b76bfe6b8c4e7cb433edfccae..f3cc57ef940e77b5cf2aa3ee0d08916c80a4b8cc 100644 (file)
@@ -181,8 +181,8 @@ public:
     startLine() << Label << ": " << (Value ? "Yes" : "No") << '\n';
   }
 
-  template <typename T_>
-  void printList(StringRef Label, const SmallVectorImpl<T_> &List) {
+  template <typename T>
+  void printList(StringRef Label, const T &List) {
     startLine() << Label << ": [";
     bool Comma = false;
     for (const auto &Item : List) {
index 990299bc2cefb4a4a5fe5808a724bd998758d309..12afacb0a858e5bce5d7cea19a293f561fbf4ae7 100644 (file)
@@ -127,6 +127,10 @@ namespace opts {
   cl::opt<bool> ProgramHeaders("program-headers",
     cl::desc("Display ELF program headers"));
 
+  // -hash-table
+  cl::opt<bool> HashTable("hash-table",
+    cl::desc("Display ELF hash table"));
+
   // -expand-relocs
   cl::opt<bool> ExpandRelocs("expand-relocs",
     cl::desc("Expand each shown relocation to multiple lines"));
@@ -300,6 +304,8 @@ static void dumpObject(const ObjectFile *Obj) {
     Dumper->printNeededLibraries();
   if (opts::ProgramHeaders)
     Dumper->printProgramHeaders();
+  if (opts::HashTable)
+    Dumper->printHashTable();
   if (Obj->getArch() == llvm::Triple::arm && Obj->isELF())
     if (opts::ARMAttributes)
       Dumper->printAttributes();