feat(nid): patch NIDs in Mach-O libraries #2247

Merged
mugurc merged 1 commit from upstream-pr/2247 into main 2026-10-10 06:15:04 +00:00
mugurc commented 2026-10-09 13:10:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mugurc on 2026-10-09T13:10:18Z.


What

nid_patcher turns the _nid_postfix names of a library into NIDs for ELF and PE. This adds Mach-O, so that a library built on macOS gets its NIDs too. It is the piece of the macOS port that ninja libs needs there; the build changes that use it come in separate PRs. Nothing changes for ELF and PE.

  • A Mach-O dylib keeps its exports in a dyld export trie and its imports in the chained fixups, so the patcher works on those. The trie is read, every exported C name (without Mach-O's leading underscore) is resolved to its NID, and the trie is rebuilt the way ld64 lays it out: node offsets are uleb128, so the layout is repeated until it is stable. A trie that no longer fits is appended to __LINKEDIT, which has to end the file, and a signed image is refused.
  • Weak definitions that are not marked _nid_postfix or _nid_no_patch keep their names, because dyld coalesces those across images by name.
  • In the chained imports, names bound to a .prx dylib that are guest facing (a _nid_postfix name or an sce prefix) are patched in place. A name that would grow, or that shares storage with another import, is an error. An image that uses bind opcodes instead of chained fixups is refused.
  • --preserve-exports reads a reference Mach-O the way it reads ELF and PE references.

Tested

  • New test nid_patcher_macho: builds small Mach-O dylibs byte by byte and runs nid_patcher on them. It checks the rebuilt export trie (including a weak export that must stay), the patched and untouched chained imports, a trie that grows and is appended to __LINKEDIT, --preserve-exports, and the three refusals (signed image, bind opcodes, an import NID longer than its name). It needs no macOS. I also ran it against two copies of the patcher with a deliberate break each (no import patching, weak exports patched), and both fail it.
  • macOS 26.5 (M5 Max): the patcher turns all 121 libraries of the macOS port into the same bytes the port builds, checked with cmp, and the port's patched_libraries test loads them all.
  • Linux and Windows: the full build and ctest, run by manual dispatch on my fork: https://github.com/mugurc/AnyPS5/actions/runs/37931715161 (Linux 508 of 508, Windows 517 of 517, nid_patcher_macho included). The relinker jobs on a pull request do not build nid_patcher, so they do not cover this change.
  • 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:4800857014 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2247). Originally posted by [mugurc](https://github.com/mugurc) on 2026-10-09T13:10:18Z. --- ### What `nid_patcher` turns the `_nid_postfix` names of a library into NIDs for ELF and PE. This adds Mach-O, so that a library built on macOS gets its NIDs too. It is the piece of the macOS port that `ninja libs` needs there; the build changes that use it come in separate PRs. Nothing changes for ELF and PE. - A Mach-O dylib keeps its exports in a dyld export trie and its imports in the chained fixups, so the patcher works on those. The trie is read, every exported C name (without Mach-O's leading underscore) is resolved to its NID, and the trie is rebuilt the way ld64 lays it out: node offsets are uleb128, so the layout is repeated until it is stable. A trie that no longer fits is appended to `__LINKEDIT`, which has to end the file, and a signed image is refused. - Weak definitions that are not marked `_nid_postfix` or `_nid_no_patch` keep their names, because dyld coalesces those across images by name. - In the chained imports, names bound to a `.prx` dylib that are guest facing (a `_nid_postfix` name or an `sce` prefix) are patched in place. A name that would grow, or that shares storage with another import, is an error. An image that uses bind opcodes instead of chained fixups is refused. - `--preserve-exports` reads a reference Mach-O the way it reads ELF and PE references. ### Tested - New test `nid_patcher_macho`: builds small Mach-O dylibs byte by byte and runs `nid_patcher` on them. It checks the rebuilt export trie (including a weak export that must stay), the patched and untouched chained imports, a trie that grows and is appended to `__LINKEDIT`, `--preserve-exports`, and the three refusals (signed image, bind opcodes, an import NID longer than its name). It needs no macOS. I also ran it against two copies of the patcher with a deliberate break each (no import patching, weak exports patched), and both fail it. - macOS 26.5 (M5 Max): the patcher turns all 121 libraries of the macOS port into the same bytes the port builds, checked with `cmp`, and the port's `patched_libraries` test loads them all. - Linux and Windows: the full build and `ctest`, run by manual dispatch on my fork: https://github.com/mugurc/AnyPS5/actions/runs/37931715161 (Linux 508 of 508, Windows 517 of 517, `nid_patcher_macho` included). The relinker jobs on a pull request do not build `nid_patcher`, so they do not cover this change. - 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
popcoder565 commented 2026-10-09 16:07:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-09T16:07:12Z.


Native Windows run, since no Windows results were posted. Setup: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head f3c51474, merged onto main d70b8998 together with #1324, #2039 and #2193.

  • nid_patcher_macho passes on Windows.
  • Full ctest in that build: 512 of 512. That needed two local workarounds in #2193, which doesn't build on Windows (reported there).

🤖 Addressed by Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6084619488 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2247#issuecomment-6084619488). Originally posted by [popcoder565](https://github.com/popcoder565) on 2026-10-09T16:07:12Z. --- Native Windows run, since no Windows results were posted. Setup: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head `f3c51474`, merged onto `main` d70b8998 together with #1324, #2039 and #2193. - `nid_patcher_macho` passes on Windows. - Full ctest in that build: 512 of 512. That needed two local workarounds in #2193, which doesn't build on Windows (reported there). _🤖 Addressed by [Claude Code](https://claude.com/claude-code)_
adriadam10 commented 2026-10-10 06:15:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T06:15:00Z.


Checked the build and ctest scoped to core/libs (11/11, nid_patcher_macho included), a clean merge onto current main, and popcoder565's native Windows run of the new test. ELF/PE paths are unchanged and the Mach-O test pins the rebuilt trie, the patched and untouched chained imports, --preserve-exports and the three refusals. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094554662 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2247#issuecomment-6094554662). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T06:15:00Z. --- Checked the build and ctest scoped to `core/libs` (11/11, `nid_patcher_macho` included), a clean merge onto current `main`, and popcoder565's native Windows run of the new test. ELF/PE paths are unchanged and the Mach-O test pins the rebuilt trie, the patched and untouched chained imports, `--preserve-exports` and the three refusals. Merging.
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!2247
No description provided.