feat(relinker): write an ad-hoc code signature into macOS output #18450

Merged
SHOnnay merged 1 commit from upstream-pr/18450 into main 2026-11-13 01:28:38 +00:00
SHOnnay commented 2026-11-11 14:50:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SHOnnay on 2026-11-11T14:50:40Z.


What

macOS output, both executables and guest-module dylibs, now carries a built-in ad-hoc code signature. Users no longer need codesign -s - before running it, and codesign --verify --strict accepts the output as written.

Implementation. New core/relinker/elfpatcher/src/macos/MacOsCodeSignature.cpp/.hpp, with SHA-256 implemented in-repo, since the relinker had none. It writes what codesign -s - writes on macOS 27.0.1, confirmed against a codesign-signed dump.

Signature layout. LC_CODE_SIGNATURE is the last load command. It points at a 16-byte-aligned SuperBlob (0xfade0cc0) at the end of __LINKEDIT, which grows to cover it. The SuperBlob holds:

  • CodeDirectory, slot 0:
    • version 0x20400, CS_ADHOC, SHA-256, 4096-byte pages
    • 2 special slots: requirements and an empty Info.plist slot
    • code slots for every page up to the signature, with the last page partial
    • execSegBase/execSegLimit = __TEXT fileoff/filesize, and CS_EXECSEG_MAIN_BINARY for executables
  • an empty requirements blob, slot 2
  • an empty 0xfade0b01 wrapper, slot 0x10000

Identifier and ordering.

  • The identifier is the output file name, as ld64 and lld use. An empty identifier is refused.
  • The signature is computed last, over the final bytes. The UUID hash and the 0x400 load-command reserve are unchanged, so re-signing with codesign -s - -f still works and keeps the entry stub.

Sources:

  • xnu-11215.1.10 osfmk/kern/cs_blobs.h
  • lld llvmorg-19.1.0 lld/MachO/SyntheticSections.cpp CodeSignatureSection
  • ld64-957.1 src/ld/LinkEdit.hpp CodeSignatureAtom

Docs. docs/user/USAGE.md notes that output is signed, that re-signing replaces the signature, and that output edited with install_name_tool or strip must be signed again.

Debt. One entry: verified only on M1 with macOS 27.0.1, and not marked CS_LINKER_SIGNED. ld64 and lld set that flag, which lets editing tools re-sign automatically. Switching is a one-flag change plus dropping two blobs; reviewers can say if they prefer it.

Tested

macOS 27 on Apple M1, relinker-only Release build.

  • Full suite: ctest --test-dir build-relinker -j 4: 780 of 780 on this branch, 779 of 779 on main.
  • New macos_code_signature (TIMEOUT 60, RUN_SERIAL on Apple):
    • checks the whole blob layout, including every page hash against Python hashlib
    • runs codesign --verify --strict and -dvvv (Signature=adhoc, CodeDirectory v=20400, Hash type=sha256)
    • launches the never-externally-signed executable under Rosetta (exit 42) and dlopens the signed guest module
    • re-signs a copy with codesign -s - -f and checks the entry stub survives
    • On main: AssertionError: executable: 0 LC_CODE_SIGNATURE commands
    • On this branch: macOS code signature tests passed
  • Mutation check: all 6 hand mutations failed the test: wrong hash type, execSeg flags forced to 0, zeroed requirements hash, broken SHA-256 padding, __LINKEDIT not grown, a byte changed after signing.
  • First-launch time: about 1 s for unsigned and signed fresh output alike on this machine.
  • #13208: ctest -j 8 -R ^macos_ ×3 passed 11 of 11 on both main and this branch with similar times, so this PR does not claim to fix #13208.
$ python3 tools/check_conventions.py --base upstream/main
All conventions checks passed.

Overlap. No open PR emits a code signature; I searched for "code signature", "codesign" and "ad-hoc signature macOS". pr_overlap.py could only report "cannot confirm" PRs. None of their diffs contains signature terms (LC_CODE_SIGNATURE, 0xfade0c, CodeDirectory, codesign). The MacOsImage.cpp change is about 19 lines, so conflicts with #18373 and #10861 should be small.

