fix(relinker): collect init and fini array roots through the containing load segment #2330

Merged
simonbbby merged 1 commit from upstream-pr/2330 into main 2026-10-10 06:31:39 +00:00
simonbbby commented 2026-10-09 15:41:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simonbbby on 2026-10-09T15:41:53Z.


What

At unused-filter=1, EntryPointCollector translated DT_INIT_ARRAY and DT_FINI_ARRAY through PT_DYNAMIC and skipped an array placed before it, which is the usual linker layout. Constructors and destructors were then not CFG roots, and imports used only by them were removed. A slot whose value exists only as a RELATIVE addend was read as 0.

Arrays are now translated through the PT_LOAD that holds them, slot targets come from RELATIVE relocations when present, 0 and -1 are skipped, and DT_PREINIT_ARRAY is collected, as in the strict filter. An array outside every file-backed PT_LOAD, or with a size that is not a multiple of 8, throws.

Changes the same lambda as #2196 (bounds checks); the conflict is textual.

Tested

macOS arm64, relinker-only Release build: ctest 39/39. test_init_array_roots.py covers arrays before and after PT_DYNAMIC, SCE tags, RELATIVE addends, terminators, FINI and PREINIT arrays, and an array outside every PT_LOAD. On main the arrays before PT_DYNAMIC, the addend-only slots and PREINIT report CFG/GOT filtering: 1 -> 0. No title was converted from a retail dump.

Also converted 43 open-source titles from the PS5 homebrew catalog (eboot.bin and bundled libc.prx, unwrapped from fSELF) with main and with this branch, for Linux and Windows at unused-filter 0, 1 and 2. No conversion changed outcome. At unused-filter=1, 22 of the 38 titles that convert keep 1 to 14 more imports (86 in total), on both outputs; none keeps fewer.

Linux (Ubuntu 24.04, GCC 13.3), relinker-only Release build: ctest 44/44.

Runtime check on Linux (Ryzen 9 9950X, RTX 3090, NVIDIA 595.99.02) with Yamagi Quake II PPSA99007 0.2.0-alpha.1 from the catalog, relinked with --registry unused-filter=1. Both trees carry #1529, without which the bundled libc.prx shim takes the title's libc imports on Linux (#1497); this PR does not depend on it.

Relinker Imports kept Result
main d70b8998 + #1529 120 of 382 SIGSEGV before any output: the first DT_INIT_ARRAY entry (0xfec30) calls freopen through its removed GOT slot
this branch + #1529 133 of 382 Runs past the constructors, then stops on the GOT slot of pthread_key_create, which #2332 keeps

