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_COMPILER_LINKER_RELATIVE_PATCHER_H_
18 #define ART_COMPILER_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/macros.h"
25 #include "method_reference.h"
26 #include "utils/array_ref.h"
27 
28 namespace art {
29 
30 class CompiledMethod;
31 class LinkerPatch;
32 class OutputStream;
33 
34 namespace linker {
35 
36 /**
37  * @class RelativePatcherTargetProvider
38  * @brief Interface for providing method offsets for relative call targets.
39  */
40 class RelativePatcherTargetProvider {
41  public:
42   /**
43    * Find the offset of the target method of a relative call if known.
44    *
45    * The process of assigning target method offsets includes calls to the relative patcher's
46    * ReserveSpace() which in turn can use FindMethodOffset() to determine if a method already
47    * has an offset assigned and, if so, what's that offset. If the offset has not yet been
48    * assigned or if it's too far for the particular architecture's relative call,
49    * ReserveSpace() may need to allocate space for a special dispatch thunk.
50    *
51    * @param ref the target method of the relative call.
52    * @return true in the first element of the pair if the method was found, false otherwise;
53    *         if found, the second element specifies the offset.
54    */
55   virtual std::pair<bool, uint32_t> FindMethodOffset(MethodReference ref) = 0;
56 
57  protected:
~RelativePatcherTargetProvider()58   virtual ~RelativePatcherTargetProvider() { }
59 };
60 
61 /**
62  * @class RelativePatcher
63  * @brief Interface for architecture-specific link-time patching of PC-relative references.
64  */
65 class RelativePatcher {
66  public:
67   static std::unique_ptr<RelativePatcher> Create(
68       InstructionSet instruction_set, const InstructionSetFeatures* features,
69       RelativePatcherTargetProvider* provider);
70 
~RelativePatcher()71   virtual ~RelativePatcher() { }
72 
CodeAlignmentSize()73   uint32_t CodeAlignmentSize() const {
74     return size_code_alignment_;
75   }
76 
RelativeCallThunksSize()77   uint32_t RelativeCallThunksSize() const {
78     return size_relative_call_thunks_;
79   }
80 
MiscThunksSize()81   uint32_t MiscThunksSize() const {
82     return size_misc_thunks_;
83   }
84 
85   // Reserve space for thunks if needed before a method, return adjusted offset.
86   virtual uint32_t ReserveSpace(uint32_t offset, const CompiledMethod* compiled_method,
87                                 MethodReference method_ref) = 0;
88 
89   // Reserve space for thunks if needed after the last method, return adjusted offset.
90   virtual uint32_t ReserveSpaceEnd(uint32_t offset) = 0;
91 
92   // Write relative call thunks if needed, return adjusted offset.
93   virtual uint32_t WriteThunks(OutputStream* out, uint32_t offset) = 0;
94 
95   // Patch method code. The input displacement is relative to the patched location,
96   // the patcher may need to adjust it if the correct base is different.
97   virtual void PatchCall(std::vector<uint8_t>* code, uint32_t literal_offset,
98                          uint32_t patch_offset, uint32_t target_offset) = 0;
99 
100   // Patch a reference to a dex cache location.
101   virtual void PatchDexCacheReference(std::vector<uint8_t>* code, const LinkerPatch& patch,
102                                       uint32_t patch_offset, uint32_t target_offset) = 0;
103 
104  protected:
RelativePatcher()105   RelativePatcher()
106       : size_code_alignment_(0u),
107         size_relative_call_thunks_(0u),
108         size_misc_thunks_(0u) {
109   }
110 
111   bool WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta);
112   bool WriteRelCallThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk);
113   bool WriteMiscThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk);
114 
115  private:
116   uint32_t size_code_alignment_;
117   uint32_t size_relative_call_thunks_;
118   uint32_t size_misc_thunks_;
119 
120   DISALLOW_COPY_AND_ASSIGN(RelativePatcher);
121 };
122 
123 }  // namespace linker
124 }  // namespace art
125 
126 #endif  // ART_COMPILER_LINKER_RELATIVE_PATCHER_H_
127