diff --git a/core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp b/core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp index f7744ddfa..9653c8b9f 100644 --- a/core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp +++ b/core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp @@ -55,10 +55,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("