Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include "oat_writer.h" |
| 4 | |
| 5 | #include "class_linker.h" |
| 6 | #include "class_loader.h" |
| 7 | #include "file.h" |
| 8 | #include "os.h" |
| 9 | #include "stl_util.h" |
| 10 | |
| 11 | namespace art { |
| 12 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 13 | bool OatWriter::Create(const std::string& filename, |
| 14 | const ClassLoader* class_loader, |
| 15 | const Compiler& compiler) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 16 | const std::vector<const DexFile*>& dex_files = ClassLoader::GetCompileTimeClassPath(class_loader); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 17 | OatWriter oat_writer(dex_files, class_loader, compiler); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 18 | return oat_writer.Write(filename); |
| 19 | } |
| 20 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 21 | OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files, |
| 22 | const ClassLoader* class_loader, |
| 23 | const Compiler& compiler) { |
| 24 | compiler_ = &compiler; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 25 | class_loader_ = class_loader; |
| 26 | dex_files_ = &dex_files; |
| 27 | |
| 28 | size_t offset = InitOatHeader(); |
| 29 | offset = InitOatDexFiles(offset); |
| 30 | offset = InitOatClasses(offset); |
| 31 | offset = InitOatMethods(offset); |
| 32 | offset = InitOatCode(offset); |
| 33 | offset = InitOatCodeDexFiles(offset); |
| 34 | |
| 35 | CHECK_EQ(dex_files_->size(), oat_dex_files_.size()); |
| 36 | CHECK_EQ(dex_files_->size(), oat_classes_.size()); |
| 37 | } |
| 38 | |
| 39 | size_t OatWriter::InitOatHeader() { |
| 40 | // create the OatHeader |
| 41 | oat_header_ = new OatHeader(dex_files_); |
| 42 | size_t offset = sizeof(*oat_header_); |
| 43 | return offset; |
| 44 | } |
| 45 | |
| 46 | size_t OatWriter::InitOatDexFiles(size_t offset) { |
| 47 | // create the OatDexFiles |
| 48 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 49 | const DexFile* dex_file = (*dex_files_)[i]; |
| 50 | CHECK(dex_file != NULL); |
| 51 | OatDexFile* oat_dex_file = new OatDexFile(*dex_file); |
| 52 | oat_dex_files_.push_back(oat_dex_file); |
| 53 | offset += oat_dex_file->SizeOf(); |
| 54 | } |
| 55 | return offset; |
| 56 | } |
| 57 | |
| 58 | size_t OatWriter::InitOatClasses(size_t offset) { |
| 59 | // create the OatClasses |
| 60 | // calculate the offsets within OatDexFiles to OatClasses |
| 61 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 62 | // set offset in OatDexFile to OatClasses |
| 63 | oat_dex_files_[i]->classes_offset_ = offset; |
| 64 | oat_dex_files_[i]->UpdateChecksum(*oat_header_); |
| 65 | |
| 66 | const DexFile* dex_file = (*dex_files_)[i]; |
| 67 | OatClasses* oat_classes = new OatClasses(*dex_file); |
| 68 | oat_classes_.push_back(oat_classes); |
| 69 | offset += oat_classes->SizeOf(); |
| 70 | } |
| 71 | return offset; |
| 72 | } |
| 73 | |
| 74 | size_t OatWriter::InitOatMethods(size_t offset) { |
| 75 | // create the OatMethods |
| 76 | // calculate the offsets within OatClasses to OatMethods |
| 77 | size_t class_index = 0; |
| 78 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 79 | const DexFile* dex_file = (*dex_files_)[i]; |
| 80 | for (size_t class_def_index = 0; |
| 81 | class_def_index < dex_file->NumClassDefs(); |
| 82 | class_def_index++, class_index++) { |
| 83 | oat_classes_[i]->methods_offsets_[class_def_index] = offset; |
| 84 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
| 85 | const byte* class_data = dex_file->GetClassData(class_def); |
| 86 | DexFile::ClassDataHeader header = dex_file->ReadClassDataHeader(&class_data); |
| 87 | size_t num_direct_methods = header.direct_methods_size_; |
| 88 | size_t num_virtual_methods = header.virtual_methods_size_; |
| 89 | uint32_t num_methods = num_direct_methods + num_virtual_methods; |
| 90 | OatMethods* oat_methods = new OatMethods(num_methods); |
| 91 | oat_methods_.push_back(oat_methods); |
| 92 | offset += oat_methods->SizeOf(); |
| 93 | } |
| 94 | oat_classes_[i]->UpdateChecksum(*oat_header_); |
| 95 | } |
| 96 | return offset; |
| 97 | } |
| 98 | |
| 99 | size_t OatWriter::InitOatCode(size_t offset) { |
| 100 | // calculate the offsets within OatHeader to executable code |
| 101 | size_t old_offset = offset; |
| 102 | // required to be on a new page boundary |
| 103 | offset = RoundUp(offset, kPageSize); |
| 104 | oat_header_->SetExecutableOffset(offset); |
| 105 | executable_offset_padding_length_ = offset - old_offset; |
| 106 | return offset; |
| 107 | } |
| 108 | |
| 109 | size_t OatWriter::InitOatCodeDexFiles(size_t offset) { |
| 110 | // calculate the offsets within OatMethods |
| 111 | size_t oat_class_index = 0; |
| 112 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 113 | const DexFile* dex_file = (*dex_files_)[i]; |
| 114 | CHECK(dex_file != NULL); |
| 115 | offset = InitOatCodeDexFile(offset, oat_class_index, *dex_file); |
| 116 | } |
| 117 | return offset; |
| 118 | } |
| 119 | |
| 120 | size_t OatWriter::InitOatCodeDexFile(size_t offset, |
| 121 | size_t& oat_class_index, |
| 122 | const DexFile& dex_file) { |
| 123 | for (size_t class_def_index = 0; |
| 124 | class_def_index < dex_file.NumClassDefs(); |
| 125 | class_def_index++, oat_class_index++) { |
| 126 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
| 127 | offset = InitOatCodeClassDef(offset, oat_class_index, dex_file, class_def); |
| 128 | oat_methods_[oat_class_index]->UpdateChecksum(*oat_header_); |
| 129 | } |
| 130 | return offset; |
| 131 | } |
| 132 | |
| 133 | size_t OatWriter::InitOatCodeClassDef(size_t offset, |
| 134 | size_t oat_class_index, |
| 135 | const DexFile& dex_file, |
| 136 | const DexFile::ClassDef& class_def) { |
| 137 | const byte* class_data = dex_file.GetClassData(class_def); |
| 138 | DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data); |
| 139 | size_t num_virtual_methods = header.virtual_methods_size_; |
| 140 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
| 141 | |
| 142 | // TODO: remove code ByteArrays from Class/Method (and therefore ClassLoader) |
| 143 | // TODO: don't write code for shared stubs |
| 144 | Class* klass = Runtime::Current()->GetClassLinker()->FindClass(descriptor, class_loader_); |
Ian Rogers | 387b699 | 2011-10-31 17:52:37 -0700 | [diff] [blame] | 145 | if (klass == NULL) { |
| 146 | LOG(WARNING) << "Didn't find class '" << descriptor << "' in dex file " << dex_file.GetLocation(); |
| 147 | Thread* thread = Thread::Current(); |
| 148 | DCHECK(thread->IsExceptionPending()); |
| 149 | thread->ClearException(); |
| 150 | return offset; |
| 151 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 152 | CHECK_EQ(klass->GetClassLoader(), class_loader_); |
| 153 | CHECK_EQ(oat_methods_[oat_class_index]->method_offsets_.size(), |
| 154 | klass->NumDirectMethods() + num_virtual_methods); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 155 | size_t class_def_method_index = 0; |
| 156 | for (size_t i = 0; i < klass->NumDirectMethods(); i++, class_def_method_index++) { |
| 157 | Method* method = klass->GetDirectMethod(i); |
| 158 | CHECK(method != NULL) << descriptor << " direct " << i; |
| 159 | offset = InitOatCodeMethod(offset, oat_class_index, class_def_method_index, method); |
| 160 | } |
| 161 | // note that num_virtual_methods != klass->NumVirtualMethods() because of miranda methods |
| 162 | for (size_t i = 0; i < num_virtual_methods; i++, class_def_method_index++) { |
| 163 | Method* method = klass->GetVirtualMethod(i); |
| 164 | CHECK(method != NULL) << descriptor << " virtual " << i; |
| 165 | offset = InitOatCodeMethod(offset, oat_class_index, class_def_method_index, method); |
| 166 | } |
| 167 | return offset; |
| 168 | } |
| 169 | |
| 170 | size_t OatWriter::InitOatCodeMethod(size_t offset, |
| 171 | size_t oat_class_index, |
| 172 | size_t class_def_method_index, |
| 173 | Method* method) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 174 | // derived from CompiledMethod if available |
| 175 | uint32_t code_offset = 0; |
| 176 | uint32_t frame_size_in_bytes = kStackAlignment; |
| 177 | uint32_t return_pc_offset_in_bytes = 0; |
| 178 | uint32_t core_spill_mask = 0; |
| 179 | uint32_t fp_spill_mask = 0; |
| 180 | uint32_t mapping_table_offset = 0; |
| 181 | uint32_t vmap_table_offset = 0; |
| 182 | // derived from CompiledInvokeStub if available |
| 183 | uint32_t invoke_stub_offset = 0; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 184 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 185 | const CompiledMethod* compiled_method = compiler_->GetCompiledMethod(method); |
| 186 | if (compiled_method != NULL) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 187 | offset = compiled_method->AlignCode(offset); |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 188 | DCHECK_ALIGNED(offset, kArmAlignment); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 189 | const std::vector<uint8_t>& code = compiled_method->GetCode(); |
| 190 | size_t code_size = code.size() * sizeof(code[0]); |
| 191 | uint32_t thumb_offset = compiled_method->CodeDelta(); |
| 192 | code_offset = (code_size == 0) ? 0 : offset + thumb_offset; |
jeffhao | 55d7821 | 2011-11-02 11:41:50 -0700 | [diff] [blame] | 193 | |
| 194 | // Deduplicate code arrays |
jeffhao | f479dcc | 2011-11-02 15:54:15 -0700 | [diff] [blame^] | 195 | 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] | 196 | if (code_iter != code_offsets_.end()) { |
| 197 | code_offset = code_iter->second; |
| 198 | } else { |
jeffhao | f479dcc | 2011-11-02 15:54:15 -0700 | [diff] [blame^] | 199 | 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] | 200 | offset += code_size; |
| 201 | oat_header_->UpdateChecksum(&code[0], code_size); |
| 202 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 203 | |
| 204 | frame_size_in_bytes = compiled_method->GetFrameSizeInBytes(); |
| 205 | return_pc_offset_in_bytes = compiled_method->GetReturnPcOffsetInBytes(); |
| 206 | core_spill_mask = compiled_method->GetCoreSpillMask(); |
| 207 | fp_spill_mask = compiled_method->GetFpSpillMask(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 208 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 209 | |
| 210 | offset += sizeof(frame_size_in_bytes); |
| 211 | oat_header_->UpdateChecksum(&frame_size_in_bytes, sizeof(frame_size_in_bytes)); |
| 212 | |
| 213 | offset += sizeof(return_pc_offset_in_bytes); |
| 214 | oat_header_->UpdateChecksum(&return_pc_offset_in_bytes, sizeof(return_pc_offset_in_bytes)); |
| 215 | |
| 216 | offset += sizeof(core_spill_mask); |
| 217 | oat_header_->UpdateChecksum(&core_spill_mask, sizeof(core_spill_mask)); |
| 218 | |
| 219 | offset += sizeof(fp_spill_mask); |
| 220 | oat_header_->UpdateChecksum(&fp_spill_mask, sizeof(fp_spill_mask)); |
| 221 | |
| 222 | if (compiled_method != NULL) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 223 | const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable(); |
| 224 | size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]); |
| 225 | mapping_table_offset = (mapping_table_size == 0) ? 0 : offset; |
jeffhao | 55d7821 | 2011-11-02 11:41:50 -0700 | [diff] [blame] | 226 | |
| 227 | // Deduplicate mapping tables |
jeffhao | f479dcc | 2011-11-02 15:54:15 -0700 | [diff] [blame^] | 228 | 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] | 229 | if (mapping_iter != mapping_table_offsets_.end()) { |
| 230 | mapping_table_offset = mapping_iter->second; |
| 231 | } else { |
jeffhao | f479dcc | 2011-11-02 15:54:15 -0700 | [diff] [blame^] | 232 | 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] | 233 | offset += mapping_table_size; |
| 234 | oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size); |
| 235 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 236 | |
| 237 | const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable(); |
| 238 | size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]); |
| 239 | vmap_table_offset = (vmap_table_size == 0) ? 0 : offset; |
jeffhao | 55d7821 | 2011-11-02 11:41:50 -0700 | [diff] [blame] | 240 | |
| 241 | // Deduplicate vmap tables |
jeffhao | f479dcc | 2011-11-02 15:54:15 -0700 | [diff] [blame^] | 242 | 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] | 243 | if (vmap_iter != vmap_table_offsets_.end()) { |
| 244 | vmap_table_offset = vmap_iter->second; |
| 245 | } else { |
jeffhao | f479dcc | 2011-11-02 15:54:15 -0700 | [diff] [blame^] | 246 | 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] | 247 | offset += vmap_table_size; |
| 248 | oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size); |
| 249 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | const CompiledInvokeStub* compiled_invoke_stub = compiler_->GetCompiledInvokeStub(method); |
| 253 | if (compiled_invoke_stub != NULL) { |
| 254 | offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet()); |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 255 | DCHECK_ALIGNED(offset, kArmAlignment); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 256 | const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode(); |
| 257 | size_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]); |
| 258 | invoke_stub_offset = (invoke_stub_size == 0) ? 0 : offset; |
jeffhao | 55d7821 | 2011-11-02 11:41:50 -0700 | [diff] [blame] | 259 | |
| 260 | // Deduplicate invoke stubs |
jeffhao | f479dcc | 2011-11-02 15:54:15 -0700 | [diff] [blame^] | 261 | 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] | 262 | if (stub_iter != code_offsets_.end()) { |
| 263 | invoke_stub_offset = stub_iter->second; |
| 264 | } else { |
jeffhao | f479dcc | 2011-11-02 15:54:15 -0700 | [diff] [blame^] | 265 | 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] | 266 | offset += invoke_stub_size; |
| 267 | oat_header_->UpdateChecksum(&invoke_stub[0], invoke_stub_size); |
| 268 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | oat_methods_[oat_class_index]->method_offsets_[class_def_method_index] |
| 272 | = OatMethodOffsets(code_offset, |
| 273 | frame_size_in_bytes, |
| 274 | return_pc_offset_in_bytes, |
| 275 | core_spill_mask, |
| 276 | fp_spill_mask, |
| 277 | mapping_table_offset, |
| 278 | vmap_table_offset, |
| 279 | invoke_stub_offset); |
| 280 | |
| 281 | // Note that we leave the offset and values back in the Method where ImageWriter will find them |
| 282 | method->SetOatCodeOffset(code_offset); |
| 283 | method->SetFrameSizeInBytes(frame_size_in_bytes); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 284 | method->SetCoreSpillMask(core_spill_mask); |
| 285 | method->SetFpSpillMask(fp_spill_mask); |
| 286 | method->SetOatMappingTableOffset(mapping_table_offset); |
| 287 | method->SetOatVmapTableOffset(vmap_table_offset); |
| 288 | method->SetOatInvokeStubOffset(invoke_stub_offset); |
| 289 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 290 | return offset; |
| 291 | } |
| 292 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 293 | #define DCHECK_CODE_OFFSET() \ |
| 294 | DCHECK_EQ(static_cast<off_t>(code_offset), lseek(file->Fd(), 0, SEEK_CUR)) |
| 295 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 296 | bool OatWriter::Write(const std::string& filename) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 297 | UniquePtr<File> file(OS::OpenFile(filename.c_str(), true)); |
| 298 | if (file.get() == NULL) { |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | if (!file->WriteFully(oat_header_, sizeof(*oat_header_))) { |
| 303 | PLOG(ERROR) << "Failed to write oat header to " << filename; |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | if (!WriteTables(file.get())) { |
| 308 | LOG(ERROR) << "Failed to write oat tables to " << filename; |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | size_t code_offset = WriteCode(file.get()); |
| 313 | if (code_offset == 0) { |
| 314 | LOG(ERROR) << "Failed to write oat code to " << filename; |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | code_offset = WriteCodeDexFiles(file.get(), code_offset); |
| 319 | if (code_offset == 0) { |
| 320 | LOG(ERROR) << "Failed to write oat code for dex files to " << filename; |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | return true; |
| 325 | } |
| 326 | |
| 327 | bool OatWriter::WriteTables(File* file) { |
| 328 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
| 329 | if (!oat_dex_files_[i]->Write(file)) { |
| 330 | PLOG(ERROR) << "Failed to write oat dex information"; |
| 331 | return false; |
| 332 | } |
| 333 | } |
| 334 | for (size_t i = 0; i != oat_classes_.size(); ++i) { |
| 335 | if (!oat_classes_[i]->Write(file)) { |
| 336 | PLOG(ERROR) << "Failed to write oat classes information"; |
| 337 | return false; |
| 338 | } |
| 339 | } |
| 340 | for (size_t i = 0; i != oat_methods_.size(); ++i) { |
| 341 | if (!oat_methods_[i]->Write(file)) { |
| 342 | PLOG(ERROR) << "Failed to write oat methods information"; |
| 343 | return false; |
| 344 | } |
| 345 | } |
| 346 | return true; |
| 347 | } |
| 348 | |
| 349 | size_t OatWriter::WriteCode(File* file) { |
| 350 | uint32_t code_offset = oat_header_->GetExecutableOffset(); |
| 351 | off_t new_offset = lseek(file->Fd(), executable_offset_padding_length_, SEEK_CUR); |
| 352 | if (static_cast<uint32_t>(new_offset) != code_offset) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 353 | PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset |
| 354 | << " Expected: " << code_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 355 | return 0; |
| 356 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 357 | DCHECK_CODE_OFFSET(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 358 | return code_offset; |
| 359 | } |
| 360 | |
| 361 | size_t OatWriter::WriteCodeDexFiles(File* file, size_t code_offset) { |
| 362 | for (size_t i = 0; i != oat_classes_.size(); ++i) { |
| 363 | const DexFile* dex_file = (*dex_files_)[i]; |
| 364 | CHECK(dex_file != NULL); |
| 365 | code_offset = WriteCodeDexFile(file, code_offset, *dex_file); |
| 366 | if (code_offset == 0) { |
| 367 | return 0; |
| 368 | } |
| 369 | } |
| 370 | return code_offset; |
| 371 | } |
| 372 | |
| 373 | size_t OatWriter::WriteCodeDexFile(File* file, |
| 374 | size_t code_offset, |
| 375 | const DexFile& dex_file) { |
| 376 | for (size_t class_def_index = 0; |
| 377 | class_def_index < dex_file.NumClassDefs(); |
| 378 | class_def_index++) { |
| 379 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
| 380 | code_offset = WriteCodeClassDef(file, code_offset, dex_file, class_def); |
| 381 | if (code_offset == 0) { |
| 382 | return 0; |
| 383 | } |
| 384 | } |
| 385 | return code_offset; |
| 386 | } |
| 387 | |
| 388 | size_t OatWriter::WriteCodeClassDef(File* file, |
| 389 | size_t code_offset, |
| 390 | const DexFile& dex_file, |
| 391 | const DexFile::ClassDef& class_def) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 392 | const byte* class_data = dex_file.GetClassData(class_def); |
| 393 | DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data); |
| 394 | size_t num_virtual_methods = header.virtual_methods_size_; |
| 395 | const char* descriptor = dex_file.GetClassDescriptor(class_def); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 396 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 397 | Class* klass = class_linker->FindClass(descriptor, class_loader_); |
Ian Rogers | 387b699 | 2011-10-31 17:52:37 -0700 | [diff] [blame] | 398 | if (klass == NULL) { |
| 399 | LOG(WARNING) << "Didn't find class '" << descriptor << "' in dex file " << dex_file.GetLocation(); |
| 400 | Thread* thread = Thread::Current(); |
| 401 | DCHECK(thread->IsExceptionPending()); |
| 402 | thread->ClearException(); |
| 403 | return code_offset; |
| 404 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 405 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 406 | // Note that we clear the code array here, image_writer will use GetCodeOffset to find it |
| 407 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
| 408 | Method* method = klass->GetDirectMethod(i); |
| 409 | code_offset = WriteCodeMethod(file, code_offset, method); |
| 410 | if (code_offset == 0) { |
| 411 | return 0; |
| 412 | } |
| 413 | } |
| 414 | // note that num_virtual_methods != klass->NumVirtualMethods() because of miranda methods |
| 415 | for (size_t i = 0; i < num_virtual_methods; i++) { |
| 416 | Method* method = klass->GetVirtualMethod(i); |
| 417 | code_offset = WriteCodeMethod(file, code_offset, method); |
| 418 | if (code_offset == 0) { |
| 419 | return 0; |
| 420 | } |
| 421 | } |
| 422 | for (size_t i = num_virtual_methods; i < klass->NumVirtualMethods(); i++) { |
| 423 | Method* method = klass->GetVirtualMethod(i); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 424 | CHECK(compiler_->GetCompiledMethod(method) == NULL) << PrettyMethod(method); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 425 | } |
| 426 | return code_offset; |
| 427 | } |
| 428 | |
Elliott Hughes | f09afe8 | 2011-10-16 14:24:21 -0700 | [diff] [blame] | 429 | size_t OatWriter::WriteCodeMethod(File* file, size_t code_offset, Method* method) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 430 | const CompiledMethod* compiled_method = compiler_->GetCompiledMethod(method); |
| 431 | if (compiled_method != NULL) { |
| 432 | uint32_t aligned_code_offset = compiled_method->AlignCode(code_offset); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 433 | uint32_t aligned_code_delta = aligned_code_offset - code_offset; |
| 434 | if (aligned_code_delta != 0) { |
| 435 | off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR); |
| 436 | if (static_cast<uint32_t>(new_offset) != aligned_code_offset) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 437 | PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset |
| 438 | << " Expected: " << aligned_code_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 439 | return false; |
| 440 | } |
| 441 | code_offset += aligned_code_delta; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 442 | DCHECK_CODE_OFFSET(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 443 | } |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 444 | DCHECK_ALIGNED(code_offset, kArmAlignment); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 445 | const std::vector<uint8_t>& code = compiled_method->GetCode(); |
| 446 | size_t code_size = code.size() * sizeof(code[0]); |
jeffhao | 55d7821 | 2011-11-02 11:41:50 -0700 | [diff] [blame] | 447 | |
| 448 | // Deduplicate code arrays |
| 449 | size_t offset = code_offset + compiled_method->CodeDelta(); |
jeffhao | f479dcc | 2011-11-02 15:54:15 -0700 | [diff] [blame^] | 450 | 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] | 451 | if (code_iter != code_offsets_.end() && offset != method->GetOatCodeOffset()) { |
| 452 | DCHECK((code_size == 0 && method->GetOatCodeOffset() == 0) |
| 453 | || code_iter->second == method->GetOatCodeOffset()) << PrettyMethod(method); |
| 454 | } else { |
| 455 | DCHECK((code_size == 0 && method->GetOatCodeOffset() == 0) |
| 456 | || offset == method->GetOatCodeOffset()) << PrettyMethod(method); |
| 457 | if (!file->WriteFully(&code[0], code_size)) { |
| 458 | PLOG(ERROR) << "Failed to write method code for " << PrettyMethod(method); |
| 459 | return false; |
| 460 | } |
| 461 | code_offset += code_size; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 462 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 463 | DCHECK_CODE_OFFSET(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 464 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 465 | |
| 466 | uint32_t frame_size_in_bytes = method->GetFrameSizeInBytes(); |
| 467 | uint32_t return_pc_offset_in_bytes = method->GetReturnPcOffsetInBytes(); |
| 468 | uint32_t core_spill_mask = method->GetCoreSpillMask(); |
| 469 | uint32_t fp_spill_mask = method->GetFpSpillMask(); |
| 470 | if (!file->WriteFully(&frame_size_in_bytes, sizeof(frame_size_in_bytes))) { |
| 471 | PLOG(ERROR) << "Failed to write method frame size for " << PrettyMethod(method); |
| 472 | return false; |
| 473 | } |
| 474 | code_offset += sizeof(frame_size_in_bytes); |
| 475 | if (!file->WriteFully(&return_pc_offset_in_bytes, sizeof(return_pc_offset_in_bytes))) { |
| 476 | PLOG(ERROR) << "Failed to write method return pc offset for " << PrettyMethod(method); |
| 477 | return false; |
| 478 | } |
| 479 | code_offset += sizeof(return_pc_offset_in_bytes); |
| 480 | if (!file->WriteFully(&core_spill_mask, sizeof(core_spill_mask))) { |
| 481 | PLOG(ERROR) << "Failed to write method core spill mask for " << PrettyMethod(method); |
| 482 | return false; |
| 483 | } |
| 484 | code_offset += sizeof(core_spill_mask); |
| 485 | if (!file->WriteFully(&fp_spill_mask, sizeof(fp_spill_mask))) { |
| 486 | PLOG(ERROR) << "Failed to write method fp spill mask for " << PrettyMethod(method); |
| 487 | return false; |
| 488 | } |
| 489 | code_offset += sizeof(fp_spill_mask); |
| 490 | |
| 491 | if (compiled_method != NULL) { |
| 492 | const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable(); |
| 493 | size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]); |
jeffhao | 55d7821 | 2011-11-02 11:41:50 -0700 | [diff] [blame] | 494 | |
| 495 | // Deduplicate mapping tables |
jeffhao | f479dcc | 2011-11-02 15:54:15 -0700 | [diff] [blame^] | 496 | 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] | 497 | if (mapping_iter != mapping_table_offsets_.end() && code_offset != method->GetOatMappingTableOffset()) { |
| 498 | DCHECK((mapping_table_size == 0 && method->GetOatMappingTableOffset() == 0) |
| 499 | || mapping_iter->second == method->GetOatMappingTableOffset()) << PrettyMethod(method); |
| 500 | } else { |
| 501 | DCHECK((mapping_table_size == 0 && method->GetOatMappingTableOffset() == 0) |
| 502 | || code_offset == method->GetOatMappingTableOffset()) << PrettyMethod(method); |
| 503 | if (!file->WriteFully(&mapping_table[0], mapping_table_size)) { |
| 504 | PLOG(ERROR) << "Failed to write mapping table for " << PrettyMethod(method); |
| 505 | return false; |
| 506 | } |
| 507 | code_offset += mapping_table_size; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 508 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 509 | DCHECK_CODE_OFFSET(); |
| 510 | |
| 511 | const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable(); |
| 512 | size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]); |
jeffhao | 55d7821 | 2011-11-02 11:41:50 -0700 | [diff] [blame] | 513 | |
| 514 | // Deduplicate vmap tables |
jeffhao | f479dcc | 2011-11-02 15:54:15 -0700 | [diff] [blame^] | 515 | 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] | 516 | if (vmap_iter != vmap_table_offsets_.end() && code_offset != method->GetOatVmapTableOffset()) { |
| 517 | DCHECK((vmap_table_size == 0 && method->GetOatVmapTableOffset() == 0) |
| 518 | || vmap_iter->second == method->GetOatVmapTableOffset()) << PrettyMethod(method); |
| 519 | } else { |
| 520 | DCHECK((vmap_table_size == 0 && method->GetOatVmapTableOffset() == 0) |
| 521 | || code_offset == method->GetOatVmapTableOffset()) << PrettyMethod(method); |
| 522 | if (!file->WriteFully(&vmap_table[0], vmap_table_size)) { |
| 523 | PLOG(ERROR) << "Failed to write vmap table for " << PrettyMethod(method); |
| 524 | return false; |
| 525 | } |
| 526 | code_offset += vmap_table_size; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 527 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 528 | DCHECK_CODE_OFFSET(); |
| 529 | } |
| 530 | |
| 531 | const CompiledInvokeStub* compiled_invoke_stub = compiler_->GetCompiledInvokeStub(method); |
| 532 | if (compiled_invoke_stub != NULL) { |
| 533 | uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset, |
| 534 | compiler_->GetInstructionSet()); |
| 535 | uint32_t aligned_code_delta = aligned_code_offset - code_offset; |
| 536 | if (aligned_code_delta != 0) { |
| 537 | off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR); |
| 538 | if (static_cast<uint32_t>(new_offset) != aligned_code_offset) { |
| 539 | PLOG(ERROR) << "Failed to seek to align invoke stub code. Actual: " << new_offset |
| 540 | << " Expected: " << aligned_code_offset; |
| 541 | return false; |
| 542 | } |
| 543 | code_offset += aligned_code_delta; |
| 544 | DCHECK_CODE_OFFSET(); |
| 545 | } |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 546 | DCHECK_ALIGNED(code_offset, kArmAlignment); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 547 | const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode(); |
| 548 | size_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]); |
jeffhao | 55d7821 | 2011-11-02 11:41:50 -0700 | [diff] [blame] | 549 | |
| 550 | // Deduplicate invoke stubs |
jeffhao | f479dcc | 2011-11-02 15:54:15 -0700 | [diff] [blame^] | 551 | 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] | 552 | if (stub_iter != code_offsets_.end() && code_offset != method->GetOatInvokeStubOffset()) { |
| 553 | DCHECK((invoke_stub_size == 0 && method->GetOatInvokeStubOffset() == 0) |
| 554 | || stub_iter->second == method->GetOatInvokeStubOffset()) << PrettyMethod(method); |
| 555 | } else { |
| 556 | DCHECK((invoke_stub_size == 0 && method->GetOatInvokeStubOffset() == 0) |
| 557 | || code_offset == method->GetOatInvokeStubOffset()) << PrettyMethod(method); |
| 558 | if (!file->WriteFully(&invoke_stub[0], invoke_stub_size)) { |
| 559 | PLOG(ERROR) << "Failed to write invoke stub code for " << PrettyMethod(method); |
| 560 | return false; |
| 561 | } |
| 562 | code_offset += invoke_stub_size; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 563 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 564 | DCHECK_CODE_OFFSET(); |
| 565 | } |
| 566 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 567 | return code_offset; |
| 568 | } |
| 569 | |
| 570 | OatWriter::~OatWriter() { |
| 571 | delete oat_header_; |
| 572 | STLDeleteElements(&oat_dex_files_); |
| 573 | STLDeleteElements(&oat_classes_); |
| 574 | STLDeleteElements(&oat_methods_); |
| 575 | } |
| 576 | |
| 577 | OatWriter::OatDexFile::OatDexFile(const DexFile& dex_file) { |
| 578 | const std::string& location = dex_file.GetLocation(); |
| 579 | dex_file_location_size_ = location.size(); |
| 580 | dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data()); |
| 581 | dex_file_checksum_ = dex_file.GetHeader().checksum_; |
| 582 | } |
| 583 | |
| 584 | size_t OatWriter::OatDexFile::SizeOf() const { |
| 585 | return sizeof(dex_file_location_size_) |
| 586 | + dex_file_location_size_ |
| 587 | + sizeof(dex_file_checksum_) |
| 588 | + sizeof(classes_offset_); |
| 589 | } |
| 590 | |
| 591 | void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const { |
| 592 | oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_)); |
| 593 | oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_); |
| 594 | oat_header.UpdateChecksum(&dex_file_checksum_, sizeof(dex_file_checksum_)); |
| 595 | oat_header.UpdateChecksum(&classes_offset_, sizeof(classes_offset_)); |
| 596 | } |
| 597 | |
| 598 | bool OatWriter::OatDexFile::Write(File* file) const { |
| 599 | if (!file->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) { |
| 600 | PLOG(ERROR) << "Failed to write dex file location length"; |
| 601 | return false; |
| 602 | } |
| 603 | if (!file->WriteFully(dex_file_location_data_, dex_file_location_size_)) { |
| 604 | PLOG(ERROR) << "Failed to write dex file location data"; |
| 605 | return false; |
| 606 | } |
| 607 | if (!file->WriteFully(&dex_file_checksum_, sizeof(dex_file_checksum_))) { |
| 608 | PLOG(ERROR) << "Failed to write dex file checksum"; |
| 609 | return false; |
| 610 | } |
| 611 | if (!file->WriteFully(&classes_offset_, sizeof(classes_offset_))) { |
| 612 | PLOG(ERROR) << "Failed to write classes offset"; |
| 613 | return false; |
| 614 | } |
| 615 | return true; |
| 616 | } |
| 617 | |
| 618 | OatWriter::OatClasses::OatClasses(const DexFile& dex_file) { |
| 619 | methods_offsets_.resize(dex_file.NumClassDefs()); |
| 620 | } |
| 621 | |
| 622 | size_t OatWriter::OatClasses::SizeOf() const { |
| 623 | return (sizeof(methods_offsets_[0]) * methods_offsets_.size()); |
| 624 | } |
| 625 | |
| 626 | void OatWriter::OatClasses::UpdateChecksum(OatHeader& oat_header) const { |
| 627 | oat_header.UpdateChecksum(&methods_offsets_[0], SizeOf()); |
| 628 | } |
| 629 | |
| 630 | bool OatWriter::OatClasses::Write(File* file) const { |
| 631 | if (!file->WriteFully(&methods_offsets_[0], SizeOf())) { |
| 632 | PLOG(ERROR) << "Failed to methods offsets"; |
| 633 | return false; |
| 634 | } |
| 635 | return true; |
| 636 | } |
| 637 | |
| 638 | OatWriter::OatMethods::OatMethods(uint32_t methods_count) { |
| 639 | method_offsets_.resize(methods_count); |
| 640 | } |
| 641 | |
| 642 | size_t OatWriter::OatMethods::SizeOf() const { |
| 643 | return (sizeof(method_offsets_[0]) * method_offsets_.size()); |
| 644 | } |
| 645 | |
| 646 | void OatWriter::OatMethods::UpdateChecksum(OatHeader& oat_header) const { |
| 647 | oat_header.UpdateChecksum(&method_offsets_[0], SizeOf()); |
| 648 | } |
| 649 | |
| 650 | bool OatWriter::OatMethods::Write(File* file) const { |
| 651 | if (!file->WriteFully(&method_offsets_[0], SizeOf())) { |
| 652 | PLOG(ERROR) << "Failed to method offsets"; |
| 653 | return false; |
| 654 | } |
| 655 | return true; |
| 656 | } |
| 657 | |
| 658 | } // namespace art |