fix(relinker): preserve weak guest exports on macOS #8118

Merged
RockENZO merged 1 commit from upstream-pr/8118 into main 2026-10-27 08:04:57 +00:00
RockENZO commented 2026-10-19 22:32:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by RockENZO on 2026-10-19T22:32:17Z.


What

A converted macOS guest module currently emits every definition as strong, even when its ELF symbol has STB_WEAK. dyld therefore does not include those definitions in its weak-definition coalescing. A native consumer with the same weak function and global keeps its own copies instead of sharing the converted provider's definitions.

Preserve STB_WEAK on ordinary and TLS exports using the existing Mach-O export flags. The writer already derives MH_WEAK_DEFINES from those flags. Strong definitions remain strong. This changes only the macOS guest writer; the executable-export implementation is already on main through #7361.

The regression test compares the converted provider with a native x86-64 dylib built by AppleClang, loading both through the real macOS loader in separate probe processes. The native weak provider's function/global give 18; before the fix the converted provider gives 165, and after the fix it gives 18. A strong-provider control matches its native counterpart as well. Structural checks cover weak and strong function, data and TLS exports, including the Mach-O header flag, on every test host.

No title-specific workaround or claim of a newly playable game is included.

Tested

macOS 27.2, Apple Silicon, AppleClang 21.0.0.21000334, Python 3.9.6, CMake 4.4.4 and Ninja 1.13.2, Release. Based on upstream 0d8c51a54e04c165f108b7bedbba3867e503118a.

  • Baseline: the new native weak-definition control fails with native=18 and converted=165.
  • Fix: the real x86-64 function/data coalescing probe under Rosetta matches the native weak and strong controls.
  • Relinker-only arm64 build: all 210 registered ctest tests pass.
  • Relinker-only x86-64/Rosetta build: all 210 registered ctest tests pass.
  • CTest commands include the temporary Ninja directory on PATH; the first runs exposed a missing Ninja in a nested CMake fixture, which passed after correcting that environment.
  • Linux and Windows guest outputs are byte-identical to the baseline for ordinary and TLS fixtures. Linux and Windows host execution was not tested locally.
  • TLS weakness is checked in the export trie and header; no full guest TLS-runtime or game execution is claimed.
  • Conventions and whitespace checks pass. The required overlap check completed without a confirmed conflict. It flagged already-conflicting PRs #5021, #6044, #6618, #6879, #6977 and #7332 for manual shared-file inspection. Their CMake changes add unrelated sources/tests or refactor test registration; #6977 changes DT_INIT handling in the writer, not export weakness. There is no functional dependency; whichever lands later may need to rebase.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4928296724 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8118). Originally posted by [RockENZO](https://github.com/RockENZO) on 2026-10-19T22:32:17Z. --- ### What A converted macOS guest module currently emits every definition as strong, even when its ELF symbol has `STB_WEAK`. dyld therefore does not include those definitions in its weak-definition coalescing. A native consumer with the same weak function and global keeps its own copies instead of sharing the converted provider's definitions. Preserve `STB_WEAK` on ordinary and TLS exports using the existing Mach-O export flags. The writer already derives `MH_WEAK_DEFINES` from those flags. Strong definitions remain strong. This changes only the macOS guest writer; the executable-export implementation is already on `main` through #7361. The regression test compares the converted provider with a native x86-64 dylib built by AppleClang, loading both through the real macOS loader in separate probe processes. The native weak provider's function/global give 18; before the fix the converted provider gives 165, and after the fix it gives 18. A strong-provider control matches its native counterpart as well. Structural checks cover weak and strong function, data and TLS exports, including the Mach-O header flag, on every test host. No title-specific workaround or claim of a newly playable game is included. ### Tested macOS 27.2, Apple Silicon, AppleClang 21.0.0.21000334, Python 3.9.6, CMake 4.4.4 and Ninja 1.13.2, Release. Based on upstream `0d8c51a54e04c165f108b7bedbba3867e503118a`. - Baseline: the new native weak-definition control fails with native=18 and converted=165. - Fix: the real x86-64 function/data coalescing probe under Rosetta matches the native weak and strong controls. - Relinker-only arm64 build: all 210 registered ctest tests pass. - Relinker-only x86-64/Rosetta build: all 210 registered ctest tests pass. - CTest commands include the temporary Ninja directory on `PATH`; the first runs exposed a missing Ninja in a nested CMake fixture, which passed after correcting that environment. - Linux and Windows guest outputs are byte-identical to the baseline for ordinary and TLS fixtures. Linux and Windows host execution was not tested locally. - TLS weakness is checked in the export trie and header; no full guest TLS-runtime or game execution is claimed. - Conventions and whitespace checks pass. The required overlap check completed without a confirmed conflict. It flagged already-conflicting PRs #5021, #6044, #6618, #6879, #6977 and #7332 for manual shared-file inspection. Their CMake changes add unrelated sources/tests or refactor test registration; #6977 changes DT_INIT handling in the writer, not export weakness. There is no functional dependency; whichever lands later may need to rebase. <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> ### 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 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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
wachi35676 commented 2026-10-20 11:39:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T11:39:13Z.


Verdict: confirm. These are results from Windows and Linux hosts, plus the effect on real guest modules.

Tested at 50bc39a50, merged onto main 02fb1ce53. Relinker-only Release builds on Windows 11 Home 10.0.26300 (Ryzen 5 9600X, WinLibs GCC 15.2.0 r7) and WSL Ubuntu 24.04 (GCC 13.3).

  • ctest: Windows 222/222, WSL 227/227 (main: 221/221 and 226/226). macos_guest_weak_exports runs its structural checks on both hosts.

  • Negative control: the PR's test_macos_guest_weak_exports.py against main's relinker fails with AssertionError: (2, False, {'_Sample#guest': (0, 65536), '_Data#guest': (0, 70528)}).

  • PS5 homebrew (PPSA99005, PPSA47402, PPSA99581, PPSA99995 with 16 Wine modules), relinked with --windows --to-intel, --macos --to-rosetta and Linux --to-intel. The Windows and Linux outputs are byte-identical to main (8 of 8 sets). In the macOS output, only the guest modules change. The export counts below are from the export trie of each .guest.prx, with main → this PR:

    module exports weak MH_WEAK_DEFINES bytes changed
    libc.prx (same file in all four titles) 2566 0 → 1010 off → on 1027
    libvulkan.prx 9907 0 → 731 off → on 748
    ntdll.prx 766 0 → 53 off → on 70
    wineps5.prx 342 0 → 11 off → on 28
    libgnutls.prx 2707 0 → 2 off → on 19
    wow64native, wowprospero, ws2_32 5 / 70 / 15 0 → 1 each off → on 18 each

    The other Wine modules and every app.macho are unchanged. File sizes are unchanged, and export counts don't change. I have no Mac, so I can't run these under dyld; this PR now marks about 1000 libc.prx exports as weak definitions for coalescing.

AI-assisted: yes (Claude Code); results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6290000772 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8118#issuecomment-6290000772). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-20T11:39:13Z. --- Verdict: confirm. These are results from Windows and Linux hosts, plus the effect on real guest modules. Tested at `50bc39a50`, merged onto `main` `02fb1ce53`. Relinker-only Release builds on Windows 11 Home 10.0.26300 (Ryzen 5 9600X, WinLibs GCC 15.2.0 r7) and WSL Ubuntu 24.04 (GCC 13.3). - `ctest`: Windows 222/222, WSL 227/227 (`main`: 221/221 and 226/226). `macos_guest_weak_exports` runs its structural checks on both hosts. - Negative control: the PR's `test_macos_guest_weak_exports.py` against `main`'s relinker fails with `AssertionError: (2, False, {'_Sample#guest': (0, 65536), '_Data#guest': (0, 70528)})`. - PS5 homebrew (PPSA99005, PPSA47402, PPSA99581, PPSA99995 with 16 Wine modules), relinked with `--windows --to-intel`, `--macos --to-rosetta` and Linux `--to-intel`. The Windows and Linux outputs are byte-identical to `main` (8 of 8 sets). In the macOS output, only the guest modules change. The export counts below are from the export trie of each `.guest.prx`, with `main` → this PR: | module | exports | weak | `MH_WEAK_DEFINES` | bytes changed | |---|---|---|---|---| | `libc.prx` (same file in all four titles) | 2566 | 0 → 1010 | off → on | 1027 | | `libvulkan.prx` | 9907 | 0 → 731 | off → on | 748 | | `ntdll.prx` | 766 | 0 → 53 | off → on | 70 | | `wineps5.prx` | 342 | 0 → 11 | off → on | 28 | | `libgnutls.prx` | 2707 | 0 → 2 | off → on | 19 | | `wow64native`, `wowprospero`, `ws2_32` | 5 / 70 / 15 | 0 → 1 each | off → on | 18 each | The other Wine modules and every `app.macho` are unchanged. File sizes are unchanged, and export counts don't change. I have no Mac, so I can't run these under dyld; this PR now marks about 1000 `libc.prx` exports as weak definitions for coalescing. AI-assisted: yes (Claude Code); results above were run on this machine.
thepoppfish commented 2026-10-25 09:13:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by thepoppfish on 2026-10-25T09:13:58Z.

