fix(relinker): preserve weak imports #4624

Closed
kallmek3nny wants to merge 1 commit from upstream-pr/4624 into main
kallmek3nny commented 2026-10-12 17:02:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kallmek3nny on 2026-10-12T17:02:16Z.


What

Fixes #4497.

Preserve weak undefined imports through relinking instead of promoting them to required global imports:

  • carry the original STB_WEAK binding through NidReference and emit weak undefined symbols in the rebuilt SysV dynamic symbol table, so a missing weak import can bind to zero on Linux
  • carry the weak flag through Windows relocation building; if no provider resolves a weak import, write zero (or its relocation addend) to the import slot instead of taking the fatal unresolved-import path
  • keep resolved weak imports using the normal resolved address path
  • add focused coverage that checks weak vs strong dynamic-symbol binding and a Windows conversion case with a deliberately missing weak import

This is generic weak-symbol behavior rather than a chroot special case. Porpoise (PPSA99764) is the motivating title from #4497.

Tested

Ubuntu 24.04, GCC 13.3.0, Release relinker-only build on current main (2a5eca11cd1bf583db39ce67034e8453842ac244):

  • python3 tools/check_conventions.py --base upstream/main passed
  • git diff --check upstream/main...HEAD passed
  • relinker-only build completed successfully
  • CTest: 134/134 passed, including strict_nid_filter and windows_import_modules

The Windows fixture conversion path is covered by the Linux run, but native Windows execution was not run in this validation.

