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