Skip to content

Commit e06cef5

Browse files
committed
Simplify relocate implementation
We're adding 1 to the high bit of the low12 chunk, letting that carry through the high bits, and then keeping only the high bits. In this case, the high bits are always zero. Spelling this out since I found it confusing at first... Given an int12, two possible cases: val hi20 low12 b31-b12 b11 b10..b0 111..11 1 (dont-care) 0 trunc(val) 000..00 0 (dont-care) 0 trunc(val)
1 parent 9f64b25 commit e06cef5

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

lld/ELF/Arch/RISCV.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -469,14 +469,12 @@ void RISCV::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
469469

470470
case INTERNAL_R_RISCV_X0REL_I:
471471
case INTERNAL_R_RISCV_X0REL_S: {
472-
assert(isInt<12>(val));
473-
uint64_t hi = (val + 0x800) >> 12;
474-
uint64_t lo = val - (hi << 12);
472+
checkInt(loc, val, 12, rel);
475473
uint32_t insn = (read32le(loc) & ~(31 << 15)) | (X_X0 << 15);
476474
if (rel.type == INTERNAL_R_RISCV_X0REL_I)
477-
insn = setLO12_I(insn, lo);
475+
insn = setLO12_I(insn, val);
478476
else
479-
insn = setLO12_S(insn, lo);
477+
insn = setLO12_S(insn, val);
480478
write32le(loc, insn);
481479
return;
482480
}

0 commit comments

Comments
 (0)