feat(libkernel): implement rtld_printf #9383

Closed
DavidSamir wants to merge 3 commits from upstream-pr/9383 into main
DavidSamir commented 2026-10-21 19:58:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-21T19:58:00Z.


What

Implements rtld_printf(fmt, ...) in libkernel, in libkernel/System/src/RtldPrintf.cpp, and removes the libkernel_sys discovery stub. It follows FreeBSD 11 libexec/rtld-elf/rtld_printf.c, where rtld_printf is rtld_vfdprintf(STDOUT_FILENO, ...) over rtld's own kvprintf. This is a separate implementation of that behaviour, not a copy of the code.

  • Output: goes to guest descriptor 1 through a 512-byte buffer.
  • Conversions: d i u o x X c s p %, plus rtld's b (bit names), D (hex dump with a separator), r (radix 10), y (signed hex) and n.
  • Flags: # + - 0 . *, with a negative * width toggling left adjustment.
  • Sizes: h hh l ll q j t z, with long 64-bit, as on the console.

Moved to libkernel so libkernel imports reach it on Windows. The PS5 3.20 export lists have rtld_printf in libkernel as well as libkernel_sys, and on Windows a libkernel import never searches libkernel_sys (WindowsEntryStubBuilder.cpp:175). With the libkernel export and no libkernel_sys stub, both imports reach it, and the duplicate-export rule holds.

FreeBSD's quirks are kept:

  • Return value: it always returns 0, because kvprintf never counts output, and %n stores 0.
  • Flush: the character that finds the 512-byte buffer full triggers the flush and is dropped, so 600 characters print as 599.
  • Unknown conversions: one is echoed as written, and every later % prints literally.
  • NULL format: prints (fmt null)\n.

One divergence: a format that ends in a lone % prints the % and the terminating NUL, then stops. FreeBSD's kvprintf instead keeps reading past the end of the string.

Tested

Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release. This is a CPU-side export, so no GPU is involved.

New test guest_kernel_sys_rtld_printf, which captures descriptor 1 through dup2 and covers:

  • every conversion, including width, precision, zero padding and left adjustment combinations;
  • negative numbers, every size modifier, %p with and without a width, * widths, %b, %3D, %r and %y;
  • the quirks: the 0 return, %n storing 0, the unknown-conversion literal mode, the NULL format, the trailing %, and the dropped character at the flush.

Results:

  • It passes, 3 of 3 runs. tools/check_conventions.py passes, and there are no warnings.
  • Negative controls:
    • keeping the flush character makes the test fail at line 75;
    • leaving literal mode off after an unknown conversion fails it at line 70;
    • making %n store 1 fails it at line 69.
  • Link control: with RtldPrintf.cpp removed from libkernel's sources, the test fails to link (undefined reference to rtld_printf_nid_postfix), so it reaches libkernel's export, which is the only one.
  • Overlap: tools/pr_overlap.py crashes on this machine's partial clone, so I ran git merge-tree against every open PR that touches these files. It merges cleanly with all of them. The test is registered at a spot in core/libs/CMakeLists.txt that no open PR edits.

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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4957545746 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9383). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-21T19:58:00Z. --- ### What Implements `rtld_printf(fmt, ...)` in libkernel, in `libkernel/System/src/RtldPrintf.cpp`, and removes the `libkernel_sys` discovery stub. It follows FreeBSD 11 `libexec/rtld-elf/rtld_printf.c`, where `rtld_printf` is `rtld_vfdprintf(STDOUT_FILENO, ...)` over rtld's own `kvprintf`. This is a separate implementation of that behaviour, not a copy of the code. - **Output:** goes to guest descriptor 1 through a 512-byte buffer. - **Conversions:** `d i u o x X c s p %`, plus rtld's `b` (bit names), `D` (hex dump with a separator), `r` (radix 10), `y` (signed hex) and `n`. - **Flags:** `# + - 0 . *`, with a negative `*` width toggling left adjustment. - **Sizes:** `h hh l ll q j t z`, with `long` 64-bit, as on the console. **Moved to libkernel so libkernel imports reach it on Windows.** The PS5 3.20 export lists have `rtld_printf` in libkernel as well as libkernel_sys, and on Windows a libkernel import never searches libkernel_sys (`WindowsEntryStubBuilder.cpp:175`). With the libkernel export and no libkernel_sys stub, both imports reach it, and the duplicate-export rule holds. FreeBSD's quirks are kept: - **Return value:** it always returns 0, because `kvprintf` never counts output, and `%n` stores 0. - **Flush:** the character that finds the 512-byte buffer full triggers the flush and is dropped, so 600 characters print as 599. - **Unknown conversions:** one is echoed as written, and every later `%` prints literally. - **NULL format:** prints `(fmt null)\n`. One divergence: a format that ends in a lone `%` prints the `%` and the terminating NUL, then stops. FreeBSD's `kvprintf` instead keeps reading past the end of the string. ### Tested Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release. This is a CPU-side export, so no GPU is involved. New test `guest_kernel_sys_rtld_printf`, which captures descriptor 1 through `dup2` and covers: - every conversion, including width, precision, zero padding and left adjustment combinations; - negative numbers, every size modifier, `%p` with and without a width, `*` widths, `%b`, `%3D`, `%r` and `%y`; - the quirks: the 0 return, `%n` storing 0, the unknown-conversion literal mode, the NULL format, the trailing `%`, and the dropped character at the flush. Results: - It passes, 3 of 3 runs. `tools/check_conventions.py` passes, and there are no warnings. - Negative controls: - keeping the flush character makes the test fail at line 75; - leaving literal mode off after an unknown conversion fails it at line 70; - making `%n` store 1 fails it at line 69. - Link control: with `RtldPrintf.cpp` removed from libkernel's sources, the test fails to link (`undefined reference to rtld_printf_nid_postfix`), so it reaches libkernel's export, which is the only one. - Overlap: `tools/pr_overlap.py` crashes on this machine's partial clone, so I ran `git merge-tree` against every open PR that touches these files. It merges cleanly with all of them. The test is registered at a spot in `core/libs/CMakeLists.txt` that no open PR edits. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-21 19:58:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-21T19:58:56Z.


Progress report (7e2ba73 → 70401ab)

📈 System libraries: 43.24% (+0.01%, +1 implemented, -1 removed functions)

✅ 1 implemented
Library Name
libkernel rtld_printf_nid_postfix
🗑️ 1 removed
Library Name
libkernel_sys rtld_printf_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6325263448 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9383#issuecomment-6325263448). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-21T19:58:56Z. --- <!-- progress-report --> ### Progress report (7e2ba73 → 70401ab) 📈 **System libraries**: 43.24% (+0.01%, +1 implemented, -1 removed functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libkernel | `rtld_printf_nid_postfix` | </details> <details> <summary>🗑️ 1 removed</summary> | Library | Name | | - | - | | libkernel_sys | `rtld_printf_nid_postfix` | </details>
DavidSamir commented 2026-10-22 02:06:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-22T02:06:26Z.


Linux: Ubuntu 24.04.1 LTS (WSL2), GCC 13.3.0, Release; head e9c12f71c merged onto main 8c26eac3c.

  • guest_kernel_sys_rtld_printf passes 3/3.
  • Link control: with rtld_printf_nid_postfix renamed in RtldPrintf.cpp (instead of removing the file from the sources), the test fails to link: undefined reference to rtld_printf_nid_postfix, as listed.

