I am trying to assemble and link this tiny x86 assembly code in Linux (Ubuntu 18.04 LTS):
;hello.asm
global _start
extern scanf, printf, exit
section .data
read_name db '%255s', 0
msg db 'Hello, %s', 0
section .text
_start:
sub esp, 256
push esp
push read_name
call scanf
add esp, 8
push esp
push msg
call printf
add esp, 264
push dword 0
call exit
I am using nasm
to assemble and ld
to link. As you can probably tell, the code uses C functions, so it has to be linked to glibc
. Since my code is using a _start
, rather than a main
, I decided that it would be better to link to a shared library, since the C runtime needs some startup code to run in _start
if the binary is linked statically.
The problem is that I cannot get my code to link, most probably because I am not using the correct glibc .so
. This is the way I assemble and link:
nasm -f elf32 hello.asm
ld hello.o -o hello -dynamic-linker /lib/libc.so.6 -lc -m elf_i386
The output file gets created, however when I try to run it this is what I get:
./hello
bash: ./hello: No such file or directory
Doing a quick search, it turns out that these are all the libc .so
s on my computer:
locate libc.so
/lib/x86_64-linux-gnu/libc.so.6
/snap/core/8268/lib/i386-linux-gnu/libc.so.6
/snap/core/8268/lib/x86_64-linux-gnu/libc.so.6
/snap/core/8689/lib/i386-linux-gnu/libc.so.6
/snap/core/8689/lib/x86_64-linux-gnu/libc.so.6
/snap/core18/1668/lib/i386-linux-gnu/libc.so.6
/snap/core18/1668/lib/x86_64-linux-gnu/libc.so.6
/usr/lib/x86_64-linux-gnu/libc.so
Can anyone tell me how to link to glibc? (I also get the same problem for 64-bit code)