3

I've been toying around with x86 64 bit assembly on linux recently and after compiling a seemingly simple program I am left scratching my head :P

Although I compile and link it throws no errors and produces a linux ELF When i try to run it I get:

.:[ h4unt3r@sp3ctr4l-h0st asm ]:.
#(0)> ./hello 
bash: ./hello: No such file or directory

I assume its producing an invalid ELF file which is why it reports hello is not there even though it IS. Not sure why-- I'll probably keep playing around with it, just curious if this can be solved the easy way ^_^

Here is my compile / link command line:

nasm -f elf64 hello.s -g
ld -o hello hello.o -lc

Here is the code:

section .data
    msg: db "Hello, world!",0xa,0

section .text
    extern printf
    global main

main:
    push rbp
    mov rbp, rsp

    mov rdi, msg
    xor rax, rax
    call printf
    xor rax, rax

    pop rbp
    ret

Edit -- I do not want to use gcc :)

5
  • If it's worth anything, the file works if you remove the -lc flag and comment out the extern of printf.
    – zxcdw
    Mar 17, 2013 at 21:19
  • How do you figure? :) Then printf is undefined symbol...
    – h4unt3r
    Mar 17, 2013 at 22:07
  • Actually I had the printf call commented out too, but what I meant to say is that if the file isn't linked against libc it works fine.
    – zxcdw
    Mar 17, 2013 at 22:27
  • Again how do you figure? :) Are you using ld? Becasue gcc does all the proper linking for you... like incldugin libs so if you do -nostdlib or -nodefaultlibs it will fail with undefined symbol
    – h4unt3r
    Mar 17, 2013 at 23:04
  • This is the command I used to link. Probably the linking is incomplete causing the program not able to load properly. Maybe someone with better knowledge can explain. ld -o hello /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o hello.o /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/5/crtbegin.o -lc --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/5/crtend.o /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crtn.o -dynamic-linker /lib64/ld-linux-x86-64.so.2
    – Den
    Apr 4, 2017 at 9:38

3 Answers 3

4

I am running 32-bit hardware, and can't test 64-bit stuff. I have seen this "no such file" error in 32-bit code. ld is, by default, using "/lib/ld-linux.so.1" - you can see this string in plain text in the executable. This is the file that doesn't exist (obviously "hello" is right there!). The solution is to tell ld -I/lib/ld-linux.so.2. I suspect that a similar solution would work for 64-bit, but I don't know what "interpreter" or "dynamic linker" you need. Try lookng in the executable for a similar string, and looking in your libs for a similar .so. You shouldn't "need" to use gcc... but gcc knows where to find this stuff! May be easier to use it. Heck of a confusing error, isn't it?

(I would expect your entrypoint to be _start, not main if you're going to do it this way. You won't be able to ret from this - use sys_exit or exit().)

I'm not familiar with the error nrz mentions about "symbol table 0". Surely not a deliberate change to Nasm! The Nasm developers hang out on or around http://www.nasm.us and would be delighted to hear feedback and bug reports there. (Okay, not "delighted" about bug reports, perhaps.) I'll see if I can find out anything...

FWIW, Nasm defaults to "stabs" debugging info with just the -g switch. To enable "dwarf" debugging info, -F dwarf... Supposed to work better...

1
  • Thanks! You were totally spot on :)
    – h4unt3r
    Mar 18, 2013 at 1:09
3

First, to use printf, you need to do linking with gcc instead of ld:

gcc -o hello hello.o

Then, another problem may be the same I have encountered myself. I'm not sure if it's a bug or an intended change in NASM:

user@computer:~/code/asm$ nasm -f elf64 hello.asm -g; gcc -o hello hello.o
/usr/bin/ld: error: relocation section 9 uses unexpected symbol table 0
collect2: error: ld returned 1 exit status

I have resolved that problem by moving to YASM, and doing the assembling and linking this way:

yasm -f elf64 hello.s -g dwarf2
gcc -o hello hello.o

Which produces an executable with the expected output:

./hello
Hello, world!
3
  • 1
    Well see thats the thing... I dont want to use gcc for linkage, I want to use ld :) gcc if I understand calls ld afterwards...
    – h4unt3r
    Mar 17, 2013 at 21:30
  • @h4unt3r you're right, The problem is that the standard library is missing
    – stdcall
    Mar 17, 2013 at 21:45
  • Well no, because the symbol printf is in stdlib and I link that explcitly with my ld flag -lc
    – h4unt3r
    Mar 17, 2013 at 22:07
2

Modify the code a little bit to this.

section .data
        msg:    db "Hello, world!",0xa,0
section .text
        extern  printf
 global _start
_start:
        ; RSP already 16-byte aligned, ready for a function call.
        mov     rdi,msg           ; or better, lea rdi, [rel msg]
        xor     eax, eax          ; AL=0  - no XMM args
        call    printf

        mov     rax,60  ; use _exit syscall
        mov     rdi,0   ; exit code 0
        syscall         ; call kernel

and link using this to set the correct modern ld.so path, not the ld default which doesn't exist on modern GNU/Linux systems.

ld hello.o -o hello -lc --dynamic-linker /lib64/ld-linux-x86-64.so.2

Note that this only works for dynamically-linked executables. If you link statically, you need to manually call glibc's init functions to init its data structures (like stdio buffers) before functions like printf will work.

2
  • int 0x80 is not preferred with 64-bit code (it is possible to generate a Linux kernel with no 32-bit code compatibility). The preferred way is via the SYSCALL instruction (careful, the system call numbers and the how data is passed differs from int 0x80). The use of ret is not an issue if linking with the C runtime as main is a function and there is a return address on the stack that goes back to the C runtime function that called it (one just has to ensure they follow the rules of which registers are caller/callee saved) Apr 4, 2017 at 15:17
  • @MichaelPetch Modified to syscall based on your input. Thanks!
    – Den
    Apr 4, 2017 at 15:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.