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 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 21 | #include "base/bit_vector.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 22 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 23 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 24 | #include "class_linker.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 25 | #include "dex_file-inl.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 26 | #include "dex/verification_results.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 27 | #include "gc/space/space.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 28 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | #include "mirror/array.h" |
| 30 | #include "mirror/class_loader.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 31 | #include "mirror/object-inl.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 32 | #include "os.h" |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 33 | #include "output_stream.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 34 | #include "safe_map.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 35 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | c645f1d | 2014-03-06 18:11:53 -0800 | [diff] [blame] | 36 | #include "sirt_ref-inl.h" |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 37 | #include "verifier/method_verifier.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 38 | |
| 39 | namespace art { |
| 40 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 41 | #define DCHECK_OFFSET() \ |
| 42 | DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out->Seek(0, kSeekCurrent)) \ |
| 43 | << "file_offset=" << file_offset << " relative_offset=" << relative_offset |
| 44 | |
| 45 | #define DCHECK_OFFSET_() \ |
| 46 | DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out->Seek(0, kSeekCurrent)) \ |
| 47 | << "file_offset=" << file_offset << " offset_=" << offset_ |
| 48 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 49 | OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 50 | uint32_t image_file_location_oat_checksum, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 51 | uintptr_t image_file_location_oat_begin, |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 52 | const std::string& image_file_location, |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 53 | const CompilerDriver* compiler, |
| 54 | TimingLogger* timings) |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 55 | : compiler_driver_(compiler), |
| 56 | dex_files_(&dex_files), |
| 57 | image_file_location_oat_checksum_(image_file_location_oat_checksum), |
| 58 | image_file_location_oat_begin_(image_file_location_oat_begin), |
| 59 | image_file_location_(image_file_location), |
| 60 | oat_header_(NULL), |
| 61 | size_dex_file_alignment_(0), |
| 62 | size_executable_offset_alignment_(0), |
| 63 | size_oat_header_(0), |
| 64 | size_oat_header_image_file_location_(0), |
| 65 | size_dex_file_(0), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 66 | size_interpreter_to_interpreter_bridge_(0), |
| 67 | size_interpreter_to_compiled_code_bridge_(0), |
| 68 | size_jni_dlsym_lookup_(0), |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 69 | size_portable_imt_conflict_trampoline_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 70 | size_portable_resolution_trampoline_(0), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 71 | size_portable_to_interpreter_bridge_(0), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 72 | size_quick_generic_jni_trampoline_(0), |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 73 | size_quick_imt_conflict_trampoline_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 74 | size_quick_resolution_trampoline_(0), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 75 | size_quick_to_interpreter_bridge_(0), |
| 76 | size_trampoline_alignment_(0), |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 77 | size_method_header_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 78 | size_code_(0), |
| 79 | size_code_alignment_(0), |
| 80 | size_mapping_table_(0), |
| 81 | size_vmap_table_(0), |
| 82 | size_gc_map_(0), |
| 83 | size_oat_dex_file_location_size_(0), |
| 84 | size_oat_dex_file_location_data_(0), |
| 85 | size_oat_dex_file_location_checksum_(0), |
| 86 | size_oat_dex_file_offset_(0), |
| 87 | size_oat_dex_file_methods_offsets_(0), |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 88 | size_oat_class_type_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 89 | size_oat_class_status_(0), |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 90 | size_oat_class_method_bitmaps_(0), |
Dave Allison | d6ed642 | 2014-04-09 23:36:15 +0000 | [diff] [blame] | 91 | size_oat_class_method_offsets_(0) { |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 92 | size_t offset; |
| 93 | { |
| 94 | TimingLogger::ScopedSplit split("InitOatHeader", timings); |
| 95 | offset = InitOatHeader(); |
| 96 | } |
| 97 | { |
| 98 | TimingLogger::ScopedSplit split("InitOatDexFiles", timings); |
| 99 | offset = InitOatDexFiles(offset); |
| 100 | } |
| 101 | { |
| 102 | TimingLogger::ScopedSplit split("InitDexFiles", timings); |
| 103 | offset = InitDexFiles(offset); |
| 104 | } |
| 105 | { |
| 106 | TimingLogger::ScopedSplit split("InitOatClasses", timings); |
| 107 | offset = InitOatClasses(offset); |
| 108 | } |
| 109 | { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 110 | TimingLogger::ScopedSplit split("InitOatMaps", timings); |
| 111 | offset = InitOatMaps(offset); |
| 112 | } |
| 113 | { |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 114 | TimingLogger::ScopedSplit split("InitOatCode", timings); |
| 115 | offset = InitOatCode(offset); |
| 116 | } |
| 117 | { |
| 118 | TimingLogger::ScopedSplit split("InitOatCodeDexFiles", timings); |
| 119 | offset = InitOatCodeDexFiles(offset); |
| 120 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 121 | size_ = offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 122 | |
| 123 | CHECK_EQ(dex_files_->size(), oat_dex_files_.size()); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 124 | CHECK(image_file_location.empty() == compiler->IsImage()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 127 | OatWriter::~OatWriter() { |
| 128 | delete oat_header_; |
| 129 | STLDeleteElements(&oat_dex_files_); |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 130 | STLDeleteElements(&oat_classes_); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 133 | struct OatWriter::GcMapDataAccess { |
| 134 | static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE { |
| 135 | return &compiled_method->GetGcMap(); |
| 136 | } |
| 137 | |
| 138 | static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE { |
| 139 | return oat_class->method_offsets_[method_offsets_index].gc_map_offset_; |
| 140 | } |
| 141 | |
| 142 | static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset) |
| 143 | ALWAYS_INLINE { |
| 144 | oat_class->method_offsets_[method_offsets_index].gc_map_offset_ = offset; |
| 145 | } |
| 146 | |
| 147 | static const char* Name() ALWAYS_INLINE { |
| 148 | return "GC map"; |
| 149 | } |
| 150 | }; |
| 151 | |
| 152 | struct OatWriter::MappingTableDataAccess { |
| 153 | static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE { |
| 154 | return &compiled_method->GetMappingTable(); |
| 155 | } |
| 156 | |
| 157 | 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] | 158 | uint32_t offset = oat_class->method_headers_[method_offsets_index].mapping_table_offset_; |
| 159 | return offset == 0u ? 0u : |
| 160 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset) |
| 164 | ALWAYS_INLINE { |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 165 | oat_class->method_headers_[method_offsets_index].mapping_table_offset_ = |
| 166 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static const char* Name() ALWAYS_INLINE { |
| 170 | return "mapping table"; |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | struct OatWriter::VmapTableDataAccess { |
| 175 | static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE { |
| 176 | return &compiled_method->GetVmapTable(); |
| 177 | } |
| 178 | |
| 179 | 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] | 180 | uint32_t offset = oat_class->method_headers_[method_offsets_index].vmap_table_offset_; |
| 181 | return offset == 0u ? 0u : |
| 182 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset) |
| 186 | ALWAYS_INLINE { |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 187 | oat_class->method_headers_[method_offsets_index].vmap_table_offset_ = |
| 188 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static const char* Name() ALWAYS_INLINE { |
| 192 | return "vmap table"; |
| 193 | } |
| 194 | }; |
| 195 | |
| 196 | class OatWriter::DexMethodVisitor { |
| 197 | public: |
| 198 | DexMethodVisitor(OatWriter* writer, size_t offset) |
| 199 | : writer_(writer), |
| 200 | offset_(offset), |
| 201 | dex_file_(nullptr), |
| 202 | class_def_index_(DexFile::kDexNoIndex) { |
| 203 | } |
| 204 | |
| 205 | virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) { |
| 206 | DCHECK(dex_file_ == nullptr); |
| 207 | DCHECK_EQ(class_def_index_, DexFile::kDexNoIndex); |
| 208 | dex_file_ = dex_file; |
| 209 | class_def_index_ = class_def_index; |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0; |
| 214 | |
| 215 | virtual bool EndClass() { |
| 216 | if (kIsDebugBuild) { |
| 217 | dex_file_ = nullptr; |
| 218 | class_def_index_ = DexFile::kDexNoIndex; |
| 219 | } |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | size_t GetOffset() const { |
| 224 | return offset_; |
| 225 | } |
| 226 | |
| 227 | protected: |
| 228 | virtual ~DexMethodVisitor() { } |
| 229 | |
| 230 | OatWriter* const writer_; |
| 231 | |
| 232 | // The offset is usually advanced for each visited method by the derived class. |
| 233 | size_t offset_; |
| 234 | |
| 235 | // The dex file and class def index are set in StartClass(). |
| 236 | const DexFile* dex_file_; |
| 237 | size_t class_def_index_; |
| 238 | }; |
| 239 | |
| 240 | class OatWriter::OatDexMethodVisitor : public DexMethodVisitor { |
| 241 | public: |
| 242 | OatDexMethodVisitor(OatWriter* writer, size_t offset) |
| 243 | : DexMethodVisitor(writer, offset), |
| 244 | oat_class_index_(0u), |
| 245 | method_offsets_index_(0u) { |
| 246 | } |
| 247 | |
| 248 | bool StartClass(const DexFile* dex_file, size_t class_def_index) { |
| 249 | DexMethodVisitor::StartClass(dex_file, class_def_index); |
| 250 | DCHECK_LT(oat_class_index_, writer_->oat_classes_.size()); |
| 251 | method_offsets_index_ = 0u; |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | bool EndClass() { |
| 256 | ++oat_class_index_; |
| 257 | return DexMethodVisitor::EndClass(); |
| 258 | } |
| 259 | |
| 260 | protected: |
| 261 | size_t oat_class_index_; |
| 262 | size_t method_offsets_index_; |
| 263 | }; |
| 264 | |
| 265 | class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor { |
| 266 | public: |
| 267 | InitOatClassesMethodVisitor(OatWriter* writer, size_t offset) |
| 268 | : DexMethodVisitor(writer, offset), |
| 269 | compiled_methods_(), |
| 270 | num_non_null_compiled_methods_(0u) { |
| 271 | compiled_methods_.reserve(256u); |
| 272 | } |
| 273 | |
| 274 | bool StartClass(const DexFile* dex_file, size_t class_def_index) { |
| 275 | DexMethodVisitor::StartClass(dex_file, class_def_index); |
| 276 | compiled_methods_.clear(); |
| 277 | num_non_null_compiled_methods_ = 0u; |
| 278 | return true; |
| 279 | } |
| 280 | |
| 281 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) { |
| 282 | // Fill in the compiled_methods_ array for methods that have a |
| 283 | // CompiledMethod. We track the number of non-null entries in |
| 284 | // num_non_null_compiled_methods_ since we only want to allocate |
| 285 | // OatMethodOffsets for the compiled methods. |
| 286 | uint32_t method_idx = it.GetMemberIndex(); |
| 287 | CompiledMethod* compiled_method = |
| 288 | writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx)); |
| 289 | compiled_methods_.push_back(compiled_method); |
| 290 | if (compiled_method != nullptr) { |
| 291 | ++num_non_null_compiled_methods_; |
| 292 | } |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | bool EndClass() { |
| 297 | ClassReference class_ref(dex_file_, class_def_index_); |
| 298 | CompiledClass* compiled_class = writer_->compiler_driver_->GetCompiledClass(class_ref); |
| 299 | mirror::Class::Status status; |
| 300 | if (compiled_class != NULL) { |
| 301 | status = compiled_class->GetStatus(); |
| 302 | } else if (writer_->compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) { |
| 303 | status = mirror::Class::kStatusError; |
| 304 | } else { |
| 305 | status = mirror::Class::kStatusNotReady; |
| 306 | } |
| 307 | |
| 308 | OatClass* oat_class = new OatClass(offset_, compiled_methods_, |
| 309 | num_non_null_compiled_methods_, status); |
| 310 | writer_->oat_classes_.push_back(oat_class); |
| 311 | offset_ += oat_class->SizeOf(); |
| 312 | return DexMethodVisitor::EndClass(); |
| 313 | } |
| 314 | |
| 315 | private: |
| 316 | std::vector<CompiledMethod*> compiled_methods_; |
| 317 | size_t num_non_null_compiled_methods_; |
| 318 | }; |
| 319 | |
| 320 | class OatWriter::InitCodeMethodVisitor : public OatDexMethodVisitor { |
| 321 | public: |
| 322 | InitCodeMethodVisitor(OatWriter* writer, size_t offset) |
| 323 | : OatDexMethodVisitor(writer, offset) { |
| 324 | } |
| 325 | |
| 326 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
| 327 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 328 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 329 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 330 | |
| 331 | if (compiled_method != nullptr) { |
| 332 | // Derived from CompiledMethod. |
| 333 | uint32_t quick_code_offset = 0; |
| 334 | uint32_t frame_size_in_bytes = kStackAlignment; |
| 335 | uint32_t core_spill_mask = 0; |
| 336 | uint32_t fp_spill_mask = 0; |
| 337 | |
| 338 | const std::vector<uint8_t>* portable_code = compiled_method->GetPortableCode(); |
| 339 | const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode(); |
| 340 | if (portable_code != nullptr) { |
| 341 | CHECK(quick_code == nullptr); |
| 342 | size_t oat_method_offsets_offset = |
| 343 | oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index); |
| 344 | compiled_method->AddOatdataOffsetToCompliledCodeOffset( |
| 345 | oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_)); |
| 346 | } else { |
| 347 | CHECK(quick_code != nullptr); |
| 348 | offset_ = compiled_method->AlignCode(offset_); |
| 349 | DCHECK_ALIGNED_PARAM(offset_, |
| 350 | GetInstructionSetAlignment(compiled_method->GetInstructionSet())); |
| 351 | uint32_t code_size = quick_code->size() * sizeof(uint8_t); |
| 352 | CHECK_NE(code_size, 0U); |
| 353 | uint32_t thumb_offset = compiled_method->CodeDelta(); |
| 354 | quick_code_offset = offset_ + sizeof(OatMethodHeader) + thumb_offset; |
| 355 | |
| 356 | std::vector<uint8_t>* cfi_info = writer_->compiler_driver_->GetCallFrameInformation(); |
| 357 | if (cfi_info != nullptr) { |
| 358 | // Copy in the FDE, if present |
| 359 | const std::vector<uint8_t>* fde = compiled_method->GetCFIInfo(); |
| 360 | if (fde != nullptr) { |
| 361 | // Copy the information into cfi_info and then fix the address in the new copy. |
| 362 | int cur_offset = cfi_info->size(); |
| 363 | cfi_info->insert(cfi_info->end(), fde->begin(), fde->end()); |
| 364 | |
| 365 | // Set the 'initial_location' field to address the start of the method. |
| 366 | uint32_t new_value = quick_code_offset - writer_->oat_header_->GetExecutableOffset(); |
| 367 | uint32_t offset_to_update = cur_offset + 2*sizeof(uint32_t); |
| 368 | (*cfi_info)[offset_to_update+0] = new_value; |
| 369 | (*cfi_info)[offset_to_update+1] = new_value >> 8; |
| 370 | (*cfi_info)[offset_to_update+2] = new_value >> 16; |
| 371 | (*cfi_info)[offset_to_update+3] = new_value >> 24; |
| 372 | std::string name = PrettyMethod(it.GetMemberIndex(), *dex_file_, false); |
| 373 | writer_->method_info_.push_back(DebugInfo(name, new_value, new_value + code_size)); |
| 374 | } |
| 375 | } |
| 376 | |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 377 | DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size()); |
| 378 | OatMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_]; |
| 379 | method_header->code_size_ = code_size; |
| 380 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 381 | // Deduplicate code arrays. |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 382 | auto code_iter = dedupe_map_.find(compiled_method); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 383 | if (code_iter != dedupe_map_.end()) { |
| 384 | quick_code_offset = code_iter->second; |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 385 | FixupMethodHeader(method_header, quick_code_offset - thumb_offset); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 386 | } else { |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 387 | dedupe_map_.Put(compiled_method, quick_code_offset); |
| 388 | FixupMethodHeader(method_header, quick_code_offset - thumb_offset); |
| 389 | writer_->oat_header_->UpdateChecksum(method_header, sizeof(*method_header)); |
| 390 | offset_ += sizeof(*method_header); // Method header is prepended before code. |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 391 | writer_->oat_header_->UpdateChecksum(&(*quick_code)[0], code_size); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 392 | offset_ += code_size; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | frame_size_in_bytes = compiled_method->GetFrameSizeInBytes(); |
| 396 | core_spill_mask = compiled_method->GetCoreSpillMask(); |
| 397 | fp_spill_mask = compiled_method->GetFpSpillMask(); |
| 398 | |
| 399 | if (kIsDebugBuild) { |
| 400 | // We expect GC maps except when the class hasn't been verified or the method is native. |
| 401 | const CompilerDriver* compiler_driver = writer_->compiler_driver_; |
| 402 | ClassReference class_ref(dex_file_, class_def_index_); |
| 403 | CompiledClass* compiled_class = compiler_driver->GetCompiledClass(class_ref); |
| 404 | mirror::Class::Status status; |
| 405 | if (compiled_class != NULL) { |
| 406 | status = compiled_class->GetStatus(); |
| 407 | } else if (compiler_driver->GetVerificationResults()->IsClassRejected(class_ref)) { |
| 408 | status = mirror::Class::kStatusError; |
| 409 | } else { |
| 410 | status = mirror::Class::kStatusNotReady; |
| 411 | } |
| 412 | const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap(); |
| 413 | size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]); |
| 414 | bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0; |
| 415 | CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified) |
| 416 | << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " " |
| 417 | << (status < mirror::Class::kStatusVerified) << " " << status << " " |
| 418 | << PrettyMethod(it.GetMemberIndex(), *dex_file_); |
| 419 | } |
| 420 | |
| 421 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 422 | OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_]; |
| 423 | offsets->code_offset_ = quick_code_offset; |
| 424 | offsets->frame_size_in_bytes_ = frame_size_in_bytes; |
| 425 | offsets->core_spill_mask_ = core_spill_mask; |
| 426 | offsets->fp_spill_mask_ = fp_spill_mask; |
| 427 | ++method_offsets_index_; |
| 428 | } |
| 429 | |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | private: |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 434 | static void FixupMethodHeader(OatMethodHeader* method_header, uint32_t code_offset) { |
| 435 | // The code offset was 0 when the mapping/vmap table offset was set, so it's set |
| 436 | // to 0-offset and we need to adjust it by code_offset. |
| 437 | if (method_header->mapping_table_offset_ != 0u) { |
| 438 | method_header->mapping_table_offset_ += code_offset; |
| 439 | DCHECK_LT(method_header->mapping_table_offset_, code_offset); |
| 440 | } |
| 441 | if (method_header->vmap_table_offset_ != 0u) { |
| 442 | method_header->vmap_table_offset_ += code_offset; |
| 443 | DCHECK_LT(method_header->vmap_table_offset_, code_offset); |
| 444 | } |
| 445 | } |
| 446 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 447 | // Deduplication is already done on a pointer basis by the compiler driver, |
| 448 | // 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] | 449 | SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 450 | }; |
| 451 | |
| 452 | template <typename DataAccess> |
| 453 | class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor { |
| 454 | public: |
| 455 | InitMapMethodVisitor(OatWriter* writer, size_t offset) |
| 456 | : OatDexMethodVisitor(writer, offset) { |
| 457 | } |
| 458 | |
| 459 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
| 460 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 461 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 462 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 463 | |
| 464 | if (compiled_method != nullptr) { |
| 465 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 466 | DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u); |
| 467 | |
| 468 | const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method); |
| 469 | uint32_t map_size = map->size() * sizeof((*map)[0]); |
| 470 | if (map_size != 0u) { |
| 471 | auto it = dedupe_map_.find(map); |
| 472 | if (it != dedupe_map_.end()) { |
| 473 | DataAccess::SetOffset(oat_class, method_offsets_index_, it->second); |
| 474 | } else { |
| 475 | DataAccess::SetOffset(oat_class, method_offsets_index_, offset_); |
| 476 | dedupe_map_.Put(map, offset_); |
| 477 | offset_ += map_size; |
| 478 | writer_->oat_header_->UpdateChecksum(&(*map)[0], map_size); |
| 479 | } |
| 480 | } |
| 481 | ++method_offsets_index_; |
| 482 | } |
| 483 | |
| 484 | return true; |
| 485 | } |
| 486 | |
| 487 | private: |
| 488 | // Deduplication is already done on a pointer basis by the compiler driver, |
| 489 | // so we can simply compare the pointers to find out if things are duplicated. |
| 490 | SafeMap<const std::vector<uint8_t>*, uint32_t> dedupe_map_; |
| 491 | }; |
| 492 | |
| 493 | class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor { |
| 494 | public: |
| 495 | InitImageMethodVisitor(OatWriter* writer, size_t offset) |
| 496 | : OatDexMethodVisitor(writer, offset) { |
| 497 | } |
| 498 | |
| 499 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
| 500 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 501 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 502 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 503 | |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 504 | OatMethodOffsets offsets(0u, kStackAlignment, 0u, 0u, 0u); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 505 | if (compiled_method != nullptr) { |
| 506 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 507 | offsets = oat_class->method_offsets_[method_offsets_index_]; |
| 508 | ++method_offsets_index_; |
| 509 | } |
| 510 | |
| 511 | // Derive frame size and spill masks for native methods without code: |
| 512 | // These are generic JNI methods... |
| 513 | uint32_t method_idx = it.GetMemberIndex(); |
| 514 | bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0; |
| 515 | if (is_native && compiled_method == nullptr) { |
| 516 | // Compute Sirt size as putting _every_ reference into it, even null ones. |
| 517 | uint32_t s_len; |
| 518 | const char* shorty = dex_file_->GetMethodShorty(dex_file_->GetMethodId(method_idx), |
| 519 | &s_len); |
| 520 | DCHECK(shorty != nullptr); |
| 521 | uint32_t refs = 1; // Native method always has "this" or class. |
| 522 | for (uint32_t i = 1; i < s_len; ++i) { |
| 523 | if (shorty[i] == 'L') { |
| 524 | refs++; |
| 525 | } |
| 526 | } |
| 527 | size_t pointer_size = GetInstructionSetPointerSize( |
| 528 | writer_->compiler_driver_->GetInstructionSet()); |
| 529 | size_t sirt_size = StackIndirectReferenceTable::GetAlignedSirtSizeTarget(pointer_size, refs); |
| 530 | |
| 531 | // Get the generic spill masks and base frame size. |
| 532 | mirror::ArtMethod* callee_save_method = |
| 533 | Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs); |
| 534 | |
| 535 | offsets.frame_size_in_bytes_ = callee_save_method->GetFrameSizeInBytes() + sirt_size; |
| 536 | offsets.core_spill_mask_ = callee_save_method->GetCoreSpillMask(); |
| 537 | offsets.fp_spill_mask_ = callee_save_method->GetFpSpillMask(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 538 | DCHECK_EQ(offsets.gc_map_offset_, 0u); |
| 539 | } |
| 540 | |
| 541 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 542 | InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_)); |
| 543 | // Unchecked as we hold mutator_lock_ on entry. |
| 544 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 545 | SirtRef<mirror::DexCache> dex_cache(soa.Self(), linker->FindDexCache(*dex_file_)); |
| 546 | SirtRef<mirror::ClassLoader> class_loader(soa.Self(), nullptr); |
| 547 | mirror::ArtMethod* method = linker->ResolveMethod(*dex_file_, method_idx, dex_cache, |
| 548 | class_loader, nullptr, invoke_type); |
| 549 | CHECK(method != NULL); |
| 550 | method->SetFrameSizeInBytes(offsets.frame_size_in_bytes_); |
| 551 | method->SetCoreSpillMask(offsets.core_spill_mask_); |
| 552 | method->SetFpSpillMask(offsets.fp_spill_mask_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 553 | // Portable code offsets are set by ElfWriterMclinker::FixupCompiledCodeOffset after linking. |
| 554 | method->SetQuickOatCodeOffset(offsets.code_offset_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 555 | method->SetOatNativeGcMapOffset(offsets.gc_map_offset_); |
| 556 | |
| 557 | return true; |
| 558 | } |
| 559 | }; |
| 560 | |
| 561 | class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor { |
| 562 | public: |
| 563 | WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset, |
| 564 | size_t relative_offset) |
| 565 | : OatDexMethodVisitor(writer, relative_offset), |
| 566 | out_(out), |
| 567 | file_offset_(file_offset) { |
| 568 | } |
| 569 | |
| 570 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) { |
| 571 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 572 | const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 573 | |
| 574 | if (compiled_method != NULL) { // ie. not an abstract method |
| 575 | size_t file_offset = file_offset_; |
| 576 | OutputStream* out = out_; |
| 577 | |
| 578 | const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode(); |
| 579 | if (quick_code != nullptr) { |
| 580 | CHECK(compiled_method->GetPortableCode() == nullptr); |
| 581 | uint32_t aligned_offset = compiled_method->AlignCode(offset_); |
| 582 | uint32_t aligned_code_delta = aligned_offset - offset_; |
| 583 | if (aligned_code_delta != 0) { |
| 584 | static const uint8_t kPadding[] = { |
| 585 | 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u |
| 586 | }; |
| 587 | DCHECK_LE(aligned_code_delta, sizeof(kPadding)); |
| 588 | if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) { |
| 589 | ReportWriteFailure("code alignment padding", it); |
| 590 | return false; |
| 591 | } |
| 592 | writer_->size_code_alignment_ += aligned_code_delta; |
| 593 | offset_ += aligned_code_delta; |
| 594 | DCHECK_OFFSET_(); |
| 595 | } |
| 596 | DCHECK_ALIGNED_PARAM(offset_, |
| 597 | GetInstructionSetAlignment(compiled_method->GetInstructionSet())); |
| 598 | uint32_t code_size = quick_code->size() * sizeof(uint8_t); |
| 599 | CHECK_NE(code_size, 0U); |
| 600 | |
| 601 | // Deduplicate code arrays. |
| 602 | const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_]; |
| 603 | DCHECK(method_offsets.code_offset_ < offset_ || method_offsets.code_offset_ == |
| 604 | offset_ + sizeof(OatMethodHeader) + compiled_method->CodeDelta()) |
| 605 | << PrettyMethod(it.GetMemberIndex(), *dex_file_); |
| 606 | if (method_offsets.code_offset_ >= offset_) { |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 607 | const OatMethodHeader& method_header = oat_class->method_headers_[method_offsets_index_]; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 608 | if (!out->WriteFully(&method_header, sizeof(method_header))) { |
| 609 | ReportWriteFailure("method header", it); |
| 610 | return false; |
| 611 | } |
| 612 | writer_->size_method_header_ += sizeof(method_header); |
| 613 | offset_ += sizeof(method_header); |
| 614 | DCHECK_OFFSET_(); |
| 615 | if (!out->WriteFully(&(*quick_code)[0], code_size)) { |
| 616 | ReportWriteFailure("method code", it); |
| 617 | return false; |
| 618 | } |
| 619 | writer_->size_code_ += code_size; |
| 620 | offset_ += code_size; |
| 621 | } |
| 622 | DCHECK_OFFSET_(); |
| 623 | } |
| 624 | ++method_offsets_index_; |
| 625 | } |
| 626 | |
| 627 | return true; |
| 628 | } |
| 629 | |
| 630 | private: |
| 631 | OutputStream* const out_; |
| 632 | size_t const file_offset_; |
| 633 | |
| 634 | void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) { |
| 635 | PLOG(ERROR) << "Failed to write " << what << " for " |
| 636 | << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation(); |
| 637 | } |
| 638 | }; |
| 639 | |
| 640 | template <typename DataAccess> |
| 641 | class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor { |
| 642 | public: |
| 643 | WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset, |
| 644 | size_t relative_offset) |
| 645 | : OatDexMethodVisitor(writer, relative_offset), |
| 646 | out_(out), |
| 647 | file_offset_(file_offset) { |
| 648 | } |
| 649 | |
| 650 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) { |
| 651 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 652 | const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 653 | |
| 654 | if (compiled_method != NULL) { // ie. not an abstract method |
| 655 | size_t file_offset = file_offset_; |
| 656 | OutputStream* out = out_; |
| 657 | |
| 658 | uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_); |
| 659 | ++method_offsets_index_; |
| 660 | |
| 661 | // Write deduplicated map. |
| 662 | const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method); |
| 663 | size_t map_size = map->size() * sizeof((*map)[0]); |
| 664 | DCHECK((map_size == 0u && map_offset == 0u) || |
| 665 | (map_size != 0u && map_offset != 0u && map_offset <= offset_)) |
| 666 | << PrettyMethod(it.GetMemberIndex(), *dex_file_); |
| 667 | if (map_size != 0u && map_offset == offset_) { |
| 668 | if (UNLIKELY(!out->WriteFully(&(*map)[0], map_size))) { |
| 669 | ReportWriteFailure(it); |
| 670 | return false; |
| 671 | } |
| 672 | offset_ += map_size; |
| 673 | } |
| 674 | DCHECK_OFFSET_(); |
| 675 | } |
| 676 | |
| 677 | return true; |
| 678 | } |
| 679 | |
| 680 | private: |
| 681 | OutputStream* const out_; |
| 682 | size_t const file_offset_; |
| 683 | |
| 684 | void ReportWriteFailure(const ClassDataItemIterator& it) { |
| 685 | PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for " |
| 686 | << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation(); |
| 687 | } |
| 688 | }; |
| 689 | |
| 690 | // Visit all methods from all classes in all dex files with the specified visitor. |
| 691 | bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) { |
| 692 | for (const DexFile* dex_file : *dex_files_) { |
| 693 | const size_t class_def_count = dex_file->NumClassDefs(); |
| 694 | for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) { |
| 695 | if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) { |
| 696 | return false; |
| 697 | } |
| 698 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
| 699 | const byte* class_data = dex_file->GetClassData(class_def); |
| 700 | if (class_data != NULL) { // ie not an empty class, such as a marker interface |
| 701 | ClassDataItemIterator it(*dex_file, class_data); |
| 702 | while (it.HasNextStaticField()) { |
| 703 | it.Next(); |
| 704 | } |
| 705 | while (it.HasNextInstanceField()) { |
| 706 | it.Next(); |
| 707 | } |
| 708 | size_t class_def_method_index = 0u; |
| 709 | while (it.HasNextDirectMethod()) { |
| 710 | if (!visitor->VisitMethod(class_def_method_index, it)) { |
| 711 | return false; |
| 712 | } |
| 713 | ++class_def_method_index; |
| 714 | it.Next(); |
| 715 | } |
| 716 | while (it.HasNextVirtualMethod()) { |
| 717 | if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) { |
| 718 | return false; |
| 719 | } |
| 720 | ++class_def_method_index; |
| 721 | it.Next(); |
| 722 | } |
| 723 | } |
| 724 | if (UNLIKELY(!visitor->EndClass())) { |
| 725 | return false; |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | return true; |
| 730 | } |
| 731 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 732 | size_t OatWriter::InitOatHeader() { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 733 | // create the OatHeader |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 734 | oat_header_ = new OatHeader(compiler_driver_->GetInstructionSet(), |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 735 | compiler_driver_->GetInstructionSetFeatures(), |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 736 | dex_files_, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 737 | image_file_location_oat_checksum_, |
| 738 | image_file_location_oat_begin_, |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 739 | image_file_location_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 740 | size_t offset = sizeof(*oat_header_); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 741 | offset += image_file_location_.size(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 742 | return offset; |
| 743 | } |
| 744 | |
| 745 | size_t OatWriter::InitOatDexFiles(size_t offset) { |
| 746 | // create the OatDexFiles |
| 747 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 748 | const DexFile* dex_file = (*dex_files_)[i]; |
| 749 | CHECK(dex_file != NULL); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 750 | OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 751 | oat_dex_files_.push_back(oat_dex_file); |
| 752 | offset += oat_dex_file->SizeOf(); |
| 753 | } |
| 754 | return offset; |
| 755 | } |
| 756 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 757 | size_t OatWriter::InitDexFiles(size_t offset) { |
| 758 | // calculate the offsets within OatDexFiles to the DexFiles |
| 759 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 760 | // dex files are required to be 4 byte aligned |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 761 | size_t original_offset = offset; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 762 | offset = RoundUp(offset, 4); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 763 | size_dex_file_alignment_ += offset - original_offset; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 764 | |
| 765 | // set offset in OatDexFile to DexFile |
| 766 | oat_dex_files_[i]->dex_file_offset_ = offset; |
| 767 | |
| 768 | const DexFile* dex_file = (*dex_files_)[i]; |
| 769 | offset += dex_file->GetHeader().file_size_; |
| 770 | } |
| 771 | return offset; |
| 772 | } |
| 773 | |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 774 | size_t OatWriter::InitOatClasses(size_t offset) { |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 775 | // calculate the offsets within OatDexFiles to OatClasses |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 776 | InitOatClassesMethodVisitor visitor(this, offset); |
| 777 | bool success = VisitDexMethods(&visitor); |
| 778 | CHECK(success); |
| 779 | offset = visitor.GetOffset(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 780 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 781 | // Update oat_dex_files_. |
| 782 | auto oat_class_it = oat_classes_.begin(); |
| 783 | for (OatDexFile* oat_dex_file : oat_dex_files_) { |
| 784 | for (uint32_t& offset : oat_dex_file->methods_offsets_) { |
| 785 | DCHECK(oat_class_it != oat_classes_.end()); |
| 786 | offset = (*oat_class_it)->offset_; |
| 787 | ++oat_class_it; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 788 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 789 | oat_dex_file->UpdateChecksum(oat_header_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 790 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 791 | CHECK(oat_class_it == oat_classes_.end()); |
| 792 | |
| 793 | return offset; |
| 794 | } |
| 795 | |
| 796 | size_t OatWriter::InitOatMaps(size_t offset) { |
| 797 | #define VISIT(VisitorType) \ |
| 798 | do { \ |
| 799 | VisitorType visitor(this, offset); \ |
| 800 | bool success = VisitDexMethods(&visitor); \ |
| 801 | DCHECK(success); \ |
| 802 | offset = visitor.GetOffset(); \ |
| 803 | } while (false) |
| 804 | |
| 805 | VISIT(InitMapMethodVisitor<GcMapDataAccess>); |
| 806 | VISIT(InitMapMethodVisitor<MappingTableDataAccess>); |
| 807 | VISIT(InitMapMethodVisitor<VmapTableDataAccess>); |
| 808 | |
| 809 | #undef VISIT |
| 810 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 811 | return offset; |
| 812 | } |
| 813 | |
| 814 | size_t OatWriter::InitOatCode(size_t offset) { |
| 815 | // calculate the offsets within OatHeader to executable code |
| 816 | size_t old_offset = offset; |
| 817 | // required to be on a new page boundary |
| 818 | offset = RoundUp(offset, kPageSize); |
| 819 | oat_header_->SetExecutableOffset(offset); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 820 | size_executable_offset_alignment_ = offset - old_offset; |
| 821 | if (compiler_driver_->IsImage()) { |
| 822 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 823 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 824 | #define DO_TRAMPOLINE(field, fn_name) \ |
| 825 | offset = CompiledCode::AlignCode(offset, instruction_set); \ |
| 826 | oat_header_->Set ## fn_name ## Offset(offset); \ |
| 827 | field.reset(compiler_driver_->Create ## fn_name()); \ |
| 828 | offset += field->size(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 829 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 830 | DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge); |
| 831 | DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge); |
| 832 | DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 833 | DO_TRAMPOLINE(portable_imt_conflict_trampoline_, PortableImtConflictTrampoline); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 834 | DO_TRAMPOLINE(portable_resolution_trampoline_, PortableResolutionTrampoline); |
| 835 | DO_TRAMPOLINE(portable_to_interpreter_bridge_, PortableToInterpreterBridge); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 836 | DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 837 | DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 838 | DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline); |
| 839 | DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 840 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 841 | #undef DO_TRAMPOLINE |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 842 | } else { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 843 | oat_header_->SetInterpreterToInterpreterBridgeOffset(0); |
| 844 | oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0); |
| 845 | oat_header_->SetJniDlsymLookupOffset(0); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 846 | oat_header_->SetPortableImtConflictTrampolineOffset(0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 847 | oat_header_->SetPortableResolutionTrampolineOffset(0); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 848 | oat_header_->SetPortableToInterpreterBridgeOffset(0); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 849 | oat_header_->SetQuickGenericJniTrampolineOffset(0); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 850 | oat_header_->SetQuickImtConflictTrampolineOffset(0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 851 | oat_header_->SetQuickResolutionTrampolineOffset(0); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 852 | oat_header_->SetQuickToInterpreterBridgeOffset(0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 853 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 854 | return offset; |
| 855 | } |
| 856 | |
| 857 | size_t OatWriter::InitOatCodeDexFiles(size_t offset) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 858 | #define VISIT(VisitorType) \ |
| 859 | do { \ |
| 860 | VisitorType visitor(this, offset); \ |
| 861 | bool success = VisitDexMethods(&visitor); \ |
| 862 | DCHECK(success); \ |
| 863 | offset = visitor.GetOffset(); \ |
| 864 | } while (false) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 865 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 866 | VISIT(InitCodeMethodVisitor); |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 867 | if (compiler_driver_->IsImage()) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 868 | VISIT(InitImageMethodVisitor); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 869 | } |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 870 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 871 | #undef VISIT |
| 872 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 873 | return offset; |
| 874 | } |
| 875 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 876 | bool OatWriter::Write(OutputStream* out) { |
| 877 | const size_t file_offset = out->Seek(0, kSeekCurrent); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 878 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 879 | if (!out->WriteFully(oat_header_, sizeof(*oat_header_))) { |
| 880 | PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 881 | return false; |
| 882 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 883 | size_oat_header_ += sizeof(*oat_header_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 884 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 885 | if (!out->WriteFully(image_file_location_.data(), image_file_location_.size())) { |
| 886 | PLOG(ERROR) << "Failed to write oat header image file location to " << out->GetLocation(); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 887 | return false; |
| 888 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 889 | size_oat_header_image_file_location_ += image_file_location_.size(); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 890 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 891 | if (!WriteTables(out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 892 | LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 893 | return false; |
| 894 | } |
| 895 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 896 | size_t relative_offset = out->Seek(0, kSeekCurrent) - file_offset; |
| 897 | relative_offset = WriteMaps(out, file_offset, relative_offset); |
| 898 | if (relative_offset == 0) { |
| 899 | LOG(ERROR) << "Failed to write oat code to " << out->GetLocation(); |
| 900 | return false; |
| 901 | } |
| 902 | |
| 903 | relative_offset = WriteCode(out, file_offset, relative_offset); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 904 | if (relative_offset == 0) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 905 | LOG(ERROR) << "Failed to write oat code to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 906 | return false; |
| 907 | } |
| 908 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 909 | relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset); |
| 910 | if (relative_offset == 0) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 911 | 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] | 912 | return false; |
| 913 | } |
| 914 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 915 | if (kIsDebugBuild) { |
| 916 | uint32_t size_total = 0; |
| 917 | #define DO_STAT(x) \ |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 918 | VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \ |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 919 | size_total += x; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 920 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 921 | DO_STAT(size_dex_file_alignment_); |
| 922 | DO_STAT(size_executable_offset_alignment_); |
| 923 | DO_STAT(size_oat_header_); |
| 924 | DO_STAT(size_oat_header_image_file_location_); |
| 925 | DO_STAT(size_dex_file_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 926 | DO_STAT(size_interpreter_to_interpreter_bridge_); |
| 927 | DO_STAT(size_interpreter_to_compiled_code_bridge_); |
| 928 | DO_STAT(size_jni_dlsym_lookup_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 929 | DO_STAT(size_portable_imt_conflict_trampoline_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 930 | DO_STAT(size_portable_resolution_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 931 | DO_STAT(size_portable_to_interpreter_bridge_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 932 | DO_STAT(size_quick_generic_jni_trampoline_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 933 | DO_STAT(size_quick_imt_conflict_trampoline_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 934 | DO_STAT(size_quick_resolution_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 935 | DO_STAT(size_quick_to_interpreter_bridge_); |
| 936 | DO_STAT(size_trampoline_alignment_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 937 | DO_STAT(size_method_header_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 938 | DO_STAT(size_code_); |
| 939 | DO_STAT(size_code_alignment_); |
| 940 | DO_STAT(size_mapping_table_); |
| 941 | DO_STAT(size_vmap_table_); |
| 942 | DO_STAT(size_gc_map_); |
| 943 | DO_STAT(size_oat_dex_file_location_size_); |
| 944 | DO_STAT(size_oat_dex_file_location_data_); |
| 945 | DO_STAT(size_oat_dex_file_location_checksum_); |
| 946 | DO_STAT(size_oat_dex_file_offset_); |
| 947 | DO_STAT(size_oat_dex_file_methods_offsets_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 948 | DO_STAT(size_oat_class_type_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 949 | DO_STAT(size_oat_class_status_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 950 | DO_STAT(size_oat_class_method_bitmaps_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 951 | DO_STAT(size_oat_class_method_offsets_); |
| 952 | #undef DO_STAT |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 953 | |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 954 | VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 955 | CHECK_EQ(file_offset + size_total, static_cast<uint32_t>(out->Seek(0, kSeekCurrent))); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 956 | CHECK_EQ(size_, size_total); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 957 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 958 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 959 | CHECK_EQ(file_offset + size_, static_cast<uint32_t>(out->Seek(0, kSeekCurrent))); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 960 | CHECK_EQ(size_, relative_offset); |
| 961 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 962 | return true; |
| 963 | } |
| 964 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 965 | bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 966 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 967 | if (!oat_dex_files_[i]->Write(this, out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 968 | PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 969 | return false; |
| 970 | } |
| 971 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 972 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 973 | 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] | 974 | off_t actual_offset = out->Seek(expected_offset, kSeekSet); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 975 | if (static_cast<uint32_t>(actual_offset) != expected_offset) { |
| 976 | const DexFile* dex_file = (*dex_files_)[i]; |
| 977 | PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset |
| 978 | << " Expected: " << expected_offset << " File: " << dex_file->GetLocation(); |
| 979 | return false; |
| 980 | } |
| 981 | const DexFile* dex_file = (*dex_files_)[i]; |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 982 | if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 983 | PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 984 | << " to " << out->GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 985 | return false; |
| 986 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 987 | size_dex_file_ += dex_file->GetHeader().file_size_; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 988 | } |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 989 | for (size_t i = 0; i != oat_classes_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 990 | if (!oat_classes_[i]->Write(this, out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 991 | PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 992 | return false; |
| 993 | } |
| 994 | } |
| 995 | return true; |
| 996 | } |
| 997 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 998 | size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) { |
| 999 | #define VISIT(VisitorType) \ |
| 1000 | do { \ |
| 1001 | VisitorType visitor(this, out, file_offset, relative_offset); \ |
| 1002 | if (UNLIKELY(!VisitDexMethods(&visitor))) { \ |
| 1003 | return 0; \ |
| 1004 | } \ |
| 1005 | relative_offset = visitor.GetOffset(); \ |
| 1006 | } while (false) |
| 1007 | |
| 1008 | size_t gc_maps_offset = relative_offset; |
| 1009 | VISIT(WriteMapMethodVisitor<GcMapDataAccess>); |
| 1010 | size_gc_map_ = relative_offset - gc_maps_offset; |
| 1011 | |
| 1012 | size_t mapping_tables_offset = relative_offset; |
| 1013 | VISIT(WriteMapMethodVisitor<MappingTableDataAccess>); |
| 1014 | size_mapping_table_ = relative_offset - mapping_tables_offset; |
| 1015 | |
| 1016 | size_t vmap_tables_offset = relative_offset; |
| 1017 | VISIT(WriteMapMethodVisitor<VmapTableDataAccess>); |
| 1018 | size_vmap_table_ = relative_offset - vmap_tables_offset; |
| 1019 | |
| 1020 | #undef VISIT |
| 1021 | |
| 1022 | return relative_offset; |
| 1023 | } |
| 1024 | |
| 1025 | 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] | 1026 | off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1027 | relative_offset += size_executable_offset_alignment_; |
| 1028 | DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset()); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1029 | size_t expected_file_offset = file_offset + relative_offset; |
| 1030 | if (static_cast<uint32_t>(new_offset) != expected_file_offset) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 1031 | PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1032 | << " Expected: " << expected_file_offset << " File: " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1033 | return 0; |
| 1034 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1035 | DCHECK_OFFSET(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1036 | if (compiler_driver_->IsImage()) { |
| 1037 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1038 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1039 | #define DO_TRAMPOLINE(field) \ |
| 1040 | do { \ |
| 1041 | uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \ |
| 1042 | uint32_t alignment_padding = aligned_offset - relative_offset; \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1043 | out->Seek(alignment_padding, kSeekCurrent); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1044 | size_trampoline_alignment_ += alignment_padding; \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1045 | if (!out->WriteFully(&(*field)[0], field->size())) { \ |
| 1046 | PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1047 | return false; \ |
| 1048 | } \ |
| 1049 | size_ ## field += field->size(); \ |
| 1050 | relative_offset += alignment_padding + field->size(); \ |
| 1051 | DCHECK_OFFSET(); \ |
| 1052 | } while (false) |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1053 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1054 | DO_TRAMPOLINE(interpreter_to_interpreter_bridge_); |
| 1055 | DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_); |
| 1056 | DO_TRAMPOLINE(jni_dlsym_lookup_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1057 | DO_TRAMPOLINE(portable_imt_conflict_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1058 | DO_TRAMPOLINE(portable_resolution_trampoline_); |
| 1059 | DO_TRAMPOLINE(portable_to_interpreter_bridge_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1060 | DO_TRAMPOLINE(quick_generic_jni_trampoline_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1061 | DO_TRAMPOLINE(quick_imt_conflict_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1062 | DO_TRAMPOLINE(quick_resolution_trampoline_); |
| 1063 | DO_TRAMPOLINE(quick_to_interpreter_bridge_); |
| 1064 | #undef DO_TRAMPOLINE |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1065 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1066 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1069 | size_t OatWriter::WriteCodeDexFiles(OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1070 | const size_t file_offset, |
| 1071 | size_t relative_offset) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1072 | #define VISIT(VisitorType) \ |
| 1073 | do { \ |
| 1074 | VisitorType visitor(this, out, file_offset, relative_offset); \ |
| 1075 | if (UNLIKELY(!VisitDexMethods(&visitor))) { \ |
| 1076 | return 0; \ |
| 1077 | } \ |
| 1078 | relative_offset = visitor.GetOffset(); \ |
| 1079 | } while (false) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1080 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1081 | VISIT(WriteCodeMethodVisitor); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1082 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1083 | #undef VISIT |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1084 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1085 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1088 | OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) { |
| 1089 | offset_ = offset; |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 1090 | const std::string& location(dex_file.GetLocation()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1091 | dex_file_location_size_ = location.size(); |
| 1092 | dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data()); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1093 | dex_file_location_checksum_ = dex_file.GetLocationChecksum(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1094 | dex_file_offset_ = 0; |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1095 | methods_offsets_.resize(dex_file.NumClassDefs()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | size_t OatWriter::OatDexFile::SizeOf() const { |
| 1099 | return sizeof(dex_file_location_size_) |
| 1100 | + dex_file_location_size_ |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1101 | + sizeof(dex_file_location_checksum_) |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1102 | + sizeof(dex_file_offset_) |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1103 | + (sizeof(methods_offsets_[0]) * methods_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1106 | void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const { |
| 1107 | oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_)); |
| 1108 | oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_); |
| 1109 | oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_)); |
| 1110 | oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_)); |
| 1111 | oat_header->UpdateChecksum(&methods_offsets_[0], |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1112 | sizeof(methods_offsets_[0]) * methods_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1115 | bool OatWriter::OatDexFile::Write(OatWriter* oat_writer, |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1116 | OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1117 | const size_t file_offset) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1118 | DCHECK_OFFSET_(); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1119 | if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) { |
| 1120 | PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1121 | return false; |
| 1122 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1123 | 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] | 1124 | if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) { |
| 1125 | PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1126 | return false; |
| 1127 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1128 | oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_; |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1129 | if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) { |
| 1130 | PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1131 | return false; |
| 1132 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1133 | 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] | 1134 | if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) { |
| 1135 | PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1136 | return false; |
| 1137 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1138 | oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1139 | if (!out->WriteFully(&methods_offsets_[0], |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 1140 | sizeof(methods_offsets_[0]) * methods_offsets_.size())) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1141 | PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1142 | return false; |
| 1143 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1144 | oat_writer->size_oat_dex_file_methods_offsets_ += |
| 1145 | sizeof(methods_offsets_[0]) * methods_offsets_.size(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1146 | return true; |
| 1147 | } |
| 1148 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1149 | OatWriter::OatClass::OatClass(size_t offset, |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1150 | const std::vector<CompiledMethod*>& compiled_methods, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1151 | uint32_t num_non_null_compiled_methods, |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1152 | mirror::Class::Status status) |
| 1153 | : compiled_methods_(compiled_methods) { |
| 1154 | uint32_t num_methods = compiled_methods.size(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1155 | CHECK_LE(num_non_null_compiled_methods, num_methods); |
| 1156 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1157 | offset_ = offset; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1158 | oat_method_offsets_offsets_from_oat_class_.resize(num_methods); |
| 1159 | |
| 1160 | // Since both kOatClassNoneCompiled and kOatClassAllCompiled could |
| 1161 | // apply when there are 0 methods, we just arbitrarily say that 0 |
| 1162 | // methods means kOatClassNoneCompiled and that we won't use |
| 1163 | // kOatClassAllCompiled unless there is at least one compiled |
| 1164 | // method. This means in an interpretter only system, we can assert |
| 1165 | // that all classes are kOatClassNoneCompiled. |
| 1166 | if (num_non_null_compiled_methods == 0) { |
| 1167 | type_ = kOatClassNoneCompiled; |
| 1168 | } else if (num_non_null_compiled_methods == num_methods) { |
| 1169 | type_ = kOatClassAllCompiled; |
| 1170 | } else { |
| 1171 | type_ = kOatClassSomeCompiled; |
| 1172 | } |
| 1173 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1174 | status_ = status; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1175 | method_offsets_.resize(num_non_null_compiled_methods); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 1176 | method_headers_.resize(num_non_null_compiled_methods); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1177 | |
| 1178 | uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_); |
| 1179 | if (type_ == kOatClassSomeCompiled) { |
| 1180 | method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator()); |
| 1181 | method_bitmap_size_ = method_bitmap_->GetSizeOf(); |
| 1182 | oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_); |
| 1183 | oat_method_offsets_offset_from_oat_class += method_bitmap_size_; |
| 1184 | } else { |
| 1185 | method_bitmap_ = NULL; |
| 1186 | method_bitmap_size_ = 0; |
| 1187 | } |
| 1188 | |
| 1189 | for (size_t i = 0; i < num_methods; i++) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1190 | CompiledMethod* compiled_method = compiled_methods_[i]; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1191 | if (compiled_method == NULL) { |
| 1192 | oat_method_offsets_offsets_from_oat_class_[i] = 0; |
| 1193 | } else { |
| 1194 | oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class; |
| 1195 | oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets); |
| 1196 | if (type_ == kOatClassSomeCompiled) { |
| 1197 | method_bitmap_->SetBit(i); |
| 1198 | } |
| 1199 | } |
| 1200 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1203 | OatWriter::OatClass::~OatClass() { |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 1204 | delete method_bitmap_; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1207 | size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader( |
| 1208 | size_t class_def_method_index_) const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1209 | uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_); |
| 1210 | if (method_offset == 0) { |
| 1211 | return 0; |
| 1212 | } |
| 1213 | return offset_ + method_offset; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass( |
| 1217 | size_t class_def_method_index_) const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1218 | return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_]; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | size_t OatWriter::OatClass::SizeOf() const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1222 | return sizeof(status_) |
| 1223 | + sizeof(type_) |
| 1224 | + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_)) |
| 1225 | + method_bitmap_size_ |
| 1226 | + (sizeof(method_offsets_[0]) * method_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1229 | void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const { |
| 1230 | oat_header->UpdateChecksum(&status_, sizeof(status_)); |
| 1231 | oat_header->UpdateChecksum(&type_, sizeof(type_)); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1232 | if (method_bitmap_size_ != 0) { |
| 1233 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1234 | oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_)); |
| 1235 | oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1236 | } |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1237 | oat_header->UpdateChecksum(&method_offsets_[0], |
| 1238 | sizeof(method_offsets_[0]) * method_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1241 | bool OatWriter::OatClass::Write(OatWriter* oat_writer, |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1242 | OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1243 | const size_t file_offset) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1244 | DCHECK_OFFSET_(); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1245 | if (!out->WriteFully(&status_, sizeof(status_))) { |
| 1246 | PLOG(ERROR) << "Failed to write class status to " << out->GetLocation(); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1247 | return false; |
| 1248 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1249 | oat_writer->size_oat_class_status_ += sizeof(status_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1250 | if (!out->WriteFully(&type_, sizeof(type_))) { |
| 1251 | PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1252 | return false; |
| 1253 | } |
| 1254 | oat_writer->size_oat_class_type_ += sizeof(type_); |
| 1255 | if (method_bitmap_size_ != 0) { |
| 1256 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1257 | if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) { |
| 1258 | PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1259 | return false; |
| 1260 | } |
| 1261 | oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1262 | if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) { |
| 1263 | PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1264 | return false; |
| 1265 | } |
| 1266 | oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_; |
| 1267 | } |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1268 | if (!out->WriteFully(&method_offsets_[0], |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 1269 | sizeof(method_offsets_[0]) * method_offsets_.size())) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1270 | PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1271 | return false; |
| 1272 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1273 | 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] | 1274 | return true; |
| 1275 | } |
| 1276 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1277 | } // namespace art |