Search code examples
macosassembly64-bitbuild-process

How does one build an x64 assembly program in OS X?


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.

Doh! ld would have included crt1.o automatically if my assembly's entry point had been _main instead of _start.


Solution

  • 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.