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 
Thumb2RelativePatcher(RelativePatcherTargetProvider * provider)26 Thumb2RelativePatcher::Thumb2RelativePatcher(RelativePatcherTargetProvider* provider)
27     : ArmBaseRelativePatcher(provider, kThumb2, CompileThunkCode(),
28                              kMaxPositiveDisplacement, kMaxNegativeDisplacement) {
29 }
30 
PatchCall(std::vector<uint8_t> * code,uint32_t literal_offset,uint32_t patch_offset,uint32_t target_offset)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   // Check that we're just overwriting an existing BL.
52   DCHECK_EQ(GetInsn32(code, literal_offset) & 0xf800d000, 0xf000d000);
53   // Write the new BL.
54   SetInsn32(code, literal_offset, value);
55 }
56 
PatchDexCacheReference(std::vector<uint8_t> * code,const LinkerPatch & patch,uint32_t patch_offset,uint32_t target_offset)57 void 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);
75 }
76 
CompileThunkCode()77 std::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,
83       ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
84   assembler.bkpt(0);
85   std::vector<uint8_t> thunk_code(assembler.CodeSize());
86   MemoryRegion code(thunk_code.data(), thunk_code.size());
87   assembler.FinalizeInstructions(code);
88   return thunk_code;
89 }
90 
SetInsn32(std::vector<uint8_t> * code,uint32_t offset,uint32_t value)91 void Thumb2RelativePatcher::SetInsn32(std::vector<uint8_t>* code, uint32_t offset, uint32_t value) {
92   DCHECK_LE(offset + 4u, code->size());
93   DCHECK_EQ(offset & 1u, 0u);
94   uint8_t* addr = &(*code)[offset];
95   addr[0] = (value >> 16) & 0xff;
96   addr[1] = (value >> 24) & 0xff;
97   addr[2] = (value >> 0) & 0xff;
98   addr[3] = (value >> 8) & 0xff;
99 }
100 
GetInsn32(ArrayRef<const uint8_t> code,uint32_t offset)101 uint32_t Thumb2RelativePatcher::GetInsn32(ArrayRef<const uint8_t> code, uint32_t offset) {
102   DCHECK_LE(offset + 4u, code.size());
103   DCHECK_EQ(offset & 1u, 0u);
104   const uint8_t* addr = &code[offset];
105   return
106       (static_cast<uint32_t>(addr[0]) << 16) +
107       (static_cast<uint32_t>(addr[1]) << 24) +
108       (static_cast<uint32_t>(addr[2]) << 0)+
109       (static_cast<uint32_t>(addr[3]) << 8);
110 }
111 
112 template <typename Alloc>
GetInsn32(std::vector<uint8_t,Alloc> * code,uint32_t offset)113 uint32_t Thumb2RelativePatcher::GetInsn32(std::vector<uint8_t, Alloc>* code, uint32_t offset) {
114   return GetInsn32(ArrayRef<const uint8_t>(*code), offset);
115 }
116 
117 }  // namespace linker
118 }  // namespace art
119