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