• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ART_DEX2OAT_LINKER_RELATIVE_PATCHER_H_
18 #define ART_DEX2OAT_LINKER_RELATIVE_PATCHER_H_
19 
20 #include <vector>
21 
22 #include "arch/instruction_set.h"
23 #include "arch/instruction_set_features.h"
24 #include "base/array_ref.h"
25 #include "base/macros.h"
26 #include "dex/method_reference.h"
27 
28 namespace art {
29 
30 class CompiledMethod;
31 class OutputStream;
32 
33 namespace debug {
34 struct MethodDebugInfo;
35 }  // namespace debug
36 
37 namespace linker {
38 
39 class LinkerPatch;
40 
41 /**
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:
~RelativePatcherThunkProvider()59   virtual ~RelativePatcherThunkProvider() { }
60 };
61 
62 /**
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:
~RelativePatcherTargetProvider()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(
94       InstructionSet instruction_set,
95       const InstructionSetFeatures* features,
96       RelativePatcherThunkProvider* thunk_provider,
97       RelativePatcherTargetProvider* target_provider);
98 
~RelativePatcher()99   virtual ~RelativePatcher() { }
100 
CodeAlignmentSize()101   uint32_t CodeAlignmentSize() const {
102     return size_code_alignment_;
103   }
104 
RelativeCallThunksSize()105   uint32_t RelativeCallThunksSize() const {
106     return size_relative_call_thunks_;
107   }
108 
MiscThunksSize()109   uint32_t MiscThunksSize() const {
110     return size_misc_thunks_;
111   }
112 
113   // Reserve space for thunks if needed before a method, return adjusted offset.
114   virtual uint32_t ReserveSpace(uint32_t offset,
115                                 const CompiledMethod* compiled_method,
116                                 MethodReference method_ref) = 0;
117 
118   // Reserve space for thunks if needed after the last method, return adjusted offset.
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.
122   virtual uint32_t ReserveSpaceEnd(uint32_t offset) = 0;
123 
124   // Write relative call thunks if needed, return adjusted offset. Returns 0 on write failure.
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.
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;
133 
134   // Patch a reference to a dex cache location.
135   virtual void PatchPcRelativeReference(std::vector<uint8_t>* code,
136                                         const LinkerPatch& patch,
137                                         uint32_t patch_offset,
138                                         uint32_t target_offset) = 0;
139 
140   // Patch a branch to a Baker read barrier thunk.
141   virtual void PatchBakerReadBarrierBranch(std::vector<uint8_t>* code,
142                                            const LinkerPatch& patch,
143                                            uint32_t patch_offset) = 0;
144 
145   virtual std::vector<debug::MethodDebugInfo> GenerateThunkDebugInfo(
146       uint32_t executable_offset) = 0;
147 
148  protected:
RelativePatcher()149   RelativePatcher()
150       : size_code_alignment_(0u),
151         size_relative_call_thunks_(0u),
152         size_misc_thunks_(0u) {
153   }
154 
155   bool WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta);
156   bool WriteThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk);
157   bool WriteMiscThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk);
158 
159  private:
160   uint32_t size_code_alignment_;
161   uint32_t size_relative_call_thunks_;
162   uint32_t size_misc_thunks_;
163 
164   DISALLOW_COPY_AND_ASSIGN(RelativePatcher);
165 };
166 
167 }  // namespace linker
168 }  // namespace art
169 
170 #endif  // ART_DEX2OAT_LINKER_RELATIVE_PATCHER_H_
171