Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | */ |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 16 | |
| 17 | #include "oat_writer.h" |
| 18 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 19 | #include <zlib.h> |
| 20 | |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 21 | #include "base/allocator.h" |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 22 | #include "base/bit_vector.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 23 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 24 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 25 | #include "class_linker.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 26 | #include "compiled_class.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 27 | #include "dex_file-inl.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 28 | #include "dex/verification_results.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 29 | #include "gc/space/space.h" |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 30 | #include "image_writer.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 31 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | #include "mirror/array.h" |
| 33 | #include "mirror/class_loader.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 34 | #include "mirror/object-inl.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 35 | #include "os.h" |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 36 | #include "output_stream.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 37 | #include "safe_map.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 38 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 39 | #include "handle_scope-inl.h" |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 40 | #include "utils/arm/assembler_thumb2.h" |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 41 | #include "utils/arm64/assembler_arm64.h" |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 42 | #include "verifier/method_verifier.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 43 | |
| 44 | namespace art { |
| 45 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 46 | class OatWriter::RelativeCallPatcher { |
| 47 | public: |
| 48 | virtual ~RelativeCallPatcher() { } |
| 49 | |
| 50 | // Reserve space for relative call thunks if needed, return adjusted offset. |
| 51 | // After all methods have been processed it's call one last time with compiled_method == nullptr. |
| 52 | virtual uint32_t ReserveSpace(uint32_t offset, const CompiledMethod* compiled_method) = 0; |
| 53 | |
| 54 | // Write relative call thunks if needed, return adjusted offset. |
| 55 | virtual uint32_t WriteThunks(OutputStream* out, uint32_t offset) = 0; |
| 56 | |
| 57 | // Patch method code. The input displacement is relative to the patched location, |
| 58 | // the patcher may need to adjust it if the correct base is different. |
| 59 | virtual void Patch(std::vector<uint8_t>* code, uint32_t literal_offset, uint32_t patch_offset, |
| 60 | uint32_t target_offset) = 0; |
| 61 | |
| 62 | protected: |
| 63 | RelativeCallPatcher() { } |
| 64 | |
| 65 | private: |
| 66 | DISALLOW_COPY_AND_ASSIGN(RelativeCallPatcher); |
| 67 | }; |
| 68 | |
| 69 | class OatWriter::NoRelativeCallPatcher FINAL : public RelativeCallPatcher { |
| 70 | public: |
| 71 | NoRelativeCallPatcher() { } |
| 72 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame^] | 73 | uint32_t ReserveSpace(uint32_t offset, |
| 74 | const CompiledMethod* compiled_method ATTRIBUTE_UNUSED) OVERRIDE { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 75 | return offset; // No space reserved; no patches expected. |
| 76 | } |
| 77 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame^] | 78 | uint32_t WriteThunks(OutputStream* out ATTRIBUTE_UNUSED, uint32_t offset) OVERRIDE { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 79 | return offset; // No thunks added; no patches expected. |
| 80 | } |
| 81 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame^] | 82 | void Patch(std::vector<uint8_t>* code ATTRIBUTE_UNUSED, uint32_t literal_offset ATTRIBUTE_UNUSED, |
| 83 | uint32_t patch_offset ATTRIBUTE_UNUSED, |
| 84 | uint32_t target_offset ATTRIBUTE_UNUSED) OVERRIDE { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 85 | LOG(FATAL) << "Unexpected relative patch."; |
| 86 | } |
| 87 | |
| 88 | private: |
| 89 | DISALLOW_COPY_AND_ASSIGN(NoRelativeCallPatcher); |
| 90 | }; |
| 91 | |
| 92 | class OatWriter::X86RelativeCallPatcher FINAL : public RelativeCallPatcher { |
| 93 | public: |
| 94 | X86RelativeCallPatcher() { } |
| 95 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame^] | 96 | uint32_t ReserveSpace(uint32_t offset, |
| 97 | const CompiledMethod* compiled_method ATTRIBUTE_UNUSED) OVERRIDE { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 98 | return offset; // No space reserved; no limit on relative call distance. |
| 99 | } |
| 100 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame^] | 101 | uint32_t WriteThunks(OutputStream* out ATTRIBUTE_UNUSED, uint32_t offset) OVERRIDE { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 102 | return offset; // No thunks added; no limit on relative call distance. |
| 103 | } |
| 104 | |
| 105 | void Patch(std::vector<uint8_t>* code, uint32_t literal_offset, uint32_t patch_offset, |
| 106 | uint32_t target_offset) OVERRIDE { |
| 107 | DCHECK_LE(literal_offset + 4u, code->size()); |
| 108 | // Unsigned arithmetic with its well-defined overflow behavior is just fine here. |
| 109 | uint32_t displacement = target_offset - patch_offset; |
| 110 | displacement -= kPcDisplacement; // The base PC is at the end of the 4-byte patch. |
| 111 | |
| 112 | typedef __attribute__((__aligned__(1))) int32_t unaligned_int32_t; |
| 113 | reinterpret_cast<unaligned_int32_t*>(&(*code)[literal_offset])[0] = displacement; |
| 114 | } |
| 115 | |
| 116 | private: |
| 117 | // PC displacement from patch location; x86 PC for relative calls points to the next |
| 118 | // instruction and the patch location is 4 bytes earlier. |
| 119 | static constexpr int32_t kPcDisplacement = 4; |
| 120 | |
| 121 | DISALLOW_COPY_AND_ASSIGN(X86RelativeCallPatcher); |
| 122 | }; |
| 123 | |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 124 | class OatWriter::ArmBaseRelativeCallPatcher : public RelativeCallPatcher { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 125 | public: |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 126 | ArmBaseRelativeCallPatcher(OatWriter* writer, |
| 127 | InstructionSet instruction_set, std::vector<uint8_t> thunk_code, |
| 128 | uint32_t max_positive_displacement, uint32_t max_negative_displacement) |
| 129 | : writer_(writer), instruction_set_(instruction_set), thunk_code_(thunk_code), |
| 130 | max_positive_displacement_(max_positive_displacement), |
| 131 | max_negative_displacement_(max_negative_displacement), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 132 | thunk_locations_(), current_thunk_to_write_(0u), unprocessed_patches_() { |
| 133 | } |
| 134 | |
| 135 | uint32_t ReserveSpace(uint32_t offset, const CompiledMethod* compiled_method) OVERRIDE { |
| 136 | // NOTE: The final thunk can be reserved from InitCodeMethodVisitor::EndClass() while it |
| 137 | // may be written early by WriteCodeMethodVisitor::VisitMethod() for a deduplicated chunk |
| 138 | // of code. To avoid any alignment discrepancies for the final chunk, we always align the |
| 139 | // offset after reserving of writing any chunk. |
| 140 | if (UNLIKELY(compiled_method == nullptr)) { |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 141 | uint32_t aligned_offset = CompiledMethod::AlignCode(offset, instruction_set_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 142 | bool needs_thunk = ReserveSpaceProcessPatches(aligned_offset); |
| 143 | if (needs_thunk) { |
| 144 | thunk_locations_.push_back(aligned_offset); |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 145 | offset = CompiledMethod::AlignCode(aligned_offset + thunk_code_.size(), instruction_set_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 146 | } |
| 147 | return offset; |
| 148 | } |
| 149 | DCHECK(compiled_method->GetQuickCode() != nullptr); |
| 150 | uint32_t quick_code_size = compiled_method->GetQuickCode()->size(); |
| 151 | uint32_t quick_code_offset = compiled_method->AlignCode(offset) + sizeof(OatQuickMethodHeader); |
| 152 | uint32_t next_aligned_offset = compiled_method->AlignCode(quick_code_offset + quick_code_size); |
| 153 | if (!unprocessed_patches_.empty() && |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 154 | next_aligned_offset - unprocessed_patches_.front().second > max_positive_displacement_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 155 | bool needs_thunk = ReserveSpaceProcessPatches(next_aligned_offset); |
| 156 | if (needs_thunk) { |
| 157 | // A single thunk will cover all pending patches. |
| 158 | unprocessed_patches_.clear(); |
| 159 | uint32_t thunk_location = compiled_method->AlignCode(offset); |
| 160 | thunk_locations_.push_back(thunk_location); |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 161 | offset = CompiledMethod::AlignCode(thunk_location + thunk_code_.size(), instruction_set_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | for (const LinkerPatch& patch : compiled_method->GetPatches()) { |
| 165 | if (patch.Type() == kLinkerPatchCallRelative) { |
| 166 | unprocessed_patches_.emplace_back(patch.TargetMethod(), |
| 167 | quick_code_offset + patch.LiteralOffset()); |
| 168 | } |
| 169 | } |
| 170 | return offset; |
| 171 | } |
| 172 | |
| 173 | uint32_t WriteThunks(OutputStream* out, uint32_t offset) OVERRIDE { |
| 174 | if (current_thunk_to_write_ == thunk_locations_.size()) { |
| 175 | return offset; |
| 176 | } |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 177 | uint32_t aligned_offset = CompiledMethod::AlignCode(offset, instruction_set_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 178 | if (UNLIKELY(aligned_offset == thunk_locations_[current_thunk_to_write_])) { |
| 179 | ++current_thunk_to_write_; |
| 180 | uint32_t aligned_code_delta = aligned_offset - offset; |
| 181 | if (aligned_code_delta != 0u && !writer_->WriteCodeAlignment(out, aligned_code_delta)) { |
| 182 | return 0u; |
| 183 | } |
| 184 | if (!out->WriteFully(thunk_code_.data(), thunk_code_.size())) { |
| 185 | return 0u; |
| 186 | } |
| 187 | writer_->size_relative_call_thunks_ += thunk_code_.size(); |
| 188 | uint32_t thunk_end_offset = aligned_offset + thunk_code_.size(); |
| 189 | // Align after writing chunk, see the ReserveSpace() above. |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 190 | offset = CompiledMethod::AlignCode(thunk_end_offset, instruction_set_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 191 | aligned_code_delta = offset - thunk_end_offset; |
| 192 | if (aligned_code_delta != 0u && !writer_->WriteCodeAlignment(out, aligned_code_delta)) { |
| 193 | return 0u; |
| 194 | } |
| 195 | } |
| 196 | return offset; |
| 197 | } |
| 198 | |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 199 | protected: |
| 200 | uint32_t CalculateDisplacement(uint32_t patch_offset, uint32_t target_offset) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 201 | // Unsigned arithmetic with its well-defined overflow behavior is just fine here. |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 202 | uint32_t displacement = target_offset - patch_offset; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 203 | // NOTE: With unsigned arithmetic we do mean to use && rather than || below. |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 204 | if (displacement > max_positive_displacement_ && displacement < -max_negative_displacement_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 205 | // Unwritten thunks have higher offsets, check if it's within range. |
| 206 | DCHECK(current_thunk_to_write_ == thunk_locations_.size() || |
| 207 | thunk_locations_[current_thunk_to_write_] > patch_offset); |
| 208 | if (current_thunk_to_write_ != thunk_locations_.size() && |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 209 | thunk_locations_[current_thunk_to_write_] - patch_offset < max_positive_displacement_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 210 | displacement = thunk_locations_[current_thunk_to_write_] - patch_offset; |
| 211 | } else { |
| 212 | // We must have a previous thunk then. |
| 213 | DCHECK_NE(current_thunk_to_write_, 0u); |
| 214 | DCHECK_LT(thunk_locations_[current_thunk_to_write_ - 1], patch_offset); |
| 215 | displacement = thunk_locations_[current_thunk_to_write_ - 1] - patch_offset; |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 216 | DCHECK(displacement >= -max_negative_displacement_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 217 | } |
| 218 | } |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 219 | return displacement; |
| 220 | } |
| 221 | |
| 222 | private: |
| 223 | bool ReserveSpaceProcessPatches(uint32_t next_aligned_offset) { |
| 224 | // Process as many patches as possible, stop only on unresolved targets or calls too far back. |
| 225 | while (!unprocessed_patches_.empty()) { |
| 226 | uint32_t patch_offset = unprocessed_patches_.front().second; |
| 227 | auto it = writer_->method_offset_map_.find(unprocessed_patches_.front().first); |
| 228 | if (it == writer_->method_offset_map_.end()) { |
| 229 | // If still unresolved, check if we have a thunk within range. |
| 230 | DCHECK(thunk_locations_.empty() || thunk_locations_.back() <= patch_offset); |
| 231 | if (thunk_locations_.empty() || |
| 232 | patch_offset - thunk_locations_.back() > max_negative_displacement_) { |
| 233 | return next_aligned_offset - patch_offset > max_positive_displacement_; |
| 234 | } |
| 235 | } else if (it->second >= patch_offset) { |
| 236 | DCHECK_LE(it->second - patch_offset, max_positive_displacement_); |
| 237 | } else { |
| 238 | // When calling back, check if we have a thunk that's closer than the actual target. |
| 239 | uint32_t target_offset = (thunk_locations_.empty() || it->second > thunk_locations_.back()) |
| 240 | ? it->second |
| 241 | : thunk_locations_.back(); |
| 242 | DCHECK_GT(patch_offset, target_offset); |
| 243 | if (patch_offset - target_offset > max_negative_displacement_) { |
| 244 | return true; |
| 245 | } |
| 246 | } |
| 247 | unprocessed_patches_.pop_front(); |
| 248 | } |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | OatWriter* const writer_; |
| 253 | const InstructionSet instruction_set_; |
| 254 | const std::vector<uint8_t> thunk_code_; |
| 255 | const uint32_t max_positive_displacement_; |
| 256 | const uint32_t max_negative_displacement_; |
| 257 | std::vector<uint32_t> thunk_locations_; |
| 258 | size_t current_thunk_to_write_; |
| 259 | |
| 260 | // ReserveSpace() tracks unprocessed patches. |
| 261 | typedef std::pair<MethodReference, uint32_t> UnprocessedPatch; |
| 262 | std::deque<UnprocessedPatch> unprocessed_patches_; |
| 263 | |
| 264 | DISALLOW_COPY_AND_ASSIGN(ArmBaseRelativeCallPatcher); |
| 265 | }; |
| 266 | |
| 267 | class OatWriter::Thumb2RelativeCallPatcher FINAL : public ArmBaseRelativeCallPatcher { |
| 268 | public: |
| 269 | explicit Thumb2RelativeCallPatcher(OatWriter* writer) |
| 270 | : ArmBaseRelativeCallPatcher(writer, kThumb2, CompileThunkCode(), |
| 271 | kMaxPositiveDisplacement, kMaxNegativeDisplacement) { |
| 272 | } |
| 273 | |
| 274 | void Patch(std::vector<uint8_t>* code, uint32_t literal_offset, uint32_t patch_offset, |
| 275 | uint32_t target_offset) OVERRIDE { |
| 276 | DCHECK_LE(literal_offset + 4u, code->size()); |
| 277 | DCHECK_EQ(literal_offset & 1u, 0u); |
| 278 | DCHECK_EQ(patch_offset & 1u, 0u); |
| 279 | DCHECK_EQ(target_offset & 1u, 1u); // Thumb2 mode bit. |
| 280 | uint32_t displacement = CalculateDisplacement(patch_offset, target_offset & ~1u); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 281 | displacement -= kPcDisplacement; // The base PC is at the end of the 4-byte patch. |
| 282 | DCHECK_EQ(displacement & 1u, 0u); |
| 283 | DCHECK((displacement >> 24) == 0u || (displacement >> 24) == 255u); // 25-bit signed. |
| 284 | uint32_t signbit = (displacement >> 31) & 0x1; |
| 285 | uint32_t i1 = (displacement >> 23) & 0x1; |
| 286 | uint32_t i2 = (displacement >> 22) & 0x1; |
| 287 | uint32_t imm10 = (displacement >> 12) & 0x03ff; |
| 288 | uint32_t imm11 = (displacement >> 1) & 0x07ff; |
| 289 | uint32_t j1 = i1 ^ (signbit ^ 1); |
| 290 | uint32_t j2 = i2 ^ (signbit ^ 1); |
| 291 | uint32_t value = (signbit << 26) | (j1 << 13) | (j2 << 11) | (imm10 << 16) | imm11; |
| 292 | value |= 0xf000d000; // BL |
| 293 | |
| 294 | uint8_t* addr = &(*code)[literal_offset]; |
| 295 | // Check that we're just overwriting an existing BL. |
| 296 | DCHECK_EQ(addr[1] & 0xf8, 0xf0); |
| 297 | DCHECK_EQ(addr[3] & 0xd0, 0xd0); |
| 298 | // Write the new BL. |
| 299 | addr[0] = (value >> 16) & 0xff; |
| 300 | addr[1] = (value >> 24) & 0xff; |
| 301 | addr[2] = (value >> 0) & 0xff; |
| 302 | addr[3] = (value >> 8) & 0xff; |
| 303 | } |
| 304 | |
| 305 | private: |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 306 | static std::vector<uint8_t> CompileThunkCode() { |
| 307 | // The thunk just uses the entry point in the ArtMethod. This works even for calls |
| 308 | // to the generic JNI and interpreter trampolines. |
| 309 | arm::Thumb2Assembler assembler; |
| 310 | assembler.LoadFromOffset( |
| 311 | arm::kLoadWord, arm::PC, arm::R0, |
| 312 | mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()); |
| 313 | assembler.bkpt(0); |
| 314 | std::vector<uint8_t> thunk_code(assembler.CodeSize()); |
| 315 | MemoryRegion code(thunk_code.data(), thunk_code.size()); |
| 316 | assembler.FinalizeInstructions(code); |
| 317 | return thunk_code; |
| 318 | } |
| 319 | |
| 320 | // PC displacement from patch location; Thumb2 PC is always at instruction address + 4. |
| 321 | static constexpr int32_t kPcDisplacement = 4; |
| 322 | |
| 323 | // Maximum positive and negative displacement measured from the patch location. |
| 324 | // (Signed 25 bit displacement with the last bit 0 has range [-2^24, 2^24-2] measured from |
| 325 | // the Thumb2 PC pointing right after the BL, i.e. 4 bytes later than the patch location.) |
| 326 | static constexpr uint32_t kMaxPositiveDisplacement = (1u << 24) - 2 + kPcDisplacement; |
| 327 | static constexpr uint32_t kMaxNegativeDisplacement = (1u << 24) - kPcDisplacement; |
| 328 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 329 | DISALLOW_COPY_AND_ASSIGN(Thumb2RelativeCallPatcher); |
| 330 | }; |
| 331 | |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 332 | class OatWriter::Arm64RelativeCallPatcher FINAL : public ArmBaseRelativeCallPatcher { |
| 333 | public: |
| 334 | explicit Arm64RelativeCallPatcher(OatWriter* writer) |
| 335 | : ArmBaseRelativeCallPatcher(writer, kArm64, CompileThunkCode(), |
| 336 | kMaxPositiveDisplacement, kMaxNegativeDisplacement) { |
| 337 | } |
| 338 | |
| 339 | void Patch(std::vector<uint8_t>* code, uint32_t literal_offset, uint32_t patch_offset, |
| 340 | uint32_t target_offset) OVERRIDE { |
| 341 | DCHECK_LE(literal_offset + 4u, code->size()); |
| 342 | DCHECK_EQ(literal_offset & 3u, 0u); |
| 343 | DCHECK_EQ(patch_offset & 3u, 0u); |
| 344 | DCHECK_EQ(target_offset & 3u, 0u); |
| 345 | uint32_t displacement = CalculateDisplacement(patch_offset, target_offset & ~1u); |
| 346 | DCHECK_EQ(displacement & 3u, 0u); |
| 347 | DCHECK((displacement >> 27) == 0u || (displacement >> 27) == 31u); // 28-bit signed. |
| 348 | uint32_t value = (displacement & 0x0fffffffu) >> 2; |
| 349 | value |= 0x94000000; // BL |
| 350 | |
| 351 | uint8_t* addr = &(*code)[literal_offset]; |
| 352 | // Check that we're just overwriting an existing BL. |
| 353 | DCHECK_EQ(addr[3] & 0xfc, 0x94); |
| 354 | // Write the new BL. |
| 355 | addr[0] = (value >> 0) & 0xff; |
| 356 | addr[1] = (value >> 8) & 0xff; |
| 357 | addr[2] = (value >> 16) & 0xff; |
| 358 | addr[3] = (value >> 24) & 0xff; |
| 359 | } |
| 360 | |
| 361 | private: |
| 362 | static std::vector<uint8_t> CompileThunkCode() { |
| 363 | // The thunk just uses the entry point in the ArtMethod. This works even for calls |
| 364 | // to the generic JNI and interpreter trampolines. |
| 365 | arm64::Arm64Assembler assembler; |
| 366 | Offset offset(mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()); |
| 367 | assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0)); |
| 368 | std::vector<uint8_t> thunk_code(assembler.CodeSize()); |
| 369 | MemoryRegion code(thunk_code.data(), thunk_code.size()); |
| 370 | assembler.FinalizeInstructions(code); |
| 371 | return thunk_code; |
| 372 | } |
| 373 | |
| 374 | // Maximum positive and negative displacement measured from the patch location. |
| 375 | // (Signed 28 bit displacement with the last bit 0 has range [-2^27, 2^27-4] measured from |
| 376 | // the ARM64 PC pointing to the BL.) |
| 377 | static constexpr uint32_t kMaxPositiveDisplacement = (1u << 27) - 4u; |
| 378 | static constexpr uint32_t kMaxNegativeDisplacement = (1u << 27); |
| 379 | |
| 380 | DISALLOW_COPY_AND_ASSIGN(Arm64RelativeCallPatcher); |
| 381 | }; |
| 382 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 383 | #define DCHECK_OFFSET() \ |
| 384 | DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out->Seek(0, kSeekCurrent)) \ |
| 385 | << "file_offset=" << file_offset << " relative_offset=" << relative_offset |
| 386 | |
| 387 | #define DCHECK_OFFSET_() \ |
| 388 | DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out->Seek(0, kSeekCurrent)) \ |
| 389 | << "file_offset=" << file_offset << " offset_=" << offset_ |
| 390 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 391 | OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 392 | uint32_t image_file_location_oat_checksum, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 393 | uintptr_t image_file_location_oat_begin, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 394 | int32_t image_patch_delta, |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 395 | const CompilerDriver* compiler, |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 396 | ImageWriter* image_writer, |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 397 | TimingLogger* timings, |
| 398 | SafeMap<std::string, std::string>* key_value_store) |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 399 | : compiler_driver_(compiler), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 400 | image_writer_(image_writer), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 401 | dex_files_(&dex_files), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 402 | size_(0u), |
| 403 | oat_data_offset_(0u), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 404 | image_file_location_oat_checksum_(image_file_location_oat_checksum), |
| 405 | image_file_location_oat_begin_(image_file_location_oat_begin), |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 406 | image_patch_delta_(image_patch_delta), |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 407 | key_value_store_(key_value_store), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 408 | oat_header_(NULL), |
| 409 | size_dex_file_alignment_(0), |
| 410 | size_executable_offset_alignment_(0), |
| 411 | size_oat_header_(0), |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 412 | size_oat_header_key_value_store_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 413 | size_dex_file_(0), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 414 | size_interpreter_to_interpreter_bridge_(0), |
| 415 | size_interpreter_to_compiled_code_bridge_(0), |
| 416 | size_jni_dlsym_lookup_(0), |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 417 | size_portable_imt_conflict_trampoline_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 418 | size_portable_resolution_trampoline_(0), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 419 | size_portable_to_interpreter_bridge_(0), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 420 | size_quick_generic_jni_trampoline_(0), |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 421 | size_quick_imt_conflict_trampoline_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 422 | size_quick_resolution_trampoline_(0), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 423 | size_quick_to_interpreter_bridge_(0), |
| 424 | size_trampoline_alignment_(0), |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 425 | size_method_header_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 426 | size_code_(0), |
| 427 | size_code_alignment_(0), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 428 | size_relative_call_thunks_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 429 | size_mapping_table_(0), |
| 430 | size_vmap_table_(0), |
| 431 | size_gc_map_(0), |
| 432 | size_oat_dex_file_location_size_(0), |
| 433 | size_oat_dex_file_location_data_(0), |
| 434 | size_oat_dex_file_location_checksum_(0), |
| 435 | size_oat_dex_file_offset_(0), |
| 436 | size_oat_dex_file_methods_offsets_(0), |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 437 | size_oat_class_type_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 438 | size_oat_class_status_(0), |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 439 | size_oat_class_method_bitmaps_(0), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 440 | size_oat_class_method_offsets_(0), |
| 441 | method_offset_map_() { |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 442 | CHECK(key_value_store != nullptr); |
| 443 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 444 | switch (compiler_driver_->GetInstructionSet()) { |
| 445 | case kX86: |
| 446 | case kX86_64: |
| 447 | relative_call_patcher_.reset(new X86RelativeCallPatcher); |
| 448 | break; |
| 449 | case kArm: |
| 450 | // Fall through: we generate Thumb2 code for "arm". |
| 451 | case kThumb2: |
| 452 | relative_call_patcher_.reset(new Thumb2RelativeCallPatcher(this)); |
| 453 | break; |
| 454 | case kArm64: |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 455 | relative_call_patcher_.reset(new Arm64RelativeCallPatcher(this)); |
| 456 | break; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 457 | default: |
| 458 | relative_call_patcher_.reset(new NoRelativeCallPatcher); |
| 459 | break; |
| 460 | } |
| 461 | |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 462 | size_t offset; |
| 463 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 464 | TimingLogger::ScopedTiming split("InitOatHeader", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 465 | offset = InitOatHeader(); |
| 466 | } |
| 467 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 468 | TimingLogger::ScopedTiming split("InitOatDexFiles", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 469 | offset = InitOatDexFiles(offset); |
| 470 | } |
| 471 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 472 | TimingLogger::ScopedTiming split("InitDexFiles", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 473 | offset = InitDexFiles(offset); |
| 474 | } |
| 475 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 476 | TimingLogger::ScopedTiming split("InitOatClasses", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 477 | offset = InitOatClasses(offset); |
| 478 | } |
| 479 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 480 | TimingLogger::ScopedTiming split("InitOatMaps", timings); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 481 | offset = InitOatMaps(offset); |
| 482 | } |
| 483 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 484 | TimingLogger::ScopedTiming split("InitOatCode", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 485 | offset = InitOatCode(offset); |
| 486 | } |
| 487 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 488 | TimingLogger::ScopedTiming split("InitOatCodeDexFiles", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 489 | offset = InitOatCodeDexFiles(offset); |
| 490 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 491 | size_ = offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 492 | |
| 493 | CHECK_EQ(dex_files_->size(), oat_dex_files_.size()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 494 | CHECK_EQ(compiler->IsImage(), image_writer_ != nullptr); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 495 | CHECK_EQ(compiler->IsImage(), |
| 496 | key_value_store_->find(OatHeader::kImageLocationKey) == key_value_store_->end()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 497 | CHECK_ALIGNED(image_patch_delta_, kPageSize); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 500 | OatWriter::~OatWriter() { |
| 501 | delete oat_header_; |
| 502 | STLDeleteElements(&oat_dex_files_); |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 503 | STLDeleteElements(&oat_classes_); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 506 | struct OatWriter::GcMapDataAccess { |
| 507 | static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 508 | return compiled_method->GetGcMap(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE { |
| 512 | return oat_class->method_offsets_[method_offsets_index].gc_map_offset_; |
| 513 | } |
| 514 | |
| 515 | static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset) |
| 516 | ALWAYS_INLINE { |
| 517 | oat_class->method_offsets_[method_offsets_index].gc_map_offset_ = offset; |
| 518 | } |
| 519 | |
| 520 | static const char* Name() ALWAYS_INLINE { |
| 521 | return "GC map"; |
| 522 | } |
| 523 | }; |
| 524 | |
| 525 | struct OatWriter::MappingTableDataAccess { |
| 526 | static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE { |
| 527 | return &compiled_method->GetMappingTable(); |
| 528 | } |
| 529 | |
| 530 | static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE { |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 531 | uint32_t offset = oat_class->method_headers_[method_offsets_index].mapping_table_offset_; |
| 532 | return offset == 0u ? 0u : |
| 533 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset) |
| 537 | ALWAYS_INLINE { |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 538 | oat_class->method_headers_[method_offsets_index].mapping_table_offset_ = |
| 539 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | static const char* Name() ALWAYS_INLINE { |
| 543 | return "mapping table"; |
| 544 | } |
| 545 | }; |
| 546 | |
| 547 | struct OatWriter::VmapTableDataAccess { |
| 548 | static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE { |
| 549 | return &compiled_method->GetVmapTable(); |
| 550 | } |
| 551 | |
| 552 | static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE { |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 553 | uint32_t offset = oat_class->method_headers_[method_offsets_index].vmap_table_offset_; |
| 554 | return offset == 0u ? 0u : |
| 555 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset) |
| 559 | ALWAYS_INLINE { |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 560 | oat_class->method_headers_[method_offsets_index].vmap_table_offset_ = |
| 561 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | static const char* Name() ALWAYS_INLINE { |
| 565 | return "vmap table"; |
| 566 | } |
| 567 | }; |
| 568 | |
| 569 | class OatWriter::DexMethodVisitor { |
| 570 | public: |
| 571 | DexMethodVisitor(OatWriter* writer, size_t offset) |
| 572 | : writer_(writer), |
| 573 | offset_(offset), |
| 574 | dex_file_(nullptr), |
| 575 | class_def_index_(DexFile::kDexNoIndex) { |
| 576 | } |
| 577 | |
| 578 | virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) { |
| 579 | DCHECK(dex_file_ == nullptr); |
| 580 | DCHECK_EQ(class_def_index_, DexFile::kDexNoIndex); |
| 581 | dex_file_ = dex_file; |
| 582 | class_def_index_ = class_def_index; |
| 583 | return true; |
| 584 | } |
| 585 | |
| 586 | virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0; |
| 587 | |
| 588 | virtual bool EndClass() { |
| 589 | if (kIsDebugBuild) { |
| 590 | dex_file_ = nullptr; |
| 591 | class_def_index_ = DexFile::kDexNoIndex; |
| 592 | } |
| 593 | return true; |
| 594 | } |
| 595 | |
| 596 | size_t GetOffset() const { |
| 597 | return offset_; |
| 598 | } |
| 599 | |
| 600 | protected: |
| 601 | virtual ~DexMethodVisitor() { } |
| 602 | |
| 603 | OatWriter* const writer_; |
| 604 | |
| 605 | // The offset is usually advanced for each visited method by the derived class. |
| 606 | size_t offset_; |
| 607 | |
| 608 | // The dex file and class def index are set in StartClass(). |
| 609 | const DexFile* dex_file_; |
| 610 | size_t class_def_index_; |
| 611 | }; |
| 612 | |
| 613 | class OatWriter::OatDexMethodVisitor : public DexMethodVisitor { |
| 614 | public: |
| 615 | OatDexMethodVisitor(OatWriter* writer, size_t offset) |
| 616 | : DexMethodVisitor(writer, offset), |
| 617 | oat_class_index_(0u), |
| 618 | method_offsets_index_(0u) { |
| 619 | } |
| 620 | |
| 621 | bool StartClass(const DexFile* dex_file, size_t class_def_index) { |
| 622 | DexMethodVisitor::StartClass(dex_file, class_def_index); |
| 623 | DCHECK_LT(oat_class_index_, writer_->oat_classes_.size()); |
| 624 | method_offsets_index_ = 0u; |
| 625 | return true; |
| 626 | } |
| 627 | |
| 628 | bool EndClass() { |
| 629 | ++oat_class_index_; |
| 630 | return DexMethodVisitor::EndClass(); |
| 631 | } |
| 632 | |
| 633 | protected: |
| 634 | size_t oat_class_index_; |
| 635 | size_t method_offsets_index_; |
| 636 | }; |
| 637 | |
| 638 | class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor { |
| 639 | public: |
| 640 | InitOatClassesMethodVisitor(OatWriter* writer, size_t offset) |
| 641 | : DexMethodVisitor(writer, offset), |
| 642 | compiled_methods_(), |
| 643 | num_non_null_compiled_methods_(0u) { |
| 644 | compiled_methods_.reserve(256u); |
| 645 | } |
| 646 | |
| 647 | bool StartClass(const DexFile* dex_file, size_t class_def_index) { |
| 648 | DexMethodVisitor::StartClass(dex_file, class_def_index); |
| 649 | compiled_methods_.clear(); |
| 650 | num_non_null_compiled_methods_ = 0u; |
| 651 | return true; |
| 652 | } |
| 653 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame^] | 654 | bool VisitMethod(size_t class_def_method_index ATTRIBUTE_UNUSED, const ClassDataItemIterator& it) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 655 | // Fill in the compiled_methods_ array for methods that have a |
| 656 | // CompiledMethod. We track the number of non-null entries in |
| 657 | // num_non_null_compiled_methods_ since we only want to allocate |
| 658 | // OatMethodOffsets for the compiled methods. |
| 659 | uint32_t method_idx = it.GetMemberIndex(); |
| 660 | CompiledMethod* compiled_method = |
| 661 | writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx)); |
| 662 | compiled_methods_.push_back(compiled_method); |
| 663 | if (compiled_method != nullptr) { |
| 664 | ++num_non_null_compiled_methods_; |
| 665 | } |
| 666 | return true; |
| 667 | } |
| 668 | |
| 669 | bool EndClass() { |
| 670 | ClassReference class_ref(dex_file_, class_def_index_); |
| 671 | CompiledClass* compiled_class = writer_->compiler_driver_->GetCompiledClass(class_ref); |
| 672 | mirror::Class::Status status; |
| 673 | if (compiled_class != NULL) { |
| 674 | status = compiled_class->GetStatus(); |
| 675 | } else if (writer_->compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) { |
| 676 | status = mirror::Class::kStatusError; |
| 677 | } else { |
| 678 | status = mirror::Class::kStatusNotReady; |
| 679 | } |
| 680 | |
| 681 | OatClass* oat_class = new OatClass(offset_, compiled_methods_, |
| 682 | num_non_null_compiled_methods_, status); |
| 683 | writer_->oat_classes_.push_back(oat_class); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 684 | oat_class->UpdateChecksum(writer_->oat_header_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 685 | offset_ += oat_class->SizeOf(); |
| 686 | return DexMethodVisitor::EndClass(); |
| 687 | } |
| 688 | |
| 689 | private: |
| 690 | std::vector<CompiledMethod*> compiled_methods_; |
| 691 | size_t num_non_null_compiled_methods_; |
| 692 | }; |
| 693 | |
| 694 | class OatWriter::InitCodeMethodVisitor : public OatDexMethodVisitor { |
| 695 | public: |
| 696 | InitCodeMethodVisitor(OatWriter* writer, size_t offset) |
| 697 | : OatDexMethodVisitor(writer, offset) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 698 | writer_->absolute_patch_locations_.reserve( |
| 699 | writer_->compiler_driver_->GetNonRelativeLinkerPatchCount()); |
| 700 | } |
| 701 | |
| 702 | bool EndClass() { |
| 703 | OatDexMethodVisitor::EndClass(); |
| 704 | if (oat_class_index_ == writer_->oat_classes_.size()) { |
| 705 | offset_ = writer_->relative_call_patcher_->ReserveSpace(offset_, nullptr); |
| 706 | } |
| 707 | return true; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
| 711 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 712 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 713 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 714 | |
| 715 | if (compiled_method != nullptr) { |
| 716 | // Derived from CompiledMethod. |
| 717 | uint32_t quick_code_offset = 0; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 718 | |
| 719 | const std::vector<uint8_t>* portable_code = compiled_method->GetPortableCode(); |
| 720 | const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode(); |
| 721 | if (portable_code != nullptr) { |
| 722 | CHECK(quick_code == nullptr); |
| 723 | size_t oat_method_offsets_offset = |
| 724 | oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index); |
| 725 | compiled_method->AddOatdataOffsetToCompliledCodeOffset( |
| 726 | oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_)); |
| 727 | } else { |
| 728 | CHECK(quick_code != nullptr); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 729 | offset_ = writer_->relative_call_patcher_->ReserveSpace(offset_, compiled_method); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 730 | offset_ = compiled_method->AlignCode(offset_); |
| 731 | DCHECK_ALIGNED_PARAM(offset_, |
| 732 | GetInstructionSetAlignment(compiled_method->GetInstructionSet())); |
| 733 | uint32_t code_size = quick_code->size() * sizeof(uint8_t); |
| 734 | CHECK_NE(code_size, 0U); |
| 735 | uint32_t thumb_offset = compiled_method->CodeDelta(); |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 736 | quick_code_offset = offset_ + sizeof(OatQuickMethodHeader) + thumb_offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 737 | |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 738 | bool deduped = false; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 739 | |
| 740 | // Deduplicate code arrays. |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 741 | auto lb = dedupe_map_.lower_bound(compiled_method); |
| 742 | if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(compiled_method, lb->first)) { |
| 743 | quick_code_offset = lb->second; |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 744 | deduped = true; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 745 | } else { |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 746 | dedupe_map_.PutBefore(lb, compiled_method, quick_code_offset); |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 747 | } |
| 748 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 749 | MethodReference method_ref(dex_file_, it.GetMemberIndex()); |
| 750 | auto method_lb = writer_->method_offset_map_.lower_bound(method_ref); |
| 751 | if (method_lb != writer_->method_offset_map_.end() && |
| 752 | !writer_->method_offset_map_.key_comp()(method_ref, method_lb->first)) { |
| 753 | // TODO: Should this be a hard failure? |
| 754 | LOG(WARNING) << "Multiple definitions of " |
| 755 | << PrettyMethod(method_ref.dex_method_index, *method_ref.dex_file) |
| 756 | << ((method_lb->second != quick_code_offset) ? "; OFFSET MISMATCH" : ""); |
| 757 | } else { |
| 758 | writer_->method_offset_map_.PutBefore(method_lb, method_ref, quick_code_offset); |
| 759 | } |
| 760 | |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 761 | // Update quick method header. |
| 762 | DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size()); |
| 763 | OatQuickMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_]; |
| 764 | uint32_t mapping_table_offset = method_header->mapping_table_offset_; |
| 765 | uint32_t vmap_table_offset = method_header->vmap_table_offset_; |
| 766 | // The code offset was 0 when the mapping/vmap table offset was set, so it's set |
| 767 | // to 0-offset and we need to adjust it by code_offset. |
| 768 | uint32_t code_offset = quick_code_offset - thumb_offset; |
| 769 | if (mapping_table_offset != 0u) { |
| 770 | mapping_table_offset += code_offset; |
| 771 | DCHECK_LT(mapping_table_offset, code_offset); |
| 772 | } |
| 773 | if (vmap_table_offset != 0u) { |
| 774 | vmap_table_offset += code_offset; |
| 775 | DCHECK_LT(vmap_table_offset, code_offset); |
| 776 | } |
| 777 | uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes(); |
| 778 | uint32_t core_spill_mask = compiled_method->GetCoreSpillMask(); |
| 779 | uint32_t fp_spill_mask = compiled_method->GetFpSpillMask(); |
| 780 | *method_header = OatQuickMethodHeader(mapping_table_offset, vmap_table_offset, |
| 781 | frame_size_in_bytes, core_spill_mask, fp_spill_mask, |
| 782 | code_size); |
| 783 | |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 784 | if (!deduped) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 785 | // Update offsets. (Checksum is updated when writing.) |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 786 | offset_ += sizeof(*method_header); // Method header is prepended before code. |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 787 | offset_ += code_size; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 788 | // Record absolute patch locations. |
| 789 | if (!compiled_method->GetPatches().empty()) { |
| 790 | uintptr_t base_loc = offset_ - code_size - writer_->oat_header_->GetExecutableOffset(); |
| 791 | for (const LinkerPatch& patch : compiled_method->GetPatches()) { |
| 792 | if (patch.Type() != kLinkerPatchCallRelative) { |
| 793 | writer_->absolute_patch_locations_.push_back(base_loc + patch.LiteralOffset()); |
| 794 | } |
| 795 | } |
| 796 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 797 | } |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 798 | |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 799 | if (writer_->compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols()) { |
| 800 | // Record debug information for this function if we are doing that. |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 801 | |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 802 | std::string name = PrettyMethod(it.GetMemberIndex(), *dex_file_, true); |
| 803 | if (deduped) { |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 804 | // TODO We should place the DEDUPED tag on the first instance of a deduplicated symbol |
| 805 | // so that it will show up in a debuggerd crash report. |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 806 | name += " [ DEDUPED ]"; |
| 807 | } |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 808 | |
| 809 | const uint32_t quick_code_start = quick_code_offset - |
| 810 | writer_->oat_header_->GetExecutableOffset(); |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 811 | const DexFile::CodeItem *code_item = it.GetMethodCodeItem(); |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 812 | writer_->method_info_.push_back(DebugInfo(name, |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 813 | dex_file_->GetSourceFile(dex_file_->GetClassDef(class_def_index_)), |
| 814 | quick_code_start, quick_code_start + code_size, |
| 815 | code_item == nullptr ? nullptr : dex_file_->GetDebugInfoStream(code_item), |
| 816 | compiled_method)); |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 817 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 818 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 819 | |
| 820 | if (kIsDebugBuild) { |
| 821 | // We expect GC maps except when the class hasn't been verified or the method is native. |
| 822 | const CompilerDriver* compiler_driver = writer_->compiler_driver_; |
| 823 | ClassReference class_ref(dex_file_, class_def_index_); |
| 824 | CompiledClass* compiled_class = compiler_driver->GetCompiledClass(class_ref); |
| 825 | mirror::Class::Status status; |
| 826 | if (compiled_class != NULL) { |
| 827 | status = compiled_class->GetStatus(); |
| 828 | } else if (compiler_driver->GetVerificationResults()->IsClassRejected(class_ref)) { |
| 829 | status = mirror::Class::kStatusError; |
| 830 | } else { |
| 831 | status = mirror::Class::kStatusNotReady; |
| 832 | } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 833 | std::vector<uint8_t> const * gc_map = compiled_method->GetGcMap(); |
| 834 | if (gc_map != nullptr) { |
| 835 | size_t gc_map_size = gc_map->size() * sizeof(gc_map[0]); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 836 | bool is_native = it.MemberIsNative(); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 837 | CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified) |
| 838 | << gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " " |
| 839 | << (status < mirror::Class::kStatusVerified) << " " << status << " " |
| 840 | << PrettyMethod(it.GetMemberIndex(), *dex_file_); |
| 841 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 845 | OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_]; |
| 846 | offsets->code_offset_ = quick_code_offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 847 | ++method_offsets_index_; |
| 848 | } |
| 849 | |
| 850 | return true; |
| 851 | } |
| 852 | |
| 853 | private: |
| 854 | // Deduplication is already done on a pointer basis by the compiler driver, |
| 855 | // so we can simply compare the pointers to find out if things are duplicated. |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 856 | SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 857 | }; |
| 858 | |
| 859 | template <typename DataAccess> |
| 860 | class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor { |
| 861 | public: |
| 862 | InitMapMethodVisitor(OatWriter* writer, size_t offset) |
| 863 | : OatDexMethodVisitor(writer, offset) { |
| 864 | } |
| 865 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame^] | 866 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it ATTRIBUTE_UNUSED) |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 867 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 868 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 869 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 870 | |
| 871 | if (compiled_method != nullptr) { |
| 872 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 873 | DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u); |
| 874 | |
| 875 | const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 876 | uint32_t map_size = map == nullptr ? 0 : map->size() * sizeof((*map)[0]); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 877 | if (map_size != 0u) { |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 878 | auto lb = dedupe_map_.lower_bound(map); |
| 879 | if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(map, lb->first)) { |
| 880 | DataAccess::SetOffset(oat_class, method_offsets_index_, lb->second); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 881 | } else { |
| 882 | DataAccess::SetOffset(oat_class, method_offsets_index_, offset_); |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 883 | dedupe_map_.PutBefore(lb, map, offset_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 884 | offset_ += map_size; |
| 885 | writer_->oat_header_->UpdateChecksum(&(*map)[0], map_size); |
| 886 | } |
| 887 | } |
| 888 | ++method_offsets_index_; |
| 889 | } |
| 890 | |
| 891 | return true; |
| 892 | } |
| 893 | |
| 894 | private: |
| 895 | // Deduplication is already done on a pointer basis by the compiler driver, |
| 896 | // so we can simply compare the pointers to find out if things are duplicated. |
| 897 | SafeMap<const std::vector<uint8_t>*, uint32_t> dedupe_map_; |
| 898 | }; |
| 899 | |
| 900 | class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor { |
| 901 | public: |
| 902 | InitImageMethodVisitor(OatWriter* writer, size_t offset) |
| 903 | : OatDexMethodVisitor(writer, offset) { |
| 904 | } |
| 905 | |
| 906 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
| 907 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 908 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 909 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 910 | |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 911 | OatMethodOffsets offsets(0u, 0u); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 912 | if (compiled_method != nullptr) { |
| 913 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 914 | offsets = oat_class->method_offsets_[method_offsets_index_]; |
| 915 | ++method_offsets_index_; |
| 916 | } |
| 917 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 918 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 919 | InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_)); |
| 920 | // Unchecked as we hold mutator_lock_ on entry. |
| 921 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 922 | StackHandleScope<2> hs(soa.Self()); |
| 923 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache(*dex_file_))); |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 924 | mirror::ArtMethod* method = linker->ResolveMethod(*dex_file_, it.GetMemberIndex(), dex_cache, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 925 | NullHandle<mirror::ClassLoader>(), |
| 926 | NullHandle<mirror::ArtMethod>(), |
| 927 | invoke_type); |
Andreas Gampe | d9efea6 | 2014-07-21 22:56:08 -0700 | [diff] [blame] | 928 | if (method == nullptr) { |
| 929 | LOG(ERROR) << "Unexpected failure to resolve a method: " |
| 930 | << PrettyMethod(it.GetMemberIndex(), *dex_file_, true); |
| 931 | soa.Self()->AssertPendingException(); |
| 932 | mirror::Throwable* exc = soa.Self()->GetException(nullptr); |
| 933 | std::string dump = exc->Dump(); |
| 934 | LOG(FATAL) << dump; |
| 935 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 936 | // Portable code offsets are set by ElfWriterMclinker::FixupCompiledCodeOffset after linking. |
| 937 | method->SetQuickOatCodeOffset(offsets.code_offset_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 938 | method->SetOatNativeGcMapOffset(offsets.gc_map_offset_); |
| 939 | |
| 940 | return true; |
| 941 | } |
| 942 | }; |
| 943 | |
| 944 | class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor { |
| 945 | public: |
| 946 | WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset, |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 947 | size_t relative_offset) SHARED_LOCK_FUNCTION(Locks::mutator_lock_) |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 948 | : OatDexMethodVisitor(writer, relative_offset), |
| 949 | out_(out), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 950 | file_offset_(file_offset), |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 951 | soa_(Thread::Current()), |
| 952 | no_thread_suspension_(soa_.Self(), "OatWriter patching"), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 953 | class_linker_(Runtime::Current()->GetClassLinker()), |
| 954 | dex_cache_(nullptr) { |
| 955 | if (writer_->image_writer_ != nullptr) { |
| 956 | // If we're creating the image, the address space must be ready so that we can apply patches. |
| 957 | CHECK(writer_->image_writer_->IsImageAddressSpaceReady()); |
| 958 | patched_code_.reserve(16 * KB); |
| 959 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 960 | } |
| 961 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 962 | ~WriteCodeMethodVisitor() UNLOCK_FUNCTION(Locks::mutator_lock_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | bool StartClass(const DexFile* dex_file, size_t class_def_index) |
| 966 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 967 | OatDexMethodVisitor::StartClass(dex_file, class_def_index); |
| 968 | if (dex_cache_ == nullptr || dex_cache_->GetDexFile() != dex_file) { |
| 969 | dex_cache_ = class_linker_->FindDexCache(*dex_file); |
| 970 | } |
| 971 | return true; |
| 972 | } |
| 973 | |
| 974 | bool EndClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 975 | bool result = OatDexMethodVisitor::EndClass(); |
| 976 | if (oat_class_index_ == writer_->oat_classes_.size()) { |
| 977 | DCHECK(result); // OatDexMethodVisitor::EndClass() never fails. |
| 978 | offset_ = writer_->relative_call_patcher_->WriteThunks(out_, offset_); |
| 979 | if (UNLIKELY(offset_ == 0u)) { |
| 980 | PLOG(ERROR) << "Failed to write final relative call thunks"; |
| 981 | result = false; |
| 982 | } |
| 983 | } |
| 984 | return result; |
| 985 | } |
| 986 | |
| 987 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
| 988 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 989 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 990 | const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 991 | |
| 992 | if (compiled_method != NULL) { // ie. not an abstract method |
| 993 | size_t file_offset = file_offset_; |
| 994 | OutputStream* out = out_; |
| 995 | |
| 996 | const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode(); |
| 997 | if (quick_code != nullptr) { |
| 998 | CHECK(compiled_method->GetPortableCode() == nullptr); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 999 | offset_ = writer_->relative_call_patcher_->WriteThunks(out, offset_); |
| 1000 | if (offset_ == 0u) { |
| 1001 | ReportWriteFailure("relative call thunk", it); |
| 1002 | return false; |
| 1003 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1004 | uint32_t aligned_offset = compiled_method->AlignCode(offset_); |
| 1005 | uint32_t aligned_code_delta = aligned_offset - offset_; |
| 1006 | if (aligned_code_delta != 0) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1007 | if (!writer_->WriteCodeAlignment(out, aligned_code_delta)) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1008 | ReportWriteFailure("code alignment padding", it); |
| 1009 | return false; |
| 1010 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1011 | offset_ += aligned_code_delta; |
| 1012 | DCHECK_OFFSET_(); |
| 1013 | } |
| 1014 | DCHECK_ALIGNED_PARAM(offset_, |
| 1015 | GetInstructionSetAlignment(compiled_method->GetInstructionSet())); |
| 1016 | uint32_t code_size = quick_code->size() * sizeof(uint8_t); |
| 1017 | CHECK_NE(code_size, 0U); |
| 1018 | |
| 1019 | // Deduplicate code arrays. |
| 1020 | const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_]; |
| 1021 | DCHECK(method_offsets.code_offset_ < offset_ || method_offsets.code_offset_ == |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 1022 | offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta()) |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1023 | << PrettyMethod(it.GetMemberIndex(), *dex_file_); |
| 1024 | if (method_offsets.code_offset_ >= offset_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1025 | const OatQuickMethodHeader& method_header = |
| 1026 | oat_class->method_headers_[method_offsets_index_]; |
| 1027 | writer_->oat_header_->UpdateChecksum(&method_header, sizeof(method_header)); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1028 | if (!out->WriteFully(&method_header, sizeof(method_header))) { |
| 1029 | ReportWriteFailure("method header", it); |
| 1030 | return false; |
| 1031 | } |
| 1032 | writer_->size_method_header_ += sizeof(method_header); |
| 1033 | offset_ += sizeof(method_header); |
| 1034 | DCHECK_OFFSET_(); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1035 | |
| 1036 | if (!compiled_method->GetPatches().empty()) { |
| 1037 | patched_code_ = *quick_code; |
| 1038 | quick_code = &patched_code_; |
| 1039 | for (const LinkerPatch& patch : compiled_method->GetPatches()) { |
| 1040 | if (patch.Type() == kLinkerPatchCallRelative) { |
| 1041 | // NOTE: Relative calls across oat files are not supported. |
| 1042 | uint32_t target_offset = GetTargetOffset(patch); |
| 1043 | uint32_t literal_offset = patch.LiteralOffset(); |
| 1044 | writer_->relative_call_patcher_->Patch(&patched_code_, literal_offset, |
| 1045 | offset_ + literal_offset, target_offset); |
| 1046 | } else if (patch.Type() == kLinkerPatchCall) { |
| 1047 | uint32_t target_offset = GetTargetOffset(patch); |
| 1048 | PatchCodeAddress(&patched_code_, patch.LiteralOffset(), target_offset); |
| 1049 | } else if (patch.Type() == kLinkerPatchMethod) { |
| 1050 | mirror::ArtMethod* method = GetTargetMethod(patch); |
| 1051 | PatchObjectAddress(&patched_code_, patch.LiteralOffset(), method); |
| 1052 | } else if (patch.Type() == kLinkerPatchType) { |
| 1053 | mirror::Class* type = GetTargetType(patch); |
| 1054 | PatchObjectAddress(&patched_code_, patch.LiteralOffset(), type); |
| 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | writer_->oat_header_->UpdateChecksum(&(*quick_code)[0], code_size); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1060 | if (!out->WriteFully(&(*quick_code)[0], code_size)) { |
| 1061 | ReportWriteFailure("method code", it); |
| 1062 | return false; |
| 1063 | } |
| 1064 | writer_->size_code_ += code_size; |
| 1065 | offset_ += code_size; |
| 1066 | } |
| 1067 | DCHECK_OFFSET_(); |
| 1068 | } |
| 1069 | ++method_offsets_index_; |
| 1070 | } |
| 1071 | |
| 1072 | return true; |
| 1073 | } |
| 1074 | |
| 1075 | private: |
| 1076 | OutputStream* const out_; |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 1077 | const size_t file_offset_; |
| 1078 | const ScopedObjectAccess soa_; |
| 1079 | const ScopedAssertNoThreadSuspension no_thread_suspension_; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1080 | ClassLinker* const class_linker_; |
| 1081 | mirror::DexCache* dex_cache_; |
| 1082 | std::vector<uint8_t> patched_code_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1083 | |
| 1084 | void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) { |
| 1085 | PLOG(ERROR) << "Failed to write " << what << " for " |
| 1086 | << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation(); |
| 1087 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1088 | |
| 1089 | mirror::ArtMethod* GetTargetMethod(const LinkerPatch& patch) |
| 1090 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1091 | MethodReference ref = patch.TargetMethod(); |
| 1092 | mirror::DexCache* dex_cache = |
| 1093 | (dex_file_ == ref.dex_file) ? dex_cache_ : class_linker_->FindDexCache(*ref.dex_file); |
| 1094 | mirror::ArtMethod* method = dex_cache->GetResolvedMethod(ref.dex_method_index); |
| 1095 | CHECK(method != nullptr); |
| 1096 | return method; |
| 1097 | } |
| 1098 | |
| 1099 | uint32_t GetTargetOffset(const LinkerPatch& patch) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1100 | auto target_it = writer_->method_offset_map_.find(patch.TargetMethod()); |
| 1101 | uint32_t target_offset = |
| 1102 | (target_it != writer_->method_offset_map_.end()) ? target_it->second : 0u; |
| 1103 | // If there's no compiled code, point to the correct trampoline. |
| 1104 | if (UNLIKELY(target_offset == 0)) { |
| 1105 | mirror::ArtMethod* target = GetTargetMethod(patch); |
| 1106 | DCHECK(target != nullptr); |
| 1107 | DCHECK_EQ(target->GetQuickOatCodeOffset(), 0u); |
| 1108 | target_offset = target->IsNative() |
| 1109 | ? writer_->oat_header_->GetQuickGenericJniTrampolineOffset() |
| 1110 | : writer_->oat_header_->GetQuickToInterpreterBridgeOffset(); |
| 1111 | } |
| 1112 | return target_offset; |
| 1113 | } |
| 1114 | |
| 1115 | mirror::Class* GetTargetType(const LinkerPatch& patch) |
| 1116 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1117 | mirror::DexCache* dex_cache = (dex_file_ == patch.TargetTypeDexFile()) |
| 1118 | ? dex_cache_ : class_linker_->FindDexCache(*patch.TargetTypeDexFile()); |
| 1119 | mirror::Class* type = dex_cache->GetResolvedType(patch.TargetTypeIndex()); |
| 1120 | CHECK(type != nullptr); |
| 1121 | return type; |
| 1122 | } |
| 1123 | |
| 1124 | void PatchObjectAddress(std::vector<uint8_t>* code, uint32_t offset, mirror::Object* object) |
| 1125 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1126 | // NOTE: Direct method pointers across oat files don't use linker patches. However, direct |
| 1127 | // type pointers across oat files do. (TODO: Investigate why.) |
| 1128 | if (writer_->image_writer_ != nullptr) { |
| 1129 | object = writer_->image_writer_->GetImageAddress(object); |
| 1130 | } |
| 1131 | uint32_t address = PointerToLowMemUInt32(object); |
| 1132 | DCHECK_LE(offset + 4, code->size()); |
| 1133 | uint8_t* data = &(*code)[offset]; |
| 1134 | data[0] = address & 0xffu; |
| 1135 | data[1] = (address >> 8) & 0xffu; |
| 1136 | data[2] = (address >> 16) & 0xffu; |
| 1137 | data[3] = (address >> 24) & 0xffu; |
| 1138 | } |
| 1139 | |
| 1140 | void PatchCodeAddress(std::vector<uint8_t>* code, uint32_t offset, uint32_t target_offset) |
| 1141 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1142 | // NOTE: Direct calls across oat files don't use linker patches. |
| 1143 | DCHECK(writer_->image_writer_ != nullptr); |
| 1144 | uint32_t address = PointerToLowMemUInt32(writer_->image_writer_->GetOatFileBegin() + |
| 1145 | writer_->oat_data_offset_ + target_offset); |
| 1146 | DCHECK_LE(offset + 4, code->size()); |
| 1147 | uint8_t* data = &(*code)[offset]; |
| 1148 | data[0] = address & 0xffu; |
| 1149 | data[1] = (address >> 8) & 0xffu; |
| 1150 | data[2] = (address >> 16) & 0xffu; |
| 1151 | data[3] = (address >> 24) & 0xffu; |
| 1152 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1153 | }; |
| 1154 | |
| 1155 | template <typename DataAccess> |
| 1156 | class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor { |
| 1157 | public: |
| 1158 | WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset, |
| 1159 | size_t relative_offset) |
| 1160 | : OatDexMethodVisitor(writer, relative_offset), |
| 1161 | out_(out), |
| 1162 | file_offset_(file_offset) { |
| 1163 | } |
| 1164 | |
| 1165 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) { |
| 1166 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 1167 | const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 1168 | |
| 1169 | if (compiled_method != NULL) { // ie. not an abstract method |
| 1170 | size_t file_offset = file_offset_; |
| 1171 | OutputStream* out = out_; |
| 1172 | |
| 1173 | uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_); |
| 1174 | ++method_offsets_index_; |
| 1175 | |
| 1176 | // Write deduplicated map. |
| 1177 | const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1178 | size_t map_size = map == nullptr ? 0 : map->size() * sizeof((*map)[0]); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1179 | DCHECK((map_size == 0u && map_offset == 0u) || |
| 1180 | (map_size != 0u && map_offset != 0u && map_offset <= offset_)) |
| 1181 | << PrettyMethod(it.GetMemberIndex(), *dex_file_); |
| 1182 | if (map_size != 0u && map_offset == offset_) { |
| 1183 | if (UNLIKELY(!out->WriteFully(&(*map)[0], map_size))) { |
| 1184 | ReportWriteFailure(it); |
| 1185 | return false; |
| 1186 | } |
| 1187 | offset_ += map_size; |
| 1188 | } |
| 1189 | DCHECK_OFFSET_(); |
| 1190 | } |
| 1191 | |
| 1192 | return true; |
| 1193 | } |
| 1194 | |
| 1195 | private: |
| 1196 | OutputStream* const out_; |
| 1197 | size_t const file_offset_; |
| 1198 | |
| 1199 | void ReportWriteFailure(const ClassDataItemIterator& it) { |
| 1200 | PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for " |
| 1201 | << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation(); |
| 1202 | } |
| 1203 | }; |
| 1204 | |
| 1205 | // Visit all methods from all classes in all dex files with the specified visitor. |
| 1206 | bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) { |
| 1207 | for (const DexFile* dex_file : *dex_files_) { |
| 1208 | const size_t class_def_count = dex_file->NumClassDefs(); |
| 1209 | for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) { |
| 1210 | if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) { |
| 1211 | return false; |
| 1212 | } |
| 1213 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1214 | const uint8_t* class_data = dex_file->GetClassData(class_def); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1215 | if (class_data != NULL) { // ie not an empty class, such as a marker interface |
| 1216 | ClassDataItemIterator it(*dex_file, class_data); |
| 1217 | while (it.HasNextStaticField()) { |
| 1218 | it.Next(); |
| 1219 | } |
| 1220 | while (it.HasNextInstanceField()) { |
| 1221 | it.Next(); |
| 1222 | } |
| 1223 | size_t class_def_method_index = 0u; |
| 1224 | while (it.HasNextDirectMethod()) { |
| 1225 | if (!visitor->VisitMethod(class_def_method_index, it)) { |
| 1226 | return false; |
| 1227 | } |
| 1228 | ++class_def_method_index; |
| 1229 | it.Next(); |
| 1230 | } |
| 1231 | while (it.HasNextVirtualMethod()) { |
| 1232 | if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) { |
| 1233 | return false; |
| 1234 | } |
| 1235 | ++class_def_method_index; |
| 1236 | it.Next(); |
| 1237 | } |
| 1238 | } |
| 1239 | if (UNLIKELY(!visitor->EndClass())) { |
| 1240 | return false; |
| 1241 | } |
| 1242 | } |
| 1243 | } |
| 1244 | return true; |
| 1245 | } |
| 1246 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 1247 | size_t OatWriter::InitOatHeader() { |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1248 | oat_header_ = OatHeader::Create(compiler_driver_->GetInstructionSet(), |
| 1249 | compiler_driver_->GetInstructionSetFeatures(), |
| 1250 | dex_files_, |
| 1251 | image_file_location_oat_checksum_, |
| 1252 | image_file_location_oat_begin_, |
| 1253 | key_value_store_); |
| 1254 | |
| 1255 | return oat_header_->GetHeaderSize(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | size_t OatWriter::InitOatDexFiles(size_t offset) { |
| 1259 | // create the OatDexFiles |
| 1260 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 1261 | const DexFile* dex_file = (*dex_files_)[i]; |
| 1262 | CHECK(dex_file != NULL); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1263 | OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1264 | oat_dex_files_.push_back(oat_dex_file); |
| 1265 | offset += oat_dex_file->SizeOf(); |
| 1266 | } |
| 1267 | return offset; |
| 1268 | } |
| 1269 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1270 | size_t OatWriter::InitDexFiles(size_t offset) { |
| 1271 | // calculate the offsets within OatDexFiles to the DexFiles |
| 1272 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 1273 | // dex files are required to be 4 byte aligned |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1274 | size_t original_offset = offset; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1275 | offset = RoundUp(offset, 4); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1276 | size_dex_file_alignment_ += offset - original_offset; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1277 | |
| 1278 | // set offset in OatDexFile to DexFile |
| 1279 | oat_dex_files_[i]->dex_file_offset_ = offset; |
| 1280 | |
| 1281 | const DexFile* dex_file = (*dex_files_)[i]; |
| 1282 | offset += dex_file->GetHeader().file_size_; |
| 1283 | } |
| 1284 | return offset; |
| 1285 | } |
| 1286 | |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 1287 | size_t OatWriter::InitOatClasses(size_t offset) { |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 1288 | // calculate the offsets within OatDexFiles to OatClasses |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1289 | InitOatClassesMethodVisitor visitor(this, offset); |
| 1290 | bool success = VisitDexMethods(&visitor); |
| 1291 | CHECK(success); |
| 1292 | offset = visitor.GetOffset(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1293 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1294 | // Update oat_dex_files_. |
| 1295 | auto oat_class_it = oat_classes_.begin(); |
| 1296 | for (OatDexFile* oat_dex_file : oat_dex_files_) { |
| 1297 | for (uint32_t& offset : oat_dex_file->methods_offsets_) { |
| 1298 | DCHECK(oat_class_it != oat_classes_.end()); |
| 1299 | offset = (*oat_class_it)->offset_; |
| 1300 | ++oat_class_it; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1301 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1302 | oat_dex_file->UpdateChecksum(oat_header_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1303 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1304 | CHECK(oat_class_it == oat_classes_.end()); |
| 1305 | |
| 1306 | return offset; |
| 1307 | } |
| 1308 | |
| 1309 | size_t OatWriter::InitOatMaps(size_t offset) { |
| 1310 | #define VISIT(VisitorType) \ |
| 1311 | do { \ |
| 1312 | VisitorType visitor(this, offset); \ |
| 1313 | bool success = VisitDexMethods(&visitor); \ |
| 1314 | DCHECK(success); \ |
| 1315 | offset = visitor.GetOffset(); \ |
| 1316 | } while (false) |
| 1317 | |
| 1318 | VISIT(InitMapMethodVisitor<GcMapDataAccess>); |
| 1319 | VISIT(InitMapMethodVisitor<MappingTableDataAccess>); |
| 1320 | VISIT(InitMapMethodVisitor<VmapTableDataAccess>); |
| 1321 | |
| 1322 | #undef VISIT |
| 1323 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1324 | return offset; |
| 1325 | } |
| 1326 | |
| 1327 | size_t OatWriter::InitOatCode(size_t offset) { |
| 1328 | // calculate the offsets within OatHeader to executable code |
| 1329 | size_t old_offset = offset; |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 1330 | size_t adjusted_offset = offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1331 | // required to be on a new page boundary |
| 1332 | offset = RoundUp(offset, kPageSize); |
| 1333 | oat_header_->SetExecutableOffset(offset); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1334 | size_executable_offset_alignment_ = offset - old_offset; |
| 1335 | if (compiler_driver_->IsImage()) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1336 | CHECK_EQ(image_patch_delta_, 0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1337 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1338 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1339 | #define DO_TRAMPOLINE(field, fn_name) \ |
| 1340 | offset = CompiledCode::AlignCode(offset, instruction_set); \ |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 1341 | adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \ |
| 1342 | oat_header_->Set ## fn_name ## Offset(adjusted_offset); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1343 | field.reset(compiler_driver_->Create ## fn_name()); \ |
| 1344 | offset += field->size(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1345 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1346 | DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge); |
| 1347 | DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge); |
| 1348 | DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1349 | DO_TRAMPOLINE(portable_imt_conflict_trampoline_, PortableImtConflictTrampoline); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1350 | DO_TRAMPOLINE(portable_resolution_trampoline_, PortableResolutionTrampoline); |
| 1351 | DO_TRAMPOLINE(portable_to_interpreter_bridge_, PortableToInterpreterBridge); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1352 | DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1353 | DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1354 | DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline); |
| 1355 | DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1356 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1357 | #undef DO_TRAMPOLINE |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1358 | } else { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1359 | oat_header_->SetInterpreterToInterpreterBridgeOffset(0); |
| 1360 | oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0); |
| 1361 | oat_header_->SetJniDlsymLookupOffset(0); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1362 | oat_header_->SetPortableImtConflictTrampolineOffset(0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1363 | oat_header_->SetPortableResolutionTrampolineOffset(0); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1364 | oat_header_->SetPortableToInterpreterBridgeOffset(0); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1365 | oat_header_->SetQuickGenericJniTrampolineOffset(0); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1366 | oat_header_->SetQuickImtConflictTrampolineOffset(0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1367 | oat_header_->SetQuickResolutionTrampolineOffset(0); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1368 | oat_header_->SetQuickToInterpreterBridgeOffset(0); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1369 | oat_header_->SetImagePatchDelta(image_patch_delta_); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1370 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1371 | return offset; |
| 1372 | } |
| 1373 | |
| 1374 | size_t OatWriter::InitOatCodeDexFiles(size_t offset) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1375 | #define VISIT(VisitorType) \ |
| 1376 | do { \ |
| 1377 | VisitorType visitor(this, offset); \ |
| 1378 | bool success = VisitDexMethods(&visitor); \ |
| 1379 | DCHECK(success); \ |
| 1380 | offset = visitor.GetOffset(); \ |
| 1381 | } while (false) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1382 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1383 | VISIT(InitCodeMethodVisitor); |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 1384 | if (compiler_driver_->IsImage()) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1385 | VISIT(InitImageMethodVisitor); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1386 | } |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 1387 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1388 | #undef VISIT |
| 1389 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1390 | return offset; |
| 1391 | } |
| 1392 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1393 | bool OatWriter::Write(OutputStream* out) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1394 | const off_t raw_file_offset = out->Seek(0, kSeekCurrent); |
| 1395 | if (raw_file_offset == (off_t) -1) { |
| 1396 | LOG(ERROR) << "Failed to get file offset in " << out->GetLocation(); |
| 1397 | return false; |
| 1398 | } |
| 1399 | const size_t file_offset = static_cast<size_t>(raw_file_offset); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1400 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1401 | // Reserve space for header. It will be written last - after updating the checksum. |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1402 | size_t header_size = oat_header_->GetHeaderSize(); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1403 | if (out->Seek(header_size, kSeekCurrent) == (off_t) -1) { |
| 1404 | PLOG(ERROR) << "Failed to reserve space for oat header in " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1405 | return false; |
| 1406 | } |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1407 | size_oat_header_ += sizeof(OatHeader); |
| 1408 | size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 1409 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1410 | if (!WriteTables(out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1411 | LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1412 | return false; |
| 1413 | } |
| 1414 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1415 | off_t tables_end_offset = out->Seek(0, kSeekCurrent); |
| 1416 | if (tables_end_offset == (off_t) -1) { |
| 1417 | LOG(ERROR) << "Failed to seek to oat code position in " << out->GetLocation(); |
| 1418 | return false; |
| 1419 | } |
| 1420 | size_t relative_offset = static_cast<size_t>(tables_end_offset) - file_offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1421 | relative_offset = WriteMaps(out, file_offset, relative_offset); |
| 1422 | if (relative_offset == 0) { |
| 1423 | LOG(ERROR) << "Failed to write oat code to " << out->GetLocation(); |
| 1424 | return false; |
| 1425 | } |
| 1426 | |
| 1427 | relative_offset = WriteCode(out, file_offset, relative_offset); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1428 | if (relative_offset == 0) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1429 | LOG(ERROR) << "Failed to write oat code to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1430 | return false; |
| 1431 | } |
| 1432 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1433 | relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset); |
| 1434 | if (relative_offset == 0) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1435 | LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1436 | return false; |
| 1437 | } |
| 1438 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1439 | const off_t oat_end_file_offset = out->Seek(0, kSeekCurrent); |
| 1440 | if (oat_end_file_offset == (off_t) -1) { |
| 1441 | LOG(ERROR) << "Failed to get oat end file offset in " << out->GetLocation(); |
| 1442 | return false; |
| 1443 | } |
| 1444 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1445 | if (kIsDebugBuild) { |
| 1446 | uint32_t size_total = 0; |
| 1447 | #define DO_STAT(x) \ |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 1448 | VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \ |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1449 | size_total += x; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1450 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1451 | DO_STAT(size_dex_file_alignment_); |
| 1452 | DO_STAT(size_executable_offset_alignment_); |
| 1453 | DO_STAT(size_oat_header_); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1454 | DO_STAT(size_oat_header_key_value_store_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1455 | DO_STAT(size_dex_file_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1456 | DO_STAT(size_interpreter_to_interpreter_bridge_); |
| 1457 | DO_STAT(size_interpreter_to_compiled_code_bridge_); |
| 1458 | DO_STAT(size_jni_dlsym_lookup_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1459 | DO_STAT(size_portable_imt_conflict_trampoline_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1460 | DO_STAT(size_portable_resolution_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1461 | DO_STAT(size_portable_to_interpreter_bridge_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1462 | DO_STAT(size_quick_generic_jni_trampoline_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1463 | DO_STAT(size_quick_imt_conflict_trampoline_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1464 | DO_STAT(size_quick_resolution_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1465 | DO_STAT(size_quick_to_interpreter_bridge_); |
| 1466 | DO_STAT(size_trampoline_alignment_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1467 | DO_STAT(size_method_header_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1468 | DO_STAT(size_code_); |
| 1469 | DO_STAT(size_code_alignment_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1470 | DO_STAT(size_relative_call_thunks_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1471 | DO_STAT(size_mapping_table_); |
| 1472 | DO_STAT(size_vmap_table_); |
| 1473 | DO_STAT(size_gc_map_); |
| 1474 | DO_STAT(size_oat_dex_file_location_size_); |
| 1475 | DO_STAT(size_oat_dex_file_location_data_); |
| 1476 | DO_STAT(size_oat_dex_file_location_checksum_); |
| 1477 | DO_STAT(size_oat_dex_file_offset_); |
| 1478 | DO_STAT(size_oat_dex_file_methods_offsets_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1479 | DO_STAT(size_oat_class_type_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1480 | DO_STAT(size_oat_class_status_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1481 | DO_STAT(size_oat_class_method_bitmaps_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1482 | DO_STAT(size_oat_class_method_offsets_); |
| 1483 | #undef DO_STAT |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1484 | |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 1485 | VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \ |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1486 | CHECK_EQ(file_offset + size_total, static_cast<size_t>(oat_end_file_offset)); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1487 | CHECK_EQ(size_, size_total); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1488 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1489 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1490 | CHECK_EQ(file_offset + size_, static_cast<size_t>(oat_end_file_offset)); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1491 | CHECK_EQ(size_, relative_offset); |
| 1492 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1493 | // Write the header now that the checksum is final. |
| 1494 | if (out->Seek(file_offset, kSeekSet) == (off_t) -1) { |
| 1495 | PLOG(ERROR) << "Failed to seek to oat header position in " << out->GetLocation(); |
| 1496 | return false; |
| 1497 | } |
| 1498 | DCHECK_EQ(raw_file_offset, out->Seek(0, kSeekCurrent)); |
| 1499 | if (!out->WriteFully(oat_header_, header_size)) { |
| 1500 | PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation(); |
| 1501 | return false; |
| 1502 | } |
| 1503 | if (out->Seek(oat_end_file_offset, kSeekSet) == (off_t) -1) { |
| 1504 | PLOG(ERROR) << "Failed to seek to end after writing oat header to " << out->GetLocation(); |
| 1505 | return false; |
| 1506 | } |
| 1507 | DCHECK_EQ(oat_end_file_offset, out->Seek(0, kSeekCurrent)); |
| 1508 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1509 | return true; |
| 1510 | } |
| 1511 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1512 | bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1513 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1514 | if (!oat_dex_files_[i]->Write(this, out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1515 | PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1516 | return false; |
| 1517 | } |
| 1518 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1519 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1520 | uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_; |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1521 | off_t actual_offset = out->Seek(expected_offset, kSeekSet); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1522 | if (static_cast<uint32_t>(actual_offset) != expected_offset) { |
| 1523 | const DexFile* dex_file = (*dex_files_)[i]; |
| 1524 | PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset |
| 1525 | << " Expected: " << expected_offset << " File: " << dex_file->GetLocation(); |
| 1526 | return false; |
| 1527 | } |
| 1528 | const DexFile* dex_file = (*dex_files_)[i]; |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1529 | if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1530 | PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1531 | << " to " << out->GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1532 | return false; |
| 1533 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1534 | size_dex_file_ += dex_file->GetHeader().file_size_; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1535 | } |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 1536 | for (size_t i = 0; i != oat_classes_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1537 | if (!oat_classes_[i]->Write(this, out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1538 | PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1539 | return false; |
| 1540 | } |
| 1541 | } |
| 1542 | return true; |
| 1543 | } |
| 1544 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1545 | size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) { |
| 1546 | #define VISIT(VisitorType) \ |
| 1547 | do { \ |
| 1548 | VisitorType visitor(this, out, file_offset, relative_offset); \ |
| 1549 | if (UNLIKELY(!VisitDexMethods(&visitor))) { \ |
| 1550 | return 0; \ |
| 1551 | } \ |
| 1552 | relative_offset = visitor.GetOffset(); \ |
| 1553 | } while (false) |
| 1554 | |
| 1555 | size_t gc_maps_offset = relative_offset; |
| 1556 | VISIT(WriteMapMethodVisitor<GcMapDataAccess>); |
| 1557 | size_gc_map_ = relative_offset - gc_maps_offset; |
| 1558 | |
| 1559 | size_t mapping_tables_offset = relative_offset; |
| 1560 | VISIT(WriteMapMethodVisitor<MappingTableDataAccess>); |
| 1561 | size_mapping_table_ = relative_offset - mapping_tables_offset; |
| 1562 | |
| 1563 | size_t vmap_tables_offset = relative_offset; |
| 1564 | VISIT(WriteMapMethodVisitor<VmapTableDataAccess>); |
| 1565 | size_vmap_table_ = relative_offset - vmap_tables_offset; |
| 1566 | |
| 1567 | #undef VISIT |
| 1568 | |
| 1569 | return relative_offset; |
| 1570 | } |
| 1571 | |
| 1572 | size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1573 | off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1574 | relative_offset += size_executable_offset_alignment_; |
| 1575 | DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset()); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1576 | size_t expected_file_offset = file_offset + relative_offset; |
| 1577 | if (static_cast<uint32_t>(new_offset) != expected_file_offset) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 1578 | PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1579 | << " Expected: " << expected_file_offset << " File: " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1580 | return 0; |
| 1581 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1582 | DCHECK_OFFSET(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1583 | if (compiler_driver_->IsImage()) { |
| 1584 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1585 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1586 | #define DO_TRAMPOLINE(field) \ |
| 1587 | do { \ |
| 1588 | uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \ |
| 1589 | uint32_t alignment_padding = aligned_offset - relative_offset; \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1590 | out->Seek(alignment_padding, kSeekCurrent); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1591 | size_trampoline_alignment_ += alignment_padding; \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1592 | if (!out->WriteFully(&(*field)[0], field->size())) { \ |
| 1593 | PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1594 | return false; \ |
| 1595 | } \ |
| 1596 | size_ ## field += field->size(); \ |
| 1597 | relative_offset += alignment_padding + field->size(); \ |
| 1598 | DCHECK_OFFSET(); \ |
| 1599 | } while (false) |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1600 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1601 | DO_TRAMPOLINE(interpreter_to_interpreter_bridge_); |
| 1602 | DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_); |
| 1603 | DO_TRAMPOLINE(jni_dlsym_lookup_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1604 | DO_TRAMPOLINE(portable_imt_conflict_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1605 | DO_TRAMPOLINE(portable_resolution_trampoline_); |
| 1606 | DO_TRAMPOLINE(portable_to_interpreter_bridge_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1607 | DO_TRAMPOLINE(quick_generic_jni_trampoline_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1608 | DO_TRAMPOLINE(quick_imt_conflict_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1609 | DO_TRAMPOLINE(quick_resolution_trampoline_); |
| 1610 | DO_TRAMPOLINE(quick_to_interpreter_bridge_); |
| 1611 | #undef DO_TRAMPOLINE |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1612 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1613 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1614 | } |
| 1615 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1616 | size_t OatWriter::WriteCodeDexFiles(OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1617 | const size_t file_offset, |
| 1618 | size_t relative_offset) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1619 | #define VISIT(VisitorType) \ |
| 1620 | do { \ |
| 1621 | VisitorType visitor(this, out, file_offset, relative_offset); \ |
| 1622 | if (UNLIKELY(!VisitDexMethods(&visitor))) { \ |
| 1623 | return 0; \ |
| 1624 | } \ |
| 1625 | relative_offset = visitor.GetOffset(); \ |
| 1626 | } while (false) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1627 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1628 | VISIT(WriteCodeMethodVisitor); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1629 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1630 | #undef VISIT |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1631 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1632 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1633 | } |
| 1634 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1635 | bool OatWriter::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) { |
| 1636 | static const uint8_t kPadding[] = { |
| 1637 | 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u |
| 1638 | }; |
| 1639 | DCHECK_LE(aligned_code_delta, sizeof(kPadding)); |
| 1640 | if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) { |
| 1641 | return false; |
| 1642 | } |
| 1643 | size_code_alignment_ += aligned_code_delta; |
| 1644 | return true; |
| 1645 | } |
| 1646 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1647 | OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) { |
| 1648 | offset_ = offset; |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 1649 | const std::string& location(dex_file.GetLocation()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1650 | dex_file_location_size_ = location.size(); |
| 1651 | dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data()); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1652 | dex_file_location_checksum_ = dex_file.GetLocationChecksum(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1653 | dex_file_offset_ = 0; |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1654 | methods_offsets_.resize(dex_file.NumClassDefs()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | size_t OatWriter::OatDexFile::SizeOf() const { |
| 1658 | return sizeof(dex_file_location_size_) |
| 1659 | + dex_file_location_size_ |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1660 | + sizeof(dex_file_location_checksum_) |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1661 | + sizeof(dex_file_offset_) |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1662 | + (sizeof(methods_offsets_[0]) * methods_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1663 | } |
| 1664 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1665 | void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const { |
| 1666 | oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_)); |
| 1667 | oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_); |
| 1668 | oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_)); |
| 1669 | oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_)); |
| 1670 | oat_header->UpdateChecksum(&methods_offsets_[0], |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1671 | sizeof(methods_offsets_[0]) * methods_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1672 | } |
| 1673 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1674 | bool OatWriter::OatDexFile::Write(OatWriter* oat_writer, |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1675 | OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1676 | const size_t file_offset) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1677 | DCHECK_OFFSET_(); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1678 | if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) { |
| 1679 | PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1680 | return false; |
| 1681 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1682 | oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1683 | if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) { |
| 1684 | PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1685 | return false; |
| 1686 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1687 | oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_; |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1688 | if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) { |
| 1689 | PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1690 | return false; |
| 1691 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1692 | oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1693 | if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) { |
| 1694 | PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1695 | return false; |
| 1696 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1697 | oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1698 | if (!out->WriteFully(&methods_offsets_[0], |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 1699 | sizeof(methods_offsets_[0]) * methods_offsets_.size())) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1700 | PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1701 | return false; |
| 1702 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1703 | oat_writer->size_oat_dex_file_methods_offsets_ += |
| 1704 | sizeof(methods_offsets_[0]) * methods_offsets_.size(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1705 | return true; |
| 1706 | } |
| 1707 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1708 | OatWriter::OatClass::OatClass(size_t offset, |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1709 | const std::vector<CompiledMethod*>& compiled_methods, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1710 | uint32_t num_non_null_compiled_methods, |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1711 | mirror::Class::Status status) |
| 1712 | : compiled_methods_(compiled_methods) { |
| 1713 | uint32_t num_methods = compiled_methods.size(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1714 | CHECK_LE(num_non_null_compiled_methods, num_methods); |
| 1715 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1716 | offset_ = offset; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1717 | oat_method_offsets_offsets_from_oat_class_.resize(num_methods); |
| 1718 | |
| 1719 | // Since both kOatClassNoneCompiled and kOatClassAllCompiled could |
| 1720 | // apply when there are 0 methods, we just arbitrarily say that 0 |
| 1721 | // methods means kOatClassNoneCompiled and that we won't use |
| 1722 | // kOatClassAllCompiled unless there is at least one compiled |
| 1723 | // method. This means in an interpretter only system, we can assert |
| 1724 | // that all classes are kOatClassNoneCompiled. |
| 1725 | if (num_non_null_compiled_methods == 0) { |
| 1726 | type_ = kOatClassNoneCompiled; |
| 1727 | } else if (num_non_null_compiled_methods == num_methods) { |
| 1728 | type_ = kOatClassAllCompiled; |
| 1729 | } else { |
| 1730 | type_ = kOatClassSomeCompiled; |
| 1731 | } |
| 1732 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1733 | status_ = status; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1734 | method_offsets_.resize(num_non_null_compiled_methods); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 1735 | method_headers_.resize(num_non_null_compiled_methods); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1736 | |
| 1737 | uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_); |
| 1738 | if (type_ == kOatClassSomeCompiled) { |
| 1739 | method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator()); |
| 1740 | method_bitmap_size_ = method_bitmap_->GetSizeOf(); |
| 1741 | oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_); |
| 1742 | oat_method_offsets_offset_from_oat_class += method_bitmap_size_; |
| 1743 | } else { |
| 1744 | method_bitmap_ = NULL; |
| 1745 | method_bitmap_size_ = 0; |
| 1746 | } |
| 1747 | |
| 1748 | for (size_t i = 0; i < num_methods; i++) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1749 | CompiledMethod* compiled_method = compiled_methods_[i]; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1750 | if (compiled_method == NULL) { |
| 1751 | oat_method_offsets_offsets_from_oat_class_[i] = 0; |
| 1752 | } else { |
| 1753 | oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class; |
| 1754 | oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets); |
| 1755 | if (type_ == kOatClassSomeCompiled) { |
| 1756 | method_bitmap_->SetBit(i); |
| 1757 | } |
| 1758 | } |
| 1759 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1760 | } |
| 1761 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1762 | OatWriter::OatClass::~OatClass() { |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 1763 | delete method_bitmap_; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1764 | } |
| 1765 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1766 | size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader( |
| 1767 | size_t class_def_method_index_) const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1768 | uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_); |
| 1769 | if (method_offset == 0) { |
| 1770 | return 0; |
| 1771 | } |
| 1772 | return offset_ + method_offset; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass( |
| 1776 | size_t class_def_method_index_) const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1777 | return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_]; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | size_t OatWriter::OatClass::SizeOf() const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1781 | return sizeof(status_) |
| 1782 | + sizeof(type_) |
| 1783 | + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_)) |
| 1784 | + method_bitmap_size_ |
| 1785 | + (sizeof(method_offsets_[0]) * method_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1786 | } |
| 1787 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1788 | void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const { |
| 1789 | oat_header->UpdateChecksum(&status_, sizeof(status_)); |
| 1790 | oat_header->UpdateChecksum(&type_, sizeof(type_)); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1791 | if (method_bitmap_size_ != 0) { |
| 1792 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1793 | oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_)); |
| 1794 | oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1795 | } |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1796 | oat_header->UpdateChecksum(&method_offsets_[0], |
| 1797 | sizeof(method_offsets_[0]) * method_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1798 | } |
| 1799 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1800 | bool OatWriter::OatClass::Write(OatWriter* oat_writer, |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1801 | OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1802 | const size_t file_offset) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1803 | DCHECK_OFFSET_(); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1804 | if (!out->WriteFully(&status_, sizeof(status_))) { |
| 1805 | PLOG(ERROR) << "Failed to write class status to " << out->GetLocation(); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1806 | return false; |
| 1807 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1808 | oat_writer->size_oat_class_status_ += sizeof(status_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1809 | if (!out->WriteFully(&type_, sizeof(type_))) { |
| 1810 | PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1811 | return false; |
| 1812 | } |
| 1813 | oat_writer->size_oat_class_type_ += sizeof(type_); |
| 1814 | if (method_bitmap_size_ != 0) { |
| 1815 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1816 | if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) { |
| 1817 | PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1818 | return false; |
| 1819 | } |
| 1820 | oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1821 | if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) { |
| 1822 | PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1823 | return false; |
| 1824 | } |
| 1825 | oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_; |
| 1826 | } |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1827 | if (!out->WriteFully(&method_offsets_[0], |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 1828 | sizeof(method_offsets_[0]) * method_offsets_.size())) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1829 | PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1830 | return false; |
| 1831 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1832 | oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1833 | return true; |
| 1834 | } |
| 1835 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1836 | } // namespace art |