blob: 06c7e70d23996bd33dd6897de40cf76153a0eec7 [file] [log] [blame]
Vladimir Markob163bb72015-03-31 21:49:49 +01001/*
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"
David Brazdild9c90372016-09-14 16:53:55 +010024#include "base/array_ref.h"
Vladimir Markob163bb72015-03-31 21:49:49 +010025#include "base/macros.h"
David Sehr312f3b22018-03-19 08:39:26 -070026#include "dex/method_reference.h"
Vladimir Markob163bb72015-03-31 21:49:49 +010027
28namespace art {
29
30class CompiledMethod;
Vladimir Markob163bb72015-03-31 21:49:49 +010031
Vladimir Marko1b404a82017-09-01 13:35:26 +010032namespace debug {
33struct MethodDebugInfo;
34} // namespace debug
35
Vladimir Markob163bb72015-03-31 21:49:49 +010036namespace linker {
37
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010038class LinkerPatch;
Vladimir Marko74527972016-11-29 15:57:32 +000039class OutputStream;
40
Vladimir Markob163bb72015-03-31 21:49:49 +010041/**
Vladimir Markoca1e0382018-04-11 09:58:41 +000042 * @class RelativePatcherThunkProvider
43 * @brief Interface for providing method offsets for relative call targets.
44 */
45class 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 Markob163bb72015-03-31 21:49:49 +010063 * @class RelativePatcherTargetProvider
64 * @brief Interface for providing method offsets for relative call targets.
65 */
66class 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 */
91class RelativePatcher {
92 public:
93 static std::unique_ptr<RelativePatcher> Create(
Vladimir Markoca1e0382018-04-11 09:58:41 +000094 InstructionSet instruction_set,
95 const InstructionSetFeatures* features,
96 RelativePatcherThunkProvider* thunk_provider,
97 RelativePatcherTargetProvider* target_provider);
Vladimir Markob163bb72015-03-31 21:49:49 +010098
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 Marko71b0ddf2015-04-02 19:45:06 +0100113 // Reserve space for thunks if needed before a method, return adjusted offset.
Vladimir Marko944da602016-02-19 12:27:55 +0000114 virtual uint32_t ReserveSpace(uint32_t offset,
115 const CompiledMethod* compiled_method,
Vladimir Marko4d23c9d2015-04-01 23:03:09 +0100116 MethodReference method_ref) = 0;
Vladimir Markob163bb72015-03-31 21:49:49 +0100117
Vladimir Marko71b0ddf2015-04-02 19:45:06 +0100118 // Reserve space for thunks if needed after the last method, return adjusted offset.
Vladimir Marko944da602016-02-19 12:27:55 +0000119 // 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 Marko71b0ddf2015-04-02 19:45:06 +0100122 virtual uint32_t ReserveSpaceEnd(uint32_t offset) = 0;
123
Vladimir Marko944da602016-02-19 12:27:55 +0000124 // Write relative call thunks if needed, return adjusted offset. Returns 0 on write failure.
Vladimir Markob163bb72015-03-31 21:49:49 +0100125 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 Marko944da602016-02-19 12:27:55 +0000129 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 Markob163bb72015-03-31 21:49:49 +0100133
134 // Patch a reference to a dex cache location.
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000135 virtual void PatchPcRelativeReference(std::vector<uint8_t>* code,
136 const LinkerPatch& patch,
137 uint32_t patch_offset,
138 uint32_t target_offset) = 0;
Vladimir Markob163bb72015-03-31 21:49:49 +0100139
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000140 // 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
Vladimir Marko1b404a82017-09-01 13:35:26 +0100145 virtual std::vector<debug::MethodDebugInfo> GenerateThunkDebugInfo(
146 uint32_t executable_offset) = 0;
147
Vladimir Markob163bb72015-03-31 21:49:49 +0100148 protected:
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);
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000156 bool WriteThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk);
Vladimir Markob163bb72015-03-31 21:49:49 +0100157 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_COMPILER_LINKER_RELATIVE_PATCHER_H_