blob: 5f4f760c148c40956d74445e1561ef3cccff3337 [file] [log] [blame]
Vladimir Markob163bb72015-03-31 21:49:49 +01001/*
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
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Vladimir Markob163bb72015-03-31 21:49:49 +010020#include "compiled_method.h"
Vladimir Markob163bb72015-03-31 21:49:49 +010021#include "utils/arm/assembler_thumb2.h"
22
23namespace art {
24namespace linker {
25
26Thumb2RelativePatcher::Thumb2RelativePatcher(RelativePatcherTargetProvider* provider)
27 : ArmBaseRelativePatcher(provider, kThumb2, CompileThunkCode(),
28 kMaxPositiveDisplacement, kMaxNegativeDisplacement) {
29}
30
31void 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
Vladimir Markob163bb72015-03-31 21:49:49 +010051 // Check that we're just overwriting an existing BL.
Vladimir Markoe5c76c52015-04-06 12:10:19 +010052 DCHECK_EQ(GetInsn32(code, literal_offset) & 0xf800d000, 0xf000d000);
Vladimir Markob163bb72015-03-31 21:49:49 +010053 // Write the new BL.
Vladimir Markoe5c76c52015-04-06 12:10:19 +010054 SetInsn32(code, literal_offset, value);
Vladimir Markob163bb72015-03-31 21:49:49 +010055}
56
Vladimir Markoe5c76c52015-04-06 12:10:19 +010057void Thumb2RelativePatcher::PatchDexCacheReference(std::vector<uint8_t>* code,
58 const LinkerPatch& patch,
59 uint32_t patch_offset,
60 uint32_t target_offset) {
61 uint32_t literal_offset = patch.LiteralOffset();
62 uint32_t pc_literal_offset = patch.PcInsnOffset();
63 uint32_t pc_base = patch_offset + (pc_literal_offset - literal_offset) + 4u /* PC adjustment */;
64 uint32_t diff = target_offset - pc_base;
65
66 uint32_t insn = GetInsn32(code, literal_offset);
67 DCHECK_EQ(insn & 0xff7ff0ffu, 0xf2400000u); // MOVW/MOVT, unpatched (imm16 == 0).
68 uint32_t diff16 = ((insn & 0x00800000u) != 0u) ? (diff >> 16) : (diff & 0xffffu);
69 uint32_t imm4 = (diff16 >> 12) & 0xfu;
70 uint32_t imm = (diff16 >> 11) & 0x1u;
71 uint32_t imm3 = (diff16 >> 8) & 0x7u;
72 uint32_t imm8 = diff16 & 0xffu;
73 insn = (insn & 0xfbf08f00u) | (imm << 26) | (imm4 << 16) | (imm3 << 12) | imm8;
74 SetInsn32(code, literal_offset, insn);
Vladimir Markob163bb72015-03-31 21:49:49 +010075}
76
77std::vector<uint8_t> Thumb2RelativePatcher::CompileThunkCode() {
78 // The thunk just uses the entry point in the ArtMethod. This works even for calls
79 // to the generic JNI and interpreter trampolines.
80 arm::Thumb2Assembler assembler;
81 assembler.LoadFromOffset(
82 arm::kLoadWord, arm::PC, arm::R0,
Mathieu Chartiere401d142015-04-22 13:56:20 -070083 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Markob163bb72015-03-31 21:49:49 +010084 assembler.bkpt(0);
Vladimir Markocf93a5c2015-06-16 11:33:24 +000085 assembler.FinalizeCode();
Vladimir Markob163bb72015-03-31 21:49:49 +010086 std::vector<uint8_t> thunk_code(assembler.CodeSize());
87 MemoryRegion code(thunk_code.data(), thunk_code.size());
88 assembler.FinalizeInstructions(code);
89 return thunk_code;
90}
91
Vladimir Markoe5c76c52015-04-06 12:10:19 +010092void Thumb2RelativePatcher::SetInsn32(std::vector<uint8_t>* code, uint32_t offset, uint32_t value) {
93 DCHECK_LE(offset + 4u, code->size());
94 DCHECK_EQ(offset & 1u, 0u);
95 uint8_t* addr = &(*code)[offset];
96 addr[0] = (value >> 16) & 0xff;
97 addr[1] = (value >> 24) & 0xff;
98 addr[2] = (value >> 0) & 0xff;
99 addr[3] = (value >> 8) & 0xff;
100}
101
102uint32_t Thumb2RelativePatcher::GetInsn32(ArrayRef<const uint8_t> code, uint32_t offset) {
103 DCHECK_LE(offset + 4u, code.size());
104 DCHECK_EQ(offset & 1u, 0u);
105 const uint8_t* addr = &code[offset];
106 return
107 (static_cast<uint32_t>(addr[0]) << 16) +
108 (static_cast<uint32_t>(addr[1]) << 24) +
109 (static_cast<uint32_t>(addr[2]) << 0)+
110 (static_cast<uint32_t>(addr[3]) << 8);
111}
112
Vladimir Markoec7802a2015-10-01 20:57:57 +0100113template <typename Vector>
114uint32_t Thumb2RelativePatcher::GetInsn32(Vector* code, uint32_t offset) {
115 static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type");
Vladimir Markoe5c76c52015-04-06 12:10:19 +0100116 return GetInsn32(ArrayRef<const uint8_t>(*code), offset);
117}
118
Vladimir Markob163bb72015-03-31 21:49:49 +0100119} // namespace linker
120} // namespace art