The title does not reach its menu with either change: at level 1 with #2332 it keeps 20 imports that AnyPS5 does not implement yet.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4802629807 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2330). Originally posted by [simonbbby](https://github.com/simonbbby) on 2026-10-09T15:41:53Z. --- ### What At `unused-filter=1`, `EntryPointCollector` translated DT_INIT_ARRAY and DT_FINI_ARRAY through PT_DYNAMIC and skipped an array placed before it, which is the usual linker layout. Constructors and destructors were then not CFG roots, and imports used only by them were removed. A slot whose value exists only as a RELATIVE addend was read as 0. Arrays are now translated through the PT_LOAD that holds them, slot targets come from RELATIVE relocations when present, 0 and -1 are skipped, and DT_PREINIT_ARRAY is collected, as in the strict filter. An array outside every file-backed PT_LOAD, or with a size that is not a multiple of 8, throws. Changes the same lambda as #2196 (bounds checks); the conflict is textual. ### Tested macOS arm64, relinker-only Release build: ctest 39/39. `test_init_array_roots.py` covers arrays before and after PT_DYNAMIC, SCE tags, RELATIVE addends, terminators, FINI and PREINIT arrays, and an array outside every PT_LOAD. On main the arrays before PT_DYNAMIC, the addend-only slots and PREINIT report `CFG/GOT filtering: 1 -> 0`. No title was converted from a retail dump. Also converted 43 open-source titles from the [PS5 homebrew catalog](https://github.com/blackbearreloaded/ps5-homebrew-catalog) (eboot.bin and bundled libc.prx, unwrapped from fSELF) with main and with this branch, for Linux and Windows at unused-filter 0, 1 and 2. No conversion changed outcome. At unused-filter=1, 22 of the 38 titles that convert keep 1 to 14 more imports (86 in total), on both outputs; none keeps fewer. Linux (Ubuntu 24.04, GCC 13.3), relinker-only Release build: ctest 44/44. Runtime check on Linux (Ryzen 9 9950X, RTX 3090, NVIDIA 595.99.02) with Yamagi Quake II PPSA99007 0.2.0-alpha.1 from the catalog, relinked with `--registry unused-filter=1`. Both trees carry #1529, without which the bundled `libc.prx` shim takes the title's libc imports on Linux (#1497); this PR does not depend on it. | Relinker | Imports kept | Result | |---|---|---| | `main` d70b8998 + #1529 | 120 of 382 | SIGSEGV before any output: the first DT_INIT_ARRAY entry (`0xfec30`) calls `freopen` through its removed GOT slot | | this branch + #1529 | 133 of 382 | Runs past the constructors, then stops on the GOT slot of `pthread_key_create`, which #2332 keeps | The title does not reach its menu with either change: at level 1 with #2332 it keeps 20 imports that AnyPS5 does not implement yet. ### 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 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
Kikoarl commented 2026-10-09 17:53:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-09T17:53:00Z.


Tested on Linux (CachyOS x86_64, Intel RPL-S iGPU / Mesa 26.3.0-devel / Vulkan 1.4.363, GCC 16.2.1) at bfaad395:

  • Build: Built cleanly in RAM (/tmp/anyps5_build) with zero warnings/errors.
  • Conventions: python3 tools/check_conventions.py --base origin/main --head HEAD -> PASS (0 errors).
  • Verification: Verified .init_array and .fini_array root collection in relinker. Guarantees guest static initializers are preserved during pruning.

LGTM!

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6086310844 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2330#issuecomment-6086310844). Originally posted by [Kikoarl](https://github.com/Kikoarl) on 2026-10-09T17:53:00Z. --- Tested on Linux (CachyOS x86_64, Intel RPL-S iGPU / Mesa 26.3.0-devel / Vulkan 1.4.363, GCC 16.2.1) at `bfaad395`: - **Build**: Built cleanly in RAM (`/tmp/anyps5_build`) with zero warnings/errors. - **Conventions**: `python3 tools/check_conventions.py --base origin/main --head HEAD` -> **PASS (0 errors)**. - **Verification**: Verified `.init_array` and `.fini_array` root collection in `relinker`. Guarantees guest static initializers are preserved during pruning. LGTM!
oneandonlydean commented 2026-10-10 05:18:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-10T05:18:51Z.


This conflicts with main in core/relinker/CMakeLists.txt; please rebase. I'll take #2196 first, since both change the same lambda in EntryPointCollector.cpp, so expect one more rebase after it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094144888 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2330#issuecomment-6094144888). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-10T05:18:51Z. --- This conflicts with main in `core/relinker/CMakeLists.txt`; please rebase. I'll take #2196 first, since both change the same lambda in `EntryPointCollector.cpp`, so expect one more rebase after it.
adriadam10 commented 2026-10-10 06:27:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T06:27:47Z.


Rebased onto current main (#2196 landed): kept both tests in core/relinker/CMakeLists.txt, and in EntryPointCollector.cpp kept fitsAt for the other reads and this PR's collectArray. Local relinker build and ctest pass, including entry_point_bounds and init_array_roots. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094649681 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2330#issuecomment-6094649681). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T06:27:47Z. --- Rebased onto current `main` (#2196 landed): kept both tests in `core/relinker/CMakeLists.txt`, and in `EntryPointCollector.cpp` kept `fitsAt` for the other reads and this PR's `collectArray`. Local relinker build and ctest pass, including `entry_point_bounds` and `init_array_roots`. Merging.
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!2330
No description provided.