feat(relinker): emit DT_GNU_HASH and exclude undefined symbols from DT_HASH #10917

Merged
slugzero merged 1 commit from upstream-pr/10917 into main 2026-11-04 04:48:57 +00:00
slugzero commented 2026-10-25 09:54:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by slugzero on 2026-10-25T09:54:25Z.


What

  • Problem: Previously, LinuxElfPatcher emitted only a single-bucket SysV DT_HASH table (nbucket = 1) that chained every symbol in .dynsym—including all undefined imported symbols (SHN_UNDEF)—sequentially into bucket 0 (1 -> 2 -> ... -> N-1). Because the main executable stands at the head of glibc's global lookup scope (l_searchlist), ld.so walked that entire chain of SHN_UNDEF entries in the main executable whenever a lookup was not rejected by a bloom filter, turning startup symbol resolution across N imports into an O(N^2) linear scan.
  • Change:
    • Add DT_GNU_HASH = 0x6ffffef5 to ElfConstants.hpp.
    • Update LinuxElfPatcher::Patch to emit both a SysV DT_HASH table and a DT_GNU_HASH table (with a 64-bit bloom filter) containing only defined dynamic symbols (st_shndx != SHN_UNDEF).
    • When the executable exports defined symbols, verify that defined symbols are contiguous and not referenced by relocations, sort that defined slice by GnuHash(name) % numDefined, and build both DT_HASH and DT_GNU_HASH from the final post-sort symbol order.
    • When all dynamic symbols in the executable are undefined imports (definedSymbols.empty()), emit an empty 1-bucket DT_HASH (buckets[0] = 0) and an empty DT_GNU_HASH (symoffset = symbolCount, bloom[0] = 0, buckets[0] = 0) so ld.so skips the main executable in O(1) during symbol lookup.
    • Update test_linux_fixed_base.py to expect DT_GNU_HASH alongside DT_HASH, and expand test_linux_executable_exports.py with a 4-export fixture (Ae, Ad, Ac, Ab hashing to distinct ElfHash % 4 and GnuHash % 4 buckets) verified under both normal DT_GNU_HASH execution and DT_HASH fallback (retagging DT_GNU_HASH to 0x6ffffdf8).

Tested

Linux x86_64 (kernel 7.0.0-31-generic, GCC, Ninja, Release). Both arms were built in the same relinker-only build directory (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON -DCMAKE_BUILD_TYPE=Release): main (f06d7fdcee) and this PR (e46c9ef7f6).

  • Synthetic import lookup benchmark (LD_DEBUG=statistics): Extended test_linux_load_alignment.fixture() with N \in \{1024, 2048, 4096\} undefined R_X86_64_GLOB_DAT imports resolved against a generated shared library (cc -shared), converted the fixture with relinker --skip-sce-module on main and on this PR, and ran each output 11 times under LD_DEBUG=statistics:
