Skip to content

[BOLT][RISCV] Implement R_RISCV_64 #67558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion bolt/lib/Core/Relocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ static bool isSupportedRISCV(uint64_t Type) {
case ELF::R_RISCV_HI20:
case ELF::R_RISCV_LO12_I:
case ELF::R_RISCV_LO12_S:
case ELF::R_RISCV_64:
return true;
}
}
Expand Down Expand Up @@ -209,6 +210,7 @@ static size_t getSizeForTypeRISCV(uint64_t Type) {
case ELF::R_RISCV_LO12_I:
case ELF::R_RISCV_LO12_S:
return 4;
case ELF::R_RISCV_64:
case ELF::R_RISCV_GOT_HI20:
// See extractValueRISCV for why this is necessary.
return 8;
Expand Down Expand Up @@ -364,6 +366,16 @@ static uint64_t encodeValueAArch64(uint64_t Type, uint64_t Value, uint64_t PC) {
return Value;
}

static uint64_t encodeValueRISCV(uint64_t Type, uint64_t Value, uint64_t PC) {
switch (Type) {
default:
llvm_unreachable("unsupported relocation");
case ELF::R_RISCV_64:
break;
}
return Value;
}

static uint64_t extractValueX86(uint64_t Type, uint64_t Contents, uint64_t PC) {
if (Type == ELF::R_X86_64_32S)
return SignExtend64<32>(Contents);
Expand Down Expand Up @@ -539,6 +551,7 @@ static uint64_t extractValueRISCV(uint64_t Type, uint64_t Contents,
return SignExtend64<8>(((Contents >> 2) & 0x1f) | ((Contents >> 5) & 0xe0));
case ELF::R_RISCV_ADD32:
case ELF::R_RISCV_SUB32:
case ELF::R_RISCV_64:
return Contents;
}
}
Expand Down Expand Up @@ -704,6 +717,7 @@ static bool isPCRelativeRISCV(uint64_t Type) {
case ELF::R_RISCV_HI20:
case ELF::R_RISCV_LO12_I:
case ELF::R_RISCV_LO12_S:
case ELF::R_RISCV_64:
return false;
case ELF::R_RISCV_JAL:
case ELF::R_RISCV_CALL:
Expand Down Expand Up @@ -756,7 +770,7 @@ uint64_t Relocation::encodeValue(uint64_t Type, uint64_t Value, uint64_t PC) {
if (Arch == Triple::aarch64)
return encodeValueAArch64(Type, Value, PC);
if (Arch == Triple::riscv64)
llvm_unreachable("not implemented");
return encodeValueRISCV(Type, Value, PC);
return encodeValueX86(Type, Value, PC);
}

Expand Down Expand Up @@ -844,6 +858,8 @@ bool Relocation::isPCRelative(uint64_t Type) {
uint64_t Relocation::getAbs64() {
if (Arch == Triple::aarch64)
return ELF::R_AARCH64_ABS64;
if (Arch == Triple::riscv64)
return ELF::R_RISCV_64;
return ELF::R_X86_64_64;
}

Expand Down
26 changes: 26 additions & 0 deletions bolt/test/RISCV/reloc-64.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: llvm-mc -triple riscv64 -filetype=obj -o %t.o %s
// RUN: ld.lld -q -o %t %t.o
// RUN: llvm-bolt -o %t.bolt %t
// RUN: llvm-readelf -s %t.bolt | FileCheck --check-prefix=SYM %s
// RUN: llvm-readelf -x .data %t.bolt | FileCheck --check-prefix=DATA %s

// SYM: {{0+}}400000 {{.*}} _start{{$}}

// DATA: Hex dump of section '.data':
// DATA-NEXT: 00004000 00000000

.data
.globl d
.p2align 3
d:
.dword _start

.text
.globl _start
.p2align 1
_start:
ret
## Dummy relocation to force relocation mode; without it, _start will not be
## moved to a new address.
.reloc 0, R_RISCV_NONE
.size _start, .-_start