1

It seems to not be possible to build a shared lib from relocable objects with R_X86_64_PC32 references and I don't understand why.

These references are IP relative and so are position independent. So why ld tell me this is not the case and I have to use -fPIC which generate a GOT reference ?

relocation R_X86_64_PC32 against symbol `infolib' can not be used when making a shared object; recompile with -fPIC
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847

1 Answers1

1

As it is said here : Difference in position-independent code: x86 vs x86-64

IP-relative offset does not work for shared libraries, because global symbols can be overridden, so x86-64 breaks down when not built with PIC.

We have to use -fPIC to go through GOT which is updated at runtime for symbol overriding.

  • 2
    Or you can make your internal symbol references use "hidden" ELF visibility, so they're protected from symbol interposition and *can* use normal `call rel32` for functions and RIP-relative addressing for private static data (C `static` keyword, as opposed to globals). You can make "hidden" aliases for global symbols so you can efficiently can functions inside your library; glibc does that in its own source code. (often with a `weakref` attribute) – Peter Cordes Feb 06 '21 at 22:55
  • IIRC, GCC has an option to make the default symbol visibility "hidden", so you have to manually mark global vars and functions you want to be "exported" from your shared library. – Peter Cordes Feb 06 '21 at 22:56