[PATCH] D118151: [JITLink] Fix the incorrect relocation behavior for R_RISCV_BRANCH
luxufan via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 25 08:09:25 PST 2022
StephenFan created this revision.
StephenFan added reviewers: lhames, fourdim, jrtc27.
Herald added subscribers: VincentWu, luke957, achieveartificialintelligence, vkmr, frasercrmck, evandro, luismarques, apazos, sameer.abuasal, s.egerton, Jim, benna, psnobl, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, kito-cheng, niosHD, sabuasal, simoncook, johnrusso, rbar, asb, hiraditya.
StephenFan requested review of this revision.
Herald added subscribers: llvm-commits, pcwang-thead, eopXD, MaskRay.
Herald added a project: LLVM.
In D116573 <https://reviews.llvm.org/D116573>, the relocation behavior of R_RISCV_BRANCH didn't consider that branch instruction like 'bge' has a branch target address which is given as a PC-relative offset, sign-extend and multiplied by 2.
Although the target address is a 12-bits number, acctually its range is [-4096, 4094].
This patch fix it.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D118151
Files:
llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp
llvm/test/ExecutionEngine/JITLink/RISCV/ELF_branch.s
Index: llvm/test/ExecutionEngine/JITLink/RISCV/ELF_branch.s
===================================================================
--- llvm/test/ExecutionEngine/JITLink/RISCV/ELF_branch.s
+++ 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: -define-abs external_func=0xfe \
+# RUN: -slab-allocate 100Kb -slab-address 0xfff00ff4 -slab-page-size 4096 \
+# RUN: -define-abs external_func_positive_offset=0xfff00ffc -define-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: -define-abs external_func=0xfe \
+# RUN: -slab-allocate 100Kb -slab-address 0xfff00ff4 -slab-page-size 4096 \
+# RUN: -define-abs external_func_positive_offset=0xfff00ffc -define-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, at 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
Index: llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp
===================================================================
--- llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp
+++ 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,10 +212,9 @@
if (AlignmentIssue) {
return AlignmentIssue;
}
- int64_t Lo = Value & 0xFFF;
- uint32_t Imm31_25 = extractBits(Lo, 5, 6) << 25 | extractBits(Lo, 12, 1)
+ uint32_t Imm31_25 = extractBits(Value, 5, 6) << 25 | extractBits(Value, 12, 1)
<< 31;
- uint32_t Imm11_7 = extractBits(Lo, 1, 4) << 8 | extractBits(Lo, 11, 1)
+ 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;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118151.402917.patch
Type: text/x-patch
Size: 3126 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220125/bd96afd9/attachment.bin>
More information about the llvm-commits
mailing list