Linking Linux x86-64 assembly hello world program with ld fails
64-bit, assembly, linux, nasm, x86-64
Solution
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...
Problem
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 :)