feat(libs): implement C runtime re-exports for libSceAmpr #545
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/545"
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 Pouare514 on 2026-10-04T16:38:25Z.
What
libSceAmpr and libc re-export part of the C runtime (statically linked CRT). 12 of the 36 unknown libSceAmpr NIDs are identified by NID hash (verified by recomputation) as standard C functions and implemented with real semantics instead of throwing stubs: localeconv (Sony's 0x90-byte Dinkumware struct), __udivti3 (manual 128-bit division, no libgcc dependency; a zero divisor throws), strtok_s, ctime_s, wcscpy_s, mbstowcs, asctime_s (takes the guest
GuestTm), __fpclassifyd (Dinkumware codes), _Sinh, _Fltrounds (CFLT_ROUNDSvalues), set_constraint_handler_s and ignore_handler_s. The semantics live once inSonyCrt.hppand are exported both by libc (real names) and libSceAmpr (sceAmpr*forwarders). This unblocks Deathloop past its first Ampr call (128-bit division during init).The remaining 24 (C++ runtime: _Pad, num_get/time_get facets, shared_ptr locks, _Thrd/_Cnd, vtables) keep throwing stubs for follow-ups.
Tested
Checklist
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-04T16:38:45Z.
Progress report (
5c4bc7d→af9dfbf)📈 System libraries: 85.55% (+0.11%, +24 implemented functions)
✅ 24 implemented
sceAmprAsctimeSsceAmprCtimeSsceAmprFltroundssceAmprFpclassifydsceAmprIgnoreHandlerSsceAmprLocaleconvsceAmprMbstowcssceAmprSetConstraintHandlerSsceAmprSinhsceAmprStrtokSsceAmprUdivti3sceAmprWcscpyS_Fltrounds_nid_postfix_Sinh_nid_postfix__fpclassifyd_nid_postfix__udivti3_nid_postfixasctime_s_nid_postfixctime_s_nid_postfixignore_handler_s_nid_postfixlocaleconv_nid_postfixmbstowcs_nid_postfixset_constraint_handler_s_nid_postfixstrtok_s_nid_postfixwcscpy_s_nid_postfixImported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T21:19:26Z.
Thanks. This one carries #407's commit, so it can only go in after #407 (or split from it). I also compared the twelve functions with Sony's own CRT: the libc.prx that PPSA21564 ships exports all twelve of these NIDs, and several behave differently from this PR:
__fpclassifydreturns the Dinkumware codes: NaN 2, inf 1, zero 0, subnormal -2, normal -1. Here zero, subnormal and normal come out as 0x10, 0x08 and 0x04._Fltroundsreads the x87 rounding field and maps it through {1, 3, 2, 0}: nearest 1, down 3, up 2, toward zero 0 (the CFLT_ROUNDSvalues). Here nearest is 0, down 1 and toward zero 3._Sinhtakes two doubles and returnsy * sinh(x)(it reads xmm1 and multiplies by it). Here it takes one.localeconvreturns Sony's staticlconv, which hasdecimal_pointat offset 0x48 (seven pointers and fourteenCHAR_MAXbytes come first). The host's struct has it at 0, so a guest readingdecimal_pointgets a different field.wcscpy_sreads 16-bit characters and calls the constraint handler on errors. The guestwchar_tis 16 bits (seeWindowsFormatting.hpp), so hostwchar_tis wrong on Linux here and inmbstowcs.set_constraint_handler_stakes a lock and installs the default handler when passed NULL; here NULL is stored and the initial handler is NULL.__udivti3returning 0 for a zero divisor: the real one traps, so this should throw.These are libc exports. If the title's import table really names libSceAmpr for them, could you check that, and implement them once where both libraries can share them instead of keeping a second copy here?
Unrelated to the CRT part, #407 exports T4ucGB8CsnM and 5tRaBjtdTzY from libSceWebBrowserDialog, but they are
sceVideoOutVrrUnpegFromFixedRateandsceVideoOutVrrPegToFixedRate(recomputed), which #518 adds to VideoOut. They should go from #407.Imported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-05T00:03:40Z.
Reworked per review (rebased onto the new #407, so this is now Ampr-only on top of it; forced update after the rebase): checked all twelve NIDs against the Deathloop ELF — each is imported only from libSceAmpr (IMPORT_LIB 48 / NEEDED 43), so Ampr keeps the exports. The implementations now live once in tools-visible SonyCrt.hpp, compiled into both libc (as real-name exports) and libSceAmpr (as sceAmpr forwarders), which also avoids cross-module calls for the same NID. All seven correctness items addressed: Dinkumware __fpclassifyd codes, C FLT_ROUNDS mapping via fegetround, two-argument _Sinh, Sony lconv layout with decimal_point at 0x48, 16-bit wcscpy_s/mbstowcs with constraint-handler invocation, locked handler with aborting default installed on NULL, and __udivti3 throwing on a zero divisor. New guest_crt test covers all twelve through the Ampr interface (udivti3 values plus the throw, handler counting, layout offset, formats). Notes: the test links libc for __gxx_personality_v0 and uses configure_windows_unwind like the other guest tests (a -fasynchronous-unwind-tables variant broke unwinding with our personality); udivti3 is exercised through the shared header because cross-prx __int128 calls from a MinGW caller come back garbled. 184/184 ctest green locally.
Imported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-05T00:54:46Z.
Rebased onto current main (this should also restart CI, which never ran for the previous push): kept both sides where main added code around this PR's areas, and dropped my two Http throwing stubs whose NIDs #555 now implements for real. Full ctest (222) passes locally.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T01:18:11Z.
Thanks, this covers all seven points: the Dinkumware
__fpclassifydcodes, theFLT_ROUNDSmapping, two-argument_Sinh, thelconvlayout withdecimal_pointat 0x48, 16-bitwcscpy_s/mbstowcswith the constraint handler, the locked handler with an aborting default for NULL, and__udivti3throwing on zero. Implementing them once inSonyCrt.hppfor both libc and libSceAmpr is a good way to do it, and the TechnicalDebt lines record the parts that are assumed. The twelve Ampr NIDs are the ones #407 used to stub, and none is exported elsewhere.Two small, optional points:
ctime_sandasctime_sreturnEINVALwithout calling the constraint handler, wherewcscpy_scalls it. If Sony's do the same as the other_sfunctions, they would call it too.mbstowcswidens each byte as it is, with no multibyte decoding. That's what the C locale would do, but a TechnicalDebt line saying it's assumed would help.This carries #407's commits, so it inherits the Linux CI failure I described on #407 (eight libSceAmpr names that interpose libstdc++).
guest_crtlinks libSceAmpr too, so it would hit the same problem. Once #407 has the fix and this is rebased on it, it's ready to merge from my side, after #407.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T02:47:15Z.
This carries #407's fd3b24e, so the libstdc++ interposition is fixed here too. The Linux CI job, which failed on the previous head, is green now.
Since #338 landed, two files conflict with main:
snwprintf_sstub at the end of the namespace, where__udivti3_nid_postfixgoes. Keep both functions.Your own commit is the same as the one I looked at last time (only the
__cxa_call_unexpected_nid_postfixcontext differs), and the two optional points from my last comment are still optional. After #407 and this rebase, it's ready to merge from my side.Imported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-05T13:31:16Z.
Rebased onto the new #407 (fork/feat/deathloop-loading @ 993ccfad). Git auto-skipped the #407 commits; I skipped the remaining redundant 42318552 (already superseded by fae345aa + _nid_postfix fix) and replayed only our own commit as 2a3a2a7c.
TechnicalDebt.md: kept main/#338 lines first (Http/Http2/Net/SessionSignaling/Trophy/SystemService/Voice + snwprintf_s + Ult queues), then our 3 CRT lines (localeconv, set_constraint_handler_s, __udivti3) after them, before the kernel UnknownNN lines.
RuntimeSupport.cpp: auto-merged cleanly - kept upstream state plus our __udivti3_nid_postfix (SonyUdivti3); snwprintf_s stays in FormattingWide.cpp as _nid_postfix, so both functions are preserved. libSceAmpr/Export.cpp: kept both sides - upstream _nid_postfix runtime renames plus our 12 sceAmprXxx forwarders calling SonyXxx with SonyCrt.hpp include; no cross-prx _nid_postfix calls, no externs.
Optional points (ctime_s/asctime_s constraint handler, mbstowcs TechnicalDebt line) left out as agreed. Ready after #407.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-06T05:34:04Z.
Thanks. Replaying only your own commit onto the new #407 is right, and the
RuntimeSupport.cppresolution keeps bothsnwprintf_sand__udivti3_nid_postfix.The branch conflicts with main again in
docs/dev/TechnicalDebt.mdonly, at the end of "Unknown function info", where main has since added_sceUlobjmgrRegisterObject,sceShareGetCurrentStatus,sceAgcDcbSet*PredicationEnableGetSizeandResource registration. It's the same resolution as #407: keep main's lines, then your three CRT lines and #407's kernel lines. The Linux job's failure on this head was anagc_driver_flat_cache_bitstimeout on lavapipe, which doesn't touch these files.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-06T09:27:56Z.
Same note as on #407: #676 has merged and removed the host
libSceNpCppWebApireplacement, so thelibSceNpCppWebApichanges here can be dropped in the rebase.Imported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-09T18:47:56Z.
Rebased onto the new #407 (
998016e7, itself rebased for the #676/#369/TD points; forced update 2a3a2a7c...0647694a). Rebase --onto old #407 base, so only our own commit replayed — no stale #407 commits carried.Resolution keeps both sides everywhere: TechnicalDebt.md has main's lines, then our 3 CRT lines (localeconv, set_constraint_handler_s, __udivti3), then #407's kernel lines. RuntimeSupport.cpp only adds the SonyCrt.hpp include + __udivti3_nid_postfix (snwprintf_s untouched upstream in FormattingWide.cpp). Ampr keeps upstream _nid_postfix renames plus our 12 sceAmprXxx forwarders calling SonyXxx, no cross-prx _nid_postfix calls.
#676: nothing to drop here — the libSceNpCppWebApi replacement lived in #407's commits, which are gone from this branch after the --onto; diff vs new #407 is Ampr/SonyCrt/libc-wrappers/GuestCrt test/TD only. Optional points left out as agreed. Ready after #407.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T09:33:41Z.
Review: CHANGES_REQUESTED
Thanks for the rework; the semantics were already covered by Dean's review. What blocks it now is a conflict with main that needs a logic change:
core/libs/prx/libc/src/Time.cpp: main now converts to the guest's 56-byteGuestTm(GuestTime.hpp,toHostTm, TZ init).asctime_s_nid_postfix(Time.cpp:127) andsceAmprAsctimeStake a hoststd::tm*, which is 36 bytes on Windows and does not match the guest layout. Takeconst GuestTm*and convert withtoHostTmbefore formatting.SonyCtimeS(SonyCrt.hpp:73) callsstd::localtimedirectly, bypassing the guest TZ handlinglocaltime_shas on main. Reuse the same path aslibc_localtime, and take the time asconst int64_t*like the other time functions.core/libs/prx/libc/src/Strings.cpp: conflict at the end of the file, keep main's lines and addstrtok_s_nid_postfixafter them.core/libs/tests/GuestCrt.cpp:59-68callsSonyUdivti3directly; test it through thesceAmprexport and the libc one so the registered exports are covered. Add anasctime_scase with aGuestTmof the guest layout.Please rebase on current main and rerun
guest_crt.Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-11T00:44:03Z.
For priority: on
main06da1176this PR is the next stop for four catalog titles on Linux (catalog builds withsce_module/libc.prxunwrapped as #2605 requires; tracked in #1578):undefined symbol: 0hlfW1O4Aa4localeconvundefined symbol: 0hlfW1O4Aa4localeconvundefined symbol: 802pFCwC9w0__udivti3undefined symbol: qlWiRfOJx1A__fpclassifydEach line is
app.elf: symbol lookup error: app.elf: undefined symbol: <NID>.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T01:53:05Z.
A data point for
localeconv, read from Sony's ownlibc.prx, the one Astro's Playroom (PPSA01325) ships insce_module/. Itslocaleconv(0hlfW1O4Aa4, at 0x2570) islea 0x114050(%rip),%rax; ret, a static struct. Reading that struct with its RELA relocations gives the full Dinkumware layout, 0x90 bytes:char*(currency_symbol,int_curr_symbol,mon_decimal_point,mon_grouping,mon_thousands_sep,negative_sign,positive_sign)""(relocated), not nullchar(frac_digits…int_p_sign_posn)0x7f(CHAR_MAX), including theint_*onesdecimal_point"."grouping,thousands_sep""_Frac_grouping,_Frac_sep""_False,_True"false","true"_No,_Yes""Compared with the TechnicalDebt line here ("seven null pointers ... the int_p/int_n tail fields are left zero and the rest follows the host layout"), three things differ:
strlen(localeconv()->currency_symbol), which is valid in the C locale, would crash on null.int_*chars areCHAR_MAX, not 0.thousands_septhe layout is Dinkumware's (_Frac_grouping,_Frac_sep,_False,_True,_No,_Yes), not the host's.The four catalog titles @babyhuey listed import
localeconvfromlibSceLibcInternal. Their bundledlibc.prxis a stub library (every export at 0x50), so on the console they get this Sony struct.I have a branch with just this struct, a
static_asserton the layout and a byte-offset test, but I'm not opening it since this PR coverslocaleconv. Happy to share it if useful.Imported from boykopovar/AnyPS5. Originally posted by Amoenus on 2026-10-11T09:43:29Z.
PPSA99902 original caller ABI evidence (2026-10-11, c34f5b08)
I checked this PR's actual libc implementation at head
0647694ad5b559856beb1ba80bbe0e78bc0cea80, not only its Ampr re-export description.SonyLconvputs seven reserved pointers and 16 bytes beforedecimal_point(offset 0x48), andSonyLocaleconv()sets the reserved pointers to null. The following original title caller evidence instead expects its decimal-point pointer at 0x00.Target: PPSA99902 Sonic Unleashed Recompiled 01.003.000, original ELF SHA-256
c34f5b0848ef49e96a855076d8254254047e966e4fe56589d0e504fe13828479. All addresses below are original guest ELF VAs, not relinked/host addresses. The five known direct references tolocaleconv(0hlfW1O4Aa4, libSceLibcInternal) resolve to GOT 0x4ba43a0:48 8b 00,mov rax,[rax]; reads the pointed byte and compares it with'.'48 8b 00,mov rax,[rax]; returns its first byte or falls back to'.'for null4c 8b 38,mov r15,[rax]; tests the string against'.'and its terminator4c 8b 38,mov r15,[rax]; same decimal-string checksVerification: current original SHA checked, independent ELF header/PT_LOAD parsing and original unwind FDE/CIE boundaries for all 5/5 callers; Capstone and GNU objdump agree on every instruction's bytes in all five bounded functions. Ghidra 12.1.4 read-only recheck matches all 5/5 full PT_LOAD byte hashes and 5/5 import references. Its existing analysis has no functions/instructions at these sites, so failed decompiler queries are not used as supporting evidence; no project mutation or inferred type is used to corroborate the layout.
This supports #3245's FreeBSD-style decimal offset 0 for this title. It does not establish all 24 field semantics or one universal PS5 ABI. Several callers tolerate a null decimal pointer, so I am not claiming a measured crash from your layout; the 96-byte copy and non-null locale result still need a compatible prefix. Please keep the differing layout evidence explicit when this work is carried. I have not built or run #545 itself.
Independent #3245 adaptation/tests: fork commit
1642b5c8. Its controlled title replay removes this unresolved import and revealsfminfbefore guest entry; nolocaleconv()runtime invocation or gameplay claim. Details on #3245 / #3249. No new PR; AI-assisted investigation: Codex.Merged Sony CRT retained in fresh startup validation (2026-10-11,main0f863)
The earlier head0647694a null-prefix observation is historical. Current main0f863 retains the corrected 0x90-byte SonyLconv, seven monetary pointers to non-null empty strings,14CHAR_MAX chars, decimal pointer at0x48 and Dinkumware tail. This aligns with adriadam10's reported Astro's Playroom library observation; I did not independently measure that console library.
Compiled the unchanged current SonyLocaleconv helper in a standalone Windows source probe5times: sizeof=144,decimal offset=72,prefix-first-byte=0,decimal-first-byte=46,prefix-empty=true. Upstream guest_crt passes5repeats, also in the full835-test integration, and5repeats afterward. This confirms current implementation/test behavior, not PS5 equivalence.
Our refreshed integrationd8fd7c7 keeps this merged layout and omits our old FreeBSD locale override. With validated math/kernel prerequisites it enters PPSA99902 guest and hits the color guard; changing only the D64 driver advances to the existing MSAA descriptor refusal5/5. Controlled details. Thus this Sony implementation does not prevent the observed startup, and the earlier locale override is not established as necessary on this newer stack.
The original ELF caller evidence remains: known libSceLibcInternal localeconv consumers load result+0 as a decimal string or copy96bytes. With today's helper, result+0 points to an empty monetary string, not null; the earlier null-fallback reasoning therefore cannot be reused for this version. These differing guest assumptions need investigation; they do not justify globally replacing the Sony layout, supported by current source and the reported vendor observation, with FreeBSD's. They could involve guest/toolchain ABI assumptions. Actual invocation of these specific locale callers and resulting formatting behavior were not traced in the new replays. No uniform-PS5-ABI, measured locale crash or gameplay claim. AI-assisted validation/report: Codex.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T09:52:11Z.
Review: APPROVED
Rebased on current main and fixed the points from the previous review on your branch (no push from you in 24 h):
Time.cpp/SonyCrt.hpp:asctime_stakesconst GuestTm*(no hoststd::tm);ctime_stakesconst int64_t*and goes throughlocaltime_s_nid_postfix, so it uses the guest time zone. Both exports (libc andsceAmpr*) shareSonyFormatAsctime.Strings.cpp/Math.cpp/Time.cpp: conflicts resolved, keeping main's lines plusstrtok_s_nid_postfix.localeconv: rewritten to the 0x90-byte Dinkumware layout read from thelibc.prxof Astro's Playroom (PPSA01325): empty monetary strings instead of null,CHAR_MAXin all 14charfields,decimal_pointat 0x48, thengrouping,thousands_sep,_Frac_grouping,_Frac_sep,_False,_True,_No,_Yes.static_asserts pin the size and offset. TechnicalDebt line updated.GuestCrt.cpp: also calls the libc exports (ctime_s,asctime_s,mbstowcs,strtok_s,__fpclassifyd,__udivti3,localeconv), addsGuestTmasctime cases (valid, bad weekday, null) and ctime buffer-size cases;Requireprints the failing line.Checked: scoped build and ctest (164 tests) green,
guest_crtpasses; merging once CI is green.