fix(relinker): reject macOS fixups outside writable segments #11045
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11045"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-25T15:02:44Z.
What
The macOS image writer emitted a dyld fixup for any target inside a segment, whatever the segment's protection: on untouched
4017310678,import_fixture()with an addedR_X86_64_RELATIVEat 0x4100 (inside thePF_R|PF_XPT_LOAD) converts withrelinker --macos --skip-sce-module, exits 0, and itsLC_DYLD_INFO_ONLYrebase stream holds a rebase at__ELF0+0x100, a segment withinitprot5. maximilianfv ran such an output on an Apple M1 Pro with macOS 27.0.1 (x86-64 under Rosetta 2,LC_BUILD_VERSIONminos and sdk 11.0): it ends with SIGBUS indyld4::Loader::applyFixupsGenericbeforemain, anddyld_info -fixupsreportspointer rebase is in non-writable segment '__ELF0'.Locateincore/relinker/elfpatcher/src/macos/MacOsImage.cpp, which both the rebase loop and the bind loop call, throwsMach-O fixup target is not in a writable segmentwith the target RVA when the segment holding the 8-byte target lacksVM_PROT_WRITE; the CLI exits 2 and writes no output. Only an executableR_X86_64_RELATIVEinto a non-writablePT_LOADreaches it today:WindowsRelocationBuilder::Applyadds it to the base relocations withoutRequireWritable, while executable imports (R_X86_64_64,GLOB_DAT,JUMP_SLOT) and every guest-module relocation inMacOsGuestModuleWriterpassRequireWritablefirst, an executableR_X86_64_64,GLOB_DATorJUMP_SLOTagainst a defined symbol is refused, and the metadata and TLS descriptor fixups lie in theRW__APS5DATA. The Windows output is unchanged: PE base relocations into read-only sections are valid. Pointer fixups in executable segments, whichdyld_info -fixupsalso reports, are not rejected: it would require changes to seven existing fixtures that rebase intoRWXsegments, and on the same M1 Pro dyld loads and runs aPF_R|PF_W|PF_Xcase; this is recorded with those measurements indocs/dev/debt/relinker.mdafter the macOS GS entry.test_macos_output.pygets a rebase opcode decoder (rebases) andcheck_fixup_protection, called aftercheck_runpath_lists: the fixture adds aRELATIVEat file offset 0x5C30 and setsDT_RELASZto 72. Target 0x5200 (PF_R|PF_W) must exit 0 with a rebase at('__ELF1', 0x200); target 0x4100 (PF_R|PF_X) must exit 2 withFAIL: Mach-O fixup target is not in a writable segment (offset 0x10100)on stderr and no output.test_interpreter_in_code.py:fixturetakes the flags of the dataPT_LOADthat itsRELATIVEtargets; the--macosruns use 6, the--windowsruns keep 4, so the PE output still covers aRELATIVEinto a read-onlyPT_LOAD. With 4 it was one of two existing tests with a fixup into a non-writable segment of macOS output (__ELF2,initprot1). The other came with #10561:strict_fixtureintest_dynamic_segment.pymarks its singlePT_LOADPF_R|PF_Xbut kept theRELATIVEat 0x300 ofoptional_plt_fixture, and itsstrict-trailing-bytes-after-nullcase also runs with--macos unused-filter=2, which this check refuses (jotitadev's merge-skew report).9e66ff6b20, jotitadev's suggested line applied by adriadam10, sets that fixture'sDT_RELASZto 0, so both strict cases still test theDT_NULLtail, now without aRELATIVErelocation in the Linux,--windowsand--macosruns alike.pr_overlap.pyreports no shared lines with open pull requests that merge withmain. It cannot compare this branch with #5021 and #8076 (docs/dev/debt/relinker.md) and with #10249 (MacOsImage.cpp), because those do not merge withmain. Checked by hand: the debt entry of this PR is a new bullet after the one on GS accesses in macOS output (line 41 onmain); #5021 adds a macOS TLS bullet before the--trace-importsbullet and #8076 adds a bullet on embedded data and relative jump tables after thesce_modulesyscall scan bullet, both near the top of the list. #10249 changes one expression inWriteMacOsImage, the displacement of a TLS stub jump (line 595 of its base), notLocate(line 222 onmain), where this PR adds the writable-segment check. The trial merges of this branch with each of them conflict in the same files and regions asmaindoes, none of them in lines of this PR.Tested
Windows 11, GCC 15.2.0 (WinLibs UCRT), relinker-only Release build (
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON); synthetic fixtures only, no title or homebrew.macos_outputwith the new test and without theLocatecheck fails withfixup-code: rebase into a non-writable segment accepted: 0after the 0x5200 control passed (e05ba6feebwith theLocateline removed). With the change it passes in 0.6 s.interpreter_in_codewith flags 6 for--macosand 4 for--windowspasses with the change and with e05ba6feeb'sMacOsImage.cpp; with flags 4 for both targets it passes without the change and fails with it on its first--macoscase, after the three--windowscases, withFAIL: Mach-O fixup target is not in a writable segment (offset 0x18300); with flags 6 for both it passes.ctest -Ron 15 tests:macos_outputandinterpreter_in_code; the seven with rebases intoRWXsegments (elf_offsets,none_relocations,optional_rela,overlapping_relocations,sce_code_extent,windows_bss,macos_fixed_base);relinker_conversionandrelinker_game_patch(C++ suites that convert to macOS);relinker_executables,guest_none_relocations,game_patchesandcli_args: 15 passed in 32.8 s.ctest -R 'macos|tls|sce_relro': 50 passed in 116.2 s (machine under load from parallel builds).e05ba6feeb(no relinker changes since4017310678); the rebased branch was rebuilt and the counter-check and the passing runs above were rerun on it; theinterpreter_in_coderuns above ran on the follow-up commit 397d534c83, with and without theLocatecheck.ctest --test-dir build-relinker -j 4one05ba6feeb: 292 tests, 289 passed,long_pathsskipped; failures known on this machine onmainas well:select_tests(Windows symlink fixture) andpatch_advisor_unit(path with spaces, fixed by open #10667); both fail again when run alone.Whatparagraph reworded to his macOS measurements,interpreter_in_codeflags per target), rebuilt:ctest -Ron 18 tests (macos_output,interpreter_in_code, the seven with rebases intoRWXsegments,relinker_conversion,relinker_game_patch,guest_none_relocationsand the six othermacos_tests) passed in 50.4 s; full suitectest --test-dir build-relinker -j 6: 292 tests, 288 passed,long_pathsskipped,select_testsandpatch_advisor_unitfailed as above, andshader_cache_runtime_sourcestimed out at 180 s, alone as well, and passes in 235 s when run without the ctest timeout; it builds the shader recompiler, which this PR does not touch.git merge-treeof the branch with upstream/main (6bc1139698), #10712, #10946, #10947, #10971, #10861 and #7173: clean.main13926067ac(e5297264e3and0326845257, the same changed lines as d42c3dc7fd and 397d534c83) and added9e66ff6b20. That head merged withmain8f255109ca: full suitectest --test-dir build-relinker -j 6: 368 tests, 365 passed,long_pathsskipped, onlyselect_testsandpatch_advisor_unitfailed as above;dynamic_segment,macos_outputandinterpreter_in_codepassed. On macOS, jotitadev ran the full suite of9e66ff6b20merged ontomain5d52b06ff3on arm64 (361/361), and maximilianfv ran it withoutaudio_in|voiceonmain801d5b9bc1as x86_64 under Rosetta 2 (359/359).python tools/check_conventions.py --base upstream/mainwith this title and description passed.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T15:10:33Z.
Review: APPROVED
Reviewed exact head
d42c3dc7fd72722939c9c1ae6d0af7e2b4e065bc. The sharedLocateguard checks that the full 8-byte fixup target is inside a segment and that the segment has write permission, covering both rebase and bind emission. The new writable-data success case and read-execute rejection case exercise the boundary; the existing interpreter fixture is made writable for its relocation. The author reports a negative control and 292-test relinker run; I reviewed the test/code and hosted Linux, Windows, macOS arm64/x86_64, ASan and conventions checks pass. Product-specific jobs are skipped by path policy. I did not independently run the macOS-target fixture locally.Imported from boykopovar/AnyPS5. Originally posted by maximilianfv on 2026-10-25T19:19:48Z.
Review: CHANGES_REQUESTED
The
Locatecheck is correct and needed: on macOS 27.0.1 the output it now rejects dies inside dyld beforemain, and every other test and fixture still converts and runs. The code needs no change. The only change requested is one sentence in the debt entry, which says dyld refuses fixups in executable segments, but here such an output loads and runs.Tested on Apple M1 Pro, macOS 27.0.1 (x86_64 build under Rosetta 2)
Applied at
d42c3dc7fdto mugurc's macOS series (27e968974) merged withmainat2742004ed2, with other open macOS fixes, then toggled only this PR. Full rebuild each way (the relinker,relinker_conversion_tests,relinker_game_patch_testsand the otherlibrelinker_coreusers relinked). The direct runs userelinker --macos --skip-sce-moduleonimport_fixture()with the extraRELATIVEat 0x5C30 andDT_RELASZ72 thatcheck_fixup_protectionbuilds, run withlibc.prxandlibkernel.prxfrom the same build inlibs/.macos_*,relinker_*,*tls*,interpreter_in_code,sce_relro,sce_code_extent,elf_offsets,none_relocations,optional_rela,overlapping_relocations,windows_bss,game_patches,cli_args, ...)macos_outputwith only the test changes (noLocateline)fixup-code: rebase into a non-writable segment accepted: 0hello from a PS5 ELF on macOSRELATIVEto 0x5200 (PF_R|PF_W)RELATIVEto 0x4100 (PF_R|PF_X)dyld_info -fixups:pointer rebase is in non-writable segment '__ELF0'. Running it: SIGBUS,EXC_BAD_ACCESS (code=2, address=0x140010100)indyld4::Loader::applyFixupsGenericFAIL: Mach-O fixup target is not in a writable segment (offset 0x10100), no outputRELATIVEto 0x4100 with the codePT_LOADset toPF_R|PF_W|PF_Xdyld_info -fixups:pointer rebase is in executable segment '__ELF0'. Runs and prints, exit 0So on this host, dyld does not refuse the non-writable rebase with a message. It writes the rebase and faults on the read-only page. Either way the output never reaches
main, which justifies the rejection.docs/dev/debt/relinker.md:37: "macOS output still writes pointer fixups into executable segments, which dyld refuses ... Not run on macOS." On macOS 27.0.1 under Rosetta, an output with a rebase into anRWX__ELF0loads, applies the fixup and runs. Onlydyld_inforeports it, as a diagnostic. The output declaresminos 11.0,sdk 11.0inLC_BUILD_VERSION, which may be why dyld enforced neither rule here: thePF_R|PF_Xcase died on the page fault, not on a refusal. Fix: state what was measured (dyld_info flags it, and dyld on macOS 27.0.1 x86_64 under Rosetta loads and runs it), or name the dyld version and declared SDK that refuse it.core/relinker/elfpatcher/src/macos/MacOsImage.cpp:192: the message gives the writer's RVA (0x10100 for the ELF address 0x4100). The other relocation errors,Runtime import target is not writablefromWindowsLoadImage::RequireWritable, give the ELF address, so 0x10100 cannot be found withreadelf -r. Fix: report the ELF address.core/relinker/relinker/tests/test_interpreter_in_code.py:27: this test also converts with--windows. With flags 6 it no longer covers aRELATIVEinto a read-onlyPT_LOADin PE output, which the PR keeps valid on purpose. I did not check whether another test covers that. Fix: keep flags 4 for the--windowsrun and use 6 only for--macos.AI-assisted: yes (Claude Code applied the PR, ran the tests and the fixtures, read the code and wrote this review).
Imported from boykopovar/AnyPS5. Originally posted by maximilianfv on 2026-10-25T22:56:42Z.
Review: APPROVED
Approving at
397d534c8. The requested change is in: the debt entry now states what was measured instead of claiming that dyld refuses the case.docs/dev/debt/relinker.mdno longer says dyld refuses fixups in executable segments. It records thedyld_info -fixupsdiagnostics, the RWX case that loads and runs here, and the RX case that ends with SIGBUS indyld4::Loader::applyFixupsGeneric, and marks other versions as not measured.test_interpreter_in_code.pypasses flags 6 only for--macos, so the--windowsrun keeps covering aRELATIVEinto a read-onlyPT_LOAD.Locateonly has RVAs, and the writer's own__APS5DATAfixups have no ELF address, so the RVA, which matches the otherLocatemessages and converts as you describe, is fine.MacOsImage.cppand the other code are unchanged sinced42c3dc7fd, so my earlier macOS results still apply to this head. Those were 63/63 relinker tests passing both ways, and thePF_R|PF_Xfixture going from a SIGBUS in dyld to a clean conversion error.AI-assisted: yes (Claude Code compared the two heads, checked the replies against the code and wrote this review).
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-27T05:35:20Z.
Review: COMMENTED
Merge-skew check: head
397d534c8merged onto currentmain(1a7ac56d27, also reproduced onabed035468) breaksdynamic_segment. The PR's CI is green because it ran before #10561 landed.Apple M2 Max, macOS 27.2, Xcode 27, relinker-only Release arm64 (
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON), full suite.mainalone (8aa1f9ef7f): 355/355.main(8aa1f9ef7f) + this PR: 354/355, onlydynamic_segmentfails (3/3 runs, not flaky):Cause: #10561 (
68f5474263, merged in47d389e6da) addedstrict-trailing-bytes-after-nulltotest_dynamic_segment.py, and it also runs in--macos unused-filter=2. Itsstrict_fixture()sets the singlePT_LOADtoPF_R|PF_X(flags = 5) so the strict filter accepts the layout, but the inheritedoptional_plt_fixture()keeps aR_X86_64_RELATIVEat0x300inside that segment. The newLocatecheck correctly rejects that rebase. Making the segment writable (7) does not work, because the strict filter then fails withunsupported executable segment layout.Minimal fix in the test, keeping what the case checks (DT_NULL tail handling under the strict filter): drop the RELATIVE relocation from the strict fixture.
(
0x400 + 5 * 16 + 8is the value ofDT_RELASZ.) With it,dynamic_segmentpasses 3/3 with this PR,dynamic_segment|macos|interpreter_in_codeis 11/11, and the test still passes onmainwithout the PR.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-27T08:48:31Z.
Review: COMMENTED
Follow-up on the merge skew I reported: fixed at head
9e66ff6b2.Apple M2 Max, macOS 27.2, Xcode 27, relinker-only Release arm64 (
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON), this head merged onto currentmain5d52b06ff3(clean merge).dynamic_segment: 3/3 pass.test_dynamic_segment.pyreverted tomain's version,dynamic_segmentfails again withMach-O fixup target is not in a writable segment (offset 0x10300)onstrict-trailing-bytes-after-null, so the fixture change is what makes it pass.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by maximilianfv on 2026-10-27T09:41:57Z.
Review: APPROVED
Re-review at
9e66ff6b2: approve. TheLocatecheck and its tests are unchanged since397d534c8. The new fixture commit removes the clash with #10561, and the full relinker suite passes on currentmainwith this PR.Tested on Apple M1 Pro, macOS 27.0.1 (x86_64 build under Rosetta 2)
A relinker-only Release build (
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON -DCMAKE_OSX_ARCHITECTURES=x86_64) ofmainat801d5b9bc1, with and without the diff at9e66ff6b2. The full suite ran with-E 'audio_in|voice'.macos_outputdynamic_segmentinterpreter_in_codetest_dynamic_segment.pyfrommainstrict-trailing-bytes-after-null,--macos unused-filter=2,FAIL: Mach-O fixup target is not in a writable segment (offset 0x10300)MacOsImage.cppfrommainfixup-code: rebase into a non-writable segment acceptedSo the fixture change in
9e66ff6b2is required once #10561 is in, and the newmacos_outputcase fails without theLocatecheck. The x86_64 build adds a second architecture to jotitadev's arm64 run.9e66ff6b2setsDT_RELASZto 0: index 5 oftest_optional_plt.fixture's tag list is(8, 24). That leaves theDT_NULL-tail parsing that both strict cases test unchanged, andconvert()never runs the output.Non-blocking:
Whatdoes not mentiontest_dynamic_segment.pyor the clash with #10561. The sentence "With 4 it was the only existing test with a fixup into a non-writable segment of macOS output" no longer holds, becausestrict_fixturewas a second one.Testedstill says "Rebased ontoe05ba6feeb" and has no run of this head. The line numbers in the Overlap bullet also need updating: the GS debt bullet is at line 41 ofdocs/dev/debt/relinker.mdnow, andLocatestarts at line 222 ofMacOsImage.cpp.core/relinker/relinker/tests/test_dynamic_segment.py:24is jotitadev's suggested line, word for word, from their review. The commit and body do not credit them. Suggestion: add aCo-authored-by: Jota <[email protected]>trailer or a line in the body.DT_RELASZ, so the strict Linux and Windows runs also lose theirRELATIVErelocation, not only--macos. The subject "drop the RELATIVE fixup" describes the effect;StrictNidFilterTestsstill covers the relative-relocation index directly.AI-assisted: yes (Claude Code applied the PR, ran the tests, read the code and wrote this review).