feat(relinker): place macOS executables at a fixed base with --macos-fixed-base #6802

Merged
DavidSamir merged 4 commits from upstream-pr/6802 into main 2026-10-19 10:54:32 +00:00
DavidSamir commented 2026-10-16 12:33:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-16T12:33:22Z.


What

Ruffle (PPSA68091), VLC (PPSA85300) and XPSemu (PPSA97358) read app0/imports.txt, a list of import slot addresses relative to an executable whose lowest load page is at 0x400000, and read each slot straight away. USAGE.md already names the three for --linux-fixed-base, and Windows has --windows-fixed-base. macOS output always places that page at 0x140010000, and no option changes it, so all three crash on their first slot read. A Mach exception port in VLC shows the fault at cmpq $0x0, 0x400000(%rax) with rax = 0x362b2c8, the first address in its imports.txt.

--macos-fixed-base <address> maps the lowest ELF load page of the macOS executable at the address, as --windows-fixed-base does:

  • The load image is built at address - 0x10000. The Mach-O header takes those 64 KiB and __PAGEZERO ends there.
  • The output stays MH_EXECUTE without MH_PIE, so dyld doesn't slide it.
  • The address must be 64 KiB aligned, from 0x20000 through 0x7fffffff0000. An image ending past the macOS user address space is refused.
  • The base also applies to executables selected with --executable, as --linux-fixed-base does. Bundled modules keep their placement.

WriteMacOsImage wrote the Windows ImageBase constant into these places, while the relocations it rebases came from the load image's base:

  • the segment and section addresses and __PAGEZERO;
  • the metadata load bias, the TLS descriptors and the init pointer.

The first commit reads the base from the load image everywhere. Without the option the output is byte-identical to main's.

Commits:

  1. The base from the load image, with no change in output.
  2. The option.
  3. The tests.
  4. USAGE.md.

Tested

macOS 26.7.1, Apple M2, AppleClang 21.0.0, Release. The relinker runs natively, and the titles run as x86-64 under Rosetta. main doesn't build the system libraries on macOS by itself, so the titles ran against the libraries of a local tree: main ce991f1ec with the macOS runtime work merged (#4627, #4630, #6124, the macOS relinker PRs, mugurc's port branches).

  • Relinker tests: all 174 pass. The macos, cli_args, linux_fixed_base and windows tests also pass at each commit.

  • macos_fixed_base relinks the linux_fixed_base fixture with and without --macos-fixed-base 0x400000, and checks:

    • every segment and section moves by the same amount, and the lowest load page is at the base;
    • __PAGEZERO ends 64 KiB below the base, and the entry offset is unchanged;
    • the metadata load bias and the relocated slot hold base-relative addresses;
    • the lowest base 0x20000 is accepted, and an unaligned, low, missing or repeated address and an image past user space are refused.
  • Wrong versions: the test fails for each of 7 deliberately wrong versions:

    • the patcher ignores the base;
    • the load bias, __PAGEZERO or the section addresses keep the default base;
    • the base is off by the header's 64 KiB;
    • the user-space check is removed;
    • the option is accepted without --macos.
  • cli_args covers parsing, the range, repetition and the --macos requirement.

  • Default output: without the option, Ruffle (42,851,864 bytes) and OpenRCT2 Native Port (PPSA99728, 1.0.0, 23,259,368 bytes) relink byte-identical to main's relinker.

  • Titles, catalog sha256 checked, relinked with --macos --to-rosetta:

    Title Without the option With --macos-fixed-base 0x400000
    Ruffle (PPSA68091, 1.2.0) segfault at 3–8 s its game library, still running at 40 s, 3 runs of 3
    VLC (PPSA85300, 0.4) segfault at 3–6 s its log reports 0 of 486 imports unresolved, then stops at gfx: no GPU, 2 runs of 2
    XPSemu (PPSA97358, Alpha-2.1) segfault at 3 s stops on setrlimit: changing the limit of resource 8 is not supported (libkernel), 2 runs of 2

    OpenRCT2 Native Port still runs at 30 s without the option.

  • tools/check_conventions.py --base origin/main and git diff --check pass.

