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 | |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 21 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 22 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 23 | #include "class_linker.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 24 | #include "dex_file-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 25 | #include "gc/space/space.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 26 | #include "mirror/abstract_method-inl.h" |
| 27 | #include "mirror/array.h" |
| 28 | #include "mirror/class_loader.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 29 | #include "mirror/object-inl.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 30 | #include "os.h" |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 31 | #include "output_stream.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 32 | #include "safe_map.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 33 | #include "scoped_thread_state_change.h" |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 34 | #include "verifier/method_verifier.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 35 | |
| 36 | namespace art { |
| 37 | |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 38 | bool OatWriter::Create(OutputStream& output_stream, |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 39 | const std::vector<const DexFile*>& dex_files, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 40 | uint32_t image_file_location_oat_checksum, |
| 41 | uint32_t image_file_location_oat_begin, |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 42 | const std::string& image_file_location, |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 43 | const CompilerDriver& driver) { |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 44 | OatWriter oat_writer(dex_files, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 45 | image_file_location_oat_checksum, |
| 46 | image_file_location_oat_begin, |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 47 | image_file_location, |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 48 | &driver); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 49 | return oat_writer.Write(output_stream); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 52 | OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 53 | uint32_t image_file_location_oat_checksum, |
| 54 | uint32_t image_file_location_oat_begin, |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 55 | const std::string& image_file_location, |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 56 | const CompilerDriver* compiler) |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 57 | : compiler_driver_(compiler), |
| 58 | dex_files_(&dex_files), |
| 59 | image_file_location_oat_checksum_(image_file_location_oat_checksum), |
| 60 | image_file_location_oat_begin_(image_file_location_oat_begin), |
| 61 | image_file_location_(image_file_location), |
| 62 | oat_header_(NULL), |
| 63 | size_dex_file_alignment_(0), |
| 64 | size_executable_offset_alignment_(0), |
| 65 | size_oat_header_(0), |
| 66 | size_oat_header_image_file_location_(0), |
| 67 | size_dex_file_(0), |
| 68 | size_interpreter_to_interpreter_entry_(0), |
| 69 | size_interpreter_to_quick_entry_(0), |
| 70 | size_portable_resolution_trampoline_(0), |
| 71 | size_quick_resolution_trampoline_(0), |
| 72 | size_stubs_alignment_(0), |
| 73 | size_code_size_(0), |
| 74 | size_code_(0), |
| 75 | size_code_alignment_(0), |
| 76 | size_mapping_table_(0), |
| 77 | size_vmap_table_(0), |
| 78 | size_gc_map_(0), |
| 79 | size_oat_dex_file_location_size_(0), |
| 80 | size_oat_dex_file_location_data_(0), |
| 81 | size_oat_dex_file_location_checksum_(0), |
| 82 | size_oat_dex_file_offset_(0), |
| 83 | size_oat_dex_file_methods_offsets_(0), |
| 84 | size_oat_class_status_(0), |
| 85 | size_oat_class_method_offsets_(0) { |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 86 | size_t offset = InitOatHeader(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 87 | offset = InitOatDexFiles(offset); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 88 | offset = InitDexFiles(offset); |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 89 | offset = InitOatClasses(offset); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 90 | offset = InitOatCode(offset); |
| 91 | offset = InitOatCodeDexFiles(offset); |
| 92 | |
| 93 | CHECK_EQ(dex_files_->size(), oat_dex_files_.size()); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 94 | CHECK(image_file_location.empty() == compiler->IsImage()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 97 | OatWriter::~OatWriter() { |
| 98 | delete oat_header_; |
| 99 | STLDeleteElements(&oat_dex_files_); |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 100 | STLDeleteElements(&oat_classes_); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 103 | size_t OatWriter::InitOatHeader() { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 104 | // create the OatHeader |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 105 | oat_header_ = new OatHeader(compiler_driver_->GetInstructionSet(), |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 106 | dex_files_, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 107 | image_file_location_oat_checksum_, |
| 108 | image_file_location_oat_begin_, |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 109 | image_file_location_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 110 | size_t offset = sizeof(*oat_header_); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 111 | offset += image_file_location_.size(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 112 | return offset; |
| 113 | } |
| 114 | |
| 115 | size_t OatWriter::InitOatDexFiles(size_t offset) { |
| 116 | // create the OatDexFiles |
| 117 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 118 | const DexFile* dex_file = (*dex_files_)[i]; |
| 119 | CHECK(dex_file != NULL); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 120 | OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 121 | oat_dex_files_.push_back(oat_dex_file); |
| 122 | offset += oat_dex_file->SizeOf(); |
| 123 | } |
| 124 | return offset; |
| 125 | } |
| 126 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 127 | size_t OatWriter::InitDexFiles(size_t offset) { |
| 128 | // calculate the offsets within OatDexFiles to the DexFiles |
| 129 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 130 | // dex files are required to be 4 byte aligned |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 131 | size_t original_offset = offset; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 132 | offset = RoundUp(offset, 4); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 133 | size_dex_file_alignment_ += offset - original_offset; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 134 | |
| 135 | // set offset in OatDexFile to DexFile |
| 136 | oat_dex_files_[i]->dex_file_offset_ = offset; |
| 137 | |
| 138 | const DexFile* dex_file = (*dex_files_)[i]; |
| 139 | offset += dex_file->GetHeader().file_size_; |
| 140 | } |
| 141 | return offset; |
| 142 | } |
| 143 | |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 144 | size_t OatWriter::InitOatClasses(size_t offset) { |
| 145 | // create the OatClasses |
| 146 | // calculate the offsets within OatDexFiles to OatClasses |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 147 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 148 | const DexFile* dex_file = (*dex_files_)[i]; |
| 149 | for (size_t class_def_index = 0; |
| 150 | class_def_index < dex_file->NumClassDefs(); |
Ian Rogers | c20a83e | 2012-01-18 18:15:32 -0800 | [diff] [blame] | 151 | class_def_index++) { |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 152 | oat_dex_files_[i]->methods_offsets_[class_def_index] = offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 153 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
| 154 | const byte* class_data = dex_file->GetClassData(class_def); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 155 | uint32_t num_methods = 0; |
| 156 | if (class_data != NULL) { // ie not an empty class, such as a marker interface |
| 157 | ClassDataItemIterator it(*dex_file, class_data); |
| 158 | size_t num_direct_methods = it.NumDirectMethods(); |
| 159 | size_t num_virtual_methods = it.NumVirtualMethods(); |
| 160 | num_methods = num_direct_methods + num_virtual_methods; |
| 161 | } |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 162 | |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 163 | ClassReference class_ref(dex_file, class_def_index); |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 164 | CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 165 | mirror::Class::Status status; |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 166 | if (compiled_class != NULL) { |
| 167 | status = compiled_class->GetStatus(); |
| 168 | } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 169 | status = mirror::Class::kStatusError; |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 170 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 171 | status = mirror::Class::kStatusNotReady; |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 172 | } |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 173 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 174 | OatClass* oat_class = new OatClass(offset, status, num_methods); |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 175 | oat_classes_.push_back(oat_class); |
| 176 | offset += oat_class->SizeOf(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 177 | } |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 178 | oat_dex_files_[i]->UpdateChecksum(*oat_header_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 179 | } |
| 180 | return offset; |
| 181 | } |
| 182 | |
| 183 | size_t OatWriter::InitOatCode(size_t offset) { |
| 184 | // calculate the offsets within OatHeader to executable code |
| 185 | size_t old_offset = offset; |
| 186 | // required to be on a new page boundary |
| 187 | offset = RoundUp(offset, kPageSize); |
| 188 | oat_header_->SetExecutableOffset(offset); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 189 | size_executable_offset_alignment_ = offset - old_offset; |
| 190 | if (compiler_driver_->IsImage()) { |
| 191 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
| 192 | oat_header_->SetInterpreterToInterpreterEntryOffset(offset); |
| 193 | interpreter_to_interpreter_entry_.reset(compiler_driver_->CreateInterpreterToInterpreterEntry()); |
| 194 | offset += interpreter_to_interpreter_entry_->size(); |
| 195 | |
| 196 | offset = CompiledCode::AlignCode(offset, instruction_set); |
| 197 | oat_header_->SetInterpreterToQuickEntryOffset(offset); |
| 198 | interpreter_to_quick_entry_.reset(compiler_driver_->CreateInterpreterToQuickEntry()); |
| 199 | offset += interpreter_to_quick_entry_->size(); |
| 200 | |
| 201 | offset = CompiledCode::AlignCode(offset, instruction_set); |
| 202 | oat_header_->SetPortableResolutionTrampolineOffset(offset); |
| 203 | portable_resolution_trampoline_.reset(compiler_driver_->CreatePortableResolutionTrampoline()); |
| 204 | offset += portable_resolution_trampoline_->size(); |
| 205 | |
| 206 | offset = CompiledCode::AlignCode(offset, instruction_set); |
| 207 | oat_header_->SetQuickResolutionTrampolineOffset(offset); |
| 208 | quick_resolution_trampoline_.reset(compiler_driver_->CreateQuickResolutionTrampoline()); |
| 209 | offset += quick_resolution_trampoline_->size(); |
| 210 | } else { |
| 211 | oat_header_->SetInterpreterToInterpreterEntryOffset(0); |
| 212 | oat_header_->SetInterpreterToQuickEntryOffset(0); |
| 213 | oat_header_->SetPortableResolutionTrampolineOffset(0); |
| 214 | oat_header_->SetQuickResolutionTrampolineOffset(0); |
| 215 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 216 | return offset; |
| 217 | } |
| 218 | |
| 219 | size_t OatWriter::InitOatCodeDexFiles(size_t offset) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 220 | size_t oat_class_index = 0; |
| 221 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 222 | const DexFile* dex_file = (*dex_files_)[i]; |
| 223 | CHECK(dex_file != NULL); |
| 224 | offset = InitOatCodeDexFile(offset, oat_class_index, *dex_file); |
| 225 | } |
| 226 | return offset; |
| 227 | } |
| 228 | |
| 229 | size_t OatWriter::InitOatCodeDexFile(size_t offset, |
| 230 | size_t& oat_class_index, |
| 231 | const DexFile& dex_file) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 232 | for (size_t class_def_index = 0; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 233 | class_def_index < dex_file.NumClassDefs(); |
| 234 | class_def_index++, oat_class_index++) { |
| 235 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Ian Rogers | c20a83e | 2012-01-18 18:15:32 -0800 | [diff] [blame] | 236 | offset = InitOatCodeClassDef(offset, oat_class_index, class_def_index, dex_file, class_def); |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 237 | oat_classes_[oat_class_index]->UpdateChecksum(*oat_header_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 238 | } |
| 239 | return offset; |
| 240 | } |
| 241 | |
| 242 | size_t OatWriter::InitOatCodeClassDef(size_t offset, |
Ian Rogers | c20a83e | 2012-01-18 18:15:32 -0800 | [diff] [blame] | 243 | size_t oat_class_index, size_t class_def_index, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 244 | const DexFile& dex_file, |
| 245 | const DexFile::ClassDef& class_def) { |
| 246 | const byte* class_data = dex_file.GetClassData(class_def); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 247 | if (class_data == NULL) { |
| 248 | // empty class, such as a marker interface |
Ian Rogers | 387b699 | 2011-10-31 17:52:37 -0700 | [diff] [blame] | 249 | return offset; |
| 250 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 251 | ClassDataItemIterator it(dex_file, class_data); |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 252 | CHECK_EQ(oat_classes_[oat_class_index]->method_offsets_.size(), |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 253 | it.NumDirectMethods() + it.NumVirtualMethods()); |
| 254 | // Skip fields |
| 255 | while (it.HasNextStaticField()) { |
| 256 | it.Next(); |
| 257 | } |
| 258 | while (it.HasNextInstanceField()) { |
| 259 | it.Next(); |
| 260 | } |
| 261 | // Process methods |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 262 | size_t class_def_method_index = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 263 | while (it.HasNextDirectMethod()) { |
Ian Rogers | c20a83e | 2012-01-18 18:15:32 -0800 | [diff] [blame] | 264 | bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 265 | offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index, |
| 266 | is_native, it.GetMethodInvokeType(class_def), it.GetMemberIndex(), |
| 267 | &dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 268 | class_def_method_index++; |
| 269 | it.Next(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 270 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 271 | while (it.HasNextVirtualMethod()) { |
Ian Rogers | c20a83e | 2012-01-18 18:15:32 -0800 | [diff] [blame] | 272 | bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 273 | offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index, |
| 274 | is_native, it.GetMethodInvokeType(class_def), it.GetMemberIndex(), |
| 275 | &dex_file); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 276 | class_def_method_index++; |
| 277 | it.Next(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 278 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 279 | DCHECK(!it.HasNext()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 280 | return offset; |
| 281 | } |
| 282 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 283 | size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index, |
| 284 | size_t __attribute__((unused)) class_def_index, |
| 285 | size_t class_def_method_index, |
| 286 | bool __attribute__((unused)) is_native, |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 287 | InvokeType invoke_type, |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 288 | uint32_t method_idx, const DexFile* dex_file) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 289 | // derived from CompiledMethod if available |
| 290 | uint32_t code_offset = 0; |
| 291 | uint32_t frame_size_in_bytes = kStackAlignment; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 292 | uint32_t core_spill_mask = 0; |
| 293 | uint32_t fp_spill_mask = 0; |
| 294 | uint32_t mapping_table_offset = 0; |
| 295 | uint32_t vmap_table_offset = 0; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 296 | uint32_t gc_map_offset = 0; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 297 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 298 | OatClass* oat_class = oat_classes_[oat_class_index]; |
| 299 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 300 | size_t oat_method_offsets_offset = |
| 301 | oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index); |
| 302 | #endif |
| 303 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 304 | CompiledMethod* compiled_method = |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 305 | compiler_driver_->GetCompiledMethod(MethodReference(dex_file, method_idx)); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 306 | if (compiled_method != NULL) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 307 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 308 | compiled_method->AddOatdataOffsetToCompliledCodeOffset( |
| 309 | oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_)); |
| 310 | #else |
| 311 | const std::vector<uint8_t>& code = compiled_method->GetCode(); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 312 | offset = compiled_method->AlignCode(offset); |
| 313 | DCHECK_ALIGNED(offset, kArmAlignment); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 314 | uint32_t code_size = code.size() * sizeof(code[0]); |
| 315 | CHECK_NE(code_size, 0U); |
| 316 | uint32_t thumb_offset = compiled_method->CodeDelta(); |
| 317 | code_offset = offset + sizeof(code_size) + thumb_offset; |
| 318 | |
| 319 | // Deduplicate code arrays |
| 320 | SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code); |
| 321 | if (code_iter != code_offsets_.end()) { |
| 322 | code_offset = code_iter->second; |
jeffhao | 55d7821 | 2011-11-02 11:41:50 -0700 | [diff] [blame] | 323 | } else { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 324 | code_offsets_.Put(&code, code_offset); |
| 325 | offset += sizeof(code_size); // code size is prepended before code |
| 326 | offset += code_size; |
| 327 | oat_header_->UpdateChecksum(&code[0], code_size); |
| 328 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 329 | #endif |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 330 | frame_size_in_bytes = compiled_method->GetFrameSizeInBytes(); |
| 331 | core_spill_mask = compiled_method->GetCoreSpillMask(); |
| 332 | fp_spill_mask = compiled_method->GetFpSpillMask(); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 333 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 334 | const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable(); |
| 335 | size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]); |
| 336 | mapping_table_offset = (mapping_table_size == 0) ? 0 : offset; |
jeffhao | 55d7821 | 2011-11-02 11:41:50 -0700 | [diff] [blame] | 337 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 338 | // Deduplicate mapping tables |
| 339 | SafeMap<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table); |
| 340 | if (mapping_iter != mapping_table_offsets_.end()) { |
| 341 | mapping_table_offset = mapping_iter->second; |
| 342 | } else { |
| 343 | mapping_table_offsets_.Put(&mapping_table, mapping_table_offset); |
| 344 | offset += mapping_table_size; |
| 345 | oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size); |
| 346 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 347 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 348 | const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable(); |
| 349 | size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]); |
| 350 | vmap_table_offset = (vmap_table_size == 0) ? 0 : offset; |
jeffhao | 55d7821 | 2011-11-02 11:41:50 -0700 | [diff] [blame] | 351 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 352 | // Deduplicate vmap tables |
| 353 | SafeMap<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table); |
| 354 | if (vmap_iter != vmap_table_offsets_.end()) { |
| 355 | vmap_table_offset = vmap_iter->second; |
| 356 | } else { |
| 357 | vmap_table_offsets_.Put(&vmap_table, vmap_table_offset); |
| 358 | offset += vmap_table_size; |
| 359 | oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size); |
| 360 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 361 | |
Ian Rogers | fbdc0fa | 2013-04-18 16:48:17 -0700 | [diff] [blame] | 362 | const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap(); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 363 | size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]); |
| 364 | gc_map_offset = (gc_map_size == 0) ? 0 : offset; |
Logan Chien | ccb7bf1 | 2012-03-28 12:52:32 +0800 | [diff] [blame] | 365 | |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 366 | #if !defined(NDEBUG) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 367 | // We expect GC maps except when the class hasn't been verified or the method is native |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 368 | ClassReference class_ref(dex_file, class_def_index); |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 369 | CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 370 | mirror::Class::Status status; |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 371 | if (compiled_class != NULL) { |
| 372 | status = compiled_class->GetStatus(); |
| 373 | } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 374 | status = mirror::Class::kStatusError; |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 375 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 376 | status = mirror::Class::kStatusNotReady; |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 377 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 378 | CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified) |
| 379 | << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " " |
| 380 | << (status < mirror::Class::kStatusVerified) << " " << status << " " |
| 381 | << PrettyMethod(method_idx, *dex_file); |
Ian Rogers | c20a83e | 2012-01-18 18:15:32 -0800 | [diff] [blame] | 382 | #endif |
| 383 | |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 384 | // Deduplicate GC maps |
| 385 | SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = gc_map_offsets_.find(&gc_map); |
| 386 | if (gc_map_iter != gc_map_offsets_.end()) { |
| 387 | gc_map_offset = gc_map_iter->second; |
| 388 | } else { |
| 389 | gc_map_offsets_.Put(&gc_map, gc_map_offset); |
| 390 | offset += gc_map_size; |
| 391 | oat_header_->UpdateChecksum(&gc_map[0], gc_map_size); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 392 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 395 | oat_class->method_offsets_[class_def_method_index] |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 396 | = OatMethodOffsets(code_offset, |
| 397 | frame_size_in_bytes, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 398 | core_spill_mask, |
| 399 | fp_spill_mask, |
| 400 | mapping_table_offset, |
| 401 | vmap_table_offset, |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 402 | gc_map_offset); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 403 | |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 404 | if (compiler_driver_->IsImage()) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 405 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 406 | mirror::DexCache* dex_cache = linker->FindDexCache(*dex_file); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 407 | // Unchecked as we hold mutator_lock_ on entry. |
| 408 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 409 | mirror::AbstractMethod* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 410 | NULL, NULL, invoke_type); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 411 | CHECK(method != NULL); |
| 412 | method->SetFrameSizeInBytes(frame_size_in_bytes); |
| 413 | method->SetCoreSpillMask(core_spill_mask); |
| 414 | method->SetFpSpillMask(fp_spill_mask); |
| 415 | method->SetOatMappingTableOffset(mapping_table_offset); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 416 | // Don't overwrite static method trampoline |
| 417 | if (!method->IsStatic() || method->IsConstructor() || |
| 418 | method->GetDeclaringClass()->IsInitialized()) { |
| 419 | method->SetOatCodeOffset(code_offset); |
| 420 | } else { |
Jeff Hao | aa4a793 | 2013-05-13 11:28:27 -0700 | [diff] [blame] | 421 | method->SetEntryPointFromCompiledCode(NULL); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 422 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 423 | method->SetOatVmapTableOffset(vmap_table_offset); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 424 | method->SetOatNativeGcMapOffset(gc_map_offset); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 425 | } |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 426 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 427 | return offset; |
| 428 | } |
| 429 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 430 | #define DCHECK_OFFSET() \ |
| 431 | DCHECK_EQ(static_cast<off_t>(offset), out.Seek(0, kSeekCurrent)) |
| 432 | |
| 433 | #define DCHECK_OFFSET_() \ |
| 434 | DCHECK_EQ(static_cast<off_t>(offset_), out.Seek(0, kSeekCurrent)) |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 435 | |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 436 | bool OatWriter::Write(OutputStream& out) { |
| 437 | if (!out.WriteFully(oat_header_, sizeof(*oat_header_))) { |
| 438 | PLOG(ERROR) << "Failed to write oat header to " << out.GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 439 | return false; |
| 440 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 441 | size_oat_header_ += sizeof(*oat_header_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 442 | |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 443 | if (!out.WriteFully(image_file_location_.data(), image_file_location_.size())) { |
| 444 | 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] | 445 | return false; |
| 446 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 447 | size_oat_header_image_file_location_ += image_file_location_.size(); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 448 | |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 449 | if (!WriteTables(out)) { |
| 450 | LOG(ERROR) << "Failed to write oat tables to " << out.GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 451 | return false; |
| 452 | } |
| 453 | |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 454 | size_t code_offset = WriteCode(out); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 455 | if (code_offset == 0) { |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 456 | LOG(ERROR) << "Failed to write oat code to " << out.GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 457 | return false; |
| 458 | } |
| 459 | |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 460 | code_offset = WriteCodeDexFiles(out, code_offset); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 461 | if (code_offset == 0) { |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 462 | 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] | 463 | return false; |
| 464 | } |
| 465 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 466 | if (kIsDebugBuild) { |
| 467 | uint32_t size_total = 0; |
| 468 | #define DO_STAT(x) \ |
| 469 | LOG(INFO) << #x "=" << PrettySize(x) << " (" << x << "B)"; \ |
| 470 | size_total += x; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 471 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 472 | DO_STAT(size_dex_file_alignment_); |
| 473 | DO_STAT(size_executable_offset_alignment_); |
| 474 | DO_STAT(size_oat_header_); |
| 475 | DO_STAT(size_oat_header_image_file_location_); |
| 476 | DO_STAT(size_dex_file_); |
| 477 | DO_STAT(size_interpreter_to_interpreter_entry_); |
| 478 | DO_STAT(size_interpreter_to_quick_entry_); |
| 479 | DO_STAT(size_portable_resolution_trampoline_); |
| 480 | DO_STAT(size_quick_resolution_trampoline_); |
| 481 | DO_STAT(size_stubs_alignment_); |
| 482 | DO_STAT(size_code_size_); |
| 483 | DO_STAT(size_code_); |
| 484 | DO_STAT(size_code_alignment_); |
| 485 | DO_STAT(size_mapping_table_); |
| 486 | DO_STAT(size_vmap_table_); |
| 487 | DO_STAT(size_gc_map_); |
| 488 | DO_STAT(size_oat_dex_file_location_size_); |
| 489 | DO_STAT(size_oat_dex_file_location_data_); |
| 490 | DO_STAT(size_oat_dex_file_location_checksum_); |
| 491 | DO_STAT(size_oat_dex_file_offset_); |
| 492 | DO_STAT(size_oat_dex_file_methods_offsets_); |
| 493 | DO_STAT(size_oat_class_status_); |
| 494 | DO_STAT(size_oat_class_method_offsets_); |
| 495 | #undef DO_STAT |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 496 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 497 | LOG(INFO) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \ |
| 498 | CHECK_EQ(size_total, static_cast<uint32_t>(out.Seek(0, kSeekCurrent))); |
| 499 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 500 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 501 | return true; |
| 502 | } |
| 503 | |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 504 | bool OatWriter::WriteTables(OutputStream& out) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 505 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 506 | if (!oat_dex_files_[i]->Write(this, out)) { |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 507 | PLOG(ERROR) << "Failed to write oat dex information to " << out.GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 508 | return false; |
| 509 | } |
| 510 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 511 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
| 512 | uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_; |
Brian Carlstrom | 49a0f15 | 2013-01-22 17:17:36 -0800 | [diff] [blame] | 513 | off_t actual_offset = out.Seek(expected_offset, kSeekSet); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 514 | if (static_cast<uint32_t>(actual_offset) != expected_offset) { |
| 515 | const DexFile* dex_file = (*dex_files_)[i]; |
| 516 | PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset |
| 517 | << " Expected: " << expected_offset << " File: " << dex_file->GetLocation(); |
| 518 | return false; |
| 519 | } |
| 520 | const DexFile* dex_file = (*dex_files_)[i]; |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 521 | if (!out.WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) { |
| 522 | PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << out.GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 523 | return false; |
| 524 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 525 | size_dex_file_ += dex_file->GetHeader().file_size_; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 526 | } |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 527 | for (size_t i = 0; i != oat_classes_.size(); ++i) { |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 528 | if (!oat_classes_[i]->Write(this, out)) { |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 529 | PLOG(ERROR) << "Failed to write oat methods information to " << out.GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 530 | return false; |
| 531 | } |
| 532 | } |
| 533 | return true; |
| 534 | } |
| 535 | |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 536 | size_t OatWriter::WriteCode(OutputStream& out) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 537 | uint32_t offset = oat_header_->GetExecutableOffset(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 538 | off_t new_offset = out.Seek(size_executable_offset_alignment_, kSeekCurrent); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 539 | if (static_cast<uint32_t>(new_offset) != offset) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 540 | PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 541 | << " Expected: " << offset << " File: " << out.GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 542 | return 0; |
| 543 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 544 | DCHECK_OFFSET(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 545 | if (compiler_driver_->IsImage()) { |
| 546 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
| 547 | if (!out.WriteFully(&(*interpreter_to_interpreter_entry_)[0], interpreter_to_interpreter_entry_->size())) { |
| 548 | PLOG(ERROR) << "Failed to write interpreter to interpreter entry to " << out.GetLocation(); |
| 549 | return false; |
| 550 | } |
| 551 | size_interpreter_to_interpreter_entry_ += interpreter_to_interpreter_entry_->size(); |
| 552 | offset += interpreter_to_interpreter_entry_->size(); |
| 553 | DCHECK_OFFSET(); |
| 554 | |
| 555 | uint32_t aligned_offset = CompiledCode::AlignCode(offset, instruction_set); |
| 556 | uint32_t alignment_padding = aligned_offset - offset; |
| 557 | out.Seek(alignment_padding, kSeekCurrent); |
| 558 | size_stubs_alignment_ += alignment_padding; |
| 559 | if (!out.WriteFully(&(*interpreter_to_quick_entry_)[0], interpreter_to_quick_entry_->size())) { |
| 560 | PLOG(ERROR) << "Failed to write interpreter to quick entry to " << out.GetLocation(); |
| 561 | return false; |
| 562 | } |
| 563 | size_interpreter_to_quick_entry_ += interpreter_to_quick_entry_->size(); |
| 564 | offset += alignment_padding + interpreter_to_quick_entry_->size(); |
| 565 | DCHECK_OFFSET(); |
| 566 | |
| 567 | aligned_offset = CompiledCode::AlignCode(offset, instruction_set); |
| 568 | alignment_padding = aligned_offset - offset; |
| 569 | out.Seek(alignment_padding, kSeekCurrent); |
| 570 | size_stubs_alignment_ += alignment_padding; |
| 571 | if (!out.WriteFully(&(*portable_resolution_trampoline_)[0], portable_resolution_trampoline_->size())) { |
| 572 | PLOG(ERROR) << "Failed to write portable resolution trampoline to " << out.GetLocation(); |
| 573 | return false; |
| 574 | } |
| 575 | size_portable_resolution_trampoline_ += portable_resolution_trampoline_->size(); |
| 576 | offset += alignment_padding + portable_resolution_trampoline_->size(); |
| 577 | DCHECK_OFFSET(); |
| 578 | |
| 579 | aligned_offset = CompiledCode::AlignCode(offset, instruction_set); |
| 580 | alignment_padding = aligned_offset - offset; |
| 581 | out.Seek(alignment_padding, kSeekCurrent); |
| 582 | size_stubs_alignment_ += alignment_padding; |
| 583 | if (!out.WriteFully(&(*quick_resolution_trampoline_)[0], quick_resolution_trampoline_->size())) { |
| 584 | PLOG(ERROR) << "Failed to write quick resolution trampoline to " << out.GetLocation(); |
| 585 | return false; |
| 586 | } |
| 587 | size_quick_resolution_trampoline_ += quick_resolution_trampoline_->size(); |
| 588 | offset += alignment_padding + quick_resolution_trampoline_->size(); |
| 589 | DCHECK_OFFSET(); |
| 590 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 591 | return offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 594 | size_t OatWriter::WriteCodeDexFiles(OutputStream& out, size_t code_offset) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 595 | size_t oat_class_index = 0; |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 596 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 597 | const DexFile* dex_file = (*dex_files_)[i]; |
| 598 | CHECK(dex_file != NULL); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 599 | code_offset = WriteCodeDexFile(out, code_offset, oat_class_index, *dex_file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 600 | if (code_offset == 0) { |
| 601 | return 0; |
| 602 | } |
| 603 | } |
| 604 | return code_offset; |
| 605 | } |
| 606 | |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 607 | size_t OatWriter::WriteCodeDexFile(OutputStream& out, size_t code_offset, size_t& oat_class_index, |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 608 | const DexFile& dex_file) { |
| 609 | for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); |
| 610 | class_def_index++, oat_class_index++) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 611 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 612 | code_offset = WriteCodeClassDef(out, code_offset, oat_class_index, dex_file, class_def); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 613 | if (code_offset == 0) { |
| 614 | return 0; |
| 615 | } |
| 616 | } |
| 617 | return code_offset; |
| 618 | } |
| 619 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 620 | void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx, |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 621 | const DexFile& dex_file, OutputStream& out) const { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 622 | PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file) |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 623 | << " to " << out.GetLocation(); |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 626 | size_t OatWriter::WriteCodeClassDef(OutputStream& out, |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 627 | size_t code_offset, size_t oat_class_index, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 628 | const DexFile& dex_file, |
| 629 | const DexFile::ClassDef& class_def) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 630 | const byte* class_data = dex_file.GetClassData(class_def); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 631 | if (class_data == NULL) { |
| 632 | // ie. an empty class such as a marker interface |
Ian Rogers | 387b699 | 2011-10-31 17:52:37 -0700 | [diff] [blame] | 633 | return code_offset; |
| 634 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 635 | ClassDataItemIterator it(dex_file, class_data); |
| 636 | // Skip fields |
| 637 | while (it.HasNextStaticField()) { |
| 638 | it.Next(); |
| 639 | } |
| 640 | while (it.HasNextInstanceField()) { |
| 641 | it.Next(); |
| 642 | } |
| 643 | // Process methods |
| 644 | size_t class_def_method_index = 0; |
| 645 | while (it.HasNextDirectMethod()) { |
| 646 | bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0; |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 647 | code_offset = WriteCodeMethod(out, code_offset, oat_class_index, class_def_method_index, |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 648 | is_static, it.GetMemberIndex(), dex_file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 649 | if (code_offset == 0) { |
| 650 | return 0; |
| 651 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 652 | class_def_method_index++; |
| 653 | it.Next(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 654 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 655 | while (it.HasNextVirtualMethod()) { |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 656 | code_offset = WriteCodeMethod(out, code_offset, oat_class_index, class_def_method_index, |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 657 | false, it.GetMemberIndex(), dex_file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 658 | if (code_offset == 0) { |
| 659 | return 0; |
| 660 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 661 | class_def_method_index++; |
| 662 | it.Next(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 663 | } |
| 664 | return code_offset; |
| 665 | } |
| 666 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 667 | size_t OatWriter::WriteCodeMethod(OutputStream& out, size_t offset, size_t oat_class_index, |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 668 | size_t class_def_method_index, bool is_static, |
| 669 | uint32_t method_idx, const DexFile& dex_file) { |
| 670 | const CompiledMethod* compiled_method = |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 671 | compiler_driver_->GetCompiledMethod(MethodReference(&dex_file, method_idx)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 672 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 673 | OatMethodOffsets method_offsets = |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 674 | oat_classes_[oat_class_index]->method_offsets_[class_def_method_index]; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 675 | |
| 676 | |
| 677 | if (compiled_method != NULL) { // ie. not an abstract method |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 678 | #if !defined(ART_USE_PORTABLE_COMPILER) |
| 679 | uint32_t aligned_offset = compiled_method->AlignCode(offset); |
| 680 | uint32_t aligned_code_delta = aligned_offset - offset; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 681 | if (aligned_code_delta != 0) { |
Brian Carlstrom | 49a0f15 | 2013-01-22 17:17:36 -0800 | [diff] [blame] | 682 | off_t new_offset = out.Seek(aligned_code_delta, kSeekCurrent); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 683 | size_code_alignment_ += aligned_code_delta; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 684 | if (static_cast<uint32_t>(new_offset) != aligned_offset) { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 685 | PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 686 | << " Expected: " << aligned_offset << " File: " << out.GetLocation(); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 687 | return 0; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 688 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 689 | offset += aligned_code_delta; |
| 690 | DCHECK_OFFSET(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 691 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 692 | DCHECK_ALIGNED(offset, kArmAlignment); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 693 | const std::vector<uint8_t>& code = compiled_method->GetCode(); |
| 694 | uint32_t code_size = code.size() * sizeof(code[0]); |
| 695 | CHECK_NE(code_size, 0U); |
| 696 | |
| 697 | // Deduplicate code arrays |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 698 | size_t code_offset = offset + sizeof(code_size) + compiled_method->CodeDelta(); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 699 | 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] | 700 | if (code_iter != code_offsets_.end() && code_offset != method_offsets.code_offset_) { |
| 701 | DCHECK(code_iter->second == method_offsets.code_offset_) |
| 702 | << PrettyMethod(method_idx, dex_file); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 703 | } else { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 704 | DCHECK(code_offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 705 | if (!out.WriteFully(&code_size, sizeof(code_size))) { |
| 706 | ReportWriteFailure("method code size", method_idx, dex_file, out); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 707 | return 0; |
| 708 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 709 | size_code_size_ += sizeof(code_size); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 710 | offset += sizeof(code_size); |
| 711 | DCHECK_OFFSET(); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 712 | if (!out.WriteFully(&code[0], code_size)) { |
| 713 | ReportWriteFailure("method code", method_idx, dex_file, out); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 714 | return 0; |
| 715 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 716 | size_code_ += code_size; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 717 | offset += code_size; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 718 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 719 | DCHECK_OFFSET(); |
| 720 | #endif |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 721 | |
| 722 | const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable(); |
| 723 | size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]); |
| 724 | |
| 725 | // Deduplicate mapping tables |
| 726 | SafeMap<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = |
| 727 | mapping_table_offsets_.find(&mapping_table); |
| 728 | if (mapping_iter != mapping_table_offsets_.end() && |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 729 | offset != method_offsets.mapping_table_offset_) { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 730 | DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0) |
| 731 | || mapping_iter->second == method_offsets.mapping_table_offset_) |
| 732 | << PrettyMethod(method_idx, dex_file); |
| 733 | } else { |
| 734 | DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0) |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 735 | || offset == method_offsets.mapping_table_offset_) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 736 | << PrettyMethod(method_idx, dex_file); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 737 | if (!out.WriteFully(&mapping_table[0], mapping_table_size)) { |
| 738 | ReportWriteFailure("mapping table", method_idx, dex_file, out); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 739 | return 0; |
| 740 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 741 | size_mapping_table_ += mapping_table_size; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 742 | offset += mapping_table_size; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 743 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 744 | DCHECK_OFFSET(); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 745 | |
| 746 | const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable(); |
| 747 | size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]); |
| 748 | |
| 749 | // Deduplicate vmap tables |
| 750 | SafeMap<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = |
| 751 | vmap_table_offsets_.find(&vmap_table); |
| 752 | if (vmap_iter != vmap_table_offsets_.end() && |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 753 | offset != method_offsets.vmap_table_offset_) { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 754 | DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0) |
| 755 | || vmap_iter->second == method_offsets.vmap_table_offset_) |
| 756 | << PrettyMethod(method_idx, dex_file); |
| 757 | } else { |
| 758 | DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0) |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 759 | || offset == method_offsets.vmap_table_offset_) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 760 | << PrettyMethod(method_idx, dex_file); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 761 | if (!out.WriteFully(&vmap_table[0], vmap_table_size)) { |
| 762 | ReportWriteFailure("vmap table", method_idx, dex_file, out); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 763 | return 0; |
| 764 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 765 | size_vmap_table_ += vmap_table_size; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 766 | offset += vmap_table_size; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 767 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 768 | DCHECK_OFFSET(); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 769 | |
Ian Rogers | fbdc0fa | 2013-04-18 16:48:17 -0700 | [diff] [blame] | 770 | const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap(); |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 771 | size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]); |
| 772 | |
| 773 | // Deduplicate GC maps |
| 774 | SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = |
| 775 | gc_map_offsets_.find(&gc_map); |
| 776 | if (gc_map_iter != gc_map_offsets_.end() && |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 777 | offset != method_offsets.gc_map_offset_) { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 778 | DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0) |
| 779 | || gc_map_iter->second == method_offsets.gc_map_offset_) |
| 780 | << PrettyMethod(method_idx, dex_file); |
| 781 | } else { |
| 782 | DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0) |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 783 | || offset == method_offsets.gc_map_offset_) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 784 | << PrettyMethod(method_idx, dex_file); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 785 | if (!out.WriteFully(&gc_map[0], gc_map_size)) { |
| 786 | ReportWriteFailure("GC map", 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_gc_map_ += gc_map_size; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 790 | offset += gc_map_size; |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 791 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 792 | DCHECK_OFFSET(); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 793 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 794 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 795 | return offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 798 | OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) { |
| 799 | offset_ = offset; |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 800 | const std::string& location(dex_file.GetLocation()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 801 | dex_file_location_size_ = location.size(); |
| 802 | dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data()); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 803 | dex_file_location_checksum_ = dex_file.GetLocationChecksum(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 804 | dex_file_offset_ = 0; |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 805 | methods_offsets_.resize(dex_file.NumClassDefs()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | size_t OatWriter::OatDexFile::SizeOf() const { |
| 809 | return sizeof(dex_file_location_size_) |
| 810 | + dex_file_location_size_ |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 811 | + sizeof(dex_file_location_checksum_) |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 812 | + sizeof(dex_file_offset_) |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 813 | + (sizeof(methods_offsets_[0]) * methods_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const { |
| 817 | oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_)); |
| 818 | oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 819 | oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_)); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 820 | oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_)); |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 821 | oat_header.UpdateChecksum(&methods_offsets_[0], |
| 822 | sizeof(methods_offsets_[0]) * methods_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 825 | bool OatWriter::OatDexFile::Write(OatWriter* oat_writer, OutputStream& out) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 826 | DCHECK_OFFSET_(); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 827 | if (!out.WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) { |
| 828 | PLOG(ERROR) << "Failed to write dex file location length to " << out.GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 829 | return false; |
| 830 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 831 | 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] | 832 | if (!out.WriteFully(dex_file_location_data_, dex_file_location_size_)) { |
| 833 | PLOG(ERROR) << "Failed to write dex file location data to " << out.GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 834 | return false; |
| 835 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 836 | oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_; |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 837 | if (!out.WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) { |
| 838 | PLOG(ERROR) << "Failed to write dex file location checksum to " << out.GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 839 | return false; |
| 840 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 841 | 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] | 842 | if (!out.WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) { |
| 843 | PLOG(ERROR) << "Failed to write dex file offset to " << out.GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 844 | return false; |
| 845 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 846 | oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 847 | if (!out.WriteFully(&methods_offsets_[0], |
| 848 | sizeof(methods_offsets_[0]) * methods_offsets_.size())) { |
| 849 | PLOG(ERROR) << "Failed to write methods offsets to " << out.GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 850 | return false; |
| 851 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 852 | oat_writer->size_oat_dex_file_methods_offsets_ += |
| 853 | sizeof(methods_offsets_[0]) * methods_offsets_.size(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 854 | return true; |
| 855 | } |
| 856 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 857 | OatWriter::OatClass::OatClass(size_t offset, mirror::Class::Status status, uint32_t methods_count) { |
| 858 | offset_ = offset; |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 859 | status_ = status; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 860 | method_offsets_.resize(methods_count); |
| 861 | } |
| 862 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 863 | size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader( |
| 864 | size_t class_def_method_index_) const { |
| 865 | return offset_ + GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_); |
| 866 | } |
| 867 | |
| 868 | size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass( |
| 869 | size_t class_def_method_index_) const { |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 870 | return sizeof(status_) |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 871 | + (sizeof(method_offsets_[0]) * class_def_method_index_); |
| 872 | } |
| 873 | |
| 874 | size_t OatWriter::OatClass::SizeOf() const { |
| 875 | return GetOatMethodOffsetsOffsetFromOatClass(method_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 876 | } |
| 877 | |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 878 | void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const { |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 879 | oat_header.UpdateChecksum(&status_, sizeof(status_)); |
| 880 | oat_header.UpdateChecksum(&method_offsets_[0], |
| 881 | sizeof(method_offsets_[0]) * method_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 884 | bool OatWriter::OatClass::Write(OatWriter* oat_writer, OutputStream& out) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 885 | DCHECK_OFFSET_(); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 886 | if (!out.WriteFully(&status_, sizeof(status_))) { |
| 887 | PLOG(ERROR) << "Failed to write class status to " << out.GetLocation(); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 888 | return false; |
| 889 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 890 | oat_writer->size_oat_class_status_ += sizeof(status_); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 891 | DCHECK_EQ(static_cast<off_t>(GetOatMethodOffsetsOffsetFromOatHeader(0)), |
| 892 | out.Seek(0, kSeekCurrent)); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 893 | if (!out.WriteFully(&method_offsets_[0], |
| 894 | sizeof(method_offsets_[0]) * method_offsets_.size())) { |
| 895 | PLOG(ERROR) << "Failed to write method offsets to " << out.GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 896 | return false; |
| 897 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 898 | oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size(); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 899 | DCHECK_EQ(static_cast<off_t>(GetOatMethodOffsetsOffsetFromOatHeader(method_offsets_.size())), |
| 900 | out.Seek(0, kSeekCurrent)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 901 | return true; |
| 902 | } |
| 903 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 904 | } // namespace art |