Nios II Classic Processor Reference Guide

ID 683620
Date 10/28/2016
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 91.  Nios II Relocation Calculation
Name Value Overflow

check

44
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_CALL2645 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 2246 Yes G 0x003FFFC0 6
R_NIOS2_CALL16 2346 Yes G 0x003FFFC0 6
R_NIOS2_GOTOFF_LO 2446 No (S + A – GOT) & 0xFFFF 0x003FFFC0 6
R_NIOS2_GOTOFF_HA 2546 No Adj (S + A – GOT) 0x003FFFC0 6
R_NIOS2_PCREL_LO 2646 No (S + A – PC) & 0xFFFF 0x003FFFC0 6
R_NIOS2_PCREL_HA 2746 No Adj (S + A – PC) 0x003FFFC0 6
R_NIOS2_TLS_GD16 2846 Yes Refer to Thread-Local Storage section 0x003FFFC0 6
R_NIOS2_TLS_LDM16 2946 Yes Refer to Thread-Local Storage section 0x003FFFC0 6
R_NIOS2_TLS_LDO16 3046 Yes Refer to Thread-Local Storage section 0x003FFFC0 6
R_NIOS2_TLS_IE16 3146 Yes Refer to Thread-Local Storage section 0x003FFFC0 6
R_NIOS2_TLS_LE16 3246 Yes Refer to Thread-Local Storage section 0x003FFFC0 6
R_NIOS2_TLS_DTPMOD 3346 No Refer to Thread-Local Storage section 0xFFFFFFFF 0
R_NIOS2_TLS_DTPREL 3446 No Refer to Thread-Local Storage section 0xFFFFFFFF 0
R_NIOS2_TLS_TPREL 3546 No Refer to Thread-Local Storage section 0xFFFFFFFF 0
R_NIOS2_COPY 3646 No Refer to Copy Relocation section. n/a n/a
R_NIOS2_GLOB_DAT 3746 No S 0xFFFFFFFF 0
R_NIOS2_JUMP_SLOT 3846 No Refer to Jump Slot Relocation section. 0xFFFFFFFF 0
R_NIOS2_RELATIVE 3946 No BA+A 0xFFFFFFFF 0
R_NIOS2_GOTOFF 4046 No S+A 0xFFFFFFFF 0
R_NIOS2_GOT_LO 4246 No G & 0xFFFF 0x003FFFC0 6
R_NIOS2_GOT_HA 4346 No Adj(G) 0x003FFFC0 6
R_NIOS2_CALL_LO 4446 No G & 0xFFFF 0x003FFFC0 6
R_NIOS2_CALL_HA 4546 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
44 For relocation types where no overflow check is performed, the relocated address is truncated to fit the instruction.
45 Linker is permitted to clobber register AT in the course of resolving overflows
46 Relocation support is provided for Linux systems.