fix(relinker): preserve imports with an empty GNU hash #25672

Merged
mcarkade merged 1 commit from upstream-pr/25672 into main 2026-11-27 18:59:23 +00:00
mcarkade commented 2026-11-22 14:14:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mcarkade on 2026-11-22T14:14:27Z.


What

GNU ld 2.42 with -shared --hash-style=gnu -s writes an empty GNU hash for a plugin with no exports, even when .dynsym contains an import. Relinking a bundled plugin built this way stops at its first import relocation with Unsupported relocation or symbol index.

When every GNU hash bucket is empty, the guest module reader, code scanner, and strict NID filter use the same helper to count symbols from the matching .dynsym section. Without section headers, the count still falls back to symoffset. A malformed section table is rejected.

Tested

Windows 11, MinGW 15.2, Python 3.13.5, CMake/CTest 4.3.1. guest_gnu_hash_import_only fails on main and passes with the fix. It checks the converted Linux relocation and provider dependency, and runs the Windows output. The extended strict_nid_filter case fails on the previous PR head because the filter drops a reachable import. It passes with the fix and also checks that a truncated section table is rejected. The conventions check passed. Of 20 focused relinker CTests, 19 passed. reloc_table_bounds failed with the same writable-segment error on the previous PR head.

A stripped plugin built with GNU ld 2.42 reproduced the failure on main. With the fix, it converted for Linux and Windows, and the Linux loader resolved the import under WSL. The Linux startup probe under WSL and the Windows output each returned 42. macOS runtime was unchecked.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5439808880 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25672). Originally posted by [mcarkade](https://github.com/mcarkade) on 2026-11-22T14:14:27Z. --- ### What GNU ld 2.42 with `-shared --hash-style=gnu -s` writes an empty GNU hash for a plugin with no exports, even when `.dynsym` contains an import. Relinking a bundled plugin built this way stops at its first import relocation with `Unsupported relocation or symbol index`. When every GNU hash bucket is empty, the guest module reader, code scanner, and strict NID filter use the same helper to count symbols from the matching `.dynsym` section. Without section headers, the count still falls back to `symoffset`. A malformed section table is rejected. ### Tested Windows 11, MinGW 15.2, Python 3.13.5, CMake/CTest 4.3.1. `guest_gnu_hash_import_only` fails on main and passes with the fix. It checks the converted Linux relocation and provider dependency, and runs the Windows output. The extended `strict_nid_filter` case fails on the previous PR head because the filter drops a reachable import. It passes with the fix and also checks that a truncated section table is rejected. The conventions check passed. Of 20 focused relinker CTests, 19 passed. `reloc_table_bounds` failed with the same writable-segment error on the previous PR head. A stripped plugin built with GNU ld 2.42 reproduced the failure on main. With the fix, it converted for Linux and Windows, and the Linux loader resolved the import under WSL. The Linux startup probe under WSL and the Windows output each returned 42. macOS runtime was unchecked. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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) and the licence notice of code adapted from upstream (see AGENTS.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
throny commented 2026-11-22 14:33:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by throny on 2026-11-22T14:33:31Z.

Review: COMMENTED


Review: ⚠️ fixes the reported case; a third copy of the same logic is left

Basis: code read; the new import-only test run against main's relinker.exe (fails with consumer.prx: Unsupported relocation or symbol index); this PR not built here

Checked

  • On main, GuestImageReader.cpp and CodeInstructionCollector.cpp fall back to symoffset as the symbol count when every GNU hash bucket is 0, so an import at index >= symoffset is dropped. Using the SHT_DYNSYM section size in that case is the only source left; the non-empty-hash, SysV and SCE paths are unchanged.

Findings

  1. StrictNidFilter.cpp:158-179 has a third copy of the same counting (count = firstSymbol when lastBucket == 0) that isn't updated, so with the strict filter a main executable with an empty GNU hash would still undercount imports while the other two readers now see them. A shared helper would keep the three in step.
  2. With no section headers (sstrip-style), the empty-hash case still falls back to symoffset and drops imports, silently; worth a line in docs/dev/debt.
  3. ReadDynamicSymbolCount now parses the section header table, so a module with an empty GNU hash and a junk or truncated section table throws where it used to convert. Probably right, but it's new behaviour.

Suggested action: apply the same count to StrictNidFilter (or share it) and record the no-section-header gap; then merge.

AI-assisted review (Claude Code) · head e4dd269b58 · base main 337a948bf1

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6012788067 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25672#pullrequestreview-6012788067). Originally posted by [throny](https://github.com/throny) on 2026-11-22T14:33:31Z. **Review:** COMMENTED --- ### Review: ⚠️ fixes the reported case; a third copy of the same logic is left **Basis:** code read; the new `import-only` test run against `main`'s `relinker.exe` (fails with `consumer.prx: Unsupported relocation or symbol index`); this PR not built here **Checked** - On `main`, `GuestImageReader.cpp` and `CodeInstructionCollector.cpp` fall back to `symoffset` as the symbol count when every GNU hash bucket is 0, so an import at index >= `symoffset` is dropped. Using the `SHT_DYNSYM` section size in that case is the only source left; the non-empty-hash, SysV and SCE paths are unchanged. **Findings** 1. `StrictNidFilter.cpp:158-179` has a third copy of the same counting (`count = firstSymbol` when `lastBucket == 0`) that isn't updated, so with the strict filter a main executable with an empty GNU hash would still undercount imports while the other two readers now see them. A shared helper would keep the three in step. 2. With no section headers (sstrip-style), the empty-hash case still falls back to `symoffset` and drops imports, silently; worth a line in `docs/dev/debt`. 3. `ReadDynamicSymbolCount` now parses the section header table, so a module with an empty GNU hash and a junk or truncated section table throws where it used to convert. Probably right, but it's new behaviour. **Suggested action:** apply the same count to `StrictNidFilter` (or share it) and record the no-section-header gap; then merge. <sub>AI-assisted review (Claude Code) · head `e4dd269b58` · base `main` `337a948bf1`</sub>
MarcZX10 commented 2026-11-22 14:39:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T14:39:27Z.

Review: APPROVED


Reviewed — looks good

The change adds a section-header fallback for counting dynamic symbols when GNU hash buckets are empty; the inspected implementation and regression fixture support the stated import-only case.

What I ran

No build or tests were run. I read the prepared diff and changed files at head e4dd269b586084032a4540f2276f04c1d45f4a04 using repo_read.py.

Results

Check Result Note
Source review passed Changed implementation and regression fixture inspected
Build and tests not run Author-reported results were not independently checked

Findings

Blocking

None in the code reviewed.

Non-blocking

None.

Not verified

The build, CTests, conventions check, and reported Windows, WSL/Linux, and macOS behavior were not independently verified. core/relinker/CMakeLists.txt, core/relinker/relinker/include/relinker/parsing/ElfReader.hpp, core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp, core/relinker/relinker/src/guest/GuestImageReader.cpp, core/relinker/relinker/src/parsing/ElfReader.cpp, and core/relinker/relinker/tests/test_guest_gnu_hash.py were read. No changed files were excluded in the manifest.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6012828536 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25672#pullrequestreview-6012828536). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-22T14:39:27Z. **Review:** APPROVED --- # Reviewed — looks good The change adds a section-header fallback for counting dynamic symbols when GNU hash buckets are empty; the inspected implementation and regression fixture support the stated import-only case. ## What I ran No build or tests were run. I read the prepared diff and changed files at head `e4dd269b586084032a4540f2276f04c1d45f4a04` using `repo_read.py`. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | Changed implementation and regression fixture inspected | | Build and tests | not run | Author-reported results were not independently checked | ## Findings ### Blocking None in the code reviewed. ### Non-blocking None. ## Not verified The build, CTests, conventions check, and reported Windows, WSL/Linux, and macOS behavior were not independently verified. `core/relinker/CMakeLists.txt`, `core/relinker/relinker/include/relinker/parsing/ElfReader.hpp`, `core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp`, `core/relinker/relinker/src/guest/GuestImageReader.cpp`, `core/relinker/relinker/src/parsing/ElfReader.cpp`, and `core/relinker/relinker/tests/test_guest_gnu_hash.py` were read. No changed files were excluded in the manifest. ## To get this to a good state Nothing is required.
PrivacyOnion commented 2026-11-22 14:50:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-22T14:50:27Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against dbccaacaa1:

  • Conventions: tools/check_conventions.py passed.
  • Build: strict_nid_filter_tests built cleanly.
  • CTest (strict_nid_filter):
    1/1 Test #6606: strict_nid_filter ................   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/relinker/relinker/include/relinker/parsing/ElfReader.hpp, core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp, core/relinker/relinker/src/analysis/UnusedNidFilter/StrictNidFilter.cpp, core/relinker/relinker/src/guest/GuestImageReader.cpp, core/relinker/relinker/src/parsing/ElfReader.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #6606: strict_nid_filter ................***Failed    0.01 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified core/relinker/relinker/src/parsing/ElfReader.cpp: ElfReader::ReadDynamicSymbolCount properly validates SHT_DYNSYM sections matching symbolOffset, rejects malformed section sizes where size % kSymEntrySize != 0 or size / kSymEntrySize < minimum with RelinkerException('Invalid dynamic symbol count'), and cleanly falls back to minimum when no section header matches.
    • Verified core/relinker/relinker/src/analysis/UnusedNidFilter/StrictNidFilter.cpp: StrictImage::Build now synchronizes with the other readers when lastBucket == 0 by invoking reader.ReadDynamicSymbolCount(*symbols, firstSymbol), followed by image boundary verification (count > bytes.size() / 24 and requireRange).
    • Verified core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp and core/relinker/relinker/src/guest/GuestImageReader.cpp: both readers consistently fall back to ElfReader(bytes).ReadDynamicSymbolCount(symbolOffset, firstSymbol) when GNU hash buckets are zero.
    • Verified core/relinker/relinker/tests/StrictNidFilterTests.cpp and test_guest_gnu_hash.py: regression tests verify that import-only modules preserve imports across Linux and Windows outputs, and that out-of-bounds section tables are rejected.
    • Verified AGENTS.md compliance: code contains no unauthorized comments, the technical debt fragment docs/dev/debt/changes/fix-relinker-gnu-hash-import-only.md documents the no-section-header fallback, and conventions check passed.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6012872603 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25672#pullrequestreview-6012872603). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-22T14:50:27Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `dbccaacaa1`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `strict_nid_filter_tests` built cleanly. - **CTest** (`strict_nid_filter`): ``` 1/1 Test #6606: strict_nid_filter ................ Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/relinker/relinker/include/relinker/parsing/ElfReader.hpp`, `core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp`, `core/relinker/relinker/src/analysis/UnusedNidFilter/StrictNidFilter.cpp`, `core/relinker/relinker/src/guest/GuestImageReader.cpp`, `core/relinker/relinker/src/parsing/ElfReader.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #6606: strict_nid_filter ................***Failed 0.01 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified core/relinker/relinker/src/parsing/ElfReader.cpp: ElfReader::ReadDynamicSymbolCount properly validates SHT_DYNSYM sections matching symbolOffset, rejects malformed section sizes where size % kSymEntrySize != 0 or size / kSymEntrySize < minimum with RelinkerException('Invalid dynamic symbol count'), and cleanly falls back to minimum when no section header matches. - Verified core/relinker/relinker/src/analysis/UnusedNidFilter/StrictNidFilter.cpp: StrictImage::Build now synchronizes with the other readers when lastBucket == 0 by invoking reader.ReadDynamicSymbolCount(*symbols, firstSymbol), followed by image boundary verification (count > bytes.size() / 24 and requireRange). - Verified core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp and core/relinker/relinker/src/guest/GuestImageReader.cpp: both readers consistently fall back to ElfReader(bytes).ReadDynamicSymbolCount(symbolOffset, firstSymbol) when GNU hash buckets are zero. - Verified core/relinker/relinker/tests/StrictNidFilterTests.cpp and test_guest_gnu_hash.py: regression tests verify that import-only modules preserve imports across Linux and Windows outputs, and that out-of-bounds section tables are rejected. - Verified AGENTS.md compliance: code contains no unauthorized comments, the technical debt fragment docs/dev/debt/changes/fix-relinker-gnu-hash-import-only.md documents the no-section-header fallback, and conventions check passed. Looks Good To Me
throny commented 2026-11-22 15:05:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by throny on 2026-11-22T15:05:28Z.

Review: APPROVED


Review: ✅ all three readers now count imports the same way; I'd merge it

Basis: code read of dbccaacaa1 (not built here)

Checked

  • ElfReader::ReadDynamicSymbolCount (ElfReader.cpp:290) takes the size of the SHT_DYNSYM section at the DT_SYMTAB file offset. It throws if the size isn't a multiple of 24 or is below symoffset, and falls back to symoffset when there is no such section. GuestImageReader, CodeInstructionCollector and StrictNidFilter (line 192) all call it on the empty-bucket path, so my finding 1 is fixed. The non-empty-hash, SysV and DT_SCE paths are unchanged.
  • The fallback when there are no section headers is recorded in fix-relinker-gnu-hash-import-only.md (finding 2). Keeping the error for a malformed table (finding 3) is the author's call, and it's reasonable.

Findings
none. The author reports that reloc_table_bounds also fails on the previous head; the relinker (windows-latest) CI job is still running and will show whether it fails on main too.

Suggested action: merge

AI-assisted review (Claude Code) · head dbccaacaa1 · base main 337a948bf1

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6012932251 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25672#pullrequestreview-6012932251). Originally posted by [throny](https://github.com/throny) on 2026-11-22T15:05:28Z. **Review:** APPROVED --- ### Review: ✅ all three readers now count imports the same way; I'd merge it **Basis:** code read of `dbccaacaa1` (not built here) **Checked** - `ElfReader::ReadDynamicSymbolCount` (ElfReader.cpp:290) takes the size of the `SHT_DYNSYM` section at the `DT_SYMTAB` file offset. It throws if the size isn't a multiple of 24 or is below `symoffset`, and falls back to `symoffset` when there is no such section. `GuestImageReader`, `CodeInstructionCollector` and `StrictNidFilter` (line 192) all call it on the empty-bucket path, so my finding 1 is fixed. The non-empty-hash, SysV and `DT_SCE` paths are unchanged. - The fallback when there are no section headers is recorded in `fix-relinker-gnu-hash-import-only.md` (finding 2). Keeping the error for a malformed table (finding 3) is the author's call, and it's reasonable. **Findings** none. The author reports that `reloc_table_bounds` also fails on the previous head; the `relinker (windows-latest)` CI job is still running and will show whether it fails on `main` too. **Suggested action:** merge <sub>AI-assisted review (Claude Code) · head `dbccaacaa1` · base `main` `337a948bf1`</sub>
MarcZX10 commented 2026-11-22 16:41:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T16:41:12Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the review found no verified blocking defect, and the author-reported tests are not treated as tests run here.

What I ran

Reviewed head dbccaacaa1ccd34c0657e1d6008abf10660045d5. No test or build commands were run; repository reading was limited to the supplied diff and targeted file ranges using repo_read.py.

Results

Check Result Note
Changed-code review passed Read all nine changed files
Tests and build not run Only the PR description reports test results

Findings

Blocking

None.

Non-blocking

None.

Not verified

The PR description reports 19 of 20 focused relinker CTests passed, with reloc_table_bounds failing on both this head and the previous PR head. Those runs were not independently verified. No build, test, Windows runtime, WSL runtime, or macOS runtime checks were run during this review. Read all files listed as reviewed in manifest.json; no files were excluded in the supplied manifest.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6013315992 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25672#pullrequestreview-6013315992). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-22T16:41:12Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the review found no verified blocking defect, and the author-reported tests are not treated as tests run here. ## What I ran Reviewed head `dbccaacaa1ccd34c0657e1d6008abf10660045d5`. No test or build commands were run; repository reading was limited to the supplied diff and targeted file ranges using `repo_read.py`. ## Results | Check | Result | Note | |---|---|---| | Changed-code review | passed | Read all nine changed files | | Tests and build | not run | Only the PR description reports test results | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The PR description reports 19 of 20 focused relinker CTests passed, with `reloc_table_bounds` failing on both this head and the previous PR head. Those runs were not independently verified. No build, test, Windows runtime, WSL runtime, or macOS runtime checks were run during this review. Read all files listed as reviewed in `manifest.json`; no files were excluded in the supplied manifest. ## To get this to a good state Nothing is required.
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!25672
No description provided.