Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 1 | /* |
| 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 Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 23 | #include <android-base/logging.h> |
| 24 | |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 25 | #include "base/bit_utils.h" |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 26 | #include "method_reference.h" |
| 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | class DexFile; |
| 31 | |
| 32 | namespace linker { |
| 33 | |
| 34 | class 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. |
| 43 | enum class Type : uint8_t { |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 44 | kDataBimgRelRo, // NOTE: Actual patching is instruction_set-dependent. |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 45 | kMethodRelative, // NOTE: Actual patching is instruction_set-dependent. |
| 46 | kMethodBssEntry, // NOTE: Actual patching is instruction_set-dependent. |
| 47 | kCall, |
| 48 | kCallRelative, // NOTE: Actual patching is instruction_set-dependent. |
| 49 | kTypeRelative, // NOTE: Actual patching is instruction_set-dependent. |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 50 | kTypeBssEntry, // NOTE: Actual patching is instruction_set-dependent. |
| 51 | kStringRelative, // NOTE: Actual patching is instruction_set-dependent. |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 52 | kStringBssEntry, // NOTE: Actual patching is instruction_set-dependent. |
| 53 | kBakerReadBarrierBranch, // NOTE: Actual patching is instruction_set-dependent. |
| 54 | }; |
| 55 | |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 56 | static LinkerPatch DataBimgRelRoPatch(size_t literal_offset, |
| 57 | uint32_t pc_insn_offset, |
| 58 | uint32_t boot_image_offset) { |
| 59 | LinkerPatch patch(literal_offset, Type::kDataBimgRelRo, /* target_dex_file */ nullptr); |
| 60 | patch.boot_image_offset_ = boot_image_offset; |
| 61 | patch.pc_insn_offset_ = pc_insn_offset; |
| 62 | return patch; |
| 63 | } |
| 64 | |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 65 | static LinkerPatch RelativeMethodPatch(size_t literal_offset, |
| 66 | const DexFile* target_dex_file, |
| 67 | uint32_t pc_insn_offset, |
| 68 | uint32_t target_method_idx) { |
| 69 | LinkerPatch patch(literal_offset, Type::kMethodRelative, target_dex_file); |
| 70 | patch.method_idx_ = target_method_idx; |
| 71 | patch.pc_insn_offset_ = pc_insn_offset; |
| 72 | return patch; |
| 73 | } |
| 74 | |
| 75 | static LinkerPatch MethodBssEntryPatch(size_t literal_offset, |
| 76 | const DexFile* target_dex_file, |
| 77 | uint32_t pc_insn_offset, |
| 78 | uint32_t target_method_idx) { |
| 79 | LinkerPatch patch(literal_offset, Type::kMethodBssEntry, target_dex_file); |
| 80 | patch.method_idx_ = target_method_idx; |
| 81 | patch.pc_insn_offset_ = pc_insn_offset; |
| 82 | return patch; |
| 83 | } |
| 84 | |
| 85 | static LinkerPatch CodePatch(size_t literal_offset, |
| 86 | const DexFile* target_dex_file, |
| 87 | uint32_t target_method_idx) { |
| 88 | LinkerPatch patch(literal_offset, Type::kCall, target_dex_file); |
| 89 | patch.method_idx_ = target_method_idx; |
| 90 | return patch; |
| 91 | } |
| 92 | |
| 93 | static LinkerPatch RelativeCodePatch(size_t literal_offset, |
| 94 | const DexFile* target_dex_file, |
| 95 | uint32_t target_method_idx) { |
| 96 | LinkerPatch patch(literal_offset, Type::kCallRelative, target_dex_file); |
| 97 | patch.method_idx_ = target_method_idx; |
| 98 | return patch; |
| 99 | } |
| 100 | |
| 101 | static LinkerPatch RelativeTypePatch(size_t literal_offset, |
| 102 | const DexFile* target_dex_file, |
| 103 | uint32_t pc_insn_offset, |
| 104 | uint32_t target_type_idx) { |
| 105 | LinkerPatch patch(literal_offset, Type::kTypeRelative, target_dex_file); |
| 106 | patch.type_idx_ = target_type_idx; |
| 107 | patch.pc_insn_offset_ = pc_insn_offset; |
| 108 | return patch; |
| 109 | } |
| 110 | |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 111 | static LinkerPatch TypeBssEntryPatch(size_t literal_offset, |
| 112 | const DexFile* target_dex_file, |
| 113 | uint32_t pc_insn_offset, |
| 114 | uint32_t target_type_idx) { |
| 115 | LinkerPatch patch(literal_offset, Type::kTypeBssEntry, target_dex_file); |
| 116 | patch.type_idx_ = target_type_idx; |
| 117 | patch.pc_insn_offset_ = pc_insn_offset; |
| 118 | return patch; |
| 119 | } |
| 120 | |
| 121 | static LinkerPatch RelativeStringPatch(size_t literal_offset, |
| 122 | const DexFile* target_dex_file, |
| 123 | uint32_t pc_insn_offset, |
| 124 | uint32_t target_string_idx) { |
| 125 | LinkerPatch patch(literal_offset, Type::kStringRelative, target_dex_file); |
| 126 | patch.string_idx_ = target_string_idx; |
| 127 | patch.pc_insn_offset_ = pc_insn_offset; |
| 128 | return patch; |
| 129 | } |
| 130 | |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 131 | static LinkerPatch StringBssEntryPatch(size_t literal_offset, |
| 132 | const DexFile* target_dex_file, |
| 133 | uint32_t pc_insn_offset, |
| 134 | uint32_t target_string_idx) { |
| 135 | LinkerPatch patch(literal_offset, Type::kStringBssEntry, target_dex_file); |
| 136 | patch.string_idx_ = target_string_idx; |
| 137 | patch.pc_insn_offset_ = pc_insn_offset; |
| 138 | return patch; |
| 139 | } |
| 140 | |
| 141 | static LinkerPatch BakerReadBarrierBranchPatch(size_t literal_offset, |
| 142 | uint32_t custom_value1 = 0u, |
| 143 | uint32_t custom_value2 = 0u) { |
| 144 | LinkerPatch patch(literal_offset, Type::kBakerReadBarrierBranch, nullptr); |
| 145 | patch.baker_custom_value1_ = custom_value1; |
| 146 | patch.baker_custom_value2_ = custom_value2; |
| 147 | return patch; |
| 148 | } |
| 149 | |
| 150 | LinkerPatch(const LinkerPatch& other) = default; |
| 151 | LinkerPatch& operator=(const LinkerPatch& other) = default; |
| 152 | |
| 153 | size_t LiteralOffset() const { |
| 154 | return literal_offset_; |
| 155 | } |
| 156 | |
| 157 | Type GetType() const { |
| 158 | return patch_type_; |
| 159 | } |
| 160 | |
| 161 | bool IsPcRelative() const { |
| 162 | switch (GetType()) { |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 163 | case Type::kDataBimgRelRo: |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 164 | case Type::kMethodRelative: |
| 165 | case Type::kMethodBssEntry: |
| 166 | case Type::kCallRelative: |
| 167 | case Type::kTypeRelative: |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 168 | case Type::kTypeBssEntry: |
| 169 | case Type::kStringRelative: |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 170 | case Type::kStringBssEntry: |
| 171 | case Type::kBakerReadBarrierBranch: |
| 172 | return true; |
| 173 | default: |
| 174 | return false; |
| 175 | } |
| 176 | } |
| 177 | |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 178 | uint32_t BootImageOffset() const { |
| 179 | DCHECK(patch_type_ == Type::kDataBimgRelRo); |
| 180 | return boot_image_offset_; |
| 181 | } |
| 182 | |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 183 | MethodReference TargetMethod() const { |
| 184 | DCHECK(patch_type_ == Type::kMethodRelative || |
| 185 | patch_type_ == Type::kMethodBssEntry || |
| 186 | patch_type_ == Type::kCall || |
| 187 | patch_type_ == Type::kCallRelative); |
| 188 | return MethodReference(target_dex_file_, method_idx_); |
| 189 | } |
| 190 | |
| 191 | const DexFile* TargetTypeDexFile() const { |
| 192 | DCHECK(patch_type_ == Type::kTypeRelative || |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 193 | patch_type_ == Type::kTypeBssEntry); |
| 194 | return target_dex_file_; |
| 195 | } |
| 196 | |
| 197 | dex::TypeIndex TargetTypeIndex() const { |
| 198 | DCHECK(patch_type_ == Type::kTypeRelative || |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 199 | patch_type_ == Type::kTypeBssEntry); |
| 200 | return dex::TypeIndex(type_idx_); |
| 201 | } |
| 202 | |
| 203 | const DexFile* TargetStringDexFile() const { |
| 204 | DCHECK(patch_type_ == Type::kStringRelative || |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 205 | patch_type_ == Type::kStringBssEntry); |
| 206 | return target_dex_file_; |
| 207 | } |
| 208 | |
| 209 | dex::StringIndex TargetStringIndex() const { |
| 210 | DCHECK(patch_type_ == Type::kStringRelative || |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 211 | patch_type_ == Type::kStringBssEntry); |
| 212 | return dex::StringIndex(string_idx_); |
| 213 | } |
| 214 | |
| 215 | uint32_t PcInsnOffset() const { |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 216 | DCHECK(patch_type_ == Type::kDataBimgRelRo || |
| 217 | patch_type_ == Type::kMethodRelative || |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 218 | patch_type_ == Type::kMethodBssEntry || |
| 219 | patch_type_ == Type::kTypeRelative || |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 220 | patch_type_ == Type::kTypeBssEntry || |
| 221 | patch_type_ == Type::kStringRelative || |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 222 | patch_type_ == Type::kStringBssEntry); |
| 223 | return pc_insn_offset_; |
| 224 | } |
| 225 | |
| 226 | uint32_t GetBakerCustomValue1() const { |
| 227 | DCHECK(patch_type_ == Type::kBakerReadBarrierBranch); |
| 228 | return baker_custom_value1_; |
| 229 | } |
| 230 | |
| 231 | uint32_t GetBakerCustomValue2() const { |
| 232 | DCHECK(patch_type_ == Type::kBakerReadBarrierBranch); |
| 233 | return baker_custom_value2_; |
| 234 | } |
| 235 | |
| 236 | private: |
| 237 | LinkerPatch(size_t literal_offset, Type patch_type, const DexFile* target_dex_file) |
| 238 | : target_dex_file_(target_dex_file), |
| 239 | literal_offset_(literal_offset), |
| 240 | patch_type_(patch_type) { |
| 241 | cmp1_ = 0u; |
| 242 | cmp2_ = 0u; |
| 243 | // The compiler rejects methods that are too big, so the compiled code |
| 244 | // of a single method really shouln't be anywhere close to 16MiB. |
| 245 | DCHECK(IsUint<24>(literal_offset)); |
| 246 | } |
| 247 | |
| 248 | const DexFile* target_dex_file_; |
| 249 | // TODO: Clean up naming. Some patched locations are literals but others are not. |
| 250 | uint32_t literal_offset_ : 24; // Method code size up to 16MiB. |
| 251 | Type patch_type_ : 8; |
| 252 | union { |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 253 | uint32_t cmp1_; // Used for relational operators. |
| 254 | uint32_t boot_image_offset_; // Data to write to the .data.bimg.rel.ro entry. |
| 255 | uint32_t method_idx_; // Method index for Call/Method patches. |
| 256 | uint32_t type_idx_; // Type index for Type patches. |
| 257 | uint32_t string_idx_; // String index for String patches. |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 258 | uint32_t baker_custom_value1_; |
| 259 | static_assert(sizeof(method_idx_) == sizeof(cmp1_), "needed by relational operators"); |
| 260 | static_assert(sizeof(type_idx_) == sizeof(cmp1_), "needed by relational operators"); |
| 261 | static_assert(sizeof(string_idx_) == sizeof(cmp1_), "needed by relational operators"); |
| 262 | static_assert(sizeof(baker_custom_value1_) == sizeof(cmp1_), "needed by relational operators"); |
| 263 | }; |
| 264 | union { |
| 265 | // Note: To avoid uninitialized padding on 64-bit systems, we use `size_t` for `cmp2_`. |
| 266 | // This allows a hashing function to treat an array of linker patches as raw memory. |
| 267 | size_t cmp2_; // Used for relational operators. |
| 268 | // Literal offset of the insn loading PC (same as literal_offset if it's the same insn, |
| 269 | // may be different if the PC-relative addressing needs multiple insns). |
| 270 | uint32_t pc_insn_offset_; |
| 271 | uint32_t baker_custom_value2_; |
| 272 | static_assert(sizeof(pc_insn_offset_) <= sizeof(cmp2_), "needed by relational operators"); |
| 273 | static_assert(sizeof(baker_custom_value2_) <= sizeof(cmp2_), "needed by relational operators"); |
| 274 | }; |
| 275 | |
| 276 | friend bool operator==(const LinkerPatch& lhs, const LinkerPatch& rhs); |
| 277 | friend bool operator<(const LinkerPatch& lhs, const LinkerPatch& rhs); |
| 278 | }; |
| 279 | std::ostream& operator<<(std::ostream& os, const LinkerPatch::Type& type); |
| 280 | |
| 281 | inline bool operator==(const LinkerPatch& lhs, const LinkerPatch& rhs) { |
| 282 | return lhs.literal_offset_ == rhs.literal_offset_ && |
| 283 | lhs.patch_type_ == rhs.patch_type_ && |
| 284 | lhs.target_dex_file_ == rhs.target_dex_file_ && |
| 285 | lhs.cmp1_ == rhs.cmp1_ && |
| 286 | lhs.cmp2_ == rhs.cmp2_; |
| 287 | } |
| 288 | |
| 289 | inline bool operator<(const LinkerPatch& lhs, const LinkerPatch& rhs) { |
| 290 | return (lhs.literal_offset_ != rhs.literal_offset_) ? lhs.literal_offset_ < rhs.literal_offset_ |
| 291 | : (lhs.patch_type_ != rhs.patch_type_) ? lhs.patch_type_ < rhs.patch_type_ |
| 292 | : (lhs.target_dex_file_ != rhs.target_dex_file_) ? lhs.target_dex_file_ < rhs.target_dex_file_ |
| 293 | : (lhs.cmp1_ != rhs.cmp1_) ? lhs.cmp1_ < rhs.cmp1_ |
| 294 | : lhs.cmp2_ < rhs.cmp2_; |
| 295 | } |
| 296 | |
| 297 | } // namespace linker |
| 298 | } // namespace art |
| 299 | |
| 300 | #endif // ART_COMPILER_LINKER_LINKER_PATCH_H_ |