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