blob: 664dcfe09c3b76b2d5691298e97d2b8fd9cc3511 [file] [log] [blame]
Brian Carlstrome24fa612011-09-29 00:53:55 -07001// 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
11namespace art {
12
Elliott Hughes234da572011-11-03 22:13:06 -070013bool OatWriter::Create(File* file,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070014 const ClassLoader* class_loader,
15 const Compiler& compiler) {
Brian Carlstromaded5f72011-10-07 17:15:04 -070016 const std::vector<const DexFile*>& dex_files = ClassLoader::GetCompileTimeClassPath(class_loader);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070017 OatWriter oat_writer(dex_files, class_loader, compiler);
Elliott Hughes234da572011-11-03 22:13:06 -070018 return oat_writer.Write(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -070019}
20
Brian Carlstrom3320cf42011-10-04 14:58:28 -070021OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
22 const ClassLoader* class_loader,
23 const Compiler& compiler) {
24 compiler_ = &compiler;
Brian Carlstrome24fa612011-09-29 00:53:55 -070025 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
39size_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
46size_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
58size_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
74size_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
99size_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
109size_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
120size_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
133size_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 Rogers387b6992011-10-31 17:52:37 -0700145 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 Carlstrome24fa612011-09-29 00:53:55 -0700152 CHECK_EQ(klass->GetClassLoader(), class_loader_);
153 CHECK_EQ(oat_methods_[oat_class_index]->method_offsets_.size(),
154 klass->NumDirectMethods() + num_virtual_methods);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700155 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
170size_t OatWriter::InitOatCodeMethod(size_t offset,
171 size_t oat_class_index,
172 size_t class_def_method_index,
173 Method* method) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700174 // 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 Carlstrome24fa612011-09-29 00:53:55 -0700184
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700185 const CompiledMethod* compiled_method = compiler_->GetCompiledMethod(method);
186 if (compiled_method != NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700187 offset = compiled_method->AlignCode(offset);
Elliott Hughes06b37d92011-10-16 11:51:29 -0700188 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700189 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;
jeffhao55d78212011-11-02 11:41:50 -0700193
194 // Deduplicate code arrays
jeffhaof479dcc2011-11-02 15:54:15 -0700195 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
jeffhao55d78212011-11-02 11:41:50 -0700196 if (code_iter != code_offsets_.end()) {
197 code_offset = code_iter->second;
198 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700199 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&code, code_offset));
jeffhao55d78212011-11-02 11:41:50 -0700200 offset += code_size;
201 oat_header_->UpdateChecksum(&code[0], code_size);
202 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700203
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 Carlstrome24fa612011-09-29 00:53:55 -0700208 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700209
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 Carlstrom3320cf42011-10-04 14:58:28 -0700223 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;
jeffhao55d78212011-11-02 11:41:50 -0700226
227 // Deduplicate mapping tables
jeffhaof479dcc2011-11-02 15:54:15 -0700228 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
jeffhao55d78212011-11-02 11:41:50 -0700229 if (mapping_iter != mapping_table_offsets_.end()) {
230 mapping_table_offset = mapping_iter->second;
231 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700232 mapping_table_offsets_.insert(std::pair<const std::vector<uint32_t>*, uint32_t>(&mapping_table, mapping_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700233 offset += mapping_table_size;
234 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
235 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700236
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;
jeffhao55d78212011-11-02 11:41:50 -0700240
241 // Deduplicate vmap tables
jeffhaof479dcc2011-11-02 15:54:15 -0700242 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
jeffhao55d78212011-11-02 11:41:50 -0700243 if (vmap_iter != vmap_table_offsets_.end()) {
244 vmap_table_offset = vmap_iter->second;
245 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700246 vmap_table_offsets_.insert(std::pair<const std::vector<uint16_t>*, uint32_t>(&vmap_table, vmap_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700247 offset += vmap_table_size;
248 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
249 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700250 }
251
252 const CompiledInvokeStub* compiled_invoke_stub = compiler_->GetCompiledInvokeStub(method);
253 if (compiled_invoke_stub != NULL) {
254 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
Elliott Hughes06b37d92011-10-16 11:51:29 -0700255 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700256 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;
jeffhao55d78212011-11-02 11:41:50 -0700259
260 // Deduplicate invoke stubs
jeffhaof479dcc2011-11-02 15:54:15 -0700261 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&invoke_stub);
jeffhao55d78212011-11-02 11:41:50 -0700262 if (stub_iter != code_offsets_.end()) {
263 invoke_stub_offset = stub_iter->second;
264 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700265 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&invoke_stub, invoke_stub_offset));
jeffhao55d78212011-11-02 11:41:50 -0700266 offset += invoke_stub_size;
267 oat_header_->UpdateChecksum(&invoke_stub[0], invoke_stub_size);
268 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700269 }
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 Carlstrom3320cf42011-10-04 14:58:28 -0700284 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 Carlstrome24fa612011-09-29 00:53:55 -0700290 return offset;
291}
292
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700293#define DCHECK_CODE_OFFSET() \
294 DCHECK_EQ(static_cast<off_t>(code_offset), lseek(file->Fd(), 0, SEEK_CUR))
295
Elliott Hughes234da572011-11-03 22:13:06 -0700296bool OatWriter::Write(File* file) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700297 if (!file->WriteFully(oat_header_, sizeof(*oat_header_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700298 PLOG(ERROR) << "Failed to write oat header to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700299 return false;
300 }
301
Elliott Hughes234da572011-11-03 22:13:06 -0700302 if (!WriteTables(file)) {
303 LOG(ERROR) << "Failed to write oat tables to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700304 return false;
305 }
306
Elliott Hughes234da572011-11-03 22:13:06 -0700307 size_t code_offset = WriteCode(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700308 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700309 LOG(ERROR) << "Failed to write oat code to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700310 return false;
311 }
312
Elliott Hughes234da572011-11-03 22:13:06 -0700313 code_offset = WriteCodeDexFiles(file, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700314 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700315 LOG(ERROR) << "Failed to write oat code for dex files to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700316 return false;
317 }
318
319 return true;
320}
321
322bool OatWriter::WriteTables(File* file) {
323 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
324 if (!oat_dex_files_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700325 PLOG(ERROR) << "Failed to write oat dex information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700326 return false;
327 }
328 }
329 for (size_t i = 0; i != oat_classes_.size(); ++i) {
330 if (!oat_classes_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700331 PLOG(ERROR) << "Failed to write oat classes information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700332 return false;
333 }
334 }
335 for (size_t i = 0; i != oat_methods_.size(); ++i) {
336 if (!oat_methods_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700337 PLOG(ERROR) << "Failed to write oat methods information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700338 return false;
339 }
340 }
341 return true;
342}
343
344size_t OatWriter::WriteCode(File* file) {
345 uint32_t code_offset = oat_header_->GetExecutableOffset();
346 off_t new_offset = lseek(file->Fd(), executable_offset_padding_length_, SEEK_CUR);
347 if (static_cast<uint32_t>(new_offset) != code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700348 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700349 << " Expected: " << code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700350 return 0;
351 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700352 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700353 return code_offset;
354}
355
356size_t OatWriter::WriteCodeDexFiles(File* file, size_t code_offset) {
357 for (size_t i = 0; i != oat_classes_.size(); ++i) {
358 const DexFile* dex_file = (*dex_files_)[i];
359 CHECK(dex_file != NULL);
360 code_offset = WriteCodeDexFile(file, code_offset, *dex_file);
361 if (code_offset == 0) {
362 return 0;
363 }
364 }
365 return code_offset;
366}
367
Elliott Hughes234da572011-11-03 22:13:06 -0700368size_t OatWriter::WriteCodeDexFile(File* file, size_t code_offset, const DexFile& dex_file) {
369 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs(); class_def_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700370 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
371 code_offset = WriteCodeClassDef(file, code_offset, dex_file, class_def);
372 if (code_offset == 0) {
373 return 0;
374 }
375 }
376 return code_offset;
377}
378
Elliott Hughes234da572011-11-03 22:13:06 -0700379void OatWriter::ReportWriteFailure(const char* what, Method* m, File* f) {
380 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(m) << " to " << f->name();
381}
382
Brian Carlstrome24fa612011-09-29 00:53:55 -0700383size_t OatWriter::WriteCodeClassDef(File* file,
384 size_t code_offset,
385 const DexFile& dex_file,
386 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700387 const byte* class_data = dex_file.GetClassData(class_def);
388 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
389 size_t num_virtual_methods = header.virtual_methods_size_;
390 const char* descriptor = dex_file.GetClassDescriptor(class_def);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700391 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700392 Class* klass = class_linker->FindClass(descriptor, class_loader_);
Ian Rogers387b6992011-10-31 17:52:37 -0700393 if (klass == NULL) {
394 LOG(WARNING) << "Didn't find class '" << descriptor << "' in dex file " << dex_file.GetLocation();
395 Thread* thread = Thread::Current();
396 DCHECK(thread->IsExceptionPending());
397 thread->ClearException();
398 return code_offset;
399 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700400
Brian Carlstrome24fa612011-09-29 00:53:55 -0700401 // Note that we clear the code array here, image_writer will use GetCodeOffset to find it
402 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
403 Method* method = klass->GetDirectMethod(i);
404 code_offset = WriteCodeMethod(file, code_offset, method);
405 if (code_offset == 0) {
406 return 0;
407 }
408 }
409 // note that num_virtual_methods != klass->NumVirtualMethods() because of miranda methods
410 for (size_t i = 0; i < num_virtual_methods; i++) {
411 Method* method = klass->GetVirtualMethod(i);
412 code_offset = WriteCodeMethod(file, code_offset, method);
413 if (code_offset == 0) {
414 return 0;
415 }
416 }
417 for (size_t i = num_virtual_methods; i < klass->NumVirtualMethods(); i++) {
418 Method* method = klass->GetVirtualMethod(i);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700419 CHECK(compiler_->GetCompiledMethod(method) == NULL) << PrettyMethod(method);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700420 }
421 return code_offset;
422}
423
Elliott Hughesf09afe82011-10-16 14:24:21 -0700424size_t OatWriter::WriteCodeMethod(File* file, size_t code_offset, Method* method) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700425 const CompiledMethod* compiled_method = compiler_->GetCompiledMethod(method);
426 if (compiled_method != NULL) {
427 uint32_t aligned_code_offset = compiled_method->AlignCode(code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700428 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
429 if (aligned_code_delta != 0) {
430 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
431 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700432 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700433 << " Expected: " << aligned_code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700434 return false;
435 }
436 code_offset += aligned_code_delta;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700437 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700438 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700439 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700440 const std::vector<uint8_t>& code = compiled_method->GetCode();
441 size_t code_size = code.size() * sizeof(code[0]);
jeffhao55d78212011-11-02 11:41:50 -0700442
443 // Deduplicate code arrays
444 size_t offset = code_offset + compiled_method->CodeDelta();
jeffhaof479dcc2011-11-02 15:54:15 -0700445 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
jeffhao55d78212011-11-02 11:41:50 -0700446 if (code_iter != code_offsets_.end() && offset != method->GetOatCodeOffset()) {
447 DCHECK((code_size == 0 && method->GetOatCodeOffset() == 0)
448 || code_iter->second == method->GetOatCodeOffset()) << PrettyMethod(method);
449 } else {
450 DCHECK((code_size == 0 && method->GetOatCodeOffset() == 0)
451 || offset == method->GetOatCodeOffset()) << PrettyMethod(method);
452 if (!file->WriteFully(&code[0], code_size)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700453 ReportWriteFailure("method code", method, file);
jeffhao55d78212011-11-02 11:41:50 -0700454 return false;
455 }
456 code_offset += code_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700457 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700458 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700459 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700460
461 uint32_t frame_size_in_bytes = method->GetFrameSizeInBytes();
462 uint32_t return_pc_offset_in_bytes = method->GetReturnPcOffsetInBytes();
463 uint32_t core_spill_mask = method->GetCoreSpillMask();
464 uint32_t fp_spill_mask = method->GetFpSpillMask();
465 if (!file->WriteFully(&frame_size_in_bytes, sizeof(frame_size_in_bytes))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700466 ReportWriteFailure("method frame size", method, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700467 return false;
468 }
469 code_offset += sizeof(frame_size_in_bytes);
470 if (!file->WriteFully(&return_pc_offset_in_bytes, sizeof(return_pc_offset_in_bytes))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700471 ReportWriteFailure("method return pc", method, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700472 return false;
473 }
474 code_offset += sizeof(return_pc_offset_in_bytes);
475 if (!file->WriteFully(&core_spill_mask, sizeof(core_spill_mask))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700476 ReportWriteFailure("method core spill mask", method, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700477 return false;
478 }
479 code_offset += sizeof(core_spill_mask);
480 if (!file->WriteFully(&fp_spill_mask, sizeof(fp_spill_mask))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700481 ReportWriteFailure("method fp spill mask", method, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700482 return false;
483 }
484 code_offset += sizeof(fp_spill_mask);
485
486 if (compiled_method != NULL) {
487 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
488 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700489
490 // Deduplicate mapping tables
jeffhaof479dcc2011-11-02 15:54:15 -0700491 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
jeffhao55d78212011-11-02 11:41:50 -0700492 if (mapping_iter != mapping_table_offsets_.end() && code_offset != method->GetOatMappingTableOffset()) {
493 DCHECK((mapping_table_size == 0 && method->GetOatMappingTableOffset() == 0)
494 || mapping_iter->second == method->GetOatMappingTableOffset()) << PrettyMethod(method);
495 } else {
496 DCHECK((mapping_table_size == 0 && method->GetOatMappingTableOffset() == 0)
497 || code_offset == method->GetOatMappingTableOffset()) << PrettyMethod(method);
498 if (!file->WriteFully(&mapping_table[0], mapping_table_size)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700499 ReportWriteFailure("mapping table", method, file);
jeffhao55d78212011-11-02 11:41:50 -0700500 return false;
501 }
502 code_offset += mapping_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700503 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700504 DCHECK_CODE_OFFSET();
505
506 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
507 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700508
509 // Deduplicate vmap tables
jeffhaof479dcc2011-11-02 15:54:15 -0700510 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
jeffhao55d78212011-11-02 11:41:50 -0700511 if (vmap_iter != vmap_table_offsets_.end() && code_offset != method->GetOatVmapTableOffset()) {
512 DCHECK((vmap_table_size == 0 && method->GetOatVmapTableOffset() == 0)
513 || vmap_iter->second == method->GetOatVmapTableOffset()) << PrettyMethod(method);
514 } else {
515 DCHECK((vmap_table_size == 0 && method->GetOatVmapTableOffset() == 0)
516 || code_offset == method->GetOatVmapTableOffset()) << PrettyMethod(method);
517 if (!file->WriteFully(&vmap_table[0], vmap_table_size)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700518 ReportWriteFailure("vmap table", method, file);
jeffhao55d78212011-11-02 11:41:50 -0700519 return false;
520 }
521 code_offset += vmap_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700522 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700523 DCHECK_CODE_OFFSET();
524 }
525
526 const CompiledInvokeStub* compiled_invoke_stub = compiler_->GetCompiledInvokeStub(method);
527 if (compiled_invoke_stub != NULL) {
528 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
529 compiler_->GetInstructionSet());
530 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
531 if (aligned_code_delta != 0) {
532 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
533 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
534 PLOG(ERROR) << "Failed to seek to align invoke stub code. Actual: " << new_offset
535 << " Expected: " << aligned_code_offset;
536 return false;
537 }
538 code_offset += aligned_code_delta;
539 DCHECK_CODE_OFFSET();
540 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700541 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700542 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
543 size_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
jeffhao55d78212011-11-02 11:41:50 -0700544
545 // Deduplicate invoke stubs
jeffhaof479dcc2011-11-02 15:54:15 -0700546 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&invoke_stub);
jeffhao55d78212011-11-02 11:41:50 -0700547 if (stub_iter != code_offsets_.end() && code_offset != method->GetOatInvokeStubOffset()) {
548 DCHECK((invoke_stub_size == 0 && method->GetOatInvokeStubOffset() == 0)
549 || stub_iter->second == method->GetOatInvokeStubOffset()) << PrettyMethod(method);
550 } else {
551 DCHECK((invoke_stub_size == 0 && method->GetOatInvokeStubOffset() == 0)
552 || code_offset == method->GetOatInvokeStubOffset()) << PrettyMethod(method);
553 if (!file->WriteFully(&invoke_stub[0], invoke_stub_size)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700554 ReportWriteFailure("invoke stub code", method, file);
jeffhao55d78212011-11-02 11:41:50 -0700555 return false;
556 }
557 code_offset += invoke_stub_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700558 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700559 DCHECK_CODE_OFFSET();
560 }
561
Brian Carlstrome24fa612011-09-29 00:53:55 -0700562 return code_offset;
563}
564
565OatWriter::~OatWriter() {
566 delete oat_header_;
567 STLDeleteElements(&oat_dex_files_);
568 STLDeleteElements(&oat_classes_);
569 STLDeleteElements(&oat_methods_);
570}
571
572OatWriter::OatDexFile::OatDexFile(const DexFile& dex_file) {
573 const std::string& location = dex_file.GetLocation();
574 dex_file_location_size_ = location.size();
575 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
576 dex_file_checksum_ = dex_file.GetHeader().checksum_;
577}
578
579size_t OatWriter::OatDexFile::SizeOf() const {
580 return sizeof(dex_file_location_size_)
581 + dex_file_location_size_
582 + sizeof(dex_file_checksum_)
583 + sizeof(classes_offset_);
584}
585
586void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
587 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
588 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
589 oat_header.UpdateChecksum(&dex_file_checksum_, sizeof(dex_file_checksum_));
590 oat_header.UpdateChecksum(&classes_offset_, sizeof(classes_offset_));
591}
592
593bool OatWriter::OatDexFile::Write(File* file) const {
594 if (!file->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700595 PLOG(ERROR) << "Failed to write dex file location length to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700596 return false;
597 }
598 if (!file->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700599 PLOG(ERROR) << "Failed to write dex file location data to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700600 return false;
601 }
602 if (!file->WriteFully(&dex_file_checksum_, sizeof(dex_file_checksum_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700603 PLOG(ERROR) << "Failed to write dex file checksum to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700604 return false;
605 }
606 if (!file->WriteFully(&classes_offset_, sizeof(classes_offset_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700607 PLOG(ERROR) << "Failed to write classes offset to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700608 return false;
609 }
610 return true;
611}
612
613OatWriter::OatClasses::OatClasses(const DexFile& dex_file) {
614 methods_offsets_.resize(dex_file.NumClassDefs());
615}
616
617size_t OatWriter::OatClasses::SizeOf() const {
618 return (sizeof(methods_offsets_[0]) * methods_offsets_.size());
619}
620
621void OatWriter::OatClasses::UpdateChecksum(OatHeader& oat_header) const {
622 oat_header.UpdateChecksum(&methods_offsets_[0], SizeOf());
623}
624
625bool OatWriter::OatClasses::Write(File* file) const {
626 if (!file->WriteFully(&methods_offsets_[0], SizeOf())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700627 PLOG(ERROR) << "Failed to write methods offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700628 return false;
629 }
630 return true;
631}
632
633OatWriter::OatMethods::OatMethods(uint32_t methods_count) {
634 method_offsets_.resize(methods_count);
635}
636
637size_t OatWriter::OatMethods::SizeOf() const {
638 return (sizeof(method_offsets_[0]) * method_offsets_.size());
639}
640
641void OatWriter::OatMethods::UpdateChecksum(OatHeader& oat_header) const {
642 oat_header.UpdateChecksum(&method_offsets_[0], SizeOf());
643}
644
645bool OatWriter::OatMethods::Write(File* file) const {
646 if (!file->WriteFully(&method_offsets_[0], SizeOf())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700647 PLOG(ERROR) << "Failed to write method offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700648 return false;
649 }
650 return true;
651}
652
653} // namespace art