Author Topic: hello world link error  (Read 15718 times)

Penfold

  • Guest
hello world link error
« on: July 25, 2007, 04:54:14 PM »
can anyone help with an error I'm getting.

I copied and pasted hello.asm from http://www.csee.umbc.edu/help/nasm/sample.shtml#hello

I assembled it using nasm and the command nasm -f elf -l hello.lst  hello.asm as suggested by the comment in the program.

However when I tried to compile using the command gcc -o hello  hello.o (also suggested in the program) I got the following error message:

pd:~/src penfold$ gcc -o hello  hello.o
/usr/bin/ld: hello.o bad magic number (not a Mach-O file)
collect2: ld returned 1 exit status

In case its relevant I'm working on a mac book and typing the commands to the compiler into a terminal window.

I rather like the sound of the error message but its jolly cryptic too.

can anyone help?

best regards
Penfold

nobody

  • Guest
Re: hello world link error
« Reply #1 on: July 25, 2007, 06:22:29 PM »
Hi Penfold,

Well, it's not a Mach-o file 'cause you asked for "-f elf"! If you've got a version of Nasm that includes "-f macho", that's what you want. If you haven't got such a version - "nasm -hf" gives a list of supported output formats - you'll want to get one.

http://www.opensource.apple.com/darwinsource/10.4.9.x86/

Or the forked (apparently) version Soniram mentions... (you do *not* want any 0.99.xx at the present time!)

MacOS isn't Linux - more like BSD. Push the parameters and call a stub which does int 80h and returns. Or, you can push any "dummy" value - doesn't have to be a valid return address.

push msg_len
push msg
push 1 ; stdout
mov eax, 4 ; __NR_write
push eax ; dummy value
int 80h
add esp, 4 * 4

push 0  ; exit code
mov eax, 1 ; __NR_exit
push eax
int 80h

I would expect the examples you link to that use libc would work just the same in Linux/BSD/Mach-o. Linux/elf doesn't want underscores on externs (main, printf, etc.) - I *think* MacOS want's 'em...

Nice to see some interest in Nasm on the Mac!

Best,
Frank

Penfold

  • Guest
Re: hello world link error
« Reply #2 on: July 25, 2007, 06:46:52 PM »
many thanks Frank! I didn't have a clue what the commands to the compiler mean so I just tried them as they were. Now however I am older and wiser and know that 'f' stands for format.

Your suggestion has solved my problem. Its popped up a new one but its unrelated and looks like its syntactic so I'll fiddle around with it for a while before I bother anyone with screams for help.

once again thank you!

Seymour