16

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.

Verloren
  • 599
  • 3
  • 17
  • 3
    This is why you'd normally link with `cc hello.o -o hello`, because C compilers know how to link object files into executables. BTW, you should use `lea rdi, [rel msg]`, instead of the `mov rdi, imm64` absolute form that requires runtime relocation. – Peter Cordes Oct 16 '18 at 08:29

3 Answers3

22

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.

AlBlue
  • 20,872
  • 14
  • 63
  • 85
Verloren
  • 599
  • 3
  • 17
  • With `10.13` as min version `ld` switches to load command `LC_MAIN` entry point convention handled by `dyld`. You're system exit call forces the process to exit without giving chance to `dyld` to flush buffers (which is irrelevant for system write call, but not for `printf`). See my answers here: https://stackoverflow.com/a/47810074/5329717 https://stackoverflow.com/a/48252805/5329717 – Kamil.S Nov 22 '18 at 11:40
  • So what is the full command to run this hello.asm? I'm using the same tutorial and just replacing your `ld` doesn't work. – Mote Zart Apr 20 '20 at 23:22
  • for learning LC_MAIN, https://h3adsh0tzz.com/2020/01/macho-file-format/ – Izana May 25 '20 at 04:33
3

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
Sihad Begovic
  • 1,706
  • 27
  • 26
2

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!
Olsonist
  • 1,208
  • 1
  • 10
  • 30