| 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/arm/relative_patcher_thumb2.h" |
| 18 | |
| 19 | #include <sstream> |
| 20 | |
| 21 | #include "arch/arm/asm_support_arm.h" |
| 22 | #include "art_method.h" |
| 23 | #include "base/bit_utils.h" |
| 24 | #include "base/malloc_arena_pool.h" |
| 25 | #include "driver/compiled_method.h" |
| 26 | #include "entrypoints/quick/quick_entrypoints_enum.h" |
| 27 | #include "linker/linker_patch.h" |
| 28 | #include "lock_word.h" |
| 29 | #include "mirror/array-inl.h" |
| 30 | #include "mirror/object.h" |
| 31 | #include "read_barrier.h" |
| 32 | |
| 33 | namespace art { |
| 34 | namespace linker { |
| 35 | |
| 36 | // PC displacement from patch location; Thumb2 PC is always at instruction address + 4. |
| 37 | static constexpr int32_t kPcDisplacement = 4; |
| 38 | |
| 39 | // Maximum positive and negative displacement for method call measured from the patch location. |
| 40 | // (Signed 25 bit displacement with the last bit 0 has range [-2^24, 2^24-2] measured from |
| 41 | // the Thumb2 PC pointing right after the BL, i.e. 4 bytes later than the patch location.) |
| 42 | constexpr uint32_t kMaxMethodCallPositiveDisplacement = (1u << 24) - 2 + kPcDisplacement; |
| 43 | constexpr uint32_t kMaxMethodCallNegativeDisplacement = (1u << 24) - kPcDisplacement; |
| 44 | |
| 45 | // Maximum positive and negative displacement for a conditional branch measured from the patch |
| 46 | // location. (Signed 21 bit displacement with the last bit 0 has range [-2^20, 2^20-2] measured |
| 47 | // from the Thumb2 PC pointing right after the B.cond, i.e. 4 bytes later than the patch location.) |
| 48 | constexpr uint32_t kMaxBcondPositiveDisplacement = (1u << 20) - 2u + kPcDisplacement; |
| 49 | constexpr uint32_t kMaxBcondNegativeDisplacement = (1u << 20) - kPcDisplacement; |
| 50 | |
| 51 | Thumb2RelativePatcher::Thumb2RelativePatcher(RelativePatcherThunkProvider* thunk_provider, |
| 52 | RelativePatcherTargetProvider* target_provider) |
| 53 | : ArmBaseRelativePatcher(thunk_provider, target_provider, InstructionSet::kThumb2) { |
| 54 | } |
| 55 | |
| 56 | void Thumb2RelativePatcher::PatchCall(std::vector<uint8_t>* code, |
| 57 | uint32_t literal_offset, |
| 58 | uint32_t patch_offset, |
| 59 | uint32_t target_offset) { |
| 60 | DCHECK_ALIGNED(patch_offset, 2u); |
| 61 | DCHECK_EQ(target_offset & 1u, 1u); // Thumb2 mode bit. |
| 62 | uint32_t displacement = CalculateMethodCallDisplacement(patch_offset, target_offset & ~1u); |
| 63 | PatchBl(code, literal_offset, displacement); |
| 64 | } |
| 65 | |
| 66 | void Thumb2RelativePatcher::PatchPcRelativeReference(std::vector<uint8_t>* code, |
| 67 | const LinkerPatch& patch, |
| 68 | uint32_t patch_offset, |
| 69 | uint32_t target_offset) { |
| 70 | uint32_t literal_offset = patch.LiteralOffset(); |
| 71 | uint32_t pc_literal_offset = patch.PcInsnOffset(); |
| 72 | uint32_t pc_base = patch_offset + (pc_literal_offset - literal_offset) + 4u /* PC adjustment */; |
| 73 | uint32_t diff = target_offset - pc_base; |
| 74 | |
| 75 | uint32_t insn = GetInsn32(code, literal_offset); |
| 76 | DCHECK_EQ(insn & 0xff7ff0ffu, 0xf2400000u); // MOVW/MOVT, unpatched (imm16 == 0). |
| 77 | uint32_t diff16 = ((insn & 0x00800000u) != 0u) ? (diff >> 16) : (diff & 0xffffu); |
| 78 | uint32_t imm4 = (diff16 >> 12) & 0xfu; |
| 79 | uint32_t imm = (diff16 >> 11) & 0x1u; |
| 80 | uint32_t imm3 = (diff16 >> 8) & 0x7u; |
| 81 | uint32_t imm8 = diff16 & 0xffu; |
| 82 | insn = (insn & 0xfbf08f00u) | (imm << 26) | (imm4 << 16) | (imm3 << 12) | imm8; |
| 83 | SetInsn32(code, literal_offset, insn); |
| 84 | } |
| 85 | |
| 86 | void Thumb2RelativePatcher::PatchEntrypointCall(std::vector<uint8_t>* code, |
| 87 | const LinkerPatch& patch, |
| 88 | uint32_t patch_offset) { |
| 89 | DCHECK_ALIGNED(patch_offset, 2u); |
| 90 | ThunkKey key = GetEntrypointCallKey(patch); |
| 91 | uint32_t target_offset = GetThunkTargetOffset(key, patch_offset); |
| 92 | DCHECK_ALIGNED(target_offset, 4u); |
| 93 | uint32_t displacement = target_offset - patch_offset; |
| 94 | PatchBl(code, patch.LiteralOffset(), displacement); |
| 95 | } |
| 96 | |
| 97 | void Thumb2RelativePatcher::PatchBakerReadBarrierBranch(std::vector<uint8_t>* code, |
| 98 | const LinkerPatch& patch, |
| 99 | uint32_t patch_offset) { |
| 100 | DCHECK_ALIGNED(patch_offset, 2u); |
| 101 | uint32_t literal_offset = patch.LiteralOffset(); |
| 102 | DCHECK_ALIGNED(literal_offset, 2u); |
| 103 | DCHECK_LT(literal_offset, code->size()); |
| 104 | uint32_t insn = GetInsn32(code, literal_offset); |
| 105 | DCHECK_EQ(insn, 0xf0408000); // BNE +0 (unpatched) |
| 106 | ThunkKey key = GetBakerThunkKey(patch); |
| 107 | uint32_t target_offset = GetThunkTargetOffset(key, patch_offset); |
| 108 | DCHECK_ALIGNED(target_offset, 4u); |
| 109 | uint32_t disp = target_offset - (patch_offset + kPcDisplacement); |
| 110 | DCHECK((disp >> 20) == 0u || (disp >> 20) == 0xfffu); // 21-bit signed. |
| 111 | insn |= ((disp << (26 - 20)) & 0x04000000u) | // Shift bit 20 to 26, "S". |
| 112 | ((disp >> (19 - 11)) & 0x00000800u) | // Shift bit 19 to 13, "J1". |
| 113 | ((disp >> (18 - 13)) & 0x00002000u) | // Shift bit 18 to 11, "J2". |
| 114 | ((disp << (16 - 12)) & 0x003f0000u) | // Shift bits 12-17 to 16-25, "imm6". |
| 115 | ((disp >> (1 - 0)) & 0x000007ffu); // Shift bits 1-12 to 0-11, "imm11". |
| 116 | SetInsn32(code, literal_offset, insn); |
| 117 | } |
| 118 | |
| 119 | uint32_t Thumb2RelativePatcher::MaxPositiveDisplacement(const ThunkKey& key) { |
| 120 | switch (key.GetType()) { |
| 121 | case ThunkType::kMethodCall: |
| 122 | case ThunkType::kEntrypointCall: |
| 123 | return kMaxMethodCallPositiveDisplacement; |
| 124 | case ThunkType::kBakerReadBarrier: |
| 125 | return kMaxBcondPositiveDisplacement; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | uint32_t Thumb2RelativePatcher::MaxNegativeDisplacement(const ThunkKey& key) { |
| 130 | switch (key.GetType()) { |
| 131 | case ThunkType::kMethodCall: |
| 132 | case ThunkType::kEntrypointCall: |
| 133 | return kMaxMethodCallNegativeDisplacement; |
| 134 | case ThunkType::kBakerReadBarrier: |
| 135 | return kMaxBcondNegativeDisplacement; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void Thumb2RelativePatcher::PatchBl(std::vector<uint8_t>* code, |
| 140 | uint32_t literal_offset, |
| 141 | uint32_t displacement) { |
| 142 | displacement -= kPcDisplacement; // The base PC is at the end of the 4-byte patch. |
| 143 | DCHECK_EQ(displacement & 1u, 0u); |
| 144 | DCHECK((displacement >> 24) == 0u || (displacement >> 24) == 255u); // 25-bit signed. |
| 145 | uint32_t signbit = (displacement >> 31) & 0x1; |
| 146 | uint32_t i1 = (displacement >> 23) & 0x1; |
| 147 | uint32_t i2 = (displacement >> 22) & 0x1; |
| 148 | uint32_t imm10 = (displacement >> 12) & 0x03ff; |
| 149 | uint32_t imm11 = (displacement >> 1) & 0x07ff; |
| 150 | uint32_t j1 = i1 ^ (signbit ^ 1); |
| 151 | uint32_t j2 = i2 ^ (signbit ^ 1); |
| 152 | uint32_t value = (signbit << 26) | (j1 << 13) | (j2 << 11) | (imm10 << 16) | imm11; |
| 153 | value |= 0xf000d000; // BL |
| 154 | |
| 155 | // Check that we're just overwriting an existing BL. |
| 156 | DCHECK_EQ(GetInsn32(code, literal_offset) & 0xf800d000, 0xf000d000); |
| 157 | // Write the new BL. |
| 158 | SetInsn32(code, literal_offset, value); |
| 159 | } |
| 160 | |
| 161 | void Thumb2RelativePatcher::SetInsn32(std::vector<uint8_t>* code, uint32_t offset, uint32_t value) { |
| 162 | DCHECK_LE(offset + 4u, code->size()); |
| 163 | DCHECK_ALIGNED(offset, 2u); |
| 164 | uint8_t* addr = &(*code)[offset]; |
| 165 | addr[0] = (value >> 16) & 0xff; |
| 166 | addr[1] = (value >> 24) & 0xff; |
| 167 | addr[2] = (value >> 0) & 0xff; |
| 168 | addr[3] = (value >> 8) & 0xff; |
| 169 | } |
| 170 | |
| 171 | uint32_t Thumb2RelativePatcher::GetInsn32(ArrayRef<const uint8_t> code, uint32_t offset) { |
| 172 | DCHECK_LE(offset + 4u, code.size()); |
| 173 | DCHECK_ALIGNED(offset, 2u); |
| 174 | const uint8_t* addr = &code[offset]; |
| 175 | return |
| 176 | (static_cast<uint32_t>(addr[0]) << 16) + |
| 177 | (static_cast<uint32_t>(addr[1]) << 24) + |
| 178 | (static_cast<uint32_t>(addr[2]) << 0)+ |
| 179 | (static_cast<uint32_t>(addr[3]) << 8); |
| 180 | } |
| 181 | |
| 182 | template <typename Vector> |
| 183 | uint32_t Thumb2RelativePatcher::GetInsn32(Vector* code, uint32_t offset) { |
| 184 | static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type"); |
| 185 | return GetInsn32(ArrayRef<const uint8_t>(*code), offset); |
| 186 | } |
| 187 | |
| 188 | uint32_t Thumb2RelativePatcher::GetInsn16(ArrayRef<const uint8_t> code, uint32_t offset) { |
| 189 | DCHECK_LE(offset + 2u, code.size()); |
| 190 | DCHECK_ALIGNED(offset, 2u); |
| 191 | const uint8_t* addr = &code[offset]; |
| 192 | return (static_cast<uint32_t>(addr[0]) << 0) + (static_cast<uint32_t>(addr[1]) << 8); |
| 193 | } |
| 194 | |
| 195 | template <typename Vector> |
| 196 | uint32_t Thumb2RelativePatcher::GetInsn16(Vector* code, uint32_t offset) { |
| 197 | static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type"); |
| 198 | return GetInsn16(ArrayRef<const uint8_t>(*code), offset); |
| 199 | } |
| 200 | |
| 201 | } // namespace linker |
| 202 | } // namespace art |