Skip to content

Commit b43debd

Browse files
committed
riscv-elf: Use simpler equivalent expression for LO12 relocations
Since LO12 only uses the low 12 bits of the value, subtracting a value that's left-shifted by 12 cannot possibly have any effect. All this did was sign-extend the value of lo12 from bit 12 upwards, which isn't particularly useful, and just leads to confusion, since this unnecessary (and backwards, given hi20's computation is based on lo12's immediate being sign-extended, so feeding hi20 back into lo12's calculation is somewhat circular) complexity has made its way into implementations in toolchains and runtimes, which is bad.
1 parent 4969a01 commit b43debd

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

riscv-elf.adoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,8 @@ is followed by an I-Type instruction (add immediate or load) with an
468468
calculated like this:
469469

470470
[horizontal]
471-
HI20:: `((symbol_address + 0x800) >> 12)`
472-
LO12:: `symbol_address - (hi20 << 12)`
471+
HI20:: `(symbol_address + 0x800) >> 12`
472+
LO12:: `symbol_address`
473473

474474
The following assembly and relocations show loading an absolute address:
475475

@@ -610,8 +610,8 @@ immediate on the add, load or store instruction the linker finds the
610610
instruction. The addresses for pair of relocations are calculated like this:
611611

612612
[horizontal]
613-
HI20:: `((symbol_address - hi20_reloc_offset + 0x800) >> 12)`
614-
LO12:: `symbol_address - hi20_reloc_offset - (hi20 << 12)`
613+
HI20:: `(symbol_address - hi20_reloc_offset + 0x800) >> 12`
614+
LO12:: `symbol_address - hi20_reloc_offset`
615615

616616
The successive instruction has a signed 12-bit immediate so the value of the
617617
preceding high 20-bit relocation may have 1 added to it.

0 commit comments

Comments
 (0)