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