Skip to content

Instantly share code, notes, and snippets.

@geoff-nixon
Forked from michaeljclark/macos-syscall.c
Created November 25, 2020 02:13
Show Gist options
  • Save geoff-nixon/16dabe5bb7b6eb56cdd70e4ce0c8c34c to your computer and use it in GitHub Desktop.
Save geoff-nixon/16dabe5bb7b6eb56cdd70e4ce0c8c34c to your computer and use it in GitHub Desktop.
simple macos process with no dependency on libsystem.dylib
/*
* cc -Wall -O3 -c macos-syscall.c -o macos-syscall.o
* ld -static -macosx_version_min 10.12 -pagezero_size 0x1000 macos-syscall.o -o macos-syscall
*/
__attribute__ ((visibility("default"))) extern void start(void) asm("start");
#define NR_exit 0x2000001
#define NR_write 0x2000004
static __inline long __syscall0(long n)
{
unsigned long ret;
__asm__ __volatile__ ("syscall"
: "=a"(ret)
: "a"(n)
: "rcx", "r11", "memory");
return ret;
}
static __inline long __syscall3(long n, long a1, long a2, long a3)
{
unsigned long ret;
__asm__ __volatile__ ("syscall"
: "=a"(ret)
: "a"(n), "D"(a1), "S"(a2), "d"(a3)
: "rcx", "r11", "memory");
return ret;
}
void exit(int status)
{
__syscall0(NR_exit);
__builtin_unreachable();
}
long write(int fildes, const void *buf, unsigned long nbyte)
{
return __syscall3(NR_write, fildes, (long)buf, nbyte);
}
void start()
{
write(1, "hello world\n", 12);
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment