From dfa179f43f5b4acb819e9930723061f07067984f Mon Sep 17 00:00:00 2001 From: raghav314 Date: Thu, 8 Oct 2026 01:02:16 +0530 Subject: [PATCH] fix(relinker): write a bucketed hash table for Linux guest modules The Linux guest module writer emitted DT_HASH with a single bucket, so every symbol lookup that reached a guest module walked its whole symbol table. Guest modules come first in the global scope, so each host prx lookup paid that walk at load and on every lazy PLT call. Build the standard SysV table from the final symbol names with one bucket per symbol, as the console's modules ship it. Co-Authored-By: Claude Opus 5.5 --- .../src/linux/LinuxGuestModuleWriter.cpp | 17 +++++++++++--- .../relinker/tests/test_guest_symbol_names.py | 23 +++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp b/core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp index 46fc14810..feb29a109 100644 --- a/core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp +++ b/core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp @@ -53,10 +53,21 @@ std::vector GuestModuleWriter::WriteLinux(const Relinker::GuestIma bytes.insert(bytes.end(), symbols.begin(), symbols.end()); const auto hashAddress = address(); const auto count = static_cast(image.Symbols.size()); - Io::AppendU32(bytes, 1); + const auto bucketCount = std::max(count, 1); + std::vector buckets(bucketCount), chains(count); + for (std::uint32_t index = count; index-- > 1;) { + std::uint32_t hash = 0; + for (auto offset = Io::ReadU32(symbols, index * 24); offset < strings.size() && strings[offset] != 0; ++offset) { + hash = (hash << 4u) + strings[offset]; + hash = (hash ^ ((hash & 0xf0000000u) >> 24u)) & 0x0fffffffu; + } + chains[index] = buckets[hash % bucketCount]; + buckets[hash % bucketCount] = index; + } + Io::AppendU32(bytes, bucketCount); Io::AppendU32(bytes, count); - Io::AppendU32(bytes, count > 1 ? 1 : 0); - for (std::uint32_t index = 0; index < count; ++index) Io::AppendU32(bytes, index != 0 && index + 1 < count ? index + 1 : 0); + for (const auto bucket : buckets) Io::AppendU32(bytes, bucket); + for (const auto chain : chains) Io::AppendU32(bytes, chain); Io::AlignBuffer(bytes, 8); const auto relaAddress = address(); bytes.insert(bytes.end(), image.Dynamic.RelaData.begin(), image.Dynamic.RelaData.end()); diff --git a/core/relinker/relinker/tests/test_guest_symbol_names.py b/core/relinker/relinker/tests/test_guest_symbol_names.py index 35558254c..a99180830 100644 --- a/core/relinker/relinker/tests/test_guest_symbol_names.py +++ b/core/relinker/relinker/tests/test_guest_symbol_names.py @@ -85,6 +85,27 @@ def module_symbols(image): return [symbol_name(image, tags, index) for index in range(1, count)] +def elf_hash(name): + value = 0 + for byte in name.encode(): + value = (value << 4) + byte + value = (value ^ ((value & 0xF0000000) >> 24)) & 0x0FFFFFFF + return value + + +def check_hash_table(image): + tags = dynamic_tags(image) + table = file_offset(image, tags[4]) + buckets, count = struct.unpack_from("