Skip to content

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

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
Zinebyte:fix/pe-forwarder-strings
Oct 19, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
Zinebyte:fix/pe-forwarder-strings

Conversation

@Zinebyte

Copy link
Copy Markdown
Contributor

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

@Baconzido

Copy link
Copy Markdown

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

Copy link
Copy Markdown
Contributor

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 (feat(tools): run titles repeatably and report their outcome #7264).

AI-assisted

@adriadam10 adriadam10 self-assigned this Oct 19, 2026
@adriadam10
adriadam10 merged commit 9494481 into boykopovar:main Oct 19, 2026
8 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants