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