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 "art_method.h" 20 #include "compiled_method.h" 21 #include "utils/arm/assembler_thumb2.h" 22 23 namespace art { 24 namespace linker { 25 26 Thumb2RelativePatcher::Thumb2RelativePatcher(RelativePatcherTargetProvider* provider) 27 : ArmBaseRelativePatcher(provider, kThumb2, CompileThunkCode(), 28 kMaxPositiveDisplacement, kMaxNegativeDisplacement) { 29 } 30 31 void Thumb2RelativePatcher::PatchCall(std::vector<uint8_t>* code, 32 uint32_t literal_offset, 33 uint32_t patch_offset, 34 uint32_t target_offset) { 35 DCHECK_LE(literal_offset + 4u, code->size()); 36 DCHECK_EQ(literal_offset & 1u, 0u); 37 DCHECK_EQ(patch_offset & 1u, 0u); 38 DCHECK_EQ(target_offset & 1u, 1u); // Thumb2 mode bit. 39 uint32_t displacement = CalculateDisplacement(patch_offset, target_offset & ~1u); 40 displacement -= kPcDisplacement; // The base PC is at the end of the 4-byte patch. 41 DCHECK_EQ(displacement & 1u, 0u); 42 DCHECK((displacement >> 24) == 0u || (displacement >> 24) == 255u); // 25-bit signed. 43 uint32_t signbit = (displacement >> 31) & 0x1; 44 uint32_t i1 = (displacement >> 23) & 0x1; 45 uint32_t i2 = (displacement >> 22) & 0x1; 46 uint32_t imm10 = (displacement >> 12) & 0x03ff; 47 uint32_t imm11 = (displacement >> 1) & 0x07ff; 48 uint32_t j1 = i1 ^ (signbit ^ 1); 49 uint32_t j2 = i2 ^ (signbit ^ 1); 50 uint32_t value = (signbit << 26) | (j1 << 13) | (j2 << 11) | (imm10 << 16) | imm11; 51 value |= 0xf000d000; // BL 52 53 // Check that we're just overwriting an existing BL. 54 DCHECK_EQ(GetInsn32(code, literal_offset) & 0xf800d000, 0xf000d000); 55 // Write the new BL. 56 SetInsn32(code, literal_offset, value); 57 } 58 59 void Thumb2RelativePatcher::PatchPcRelativeReference(std::vector<uint8_t>* code, 60 const LinkerPatch& patch, 61 uint32_t patch_offset, 62 uint32_t target_offset) { 63 uint32_t literal_offset = patch.LiteralOffset(); 64 uint32_t pc_literal_offset = patch.PcInsnOffset(); 65 uint32_t pc_base = patch_offset + (pc_literal_offset - literal_offset) + 4u /* PC adjustment */; 66 uint32_t diff = target_offset - pc_base; 67 68 uint32_t insn = GetInsn32(code, literal_offset); 69 DCHECK_EQ(insn & 0xff7ff0ffu, 0xf2400000u); // MOVW/MOVT, unpatched (imm16 == 0). 70 uint32_t diff16 = ((insn & 0x00800000u) != 0u) ? (diff >> 16) : (diff & 0xffffu); 71 uint32_t imm4 = (diff16 >> 12) & 0xfu; 72 uint32_t imm = (diff16 >> 11) & 0x1u; 73 uint32_t imm3 = (diff16 >> 8) & 0x7u; 74 uint32_t imm8 = diff16 & 0xffu; 75 insn = (insn & 0xfbf08f00u) | (imm << 26) | (imm4 << 16) | (imm3 << 12) | imm8; 76 SetInsn32(code, literal_offset, insn); 77 } 78 79 std::vector<uint8_t> Thumb2RelativePatcher::CompileThunkCode() { 80 // The thunk just uses the entry point in the ArtMethod. This works even for calls 81 // to the generic JNI and interpreter trampolines. 82 ArenaPool pool; 83 ArenaAllocator arena(&pool); 84 arm::Thumb2Assembler assembler(&arena); 85 assembler.LoadFromOffset( 86 arm::kLoadWord, arm::PC, arm::R0, 87 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value()); 88 assembler.bkpt(0); 89 assembler.FinalizeCode(); 90 std::vector<uint8_t> thunk_code(assembler.CodeSize()); 91 MemoryRegion code(thunk_code.data(), thunk_code.size()); 92 assembler.FinalizeInstructions(code); 93 return thunk_code; 94 } 95 96 void Thumb2RelativePatcher::SetInsn32(std::vector<uint8_t>* code, uint32_t offset, uint32_t value) { 97 DCHECK_LE(offset + 4u, code->size()); 98 DCHECK_EQ(offset & 1u, 0u); 99 uint8_t* addr = &(*code)[offset]; 100 addr[0] = (value >> 16) & 0xff; 101 addr[1] = (value >> 24) & 0xff; 102 addr[2] = (value >> 0) & 0xff; 103 addr[3] = (value >> 8) & 0xff; 104 } 105 106 uint32_t Thumb2RelativePatcher::GetInsn32(ArrayRef<const uint8_t> code, uint32_t offset) { 107 DCHECK_LE(offset + 4u, code.size()); 108 DCHECK_EQ(offset & 1u, 0u); 109 const uint8_t* addr = &code[offset]; 110 return 111 (static_cast<uint32_t>(addr[0]) << 16) + 112 (static_cast<uint32_t>(addr[1]) << 24) + 113 (static_cast<uint32_t>(addr[2]) << 0)+ 114 (static_cast<uint32_t>(addr[3]) << 8); 115 } 116 117 template <typename Vector> 118 uint32_t Thumb2RelativePatcher::GetInsn32(Vector* code, uint32_t offset) { 119 static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type"); 120 return GetInsn32(ArrayRef<const uint8_t>(*code), offset); 121 } 122 123 } // namespace linker 124 } // namespace art 125