fix(relinker): run guest preinit arrays before the module initializer #6112

Merged
ayam04 merged 4 commits from upstream-pr/6112 into main 2026-10-18 18:33:14 +00:00
ayam04 commented 2026-10-14 22:36:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ayam04 on 2026-10-14T22:36:33Z.


What

Converted guest modules never run their DT_PREINIT_ARRAY entries. The reader validates the array and rejects one without an initializer, and the unused-NID filter keeps the array's targets as roots, but no writer emitted or ran it: the Linux output only tags DT_INIT_ARRAY/DT_FINI_ARRAY, and the macOS and Windows startup stubs only call the initializer and the init array.

ld.so runs a shared object's preinit array only for the main executable (_dl_init reads it from main_map), so the Linux output cannot use the tag. The preinit entries now run from the module's DT_INIT stub before the initializer, with zeroed argument registers and a null check per slot, the way the init array entries are called. The macOS and Windows startup stubs call them before the initializer. A deferred Windows module exports a thunk as its .ginit entry that runs the preinit entries before the initializer, since the deferred loader's slot list runs after the entry. The reader records the entries like the init and fini arrays.

A module with a preinit array and no initializer is still rejected: the Linux stub that would run the entries exists only with one.

Tested

Windows 11, WinLibs GCC 15.2.0 r7, Release, relinker-only build (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON, CMake 4.4.4, Ninja):

  • python3 tools/check_conventions.py --base origin/main: all checks passed
  • full ctest: 168/168 passed (167 on main plus the new guest_preinit)
  • guest_preinit converts a synthetic module with a two-entry preinit array whose entries record their calls:
    • Linux output: the DT_INIT stub runs the entries before the initializer, and the static check decodes the stub and its branch targets. On a Linux host the converted module loads and records (0, 0) for each preinit entry, then the initializer with the pending arguments, then its return value, in order.
    • macOS output: the startup stub calls both entries, with the branch targets resolving to the array slots.
    • Windows output: the startup stub calls both entries before the initializer; the converted app runs natively and exits 42 only when the preinit entries ran before the initializer (the fixture's initializer faults otherwise).
    • Windows deferred output: the .ginit entry RVA is the emitted thunk, its slots hold the array slots and the initializer, and the base relocations cover the slots.
  • Negative control: reverting the startup wiring fails the test (0x12840 is missing from the entry code).

Linux native execution of the new test runs in the relinker matrix on Linux hosts; it was not run here (Windows host only), and the other relinker tests skip native execution on Windows as on main.

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 under Silent stubs in their library's technical debt file
  • 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: #5614, #6328, #6586
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4865516658 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6112). Originally posted by [ayam04](https://github.com/ayam04) on 2026-10-14T22:36:33Z. --- ### What Converted guest modules never run their `DT_PREINIT_ARRAY` entries. The reader validates the array and rejects one without an initializer, and the unused-NID filter keeps the array's targets as roots, but no writer emitted or ran it: the Linux output only tags `DT_INIT_ARRAY`/`DT_FINI_ARRAY`, and the macOS and Windows startup stubs only call the initializer and the init array. `ld.so` runs a shared object's preinit array only for the main executable (`_dl_init` reads it from `main_map`), so the Linux output cannot use the tag. The preinit entries now run from the module's `DT_INIT` stub before the initializer, with zeroed argument registers and a null check per slot, the way the init array entries are called. The macOS and Windows startup stubs call them before the initializer. A deferred Windows module exports a thunk as its `.ginit` entry that runs the preinit entries before the initializer, since the deferred loader's slot list runs after the entry. The reader records the entries like the init and fini arrays. A module with a preinit array and no initializer is still rejected: the Linux stub that would run the entries exists only with one. ### Tested Windows 11, WinLibs GCC 15.2.0 r7, Release, relinker-only build (`-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`, CMake 4.4.4, Ninja): - `python3 tools/check_conventions.py --base origin/main`: all checks passed - full `ctest`: 168/168 passed (167 on `main` plus the new `guest_preinit`) - `guest_preinit` converts a synthetic module with a two-entry preinit array whose entries record their calls: - Linux output: the `DT_INIT` stub runs the entries before the initializer, and the static check decodes the stub and its branch targets. On a Linux host the converted module loads and records `(0, 0)` for each preinit entry, then the initializer with the pending arguments, then its return value, in order. - macOS output: the startup stub calls both entries, with the branch targets resolving to the array slots. - Windows output: the startup stub calls both entries before the initializer; the converted app runs natively and exits 42 only when the preinit entries ran before the initializer (the fixture's initializer faults otherwise). - Windows deferred output: the `.ginit` entry RVA is the emitted thunk, its slots hold the array slots and the initializer, and the base relocations cover the slots. - Negative control: reverting the startup wiring fails the test (`0x12840` is missing from the entry code). Linux native execution of the new test runs in the relinker matrix on Linux hosts; it was not run here (Windows host only), and the other relinker tests skip native execution on Windows as on `main`. ### 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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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: #5614, #6328, #6586 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
ayam04 commented 2026-10-15 03:02:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ayam04 on 2026-10-15T03:02:49Z.


Declared the merge order for the overlap: the checklist now reads Depends on: #5559, #5614. This one touches the same regions as #5559 (core/relinker/CMakeLists.txt, docs/dev/debt/relinker.md) and #5614 (the Linux stub emission in LinuxGuestModuleWriter.cpp), but there is no functional dependency in either direction, so the declaration is just the order: theirs first, and I will rebase this one onto main once either lands.

@JJenkx if you would rather have this one go first, say so and I will flip the declaration.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6183427121 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6112#issuecomment-6183427121). Originally posted by [ayam04](https://github.com/ayam04) on 2026-10-15T03:02:49Z. --- Declared the merge order for the overlap: the checklist now reads `Depends on: #5559, #5614`. This one touches the same regions as #5559 (`core/relinker/CMakeLists.txt`, `docs/dev/debt/relinker.md`) and #5614 (the Linux stub emission in `LinuxGuestModuleWriter.cpp`), but there is no functional dependency in either direction, so the declaration is just the order: theirs first, and I will rebase this one onto `main` once either lands. @JJenkx if you would rather have this one go first, say so and I will flip the declaration.
github-actions[bot] commented 2026-10-15 12:14:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:14:57Z.


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
PR 6684 core/relinker/elfpatcher/src/macos/MacOsGuestModuleWriter.cpp L111-112
PR 7450 core/relinker/elfpatcher/src/macos/MacOsGuestModuleWriter.cpp L111-112
core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp L3, L157, L203
core/relinker/relinker/tests/test_guest_preinit.py
docs/dev/debt/relinker.md L6
core/relinker/relinker/tests/test_guest_preinit.py

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 2780 core/relinker/CMakeLists.txt
PR 5021 core/relinker/CMakeLists.txt
docs/dev/debt/relinker.md
PR 5925 docs/dev/ARCHITECTURE.md
PR 6044 core/relinker/CMakeLists.txt
docs/dev/ARCHITECTURE.md
PR 6412 docs/dev/ARCHITECTURE.md
PR 6454 docs/dev/ARCHITECTURE.md
PR 6503 docs/dev/ARCHITECTURE.md
PR 6763 docs/dev/ARCHITECTURE.md
PR 6879 core/relinker/CMakeLists.txt
PR 6931 docs/dev/ARCHITECTURE.md
PR 6946 docs/dev/ARCHITECTURE.md
PR 6947 docs/dev/ARCHITECTURE.md
PR 6977 core/relinker/CMakeLists.txt
core/relinker/domain/include/domain/GuestRuntime.hpp
core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp
core/relinker/elfpatcher/src/macos/MacOsGuestModuleWriter.cpp
core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp
core/relinker/relinker/include/relinker/guest/GuestImage.hpp
core/relinker/relinker/src/guest/GuestImageReader.cpp
docs/dev/debt/relinker.md
PR 7173 core/relinker/CMakeLists.txt
docs/dev/debt/relinker.md
PR 7332 core/relinker/CMakeLists.txt
core/relinker/elfpatcher/include/elfpatcher/macos/MacOsImage.hpp
core/relinker/elfpatcher/src/macos/MacOsImage.cpp
docs/dev/debt/relinker.md
PR 7531 core/relinker/CMakeLists.txt
docs/dev/debt/relinker.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191800503 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6112#issuecomment-6191800503). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T12:14:57Z. --- <!-- 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 | |---|---|---|---| | [PR 6684](https://www.github.com/boykopovar/AnyPS5/pull/6684) | `core/relinker/elfpatcher/src/macos/MacOsGuestModuleWriter.cpp` [L111-112](https://github.com/boykopovar/AnyPS5/blob/61d3d3080c372dd0b6adcf4dc13247108495d763/core/relinker/elfpatcher/src/macos/MacOsGuestModuleWriter.cpp#L111-L112) | | | | [PR 7450](https://www.github.com/boykopovar/AnyPS5/pull/7450) | `core/relinker/elfpatcher/src/macos/MacOsGuestModuleWriter.cpp` [L111-112](https://github.com/boykopovar/AnyPS5/blob/61d3d3080c372dd0b6adcf4dc13247108495d763/core/relinker/elfpatcher/src/macos/MacOsGuestModuleWriter.cpp#L111-L112)<br>`core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp` [L3](https://github.com/boykopovar/AnyPS5/blob/61d3d3080c372dd0b6adcf4dc13247108495d763/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp#L3), [L157](https://github.com/boykopovar/AnyPS5/blob/61d3d3080c372dd0b6adcf4dc13247108495d763/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp#L157), [L203](https://github.com/boykopovar/AnyPS5/blob/61d3d3080c372dd0b6adcf4dc13247108495d763/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp#L203)<br>`core/relinker/relinker/tests/test_guest_preinit.py` <br>`docs/dev/debt/relinker.md` [L6](https://github.com/boykopovar/AnyPS5/blob/61d3d3080c372dd0b6adcf4dc13247108495d763/docs/dev/debt/relinker.md#L6) | | `core/relinker/relinker/tests/test_guest_preinit.py` | Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 2780](https://www.github.com/boykopovar/AnyPS5/pull/2780) | `core/relinker/CMakeLists.txt` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/relinker/CMakeLists.txt`<br>`docs/dev/debt/relinker.md` | | [PR 5925](https://www.github.com/boykopovar/AnyPS5/pull/5925) | `docs/dev/ARCHITECTURE.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/relinker/CMakeLists.txt`<br>`docs/dev/ARCHITECTURE.md` | | [PR 6412](https://www.github.com/boykopovar/AnyPS5/pull/6412) | `docs/dev/ARCHITECTURE.md` | | [PR 6454](https://www.github.com/boykopovar/AnyPS5/pull/6454) | `docs/dev/ARCHITECTURE.md` | | [PR 6503](https://www.github.com/boykopovar/AnyPS5/pull/6503) | `docs/dev/ARCHITECTURE.md` | | [PR 6763](https://www.github.com/boykopovar/AnyPS5/pull/6763) | `docs/dev/ARCHITECTURE.md` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/relinker/CMakeLists.txt` | | [PR 6931](https://www.github.com/boykopovar/AnyPS5/pull/6931) | `docs/dev/ARCHITECTURE.md` | | [PR 6946](https://www.github.com/boykopovar/AnyPS5/pull/6946) | `docs/dev/ARCHITECTURE.md` | | [PR 6947](https://www.github.com/boykopovar/AnyPS5/pull/6947) | `docs/dev/ARCHITECTURE.md` | | [PR 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) | `core/relinker/CMakeLists.txt`<br>`core/relinker/domain/include/domain/GuestRuntime.hpp`<br>`core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp`<br>`core/relinker/elfpatcher/src/macos/MacOsGuestModuleWriter.cpp`<br>`core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp`<br>`core/relinker/relinker/include/relinker/guest/GuestImage.hpp`<br>`core/relinker/relinker/src/guest/GuestImageReader.cpp`<br>`docs/dev/debt/relinker.md` | | [PR 7173](https://www.github.com/boykopovar/AnyPS5/pull/7173) | `core/relinker/CMakeLists.txt`<br>`docs/dev/debt/relinker.md` | | [PR 7332](https://www.github.com/boykopovar/AnyPS5/pull/7332) | `core/relinker/CMakeLists.txt`<br>`core/relinker/elfpatcher/include/elfpatcher/macos/MacOsImage.hpp`<br>`core/relinker/elfpatcher/src/macos/MacOsImage.cpp`<br>`docs/dev/debt/relinker.md` | | [PR 7531](https://www.github.com/boykopovar/AnyPS5/pull/7531) | `core/relinker/CMakeLists.txt`<br>`docs/dev/debt/relinker.md` |
ayam04 commented 2026-10-16 02:57:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ayam04 on 2026-10-16T02:57:55Z.


Re-ran after #5559 merged and the overlap list refreshed: this now shares docs/dev/debt/relinker.md with #6328 and LinuxGuestModuleWriter.cpp plus relinker.md with #6586, so the checklist reads Depends on: #5614, #6328, #6586 (#5559 dropped now that it has merged). The declaration is merge order only, not a functional dependency: I will rebase this onto main once either lands. @aaro56 @JJenkx if you would rather have this one go first, say so and I will flip the declaration.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6207015894 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6112#issuecomment-6207015894). Originally posted by [ayam04](https://github.com/ayam04) on 2026-10-16T02:57:55Z. --- Re-ran after #5559 merged and the overlap list refreshed: this now shares `docs/dev/debt/relinker.md` with #6328 and `LinuxGuestModuleWriter.cpp` plus `relinker.md` with #6586, so the checklist reads `Depends on: #5614, #6328, #6586` (#5559 dropped now that it has merged). The declaration is merge order only, not a functional dependency: I will rebase this onto `main` once either lands. @aaro56 @JJenkx if you would rather have this one go first, say so and I will flip the declaration.
SandstromPL commented 2026-10-16 04:50:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SandstromPL on 2026-10-16T04:50:00Z.


@ayam04, a heads-up about an overlap with #6684. That PR treats a DT_INIT of 0 as the module initializer: Until Dawn's bundled libc.prx and libSceFios2.prx both have tag 12 present with value 0 (SuperCatElvis on #3093). It adds GuestImage::HasInit, set when tag 12 is present, and uses it in place of Init != 0 / Init == 0.

Merged with bf4a202ac:

  • MacOsGuestModuleWriter.cpp conflicts at L112. Your new PreInitArrayRvas line sits next to the input.InitRva line that #6684 changes. Keeping both lines, with input.InitRva = guest.HasInit ? image.GetRva(guest.Init) : 0;, resolves it.
  • WindowsGuestModuleWriter.cpp L160, in the deferred .ginit thunk, merges cleanly but still tests guest.Init == 0. A --module-dir module shaped like libc.prx (DT_INIT 0 and a one-entry preinit array) then fails with Guest preinit array has no initializer. It needs if (!guest.HasInit). The Linux check at L156 is fine as it is, because it tests the stub address.

With both changes, the relinker-only suite passes 184/184 on Ubuntu 24.04 with GCC 13.3, including guest_preinit and #6684's guest_zero_initializer. With only the conflict resolved, guest_zero_initializer fails on that deferred case. Whichever of the two lands second makes the change; if yours goes first, I'll make it in #6684.

An open question, not a finding: the loaders I checked start a module only through DT_INIT. fpPS4's kern digest_dynamic ignores DT_PREINIT_ARRAY (subr_dynlib.pas L1594-L1595), and shadPS4 calls base + DT_INIT (module.cpp L104). If libc.prx's DT_INIT code walks its own preinit array, running the entries from the stub as well would run them twice. I've asked SuperCatElvis on #6684 to disassemble libc.prx from 0 to the PLT at 0xc8, which should answer it.

AI Assisted

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6208330680 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6112#issuecomment-6208330680). Originally posted by [SandstromPL](https://github.com/SandstromPL) on 2026-10-16T04:50:00Z. --- @ayam04, a heads-up about an overlap with #6684. That PR treats a DT_INIT of 0 as the module initializer: Until Dawn's bundled `libc.prx` and `libSceFios2.prx` both have tag 12 present with value 0 ([SuperCatElvis on #3093](https://github.com/boykopovar/AnyPS5/issues/3093#issuecomment-6202309687)). It adds `GuestImage::HasInit`, set when tag 12 is present, and uses it in place of `Init != 0` / `Init == 0`. Merged with bf4a202ac: - `MacOsGuestModuleWriter.cpp` conflicts at L112. Your new `PreInitArrayRvas` line sits next to the `input.InitRva` line that #6684 changes. Keeping both lines, with `input.InitRva = guest.HasInit ? image.GetRva(guest.Init) : 0;`, resolves it. - `WindowsGuestModuleWriter.cpp` [L160](https://github.com/boykopovar/AnyPS5/blob/bf4a202acccba307d1e5519bebf124100efc32a2/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp#L160), in the deferred `.ginit` thunk, merges cleanly but still tests `guest.Init == 0`. A `--module-dir` module shaped like `libc.prx` (DT_INIT 0 and a one-entry preinit array) then fails with `Guest preinit array has no initializer`. It needs `if (!guest.HasInit)`. The Linux check at L156 is fine as it is, because it tests the stub address. With both changes, the relinker-only suite passes 184/184 on Ubuntu 24.04 with GCC 13.3, including `guest_preinit` and #6684's `guest_zero_initializer`. With only the conflict resolved, `guest_zero_initializer` fails on that deferred case. Whichever of the two lands second makes the change; if yours goes first, I'll make it in #6684. An open question, not a finding: the loaders I checked start a module only through DT_INIT. fpPS4's kern `digest_dynamic` ignores DT_PREINIT_ARRAY ([subr_dynlib.pas L1594-L1595](https://github.com/red-prig/fpPS4/blob/036a611bef2ca35da838c4d57d8502907ed5e4a1/sys/kern/subr_dynlib.pas#L1594-L1595)), and shadPS4 calls base + DT_INIT ([module.cpp L104](https://github.com/shadps4-emu/shadPS4/blob/5aa7ffa3f4f21bba827d7865ed8f69f98f993542/src/core/module.cpp#L104)). If `libc.prx`'s DT_INIT code walks its own preinit array, running the entries from the stub as well would run them twice. I've asked SuperCatElvis on #6684 to disassemble `libc.prx` from 0 to the PLT at 0xc8, which should answer it. AI Assisted
adriadam10 commented 2026-10-16 23:28:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T23:28:41Z.


Checked the Linux stub (stack alignment, rel32 ranges, null check per slot), the macOS and Windows startup calls and the Windows deferred thunk (size, slots, relocations). Relinker ctest scope is green, guest_preinit runs the converted module natively on Linux in the right order and fails when the code change is reverted; docs updated. Merging once the full Build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6226606157 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6112#issuecomment-6226606157). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-16T23:28:41Z. --- Checked the Linux stub (stack alignment, rel32 ranges, null check per slot), the macOS and Windows startup calls and the Windows deferred thunk (size, slots, relocations). Relinker ctest scope is green, `guest_preinit` runs the converted module natively on Linux in the right order and fails when the code change is reverted; docs updated. Merging once the full Build is green.
ayam04 commented 2026-10-17 02:42:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ayam04 on 2026-10-17T02:42:44Z.


Thanks for the heads-up and for testing the combination, @SandstromPL. Agreed on both edits and on the land-order split:

  • MacOsGuestModuleWriter.cpp: keep the new PreInitArrayRvas line and take input.InitRva = guest.HasInit ? image.GetRva(guest.Init) : 0;.
  • WindowsGuestModuleWriter.cpp: in the deferred .ginit thunk, the preinit guard becomes if (!guest.HasInit) throw Domain::RelinkerException("Guest preinit array has no initializer");. That guard is this PR's line, so it is the one piece the merge leaves on us; the Io::AppendU32(lifecycle.Data, ...) first word and runtime.InitRva come from #6684 cleanly.
  • The Linux stub check stays as-is, since it tests the stub address.

I'm not pushing anything right now so the green CI on 41cfc8b stays valid. If #6684 lands first I'll rebase onto main, apply exactly the above, and re-run the relinker scope before pushing.

On the open question: agreed it is the right thing to check. The stub runs the preinit entries with zeroed argument registers and a null check per slot, before the initializer. If the disassembly shows libc.prx's DT_INIT walks its own preinit array, the entries would run twice for that module and I would rather adjust the stub than guess, so I will follow SuperCatElvis's answer on #6684.

@adriadam10 the full Build on 41cfc8b finished green, along with Conventions, so nothing is blocking the merge from our side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6228636840 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6112#issuecomment-6228636840). Originally posted by [ayam04](https://github.com/ayam04) on 2026-10-17T02:42:44Z. --- Thanks for the heads-up and for testing the combination, @SandstromPL. Agreed on both edits and on the land-order split: - `MacOsGuestModuleWriter.cpp`: keep the new `PreInitArrayRvas` line and take `input.InitRva = guest.HasInit ? image.GetRva(guest.Init) : 0;`. - `WindowsGuestModuleWriter.cpp`: in the deferred `.ginit` thunk, the preinit guard becomes `if (!guest.HasInit) throw Domain::RelinkerException("Guest preinit array has no initializer");`. That guard is this PR's line, so it is the one piece the merge leaves on us; the `Io::AppendU32(lifecycle.Data, ...)` first word and `runtime.InitRva` come from #6684 cleanly. - The Linux stub check stays as-is, since it tests the stub address. I'm not pushing anything right now so the green CI on `41cfc8b` stays valid. If #6684 lands first I'll rebase onto `main`, apply exactly the above, and re-run the relinker scope before pushing. On the open question: agreed it is the right thing to check. The stub runs the preinit entries with zeroed argument registers and a null check per slot, before the initializer. If the disassembly shows `libc.prx`'s `DT_INIT` walks its own preinit array, the entries would run twice for that module and I would rather adjust the stub than guess, so I will follow SuperCatElvis's answer on #6684. @adriadam10 the full Build on `41cfc8b` finished green, along with Conventions, so nothing is blocking the merge from our side.
SandstromPL commented 2026-10-17 02:51:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SandstromPL on 2026-10-17T02:51:12Z.


Thanks, @ayam04, that matches what I tested. I merged main and this PR into #6684 and resolved the macOS line that way, with the thunk guard on !guest.HasInit. The relinker suite passes 188/188, including guest_preinit and guest_zero_initializer. Without the guard change, guest_zero_initializer fails on its --module-dir preinit case. If this PR lands first, I'll push exactly that to #6684, so there's nothing for you to do in that order.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6228710900 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6112#issuecomment-6228710900). Originally posted by [SandstromPL](https://github.com/SandstromPL) on 2026-10-17T02:51:12Z. --- Thanks, @ayam04, that matches what I tested. I merged `main` and this PR into #6684 and resolved the macOS line that way, with the thunk guard on `!guest.HasInit`. The relinker suite passes 188/188, including `guest_preinit` and `guest_zero_initializer`. Without the guard change, `guest_zero_initializer` fails on its `--module-dir` preinit case. If this PR lands first, I'll push exactly that to #6684, so there's nothing for you to do in that order. AI-assisted: yes (Claude Code)
milescoviello commented 2026-10-17 14:27:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-17T14:27:15Z.


A note for when this is rebased: #7147 (now on main) added add_asan_test_options, so the CPU-side tests pass in an ANYPS5_ENABLE_ASAN build. guest_module_args, guest_deferred_dependencies and guest_module_fini load the instrumented guest_module_args_host.prx into Python, so they get verify_asan_link_order=0. The calls are grouped right after guest_module_fini's add_test. guest_preinit loads the same host, so in an ASan build it will need the same line:

        add_asan_test_options(guest_preinit verify_asan_link_order=0)

Without it, it fails there with ASan runtime does not come first in initial library list. Normal builds are unaffected, because the function does nothing outside ASan builds.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6235074850 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6112#issuecomment-6235074850). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-17T14:27:15Z. --- A note for when this is rebased: #7147 (now on `main`) added `add_asan_test_options`, so the CPU-side tests pass in an `ANYPS5_ENABLE_ASAN` build. `guest_module_args`, `guest_deferred_dependencies` and `guest_module_fini` load the instrumented `guest_module_args_host.prx` into Python, so they get `verify_asan_link_order=0`. The calls are grouped right after `guest_module_fini`'s `add_test`. `guest_preinit` loads the same host, so in an ASan build it will need the same line: ```cmake add_asan_test_options(guest_preinit verify_asan_link_order=0) ``` Without it, it fails there with `ASan runtime does not come first in initial library list`. Normal builds are unaffected, because the function does nothing outside ASan builds. AI-assisted: yes (Claude Code)
ayam04 commented 2026-10-18 02:44:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ayam04 on 2026-10-18T02:44:59Z.


Thanks @milescoviello. Rebased on main in da00f6f3: guest_preinit now goes through add_asan_test_options(guest_preinit verify_asan_link_order=0), the same as the sibling module tests, so it should pass in an ANYPS5_ENABLE_ASAN build now. No other changes in the rebase.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6244856881 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6112#issuecomment-6244856881). Originally posted by [ayam04](https://github.com/ayam04) on 2026-10-18T02:44:59Z. --- Thanks @milescoviello. Rebased on main in da00f6f3: guest_preinit now goes through add_asan_test_options(guest_preinit verify_asan_link_order=0), the same as the sibling module tests, so it should pass in an ANYPS5_ENABLE_ASAN build now. No other changes in the rebase.
SandstromPL commented 2026-10-18 07:15:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SandstromPL on 2026-10-18T07:15:45Z.


@ayam04 one thing in the Windows preinit thunk (.ginitc, WindowsGuestModuleWriter.cpp), checked on da00f6f3a.

The thunk pushes r12, r13, r14 but pops them in the same order. This is test_guest_preinit.py's flag_module, converted as a deferred --module-dir module:

140014000:  41 54   push %r12
140014002:  41 55   push %r13
140014004:  41 56   push %r14
...
14001404f:  41 5c   pop  %r12
140014051:  41 5d   pop  %r13
140014053:  41 5e   pop  %r14
140014055:  c3      ret

So the caller gets its r12 and r14 back swapped. Both are callee-saved under SysV and Win64. The only caller is InitializeDeferredModule in DynamicLoader.cpp, which calls the entry word as an APS5_VABI function and keeps using base, table, count and the lock state after the call. If the compiler keeps any of them in r12 or r14, opening a deferred module that has a preinit array corrupts the loader's state. The startup path is not affected: it calls the preinit entries directly.

The fix keeps the length, so codeSize, the slot RVAs and the test offsets stay the same:

--- a/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp
+++ b/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp
@@ -172,7 +172,7 @@
             code.Emit({0x4c, 0x89, 0xe7, 0x4c, 0x89, 0xee, 0x4c, 0x89, 0xf2});
             code.Rip({0x48, 0x8b, 0x05}, slotsRva + static_cast<std::uint32_t>(guest.PreInitArray.size()) * 8);
             code.Emit({0xff, 0xd0});
-            code.Emit({0x41, 0x5c, 0x41, 0x5d, 0x41, 0x5e, 0xc3});
+            code.Emit({0x41, 0x5e, 0x41, 0x5d, 0x41, 0x5c, 0xc3});
             auto thunk = code.TakeBytes();
             while (thunk.size() % 8 != 0) thunk.push_back(0x90);
             for (const auto slot : guest.PreInitArray) {
--- a/core/relinker/relinker/tests/test_guest_preinit.py
+++ b/core/relinker/relinker/tests/test_guest_preinit.py
@@ -173,7 +173,7 @@
     restore = 15 + 23 * len(slots)
     assert code[restore:restore + 12] == b'\x4c\x89\xe7\x4c\x89\xee\x4c\x89\xf2\x48\x8b\x05', code[restore:restore + 16]
     assert code[restore + 16:restore + 18] == b'\xff\xd0', code[restore:restore + 25]
-    assert code[restore + 18:restore + 25] == b'\x41\x5c\x41\x5d\x41\x5e\xc3', code[restore:restore + 25]
+    assert code[restore + 18:restore + 25] == b'\x41\x5e\x41\x5d\x41\x5c\xc3', code[restore:restore + 25]
     slots_offset = code_size + (8 - code_size % 8) % 8
     slots_rva = ginitc_rva + slots_offset
     for index, slot in enumerate(slots):

With that applied (Ubuntu 24.04, GCC 13.3, relinker-only Release), the converted thunk ends in pop %r14; pop %r13; pop %r12; ret and all 196 relinker tests pass. Without the test change, guest_preinit fails at the pinned bytes. Not run on Windows.

No test runs the thunk today: check_windows_deferred matches bytes, the nt run takes the startup path, and GuestDeferredModuleFixture.S has no thunk. A Windows test that opens a converted preinit module through dlopen would catch this kind of thing, if you want to add one.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6247036705 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6112#issuecomment-6247036705). Originally posted by [SandstromPL](https://github.com/SandstromPL) on 2026-10-18T07:15:45Z. --- @ayam04 one thing in the Windows preinit thunk (`.ginitc`, WindowsGuestModuleWriter.cpp), checked on da00f6f3a. The thunk pushes r12, r13, r14 but pops them in the same order. This is `test_guest_preinit.py`'s `flag_module`, converted as a deferred `--module-dir` module: ``` 140014000: 41 54 push %r12 140014002: 41 55 push %r13 140014004: 41 56 push %r14 ... 14001404f: 41 5c pop %r12 140014051: 41 5d pop %r13 140014053: 41 5e pop %r14 140014055: c3 ret ``` So the caller gets its r12 and r14 back swapped. Both are callee-saved under SysV and Win64. The only caller is `InitializeDeferredModule` in DynamicLoader.cpp, which calls the entry word as an `APS5_VABI` function and keeps using `base`, `table`, `count` and the lock state after the call. If the compiler keeps any of them in r12 or r14, opening a deferred module that has a preinit array corrupts the loader's state. The startup path is not affected: it calls the preinit entries directly. The fix keeps the length, so `codeSize`, the slot RVAs and the test offsets stay the same: ```diff --- a/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp +++ b/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp @@ -172,7 +172,7 @@ code.Emit({0x4c, 0x89, 0xe7, 0x4c, 0x89, 0xee, 0x4c, 0x89, 0xf2}); code.Rip({0x48, 0x8b, 0x05}, slotsRva + static_cast<std::uint32_t>(guest.PreInitArray.size()) * 8); code.Emit({0xff, 0xd0}); - code.Emit({0x41, 0x5c, 0x41, 0x5d, 0x41, 0x5e, 0xc3}); + code.Emit({0x41, 0x5e, 0x41, 0x5d, 0x41, 0x5c, 0xc3}); auto thunk = code.TakeBytes(); while (thunk.size() % 8 != 0) thunk.push_back(0x90); for (const auto slot : guest.PreInitArray) { --- a/core/relinker/relinker/tests/test_guest_preinit.py +++ b/core/relinker/relinker/tests/test_guest_preinit.py @@ -173,7 +173,7 @@ restore = 15 + 23 * len(slots) assert code[restore:restore + 12] == b'\x4c\x89\xe7\x4c\x89\xee\x4c\x89\xf2\x48\x8b\x05', code[restore:restore + 16] assert code[restore + 16:restore + 18] == b'\xff\xd0', code[restore:restore + 25] - assert code[restore + 18:restore + 25] == b'\x41\x5c\x41\x5d\x41\x5e\xc3', code[restore:restore + 25] + assert code[restore + 18:restore + 25] == b'\x41\x5e\x41\x5d\x41\x5c\xc3', code[restore:restore + 25] slots_offset = code_size + (8 - code_size % 8) % 8 slots_rva = ginitc_rva + slots_offset for index, slot in enumerate(slots): ``` With that applied (Ubuntu 24.04, GCC 13.3, relinker-only Release), the converted thunk ends in `pop %r14; pop %r13; pop %r12; ret` and all 196 relinker tests pass. Without the test change, `guest_preinit` fails at the pinned bytes. Not run on Windows. No test runs the thunk today: `check_windows_deferred` matches bytes, the `nt` run takes the startup path, and `GuestDeferredModuleFixture.S` has no thunk. A Windows test that opens a converted preinit module through `dlopen` would catch this kind of thing, if you want to add one. AI-assisted: yes (Claude Code)
ayam04 commented 2026-10-18 15:43:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ayam04 on 2026-10-18T15:43:46Z.


Good catch @SandstromPL, and thanks for the repro. Fixed in f294956: the thunk now ends pop %r14; pop %r13; pop %r12; ret instead of popping in push order, and test_guest_preinit pins the new bytes. The two hunks are exactly what you posted.

The failure mode you describe is the one that matters here: InitializeDeferredModule in DynamicLoader.cpp keeps base, table, count and the lock state live across the call into the thunk, and InitializeDeferredModule is a normal C++ function, so any of those living in r12 or r14 comes back swapped after the first deferred module with a preinit array is opened. Win64 and SysV both treat r12 and r14 as nonvolatile, so the thunk has to restore them in reverse push order.

Length is unchanged, so codeSize, the slot RVAs and the test offsets are the same, and the startup path from .ginit is untouched.

Verified locally on Windows (mingw g++, relinker Release): ctest -R ^guest_preinit$ passes and the wider guest/windows/amd64 set is 38/38. The Linux byte dump matches your 14001404f: pop %r14 / pop %r13 / pop %r12 / ret.

On a dlopen test for the converted preinit module: agreed that nothing runs the thunk today. Happy to add one in this PR if @milescoviello wants it here, otherwise it can land as a follow-up so this stays a two-hunk fix.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6251840113 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6112#issuecomment-6251840113). Originally posted by [ayam04](https://github.com/ayam04) on 2026-10-18T15:43:46Z. --- Good catch @SandstromPL, and thanks for the repro. Fixed in f294956: the thunk now ends `pop %r14; pop %r13; pop %r12; ret` instead of popping in push order, and `test_guest_preinit` pins the new bytes. The two hunks are exactly what you posted. The failure mode you describe is the one that matters here: `InitializeDeferredModule` in DynamicLoader.cpp keeps `base`, `table`, `count` and the lock state live across the call into the thunk, and `InitializeDeferredModule` is a normal C++ function, so any of those living in r12 or r14 comes back swapped after the first deferred module with a preinit array is opened. Win64 and SysV both treat r12 and r14 as nonvolatile, so the thunk has to restore them in reverse push order. Length is unchanged, so `codeSize`, the slot RVAs and the test offsets are the same, and the startup path from `.ginit` is untouched. Verified locally on Windows (mingw g++, relinker Release): `ctest -R ^guest_preinit$` passes and the wider guest/windows/amd64 set is 38/38. The Linux byte dump matches your `14001404f: pop %r14 / pop %r13 / pop %r12 / ret`. On a dlopen test for the converted preinit module: agreed that nothing runs the thunk today. Happy to add one in this PR if @milescoviello wants it here, otherwise it can land as a follow-up so this stays a two-hunk fix.
milescoviello commented 2026-10-18 17:53:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-18T17:53:03Z.


@ayam04 the dlopen test point came from @SandstromPL, not me, so it's their call. For my part, a follow-up is fine and keeps this a two-hunk fix.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6253169478 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6112#issuecomment-6253169478). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-18T17:53:03Z. --- @ayam04 the dlopen test point came from @SandstromPL, not me, so it's their call. For my part, a follow-up is fine and keeps this a two-hunk fix. AI-assisted: yes (Claude Code)
oneandonlydean commented 2026-10-18 18:33:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-18T18:33:27Z.


Merged while @adriadam10 is away: the review passed and full CI is green on this head.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6253584288 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6112#issuecomment-6253584288). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-18T18:33:27Z. --- Merged while @adriadam10 is away: the review passed and full CI is green on this head.
babyhuey commented 2026-10-18 18:46:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-18T18:46:44Z.


@ayam04 @oneandonlydean since this merge, every title I run on Linux aborts at start-up with Thread destructor callback is already registered. The first parent of the merge, d7a455a90, is fine. Details and the bisect are in #7636.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6253720632 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6112#issuecomment-6253720632). Originally posted by [babyhuey](https://github.com/babyhuey) on 2026-10-18T18:46:44Z. --- @ayam04 @oneandonlydean since this merge, every title I run on Linux aborts at start-up with `Thread destructor callback is already registered`. The first parent of the merge, `d7a455a90`, is fine. Details and the bisect are in #7636. AI-assisted: yes
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!6112
No description provided.