Skip to contentSkip to conversation

-fPIC position independent code on parisc

parisc-linux Alexander Gabert 2 participants 3 messages Apr 27, 2003 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

hi,

can someone explain the difference between "normal" and "-fPIC"?

i have compiled a test program:

#include <stdio.h>

int main(void) {
        printf("hello\n");
        return(0);
}

first one without -fPIC
gcc -S hello.c

     1          .LEVEL 1.1
     2          .section        .rodata
     3          .align 4
     4  .LC0:
     5  .stringz"hello\n"
     6          .text
     7          .align 4
     8  .globl main
     9                  .type            main,@function
    10  main:
    11          .PROC
    12          .CALLINFO FRAME=64,CALLS,SAVE_RP,SAVE_SP,ENTRY_GR=3
    13          .ENTRY
    14          stw %r2,-20(%r30)
    15          copy %r3,%r1
    16          copy %r30,%r3
    17          stwm %r1,64(%r30)
    18          ldil LR'.LC0,%r19
    19          ldo RR'.LC0(%r19),%r26
    20          bl printf,%r2
    21          nop
    22          ldi 0,%r28
    23          ldw -20(%r3),%r2
    24          ldo 64(%r3),%r30
    25          ldwm -64(%r30),%r3
    26          bv,n %r0(%r2)
    27          .EXIT
    28          .PROCEND
    29  .Lfe1:
    30          .size   main,.Lfe1-main
    31          .ident  "GCC: (GNU) 3.0.4"

second one with -fPIC
gcc -S -fPIC hello_fPIC.c

     1          .LEVEL 1.1
     2          .section        .rodata
     3          .align 4
     4  .LC0:
     5  .stringz"hello\n"
     6          .text
     7          .align 4
     8  .globl main
     9                  .type            main,@function
    10  main:
    11          .PROC
    12          .CALLINFO FRAME=64,CALLS,SAVE_RP,SAVE_SP,ENTRY_GR=4
    13          .ENTRY
    14          stw %r2,-20(%r30)
    15          copy %r3,%r1
    16          copy %r30,%r3
    17          stwm %r1,64(%r30)
    18          stw %r4,8(%r3)
    19          stw %r19,-32(%r30)
    20          copy %r19,%r4
    21          addil LT'.LC0,%r19
    22          ldw RT'.LC0(%r1),%r1
    23          copy %r1,%r26
    24          bl printf,%r2
    25          nop
    26          copy %r4,%r19
    27          ldi 0,%r28
    28          ldw -20(%r3),%r2
    29          ldw 8(%r3),%r4
    30          ldo 64(%r3),%r30
    31          ldwm -64(%r30),%r3
    32          bv,n %r0(%r2)
    33          .EXIT
    34          .PROCEND
    35  .Lfe1:
    36          .size   main,.Lfe1-main
    37          .ident  "GCC: (GNU) 3.0.4"

i know that it is needed for shared libraries...
but what exactly is position independent code?

gcc is version 3.0.4
glibc is version 2.2.5

thanks in advance,

Alex
--
pub 1024/05E1A80C 2001/12/16 Alexander Gabert (http://nikita.ath.cx) <[email protected]>

          Key fingerprint =  2D 84 B0 CB F5 67 8A 22  8D 37 6E 6B 8A 3B 7F D6  05 E1 A8 0C
#1 lore raw
Quoted 1 line

but what exactly is position independent code?

it's exactly what it says, it will work no matter at what position the code is loaded.

e.g.:

non-PIC

Quoted 3 lines
    18          ldil LR'.LC0,%r19
    19          ldo RR'.LC0(%r19),%r26
    20          bl printf,%r2

This loads the address of .LC0 (your string) relative to a fixed address into r26 (arg0). The address is resolved statically at link time, so if the code is moved to a different starting address, the address will be wrong.

PIC:

Quoted 4 lines
    21          addil LT'.LC0,%r19
    22          ldw RT'.LC0(%r1),%r1
    23          copy %r1,%r26
    24          bl printf,%r2

This calculates the offset of .LC0 relative to the linkage table pointer (r19) by creating a GOT entry. The linkage table pointer is assigned value at runtime based on where the dynamic loader loads the code into memory, so the load will always point to the the right place wherever it is loaded.

When you have a shared library, different programs that are linked against the library may load it at different offsets, so the code must be position independent for it to work.

HTH
randolph

Signature · 3 lines
Randolph Chung
Debian GNU/Linux Developer, hppa/ia64 ports
http://www.tausq.org/
#3 lore raw
In reply to a message not in the archive

(Please reply on list and not via private mail)

Quoted 1 line

i do not understand the "LR'" syntax

[etc]

Try
http://docs.hp.com/cgi-bin/onlinedocs.py?mpn=92432-90012&service=hpux&path=00/00/19&title=HP%20Assembler%20Reference%20Manual

Quoted 1 line

it has been linked already, how do i find out, where it is resolved?

After you link an application, you can disassmble the executable.

Quoted 1 line

but what with those LR' RR' LT' RT'

They are all part of the assembler operators for field selection.

Quoted 2 lines

linkage table pointer?
what is this? (i am new)

There are several documents that describe the PA runtime linked on the parisc-linux website. You might want to check them out. The ELF specification, together with the related architecture supplements, describe how relocations work.

randolph

Signature · 3 lines
Randolph Chung
Debian GNU/Linux Developer, hppa/ia64 ports
http://www.tausq.org/