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