Can't link assembly file in Mac OS X using ld

Viewed 18790


I'm trying to run a basic assembly file using 64 Bit Mac OS X Lion, using nasm and ld which are installed by default with Xcode.

I've written an assembly file, which prints a character, and I got it to build using nasm.

nasm -f elf -o program.o main.asm

However, when I go to link it with ld, it fails with quite a few errors/warnings:

ld -o program program.o

ld: warning: -arch not specified
ld: warning: -macosx_version_min not specificed, assuming 10.7
ld: warning: ignoring file program.o, file was built for unsupported file format which is not the architecture being linked (x86_64)
ld: warning: symbol dyld_stub_binder not found, normally in libSystem.dylib
ld: entry point (start) undefined.  Usually in crt1.o for inferred architecture x86_64

So, I tried to rectify a few of these issues, and got nowhere.

Here's one of things I've tried:

ld -arch i386 -e _start -o program program.o

Which I thought would work, but I was wrong.

How do you make the object file a compatible architecture that nasm and ld will agree with?

Also, how would you define the entry point in the program (right now I'm using global _start in .section text, which is above _start, which doesn't seem to do much good.)

I'm a bit confused as to how you would successfully link an object file to a binary file using ld, and I think I'm just missing some code (or argument to nasm or ld) that will make them agree.

Any help appreciated.

4 Answers
Related