Checklist

  • Based on current main; no open PR found implementing the same weak-import behavior
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unsupported paths still throw; this only preserves standard weak-symbol semantics
  • No notes, investigation .md files or images added to the repository
  • No new third-party code or dependencies
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: PPSA99764 #4497
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4832565722 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4624). Originally posted by [kallmek3nny](https://github.com/kallmek3nny) on 2026-10-12T17:02:16Z. --- ### What Fixes #4497. Preserve weak undefined imports through relinking instead of promoting them to required global imports: - carry the original `STB_WEAK` binding through `NidReference` and emit weak undefined symbols in the rebuilt SysV dynamic symbol table, so a missing weak import can bind to zero on Linux - carry the weak flag through Windows relocation building; if no provider resolves a weak import, write zero (or its relocation addend) to the import slot instead of taking the fatal unresolved-import path - keep resolved weak imports using the normal resolved address path - add focused coverage that checks weak vs strong dynamic-symbol binding and a Windows conversion case with a deliberately missing weak import This is generic weak-symbol behavior rather than a `chroot` special case. Porpoise (PPSA99764) is the motivating title from #4497. ### Tested Ubuntu 24.04, GCC 13.3.0, Release relinker-only build on current `main` (`2a5eca11cd1bf583db39ce67034e8453842ac244`): - `python3 tools/check_conventions.py --base upstream/main` passed - `git diff --check upstream/main...HEAD` passed - relinker-only build completed successfully - CTest: 134/134 passed, including `strict_nid_filter` and `windows_import_modules` The Windows fixture conversion path is covered by the Linux run, but native Windows execution was not run in this validation. ### Checklist - [x] Based on current `main`; no open PR found implementing the same weak-import behavior - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unsupported paths still throw; this only preserves standard weak-symbol semantics - [x] No notes, investigation `.md` files or images added to the repository - [x] No new third-party code or dependencies - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: PPSA99764 #4497 - [x] AI-assisted: yes
babyhuey commented 2026-10-12 21:48:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-12T21:48:53Z.


Checked this with Porpoise (PPSA99764, the title from #4497) on both systems. It conflicts with main now, so I merged it locally onto main 559979dc (fork branch test/verify-weak, ed42b152). In SysVDynamicSectionBuilder.cpp, main now builds the binding in a symbolInfo(ref) helper (from #4452), so I kept that and gave it your ref.Weak check. In RelinkerPipeline.cpp I kept main's null-symbol check and added weak to the push_back.

  • main 559979dc: undefined symbol: AKknh8Cx-V8 (chroot) on Linux, FAIL: unresolved ELF import AKknh8Cx-V8 (chroot) from libkernel_sys.prx on Windows.
  • With this PR, on Linux (GCC 13) and Windows 11 (WinLibs GCC 15.2): the relink succeeds with chroot, fchdir and malloc_usable_size left unresolved. On Windows the stub writes 0 + addend for the unresolved weak slot. Porpoise starts (Porpoise: main() entered) and runs to its HEN loop (jailbreak: /data isn't reachable (uid 0); asking the HEN, then polling ports 9028/9069 and the /download0 jailbreak files). That's the same place it reached when chroot was a real export; what remains there is #4643.
  • windows_import_modules passes 5/5 on Windows; windows_import_modules and strict_nid_filter pass on Linux. No new warnings in the PR's files on Windows; the only warnings in that rebuild are the existing stl_construct.h ones from WindowsTlsBuilder.cpp, recompiled because WindowsPeFormat.hpp changed.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6133234216 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4624#issuecomment-6133234216). Originally posted by [babyhuey](https://github.com/babyhuey) on 2026-10-12T21:48:53Z. --- Checked this with Porpoise (PPSA99764, the title from #4497) on both systems. It conflicts with `main` now, so I merged it locally onto `main` 559979dc (fork branch `test/verify-weak`, ed42b152). In `SysVDynamicSectionBuilder.cpp`, `main` now builds the binding in a `symbolInfo(ref)` helper (from #4452), so I kept that and gave it your `ref.Weak` check. In `RelinkerPipeline.cpp` I kept `main`'s null-symbol check and added `weak` to the `push_back`. - `main` 559979dc: `undefined symbol: AKknh8Cx-V8 (chroot)` on Linux, `FAIL: unresolved ELF import AKknh8Cx-V8 (chroot) from libkernel_sys.prx` on Windows. - With this PR, on Linux (GCC 13) and Windows 11 (WinLibs GCC 15.2): the relink succeeds with `chroot`, `fchdir` and `malloc_usable_size` left unresolved. On Windows the stub writes `0 + addend` for the unresolved weak slot. Porpoise starts (`Porpoise: main() entered`) and runs to its HEN loop (`jailbreak: /data isn't reachable (uid 0); asking the HEN`, then polling ports 9028/9069 and the `/download0` jailbreak files). That's the same place it reached when `chroot` was a real export; what remains there is #4643. - `windows_import_modules` passes 5/5 on Windows; `windows_import_modules` and `strict_nid_filter` pass on Linux. No new warnings in the PR's files on Windows; the only warnings in that rebuild are the existing `stl_construct.h` ones from `WindowsTlsBuilder.cpp`, recompiled because `WindowsPeFormat.hpp` changed.
babyhuey commented 2026-10-12 22:29:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-12T22:29:02Z.


FYI: #4510 (keep weak undefined imports weak) merged at 22:17 and closed #4497. On main 43e3a70f, Porpoise (PPSA99764) already gets past its weak chroot import on Linux and reaches the same HEN loop as with this PR, so this one now overlaps #4510.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6133816604 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4624#issuecomment-6133816604). Originally posted by [babyhuey](https://github.com/babyhuey) on 2026-10-12T22:29:02Z. --- FYI: #4510 (keep weak undefined imports weak) merged at 22:17 and closed #4497. On `main` 43e3a70f, Porpoise (PPSA99764) already gets past its weak `chroot` import on Linux and reaches the same HEN loop as with this PR, so this one now overlaps #4510.
adriadam10 commented 2026-10-13 14:46:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T14:46:55Z.


Closing as a duplicate: #4510 (merged 2026-10-12) already preserves weak undefined imports with the same approach (NidReference/PeImport weak flag, STB_WEAK in the rebuilt .dynsym, Windows startup stub writing 0 plus addend for a missing weak import) and closed #4497. This PR touches the same files, and conflicts with main now.

If you think a case is left uncovered (for example the symOff bounds check in RelinkerPipeline.cpp, or a test of the Windows missing-weak path), feel free to open a new small PR on top of current main with just that.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6148237104 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4624#issuecomment-6148237104). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-13T14:46:55Z. --- Closing as a duplicate: #4510 (merged 2026-10-12) already preserves weak undefined imports with the same approach (`NidReference`/`PeImport` weak flag, `STB_WEAK` in the rebuilt `.dynsym`, Windows startup stub writing 0 plus addend for a missing weak import) and closed #4497. This PR touches the same files, and conflicts with `main` now. If you think a case is left uncovered (for example the `symOff` bounds check in `RelinkerPipeline.cpp`, or a test of the Windows missing-weak path), feel free to open a new small PR on top of current `main` with just that.

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