views:

590

answers:

2

I'm trying to build Hello World in x64 assembly on my Leopard MacBook Pro. It assembles fine, but I get this error when I try to link it: ld: symbol dyld_stub_binding_helper not defined (usually in crt1.o/dylib1.o/bundle1.o) for inferred architecture x86_64

I loaded it with ld -o hello64 hello64.o -lc

My assembler is Yasm.

EDIT: As far as I can tell, unlike for 32-bit code, you have to supply the stub helper yourself, and since I don't know how the 64-bit stub helper works I'll do as Bastien said and have GCC link it since it includes it's own stub helper.

+2  A: 

You haven't specified an assembler. Personally, I had done this before using yasm assembler (and had written a blog post in that regard but since my blog has been down for a long time, I can't post a link). Basically, a hello world would be:

SECTION .data
   hello db 'hello, world', 10
   hellolen equ $ - hello

SECTION .text
   global start

start:
   mov rax, 0x2000004    ; sys_write
   mov rdi, 1            ; stdout 
   mov rsi, qword hello  ; string
   mov rdx, hellolen     ; length
   syscall
   mov rax, 0x2000001    ; sys_exit
   xor rdi, rdi          ; exit code
   syscall

Assembled with:

yasm -f macho64 file.asm
ld a.o
./a.out
Mehrdad Afshari
I'm using Yasm as well. But on OS X, you really aren't supposed to use the syscalls since they are not only undocumented they can change on any given version number increase, and in any case I still need to be able to call library functions.
Michael Morris
@Micheal: Yeah, but you aren't really supposed to code in x64 asm *most* of the time... When I did that, I was actually experimenting with x64 syscalls ;) and I thought you're building something standalone (w/o linking to libc).
Mehrdad Afshari
+2  A: 

Simply let GCC handle the linker invocation. Something like this:

gcc hello64.o -o hello64

Your assembly code will probably have to define a main symbol, or maybe start.

[Edit]

The reason why I'm suggesting this is that different platforms invoke the linker differently. If you see which arguments GCC passes to the linker with the --verbose command-line option, you will probably realize that it's complicated and implementation-dependent.

Bastien Léonard