LibELF: Avoid doing strlen() on everything while iterating GNU hash

It's a lot faster to iterate the GNU hash tables if we don't have to
compute the length of every symbol name before rejecting it anyway while
comparing the first character. :^)
This commit is contained in:
Andreas Kling 2021-02-23 19:00:41 +01:00
parent 46a94a9a9e
commit 22b8110554
2 changed files with 9 additions and 5 deletions

View file

@ -310,13 +310,10 @@ auto DynamicObject::HashSection::lookup_gnu_symbol(const StringView& name, u32 h
for (hash1 &= ~1;; ++current_sym) {
hash2 = *(current_chain++);
auto symbol = m_dynamic.symbol(current_sym);
if ((hash1 == (hash2 & ~1)) && name == symbol.name()) {
dbgln_if(DYNAMIC_LOAD_DEBUG, "Returning GNU dynamic symbol with index {} for {}: {}", current_sym, symbol.name(), symbol.address().as_ptr());
if ((hash1 == (hash2 & ~1)) && name == symbol.raw_name())
return symbol;
}
if (hash2 & 1) {
if (hash2 & 1)
break;
}
}
return {};
@ -327,6 +324,11 @@ StringView DynamicObject::symbol_string_table_string(Elf32_Word index) const
return StringView { (const char*)base_address().offset(m_string_table_offset + index).as_ptr() };
}
const char* DynamicObject::raw_symbol_string_table_string(Elf32_Word index) const
{
return (const char*)base_address().offset(m_string_table_offset + index).as_ptr();
}
DynamicObject::InitializationFunction DynamicObject::init_section_function() const
{
ASSERT(has_init_section());

View file

@ -75,6 +75,7 @@ public:
}
StringView name() const { return m_dynamic.symbol_string_table_string(m_sym.st_name); }
const char* raw_name() const { return m_dynamic.raw_symbol_string_table_string(m_sym.st_name); }
unsigned section_index() const { return m_sym.st_shndx; }
unsigned value() const { return m_sym.st_value; }
unsigned size() const { return m_sym.st_size; }
@ -264,6 +265,7 @@ private:
explicit DynamicObject(VirtualAddress base_address, VirtualAddress dynamic_section_address);
StringView symbol_string_table_string(Elf32_Word) const;
const char* raw_symbol_string_table_string(Elf32_Word) const;
void parse();
template<typename F>