feat(relinker): add --trace-imports for Windows executables #4547

Merged
codingreapermaps merged 1 commit from upstream-pr/4547 into main 2026-10-13 15:48:22 +00:00
codingreapermaps commented 2026-10-12 15:23:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by codingreapermaps on 2026-10-12T15:23:03Z.


What

--trace-imports makes the Windows entry stub pass each function import through TraceImport_nid_no_patch in libc.prx (#4546) before writing its slot, so the calls a title makes to the libraries named in APS5_TRACE_CALLS are recorded and shown by the crash and abort reports. Without the option the output is unchanged. It covers part of #3815: the entry of a call, on Windows; arguments, results and Linux are not done.

Traced: every PLT slot, and a GOT slot only when the original symbol is a function. The second rule matters: the rebuilt dynamic symbol table types every import as a function, so the original type is carried from the input relocation. Without it a data import such as __stdoutp got a thunk and a homebrew title crashed when it wrote through it.

The option conflicts with --lazy-binding and needs libc.prx among the libraries; a libc.prx without the export fails at start-up with a message. Weak imports, imports of modules loaded at run time and functions looked up with sceKernelDlsym are not traced. Limits are in docs/dev/debt/libc.md and docs/dev/debt/relinker.md, usage in USAGE.md.

This branch contains the commit of #4546; only the second commit belongs to this PR.

Tested

Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), on main 43e3a70f8.

  • windows_trace_imports (new): a relinked fixture reaches the hook with the import's name and library and runs the address the hook returns; a data-typed GOT import is left alone; a weak import still resolves; a libc.prx without the export fails with the message; the option is rejected without --windows, with --lazy-binding and without libc.prx.
  • All 116 relinker tests pass.
  • A fixture importing getpagesize from the built libkernel.prx returns 0x4000 without the option, with it and the variable unset, and with it and APS5_TRACE_CALLS=libkernel; only the last prints the call.
  • DOOM Shareware (PPSA99666, lowbit/ps5-doom v1.1.1; eboot.bin and sce_module/libc.prx unwrapped from fSELF, relinked with --windows --to-intel --trace-imports; Intel i7-1165G7; before the rebase onto current main): with every library traced it reaches the same point as the untraced run (WAD loaded, audio opened at 48000 Hz, ST_Init) and ends with a normal process exit after 901,307,599 recorded calls. The last 64 were 62 sin, one pthread_mutex_unlock and one sceAudioOutOutput, which is why #4546 also counts calls per import.
  • Yamagi Quake II (PPSA99007, v0.2.0-alpha.1), relinked with --windows --to-intel --trace-imports: the option was used, together with APS5_HANG_DUMP_SECS, to confirm the start-up hang described in #4280 (main thread inside the mspace allocator, no window), now addressed by #4407. The report from that run was not kept. With local changes outside this PR the title goes on to load demo1 and present frames; that part does not depend on this option.
  • tools/check_conventions.py and tools/check_test_registration.py pass.

