fix(relinker): hand the guest the real argv on Linux #418

Merged
oneandonlydean merged 1 commit from upstream-pr/418 into main 2026-10-03 22:30:37 +00:00
oneandonlydean commented 2026-10-02 22:34:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-02T22:34:52Z.


What

The Linux entry stub handed the guest's _start the wrong argv. The guest expects a pointer to its entry parameter block, {int argc; pad; const char* argv[]}, with the argv pointers inline after argc. The stub popped argc, built a copy of that block on the stack and stored a pointer to the host argv array in it, so the guest read that pointer as argv[0] and uninitialised stack as argv[1..]: it saw a garbage argv[0] and none of the real arguments.

At process entry the kernel leaves rsp at argc (a qword whose high half is zero), followed by the argv pointers and a null. That is already the guest's layout, so the stub now passes rsp itself in rdi, aligns the stack and calls the entry as before. The constants of the removed instructions are dropped.

Titles that parse their own command line now see the extra arguments; for example PPSA21564 accepts -lvl <level file> to boot straight into a level.

The Windows entry stub already builds the block this way (checked by @QDarkRex below), so only the Linux one changes.

Tested

  • Linux (CachyOS, x86-64): full build with BUILD_TESTING=ON and ctest pass.
  • New test linux_entry_argv (test_linux_entry_argv.py): relinks a fixture whose entry checks argc == 2, argv[0] != NULL, argv[1] == "Z" and argv[2] == NULL, then runs the output on Linux x86-64 with Z (expects the entry's int3) and with Z extra (expects its ud2). On other hosts it only checks the relink, like the Windows tests that run their output only on Windows. Against main's relinker the first run ends in SIGILL instead of SIGTRAP (the guest sees the wrong argv), so the test fails without the fix.
  • PPSA21564: the change has been on my integration branch through repeated passes of my visual regression check (checkpoint frames, flicker, shimmer, video tearing, FPS), and -lvl reaches the requested level.

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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4722170206 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/418). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-02T22:34:52Z. --- ### What The Linux entry stub handed the guest's `_start` the wrong argv. The guest expects a pointer to its entry parameter block, `{int argc; pad; const char* argv[]}`, with the argv pointers inline after argc. The stub popped argc, built a copy of that block on the stack and stored a pointer to the host argv array in it, so the guest read that pointer as `argv[0]` and uninitialised stack as `argv[1..]`: it saw a garbage `argv[0]` and none of the real arguments. At process entry the kernel leaves rsp at argc (a qword whose high half is zero), followed by the argv pointers and a null. That is already the guest's layout, so the stub now passes rsp itself in rdi, aligns the stack and calls the entry as before. The constants of the removed instructions are dropped. Titles that parse their own command line now see the extra arguments; for example PPSA21564 accepts `-lvl <level file>` to boot straight into a level. The Windows entry stub already builds the block this way (checked by @QDarkRex below), so only the Linux one changes. ### Tested - Linux (CachyOS, x86-64): full build with `BUILD_TESTING=ON` and `ctest` pass. - New test `linux_entry_argv` (`test_linux_entry_argv.py`): relinks a fixture whose entry checks `argc == 2`, `argv[0] != NULL`, `argv[1] == "Z"` and `argv[2] == NULL`, then runs the output on Linux x86-64 with `Z` (expects the entry's `int3`) and with `Z extra` (expects its `ud2`). On other hosts it only checks the relink, like the Windows tests that run their output only on Windows. Against main's relinker the first run ends in SIGILL instead of SIGTRAP (the guest sees the wrong argv), so the test fails without the fix. - PPSA21564: the change has been on my integration branch through repeated passes of my visual regression check (checkpoint frames, flicker, shimmer, video tearing, FPS), and `-lvl` reaches the requested level. ### 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) - [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: none - [x] AI-assisted: yes
QDarkRex commented 2026-10-03 04:31:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by QDarkRex on 2026-10-03T04:31:06Z.


I checked the Windows side you mentioned: its entry stub already builds the block the way this PR now does on Linux. It writes argc as a qword, then the UTF-8 argv pointers inline after it, with a null after the last one (from CommandLineToArgvW + WideCharToMultiByte into a zeroed VirtualAlloc block), and passes that block in rdi. I ran your ARGV_CHECK fixture through relinker --windows on main (313de1c) and ran the exe on Windows 11: Z hits the int3, while Z extra, no argument, Y and Zz all hit the ud2. So -lvl should already reach the guest on Windows without a change there.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5965543859 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/418#issuecomment-5965543859). Originally posted by [QDarkRex](https://github.com/QDarkRex) on 2026-10-03T04:31:06Z. --- I checked the Windows side you mentioned: its entry stub already builds the block the way this PR now does on Linux. It writes argc as a qword, then the UTF-8 argv pointers inline after it, with a null after the last one (from `CommandLineToArgvW` + `WideCharToMultiByte` into a zeroed `VirtualAlloc` block), and passes that block in rdi. I ran your `ARGV_CHECK` fixture through `relinker --windows` on main (313de1c) and ran the exe on Windows 11: `Z` hits the `int3`, while `Z extra`, no argument, `Y` and `Zz` all hit the `ud2`. So `-lvl` should already reach the guest on Windows without a change there.
oneandonlydean commented 2026-10-03 06:28:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-03T06:28:34Z.


Thanks for checking the Windows stub and running the fixture on Windows 11. I'll update the description to say the Windows stub already builds the block this way, so only the Linux one needed the fix.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5966362785 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/418#issuecomment-5966362785). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-03T06:28:34Z. --- Thanks for checking the Windows stub and running the fixture on Windows 11. I'll update the description to say the Windows stub already builds the block this way, so only the Linux one needed the fix.
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!418
No description provided.