From the nasm tutorial here I got the following code for an assembly program:
global main
extern puts
section .text
main:
mov rdi, message
call puts
mov rax, 0
ret
message:
db "Hola, mundo", 0
I assemble it with nasm -felf64 hola.asm
However, I haven't been able to link the resulting hola.o file.
I have tried (per the tutorial) running gcc hola.o
but that results in the following error:
/usr/local/bin/ld: error: hola.o: requires dynamic R_X86_64_PC32 reloc against 'puts' which may overflow at runtime; recompile with -fPIC
collect2: error: ld returned 1 exit status
passing -fPIC
to doesn't help, and neither does -Wl,-I/lib/ld-2.25.so
.
My attempts to link directly with ld have failed, I think because I'm missing whatever actually defines the _start
symbol, resulting in seg fault when I urn the resulting executable.
I also tried adding a default rel
as suggested at nasm issue relocation R_X86_64_PC32 shared library, but that doesn't work either.
I've also tried writing the main method in C, then calling a function that calls puts in assembly. That also gives me the dynamic reloc error.
How can I link a nasm assembly file so it can call a function in libc on x86_64 linux?