[PATCH] D93362: [llvm-elfabi] Support ELF file that lacks .gnu_hash section

Haowei Wu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 15 20:33:01 PST 2020


haowei created this revision.
haowei added reviewers: amontanez, mcgrathr, phosek, MaskRay, grimar, jhenderson.
Herald added a subscriber: hiraditya.
haowei requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Before this change, when reading ELF file, elfabi determines number of entries in .dynsym by reading the .gnu_hash section. This change makes elfabi read section headers directly. This change allows elfabi works on ELF files which do not have .gnu_hash sections.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D93362

Files:
  llvm/lib/InterfaceStub/ELFObjHandler.cpp


Index: llvm/lib/InterfaceStub/ELFObjHandler.cpp
===================================================================
--- llvm/lib/InterfaceStub/ELFObjHandler.cpp
+++ llvm/lib/InterfaceStub/ELFObjHandler.cpp
@@ -439,58 +439,19 @@
   return Error::success();
 }
 
-/// This function finds the number of dynamic symbols using a GNU hash table.
-///
-/// @param Table The GNU hash table for .dynsym.
-template <class ELFT>
-static uint64_t getDynSymtabSize(const typename ELFT::GnuHash &Table) {
-  using Elf_Word = typename ELFT::Word;
-  if (Table.nbuckets == 0)
-    return Table.symndx + 1;
-  uint64_t LastSymIdx = 0;
-  uint64_t BucketVal = 0;
-  // Find the index of the first symbol in the last chain.
-  for (Elf_Word Val : Table.buckets()) {
-    BucketVal = std::max(BucketVal, (uint64_t)Val);
-  }
-  LastSymIdx += BucketVal;
-  const Elf_Word *It =
-      reinterpret_cast<const Elf_Word *>(Table.values(BucketVal).end());
-  // Locate the end of the chain to find the last symbol index.
-  while ((*It & 1) == 0) {
-    LastSymIdx++;
-    It++;
-  }
-  return LastSymIdx + 1;
-}
-
 /// This function determines the number of dynamic symbols.
-/// Without access to section headers, the number of symbols must be determined
-/// by parsing dynamic hash tables.
 ///
-/// @param Dyn Entries with the locations of hash tables.
 /// @param ElfFile The ElfFile that the section contents reside in.
 template <class ELFT>
-static Expected<uint64_t> getNumSyms(DynamicEntries &Dyn,
-                                     const ELFFile<ELFT> &ElfFile) {
-  using Elf_Hash = typename ELFT::Hash;
-  using Elf_GnuHash = typename ELFT::GnuHash;
-  // Search GNU hash table to try to find the upper bound of dynsym.
-  if (Dyn.GnuHash.hasValue()) {
-    Expected<const uint8_t *> TablePtr = ElfFile.toMappedAddr(*Dyn.GnuHash);
-    if (!TablePtr)
-      return TablePtr.takeError();
-    const Elf_GnuHash *Table =
-        reinterpret_cast<const Elf_GnuHash *>(TablePtr.get());
-    return getDynSymtabSize<ELFT>(*Table);
-  }
-  // Search SYSV hash table to try to find the upper bound of dynsym.
-  if (Dyn.ElfHash.hasValue()) {
-    Expected<const uint8_t *> TablePtr = ElfFile.toMappedAddr(*Dyn.ElfHash);
-    if (!TablePtr)
-      return TablePtr.takeError();
-    const Elf_Hash *Table = reinterpret_cast<const Elf_Hash *>(TablePtr.get());
-    return Table->nchain;
+static Expected<uint64_t> getNumSyms(const ELFFile<ELFT> &ElfFile) {
+  using Elf_Shdr_Range = typename ELFT::ShdrRange;
+  using Elf_Shdr = typename ELFT::Shdr;
+  Expected<Elf_Shdr_Range> SectionsOrError = ElfFile.sections();
+  if (!SectionsOrError)
+    return SectionsOrError.takeError();
+  for (const Elf_Shdr &Sec : *SectionsOrError) {
+    if (Sec.sh_type == ELF::SHT_DYNSYM)
+      return Sec.sh_size / Sec.sh_entsize;
   }
   return 0;
 }
@@ -636,7 +597,7 @@
   }
 
   // Populate Symbols from .dynsym table and dynamic string table.
-  Expected<uint64_t> SymCount = getNumSyms(DynEnt, ElfFile);
+  Expected<uint64_t> SymCount = getNumSyms(ElfFile);
   if (!SymCount)
     return SymCount.takeError();
   if (*SymCount > 0) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93362.312092.patch
Type: text/x-patch
Size: 3108 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201216/0d7e662f/attachment.bin>


More information about the llvm-commits mailing list