diff --git a/llvm/lib/InterfaceStub/ELFObjHandler.cpp b/llvm/lib/InterfaceStub/ELFObjHandler.cpp --- a/llvm/lib/InterfaceStub/ELFObjHandler.cpp +++ b/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 -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(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 -static Expected getNumSyms(DynamicEntries &Dyn, - const ELFFile &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 TablePtr = ElfFile.toMappedAddr(*Dyn.GnuHash); - if (!TablePtr) - return TablePtr.takeError(); - const Elf_GnuHash *Table = - reinterpret_cast(TablePtr.get()); - return getDynSymtabSize(*Table); - } - // Search SYSV hash table to try to find the upper bound of dynsym. - if (Dyn.ElfHash.hasValue()) { - Expected TablePtr = ElfFile.toMappedAddr(*Dyn.ElfHash); - if (!TablePtr) - return TablePtr.takeError(); - const Elf_Hash *Table = reinterpret_cast(TablePtr.get()); - return Table->nchain; +static Expected getNumSyms(const ELFFile &ElfFile) { + using Elf_Shdr_Range = typename ELFT::ShdrRange; + using Elf_Shdr = typename ELFT::Shdr; + Expected 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 SymCount = getNumSyms(DynEnt, ElfFile); + Expected SymCount = getNumSyms(ElfFile); if (!SymCount) return SymCount.takeError(); if (*SymCount > 0) {