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 | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 36 | #include "handle_scope-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 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 94 | TimingLogger::ScopedTiming split("InitOatHeader", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 95 | offset = InitOatHeader(); |
| 96 | } |
| 97 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 98 | TimingLogger::ScopedTiming split("InitOatDexFiles", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 99 | offset = InitOatDexFiles(offset); |
| 100 | } |
| 101 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 102 | TimingLogger::ScopedTiming split("InitDexFiles", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 103 | offset = InitDexFiles(offset); |
| 104 | } |
| 105 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 106 | TimingLogger::ScopedTiming split("InitOatClasses", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 107 | offset = InitOatClasses(offset); |
| 108 | } |
| 109 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 110 | TimingLogger::ScopedTiming split("InitOatMaps", timings); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 111 | offset = InitOatMaps(offset); |
| 112 | } |
| 113 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 114 | TimingLogger::ScopedTiming split("InitOatCode", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 115 | offset = InitOatCode(offset); |
| 116 | } |
| 117 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 118 | TimingLogger::ScopedTiming split("InitOatCodeDexFiles", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 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; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 334 | |
| 335 | const std::vector<uint8_t>* portable_code = compiled_method->GetPortableCode(); |
| 336 | const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode(); |
| 337 | if (portable_code != nullptr) { |
| 338 | CHECK(quick_code == nullptr); |
| 339 | size_t oat_method_offsets_offset = |
| 340 | oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index); |
| 341 | compiled_method->AddOatdataOffsetToCompliledCodeOffset( |
| 342 | oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_)); |
| 343 | } else { |
| 344 | CHECK(quick_code != nullptr); |
| 345 | offset_ = compiled_method->AlignCode(offset_); |
| 346 | DCHECK_ALIGNED_PARAM(offset_, |
| 347 | GetInstructionSetAlignment(compiled_method->GetInstructionSet())); |
| 348 | uint32_t code_size = quick_code->size() * sizeof(uint8_t); |
| 349 | CHECK_NE(code_size, 0U); |
| 350 | uint32_t thumb_offset = compiled_method->CodeDelta(); |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 351 | quick_code_offset = offset_ + sizeof(OatQuickMethodHeader) + thumb_offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 352 | |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 353 | bool force_debug_capture = false; |
| 354 | bool deduped = false; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 355 | |
| 356 | // Deduplicate code arrays. |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 357 | auto code_iter = dedupe_map_.find(compiled_method); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 358 | if (code_iter != dedupe_map_.end()) { |
| 359 | quick_code_offset = code_iter->second; |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 360 | deduped = true; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 361 | } else { |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 362 | dedupe_map_.Put(compiled_method, quick_code_offset); |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | // Update quick method header. |
| 366 | DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size()); |
| 367 | OatQuickMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_]; |
| 368 | uint32_t mapping_table_offset = method_header->mapping_table_offset_; |
| 369 | uint32_t vmap_table_offset = method_header->vmap_table_offset_; |
| 370 | // The code offset was 0 when the mapping/vmap table offset was set, so it's set |
| 371 | // to 0-offset and we need to adjust it by code_offset. |
| 372 | uint32_t code_offset = quick_code_offset - thumb_offset; |
| 373 | if (mapping_table_offset != 0u) { |
| 374 | mapping_table_offset += code_offset; |
| 375 | DCHECK_LT(mapping_table_offset, code_offset); |
| 376 | } |
| 377 | if (vmap_table_offset != 0u) { |
| 378 | vmap_table_offset += code_offset; |
| 379 | DCHECK_LT(vmap_table_offset, code_offset); |
| 380 | } |
| 381 | uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes(); |
| 382 | uint32_t core_spill_mask = compiled_method->GetCoreSpillMask(); |
| 383 | uint32_t fp_spill_mask = compiled_method->GetFpSpillMask(); |
| 384 | *method_header = OatQuickMethodHeader(mapping_table_offset, vmap_table_offset, |
| 385 | frame_size_in_bytes, core_spill_mask, fp_spill_mask, |
| 386 | code_size); |
| 387 | |
| 388 | // Update checksum if this wasn't a duplicate. |
| 389 | if (code_iter == dedupe_map_.end()) { |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 390 | writer_->oat_header_->UpdateChecksum(method_header, sizeof(*method_header)); |
| 391 | offset_ += sizeof(*method_header); // Method header is prepended before code. |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 392 | writer_->oat_header_->UpdateChecksum(&(*quick_code)[0], code_size); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 393 | offset_ += code_size; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 394 | } |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 395 | |
| 396 | uint32_t quick_code_start = quick_code_offset - writer_->oat_header_->GetExecutableOffset(); |
| 397 | std::vector<uint8_t>* cfi_info = writer_->compiler_driver_->GetCallFrameInformation(); |
| 398 | if (cfi_info != nullptr) { |
| 399 | // Copy in the FDE, if present |
| 400 | const std::vector<uint8_t>* fde = compiled_method->GetCFIInfo(); |
| 401 | if (fde != nullptr) { |
| 402 | // Copy the information into cfi_info and then fix the address in the new copy. |
| 403 | int cur_offset = cfi_info->size(); |
| 404 | cfi_info->insert(cfi_info->end(), fde->begin(), fde->end()); |
| 405 | |
| 406 | // Set the 'initial_location' field to address the start of the method. |
| 407 | uint32_t offset_to_update = cur_offset + 2*sizeof(uint32_t); |
| 408 | (*cfi_info)[offset_to_update+0] = quick_code_start; |
| 409 | (*cfi_info)[offset_to_update+1] = quick_code_start >> 8; |
| 410 | (*cfi_info)[offset_to_update+2] = quick_code_start >> 16; |
| 411 | (*cfi_info)[offset_to_update+3] = quick_code_start >> 24; |
| 412 | force_debug_capture = true; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | |
| 417 | if (writer_->compiler_driver_->DidIncludeDebugSymbols() || force_debug_capture) { |
| 418 | // Record debug information for this function if we are doing that or |
| 419 | // we have CFI and so need it. |
| 420 | std::string name = PrettyMethod(it.GetMemberIndex(), *dex_file_, true); |
| 421 | if (deduped) { |
| 422 | // TODO We should place the DEDUPED tag on the first instance of a |
| 423 | // deduplicated symbol so that it will show up in a debuggerd crash |
| 424 | // report. |
| 425 | name += " [ DEDUPED ]"; |
| 426 | } |
| 427 | writer_->method_info_.push_back(DebugInfo(name, quick_code_start, |
| 428 | quick_code_start + code_size)); |
| 429 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 430 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 431 | |
| 432 | if (kIsDebugBuild) { |
| 433 | // We expect GC maps except when the class hasn't been verified or the method is native. |
| 434 | const CompilerDriver* compiler_driver = writer_->compiler_driver_; |
| 435 | ClassReference class_ref(dex_file_, class_def_index_); |
| 436 | CompiledClass* compiled_class = compiler_driver->GetCompiledClass(class_ref); |
| 437 | mirror::Class::Status status; |
| 438 | if (compiled_class != NULL) { |
| 439 | status = compiled_class->GetStatus(); |
| 440 | } else if (compiler_driver->GetVerificationResults()->IsClassRejected(class_ref)) { |
| 441 | status = mirror::Class::kStatusError; |
| 442 | } else { |
| 443 | status = mirror::Class::kStatusNotReady; |
| 444 | } |
| 445 | const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap(); |
| 446 | size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]); |
| 447 | bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0; |
| 448 | CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified) |
| 449 | << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " " |
| 450 | << (status < mirror::Class::kStatusVerified) << " " << status << " " |
| 451 | << PrettyMethod(it.GetMemberIndex(), *dex_file_); |
| 452 | } |
| 453 | |
| 454 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 455 | OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_]; |
| 456 | offsets->code_offset_ = quick_code_offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 457 | ++method_offsets_index_; |
| 458 | } |
| 459 | |
| 460 | return true; |
| 461 | } |
| 462 | |
| 463 | private: |
| 464 | // Deduplication is already done on a pointer basis by the compiler driver, |
| 465 | // 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] | 466 | SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 467 | }; |
| 468 | |
| 469 | template <typename DataAccess> |
| 470 | class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor { |
| 471 | public: |
| 472 | InitMapMethodVisitor(OatWriter* writer, size_t offset) |
| 473 | : OatDexMethodVisitor(writer, offset) { |
| 474 | } |
| 475 | |
| 476 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
| 477 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 478 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 479 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 480 | |
| 481 | if (compiled_method != nullptr) { |
| 482 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 483 | DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u); |
| 484 | |
| 485 | const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method); |
| 486 | uint32_t map_size = map->size() * sizeof((*map)[0]); |
| 487 | if (map_size != 0u) { |
| 488 | auto it = dedupe_map_.find(map); |
| 489 | if (it != dedupe_map_.end()) { |
| 490 | DataAccess::SetOffset(oat_class, method_offsets_index_, it->second); |
| 491 | } else { |
| 492 | DataAccess::SetOffset(oat_class, method_offsets_index_, offset_); |
| 493 | dedupe_map_.Put(map, offset_); |
| 494 | offset_ += map_size; |
| 495 | writer_->oat_header_->UpdateChecksum(&(*map)[0], map_size); |
| 496 | } |
| 497 | } |
| 498 | ++method_offsets_index_; |
| 499 | } |
| 500 | |
| 501 | return true; |
| 502 | } |
| 503 | |
| 504 | private: |
| 505 | // Deduplication is already done on a pointer basis by the compiler driver, |
| 506 | // so we can simply compare the pointers to find out if things are duplicated. |
| 507 | SafeMap<const std::vector<uint8_t>*, uint32_t> dedupe_map_; |
| 508 | }; |
| 509 | |
| 510 | class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor { |
| 511 | public: |
| 512 | InitImageMethodVisitor(OatWriter* writer, size_t offset) |
| 513 | : OatDexMethodVisitor(writer, offset) { |
| 514 | } |
| 515 | |
| 516 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
| 517 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 518 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 519 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 520 | |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 521 | OatMethodOffsets offsets(0u, 0u); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 522 | if (compiled_method != nullptr) { |
| 523 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 524 | offsets = oat_class->method_offsets_[method_offsets_index_]; |
| 525 | ++method_offsets_index_; |
| 526 | } |
| 527 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 528 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 529 | InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_)); |
| 530 | // Unchecked as we hold mutator_lock_ on entry. |
| 531 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 532 | StackHandleScope<2> hs(soa.Self()); |
| 533 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache(*dex_file_))); |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 534 | mirror::ArtMethod* method = linker->ResolveMethod(*dex_file_, it.GetMemberIndex(), dex_cache, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 535 | NullHandle<mirror::ClassLoader>(), |
| 536 | NullHandle<mirror::ArtMethod>(), |
| 537 | invoke_type); |
Brian Carlstrom | 3c911d6 | 2014-06-13 18:23:20 -0700 | [diff] [blame] | 538 | CHECK(method != NULL) << PrettyMethod(it.GetMemberIndex(), *dex_file_, true); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 539 | // Portable code offsets are set by ElfWriterMclinker::FixupCompiledCodeOffset after linking. |
| 540 | method->SetQuickOatCodeOffset(offsets.code_offset_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 541 | method->SetOatNativeGcMapOffset(offsets.gc_map_offset_); |
| 542 | |
| 543 | return true; |
| 544 | } |
| 545 | }; |
| 546 | |
| 547 | class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor { |
| 548 | public: |
| 549 | WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset, |
| 550 | size_t relative_offset) |
| 551 | : OatDexMethodVisitor(writer, relative_offset), |
| 552 | out_(out), |
| 553 | file_offset_(file_offset) { |
| 554 | } |
| 555 | |
| 556 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) { |
| 557 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 558 | const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 559 | |
| 560 | if (compiled_method != NULL) { // ie. not an abstract method |
| 561 | size_t file_offset = file_offset_; |
| 562 | OutputStream* out = out_; |
| 563 | |
| 564 | const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode(); |
| 565 | if (quick_code != nullptr) { |
| 566 | CHECK(compiled_method->GetPortableCode() == nullptr); |
| 567 | uint32_t aligned_offset = compiled_method->AlignCode(offset_); |
| 568 | uint32_t aligned_code_delta = aligned_offset - offset_; |
| 569 | if (aligned_code_delta != 0) { |
| 570 | static const uint8_t kPadding[] = { |
| 571 | 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u |
| 572 | }; |
| 573 | DCHECK_LE(aligned_code_delta, sizeof(kPadding)); |
| 574 | if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) { |
| 575 | ReportWriteFailure("code alignment padding", it); |
| 576 | return false; |
| 577 | } |
| 578 | writer_->size_code_alignment_ += aligned_code_delta; |
| 579 | offset_ += aligned_code_delta; |
| 580 | DCHECK_OFFSET_(); |
| 581 | } |
| 582 | DCHECK_ALIGNED_PARAM(offset_, |
| 583 | GetInstructionSetAlignment(compiled_method->GetInstructionSet())); |
| 584 | uint32_t code_size = quick_code->size() * sizeof(uint8_t); |
| 585 | CHECK_NE(code_size, 0U); |
| 586 | |
| 587 | // Deduplicate code arrays. |
| 588 | const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_]; |
| 589 | DCHECK(method_offsets.code_offset_ < offset_ || method_offsets.code_offset_ == |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 590 | offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta()) |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 591 | << PrettyMethod(it.GetMemberIndex(), *dex_file_); |
| 592 | if (method_offsets.code_offset_ >= offset_) { |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 593 | const OatQuickMethodHeader& method_header = oat_class->method_headers_[method_offsets_index_]; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 594 | if (!out->WriteFully(&method_header, sizeof(method_header))) { |
| 595 | ReportWriteFailure("method header", it); |
| 596 | return false; |
| 597 | } |
| 598 | writer_->size_method_header_ += sizeof(method_header); |
| 599 | offset_ += sizeof(method_header); |
| 600 | DCHECK_OFFSET_(); |
| 601 | if (!out->WriteFully(&(*quick_code)[0], code_size)) { |
| 602 | ReportWriteFailure("method code", it); |
| 603 | return false; |
| 604 | } |
| 605 | writer_->size_code_ += code_size; |
| 606 | offset_ += code_size; |
| 607 | } |
| 608 | DCHECK_OFFSET_(); |
| 609 | } |
| 610 | ++method_offsets_index_; |
| 611 | } |
| 612 | |
| 613 | return true; |
| 614 | } |
| 615 | |
| 616 | private: |
| 617 | OutputStream* const out_; |
| 618 | size_t const file_offset_; |
| 619 | |
| 620 | void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) { |
| 621 | PLOG(ERROR) << "Failed to write " << what << " for " |
| 622 | << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation(); |
| 623 | } |
| 624 | }; |
| 625 | |
| 626 | template <typename DataAccess> |
| 627 | class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor { |
| 628 | public: |
| 629 | WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset, |
| 630 | size_t relative_offset) |
| 631 | : OatDexMethodVisitor(writer, relative_offset), |
| 632 | out_(out), |
| 633 | file_offset_(file_offset) { |
| 634 | } |
| 635 | |
| 636 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) { |
| 637 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 638 | const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 639 | |
| 640 | if (compiled_method != NULL) { // ie. not an abstract method |
| 641 | size_t file_offset = file_offset_; |
| 642 | OutputStream* out = out_; |
| 643 | |
| 644 | uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_); |
| 645 | ++method_offsets_index_; |
| 646 | |
| 647 | // Write deduplicated map. |
| 648 | const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method); |
| 649 | size_t map_size = map->size() * sizeof((*map)[0]); |
| 650 | DCHECK((map_size == 0u && map_offset == 0u) || |
| 651 | (map_size != 0u && map_offset != 0u && map_offset <= offset_)) |
| 652 | << PrettyMethod(it.GetMemberIndex(), *dex_file_); |
| 653 | if (map_size != 0u && map_offset == offset_) { |
| 654 | if (UNLIKELY(!out->WriteFully(&(*map)[0], map_size))) { |
| 655 | ReportWriteFailure(it); |
| 656 | return false; |
| 657 | } |
| 658 | offset_ += map_size; |
| 659 | } |
| 660 | DCHECK_OFFSET_(); |
| 661 | } |
| 662 | |
| 663 | return true; |
| 664 | } |
| 665 | |
| 666 | private: |
| 667 | OutputStream* const out_; |
| 668 | size_t const file_offset_; |
| 669 | |
| 670 | void ReportWriteFailure(const ClassDataItemIterator& it) { |
| 671 | PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for " |
| 672 | << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation(); |
| 673 | } |
| 674 | }; |
| 675 | |
| 676 | // Visit all methods from all classes in all dex files with the specified visitor. |
| 677 | bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) { |
| 678 | for (const DexFile* dex_file : *dex_files_) { |
| 679 | const size_t class_def_count = dex_file->NumClassDefs(); |
| 680 | for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) { |
| 681 | if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) { |
| 682 | return false; |
| 683 | } |
| 684 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
| 685 | const byte* class_data = dex_file->GetClassData(class_def); |
| 686 | if (class_data != NULL) { // ie not an empty class, such as a marker interface |
| 687 | ClassDataItemIterator it(*dex_file, class_data); |
| 688 | while (it.HasNextStaticField()) { |
| 689 | it.Next(); |
| 690 | } |
| 691 | while (it.HasNextInstanceField()) { |
| 692 | it.Next(); |
| 693 | } |
| 694 | size_t class_def_method_index = 0u; |
| 695 | while (it.HasNextDirectMethod()) { |
| 696 | if (!visitor->VisitMethod(class_def_method_index, it)) { |
| 697 | return false; |
| 698 | } |
| 699 | ++class_def_method_index; |
| 700 | it.Next(); |
| 701 | } |
| 702 | while (it.HasNextVirtualMethod()) { |
| 703 | if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) { |
| 704 | return false; |
| 705 | } |
| 706 | ++class_def_method_index; |
| 707 | it.Next(); |
| 708 | } |
| 709 | } |
| 710 | if (UNLIKELY(!visitor->EndClass())) { |
| 711 | return false; |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | return true; |
| 716 | } |
| 717 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 718 | size_t OatWriter::InitOatHeader() { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 719 | // create the OatHeader |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 720 | oat_header_ = new OatHeader(compiler_driver_->GetInstructionSet(), |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 721 | compiler_driver_->GetInstructionSetFeatures(), |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 722 | dex_files_, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 723 | image_file_location_oat_checksum_, |
| 724 | image_file_location_oat_begin_, |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 725 | image_file_location_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 726 | size_t offset = sizeof(*oat_header_); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 727 | offset += image_file_location_.size(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 728 | return offset; |
| 729 | } |
| 730 | |
| 731 | size_t OatWriter::InitOatDexFiles(size_t offset) { |
| 732 | // create the OatDexFiles |
| 733 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 734 | const DexFile* dex_file = (*dex_files_)[i]; |
| 735 | CHECK(dex_file != NULL); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 736 | OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 737 | oat_dex_files_.push_back(oat_dex_file); |
| 738 | offset += oat_dex_file->SizeOf(); |
| 739 | } |
| 740 | return offset; |
| 741 | } |
| 742 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 743 | size_t OatWriter::InitDexFiles(size_t offset) { |
| 744 | // calculate the offsets within OatDexFiles to the DexFiles |
| 745 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 746 | // dex files are required to be 4 byte aligned |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 747 | size_t original_offset = offset; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 748 | offset = RoundUp(offset, 4); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 749 | size_dex_file_alignment_ += offset - original_offset; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 750 | |
| 751 | // set offset in OatDexFile to DexFile |
| 752 | oat_dex_files_[i]->dex_file_offset_ = offset; |
| 753 | |
| 754 | const DexFile* dex_file = (*dex_files_)[i]; |
| 755 | offset += dex_file->GetHeader().file_size_; |
| 756 | } |
| 757 | return offset; |
| 758 | } |
| 759 | |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 760 | size_t OatWriter::InitOatClasses(size_t offset) { |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 761 | // calculate the offsets within OatDexFiles to OatClasses |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 762 | InitOatClassesMethodVisitor visitor(this, offset); |
| 763 | bool success = VisitDexMethods(&visitor); |
| 764 | CHECK(success); |
| 765 | offset = visitor.GetOffset(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 766 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 767 | // Update oat_dex_files_. |
| 768 | auto oat_class_it = oat_classes_.begin(); |
| 769 | for (OatDexFile* oat_dex_file : oat_dex_files_) { |
| 770 | for (uint32_t& offset : oat_dex_file->methods_offsets_) { |
| 771 | DCHECK(oat_class_it != oat_classes_.end()); |
| 772 | offset = (*oat_class_it)->offset_; |
| 773 | ++oat_class_it; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 774 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 775 | oat_dex_file->UpdateChecksum(oat_header_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 776 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 777 | CHECK(oat_class_it == oat_classes_.end()); |
| 778 | |
| 779 | return offset; |
| 780 | } |
| 781 | |
| 782 | size_t OatWriter::InitOatMaps(size_t offset) { |
| 783 | #define VISIT(VisitorType) \ |
| 784 | do { \ |
| 785 | VisitorType visitor(this, offset); \ |
| 786 | bool success = VisitDexMethods(&visitor); \ |
| 787 | DCHECK(success); \ |
| 788 | offset = visitor.GetOffset(); \ |
| 789 | } while (false) |
| 790 | |
| 791 | VISIT(InitMapMethodVisitor<GcMapDataAccess>); |
| 792 | VISIT(InitMapMethodVisitor<MappingTableDataAccess>); |
| 793 | VISIT(InitMapMethodVisitor<VmapTableDataAccess>); |
| 794 | |
| 795 | #undef VISIT |
| 796 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 797 | return offset; |
| 798 | } |
| 799 | |
| 800 | size_t OatWriter::InitOatCode(size_t offset) { |
| 801 | // calculate the offsets within OatHeader to executable code |
| 802 | size_t old_offset = offset; |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 803 | size_t adjusted_offset = offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 804 | // required to be on a new page boundary |
| 805 | offset = RoundUp(offset, kPageSize); |
| 806 | oat_header_->SetExecutableOffset(offset); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 807 | size_executable_offset_alignment_ = offset - old_offset; |
| 808 | if (compiler_driver_->IsImage()) { |
| 809 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 810 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 811 | #define DO_TRAMPOLINE(field, fn_name) \ |
| 812 | offset = CompiledCode::AlignCode(offset, instruction_set); \ |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 813 | adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \ |
| 814 | oat_header_->Set ## fn_name ## Offset(adjusted_offset); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 815 | field.reset(compiler_driver_->Create ## fn_name()); \ |
| 816 | offset += field->size(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 817 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 818 | DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge); |
| 819 | DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge); |
| 820 | DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 821 | DO_TRAMPOLINE(portable_imt_conflict_trampoline_, PortableImtConflictTrampoline); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 822 | DO_TRAMPOLINE(portable_resolution_trampoline_, PortableResolutionTrampoline); |
| 823 | DO_TRAMPOLINE(portable_to_interpreter_bridge_, PortableToInterpreterBridge); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 824 | DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 825 | DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 826 | DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline); |
| 827 | DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 828 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 829 | #undef DO_TRAMPOLINE |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 830 | } else { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 831 | oat_header_->SetInterpreterToInterpreterBridgeOffset(0); |
| 832 | oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0); |
| 833 | oat_header_->SetJniDlsymLookupOffset(0); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 834 | oat_header_->SetPortableImtConflictTrampolineOffset(0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 835 | oat_header_->SetPortableResolutionTrampolineOffset(0); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 836 | oat_header_->SetPortableToInterpreterBridgeOffset(0); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 837 | oat_header_->SetQuickGenericJniTrampolineOffset(0); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 838 | oat_header_->SetQuickImtConflictTrampolineOffset(0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 839 | oat_header_->SetQuickResolutionTrampolineOffset(0); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 840 | oat_header_->SetQuickToInterpreterBridgeOffset(0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 841 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 842 | return offset; |
| 843 | } |
| 844 | |
| 845 | size_t OatWriter::InitOatCodeDexFiles(size_t offset) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 846 | #define VISIT(VisitorType) \ |
| 847 | do { \ |
| 848 | VisitorType visitor(this, offset); \ |
| 849 | bool success = VisitDexMethods(&visitor); \ |
| 850 | DCHECK(success); \ |
| 851 | offset = visitor.GetOffset(); \ |
| 852 | } while (false) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 853 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 854 | VISIT(InitCodeMethodVisitor); |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 855 | if (compiler_driver_->IsImage()) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 856 | VISIT(InitImageMethodVisitor); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 857 | } |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 858 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 859 | #undef VISIT |
| 860 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 861 | return offset; |
| 862 | } |
| 863 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 864 | bool OatWriter::Write(OutputStream* out) { |
| 865 | const size_t file_offset = out->Seek(0, kSeekCurrent); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 866 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 867 | if (!out->WriteFully(oat_header_, sizeof(*oat_header_))) { |
| 868 | PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 869 | return false; |
| 870 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 871 | size_oat_header_ += sizeof(*oat_header_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 872 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 873 | if (!out->WriteFully(image_file_location_.data(), image_file_location_.size())) { |
| 874 | 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] | 875 | return false; |
| 876 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 877 | size_oat_header_image_file_location_ += image_file_location_.size(); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 878 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 879 | if (!WriteTables(out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 880 | LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 881 | return false; |
| 882 | } |
| 883 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 884 | size_t relative_offset = out->Seek(0, kSeekCurrent) - file_offset; |
| 885 | relative_offset = WriteMaps(out, file_offset, relative_offset); |
| 886 | if (relative_offset == 0) { |
| 887 | LOG(ERROR) << "Failed to write oat code to " << out->GetLocation(); |
| 888 | return false; |
| 889 | } |
| 890 | |
| 891 | relative_offset = WriteCode(out, file_offset, relative_offset); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 892 | if (relative_offset == 0) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 893 | LOG(ERROR) << "Failed to write oat code to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 894 | return false; |
| 895 | } |
| 896 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 897 | relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset); |
| 898 | if (relative_offset == 0) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 899 | 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] | 900 | return false; |
| 901 | } |
| 902 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 903 | if (kIsDebugBuild) { |
| 904 | uint32_t size_total = 0; |
| 905 | #define DO_STAT(x) \ |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 906 | VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \ |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 907 | size_total += x; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 908 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 909 | DO_STAT(size_dex_file_alignment_); |
| 910 | DO_STAT(size_executable_offset_alignment_); |
| 911 | DO_STAT(size_oat_header_); |
| 912 | DO_STAT(size_oat_header_image_file_location_); |
| 913 | DO_STAT(size_dex_file_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 914 | DO_STAT(size_interpreter_to_interpreter_bridge_); |
| 915 | DO_STAT(size_interpreter_to_compiled_code_bridge_); |
| 916 | DO_STAT(size_jni_dlsym_lookup_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 917 | DO_STAT(size_portable_imt_conflict_trampoline_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 918 | DO_STAT(size_portable_resolution_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 919 | DO_STAT(size_portable_to_interpreter_bridge_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 920 | DO_STAT(size_quick_generic_jni_trampoline_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 921 | DO_STAT(size_quick_imt_conflict_trampoline_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 922 | DO_STAT(size_quick_resolution_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 923 | DO_STAT(size_quick_to_interpreter_bridge_); |
| 924 | DO_STAT(size_trampoline_alignment_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 925 | DO_STAT(size_method_header_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 926 | DO_STAT(size_code_); |
| 927 | DO_STAT(size_code_alignment_); |
| 928 | DO_STAT(size_mapping_table_); |
| 929 | DO_STAT(size_vmap_table_); |
| 930 | DO_STAT(size_gc_map_); |
| 931 | DO_STAT(size_oat_dex_file_location_size_); |
| 932 | DO_STAT(size_oat_dex_file_location_data_); |
| 933 | DO_STAT(size_oat_dex_file_location_checksum_); |
| 934 | DO_STAT(size_oat_dex_file_offset_); |
| 935 | DO_STAT(size_oat_dex_file_methods_offsets_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 936 | DO_STAT(size_oat_class_type_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 937 | DO_STAT(size_oat_class_status_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 938 | DO_STAT(size_oat_class_method_bitmaps_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 939 | DO_STAT(size_oat_class_method_offsets_); |
| 940 | #undef DO_STAT |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 941 | |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 942 | VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 943 | 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] | 944 | CHECK_EQ(size_, size_total); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 945 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 946 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 947 | 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] | 948 | CHECK_EQ(size_, relative_offset); |
| 949 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 950 | return true; |
| 951 | } |
| 952 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 953 | bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 954 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 955 | if (!oat_dex_files_[i]->Write(this, out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 956 | PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 957 | return false; |
| 958 | } |
| 959 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 960 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 961 | 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] | 962 | off_t actual_offset = out->Seek(expected_offset, kSeekSet); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 963 | if (static_cast<uint32_t>(actual_offset) != expected_offset) { |
| 964 | const DexFile* dex_file = (*dex_files_)[i]; |
| 965 | PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset |
| 966 | << " Expected: " << expected_offset << " File: " << dex_file->GetLocation(); |
| 967 | return false; |
| 968 | } |
| 969 | const DexFile* dex_file = (*dex_files_)[i]; |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 970 | if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 971 | PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 972 | << " to " << out->GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 973 | return false; |
| 974 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 975 | size_dex_file_ += dex_file->GetHeader().file_size_; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 976 | } |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 977 | for (size_t i = 0; i != oat_classes_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 978 | if (!oat_classes_[i]->Write(this, out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 979 | PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 980 | return false; |
| 981 | } |
| 982 | } |
| 983 | return true; |
| 984 | } |
| 985 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 986 | size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) { |
| 987 | #define VISIT(VisitorType) \ |
| 988 | do { \ |
| 989 | VisitorType visitor(this, out, file_offset, relative_offset); \ |
| 990 | if (UNLIKELY(!VisitDexMethods(&visitor))) { \ |
| 991 | return 0; \ |
| 992 | } \ |
| 993 | relative_offset = visitor.GetOffset(); \ |
| 994 | } while (false) |
| 995 | |
| 996 | size_t gc_maps_offset = relative_offset; |
| 997 | VISIT(WriteMapMethodVisitor<GcMapDataAccess>); |
| 998 | size_gc_map_ = relative_offset - gc_maps_offset; |
| 999 | |
| 1000 | size_t mapping_tables_offset = relative_offset; |
| 1001 | VISIT(WriteMapMethodVisitor<MappingTableDataAccess>); |
| 1002 | size_mapping_table_ = relative_offset - mapping_tables_offset; |
| 1003 | |
| 1004 | size_t vmap_tables_offset = relative_offset; |
| 1005 | VISIT(WriteMapMethodVisitor<VmapTableDataAccess>); |
| 1006 | size_vmap_table_ = relative_offset - vmap_tables_offset; |
| 1007 | |
| 1008 | #undef VISIT |
| 1009 | |
| 1010 | return relative_offset; |
| 1011 | } |
| 1012 | |
| 1013 | 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] | 1014 | off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1015 | relative_offset += size_executable_offset_alignment_; |
| 1016 | DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset()); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1017 | size_t expected_file_offset = file_offset + relative_offset; |
| 1018 | if (static_cast<uint32_t>(new_offset) != expected_file_offset) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 1019 | PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1020 | << " Expected: " << expected_file_offset << " File: " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1021 | return 0; |
| 1022 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1023 | DCHECK_OFFSET(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1024 | if (compiler_driver_->IsImage()) { |
| 1025 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1026 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1027 | #define DO_TRAMPOLINE(field) \ |
| 1028 | do { \ |
| 1029 | uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \ |
| 1030 | uint32_t alignment_padding = aligned_offset - relative_offset; \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1031 | out->Seek(alignment_padding, kSeekCurrent); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1032 | size_trampoline_alignment_ += alignment_padding; \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1033 | if (!out->WriteFully(&(*field)[0], field->size())) { \ |
| 1034 | PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1035 | return false; \ |
| 1036 | } \ |
| 1037 | size_ ## field += field->size(); \ |
| 1038 | relative_offset += alignment_padding + field->size(); \ |
| 1039 | DCHECK_OFFSET(); \ |
| 1040 | } while (false) |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1041 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1042 | DO_TRAMPOLINE(interpreter_to_interpreter_bridge_); |
| 1043 | DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_); |
| 1044 | DO_TRAMPOLINE(jni_dlsym_lookup_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1045 | DO_TRAMPOLINE(portable_imt_conflict_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1046 | DO_TRAMPOLINE(portable_resolution_trampoline_); |
| 1047 | DO_TRAMPOLINE(portable_to_interpreter_bridge_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1048 | DO_TRAMPOLINE(quick_generic_jni_trampoline_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1049 | DO_TRAMPOLINE(quick_imt_conflict_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1050 | DO_TRAMPOLINE(quick_resolution_trampoline_); |
| 1051 | DO_TRAMPOLINE(quick_to_interpreter_bridge_); |
| 1052 | #undef DO_TRAMPOLINE |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1053 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1054 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1057 | size_t OatWriter::WriteCodeDexFiles(OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1058 | const size_t file_offset, |
| 1059 | size_t relative_offset) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1060 | #define VISIT(VisitorType) \ |
| 1061 | do { \ |
| 1062 | VisitorType visitor(this, out, file_offset, relative_offset); \ |
| 1063 | if (UNLIKELY(!VisitDexMethods(&visitor))) { \ |
| 1064 | return 0; \ |
| 1065 | } \ |
| 1066 | relative_offset = visitor.GetOffset(); \ |
| 1067 | } while (false) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1068 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1069 | VISIT(WriteCodeMethodVisitor); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1070 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1071 | #undef VISIT |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1072 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1073 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1076 | OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) { |
| 1077 | offset_ = offset; |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 1078 | const std::string& location(dex_file.GetLocation()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1079 | dex_file_location_size_ = location.size(); |
| 1080 | dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data()); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1081 | dex_file_location_checksum_ = dex_file.GetLocationChecksum(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1082 | dex_file_offset_ = 0; |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1083 | methods_offsets_.resize(dex_file.NumClassDefs()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | size_t OatWriter::OatDexFile::SizeOf() const { |
| 1087 | return sizeof(dex_file_location_size_) |
| 1088 | + dex_file_location_size_ |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1089 | + sizeof(dex_file_location_checksum_) |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1090 | + sizeof(dex_file_offset_) |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1091 | + (sizeof(methods_offsets_[0]) * methods_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1094 | void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const { |
| 1095 | oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_)); |
| 1096 | oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_); |
| 1097 | oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_)); |
| 1098 | oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_)); |
| 1099 | oat_header->UpdateChecksum(&methods_offsets_[0], |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1100 | sizeof(methods_offsets_[0]) * methods_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1103 | bool OatWriter::OatDexFile::Write(OatWriter* oat_writer, |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1104 | OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1105 | const size_t file_offset) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1106 | DCHECK_OFFSET_(); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1107 | if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) { |
| 1108 | PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1109 | return false; |
| 1110 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1111 | 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] | 1112 | if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) { |
| 1113 | PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1114 | return false; |
| 1115 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1116 | oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_; |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1117 | if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) { |
| 1118 | PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1119 | return false; |
| 1120 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1121 | 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] | 1122 | if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) { |
| 1123 | PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1124 | return false; |
| 1125 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1126 | oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1127 | if (!out->WriteFully(&methods_offsets_[0], |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 1128 | sizeof(methods_offsets_[0]) * methods_offsets_.size())) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1129 | PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1130 | return false; |
| 1131 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1132 | oat_writer->size_oat_dex_file_methods_offsets_ += |
| 1133 | sizeof(methods_offsets_[0]) * methods_offsets_.size(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1134 | return true; |
| 1135 | } |
| 1136 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1137 | OatWriter::OatClass::OatClass(size_t offset, |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1138 | const std::vector<CompiledMethod*>& compiled_methods, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1139 | uint32_t num_non_null_compiled_methods, |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1140 | mirror::Class::Status status) |
| 1141 | : compiled_methods_(compiled_methods) { |
| 1142 | uint32_t num_methods = compiled_methods.size(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1143 | CHECK_LE(num_non_null_compiled_methods, num_methods); |
| 1144 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1145 | offset_ = offset; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1146 | oat_method_offsets_offsets_from_oat_class_.resize(num_methods); |
| 1147 | |
| 1148 | // Since both kOatClassNoneCompiled and kOatClassAllCompiled could |
| 1149 | // apply when there are 0 methods, we just arbitrarily say that 0 |
| 1150 | // methods means kOatClassNoneCompiled and that we won't use |
| 1151 | // kOatClassAllCompiled unless there is at least one compiled |
| 1152 | // method. This means in an interpretter only system, we can assert |
| 1153 | // that all classes are kOatClassNoneCompiled. |
| 1154 | if (num_non_null_compiled_methods == 0) { |
| 1155 | type_ = kOatClassNoneCompiled; |
| 1156 | } else if (num_non_null_compiled_methods == num_methods) { |
| 1157 | type_ = kOatClassAllCompiled; |
| 1158 | } else { |
| 1159 | type_ = kOatClassSomeCompiled; |
| 1160 | } |
| 1161 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1162 | status_ = status; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1163 | method_offsets_.resize(num_non_null_compiled_methods); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 1164 | method_headers_.resize(num_non_null_compiled_methods); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1165 | |
| 1166 | uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_); |
| 1167 | if (type_ == kOatClassSomeCompiled) { |
| 1168 | method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator()); |
| 1169 | method_bitmap_size_ = method_bitmap_->GetSizeOf(); |
| 1170 | oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_); |
| 1171 | oat_method_offsets_offset_from_oat_class += method_bitmap_size_; |
| 1172 | } else { |
| 1173 | method_bitmap_ = NULL; |
| 1174 | method_bitmap_size_ = 0; |
| 1175 | } |
| 1176 | |
| 1177 | for (size_t i = 0; i < num_methods; i++) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1178 | CompiledMethod* compiled_method = compiled_methods_[i]; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1179 | if (compiled_method == NULL) { |
| 1180 | oat_method_offsets_offsets_from_oat_class_[i] = 0; |
| 1181 | } else { |
| 1182 | oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class; |
| 1183 | oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets); |
| 1184 | if (type_ == kOatClassSomeCompiled) { |
| 1185 | method_bitmap_->SetBit(i); |
| 1186 | } |
| 1187 | } |
| 1188 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1191 | OatWriter::OatClass::~OatClass() { |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 1192 | delete method_bitmap_; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1195 | size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader( |
| 1196 | size_t class_def_method_index_) const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1197 | uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_); |
| 1198 | if (method_offset == 0) { |
| 1199 | return 0; |
| 1200 | } |
| 1201 | return offset_ + method_offset; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass( |
| 1205 | size_t class_def_method_index_) const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1206 | return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_]; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | size_t OatWriter::OatClass::SizeOf() const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1210 | return sizeof(status_) |
| 1211 | + sizeof(type_) |
| 1212 | + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_)) |
| 1213 | + method_bitmap_size_ |
| 1214 | + (sizeof(method_offsets_[0]) * method_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1217 | void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const { |
| 1218 | oat_header->UpdateChecksum(&status_, sizeof(status_)); |
| 1219 | oat_header->UpdateChecksum(&type_, sizeof(type_)); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1220 | if (method_bitmap_size_ != 0) { |
| 1221 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1222 | oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_)); |
| 1223 | oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1224 | } |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1225 | oat_header->UpdateChecksum(&method_offsets_[0], |
| 1226 | sizeof(method_offsets_[0]) * method_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1229 | bool OatWriter::OatClass::Write(OatWriter* oat_writer, |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1230 | OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1231 | const size_t file_offset) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1232 | DCHECK_OFFSET_(); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1233 | if (!out->WriteFully(&status_, sizeof(status_))) { |
| 1234 | PLOG(ERROR) << "Failed to write class status to " << out->GetLocation(); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1235 | return false; |
| 1236 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1237 | oat_writer->size_oat_class_status_ += sizeof(status_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1238 | if (!out->WriteFully(&type_, sizeof(type_))) { |
| 1239 | PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1240 | return false; |
| 1241 | } |
| 1242 | oat_writer->size_oat_class_type_ += sizeof(type_); |
| 1243 | if (method_bitmap_size_ != 0) { |
| 1244 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1245 | if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) { |
| 1246 | PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1247 | return false; |
| 1248 | } |
| 1249 | oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1250 | if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) { |
| 1251 | PLOG(ERROR) << "Failed to write method bitmap 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_method_bitmaps_ += method_bitmap_size_; |
| 1255 | } |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1256 | if (!out->WriteFully(&method_offsets_[0], |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 1257 | sizeof(method_offsets_[0]) * method_offsets_.size())) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1258 | PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1259 | return false; |
| 1260 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1261 | 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] | 1262 | return true; |
| 1263 | } |
| 1264 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1265 | } // namespace art |