nasm - Can't link object file with ld on macOS

2020-07-10 07:01发布

问题:

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.

回答1:

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.