I'm trying to assemble a simple Hello World, which worked fine in the previous macOS version:
global start
section .text
start: mov rax, 0x02000004
mov rdi, 1
mov rsi, msg
mov rdx, 13
syscall
mov rax, 0x02000001
xor rdi, rdi
syscall
section .data
msg: db "Hello world!", 10
Then I use nasm
and ld
as I did before:
$ nasm -f macho64 hello.asm
$ ld hello.o -o hello
But ld
gives me the following error:
ld: warning: No version-min specified on command line
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for inferred architecture x86_64
I tried switching start
to _main
, but got the following:
ld: warning: No version-min specified on command line
ld: dynamic main executables must link with libSystem.dylib for inferred architecture x86_64
Don't even know what that might mean.
ld
needs -lSystem
flag to prevent it from throwing this error. Also it needs -macosx_version_min
to remove the warning. The correct way of using ld
would be: ld hello.o -o hello -macosx_version_min 10.13 -lSystem
.
Updated on macOS 11 and above, you need to pass -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib
as well so that it locates the -lSystem
library correctly. You can use -L$(xcode-select -p)/SDKs/MacOSX.sdk/usr/lib
to evaluate the right path dynamically if required.