Run from the PC (Windows host, Linux in WSL2), not from the PR author's machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6330944184 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9383#issuecomment-6330944184). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-22T02:06:26Z. --- Linux: Ubuntu 24.04.1 LTS (WSL2), GCC 13.3.0, Release; head `e9c12f71c` merged onto `main` `8c26eac3c`. - `guest_kernel_sys_rtld_printf` passes 3/3. - Link control: with `rtld_printf_nid_postfix` renamed in `RtldPrintf.cpp` (instead of removing the file from the sources), the test fails to link: `undefined reference to rtld_printf_nid_postfix`, as listed. Run from the PC (Windows host, Linux in WSL2), not from the PR author's machine.
MarcZX10 commented 2026-10-22 06:31:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T06:31:53Z.

Review: DISMISSED


I reviewed the implementation against the FreeBSD 11 rtld formatter behavior and the stated export resolution path. The formatter's buffer boundary behavior, conversion coverage, and documented quirks are internally consistent with the PR description; I did not find a reproducible code correctness defect in the reviewed diff. The test exercises the formatting output and export link path, and the PR includes useful negative controls. Validation on this host: - python3 tools/check_conventions.py --base upstream/main --head refs/review/9383: passed. - git diff --check upstream/main...refs/review/9383: passed. - guest_kernel_sys_rtld_printf_tests: target was already built. CTest aborts before reaching test assertions because this host Xeon E5-1620 v2 lacks LZCNT, BMI1, BMI2, MOVBE, AVX2 and FMA, which AnyPS5 requires to run guest tests. This is an environment limitation, not a failure attributed to this change. - I see an independent Windows/WSL report on this head with 3/3 passing runs and a link negative control. - The PR head merges cleanly with current upstream main in git merge-tree, but is 483 commits behind it. There is one contribution-process blocker before merge: CONTRIBUTING.md says branches should be kept up to date with main. Please rebase this branch onto current main and rerun the required checks before requesting merge. I am leaving this review as changes requested for the documented branch-freshness requirement only, not because I found a formatter defect. The queue comment is not a reason to hold this PR; it is fine to keep submitting PRs and they will be handled in due course.

I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5610118036 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9383#pullrequestreview-5610118036). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T06:31:53Z. **Review:** DISMISSED --- I reviewed the implementation against the FreeBSD 11 rtld formatter behavior and the stated export resolution path. The formatter's buffer boundary behavior, conversion coverage, and documented quirks are internally consistent with the PR description; I did not find a reproducible code correctness defect in the reviewed diff. The test exercises the formatting output and export link path, and the PR includes useful negative controls. Validation on this host: - `python3 tools/check_conventions.py --base upstream/main --head refs/review/9383`: passed. - `git diff --check upstream/main...refs/review/9383`: passed. - `guest_kernel_sys_rtld_printf_tests`: target was already built. CTest aborts before reaching test assertions because this host Xeon E5-1620 v2 lacks LZCNT, BMI1, BMI2, MOVBE, AVX2 and FMA, which AnyPS5 requires to run guest tests. This is an environment limitation, not a failure attributed to this change. - I see an independent Windows/WSL report on this head with 3/3 passing runs and a link negative control. - The PR head merges cleanly with current upstream `main` in `git merge-tree`, but is 483 commits behind it. There is one contribution-process blocker before merge: CONTRIBUTING.md says branches should be kept up to date with `main`. Please rebase this branch onto current `main` and rerun the required checks before requesting merge. I am leaving this review as changes requested for the documented branch-freshness requirement only, not because I found a formatter defect. The queue comment is not a reason to hold this PR; it is fine to keep submitting PRs and they will be handled in due course. I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.
DavidSamir commented 2026-10-22 08:48:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-22T08:48:08Z.


Thanks for the careful review, @MarcZX10, and for checking the code path itself.

