diff --git a/llvm/include/llvm/ExecutionEngine/JITLink/riscv.h b/llvm/include/llvm/ExecutionEngine/JITLink/riscv.h --- a/llvm/include/llvm/ExecutionEngine/JITLink/riscv.h +++ b/llvm/include/llvm/ExecutionEngine/JITLink/riscv.h @@ -37,10 +37,10 @@ /// R_RISCV_64, - /// Low 12 bits of PC-relative branch pointer value relocation + /// PC-relative branch pointer value relocation /// /// Fixup expression: - /// Fixup <- (Target - Fixup + Addend) & 0xFFF + /// Fixup <- (Target - Fixup + Addend) /// R_RISCV_BRANCH, diff --git a/llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp b/llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp --- a/llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp +++ b/llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp @@ -160,7 +160,7 @@ } static uint32_t extractBits(uint32_t Num, unsigned Low, unsigned Size) { - return (Num & (((1ULL << (Size + 1)) - 1) << Low)) >> Low; + return (Num & (((1ULL << Size) - 1) << Low)) >> Low; } inline Error checkAlignment(llvm::orc::ExecutorAddr loc, uint64_t v, int n, @@ -212,11 +212,10 @@ if (AlignmentIssue) { return AlignmentIssue; } - int64_t Lo = Value & 0xFFF; - uint32_t Imm31_25 = extractBits(Lo, 5, 6) << 25 | extractBits(Lo, 12, 1) - << 31; - uint32_t Imm11_7 = extractBits(Lo, 1, 4) << 8 | extractBits(Lo, 11, 1) - << 7; + uint32_t Imm31_25 = + extractBits(Value, 5, 6) << 25 | extractBits(Value, 12, 1) << 31; + uint32_t Imm11_7 = + extractBits(Value, 1, 4) << 8 | extractBits(Value, 11, 1) << 7; uint32_t RawInstr = *(little32_t *)FixupPtr; *(little32_t *)FixupPtr = (RawInstr & 0x1FFF07F) | Imm31_25 | Imm11_7; break; diff --git a/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_branch.s b/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_branch.s --- a/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_branch.s +++ b/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_branch.s @@ -4,18 +4,16 @@ # RUN: llvm-mc -triple=riscv32 -filetype=obj \ # RUN: -o %t/elf_riscv32_branch.o %s # RUN: llvm-jitlink -noexec \ -# RUN: -slab-allocate 100Kb -slab-address 0xfff00000 -slab-page-size 4096 \ -# RUN: -abs external_func=0xfe \ +# RUN: -slab-allocate 100Kb -slab-address 0xfff00ff4 -slab-page-size 4096 \ +# RUN: -abs external_func_positive_offset=0xfff00ffc -abs external_func_negative_offset=0xfff00000\ # RUN: -check %s %t/elf_riscv64_branch.o # RUN: llvm-jitlink -noexec \ -# RUN: -slab-allocate 100Kb -slab-address 0xfff00000 -slab-page-size 4096 \ -# RUN: -abs external_func=0xfe \ +# RUN: -slab-allocate 100Kb -slab-address 0xfff00ff4 -slab-page-size 4096 \ +# RUN: -abs external_func_positive_offset=0xfff00ffc -abs external_func_negative_offset=0xfff00000 \ # RUN: -check %s %t/elf_riscv32_branch.o # .text - .file "testcase.c" - # Empty main entry point. .globl main .p2align 1 @@ -27,11 +25,13 @@ # Test R_RISCV_BRANCH -# jitlink-check: decode_operand(test_branch, 2)[11:0] = (external_func - test_branch)[11:0] +# jitlink-check: decode_operand(test_branch, 2)[12:0] = (external_func_positive_offset - test_branch)[12:0] +# jitlink-check: decode_operand(test_branch+4, 2)[12:0] = (external_func_negative_offset - test_branch - 4)[12:0] .globl test_branch .p2align 1 .type test_branch,@function test_branch: - bge a0, a1, external_func + bge a0, a1, external_func_positive_offset + bge a0, a1, external_func_negative_offset .size test_branch, .-test_branch