Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 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/instruction_set_features_arm64.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 20 | #include "art_method.h" |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 21 | #include "compiled_method.h" |
| 22 | #include "driver/compiler_driver.h" |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 23 | #include "linker/output_stream.h" |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 24 | #include "oat.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 25 | #include "oat_quick_method_header.h" |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 26 | #include "utils/arm64/assembler_arm64.h" |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | namespace linker { |
| 30 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame^] | 31 | namespace { |
| 32 | |
| 33 | inline bool IsAdrpPatch(const LinkerPatch& patch) { |
| 34 | LinkerPatchType type = patch.Type(); |
| 35 | return (type == kLinkerPatchStringRelative || type == kLinkerPatchDexCacheArray) && |
| 36 | patch.LiteralOffset() == patch.PcInsnOffset(); |
| 37 | } |
| 38 | |
| 39 | } // anonymous namespace |
| 40 | |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 41 | Arm64RelativePatcher::Arm64RelativePatcher(RelativePatcherTargetProvider* provider, |
| 42 | const Arm64InstructionSetFeatures* features) |
| 43 | : ArmBaseRelativePatcher(provider, kArm64, CompileThunkCode(), |
| 44 | kMaxPositiveDisplacement, kMaxNegativeDisplacement), |
| 45 | fix_cortex_a53_843419_(features->NeedFixCortexA53_843419()), |
| 46 | reserved_adrp_thunks_(0u), |
| 47 | processed_adrp_thunks_(0u) { |
| 48 | if (fix_cortex_a53_843419_) { |
| 49 | adrp_thunk_locations_.reserve(16u); |
| 50 | current_method_thunks_.reserve(16u * kAdrpThunkSize); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | uint32_t Arm64RelativePatcher::ReserveSpace(uint32_t offset, |
Vladimir Marko | 4d23c9d | 2015-04-01 23:03:09 +0100 | [diff] [blame] | 55 | const CompiledMethod* compiled_method, |
| 56 | MethodReference method_ref) { |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 57 | if (!fix_cortex_a53_843419_) { |
| 58 | DCHECK(adrp_thunk_locations_.empty()); |
Vladimir Marko | 4d23c9d | 2015-04-01 23:03:09 +0100 | [diff] [blame] | 59 | return ReserveSpaceInternal(offset, compiled_method, method_ref, 0u); |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // Add thunks for previous method if any. |
| 63 | if (reserved_adrp_thunks_ != adrp_thunk_locations_.size()) { |
| 64 | size_t num_adrp_thunks = adrp_thunk_locations_.size() - reserved_adrp_thunks_; |
| 65 | offset = CompiledMethod::AlignCode(offset, kArm64) + kAdrpThunkSize * num_adrp_thunks; |
| 66 | reserved_adrp_thunks_ = adrp_thunk_locations_.size(); |
| 67 | } |
| 68 | |
| 69 | // Count the number of ADRP insns as the upper bound on the number of thunks needed |
| 70 | // and use it to reserve space for other linker patches. |
| 71 | size_t num_adrp = 0u; |
Vladimir Marko | 71b0ddf | 2015-04-02 19:45:06 +0100 | [diff] [blame] | 72 | DCHECK(compiled_method != nullptr); |
| 73 | for (const LinkerPatch& patch : compiled_method->GetPatches()) { |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame^] | 74 | if (IsAdrpPatch(patch)) { |
Vladimir Marko | 71b0ddf | 2015-04-02 19:45:06 +0100 | [diff] [blame] | 75 | ++num_adrp; |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 76 | } |
| 77 | } |
Vladimir Marko | 4d23c9d | 2015-04-01 23:03:09 +0100 | [diff] [blame] | 78 | offset = ReserveSpaceInternal(offset, compiled_method, method_ref, kAdrpThunkSize * num_adrp); |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 79 | if (num_adrp == 0u) { |
| 80 | return offset; |
| 81 | } |
| 82 | |
| 83 | // Now that we have the actual offset where the code will be placed, locate the ADRP insns |
| 84 | // that actually require the thunk. |
| 85 | uint32_t quick_code_offset = compiled_method->AlignCode(offset) + sizeof(OatQuickMethodHeader); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 86 | ArrayRef<const uint8_t> code = compiled_method->GetQuickCode(); |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 87 | uint32_t thunk_offset = compiled_method->AlignCode(quick_code_offset + code.size()); |
| 88 | DCHECK(compiled_method != nullptr); |
| 89 | for (const LinkerPatch& patch : compiled_method->GetPatches()) { |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame^] | 90 | if (IsAdrpPatch(patch)) { |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 91 | uint32_t patch_offset = quick_code_offset + patch.LiteralOffset(); |
| 92 | if (NeedsErratum843419Thunk(code, patch.LiteralOffset(), patch_offset)) { |
| 93 | adrp_thunk_locations_.emplace_back(patch_offset, thunk_offset); |
| 94 | thunk_offset += kAdrpThunkSize; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | return offset; |
| 99 | } |
| 100 | |
Vladimir Marko | 71b0ddf | 2015-04-02 19:45:06 +0100 | [diff] [blame] | 101 | uint32_t Arm64RelativePatcher::ReserveSpaceEnd(uint32_t offset) { |
| 102 | if (!fix_cortex_a53_843419_) { |
| 103 | DCHECK(adrp_thunk_locations_.empty()); |
| 104 | } else { |
| 105 | // Add thunks for the last method if any. |
| 106 | if (reserved_adrp_thunks_ != adrp_thunk_locations_.size()) { |
| 107 | size_t num_adrp_thunks = adrp_thunk_locations_.size() - reserved_adrp_thunks_; |
| 108 | offset = CompiledMethod::AlignCode(offset, kArm64) + kAdrpThunkSize * num_adrp_thunks; |
| 109 | reserved_adrp_thunks_ = adrp_thunk_locations_.size(); |
| 110 | } |
| 111 | } |
| 112 | return ArmBaseRelativePatcher::ReserveSpaceEnd(offset); |
| 113 | } |
| 114 | |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 115 | uint32_t Arm64RelativePatcher::WriteThunks(OutputStream* out, uint32_t offset) { |
| 116 | if (fix_cortex_a53_843419_) { |
| 117 | if (!current_method_thunks_.empty()) { |
| 118 | uint32_t aligned_offset = CompiledMethod::AlignCode(offset, kArm64); |
| 119 | if (kIsDebugBuild) { |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 120 | CHECK_ALIGNED(current_method_thunks_.size(), kAdrpThunkSize); |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 121 | size_t num_thunks = current_method_thunks_.size() / kAdrpThunkSize; |
| 122 | CHECK_LE(num_thunks, processed_adrp_thunks_); |
| 123 | for (size_t i = 0u; i != num_thunks; ++i) { |
| 124 | const auto& entry = adrp_thunk_locations_[processed_adrp_thunks_ - num_thunks + i]; |
| 125 | CHECK_EQ(entry.second, aligned_offset + i * kAdrpThunkSize); |
| 126 | } |
| 127 | } |
| 128 | uint32_t aligned_code_delta = aligned_offset - offset; |
| 129 | if (aligned_code_delta != 0u && !WriteCodeAlignment(out, aligned_code_delta)) { |
| 130 | return 0u; |
| 131 | } |
| 132 | if (!WriteMiscThunk(out, ArrayRef<const uint8_t>(current_method_thunks_))) { |
| 133 | return 0u; |
| 134 | } |
| 135 | offset = aligned_offset + current_method_thunks_.size(); |
| 136 | current_method_thunks_.clear(); |
| 137 | } |
| 138 | } |
| 139 | return ArmBaseRelativePatcher::WriteThunks(out, offset); |
| 140 | } |
| 141 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 142 | void Arm64RelativePatcher::PatchCall(std::vector<uint8_t>* code, |
| 143 | uint32_t literal_offset, |
| 144 | uint32_t patch_offset, uint32_t |
| 145 | target_offset) { |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 146 | DCHECK_LE(literal_offset + 4u, code->size()); |
| 147 | DCHECK_EQ(literal_offset & 3u, 0u); |
| 148 | DCHECK_EQ(patch_offset & 3u, 0u); |
| 149 | DCHECK_EQ(target_offset & 3u, 0u); |
| 150 | uint32_t displacement = CalculateDisplacement(patch_offset, target_offset & ~1u); |
| 151 | DCHECK_EQ(displacement & 3u, 0u); |
| 152 | DCHECK((displacement >> 27) == 0u || (displacement >> 27) == 31u); // 28-bit signed. |
| 153 | uint32_t insn = (displacement & 0x0fffffffu) >> 2; |
| 154 | insn |= 0x94000000; // BL |
| 155 | |
| 156 | // Check that we're just overwriting an existing BL. |
| 157 | DCHECK_EQ(GetInsn(code, literal_offset) & 0xfc000000u, 0x94000000u); |
| 158 | // Write the new BL. |
| 159 | SetInsn(code, literal_offset, insn); |
| 160 | } |
| 161 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame^] | 162 | void Arm64RelativePatcher::PatchPcRelativeReference(std::vector<uint8_t>* code, |
| 163 | const LinkerPatch& patch, |
| 164 | uint32_t patch_offset, |
| 165 | uint32_t target_offset) { |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 166 | DCHECK_EQ(patch_offset & 3u, 0u); |
| 167 | DCHECK_EQ(target_offset & 3u, 0u); |
| 168 | uint32_t literal_offset = patch.LiteralOffset(); |
| 169 | uint32_t insn = GetInsn(code, literal_offset); |
| 170 | uint32_t pc_insn_offset = patch.PcInsnOffset(); |
| 171 | uint32_t disp = target_offset - ((patch_offset - literal_offset + pc_insn_offset) & ~0xfffu); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 172 | bool wide = (insn & 0x40000000) != 0; |
| 173 | uint32_t shift = wide ? 3u : 2u; |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 174 | if (literal_offset == pc_insn_offset) { |
| 175 | // Check it's an ADRP with imm == 0 (unset). |
| 176 | DCHECK_EQ((insn & 0xffffffe0u), 0x90000000u) |
| 177 | << literal_offset << ", " << pc_insn_offset << ", 0x" << std::hex << insn; |
| 178 | if (fix_cortex_a53_843419_ && processed_adrp_thunks_ != adrp_thunk_locations_.size() && |
| 179 | adrp_thunk_locations_[processed_adrp_thunks_].first == patch_offset) { |
| 180 | DCHECK(NeedsErratum843419Thunk(ArrayRef<const uint8_t>(*code), |
| 181 | literal_offset, patch_offset)); |
| 182 | uint32_t thunk_offset = adrp_thunk_locations_[processed_adrp_thunks_].second; |
| 183 | uint32_t adrp_disp = target_offset - (thunk_offset & ~0xfffu); |
| 184 | uint32_t adrp = PatchAdrp(insn, adrp_disp); |
| 185 | |
| 186 | uint32_t out_disp = thunk_offset - patch_offset; |
| 187 | DCHECK_EQ(out_disp & 3u, 0u); |
| 188 | DCHECK((out_disp >> 27) == 0u || (out_disp >> 27) == 31u); // 28-bit signed. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 189 | insn = (out_disp & 0x0fffffffu) >> shift; |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 190 | insn |= 0x14000000; // B <thunk> |
| 191 | |
| 192 | uint32_t back_disp = -out_disp; |
| 193 | DCHECK_EQ(back_disp & 3u, 0u); |
| 194 | DCHECK((back_disp >> 27) == 0u || (back_disp >> 27) == 31u); // 28-bit signed. |
| 195 | uint32_t b_back = (back_disp & 0x0fffffffu) >> 2; |
| 196 | b_back |= 0x14000000; // B <back> |
| 197 | size_t thunks_code_offset = current_method_thunks_.size(); |
| 198 | current_method_thunks_.resize(thunks_code_offset + kAdrpThunkSize); |
| 199 | SetInsn(¤t_method_thunks_, thunks_code_offset, adrp); |
| 200 | SetInsn(¤t_method_thunks_, thunks_code_offset + 4u, b_back); |
| 201 | static_assert(kAdrpThunkSize == 2 * 4u, "thunk has 2 instructions"); |
| 202 | |
| 203 | processed_adrp_thunks_ += 1u; |
| 204 | } else { |
| 205 | insn = PatchAdrp(insn, disp); |
| 206 | } |
| 207 | // Write the new ADRP (or B to the erratum 843419 thunk). |
| 208 | SetInsn(code, literal_offset, insn); |
| 209 | } else { |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame^] | 210 | if ((insn & 0xfffffc00) == 0x91000000) { |
| 211 | // ADD immediate, 64-bit with imm12 == 0 (unset). |
| 212 | DCHECK(patch.Type() == kLinkerPatchStringRelative) << patch.Type(); |
| 213 | shift = 0u; // No shift for ADD. |
| 214 | } else { |
| 215 | // LDR 32-bit or 64-bit with imm12 == 0 (unset). |
| 216 | DCHECK(patch.Type() == kLinkerPatchDexCacheArray) << patch.Type(); |
| 217 | DCHECK_EQ(insn & 0xbffffc00, 0xb9400000) << std::hex << insn; |
| 218 | } |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 219 | if (kIsDebugBuild) { |
| 220 | uint32_t adrp = GetInsn(code, pc_insn_offset); |
| 221 | if ((adrp & 0x9f000000u) != 0x90000000u) { |
| 222 | CHECK(fix_cortex_a53_843419_); |
| 223 | CHECK_EQ(adrp & 0xfc000000u, 0x14000000u); // B <thunk> |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 224 | CHECK_ALIGNED(current_method_thunks_.size(), kAdrpThunkSize); |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 225 | size_t num_thunks = current_method_thunks_.size() / kAdrpThunkSize; |
| 226 | CHECK_LE(num_thunks, processed_adrp_thunks_); |
| 227 | uint32_t b_offset = patch_offset - literal_offset + pc_insn_offset; |
| 228 | for (size_t i = processed_adrp_thunks_ - num_thunks; ; ++i) { |
| 229 | CHECK_NE(i, processed_adrp_thunks_); |
| 230 | if (adrp_thunk_locations_[i].first == b_offset) { |
| 231 | size_t idx = num_thunks - (processed_adrp_thunks_ - i); |
| 232 | adrp = GetInsn(¤t_method_thunks_, idx * kAdrpThunkSize); |
| 233 | break; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | CHECK_EQ(adrp & 0x9f00001fu, // Check that pc_insn_offset points |
| 238 | 0x90000000 | ((insn >> 5) & 0x1fu)); // to ADRP with matching register. |
| 239 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 240 | uint32_t imm12 = (disp & 0xfffu) >> shift; |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 241 | insn = (insn & ~(0xfffu << 10)) | (imm12 << 10); |
| 242 | SetInsn(code, literal_offset, insn); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | std::vector<uint8_t> Arm64RelativePatcher::CompileThunkCode() { |
| 247 | // The thunk just uses the entry point in the ArtMethod. This works even for calls |
| 248 | // to the generic JNI and interpreter trampolines. |
| 249 | arm64::Arm64Assembler assembler; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 250 | Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset( |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 251 | kArm64PointerSize).Int32Value()); |
| 252 | assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0)); |
| 253 | // Ensure we emit the literal pool. |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 254 | assembler.FinalizeCode(); |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 255 | std::vector<uint8_t> thunk_code(assembler.CodeSize()); |
| 256 | MemoryRegion code(thunk_code.data(), thunk_code.size()); |
| 257 | assembler.FinalizeInstructions(code); |
| 258 | return thunk_code; |
| 259 | } |
| 260 | |
| 261 | uint32_t Arm64RelativePatcher::PatchAdrp(uint32_t adrp, uint32_t disp) { |
| 262 | return (adrp & 0x9f00001fu) | // Clear offset bits, keep ADRP with destination reg. |
| 263 | // Bottom 12 bits are ignored, the next 2 lowest bits are encoded in bits 29-30. |
| 264 | ((disp & 0x00003000u) << (29 - 12)) | |
| 265 | // The next 16 bits are encoded in bits 5-22. |
| 266 | ((disp & 0xffffc000u) >> (12 + 2 - 5)) | |
| 267 | // Since the target_offset is based on the beginning of the oat file and the |
| 268 | // image space precedes the oat file, the target_offset into image space will |
| 269 | // be negative yet passed as uint32_t. Therefore we limit the displacement |
| 270 | // to +-2GiB (rather than the maximim +-4GiB) and determine the sign bit from |
| 271 | // the highest bit of the displacement. This is encoded in bit 23. |
| 272 | ((disp & 0x80000000u) >> (31 - 23)); |
| 273 | } |
| 274 | |
| 275 | bool Arm64RelativePatcher::NeedsErratum843419Thunk(ArrayRef<const uint8_t> code, |
| 276 | uint32_t literal_offset, |
| 277 | uint32_t patch_offset) { |
| 278 | DCHECK_EQ(patch_offset & 0x3u, 0u); |
| 279 | if ((patch_offset & 0xff8) == 0xff8) { // ...ff8 or ...ffc |
| 280 | uint32_t adrp = GetInsn(code, literal_offset); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame^] | 281 | DCHECK_EQ(adrp & 0x9f000000, 0x90000000); |
Matteo Franchin | 97e2f26 | 2015-04-02 15:49:06 +0100 | [diff] [blame] | 282 | uint32_t next_offset = patch_offset + 4u; |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 283 | uint32_t next_insn = GetInsn(code, literal_offset + 4u); |
Matteo Franchin | 97e2f26 | 2015-04-02 15:49:06 +0100 | [diff] [blame] | 284 | |
| 285 | // Below we avoid patching sequences where the adrp is followed by a load which can easily |
| 286 | // be proved to be aligned. |
| 287 | |
| 288 | // First check if the next insn is the LDR using the result of the ADRP. |
| 289 | // LDR <Wt>, [<Xn>, #pimm], where <Xn> == ADRP destination reg. |
| 290 | if ((next_insn & 0xffc00000) == 0xb9400000 && |
| 291 | (((next_insn >> 5) ^ adrp) & 0x1f) == 0) { |
| 292 | return false; |
| 293 | } |
| 294 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame^] | 295 | // And since kLinkerPatchStringRelative is using the result of the ADRP for an ADD immediate, |
| 296 | // check for that as well. We generalize a bit to include ADD/ADDS/SUB/SUBS immediate that |
| 297 | // either uses the ADRP destination or stores the result to a different register. |
| 298 | if ((next_insn & 0x1f000000) == 0x11000000 && |
| 299 | ((((next_insn >> 5) ^ adrp) & 0x1f) == 0 || ((next_insn ^ adrp) & 0x1f) != 0)) { |
| 300 | return false; |
| 301 | } |
| 302 | |
Matteo Franchin | 97e2f26 | 2015-04-02 15:49:06 +0100 | [diff] [blame] | 303 | // LDR <Wt>, <label> is always aligned and thus it doesn't cause boundary crossing. |
| 304 | if ((next_insn & 0xff000000) == 0x18000000) { |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | // LDR <Xt>, <label> is aligned iff the pc + displacement is a multiple of 8. |
| 309 | if ((next_insn & 0xff000000) == 0x58000000) { |
| 310 | bool is_aligned_load = (((next_offset >> 2) ^ (next_insn >> 5)) & 1) == 0; |
| 311 | return !is_aligned_load; |
| 312 | } |
| 313 | |
| 314 | // LDR <Wt>, [SP, #<pimm>] and LDR <Xt>, [SP, #<pimm>] are always aligned loads, as SP is |
| 315 | // guaranteed to be 128-bits aligned and <pimm> is multiple of the load size. |
| 316 | if ((next_insn & 0xbfc003e0) == 0xb94003e0) { |
| 317 | return false; |
| 318 | } |
| 319 | return true; |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 320 | } |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | void Arm64RelativePatcher::SetInsn(std::vector<uint8_t>* code, uint32_t offset, uint32_t value) { |
| 325 | DCHECK_LE(offset + 4u, code->size()); |
| 326 | DCHECK_EQ(offset & 3u, 0u); |
| 327 | uint8_t* addr = &(*code)[offset]; |
| 328 | addr[0] = (value >> 0) & 0xff; |
| 329 | addr[1] = (value >> 8) & 0xff; |
| 330 | addr[2] = (value >> 16) & 0xff; |
| 331 | addr[3] = (value >> 24) & 0xff; |
| 332 | } |
| 333 | |
| 334 | uint32_t Arm64RelativePatcher::GetInsn(ArrayRef<const uint8_t> code, uint32_t offset) { |
| 335 | DCHECK_LE(offset + 4u, code.size()); |
| 336 | DCHECK_EQ(offset & 3u, 0u); |
| 337 | const uint8_t* addr = &code[offset]; |
| 338 | return |
| 339 | (static_cast<uint32_t>(addr[0]) << 0) + |
| 340 | (static_cast<uint32_t>(addr[1]) << 8) + |
| 341 | (static_cast<uint32_t>(addr[2]) << 16)+ |
| 342 | (static_cast<uint32_t>(addr[3]) << 24); |
| 343 | } |
| 344 | |
| 345 | template <typename Alloc> |
| 346 | uint32_t Arm64RelativePatcher::GetInsn(std::vector<uint8_t, Alloc>* code, uint32_t offset) { |
| 347 | return GetInsn(ArrayRef<const uint8_t>(*code), offset); |
| 348 | } |
| 349 | |
| 350 | } // namespace linker |
| 351 | } // namespace art |