1

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?

Thayne
  • 6,619
  • 2
  • 42
  • 67

1 Answers1

0

I was able to get this working by changing call puts to call puts wrt ..plt.

I figured this out after reading secion 9.2.5 of the nasm documentation: http://www.nasm.us/doc/nasmdoc9.html#section-9.2.5.

Thayne
  • 6,619
  • 2
  • 42
  • 67