Search code examples
gccexecutableobject-files

MWE among object file compiled with and without `-fPIC`


I was doing some experiment with object file produced by gcc.

While I know, at least superficially, what is the use of position independent code, I was trying to spot it concretely.

I produced this simple code in a file timestwo.c:

 int timestwo(int x)
 {
     return 2 * x;
 }

which I compiled:

  • gcc -c timestwo.c
  • gcc -c -fPIC timestwo.c -o timestwo_pic.o

then the resulting object files timestwo.o and timestwo_pic.o are bitwise the same. Also with readelf -a I cannot spot any difference.

Can you provide a MWE in which i can see the difference between pic and non-pic code?


Solution

  • Nowadays it's probable that your distro's GCC compiler emits PIC code by default, so to ensure it emits non-PIC code you should specify -fno-PIC.

    Anyhow, you are not going to see any difference with example code, like yours, that references only local symbols and/or constants. PIC only bites when code references an external symbol - whose runtime address will be an absolute value for a non-position-independent program but for a position-independent program will be computed at runtime by looking up the program's Global Offset Table (GOT), which has been patched up by the dynamic linker with the symbol addresses it has actually decided on.

    Here's an elementary file that compiles differently with -fPIC v. -fno-PIC:

    $ cat file.c
    extern int foo;
    
    int bar()
    {
        return foo;
    } 
    
    
    $ gcc -c -fno-PIC file.c -o file_no_PIC.o -save-temps
    $ gcc -c -fPIC file.c -o file_PIC.o -save-temps
    

    I'm specifying -save-temps just to generate the respective assembly listings for comparison.

    The object binaries are different:

    $ diff file_no_PIC.o file_PIC.o
    Binary files file_no_PIC.o and file_PIC.o differ
    

    Here's the diff between the assembly listings:

    $ diff file_no_PIC.s file_PIC.s
    15c15,16
    <   movl    foo(%rip), %eax
    ---
    >   movq    foo@GOTPCREL(%rip), %rax
    >   movl    (%rax), %eax
    

    In the non-PIC code, foo is returned from bar() simply by loading the 32 bits from the (PC-relative) address of foo into the eax register. In the PIC code, it is returned by first loading the 64 bits at the (PC relative) address of the Global Offset Table value for foo into the rax register - making rax hold the runtime address of foo - then loading the 32 bits (an int) at that address into eax.