fix(relinker): share Windows --module-dir modules between executables #7450
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7450"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by SandstromPL on 2026-10-18T07:18:39Z.
What
Rebased onto
main2ed704842, without #6112 or #6684. The previous head stacked on both because all three change the same.ginitblock, and its test borrowed #6112'sflag_module. #6112 was merged and then reverted in #7646: a title's ownlibc.prxalready runs its preinit routine fromDT_INIT, so running the preinit array as well ran it twice (#7636). This branch no longer carries any #6112 code, and its test has its own fixture. The two commits below are now the whole branch. The Windows results under Tested are from the previous head9a82a1b45.METAL GEAR SOLID: Peace Walker (PPSA18216, #5268) ships a launcher (
src/eboot.elf) that starts the game (mgspw/mgspw.elf) withsceSystemServiceLoadExec. Both use the Unity modules inMedia/ModulesandMedia/Plugins. mrfelek reached the beach tutorial on Linux and Windows. On Windows, though, the combined relinkfails with
For that run he relinked the two separately and gave the game the launcher's guest modules. The only module that differs is
Media/Modules/Il2cppUserAssemblies.prx.Why it differs:
--executable(#4786) converts each executable with the full pipeline and requires every guest module to come out byte-identical (main.cpp L234-L243), because one module tree is written for all of them (L311-L315). For a module that one executable needs and another does not, #4786 notes that "on Windows that is an error rather than a second module tree". #4793 had asked about one shared tree with initialization decided at run time; that was never built.--module-dirmodule is deferred unless the converting executable'sDT_NEEDED(or, on Windows, another guest module) requires it (GuestModuleBuilder.cpp L258-L269)..ginittable and the__aps5_guest_initializeexport (WindowsGuestModuleWriter.cpp L150-L157), which also moves the sections after it. The startup initializes the other modules through their handles and skips the deferred ones (WindowsGuestStartup.cpp L30-L37). A latersceKernelLoadStartModuleruns a deferred module's entry and initializers once through the table (DynamicLoader.cpp L113-L162), and does nothing for a module without the export (L119-L120).Il2cppUserAssemblies.prxat startup, andmgspw.elfloads it at run time, so the two conversions write different files. The rejection is pinned in test_executables.py L87-L89. On Linux the same choice only changes the executable'sDT_NEEDED, so the modules match (L90-L94).The workaround also has a side effect, found by reading the code (not run). With the launcher's copy, the game's startup skips the module, and its run-time load finds no table. So
Il2cppUserAssemblies.prx's DT_INIT and init_array never run in the game process on Windows.With this change, the module no longer depends on which executable converts it:
--executableis given, every module of a--module-dirdirectory gets the table, whether this executable defers it or not (GuestRuntime::ExportInitialization).DeferInitializationis still decided per executable. It now only decides what that executable's startup does.ExportInitialization, the Windows writer puts.ginitin a writable section and records its RVA (GuestRuntime::InitializationTableRva).WindowsGuestStartup::Initializestores 0 over the first two words (entry and count) of every table among the modules it initializes. The store ismov rax, [handle]followed bymov qword ptr [rax + table], 0. A latersceKernelLoadStartModuleof such a module then finds{0, 0}and returns, whichDynamicLoaderalready does after it has initialized a module itself (L128-L130). All tables are cleared before the first initializer runs. So if one module's initializer loads another startup module,DynamicLoaderdoes not initialize that module early, before the startup initializes it again.DynamicLoaderruns the entry with the pending start arguments, then the initializers, when the title loads it.For Peace Walker, the launcher clears
Il2cppUserAssemblies.prx's table and initializes it at startup, as before. The game skips it at startup and initializes it when it loads it, which the workaround never did.A writable section keeps the startup's part to one store per module. The alternative is a read-only table that the startup unprotects with
VirtualProtect, which the entry stub does not import today. That is the fallback if a write-copy table causes trouble on Windows.What changes in Windows output with
--executable, besides the fix:--module-dirmodule that every executable initializes at startup now also carries the table, and every startup clears it. Its.edata,.relocand.dllmainsections move by the table's size.--module-dirmodule that every executable defers (for example an unused plugin inMedia/Plugins) keeps its table, now in a writable section.DynamicLoader'sVirtualProtectthen changes a write-copy page instead of a read-only one.Not changed:
--executable: a--module-dirmodule gets the table only when it is deferred, in a read-only section.sce_modulemodules never get the table.ExportInitialization.DynamicLoaderand the table's layout.main.cppstays for any other difference between the conversions.Docs: a paragraph in the "Windows deferred module initialization" section of
ARCHITECTURE.md, and one sentence inUSAGE.md's--executablerow.This PR does not depend on #6684, which changes the same
.ginitblock (guest.HasInitinstead ofguest.Init == 0). The writer marks the section writable and records its RVA after the table's words, not in thePeSectionline, so the two branches don't change adjacent lines.git merge-treemerges them cleanly in either order, to the same tree, onmain2ed704842and onmain3259e4c3e. The full relinker suite passes on the3259e4c3etree with both (210/210).It has two commits:
fix(relinker): share Windows runtime modules between executables that initialize them differently: the change, the relinker tests and the docs.test(libkernel): load a deferred module whose initialization table is writable. This commit is Windows-only. mrfelek ran its tests on Windows (see Tested). It can be dropped on its own; without it, the first commit's debt line about the untested writable table stays.Tested
Ubuntu 24.04, GCC 13.3, CMake 3.28, Ninja 1.11, Python 3.12, Release relinker-only build (
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON), onmain2ed704842. Fullctest: 208/208. The same with-DANYPS5_ENABLE_ASAN=ON(ASAN_OPTIONS=detect_leaks=0): 208/208. The new checks extend existing tests, so the count is the same as onmain.relinker_executables:picker/picker.binneedsMedia/Modules/needed.prxandeboot.bindefers it, used to assert the rejection. It now converts, and the shared module exports the table.--module-dirmodules,needed.prxandsecond.prx, built from a new fixture with a DT_INIT and two init_array entries. The DT_INIT sets a flag, and each init_array entry traps unless the flag is set. The two modules put their DT_INIT and init_array at different addresses and have data segments of different sizes, so their tables land at different RVAs and every store and call can be matched to its module. One executable needs both modules, and the other defers both. The case runs in both orientations, with the picker needing them and witheboot.binneeding them. In each:.ginitand export the table, which holds the module's own DT_INIT RVA, the count 2 and its two slot RVAs;.entrycode has one handle-relative qword store per module and no other. Each store clears that module's table through its own handle slot, and that slot also carries the module's three lifecycle calls;sce_module/bundled.prxhas no table;guest_module_dir: a single-executable deferred module's.ginitis still read-only. Its existing checks that a startup module has no table without--executablepass unchanged.On
main,relinker_executablesfails attest_executables.py:161:With that case removed in a scratch copy, the new case fails at L178 with the same message. The new
guest_module_dircheck passes onmain, becausemainalready writes a read-only.ginitthere.I also ran 19 mutants of the startup, writer and builder code against
relinker_executablesandguest_module_dir, and each one fails at least one assertion:.ginitin a shared module;sce_module;test_guest_module_dir.py:178: a writable.ginit, or the table on every--module-dirmodule, without--executable;Four of them pass when the two modules are built identically, which is why the fixture gives them different layouts: both swaps between the shared modules' slots, and both single-table-RVA clears.
Not covered by a test:
--module-dirmodule that every executable needs at startup;tools/check_conventions.pyfrommainpasses on both commits and on this title and description.On Windows, on this head
9a2d69870:mainc9e6c45deplus the open Peace Walker PRs), so he cherry-picked its two commits. The only conflicts were in linesmainadded later, and theSharedGuestModuleslines went in unchanged.relinker --windows --registry --executable mgspw/mgspw.elf --module-dir Media/Modules --module-dir Media/Pluginsexits 0 and writesapp.exeandmgspw.mgspw.elf.exewith 12 guest modules, wheremainstops withGuest modules convert differently.app.exestartsmgspw.mgspw.elf, and the tutorial and the next mission work. The log has no abort, no exception and noThread destructor callback is already registered.ctest -R guest_deferred_module: the 4 existing tests and the 5 newguest_deferred_module_writable_*ones pass (9/9). They load the deferred-module fixture built a second time with a writable.ginit, and theclearedmode checks that a load after the startup's clear runs neither the entry nor the constructor.ntbranch ofrelinker_executables, so the startup's store into the write-copy.ginitpage, followed by both modules' initializers, has run on a Windows host.The previous head
9a82a1b45, which still carried #6112 and #6684, had the same results (Peace Walker, tests). What changed since then:main.PeSectionline. The output is the same.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T07:31:54Z.
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.
docs/user/USAGE.mdcore/libs/prx/libkernel/CMakeLists.txtdocs/user/USAGE.mdcore/libs/prx/libkernel/CMakeLists.txtdocs/user/USAGE.mddocs/dev/ARCHITECTURE.mddocs/user/USAGE.mdcore/libs/prx/libkernel/CMakeLists.txtcore/libs/tests/GuestDeferredModule.cppcore/libs/tests/GuestDeferredModuleFixture.Sdocs/dev/ARCHITECTURE.mddocs/user/USAGE.mddocs/dev/ARCHITECTURE.mdcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/relinker/relinker/src/guest/GuestModuleBuilder.cppdocs/user/USAGE.mddocs/dev/ARCHITECTURE.mddocs/user/USAGE.mdcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtdocs/user/USAGE.mdcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/ARCHITECTURE.mddocs/user/USAGE.mddocs/dev/ARCHITECTURE.mddocs/user/USAGE.mdcore/libs/prx/libkernel/CMakeLists.txtcore/relinker/domain/include/domain/GuestRuntime.hppcore/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cppcore/relinker/relinker/include/relinker/guest/GuestImage.hppcore/relinker/relinker/src/guest/GuestModuleBuilder.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/ARCHITECTURE.mddocs/user/USAGE.mddocs/dev/ARCHITECTURE.mddocs/dev/ARCHITECTURE.mddocs/user/USAGE.mddocs/dev/ARCHITECTURE.mdcore/libs/prx/libkernel/CMakeLists.txtdocs/user/USAGE.mddocs/user/USAGE.mddocs/user/USAGE.mdcore/libs/prx/libkernel/CMakeLists.txtcore/relinker/relinker/src/guest/GuestModuleBuilder.cppdocs/dev/ARCHITECTURE.mddocs/dev/ARCHITECTURE.mdcore/relinker/main.cppdocs/dev/ARCHITECTURE.mdcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/ARCHITECTURE.mddocs/dev/ARCHITECTURE.mdcore/relinker/main.cppdocs/dev/ARCHITECTURE.mdcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/ARCHITECTURE.mdcore/relinker/main.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtdocs/user/USAGE.mddocs/user/USAGE.mdcore/libs/prx/libkernel/CMakeLists.txtcore/relinker/main.cppcore/relinker/relinker/include/relinker/pipeline/Conversion.hppcore/relinker/relinker/src/pipeline/Conversion.cppdocs/user/USAGE.mdcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtdocs/user/USAGE.mdcore/libs/prx/libkernel/CMakeLists.txtdocs/user/USAGE.mdcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/ARCHITECTURE.mdImported from boykopovar/AnyPS5. Originally posted by mrfelek on 2026-10-18T14:03:16Z.
@SandstromPL thanks for opening this so quickly. I tested it on Windows with Peace Walker (PPSA18216). The combined relink works now, and the game reaches the beach tutorial and plays on into the next mission.
Setup: laptop with Windows 11 Pro 26200, RTX A2000 Laptop GPU (driver 32.0.16.1060), i7-11850H, WinLibs GCC 15.2.0, Release.
Tree: our Peace Walker test branch
test/ppsa18216-ingame(8e5e16820), with this PR's head 9a82a1b45 merged. The merge was clean. That branch ismainc9e6c45deplus #6014, #6196, #6237, #6842, #7072 and #7200, plus one local commit (MSAA resolve, lines, NGS2 callback reset). That's the same tree as the run in #5268.Relink:
It exits 0 and writes
app.exe,mgspw.mgspw.elf.exeand one module tree (Media/Modules,Media/Plugins,sce_module, withIl2cppUserAssemblies.prx.guest.prxamong them). Without this PR the same command stopped withGuest modules convert differently for mgspw/mgspw.elf, so it cannot share them with the input executable.mgspw.elf(ExitAndStartNewProject)AI-assisted; the runs are from my machine(s).
Imported from boykopovar/AnyPS5. Originally posted by SandstromPL on 2026-10-18T17:26:01Z.
@mrfelek thank you, that's exactly the check this needed. I've added your run to the description, next to the relinker CI's Windows job, which passed on the same head.
One small thing, only if you have a Windows build with tests on:
ctest -R guest_deferred_moduleruns the newguest_deferred_module_writable_*loader tests (plus the 4 existing ones). I couldn't compile them here.I'll rebase this onto
mainonce #6112 and #6684 are in. ayam04 has already fixed the.ginitcregister order in #6112, and the rebase picks that up.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by mrfelek on 2026-10-18T20:00:47Z.
@SandstromPL ran it on the same laptop (Windows 11 Pro 26200, RTX A2000 Laptop, WinLibs GCC 15.2.0, Release,
BUILD_TESTING=ON) on this PR's head 9a82a1b45:AI-assisted; the runs are from my machine(s).
Imported from boykopovar/AnyPS5. Originally posted by SandstromPL on 2026-10-19T03:29:46Z.
@mrfelek thank you for the
guest_deferred_modulerun, 9/9 is what the libkernel commit needed. I've added it to the description.#6112 was reverted in #7646, so I rebased this onto
main2ed704842without it or #6684. The branch is now only this PR's two commits, at9a2d69870. Since your runs:main.relinker_executablescase has its own fixture instead of #6112's.Your Peace Walker run and the 9/9 were on the old head 9a82a1b45, which still had #6112 in it. When convenient, could you rerun the combined relink (
--executable mgspw/mgspw.elfwith both--module-dirs) and a short play on the new head?AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by mrfelek on 2026-10-19T10:50:50Z.
@SandstromPL reran on the new head, same laptop (Windows 11 Pro 26200, RTX A2000 Laptop, WinLibs GCC 15.2.0, Release).
Peace Walker: the playable test branch (
mainc9e6c45de+ the open Peace Walker PRs, 8e5e16820) plus this PR's two commits.9a2d69870doesn't merge cleanly onto that older base, so I cherry-picked2088dc38cand9a2d69870. The only conflicts wereoptions.Patches/TargetNameinmain.cppandConversion.hpp, whichmainadded later, and two docs; this PR'sSharedGuestModuleslines went in unchanged.relinker --windows --registry --executable mgspw/mgspw.elf --module-dir Media/Modules --module-dir Media/Plugins src/eboot.elf app.exe: exit 0, 12 guest modules,app.exeandmgspw.mgspw.elf.exewritten.app.exestartsmgspw.mgspw.elf. The scripted start reaches the tutorial stage after about 28 s, as on the old head.Thread destructor callback is already registeredin the log.Tests: this PR's head
9a2d69870itself (not the cherry-picked tree),BUILD_TESTING=ON:AI-assisted; the runs are from my machine(s).
Imported from boykopovar/AnyPS5. Originally posted by SandstromPL on 2026-10-19T19:21:08Z.
@mrfelek thank you for rerunning it on this head, and for the
ctestrun on the head itself. I've updated the description's Windows section with both, so it no longer rests on the old head.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by SandstromPL on 2026-10-21T16:11:39Z.
Closing in favour of #8162, which is now merged. With its uniform deferral on Windows, a
--module-dirmodule converts the same way for every executable, so the--executablemismatch this PR handled can no longer happen. mrfelek confirmed that Peace Walker works on Windows with #8162 alone (https://github.com/boykopovar/AnyPS5/pull/8162#issuecomment-6288011511). Thanks to mrfelek for all the Windows runs here.AI-assisted: yes (Claude Code)
Pull request closed