aboutsummaryrefslogtreecommitdiff
blob: de96fceccf2ba0f332e0df3de6d6772309033983 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link title="new" rel="stylesheet" href="http://www.gentoo.org/css/main.css" type="text/css">
<link REL="shortcut icon" HREF="http://www.gentoo.org/favicon.ico" TYPE="image/x-icon">
<link rel="search" type="application/opensearchdescription+xml" href="http://www.gentoo.org/search/www-gentoo-org.xml" title="Gentoo Website">
<link rel="search" type="application/opensearchdescription+xml" href="http://www.gentoo.org/search/forums-gentoo-org.xml" title="Gentoo Forums">
<link rel="search" type="application/opensearchdescription+xml" href="http://www.gentoo.org/search/bugs-gentoo-org.xml" title="Gentoo Bugzilla">
<link rel="search" type="application/opensearchdescription+xml" href="http://www.gentoo.org/search/packages-gentoo-org.xml" title="Gentoo Packages">
<link rel="search" type="application/opensearchdescription+xml" href="http://www.gentoo.org/search/archives-gentoo-org.xml" title="Gentoo List Archives">
<title>Gentoo Linux Documentation
--
  Introduction to Position Independent Code</title>
</head>
<body style="margin:0px;" bgcolor="#ffffff"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td valign="top" height="125" bgcolor="#45347b"><a href="http://www.gentoo.org/"><img border="0" src="http://www.gentoo.org/images/gtop-www.jpg" alt="Gentoo Logo"></a></td></tr>
<tr><td valign="top" align="right" colspan="1" bgcolor="#ffffff"><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr>
<td width="99%" class="content" valign="top" align="left">
<br><h1>Introduction to Position Independent Code</h1>
<p class="chaphead"><a name="doc_chap1"></a><span class="chapnum">1.
            </span>Introduction to PIC - (Position Independent Code)</p>
<p>
PIC code radically differs from conventional code in the way it
calls functions and operates on data variables.<br>

It will access these functions and data through an indirection table,
the "Global Offset Table" (GOT), by software convention accessible using
the reserved name "_GLOBAL_OFFSET_TABLE_".<br>

The exact mechanism used for this is hardware architecture dependent,
but usually a special machine register is reserved for setting up
the location of the GOT when entering a function.<br>

The rationale behind this indirect addressing is to generate code
that can be independently accessed of the actual load address.<br>

In a true PIC library without relocations in the text segment,
only the symbols exported in the "Global Offset Table" need updating
at run-time depending on the current load address of the various
shared libraries in the address space of the running process.
</p>
<p>
Likewise, procedure calls to globally defined functions are redirected
through the "Procedure Linkage Table" (PLT) residing in the data segment
of the core image.  Again, this is done to avoid run-time modifications
to the text segment.
</p>
<p>
The linker-editor allocates the Global Offset Table and Procedure
Linkage Table when combining PIC object files into an image suitable for
mapping into the process address space.  It also collects all symbols
that may be needed by the run-time link-editor and stores these along
with the image's text and data bits.  Another reserved symbol, _DYNAMIC
is used to indicate the presence of the run-time linker structures. 
Whenever _DYNAMIC is relocated to 0, there is no need to invoke the
run-time link- editor.  If this symbol is non-zero, it points at a data
structure from which the location of the necessary relocation- and
symbol information can be derived.  This is most notably used by the
start-up module, crt0, crt1S and more recently Scrt1. The _DYNAMIC
structure is conventionally located at the start of the data segment of
the image to which it pertains.
</p>
<p>
On most architectures, when you compile source code to object code, you
need to specify whether the object code should be position independent
or not. There are occasional architectures which don't make the
distinction, usually because all object code is position independent by
virtue of the Application Binary Interface (ABI), or less often because
the load address of the object is fixed at compile time, which implies
that shared libraries are not supported by such a platform.

