blob: 2992487062661f16c42d4ffc696cfa2120b82b0f [file] [log] [blame]
Alexey Frunze19f6c692016-11-30 19:19:55 -08001/*
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/mips64/relative_patcher_mips64.h"
18
19#include "compiled_method.h"
Vladimir Marko1b404a82017-09-01 13:35:26 +010020#include "debug/method_debug_info.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010021#include "linker/linker_patch.h"
Alexey Frunze19f6c692016-11-30 19:19:55 -080022
23namespace art {
24namespace linker {
25
26uint32_t Mips64RelativePatcher::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
33uint32_t Mips64RelativePatcher::ReserveSpaceEnd(uint32_t offset) {
34 return offset; // No space reserved; no limit on relative call distance.
35}
36
37uint32_t Mips64RelativePatcher::WriteThunks(OutputStream* out ATTRIBUTE_UNUSED, uint32_t offset) {
38 return offset; // No thunks added; no limit on relative call distance.
39}
40
Alexey Frunze5fa5c042017-06-01 21:07:52 -070041void Mips64RelativePatcher::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 MIPS64";
Alexey Frunze19f6c692016-11-30 19:19:55 -080046}
47
48void Mips64RelativePatcher::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();
Alexey Frunze5fa5c042017-06-01 21:07:52 -070054 bool high_patch = ((*code)[literal_offset + 0] == 0x34) && ((*code)[literal_offset + 1] == 0x12);
Alexey Frunze19f6c692016-11-30 19:19:55 -080055
Alexey Frunze5fa5c042017-06-01 21:07:52 -070056 // Perform basic sanity checks.
57 if (high_patch) {
58 // auipc reg, offset_high
59 DCHECK_EQ(((*code)[literal_offset + 2] & 0x1F), 0x1E);
60 DCHECK_EQ(((*code)[literal_offset + 3] & 0xFC), 0xEC);
61 } else {
62 // instr reg(s), offset_low
63 CHECK_EQ((*code)[literal_offset + 0], 0x78);
64 CHECK_EQ((*code)[literal_offset + 1], 0x56);
65 }
Alexey Frunze19f6c692016-11-30 19:19:55 -080066
67 // Apply patch.
68 uint32_t anchor_offset = patch_offset - literal_offset + anchor_literal_offset;
69 uint32_t diff = target_offset - anchor_offset;
70 // Note that a combination of auipc with an instruction that adds a sign-extended
71 // 16-bit immediate operand (e.g. ld) provides a PC-relative range of
72 // PC-0x80000000 to PC+0x7FFF7FFF on MIPS64, that is, short of 2GB on one end
73 // by 32KB.
Alexey Frunze5fa5c042017-06-01 21:07:52 -070074 diff += (diff & 0x8000) << 1; // Account for sign extension in "instr reg(s), offset_low".
Alexey Frunze19f6c692016-11-30 19:19:55 -080075
Alexey Frunze5fa5c042017-06-01 21:07:52 -070076 if (high_patch) {
77 // 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 Frunze19f6c692016-11-30 19:19:55 -080085}
86
Vladimir Markof6675082019-05-17 12:05:28 +010087void Mips64RelativePatcher::PatchEntrypointCall(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
Vladimir Markof4f2daa2017-03-20 18:26:59 +000093void Mips64RelativePatcher::PatchBakerReadBarrierBranch(std::vector<uint8_t>* code ATTRIBUTE_UNUSED,
94 const LinkerPatch& patch ATTRIBUTE_UNUSED,
95 uint32_t patch_offset ATTRIBUTE_UNUSED) {
96 LOG(FATAL) << "UNIMPLEMENTED";
97}
98
Vladimir Marko1b404a82017-09-01 13:35:26 +010099std::vector<debug::MethodDebugInfo> Mips64RelativePatcher::GenerateThunkDebugInfo(
100 uint32_t executable_offset ATTRIBUTE_UNUSED) {
101 return std::vector<debug::MethodDebugInfo>(); // No thunks added.
102}
103
Alexey Frunze19f6c692016-11-30 19:19:55 -0800104} // namespace linker
105} // namespace art