feat(libkernel): implement __elf_phdr_match_addr #337

Merged
DotDebian merged 1 commit from upstream-pr/337 into main 2026-10-04 23:10:33 +00:00
DotDebian commented 2026-10-02 09:18:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DotDebian on 2026-10-02T09:18:13Z.


What

Implements __elf_phdr_match_addr in core/libs/prx/libkernel/Module/src/Module.cpp. It returns 1 when the address lies in an executable PT_LOAD segment of the given module, 0 otherwise.

  • Behaviour follows the public FreeBSD libc source, lib/libc/gen/elf_utils.c (same symbol name), as found in releng/9.0 and releng/11.0: segments that are not PT_LOAD or lack PF_X are skipped, and a segment matches when dlpi_addr + p_vaddr <= addr and addr + sizeof(addr) < dlpi_addr + p_vaddr + p_memsz.
  • The signature changes. The stub was declared (ModuleInfo* module, std::uint64_t address); FreeBSD declares (struct dl_phdr_info *phdr_info, void *addr). The parameters are now (dl_phdr_info*, void*), using the existing types from prx/libc/include/specifics/linux/ElfTypes.hpp. Symbol name and argument count are unchanged.
  • FreeBSD dereferences phdr_info unchecked. A null phdr_info throws std::invalid_argument here.
  • FreeBSD main has since dropped the + sizeof(addr) term. This PR keeps it, as the 9.x and 11.x branches do. That the console's libkernel matches those branches is inferred from the lineage, not checked against a binary.

Tested

Windows 11, MinGW-w64 GCC 15.2.0, Release build with -DBUILD_TESTING=ON. No title or homebrew was run: dl_iterate_phdr still throws on Windows, so nothing hands a dl_phdr_info to this function end to end yet. Linux was not built locally.

New ctest guest_elf_phdr_match (core/libs/tests/GuestElfPhdrMatch.cpp), on a synthetic dl_phdr_info with four program headers and a non-zero dlpi_addr:

  • address inside an executable PT_LOAD → 1;
  • address inside a PT_LOAD without PF_X → 0;
  • address inside an executable segment that is not PT_LOAD → 0;
  • exact segment start → 1, one byte below → 0;
  • addr + 8 == end - 1 → 1, addr + 8 == end → 0, addr == end → 0;
  • address below every segment → 0;
  • segment p_vaddr without dlpi_addr added → 0;
  • match in a later executable segment of the array → 1;
  • dlpi_phnum == 0 → 0.

The null phdr_info case is not covered by the test. With the upper bound changed from < to <= the test fails. Full suite: 108/108 tests pass.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4714771823 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/337). Originally posted by [DotDebian](https://github.com/DotDebian) on 2026-10-02T09:18:13Z. --- ### What Implements `__elf_phdr_match_addr` in `core/libs/prx/libkernel/Module/src/Module.cpp`. It returns 1 when the address lies in an executable `PT_LOAD` segment of the given module, 0 otherwise. - Behaviour follows the public FreeBSD libc source, `lib/libc/gen/elf_utils.c` (same symbol name), as found in `releng/9.0` and `releng/11.0`: segments that are not `PT_LOAD` or lack `PF_X` are skipped, and a segment matches when `dlpi_addr + p_vaddr <= addr` and `addr + sizeof(addr) < dlpi_addr + p_vaddr + p_memsz`. - **The signature changes.** The stub was declared `(ModuleInfo* module, std::uint64_t address)`; FreeBSD declares `(struct dl_phdr_info *phdr_info, void *addr)`. The parameters are now `(dl_phdr_info*, void*)`, using the existing types from `prx/libc/include/specifics/linux/ElfTypes.hpp`. Symbol name and argument count are unchanged. - FreeBSD dereferences `phdr_info` unchecked. A null `phdr_info` throws `std::invalid_argument` here. - FreeBSD `main` has since dropped the `+ sizeof(addr)` term. This PR keeps it, as the 9.x and 11.x branches do. That the console's libkernel matches those branches is inferred from the lineage, not checked against a binary. ### Tested Windows 11, MinGW-w64 GCC 15.2.0, Release build with `-DBUILD_TESTING=ON`. No title or homebrew was run: `dl_iterate_phdr` still throws on Windows, so nothing hands a `dl_phdr_info` to this function end to end yet. Linux was not built locally. New ctest `guest_elf_phdr_match` (`core/libs/tests/GuestElfPhdrMatch.cpp`), on a synthetic `dl_phdr_info` with four program headers and a non-zero `dlpi_addr`: - address inside an executable `PT_LOAD` → 1; - address inside a `PT_LOAD` without `PF_X` → 0; - address inside an executable segment that is not `PT_LOAD` → 0; - exact segment start → 1, one byte below → 0; - `addr + 8 == end - 1` → 1, `addr + 8 == end` → 0, `addr == end` → 0; - address below every segment → 0; - segment `p_vaddr` without `dlpi_addr` added → 0; - match in a later executable segment of the array → 1; - `dlpi_phnum == 0` → 0. The null `phdr_info` case is not covered by the test. With the upper bound changed from `<` to `<=` the test fails. Full suite: 108/108 tests pass. ### Checklist - [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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-04 05:26:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-04T05:26:04Z.


Progress report (e1654a9 → 7fd0890)

📈 System libraries: 80.8% (+0.04%, +1 implemented functions)

✅ 1 implemented
Library Name
libkernel __elf_phdr_match_addr_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5976919006 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/337#issuecomment-5976919006). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-04T05:26:04Z. --- <!-- progress-report --> ### Progress report (e1654a9 → 7fd0890) 📈 **System libraries**: 80.8% (+0.04%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libkernel | `__elf_phdr_match_addr_nid_postfix` | </details>
oneandonlydean commented 2026-10-04 23:09:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T23:09:15Z.


Thanks. This is a line-for-line port of FreeBSD releng/11.0 lib/libc/gen/elf_utils.c: it skips headers that aren't PT_LOAD or lack PF_X, and matches on dlpi_addr + p_vaddr <= addr && addr + sizeof(addr) < dlpi_addr + p_vaddr + p_memsz. The old (ModuleInfo*, uint64_t) signature was wrong, and the repo's dl_phdr_info has the same first four fields as FreeBSD's, which are the only ones read here. The test covers both edges of the sizeof(addr) term.

guest_elf_phdr_match passes on my Linux build (in a batch with this PR), and CI is now green on both platforms. Good to merge from my side.

For context: PPSA21564's eboot doesn't import __elf_phdr_match_addr (Fjc4-n1+y2g). Only the Sony libc.prx it ships references it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5985449970 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/337#issuecomment-5985449970). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T23:09:15Z. --- Thanks. This is a line-for-line port of FreeBSD releng/11.0 `lib/libc/gen/elf_utils.c`: it skips headers that aren't `PT_LOAD` or lack `PF_X`, and matches on `dlpi_addr + p_vaddr <= addr && addr + sizeof(addr) < dlpi_addr + p_vaddr + p_memsz`. The old `(ModuleInfo*, uint64_t)` signature was wrong, and the repo's `dl_phdr_info` has the same first four fields as FreeBSD's, which are the only ones read here. The test covers both edges of the `sizeof(addr)` term. `guest_elf_phdr_match` passes on my Linux build (in a batch with this PR), and CI is now green on both platforms. Good to merge from my side. For context: PPSA21564's eboot doesn't import `__elf_phdr_match_addr` (Fjc4-n1+y2g). Only the Sony `libc.prx` it ships references it.
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!337
No description provided.