I’ve compiled a rough plan for porting libc to macos:
Phase 1: Basic Process Control & Syscall Foundation
- Implement Core Syscall Interface: Create the assembly and header files for the kernel interface in
__support/OSUtil/darwin/for bothaarch64andx86_64. This is the mechanism for all subsequent syscall wrappers. - Implement
exitandabort: Following @michaelrj-google’s advice Implement_exit(unistd/darwin/),exit(stdlib/darwin/), andabort(stdlib/darwin/). This makes a “full build” at least runnable, as it can now terminate. - Implement
__assert_fail: Create the Darwin-specific handler forassert.h. This is for internal development and testing of all subsequent modules. It can be built usingabort.
Phase 2: Core Synchronization Primitives
- Create Futex Abstraction: Following @SchrodingerZhu’s advice, implement a platform-specific futex-like wait/wake API in
__support/threads/darwin/. This will be built on the macOSos_sync_wait_on_addressAPI. - Implement
Mutex: Port the genericMutexclass (__support/threads/mutex.h) to use this newos_sync_wait_on_addressabstraction. - Port Thread Primitives: Use the new futex abstraction to implement the other core thread primitives, such as
call_onceandCndVar.
Phase 3: File I/O and stdio
With synchronization available, this phase implements the I/O subsystems, enabling basic functionality like printf.
- Implement Low-Level I/O Wrappers: Create the Darwin implementations for
fcntl,unistd(read,write,lseek,close), andsys/stat(fstat). These are the syscalls needed for file operations. - Implement
__support::File: Create the__support/File/darwin/implementation. This internal abstraction requires theMutexfrom Phase 2 for thread-safe operations. - Implement
stdio: Create thestdio/darwin/backend. This will provide theFILE*structures (includingstdin,stdout,stderr) and functions (fopen,fprintf,fread), which are built on top of the__support::Fileabstraction.
Phase 4: Memory, Time, and Process Management
This phase adds the remaining core components required for a modern POSIX environment.
- Implement
mman: Createsys/mman/darwin/to provide memory management functions likemmap,munmap, andmprotect. - Implement Time: Create
__support/time/darwin/for time abstractions andsys/time/darwin/for syscalls likegettimeofday. - Implement Architecture-Specifics: Implement the
setjmp/darwin/aarch64/andx86_64/assembly functions forsetjmpandlongjmp. - Implement Process & Signals: Create
spawn/darwin/,sys/wait/darwin/, andsignal/darwin/to support process creation and signal handling.
Phase 5: Full POSIX Compliance and pthread
This final phase completes the library by implementing the remaining high-level POSIX APIs.
- I/O Multiplexing: Implement
poll(poll/darwin/) andselect(sys/select/darwin/). Thepollimplementation can be built on top of the macOS-nativekqueueAPI (sys/event.h). - Networking: Implement the
sys/socket/darwin/API. - Complete
sys/Modules: Implement the remaining POSIXsys/modules, such assys/resource,sys/utsname, andtermios. - Implement
pthreads: Build out the fullpthread/API (e.g.,pthread_create) on top of the synchronization primitives from Phase 2 and the thread management APIs. - Dynamic Linking: Implement
dlfcn/darwin/(dlopen,dlsym). (These are missing from linux too)
I surely missed a lot more things, please let me know your thoughts.