Not tested: Linux and Windows. Their output paths are unchanged. The Linux option's parser now shares its body with the macOS one, and linux_fixed_base and cli_args pass here.

Not tested past the title's own renderer. VLC's own log reports gfx: no GPU right after it resolves its imports. That comes from VLC's renderer, not from an AnyPS5 error, and I haven't found its cause, so VLC is not tested past that point. Wind Waker HD (PPSA99641), whose renderer prints RADV reports no PS5 GPU, suggests such renderers look for a PS5 GPU from inside the title, which AnyPS5 doesn't provide on any platform. That is unconfirmed for VLC. XPSemu's next stop, the setrlimit refusal, is in libkernel, is the same on every platform, and is not changed here.

Overlap:

  • #6314 edits the usage string and the lines next to ParseLinuxFixedBase in CliArgs.cpp, plus the option structs in Cli.hpp, Conversion.hpp and main.cpp. It also adds --linux-fixed-base to the options --verify-patches rejects, and --macos-fixed-base belongs in that list too.
  • #6410, #6419, #6448, #6458, #6466 and #6112 edit MacOsImage.cpp, but none of their hunks changes the lines this branch changes.
  • Several relinker PRs add tests elsewhere in core/relinker/CMakeLists.txt.

tools/pr_overlap.py stopped on a pull request ref it couldn't fetch, so this list comes from the files of the 773 open pull requests. Whichever lands second rebases.

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
  • Unblocks: PPSA68091, PPSA85300 and PPSA97358 #6289 (macOS; the next stops of VLC and XPSemu are named above)
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4888417709 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6802). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-16T12:33:22Z. --- ### What Ruffle (PPSA68091), VLC (PPSA85300) and XPSemu (PPSA97358) read `app0/imports.txt`, a list of import slot addresses relative to an executable whose lowest load page is at `0x400000`, and read each slot straight away. `USAGE.md` already names the three for `--linux-fixed-base`, and Windows has `--windows-fixed-base`. macOS output always places that page at `0x140010000`, and no option changes it, so all three crash on their first slot read. A Mach exception port in VLC shows the fault at `cmpq $0x0, 0x400000(%rax)` with `rax = 0x362b2c8`, the first address in its `imports.txt`. `--macos-fixed-base <address>` maps the lowest ELF load page of the macOS executable at the address, as `--windows-fixed-base` does: - The load image is built at `address - 0x10000`. The Mach-O header takes those 64 KiB and `__PAGEZERO` ends there. - The output stays `MH_EXECUTE` without `MH_PIE`, so dyld doesn't slide it. - The address must be 64 KiB aligned, from `0x20000` through `0x7fffffff0000`. An image ending past the macOS user address space is refused. - The base also applies to executables selected with `--executable`, as `--linux-fixed-base` does. Bundled modules keep their placement. `WriteMacOsImage` wrote the Windows `ImageBase` constant into these places, while the relocations it rebases came from the load image's base: - the segment and section addresses and `__PAGEZERO`; - the metadata load bias, the TLS descriptors and the init pointer. The first commit reads the base from the load image everywhere. Without the option the output is byte-identical to `main`'s. Commits: 1. The base from the load image, with no change in output. 2. The option. 3. The tests. 4. `USAGE.md`. ### Tested macOS 26.7.1, Apple M2, AppleClang 21.0.0, Release. The relinker runs natively, and the titles run as x86-64 under Rosetta. `main` doesn't build the system libraries on macOS by itself, so the titles ran against the libraries of a local tree: `main` ce991f1ec with the macOS runtime work merged (#4627, #4630, #6124, the macOS relinker PRs, mugurc's port branches). - **Relinker tests:** all 174 pass. The `macos`, `cli_args`, `linux_fixed_base` and `windows` tests also pass at each commit. - **`macos_fixed_base`** relinks the `linux_fixed_base` fixture with and without `--macos-fixed-base 0x400000`, and checks: - every segment and section moves by the same amount, and the lowest load page is at the base; - `__PAGEZERO` ends 64 KiB below the base, and the entry offset is unchanged; - the metadata load bias and the relocated slot hold base-relative addresses; - the lowest base `0x20000` is accepted, and an unaligned, low, missing or repeated address and an image past user space are refused. - **Wrong versions:** the test fails for each of 7 deliberately wrong versions: - the patcher ignores the base; - the load bias, `__PAGEZERO` or the section addresses keep the default base; - the base is off by the header's 64 KiB; - the user-space check is removed; - the option is accepted without `--macos`. - **`cli_args`** covers parsing, the range, repetition and the `--macos` requirement. - **Default output:** without the option, Ruffle (42,851,864 bytes) and OpenRCT2 Native Port (PPSA99728, 1.0.0, 23,259,368 bytes) relink byte-identical to `main`'s relinker. - **Titles,** catalog sha256 checked, relinked with `--macos --to-rosetta`: | Title | Without the option | With `--macos-fixed-base 0x400000` | |---|---|---| | Ruffle (PPSA68091, 1.2.0) | segfault at 3–8 s | its game library, still running at 40 s, 3 runs of 3 | | VLC (PPSA85300, 0.4) | segfault at 3–6 s | its log reports `0 of 486 imports unresolved`, then stops at `gfx: no GPU`, 2 runs of 2 | | XPSemu (PPSA97358, Alpha-2.1) | segfault at 3 s | stops on `setrlimit: changing the limit of resource 8 is not supported` (libkernel), 2 runs of 2 | OpenRCT2 Native Port still runs at 30 s without the option. - `tools/check_conventions.py --base origin/main` and `git diff --check` pass. Not tested: Linux and Windows. Their output paths are unchanged. The Linux option's parser now shares its body with the macOS one, and `linux_fixed_base` and `cli_args` pass here. **Not tested past the title's own renderer.** VLC's own log reports `gfx: no GPU` right after it resolves its imports. That comes from VLC's renderer, not from an AnyPS5 error, and I haven't found its cause, so VLC is not tested past that point. Wind Waker HD (PPSA99641), whose renderer prints `RADV reports no PS5 GPU`, suggests such renderers look for a PS5 GPU from inside the title, which AnyPS5 doesn't provide on any platform. That is unconfirmed for VLC. XPSemu's next stop, the `setrlimit` refusal, is in libkernel, is the same on every platform, and is not changed here. Overlap: - #6314 edits the usage string and the lines next to `ParseLinuxFixedBase` in `CliArgs.cpp`, plus the option structs in `Cli.hpp`, `Conversion.hpp` and `main.cpp`. It also adds `--linux-fixed-base` to the options `--verify-patches` rejects, and `--macos-fixed-base` belongs in that list too. - #6410, #6419, #6448, #6458, #6466 and #6112 edit `MacOsImage.cpp`, but none of their hunks changes the lines this branch changes. - Several relinker PRs add tests elsewhere in `core/relinker/CMakeLists.txt`. `tools/pr_overlap.py` stopped on a pull request ref it couldn't fetch, so this list comes from the files of the 773 open pull requests. Whichever lands second rebases. ### 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 - [x] Unblocks: PPSA68091, PPSA85300 and PPSA97358 #6289 (macOS; the next stops of VLC and XPSemu are named above) - [x] GPUs tested: n/a - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-16 12:41:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T12:41:50Z.


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
docs/user/USAGE.md
PR 5925 docs/user/USAGE.md
PR 6044 core/relinker/CMakeLists.txt
PR 6171 docs/user/USAGE.md
PR 6618 core/relinker/CMakeLists.txt
PR 6788 docs/user/USAGE.md
PR 6879 core/relinker/CMakeLists.txt
PR 6924 docs/user/USAGE.md
PR 6946 docs/user/USAGE.md
PR 6947 docs/user/USAGE.md
PR 6977 core/relinker/CMakeLists.txt
PR 7173 core/relinker/CMakeLists.txt
PR 7417 core/relinker/CMakeLists.txt
core/relinker/cli/src/CliArgs.cpp
core/relinker/relinker/src/pipeline/Conversion.cpp
docs/user/USAGE.md
PR 7453 core/relinker/CMakeLists.txt
PR 7594 core/relinker/CMakeLists.txt
PR 7642 docs/user/USAGE.md
PR 7800 docs/user/USAGE.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6215729141 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6802#issuecomment-6215729141). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-16T12:41:50Z. --- <!-- pr-overlap --> 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`<br>`docs/user/USAGE.md` | | [PR 5925](https://www.github.com/boykopovar/AnyPS5/pull/5925) | `docs/user/USAGE.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/relinker/CMakeLists.txt` | | [PR 6171](https://www.github.com/boykopovar/AnyPS5/pull/6171) | `docs/user/USAGE.md` | | [PR 6618](https://www.github.com/boykopovar/AnyPS5/pull/6618) | `core/relinker/CMakeLists.txt` | | [PR 6788](https://www.github.com/boykopovar/AnyPS5/pull/6788) | `docs/user/USAGE.md` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/relinker/CMakeLists.txt` | | [PR 6924](https://www.github.com/boykopovar/AnyPS5/pull/6924) | `docs/user/USAGE.md` | | [PR 6946](https://www.github.com/boykopovar/AnyPS5/pull/6946) | `docs/user/USAGE.md` | | [PR 6947](https://www.github.com/boykopovar/AnyPS5/pull/6947) | `docs/user/USAGE.md` | | [PR 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) | `core/relinker/CMakeLists.txt` | | [PR 7173](https://www.github.com/boykopovar/AnyPS5/pull/7173) | `core/relinker/CMakeLists.txt` | | [PR 7417](https://www.github.com/boykopovar/AnyPS5/pull/7417) | `core/relinker/CMakeLists.txt`<br>`core/relinker/cli/src/CliArgs.cpp`<br>`core/relinker/relinker/src/pipeline/Conversion.cpp`<br>`docs/user/USAGE.md` | | [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` | | [PR 7642](https://www.github.com/boykopovar/AnyPS5/pull/7642) | `docs/user/USAGE.md` | | [PR 7800](https://www.github.com/boykopovar/AnyPS5/pull/7800) | `docs/user/USAGE.md` |
jotitadev commented 2026-10-18 11:28:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-18T11:28:33Z.

