Skip to content

Commit 9555736

Browse files
authored
[BOLT][RISCV] Implement LO/HI relocations (#67444)
Implement the following relocations used by the medlow code model and non-PIE binaries: - R_RISCV_HI20 - R_RISCV_LO12_I - R_RISCV_LO12_S
1 parent 733e3c6 commit 9555736

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

bolt/lib/Core/Relocation.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ static bool isSupportedRISCV(uint64_t Type) {
106106
case ELF::R_RISCV_RVC_BRANCH:
107107
case ELF::R_RISCV_ADD32:
108108
case ELF::R_RISCV_SUB32:
109+
case ELF::R_RISCV_HI20:
110+
case ELF::R_RISCV_LO12_I:
111+
case ELF::R_RISCV_LO12_S:
109112
return true;
110113
}
111114
}
@@ -202,6 +205,9 @@ static size_t getSizeForTypeRISCV(uint64_t Type) {
202205
case ELF::R_RISCV_CALL_PLT:
203206
case ELF::R_RISCV_ADD32:
204207
case ELF::R_RISCV_SUB32:
208+
case ELF::R_RISCV_HI20:
209+
case ELF::R_RISCV_LO12_I:
210+
case ELF::R_RISCV_LO12_S:
205211
return 4;
206212
case ELF::R_RISCV_GOT_HI20:
207213
// See extractValueRISCV for why this is necessary.
@@ -519,10 +525,13 @@ static uint64_t extractValueRISCV(uint64_t Type, uint64_t Contents,
519525
return extractUImmRISCV(Contents & 0xffffffff) +
520526
extractIImmRISCV(Contents >> 32);
521527
case ELF::R_RISCV_PCREL_HI20:
528+
case ELF::R_RISCV_HI20:
522529
return extractUImmRISCV(Contents);
523530
case ELF::R_RISCV_PCREL_LO12_I:
531+
case ELF::R_RISCV_LO12_I:
524532
return extractIImmRISCV(Contents);
525533
case ELF::R_RISCV_PCREL_LO12_S:
534+
case ELF::R_RISCV_LO12_S:
526535
return extractSImmRISCV(Contents);
527536
case ELF::R_RISCV_RVC_JUMP:
528537
return SignExtend64<11>(Contents >> 2);
@@ -692,6 +701,9 @@ static bool isPCRelativeRISCV(uint64_t Type) {
692701
llvm_unreachable("Unknown relocation type");
693702
case ELF::R_RISCV_ADD32:
694703
case ELF::R_RISCV_SUB32:
704+
case ELF::R_RISCV_HI20:
705+
case ELF::R_RISCV_LO12_I:
706+
case ELF::R_RISCV_LO12_S:
695707
return false;
696708
case ELF::R_RISCV_JAL:
697709
case ELF::R_RISCV_CALL:

bolt/lib/Target/RISCV/RISCVMCPlusBuilder.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class RISCVMCPlusBuilder : public MCPlusBuilder {
4343
case ELF::R_RISCV_PCREL_HI20:
4444
case ELF::R_RISCV_PCREL_LO12_I:
4545
case ELF::R_RISCV_PCREL_LO12_S:
46+
case ELF::R_RISCV_HI20:
47+
case ELF::R_RISCV_LO12_I:
48+
case ELF::R_RISCV_LO12_S:
4649
return true;
4750
default:
4851
llvm_unreachable("Unexpected RISCV relocation type in code");
@@ -399,6 +402,11 @@ class RISCVMCPlusBuilder : public MCPlusBuilder {
399402
case ELF::R_RISCV_PCREL_LO12_I:
400403
case ELF::R_RISCV_PCREL_LO12_S:
401404
return RISCVMCExpr::create(Expr, RISCVMCExpr::VK_RISCV_PCREL_LO, Ctx);
405+
case ELF::R_RISCV_HI20:
406+
return RISCVMCExpr::create(Expr, RISCVMCExpr::VK_RISCV_HI, Ctx);
407+
case ELF::R_RISCV_LO12_I:
408+
case ELF::R_RISCV_LO12_S:
409+
return RISCVMCExpr::create(Expr, RISCVMCExpr::VK_RISCV_LO, Ctx);
402410
case ELF::R_RISCV_CALL:
403411
return RISCVMCExpr::create(Expr, RISCVMCExpr::VK_RISCV_CALL, Ctx);
404412
case ELF::R_RISCV_CALL_PLT:

bolt/test/RISCV/reloc-lohi.s

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: llvm-mc -triple riscv64 -filetype=obj -o %t.o %s
2+
// RUN: ld.lld -q -o %t %t.o
3+
// RUN: llvm-bolt --print-cfg --print-only=_start -o /dev/null %t \
4+
// RUN: | FileCheck %s
5+
6+
.data
7+
.globl d
8+
.p2align 3
9+
d:
10+
.dword 0
11+
12+
// CHECK-LABEL: Binary Function "_start" after building cfg {
13+
// CHECK: lui t0, %hi(d)
14+
// CHECK-NEXT: ld t0, %lo(d)(t0)
15+
// CHECK-NEXT: lui t0, %hi(d)
16+
// CHECK-NEXT: sd t0, %lo(d)(t0)
17+
.text
18+
.globl _start
19+
.p2align 1
20+
_start:
21+
lui t0, %hi(d)
22+
ld t0, %lo(d)(t0)
23+
lui t0, %hi(d)
24+
sd t0, %lo(d)(t0)
25+
ret
26+
.size _start, .-_start

0 commit comments

Comments
 (0)