Skip to content

feat(libkernel): implement rtld_printf - #9383

Closed
DavidSamir wants to merge 3 commits into
boykopovar:mainfrom
DavidSamir:feat/libkernel-sys-rtld-printf
Closed

DavidSamir wants to merge 3 commits into
boykopovar:mainfrom
DavidSamir:feat/libkernel-sys-rtld-printf

Conversation

@DavidSamir

@DavidSamir DavidSamir commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

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

@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

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

@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

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 18112 rtld_printf

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 4563 core/libs/prx/libkernel/CMakeLists.txt
PR 6149 core/libs/prx/libkernel/CMakeLists.txt
PR 6936 core/libs/prx/libkernel/CMakeLists.txt
PR 7275 core/libs/CMakeLists.txt
PR 8041 core/libs/prx/libkernel/CMakeLists.txt
PR 9389 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel_sys/Export.Discovery.cpp
PR 9938 core/libs/prx/libkernel/CMakeLists.txt
PR 10657 core/libs/prx/libkernel_sys/Export.Discovery.cpp
PR 11896 core/libs/prx/libkernel/CMakeLists.txt
PR 12130 core/libs/CMakeLists.txt
core/libs/prx/libkernel_sys/Export.Discovery.cpp
PR 12544 core/libs/prx/libkernel/CMakeLists.txt
PR 12642 core/libs/CMakeLists.txt
core/libs/prx/libkernel_sys/Export.Discovery.cpp
PR 12682 core/libs/prx/libkernel/CMakeLists.txt
PR 12688 core/libs/prx/libkernel/CMakeLists.txt
PR 12700 core/libs/prx/libkernel/CMakeLists.txt
PR 12965 core/libs/prx/libkernel/CMakeLists.txt
PR 12994 core/libs/CMakeLists.txt
PR 15037 core/libs/prx/libkernel_sys/Export.Discovery.cpp
PR 15838 core/libs/CMakeLists.txt
PR 16018 core/libs/CMakeLists.txt
PR 16052 core/libs/CMakeLists.txt
PR 16372 core/libs/prx/libkernel/CMakeLists.txt
PR 16790 core/libs/prx/libkernel/CMakeLists.txt
PR 16793 core/libs/CMakeLists.txt
PR 17439 core/libs/prx/libkernel/CMakeLists.txt
PR 17463 core/libs/prx/libkernel/CMakeLists.txt
PR 17484 core/libs/prx/libkernel/CMakeLists.txt
PR 17513 core/libs/prx/libkernel/CMakeLists.txt
PR 17936 core/libs/CMakeLists.txt
core/libs/prx/libkernel_sys/Export.Discovery.cpp
PR 18112 core/libs/prx/libkernel/CMakeLists.txt
PR 18190 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel_sys/Export.Discovery.cpp
PR 18293 core/libs/prx/libkernel/CMakeLists.txt
PR 18300 core/libs/CMakeLists.txt
PR 19181 core/libs/prx/libkernel/CMakeLists.txt
PR 19434 core/libs/CMakeLists.txt
PR 21020 core/libs/prx/libkernel/CMakeLists.txt
PR 21026 core/libs/prx/libkernel/CMakeLists.txt
PR 21041 core/libs/prx/libkernel/CMakeLists.txt
PR 21051 core/libs/prx/libkernel/CMakeLists.txt
PR 21058 core/libs/prx/libkernel/CMakeLists.txt
PR 21132 core/libs/prx/libkernel/CMakeLists.txt
PR 21135 core/libs/prx/libkernel/CMakeLists.txt
PR 21464 core/libs/CMakeLists.txt
core/libs/prx/libkernel_sys/Export.Discovery.cpp
PR 21604 core/libs/prx/libkernel/CMakeLists.txt
PR 21619 core/libs/prx/libkernel/CMakeLists.txt
PR 21625 core/libs/prx/libkernel/CMakeLists.txt
PR 21626 core/libs/prx/libkernel/CMakeLists.txt
PR 21647 core/libs/prx/libkernel/CMakeLists.txt
PR 21655 core/libs/prx/libkernel/CMakeLists.txt
PR 21663 core/libs/prx/libkernel/CMakeLists.txt
PR 21990 core/libs/CMakeLists.txt
PR 22391 core/libs/prx/libkernel/CMakeLists.txt
PR 22479 core/libs/prx/libkernel/CMakeLists.txt
PR 23395 core/libs/CMakeLists.txt
PR 23786 core/libs/CMakeLists.txt
PR 23975 core/libs/CMakeLists.txt
PR 24015 core/libs/CMakeLists.txt
PR 24343 core/libs/prx/libkernel/CMakeLists.txt
PR 24731 core/libs/CMakeLists.txt
PR 25454 core/libs/CMakeLists.txt
PR 25470 core/libs/CMakeLists.txt
PR 25477 core/libs/CMakeLists.txt
PR 25493 core/libs/CMakeLists.txt
PR 25500 core/libs/CMakeLists.txt
PR 25949 core/libs/prx/libkernel/CMakeLists.txt
PR 26174 core/libs/prx/libkernel/CMakeLists.txt
PR 26206 core/libs/prx/libkernel/CMakeLists.txt
PR 26805 core/libs/CMakeLists.txt
PR 27547 core/libs/CMakeLists.txt
PR 27563 core/libs/prx/libkernel/CMakeLists.txt
PR 27717 core/libs/prx/libkernel/CMakeLists.txt
PR 27750 core/libs/CMakeLists.txt
PR 27762 core/libs/CMakeLists.txt
PR 27813 core/libs/prx/libkernel/CMakeLists.txt
PR 27864 core/libs/prx/libkernel/CMakeLists.txt
PR 28043 core/libs/CMakeLists.txt
core/libs/prx/libkernel/CMakeLists.txt
PR 28045 core/libs/prx/libkernel/CMakeLists.txt
PR 28051 core/libs/prx/libkernel/CMakeLists.txt
PR 28144 core/libs/CMakeLists.txt
PR 28253 core/libs/prx/libkernel/CMakeLists.txt
PR 28294 core/libs/CMakeLists.txt
PR 28299 core/libs/prx/libkernel/CMakeLists.txt
PR 28323 core/libs/prx/libkernel/CMakeLists.txt
PR 28434 core/libs/prx/libkernel/CMakeLists.txt
PR 28702 core/libs/prx/libkernel/CMakeLists.txt

