1 /*
2 * Copyright (C) 2016 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/mips/relative_patcher_mips.h"
18
19 #include "compiled_method.h"
20
21 namespace art {
22 namespace linker {
23
ReserveSpace(uint32_t offset,const CompiledMethod * compiled_method ATTRIBUTE_UNUSED,MethodReference method_ref ATTRIBUTE_UNUSED)24 uint32_t MipsRelativePatcher::ReserveSpace(
25 uint32_t offset,
26 const CompiledMethod* compiled_method ATTRIBUTE_UNUSED,
27 MethodReference method_ref ATTRIBUTE_UNUSED) {
28 return offset; // No space reserved; no limit on relative call distance.
29 }
30
ReserveSpaceEnd(uint32_t offset)31 uint32_t MipsRelativePatcher::ReserveSpaceEnd(uint32_t offset) {
32 return offset; // No space reserved; no limit on relative call distance.
33 }
34
WriteThunks(OutputStream * out ATTRIBUTE_UNUSED,uint32_t offset)35 uint32_t MipsRelativePatcher::WriteThunks(OutputStream* out ATTRIBUTE_UNUSED, uint32_t offset) {
36 return offset; // No thunks added; no limit on relative call distance.
37 }
38
PatchCall(std::vector<uint8_t> * code ATTRIBUTE_UNUSED,uint32_t literal_offset ATTRIBUTE_UNUSED,uint32_t patch_offset ATTRIBUTE_UNUSED,uint32_t target_offset ATTRIBUTE_UNUSED)39 void MipsRelativePatcher::PatchCall(std::vector<uint8_t>* code ATTRIBUTE_UNUSED,
40 uint32_t literal_offset ATTRIBUTE_UNUSED,
41 uint32_t patch_offset ATTRIBUTE_UNUSED,
42 uint32_t target_offset ATTRIBUTE_UNUSED) {
43 UNIMPLEMENTED(FATAL) << "PatchCall unimplemented on MIPS";
44 }
45
PatchPcRelativeReference(std::vector<uint8_t> * code,const LinkerPatch & patch,uint32_t patch_offset,uint32_t target_offset)46 void MipsRelativePatcher::PatchPcRelativeReference(std::vector<uint8_t>* code,
47 const LinkerPatch& patch,
48 uint32_t patch_offset,
49 uint32_t target_offset) {
50 uint32_t anchor_literal_offset = patch.PcInsnOffset();
51 uint32_t literal_offset = patch.LiteralOffset();
52 uint32_t literal_low_offset;
53 bool dex_cache_array = (patch.GetType() == LinkerPatch::Type::kDexCacheArray);
54
55 // Perform basic sanity checks and initialize `literal_low_offset` to point
56 // to the instruction containing the 16 least significant bits of the
57 // relative address.
58 if (is_r6) {
59 DCHECK_GE(code->size(), 8u);
60 DCHECK_LE(literal_offset, code->size() - 8u);
61 DCHECK_EQ(literal_offset, anchor_literal_offset);
62 // AUIPC reg, offset_high
63 DCHECK_EQ((*code)[literal_offset + 0], 0x34);
64 DCHECK_EQ((*code)[literal_offset + 1], 0x12);
65 DCHECK_EQ(((*code)[literal_offset + 2] & 0x1F), 0x1E);
66 DCHECK_EQ(((*code)[literal_offset + 3] & 0xFC), 0xEC);
67 // instr reg(s), offset_low
68 DCHECK_EQ((*code)[literal_offset + 4], 0x78);
69 DCHECK_EQ((*code)[literal_offset + 5], 0x56);
70 literal_low_offset = literal_offset + 4;
71 } else {
72 DCHECK_GE(code->size(), 16u);
73 DCHECK_LE(literal_offset, code->size() - 12u);
74 DCHECK_GE(literal_offset, 4u);
75 // The NAL instruction may not precede immediately as the PC+0 value may
76 // come from HMipsComputeBaseMethodAddress.
77 if (dex_cache_array) {
78 DCHECK_EQ(literal_offset + 4u, anchor_literal_offset);
79 // NAL
80 DCHECK_EQ((*code)[literal_offset - 4], 0x00);
81 DCHECK_EQ((*code)[literal_offset - 3], 0x00);
82 DCHECK_EQ((*code)[literal_offset - 2], 0x10);
83 DCHECK_EQ((*code)[literal_offset - 1], 0x04);
84 }
85 // LUI reg, offset_high
86 DCHECK_EQ((*code)[literal_offset + 0], 0x34);
87 DCHECK_EQ((*code)[literal_offset + 1], 0x12);
88 DCHECK_EQ(((*code)[literal_offset + 2] & 0xE0), 0x00);
89 DCHECK_EQ((*code)[literal_offset + 3], 0x3C);
90 // ADDU reg, reg, reg2
91 DCHECK_EQ((*code)[literal_offset + 4], 0x21);
92 DCHECK_EQ(((*code)[literal_offset + 5] & 0x07), 0x00);
93 if (dex_cache_array) {
94 // reg2 is either RA or from HMipsComputeBaseMethodAddress.
95 DCHECK_EQ(((*code)[literal_offset + 6] & 0x1F), 0x1F);
96 }
97 DCHECK_EQ(((*code)[literal_offset + 7] & 0xFC), 0x00);
98 // instr reg(s), offset_low
99 DCHECK_EQ((*code)[literal_offset + 8], 0x78);
100 DCHECK_EQ((*code)[literal_offset + 9], 0x56);
101 literal_low_offset = literal_offset + 8;
102 }
103
104 // Apply patch.
105 uint32_t anchor_offset = patch_offset - literal_offset + anchor_literal_offset;
106 uint32_t diff = target_offset - anchor_offset;
107 if (dex_cache_array && !is_r6) {
108 diff += kDexCacheArrayLwOffset;
109 }
110 diff += (diff & 0x8000) << 1; // Account for sign extension in "instr reg(s), offset_low".
111
112 // LUI reg, offset_high / AUIPC reg, offset_high
113 (*code)[literal_offset + 0] = static_cast<uint8_t>(diff >> 16);
114 (*code)[literal_offset + 1] = static_cast<uint8_t>(diff >> 24);
115 // instr reg(s), offset_low
116 (*code)[literal_low_offset + 0] = static_cast<uint8_t>(diff >> 0);
117 (*code)[literal_low_offset + 1] = static_cast<uint8_t>(diff >> 8);
118 }
119
PatchBakerReadBarrierBranch(std::vector<uint8_t> * code ATTRIBUTE_UNUSED,const LinkerPatch & patch ATTRIBUTE_UNUSED,uint32_t patch_offset ATTRIBUTE_UNUSED)120 void MipsRelativePatcher::PatchBakerReadBarrierBranch(std::vector<uint8_t>* code ATTRIBUTE_UNUSED,
121 const LinkerPatch& patch ATTRIBUTE_UNUSED,
122 uint32_t patch_offset ATTRIBUTE_UNUSED) {
123 LOG(FATAL) << "UNIMPLEMENTED";
124 }
125
126 } // namespace linker
127 } // namespace art
128