9

I have made a nasm assembly hello world program like this:

global start


section .text

start:
    mov rax, 0x20000004
    mov rdi, 1
    lea rsi, [rel msg]
    mov rdx, msg.len
    syscall

    mov rax, 0x20000001
    mov rdi, 0
    syscall


section .data

msg:    db  "Hello, World!", 10
.len:   equ $ - msg

Doing nasm -f macho64 print.asm does fine, but ld -macosx_version_min 10.10 -o print print.o gives an error like so:

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 have no idea what this means :). Could someone please help me fix this problem? Thanks! P.S. I am using a Mac with OS X Yosemite.

Jerfov2
  • 5,264
  • 5
  • 30
  • 52
  • 3
    Probably because you need to use `_main` instead of `start` or you need to tell ld to use `start` instead of `_main`. – Ross Ridge Jul 04 '15 at 16:46
  • 1
    @RossRidge How would I do that? – Jerfov2 Jul 04 '15 at 17:07
  • 1
    I would have guessed you want `_start` with an underscore, but try `_main`, too. It is not causing this problem, but be aware there's a bad bug in Nasm-2.11.08 with `-f macho64`. Avoid that version. – Frank Kotler Jul 04 '15 at 18:23
  • 3
    I fixed it by using `_main` – Jerfov2 Jul 04 '15 at 19:30
  • 2
    I think the main problem is that you compile with default libraries. This means that the default start-up-code will be linked into your program. This code will do some initializations and then call the C function "main()". – Martin Rosenau Jul 04 '15 at 21:29
  • nasm / yasm + `ld` won't link the standard libraries, unless OS X is very different from GNU `ld`. On 64bit Linux, `GLOBAL _start` and `_start:` at the top of my stand-alone asm did the trick. (Without that, I got a warning from `ld` that it couldn't find a `_start`, and was just going to start at `0x04000c0`, which is where `yasm` put my code's start address in the `.o`. – Peter Cordes Jul 13 '15 at 04:33

0 Answers0