Chapter 7 265
Position-Independent Code
Generating Position-Independent Code
NOTE Any code which is PIC or which makes calls to PIC must follow the
standard procedure call mechanism.
When linking files produced by the assembler, the linker exports only
those assembly language routines that have been explicitly exported as
entry (that is, symbols of type ST_ENTRY). Compiler generated
assembly code does not explicitly export routines with the entry type
specified, so the assembly language programmer must ensure that this is
done with the .EXPORT pseudo-op.
For example: In assembly language, a symbol is exported using
.EXPORT foo, type
where type can be code, data, entry, and others. To ensure that foo is
exported from a shared library, the assembly statement must be:
.EXPORT foo,entry
Long Calls
Normally, the compilers generate a single-instruction call sequence using
the BL instruction. The compilers can be forced to generate a long call
sequence when the module is so large that the BL is not guaranteed to
reach the beginning of the subspace. In the latter case, the linker can
insert a stub. The existing long call sequence is three instructions, using
an absolute target address:
LDIL L'target,%r1
BLE R'target(%sr4,%r1)
COPY %r1,%rp
When the PIC option is in effect, the compilers must generate the
following instruction sequence, which is PC-relative:
BL .+8,%rp ; get pc into rp
ADDIL L'target - $L0 + 4, %rp ; add pc-rel offset to rp
LDO R'target - $L1 + 8(%r1), %r1
$L0: LDSID (%r1), %r31
$L1: MTSP %r31, %sr0
BLE 0(%sr0,%r1)
COPY %r31,%rp
Long Branches and Switch Tables
Long branches are similar to long calls, but are only two instructions
because the return pointer is not needed: