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