Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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/relative_patcher.h" |
| 18 | |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 19 | #ifdef ART_ENABLE_CODEGEN_arm |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 20 | #include "linker/arm/relative_patcher_thumb2.h" |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 21 | #endif |
| 22 | #ifdef ART_ENABLE_CODEGEN_arm64 |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 23 | #include "linker/arm64/relative_patcher_arm64.h" |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 24 | #endif |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 25 | #ifdef ART_ENABLE_CODEGEN_mips |
| 26 | #include "linker/mips/relative_patcher_mips.h" |
| 27 | #endif |
Alexey Frunze | 19f6c69 | 2016-11-30 19:19:55 -0800 | [diff] [blame] | 28 | #ifdef ART_ENABLE_CODEGEN_mips64 |
| 29 | #include "linker/mips64/relative_patcher_mips64.h" |
| 30 | #endif |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 31 | #ifdef ART_ENABLE_CODEGEN_x86 |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 32 | #include "linker/x86/relative_patcher_x86.h" |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 33 | #endif |
| 34 | #ifdef ART_ENABLE_CODEGEN_x86_64 |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 35 | #include "linker/x86_64/relative_patcher_x86_64.h" |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 36 | #endif |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 37 | #include "output_stream.h" |
| 38 | |
| 39 | namespace art { |
| 40 | namespace linker { |
| 41 | |
| 42 | std::unique_ptr<RelativePatcher> RelativePatcher::Create( |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 43 | InstructionSet instruction_set, |
| 44 | const InstructionSetFeatures* features, |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 45 | RelativePatcherTargetProvider* provider) { |
| 46 | class RelativePatcherNone FINAL : public RelativePatcher { |
| 47 | public: |
| 48 | RelativePatcherNone() { } |
| 49 | |
| 50 | uint32_t ReserveSpace(uint32_t offset, |
Vladimir Marko | 4d23c9d | 2015-04-01 23:03:09 +0100 | [diff] [blame] | 51 | const CompiledMethod* compiled_method ATTRIBUTE_UNUSED, |
| 52 | MethodReference method_ref ATTRIBUTE_UNUSED) OVERRIDE { |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 53 | return offset; // No space reserved; no patches expected. |
| 54 | } |
| 55 | |
Vladimir Marko | 71b0ddf | 2015-04-02 19:45:06 +0100 | [diff] [blame] | 56 | uint32_t ReserveSpaceEnd(uint32_t offset) OVERRIDE { |
| 57 | return offset; // No space reserved; no patches expected. |
| 58 | } |
| 59 | |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 60 | uint32_t WriteThunks(OutputStream* out ATTRIBUTE_UNUSED, uint32_t offset) OVERRIDE { |
| 61 | return offset; // No thunks added; no patches expected. |
| 62 | } |
| 63 | |
| 64 | void PatchCall(std::vector<uint8_t>* code ATTRIBUTE_UNUSED, |
| 65 | uint32_t literal_offset ATTRIBUTE_UNUSED, |
| 66 | uint32_t patch_offset ATTRIBUTE_UNUSED, |
| 67 | uint32_t target_offset ATTRIBUTE_UNUSED) OVERRIDE { |
| 68 | LOG(FATAL) << "Unexpected relative call patch."; |
| 69 | } |
| 70 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 71 | void PatchPcRelativeReference(std::vector<uint8_t>* code ATTRIBUTE_UNUSED, |
| 72 | const LinkerPatch& patch ATTRIBUTE_UNUSED, |
| 73 | uint32_t patch_offset ATTRIBUTE_UNUSED, |
| 74 | uint32_t target_offset ATTRIBUTE_UNUSED) OVERRIDE { |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 75 | LOG(FATAL) << "Unexpected relative dex cache array patch."; |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | DISALLOW_COPY_AND_ASSIGN(RelativePatcherNone); |
| 80 | }; |
| 81 | |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 82 | UNUSED(features); |
| 83 | UNUSED(provider); |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 84 | switch (instruction_set) { |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 85 | #ifdef ART_ENABLE_CODEGEN_x86 |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 86 | case kX86: |
| 87 | return std::unique_ptr<RelativePatcher>(new X86RelativePatcher()); |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 88 | #endif |
| 89 | #ifdef ART_ENABLE_CODEGEN_x86_64 |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 90 | case kX86_64: |
| 91 | return std::unique_ptr<RelativePatcher>(new X86_64RelativePatcher()); |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 92 | #endif |
| 93 | #ifdef ART_ENABLE_CODEGEN_arm |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 94 | case kArm: |
| 95 | // Fall through: we generate Thumb2 code for "arm". |
| 96 | case kThumb2: |
| 97 | return std::unique_ptr<RelativePatcher>(new Thumb2RelativePatcher(provider)); |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 98 | #endif |
| 99 | #ifdef ART_ENABLE_CODEGEN_arm64 |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 100 | case kArm64: |
| 101 | return std::unique_ptr<RelativePatcher>( |
| 102 | new Arm64RelativePatcher(provider, features->AsArm64InstructionSetFeatures())); |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 103 | #endif |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 104 | #ifdef ART_ENABLE_CODEGEN_mips |
| 105 | case kMips: |
| 106 | return std::unique_ptr<RelativePatcher>( |
| 107 | new MipsRelativePatcher(features->AsMipsInstructionSetFeatures())); |
| 108 | #endif |
Alexey Frunze | 19f6c69 | 2016-11-30 19:19:55 -0800 | [diff] [blame] | 109 | #ifdef ART_ENABLE_CODEGEN_mips64 |
| 110 | case kMips64: |
| 111 | return std::unique_ptr<RelativePatcher>(new Mips64RelativePatcher()); |
| 112 | #endif |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 113 | default: |
| 114 | return std::unique_ptr<RelativePatcher>(new RelativePatcherNone); |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | bool RelativePatcher::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) { |
| 119 | static const uint8_t kPadding[] = { |
| 120 | 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u |
| 121 | }; |
| 122 | DCHECK_LE(aligned_code_delta, sizeof(kPadding)); |
| 123 | if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) { |
| 124 | return false; |
| 125 | } |
| 126 | size_code_alignment_ += aligned_code_delta; |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | bool RelativePatcher::WriteRelCallThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk) { |
| 131 | if (UNLIKELY(!out->WriteFully(thunk.data(), thunk.size()))) { |
| 132 | return false; |
| 133 | } |
| 134 | size_relative_call_thunks_ += thunk.size(); |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | bool RelativePatcher::WriteMiscThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk) { |
| 139 | if (UNLIKELY(!out->WriteFully(thunk.data(), thunk.size()))) { |
| 140 | return false; |
| 141 | } |
| 142 | size_misc_thunks_ += thunk.size(); |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | } // namespace linker |
| 147 | } // namespace art |