Skip to content

Commit 1b78742

Browse files
authored
[BOLT][RISCV] Implement R_RISCV_PCREL_LO12_S (#65204)
Relocation used for store instructions.
1 parent 1b622ff commit 1b78742

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

bolt/lib/Core/Relocation.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ static bool isSupportedRISCV(uint64_t Type) {
101101
case ELF::R_RISCV_GOT_HI20:
102102
case ELF::R_RISCV_PCREL_HI20:
103103
case ELF::R_RISCV_PCREL_LO12_I:
104+
case ELF::R_RISCV_PCREL_LO12_S:
104105
case ELF::R_RISCV_RVC_JUMP:
105106
case ELF::R_RISCV_RVC_BRANCH:
106107
case ELF::R_RISCV_ADD32:
@@ -195,6 +196,7 @@ static size_t getSizeForTypeRISCV(uint64_t Type) {
195196
case ELF::R_RISCV_BRANCH:
196197
case ELF::R_RISCV_PCREL_HI20:
197198
case ELF::R_RISCV_PCREL_LO12_I:
199+
case ELF::R_RISCV_PCREL_LO12_S:
198200
case ELF::R_RISCV_32_PCREL:
199201
case ELF::R_RISCV_CALL:
200202
case ELF::R_RISCV_CALL_PLT:
@@ -480,6 +482,10 @@ static uint64_t extractIImmRISCV(uint32_t Contents) {
480482
return SignExtend64<12>(Contents >> 20);
481483
}
482484

485+
static uint64_t extractSImmRISCV(uint32_t Contents) {
486+
return SignExtend64<12>(((Contents >> 7) & 0x1f) | ((Contents >> 25) << 5));
487+
}
488+
483489
static uint64_t extractJImmRISCV(uint32_t Contents) {
484490
return SignExtend64<21>(
485491
(((Contents >> 21) & 0x3ff) << 1) | (((Contents >> 20) & 0x1) << 11) |
@@ -516,6 +522,8 @@ static uint64_t extractValueRISCV(uint64_t Type, uint64_t Contents,
516522
return extractUImmRISCV(Contents);
517523
case ELF::R_RISCV_PCREL_LO12_I:
518524
return extractIImmRISCV(Contents);
525+
case ELF::R_RISCV_PCREL_LO12_S:
526+
return extractSImmRISCV(Contents);
519527
case ELF::R_RISCV_RVC_JUMP:
520528
return SignExtend64<11>(Contents >> 2);
521529
case ELF::R_RISCV_RVC_BRANCH:
@@ -692,6 +700,7 @@ static bool isPCRelativeRISCV(uint64_t Type) {
692700
case ELF::R_RISCV_GOT_HI20:
693701
case ELF::R_RISCV_PCREL_HI20:
694702
case ELF::R_RISCV_PCREL_LO12_I:
703+
case ELF::R_RISCV_PCREL_LO12_S:
695704
case ELF::R_RISCV_RVC_JUMP:
696705
case ELF::R_RISCV_RVC_BRANCH:
697706
case ELF::R_RISCV_32_PCREL:

bolt/lib/Target/RISCV/RISCVMCPlusBuilder.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class RISCVMCPlusBuilder : public MCPlusBuilder {
4242
case ELF::R_RISCV_GOT_HI20:
4343
case ELF::R_RISCV_PCREL_HI20:
4444
case ELF::R_RISCV_PCREL_LO12_I:
45+
case ELF::R_RISCV_PCREL_LO12_S:
4546
return true;
4647
default:
4748
llvm_unreachable("Unexpected RISCV relocation type in code");
@@ -352,6 +353,7 @@ class RISCVMCPlusBuilder : public MCPlusBuilder {
352353
case ELF::R_RISCV_PCREL_HI20:
353354
return RISCVMCExpr::create(Expr, RISCVMCExpr::VK_RISCV_PCREL_HI, Ctx);
354355
case ELF::R_RISCV_PCREL_LO12_I:
356+
case ELF::R_RISCV_PCREL_LO12_S:
355357
return RISCVMCExpr::create(Expr, RISCVMCExpr::VK_RISCV_PCREL_LO, Ctx);
356358
case ELF::R_RISCV_CALL:
357359
return RISCVMCExpr::create(Expr, RISCVMCExpr::VK_RISCV_CALL, Ctx);

bolt/test/RISCV/reloc-pcrel.s

+4
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,9 @@ _start:
1818
// CHECK: auipc t0, %pcrel_hi(d)
1919
// CHECK-NEXT: ld t0, %pcrel_lo(.Ltmp0)(t0)
2020
ld t0, d
21+
// CHECK: .Ltmp1
22+
// CHECK: auipc t1, %pcrel_hi(d)
23+
// CHECK-NEXT: sd t0, %pcrel_lo(.Ltmp1)(t1)
24+
sd t0, d, t1
2125
ret
2226
.size _start, .-_start

0 commit comments

Comments
 (0)