[PATCH] D118151: [JITLink] Fix the incorrect relocation behavior for R_RISCV_BRANCH and extractBits function

luxufan via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 6 22:39:06 PST 2022


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG9920943ea201: [JITLink] Fix the incorrect relocation behavior for R_RISCV_BRANCH (authored by StephenFan).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118151/new/

https://reviews.llvm.org/D118151

Files:
  llvm/include/llvm/ExecutionEngine/JITLink/riscv.h
  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:     -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, 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,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;
Index: llvm/include/llvm/ExecutionEngine/JITLink/riscv.h
===================================================================
--- llvm/include/llvm/ExecutionEngine/JITLink/riscv.h
+++ 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,
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118151.406324.patch
Type: text/x-patch
Size: 3684 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220207/2c954a2e/attachment.bin>


More information about the llvm-commits mailing list