Skip to content

fix(relinker): write Windows import diagnostics after the image is built - #4665

Closed
SandstromPL wants to merge 1 commit into
boykopovar:mainfrom
SandstromPL:fix/relinker-diagnostics-after-patch
Closed

SandstromPL wants to merge 1 commit into
boykopovar:mainfrom
SandstromPL:fix/relinker-diagnostics-after-patch

Conversation

@SandstromPL

@SandstromPL SandstromPL commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

What

With --windows --windows-diagnostics, a relink that fails late in WindowsPePatcher::Patch still replaces windows-diagnostics-imports.txt in the working directory. It prints Wrote Windows import diagnostics to …, then FAIL:, and exits 2. The previous executable is kept, so the file beside it now lists the imports of an input that was never converted. Checks after the write that can fail include, for example:

  • an sce_sys/icon0.png that fails the Windows icon check (Invalid Windows icon PNG);
  • a run path Windows does not support, such as --rpath '${ORIGIN}/libs' (Unsupported variable in Windows run path) or --rpath /opt/libs (Ambiguous Windows run path).

Patch now writes the file after the PE image is built, including WindowsPeWriter::Write, which can also throw. A failed check therefore leaves the previous file untouched. This is the rule of ca330dfef (a malformed icon0.png leaves no output behind) and #4346, applied to the diagnostics file. The list itself is unchanged: relocations.Imports is only read in between, by WindowsEntryStubBuilder::Build through a const reference.

Tested

Ubuntu 24.04, GCC 13.3, relinker-only build with BUILD_TESTING=ON: 134/134 tests pass.

  • test_input_preservation.py gains check_diagnostics_after_failed_patch. It runs --windows --windows-diagnostics with a previous executable and a previous diagnostics file, once with a malformed sce_sys/icon0.png and once with --rpath '${ORIGIN}/libs'. Each run must exit 2 with its error, print no Wrote line, and leave both files unchanged. On main the test fails: the Wrote line is printed and the diagnostics file is overwritten.
  • I relinked the test fixtures with main and with this branch: the main executable alone, with a bundled module, and with the libc.prx/other.prx modules from test_guest_registry.py (a non-empty import list), each with and without a valid icon. Successful relinks give byte-identical executables, guest modules, diagnostics files and output. On the failing inputs, only main replaces the diagnostics file.
  • Three open pull requests edit neighbouring lines of WindowsPePatcher.cpp. feat(relinker): add --trace-imports for Windows executables #4547 edits the Build call after the removed lines; it already needs a rebase on main. feat(relinker): support fixed Windows guest image bases #4140 and fix(relinker): load Windows executables at a fixed low base #4237 edit the final return, where they already conflict with each other. None needs to go first: whichever merges second keeps the other's line, with the diagnostics call after Write.
  • tools/check_conventions.py from main passes with this title and body.

Not covered: the file is still written before the guest modules and the executable, so a failed executable write leaves the new list beside the previous executable. Writing it after the executable would need the import list out of Patch, which changes the patcher interface.

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 in TechnicalDebt
  • 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
  • Unblocks: none
  • AI-assisted: yes (Claude Code); I reviewed and tested every line

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@SandstromPL

Copy link
Copy Markdown
Contributor Author

Neither builds on the other: #4140 and #4237 change the arguments of the Write call on L109, and this PR only writes the diagnostics file after it. Whichever merges second keeps both, so there's no Depends on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with main, rebase needed relinker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant