feat(relinker): write Mach-O output for macOS with --macos #1597

Merged
mugurc merged 1 commit from upstream-pr/1597 into main 2026-10-10 01:13:22 +00:00
mugurc commented 2026-10-08 14:03:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mugurc on 2026-10-08T14:03:16Z.


What

Adds --macos to the relinker: the executable and the bundled modules are written as x86-64 Mach-O files (modules as dylibs in app0/sce_module) that run under Rosetta on Apple silicon. This is the second piece of the macOS port after #927, as announced on #1 (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6060813714).

  • MacOsMachOPatcher writes the executable and GuestModuleWriter::WriteMacOs the modules, both through MacOsImage. The ELF segments keep their layout (__ELF0, __ELF1, ...). Imports bind by NID to the prx libraries through @rpath, exports and TLS imports use the dynamic symbol names, and the original program headers and the image metadata go to __APS5DATA,__meta, the section the unwinder from #927 reads.
  • FreeBSD code reaches its thread pointer through %fs, which macOS cannot set. Each %fs access becomes a stub that reads the thread pointer from a pthread key and allocates the TLS block on a thread's first access; dynamic TLS goes through the image's descriptor instead of __tls_get_addr.
  • Relocations and imports are collected with the existing Windows::WindowsLoadImage and WindowsRelocationBuilder. Moving them to general/ is a refactor for a follow-up.
  • --macos conflicts with --windows. --to-intel with --macos still uses the Intel target here; the Rosetta target comes in its own PR.

The Linux and Windows output is unchanged. The Mach-O output needs the macOS runtime (TLS allocation in libkernel, libraries built for macOS), which comes in separate PRs.

