0

Well, I use nasm to produce test.o from my test.s. When I then run gcc test.o, it gives me the error message

/usr/bin/ld: test.o: relocation R_X86_64_PC32 against symbol `puts@@GLIBC_2.2.5' can not be used when making a PIE object; recompile with -fPIE /usr/bin/ld: final link failed: bad value collect2: error: ld returned 1 exit status

test.s:
    global main
    extern puts

    section .text
main:
    mov rdi, message
    call    puts
    ret
message:
    db  "Hello World", 0

What this error message means and how to fix it?

Also, in this case i use gcc to link my object file with the standard library, but how can i use ld to do the same job?

I am running Ubuntu 20.04 LTS

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
mathway
  • 143
  • 11
  • Use `call puts wrt ..plt` – Michael Petch Oct 31 '20 at 21:03
  • 1
    Alternatively, link with `-no-pie`. – fuz Oct 31 '20 at 21:08
  • Wow, it actually works... But what this bunch of words after `call` means? Why I found on the internet a lot of `asm` code snippets and used toolchains like `nasm -felf64 test.s && gcc test.o && ./a.out`, but in my case that didn't work? – mathway Oct 31 '20 at 21:11
  • 1
    My answer on the linked duplicate [Can't call C standard library function on 64-bit Linux from assembly (yasm) code](https://stackoverflow.com/a/52131094) explains what this does. The tutorials you're finding were written before `-pie` was the default for gcc. See also [32-bit absolute addresses no longer allowed in x86-64 Linux?](https://stackoverflow.com/q/43367427) which will also be a potential problem if you're following tutorials written for non-PIE linking. – Peter Cordes Oct 31 '20 at 21:15
  • Ok, thx you all!! – mathway Oct 31 '20 at 21:19

0 Answers0