Review: COMMENTED


Ran this on macOS, including the converted executable under Rosetta. The Mach-O loader and Rosetta are what the option targets, and the test checks the layout without running the output.

Machine: Apple M2 Max, macOS 27.2 (26B5091g), Xcode 27 clang, relinker-only Release builds. Head e4ff92713 merged onto main 9f25d0bf5 (clean merge).

  • arm64: 187/187.
  • x86_64 under Rosetta with /usr/bin/python3 3.9.6: 187/187, including macos_fixed_base and cli_args. main passes 186/186 on both.
  • Without the option, the macOS output for the linux_fixed_base fixture is byte-identical to main's.

Execution under Rosetta. I converted the linux_fixed_base fixture with --macos --skip-sce-module. Its entry checks that its own address and the relocated slot both equal base + 0x10, then hits int3; on a mismatch it hits ud2. Next to it I put an x86_64 libs/libkernel.prx whose Aps5StartGuest_nid_no_patch just calls the entry, plus an empty libs/libc.prx.

Output Image header (dladdr) Entry Result
default 0x140000000 0x140010010 ud2 (SIGILL), as expected for a fixture built for 0x400000
--macos-fixed-base 0x400000 0x3f0000 0x400010 int3 (SIGTRAP), slot rebased correctly

With the fixture's code rebuilt for each base, the following load at the requested address and reach int3: 0x20000 (the lowest accepted base, with a 64 KiB __PAGEZERO), 0x30000, 0x100000, 0x10000000, 0x100000000 and 0x7f0000000000.

