9.39.3 RISC-V Assembler Modifiers

The RISC-V assembler supports following modifiers for relocatable addresses used in RISC-V instruction operands. However, we also support some pseudo instructions that are easier to use than these modifiers.

%lo(symbol)

The low 12 bits of absolute address for symbol.

%hi(symbol)

The high 20 bits of absolute address for symbol. This is usually used with the %lo modifier to represent a 32-bit absolute address.

	lui        a0, %hi(symbol)     // R_RISCV_HI20
	addi       a0, a0, %lo(symbol) // R_RISCV_LO12_I

	lui        a0, %hi(symbol)     // R_RISCV_HI20
	load/store a0, %lo(symbol)(a0) // R_RISCV_LO12_I/S
%pcrel_lo(label)

The low 12 bits of relative address between pc and symbol. The symbol is related to the high part instruction which is marked by label.

%pcrel_hi(symbol)

The high 20 bits of relative address between pc and symbol. This is usually used with the %pcrel_lo modifier to represent a +/-2GB pc-relative range.

label:
	auipc      a0, %pcrel_hi(symbol)    // R_RISCV_PCREL_HI20
	addi       a0, a0, %pcrel_lo(label) // R_RISCV_PCREL_LO12_I

label:
	auipc      a0, %pcrel_hi(symbol)    // R_RISCV_PCREL_HI20
	load/store a0, %pcrel_lo(label)(a0) // R_RISCV_PCREL_LO12_I/S

Or you can use the pseudo lla/lw/sw/... instruction to do this.

	lla  a0, symbol
%got_pcrel_hi(symbol)

The high 20 bits of relative address between pc and the GOT entry of symbol. This is usually used with the %pcrel_lo modifier to access the GOT entry.

label:
	auipc      a0, %got_pcrel_hi(symbol) // R_RISCV_GOT_HI20
	addi       a0, a0, %pcrel_lo(label)  // R_RISCV_PCREL_LO12_I

label:
	auipc      a0, %got_pcrel_hi(symbol) // R_RISCV_GOT_HI20
	load/store a0, %pcrel_lo(label)(a0)  // R_RISCV_PCREL_LO12_I/S

Also, the pseudo la instruction with PIC has similar behavior.

%tprel_add(symbol)

This is used purely to associate the R_RISCV_TPREL_ADD relocation for TLS relaxation. This one is only valid as the fourth operand to the normally 3 operand add instruction.

%tprel_lo(symbol)

The low 12 bits of relative address between tp and symbol.

%tprel_hi(symbol)

The high 20 bits of relative address between tp and symbol. This is usually used with the %tprel_lo and %tprel_add modifiers to access the thread local variable symbol in TLS Local Exec.

	lui        a5, %tprel_hi(symbol)          // R_RISCV_TPREL_HI20
	add        a5, a5, tp, %tprel_add(symbol) // R_RISCV_TPREL_ADD
	load/store t0, %tprel_lo(symbol)(a5)      // R_RISCV_TPREL_LO12_I/S
%tls_ie_pcrel_hi(symbol)

The high 20 bits of relative address between pc and GOT entry. It is usually used with the %pcrel_lo modifier to access the thread local variable symbol in TLS Initial Exec.

	la.tls.ie  a5, symbol
	add        a5, a5, tp
	load/store t0, 0(a5)

The pseudo la.tls.ie instruction can be expended to

label:
	auipc a5, %tls_ie_pcrel_hi(symbol) // R_RISCV_TLS_GOT_HI20
	load  a5, %pcrel_lo(label)(a5)     // R_RISCV_PCREL_LO12_I
%tls_gd_pcrel_hi(symbol)

The high 20 bits of relative address between pc and GOT entry. It is usually used with the %pcrel_lo modifier to access the thread local variable symbol in TLS Global Dynamic.

	la.tls.gd  a0, symbol
	call       __tls_get_addr@plt
	mv         a5, a0
	load/store t0, 0(a5)

The pseudo la.tls.gd instruction can be expended to

label:
	auipc a0, %tls_gd_pcrel_hi(symbol) // R_RISCV_TLS_GD_HI20
	addi  a0, a0, %pcrel_lo(label)     // R_RISCV_PCREL_LO12_I