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