If an object is compiled as position independent code (PIC),
then the operating system can load the object at any address
in preparation for execution. This involves a time overhead,
in replacing direct address references with relative addresses at
compile time, and a space overhead, in maintaining information to help
the runtime loader fill in the unresolved addresses at runtime.
Consequently, PIC objects are usually slightly larger and slower at
runtime than the equivalent non-PIC object. The advantage of sharing
library code on disk and in memory outweigh these problems as soon as
the PIC object code in shared libraries is reused.
</p>
<p>
PIC compilation is exactly what is required for objects which will
become part of a shared library. Consequently, libtool builds PIC
objects for use in shared libraries and non-PIC objects for use in
static libraries. Whenever libtool instructs the compiler to generate a
PIC object, it also defines the preprocessor symbol, `PIC', so that
assembly code can be aware of whether it will reside in a PIC object or
not.
</p>
<p>
Typically, as libtool is compiling sources, it will generate a `.lo'
object, as PIC, and a `.o' object, as non-PIC, and then it will use the
appropriate one of the pair when linking executables and libraries of
various sorts. On architectures where there is no distinction, the `.lo'
file is just a soft link to the `.o' file.
</p>
<p>
In practice, you can link PIC objects into a static archive for a small
overhead in execution and load speed, and often you can similarly link
non-PIC objects into shared archives.
</p>
<p>
When you use position-independent code, relocatable references are
generated as an indirection that use data in the shared object's data
segment. The text segment code remains read-only, and all relocation
updates are applied to corresponding entries within the data segment. 
</p>
<p>
If a shared object is built from code that is not position-independent,
the text segment will usually require a large number of relocations to
be performed at runtime. Although the runtime linker is equipped to
handle this, the system overhead this creates can cause serious
performance degradation.
</p>
<p>
You can identify a shared object that requires relocations against its
text segment using tools such as 'readelf -d foo' and inspect the output
for any TEXTREL entry. The value of the TEXTREL entry is irrelevant. Its
presence in a shared object indicates that text relocations exist.
</p>
<p>
References:
</p>
<ul>
 <li><a href="link.5.html">NetBSD link(5) File Formats Manual</a></li>
 <li><a href="http://sources.redhat.com/autobook/autobook/autobook_71.html#SEC71">Autobook (Position Independent Code) from Chapter 10.2.1</a></li>
 <li><a href="http://docs.sun.com/app/docs/doc/817-1984">docs.sun.com: Linker and Libraries Guide</a></li>
 <li>Linkers and Loaders <a href="http://www.iecc.com/linker/linker08.html">chapter 8</a> and <a href="http://www.iecc.com/linker/linker10.html">chapter 10</a>
</li>
</ul>
<br><p class="copyright">
	The contents of this document, unless otherwise expressly stated, are licensed under the <a href="http://creativecommons.org/licenses/by-sa/2.5">CC-BY-SA-2.5</a> license. The <a href="http://www.gentoo.org/main/en/name-logo.xml"> Gentoo Name and Logo Usage Guidelines </a> apply.
  </p>
<!--
  <rdf:RDF xmlns="http://web.resource.org/cc/"
      xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  
  <license rdf:about="http://creativecommons.org/licenses/by-sa/2.5/">
    
     <permits rdf:resource="http://web.resource.org/cc/Reproduction" />
     <permits rdf:resource="http://web.resource.org/cc/Distribution" />
     <requires rdf:resource="http://web.resource.org/cc/Notice" />
     <requires rdf:resource="http://web.resource.org/cc/Attribution" />
     <permits rdf:resource="http://web.resource.org/cc/DerivativeWorks" />
     <requires rdf:resource="http://web.resource.org/cc/ShareAlike" />
  </License>
  </rdf:RDF>
--><br>
</td>
<td width="1%" bgcolor="#dddaec" valign="top"><table border="0" cellspacing="4px" cellpadding="4px">
<tr><td class="topsep" align="center"><p class="altmenu"><a title="View a printer-friendly version" class="altlink" href="pic-guide.xml?style=printable">Print</a></p></td></tr>
<tr><td class="topsep" align="center"><p class="alttext">Page updated October 11, 2005</p></td></tr>
<tr><td class="topsep" align="left"><p class="alttext"><b>Summary: </b>What every developer should understand about using Position Independent Code</p></td></tr>
<tr><td align="left" class="topsep"><p class="alttext">
 <a href="mailto:solar@gentoo.org" class="altlink"><b>solar</b></a>
<br><i>Author</i><br><br>
 <a href="mailto:a.gabert@fh-trier.de" class="altlink"><b>Alexander Gabert</b></a>
<br><i>Editor</i><br></p></td></tr>
<tr lang="en"><td align="center" class="topsep">
<p class="alttext"><b>Donate</b> to support our development efforts.
        </p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick"><input type="hidden" name="business" value="paypal@gentoo.org"><input type="hidden" name="item_name" value="Gentoo Linux Support"><input type="hidden" name="item_number" value="1000"><input type="hidden" name="image_url" value="http://www.gentoo.org/images/paypal.png"><input type="hidden" name="no_shipping" value="1"><input type="hidden" name="return" value="http://www.gentoo.org"><input type="hidden" name="cancel_return" value="http://www.gentoo.org"><input type="image" src="http://images.paypal.com/images/x-click-but21.gif" name="submit" alt="Donate to Gentoo">
</form>
</td></tr>
<tr lang="en"><td align="center"><iframe src="http://sidebar.gentoo.org" scrolling="no" width="125" height="850" frameborder="0" style="border:0px padding:0x" marginwidth="0" marginheight="0"><p>Your browser does not support iframes.</p></iframe></td></tr>
</table></td>
</tr></table></td></tr>
<tr><td colspan="2" align="right" class="infohead">
Copyright 2001-2012 Gentoo Foundation, Inc. Questions, Comments? <a class="highlight" href="http://www.gentoo.org/main/en/contact.xml">Contact us</a>.
</td></tr>
</table></body>
</html>