Skip to content

Commit cd7f171

Browse files
authored
[BOLT][RISCV] Implement R_RISCV_64 (llvm#67558)
Relocation for 64-bit absolute values.
1 parent 2e9a04b commit cd7f171

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

bolt/lib/Core/Relocation.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ static bool isSupportedRISCV(uint64_t Type) {
109109
case ELF::R_RISCV_HI20:
110110
case ELF::R_RISCV_LO12_I:
111111
case ELF::R_RISCV_LO12_S:
112+
case ELF::R_RISCV_64:
112113
return true;
113114
}
114115
}
@@ -209,6 +210,7 @@ static size_t getSizeForTypeRISCV(uint64_t Type) {
209210
case ELF::R_RISCV_LO12_I:
210211
case ELF::R_RISCV_LO12_S:
211212
return 4;
213+
case ELF::R_RISCV_64:
212214
case ELF::R_RISCV_GOT_HI20:
213215
// See extractValueRISCV for why this is necessary.
214216
return 8;
@@ -364,6 +366,16 @@ static uint64_t encodeValueAArch64(uint64_t Type, uint64_t Value, uint64_t PC) {
364366
return Value;
365367
}
366368

369+
static uint64_t encodeValueRISCV(uint64_t Type, uint64_t Value, uint64_t PC) {
370+
switch (Type) {
371+
default:
372+
llvm_unreachable("unsupported relocation");
373+
case ELF::R_RISCV_64:
374+
break;
375+
}
376+
return Value;
377+
}
378+
367379
static uint64_t extractValueX86(uint64_t Type, uint64_t Contents, uint64_t PC) {
368380
if (Type == ELF::R_X86_64_32S)
369381
return SignExtend64<32>(Contents);
@@ -539,6 +551,7 @@ static uint64_t extractValueRISCV(uint64_t Type, uint64_t Contents,
539551
return SignExtend64<8>(((Contents >> 2) & 0x1f) | ((Contents >> 5) & 0xe0));
540552
case ELF::R_RISCV_ADD32:
541553
case ELF::R_RISCV_SUB32:
554+
case ELF::R_RISCV_64:
542555
return Contents;
543556
}
544557
}
@@ -704,6 +717,7 @@ static bool isPCRelativeRISCV(uint64_t Type) {
704717
case ELF::R_RISCV_HI20:
705718
case ELF::R_RISCV_LO12_I:
706719
case ELF::R_RISCV_LO12_S:
720+
case ELF::R_RISCV_64:
707721
return false;
708722
case ELF::R_RISCV_JAL:
709723
case ELF::R_RISCV_CALL:
@@ -756,7 +770,7 @@ uint64_t Relocation::encodeValue(uint64_t Type, uint64_t Value, uint64_t PC) {
756770
if (Arch == Triple::aarch64)
757771
return encodeValueAArch64(Type, Value, PC);
758772
if (Arch == Triple::riscv64)
759-
llvm_unreachable("not implemented");
773+
return encodeValueRISCV(Type, Value, PC);
760774
return encodeValueX86(Type, Value, PC);
761775
}
762776

@@ -844,6 +858,8 @@ bool Relocation::isPCRelative(uint64_t Type) {
844858
uint64_t Relocation::getAbs64() {
845859
if (Arch == Triple::aarch64)
846860
return ELF::R_AARCH64_ABS64;
861+
if (Arch == Triple::riscv64)
862+
return ELF::R_RISCV_64;
847863
return ELF::R_X86_64_64;
848864
}
849865

bolt/test/RISCV/reloc-64.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 -o %t.bolt %t
4+
// RUN: llvm-readelf -s %t.bolt | FileCheck --check-prefix=SYM %s
5+
// RUN: llvm-readelf -x .data %t.bolt | FileCheck --check-prefix=DATA %s
6+
7+
// SYM: {{0+}}400000 {{.*}} _start{{$}}
8+
9+
// DATA: Hex dump of section '.data':
10+
// DATA-NEXT: 00004000 00000000
11+
12+
.data
13+
.globl d
14+
.p2align 3
15+
d:
16+
.dword _start
17+
18+
.text
19+
.globl _start
20+
.p2align 1
21+
_start:
22+
ret
23+
## Dummy relocation to force relocation mode; without it, _start will not be
24+
## moved to a new address.
25+
.reloc 0, R_RISCV_NONE
26+
.size _start, .-_start

0 commit comments

Comments
 (0)