Nios® II Processor Reference Guide

ID 683836
Date 8/28/2023
Public
Document Table of Contents

7.8. Relocation

In a Nios II object file, each relocatable address reference possesses a relocation type. The relocation type specifies how to calculate the relocated address. The bit mask specifies where the address is found in the instruction.
Table 82.  Nios II Relocation Calculation
Name Value Overflow

check

40
Relocated Address

R

Bit Mask

M

Bit Shift

B

R_NIOS2_NONE 0 n/a None n/a n/a
R_NIOS2_S16 1 Yes S + A 0x003FFFC0 6
R_NIOS2_U16 2 Yes S + A 0x003FFFC0 6
R_NIOS2_PCREL16 3 Yes ((S + A) – 4) – PC 0x003FFFC0 6
R_NIOS2_CALL2641 4 Yes (S + A) >> 2 0xFFFFFFC0 6
R_NIOS2_CALL26_NOAT 41 No (S + A) >> 2 0xFFFFFFC0 6
R_NIOS2_IMM5 5 Yes (S + A) & 0x1F 0x000007C0 6
R_NIOS2_CACHE_OPX 6 Yes (S + A) & 0x1F 0x07C00000 22
R_NIOS2_IMM6 7 Yes (S + A) & 0x3F 0x00000FC0 6
R_NIOS2_IMM8 8 Yes (S + A) & 0xFF 0x00003FC0 6
R_NIOS2_HI16 9 No ((S + A) >> 16) & 0xFFFF 0x003FFFC0 6
R_NIOS2_LO16 10 No (S + A) & 0xFFFF 0x003FFFC0 6
R_NIOS2_HIADJ16 11 No Adj(S+A) 0x003FFFC0 6
R_NIOS2_BFD_RELOC_32 12 No S + A 0xFFFFFFFF 0
R_NIOS2_BFD_RELOC_16 13 Yes (S + A) & 0xFFFF 0x0000FFFF 0
R_NIOS2_BFD_RELOC_8 14 Yes (S + A) & 0xFF 0x000000FF 0
R_NIOS2_GPREL 15 No (S + A – GP) & 0xFFFF 0x003FFFC0 6
R_NIOS2_GNU_VTINHERIT 16 n/a None n/a n/a
R_NIOS2_GNU_VTENTRY 17 n/a None n/a n/a
R_NIOS2_UJMP 18 No ((S + A) >> 16) & 0xFFFF,

(S + A + 4) & 0xFFFF

0x003FFFC0 6
R_NIOS2_CJMP 19 No ((S + A) >> 16) & 0xFFFF,

(S + A + 4) & 0xFFFF

0x003FFFC0 6
R_NIOS2_CALLR 20 No ((S + A) >> 16) & 0xFFFF)

(S + A + 4) & 0xFFFF

0x003FFFC0 6
R_NIOS2_ALIGN 21 n/a None n/a n/a
R_NIOS2_GOT16 2242 Yes G 0x003FFFC0 6
R_NIOS2_CALL16 2342 Yes G 0x003FFFC0 6
R_NIOS2_GOTOFF_LO 2442 No (S + A – GOT) & 0xFFFF 0x003FFFC0 6
R_NIOS2_GOTOFF_HA 2542 No Adj (S + A – GOT) 0x003FFFC0 6
R_NIOS2_PCREL_LO 2642 No (S + A – PC) & 0xFFFF 0x003FFFC0 6
R_NIOS2_PCREL_HA 2742 No Adj (S + A – PC) 0x003FFFC0 6
R_NIOS2_TLS_GD16 2842 Yes Refer to Thread-Local Storage section 0x003FFFC0 6
R_NIOS2_TLS_LDM16 2942 Yes Refer to Thread-Local Storage section 0x003FFFC0 6
R_NIOS2_TLS_LDO16 3042 Yes Refer to Thread-Local Storage section 0x003FFFC0 6
R_NIOS2_TLS_IE16 3142 Yes Refer to Thread-Local Storage section 0x003FFFC0 6
R_NIOS2_TLS_LE16 3242 Yes Refer to Thread-Local Storage section 0x003FFFC0 6
R_NIOS2_TLS_DTPMOD 3342 No Refer to Thread-Local Storage section 0xFFFFFFFF 0
R_NIOS2_TLS_DTPREL 3442 No Refer to Thread-Local Storage section 0xFFFFFFFF 0
R_NIOS2_TLS_TPREL 3542 No Refer to Thread-Local Storage section 0xFFFFFFFF 0
R_NIOS2_COPY 3642 No Refer to Copy Relocation section. n/a n/a
R_NIOS2_GLOB_DAT 3742 No S 0xFFFFFFFF 0
R_NIOS2_JUMP_SLOT 3842 No Refer to Jump Slot Relocation section. 0xFFFFFFFF 0
R_NIOS2_RELATIVE 3942 No BA+A 0xFFFFFFFF 0
R_NIOS2_GOTOFF 4042 No S+A 0xFFFFFFFF 0
R_NIOS2_GOT_LO 4242 No G & 0xFFFF 0x003FFFC0 6
R_NIOS2_GOT_HA 4342 No Adj(G) 0x003FFFC0 6
R_NIOS2_CALL_LO 4442 No G & 0xFFFF 0x003FFFC0 6
R_NIOS2_CALL_HA 4542 No Adj(G) 0x003FFFC0 6

Expressions in the table above use the following conventions:

  • S: Symbol address
  • A: Addend
  • PC: Program counter
  • GP: Global pointer
  • Adj(X): (((X >> 16) & 0xFFFF) + ((X >> 15) & 0x1)) & 0xFFFF
  • BA: The base address at which a shared library is loaded
  • GOT: The value of the Global Offset Table (GOT) pointer (Linux only)
  • G: The offset into the GOT for the GOT slot for symbol S (Linux only)

With the information in the table above, any Nios II instruction can be relocated by manipulating it as an unsigned 32-bit integer, as follows:

Xr = (( R << B ) & M | ( X & ~M ));

where:

  • R is the relocated address, calculated in the above table
  • B is the bit shift
  • M is the bit mask
  • X is the original instruction
  • Xr is the relocated instruction
40 For relocation types where no overflow check is performed, the relocated address is truncated to fit the instruction.
41 Linker is permitted to clobber register AT in the course of resolving overflows
42 Relocation support is provided for Linux systems.