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.
In addition to the @Verloren answer above (https://stackoverflow.com/a/52830915/1189569)
I had an issue with macOS Big Sur (macOS 11.1), where flag -lSystem
could not locate libSystem.dylib
, with the error
ld: library not found for -lSystem
I found out for macOS Big Sur, quoted from the link: https://developer.apple.com/documentation/macos-release-notes/macos-big-sur-11_0_1-release-notes
New in macOS Big Sur 11.0.1, the system ships with a built-in dynamic linker cache of all system-provided libraries. As part of this change, copies of dynamic libraries are no longer present on the filesystem. Code that attempts to check for dynamic library presence by looking for a file at a path or enumerating a directory will fail...
that all copies of dynamic libraries are not located in usr/lib/
and similar, so flag -lSystem
could not found libSystem.dylib
by default.
The solution to this was to update/install the latest version of Command Line Tools, if not already, and to set flag -L
of the ld
command to /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib
.
So full command would look like this:
ld hello.o -o hello -macosx_version_min 11.0 -L /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -lSystem
Simpler answer. ld is defaulting to dynamic linking and tries to load crt1 which is looking for main. So specify static linking.
% ld -e start -static hello.o -o hello
% ./hello
Hello world!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With