Undefined imports (SHN_UNDEF) main (f06d7fdcee) relocation cycles (median of 11) This PR (e46c9ef7f6) relocation cycles (median of 11) Relocation change main total loader cycles This PR total loader cycles Total loader change
1,024 3,055,916 29,997 −99.0% (101.9×) 3,483,623 124,929 −96.4%
2,048 5,616,724 42,652 −99.2% (131.7×) 5,930,354 115,476 −98.1%
4,096 23,709,123 102,151 −99.6% (232.1×) 24,234,726 190,275 −99.2%
  • Tests: All 123 relinker tests pass (ctest), including linux_executable_exports (1-export and 4-export Ae, Ad, Ac, Ab fixtures under both DT_GNU_HASH and DT_HASH fallback 0x6ffffdf8), linux_fixed_base, linux_trace_imports, and elf_header.
  • Conventions & overlap: python3 tools/check_conventions.py --base origin/main passes. Verified that #7532 is an unrelated PR and not a dependency, and that git merge-tree --write-tree HEAD refs/pull/7532/head exits 0 (2ada440ae4, clean automatic merge).

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Antigravity)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5003673866 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10917). Originally posted by [slugzero](https://github.com/slugzero) on 2026-10-25T09:54:25Z. --- ### What - **Problem:** Previously, `LinuxElfPatcher` emitted only a single-bucket SysV `DT_HASH` table (`nbucket = 1`) that chained every symbol in `.dynsym`—including all undefined imported symbols (`SHN_UNDEF`)—sequentially into bucket 0 (`1 -> 2 -> ... -> N-1`). Because the main executable stands at the head of glibc's global lookup scope (`l_searchlist`), `ld.so` walked that entire chain of `SHN_UNDEF` entries in the main executable whenever a lookup was not rejected by a bloom filter, turning startup symbol resolution across $N$ imports into an $O(N^2)$ linear scan. - **Change:** - Add `DT_GNU_HASH = 0x6ffffef5` to `ElfConstants.hpp`. - Update `LinuxElfPatcher::Patch` to emit both a SysV `DT_HASH` table and a `DT_GNU_HASH` table (with a 64-bit bloom filter) containing only defined dynamic symbols (`st_shndx != SHN_UNDEF`). - When the executable exports defined symbols, verify that defined symbols are contiguous and not referenced by relocations, sort that defined slice by `GnuHash(name) % numDefined`, and build both `DT_HASH` and `DT_GNU_HASH` from the final post-sort symbol order. - When all dynamic symbols in the executable are undefined imports (`definedSymbols.empty()`), emit an empty 1-bucket `DT_HASH` (`buckets[0] = 0`) and an empty `DT_GNU_HASH` (`symoffset = symbolCount`, `bloom[0] = 0`, `buckets[0] = 0`) so `ld.so` skips the main executable in $O(1)$ during symbol lookup. - Update `test_linux_fixed_base.py` to expect `DT_GNU_HASH` alongside `DT_HASH`, and expand `test_linux_executable_exports.py` with a 4-export fixture (`Ae`, `Ad`, `Ac`, `Ab` hashing to distinct `ElfHash % 4` and `GnuHash % 4` buckets) verified under both normal `DT_GNU_HASH` execution and `DT_HASH` fallback (retagging `DT_GNU_HASH` to `0x6ffffdf8`). ### Tested Linux x86_64 (kernel 7.0.0-31-generic, GCC, Ninja, Release). Both arms were built in the same relinker-only build directory (`-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON -DCMAKE_BUILD_TYPE=Release`): `main` (`f06d7fdcee`) and this PR (`e46c9ef7f6`). - **Synthetic import lookup benchmark (`LD_DEBUG=statistics`):** Extended `test_linux_load_alignment.fixture()` with $N \in \{1024, 2048, 4096\}$ undefined `R_X86_64_GLOB_DAT` imports resolved against a generated shared library (`cc -shared`), converted the fixture with `relinker --skip-sce-module` on `main` and on this PR, and ran each output 11 times under `LD_DEBUG=statistics`: | Undefined imports (`SHN_UNDEF`) | `main` (`f06d7fdcee`) relocation cycles (median of 11) | This PR (`e46c9ef7f6`) relocation cycles (median of 11) | Relocation change | `main` total loader cycles | This PR total loader cycles | Total loader change | |---|---|---|---|---|---|---| | 1,024 | 3,055,916 | 29,997 | −99.0% (101.9×) | 3,483,623 | 124,929 | −96.4% | | 2,048 | 5,616,724 | 42,652 | −99.2% (131.7×) | 5,930,354 | 115,476 | −98.1% | | 4,096 | 23,709,123 | 102,151 | −99.6% (232.1×) | 24,234,726 | 190,275 | −99.2% | - **Tests:** All 123 relinker tests pass (`ctest`), including `linux_executable_exports` (1-export and 4-export `Ae`, `Ad`, `Ac`, `Ab` fixtures under both `DT_GNU_HASH` and `DT_HASH` fallback `0x6ffffdf8`), `linux_fixed_base`, `linux_trace_imports`, and `elf_header`. - **Conventions & overlap:** `python3 tools/check_conventions.py --base origin/main` passes. Verified that #7532 is an unrelated PR and not a dependency, and that `git merge-tree --write-tree HEAD refs/pull/7532/head` exits 0 (`2ada440ae4`, clean automatic merge). ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Antigravity)
MarcZX10 commented 2026-10-25 10:02:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T10:02:18Z.

Review: APPROVED


Reviewed exact head 14c32bf8f7a1951b124c9122cefa9de593a5e033. The SysV and GNU hash tables are generated from defined symbols; reordering is confined to the defined-symbol suffix, so undefined-symbol indices used by relocations remain unchanged. The added Linux executable-export test exercises dlsym in both ET_DYN and fixed-base output, and the author reports a real Minecraft launch. I independently ran git diff --check upstream/main...HEAD and python3 tools/check_conventions.py --base upstream/main --head HEAD; both passed. Hosted relinker checks for Ubuntu, Windows, macOS arm64 and macOS x86_64 plus ASan all pass. Product build jobs are skipped by repository path policy; I did not run the Linux executable-export test locally because this checkout has no built relinker binary.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642644010 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10917#pullrequestreview-5642644010). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T10:02:18Z. **Review:** APPROVED --- Reviewed exact head `14c32bf8f7a1951b124c9122cefa9de593a5e033`. The SysV and GNU hash tables are generated from defined symbols; reordering is confined to the defined-symbol suffix, so undefined-symbol indices used by relocations remain unchanged. The added Linux executable-export test exercises `dlsym` in both ET_DYN and fixed-base output, and the author reports a real Minecraft launch. I independently ran `git diff --check upstream/main...HEAD` and `python3 tools/check_conventions.py --base upstream/main --head HEAD`; both passed. Hosted relinker checks for Ubuntu, Windows, macOS arm64 and macOS x86_64 plus ASan all pass. Product build jobs are skipped by repository path policy; I did not run the Linux executable-export test locally because this checkout has no built relinker binary.
ahmadmuj commented 2026-10-25 11:09:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T11:09:27Z.

Review: COMMENTED


Reviewed head 14c32bf8f7a1, merged locally onto main b91a6a60c5ab (clean). Linux x86_64, relinker-only build of both main and the merged tree.

What I ran:

  • ctest -R linux on the merged build: 17/17 pass.
  • A 4-export fixture (built from test_linux_load_alignment.fixture(), exports Ae, Ad, Ac, Ab, unused-filter=0) through main's relinker and this PR's, then a probe that dlsyms every export and checks the relocated slots. Both rc 42.
  • A small PT_DYNAMIC walker that looks up each export through DT_HASH and through DT_GNU_HASH.

The GNU hash side looks right: all 4 exports resolve through it, and glibc loads the output fine.

DT_HASH goes stale once there are 2+ exports. The SysV buckets and chains are filled with the pre-sort indices (LinuxElfPatcher.cpp, the sysvChains[symIdx] loop), then the stable_sort + memcpy moves the exports, and DT_HASH is never rebuilt.

Example: exports at dynsym 4..7 in order Ae, Ad, Ac, Ab. ELF hash % 4 is 1, 0, 3, 2, so DT_HASH gets buckets=[5,4,7,6] with all chains 0. GNU hash % 4 is 3, 2, 1, 0, so the sort turns 4..7 into Ab, Ac, Ad, Ae. Now bucket 0 (meant for Ad) points at index 5, which is Ac, and all 4 DT_HASH lookups miss.

glibc doesn't notice because it prefers DT_GNU_HASH. If I retag DT_GNU_HASH as DT_CHECKSUM on the PR's output so ld.so falls back to DT_HASH, it fails: symbol lookup error: ./sysv-only.elf: undefined symbol: Ad, rc 127. So anything that still reads DT_HASH gets wrong answers. Building the SysV table after the sort, from the final order, fixes it.

Smaller notes:

  • The in-place reorder only works because exports are one contiguous block after all imports (SysVDynamicSectionBuilder.cpp emits the UNDEF imports first, RelinkerPipeline.cpp appends exports last) and no relocation names an export. Both hold today, but Patch doesn't check either. Calling Patch directly with dynsym [null, Ae, Imp(UNDEF), Ac] gives [null, Ae, Ac, Ac], and the GLOB_DAT against Imp now names Ac. A throw when the defined indices aren't contiguous, or when a relocation's r_sym falls in the defined range, would stop a later change from mis-binding silently.
  • No existing test has more than 1 export (I checked the shapes of all Linux and export tests), so neither the reorder nor DT_HASH is covered. The Ae, Ad, Ac, Ab case above plus a DT_HASH lookup would catch this.
  • Minor: with --trace-imports the UNDEF TraceImport_nid_no_patch sits after the exports, so it's past symoffset but outside the GNU chain array. glibc doesn't care; tools that count dynsym from DT_GNU_HASH will come up one short.

Limits: synthetic fixtures only, no game dumps, so I can't confirm the Minecraft startup numbers.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642868068 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10917#pullrequestreview-5642868068). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-10-25T11:09:27Z. **Review:** COMMENTED --- Reviewed head `14c32bf8f7a1`, merged locally onto main `b91a6a60c5ab` (clean). Linux x86_64, relinker-only build of both main and the merged tree. What I ran: - `ctest -R linux` on the merged build: 17/17 pass. - A 4-export fixture (built from `test_linux_load_alignment.fixture()`, exports `Ae, Ad, Ac, Ab`, `unused-filter=0`) through main's relinker and this PR's, then a probe that dlsyms every export and checks the relocated slots. Both rc 42. - A small PT_DYNAMIC walker that looks up each export through DT_HASH and through DT_GNU_HASH. The GNU hash side looks right: all 4 exports resolve through it, and glibc loads the output fine. **DT_HASH goes stale once there are 2+ exports.** The SysV buckets and chains are filled with the pre-sort indices (`LinuxElfPatcher.cpp`, the `sysvChains[symIdx]` loop), then the `stable_sort` + memcpy moves the exports, and DT_HASH is never rebuilt. Example: exports at dynsym 4..7 in order `Ae, Ad, Ac, Ab`. ELF hash % 4 is 1, 0, 3, 2, so DT_HASH gets `buckets=[5,4,7,6]` with all chains 0. GNU hash % 4 is 3, 2, 1, 0, so the sort turns 4..7 into `Ab, Ac, Ad, Ae`. Now bucket 0 (meant for `Ad`) points at index 5, which is `Ac`, and all 4 DT_HASH lookups miss. glibc doesn't notice because it prefers DT_GNU_HASH. If I retag DT_GNU_HASH as DT_CHECKSUM on the PR's output so ld.so falls back to DT_HASH, it fails: `symbol lookup error: ./sysv-only.elf: undefined symbol: Ad`, rc 127. So anything that still reads DT_HASH gets wrong answers. Building the SysV table after the sort, from the final order, fixes it. Smaller notes: - The in-place reorder only works because exports are one contiguous block after all imports (`SysVDynamicSectionBuilder.cpp` emits the UNDEF imports first, `RelinkerPipeline.cpp` appends exports last) and no relocation names an export. Both hold today, but `Patch` doesn't check either. Calling `Patch` directly with dynsym `[null, Ae, Imp(UNDEF), Ac]` gives `[null, Ae, Ac, Ac]`, and the GLOB_DAT against `Imp` now names `Ac`. A throw when the defined indices aren't contiguous, or when a relocation's r_sym falls in the defined range, would stop a later change from mis-binding silently. - No existing test has more than 1 export (I checked the shapes of all Linux and export tests), so neither the reorder nor DT_HASH is covered. The `Ae, Ad, Ac, Ab` case above plus a DT_HASH lookup would catch this. - Minor: with `--trace-imports` the UNDEF `TraceImport_nid_no_patch` sits after the exports, so it's past symoffset but outside the GNU chain array. glibc doesn't care; tools that count dynsym from DT_GNU_HASH will come up one short. Limits: synthetic fixtures only, no game dumps, so I can't confirm the Minecraft startup numbers.
MarcZX10 commented 2026-10-25 13:41:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T13:41:20Z.

Review: APPROVED


Re-reviewed exact updated head 1ad89aa8e7da35fd51389525dfe0b1229fec317f, including the follow-up SysV hash fix. Defined dynamic symbols are contiguous after the undefined prefix; the code explicitly verifies that and rejects relocations referencing the region before reordering, so existing undefined-symbol indices remain stable. The common sorted export order now feeds both SysV chains/buckets and GNU buckets/chains, avoiding a mismatch between the two lookup formats. The added multi-export fixture deliberately uses names whose bucket ordering differs from input order, checks exported lookup through normal dual-hash output, then changes the dynamic tag to force the SysV-only path and runs the executable again. check_conventions.py and git diff --check pass locally. Hosted Ubuntu, Windows, macOS ARM/x86_64, and ASan relinker jobs are green; product jobs are skipped by policy. The earlier independent Linux review also exercised executable exports on a merge build.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643455669 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10917#pullrequestreview-5643455669). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T13:41:20Z. **Review:** APPROVED --- Re-reviewed exact updated head `1ad89aa8e7da35fd51389525dfe0b1229fec317f`, including the follow-up SysV hash fix. Defined dynamic symbols are contiguous after the undefined prefix; the code explicitly verifies that and rejects relocations referencing the region before reordering, so existing undefined-symbol indices remain stable. The common sorted export order now feeds both SysV chains/buckets and GNU buckets/chains, avoiding a mismatch between the two lookup formats. The added multi-export fixture deliberately uses names whose bucket ordering differs from input order, checks exported lookup through normal dual-hash output, then changes the dynamic tag to force the SysV-only path and runs the executable again. `check_conventions.py` and `git diff --check` pass locally. Hosted Ubuntu, Windows, macOS ARM/x86_64, and ASan relinker jobs are green; product jobs are skipped by policy. The earlier independent Linux review also exercised executable exports on a merge build.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!10917
No description provided.