High bases that pass the new check but don't start. The new check refuses images that end past 0x7fffffe00000. Under Rosetta here, the kernel kills the process with SIGKILL before dyld runs, 20 out of 20 runs, for bases 0x7ff700000000, 0x7ff740000000, 0x7ff780000000, 0x7ff7b0000000, 0x7ff7c0000000 and 0x7ff7e0000000. 0x7ff6f0000000 and lower start 20 out of 20. A bisection put the edge near 0x7ff6ff800000, and it moved slightly between runs. The x86_64 shared cache maps from 0x7ff8... and the Rosetta runtime sits just below it at 0x7ff7ffxxx000. None of the titles in the description need such a base. Still, a lower bound in the check, or a note in USAGE.md, would turn a silent SIGKILL into an error at conversion time.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5562736764 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6802#pullrequestreview-5562736764). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-18T11:28:33Z. **Review:** COMMENTED --- Ran this on macOS, including the converted executable under Rosetta. The Mach-O loader and Rosetta are what the option targets, and the test checks the layout without running the output. Machine: Apple M2 Max, macOS 27.2 (26B5091g), Xcode 27 clang, relinker-only Release builds. Head `e4ff92713` merged onto `main` `9f25d0bf5` (clean merge). - arm64: 187/187. - x86_64 under Rosetta with `/usr/bin/python3` 3.9.6: 187/187, including `macos_fixed_base` and `cli_args`. `main` passes 186/186 on both. - Without the option, the macOS output for the `linux_fixed_base` fixture is byte-identical to `main`'s. **Execution under Rosetta.** I converted the `linux_fixed_base` fixture with `--macos --skip-sce-module`. Its entry checks that its own address and the relocated slot both equal `base + 0x10`, then hits `int3`; on a mismatch it hits `ud2`. Next to it I put an x86_64 `libs/libkernel.prx` whose `Aps5StartGuest_nid_no_patch` just calls the entry, plus an empty `libs/libc.prx`. | Output | Image header (`dladdr`) | Entry | Result | |---|---|---|---| | default | `0x140000000` | `0x140010010` | `ud2` (SIGILL), as expected for a fixture built for `0x400000` | | `--macos-fixed-base 0x400000` | `0x3f0000` | `0x400010` | `int3` (SIGTRAP), slot rebased correctly | With the fixture's code rebuilt for each base, the following load at the requested address and reach `int3`: `0x20000` (the lowest accepted base, with a 64 KiB `__PAGEZERO`), `0x30000`, `0x100000`, `0x10000000`, `0x100000000` and `0x7f0000000000`. **High bases that pass the new check but don't start.** The new check refuses images that end past `0x7fffffe00000`. Under Rosetta here, the kernel kills the process with SIGKILL before dyld runs, 20 out of 20 runs, for bases `0x7ff700000000`, `0x7ff740000000`, `0x7ff780000000`, `0x7ff7b0000000`, `0x7ff7c0000000` and `0x7ff7e0000000`. `0x7ff6f0000000` and lower start 20 out of 20. A bisection put the edge near `0x7ff6ff800000`, and it moved slightly between runs. The x86_64 shared cache maps from `0x7ff8...` and the Rosetta runtime sits just below it at `0x7ff7ffxxx000`. None of the titles in the description need such a base. Still, a lower bound in the check, or a note in `USAGE.md`, would turn a silent SIGKILL into an error at conversion time. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
adriadam10 commented 2026-10-19 10:55:56 +00:00 (Migrated from github.com)

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


Re-checked against current main: the head is the earlier rebase plus the --verify-patches rejection of --macos-fixed-base (with its test) and the USAGE.md entry; no logic changes since the last review. Layout, range, alignment and the --macos requirement are covered by macos_fixed_base and cli_args, confirmed on macOS/Rosetta by @jotitadev; relinker CI is green. Merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6265580434 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6802#issuecomment-6265580434). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-19T10:55:56Z. --- Re-checked against current `main`: the head is the earlier rebase plus the `--verify-patches` rejection of `--macos-fixed-base` (with its test) and the USAGE.md entry; no logic changes since the last review. Layout, range, alignment and the `--macos` requirement are covered by `macos_fixed_base` and `cli_args`, confirmed on macOS/Rosetta by @jotitadev; relinker CI is green. 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!6802
No description provided.