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" |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 36 | #include "verifier/method_verifier.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 37 | |
| 38 | namespace art { |
| 39 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 40 | OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 41 | uint32_t image_file_location_oat_checksum, |
| 42 | uint32_t image_file_location_oat_begin, |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 43 | const std::string& image_file_location, |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 44 | const CompilerDriver* compiler, |
| 45 | TimingLogger* timings) |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 46 | : compiler_driver_(compiler), |
| 47 | dex_files_(&dex_files), |
| 48 | image_file_location_oat_checksum_(image_file_location_oat_checksum), |
| 49 | image_file_location_oat_begin_(image_file_location_oat_begin), |
| 50 | image_file_location_(image_file_location), |
| 51 | oat_header_(NULL), |
| 52 | size_dex_file_alignment_(0), |
| 53 | size_executable_offset_alignment_(0), |
| 54 | size_oat_header_(0), |
| 55 | size_oat_header_image_file_location_(0), |
| 56 | size_dex_file_(0), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 57 | size_interpreter_to_interpreter_bridge_(0), |
| 58 | size_interpreter_to_compiled_code_bridge_(0), |
| 59 | size_jni_dlsym_lookup_(0), |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 60 | size_portable_imt_conflict_trampoline_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 61 | size_portable_resolution_trampoline_(0), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 62 | size_portable_to_interpreter_bridge_(0), |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 63 | size_quick_imt_conflict_trampoline_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 64 | size_quick_resolution_trampoline_(0), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 65 | size_quick_to_interpreter_bridge_(0), |
| 66 | size_trampoline_alignment_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 67 | size_code_size_(0), |
| 68 | size_code_(0), |
| 69 | size_code_alignment_(0), |
| 70 | size_mapping_table_(0), |
| 71 | size_vmap_table_(0), |
| 72 | size_gc_map_(0), |
| 73 | size_oat_dex_file_location_size_(0), |
| 74 | size_oat_dex_file_location_data_(0), |
| 75 | size_oat_dex_file_location_checksum_(0), |
| 76 | size_oat_dex_file_offset_(0), |
| 77 | size_oat_dex_file_methods_offsets_(0), |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 78 | size_oat_class_type_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 79 | size_oat_class_status_(0), |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 80 | size_oat_class_method_bitmaps_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 81 | size_oat_class_method_offsets_(0) { |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 82 | size_t offset; |
| 83 | { |
| 84 | TimingLogger::ScopedSplit split("InitOatHeader", timings); |
| 85 | offset = InitOatHeader(); |
| 86 | } |
| 87 | { |
| 88 | TimingLogger::ScopedSplit split("InitOatDexFiles", timings); |
| 89 | offset = InitOatDexFiles(offset); |
| 90 | } |
| 91 | { |
| 92 | TimingLogger::ScopedSplit split("InitDexFiles", timings); |
| 93 | offset = InitDexFiles(offset); |
| 94 | } |
| 95 | { |
| 96 | TimingLogger::ScopedSplit split("InitOatClasses", timings); |
| 97 | offset = InitOatClasses(offset); |
| 98 | } |
| 99 | { |
| 100 | TimingLogger::ScopedSplit split("InitOatCode", timings); |
| 101 | offset = InitOatCode(offset); |
| 102 | } |
| 103 | { |
| 104 | TimingLogger::ScopedSplit split("InitOatCodeDexFiles", timings); |
| 105 | offset = InitOatCodeDexFiles(offset); |
| 106 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 107 | size_ = offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 108 | |
| 109 | CHECK_EQ(dex_files_->size(), oat_dex_files_.size()); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 110 | CHECK(image_file_location.empty() == compiler->IsImage()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 113 | OatWriter::~OatWriter() { |
| 114 | delete oat_header_; |
| 115 | STLDeleteElements(&oat_dex_files_); |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 116 | STLDeleteElements(&oat_classes_); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 119 | size_t OatWriter::InitOatHeader() { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 120 | // create the OatHeader |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 121 | oat_header_ = new OatHeader(compiler_driver_->GetInstructionSet(), |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 122 | compiler_driver_->GetInstructionSetFeatures(), |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 123 | dex_files_, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 124 | image_file_location_oat_checksum_, |
| 125 | image_file_location_oat_begin_, |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 126 | image_file_location_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 127 | size_t offset = sizeof(*oat_header_); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 128 | offset += image_file_location_.size(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 129 | return offset; |
| 130 | } |
| 131 | |
| 132 | size_t OatWriter::InitOatDexFiles(size_t offset) { |
| 133 | // create the OatDexFiles |
| 134 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 135 | const DexFile* dex_file = (*dex_files_)[i]; |
| 136 | CHECK(dex_file != NULL); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 137 | OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 138 | oat_dex_files_.push_back(oat_dex_file); |
| 139 | offset += oat_dex_file->SizeOf(); |
| 140 | } |
| 141 | return offset; |
| 142 | } |
| 143 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 144 | size_t OatWriter::InitDexFiles(size_t offset) { |
| 145 | // calculate the offsets within OatDexFiles to the DexFiles |
| 146 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 147 | // dex files are required to be 4 byte aligned |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 148 | size_t original_offset = offset; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 149 | offset = RoundUp(offset, 4); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 150 | size_dex_file_alignment_ += offset - original_offset; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 151 | |
| 152 | // set offset in OatDexFile to DexFile |
| 153 | oat_dex_files_[i]->dex_file_offset_ = offset; |
| 154 | |
| 155 | const DexFile* dex_file = (*dex_files_)[i]; |
| 156 | offset += dex_file->GetHeader().file_size_; |
| 157 | } |
| 158 | return offset; |
| 159 | } |
| 160 | |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 161 | size_t OatWriter::InitOatClasses(size_t offset) { |
| 162 | // create the OatClasses |
| 163 | // calculate the offsets within OatDexFiles to OatClasses |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 164 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 165 | const DexFile* dex_file = (*dex_files_)[i]; |
| 166 | for (size_t class_def_index = 0; |
| 167 | class_def_index < dex_file->NumClassDefs(); |
Ian Rogers | c20a83e | 2012-01-18 18:15:32 -0800 | [diff] [blame] | 168 | class_def_index++) { |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 169 | oat_dex_files_[i]->methods_offsets_[class_def_index] = offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 170 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
| 171 | const byte* class_data = dex_file->GetClassData(class_def); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 172 | uint32_t num_non_null_compiled_methods = 0; |
| 173 | UniquePtr<std::vector<CompiledMethod*> > compiled_methods(new std::vector<CompiledMethod*>()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 174 | if (class_data != NULL) { // ie not an empty class, such as a marker interface |
| 175 | ClassDataItemIterator it(*dex_file, class_data); |
| 176 | size_t num_direct_methods = it.NumDirectMethods(); |
| 177 | size_t num_virtual_methods = it.NumVirtualMethods(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 178 | size_t num_methods = num_direct_methods + num_virtual_methods; |
| 179 | |
| 180 | // Fill in the compiled_methods_ array for methods that have a |
| 181 | // CompiledMethod. We track the number of non-null entries in |
| 182 | // num_non_null_compiled_methods since we only want to allocate |
| 183 | // OatMethodOffsets for the compiled methods. |
| 184 | compiled_methods->reserve(num_methods); |
| 185 | while (it.HasNextStaticField()) { |
| 186 | it.Next(); |
| 187 | } |
| 188 | while (it.HasNextInstanceField()) { |
| 189 | it.Next(); |
| 190 | } |
| 191 | size_t class_def_method_index = 0; |
| 192 | while (it.HasNextDirectMethod()) { |
| 193 | uint32_t method_idx = it.GetMemberIndex(); |
| 194 | CompiledMethod* compiled_method = |
| 195 | compiler_driver_->GetCompiledMethod(MethodReference(dex_file, method_idx)); |
| 196 | compiled_methods->push_back(compiled_method); |
| 197 | if (compiled_method != NULL) { |
| 198 | num_non_null_compiled_methods++; |
| 199 | } |
| 200 | class_def_method_index++; |
| 201 | it.Next(); |
| 202 | } |
| 203 | while (it.HasNextVirtualMethod()) { |
| 204 | uint32_t method_idx = it.GetMemberIndex(); |
| 205 | CompiledMethod* compiled_method = |
| 206 | compiler_driver_->GetCompiledMethod(MethodReference(dex_file, method_idx)); |
| 207 | compiled_methods->push_back(compiled_method); |
| 208 | if (compiled_method != NULL) { |
| 209 | num_non_null_compiled_methods++; |
| 210 | } |
| 211 | class_def_method_index++; |
| 212 | it.Next(); |
| 213 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 214 | } |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 215 | |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 216 | ClassReference class_ref(dex_file, class_def_index); |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 217 | CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 218 | mirror::Class::Status status; |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 219 | if (compiled_class != NULL) { |
| 220 | status = compiled_class->GetStatus(); |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame^] | 221 | } else if (compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 222 | status = mirror::Class::kStatusError; |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 223 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 224 | status = mirror::Class::kStatusNotReady; |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 225 | } |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 226 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 227 | OatClass* oat_class = new OatClass(offset, compiled_methods.release(), |
| 228 | num_non_null_compiled_methods, status); |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 229 | oat_classes_.push_back(oat_class); |
| 230 | offset += oat_class->SizeOf(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 231 | } |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 232 | oat_dex_files_[i]->UpdateChecksum(*oat_header_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 233 | } |
| 234 | return offset; |
| 235 | } |
| 236 | |
| 237 | size_t OatWriter::InitOatCode(size_t offset) { |
| 238 | // calculate the offsets within OatHeader to executable code |
| 239 | size_t old_offset = offset; |
| 240 | // required to be on a new page boundary |
| 241 | offset = RoundUp(offset, kPageSize); |
| 242 | oat_header_->SetExecutableOffset(offset); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 243 | size_executable_offset_alignment_ = offset - old_offset; |
| 244 | if (compiler_driver_->IsImage()) { |
| 245 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 246 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 247 | #define DO_TRAMPOLINE(field, fn_name) \ |
| 248 | offset = CompiledCode::AlignCode(offset, instruction_set); \ |
| 249 | oat_header_->Set ## fn_name ## Offset(offset); \ |
| 250 | field.reset(compiler_driver_->Create ## fn_name()); \ |
| 251 | offset += field->size(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 252 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 253 | DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge); |
| 254 | DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge); |
| 255 | DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 256 | DO_TRAMPOLINE(portable_imt_conflict_trampoline_, PortableImtConflictTrampoline); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 257 | DO_TRAMPOLINE(portable_resolution_trampoline_, PortableResolutionTrampoline); |
| 258 | DO_TRAMPOLINE(portable_to_interpreter_bridge_, PortableToInterpreterBridge); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 259 | DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 260 | DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline); |
| 261 | DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 262 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 263 | #undef DO_TRAMPOLINE |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 264 | } else { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 265 | oat_header_->SetInterpreterToInterpreterBridgeOffset(0); |
| 266 | oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0); |
| 267 | oat_header_->SetJniDlsymLookupOffset(0); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 268 | oat_header_->SetPortableImtConflictTrampolineOffset(0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 269 | oat_header_->SetPortableResolutionTrampolineOffset(0); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 270 | oat_header_->SetPortableToInterpreterBridgeOffset(0); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 271 | oat_header_->SetQuickImtConflictTrampolineOffset(0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 272 | oat_header_->SetQuickResolutionTrampolineOffset(0); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 273 | oat_header_->SetQuickToInterpreterBridgeOffset(0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 274 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 275 | return offset; |
| 276 | } |
| 277 | |
| 278 | size_t OatWriter::InitOatCodeDexFiles(size_t offset) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 279 | size_t oat_class_index = 0; |
| 280 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 281 | const DexFile* dex_file = (*dex_files_)[i]; |
| 282 | CHECK(dex_file != NULL); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 283 | offset = InitOatCodeDexFile(offset, &oat_class_index, *dex_file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 284 | } |
| 285 | return offset; |
| 286 | } |
| 287 | |
| 288 | size_t OatWriter::InitOatCodeDexFile(size_t offset, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 289 | size_t* oat_class_index, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 290 | const DexFile& dex_file) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 291 | for (size_t class_def_index = 0; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 292 | class_def_index < dex_file.NumClassDefs(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 293 | class_def_index++, (*oat_class_index)++) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 294 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 295 | offset = InitOatCodeClassDef(offset, *oat_class_index, class_def_index, dex_file, class_def); |
| 296 | oat_classes_[*oat_class_index]->UpdateChecksum(*oat_header_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 297 | } |
| 298 | return offset; |
| 299 | } |
| 300 | |
| 301 | size_t OatWriter::InitOatCodeClassDef(size_t offset, |
Ian Rogers | c20a83e | 2012-01-18 18:15:32 -0800 | [diff] [blame] | 302 | size_t oat_class_index, size_t class_def_index, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 303 | const DexFile& dex_file, |
| 304 | const DexFile::ClassDef& class_def) { |
| 305 | const byte* class_data = dex_file.GetClassData(class_def); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 306 | if (class_data == NULL) { |
| 307 | // empty class, such as a marker interface |
Ian Rogers | 387b699 | 2011-10-31 17:52:37 -0700 | [diff] [blame] | 308 | return offset; |
| 309 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 310 | ClassDataItemIterator it(dex_file, class_data); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 311 | CHECK_LE(oat_classes_[oat_class_index]->method_offsets_.size(), |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 312 | it.NumDirectMethods() + it.NumVirtualMethods()); |
| 313 | // Skip fields |
| 314 | while (it.HasNextStaticField()) { |
| 315 | it.Next(); |
| 316 | } |
| 317 | while (it.HasNextInstanceField()) { |
| 318 | it.Next(); |
| 319 | } |
| 320 | // Process methods |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 321 | size_t class_def_method_index = 0; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 322 | size_t method_offsets_index = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 323 | while (it.HasNextDirectMethod()) { |
Ian Rogers | c20a83e | 2012-01-18 18:15:32 -0800 | [diff] [blame] | 324 | bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 325 | offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 326 | &method_offsets_index, is_native, |
| 327 | it.GetMethodInvokeType(class_def), it.GetMemberIndex(), dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 328 | class_def_method_index++; |
| 329 | it.Next(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 330 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 331 | while (it.HasNextVirtualMethod()) { |
Ian Rogers | c20a83e | 2012-01-18 18:15:32 -0800 | [diff] [blame] | 332 | bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 333 | offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 334 | &method_offsets_index, is_native, |
| 335 | it.GetMethodInvokeType(class_def), it.GetMemberIndex(), dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 336 | class_def_method_index++; |
| 337 | it.Next(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 338 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 339 | DCHECK(!it.HasNext()); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 340 | CHECK_LE(method_offsets_index, class_def_method_index); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 341 | return offset; |
| 342 | } |
| 343 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 344 | size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index, |
| 345 | size_t __attribute__((unused)) class_def_index, |
| 346 | size_t class_def_method_index, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 347 | size_t* method_offsets_index, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 348 | bool __attribute__((unused)) is_native, |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 349 | InvokeType invoke_type, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 350 | uint32_t method_idx, const DexFile& dex_file) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 351 | // derived from CompiledMethod if available |
| 352 | uint32_t code_offset = 0; |
| 353 | uint32_t frame_size_in_bytes = kStackAlignment; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 354 | uint32_t core_spill_mask = 0; |
| 355 | uint32_t fp_spill_mask = 0; |
| 356 | uint32_t mapping_table_offset = 0; |
| 357 | uint32_t vmap_table_offset = 0; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 358 | uint32_t gc_map_offset = 0; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 359 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 360 | OatClass* oat_class = oat_classes_[oat_class_index]; |
| 361 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 362 | size_t oat_method_offsets_offset = |
| 363 | oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index); |
| 364 | #endif |
| 365 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 366 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 367 | if (compiled_method != NULL) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 368 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 369 | compiled_method->AddOatdataOffsetToCompliledCodeOffset( |
| 370 | oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_)); |
| 371 | #else |
| 372 | const std::vector<uint8_t>& code = compiled_method->GetCode(); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 373 | offset = compiled_method->AlignCode(offset); |
| 374 | DCHECK_ALIGNED(offset, kArmAlignment); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 375 | uint32_t code_size = code.size() * sizeof(code[0]); |
| 376 | CHECK_NE(code_size, 0U); |
| 377 | uint32_t thumb_offset = compiled_method->CodeDelta(); |
| 378 | code_offset = offset + sizeof(code_size) + thumb_offset; |
| 379 | |
| 380 | // Deduplicate code arrays |
| 381 | SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code); |
| 382 | if (code_iter != code_offsets_.end()) { |
| 383 | code_offset = code_iter->second; |
jeffhao | 55d7821 | 2011-11-02 11:41:50 -0700 | [diff] [blame] | 384 | } else { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 385 | code_offsets_.Put(&code, code_offset); |
| 386 | offset += sizeof(code_size); // code size is prepended before code |
| 387 | offset += code_size; |
| 388 | oat_header_->UpdateChecksum(&code[0], code_size); |
| 389 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 390 | #endif |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 391 | frame_size_in_bytes = compiled_method->GetFrameSizeInBytes(); |
| 392 | core_spill_mask = compiled_method->GetCoreSpillMask(); |
| 393 | fp_spill_mask = compiled_method->GetFpSpillMask(); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 394 | |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 395 | const std::vector<uint8_t>& mapping_table = compiled_method->GetMappingTable(); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 396 | size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]); |
| 397 | mapping_table_offset = (mapping_table_size == 0) ? 0 : offset; |
jeffhao | 55d7821 | 2011-11-02 11:41:50 -0700 | [diff] [blame] | 398 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 399 | // Deduplicate mapping tables |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 400 | SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator mapping_iter = |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 401 | mapping_table_offsets_.find(&mapping_table); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 402 | if (mapping_iter != mapping_table_offsets_.end()) { |
| 403 | mapping_table_offset = mapping_iter->second; |
| 404 | } else { |
| 405 | mapping_table_offsets_.Put(&mapping_table, mapping_table_offset); |
| 406 | offset += mapping_table_size; |
| 407 | oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size); |
| 408 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 409 | |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 410 | const std::vector<uint8_t>& vmap_table = compiled_method->GetVmapTable(); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 411 | size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]); |
| 412 | vmap_table_offset = (vmap_table_size == 0) ? 0 : offset; |
jeffhao | 55d7821 | 2011-11-02 11:41:50 -0700 | [diff] [blame] | 413 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 414 | // Deduplicate vmap tables |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 415 | SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator vmap_iter = |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 416 | vmap_table_offsets_.find(&vmap_table); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 417 | if (vmap_iter != vmap_table_offsets_.end()) { |
| 418 | vmap_table_offset = vmap_iter->second; |
| 419 | } else { |
| 420 | vmap_table_offsets_.Put(&vmap_table, vmap_table_offset); |
| 421 | offset += vmap_table_size; |
| 422 | oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size); |
| 423 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 424 | |
Ian Rogers | fbdc0fa | 2013-04-18 16:48:17 -0700 | [diff] [blame] | 425 | const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap(); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 426 | size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]); |
| 427 | gc_map_offset = (gc_map_size == 0) ? 0 : offset; |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 428 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 429 | if (kIsDebugBuild) { |
| 430 | // We expect GC maps except when the class hasn't been verified or the method is native |
| 431 | ClassReference class_ref(&dex_file, class_def_index); |
| 432 | CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref); |
| 433 | mirror::Class::Status status; |
| 434 | if (compiled_class != NULL) { |
| 435 | status = compiled_class->GetStatus(); |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame^] | 436 | } else if (compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 437 | status = mirror::Class::kStatusError; |
| 438 | } else { |
| 439 | status = mirror::Class::kStatusNotReady; |
| 440 | } |
| 441 | CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified) |
| 442 | << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " " |
| 443 | << (status < mirror::Class::kStatusVerified) << " " << status << " " |
| 444 | << PrettyMethod(method_idx, dex_file); |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 445 | } |
Ian Rogers | c20a83e | 2012-01-18 18:15:32 -0800 | [diff] [blame] | 446 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 447 | // Deduplicate GC maps |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 448 | SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = |
| 449 | gc_map_offsets_.find(&gc_map); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 450 | if (gc_map_iter != gc_map_offsets_.end()) { |
| 451 | gc_map_offset = gc_map_iter->second; |
| 452 | } else { |
| 453 | gc_map_offsets_.Put(&gc_map, gc_map_offset); |
| 454 | offset += gc_map_size; |
| 455 | oat_header_->UpdateChecksum(&gc_map[0], gc_map_size); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 456 | } |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 457 | |
| 458 | oat_class->method_offsets_[*method_offsets_index] = |
| 459 | OatMethodOffsets(code_offset, |
| 460 | frame_size_in_bytes, |
| 461 | core_spill_mask, |
| 462 | fp_spill_mask, |
| 463 | mapping_table_offset, |
| 464 | vmap_table_offset, |
| 465 | gc_map_offset); |
| 466 | (*method_offsets_index)++; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 469 | |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 470 | if (compiler_driver_->IsImage()) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 471 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 472 | // Unchecked as we hold mutator_lock_ on entry. |
| 473 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 474 | SirtRef<mirror::DexCache> dex_cache(soa.Self(), linker->FindDexCache(dex_file)); |
| 475 | SirtRef<mirror::ClassLoader> class_loader(soa.Self(), nullptr); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 476 | mirror::ArtMethod* method = linker->ResolveMethod(dex_file, method_idx, dex_cache, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 477 | class_loader, nullptr, invoke_type); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 478 | CHECK(method != NULL); |
| 479 | method->SetFrameSizeInBytes(frame_size_in_bytes); |
| 480 | method->SetCoreSpillMask(core_spill_mask); |
| 481 | method->SetFpSpillMask(fp_spill_mask); |
| 482 | method->SetOatMappingTableOffset(mapping_table_offset); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 483 | // Don't overwrite static method trampoline |
| 484 | if (!method->IsStatic() || method->IsConstructor() || |
| 485 | method->GetDeclaringClass()->IsInitialized()) { |
| 486 | method->SetOatCodeOffset(code_offset); |
| 487 | } else { |
Jeff Hao | aa4a793 | 2013-05-13 11:28:27 -0700 | [diff] [blame] | 488 | method->SetEntryPointFromCompiledCode(NULL); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 489 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 490 | method->SetOatVmapTableOffset(vmap_table_offset); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 491 | method->SetOatNativeGcMapOffset(gc_map_offset); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 492 | } |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 493 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 494 | return offset; |
| 495 | } |
| 496 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 497 | #define DCHECK_OFFSET() \ |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 498 | DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out.Seek(0, kSeekCurrent)) \ |
| 499 | << "file_offset=" << file_offset << " relative_offset=" << relative_offset |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 500 | |
| 501 | #define DCHECK_OFFSET_() \ |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 502 | DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out.Seek(0, kSeekCurrent)) \ |
| 503 | << "file_offset=" << file_offset << " offset_=" << offset_ |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 504 | |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 505 | bool OatWriter::Write(OutputStream& out) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 506 | const size_t file_offset = out.Seek(0, kSeekCurrent); |
| 507 | |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 508 | if (!out.WriteFully(oat_header_, sizeof(*oat_header_))) { |
| 509 | PLOG(ERROR) << "Failed to write oat header to " << out.GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 510 | return false; |
| 511 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 512 | size_oat_header_ += sizeof(*oat_header_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 513 | |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 514 | if (!out.WriteFully(image_file_location_.data(), image_file_location_.size())) { |
| 515 | 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] | 516 | return false; |
| 517 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 518 | size_oat_header_image_file_location_ += image_file_location_.size(); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 519 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 520 | if (!WriteTables(out, file_offset)) { |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 521 | LOG(ERROR) << "Failed to write oat tables to " << out.GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 522 | return false; |
| 523 | } |
| 524 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 525 | size_t relative_offset = WriteCode(out, file_offset); |
| 526 | if (relative_offset == 0) { |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 527 | LOG(ERROR) << "Failed to write oat code to " << out.GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 528 | return false; |
| 529 | } |
| 530 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 531 | relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset); |
| 532 | if (relative_offset == 0) { |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 533 | 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] | 534 | return false; |
| 535 | } |
| 536 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 537 | if (kIsDebugBuild) { |
| 538 | uint32_t size_total = 0; |
| 539 | #define DO_STAT(x) \ |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 540 | VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \ |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 541 | size_total += x; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 542 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 543 | DO_STAT(size_dex_file_alignment_); |
| 544 | DO_STAT(size_executable_offset_alignment_); |
| 545 | DO_STAT(size_oat_header_); |
| 546 | DO_STAT(size_oat_header_image_file_location_); |
| 547 | DO_STAT(size_dex_file_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 548 | DO_STAT(size_interpreter_to_interpreter_bridge_); |
| 549 | DO_STAT(size_interpreter_to_compiled_code_bridge_); |
| 550 | DO_STAT(size_jni_dlsym_lookup_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 551 | DO_STAT(size_portable_imt_conflict_trampoline_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 552 | DO_STAT(size_portable_resolution_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 553 | DO_STAT(size_portable_to_interpreter_bridge_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 554 | DO_STAT(size_quick_imt_conflict_trampoline_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 555 | DO_STAT(size_quick_resolution_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 556 | DO_STAT(size_quick_to_interpreter_bridge_); |
| 557 | DO_STAT(size_trampoline_alignment_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 558 | DO_STAT(size_code_size_); |
| 559 | DO_STAT(size_code_); |
| 560 | DO_STAT(size_code_alignment_); |
| 561 | DO_STAT(size_mapping_table_); |
| 562 | DO_STAT(size_vmap_table_); |
| 563 | DO_STAT(size_gc_map_); |
| 564 | DO_STAT(size_oat_dex_file_location_size_); |
| 565 | DO_STAT(size_oat_dex_file_location_data_); |
| 566 | DO_STAT(size_oat_dex_file_location_checksum_); |
| 567 | DO_STAT(size_oat_dex_file_offset_); |
| 568 | DO_STAT(size_oat_dex_file_methods_offsets_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 569 | DO_STAT(size_oat_class_type_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 570 | DO_STAT(size_oat_class_status_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 571 | DO_STAT(size_oat_class_method_bitmaps_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 572 | DO_STAT(size_oat_class_method_offsets_); |
| 573 | #undef DO_STAT |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 574 | |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 575 | VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \ |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 576 | CHECK_EQ(file_offset + size_total, static_cast<uint32_t>(out.Seek(0, kSeekCurrent))); |
| 577 | CHECK_EQ(size_, size_total); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 578 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 579 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 580 | CHECK_EQ(file_offset + size_, static_cast<uint32_t>(out.Seek(0, kSeekCurrent))); |
| 581 | CHECK_EQ(size_, relative_offset); |
| 582 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 583 | return true; |
| 584 | } |
| 585 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 586 | bool OatWriter::WriteTables(OutputStream& out, const size_t file_offset) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 587 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 588 | if (!oat_dex_files_[i]->Write(this, out, file_offset)) { |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 589 | PLOG(ERROR) << "Failed to write oat dex information to " << out.GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 590 | return false; |
| 591 | } |
| 592 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 593 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 594 | uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_; |
Brian Carlstrom | 49a0f15 | 2013-01-22 17:17:36 -0800 | [diff] [blame] | 595 | off_t actual_offset = out.Seek(expected_offset, kSeekSet); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 596 | if (static_cast<uint32_t>(actual_offset) != expected_offset) { |
| 597 | const DexFile* dex_file = (*dex_files_)[i]; |
| 598 | PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset |
| 599 | << " Expected: " << expected_offset << " File: " << dex_file->GetLocation(); |
| 600 | return false; |
| 601 | } |
| 602 | const DexFile* dex_file = (*dex_files_)[i]; |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 603 | if (!out.WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 604 | PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() |
| 605 | << " to " << out.GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 606 | return false; |
| 607 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 608 | size_dex_file_ += dex_file->GetHeader().file_size_; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 609 | } |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 610 | for (size_t i = 0; i != oat_classes_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 611 | if (!oat_classes_[i]->Write(this, out, file_offset)) { |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 612 | PLOG(ERROR) << "Failed to write oat methods information to " << out.GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 613 | return false; |
| 614 | } |
| 615 | } |
| 616 | return true; |
| 617 | } |
| 618 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 619 | size_t OatWriter::WriteCode(OutputStream& out, const size_t file_offset) { |
| 620 | size_t relative_offset = oat_header_->GetExecutableOffset(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 621 | off_t new_offset = out.Seek(size_executable_offset_alignment_, kSeekCurrent); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 622 | size_t expected_file_offset = file_offset + relative_offset; |
| 623 | if (static_cast<uint32_t>(new_offset) != expected_file_offset) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 624 | PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 625 | << " Expected: " << expected_file_offset << " File: " << out.GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 626 | return 0; |
| 627 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 628 | DCHECK_OFFSET(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 629 | if (compiler_driver_->IsImage()) { |
| 630 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 631 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 632 | #define DO_TRAMPOLINE(field) \ |
| 633 | do { \ |
| 634 | uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \ |
| 635 | uint32_t alignment_padding = aligned_offset - relative_offset; \ |
| 636 | out.Seek(alignment_padding, kSeekCurrent); \ |
| 637 | size_trampoline_alignment_ += alignment_padding; \ |
| 638 | if (!out.WriteFully(&(*field)[0], field->size())) { \ |
| 639 | PLOG(ERROR) << "Failed to write " # field " to " << out.GetLocation(); \ |
| 640 | return false; \ |
| 641 | } \ |
| 642 | size_ ## field += field->size(); \ |
| 643 | relative_offset += alignment_padding + field->size(); \ |
| 644 | DCHECK_OFFSET(); \ |
| 645 | } while (false) |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 646 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 647 | DO_TRAMPOLINE(interpreter_to_interpreter_bridge_); |
| 648 | DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_); |
| 649 | DO_TRAMPOLINE(jni_dlsym_lookup_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 650 | DO_TRAMPOLINE(portable_imt_conflict_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 651 | DO_TRAMPOLINE(portable_resolution_trampoline_); |
| 652 | DO_TRAMPOLINE(portable_to_interpreter_bridge_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 653 | DO_TRAMPOLINE(quick_imt_conflict_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 654 | DO_TRAMPOLINE(quick_resolution_trampoline_); |
| 655 | DO_TRAMPOLINE(quick_to_interpreter_bridge_); |
| 656 | #undef DO_TRAMPOLINE |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 657 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 658 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 661 | size_t OatWriter::WriteCodeDexFiles(OutputStream& out, |
| 662 | const size_t file_offset, |
| 663 | size_t relative_offset) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 664 | size_t oat_class_index = 0; |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 665 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 666 | const DexFile* dex_file = (*dex_files_)[i]; |
| 667 | CHECK(dex_file != NULL); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 668 | relative_offset = WriteCodeDexFile(out, file_offset, relative_offset, &oat_class_index, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 669 | *dex_file); |
| 670 | if (relative_offset == 0) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 671 | return 0; |
| 672 | } |
| 673 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 674 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 677 | size_t OatWriter::WriteCodeDexFile(OutputStream& out, const size_t file_offset, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 678 | size_t relative_offset, size_t* oat_class_index, |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 679 | const DexFile& dex_file) { |
| 680 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 681 | class_def_index++, (*oat_class_index)++) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 682 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 683 | relative_offset = WriteCodeClassDef(out, file_offset, relative_offset, *oat_class_index, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 684 | dex_file, class_def); |
| 685 | if (relative_offset == 0) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 686 | return 0; |
| 687 | } |
| 688 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 689 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 692 | void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx, |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 693 | const DexFile& dex_file, OutputStream& out) const { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 694 | PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file) |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 695 | << " to " << out.GetLocation(); |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 698 | size_t OatWriter::WriteCodeClassDef(OutputStream& out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 699 | const size_t file_offset, |
| 700 | size_t relative_offset, |
| 701 | size_t oat_class_index, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 702 | const DexFile& dex_file, |
| 703 | const DexFile::ClassDef& class_def) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 704 | const byte* class_data = dex_file.GetClassData(class_def); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 705 | if (class_data == NULL) { |
| 706 | // ie. an empty class such as a marker interface |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 707 | return relative_offset; |
Ian Rogers | 387b699 | 2011-10-31 17:52:37 -0700 | [diff] [blame] | 708 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 709 | ClassDataItemIterator it(dex_file, class_data); |
| 710 | // Skip fields |
| 711 | while (it.HasNextStaticField()) { |
| 712 | it.Next(); |
| 713 | } |
| 714 | while (it.HasNextInstanceField()) { |
| 715 | it.Next(); |
| 716 | } |
| 717 | // Process methods |
| 718 | size_t class_def_method_index = 0; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 719 | size_t method_offsets_index = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 720 | while (it.HasNextDirectMethod()) { |
| 721 | bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0; |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 722 | relative_offset = WriteCodeMethod(out, file_offset, relative_offset, oat_class_index, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 723 | class_def_method_index, &method_offsets_index, is_static, |
| 724 | it.GetMemberIndex(), dex_file); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 725 | if (relative_offset == 0) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 726 | return 0; |
| 727 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 728 | class_def_method_index++; |
| 729 | it.Next(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 730 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 731 | while (it.HasNextVirtualMethod()) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 732 | relative_offset = WriteCodeMethod(out, file_offset, relative_offset, oat_class_index, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 733 | class_def_method_index, &method_offsets_index, false, |
| 734 | it.GetMemberIndex(), dex_file); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 735 | if (relative_offset == 0) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 736 | return 0; |
| 737 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 738 | class_def_method_index++; |
| 739 | it.Next(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 740 | } |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 741 | DCHECK(!it.HasNext()); |
| 742 | CHECK_LE(method_offsets_index, class_def_method_index); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 743 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 746 | size_t OatWriter::WriteCodeMethod(OutputStream& out, const size_t file_offset, |
| 747 | size_t relative_offset, size_t oat_class_index, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 748 | size_t class_def_method_index, size_t* method_offsets_index, |
| 749 | bool is_static, uint32_t method_idx, const DexFile& dex_file) { |
| 750 | OatClass* oat_class = oat_classes_[oat_class_index]; |
| 751 | const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 752 | |
| 753 | if (compiled_method != NULL) { // ie. not an abstract method |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 754 | const OatMethodOffsets method_offsets = oat_class->method_offsets_[*method_offsets_index]; |
| 755 | (*method_offsets_index)++; |
| 756 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 757 | #if !defined(ART_USE_PORTABLE_COMPILER) |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 758 | uint32_t aligned_offset = compiled_method->AlignCode(relative_offset); |
| 759 | uint32_t aligned_code_delta = aligned_offset - relative_offset; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 760 | if (aligned_code_delta != 0) { |
Brian Carlstrom | 49a0f15 | 2013-01-22 17:17:36 -0800 | [diff] [blame] | 761 | off_t new_offset = out.Seek(aligned_code_delta, kSeekCurrent); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 762 | size_code_alignment_ += aligned_code_delta; |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 763 | uint32_t expected_offset = file_offset + aligned_offset; |
| 764 | if (static_cast<uint32_t>(new_offset) != expected_offset) { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 765 | PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 766 | << " Expected: " << expected_offset << " File: " << out.GetLocation(); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 767 | return 0; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 768 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 769 | relative_offset += aligned_code_delta; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 770 | DCHECK_OFFSET(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 771 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 772 | DCHECK_ALIGNED(relative_offset, kArmAlignment); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 773 | const std::vector<uint8_t>& code = compiled_method->GetCode(); |
| 774 | uint32_t code_size = code.size() * sizeof(code[0]); |
| 775 | CHECK_NE(code_size, 0U); |
| 776 | |
| 777 | // Deduplicate code arrays |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 778 | size_t code_offset = relative_offset + sizeof(code_size) + compiled_method->CodeDelta(); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 779 | SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 780 | if (code_iter != code_offsets_.end() && code_offset != method_offsets.code_offset_) { |
| 781 | DCHECK(code_iter->second == method_offsets.code_offset_) |
| 782 | << PrettyMethod(method_idx, dex_file); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 783 | } else { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 784 | DCHECK(code_offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 785 | if (!out.WriteFully(&code_size, sizeof(code_size))) { |
| 786 | ReportWriteFailure("method code size", method_idx, dex_file, out); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 787 | return 0; |
| 788 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 789 | size_code_size_ += sizeof(code_size); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 790 | relative_offset += sizeof(code_size); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 791 | DCHECK_OFFSET(); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 792 | if (!out.WriteFully(&code[0], code_size)) { |
| 793 | ReportWriteFailure("method code", method_idx, dex_file, out); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 794 | return 0; |
| 795 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 796 | size_code_ += code_size; |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 797 | relative_offset += code_size; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 798 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 799 | DCHECK_OFFSET(); |
| 800 | #endif |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 801 | |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 802 | const std::vector<uint8_t>& mapping_table = compiled_method->GetMappingTable(); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 803 | size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]); |
| 804 | |
| 805 | // Deduplicate mapping tables |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 806 | SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator mapping_iter = |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 807 | mapping_table_offsets_.find(&mapping_table); |
| 808 | if (mapping_iter != mapping_table_offsets_.end() && |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 809 | relative_offset != method_offsets.mapping_table_offset_) { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 810 | DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0) |
| 811 | || mapping_iter->second == method_offsets.mapping_table_offset_) |
| 812 | << PrettyMethod(method_idx, dex_file); |
| 813 | } else { |
| 814 | DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0) |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 815 | || relative_offset == method_offsets.mapping_table_offset_) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 816 | << PrettyMethod(method_idx, dex_file); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 817 | if (!out.WriteFully(&mapping_table[0], mapping_table_size)) { |
| 818 | ReportWriteFailure("mapping table", method_idx, dex_file, out); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 819 | return 0; |
| 820 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 821 | size_mapping_table_ += mapping_table_size; |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 822 | relative_offset += mapping_table_size; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 823 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 824 | DCHECK_OFFSET(); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 825 | |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 826 | const std::vector<uint8_t>& vmap_table = compiled_method->GetVmapTable(); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 827 | size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]); |
| 828 | |
| 829 | // Deduplicate vmap tables |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 830 | SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator vmap_iter = |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 831 | vmap_table_offsets_.find(&vmap_table); |
| 832 | if (vmap_iter != vmap_table_offsets_.end() && |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 833 | relative_offset != method_offsets.vmap_table_offset_) { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 834 | DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0) |
| 835 | || vmap_iter->second == method_offsets.vmap_table_offset_) |
| 836 | << PrettyMethod(method_idx, dex_file); |
| 837 | } else { |
| 838 | DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0) |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 839 | || relative_offset == method_offsets.vmap_table_offset_) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 840 | << PrettyMethod(method_idx, dex_file); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 841 | if (!out.WriteFully(&vmap_table[0], vmap_table_size)) { |
| 842 | ReportWriteFailure("vmap table", method_idx, dex_file, out); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 843 | return 0; |
| 844 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 845 | size_vmap_table_ += vmap_table_size; |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 846 | relative_offset += vmap_table_size; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 847 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 848 | DCHECK_OFFSET(); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 849 | |
Ian Rogers | fbdc0fa | 2013-04-18 16:48:17 -0700 | [diff] [blame] | 850 | const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap(); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 851 | size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]); |
| 852 | |
| 853 | // Deduplicate GC maps |
| 854 | SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = |
| 855 | gc_map_offsets_.find(&gc_map); |
| 856 | if (gc_map_iter != gc_map_offsets_.end() && |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 857 | relative_offset != method_offsets.gc_map_offset_) { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 858 | DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0) |
| 859 | || gc_map_iter->second == method_offsets.gc_map_offset_) |
| 860 | << PrettyMethod(method_idx, dex_file); |
| 861 | } else { |
| 862 | DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0) |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 863 | || relative_offset == method_offsets.gc_map_offset_) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 864 | << PrettyMethod(method_idx, dex_file); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 865 | if (!out.WriteFully(&gc_map[0], gc_map_size)) { |
| 866 | ReportWriteFailure("GC map", method_idx, dex_file, out); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 867 | return 0; |
| 868 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 869 | size_gc_map_ += gc_map_size; |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 870 | relative_offset += gc_map_size; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 871 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 872 | DCHECK_OFFSET(); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 873 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 874 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 875 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 876 | } |
| 877 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 878 | OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) { |
| 879 | offset_ = offset; |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 880 | const std::string& location(dex_file.GetLocation()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 881 | dex_file_location_size_ = location.size(); |
| 882 | dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data()); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 883 | dex_file_location_checksum_ = dex_file.GetLocationChecksum(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 884 | dex_file_offset_ = 0; |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 885 | methods_offsets_.resize(dex_file.NumClassDefs()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | size_t OatWriter::OatDexFile::SizeOf() const { |
| 889 | return sizeof(dex_file_location_size_) |
| 890 | + dex_file_location_size_ |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 891 | + sizeof(dex_file_location_checksum_) |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 892 | + sizeof(dex_file_offset_) |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 893 | + (sizeof(methods_offsets_[0]) * methods_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const { |
| 897 | oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_)); |
| 898 | oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 899 | oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_)); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 900 | oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_)); |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 901 | oat_header.UpdateChecksum(&methods_offsets_[0], |
| 902 | sizeof(methods_offsets_[0]) * methods_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 903 | } |
| 904 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 905 | bool OatWriter::OatDexFile::Write(OatWriter* oat_writer, |
| 906 | OutputStream& out, |
| 907 | const size_t file_offset) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 908 | DCHECK_OFFSET_(); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 909 | if (!out.WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) { |
| 910 | PLOG(ERROR) << "Failed to write dex file location length to " << out.GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 911 | return false; |
| 912 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 913 | oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 914 | if (!out.WriteFully(dex_file_location_data_, dex_file_location_size_)) { |
| 915 | PLOG(ERROR) << "Failed to write dex file location data to " << out.GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 916 | return false; |
| 917 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 918 | oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_; |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 919 | if (!out.WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) { |
| 920 | PLOG(ERROR) << "Failed to write dex file location checksum to " << out.GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 921 | return false; |
| 922 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 923 | oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 924 | if (!out.WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) { |
| 925 | PLOG(ERROR) << "Failed to write dex file offset to " << out.GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 926 | return false; |
| 927 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 928 | oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 929 | if (!out.WriteFully(&methods_offsets_[0], |
| 930 | sizeof(methods_offsets_[0]) * methods_offsets_.size())) { |
| 931 | PLOG(ERROR) << "Failed to write methods offsets to " << out.GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 932 | return false; |
| 933 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 934 | oat_writer->size_oat_dex_file_methods_offsets_ += |
| 935 | sizeof(methods_offsets_[0]) * methods_offsets_.size(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 936 | return true; |
| 937 | } |
| 938 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 939 | OatWriter::OatClass::OatClass(size_t offset, |
| 940 | std::vector<CompiledMethod*>* compiled_methods, |
| 941 | uint32_t num_non_null_compiled_methods, |
| 942 | mirror::Class::Status status) { |
| 943 | CHECK(compiled_methods != NULL); |
| 944 | uint32_t num_methods = compiled_methods->size(); |
| 945 | CHECK_LE(num_non_null_compiled_methods, num_methods); |
| 946 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 947 | offset_ = offset; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 948 | compiled_methods_ = compiled_methods; |
| 949 | oat_method_offsets_offsets_from_oat_class_.resize(num_methods); |
| 950 | |
| 951 | // Since both kOatClassNoneCompiled and kOatClassAllCompiled could |
| 952 | // apply when there are 0 methods, we just arbitrarily say that 0 |
| 953 | // methods means kOatClassNoneCompiled and that we won't use |
| 954 | // kOatClassAllCompiled unless there is at least one compiled |
| 955 | // method. This means in an interpretter only system, we can assert |
| 956 | // that all classes are kOatClassNoneCompiled. |
| 957 | if (num_non_null_compiled_methods == 0) { |
| 958 | type_ = kOatClassNoneCompiled; |
| 959 | } else if (num_non_null_compiled_methods == num_methods) { |
| 960 | type_ = kOatClassAllCompiled; |
| 961 | } else { |
| 962 | type_ = kOatClassSomeCompiled; |
| 963 | } |
| 964 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 965 | status_ = status; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 966 | method_offsets_.resize(num_non_null_compiled_methods); |
| 967 | |
| 968 | uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_); |
| 969 | if (type_ == kOatClassSomeCompiled) { |
| 970 | method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator()); |
| 971 | method_bitmap_size_ = method_bitmap_->GetSizeOf(); |
| 972 | oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_); |
| 973 | oat_method_offsets_offset_from_oat_class += method_bitmap_size_; |
| 974 | } else { |
| 975 | method_bitmap_ = NULL; |
| 976 | method_bitmap_size_ = 0; |
| 977 | } |
| 978 | |
| 979 | for (size_t i = 0; i < num_methods; i++) { |
| 980 | CompiledMethod* compiled_method = (*compiled_methods_)[i]; |
| 981 | if (compiled_method == NULL) { |
| 982 | oat_method_offsets_offsets_from_oat_class_[i] = 0; |
| 983 | } else { |
| 984 | oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class; |
| 985 | oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets); |
| 986 | if (type_ == kOatClassSomeCompiled) { |
| 987 | method_bitmap_->SetBit(i); |
| 988 | } |
| 989 | } |
| 990 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 991 | } |
| 992 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 993 | OatWriter::OatClass::~OatClass() { |
| 994 | delete compiled_methods_; |
| 995 | } |
| 996 | |
| 997 | #if defined(ART_USE_PORTABLE_COMPILER) |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 998 | size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader( |
| 999 | size_t class_def_method_index_) const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1000 | uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_); |
| 1001 | if (method_offset == 0) { |
| 1002 | return 0; |
| 1003 | } |
| 1004 | return offset_ + method_offset; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass( |
| 1008 | size_t class_def_method_index_) const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1009 | return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_]; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1010 | } |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1011 | #endif |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1012 | |
| 1013 | size_t OatWriter::OatClass::SizeOf() const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1014 | return sizeof(status_) |
| 1015 | + sizeof(type_) |
| 1016 | + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_)) |
| 1017 | + method_bitmap_size_ |
| 1018 | + (sizeof(method_offsets_[0]) * method_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 1021 | void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const { |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1022 | oat_header.UpdateChecksum(&status_, sizeof(status_)); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1023 | oat_header.UpdateChecksum(&type_, sizeof(type_)); |
| 1024 | if (method_bitmap_size_ != 0) { |
| 1025 | CHECK_EQ(kOatClassSomeCompiled, type_); |
| 1026 | oat_header.UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_)); |
| 1027 | oat_header.UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_); |
| 1028 | } |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1029 | oat_header.UpdateChecksum(&method_offsets_[0], |
| 1030 | sizeof(method_offsets_[0]) * method_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1033 | bool OatWriter::OatClass::Write(OatWriter* oat_writer, |
| 1034 | OutputStream& out, |
| 1035 | const size_t file_offset) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1036 | DCHECK_OFFSET_(); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 1037 | if (!out.WriteFully(&status_, sizeof(status_))) { |
| 1038 | PLOG(ERROR) << "Failed to write class status to " << out.GetLocation(); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1039 | return false; |
| 1040 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1041 | oat_writer->size_oat_class_status_ += sizeof(status_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1042 | if (!out.WriteFully(&type_, sizeof(type_))) { |
| 1043 | PLOG(ERROR) << "Failed to write oat class type to " << out.GetLocation(); |
| 1044 | return false; |
| 1045 | } |
| 1046 | oat_writer->size_oat_class_type_ += sizeof(type_); |
| 1047 | if (method_bitmap_size_ != 0) { |
| 1048 | CHECK_EQ(kOatClassSomeCompiled, type_); |
| 1049 | if (!out.WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) { |
| 1050 | PLOG(ERROR) << "Failed to write method bitmap size to " << out.GetLocation(); |
| 1051 | return false; |
| 1052 | } |
| 1053 | oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_); |
| 1054 | if (!out.WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) { |
| 1055 | PLOG(ERROR) << "Failed to write method bitmap to " << out.GetLocation(); |
| 1056 | return false; |
| 1057 | } |
| 1058 | oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_; |
| 1059 | } |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 1060 | if (!out.WriteFully(&method_offsets_[0], |
| 1061 | sizeof(method_offsets_[0]) * method_offsets_.size())) { |
| 1062 | PLOG(ERROR) << "Failed to write method offsets to " << out.GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1063 | return false; |
| 1064 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1065 | 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] | 1066 | return true; |
| 1067 | } |
| 1068 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1069 | } // namespace art |