__elf_phdr_match_addr walked the caller's ModuleInfoEx segments in place,
so an unreadable record crashed the host. The four segment records and
their count are now read with one GuestCopyIn_nid_no_patch, keeping errno,
and an unreadable record throws, as a null one does.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sceKernelLoadStartModule and sceKernelStopUnloadModule stored res straight
through the caller's pointer. sceKernelLoadStartModule now writes it with
GuestCopyOut_nid_no_patch and returns EFAULT before loading for an
unwritable res; sceKernelStopUnloadModule checks res with
GuestWritable_nid_no_patch before unloading and returns EFAULT, leaving the
module loaded. errno is kept.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A module's sweep list is read from its metadata, which is cleared once the module has started, so a sweep skipped at a load without start arguments could never run at that module's next load. The skipped list is now remembered and run at the next deferred load that carries start arguments.
The deferred sweep starts every module another module depends on at the
first deferred load of the title. When that first load carries no
argument block (a module the title starts with args 0 and argp null),
the swept modules were started with the same empty block, and a module
that reads *argp in its module_start faulted: Darkest Dungeon II
(PPSA19591) aborted in NativePluginPS5Example a few seconds after start.
A load whose argp is null no longer triggers the sweep and leaves it
pending, so it runs at the next deferred load that carries start
arguments, which is the block the swept modules get when the title
starts them through their dependent. guest_deferred_module_sweep_no_arguments
loads a module with no arguments first and checks that the swept module
is not started, then loads it with arguments and checks that the sweep
ran with them.
Root cause found and reported by JJenkx in #18053.
The deferred-dependency start state in InitializeDeferredModule (the
resolving vector and the started-results map) were thread_local objects with
destructors. On Windows a thread_local destructor in a module runs after
emulated TLS has freed the object, and prx_thread_local_destructors fails on
main for it. Hold both in HostThreadLocal like the other prx modules; the
trivial depth counter stays thread_local. Behaviour is unchanged.
Firmware 13.42's libkernel_sys.sprx returns 0x80020016 from
sceKernelLoadStartModule for nonzero flags or a non-null opt, and from
sceKernelStopUnloadModule for nonzero flags, a non-null opt or handle 0.
Both ignored these arguments. sceKernelLoadStartModuleForSysmodule
accepts flags there, so it no longer forwards them.
Record that sceKernelStopUnloadModule never runs module_stop.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A module started as a dependency or by the sweep kept its start result, so a later
load of it by the title returned that result instead of 0 (guest_deferred_module_*
failed in the Windows job with 'module start result').
A swept module whose start throws is skipped with a log line, whatever it throws, and
the load that triggered the sweep, and later loads, still succeed.
Concurrent first loads now wait for the one sweep; a failed sweep is retried by the
next first load. The sweep no longer skips the module that triggered it, so a swept
module that depends on it starts after it, and the trigger keeps its start result.
A deferred module that only another deferred module names (PSNCommon, needed by
PSNCore) started after the plugins that use it (SPFXUnityPlugin) and crashed
Soul Hackers 2 on Windows. The first title load of any deferred module now starts
every deferred module some module depends on, in dependency order, with that
load's start arguments, and logs the sweep. Boot is unchanged.
On Windows a module the executable needs or imports from stays deferred, but its
dependencies were deferred too and started only when the title loads it, so a title
that calls it first ran them uninitialised (Soul Hackers 2, #15370 rerun). They start
at startup again, as on main. The loader prints one line per deferred start.
Since #12621 a Windows runtime module that another runtime module needs is deferred instead of started at boot, but loading a deferred module only started that module. A Unity plugin such as SPFXUnityPlugin that calls into a deferred module it needs (Il2CppUserAssemblies in Media/Modules) ran against code whose initializers had never run, and PPSA03743 crashed about 2.6 s in.
A deferred module's initialization table now lists the deferred modules it depends on (names, after the initializer array, behind a magic number so older output reads as having none). DynamicLoader starts those first, transitively and once each through the existing per-module state, with the start arguments of the load that began it, then starts the module itself. The module start result stays the module's own. Boot still starts no deferred module.
Linux is unchanged: its deferred modules still start only when loaded.
NativeStat, SharedMemory, Stdio, DynamicLoader, ModuleInfo and Rtld used
Linux-only APIs (st_atim, memfd_create, sys/vfs.h, link.h and dl_iterate_phdr),
so libkernel did not compile on macOS. Add macOS paths behind __APPLE__:
st_*timespec and a long-path fstatat walk, an unlinked temp file for shared
memory, statfs, dlopen/dladdr and Mach-O segment walks for module and rtld
queries. The Linux and Windows paths are unchanged.
Extracted from feat/macos-port (#8658).
Co-authored-by: Himansh Raj <iamthehimansh@gmail.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Return the main program's handle, the first one sceKernelGetModuleList
lists and one sceKernelGetModuleInfo accepts. reSOT (PPSA99023) imports
it from libkernel and passes the handle to sceKernelGetModuleInfo to find
its own code segment; before this its eboot failed to load on the
unresolved import.
The PS5 3.20 export lists have it in libkernel as well as libkernel_sys,
and on Windows a libkernel import never reaches libkernel_sys, so the
throwing libkernel_sys stub moves to libkernel as pselect did.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The public header declared the unimplemented placeholder int __tls_get_addr(void). It now declares void* __tls_get_addr(const KernelTlsIndex*), the tls_index pointer the ABI passes, and the implementation and the test use that declaration and type instead of their own.
Resolve a FreeBSD/ELF tls_index {module, offset} the way the relinker's
DTPMOD64 values are laid out: on Windows through the PE _tls_index the
module word points at and the thread's ThreadLocalStoragePointer, on
Linux through glibc's __tls_get_addr.
dlerror returned the last loader error from a 512-byte buffer, so a
longer message was cut off. On macOS the reason dyld gives for a failed
dlopen lists every path it tried, and the message for a module that was
not relinked runs to over 1,000 characters; the end of it, the hint to
relink with --module-dir, was lost, and guest_dynamic_loader failed.
Keep the message in a host thread-local string, through
HostThreadLocal so MinGW does not run its destructor on freed
thread-local storage.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Flags 1 finds any module and flags 2 only non-system ones; other flags return EINVAL instead of throwing, every failure clears the 0x1A8 bytes, and st_size is left 0.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>