xref: /aosp_15_r20/art/dex2oat/linker/arm64/relative_patcher_arm64.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "linker/arm64/relative_patcher_arm64.h"
18 
19 #include "arch/arm64/asm_support_arm64.h"
20 #include "arch/arm64/instruction_set_features_arm64.h"
21 #include "art_method.h"
22 #include "base/bit_utils.h"
23 #include "base/malloc_arena_pool.h"
24 #include "driver/compiled_method-inl.h"
25 #include "driver/compiler_driver.h"
26 #include "entrypoints/quick/quick_entrypoints_enum.h"
27 #include "heap_poisoning.h"
28 #include "linker/linker_patch.h"
29 #include "lock_word.h"
30 #include "mirror/array-inl.h"
31 #include "mirror/object.h"
32 #include "oat/oat.h"
33 #include "oat/oat_quick_method_header.h"
34 #include "read_barrier.h"
35 #include "stream/output_stream.h"
36 
37 namespace art {
38 namespace linker {
39 
40 namespace {
41 
42 // Maximum positive and negative displacement for method call measured from the patch location.
43 // (Signed 28 bit displacement with the last two bits 0 has range [-2^27, 2^27-4] measured from
44 // the ARM64 PC pointing to the BL.)
45 constexpr uint32_t kMaxMethodCallPositiveDisplacement = (1u << 27) - 4u;
46 constexpr uint32_t kMaxMethodCallNegativeDisplacement = (1u << 27);
47 
48 // Maximum positive and negative displacement for a conditional branch measured from the patch
49 // location. (Signed 21 bit displacement with the last two bits 0 has range [-2^20, 2^20-4]
50 // measured from the ARM64 PC pointing to the B.cond.)
51 constexpr uint32_t kMaxBcondPositiveDisplacement = (1u << 20) - 4u;
52 constexpr uint32_t kMaxBcondNegativeDisplacement = (1u << 20);
53 
54 // The ADRP thunk for erratum 843419 is 2 instructions, i.e. 8 bytes.
55 constexpr uint32_t kAdrpThunkSize = 8u;
56 
IsAdrpPatch(const LinkerPatch & patch)57 inline bool IsAdrpPatch(const LinkerPatch& patch) {
58   switch (patch.GetType()) {
59     case LinkerPatch::Type::kCallRelative:
60     case LinkerPatch::Type::kCallEntrypoint:
61     case LinkerPatch::Type::kBakerReadBarrierBranch:
62       return false;
63     case LinkerPatch::Type::kIntrinsicReference:
64     case LinkerPatch::Type::kBootImageRelRo:
65     case LinkerPatch::Type::kMethodRelative:
66     case LinkerPatch::Type::kMethodAppImageRelRo:
67     case LinkerPatch::Type::kMethodBssEntry:
68     case LinkerPatch::Type::kJniEntrypointRelative:
69     case LinkerPatch::Type::kTypeRelative:
70     case LinkerPatch::Type::kTypeAppImageRelRo:
71     case LinkerPatch::Type::kTypeBssEntry:
72     case LinkerPatch::Type::kPublicTypeBssEntry:
73     case LinkerPatch::Type::kPackageTypeBssEntry:
74     case LinkerPatch::Type::kStringRelative:
75     case LinkerPatch::Type::kStringBssEntry:
76     case LinkerPatch::Type::kMethodTypeBssEntry:
77       return patch.LiteralOffset() == patch.PcInsnOffset();
78   }
79 }
80 
MaxExtraSpace(size_t num_adrp,size_t code_size)81 inline uint32_t MaxExtraSpace(size_t num_adrp, size_t code_size) {
82   if (num_adrp == 0u) {
83     return 0u;
84   }
85   uint32_t alignment_bytes =
86       CompiledMethod::AlignCode(code_size, InstructionSet::kArm64) - code_size;
87   return kAdrpThunkSize * num_adrp + alignment_bytes;
88 }
89 
90 }  // anonymous namespace
91 
Arm64RelativePatcher(RelativePatcherThunkProvider * thunk_provider,RelativePatcherTargetProvider * target_provider,const Arm64InstructionSetFeatures * features)92 Arm64RelativePatcher::Arm64RelativePatcher(RelativePatcherThunkProvider* thunk_provider,
93                                            RelativePatcherTargetProvider* target_provider,
94                                            const Arm64InstructionSetFeatures* features)
95     : ArmBaseRelativePatcher(thunk_provider, target_provider, InstructionSet::kArm64),
96       fix_cortex_a53_843419_(features->NeedFixCortexA53_843419()),
97       reserved_adrp_thunks_(0u),
98       processed_adrp_thunks_(0u) {
99   if (fix_cortex_a53_843419_) {
100     adrp_thunk_locations_.reserve(16u);
101     current_method_thunks_.reserve(16u * kAdrpThunkSize);
102   }
103 }
104 
ReserveSpace(uint32_t offset,const CompiledMethod * compiled_method,MethodReference method_ref)105 uint32_t Arm64RelativePatcher::ReserveSpace(uint32_t offset,
106                                             const CompiledMethod* compiled_method,
107                                             MethodReference method_ref) {
108   if (!fix_cortex_a53_843419_) {
109     DCHECK(adrp_thunk_locations_.empty());
110     return ReserveSpaceInternal(offset, compiled_method, method_ref, 0u);
111   }
112 
113   // Add thunks for previous method if any.
114   if (reserved_adrp_thunks_ != adrp_thunk_locations_.size()) {
115     size_t num_adrp_thunks = adrp_thunk_locations_.size() - reserved_adrp_thunks_;
116     offset = CompiledMethod::AlignCode(offset, InstructionSet::kArm64) +
117              kAdrpThunkSize * num_adrp_thunks;
118     reserved_adrp_thunks_ = adrp_thunk_locations_.size();
119   }
120 
121   // Count the number of ADRP insns as the upper bound on the number of thunks needed
122   // and use it to reserve space for other linker patches.
123   size_t num_adrp = 0u;
124   DCHECK(compiled_method != nullptr);
125   for (const LinkerPatch& patch : compiled_method->GetPatches()) {
126     if (IsAdrpPatch(patch)) {
127       ++num_adrp;
128     }
129   }
130   ArrayRef<const uint8_t> code = compiled_method->GetQuickCode();
131   uint32_t max_extra_space = MaxExtraSpace(num_adrp, code.size());
132   offset = ReserveSpaceInternal(offset, compiled_method, method_ref, max_extra_space);
133   if (num_adrp == 0u) {
134     return offset;
135   }
136 
137   // Now that we have the actual offset where the code will be placed, locate the ADRP insns
138   // that actually require the thunk.
139   uint32_t quick_code_offset = compiled_method->AlignCode(offset + sizeof(OatQuickMethodHeader));
140   uint32_t thunk_offset = compiled_method->AlignCode(quick_code_offset + code.size());
141   DCHECK(compiled_method != nullptr);
142   for (const LinkerPatch& patch : compiled_method->GetPatches()) {
143     if (IsAdrpPatch(patch)) {
144       uint32_t patch_offset = quick_code_offset + patch.LiteralOffset();
145       if (NeedsErratum843419Thunk(code, patch.LiteralOffset(), patch_offset)) {
146         adrp_thunk_locations_.emplace_back(patch_offset, thunk_offset);
147         thunk_offset += kAdrpThunkSize;
148       }
149     }
150   }
151   return offset;
152 }
153 
ReserveSpaceEnd(uint32_t offset)154 uint32_t Arm64RelativePatcher::ReserveSpaceEnd(uint32_t offset) {
155   if (!fix_cortex_a53_843419_) {
156     DCHECK(adrp_thunk_locations_.empty());
157   } else {
158     // Add thunks for the last method if any.
159     if (reserved_adrp_thunks_ != adrp_thunk_locations_.size()) {
160       size_t num_adrp_thunks = adrp_thunk_locations_.size() - reserved_adrp_thunks_;
161       offset = CompiledMethod::AlignCode(offset, InstructionSet::kArm64) +
162                kAdrpThunkSize * num_adrp_thunks;
163       reserved_adrp_thunks_ = adrp_thunk_locations_.size();
164     }
165   }
166   return ArmBaseRelativePatcher::ReserveSpaceEnd(offset);
167 }
168 
WriteThunks(OutputStream * out,uint32_t offset)169 uint32_t Arm64RelativePatcher::WriteThunks(OutputStream* out, uint32_t offset) {
170   if (fix_cortex_a53_843419_) {
171     if (!current_method_thunks_.empty()) {
172       uint32_t aligned_offset = CompiledMethod::AlignCode(offset, InstructionSet::kArm64);
173       if (kIsDebugBuild) {
174         CHECK_ALIGNED(current_method_thunks_.size(), kAdrpThunkSize);
175         size_t num_thunks = current_method_thunks_.size() / kAdrpThunkSize;
176         CHECK_LE(num_thunks, processed_adrp_thunks_);
177         for (size_t i = 0u; i != num_thunks; ++i) {
178           const auto& entry = adrp_thunk_locations_[processed_adrp_thunks_ - num_thunks + i];
179           CHECK_EQ(entry.second, aligned_offset + i * kAdrpThunkSize);
180         }
181       }
182       uint32_t aligned_code_delta = aligned_offset - offset;
183       if (aligned_code_delta != 0u && !WriteCodeAlignment(out, aligned_code_delta)) {
184         return 0u;
185       }
186       if (!WriteMiscThunk(out, ArrayRef<const uint8_t>(current_method_thunks_))) {
187         return 0u;
188       }
189       offset = aligned_offset + current_method_thunks_.size();
190       current_method_thunks_.clear();
191     }
192   }
193   return ArmBaseRelativePatcher::WriteThunks(out, offset);
194 }
195 
PatchCall(std::vector<uint8_t> * code,uint32_t literal_offset,uint32_t patch_offset,uint32_t target_offset)196 void Arm64RelativePatcher::PatchCall(std::vector<uint8_t>* code,
197                                      uint32_t literal_offset,
198                                      uint32_t patch_offset,
199                                      uint32_t target_offset) {
200   DCHECK_ALIGNED(literal_offset, 4u);
201   DCHECK_ALIGNED(patch_offset, 4u);
202   DCHECK_ALIGNED(target_offset, 4u);
203   uint32_t displacement = CalculateMethodCallDisplacement(patch_offset, target_offset & ~1u);
204   PatchBl(code, literal_offset, displacement);
205 }
206 
PatchPcRelativeReference(std::vector<uint8_t> * code,const LinkerPatch & patch,uint32_t patch_offset,uint32_t target_offset)207 void Arm64RelativePatcher::PatchPcRelativeReference(std::vector<uint8_t>* code,
208                                                     const LinkerPatch& patch,
209                                                     uint32_t patch_offset,
210                                                     uint32_t target_offset) {
211   DCHECK_ALIGNED(patch_offset, 4u);
212   DCHECK_ALIGNED(target_offset, 4u);
213   uint32_t literal_offset = patch.LiteralOffset();
214   uint32_t insn = GetInsn(code, literal_offset);
215   uint32_t pc_insn_offset = patch.PcInsnOffset();
216   uint32_t disp = target_offset - ((patch_offset - literal_offset + pc_insn_offset) & ~0xfffu);
217   bool wide = (insn & 0x40000000) != 0;
218   uint32_t shift = wide ? 3u : 2u;
219   if (literal_offset == pc_insn_offset) {
220     // Check it's an ADRP with imm == 0 (unset).
221     DCHECK_EQ((insn & 0xffffffe0u), 0x90000000u)
222         << literal_offset << ", " << pc_insn_offset << ", 0x" << std::hex << insn;
223     if (fix_cortex_a53_843419_ && processed_adrp_thunks_ != adrp_thunk_locations_.size() &&
224         adrp_thunk_locations_[processed_adrp_thunks_].first == patch_offset) {
225       DCHECK(NeedsErratum843419Thunk(ArrayRef<const uint8_t>(*code),
226                                      literal_offset, patch_offset));
227       uint32_t thunk_offset = adrp_thunk_locations_[processed_adrp_thunks_].second;
228       uint32_t adrp_disp = target_offset - (thunk_offset & ~0xfffu);
229       uint32_t adrp = PatchAdrp(insn, adrp_disp);
230 
231       uint32_t out_disp = thunk_offset - patch_offset;
232       DCHECK_EQ(out_disp & 3u, 0u);
233       DCHECK((out_disp >> 27) == 0u || (out_disp >> 27) == 31u);  // 28-bit signed.
234       insn = (out_disp & 0x0fffffffu) >> shift;
235       insn |= 0x14000000;  // B <thunk>
236 
237       uint32_t back_disp = -out_disp;
238       DCHECK_EQ(back_disp & 3u, 0u);
239       DCHECK((back_disp >> 27) == 0u || (back_disp >> 27) == 31u);  // 28-bit signed.
240       uint32_t b_back = (back_disp & 0x0fffffffu) >> 2;
241       b_back |= 0x14000000;  // B <back>
242       size_t thunks_code_offset = current_method_thunks_.size();
243       current_method_thunks_.resize(thunks_code_offset + kAdrpThunkSize);
244       SetInsn(&current_method_thunks_, thunks_code_offset, adrp);
245       SetInsn(&current_method_thunks_, thunks_code_offset + 4u, b_back);
246       static_assert(kAdrpThunkSize == 2 * 4u, "thunk has 2 instructions");
247 
248       processed_adrp_thunks_ += 1u;
249     } else {
250       insn = PatchAdrp(insn, disp);
251     }
252     // Write the new ADRP (or B to the erratum 843419 thunk).
253     SetInsn(code, literal_offset, insn);
254   } else {
255     if ((insn & 0xfffffc00) == 0x91000000) {
256       // ADD immediate, 64-bit with imm12 == 0 (unset).
257       if (kUseBakerReadBarrier) {
258         DCHECK(patch.GetType() == LinkerPatch::Type::kIntrinsicReference ||
259                patch.GetType() == LinkerPatch::Type::kMethodRelative ||
260                patch.GetType() == LinkerPatch::Type::kTypeRelative ||
261                patch.GetType() == LinkerPatch::Type::kStringRelative) << patch.GetType();
262       } else {
263         // With the read barrier (non-Baker) enabled, it could be kStringBssEntry or k*TypeBssEntry.
264         DCHECK(patch.GetType() == LinkerPatch::Type::kIntrinsicReference ||
265                patch.GetType() == LinkerPatch::Type::kMethodRelative ||
266                patch.GetType() == LinkerPatch::Type::kTypeRelative ||
267                patch.GetType() == LinkerPatch::Type::kStringRelative ||
268                patch.GetType() == LinkerPatch::Type::kTypeBssEntry ||
269                patch.GetType() == LinkerPatch::Type::kPublicTypeBssEntry ||
270                patch.GetType() == LinkerPatch::Type::kPackageTypeBssEntry ||
271                patch.GetType() == LinkerPatch::Type::kStringBssEntry) << patch.GetType();
272       }
273       shift = 0u;  // No shift for ADD.
274     } else {
275       // LDR/STR 32-bit or 64-bit with imm12 == 0 (unset).
276       DCHECK(patch.GetType() == LinkerPatch::Type::kBootImageRelRo ||
277              patch.GetType() == LinkerPatch::Type::kMethodAppImageRelRo ||
278              patch.GetType() == LinkerPatch::Type::kMethodBssEntry ||
279              patch.GetType() == LinkerPatch::Type::kJniEntrypointRelative ||
280              patch.GetType() == LinkerPatch::Type::kTypeAppImageRelRo ||
281              patch.GetType() == LinkerPatch::Type::kTypeBssEntry ||
282              patch.GetType() == LinkerPatch::Type::kPublicTypeBssEntry ||
283              patch.GetType() == LinkerPatch::Type::kPackageTypeBssEntry ||
284              patch.GetType() == LinkerPatch::Type::kStringBssEntry ||
285              patch.GetType() == LinkerPatch::Type::kMethodTypeBssEntry) << patch.GetType();
286       DCHECK_EQ(insn & 0xbfbffc00, 0xb9000000) << std::hex << insn;
287     }
288     if (kIsDebugBuild) {
289       uint32_t adrp = GetInsn(code, pc_insn_offset);
290       if ((adrp & 0x9f000000u) != 0x90000000u) {
291         CHECK(fix_cortex_a53_843419_);
292         CHECK_EQ(adrp & 0xfc000000u, 0x14000000u);  // B <thunk>
293         CHECK_ALIGNED(current_method_thunks_.size(), kAdrpThunkSize);
294         size_t num_thunks = current_method_thunks_.size() / kAdrpThunkSize;
295         CHECK_LE(num_thunks, processed_adrp_thunks_);
296         uint32_t b_offset = patch_offset - literal_offset + pc_insn_offset;
297         for (size_t i = processed_adrp_thunks_ - num_thunks; ; ++i) {
298           CHECK_NE(i, processed_adrp_thunks_);
299           if (adrp_thunk_locations_[i].first == b_offset) {
300             size_t idx = num_thunks - (processed_adrp_thunks_ - i);
301             adrp = GetInsn(&current_method_thunks_, idx * kAdrpThunkSize);
302             break;
303           }
304         }
305       }
306       CHECK_EQ(adrp & 0x9f00001fu,                    // Check that pc_insn_offset points
307                0x90000000 | ((insn >> 5) & 0x1fu));   // to ADRP with matching register.
308     }
309     uint32_t imm12 = (disp & 0xfffu) >> shift;
310     insn = (insn & ~(0xfffu << 10)) | (imm12 << 10);
311     SetInsn(code, literal_offset, insn);
312   }
313 }
314 
PatchEntrypointCall(std::vector<uint8_t> * code,const LinkerPatch & patch,uint32_t patch_offset)315 void Arm64RelativePatcher::PatchEntrypointCall(std::vector<uint8_t>* code,
316                                                const LinkerPatch& patch,
317                                                uint32_t patch_offset) {
318   DCHECK_ALIGNED(patch_offset, 4u);
319   ThunkKey key = GetEntrypointCallKey(patch);
320   uint32_t target_offset = GetThunkTargetOffset(key, patch_offset);
321   uint32_t displacement = target_offset - patch_offset;
322   PatchBl(code, patch.LiteralOffset(), displacement);
323 }
324 
PatchBakerReadBarrierBranch(std::vector<uint8_t> * code,const LinkerPatch & patch,uint32_t patch_offset)325 void Arm64RelativePatcher::PatchBakerReadBarrierBranch(std::vector<uint8_t>* code,
326                                                        const LinkerPatch& patch,
327                                                        uint32_t patch_offset) {
328   DCHECK_ALIGNED(patch_offset, 4u);
329   uint32_t literal_offset = patch.LiteralOffset();
330   uint32_t insn = GetInsn(code, literal_offset);
331   DCHECK_EQ(insn & 0xffffffe0u, 0xb5000000);  // CBNZ Xt, +0 (unpatched)
332   ThunkKey key = GetBakerThunkKey(patch);
333   uint32_t target_offset = GetThunkTargetOffset(key, patch_offset);
334   DCHECK_ALIGNED(target_offset, 4u);
335   uint32_t disp = target_offset - patch_offset;
336   DCHECK((disp >> 20) == 0u || (disp >> 20) == 4095u);  // 21-bit signed.
337   insn |= (disp << (5 - 2)) & 0x00ffffe0u;              // Shift bits 2-20 to 5-23.
338   SetInsn(code, literal_offset, insn);
339 }
340 
MaxPositiveDisplacement(const ThunkKey & key)341 uint32_t Arm64RelativePatcher::MaxPositiveDisplacement(const ThunkKey& key) {
342   switch (key.GetType()) {
343     case ThunkType::kMethodCall:
344     case ThunkType::kEntrypointCall:
345       return kMaxMethodCallPositiveDisplacement;
346     case ThunkType::kBakerReadBarrier:
347       return kMaxBcondPositiveDisplacement;
348   }
349 }
350 
MaxNegativeDisplacement(const ThunkKey & key)351 uint32_t Arm64RelativePatcher::MaxNegativeDisplacement(const ThunkKey& key) {
352   switch (key.GetType()) {
353     case ThunkType::kMethodCall:
354     case ThunkType::kEntrypointCall:
355       return kMaxMethodCallNegativeDisplacement;
356     case ThunkType::kBakerReadBarrier:
357       return kMaxBcondNegativeDisplacement;
358   }
359 }
360 
PatchAdrp(uint32_t adrp,uint32_t disp)361 uint32_t Arm64RelativePatcher::PatchAdrp(uint32_t adrp, uint32_t disp) {
362   return (adrp & 0x9f00001fu) |  // Clear offset bits, keep ADRP with destination reg.
363       // Bottom 12 bits are ignored, the next 2 lowest bits are encoded in bits 29-30.
364       ((disp & 0x00003000u) << (29 - 12)) |
365       // The next 16 bits are encoded in bits 5-22.
366       ((disp & 0xffffc000u) >> (12 + 2 - 5)) |
367       // Since the target_offset is based on the beginning of the oat file and the
368       // image space precedes the oat file, the target_offset into image space will
369       // be negative yet passed as uint32_t. Therefore we limit the displacement
370       // to +-2GiB (rather than the maximim +-4GiB) and determine the sign bit from
371       // the highest bit of the displacement. This is encoded in bit 23.
372       ((disp & 0x80000000u) >> (31 - 23));
373 }
374 
PatchBl(std::vector<uint8_t> * code,uint32_t literal_offset,uint32_t displacement)375 void Arm64RelativePatcher::PatchBl(std::vector<uint8_t>* code,
376                                    uint32_t literal_offset,
377                                    uint32_t displacement) {
378   DCHECK_ALIGNED(displacement, 4u);
379   DCHECK((displacement >> 27) == 0u || (displacement >> 27) == 31u);  // 28-bit signed.
380   uint32_t insn = (displacement & 0x0fffffffu) >> 2;
381   insn |= 0x94000000;  // BL
382 
383   // Check that we're just overwriting an existing BL.
384   DCHECK_EQ(GetInsn(code, literal_offset) & 0xfc000000u, 0x94000000u);
385   // Write the new BL.
386   SetInsn(code, literal_offset, insn);
387 }
388 
NeedsErratum843419Thunk(ArrayRef<const uint8_t> code,uint32_t literal_offset,uint32_t patch_offset)389 bool Arm64RelativePatcher::NeedsErratum843419Thunk(ArrayRef<const uint8_t> code,
390                                                    uint32_t literal_offset,
391                                                    uint32_t patch_offset) {
392   DCHECK_EQ(patch_offset & 0x3u, 0u);
393   if ((patch_offset & 0xff8) == 0xff8) {  // ...ff8 or ...ffc
394     uint32_t adrp = GetInsn(code, literal_offset);
395     DCHECK_EQ(adrp & 0x9f000000, 0x90000000);
396     uint32_t next_offset = patch_offset + 4u;
397     uint32_t next_insn = GetInsn(code, literal_offset + 4u);
398 
399     // Below we avoid patching sequences where the adrp is followed by a load which can easily
400     // be proved to be aligned.
401 
402     // First check if the next insn is the LDR using the result of the ADRP.
403     // LDR <Wt>, [<Xn>, #pimm], where <Xn> == ADRP destination reg.
404     if ((next_insn & 0xffc00000) == 0xb9400000 &&
405         (((next_insn >> 5) ^ adrp) & 0x1f) == 0) {
406       return false;
407     }
408 
409     // And since LinkerPatch::Type::k{Method,Type,String}Relative is using the result
410     // of the ADRP for an ADD immediate, check for that as well. We generalize a bit
411     // to include ADD/ADDS/SUB/SUBS immediate that either uses the ADRP destination
412     // or stores the result to a different register.
413     if ((next_insn & 0x1f000000) == 0x11000000 &&
414         ((((next_insn >> 5) ^ adrp) & 0x1f) == 0 || ((next_insn ^ adrp) & 0x1f) != 0)) {
415       return false;
416     }
417 
418     // LDR <Wt>, <label> is always aligned and thus it doesn't cause boundary crossing.
419     if ((next_insn & 0xff000000) == 0x18000000) {
420       return false;
421     }
422 
423     // LDR <Xt>, <label> is aligned iff the pc + displacement is a multiple of 8.
424     if ((next_insn & 0xff000000) == 0x58000000) {
425       bool is_aligned_load = (((next_offset >> 2) ^ (next_insn >> 5)) & 1) == 0;
426       return !is_aligned_load;
427     }
428 
429     // LDR <Wt>, [SP, #<pimm>] and LDR <Xt>, [SP, #<pimm>] are always aligned loads, as SP is
430     // guaranteed to be 128-bits aligned and <pimm> is multiple of the load size.
431     if ((next_insn & 0xbfc003e0) == 0xb94003e0) {
432       return false;
433     }
434     return true;
435   }
436   return false;
437 }
438 
SetInsn(std::vector<uint8_t> * code,uint32_t offset,uint32_t value)439 void Arm64RelativePatcher::SetInsn(std::vector<uint8_t>* code, uint32_t offset, uint32_t value) {
440   DCHECK_LE(offset + 4u, code->size());
441   DCHECK_ALIGNED(offset, 4u);
442   uint8_t* addr = &(*code)[offset];
443   addr[0] = (value >> 0) & 0xff;
444   addr[1] = (value >> 8) & 0xff;
445   addr[2] = (value >> 16) & 0xff;
446   addr[3] = (value >> 24) & 0xff;
447 }
448 
GetInsn(ArrayRef<const uint8_t> code,uint32_t offset)449 uint32_t Arm64RelativePatcher::GetInsn(ArrayRef<const uint8_t> code, uint32_t offset) {
450   DCHECK_LE(offset + 4u, code.size());
451   DCHECK_ALIGNED(offset, 4u);
452   const uint8_t* addr = &code[offset];
453   return
454       (static_cast<uint32_t>(addr[0]) << 0) +
455       (static_cast<uint32_t>(addr[1]) << 8) +
456       (static_cast<uint32_t>(addr[2]) << 16)+
457       (static_cast<uint32_t>(addr[3]) << 24);
458 }
459 
460 template <typename Alloc>
GetInsn(std::vector<uint8_t,Alloc> * code,uint32_t offset)461 uint32_t Arm64RelativePatcher::GetInsn(std::vector<uint8_t, Alloc>* code, uint32_t offset) {
462   return GetInsn(ArrayRef<const uint8_t>(*code), offset);
463 }
464 
465 }  // namespace linker
466 }  // namespace art
467