diff --git a/Userland/Libraries/LibELF/DynamicObject.cpp b/Userland/Libraries/LibELF/DynamicObject.cpp index a238f731cf..2d0c48acaf 100644 --- a/Userland/Libraries/LibELF/DynamicObject.cpp +++ b/Userland/Libraries/LibELF/DynamicObject.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -248,36 +249,6 @@ DynamicObject::RelocationSection DynamicObject::plt_relocation_section() const return RelocationSection(Section(*this, m_plt_relocation_offset_location, m_size_of_plt_relocation_entry_list, m_size_of_relocation_entry, "DT_JMPREL")); } -u32 DynamicObject::HashSection::calculate_elf_hash(const StringView& name) -{ - // SYSV ELF hash algorithm - // Note that the GNU HASH algorithm has less collisions - - uint32_t hash = 0; - - for (auto ch : name) { - hash = hash << 4; - hash += ch; - - const uint32_t top_nibble_of_hash = hash & 0xF0000000U; - hash ^= top_nibble_of_hash >> 24; - hash &= ~top_nibble_of_hash; - } - - return hash; -} - -u32 DynamicObject::HashSection::calculate_gnu_hash(const StringView& name) -{ - // GNU ELF hash algorithm - u32 hash = 5381; - - for (auto ch : name) - hash = hash * 33 + ch; - - return hash; -} - auto DynamicObject::HashSection::lookup_symbol(const StringView& name) const -> Optional { return (this->*(m_lookup_function))(name); @@ -285,7 +256,7 @@ auto DynamicObject::HashSection::lookup_symbol(const StringView& name) const -> auto DynamicObject::HashSection::lookup_elf_symbol(const StringView& name) const -> Optional { - u32 hash_value = calculate_elf_hash(name); + u32 hash_value = compute_sysv_hash(name); u32* hash_table_begin = (u32*)address().as_ptr(); size_t num_buckets = hash_table_begin[0]; @@ -328,7 +299,7 @@ auto DynamicObject::HashSection::lookup_gnu_symbol(const StringView& name) const const u32* const buckets = &bloom_words[num_maskwords]; const u32* const chains = &buckets[num_buckets]; - BloomWord hash1 = calculate_gnu_hash(name); + BloomWord hash1 = compute_gnu_hash(name); BloomWord hash2 = hash1 >> shift2; const BloomWord bitmask = (1 << (hash1 % bloom_word_size)) | (1 << (hash2 % bloom_word_size)); diff --git a/Userland/Libraries/LibELF/DynamicObject.h b/Userland/Libraries/LibELF/DynamicObject.h index e65b02652f..dd6d039cfc 100644 --- a/Userland/Libraries/LibELF/DynamicObject.h +++ b/Userland/Libraries/LibELF/DynamicObject.h @@ -200,9 +200,6 @@ public: Optional lookup_symbol(const StringView& name) const; private: - static u32 calculate_elf_hash(const StringView& name); - static u32 calculate_gnu_hash(const StringView& name); - Optional lookup_elf_symbol(const StringView& name) const; Optional lookup_gnu_symbol(const StringView& name) const; diff --git a/Userland/Libraries/LibELF/Hashes.h b/Userland/Libraries/LibELF/Hashes.h new file mode 100644 index 0000000000..9bc06358b5 --- /dev/null +++ b/Userland/Libraries/LibELF/Hashes.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2019-2020, Andrew Kaster + * Copyright (c) 2020, Itamar S. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include + +namespace ELF { + +constexpr u32 compute_sysv_hash(const StringView& name) +{ + // SYSV ELF hash algorithm + // Note that the GNU HASH algorithm has less collisions + + u32 hash = 0; + + for (auto ch : name) { + hash = hash << 4; + hash += ch; + + const u32 top_nibble_of_hash = hash & 0xf0000000u; + hash ^= top_nibble_of_hash >> 24; + hash &= ~top_nibble_of_hash; + } + + return hash; +} + +constexpr u32 compute_gnu_hash(const StringView& name) +{ + // GNU ELF hash algorithm + u32 hash = 5381; + + for (auto ch : name) + hash = hash * 33 + ch; + + return hash; +} + +}