[llvm] dda116b - [JITLink] Add support of R_X86_64_32S relocation

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 22 01:51:57 PDT 2021


Author: luxufan
Date: 2021-08-22T16:45:25+08:00
New Revision: dda116bc3d9c126b3c962563fc80ae332d42801d

URL: https://github.com/llvm/llvm-project/commit/dda116bc3d9c126b3c962563fc80ae332d42801d
DIFF: https://github.com/llvm/llvm-project/commit/dda116bc3d9c126b3c962563fc80ae332d42801d.diff

LOG: [JITLink] Add support of R_X86_64_32S relocation

This patch supported the R_X86_64_32S relocation and add the Pointer32Signed generic edge kind.

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D108446

Added: 
    llvm/test/ExecutionEngine/JITLink/X86/ELF_x86_64_absolute_relocations.s

Modified: 
    llvm/include/llvm/ExecutionEngine/JITLink/ELF_x86_64.h
    llvm/include/llvm/ExecutionEngine/JITLink/x86_64.h
    llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp
    llvm/lib/ExecutionEngine/JITLink/x86_64.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ExecutionEngine/JITLink/ELF_x86_64.h b/llvm/include/llvm/ExecutionEngine/JITLink/ELF_x86_64.h
index 36e346c593251..611d58b974494 100644
--- a/llvm/include/llvm/ExecutionEngine/JITLink/ELF_x86_64.h
+++ b/llvm/include/llvm/ExecutionEngine/JITLink/ELF_x86_64.h
@@ -21,6 +21,7 @@ namespace jitlink {
 namespace ELF_x86_64_Edges {
 enum ELFX86RelocationKind : Edge::Kind {
   Branch32 = Edge::FirstRelocation,
+  Pointer32Signed,
   Pointer64,
   PCRel32,
   PCRel32GOTLoad,

diff  --git a/llvm/include/llvm/ExecutionEngine/JITLink/x86_64.h b/llvm/include/llvm/ExecutionEngine/JITLink/x86_64.h
index fdf804047db7c..a11174a7f34ec 100644
--- a/llvm/include/llvm/ExecutionEngine/JITLink/x86_64.h
+++ b/llvm/include/llvm/ExecutionEngine/JITLink/x86_64.h
@@ -42,6 +42,16 @@ enum EdgeKind_x86_64 : Edge::Kind {
   ///
   Pointer32,
 
+  /// A signed 32-bit pointer value relocation
+  ///
+  /// Fixup expression:
+  ///   Fixup <- Target + Addend : int32
+  ///
+  /// Errors:
+  ///   - The target must reside in the signed 32-bits([-2**31, 2**32 - 1]) of
+  ///   the address space, otherwise an out-of-range error will be returned.
+  Pointer32Signed,
+
   /// A 64-bit delta.
   ///
   /// Delta from the fixup to the target.
@@ -380,6 +390,14 @@ inline Error applyFixup(LinkGraph &G, Block &B, const Edge &E,
       return makeTargetOutOfRangeError(G, B, E);
     break;
   }
+  case Pointer32Signed: {
+    int64_t Value = E.getTarget().getAddress() + E.getAddend();
+    if (LLVM_LIKELY(isInRangeForImmS32(Value)))
+      *(little32_t *)FixupPtr = Value;
+    else
+      return makeTargetOutOfRangeError(G, B, E);
+    break;
+  }
 
   case BranchPCRel32:
   case BranchPCRel32ToPtrJumpStub:

diff  --git a/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp b/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp
index 736bcada9c8f0..e65af56a2f246 100644
--- a/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp
@@ -176,6 +176,8 @@ class ELFLinkGraphBuilder_x86_64 : public ELFLinkGraphBuilder<object::ELF64LE> {
   static Expected<ELF_x86_64_Edges::ELFX86RelocationKind>
   getRelocationKind(const uint32_t Type) {
     switch (Type) {
+    case ELF::R_X86_64_32S:
+      return ELF_x86_64_Edges::ELFX86RelocationKind::Pointer32Signed;
     case ELF::R_X86_64_PC32:
       return ELF_x86_64_Edges::ELFX86RelocationKind::PCRel32;
     case ELF::R_X86_64_PC64:
@@ -298,6 +300,9 @@ class ELFLinkGraphBuilder_x86_64 : public ELFLinkGraphBuilder<object::ELF64LE> {
         case Delta64:
           Kind = x86_64::Delta64;
           break;
+        case Pointer32Signed:
+          Kind = x86_64::Pointer32Signed;
+          break;
         case Pointer64:
           Kind = x86_64::Pointer64;
           break;
@@ -497,6 +502,8 @@ const char *getELFX86RelocationKindName(Edge::Kind R) {
   switch (R) {
   case Branch32:
     return "Branch32";
+  case Pointer32Signed:
+    return "Pointer32Signed";
   case Pointer64:
     return "Pointer64";
   case PCRel32:

diff  --git a/llvm/lib/ExecutionEngine/JITLink/x86_64.cpp b/llvm/lib/ExecutionEngine/JITLink/x86_64.cpp
index c2e25c6c685eb..4c107cbf41924 100644
--- a/llvm/lib/ExecutionEngine/JITLink/x86_64.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/x86_64.cpp
@@ -24,6 +24,8 @@ const char *getEdgeKindName(Edge::Kind K) {
     return "Pointer64";
   case Pointer32:
     return "Pointer32";
+  case Pointer32Signed:
+    return "Pointer32Signed";
   case Delta64:
     return "Delta64";
   case Delta32:

diff  --git a/llvm/test/ExecutionEngine/JITLink/X86/ELF_x86_64_absolute_relocations.s b/llvm/test/ExecutionEngine/JITLink/X86/ELF_x86_64_absolute_relocations.s
new file mode 100644
index 0000000000000..56dc1e0eca200
--- /dev/null
+++ b/llvm/test/ExecutionEngine/JITLink/X86/ELF_x86_64_absolute_relocations.s
@@ -0,0 +1,35 @@
+# RUN: rm -rf %t && mkdir -p %t
+# RUN: llvm-mc -triple=x86_64-unknown-linux -position-independent -filetype=obj \
+# RUN:         -o %t/elf_abs_reloc.o %s
+# RUN: llvm-jitlink -noexec -slab-allocate 100Kb -slab-address 0xfff00000 \
+# RUN:              -define-abs external_data_low=0x1 \
+# RUN:              -define-abs external_data_high=0xffffffff80000000 \
+# RUN:              -check %s %t/elf_abs_reloc.o
+#
+# Test ELF absolute relocations.
+
+
+        .text
+        .file   "testcase.c"
+
+# Empty main entry point.
+        .globl  main
+        .p2align        4, 0x90
+        .type   main, at function
+main:
+        retq
+
+        .size   main, .-main
+
+# R_X86_64_32S handling
+# Test the target value is in range of signed 32-bits imm
+# jitlink-check: decode_operand(test_abs_32S, 4) = external_data_low
+# jitlink-check: decode_operand(test_abs_32S+7, 4)[31:0] = external_data_high[31:0]
+        .globl  test_abs_32S
+        .p2align       4, 0x90
+        .type   test_abs_32S, at function
+test_abs_32S:
+        movl    external_data_low, %eax
+        movl    external_data_high, %esi
+
+         .size   test_abs_32S, .-test_abs_32S


        


More information about the llvm-commits mailing list