fix(relinker): leave weak imports of bundled modules unbound on Windows #6702

Merged
karimibrahim07 merged 1 commit from upstream-pr/6702 into main 2026-10-19 08:49:11 +00:00
karimibrahim07 commented 2026-10-16 05:19:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karimibrahim07 on 2026-10-16T05:19:15Z.


What

On Windows, a weak import of a bundled module that no library exports stopped the title with FAIL: unresolved ELF import. Domain::GuestImport had no weak flag, so WindowsPePatcher handed every guest import to the entry stub as a strong one, although the stub already binds an executable's weak imports to null (WindowsEntryStubBuilder.cpp L602-L614).

GuestImport now carries Weak. WindowsGuestModuleWriter sets it from the symbol's ELF binding (STB_WEAK) for R_X86_64_64, GLOB_DAT and JUMP_SLOT imports, and WindowsPePatcher passes it on. A weak guest import that a library exports still binds to it.

This is the Windows side of what #6080 does for Linux guest modules, and it merges cleanly with it. #6240 records the same binding in the guest registries, which only changes diagnostics. The test is a new file, so it doesn't touch the test_weak_imports.py lines #6080 changes.

Tested

Windows 11 Pro (26300), WinLibs GCC 15.2.0 r7, Ninja Release, BUILD_TESTING=ON, on main 5d9dcc42d:

  • New windows_guest_weak_imports test. A bundled module imports absent#A#B from a.prx, which doesn't export it, and its DT_INIT traps unless the slot is null:

    • With the import weak, the executable runs and exits with 11 from a.prx.
    • With it global, it still stops with unresolved ELF import absent from a.prx.

    With the three source files from main, the weak case fails with that same unresolved ELF import absent from a.prx.

  • The relinker tests pass, 131 of 131. python tools/check_conventions.py --base origin/main passes.

