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