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