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