fix(relinker): keep weak undefined imports weak #4510

Merged
simonbronk merged 3 commits from upstream-pr/4510 into main 2026-10-12 22:17:26 +00:00
simonbronk commented 2026-10-12 13:50:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-12T13:50:35Z.


What

Fixes #4497.

Weak undefined imports (STB_WEAK, section 0) in the input ELF were written to the relinked .dynsym as STB_GLOBAL, so a title that weak-imports a function no module exports stopped at startup instead of binding the import to null.

  • NidReference records whether the source symbol was weak; SysVDynamicSectionBuilder writes STB_WEAK for those imports instead of STB_GLOBAL. On Linux, ld.so then binds a missing weak import to 0.
  • Windows: WindowsRelocationBuilder reads the binding into PeImport::Weak. When no searched module exports a weak import, the startup stub writes 0 plus the addend to its slot and continues; it does not print unresolved ELF import or install a lazy stub. A weak import that is exported resolves as before.
  • Non-weak imports are unchanged: a missing one still fails at startup.

Guest module imports (LinuxGuestModuleWriter, WindowsGuestModuleWriter) are not touched.

Tested

macOS 27 (Apple Silicon, AppleClang 21), relinker-only build, Release:

  • ctest --test-dir build-relinker: 125/125 passed, including the new weak_imports.
  • weak_imports builds an executable with one weak GLOB_DAT import whose code returns 33 when the slot is null and calls through it otherwise:
    • Linux output: the import is STB_WEAK, and a non-weak import is still STB_GLOBAL. Without the fix the test fails with {'absent': 1}.
    • Windows output: relinks with the import missing and present; on Windows it runs and expects 33 (missing) and 11 (exported by a.prx). I could not run the Windows executables; this part needs Windows CI.
  • python3 tools/check_conventions.py --base origin/main: passed.

Porpoise itself not tested (no dump).

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
  • Unblocks: PPSA99764 (Porpoise), no Game bring-up discussion found
  • AI-assisted: yes (Claude Code, Opus 5.5)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4830369474 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4510). Originally posted by [simonbronk](https://github.com/simonbronk) on 2026-10-12T13:50:35Z. --- ### What Fixes #4497. Weak undefined imports (`STB_WEAK`, section 0) in the input ELF were written to the relinked `.dynsym` as `STB_GLOBAL`, so a title that weak-imports a function no module exports stopped at startup instead of binding the import to null. - `NidReference` records whether the source symbol was weak; `SysVDynamicSectionBuilder` writes `STB_WEAK` for those imports instead of `STB_GLOBAL`. On Linux, `ld.so` then binds a missing weak import to 0. - Windows: `WindowsRelocationBuilder` reads the binding into `PeImport::Weak`. When no searched module exports a weak import, the startup stub writes 0 plus the addend to its slot and continues; it does not print `unresolved ELF import` or install a lazy stub. A weak import that is exported resolves as before. - Non-weak imports are unchanged: a missing one still fails at startup. Guest module imports (`LinuxGuestModuleWriter`, `WindowsGuestModuleWriter`) are not touched. ### Tested macOS 27 (Apple Silicon, AppleClang 21), relinker-only build, Release: - `ctest --test-dir build-relinker`: 125/125 passed, including the new `weak_imports`. - `weak_imports` builds an executable with one weak `GLOB_DAT` import whose code returns 33 when the slot is null and calls through it otherwise: - Linux output: the import is `STB_WEAK`, and a non-weak import is still `STB_GLOBAL`. Without the fix the test fails with `{'absent': 1}`. - Windows output: relinks with the import missing and present; on Windows it runs and expects 33 (missing) and 11 (exported by `a.prx`). I could not run the Windows executables; this part needs Windows CI. - `python3 tools/check_conventions.py --base origin/main`: passed. Porpoise itself not tested (no dump). ### 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] Unblocks: PPSA99764 (Porpoise), no Game bring-up discussion found - [x] AI-assisted: yes (Claude Code, Opus 5.5) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
simonbronk commented 2026-10-12 14:33:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-12T14:33:57Z.


Overlaps with #4452 on the same lines in SysVDynamicSectionBuilder.cpp and RelinkerPipeline.cpp, but the changes are independent and compatible (#4452 handles all-zero symbol entries, this one keeps STB_WEAK from named symbols). Happy to rebase onto #4452 if it lands first.

The CMakeLists.txt overlap with #4062 is resolved: the test registration moved next to elf_header.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6126391075 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4510#issuecomment-6126391075). Originally posted by [simonbronk](https://github.com/simonbronk) on 2026-10-12T14:33:57Z. --- Overlaps with #4452 on the same lines in `SysVDynamicSectionBuilder.cpp` and `RelinkerPipeline.cpp`, but the changes are independent and compatible (#4452 handles all-zero symbol entries, this one keeps `STB_WEAK` from named symbols). Happy to rebase onto #4452 if it lands first. The `CMakeLists.txt` overlap with #4062 is resolved: the test registration moved next to `elf_header`.
simonbronk commented 2026-10-12 21:41:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-12T21:41:04Z.


@gabichuuuuu thanks for running it on Windows.

#4452 is merged, so I merged main into this branch: symbolInfo from #4452 is kept (all-zero entries stay STB_WEAK | STT_NOTYPE) and named imports now take STB_WEAK or STB_GLOBAL from the source symbol. Relinker-only build on macOS: 134/134 tests pass; conventions check passes.

On the review note: agreed, the 16/17 loads are unreachable in the weak branch because only executable imports set Weak, so I left it as is.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6133123523 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4510#issuecomment-6133123523). Originally posted by [simonbronk](https://github.com/simonbronk) on 2026-10-12T21:41:04Z. --- @gabichuuuuu thanks for running it on Windows. #4452 is merged, so I merged `main` into this branch: `symbolInfo` from #4452 is kept (all-zero entries stay `STB_WEAK | STT_NOTYPE`) and named imports now take `STB_WEAK` or `STB_GLOBAL` from the source symbol. Relinker-only build on macOS: 134/134 tests pass; conventions check passes. On the review note: agreed, the 16/17 loads are unreachable in the weak branch because only executable imports set `Weak`, so I left it as is.
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!4510
No description provided.