blob: c09950cd5de5e4f091fc2566b99f6a7311e6b0ac [file] [log] [blame]
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001/*
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
21namespace art {
22namespace linker {
23
24uint32_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
31uint32_t MipsRelativePatcher::ReserveSpaceEnd(uint32_t offset) {
32 return offset; // No space reserved; no limit on relative call distance.
33}
34
35uint32_t MipsRelativePatcher::WriteThunks(OutputStream* out ATTRIBUTE_UNUSED, uint32_t offset) {
36 return offset; // No thunks added; no limit on relative call distance.
37}
38
39void 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
46void 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();
Alexey Frunze06a46c42016-07-19 15:00:40 -070052 bool dex_cache_array = (patch.GetType() == LinkerPatch::Type::kDexCacheArray);
Alexey Frunzee3fb2452016-05-10 16:08:05 -070053
54 // Basic sanity checks.
55 if (is_r6) {
56 DCHECK_GE(code->size(), 8u);
57 DCHECK_LE(literal_offset, code->size() - 8u);
58 DCHECK_EQ(literal_offset, anchor_literal_offset);
59 // AUIPC reg, offset_high
60 DCHECK_EQ((*code)[literal_offset + 0], 0x34);
61 DCHECK_EQ((*code)[literal_offset + 1], 0x12);
62 DCHECK_EQ(((*code)[literal_offset + 2] & 0x1F), 0x1E);
63 DCHECK_EQ(((*code)[literal_offset + 3] & 0xFC), 0xEC);
64 // ADDIU reg, reg, offset_low
65 DCHECK_EQ((*code)[literal_offset + 4], 0x78);
66 DCHECK_EQ((*code)[literal_offset + 5], 0x56);
67 DCHECK_EQ(((*code)[literal_offset + 7] & 0xFC), 0x24);
68 } else {
69 DCHECK_GE(code->size(), 16u);
70 DCHECK_LE(literal_offset, code->size() - 12u);
71 DCHECK_GE(literal_offset, 4u);
Alexey Frunze06a46c42016-07-19 15:00:40 -070072 // The NAL instruction may not precede immediately as the PC+0 value may
73 // come from HMipsComputeBaseMethodAddress.
74 if (dex_cache_array) {
75 DCHECK_EQ(literal_offset + 4u, anchor_literal_offset);
76 // NAL
77 DCHECK_EQ((*code)[literal_offset - 4], 0x00);
78 DCHECK_EQ((*code)[literal_offset - 3], 0x00);
79 DCHECK_EQ((*code)[literal_offset - 2], 0x10);
80 DCHECK_EQ((*code)[literal_offset - 1], 0x04);
81 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -070082 // LUI reg, offset_high
83 DCHECK_EQ((*code)[literal_offset + 0], 0x34);
84 DCHECK_EQ((*code)[literal_offset + 1], 0x12);
85 DCHECK_EQ(((*code)[literal_offset + 2] & 0xE0), 0x00);
86 DCHECK_EQ((*code)[literal_offset + 3], 0x3C);
87 // ORI reg, reg, offset_low
88 DCHECK_EQ((*code)[literal_offset + 4], 0x78);
89 DCHECK_EQ((*code)[literal_offset + 5], 0x56);
90 DCHECK_EQ(((*code)[literal_offset + 7] & 0xFC), 0x34);
Alexey Frunze06a46c42016-07-19 15:00:40 -070091 // ADDU reg, reg, reg2
Alexey Frunzee3fb2452016-05-10 16:08:05 -070092 DCHECK_EQ((*code)[literal_offset + 8], 0x21);
93 DCHECK_EQ(((*code)[literal_offset + 9] & 0x07), 0x00);
Alexey Frunze06a46c42016-07-19 15:00:40 -070094 if (dex_cache_array) {
95 // reg2 is either RA or from HMipsComputeBaseMethodAddress.
96 DCHECK_EQ(((*code)[literal_offset + 10] & 0x1F), 0x1F);
97 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -070098 DCHECK_EQ(((*code)[literal_offset + 11] & 0xFC), 0x00);
99 }
100
101 // Apply patch.
102 uint32_t anchor_offset = patch_offset - literal_offset + anchor_literal_offset;
Alexey Frunze06a46c42016-07-19 15:00:40 -0700103 uint32_t diff = target_offset - anchor_offset;
104 if (dex_cache_array) {
105 diff += kDexCacheArrayLwOffset;
106 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700107 if (is_r6) {
108 diff += (diff & 0x8000) << 1; // Account for sign extension in ADDIU.
109 }
110
111 // LUI reg, offset_high / AUIPC reg, offset_high
112 (*code)[literal_offset + 0] = static_cast<uint8_t>(diff >> 16);
113 (*code)[literal_offset + 1] = static_cast<uint8_t>(diff >> 24);
114 // ORI reg, reg, offset_low / ADDIU reg, reg, offset_low
115 (*code)[literal_offset + 4] = static_cast<uint8_t>(diff >> 0);
116 (*code)[literal_offset + 5] = static_cast<uint8_t>(diff >> 8);
117}
118
119} // namespace linker
120} // namespace art