Skip to content

Commit 5cae6a9

Browse files
committed
[JITLink][AArch32] Add support for ELF::R_ARM_THM_MOV{W_PREL_NC,T_PREL}
Support for ELF::R_ARM_THM_MOVW_PREL_NC and ELF::R_ARM_THM_MOVT_PREL is added. Move instructions with PC-relative immediates can be handled in Thumb mode with this addition.
1 parent 5aa934e commit 5cae6a9

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

llvm/include/llvm/ExecutionEngine/JITLink/aarch32.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,15 @@ enum EdgeKind_aarch32 : Edge::Kind {
8989
/// Write immediate value to the top halfword of the destination register
9090
Thumb_MovtAbs,
9191

92-
LastThumbRelocation = Thumb_MovtAbs,
92+
/// Write PC-relative immediate value to the lower halfword of the destination
93+
/// register
94+
Thumb_MovwPrelNC,
95+
96+
/// Write PC-relative immediate value to the top halfword of the destination
97+
/// register
98+
Thumb_MovtPrel,
99+
100+
LastThumbRelocation = Thumb_MovtPrel,
93101
};
94102

95103
/// Flags enum for AArch32-specific symbol properties

llvm/lib/ExecutionEngine/JITLink/ELF_aarch32.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ Expected<aarch32::EdgeKind_aarch32> getJITLinkEdgeKind(uint32_t ELFType) {
5353
return aarch32::Thumb_MovwAbsNC;
5454
case ELF::R_ARM_THM_MOVT_ABS:
5555
return aarch32::Thumb_MovtAbs;
56+
case ELF::R_ARM_THM_MOVW_PREL_NC:
57+
return aarch32::Thumb_MovwPrelNC;
58+
case ELF::R_ARM_THM_MOVT_PREL:
59+
return aarch32::Thumb_MovtPrel;
5660
}
5761

5862
return make_error<JITLinkError>(
@@ -83,6 +87,10 @@ Expected<uint32_t> getELFRelocationType(Edge::Kind Kind) {
8387
return ELF::R_ARM_THM_MOVW_ABS_NC;
8488
case aarch32::Thumb_MovtAbs:
8589
return ELF::R_ARM_THM_MOVT_ABS;
90+
case aarch32::Thumb_MovwPrelNC:
91+
return ELF::R_ARM_THM_MOVW_PREL_NC;
92+
case aarch32::Thumb_MovtPrel:
93+
return ELF::R_ARM_THM_MOVT_PREL;
8694
}
8795

8896
return make_error<JITLinkError>(formatv("Invalid aarch32 edge {0:d}: ",

llvm/lib/ExecutionEngine/JITLink/aarch32.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,12 +377,14 @@ Expected<int64_t> readAddendThumb(LinkGraph &G, Block &B, const Edge &E,
377377
: decodeImmBT4BlT1BlxT2(R.Hi, R.Lo);
378378

379379
case Thumb_MovwAbsNC:
380+
case Thumb_MovwPrelNC:
380381
if (!checkOpcode<Thumb_MovwAbsNC>(R))
381382
return makeUnexpectedOpcodeError(G, R, Kind);
382383
// Initial addend is interpreted as a signed value
383384
return SignExtend64<16>(decodeImmMovtT1MovwT3(R.Hi, R.Lo));
384385

385386
case Thumb_MovtAbs:
387+
case Thumb_MovtPrel:
386388
if (!checkOpcode<Thumb_MovtAbs>(R))
387389
return makeUnexpectedOpcodeError(G, R, Kind);
388390
// Initial addend is interpreted as a signed value
@@ -610,6 +612,20 @@ Error applyFixupThumb(LinkGraph &G, Block &B, const Edge &E,
610612
writeImmediate<Thumb_MovtAbs>(R, encodeImmMovtT1MovwT3(Value));
611613
return Error::success();
612614
}
615+
case Thumb_MovwPrelNC: {
616+
if (!checkOpcode<Thumb_MovwAbsNC>(R))
617+
return makeUnexpectedOpcodeError(G, R, Kind);
618+
uint16_t Value = ((TargetAddress + Addend - FixupAddress) & 0xffff);
619+
writeImmediate<Thumb_MovwAbsNC>(R, encodeImmMovtT1MovwT3(Value));
620+
return Error::success();
621+
}
622+
case Thumb_MovtPrel: {
623+
if (!checkOpcode<Thumb_MovtAbs>(R))
624+
return makeUnexpectedOpcodeError(G, R, Kind);
625+
uint16_t Value = (((TargetAddress + Addend - FixupAddress) >> 16) & 0xffff);
626+
writeImmediate<Thumb_MovtAbs>(R, encodeImmMovtT1MovwT3(Value));
627+
return Error::success();
628+
}
613629

614630
default:
615631
return make_error<JITLinkError>(
@@ -659,6 +675,8 @@ const char *getEdgeKindName(Edge::Kind K) {
659675
KIND_NAME_CASE(Thumb_Jump24)
660676
KIND_NAME_CASE(Thumb_MovwAbsNC)
661677
KIND_NAME_CASE(Thumb_MovtAbs)
678+
KIND_NAME_CASE(Thumb_MovwPrelNC)
679+
KIND_NAME_CASE(Thumb_MovtPrel)
662680
default:
663681
return getGenericEdgeKindName(K);
664682
}

llvm/test/ExecutionEngine/JITLink/AArch32/ELF_static_thumb_reloc.s

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
# RUN: llvm-objdump -r %t.o | FileCheck --check-prefix=CHECK-TYPE %s
33
# RUN: llvm-objdump --disassemble %t.o | FileCheck --check-prefix=CHECK-INSTR %s
44
# RUN: llvm-jitlink -noexec -slab-address 0x76ff0000 -slab-allocate 10Kb \
5-
# RUN: -slab-page-size 4096 -show-entry-es -check %s %t.o
5+
# RUN: -slab-page-size 4096 -abs external_func=0x76bbe880 \
6+
# RUN: -check %s %t.o
67

78

89
.text
@@ -95,6 +96,34 @@ data_symbol:
9596

9697
.text
9798

99+
# CHECK-TYPE: {{[0-9a-f]+}} R_ARM_THM_MOVW_PREL_NC external_func
100+
# CHECK-INSTR: 00000014 <movw_prel>:
101+
# CHECK-INSTR: 14: f240 0000 movw r0, #0x0
102+
# jitlink-check: decode_operand(movw_prel, 1) = \
103+
# jitlink-check: ((external_func - next_pc(movw_prel) + 4)&0x0000ffff)
104+
.globl movw_prel
105+
.type movw_prel,%function
106+
.p2align 1
107+
.code 16
108+
.thumb_func
109+
movw_prel:
110+
movw r0, :lower16:external_func - .
111+
.size movw_prel, .-movw_prel
112+
113+
# CHECK-TYPE: {{[0-9a-f]+}} R_ARM_THM_MOVT_PREL external_func
114+
# CHECK-INSTR: 00000018 <movt_prel>:
115+
# CHECK-INSTR: 18: f2c0 0000 movt r0, #0x0
116+
# jitlink-check: decode_operand(movt_prel, 2) = \
117+
# jitlink-check: ((external_func - next_pc(movt_prel) + 4)&0xffff0000>>16)
118+
.globl movt_prel
119+
.type movt_prel,%function
120+
.p2align 1
121+
.code 16
122+
.thumb_func
123+
movt_prel:
124+
movt r0, :upper16:external_func - .
125+
.size movt_prel, .-movt_prel
126+
98127
# Empty main function for jitlink to be happy
99128
.globl main
100129
.type main,%function

0 commit comments

Comments
 (0)