* [parisc-linux] -fPIC position independent code on parisc
@ 2003-04-27 20:04 Alexander Gabert
2003-04-27 20:12 ` Randolph Chung
0 siblings, 1 reply; 3+ messages in thread
From: Alexander Gabert @ 2003-04-27 20:04 UTC (permalink / raw)
To: parisc-linux
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) <pappy@nikita.ath.cx>
Key fingerprint = 2D 84 B0 CB F5 67 8A 22 8D 37 6E 6B 8A 3B 7F D6 05 E1 A8 0C
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [parisc-linux] -fPIC position independent code on parisc
2003-04-27 20:04 [parisc-linux] -fPIC position independent code on parisc Alexander Gabert
@ 2003-04-27 20:12 ` Randolph Chung
[not found] ` <20030427231210.GA31520@nikita.ath.cx>
0 siblings, 1 reply; 3+ messages in thread
From: Randolph Chung @ 2003-04-27 20:12 UTC (permalink / raw)
To: Alexander Gabert; +Cc: parisc-linux
> 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
> 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:
> 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
--
Randolph Chung
Debian GNU/Linux Developer, hppa/ia64 ports
http://www.tausq.org/
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2003-04-27 23:28 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-04-27 20:04 [parisc-linux] -fPIC position independent code on parisc Alexander Gabert
2003-04-27 20:12 ` Randolph Chung
[not found] ` <20030427231210.GA31520@nikita.ath.cx>
2003-04-27 22:34 ` Randolph Chung
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox