Repository navigation
Macos libs - #3948
Closed
mohamedtr wants to merge 3 commits into
Closed
Macos libs#3948mohamedtr wants to merge 3 commits into
mohamedtr wants to merge 3 commits into
Conversation
added 3 commits
October 11, 2026 16:57
The relinker already produced macOS executables, but nothing built the
system libraries they load, so no title could start. This ports the
library tree to Darwin: all 122 build as x86-64 Mach-O dylibs and
dlopen cleanly, and a relinked title now reaches its first unresolved
import.
The bulk is glibc APIs that have no macOS spelling: <link.h>, mremap,
/proc/self/maps, getdents64, stat::st_mtim, pthread_getattr_np,
ppoll, RUSAGE_THREAD, memfd_create and others. ElfTypes.hpp carries the
ELF-shaped declarations Darwin needs and defers to <link.h> elsewhere,
so the other platforms are untouched.
Three areas needed more than a substitution:
- The guest arena seeds itself from procfs and reserves gaps with
MAP_FIXED_NOREPLACE. Neither exists on Darwin and mach_vm_region
faults under Rosetta, so the macOS path probes with hinted mmap and
keeps only the chunks the kernel placed exactly.
- Inline assembly fails at link time here, not compile time. Mach-O
has no .type or .size directive, calls need no @plt, and symbols
written in asm need the leading underscore the C compiler adds.
AsmMacros.hpp now covers all three formats, and APS5_EXPORT emits a
tail jump instead of the ELF-only .set alias.
- libc++ lacks std::atomic<std::shared_ptr<T>> and wants a non-const
atomic_ref, so AtomicSharedPtr.hpp provides a mutex-backed drop-in.
__stack_chk_guard needed both a default visibility attribute to be
exported from libc.prx and a carve-out in the NID patcher: it is a
compiler ABI symbol, and renaming it broke the reference every other
library had already recorded against libc.prx.
Two build files also needed forwarding: libjpeg-turbo did not inherit
CMAKE_OSX_ARCHITECTURES, and $ORIGIN is an AnyPS5 loader convention that
dyld cannot resolve.
import_audit.py read ELF and PE only, so it refused every library the macOS build produces. Mach-O exports live in an export trie rather than a symbol table, so this walks the trie from LC_DYLD_INFO(_ONLY) or LC_DYLD_EXPORTS_TRIE, skips weak definitions the way the ELF reader does, and strips the leading underscore dyld hides and shows again on lookup. Running it on PPSA34356 turned up two absent imports, and they turned out to be __stack_chk_fail and __stack_chk_guard: compiler symbols the game imports by NID like any other. Adding them to a list kept verbatim was wrong, because the guest resolves the NID name and nothing exported it. Letting them be renamed to _f7uOxY9mM1U and _Ou3iL1abvng fixes the guest side but leaves libkernel.prx holding a reference to a name libc.prx no longer exports, because PatchChainedImports only rewrites imports that look guest-facing -- an NID postfix or an sce prefix, neither of which a compiler symbol has. Widening that filter to the stack-protector names lets the existing rewrite follow the rename, so both sides agree and every import in the title resolves. The export trie reader has tests built around a real trie: a flat root with one terminal child per name, weak definitions, and the bounds checks.
The relinker's Mach-O writer gives every relinked image a TLS descriptor and rewrites each %fs access into a call-out that reads %fs:0, which on Darwin is pthread_self()->tsd, allocating through Aps5GuestTlsAllocate when it is null. It also binds Aps5GuestTlsRegister from the module init stub and Aps5GuestTlsGetAddr with an addend for a guest that imports __tls_get_addr. None of the three existed, so any title with a PT_TLS segment stopped at dyld with "symbol not found in flat namespace". Aps5GuestTlsAllocate sizes the block from the descriptor, copies the init image, writes the guest thread pointer into the 16 bytes in front of the block so a %fs:-16 access reaches it, and publishes the block through host pthread key 0, which is free because the guest's own pthread keys are emulated in separate storage. Aps5GuestTlsGetAddr is the block itself as a thread-local variable, since the relinker binds its address and adds the guest's offset to it. The variable needs an asm label: this toolchain emits a Mach-O __thread variable only outside an extern "C" block, and the default visibility because the build compiles with -fvisibility=hidden. Verified against the relinker's own TLS fixture and PPSA34356, both of which now clear the TLS binds and stop on Aps5StartGuest instead.
This was referenced Oct 11, 2026
Merged
|
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
|
Contributor
|
Thanks for working on macOS. As submitted, this is hard to review:
AI-assisted: yes. Written with AI assistance (Claude Code); I reviewed it. |
Collaborator
|
Closing: the description is empty, Conventions fails (title, checklist, 138 comments), and the three commits are three topics over 45 files that conflict with main. Please reopen as one PR per topic with What and Tested filled in; the library build overlaps the macOS part of #3891, so check against it first. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Tested
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)