feat(libkernel): implement __tls_get_addr #9059

Merged
mehmetbayoglu merged 2 commits from upstream-pr/9059 into main 2026-10-25 02:23:45 +00:00
mehmetbayoglu commented 2026-10-21 11:10:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mehmetbayoglu on 2026-10-21T11:10:09Z.


What

__tls_get_addr (NID vNe1w4diLCs) was an unimplemented stub in libkernel/Unimplemented.cpp. It now takes the FreeBSD/ELF tls_index {module, offset} and returns the calling thread's address of that offset in the module's TLS block. The module word is read the way the relinker writes it for R_X86_64_DTPMOD64:

  • on Windows, the address of the converted module's PE _tls_index, resolved through the thread's ThreadLocalStoragePointer, as in the resolver the relinker emits for guest modules (WindowsGuestStartup.cpp);
  • on Linux, the glibc module ID, passed on to glibc's __tls_get_addr.

A null index, module 0 and a Windows thread without a block for the module throw. macOS throws, recorded in docs/dev/debt/libkernel.md: the relinker binds guest modules there to Aps5GuestTlsGetAddr_nid_no_patch, which no library defines yet.

Tested

  • Windows 11 Pro, MinGW GCC 15.2, full build with -DBUILD_TESTING=ON.
  • New guest_tls_get_addr: a tls_index for the test executable's own TLS block (its PE _tls_index on Windows) resolves to the calling thread's block, and on a second thread to that thread's own, freshly initialized block; writes on one thread do not show on the other; a null index throws.
  • The 67 TLS, thread-local and module tests pass (ctest -R "tls|thread_local|module").
  • Linux: not tested here; JJenkx ran guest_tls_get_addr on Arch Linux (GCC 16.2.1, glibc 2.44) at da9728f5ac, 3 of 3 runs pass, plus a dlopen TLS module probe (comment on this PR).
  • One test does not compile with MinGW here, on main as well: agc_driver_image_array_window_past_layer_limit_tests (alignas(65536) exceeds the object file maximum of 8192).

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:4950588006 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9059). Originally posted by [mehmetbayoglu](https://github.com/mehmetbayoglu) on 2026-10-21T11:10:09Z. --- ### What `__tls_get_addr` (NID `vNe1w4diLCs`) was an unimplemented stub in `libkernel/Unimplemented.cpp`. It now takes the FreeBSD/ELF `tls_index` {module, offset} and returns the calling thread's address of that offset in the module's TLS block. The module word is read the way the relinker writes it for `R_X86_64_DTPMOD64`: - on Windows, the address of the converted module's PE `_tls_index`, resolved through the thread's `ThreadLocalStoragePointer`, as in the resolver the relinker emits for guest modules (`WindowsGuestStartup.cpp`); - on Linux, the glibc module ID, passed on to glibc's `__tls_get_addr`. A null index, module 0 and a Windows thread without a block for the module throw. macOS throws, recorded in `docs/dev/debt/libkernel.md`: the relinker binds guest modules there to `Aps5GuestTlsGetAddr_nid_no_patch`, which no library defines yet. ### Tested - Windows 11 Pro, MinGW GCC 15.2, full build with `-DBUILD_TESTING=ON`. - New `guest_tls_get_addr`: a `tls_index` for the test executable's own TLS block (its PE `_tls_index` on Windows) resolves to the calling thread's block, and on a second thread to that thread's own, freshly initialized block; writes on one thread do not show on the other; a null index throws. - The 67 TLS, thread-local and module tests pass (`ctest -R "tls|thread_local|module"`). - Linux: not tested here; JJenkx ran `guest_tls_get_addr` on Arch Linux (GCC 16.2.1, glibc 2.44) at da9728f5ac, 3 of 3 runs pass, plus a `dlopen` TLS module probe (comment on this PR). - One test does not compile with MinGW here, on `main` as well: `agc_driver_image_array_window_past_layer_limit_tests` (`alignas(65536)` exceeds the object file maximum of 8192). ### 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
mehmetbayoglu commented 2026-10-21 13:27:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mehmetbayoglu on 2026-10-21T13:27:54Z.


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 9101 docs/dev/debt/libkernel.md L242
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 4630 docs/dev/debt/libkernel.md
PR 5021 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 6044 core/libs/prx/libkernel/CMakeLists.txt
PR 6149 docs/dev/debt/libkernel.md
PR 6469 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 6740 docs/dev/debt/libkernel.md
PR 6879 docs/dev/debt/libkernel.md
PR 6936 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 6977 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Unimplemented.cpp
docs/dev/debt/libkernel.md
PR 7049 docs/dev/debt/libkernel.md
PR 7088 docs/dev/debt/libkernel.md
PR 7105 docs/dev/debt/libkernel.md
PR 7121 docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7192 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 7201 core/libs/prx/libkernel/CMakeLists.txt
PR 7447 docs/dev/debt/libkernel.md
PR 7509 docs/dev/debt/libkernel.md
PR 8520 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 8726 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md

no more line conflicts after new comitt

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6317270778 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9059#issuecomment-6317270778). Originally posted by [mehmetbayoglu](https://github.com/mehmetbayoglu) on 2026-10-21T13:27:54Z. --- > 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 9101](https://www.github.com/boykopovar/AnyPS5/pull/9101) `docs/dev/debt/libkernel.md` [L242](https://github.com/boykopovar/AnyPS5/blob/745b3946294a9d79e1177a289482768d6ab88c9a/docs/dev/debt/libkernel.md#L242) > 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 4630](https://www.github.com/boykopovar/AnyPS5/pull/4630) `docs/dev/debt/libkernel.md` > [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) `core/libs/prx/libkernel/CMakeLists.txt` > `docs/dev/debt/libkernel.md` > [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) `core/libs/prx/libkernel/CMakeLists.txt` > [PR 6149](https://www.github.com/boykopovar/AnyPS5/pull/6149) `docs/dev/debt/libkernel.md` > [PR 6469](https://www.github.com/boykopovar/AnyPS5/pull/6469) `core/libs/prx/libkernel/CMakeLists.txt` > `docs/dev/debt/libkernel.md` > [PR 6740](https://www.github.com/boykopovar/AnyPS5/pull/6740) `docs/dev/debt/libkernel.md` > [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) `docs/dev/debt/libkernel.md` > [PR 6936](https://www.github.com/boykopovar/AnyPS5/pull/6936) `core/libs/prx/libkernel/CMakeLists.txt` > `docs/dev/debt/libkernel.md` > [PR 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) `core/libs/prx/libkernel/CMakeLists.txt` > `core/libs/prx/libkernel/Unimplemented.cpp` > `docs/dev/debt/libkernel.md` > [PR 7049](https://www.github.com/boykopovar/AnyPS5/pull/7049) `docs/dev/debt/libkernel.md` > [PR 7088](https://www.github.com/boykopovar/AnyPS5/pull/7088) `docs/dev/debt/libkernel.md` > [PR 7105](https://www.github.com/boykopovar/AnyPS5/pull/7105) `docs/dev/debt/libkernel.md` > [PR 7121](https://www.github.com/boykopovar/AnyPS5/pull/7121) `docs/dev/debt/libkernel.md` > [PR 7149](https://www.github.com/boykopovar/AnyPS5/pull/7149) `docs/dev/debt/libkernel.md` > [PR 7192](https://www.github.com/boykopovar/AnyPS5/pull/7192) `core/libs/prx/libkernel/CMakeLists.txt` > `docs/dev/debt/libkernel.md` > [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) `core/libs/prx/libkernel/CMakeLists.txt` > [PR 7447](https://www.github.com/boykopovar/AnyPS5/pull/7447) `docs/dev/debt/libkernel.md` > [PR 7509](https://www.github.com/boykopovar/AnyPS5/pull/7509) `docs/dev/debt/libkernel.md` > [PR 8520](https://www.github.com/boykopovar/AnyPS5/pull/8520) `core/libs/prx/libkernel/CMakeLists.txt` > `docs/dev/debt/libkernel.md` > [PR 8726](https://www.github.com/boykopovar/AnyPS5/pull/8726) `core/libs/prx/libkernel/CMakeLists.txt` > `docs/dev/debt/libkernel.md` no more line conflicts after new comitt
harisibrar1 commented 2026-10-22 05:47:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T05:47:42Z.

Review: APPROVED


Checked against main 4a17c7175 and the relinker's TLS handling (WindowsGuestStartup.cpp EmitTlsResolver, WindowsGuestModuleWriter.cpp:98, LinuxGuestModuleWriter.cpp:60-67):

  • On Windows the export does the same thing as the resolver the relinker emits ([[rdi]] as the slot, the gs:[0x58] array, +[rdi+8]). Guest DTPMOD64 is written as ImageBase + _tls_index RVA, and executable DTPMOD64 is rejected at relink time, so no other module-word encoding can reach it.
  • On Linux, guest vNe1w4diLCs imports are already renamed to glibc's __tls_get_addr, and sceKernelGetModuleInfo reports dlpi_tls_modid. Forwarding to glibc keeps the same id space.
  • The macOS throw and debt line match MacOsGuestModuleWriter.cpp:86, which binds to Aps5GuestTlsGetAddr_nid_no_patch. The SDK's sce_stubs/libkernel.c confirms the libkernel export. I found nothing to change.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5609774780 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9059#pullrequestreview-5609774780). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-22T05:47:42Z. **Review:** APPROVED --- Checked against main 4a17c7175 and the relinker's TLS handling (`WindowsGuestStartup.cpp` EmitTlsResolver, `WindowsGuestModuleWriter.cpp:98`, `LinuxGuestModuleWriter.cpp:60-67`): - On Windows the export does the same thing as the resolver the relinker emits (`[[rdi]]` as the slot, the `gs:[0x58]` array, `+[rdi+8]`). Guest DTPMOD64 is written as `ImageBase + _tls_index` RVA, and executable DTPMOD64 is rejected at relink time, so no other module-word encoding can reach it. - On Linux, guest `vNe1w4diLCs` imports are already renamed to glibc's `__tls_get_addr`, and `sceKernelGetModuleInfo` reports `dlpi_tls_modid`. Forwarding to glibc keeps the same id space. - The macOS throw and debt line match `MacOsGuestModuleWriter.cpp:86`, which binds to `Aps5GuestTlsGetAddr_nid_no_patch`. The SDK's `sce_stubs/libkernel.c` confirms the libkernel export. I found nothing to change. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
JJenkx commented 2026-10-22 06:23:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T06:23:47Z.


Independent Windows run of this PR, as data only. The machine is a Ryzen 9 5950X on Windows 11 (10.0.22621), built with WinLibs GCC 15.2.0 UCRT r7, Release, BUILD_TESTING=ON. I tested a local merge of your head da9728f5ac into main d6eafa54d0 (f37482568e). It merged cleanly; git auto-merged libkernel's CMakeLists.txt, Unimplemented.cpp and debt/libkernel.md.

  • TLS tests: ctest -R "tls|thread_local|module" passes 69/69 in each of 3 runs, guest_tls_get_addr included.
  • libkernel suite: 207/208. The one failure is guest_raise_in_host_code (30 s timeout), which also times out intermittently on main on this machine.

A note for import_audit (you also have #9372): with this PR's source as --source, tools/import_audit.py still classifies vNe1w4diLCs (__tls_get_addr) as stub. I checked on the registries of Graven (PPSA09614), Goat Simulator 3 (PPSA04158) and Overcooked! All You Can Eat (PPSA01528), with main's and this PR's trees.

The cause is the non-Windows, non-Linux #else branch, which calls NotImplemented_nid_no_patch("__tls_get_addr on this host"). The source scan in progress.scan_library doesn't evaluate preprocessor branches, so any reachable-looking stub call marks the export as to-do. That's arguably right for macOS, but titles audited for Windows or Linux will keep listing __tls_get_addr as throwing, even after this merges. I haven't checked how #9372's partial detection treats a #if branch.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6334256720 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9059#issuecomment-6334256720). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-22T06:23:47Z. --- Independent Windows run of this PR, as data only. The machine is a Ryzen 9 5950X on Windows 11 (10.0.22621), built with WinLibs GCC 15.2.0 UCRT r7, Release, `BUILD_TESTING=ON`. I tested a local merge of your head da9728f5ac into `main` d6eafa54d0 (f37482568e). It merged cleanly; git auto-merged libkernel's `CMakeLists.txt`, `Unimplemented.cpp` and `debt/libkernel.md`. - **TLS tests:** `ctest -R "tls|thread_local|module"` passes 69/69 in each of 3 runs, `guest_tls_get_addr` included. - **libkernel suite:** 207/208. The one failure is `guest_raise_in_host_code` (30 s timeout), which also times out intermittently on `main` on this machine. **A note for `import_audit` (you also have #9372):** with this PR's source as `--source`, `tools/import_audit.py` still classifies `vNe1w4diLCs` (`__tls_get_addr`) as **stub**. I checked on the registries of Graven (PPSA09614), Goat Simulator 3 (PPSA04158) and Overcooked! All You Can Eat (PPSA01528), with `main`'s and this PR's trees. The cause is the non-Windows, non-Linux `#else` branch, which calls `NotImplemented_nid_no_patch("__tls_get_addr on this host")`. The source scan in `progress.scan_library` doesn't evaluate preprocessor branches, so any reachable-looking stub call marks the export as to-do. That's arguably right for macOS, but titles audited for Windows or Linux will keep listing `__tls_get_addr` as throwing, even after this merges. I haven't checked how #9372's partial detection treats a `#if` branch.
mehmetbayoglu commented 2026-10-22 06:45:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mehmetbayoglu on 2026-10-22T06:45:17Z.


Thanks @harisibrar1 for the review and @JJenkx for the 5950X run. On the audit: right, the throw is only in the #else branch for hosts other than Windows and Linux, but neither progress.py nor #9372's partial detection evaluates preprocessor branches, so it still reads as a stub. That belongs to the audit, not to this PR: I'll extend #9372 so a NotImplemented_nid_no_patch reached only under #if/#elif/#else counts as partial, with a test.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6334574314 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9059#issuecomment-6334574314). Originally posted by [mehmetbayoglu](https://github.com/mehmetbayoglu) on 2026-10-22T06:45:17Z. --- Thanks @harisibrar1 for the review and @JJenkx for the 5950X run. On the audit: right, the throw is only in the `#else` branch for hosts other than Windows and Linux, but neither `progress.py` nor #9372's partial detection evaluates preprocessor branches, so it still reads as a stub. That belongs to the audit, not to this PR: I'll extend #9372 so a `NotImplemented_nid_no_patch` reached only under `#if`/`#elif`/`#else` counts as partial, with a test.
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!9059
No description provided.