blob: 214d177dde228251f693b1ffad36417e5f09748a [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
Brian Carlstrom3320cf42011-10-04 14:58:28 -070013bool OatWriter::Create(const std::string& filename,
14 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);
Brian Carlstrome24fa612011-09-29 00:53:55 -070018 return oat_writer.Write(filename);
19}
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
Brian Carlstrome24fa612011-09-29 00:53:55 -0700296bool OatWriter::Write(const std::string& filename) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700297 UniquePtr<File> file(OS::OpenFile(filename.c_str(), true));
298 if (file.get() == NULL) {
Ian Rogers3fe79572011-11-02 18:24:30 -0700299 PLOG(ERROR) << "Failed to open file " << filename << " for writing";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700300 return false;
301 }
302
303 if (!file->WriteFully(oat_header_, sizeof(*oat_header_))) {
304 PLOG(ERROR) << "Failed to write oat header to " << filename;
305 return false;
306 }
307
308 if (!WriteTables(file.get())) {
309 LOG(ERROR) << "Failed to write oat tables to " << filename;
310 return false;
311 }
312
313 size_t code_offset = WriteCode(file.get());
314 if (code_offset == 0) {
315 LOG(ERROR) << "Failed to write oat code to " << filename;
316 return false;
317 }
318
319 code_offset = WriteCodeDexFiles(file.get(), code_offset);
320 if (code_offset == 0) {
321 LOG(ERROR) << "Failed to write oat code for dex files to " << filename;
322 return false;
323 }
324
325 return true;
326}
327
328bool OatWriter::WriteTables(File* file) {
329 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
330 if (!oat_dex_files_[i]->Write(file)) {
331 PLOG(ERROR) << "Failed to write oat dex information";
332 return false;
333 }
334 }
335 for (size_t i = 0; i != oat_classes_.size(); ++i) {
336 if (!oat_classes_[i]->Write(file)) {
337 PLOG(ERROR) << "Failed to write oat classes information";
338 return false;
339 }
340 }
341 for (size_t i = 0; i != oat_methods_.size(); ++i) {
342 if (!oat_methods_[i]->Write(file)) {
343 PLOG(ERROR) << "Failed to write oat methods information";
344 return false;
345 }
346 }
347 return true;
348}
349
350size_t OatWriter::WriteCode(File* file) {
351 uint32_t code_offset = oat_header_->GetExecutableOffset();
352 off_t new_offset = lseek(file->Fd(), executable_offset_padding_length_, SEEK_CUR);
353 if (static_cast<uint32_t>(new_offset) != code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700354 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
355 << " Expected: " << code_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700356 return 0;
357 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700358 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700359 return code_offset;
360}
361
362size_t OatWriter::WriteCodeDexFiles(File* file, size_t code_offset) {
363 for (size_t i = 0; i != oat_classes_.size(); ++i) {
364 const DexFile* dex_file = (*dex_files_)[i];
365 CHECK(dex_file != NULL);
366 code_offset = WriteCodeDexFile(file, code_offset, *dex_file);
367 if (code_offset == 0) {
368 return 0;
369 }
370 }
371 return code_offset;
372}
373
374size_t OatWriter::WriteCodeDexFile(File* file,
375 size_t code_offset,
376 const DexFile& dex_file) {
377 for (size_t class_def_index = 0;
378 class_def_index < dex_file.NumClassDefs();
379 class_def_index++) {
380 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
381 code_offset = WriteCodeClassDef(file, code_offset, dex_file, class_def);
382 if (code_offset == 0) {
383 return 0;
384 }
385 }
386 return code_offset;
387}
388
389size_t OatWriter::WriteCodeClassDef(File* file,
390 size_t code_offset,
391 const DexFile& dex_file,
392 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700393 const byte* class_data = dex_file.GetClassData(class_def);
394 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
395 size_t num_virtual_methods = header.virtual_methods_size_;
396 const char* descriptor = dex_file.GetClassDescriptor(class_def);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700397 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700398 Class* klass = class_linker->FindClass(descriptor, class_loader_);
Ian Rogers387b6992011-10-31 17:52:37 -0700399 if (klass == NULL) {
400 LOG(WARNING) << "Didn't find class '" << descriptor << "' in dex file " << dex_file.GetLocation();
401 Thread* thread = Thread::Current();
402 DCHECK(thread->IsExceptionPending());
403 thread->ClearException();
404 return code_offset;
405 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700406
Brian Carlstrome24fa612011-09-29 00:53:55 -0700407 // Note that we clear the code array here, image_writer will use GetCodeOffset to find it
408 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
409 Method* method = klass->GetDirectMethod(i);
410 code_offset = WriteCodeMethod(file, code_offset, method);
411 if (code_offset == 0) {
412 return 0;
413 }
414 }
415 // note that num_virtual_methods != klass->NumVirtualMethods() because of miranda methods
416 for (size_t i = 0; i < num_virtual_methods; i++) {
417 Method* method = klass->GetVirtualMethod(i);
418 code_offset = WriteCodeMethod(file, code_offset, method);
419 if (code_offset == 0) {
420 return 0;
421 }
422 }
423 for (size_t i = num_virtual_methods; i < klass->NumVirtualMethods(); i++) {
424 Method* method = klass->GetVirtualMethod(i);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700425 CHECK(compiler_->GetCompiledMethod(method) == NULL) << PrettyMethod(method);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700426 }
427 return code_offset;
428}
429
Elliott Hughesf09afe82011-10-16 14:24:21 -0700430size_t OatWriter::WriteCodeMethod(File* file, size_t code_offset, Method* method) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700431 const CompiledMethod* compiled_method = compiler_->GetCompiledMethod(method);
432 if (compiled_method != NULL) {
433 uint32_t aligned_code_offset = compiled_method->AlignCode(code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700434 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
435 if (aligned_code_delta != 0) {
436 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
437 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700438 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
439 << " Expected: " << aligned_code_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700440 return false;
441 }
442 code_offset += aligned_code_delta;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700443 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700444 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700445 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700446 const std::vector<uint8_t>& code = compiled_method->GetCode();
447 size_t code_size = code.size() * sizeof(code[0]);
jeffhao55d78212011-11-02 11:41:50 -0700448
449 // Deduplicate code arrays
450 size_t offset = code_offset + compiled_method->CodeDelta();
jeffhaof479dcc2011-11-02 15:54:15 -0700451 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
jeffhao55d78212011-11-02 11:41:50 -0700452 if (code_iter != code_offsets_.end() && offset != method->GetOatCodeOffset()) {
453 DCHECK((code_size == 0 && method->GetOatCodeOffset() == 0)
454 || code_iter->second == method->GetOatCodeOffset()) << PrettyMethod(method);
455 } else {
456 DCHECK((code_size == 0 && method->GetOatCodeOffset() == 0)
457 || offset == method->GetOatCodeOffset()) << PrettyMethod(method);
458 if (!file->WriteFully(&code[0], code_size)) {
459 PLOG(ERROR) << "Failed to write method code for " << PrettyMethod(method);
460 return false;
461 }
462 code_offset += code_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700463 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700464 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700465 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700466
467 uint32_t frame_size_in_bytes = method->GetFrameSizeInBytes();
468 uint32_t return_pc_offset_in_bytes = method->GetReturnPcOffsetInBytes();
469 uint32_t core_spill_mask = method->GetCoreSpillMask();
470 uint32_t fp_spill_mask = method->GetFpSpillMask();
471 if (!file->WriteFully(&frame_size_in_bytes, sizeof(frame_size_in_bytes))) {
472 PLOG(ERROR) << "Failed to write method frame size for " << PrettyMethod(method);
473 return false;
474 }
475 code_offset += sizeof(frame_size_in_bytes);
476 if (!file->WriteFully(&return_pc_offset_in_bytes, sizeof(return_pc_offset_in_bytes))) {
477 PLOG(ERROR) << "Failed to write method return pc offset for " << PrettyMethod(method);
478 return false;
479 }
480 code_offset += sizeof(return_pc_offset_in_bytes);
481 if (!file->WriteFully(&core_spill_mask, sizeof(core_spill_mask))) {
482 PLOG(ERROR) << "Failed to write method core spill mask for " << PrettyMethod(method);
483 return false;
484 }
485 code_offset += sizeof(core_spill_mask);
486 if (!file->WriteFully(&fp_spill_mask, sizeof(fp_spill_mask))) {
487 PLOG(ERROR) << "Failed to write method fp spill mask for " << PrettyMethod(method);
488 return false;
489 }
490 code_offset += sizeof(fp_spill_mask);
491
492 if (compiled_method != NULL) {
493 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
494 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700495
496 // Deduplicate mapping tables
jeffhaof479dcc2011-11-02 15:54:15 -0700497 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
jeffhao55d78212011-11-02 11:41:50 -0700498 if (mapping_iter != mapping_table_offsets_.end() && code_offset != method->GetOatMappingTableOffset()) {
499 DCHECK((mapping_table_size == 0 && method->GetOatMappingTableOffset() == 0)
500 || mapping_iter->second == method->GetOatMappingTableOffset()) << PrettyMethod(method);
501 } else {
502 DCHECK((mapping_table_size == 0 && method->GetOatMappingTableOffset() == 0)
503 || code_offset == method->GetOatMappingTableOffset()) << PrettyMethod(method);
504 if (!file->WriteFully(&mapping_table[0], mapping_table_size)) {
505 PLOG(ERROR) << "Failed to write mapping table for " << PrettyMethod(method);
506 return false;
507 }
508 code_offset += mapping_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700509 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700510 DCHECK_CODE_OFFSET();
511
512 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
513 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700514
515 // Deduplicate vmap tables
jeffhaof479dcc2011-11-02 15:54:15 -0700516 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
jeffhao55d78212011-11-02 11:41:50 -0700517 if (vmap_iter != vmap_table_offsets_.end() && code_offset != method->GetOatVmapTableOffset()) {
518 DCHECK((vmap_table_size == 0 && method->GetOatVmapTableOffset() == 0)
519 || vmap_iter->second == method->GetOatVmapTableOffset()) << PrettyMethod(method);
520 } else {
521 DCHECK((vmap_table_size == 0 && method->GetOatVmapTableOffset() == 0)
522 || code_offset == method->GetOatVmapTableOffset()) << PrettyMethod(method);
523 if (!file->WriteFully(&vmap_table[0], vmap_table_size)) {
524 PLOG(ERROR) << "Failed to write vmap table for " << PrettyMethod(method);
525 return false;
526 }
527 code_offset += vmap_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700528 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700529 DCHECK_CODE_OFFSET();
530 }
531
532 const CompiledInvokeStub* compiled_invoke_stub = compiler_->GetCompiledInvokeStub(method);
533 if (compiled_invoke_stub != NULL) {
534 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
535 compiler_->GetInstructionSet());
536 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
537 if (aligned_code_delta != 0) {
538 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
539 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
540 PLOG(ERROR) << "Failed to seek to align invoke stub code. Actual: " << new_offset
541 << " Expected: " << aligned_code_offset;
542 return false;
543 }
544 code_offset += aligned_code_delta;
545 DCHECK_CODE_OFFSET();
546 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700547 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700548 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
549 size_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
jeffhao55d78212011-11-02 11:41:50 -0700550
551 // Deduplicate invoke stubs
jeffhaof479dcc2011-11-02 15:54:15 -0700552 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&invoke_stub);
jeffhao55d78212011-11-02 11:41:50 -0700553 if (stub_iter != code_offsets_.end() && code_offset != method->GetOatInvokeStubOffset()) {
554 DCHECK((invoke_stub_size == 0 && method->GetOatInvokeStubOffset() == 0)
555 || stub_iter->second == method->GetOatInvokeStubOffset()) << PrettyMethod(method);
556 } else {
557 DCHECK((invoke_stub_size == 0 && method->GetOatInvokeStubOffset() == 0)
558 || code_offset == method->GetOatInvokeStubOffset()) << PrettyMethod(method);
559 if (!file->WriteFully(&invoke_stub[0], invoke_stub_size)) {
560 PLOG(ERROR) << "Failed to write invoke stub code for " << PrettyMethod(method);
561 return false;
562 }
563 code_offset += invoke_stub_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700564 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700565 DCHECK_CODE_OFFSET();
566 }
567
Brian Carlstrome24fa612011-09-29 00:53:55 -0700568 return code_offset;
569}
570
571OatWriter::~OatWriter() {
572 delete oat_header_;
573 STLDeleteElements(&oat_dex_files_);
574 STLDeleteElements(&oat_classes_);
575 STLDeleteElements(&oat_methods_);
576}
577
578OatWriter::OatDexFile::OatDexFile(const DexFile& dex_file) {
579 const std::string& location = dex_file.GetLocation();
580 dex_file_location_size_ = location.size();
581 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
582 dex_file_checksum_ = dex_file.GetHeader().checksum_;
583}
584
585size_t OatWriter::OatDexFile::SizeOf() const {
586 return sizeof(dex_file_location_size_)
587 + dex_file_location_size_
588 + sizeof(dex_file_checksum_)
589 + sizeof(classes_offset_);
590}
591
592void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
593 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
594 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
595 oat_header.UpdateChecksum(&dex_file_checksum_, sizeof(dex_file_checksum_));
596 oat_header.UpdateChecksum(&classes_offset_, sizeof(classes_offset_));
597}
598
599bool OatWriter::OatDexFile::Write(File* file) const {
600 if (!file->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
601 PLOG(ERROR) << "Failed to write dex file location length";
602 return false;
603 }
604 if (!file->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
605 PLOG(ERROR) << "Failed to write dex file location data";
606 return false;
607 }
608 if (!file->WriteFully(&dex_file_checksum_, sizeof(dex_file_checksum_))) {
609 PLOG(ERROR) << "Failed to write dex file checksum";
610 return false;
611 }
612 if (!file->WriteFully(&classes_offset_, sizeof(classes_offset_))) {
613 PLOG(ERROR) << "Failed to write classes offset";
614 return false;
615 }
616 return true;
617}
618
619OatWriter::OatClasses::OatClasses(const DexFile& dex_file) {
620 methods_offsets_.resize(dex_file.NumClassDefs());
621}
622
623size_t OatWriter::OatClasses::SizeOf() const {
624 return (sizeof(methods_offsets_[0]) * methods_offsets_.size());
625}
626
627void OatWriter::OatClasses::UpdateChecksum(OatHeader& oat_header) const {
628 oat_header.UpdateChecksum(&methods_offsets_[0], SizeOf());
629}
630
631bool OatWriter::OatClasses::Write(File* file) const {
632 if (!file->WriteFully(&methods_offsets_[0], SizeOf())) {
633 PLOG(ERROR) << "Failed to methods offsets";
634 return false;
635 }
636 return true;
637}
638
639OatWriter::OatMethods::OatMethods(uint32_t methods_count) {
640 method_offsets_.resize(methods_count);
641}
642
643size_t OatWriter::OatMethods::SizeOf() const {
644 return (sizeof(method_offsets_[0]) * method_offsets_.size());
645}
646
647void OatWriter::OatMethods::UpdateChecksum(OatHeader& oat_header) const {
648 oat_header.UpdateChecksum(&method_offsets_[0], SizeOf());
649}
650
651bool OatWriter::OatMethods::Write(File* file) const {
652 if (!file->WriteFully(&method_offsets_[0], SizeOf())) {
653 PLOG(ERROR) << "Failed to method offsets";
654 return false;
655 }
656 return true;
657}
658
659} // namespace art