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