Sign in Join
Wiki copytemporary

PIC information: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11The linker/loader maintain a global offset table, which consists of an array of pointers to exported variables. All access to those variables is supposed to go through these pointers, if the variable could reside within another dynamic object.
22
33If 'variable' is an exported symbol, then 'variable@GOT' is replaced by the linker with a constant: the offset of the GOT slot containing the pointer to variable. So, if the %ebx register contains the start address of the GOT, you can retrieve this pointer via
44
55``` asm
66movl variable@GOT(%ebx), %eax
77```
88
99If you want the value of the variable, you need another indirection:
1010
1111``` asm
1212movl (%eax), %eax
1313```
1414
1515If you know, however, that the variable in question must reside in the same dynamic object as the user, then you can avoid that double indirection, by using the GOTOFF facility. 'variable@GOTOFF' is replaced by the linker with another constant: the difference between the absolute address of the variable and the address of the global offset table (of the current dynamic object). Thus, you can retrieve the value of the variable in one step using:
1616
1717``` asm
1818movl variable@GOTOFF(%ebx), %eax
1919```
2020
2121------------------------------------------------------------------------
2222
2323**source unknown**
2424
25-`From: Joerg Pommnitz (pommnitz@heidelbg.ibm.com)`\
26-`Subject: Re: ELF--Position-Independent Code: Why? `\
25+``` pascal
26+From: Joerg Pommnitz (pommnitz@heidelbg.ibm.com)
27+Subject: Re: ELF--Position-Independent Code: Why?
2728
28-`View this article only `\
29-`Newsgroups: comp.os.linux.development.system`\
30-`Date: 1995/08/01 `\
29+View this article only
30+Newsgroups: comp.os.linux.development.system
31+Date: 1995/08/01
3132
32-`Just to add a small remark to this thread:`\
33-`It is NO requirement that ELF dynamic libraries are position independent. You`\
34-`can build dynamic libraries from plain, non-pic object files. The disadvantage`\
35-`of this is the fact that they are not shared very much. The ELF loader`\
36-`has to do relocations, so lots of pages become dirty and can't be shared`\
37-`among processes. That's why non-pic ELF dynamic libraries should not be used`\
38-`for code that's normally shared among lots of processes running at the same`\
39-`time (i.e. libc, libX11, ...).`\
33+Just to add a small remark to this thread:
34+It is NO requirement that ELF dynamic libraries are position independent. You
35+can build dynamic libraries from plain, non-pic object files. The disadvantage
36+of this is the fact that they are not shared very much. The ELF loader
37+has to do relocations, so lots of pages become dirty and can't be shared
38+among processes. That's why non-pic ELF dynamic libraries should not be used
39+for code that's normally shared among lots of processes running at the same
40+time (i.e. libc, libX11, ...).
4041
41-`Best Regards`\
42-`       Joerg`
42+Best Regards
43+ Joerg
44+```
4345
4446------------------------------------------------------------------------
4547
4648However pic code is also bulkier and slower, and specially by C++ code with its massive number of symbols this is a problem. This can be avoided by taking a large heap of libraries and relocate them as a kind of library cache. Most startup accelerators in commercial OSes work that way (OSX/Windows).
4749
4850Marcov 13:57, 20 March 2007 (CET)
4951
5052### fpcmake
5153
5254fpcmake automatically adds -Cg/-fPIC for x86-64 platforms:
5355
5456- for freebsd, openbsd, netbsd, linux, solaris
5557- for darwin -Cg is enabled by the compiler (see below)
5658
5759See [this thread](http://lists.freepascal.org/pipermail/fpc-pascal/2014-November/043159.html) for more details.
5860
5961### fpc compiler
6062
6163PIC code is on by default for
6264
6365- for darwin, -Cg is always on by default on all (i386, x86-64, arm). Because it's also on by default in the system compilers (apple's gcc). Apple also requires any iOS code to be PIC (starting with iOS 7.0)
6466
6567For other platforms it's off by default, because system native compiler has it off by default as well.
6668
6769---
6870*From the Free Pascal wiki, page "PIC information", as it stood on 2014-11-19 after a change by Skalogryz, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
6971Category: FPC internals

Back to history