mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 04:47:35 +00:00
LibELF: Move ELF hash functions to their own file (and make constexpr)
This commit is contained in:
parent
145923bdc9
commit
37420f1baf
3 changed files with 67 additions and 35 deletions
|
@ -30,6 +30,7 @@
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <LibELF/DynamicLoader.h>
|
#include <LibELF/DynamicLoader.h>
|
||||||
#include <LibELF/DynamicObject.h>
|
#include <LibELF/DynamicObject.h>
|
||||||
|
#include <LibELF/Hashes.h>
|
||||||
#include <LibELF/exec_elf.h>
|
#include <LibELF/exec_elf.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
@ -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"));
|
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<Symbol>
|
auto DynamicObject::HashSection::lookup_symbol(const StringView& name) const -> Optional<Symbol>
|
||||||
{
|
{
|
||||||
return (this->*(m_lookup_function))(name);
|
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<Symbol>
|
auto DynamicObject::HashSection::lookup_elf_symbol(const StringView& name) const -> Optional<Symbol>
|
||||||
{
|
{
|
||||||
u32 hash_value = calculate_elf_hash(name);
|
u32 hash_value = compute_sysv_hash(name);
|
||||||
u32* hash_table_begin = (u32*)address().as_ptr();
|
u32* hash_table_begin = (u32*)address().as_ptr();
|
||||||
size_t num_buckets = hash_table_begin[0];
|
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 buckets = &bloom_words[num_maskwords];
|
||||||
const u32* const chains = &buckets[num_buckets];
|
const u32* const chains = &buckets[num_buckets];
|
||||||
|
|
||||||
BloomWord hash1 = calculate_gnu_hash(name);
|
BloomWord hash1 = compute_gnu_hash(name);
|
||||||
BloomWord hash2 = hash1 >> shift2;
|
BloomWord hash2 = hash1 >> shift2;
|
||||||
const BloomWord bitmask = (1 << (hash1 % bloom_word_size)) | (1 << (hash2 % bloom_word_size));
|
const BloomWord bitmask = (1 << (hash1 % bloom_word_size)) | (1 << (hash2 % bloom_word_size));
|
||||||
|
|
||||||
|
|
|
@ -200,9 +200,6 @@ public:
|
||||||
Optional<Symbol> lookup_symbol(const StringView& name) const;
|
Optional<Symbol> lookup_symbol(const StringView& name) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static u32 calculate_elf_hash(const StringView& name);
|
|
||||||
static u32 calculate_gnu_hash(const StringView& name);
|
|
||||||
|
|
||||||
Optional<Symbol> lookup_elf_symbol(const StringView& name) const;
|
Optional<Symbol> lookup_elf_symbol(const StringView& name) const;
|
||||||
Optional<Symbol> lookup_gnu_symbol(const StringView& name) const;
|
Optional<Symbol> lookup_gnu_symbol(const StringView& name) const;
|
||||||
|
|
||||||
|
|
64
Userland/Libraries/LibELF/Hashes.h
Normal file
64
Userland/Libraries/LibELF/Hashes.h
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2019-2020, Andrew Kaster <andrewdkaster@gmail.com>
|
||||||
|
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
||||||
|
* 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 <AK/StringView.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue