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