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