Not verified: Intel Macs, macOS before 27, real titles, and the web converter path, which shares this code.

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 (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5261575853 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18450). Originally posted by [SHOnnay](https://github.com/SHOnnay) on 2026-11-11T14:50:40Z. --- ### What macOS output, both executables and guest-module dylibs, now carries a built-in ad-hoc code signature. Users no longer need `codesign -s -` before running it, and `codesign --verify --strict` accepts the output as written. **Implementation.** New `core/relinker/elfpatcher/src/macos/MacOsCodeSignature.cpp/.hpp`, with SHA-256 implemented in-repo, since the relinker had none. It writes what `codesign -s -` writes on macOS 27.0.1, confirmed against a codesign-signed dump. **Signature layout.** `LC_CODE_SIGNATURE` is the last load command. It points at a 16-byte-aligned SuperBlob (`0xfade0cc0`) at the end of `__LINKEDIT`, which grows to cover it. The SuperBlob holds: - **CodeDirectory, slot 0:** - version `0x20400`, `CS_ADHOC`, SHA-256, 4096-byte pages - 2 special slots: requirements and an empty Info.plist slot - code slots for every page up to the signature, with the last page partial - `execSegBase`/`execSegLimit` = `__TEXT` fileoff/filesize, and `CS_EXECSEG_MAIN_BINARY` for executables - an empty requirements blob, slot 2 - an empty `0xfade0b01` wrapper, slot 0x10000 **Identifier and ordering.** - The identifier is the output file name, as ld64 and lld use. An empty identifier is refused. - The signature is computed last, over the final bytes. The UUID hash and the 0x400 load-command reserve are unchanged, so re-signing with `codesign -s - -f` still works and keeps the entry stub. **Sources:** - xnu-11215.1.10 `osfmk/kern/cs_blobs.h` - lld llvmorg-19.1.0 `lld/MachO/SyntheticSections.cpp` `CodeSignatureSection` - ld64-957.1 `src/ld/LinkEdit.hpp` `CodeSignatureAtom` **Docs.** `docs/user/USAGE.md` notes that output is signed, that re-signing replaces the signature, and that output edited with `install_name_tool` or `strip` must be signed again. **Debt.** One entry: verified only on M1 with macOS 27.0.1, and not marked `CS_LINKER_SIGNED`. ld64 and lld set that flag, which lets editing tools re-sign automatically. Switching is a one-flag change plus dropping two blobs; reviewers can say if they prefer it. ### Tested macOS 27 on Apple M1, relinker-only Release build. - **Full suite:** `ctest --test-dir build-relinker -j 4`: 780 of 780 on this branch, 779 of 779 on `main`. - **New `macos_code_signature`** (TIMEOUT 60, RUN_SERIAL on Apple): - checks the whole blob layout, including every page hash against Python `hashlib` - runs `codesign --verify --strict` and `-dvvv` (`Signature=adhoc`, `CodeDirectory v=20400`, `Hash type=sha256`) - launches the never-externally-signed executable under Rosetta (exit 42) and `dlopen`s the signed guest module - re-signs a copy with `codesign -s - -f` and checks the entry stub survives - On `main`: `AssertionError: executable: 0 LC_CODE_SIGNATURE commands` - On this branch: `macOS code signature tests passed` - **Mutation check:** all 6 hand mutations failed the test: wrong hash type, execSeg flags forced to 0, zeroed requirements hash, broken SHA-256 padding, `__LINKEDIT` not grown, a byte changed after signing. - **First-launch time:** about 1 s for unsigned and signed fresh output alike on this machine. - **#13208:** `ctest -j 8 -R ^macos_` ×3 passed 11 of 11 on both `main` and this branch with similar times, so this PR does not claim to fix #13208. ```text $ python3 tools/check_conventions.py --base upstream/main All conventions checks passed. ``` **Overlap.** No open PR emits a code signature; I searched for "code signature", "codesign" and "ad-hoc signature macOS". `pr_overlap.py` could only report "cannot confirm" PRs. None of their diffs contains signature terms (`LC_CODE_SIGNATURE`, `0xfade0c`, `CodeDirectory`, `codesign`). The `MacOsImage.cpp` change is about 19 lines, so conflicts with #18373 and #10861 should be small. **Not verified:** Intel Macs, macOS before 27, real titles, and the web converter path, which shares this code. ### 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: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-12 14:26:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-12T14:26:17Z.

Review: COMMENTED


Reviewed the macOS ad-hoc code signature — no blocking findings

The diff at head 7cb53a29af0af180868561aec7c0ba87eaa934b4 writes a valid-looking ad-hoc signature for macOS output; I found no concrete defect, but I ran no build or test, so the layout is checked by reading only.

What I ran

  • Exact head: 7cb53a29af0af180868561aec7c0ba87eaa934b4, base main.
  • No build, no ctest, no codesign run here (no macOS host).
  • Read the diff hunks of all 12 files in files.json under reviewed.
  • Searched the repository at head for every caller of MacOsMachOPatcher, MacOsImageInput, PadTo and LoadRva, and for the test helpers the new test imports.
Reader calls
repo_read.py search MacOsMachOPatcher
repo_read.py search MacOsImageInput
repo_read.py search LoadRva core/relinker/elfpatcher/src/macos/MacOsImage.cpp
repo_read.py search PadTo core/relinker/elfpatcher/src/macos/MacOsImage.cpp
repo_read.py search "def main_fixture" core/relinker/relinker/tests
repo_read.py search "def executable" core/relinker/relinker/tests

Results

Check Result Note
Build and ctest not run Author reports 780 of 780 on M1
macos_code_signature test not run Author reports pass on M1
Signature layout, read by hand passed Reading only
Callers of the new identifier passed Only Conversion.cpp and the guest writer
__TEXT range used for the exec segment passed Starts at 0, size LoadRva

Findings

Blocking

None.

Non-blocking

  • core/relinker/relinker/tests/test_macos_code_signature.py:134: when the executable reports Bad CPU type and the host is not CI, run_native returns before the dlopen check. A non-CI run then passes without launching the signed executable or loading the module. Make that branch print a skip notice, or fail it.
  • core/relinker/relinker/src/pipeline/Conversion.cpp:162: the identifier is OutputPath.filename(). If that is empty (a path ending in a separator, under std::filesystem rules; not run), the conversion finishes and only then AppendCodeSignature refuses it. Checking it before the conversion starts would fail sooner.

Sources

I linked no behaviour claims. The external references in the description (xnu-11215.1.10, lld llvmorg-19.1.0, ld64-957.1) were not checked.

Not verified

  • Build, the full suite, and every codesign and Rosetta launch: only the author's M1 run is reported.
  • Intel Macs and macOS before 27, as the description states.
  • The 0x400 load-command reserve: I did not find the check that keeps the new LC_CODE_SIGNATURE inside it.
  • Full file text: the 12 files were read as diff hunks plus the repository searches above, not as whole files.
  • The SHA-256 implementation and the SuperBlob and CodeDirectory offsets were checked against the codesign layout by reading, not by running them against hashlib or codesign.

To get this to a good state

  • Make the Rosetta-missing branch in test_macos_code_signature.py fail or print a clear skip (line 134).
  • Optionally refuse an empty identifier before the conversion starts (Conversion.cpp:162).
  • Re-run on the new head: ctest --test-dir build-relinker -R ^macos_code_signature$, then the full suite.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5876100759 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18450#pullrequestreview-5876100759). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-12T14:26:17Z. **Review:** COMMENTED --- # Reviewed the macOS ad-hoc code signature — no blocking findings The diff at head `7cb53a29af0af180868561aec7c0ba87eaa934b4` writes a valid-looking ad-hoc signature for macOS output; I found no concrete defect, but I ran no build or test, so the layout is checked by reading only. ## What I ran - Exact head: `7cb53a29af0af180868561aec7c0ba87eaa934b4`, base `main`. - No build, no `ctest`, no `codesign` run here (no macOS host). - Read the diff hunks of all 12 files in `files.json` under `reviewed`. - Searched the repository at head for every caller of `MacOsMachOPatcher`, `MacOsImageInput`, `PadTo` and `LoadRva`, and for the test helpers the new test imports. <details> <summary>Reader calls</summary> ```text repo_read.py search MacOsMachOPatcher repo_read.py search MacOsImageInput repo_read.py search LoadRva core/relinker/elfpatcher/src/macos/MacOsImage.cpp repo_read.py search PadTo core/relinker/elfpatcher/src/macos/MacOsImage.cpp repo_read.py search "def main_fixture" core/relinker/relinker/tests repo_read.py search "def executable" core/relinker/relinker/tests ``` </details> ## Results | Check | Result | Note | | --- | --- | --- | | Build and `ctest` | not run | Author reports 780 of 780 on M1 | | `macos_code_signature` test | not run | Author reports pass on M1 | | Signature layout, read by hand | passed | Reading only | | Callers of the new identifier | passed | Only `Conversion.cpp` and the guest writer | | `__TEXT` range used for the exec segment | passed | Starts at 0, size `LoadRva` | ## Findings ### Blocking None. ### Non-blocking - `core/relinker/relinker/tests/test_macos_code_signature.py:134`: when the executable reports `Bad CPU type` and the host is not CI, `run_native` returns before the `dlopen` check. A non-CI run then passes without launching the signed executable or loading the module. Make that branch print a skip notice, or fail it. - `core/relinker/relinker/src/pipeline/Conversion.cpp:162`: the identifier is `OutputPath.filename()`. If that is empty (a path ending in a separator, under `std::filesystem` rules; not run), the conversion finishes and only then `AppendCodeSignature` refuses it. Checking it before the conversion starts would fail sooner. ## Sources I linked no behaviour claims. The external references in the description (xnu-11215.1.10, lld llvmorg-19.1.0, ld64-957.1) were not checked. ## Not verified - Build, the full suite, and every `codesign` and Rosetta launch: only the author's M1 run is reported. - Intel Macs and macOS before 27, as the description states. - The `0x400` load-command reserve: I did not find the check that keeps the new `LC_CODE_SIGNATURE` inside it. - Full file text: the 12 files were read as diff hunks plus the repository searches above, not as whole files. - The SHA-256 implementation and the SuperBlob and CodeDirectory offsets were checked against the codesign layout by reading, not by running them against `hashlib` or `codesign`. ## To get this to a good state - Make the Rosetta-missing branch in `test_macos_code_signature.py` fail or print a clear skip (line 134). - Optionally refuse an empty identifier before the conversion starts (`Conversion.cpp:162`). - Re-run on the new head: `ctest --test-dir build-relinker -R ^macos_code_signature$`, then the full suite.
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!18450
No description provided.