Skip to content

Commit 1ae74d5

Browse files
committed
[Bolt][Instrumentation] Initial instrumentation support for RISCV64
This patch adds code generation for RISCV64 instrumentation.The work involved includes the following three points: a) Implements support for instrumenting direct function call and jump on RISC-V which relies on , Atomic instructions (used to increment counters) are only available on RISC-V when the A extension is used. b) Implements support for instrumenting direct function inderect call by implementing the createInstrumentedIndCallHandlerEntryBB and createInstrumentedIndCallHandlerExitBB interfaces. In this process, we need to accurately record the target address and IndCallID to ensure the correct recording of the indirect call counters. c)Implemented the RISCV64 Bolt runtime library, implemented some system call interfaces through embedded assembly. Get the difference between runtime addrress of .text section andstatic address in section header table, which in turn can be used to search for indirect call description. However, the community code currently has problems with relocation in some scenarios, but this has nothing to do with instrumentation. We may continue to submit patches to fix the related bugs.
1 parent 5c65a32 commit 1ae74d5

File tree

11 files changed

+979
-6
lines changed

11 files changed

+979
-6
lines changed

bolt/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ endforeach()
8282

8383
set(BOLT_ENABLE_RUNTIME_default OFF)
8484
if ((CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64"
85-
OR CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)$")
85+
OR CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)$"
86+
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
8687
AND (CMAKE_SYSTEM_NAME STREQUAL "Linux"
8788
OR CMAKE_SYSTEM_NAME STREQUAL "Darwin")
8889
AND (NOT CMAKE_CROSSCOMPILING))

bolt/lib/Core/Relocation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ static bool isSupportedRISCV(uint32_t Type) {
122122
case ELF::R_RISCV_LO12_S:
123123
case ELF::R_RISCV_64:
124124
case ELF::R_RISCV_TLS_GOT_HI20:
125+
case ELF::R_RISCV_TLS_GD_HI20:
125126
case ELF::R_RISCV_TPREL_HI20:
126127
case ELF::R_RISCV_TPREL_ADD:
127128
case ELF::R_RISCV_TPREL_LO12_I:
@@ -234,6 +235,7 @@ static size_t getSizeForTypeRISCV(uint32_t Type) {
234235
case ELF::R_RISCV_64:
235236
case ELF::R_RISCV_GOT_HI20:
236237
case ELF::R_RISCV_TLS_GOT_HI20:
238+
case ELF::R_RISCV_TLS_GD_HI20:
237239
// See extractValueRISCV for why this is necessary.
238240
return 8;
239241
}
@@ -468,6 +470,7 @@ static uint64_t extractValueRISCV(uint32_t Type, uint64_t Contents,
468470
return extractBImmRISCV(Contents);
469471
case ELF::R_RISCV_GOT_HI20:
470472
case ELF::R_RISCV_TLS_GOT_HI20:
473+
case ELF::R_RISCV_TLS_GD_HI20:
471474
// We need to know the exact address of the GOT entry so we extract the
472475
// value from both the AUIPC and L[D|W]. We cannot rely on the symbol in the
473476
// relocation for this since it simply refers to the object that is stored
@@ -683,6 +686,7 @@ static bool isPCRelativeRISCV(uint32_t Type) {
683686
case ELF::R_RISCV_RVC_BRANCH:
684687
case ELF::R_RISCV_32_PCREL:
685688
case ELF::R_RISCV_TLS_GOT_HI20:
689+
case ELF::R_RISCV_TLS_GD_HI20:
686690
return true;
687691
}
688692
}

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2901,7 +2901,7 @@ void RewriteInstance::handleRelocation(const SectionRef &RelocatedSection,
29012901
// of sections and whose symbol address is not really what should be
29022902
// encoded in the instruction). So we essentially disabled this check
29032903
// for AArch64 and live with bogus names for objects.
2904-
assert((IsAArch64 || IsSectionRelocation ||
2904+
assert((IsAArch64 || BC->isRISCV() || IsSectionRelocation ||
29052905
BD->nameStartsWith(SymbolName) ||
29062906
BD->nameStartsWith("PG" + SymbolName) ||
29072907
(BD->nameStartsWith("ANONYMOUS") &&

0 commit comments

Comments
 (0)