Review: APPROVED


Source-focused review of exact head 50bc39a50db7b91298dbac24963d1925a1f9845e. No blocking findings.

The new binding check sets ExportWeakDefinition only for STB_WEAK and carries the flag through both ordinary and TLS exports. This matches the existing executable-export path in MacOsMachOPatcher.cpp. MacOsImage.cpp carries the TLS descriptor's flags into the final export entries before deriving MH_WEAK_DEFINES, so TLS-only weakness reaches the header too. The existing filtering of local, undefined, absolute, hidden and internal symbols remains in place; strong exports keep zero flags.

I reviewed all three changed files and the structural/native-control test paths. The test covers strong/weak function, data and TLS metadata on every host, and on supported macOS hosts compares function/data coalescing with native weak and strong providers. Its CI guard prevents missing Rosetta from silently skipping the native probe there. TLS runtime coalescing is not covered or claimed.

My local checks: tools/check_conventions.py --base origin/main --head refs/review/8118, whitespace checks, a clean merge-tree against main 2742004ed28f765c7147db0e0269ff5868e9eb6f, and Python syntax compilation of the new regression. I did not build or execute this PR's tests or run macOS/dyld locally.

The existing Windows/Linux report and macOS/Rosetta report already provide independent runtime/structural results and negative controls at this head; those are their reported results, not new runs by me. This approval adds a source review. No new title, TLS-runtime or full-integration claim is made. AI-assisted review: yes (OpenAI Codex).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642411987 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8118#pullrequestreview-5642411987). Originally posted by [thepoppfish](https://github.com/thepoppfish) on 2026-10-25T09:13:58Z. **Review:** APPROVED --- Source-focused review of exact head `50bc39a50db7b91298dbac24963d1925a1f9845e`. No blocking findings. The new binding check sets `ExportWeakDefinition` only for `STB_WEAK` and carries the flag through both ordinary and TLS exports. This matches the existing executable-export path in `MacOsMachOPatcher.cpp`. `MacOsImage.cpp` carries the TLS descriptor's flags into the final export entries before deriving `MH_WEAK_DEFINES`, so TLS-only weakness reaches the header too. The existing filtering of local, undefined, absolute, hidden and internal symbols remains in place; strong exports keep zero flags. I reviewed all three changed files and the structural/native-control test paths. The test covers strong/weak function, data and TLS metadata on every host, and on supported macOS hosts compares function/data coalescing with native weak and strong providers. Its CI guard prevents missing Rosetta from silently skipping the native probe there. TLS runtime coalescing is not covered or claimed. My local checks: `tools/check_conventions.py --base origin/main --head refs/review/8118`, whitespace checks, a clean merge-tree against main `2742004ed28f765c7147db0e0269ff5868e9eb6f`, and Python syntax compilation of the new regression. I did not build or execute this PR's tests or run macOS/dyld locally. The existing [Windows/Linux report](https://github.com/boykopovar/AnyPS5/pull/8118#issuecomment-6290000772) and [macOS/Rosetta report](https://github.com/boykopovar/AnyPS5/pull/8118#issuecomment-6380930259) already provide independent runtime/structural results and negative controls at this head; those are their reported results, not new runs by me. This approval adds a source review. No new title, TLS-runtime or full-integration claim is made. AI-assisted review: yes (OpenAI Codex).
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!8118
No description provided.