Skip to content

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

Merged
adriadam10 merged 4 commits into
boykopovar:mainfrom
DavidSamir:feat/relinker-macos-fixed-base
Oct 19, 2026
Merged

adriadam10 merged 4 commits into
boykopovar:mainfrom
DavidSamir:feat/relinker-macos-fixed-base

Conversation

@DavidSamir

@DavidSamir DavidSamir commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

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 ce991f1 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:

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 PS5 OpenGL Showcase (PPSA99005) #6289 (macOS; the next stops of VLC and XPSemu are named above)
  • GPUs tested: n/a
  • AI-assisted: yes

@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

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

ycarogabriel-lgtm added a commit to ycarogabriel-lgtm/AnyPS5 that referenced this pull request Oct 17, 2026
…a fixed base with --macos-fixed-base (@DavidSamir)

Open upstream PR that adds macOS behaviour this branch did not have. The only conflict was a list both sides appended to (a debt index or the relinker test registrations), so both entries are kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ycarogabriel-lgtm added a commit to ycarogabriel-lgtm/AnyPS5 that referenced this pull request Oct 18, 2026
…a fixed base with --macos-fixed-base (@DavidSamir)

Open upstream PR that adds macOS behaviour this branch did not have. The only conflict was a list both sides appended to (a debt index or the relinker test registrations), so both entries are kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@jotitadev jotitadev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions github-actions Bot added unblocks title Names a title ID on the Unblocks line needs rebase Conflicts with main, rebase needed labels Oct 18, 2026
@adriadam10 adriadam10 self-assigned this Oct 19, 2026
WriteMacOsImage wrote the Windows ImageBase constant into the Mach-O
segment and section addresses, __PAGEZERO, the metadata load bias, the
TLS descriptors and the init pointer, while the relocations it rebases
were computed from the WindowsLoadImage base. Read the base from the
load image everywhere instead. Every caller still builds its image at
ImageBase, so the output is unchanged.
…fixed-base

Ruffle (PPSA68091), VLC (PPSA85300) and XPSemu (PPSA97358) read
app0/imports.txt, whose import slot addresses assume the executable's
lowest ELF load page at 0x400000, and dereference them at once. macOS
output always placed that page at 0x140010000, so all three fault on
their first slot read. Linux and Windows have --linux-fixed-base and
--windows-fixed-base for this; macOS had no equivalent.

--macos-fixed-base <address> builds the load image so that the lowest
ELF load page is at the address, as --windows-fixed-base does. The
Mach-O header takes the 64 KiB below it and __PAGEZERO ends there. The
executable is MH_EXECUTE without MH_PIE, so dyld does not slide it. The
address must be 64 KiB aligned, from 0x20000 through 0x7fffffff0000,
and an image ending past the macOS user address space is refused.
macos_fixed_base relinks the fixed-base fixture with and without
--macos-fixed-base 0x400000 and checks that every segment and section
moves by the same amount, __PAGEZERO ends 64 KiB below the base, the
lowest load page is at the base, the entry offset is unchanged, and the
metadata load bias and the relocated slot hold base-relative addresses.
It also checks the lowest base and the refused options and images.

cli_args covers the parsing, range, repetition and --macos requirement.
@adriadam10
adriadam10 force-pushed the feat/relinker-macos-fixed-base branch from e4ff927 to a29f7d0 Compare October 19, 2026 08:34
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 19, 2026
@adriadam10
adriadam10 merged commit 0fa6ddb into boykopovar:main Oct 19, 2026
12 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

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.

ycarogabriel-lgtm added a commit to ycarogabriel-lgtm/AnyPS5 that referenced this pull request Oct 19, 2026
Brings in main's 49 newer commits. Seven of the PRs this branch carried
are now in main (boykopovar#6647, boykopovar#6667, boykopovar#6668, boykopovar#6685, boykopovar#6686, boykopovar#6767, boykopovar#6802), so
the branch shrinks by them.

Conflicts:
- GuestGetrlimit: main adds a check that 1024 stdio streams open on
  Windows; it runs after this branch's descriptor exhaustion helper,
  which Windows and macOS share.
- the relinker keeps this branch's macOS fixture test beside main's
  macOS fixed-base test from boykopovar#6802.
- the libkernel and shader debt files take main's text and keep this
  branch's short macOS entries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ycarogabriel-lgtm added a commit to ycarogabriel-lgtm/AnyPS5 that referenced this pull request Oct 25, 2026
…a fixed base with --macos-fixed-base (@DavidSamir)

Open upstream PR that adds macOS behaviour this branch did not have. The only conflict was a list both sides appended to (a debt index or the relinker test registrations), so both entries are kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build docs relinker unblocks title Names a title ID on the Unblocks line

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants