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 | |
Vladimir Marko | 6d66fcf | 2018-04-12 13:15:27 +0100 | [diff] [blame] | 17 | #ifndef ART_DEX2OAT_LINKER_RELATIVE_PATCHER_H_ |
| 18 | #define ART_DEX2OAT_LINKER_RELATIVE_PATCHER_H_ |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 19 | |
| 20 | #include <vector> |
| 21 | |
| 22 | #include "arch/instruction_set.h" |
| 23 | #include "arch/instruction_set_features.h" |
David Brazdil | d9c9037 | 2016-09-14 16:53:55 +0100 | [diff] [blame] | 24 | #include "base/array_ref.h" |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 25 | #include "base/macros.h" |
David Sehr | 312f3b2 | 2018-03-19 08:39:26 -0700 | [diff] [blame] | 26 | #include "dex/method_reference.h" |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | class CompiledMethod; |
David Srbecky | 2faab00 | 2019-02-12 16:35:48 +0000 | [diff] [blame] | 31 | class OutputStream; |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 32 | |
Vladimir Marko | 1b404a8 | 2017-09-01 13:35:26 +0100 | [diff] [blame] | 33 | namespace debug { |
| 34 | struct MethodDebugInfo; |
| 35 | } // namespace debug |
| 36 | |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 37 | namespace linker { |
| 38 | |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 39 | class LinkerPatch; |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 40 | |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 41 | /** |
Vladimir Marko | ca1e038 | 2018-04-11 09:58:41 +0000 | [diff] [blame] | 42 | * @class RelativePatcherThunkProvider |
| 43 | * @brief Interface for providing method offsets for relative call targets. |
| 44 | */ |
| 45 | class RelativePatcherThunkProvider { |
| 46 | public: |
| 47 | /** |
| 48 | * Get the code and debug name of a thunk needed by the given linker patch. |
| 49 | * |
| 50 | * @param patch The patch for which we need to retrieve the thunk code. |
| 51 | * @param code A variable to receive the code of the thunk. This code must not be empty. |
| 52 | * @param debug_name A variable to receive the debug name of the thunk. |
| 53 | */ |
| 54 | virtual void GetThunkCode(const LinkerPatch& patch, |
| 55 | /*out*/ ArrayRef<const uint8_t>* code, |
| 56 | /*out*/ std::string* debug_name) = 0; |
| 57 | |
| 58 | protected: |
| 59 | virtual ~RelativePatcherThunkProvider() { } |
| 60 | }; |
| 61 | |
| 62 | /** |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 63 | * @class RelativePatcherTargetProvider |
| 64 | * @brief Interface for providing method offsets for relative call targets. |
| 65 | */ |
| 66 | class RelativePatcherTargetProvider { |
| 67 | public: |
| 68 | /** |
| 69 | * Find the offset of the target method of a relative call if known. |
| 70 | * |
| 71 | * The process of assigning target method offsets includes calls to the relative patcher's |
| 72 | * ReserveSpace() which in turn can use FindMethodOffset() to determine if a method already |
| 73 | * has an offset assigned and, if so, what's that offset. If the offset has not yet been |
| 74 | * assigned or if it's too far for the particular architecture's relative call, |
| 75 | * ReserveSpace() may need to allocate space for a special dispatch thunk. |
| 76 | * |
| 77 | * @param ref the target method of the relative call. |
| 78 | * @return true in the first element of the pair if the method was found, false otherwise; |
| 79 | * if found, the second element specifies the offset. |
| 80 | */ |
| 81 | virtual std::pair<bool, uint32_t> FindMethodOffset(MethodReference ref) = 0; |
| 82 | |
| 83 | protected: |
| 84 | virtual ~RelativePatcherTargetProvider() { } |
| 85 | }; |
| 86 | |
| 87 | /** |
| 88 | * @class RelativePatcher |
| 89 | * @brief Interface for architecture-specific link-time patching of PC-relative references. |
| 90 | */ |
| 91 | class RelativePatcher { |
| 92 | public: |
| 93 | static std::unique_ptr<RelativePatcher> Create( |
Vladimir Marko | ca1e038 | 2018-04-11 09:58:41 +0000 | [diff] [blame] | 94 | InstructionSet instruction_set, |
| 95 | const InstructionSetFeatures* features, |
| 96 | RelativePatcherThunkProvider* thunk_provider, |
| 97 | RelativePatcherTargetProvider* target_provider); |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 98 | |
| 99 | virtual ~RelativePatcher() { } |
| 100 | |
| 101 | uint32_t CodeAlignmentSize() const { |
| 102 | return size_code_alignment_; |
| 103 | } |
| 104 | |
| 105 | uint32_t RelativeCallThunksSize() const { |
| 106 | return size_relative_call_thunks_; |
| 107 | } |
| 108 | |
| 109 | uint32_t MiscThunksSize() const { |
| 110 | return size_misc_thunks_; |
| 111 | } |
| 112 | |
Vladimir Marko | 71b0ddf | 2015-04-02 19:45:06 +0100 | [diff] [blame] | 113 | // Reserve space for thunks if needed before a method, return adjusted offset. |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 114 | virtual uint32_t ReserveSpace(uint32_t offset, |
| 115 | const CompiledMethod* compiled_method, |
Vladimir Marko | 4d23c9d | 2015-04-01 23:03:09 +0100 | [diff] [blame] | 116 | MethodReference method_ref) = 0; |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 117 | |
Vladimir Marko | 71b0ddf | 2015-04-02 19:45:06 +0100 | [diff] [blame] | 118 | // Reserve space for thunks if needed after the last method, return adjusted offset. |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 119 | // The caller may use this method to preemptively force thunk space reservation and |
| 120 | // then resume reservation for more methods. This is useful when there is a gap in |
| 121 | // the .text segment, for example when going to the next oat file for multi-image. |
Vladimir Marko | 71b0ddf | 2015-04-02 19:45:06 +0100 | [diff] [blame] | 122 | virtual uint32_t ReserveSpaceEnd(uint32_t offset) = 0; |
| 123 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 124 | // Write relative call thunks if needed, return adjusted offset. Returns 0 on write failure. |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 125 | virtual uint32_t WriteThunks(OutputStream* out, uint32_t offset) = 0; |
| 126 | |
| 127 | // Patch method code. The input displacement is relative to the patched location, |
| 128 | // the patcher may need to adjust it if the correct base is different. |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 129 | virtual void PatchCall(std::vector<uint8_t>* code, |
| 130 | uint32_t literal_offset, |
| 131 | uint32_t patch_offset, |
| 132 | uint32_t target_offset) = 0; |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 133 | |
| 134 | // Patch a reference to a dex cache location. |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 135 | virtual void PatchPcRelativeReference(std::vector<uint8_t>* code, |
| 136 | const LinkerPatch& patch, |
| 137 | uint32_t patch_offset, |
| 138 | uint32_t target_offset) = 0; |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 139 | |
Vladimir Marko | f667508 | 2019-05-17 12:05:28 +0100 | [diff] [blame] | 140 | // Patch a call to an entrypoint trampoline. |
| 141 | virtual void PatchEntrypointCall(std::vector<uint8_t>* code, |
| 142 | const LinkerPatch& patch, |
| 143 | uint32_t patch_offset) = 0; |
| 144 | |
Vladimir Marko | f4f2daa | 2017-03-20 18:26:59 +0000 | [diff] [blame] | 145 | // Patch a branch to a Baker read barrier thunk. |
| 146 | virtual void PatchBakerReadBarrierBranch(std::vector<uint8_t>* code, |
| 147 | const LinkerPatch& patch, |
| 148 | uint32_t patch_offset) = 0; |
| 149 | |
Vladimir Marko | 1b404a8 | 2017-09-01 13:35:26 +0100 | [diff] [blame] | 150 | virtual std::vector<debug::MethodDebugInfo> GenerateThunkDebugInfo( |
| 151 | uint32_t executable_offset) = 0; |
| 152 | |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 153 | protected: |
| 154 | RelativePatcher() |
| 155 | : size_code_alignment_(0u), |
| 156 | size_relative_call_thunks_(0u), |
| 157 | size_misc_thunks_(0u) { |
| 158 | } |
| 159 | |
| 160 | bool WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta); |
Vladimir Marko | f4f2daa | 2017-03-20 18:26:59 +0000 | [diff] [blame] | 161 | bool WriteThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk); |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 162 | bool WriteMiscThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk); |
| 163 | |
| 164 | private: |
| 165 | uint32_t size_code_alignment_; |
| 166 | uint32_t size_relative_call_thunks_; |
| 167 | uint32_t size_misc_thunks_; |
| 168 | |
| 169 | DISALLOW_COPY_AND_ASSIGN(RelativePatcher); |
| 170 | }; |
| 171 | |
| 172 | } // namespace linker |
| 173 | } // namespace art |
| 174 | |
Vladimir Marko | 6d66fcf | 2018-04-12 13:15:27 +0100 | [diff] [blame] | 175 | #endif // ART_DEX2OAT_LINKER_RELATIVE_PATCHER_H_ |