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