blob: b7beb7bdb407fc1d81041d942979f3e296c7ad3a [file] [log] [blame]
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001/*
2 * Copyright (C) 2017 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_LINKER_PATCH_H_
18#define ART_COMPILER_LINKER_LINKER_PATCH_H_
19
20#include <iosfwd>
21#include <stdint.h>
22
Andreas Gampe57943812017-12-06 21:39:13 -080023#include <android-base/logging.h>
24
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010025#include "base/bit_utils.h"
David Sehr312f3b22018-03-19 08:39:26 -070026#include "dex/method_reference.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010027
28namespace art {
29
30class DexFile;
31
32namespace linker {
33
34class LinkerPatch {
35 public:
36 // Note: We explicitly specify the underlying type of the enum because GCC
37 // would otherwise select a bigger underlying type and then complain that
38 // 'art::LinkerPatch::patch_type_' is too small to hold all
39 // values of 'enum class art::LinkerPatch::Type'
40 // which is ridiculous given we have only a handful of values here. If we
41 // choose to squeeze the Type into fewer than 8 bits, we'll have to declare
42 // patch_type_ as an uintN_t and do explicit static_cast<>s.
Vladimir Marko6fd16062018-06-26 11:02:04 +010043 //
44 // Note: Actual patching is instruction_set-dependent.
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010045 enum class Type : uint8_t {
Vladimir Marko6fd16062018-06-26 11:02:04 +010046 kIntrinsicReference, // Boot image reference for an intrinsic, see IntrinsicObjects.
47 kDataBimgRelRo,
48 kMethodRelative,
49 kMethodBssEntry,
50 kCall, // TODO: Remove. (Deprecated, non-PIC.)
51 kCallRelative,
52 kTypeRelative,
53 kTypeBssEntry,
54 kStringRelative,
55 kStringBssEntry,
56 kBakerReadBarrierBranch,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010057 };
58
Vladimir Marko6fd16062018-06-26 11:02:04 +010059 static LinkerPatch IntrinsicReferencePatch(size_t literal_offset,
60 uint32_t pc_insn_offset,
61 uint32_t intrinsic_data) {
62 LinkerPatch patch(literal_offset, Type::kIntrinsicReference, /* target_dex_file */ nullptr);
63 patch.intrinsic_data_ = intrinsic_data;
64 patch.pc_insn_offset_ = pc_insn_offset;
65 return patch;
66 }
67
Vladimir Markob066d432018-01-03 13:14:37 +000068 static LinkerPatch DataBimgRelRoPatch(size_t literal_offset,
69 uint32_t pc_insn_offset,
70 uint32_t boot_image_offset) {
71 LinkerPatch patch(literal_offset, Type::kDataBimgRelRo, /* target_dex_file */ nullptr);
72 patch.boot_image_offset_ = boot_image_offset;
73 patch.pc_insn_offset_ = pc_insn_offset;
74 return patch;
75 }
76
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010077 static LinkerPatch RelativeMethodPatch(size_t literal_offset,
78 const DexFile* target_dex_file,
79 uint32_t pc_insn_offset,
80 uint32_t target_method_idx) {
81 LinkerPatch patch(literal_offset, Type::kMethodRelative, target_dex_file);
82 patch.method_idx_ = target_method_idx;
83 patch.pc_insn_offset_ = pc_insn_offset;
84 return patch;
85 }
86
87 static LinkerPatch MethodBssEntryPatch(size_t literal_offset,
88 const DexFile* target_dex_file,
89 uint32_t pc_insn_offset,
90 uint32_t target_method_idx) {
91 LinkerPatch patch(literal_offset, Type::kMethodBssEntry, target_dex_file);
92 patch.method_idx_ = target_method_idx;
93 patch.pc_insn_offset_ = pc_insn_offset;
94 return patch;
95 }
96
97 static LinkerPatch CodePatch(size_t literal_offset,
98 const DexFile* target_dex_file,
99 uint32_t target_method_idx) {
100 LinkerPatch patch(literal_offset, Type::kCall, target_dex_file);
101 patch.method_idx_ = target_method_idx;
102 return patch;
103 }
104
105 static LinkerPatch RelativeCodePatch(size_t literal_offset,
106 const DexFile* target_dex_file,
107 uint32_t target_method_idx) {
108 LinkerPatch patch(literal_offset, Type::kCallRelative, target_dex_file);
109 patch.method_idx_ = target_method_idx;
110 return patch;
111 }
112
113 static LinkerPatch RelativeTypePatch(size_t literal_offset,
114 const DexFile* target_dex_file,
115 uint32_t pc_insn_offset,
116 uint32_t target_type_idx) {
117 LinkerPatch patch(literal_offset, Type::kTypeRelative, target_dex_file);
118 patch.type_idx_ = target_type_idx;
119 patch.pc_insn_offset_ = pc_insn_offset;
120 return patch;
121 }
122
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100123 static LinkerPatch TypeBssEntryPatch(size_t literal_offset,
124 const DexFile* target_dex_file,
125 uint32_t pc_insn_offset,
126 uint32_t target_type_idx) {
127 LinkerPatch patch(literal_offset, Type::kTypeBssEntry, target_dex_file);
128 patch.type_idx_ = target_type_idx;
129 patch.pc_insn_offset_ = pc_insn_offset;
130 return patch;
131 }
132
133 static LinkerPatch RelativeStringPatch(size_t literal_offset,
134 const DexFile* target_dex_file,
135 uint32_t pc_insn_offset,
136 uint32_t target_string_idx) {
137 LinkerPatch patch(literal_offset, Type::kStringRelative, target_dex_file);
138 patch.string_idx_ = target_string_idx;
139 patch.pc_insn_offset_ = pc_insn_offset;
140 return patch;
141 }
142
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100143 static LinkerPatch StringBssEntryPatch(size_t literal_offset,
144 const DexFile* target_dex_file,
145 uint32_t pc_insn_offset,
146 uint32_t target_string_idx) {
147 LinkerPatch patch(literal_offset, Type::kStringBssEntry, target_dex_file);
148 patch.string_idx_ = target_string_idx;
149 patch.pc_insn_offset_ = pc_insn_offset;
150 return patch;
151 }
152
153 static LinkerPatch BakerReadBarrierBranchPatch(size_t literal_offset,
154 uint32_t custom_value1 = 0u,
155 uint32_t custom_value2 = 0u) {
Vladimir Markoca1e0382018-04-11 09:58:41 +0000156 LinkerPatch patch(literal_offset, Type::kBakerReadBarrierBranch, /* target_dex_file */ nullptr);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100157 patch.baker_custom_value1_ = custom_value1;
158 patch.baker_custom_value2_ = custom_value2;
159 return patch;
160 }
161
162 LinkerPatch(const LinkerPatch& other) = default;
163 LinkerPatch& operator=(const LinkerPatch& other) = default;
164
165 size_t LiteralOffset() const {
166 return literal_offset_;
167 }
168
169 Type GetType() const {
170 return patch_type_;
171 }
172
173 bool IsPcRelative() const {
174 switch (GetType()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +0100175 case Type::kIntrinsicReference:
Vladimir Markob066d432018-01-03 13:14:37 +0000176 case Type::kDataBimgRelRo:
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100177 case Type::kMethodRelative:
178 case Type::kMethodBssEntry:
179 case Type::kCallRelative:
180 case Type::kTypeRelative:
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100181 case Type::kTypeBssEntry:
182 case Type::kStringRelative:
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100183 case Type::kStringBssEntry:
184 case Type::kBakerReadBarrierBranch:
185 return true;
186 default:
187 return false;
188 }
189 }
190
Vladimir Marko6fd16062018-06-26 11:02:04 +0100191 uint32_t IntrinsicData() const {
192 DCHECK(patch_type_ == Type::kIntrinsicReference);
193 return intrinsic_data_;
194 }
195
Vladimir Markob066d432018-01-03 13:14:37 +0000196 uint32_t BootImageOffset() const {
197 DCHECK(patch_type_ == Type::kDataBimgRelRo);
198 return boot_image_offset_;
199 }
200
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100201 MethodReference TargetMethod() const {
202 DCHECK(patch_type_ == Type::kMethodRelative ||
203 patch_type_ == Type::kMethodBssEntry ||
204 patch_type_ == Type::kCall ||
205 patch_type_ == Type::kCallRelative);
206 return MethodReference(target_dex_file_, method_idx_);
207 }
208
209 const DexFile* TargetTypeDexFile() const {
210 DCHECK(patch_type_ == Type::kTypeRelative ||
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100211 patch_type_ == Type::kTypeBssEntry);
212 return target_dex_file_;
213 }
214
215 dex::TypeIndex TargetTypeIndex() const {
216 DCHECK(patch_type_ == Type::kTypeRelative ||
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100217 patch_type_ == Type::kTypeBssEntry);
218 return dex::TypeIndex(type_idx_);
219 }
220
221 const DexFile* TargetStringDexFile() const {
222 DCHECK(patch_type_ == Type::kStringRelative ||
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100223 patch_type_ == Type::kStringBssEntry);
224 return target_dex_file_;
225 }
226
227 dex::StringIndex TargetStringIndex() const {
228 DCHECK(patch_type_ == Type::kStringRelative ||
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100229 patch_type_ == Type::kStringBssEntry);
230 return dex::StringIndex(string_idx_);
231 }
232
233 uint32_t PcInsnOffset() const {
Vladimir Marko6fd16062018-06-26 11:02:04 +0100234 DCHECK(patch_type_ == Type::kIntrinsicReference ||
235 patch_type_ == Type::kDataBimgRelRo ||
Vladimir Markob066d432018-01-03 13:14:37 +0000236 patch_type_ == Type::kMethodRelative ||
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100237 patch_type_ == Type::kMethodBssEntry ||
238 patch_type_ == Type::kTypeRelative ||
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100239 patch_type_ == Type::kTypeBssEntry ||
240 patch_type_ == Type::kStringRelative ||
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100241 patch_type_ == Type::kStringBssEntry);
242 return pc_insn_offset_;
243 }
244
245 uint32_t GetBakerCustomValue1() const {
246 DCHECK(patch_type_ == Type::kBakerReadBarrierBranch);
247 return baker_custom_value1_;
248 }
249
250 uint32_t GetBakerCustomValue2() const {
251 DCHECK(patch_type_ == Type::kBakerReadBarrierBranch);
252 return baker_custom_value2_;
253 }
254
255 private:
256 LinkerPatch(size_t literal_offset, Type patch_type, const DexFile* target_dex_file)
257 : target_dex_file_(target_dex_file),
258 literal_offset_(literal_offset),
259 patch_type_(patch_type) {
260 cmp1_ = 0u;
261 cmp2_ = 0u;
262 // The compiler rejects methods that are too big, so the compiled code
263 // of a single method really shouln't be anywhere close to 16MiB.
264 DCHECK(IsUint<24>(literal_offset));
265 }
266
267 const DexFile* target_dex_file_;
268 // TODO: Clean up naming. Some patched locations are literals but others are not.
269 uint32_t literal_offset_ : 24; // Method code size up to 16MiB.
270 Type patch_type_ : 8;
271 union {
Vladimir Markob066d432018-01-03 13:14:37 +0000272 uint32_t cmp1_; // Used for relational operators.
273 uint32_t boot_image_offset_; // Data to write to the .data.bimg.rel.ro entry.
274 uint32_t method_idx_; // Method index for Call/Method patches.
275 uint32_t type_idx_; // Type index for Type patches.
276 uint32_t string_idx_; // String index for String patches.
Vladimir Marko6fd16062018-06-26 11:02:04 +0100277 uint32_t intrinsic_data_; // Data for IntrinsicObjects.
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100278 uint32_t baker_custom_value1_;
279 static_assert(sizeof(method_idx_) == sizeof(cmp1_), "needed by relational operators");
280 static_assert(sizeof(type_idx_) == sizeof(cmp1_), "needed by relational operators");
281 static_assert(sizeof(string_idx_) == sizeof(cmp1_), "needed by relational operators");
Vladimir Marko6fd16062018-06-26 11:02:04 +0100282 static_assert(sizeof(intrinsic_data_) == sizeof(cmp1_), "needed by relational operators");
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100283 static_assert(sizeof(baker_custom_value1_) == sizeof(cmp1_), "needed by relational operators");
284 };
285 union {
286 // Note: To avoid uninitialized padding on 64-bit systems, we use `size_t` for `cmp2_`.
287 // This allows a hashing function to treat an array of linker patches as raw memory.
288 size_t cmp2_; // Used for relational operators.
289 // Literal offset of the insn loading PC (same as literal_offset if it's the same insn,
290 // may be different if the PC-relative addressing needs multiple insns).
291 uint32_t pc_insn_offset_;
292 uint32_t baker_custom_value2_;
293 static_assert(sizeof(pc_insn_offset_) <= sizeof(cmp2_), "needed by relational operators");
294 static_assert(sizeof(baker_custom_value2_) <= sizeof(cmp2_), "needed by relational operators");
295 };
296
297 friend bool operator==(const LinkerPatch& lhs, const LinkerPatch& rhs);
298 friend bool operator<(const LinkerPatch& lhs, const LinkerPatch& rhs);
299};
300std::ostream& operator<<(std::ostream& os, const LinkerPatch::Type& type);
301
302inline bool operator==(const LinkerPatch& lhs, const LinkerPatch& rhs) {
303 return lhs.literal_offset_ == rhs.literal_offset_ &&
304 lhs.patch_type_ == rhs.patch_type_ &&
305 lhs.target_dex_file_ == rhs.target_dex_file_ &&
306 lhs.cmp1_ == rhs.cmp1_ &&
307 lhs.cmp2_ == rhs.cmp2_;
308}
309
310inline bool operator<(const LinkerPatch& lhs, const LinkerPatch& rhs) {
311 return (lhs.literal_offset_ != rhs.literal_offset_) ? lhs.literal_offset_ < rhs.literal_offset_
312 : (lhs.patch_type_ != rhs.patch_type_) ? lhs.patch_type_ < rhs.patch_type_
313 : (lhs.target_dex_file_ != rhs.target_dex_file_) ? lhs.target_dex_file_ < rhs.target_dex_file_
314 : (lhs.cmp1_ != rhs.cmp1_) ? lhs.cmp1_ < rhs.cmp1_
315 : lhs.cmp2_ < rhs.cmp2_;
316}
317
318} // namespace linker
319} // namespace art
320
321#endif // ART_COMPILER_LINKER_LINKER_PATCH_H_