blob: 6c974c308fd03fe0a22cc6b23a390710ab39686b [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 Frunze5fa5c042017-06-01 21:07:52 -070052 bool high_patch = ((*code)[literal_offset + 0] == 0x34) && ((*code)[literal_offset + 1] == 0x12);
Alexey Frunzee3fb2452016-05-10 16:08:05 -070053
Alexey Frunze5fa5c042017-06-01 21:07:52 -070054 // Perform basic sanity checks.
55 if (high_patch) {
56 if (is_r6) {
57 // auipc reg, offset_high
58 DCHECK_EQ(((*code)[literal_offset + 2] & 0x1F), 0x1E);
59 DCHECK_EQ(((*code)[literal_offset + 3] & 0xFC), 0xEC);
60 } else {
61 // lui reg, offset_high
62 DCHECK_EQ(((*code)[literal_offset + 2] & 0xE0), 0x00);
63 DCHECK_EQ((*code)[literal_offset + 3], 0x3C);
Alexey Frunze5fa5c042017-06-01 21:07:52 -070064 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -070065 } else {
Alexey Frunze6b892cd2017-01-03 17:11:38 -080066 // instr reg(s), offset_low
Alexey Frunze5fa5c042017-06-01 21:07:52 -070067 CHECK_EQ((*code)[literal_offset + 0], 0x78);
68 CHECK_EQ((*code)[literal_offset + 1], 0x56);
Alexey Frunzee3fb2452016-05-10 16:08:05 -070069 }
70
71 // Apply patch.
72 uint32_t anchor_offset = patch_offset - literal_offset + anchor_literal_offset;
Alexey Frunze06a46c42016-07-19 15:00:40 -070073 uint32_t diff = target_offset - anchor_offset;
Alexey Frunze6b892cd2017-01-03 17:11:38 -080074 diff += (diff & 0x8000) << 1; // Account for sign extension in "instr reg(s), offset_low".
Alexey Frunzee3fb2452016-05-10 16:08:05 -070075
Alexey Frunze5fa5c042017-06-01 21:07:52 -070076 if (high_patch) {
77 // lui reg, offset_high / auipc reg, offset_high
78 (*code)[literal_offset + 0] = static_cast<uint8_t>(diff >> 16);
79 (*code)[literal_offset + 1] = static_cast<uint8_t>(diff >> 24);
80 } else {
81 // instr reg(s), offset_low
82 (*code)[literal_offset + 0] = static_cast<uint8_t>(diff >> 0);
83 (*code)[literal_offset + 1] = static_cast<uint8_t>(diff >> 8);
84 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -070085}
86
Vladimir Markof4f2daa2017-03-20 18:26:59 +000087void MipsRelativePatcher::PatchBakerReadBarrierBranch(std::vector<uint8_t>* code ATTRIBUTE_UNUSED,
88 const LinkerPatch& patch ATTRIBUTE_UNUSED,
89 uint32_t patch_offset ATTRIBUTE_UNUSED) {
90 LOG(FATAL) << "UNIMPLEMENTED";
91}
92
Alexey Frunzee3fb2452016-05-10 16:08:05 -070093} // namespace linker
94} // namespace art