1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
//===------- AArch32Tests.cpp - Unit tests for the AArch32 backend --------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <llvm/BinaryFormat/ELF.h>
#include <llvm/ExecutionEngine/JITLink/aarch32.h>
#include <llvm/Support/Compiler.h>
#include "gtest/gtest.h"
using namespace llvm;
using namespace llvm::jitlink;
using namespace llvm::jitlink::aarch32;
using namespace llvm::support;
using namespace llvm::support::endian;
struct MutableHalfWords {
MutableHalfWords(HalfWords Preset) : Hi(Preset.Hi), Lo(Preset.Lo) {}
void patch(HalfWords Value, HalfWords Mask) {
Hi = (Hi & ~Mask.Hi) | Value.Hi;
Lo = (Lo & ~Mask.Lo) | Value.Lo;
}
uint16_t Hi; // First halfword
uint16_t Lo; // Second halfword
};
struct MutableWord {
MutableWord(uint32_t Preset) : Wd(Preset) {}
void patch(uint32_t Value, uint32_t Mask) { Wd = (Wd & ~Mask) | Value; }
uint32_t Wd;
};
namespace llvm {
namespace jitlink {
Expected<aarch32::EdgeKind_aarch32>
getJITLinkEdgeKind(uint32_t ELFType, const aarch32::ArmConfig &Cfg);
Expected<uint32_t> getELFRelocationType(Edge::Kind Kind);
} // namespace jitlink
} // namespace llvm
TEST(AArch32_ELF, EdgeKinds) {
// Fails: Invalid ELF type -> JITLink kind
aarch32::ArmConfig Cfg;
Expected<uint32_t> ErrKind = getJITLinkEdgeKind(ELF::R_ARM_ME_TOO, Cfg);
EXPECT_TRUE(errorToBool(ErrKind.takeError()));
// Fails: Invalid JITLink kind -> ELF type
Expected<uint32_t> ErrType = getELFRelocationType(Edge::Invalid);
EXPECT_TRUE(errorToBool(ErrType.takeError()));
for (Edge::Kind K = FirstDataRelocation; K < LastThumbRelocation; K += 1) {
Expected<uint32_t> ELFType = getELFRelocationType(K);
EXPECT_FALSE(errorToBool(ELFType.takeError()))
<< "Failed to translate JITLink kind -> ELF type";
Expected<Edge::Kind> JITLinkKind = getJITLinkEdgeKind(*ELFType, Cfg);
EXPECT_FALSE(errorToBool(JITLinkKind.takeError()))
<< "Failed to translate ELF type -> JITLink kind";
EXPECT_EQ(*JITLinkKind, K) << "Round-trip value inconsistent?";
}
}
TEST(AArch32_ELF, DynFixupInfos) {
// We can do an opcode check for all Arm edges
for (Edge::Kind K = FirstArmRelocation; K < LastArmRelocation; K += 1) {
const auto *Info = FixupInfoBase::getDynFixupInfo(K);
EXPECT_NE(Info, nullptr);
const auto *InfoArm = static_cast<const FixupInfoArm *>(Info);
EXPECT_NE(InfoArm->checkOpcode, nullptr);
EXPECT_FALSE(InfoArm->checkOpcode(0x00000000));
}
// We can do an opcode check for all Thumb edges
for (Edge::Kind K = FirstThumbRelocation; K < LastThumbRelocation; K += 1) {
const auto *Info = FixupInfoBase::getDynFixupInfo(K);
EXPECT_NE(Info, nullptr);
const auto *InfoThumb = static_cast<const FixupInfoThumb *>(Info);
EXPECT_NE(InfoThumb->checkOpcode, nullptr);
EXPECT_FALSE(InfoThumb->checkOpcode(0x0000, 0x0000));
}
// We cannot do it for Data and generic edges
EXPECT_EQ(FixupInfoBase::getDynFixupInfo(FirstDataRelocation), nullptr);
EXPECT_EQ(FixupInfoBase::getDynFixupInfo(Edge::GenericEdgeKind::Invalid),
nullptr);
}
namespace llvm {
namespace jitlink {
namespace aarch32 {
LLVM_ABI HalfWords encodeImmBT4BlT1BlxT2(int64_t Value);
LLVM_ABI HalfWords encodeImmBT4BlT1BlxT2_J1J2(int64_t Value);
LLVM_ABI uint32_t encodeImmBA1BlA1BlxA2(int64_t Value);
LLVM_ABI HalfWords encodeImmMovtT1MovwT3(uint16_t Value);
LLVM_ABI HalfWords encodeRegMovtT1MovwT3(int64_t Value);
LLVM_ABI uint32_t encodeImmMovtA1MovwA2(uint16_t Value);
LLVM_ABI uint32_t encodeRegMovtA1MovwA2(int64_t Value);
LLVM_ABI int64_t decodeImmBT4BlT1BlxT2(uint32_t Hi, uint32_t Lo);
LLVM_ABI int64_t decodeImmBT4BlT1BlxT2_J1J2(uint32_t Hi, uint32_t Lo);
LLVM_ABI int64_t decodeImmBA1BlA1BlxA2(int64_t Value);
LLVM_ABI uint16_t decodeImmMovtT1MovwT3(uint32_t Hi, uint32_t Lo);
LLVM_ABI int64_t decodeRegMovtT1MovwT3(uint32_t Hi, uint32_t Lo);
LLVM_ABI uint16_t decodeImmMovtA1MovwA2(uint64_t Value);
LLVM_ABI int64_t decodeRegMovtA1MovwA2(uint64_t Value);
} // namespace aarch32
} // namespace jitlink
} // namespace llvm
// Big-endian for v7 and v8 (and v6 unless in legacy backwards compatible mode
// be32) have little-endian instructions and big-endian data. In ELF relocatable
// objects big-endian instructions may still be encountered. A be8 supporting
// linker is expected to endian-reverse instructions for the executable.
template <endianness Endian>
static HalfWords makeHalfWords(std::array<uint8_t, 4> Mem) {
return HalfWords{read16<Endian>(Mem.data()), read16<Endian>(Mem.data() + 2)};
}
/// 25-bit branch with link (with J1J2 range extension)
TEST(AArch32_Relocations, Thumb_Call_J1J2) {
static_assert(isInt<25>(16777215), "Max value");
static_assert(isInt<25>(-16777215), "Min value");
static_assert(!isInt<25>(16777217), "First overflow");
static_assert(!isInt<25>(-16777217), "First underflow");
constexpr HalfWords ImmMask = FixupInfo<Thumb_Call>::ImmMask;
static std::array<HalfWords, 3> MemPresets{
makeHalfWords<endianness::little>({0xff, 0xf7, 0xfe, 0xef}), // common
makeHalfWords<endianness::little>({0x00, 0x00, 0x00, 0x00}), // zeros
makeHalfWords<endianness::little>({0xff, 0xff, 0xff, 0xff}), // ones
};
auto EncodeDecode = [ImmMask](int64_t In, MutableHalfWords &Mem) {
Mem.patch(encodeImmBT4BlT1BlxT2_J1J2(In), ImmMask);
return decodeImmBT4BlT1BlxT2_J1J2(Mem.Hi, Mem.Lo);
};
for (MutableHalfWords Mem : MemPresets) {
HalfWords UnaffectedBits(Mem.Hi & ~ImmMask.Hi, Mem.Lo & ~ImmMask.Lo);
EXPECT_EQ(EncodeDecode(1, Mem), 0); // Zero value
EXPECT_EQ(EncodeDecode(0x41, Mem), 0x40); // Common value
EXPECT_EQ(EncodeDecode(16777215, Mem), 16777214); // Maximum value
EXPECT_EQ(EncodeDecode(-16777215, Mem), -16777216); // Minimum value
EXPECT_NE(EncodeDecode(16777217, Mem), 16777217); // First overflow
EXPECT_NE(EncodeDecode(-16777217, Mem), -16777217); // First underflow
EXPECT_TRUE(UnaffectedBits.Hi == (Mem.Hi & ~ImmMask.Hi) &&
UnaffectedBits.Lo == (Mem.Lo & ~ImmMask.Lo))
<< "Diff outside immediate field";
}
}
/// 22-bit branch with link (without J1J2 range extension)
TEST(AArch32_Relocations, Thumb_Call_Bare) {
static_assert(isInt<22>(2097151), "Max value");
static_assert(isInt<22>(-2097151), "Min value");
static_assert(!isInt<22>(2097153), "First overflow");
static_assert(!isInt<22>(-2097153), "First underflow");
constexpr HalfWords ImmMask = FixupInfo<Thumb_Call>::ImmMask;
static std::array<HalfWords, 3> MemPresets{
makeHalfWords<endianness::little>({0xff, 0xf7, 0xfe, 0xef}), // common
makeHalfWords<endianness::little>({0x00, 0x00, 0x00, 0x00}), // zeros
makeHalfWords<endianness::little>({0xff, 0xff, 0xff, 0xff}), // ones
};
auto EncodeDecode = [ImmMask](int64_t In, MutableHalfWords &Mem) {
Mem.patch(encodeImmBT4BlT1BlxT2_J1J2(In), ImmMask);
return decodeImmBT4BlT1BlxT2_J1J2(Mem.Hi, Mem.Lo);
};
for (MutableHalfWords Mem : MemPresets) {
HalfWords UnaffectedBits(Mem.Hi & ~ImmMask.Hi, Mem.Lo & ~ImmMask.Lo);
EXPECT_EQ(EncodeDecode(1, Mem), 0); // Zero value
EXPECT_EQ(EncodeDecode(0x41, Mem), 0x40); // Common value
EXPECT_EQ(EncodeDecode(2097151, Mem), 2097150); // Maximum value
EXPECT_EQ(EncodeDecode(-2097151, Mem), -2097152); // Minimum value
EXPECT_NE(EncodeDecode(2097153, Mem), 2097153); // First overflow
EXPECT_NE(EncodeDecode(-2097153, Mem), -2097153); // First underflow
EXPECT_TRUE(UnaffectedBits.Hi == (Mem.Hi & ~ImmMask.Hi) &&
UnaffectedBits.Lo == (Mem.Lo & ~ImmMask.Lo))
<< "Diff outside immediate field";
}
}
/// 26-bit branch with link
TEST(AArch32_Relocations, Arm_Call_Bare) {
static_assert(isInt<26>(33554430), "Max value");
static_assert(isInt<26>(-33554432), "Min value");
static_assert(!isInt<26>(33554432), "First overflow");
static_assert(!isInt<26>(-33554434), "First underflow");
constexpr uint32_t ImmMask = FixupInfo<Arm_Call>::ImmMask;
static std::array<uint32_t, 3> MemPresets{
0xfeeffff7, // common
0x00000000, // zeros
0xffffffff, // ones
};
auto EncodeDecode = [=](int64_t In, MutableWord &Mem) {
Mem.patch(encodeImmBA1BlA1BlxA2(In), ImmMask);
return decodeImmBA1BlA1BlxA2(Mem.Wd);
};
for (MutableWord Mem : MemPresets) {
MutableWord UnaffectedBits(Mem.Wd & ~ImmMask);
EXPECT_EQ(EncodeDecode(0, Mem), 0); // Zero value
EXPECT_EQ(EncodeDecode(0x40, Mem), 0x40); // Common value
EXPECT_EQ(EncodeDecode(33554428, Mem), 33554428); // Maximum value
EXPECT_EQ(EncodeDecode(-33554432, Mem), -33554432); // Minimum value
EXPECT_NE(EncodeDecode(33554434, Mem), 33554434); // First overflow
EXPECT_NE(EncodeDecode(-33554434, Mem), -33554434); // First underflow
EXPECT_TRUE(UnaffectedBits.Wd == (Mem.Wd & ~ImmMask))
<< "Diff outside immediate field";
}
}
/// Write immediate value to the top halfword of the destination register
TEST(AArch32_Relocations, Thumb_MovtAbs) {
static_assert(isUInt<16>(65535), "Max value");
static_assert(!isUInt<16>(65536), "First overflow");
constexpr HalfWords ImmMask = FixupInfo<Thumb_MovtAbs>::ImmMask;
constexpr HalfWords RegMask = FixupInfo<Thumb_MovtAbs>::RegMask;
static std::array<uint8_t, 3> Registers{0, 5, 12};
static std::array<HalfWords, 3> MemPresets{
makeHalfWords<endianness::little>({0xff, 0xf7, 0xfe, 0xef}), // common
makeHalfWords<endianness::little>({0x00, 0x00, 0x00, 0x00}), // zeros
makeHalfWords<endianness::little>({0xff, 0xff, 0xff, 0xff}), // ones
};
auto EncodeDecode = [ImmMask](uint32_t In, MutableHalfWords &Mem) {
Mem.patch(encodeImmMovtT1MovwT3(In), ImmMask);
return decodeImmMovtT1MovwT3(Mem.Hi, Mem.Lo);
};
for (MutableHalfWords Mem : MemPresets) {
for (uint8_t Reg : Registers) {
HalfWords UnaffectedBits(Mem.Hi & ~(ImmMask.Hi | RegMask.Hi),
Mem.Lo & ~(ImmMask.Lo | RegMask.Lo));
Mem.patch(encodeRegMovtT1MovwT3(Reg), RegMask);
EXPECT_EQ(EncodeDecode(0x76bb, Mem), 0x76bb); // Common value
EXPECT_EQ(EncodeDecode(0, Mem), 0); // Minimum value
EXPECT_EQ(EncodeDecode(0xffff, Mem), 0xffff); // Maximum value
EXPECT_NE(EncodeDecode(0x10000, Mem), 0x10000); // First overflow
// Destination register as well as unaffected bits should be intact
EXPECT_EQ(decodeRegMovtT1MovwT3(Mem.Hi, Mem.Lo), Reg);
EXPECT_TRUE(UnaffectedBits.Hi == (Mem.Hi & ~(ImmMask.Hi | RegMask.Hi)) &&
UnaffectedBits.Lo == (Mem.Lo & ~(ImmMask.Lo | RegMask.Lo)))
<< "Diff outside immediate/register field";
}
}
}
/// Write immediate value to the top halfword of the destination register
TEST(AArch32_Relocations, Arm_MovtAbs) {
static_assert(isUInt<16>(65535), "Max value");
static_assert(!isUInt<16>(65536), "First overflow");
constexpr uint32_t ImmMask = FixupInfo<Arm_MovtAbs>::ImmMask;
constexpr uint32_t RegMask = FixupInfo<Arm_MovtAbs>::RegMask;
static std::array<uint8_t, 3> Registers{0, 5, 12};
static std::array<uint32_t, 3> MemPresets{
0xfeeffff7, // common
0x00000000, // zeros
0xffffffff, // ones
};
auto EncodeDecode = [=](uint64_t In, MutableWord &Mem) {
Mem.patch(encodeImmMovtA1MovwA2(In), ImmMask);
return decodeImmMovtA1MovwA2(Mem.Wd);
};
for (MutableWord Mem : MemPresets) {
for (uint8_t Reg : Registers) {
MutableWord UnaffectedBits(Mem.Wd & ~(ImmMask | RegMask));
Mem.patch(encodeRegMovtA1MovwA2(Reg), RegMask);
EXPECT_EQ(EncodeDecode(0x76bb, Mem), 0x76bb); // Common value
EXPECT_EQ(EncodeDecode(0, Mem), 0); // Minimum value
EXPECT_EQ(EncodeDecode(0xffff, Mem), 0xffff); // Maximum value
EXPECT_NE(EncodeDecode(0x10000, Mem), 0x10000); // First overflow
// Destination register as well as unaffected bits should be intact
EXPECT_EQ(decodeRegMovtA1MovwA2(Mem.Wd), Reg);
EXPECT_TRUE(UnaffectedBits.Wd == (Mem.Wd & ~(ImmMask | RegMask)))
<< "Diff outside immediate/register field";
}
}
}
|