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/arm/relative_patcher_thumb2.h" |
| 18 | |
| 19 | #include "compiled_method.h" |
| 20 | #include "mirror/art_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, uint32_t literal_offset, |
| 32 | uint32_t patch_offset, uint32_t target_offset) { |
| 33 | DCHECK_LE(literal_offset + 4u, code->size()); |
| 34 | DCHECK_EQ(literal_offset & 1u, 0u); |
| 35 | DCHECK_EQ(patch_offset & 1u, 0u); |
| 36 | DCHECK_EQ(target_offset & 1u, 1u); // Thumb2 mode bit. |
| 37 | uint32_t displacement = CalculateDisplacement(patch_offset, target_offset & ~1u); |
| 38 | displacement -= kPcDisplacement; // The base PC is at the end of the 4-byte patch. |
| 39 | DCHECK_EQ(displacement & 1u, 0u); |
| 40 | DCHECK((displacement >> 24) == 0u || (displacement >> 24) == 255u); // 25-bit signed. |
| 41 | uint32_t signbit = (displacement >> 31) & 0x1; |
| 42 | uint32_t i1 = (displacement >> 23) & 0x1; |
| 43 | uint32_t i2 = (displacement >> 22) & 0x1; |
| 44 | uint32_t imm10 = (displacement >> 12) & 0x03ff; |
| 45 | uint32_t imm11 = (displacement >> 1) & 0x07ff; |
| 46 | uint32_t j1 = i1 ^ (signbit ^ 1); |
| 47 | uint32_t j2 = i2 ^ (signbit ^ 1); |
| 48 | uint32_t value = (signbit << 26) | (j1 << 13) | (j2 << 11) | (imm10 << 16) | imm11; |
| 49 | value |= 0xf000d000; // BL |
| 50 | |
| 51 | uint8_t* addr = &(*code)[literal_offset]; |
| 52 | // Check that we're just overwriting an existing BL. |
| 53 | DCHECK_EQ(addr[1] & 0xf8, 0xf0); |
| 54 | DCHECK_EQ(addr[3] & 0xd0, 0xd0); |
| 55 | // Write the new BL. |
| 56 | addr[0] = (value >> 16) & 0xff; |
| 57 | addr[1] = (value >> 24) & 0xff; |
| 58 | addr[2] = (value >> 0) & 0xff; |
| 59 | addr[3] = (value >> 8) & 0xff; |
| 60 | } |
| 61 | |
| 62 | void Thumb2RelativePatcher::PatchDexCacheReference(std::vector<uint8_t>* code ATTRIBUTE_UNUSED, |
| 63 | const LinkerPatch& patch ATTRIBUTE_UNUSED, |
| 64 | uint32_t patch_offset ATTRIBUTE_UNUSED, |
| 65 | uint32_t target_offset ATTRIBUTE_UNUSED) { |
| 66 | LOG(FATAL) << "Unexpected relative dex cache array patch."; |
| 67 | } |
| 68 | |
| 69 | std::vector<uint8_t> Thumb2RelativePatcher::CompileThunkCode() { |
| 70 | // The thunk just uses the entry point in the ArtMethod. This works even for calls |
| 71 | // to the generic JNI and interpreter trampolines. |
| 72 | arm::Thumb2Assembler assembler; |
| 73 | assembler.LoadFromOffset( |
| 74 | arm::kLoadWord, arm::PC, arm::R0, |
| 75 | mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value()); |
| 76 | assembler.bkpt(0); |
| 77 | std::vector<uint8_t> thunk_code(assembler.CodeSize()); |
| 78 | MemoryRegion code(thunk_code.data(), thunk_code.size()); |
| 79 | assembler.FinalizeInstructions(code); |
| 80 | return thunk_code; |
| 81 | } |
| 82 | |
| 83 | } // namespace linker |
| 84 | } // namespace art |