Sign in Join
Wiki copytemporary

PIC information: what changed

Revision 1 to 2. 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
2525`From: Joerg Pommnitz (pommnitz@heidelbg.ibm.com)`\
2626`Subject: Re: ELF--Position-Independent Code: Why? `\
27-\
27+
2828`View this article only `\
2929`Newsgroups: comp.os.linux.development.system`\
3030`Date: 1995/08/01 `\
31-\
32-\
31+
3332`Just to add a small remark to this thread:`\
3433`It is NO requirement that ELF dynamic libraries are position independent. You`\
3534`can build dynamic libraries from plain, non-pic object files. The disadvantage`\
3635`of this is the fact that they are not shared very much. The ELF loader`\
3736`has to do relocations, so lots of pages become dirty and can't be shared`\
3837`among processes. That's why non-pic ELF dynamic libraries should not be used`\
3938`for code that's normally shared among lots of processes running at the same`\
4039`time (i.e. libc, libX11, ...).`\
41-\
40+
4241`Best Regards`\
4342`       Joerg`
4443
4544------------------------------------------------------------------------
4645
4746However 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).
4847
4948Marcov 13:57, 20 March 2007 (CET)
5049
5150### fpcmake
5251
5352fpcmake automatically adds -Cg/-fPIC for x86-64 platforms:
5453
5554- for freebsd, openbsd, netbsd, linux, solaris
5655- for darwin -Cg is enabled by the compiler (see below)
5756
5857See [this thread](http://lists.freepascal.org/pipermail/fpc-pascal/2014-November/043159.html) for more details.
5958
6059### fpc compiler
6160
6261PIC code is on by default for
6362
6463- 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)
6564
6665For other platforms it's off by default, because system native compiler has it off by default as well.
6766
6867---
6968*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).*
7069Category: FPC internals

Back to history