The Quake homebrew (#6698), relinked with --windows --to-intel --trap-sdk-syscall-stubs --registry and its libOSMesa.so.8 in sce_module/:

  • With #6697, 11 imports of libOSMesa.so.8 stay unresolved. 10 of them are weak (STB_WEAK in its .dynsym):
    • ZSTD_trace_compress_begin, ZSTD_trace_compress_end, ZSTD_trace_decompress_begin, ZSTD_trace_decompress_end
    • the SDK's __dlopen, __dlsym, __dladdr, __dlclose, __dlerror
    • kernel_mprotect
  • With #6697 and this PR, only qsort_s remains, a strong import that libc doesn't implement yet.

Linux and macOS not affected; the change is in the Windows writer and patcher.

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 for the change; the Quake evidence above needs #6697
  • Unblocks: Quake homebrew #6698
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4883871225 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6702). Originally posted by [karimibrahim07](https://github.com/karimibrahim07) on 2026-10-16T05:19:15Z. --- ### What On Windows, a weak import of a bundled module that no library exports stopped the title with `FAIL: unresolved ELF import`. `Domain::GuestImport` had no weak flag, so `WindowsPePatcher` handed every guest import to the entry stub as a strong one, although the stub already binds an executable's weak imports to null ([`WindowsEntryStubBuilder.cpp` L602-L614](https://github.com/boykopovar/AnyPS5/blob/5d9dcc42d2ec5401aebd089028b8d4d7410a77b4/core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp#L602-L614)). `GuestImport` now carries `Weak`. `WindowsGuestModuleWriter` sets it from the symbol's ELF binding (`STB_WEAK`) for `R_X86_64_64`, `GLOB_DAT` and `JUMP_SLOT` imports, and `WindowsPePatcher` passes it on. A weak guest import that a library exports still binds to it. This is the Windows side of what #6080 does for Linux guest modules, and it merges cleanly with it. #6240 records the same binding in the guest registries, which only changes diagnostics. The test is a new file, so it doesn't touch the `test_weak_imports.py` lines #6080 changes. ### Tested Windows 11 Pro (26300), WinLibs GCC 15.2.0 r7, Ninja Release, `BUILD_TESTING=ON`, on `main` `5d9dcc42d`: - New `windows_guest_weak_imports` test. A bundled module imports `absent#A#B` from `a.prx`, which doesn't export it, and its `DT_INIT` traps unless the slot is null: - With the import weak, the executable runs and exits with 11 from `a.prx`. - With it global, it still stops with `unresolved ELF import absent from a.prx`. With the three source files from `main`, the weak case fails with that same `unresolved ELF import absent from a.prx`. - The relinker tests pass, 131 of 131. `python tools/check_conventions.py --base origin/main` passes. The Quake homebrew (#6698), relinked with `--windows --to-intel --trap-sdk-syscall-stubs --registry` and its `libOSMesa.so.8` in `sce_module/`: - With #6697, 11 imports of `libOSMesa.so.8` stay unresolved. 10 of them are weak (`STB_WEAK` in its `.dynsym`): - `ZSTD_trace_compress_begin`, `ZSTD_trace_compress_end`, `ZSTD_trace_decompress_begin`, `ZSTD_trace_decompress_end` - the SDK's `__dlopen`, `__dlsym`, `__dladdr`, `__dlclose`, `__dlerror` - `kernel_mprotect` - With #6697 and this PR, only `qsort_s` remains, a strong import that libc doesn't implement yet. Linux and macOS not affected; the change is in the Windows writer and patcher. ### 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 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 for the change; the Quake evidence above needs #6697 - [x] Unblocks: Quake homebrew #6698 - [x] GPUs tested: n/a - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-16 05:32:35 +00:00 (Migrated from github.com)

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


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 6977 core/relinker/relinker/tests/test_windows_guest_weak_imports.py

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 2780 core/relinker/CMakeLists.txt
PR 5021 core/relinker/CMakeLists.txt
PR 6044 core/relinker/CMakeLists.txt
PR 6618 core/relinker/CMakeLists.txt
PR 6879 core/relinker/CMakeLists.txt
PR 6977 core/relinker/CMakeLists.txt
core/relinker/domain/include/domain/GuestRuntime.hpp
core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp
core/relinker/elfpatcher/src/windows/WindowsPePatcher.cpp
core/relinker/relinker/tests/test_windows_guest_weak_imports.py
PR 7173 core/relinker/CMakeLists.txt
PR 7332 core/relinker/CMakeLists.txt
PR 7453 core/relinker/CMakeLists.txt
PR 7594 core/relinker/CMakeLists.txt
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6208828421 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6702#issuecomment-6208828421). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-16T05:32:35Z. --- <!-- pr-overlap --> 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 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) | | | `core/relinker/relinker/tests/test_windows_guest_weak_imports.py` | 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 2780](https://www.github.com/boykopovar/AnyPS5/pull/2780) | `core/relinker/CMakeLists.txt` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/relinker/CMakeLists.txt` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/relinker/CMakeLists.txt` | | [PR 6618](https://www.github.com/boykopovar/AnyPS5/pull/6618) | `core/relinker/CMakeLists.txt` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/relinker/CMakeLists.txt` | | [PR 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) | `core/relinker/CMakeLists.txt`<br>`core/relinker/domain/include/domain/GuestRuntime.hpp`<br>`core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp`<br>`core/relinker/elfpatcher/src/windows/WindowsPePatcher.cpp`<br>`core/relinker/relinker/tests/test_windows_guest_weak_imports.py` | | [PR 7173](https://www.github.com/boykopovar/AnyPS5/pull/7173) | `core/relinker/CMakeLists.txt` | | [PR 7332](https://www.github.com/boykopovar/AnyPS5/pull/7332) | `core/relinker/CMakeLists.txt` | | [PR 7453](https://www.github.com/boykopovar/AnyPS5/pull/7453) | `core/relinker/CMakeLists.txt` | | [PR 7594](https://www.github.com/boykopovar/AnyPS5/pull/7594) | `core/relinker/CMakeLists.txt` |
screamm commented 2026-10-16 11:20:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-16T11:20:29Z.


Cross-validation on Windows 11 Pro 26220, WinLibs MinGW-w64 GCC 15.2, PR head 18ecee64d on main 5d9dcc42d (fast-forward, no conflicts). Windows-only path, so the checks are on the relinker; I did not run Quake (not available here).

  • windows_guest_weak_imports, --repeat until-fail:3: 3 of 3. With #6697 merged on top (no conflict) both windows_named_imports and windows_guest_weak_imports pass 3 of 3, and the 101 relinker tests of that tree pass.
  • Negative control: with GuestRuntime.hpp, WindowsGuestModuleWriter.cpp and WindowsPePatcher.cpp from main (test and CMake kept) the weak case fails with FAIL: unresolved ELF import absent from a.prx (exit 0xC0000139). With the changes it passes. Same result as your description.
  • tools/check_conventions.py --base origin/main and git diff --check: pass.
  • Relink-only pass over the 51 local titles that have an eboot.elf, main's relinker against this PR's (same flags, no runs): the same outcome for every title (PPSA99995 stops in both with FAIL: Unsupported Windows guest TLS instruction (bytes: 65 48 8b 0c 25 78 03 00 00)), no other FAIL, no WARN, the registries are byte-identical. app.exe differs in 4 titles (PPSA28319, PPSA99109, PPSA99110, PPSA99920); the other 47 are byte-identical.
  • Before and after on those four (130 s, same runtime, PPSA28319 and PPSA99109 twice): all 12 runs (main and PR) reach the limit with no stop line and no unresolved line; the logs differ only in addresses and paths.

No local title has a weak import of a bundled module that nothing exports, so these runs show no regression, not the fix itself; the Quake result (11 down to 1 unresolved import) is not reproduced here.

Not checked: Quake, Linux, macOS, a full-tree build (relinker targets only).

Written with Claude Code assistance (validation by a subagent, reviewed in this session).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6214377680 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6702#issuecomment-6214377680). Originally posted by [screamm](https://github.com/screamm) on 2026-10-16T11:20:29Z. --- Cross-validation on Windows 11 Pro 26220, WinLibs MinGW-w64 GCC 15.2, PR head `18ecee64d` on main `5d9dcc42d` (fast-forward, no conflicts). Windows-only path, so the checks are on the relinker; I did not run Quake (not available here). - `windows_guest_weak_imports`, `--repeat until-fail:3`: 3 of 3. With #6697 merged on top (no conflict) both `windows_named_imports` and `windows_guest_weak_imports` pass 3 of 3, and the 101 relinker tests of that tree pass. - Negative control: with `GuestRuntime.hpp`, `WindowsGuestModuleWriter.cpp` and `WindowsPePatcher.cpp` from main (test and CMake kept) the weak case fails with `FAIL: unresolved ELF import absent from a.prx` (exit `0xC0000139`). With the changes it passes. Same result as your description. - `tools/check_conventions.py --base origin/main` and `git diff --check`: pass. - Relink-only pass over the 51 local titles that have an `eboot.elf`, main's relinker against this PR's (same flags, no runs): the same outcome for every title (PPSA99995 stops in both with `FAIL: Unsupported Windows guest TLS instruction (bytes: 65 48 8b 0c 25 78 03 00 00)`), no other `FAIL`, no `WARN`, the registries are byte-identical. `app.exe` differs in 4 titles (PPSA28319, PPSA99109, PPSA99110, PPSA99920); the other 47 are byte-identical. - Before and after on those four (130 s, same runtime, PPSA28319 and PPSA99109 twice): all 12 runs (main and PR) reach the limit with no stop line and no `unresolved` line; the logs differ only in addresses and paths. No local title has a weak import of a bundled module that nothing exports, so these runs show no regression, not the fix itself; the Quake result (11 down to 1 unresolved import) is not reproduced here. Not checked: Quake, Linux, macOS, a full-tree build (relinker targets only). Written with Claude Code assistance (validation by a subagent, reviewed in this session).
adriadam10 commented 2026-10-19 08:50:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T08:50:55Z.


Checked the diff: GuestImport::Weak is set from STB_WEAK for the three import relocation kinds, passed through WindowsPePatcher, and the entry stub already binds weak imports to null. The new test covers both the weak (exit 11) and the global (still fails) case, and @screamm's Windows run with a negative control confirms it pins the fix. Merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6263395063 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6702#issuecomment-6263395063). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-19T08:50:55Z. --- Checked the diff: `GuestImport::Weak` is set from `STB_WEAK` for the three import relocation kinds, passed through `WindowsPePatcher`, and the entry stub already binds weak imports to null. The new test covers both the weak (exit 11) and the global (still fails) case, and @screamm's Windows run with a negative control confirms it pins the fix. Merged.
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!6702
No description provided.