3 ms·
macOS, in some sense a BSD (at least nominally), would like you to not make system calls yourself as well. Actually, not linking against libc has a number of hi
by saagarjha 6y ago
macOS, in some sense a BSD (at least nominally), would like you to not make system calls yourself as well. Actually, not linking against libc has a number of hilarious consequences, one of which is that you bypass the platform sandbox because apparently the engineers thought it couldn't be possible to write a program without it :P
- khrbtxyz 6y agoIs there an example somewhere on how to link without libc and make my own syscalls? I tried this a while ago (can't remember which version of macOS it was), fiddling with Csu, nasm etc. but couldn't quite figure it out.
- saagarjha 6y ago-static -nostdlib, and make sure you have an entry point set.
- deleted 6y ago[deleted]
- jmillikin 6y agohttps://john-millikin.com/unix-syscalls#darwin https://john-millikin.com/unix-syscalls#darwin is a small, "hello world" example.
- saagarjha 6y ago> Note that I have left out the instructions to statically link binaries because they are documented as unsupported That's a bit annoying, especially since you're already using raw syscall numbers anyways. Here's how to make it static: .intel_syntax noprefix #include <sys/syscall.h> #define UNIX_SYSCALL 0x2000000 .globl start start: mov rax, UNIX_SYSCALL | SYS_write mov rdi, 1 lea rsi, text[rip] lea rdx, length syscall mov rax, UNIX_SYSCALL | SYS_exit xor rdi, rdi syscall text: .asciz "Hello, world!\n" .equ length, . - text Compile that with clang -static -nostdlib.
- jmillikin 6y agoYou don't have to change the source or compile with `clang` -- switching the LD command to: ld -arch x86_64 -o hello hello.o -macosx_version_min 10.8 -static -e _main is sufficient if you're determined to violate the OS vendor's compatibility requirements.
- saagarjha 6y agoThat works too, but I'm lazy :P