| 55 | |
| 56 | |
| 57 | [[nodiscard]] |
| 58 | static bool findLocalSymByGnuHash64(const ElfView::ElfInfo &info, const char *symbol, |
| 59 | const Elf64_Sym **ppsym, uint32_t *psymidx) { |
| 60 | using bloom_el_t = uint64_t; |
| 61 | constexpr int ELFCLASS_BITS = 64; |
| 62 | const uint32_t *hashtab = static_cast<const uint32_t *>(info.gnu_hash); |
| 63 | const uint32_t symbol_hash = elf_gnu_hash(symbol); |
| 64 | const uint32_t nbuckets = hashtab[0]; |
| 65 | const uint32_t symoffset = hashtab[1]; |
| 66 | const uint32_t bloom_size = hashtab[2]; |
| 67 | const uint32_t bloom_shift = hashtab[3]; |
| 68 | const bloom_el_t *bloom = reinterpret_cast<const bloom_el_t *>(&hashtab[4]); |
| 69 | const uint32_t *buckets = reinterpret_cast<const uint32_t *>(&bloom[bloom_size]); |
| 70 | const uint32_t *chain = &buckets[nbuckets]; |
| 71 | bloom_el_t word = bloom[(symbol_hash / ELFCLASS_BITS) % bloom_size]; |
| 72 | bloom_el_t mask = (bloom_el_t) 1 << (symbol_hash % ELFCLASS_BITS) |
| 73 | | (bloom_el_t) 1 << ((symbol_hash >> bloom_shift) % ELFCLASS_BITS); |
| 74 | /* If at least one bit is not set, a symbol is surely missing. */ |
| 75 | if ((word & mask) != mask) { |
| 76 | return false; |
| 77 | } |
| 78 | uint32_t symix = buckets[symbol_hash % nbuckets]; |
| 79 | if (symix < symoffset) { |
| 80 | return false; |
| 81 | } |
| 82 | const auto *symtab = static_cast<const Elf64_Sym *>(info.symtab); |
| 83 | /* Loop through the chain. */ |
| 84 | while (true) { |
| 85 | const char *symname = info.symstr + symtab[symix].st_name; |
| 86 | const uint32_t hash = chain[symix - symoffset]; |
| 87 | if ((symbol_hash | 1) == (hash | 1) && strcmp(symbol, symname) == 0) { |
| 88 | const Elf64_Sym &sym = symtab[symix]; |
| 89 | // found. |
| 90 | *psymidx = symix; |
| 91 | *ppsym = &sym; |
| 92 | return true; |
| 93 | } |
| 94 | /* Chain ends with an element with the lowest bit set to 1. */ |
| 95 | if (hash & 1) { |
| 96 | break; |
| 97 | } |
| 98 | symix++; |
| 99 | } |
| 100 | // not found |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | [[nodiscard]] |
| 105 | static bool findLocalSymByGnuHash32(const ElfView::ElfInfo &info, const char *symbol, |
no test coverage detected