Tested

  • Linux and Windows: this branch's CI run on my fork, https://github.com/mugurc/AnyPS5/actions/runs/37784600652 (Linux 462 of 462, Windows 454 of 454, conventions and report green).
  • New test macos_output: relinks two byte-built guests and reads the Mach-O back (header, segment layout and protections, entry point, dependencies, run path, binds by NID, the TLS descriptor, and that no %fs access is left). It needs no macOS, so it runs in the Linux and Windows jobs.
  • macOS 26.5 on an M5 Max: the relinker builds and all 32 relinker tests pass, macos_output included.
  • Running relinked programs needs the runtime pieces that are not in this PR. On my full port branch (https://github.com/mugurc/AnyPS5/tree/macos-port) 10 of 10 relinked fixtures run under Rosetta, but that is not this branch alone.
  • No commercial title was tested; I have no PS5.

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 (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4787983843 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1597). Originally posted by [mugurc](https://github.com/mugurc) on 2026-10-08T14:03:16Z. --- ### What Adds `--macos` to the relinker: the executable and the bundled modules are written as x86-64 Mach-O files (modules as dylibs in `app0/sce_module`) that run under Rosetta on Apple silicon. This is the second piece of the macOS port after #927, as announced on #1 (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6060813714). - `MacOsMachOPatcher` writes the executable and `GuestModuleWriter::WriteMacOs` the modules, both through `MacOsImage`. The ELF segments keep their layout (`__ELF0`, `__ELF1`, ...). Imports bind by NID to the prx libraries through `@rpath`, exports and TLS imports use the dynamic symbol names, and the original program headers and the image metadata go to `__APS5DATA,__meta`, the section the unwinder from #927 reads. - FreeBSD code reaches its thread pointer through `%fs`, which macOS cannot set. Each `%fs` access becomes a stub that reads the thread pointer from a pthread key and allocates the TLS block on a thread's first access; dynamic TLS goes through the image's descriptor instead of `__tls_get_addr`. - Relocations and imports are collected with the existing `Windows::WindowsLoadImage` and `WindowsRelocationBuilder`. Moving them to `general/` is a refactor for a follow-up. - `--macos` conflicts with `--windows`. `--to-intel` with `--macos` still uses the Intel target here; the Rosetta target comes in its own PR. The Linux and Windows output is unchanged. The Mach-O output needs the macOS runtime (TLS allocation in libkernel, libraries built for macOS), which comes in separate PRs. ### Tested - Linux and Windows: this branch's CI run on my fork, https://github.com/mugurc/AnyPS5/actions/runs/37784600652 (Linux 462 of 462, Windows 454 of 454, conventions and report green). - New test `macos_output`: relinks two byte-built guests and reads the Mach-O back (header, segment layout and protections, entry point, dependencies, run path, binds by NID, the TLS descriptor, and that no `%fs` access is left). It needs no macOS, so it runs in the Linux and Windows jobs. - macOS 26.5 on an M5 Max: the relinker builds and all 32 relinker tests pass, `macos_output` included. - Running relinked programs needs the runtime pieces that are not in this PR. On my full port branch (https://github.com/mugurc/AnyPS5/tree/macos-port) 10 of 10 relinked fixtures run under Rosetta, but that is not this branch alone. - No commercial title was tested; I have no PS5. ### 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 (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-09 22:04:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T22:04:51Z.


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
#2117 core/relinker/cli/src/CliArgs.cpp L66-70
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6090024317 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1597#issuecomment-6090024317). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-09T22:04:51Z. --- <!-- 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 | |---|---|---|---| | #2117 | `core/relinker/cli/src/CliArgs.cpp` [L66-70](https://github.com/boykopovar/AnyPS5/blob/7703fda15e4ab4025dd817eae5964315e6dd134b/core/relinker/cli/src/CliArgs.cpp#L66-L70) | | |
adriadam10 commented 2026-10-10 01:13:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T01:13:18Z.


Checked the build and the relinker ctest scope on Linux (43/43, macos_output included), the head's CI (conventions, eligibility, relinker on ubuntu/windows/macos), and that Linux/Windows output paths are untouched. macos_output fails without the change. Runtime behaviour on macOS/Rosetta is not verified here (it needs the runtime pieces from the follow-up PRs); merging for the structure and the CLI. Note #2117 touches the usage line in CliArgs.cpp, whoever lands second rebases.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6092047548 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1597#issuecomment-6092047548). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T01:13:18Z. --- Checked the build and the relinker ctest scope on Linux (43/43, `macos_output` included), the head's CI (conventions, eligibility, relinker on ubuntu/windows/macos), and that Linux/Windows output paths are untouched. `macos_output` fails without the change. Runtime behaviour on macOS/Rosetta is not verified here (it needs the runtime pieces from the follow-up PRs); merging for the structure and the CLI. Note #2117 touches the usage line in `CliArgs.cpp`, whoever lands second rebases.
mrzacarias commented 2026-10-10 02:19:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mrzacarias on 2026-10-10T02:19:31Z.


Tried mugurc/macos-port (e9db5ebf) with Balatro (PPSA21402) on an M1 Pro, macOS 26.4, LunarG SDK 1.4.363. It gets to gameplay at ~33 FPS. On top of the branch I needed:

Still open: text truncated partway through long batches with a stray triangle to (0,0); host import, draw sync, texture cache and flip pacing are ruled out. Also a crash on exit in sceKernelGetModuleInfoFromAddr (garbage st_size), and an occasional "guest protection or unmap range has a hole" at startup. Happy to send PRs once intake reopens.

Branch: https://github.com/mrzacarias/AnyPS5/tree/balatro-macos

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6092647313 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1597#issuecomment-6092647313). Originally posted by [mrzacarias](https://github.com/mrzacarias) on 2026-10-10T02:19:31Z. --- Tried `mugurc/macos-port` (e9db5ebf) with Balatro (PPSA21402) on an M1 Pro, macOS 26.4, LunarG SDK 1.4.363. It gets to gameplay at ~33 FPS. On top of the branch I needed: - #2015 (nullDescriptor) and #1653 (no fragment barycentric on MoltenVK) - one-lane waves for stages without subgroup support (MoltenVK vertex shaders), instead of skipping as in #1565: https://github.com/mrzacarias/AnyPS5/commit/d215500687a58af11e9198a1cf6bf30578565ff6 - skip multisampled storage-image modes when the device can't write them (MoltenVK): https://github.com/mrzacarias/AnyPS5/commit/f0fbff4c80195ea03f4e32773a451871533f2dc0 - clamp the viewport depth range when the draw doesn't use depth (no VK_EXT_depth_range_unrestricted): https://github.com/mrzacarias/AnyPS5/commit/cbc1bc5dc0cc1ea62f9f9ab4856786266d1fe2e9 - packager: a `Contents/MacOS/app0` link so titles with bundled modules load (https://github.com/mrzacarias/AnyPS5/commit/1571fbaa919b2ac512af8ad83f1acee661a66352), and saves kept in Application Support so repackaging doesn't wipe them (https://github.com/mrzacarias/AnyPS5/commit/a338cf230a3911cd49772fcfa9fcce9d7f94a810) - flip limit of 2 for blocking submits, per #1034: https://github.com/mrzacarias/AnyPS5/commit/f6f6f118eb2648922daebf89de0ed2df90ec9e9e Still open: text truncated partway through long batches with a stray triangle to (0,0); host import, draw sync, texture cache and flip pacing are ruled out. Also a crash on exit in sceKernelGetModuleInfoFromAddr (garbage st_size), and an occasional "guest protection or unmap range has a hole" at startup. Happy to send PRs once intake reopens. Branch: https://github.com/mrzacarias/AnyPS5/tree/balatro-macos
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!1597
No description provided.