Not tested: either title after the rebase, a commercial title, Linux or macOS output (the option is rejected there).

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: #4546
  • Unblocks: none
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4831475785 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4547). Originally posted by [codingreapermaps](https://github.com/codingreapermaps) on 2026-10-12T15:23:03Z. --- ### What `--trace-imports` makes the Windows entry stub pass each function import through `TraceImport_nid_no_patch` in libc.prx (#4546) before writing its slot, so the calls a title makes to the libraries named in `APS5_TRACE_CALLS` are recorded and shown by the crash and abort reports. Without the option the output is unchanged. It covers part of #3815: the entry of a call, on Windows; arguments, results and Linux are not done. Traced: every PLT slot, and a GOT slot only when the original symbol is a function. The second rule matters: the rebuilt dynamic symbol table types every import as a function, so the original type is carried from the input relocation. Without it a data import such as `__stdoutp` got a thunk and a homebrew title crashed when it wrote through it. The option conflicts with `--lazy-binding` and needs libc.prx among the libraries; a libc.prx without the export fails at start-up with a message. Weak imports, imports of modules loaded at run time and functions looked up with `sceKernelDlsym` are not traced. Limits are in `docs/dev/debt/libc.md` and `docs/dev/debt/relinker.md`, usage in USAGE.md. This branch contains the commit of #4546; only the second commit belongs to this PR. ### Tested Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), on main 43e3a70f8. - `windows_trace_imports` (new): a relinked fixture reaches the hook with the import's name and library and runs the address the hook returns; a data-typed GOT import is left alone; a weak import still resolves; a libc.prx without the export fails with the message; the option is rejected without `--windows`, with `--lazy-binding` and without libc.prx. - All 116 relinker tests pass. - A fixture importing `getpagesize` from the built libkernel.prx returns 0x4000 without the option, with it and the variable unset, and with it and `APS5_TRACE_CALLS=libkernel`; only the last prints the call. - DOOM Shareware (PPSA99666, lowbit/ps5-doom v1.1.1; `eboot.bin` and `sce_module/libc.prx` unwrapped from fSELF, relinked with `--windows --to-intel --trace-imports`; Intel i7-1165G7; before the rebase onto current main): with every library traced it reaches the same point as the untraced run (WAD loaded, audio opened at 48000 Hz, `ST_Init`) and ends with a normal process exit after 901,307,599 recorded calls. The last 64 were 62 `sin`, one `pthread_mutex_unlock` and one `sceAudioOutOutput`, which is why #4546 also counts calls per import. - Yamagi Quake II (PPSA99007, v0.2.0-alpha.1), relinked with `--windows --to-intel --trace-imports`: the option was used, together with `APS5_HANG_DUMP_SECS`, to confirm the start-up hang described in #4280 (main thread inside the mspace allocator, no window), now addressed by #4407. The report from that run was not kept. With local changes outside this PR the title goes on to load `demo1` and present frames; that part does not depend on this option. - `tools/check_conventions.py` and `tools/check_test_registration.py` pass. Not tested: either title after the rebase, a commercial title, Linux or macOS output (the option is rejected there). ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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: #4546 - [x] Unblocks: none - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
codingreapermaps commented 2026-10-12 18:11:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by codingreapermaps on 2026-10-12T18:11:48Z.


On the overlap comment: none of the four implements the same thing, and this PR does not build on any of them, so Depends on stays #4546 only. They are edits to neighbouring lines. Each merges cleanly into main and conflicts with this branch, so whichever goes second needs a rebase; I will rebase this one as they land.

  • #4337: both add a test registration next to each other in core/relinker/CMakeLists.txt. Keep both lines.
  • #4452: adds a check beside the line in RelinkerPipeline.cpp where this PR reads the symbol type. Independent; a null symbol entry reads as not a function here, which is what is wanted.
  • #4510: adds a Weak flag in the same four places where this PR adds Function, read from the same st_info byte. Both append a positional bool to the same initializers, so the merged code has to pass each value to its own field; resolved carelessly it would compile and swap them.
  • #4140: adds a constructor parameter to WindowsPePatcher in the position this PR uses. Same caveat: a bool passed where its optional address is expected converts without a compile error, so the call in main.cpp has to be rewritten, not just merged.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6129934734 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4547#issuecomment-6129934734). Originally posted by [codingreapermaps](https://github.com/codingreapermaps) on 2026-10-12T18:11:48Z. --- On the overlap comment: none of the four implements the same thing, and this PR does not build on any of them, so `Depends on` stays #4546 only. They are edits to neighbouring lines. Each merges cleanly into `main` and conflicts with this branch, so whichever goes second needs a rebase; I will rebase this one as they land. - #4337: both add a test registration next to each other in `core/relinker/CMakeLists.txt`. Keep both lines. - #4452: adds a check beside the line in `RelinkerPipeline.cpp` where this PR reads the symbol type. Independent; a null symbol entry reads as not a function here, which is what is wanted. - #4510: adds a `Weak` flag in the same four places where this PR adds `Function`, read from the same `st_info` byte. Both append a positional `bool` to the same initializers, so the merged code has to pass each value to its own field; resolved carelessly it would compile and swap them. - #4140: adds a constructor parameter to `WindowsPePatcher` in the position this PR uses. Same caveat: a `bool` passed where its optional address is expected converts without a compile error, so the call in `main.cpp` has to be rewritten, not just merged. AI-assisted (Claude Code).
adriadam10 commented 2026-10-13 14:59:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T14:59:18Z.


Rebased onto current main: the branch still carried the first commit of #4546, which merged separately, so I dropped it, and resolved the conflicts with the refactored main.cpp/WindowsPePatcher (traceImports now goes through ConversionOptions into Conversion.cpp) plus the USAGE.md/debt/libc.md neighbours.

Checked the entry-stub change, the function/data split of imports (FunctionImports) and the option validation; relinker tests pass (125/125) and windows_trace_imports fails without the non-test changes. The Windows execution part of that test is covered by the full Build; merging once it is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6148489054 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4547#issuecomment-6148489054). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-13T14:59:18Z. --- Rebased onto current `main`: the branch still carried the first commit of #4546, which merged separately, so I dropped it, and resolved the conflicts with the refactored `main.cpp`/`WindowsPePatcher` (`traceImports` now goes through `ConversionOptions` into `Conversion.cpp`) plus the `USAGE.md`/`debt/libc.md` neighbours. Checked the entry-stub change, the function/data split of imports (`FunctionImports`) and the option validation; relinker tests pass (125/125) and `windows_trace_imports` fails without the non-test changes. The Windows execution part of that test is covered by the full Build; merging once it is green.
github-actions[bot] commented 2026-10-13 15:01:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T15:01:56Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#4593 core/relinker/cli/include/Cli.hpp L26
core/relinker/cli/src/CliArgs.cpp L13, L126
docs/user/USAGE.md L53
#4774 core/relinker/elfpatcher/include/elfpatcher/windows/WindowsEntryStubBuilder.hpp L23
core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp L69
core/relinker/elfpatcher/src/windows/WindowsPePatcher.cpp L90
#4780 docs/user/USAGE.md L159
#5021 docs/dev/debt/relinker.md L15
#5052 core/relinker/cli/src/CliArgs.cpp L13
#5087 docs/dev/debt/libc.md L67
#5229 core/relinker/domain/include/domain/Types.hpp L64
core/relinker/relinker/src/output/SysVDynamicSectionBuilder.cpp L96
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6148542820 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4547#issuecomment-6148542820). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-13T15:01:56Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #4593 | `core/relinker/cli/include/Cli.hpp` [L26](https://github.com/boykopovar/AnyPS5/blob/c7beafd21a70503b6050766ad60263bc064ee5c2/core/relinker/cli/include/Cli.hpp#L26)<br>`core/relinker/cli/src/CliArgs.cpp` [L13](https://github.com/boykopovar/AnyPS5/blob/c7beafd21a70503b6050766ad60263bc064ee5c2/core/relinker/cli/src/CliArgs.cpp#L13), [L126](https://github.com/boykopovar/AnyPS5/blob/c7beafd21a70503b6050766ad60263bc064ee5c2/core/relinker/cli/src/CliArgs.cpp#L126)<br>`docs/user/USAGE.md` [L53](https://github.com/boykopovar/AnyPS5/blob/c7beafd21a70503b6050766ad60263bc064ee5c2/docs/user/USAGE.md#L53) | | | | #4774 | `core/relinker/elfpatcher/include/elfpatcher/windows/WindowsEntryStubBuilder.hpp` [L23](https://github.com/boykopovar/AnyPS5/blob/c7beafd21a70503b6050766ad60263bc064ee5c2/core/relinker/elfpatcher/include/elfpatcher/windows/WindowsEntryStubBuilder.hpp#L23)<br>`core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp` [L69](https://github.com/boykopovar/AnyPS5/blob/c7beafd21a70503b6050766ad60263bc064ee5c2/core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp#L69)<br>`core/relinker/elfpatcher/src/windows/WindowsPePatcher.cpp` [L90](https://github.com/boykopovar/AnyPS5/blob/c7beafd21a70503b6050766ad60263bc064ee5c2/core/relinker/elfpatcher/src/windows/WindowsPePatcher.cpp#L90) | | | | #4780 | `docs/user/USAGE.md` [L159](https://github.com/boykopovar/AnyPS5/blob/c7beafd21a70503b6050766ad60263bc064ee5c2/docs/user/USAGE.md#L159) | | | | #5021 | `docs/dev/debt/relinker.md` [L15](https://github.com/boykopovar/AnyPS5/blob/c7beafd21a70503b6050766ad60263bc064ee5c2/docs/dev/debt/relinker.md#L15) | | | | #5052 | `core/relinker/cli/src/CliArgs.cpp` [L13](https://github.com/boykopovar/AnyPS5/blob/c7beafd21a70503b6050766ad60263bc064ee5c2/core/relinker/cli/src/CliArgs.cpp#L13) | | | | #5087 | `docs/dev/debt/libc.md` [L67](https://github.com/boykopovar/AnyPS5/blob/c7beafd21a70503b6050766ad60263bc064ee5c2/docs/dev/debt/libc.md#L67) | | | | #5229 | `core/relinker/domain/include/domain/Types.hpp` [L64](https://github.com/boykopovar/AnyPS5/blob/c7beafd21a70503b6050766ad60263bc064ee5c2/core/relinker/domain/include/domain/Types.hpp#L64)<br>`core/relinker/relinker/src/output/SysVDynamicSectionBuilder.cpp` [L96](https://github.com/boykopovar/AnyPS5/blob/c7beafd21a70503b6050766ad60263bc064ee5c2/core/relinker/relinker/src/output/SysVDynamicSectionBuilder.cpp#L96) | | |
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!4547
No description provided.