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 #include "debug/method_debug_info.h"
21 #include "linker/linker_patch.h"
22
23 namespace art {
24 namespace linker {
25
ReserveSpace(uint32_t offset,const CompiledMethod * compiled_method ATTRIBUTE_UNUSED,MethodReference method_ref ATTRIBUTE_UNUSED)26 uint32_t MipsRelativePatcher::ReserveSpace(
27 uint32_t offset,
28 const CompiledMethod* compiled_method ATTRIBUTE_UNUSED,
29 MethodReference method_ref ATTRIBUTE_UNUSED) {
30 return offset; // No space reserved; no limit on relative call distance.
31 }
32
ReserveSpaceEnd(uint32_t offset)33 uint32_t MipsRelativePatcher::ReserveSpaceEnd(uint32_t offset) {
34 return offset; // No space reserved; no limit on relative call distance.
35 }
36
WriteThunks(OutputStream * out ATTRIBUTE_UNUSED,uint32_t offset)37 uint32_t MipsRelativePatcher::WriteThunks(OutputStream* out ATTRIBUTE_UNUSED, uint32_t offset) {
38 return offset; // No thunks added; no limit on relative call distance.
39 }
40
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)41 void MipsRelativePatcher::PatchCall(std::vector<uint8_t>* code ATTRIBUTE_UNUSED,
42 uint32_t literal_offset ATTRIBUTE_UNUSED,
43 uint32_t patch_offset ATTRIBUTE_UNUSED,
44 uint32_t target_offset ATTRIBUTE_UNUSED) {
45 UNIMPLEMENTED(FATAL) << "PatchCall unimplemented on MIPS";
46 }
47
PatchPcRelativeReference(std::vector<uint8_t> * code,const LinkerPatch & patch,uint32_t patch_offset,uint32_t target_offset)48 void MipsRelativePatcher::PatchPcRelativeReference(std::vector<uint8_t>* code,
49 const LinkerPatch& patch,
50 uint32_t patch_offset,
51 uint32_t target_offset) {
52 uint32_t anchor_literal_offset = patch.PcInsnOffset();
53 uint32_t literal_offset = patch.LiteralOffset();
54 bool high_patch = ((*code)[literal_offset + 0] == 0x34) && ((*code)[literal_offset + 1] == 0x12);
55
56 // Perform basic sanity checks.
57 if (high_patch) {
58 if (is_r6) {
59 // auipc reg, offset_high
60 DCHECK_EQ(((*code)[literal_offset + 2] & 0x1F), 0x1E);
61 DCHECK_EQ(((*code)[literal_offset + 3] & 0xFC), 0xEC);
62 } else {
63 // lui reg, offset_high
64 DCHECK_EQ(((*code)[literal_offset + 2] & 0xE0), 0x00);
65 DCHECK_EQ((*code)[literal_offset + 3], 0x3C);
66 }
67 } else {
68 // instr reg(s), offset_low
69 CHECK_EQ((*code)[literal_offset + 0], 0x78);
70 CHECK_EQ((*code)[literal_offset + 1], 0x56);
71 }
72
73 // Apply patch.
74 uint32_t anchor_offset = patch_offset - literal_offset + anchor_literal_offset;
75 uint32_t diff = target_offset - anchor_offset;
76 diff += (diff & 0x8000) << 1; // Account for sign extension in "instr reg(s), offset_low".
77
78 if (high_patch) {
79 // lui reg, offset_high / auipc reg, offset_high
80 (*code)[literal_offset + 0] = static_cast<uint8_t>(diff >> 16);
81 (*code)[literal_offset + 1] = static_cast<uint8_t>(diff >> 24);
82 } else {
83 // instr reg(s), offset_low
84 (*code)[literal_offset + 0] = static_cast<uint8_t>(diff >> 0);
85 (*code)[literal_offset + 1] = static_cast<uint8_t>(diff >> 8);
86 }
87 }
88
PatchBakerReadBarrierBranch(std::vector<uint8_t> * code ATTRIBUTE_UNUSED,const LinkerPatch & patch ATTRIBUTE_UNUSED,uint32_t patch_offset ATTRIBUTE_UNUSED)89 void MipsRelativePatcher::PatchBakerReadBarrierBranch(std::vector<uint8_t>* code ATTRIBUTE_UNUSED,
90 const LinkerPatch& patch ATTRIBUTE_UNUSED,
91 uint32_t patch_offset ATTRIBUTE_UNUSED) {
92 LOG(FATAL) << "UNIMPLEMENTED";
93 }
94
GenerateThunkDebugInfo(uint32_t executable_offset ATTRIBUTE_UNUSED)95 std::vector<debug::MethodDebugInfo> MipsRelativePatcher::GenerateThunkDebugInfo(
96 uint32_t executable_offset ATTRIBUTE_UNUSED) {
97 return std::vector<debug::MethodDebugInfo>(); // No thunks added.
98 }
99
100 } // namespace linker
101 } // namespace art
102