Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [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 | #ifndef ART_COMPILER_LINKER_MULTI_OAT_RELATIVE_PATCHER_H_ |
| 18 | #define ART_COMPILER_LINKER_MULTI_OAT_RELATIVE_PATCHER_H_ |
| 19 | |
| 20 | #include "arch/instruction_set.h" |
Vladimir Marko | 1b404a8 | 2017-09-01 13:35:26 +0100 | [diff] [blame^] | 21 | #include "debug/method_debug_info.h" |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 22 | #include "method_reference.h" |
| 23 | #include "relative_patcher.h" |
| 24 | #include "safe_map.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | class CompiledMethod; |
| 29 | class LinkerPatch; |
| 30 | class InstructionSetFeatures; |
| 31 | |
| 32 | namespace linker { |
| 33 | |
| 34 | // MultiOatRelativePatcher is a helper class for handling patching across |
| 35 | // any number of oat files. It provides storage for method code offsets |
| 36 | // and wraps RelativePatcher calls, adjusting relative offsets according |
| 37 | // to the value set by SetAdjustment(). |
| 38 | class MultiOatRelativePatcher FINAL { |
| 39 | public: |
| 40 | using const_iterator = |
| 41 | SafeMap<MethodReference, uint32_t, MethodReferenceComparator>::const_iterator; |
| 42 | |
| 43 | MultiOatRelativePatcher(InstructionSet instruction_set, const InstructionSetFeatures* features); |
| 44 | |
| 45 | // Mark the start of a new oat file (for statistics retrieval) and set the |
| 46 | // adjustment for a new oat file to apply to all relative offsets that are |
| 47 | // passed to the MultiOatRelativePatcher. |
| 48 | // |
| 49 | // The adjustment should be the global offset of the base from which relative |
| 50 | // offsets are calculated, such as the start of .rodata for the current oat file. |
| 51 | // It must must never point directly to a method's code to avoid relative offsets |
| 52 | // with value 0 because this value is used as a missing offset indication in |
| 53 | // GetOffset() and an error indication in WriteThunks(). Additionally, it must be |
| 54 | // page-aligned, so that it does not skew alignment calculations, say arm64 ADRP. |
| 55 | void StartOatFile(uint32_t adjustment); |
| 56 | |
| 57 | // Get relative offset. Returns 0 when the offset has not been set yet. |
| 58 | uint32_t GetOffset(MethodReference method_ref) { |
| 59 | auto it = method_offset_map_.map.find(method_ref); |
| 60 | return (it != method_offset_map_.map.end()) ? it->second - adjustment_ : 0u; |
| 61 | } |
| 62 | |
| 63 | // Set the offset. |
| 64 | void SetOffset(MethodReference method_ref, uint32_t offset) { |
| 65 | method_offset_map_.map.Put(method_ref, offset + adjustment_); |
| 66 | } |
| 67 | |
| 68 | // Wrapper around RelativePatcher::ReserveSpace(), doing offset adjustment. |
| 69 | uint32_t ReserveSpace(uint32_t offset, |
| 70 | const CompiledMethod* compiled_method, |
| 71 | MethodReference method_ref) { |
| 72 | offset += adjustment_; |
| 73 | offset = relative_patcher_->ReserveSpace(offset, compiled_method, method_ref); |
| 74 | offset -= adjustment_; |
| 75 | return offset; |
| 76 | } |
| 77 | |
| 78 | // Wrapper around RelativePatcher::ReserveSpaceEnd(), doing offset adjustment. |
| 79 | uint32_t ReserveSpaceEnd(uint32_t offset) { |
| 80 | offset += adjustment_; |
| 81 | offset = relative_patcher_->ReserveSpaceEnd(offset); |
| 82 | offset -= adjustment_; |
| 83 | return offset; |
| 84 | } |
| 85 | |
| 86 | // Wrapper around RelativePatcher::WriteThunks(), doing offset adjustment. |
| 87 | uint32_t WriteThunks(OutputStream* out, uint32_t offset) { |
| 88 | offset += adjustment_; |
| 89 | offset = relative_patcher_->WriteThunks(out, offset); |
| 90 | if (offset != 0u) { // 0u indicates write error. |
| 91 | offset -= adjustment_; |
| 92 | } |
| 93 | return offset; |
| 94 | } |
| 95 | |
| 96 | // Wrapper around RelativePatcher::PatchCall(), doing offset adjustment. |
| 97 | void PatchCall(std::vector<uint8_t>* code, |
| 98 | uint32_t literal_offset, |
| 99 | uint32_t patch_offset, |
| 100 | uint32_t target_offset) { |
| 101 | patch_offset += adjustment_; |
| 102 | target_offset += adjustment_; |
| 103 | relative_patcher_->PatchCall(code, literal_offset, patch_offset, target_offset); |
| 104 | } |
| 105 | |
Vladimir Marko | 5f07820 | 2017-05-18 13:32:53 +0100 | [diff] [blame] | 106 | // Wrapper around RelativePatcher::PatchPcRelativeReference(), doing offset adjustment. |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 107 | void PatchPcRelativeReference(std::vector<uint8_t>* code, |
| 108 | const LinkerPatch& patch, |
| 109 | uint32_t patch_offset, |
| 110 | uint32_t target_offset) { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 111 | patch_offset += adjustment_; |
| 112 | target_offset += adjustment_; |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 113 | relative_patcher_->PatchPcRelativeReference(code, patch, patch_offset, target_offset); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Vladimir Marko | f4f2daa | 2017-03-20 18:26:59 +0000 | [diff] [blame] | 116 | void PatchBakerReadBarrierBranch(std::vector<uint8_t>* code, |
| 117 | const LinkerPatch& patch, |
| 118 | uint32_t patch_offset) { |
| 119 | patch_offset += adjustment_; |
| 120 | relative_patcher_->PatchBakerReadBarrierBranch(code, patch, patch_offset); |
| 121 | } |
| 122 | |
Vladimir Marko | 1b404a8 | 2017-09-01 13:35:26 +0100 | [diff] [blame^] | 123 | std::vector<debug::MethodDebugInfo> GenerateThunkDebugInfo(size_t executable_offset) { |
| 124 | executable_offset += adjustment_; |
| 125 | return relative_patcher_->GenerateThunkDebugInfo(executable_offset); |
| 126 | } |
| 127 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 128 | // Wrappers around RelativePatcher for statistics retrieval. |
| 129 | uint32_t CodeAlignmentSize() const; |
| 130 | uint32_t RelativeCallThunksSize() const; |
| 131 | uint32_t MiscThunksSize() const; |
| 132 | |
| 133 | private: |
| 134 | // Map method reference to assigned offset. |
| 135 | // Wrap the map in a class implementing linker::RelativePatcherTargetProvider. |
| 136 | class MethodOffsetMap : public linker::RelativePatcherTargetProvider { |
| 137 | public: |
| 138 | std::pair<bool, uint32_t> FindMethodOffset(MethodReference ref) OVERRIDE; |
| 139 | SafeMap<MethodReference, uint32_t, MethodReferenceComparator> map; |
| 140 | }; |
| 141 | |
| 142 | MethodOffsetMap method_offset_map_; |
| 143 | std::unique_ptr<RelativePatcher> relative_patcher_; |
| 144 | uint32_t adjustment_; |
| 145 | InstructionSet instruction_set_; |
| 146 | |
| 147 | uint32_t start_size_code_alignment_; |
| 148 | uint32_t start_size_relative_call_thunks_; |
| 149 | uint32_t start_size_misc_thunks_; |
| 150 | |
| 151 | friend class MultiOatRelativePatcherTest; |
| 152 | |
| 153 | DISALLOW_COPY_AND_ASSIGN(MultiOatRelativePatcher); |
| 154 | }; |
| 155 | |
| 156 | } // namespace linker |
| 157 | } // namespace art |
| 158 | |
| 159 | #endif // ART_COMPILER_LINKER_MULTI_OAT_RELATIVE_PATCHER_H_ |