On the intake point: this PR implements a documented export (FreeBSD 11's rtld_printf formatter), with a focused test and negative controls, and replaces a throwing stub. I searched the import tables of the 98 homebrew titles I convert: none imports rtld_printf (NID u8M56qpqTtw) today, so Unblocks stays none for now. I'll add the title the moment one is known to stop on it. If a maintainer asks on this PR to hold it, I'll convert it to a draft right away.

The branch is now up to date with main (new merge commit, no force-push). The focused tests rerun on it are listed below.

Merge commit 70401ab80b (current main d6eafa54d0), Windows 11 + WinLibs GCC 15.2: guest_kernel_sys_rtld_printf passes; check_conventions.py passes.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6336676835 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9383#issuecomment-6336676835). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-22T08:48:08Z. --- Thanks for the careful review, @MarcZX10, and for checking the code path itself. On the intake point: this PR implements a documented export (FreeBSD 11's rtld_printf formatter), with a focused test and negative controls, and replaces a throwing stub. I searched the import tables of the 98 homebrew titles I convert: none imports `rtld_printf` (NID `u8M56qpqTtw`) today, so `Unblocks` stays `none` for now. I'll add the title the moment one is known to stop on it. If a maintainer asks on this PR to hold it, I'll convert it to a draft right away. The branch is now up to date with `main` (new merge commit, no force-push). The focused tests rerun on it are listed below. Merge commit `70401ab80b` (current `main` `d6eafa54d0`), Windows 11 + WinLibs GCC 15.2: guest_kernel_sys_rtld_printf passes; check_conventions.py passes. AI-assisted: yes
harisibrar1 commented 2026-10-22 13:32:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T13:32:19Z.

Review: APPROVED


Checked against main b786b907c8 and FreeBSD 11.4 libexec/rtld-elf/rtld_printf.c:

  • I compared RtldFormat line by line with kvprintf. Flag, width and precision parsing, the size modifiers (including hhh→short and %tn→int*, since FreeBSD's %n ignores tflag), %p's width-dependent 0x, %r (radix 10, signed only with +), %y, %b and %D all match.
  • The 0 return and %n storing 0 match too, because PCHAR never increments retval. So does the dropped character on a full buffer (snprintf_func calls printf_out without storing it).
  • The __builtin_sysv_va_list + __builtin_va_arg use matches the existing pattern in libSceLibcInternal/UserContext.cpp on main, and the write_nid_postfix declaration matches its definition in Stdio.cpp.
  • No defects found. The trailing-% divergence is safer than FreeBSD's read past the end.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5615030295 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9383#pullrequestreview-5615030295). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-22T13:32:19Z. **Review:** APPROVED --- Checked against main b786b907c8 and FreeBSD 11.4 `libexec/rtld-elf/rtld_printf.c`: - I compared `RtldFormat` line by line with `kvprintf`. Flag, width and precision parsing, the size modifiers (including `hhh`→short and `%tn`→`int*`, since FreeBSD's `%n` ignores `tflag`), `%p`'s width-dependent `0x`, `%r` (radix 10, signed only with `+`), `%y`, `%b` and `%D` all match. - The 0 return and `%n` storing 0 match too, because `PCHAR` never increments `retval`. So does the dropped character on a full buffer (`snprintf_func` calls `printf_out` without storing it). - The `__builtin_sysv_va_list` + `__builtin_va_arg` use matches the existing pattern in `libSceLibcInternal/UserContext.cpp` on main, and the `write_nid_postfix` declaration matches its definition in `Stdio.cpp`. - No defects found. The trailing-`%` divergence is safer than FreeBSD's read past the end. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
MarcZX10 commented 2026-11-14 11:03:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-14T11:03:48Z.

Review: CHANGES_REQUESTED


Reviewed — changes requested

The implementation does not honor the advertised + flag and mishandles %tn by writing an int through a ptrdiff_t pointer.

What I ran

Reviewed head 70401ab80bfe6c4f9bf0395ee2ef8a3e116d8683 using the provided read-only PR reader. No tests or builds were run; no merged-preview check was performed.

Results

Check Result Note
Diff and changed source review passed All changed paths were included and read
Build and tests not run Author-reported results were not independently verified
Negative control not run No negative-control test was run during this review

Findings

Blocking

  • + is parsed into spec.sign, but signed conversions set that same flag regardless, and RtldNumber emits a sign only for negative values. Thus %+d with a positive value omits the required +. The test includes %+d but expects 5, so it codifies the defect. See RtldPrintf.cpp at the pinned head and the test expectation.
  • %tn falls through to the int* case in RtldStoreCount, although t marks a ptrdiff_t argument. On the 64-bit target this writes only part of the pointed-to value and accesses it through the wrong type. Handle ptrdiffSize with the correct pointer type. See RtldPrintf.cpp at the pinned head.

Non-blocking

None.

Not verified

The build, tests, author-reported negative controls, Windows runtime behavior, and merge overlap were not independently verified. The review folder’s files.json was unavailable; the file listing showed all five changed paths included. I read the complete diff and the added implementation, test, and CMake ranges; I did not read the full unchanged context of Export.Discovery.cpp.

To get this to a good state

Implement the positive sign behavior for + and add a test that expects it for positive signed values. Handle %tn using the correct ptrdiff_t pointer type and add a corresponding test. Then rerun the build and guest_kernel_sys_rtld_printf test on the new head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5903982772 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9383#pullrequestreview-5903982772). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-14T11:03:48Z. **Review:** CHANGES_REQUESTED --- # Reviewed — changes requested The implementation does not honor the advertised `+` flag and mishandles `%tn` by writing an `int` through a `ptrdiff_t` pointer. ## What I ran Reviewed head `70401ab80bfe6c4f9bf0395ee2ef8a3e116d8683` using the provided read-only PR reader. No tests or builds were run; no merged-preview check was performed. ## Results | Check | Result | Note | |---|---|---| | Diff and changed source review | passed | All changed paths were included and read | | Build and tests | not run | Author-reported results were not independently verified | | Negative control | not run | No negative-control test was run during this review | ## Findings ### Blocking - `+` is parsed into `spec.sign`, but signed conversions set that same flag regardless, and `RtldNumber` emits a sign only for negative values. Thus `%+d` with a positive value omits the required `+`. The test includes `%+d` but expects `5`, so it codifies the defect. See [RtldPrintf.cpp at the pinned head](https://github.com/boykopovar/AnyPS5/blob/70401ab80bfe6c4f9bf0395ee2ef8a3e116d8683/core/libs/prx/libkernel/System/src/RtldPrintf.cpp#L192) and [the test expectation](https://github.com/boykopovar/AnyPS5/blob/70401ab80bfe6c4f9bf0395ee2ef8a3e116d8683/core/libs/tests/GuestKernelSysRtldPrintf.cpp#L51). - `%tn` falls through to the `int*` case in `RtldStoreCount`, although `t` marks a `ptrdiff_t` argument. On the 64-bit target this writes only part of the pointed-to value and accesses it through the wrong type. Handle `ptrdiffSize` with the correct pointer type. See [RtldPrintf.cpp at the pinned head](https://github.com/boykopovar/AnyPS5/blob/70401ab80bfe6c4f9bf0395ee2ef8a3e116d8683/core/libs/prx/libkernel/System/src/RtldPrintf.cpp#L157). ### Non-blocking None. ## Not verified The build, tests, author-reported negative controls, Windows runtime behavior, and merge overlap were not independently verified. The review folder’s `files.json` was unavailable; the file listing showed all five changed paths included. I read the complete diff and the added implementation, test, and CMake ranges; I did not read the full unchanged context of `Export.Discovery.cpp`. ## To get this to a good state Implement the positive sign behavior for `+` and add a test that expects it for positive signed values. Handle `%tn` using the correct `ptrdiff_t` pointer type and add a corresponding test. Then rerun the build and `guest_kernel_sys_rtld_printf` test on the new head.

Pull request closed

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!9383
No description provided.