The PS5 3.20 export lists have rtld_printf in libkernel as well as
libkernel_sys, and on Windows a libkernel import never reaches
libkernel_sys. The implementation moves unchanged to
libkernel/System/src/RtldPrintf.cpp.
@DavidSamir DavidSamir changed the title feat(libkernel_sys): implement rtld_printf feat(libkernel): implement rtld_printf Oct 21, 2026
@DavidSamir

Copy link
Copy Markdown
Contributor Author

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 MarcZX10 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

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 harisibrar1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked against main b786b90 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

Copy link
Copy Markdown
Contributor

I’ve noticed that my earlier request to hold this PR based on #5239 was unnecessary. It’s fine to keep submitting PRs; maintainers will handle them in due course. Please disregard that request.

@adriadam10 adriadam10 self-assigned this Oct 25, 2026
@adriadam10
adriadam10 dismissed MarcZX10’s stale review October 25, 2026 12:30

Dismissing a stale review: the requested changes were addressed after it (new head) and the merge queue blocks on it. Please request changes again if anything is still open.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 25, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

@DavidSamir this conflicts with main in core/libs/CMakeLists.txt and core/libs/prx/libkernel/CMakeLists.txt (source and test lists). Please merge main.

One more fix while you are there: main declares the export once, as rtld_printf_nid_postfix(void) at core/libs/prx/libkernel_sys/include/Api.hpp:178. After the move into libkernel the function is defined with (const char*, ...), so that declaration needs the same signature (the public header tests check one declaration per export).

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@adriadam10 adriadam10 removed their assignment Nov 17, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

I am closing this for now. After moving rtld_printf into libkernel, the public Api.hpp still declares a no-argument signature for a function defined with a format string and varargs, which needs to match the one declaration rule on main. There has been no push since 22 Oct and the branch conflicts. Feel free to reopen or push a rebased branch at any time and we will look at it again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build libkernel needs rebase Conflicts with main, rebase needed prx

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants