[llvm] [JITLink][AArch32] Add support for ELF::R_ARM_THM_MOV{W_PREL_NC,T_ABS} (PR #70364)

Eymen Ünay via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 26 11:31:23 PDT 2023


https://github.com/eymay created https://github.com/llvm/llvm-project/pull/70364

    Support for ELF::R_ARM_THM_MOVW_PREL_NC and ELF::R_ARM_THM_MOVW_PREL_NC
    is added. Move instructions with PC-relative immediates can be handled
    in Thumb mode with this addition.


>From 20caa6fb7873d127b8f556738fa3b37adb0c2896 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Eymen=20=C3=9Cnay?= <eymenunay at outlook.com>
Date: Thu, 26 Oct 2023 18:24:14 +0300
Subject: [PATCH 1/2] [JITLink][AArch32] Add test for
 ELF::R_ARM_THM_MOV{W_ABS_NC,T_ABS}

Test cases are similar to those in ELF_static_arm_reloc.s.
---
 .../JITLink/AArch32/ELF_static_thumb_reloc.s  | 35 +++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_static_thumb_reloc.s b/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_static_thumb_reloc.s
index b0d6db045efd216..c694a65ae1061aa 100644
--- a/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_static_thumb_reloc.s
+++ b/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_static_thumb_reloc.s
@@ -60,6 +60,41 @@ jump24_target:
 	bx	lr
 	.size	jump24_target,	.-jump24_target
 
+# CHECK-TYPE: {{[0-9a-f]+}} R_ARM_THM_MOVW_ABS_NC data_symbol
+# CHECK-INSTR: 	0000000c <movw>:
+# CHECK-INSTR: 	       c: f240 0000     movw    r0, #0x0
+# jitlink-check: decode_operand(movw, 1) = (data_symbol&0x0000ffff)
+	.globl	movw
+	.type	movw,%function
+	.p2align	1
+	.code	16
+	.thumb_func
+movw:
+	movw r0, :lower16:data_symbol
+	.size	movw,	.-movw
+
+# CHECK-TYPE: {{[0-9a-f]+}} R_ARM_THM_MOVT_ABS data_symbol
+# CHECK-INSTR: 	00000010 <movt>:
+# CHECK-INSTR: 	      10: f2c0 0000     movt    r0, #0x0
+# We decode the operand with index 2, because movt generates one leading implicit
+# predicate operand that we have to skip in order to decode the data_symbol operand
+# jitlink-check: decode_operand(movt, 2) = (data_symbol&0xffff0000>>16)
+	.globl	movt
+	.type	movt,%function
+	.p2align	1
+	.code	16
+	.thumb_func
+movt:
+	movt r0, :upper16:data_symbol
+	.size	movt,	.-movt
+
+	.data
+	.global data_symbol
+data_symbol:
+	.long 1073741822
+
+	.text
+
 # Empty main function for jitlink to be happy
 	.globl	main
 	.type	main,%function

>From 4c2fd62248cf23af49165ad668ae9b52c2cafe87 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Eymen=20=C3=9Cnay?= <eymenunay at outlook.com>
Date: Thu, 26 Oct 2023 18:26:50 +0300
Subject: [PATCH 2/2] [JITLink][AArch32] Add support for
 ELF::R_ARM_THM_MOV{W_PREL_NC,T_ABS}

Support for ELF::R_ARM_THM_MOVW_PREL_NC and ELF::R_ARM_THM_MOVW_PREL_NC
is added. Move instructions with PC-relative immediates can be handled
in Thumb mode with this addition.
---
 .../llvm/ExecutionEngine/JITLink/aarch32.h    | 10 +++++-
 .../ExecutionEngine/JITLink/ELF_aarch32.cpp   |  8 +++++
 llvm/lib/ExecutionEngine/JITLink/aarch32.cpp  | 18 +++++++++++
 .../JITLink/AArch32/ELF_static_thumb_reloc.s  | 31 ++++++++++++++++++-
 4 files changed, 65 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/ExecutionEngine/JITLink/aarch32.h b/llvm/include/llvm/ExecutionEngine/JITLink/aarch32.h
index 3f36b53d6684a79..97999a61dfe7334 100644
--- a/llvm/include/llvm/ExecutionEngine/JITLink/aarch32.h
+++ b/llvm/include/llvm/ExecutionEngine/JITLink/aarch32.h
@@ -89,7 +89,15 @@ enum EdgeKind_aarch32 : Edge::Kind {
   /// Write immediate value to the top halfword of the destination register
   Thumb_MovtAbs,
 
-  LastThumbRelocation = Thumb_MovtAbs,
+  /// Write PC-relative immediate value to the lower halfword of the destination
+  /// register
+  Thumb_MovwPrelNC,
+
+  /// Write PC-relative immediate value to the top halfword of the destination
+  /// register
+  Thumb_MovtPrel,
+
+  LastThumbRelocation = Thumb_MovtPrel,
 };
 
 /// Flags enum for AArch32-specific symbol properties
diff --git a/llvm/lib/ExecutionEngine/JITLink/ELF_aarch32.cpp b/llvm/lib/ExecutionEngine/JITLink/ELF_aarch32.cpp
index 23946c7de9adbff..f5b1a14d6f0e0a2 100644
--- a/llvm/lib/ExecutionEngine/JITLink/ELF_aarch32.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/ELF_aarch32.cpp
@@ -54,6 +54,10 @@ Expected<aarch32::EdgeKind_aarch32> getJITLinkEdgeKind(uint32_t ELFType) {
     return aarch32::Thumb_MovwAbsNC;
   case ELF::R_ARM_THM_MOVT_ABS:
     return aarch32::Thumb_MovtAbs;
+  case ELF::R_ARM_THM_MOVW_PREL_NC:
+    return aarch32::Thumb_MovwPrelNC;
+  case ELF::R_ARM_THM_MOVT_PREL:
+    return aarch32::Thumb_MovtPrel;
   }
 
   return make_error<JITLinkError>(
@@ -84,6 +88,10 @@ Expected<uint32_t> getELFRelocationType(Edge::Kind Kind) {
     return ELF::R_ARM_THM_MOVW_ABS_NC;
   case aarch32::Thumb_MovtAbs:
     return ELF::R_ARM_THM_MOVT_ABS;
+  case aarch32::Thumb_MovwPrelNC:
+    return ELF::R_ARM_THM_MOVW_PREL_NC;
+  case aarch32::Thumb_MovtPrel:
+    return ELF::R_ARM_THM_MOVT_PREL;
   }
 
   return make_error<JITLinkError>(formatv("Invalid aarch32 edge {0:d}: ",
diff --git a/llvm/lib/ExecutionEngine/JITLink/aarch32.cpp b/llvm/lib/ExecutionEngine/JITLink/aarch32.cpp
index 4aed64966654420..c3823f985ce182d 100644
--- a/llvm/lib/ExecutionEngine/JITLink/aarch32.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/aarch32.cpp
@@ -377,12 +377,14 @@ Expected<int64_t> readAddendThumb(LinkGraph &G, Block &B, const Edge &E,
                   : decodeImmBT4BlT1BlxT2(R.Hi, R.Lo);
 
   case Thumb_MovwAbsNC:
+  case Thumb_MovwPrelNC:
     if (!checkOpcode<Thumb_MovwAbsNC>(R))
       return makeUnexpectedOpcodeError(G, R, Kind);
     // Initial addend is interpreted as a signed value
     return SignExtend64<16>(decodeImmMovtT1MovwT3(R.Hi, R.Lo));
 
   case Thumb_MovtAbs:
+  case Thumb_MovtPrel:
     if (!checkOpcode<Thumb_MovtAbs>(R))
       return makeUnexpectedOpcodeError(G, R, Kind);
     // Initial addend is interpreted as a signed value
@@ -610,6 +612,20 @@ Error applyFixupThumb(LinkGraph &G, Block &B, const Edge &E,
     writeImmediate<Thumb_MovtAbs>(R, encodeImmMovtT1MovwT3(Value));
     return Error::success();
   }
+  case Thumb_MovwPrelNC: {
+    if (!checkOpcode<Thumb_MovwAbsNC>(R))
+      return makeUnexpectedOpcodeError(G, R, Kind);
+    uint16_t Value = ((TargetAddress + Addend - FixupAddress) & 0xffff);
+    writeImmediate<Thumb_MovwAbsNC>(R, encodeImmMovtT1MovwT3(Value));
+    return Error::success();
+  }
+  case Thumb_MovtPrel: {
+    if (!checkOpcode<Thumb_MovtAbs>(R))
+      return makeUnexpectedOpcodeError(G, R, Kind);
+    uint16_t Value = (((TargetAddress + Addend - FixupAddress) >> 16) & 0xffff);
+    writeImmediate<Thumb_MovtAbs>(R, encodeImmMovtT1MovwT3(Value));
+    return Error::success();
+  }
 
   default:
     return make_error<JITLinkError>(
@@ -659,6 +675,8 @@ const char *getEdgeKindName(Edge::Kind K) {
     KIND_NAME_CASE(Thumb_Jump24)
     KIND_NAME_CASE(Thumb_MovwAbsNC)
     KIND_NAME_CASE(Thumb_MovtAbs)
+    KIND_NAME_CASE(Thumb_MovwPrelNC)
+    KIND_NAME_CASE(Thumb_MovtPrel)
   default:
     return getGenericEdgeKindName(K);
   }
diff --git a/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_static_thumb_reloc.s b/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_static_thumb_reloc.s
index c694a65ae1061aa..a523ada96ed32a6 100644
--- a/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_static_thumb_reloc.s
+++ b/llvm/test/ExecutionEngine/JITLink/AArch32/ELF_static_thumb_reloc.s
@@ -2,7 +2,8 @@
 # RUN: llvm-objdump -r %t.o | FileCheck --check-prefix=CHECK-TYPE %s
 # RUN: llvm-objdump --disassemble %t.o | FileCheck --check-prefix=CHECK-INSTR %s
 # RUN: llvm-jitlink -noexec -slab-address 0x76ff0000 -slab-allocate 10Kb \
-# RUN:              -slab-page-size 4096 -show-entry-es -check %s %t.o
+# RUN:              -slab-page-size 4096 -abs external_func=0x76bbe880 \
+# RUN:              -check %s %t.o
 
 
 	.text
@@ -95,6 +96,34 @@ data_symbol:
 
 	.text
 
+# CHECK-TYPE: {{[0-9a-f]+}} R_ARM_THM_MOVW_PREL_NC external_func
+# CHECK-INSTR: 	00000014 <movw_prel>:
+# CHECK-INSTR: 	      14: f240 0000     movw    r0, #0x0
+# jitlink-check: decode_operand(movw_prel, 1) = \
+# jitlink-check:              ((external_func - next_pc(movw_prel) + 4)&0x0000ffff)
+.globl	movw_prel
+.type	movw_prel,%function
+.p2align	1
+.code	16
+.thumb_func
+movw_prel:
+	movw r0, :lower16:external_func - .
+	.size	movw_prel,	.-movw_prel
+
+# CHECK-TYPE: {{[0-9a-f]+}} R_ARM_THM_MOVT_PREL external_func 
+# CHECK-INSTR: 	00000018 <movt_prel>:
+# CHECK-INSTR: 	      18: f2c0 0000    movt    r0, #0x0
+# jitlink-check: decode_operand(movt_prel, 2) = \
+# jitlink-check:               ((external_func - next_pc(movt_prel) + 4)&0xffff0000>>16)
+.globl	movt_prel
+.type	movt_prel,%function
+.p2align	1
+.code	16
+.thumb_func
+movt_prel:
+	movt r0, :upper16:external_func - .
+	.size	movt_prel,	.-movt_prel
+
 # Empty main function for jitlink to be happy
 	.globl	main
 	.type	main,%function



More information about the llvm-commits mailing list