Files
AnyPS5/docs/dev
Emanueleandmugurc 86cdfb8647 feat(libkernel): thread-local storage for Mach-O guests
FreeBSD code reaches its thread pointer through %fs and macOS cannot set
the fs base. The macOS relinker turns each %fs access of a guest image
into a stub that reads the thread pointer from a pthread key and calls
Aps5GuestTlsAllocate on a thread's first access, registers the TLS
descriptor of an image with Aps5GuestTlsRegister, and routes
__tls_get_addr to Aps5GuestTlsGetAddr. No library provided them, so a
relinked executable could not start.

- Aps5GuestTlsRegister validates the descriptor the relinker wrote and
  creates the key; its destructor frees the block of a thread.
- Aps5GuestTlsAllocateThreadPointer allocates the block (the TLS template
  followed by zeros, then a 0x100-byte thread control block whose first
  word points at itself, as on FreeBSD) and stores the thread pointer
  under the key.
- Aps5GuestTlsAllocate is the entry the stubs call: rax holds the
  descriptor and returns the thread pointer, every other register is
  preserved, the upper halves of the ymm registers only where AVX exists.
- Aps5GuestTlsGetAddr returns the block of the thread plus the offset.

Co-authored-by: mugurc <[email protected]>
2026-10-27 08:42:04 +01:00
..