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