What is a
In order for the object code in a shared library to be fully shareable, it must not depend on its position in the virtual address space of any particular process. The object code in a shared library may be attached at different points in different processes, so it must work independent of being located at any particular position. Hence the term
Position independence is achieved by two mechanisms: First,
Calls to PIC routines are accomplished through a procedure linkage table (PLT), which is built by the linker. Similarly, references to data are accomplished through a data linkage table (DLT). Both tables reside in a process's data segment. The dynamic loader fills in these tables with the absolute virtual addresses of the routines and data in a shared library at run time (known as binding).
Because of this, PIC can be loaded and executed anywhere that a process has free space.
On compilers that support PIC generation, the +z and +Z options cause the compiler to create PIC relocatable object code.
Generating
To be
Register 19 (%r19) is the designated pointer to the linkage table. The linker generates stubs that ensure %r19 always points to the correct value for the target routine and that handle the
The linker generates an import stub for each external reference to a routine. The call to the routine is redirected to branch to the import stub, which obtains the target routine address and the new linkage table pointer value from the current linkage table; it then branches to an export stub for the target routine. In
NOTE: The
Shown below is the PIC code generated for import and export stubs. Note that this code is generated automatically by the linker. You do not have to generate the stubs yourself.
;Import Stub (Incomplete Executable)
X': ADDIL | L'lt_ptr+ltoff,%dp | ; get procedure entry point | |
LDW | R'lt_ptr+ltoff(%r1),%r21 | ||
LDW | R'lt_ptr+ltoff+4(%r1),%r19 ; get new r19 value. | ||
LDSID | (%r21),%r1 |
|
|
MTSP | %r1,%sr0 |
|
|
BE | 0(%sr0,%r21) | ; | branch to target |
STW | ; | save rp |
;Import Stub (Shared Library)
186 Writing and Generating