Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
oneandonlydean:fix/relinker-linux-argv
Oct 3, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
oneandonlydean:fix/relinker-linux-argv

Conversation

@oneandonlydean

@oneandonlydean oneandonlydean commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

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

The Linux entry stub built a copy of the entry parameter block holding
argc and a pointer to the host argv array, so the guest read that pointer
as argv[0] and uninitialised stack as argv[1..]. The process stack at
entry already has the guest's layout (argc as a qword, then the argv
pointers and a null), so pass rsp itself.

Test: linux_entry_argv relinks a fixture whose entry checks argc and
argv and runs the output on Linux x86-64.
@oneandonlydean
oneandonlydean force-pushed the fix/relinker-linux-argv branch from f736d9f to d8b8aaf Compare October 3, 2026 00:50
@QDarkRex

QDarkRex commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Collaborator Author

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.

oneandonlydean added a commit to oneandonlydean/AnyPS5 that referenced this pull request Oct 3, 2026
…heir PR heads

The code of the three open PRs is unchanged on their heads apart from the
opt-outs and comments astrobot keeps. Their heads add tests: boykopovar#415's
EvaluatedValues table test (and drops an unused include), boykopovar#417's flip test
releases its GPU work without an interrupt select, and boykopovar#418's
linux_entry_argv relinks a fixture whose entry checks argc and argv.
@oneandonlydean
oneandonlydean merged commit d8243c3 into boykopovar:main Oct 3, 2026
3 checks passed
@oneandonlydean
oneandonlydean deleted the fix/relinker-linux-argv branch October 3, 2026 22:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants