fix(nid): preserve PE forwarders when repacking export names #6912

Merged
Zinebyte merged 1 commit from upstream-pr/6912 into main 2026-10-19 13:14:39 +00:00
Zinebyte commented 2026-10-16 17:53:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Zinebyte on 2026-10-16T17:53:39Z.


What

Renaming PE exports can overwrite forwarded-export targets while nid_patcher reports success. With GNU Binutils 2.42, an ordinary DLL exporting a=local and b=KERNEL32.GetCurrentProcessId places a name before the forwarder string. On current main, patching leaves the forwarder RVA at 0x304c but changes its target from KERNEL32.GetCurrentProcessId to k3ozzWYBg, part of a replacement NID.

Capture forwarded targets before repacking, deduplicate their storage and update every affected export-address entry, including unnamed exports. Preserve the DLL name, original name-to-ordinal associations and direct code/data exports. Update the directory size to cover the packed export strings while checking that every direct export remains outside that range: the PE export-address table format uses the directory bounds to distinguish direct exports from forwarders. Reclaim old string space and refuse before writing the file when new metadata would overlap exported data, change a direct export into a forwarder or exceed available space.

Tested

Linux x86_64, G++ 13.3, GNU Binutils 2.42; built the actual tool from all nine NID sources.

  • Reproduced the corruption with an unmodified GNU-linked PE32+ DLL. The fixed tool preserves KERNEL32.GetCurrentProcessId, the DLL name, direct code RVA 0x1000, every .text byte and the correct sorted NID-to-ordinal mapping. GNU objdump displays both full NIDs without corrupt-offset warnings; the directory size grows from 0x6b to 0x7f while direct exports retain their classification. Native Windows loading was not run.
  • All ten tests in the existing test_pe_references.py group pass. Coverage includes PE32/PE32+, interleaved strings, named/ordinal/unnamed/shared forwarders, long-name space reclamation, external DLL metadata, direct-data preservation, directory growth and unchanged-file refusal when growth would overwrite exported data or reclassify a direct export. Main fails the forwarder and overlapping-growth regressions.
  • All eight applicable NID Python test groups pass: GNU hash, ELF layout, Mach-O, native paths, PE references, shared strings, symbol versions and version requirements.
  • Independent adverse review reproduced the original corruption, checked the linked DLL and exercised a separate external-DLL-name/direct-data case. Conventions and whitespace checks pass.

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; silent stubs are listed under Silent stubs
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4892233776 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6912). Originally posted by [Zinebyte](https://github.com/Zinebyte) on 2026-10-16T17:53:39Z. --- ### What Renaming PE exports can overwrite forwarded-export targets while `nid_patcher` reports success. With GNU Binutils 2.42, an ordinary DLL exporting `a=local` and `b=KERNEL32.GetCurrentProcessId` places a name before the forwarder string. On current main, patching leaves the forwarder RVA at `0x304c` but changes its target from `KERNEL32.GetCurrentProcessId` to `k3ozzWYBg`, part of a replacement NID. Capture forwarded targets before repacking, deduplicate their storage and update every affected export-address entry, including unnamed exports. Preserve the DLL name, original name-to-ordinal associations and direct code/data exports. Update the directory size to cover the packed export strings while checking that every direct export remains outside that range: the [PE export-address table format](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#export-address-table) uses the directory bounds to distinguish direct exports from forwarders. Reclaim old string space and refuse before writing the file when new metadata would overlap exported data, change a direct export into a forwarder or exceed available space. ### Tested Linux x86_64, G++ 13.3, GNU Binutils 2.42; built the actual tool from all nine NID sources. - Reproduced the corruption with an unmodified GNU-linked PE32+ DLL. The fixed tool preserves `KERNEL32.GetCurrentProcessId`, the DLL name, direct code RVA `0x1000`, every `.text` byte and the correct sorted NID-to-ordinal mapping. GNU `objdump` displays both full NIDs without corrupt-offset warnings; the directory size grows from `0x6b` to `0x7f` while direct exports retain their classification. Native Windows loading was not run. - All ten tests in the existing `test_pe_references.py` group pass. Coverage includes PE32/PE32+, interleaved strings, named/ordinal/unnamed/shared forwarders, long-name space reclamation, external DLL metadata, direct-data preservation, directory growth and unchanged-file refusal when growth would overwrite exported data or reclassify a direct export. Main fails the forwarder and overlapping-growth regressions. - All eight applicable NID Python test groups pass: GNU hash, ELF layout, Mach-O, native paths, PE references, shared strings, symbol versions and version requirements. - Independent adverse review reproduced the original corruption, checked the linked DLL and exercised a separate external-DLL-name/direct-data case. Conventions and whitespace checks pass. ### 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 - [x] Unimplemented paths throw; silent stubs are listed under Silent stubs - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
Baconzido commented 2026-10-17 21:41:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Baconzido on 2026-10-17T21:41:43Z.


macOS result for the NID Python groups. The description says native Windows loading was not run; this does not cover that either.

Apple M5 Pro (arm64), macOS 27.0.1, Python 3.14.7. nid_patcher is not part of the relinker-only build, so I built it from the nine sources in core/libs/nid/src with AppleClang 21.0.0 (-std=c++20 -O1). Head 1295a8001 merged onto main 922dc8999 (clean merge).

  • test_pe_references.py with the PR's patcher: 10/10 pass.
  • Negative control: same tests against a patcher built with main's PeNidPatcher.cpp: 7 of 10 fail (test_directory_grows_to_include_repacked_names, test_directory_growth_cannot_reclassify_a_direct_export, test_name_growth_cannot_overwrite_direct_export_data, and test_forwarder_targets_and_ordinals_survive_name_repacking for bits 32/64 × dll_last False/True).
  • Other groups with the PR's patcher: test_elf_duplicates 4/4, test_elf_layout 3/3, test_gnu_hash 6/6, test_native_paths 3/3, test_shared_strings 3/3, test_verneed 6/6, test_macho_nids and test_symbol_versions pass. test_dynamic_strings.py takes an ELF file as its argument rather than the patcher, so I did not count it.

Not checked: native Windows loading of a repacked DLL, Windows host.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6241606653 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6912#issuecomment-6241606653). Originally posted by [Baconzido](https://github.com/Baconzido) on 2026-10-17T21:41:43Z. --- macOS result for the NID Python groups. The description says native Windows loading was not run; this does not cover that either. Apple M5 Pro (arm64), macOS 27.0.1, Python 3.14.7. `nid_patcher` is not part of the relinker-only build, so I built it from the nine sources in `core/libs/nid/src` with AppleClang 21.0.0 (`-std=c++20 -O1`). Head `1295a8001` merged onto `main` `922dc8999` (clean merge). - `test_pe_references.py` with the PR's patcher: 10/10 pass. - Negative control: same tests against a patcher built with `main`'s `PeNidPatcher.cpp`: 7 of 10 fail (`test_directory_grows_to_include_repacked_names`, `test_directory_growth_cannot_reclassify_a_direct_export`, `test_name_growth_cannot_overwrite_direct_export_data`, and `test_forwarder_targets_and_ordinals_survive_name_repacking` for bits 32/64 × dll_last False/True). - Other groups with the PR's patcher: `test_elf_duplicates` 4/4, `test_elf_layout` 3/3, `test_gnu_hash` 6/6, `test_native_paths` 3/3, `test_shared_strings` 3/3, `test_verneed` 6/6, `test_macho_nids` and `test_symbol_versions` pass. `test_dynamic_strings.py` takes an ELF file as its argument rather than the patcher, so I did not count it. Not checked: native Windows loading of a repacked DLL, Windows host. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
LeandroLP23 commented 2026-10-18 16:13:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-18T16:13:27Z.


Windows run, covering the native loading the description lists as not run.

Radeon 860M (RDNA 3.5), Ryzen AI 7 350, Windows 11 26300, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. main 6efb7261b with #7432, #6912 and #6893 merged on top (no conflicts, disjoint files).

  • The ten nid_* tests pass, among them nid_pe_references.
  • Every core/libs/libs/*.prx in this build was patched by this PR's nid_patcher. A title loaded them natively: VLC (PPSA85300 01.000.005), relinked with --windows, reached rendering at 39.7 FPS in a 30 s run through tools/title_run.py (#7264).

AI-assisted

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6252141459 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6912#issuecomment-6252141459). Originally posted by [LeandroLP23](https://github.com/LeandroLP23) on 2026-10-18T16:13:27Z. --- Windows run, covering the native loading the description lists as not run. Radeon 860M (RDNA 3.5), Ryzen AI 7 350, Windows 11 26300, WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`. `main` `6efb7261b` with #7432, #6912 and #6893 merged on top (no conflicts, disjoint files). - The ten `nid_*` tests pass, among them `nid_pe_references`. - Every `core/libs/libs/*.prx` in this build was patched by this PR's `nid_patcher`. A title loaded them natively: VLC (PPSA85300 01.000.005), relinked with `--windows`, reached `rendering` at 39.7 FPS in a 30 s run through `tools/title_run.py` (#7264). AI-assisted
adriadam10 commented 2026-10-19 13:16:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T13:16:01Z.


Read the diff against the PE export format: forwarder detection uses the directory bounds, direct exports are kept outside the grown range, and the patch refuses before writing when it would overlap exported data or reclassify a direct export. The new tests pin forwarder targets, ordinals and directory size; the macOS negative control (7/10 fail on main's patcher) and the Windows run cover what we can't run here; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6268121923 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6912#issuecomment-6268121923). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-19T13:16:01Z. --- Read the diff against the PE export format: forwarder detection uses the directory bounds, direct exports are kept outside the grown range, and the patch refuses before writing when it would overlap exported data or reclassify a direct export. The new tests pin forwarder targets, ordinals and directory size; the macOS negative control (7/10 fail on main's patcher) and the Windows run cover what we can't run here; merged.
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!6912
No description provided.