Author Topic: can't link nasm files  (Read 6363 times)

nobody

  • Guest
can't link nasm files
« on: October 08, 2005, 09:53:15 PM »
bash-3.00$ cat helloworld.asm
; ----------------------------------------------------------------------------
; helloworld.asm
;
; This is a console program that writes "Hello, World" on one line and
; then exits.  It needs to be linked with a C library.
; ----------------------------------------------------------------------------

global  _start
        extern  printf

section .text
_start:
        push    message
        call    printf
        add     esp, 4
        ret
message:
        db      'Hello, World', 10, 0
bash-3.00$ nasm -f elf helloworld.asm
bash-3.00$ ld helloworld.o -lc
bash-3.00$ ls
#hello.asm#  hello.asm   hello3.asm   hello3.o    helloworld.asm   helloworld.o
a.out        hello.asm~  hello3.asm~  helloworld  helloworld.asm~  printf.lst
bash-3.00$ ./a.out
bash: ./a.out: No such file or directory


-----------------------------------------------------------

I wish to know what am I doing wrong  , because it seems that everything should work just fine... if I use gcc to link it does work, however can't use ld to link.

realcr.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: can't link nasm files
« Reply #1 on: October 09, 2005, 03:13:55 AM »
By default, ld wants to use "/lib/ld-linux.so.1" as an interpreter. That's the "file or directory" that's missing. Add "-I/lib/ld-linux.so.2" to ld's command line, and I think it'll link up correctly...

However, unlike "main", "_start" is jumped-to, not called, so the "ret" won't work to end your program (no return address on the stack) - call either "exit" (the libc function) or "sys_exit" to exit back to bash.

I think those two changes will make it work... might want to add "-o helloworld" to the ld command line, if you don't want your program to be named "a.out"...

Best,
Frank

nobody

  • Guest
Re: can't link nasm files
« Reply #2 on: October 09, 2005, 10:43:13 AM »
It works great , tnx for your help frank.
I still don't get what does -I/lib/ld-linux.so.2 means but I'll probably find a way to figure it out.

thanks again,
bar.