Specifications
The call and jmpi instructions are not available in position-independent code. Instead, all calls are made
through the GOT. Function addresses may be loaded with %call, which allows lazy binding. To initialize
a function pointer, load the address of the function with %got instead. If no input object requires the
address of the function its GOT entry is placed in the PLT GOT for lazy binding, as shown in the example
below.
For information about the PLT, refer to the "Procedure Linkage Table" section.
Example 7-17: Small GOT Model entry in PLT GOT
ldw r3, %call(fun)(r22) # R_NIOS2_CALL16 fun
callr r3
PLTGOT[n] R_NIOS_JUMP_SLOT fun
Example 7-18: Large GOT Model entry in PLT GOT
movhi r3, %call_hiadj(x) # R_NIOS2_CALL_HA
addi r3, r3, %call_lo(x) # R_NIOS2_CALL_LO
add r3, r3, r22
ldw r3, 0(r3)
callr r3
PLTGOT[n] R_NIOS_JUMP_SLOT fun
When a function or variable resides in the current shared object at compile time, it can be accessed via a
PC-relative or GOT-relative offset, as shown below.
Example 7-19: Accessing Function or Variable in Current Shared Object
orhi r3, %gotoff_hiadj(x) # R_NIOS2_GOTOFF_HA x
addi r3, r3, %gotoff_lo(x) # R_NIOS2_GOTOFF_LO x
add r3, r22, r3
# Address of x in r3
Multiway branches such as switch statements can be implemented with a table of GOT-relative offsets, as
shown below.
Example 7-20: Switch Statement Implemented with Table
# Scaled table offset in r4
orhi r3, %gotoff_hiadj(Ltable) # R_NIOS2_GOTOFF_HA Ltable
addi r3, r3, %gotoff_lo(Ltable) # R_NIOS2_GOTOFF_LO Ltable
add r3, r22, r3 # r3 == &Ltable
add r3, r3, r4
ldw r4, 0(r3) # r3 == Ltable[index]
add r4, r4, r22 # Convert offset into destina-
tion
jmp r4
...
NII51016
2015.04.02
Linux Position-Independent Code
7-19
Application Binary Interface
Altera Corporation
Send Feedback