blob: fa27737c988906237f1046e0af4493c6da2e0fcf [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;
Ian Rogers0571d352011-11-03 19:51:38 -070027 oat_header_ = NULL;
28 executable_offset_padding_length_ = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -070029
30 size_t offset = InitOatHeader();
31 offset = InitOatDexFiles(offset);
Brian Carlstrom89521892011-12-07 22:05:07 -080032 offset = InitDexFiles(offset);
Brian Carlstrom389efb02012-01-11 12:06:26 -080033 offset = InitOatClasses(offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -070034 offset = InitOatCode(offset);
35 offset = InitOatCodeDexFiles(offset);
36
37 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -070038}
39
Ian Rogers0571d352011-11-03 19:51:38 -070040OatWriter::~OatWriter() {
41 delete oat_header_;
42 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -080043 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -070044}
45
Brian Carlstrome24fa612011-09-29 00:53:55 -070046size_t OatWriter::InitOatHeader() {
47 // create the OatHeader
48 oat_header_ = new OatHeader(dex_files_);
49 size_t offset = sizeof(*oat_header_);
50 return offset;
51}
52
53size_t OatWriter::InitOatDexFiles(size_t offset) {
54 // create the OatDexFiles
55 for (size_t i = 0; i != dex_files_->size(); ++i) {
56 const DexFile* dex_file = (*dex_files_)[i];
57 CHECK(dex_file != NULL);
58 OatDexFile* oat_dex_file = new OatDexFile(*dex_file);
59 oat_dex_files_.push_back(oat_dex_file);
60 offset += oat_dex_file->SizeOf();
61 }
62 return offset;
63}
64
Brian Carlstrom89521892011-12-07 22:05:07 -080065size_t OatWriter::InitDexFiles(size_t offset) {
66 // calculate the offsets within OatDexFiles to the DexFiles
67 for (size_t i = 0; i != dex_files_->size(); ++i) {
68 // dex files are required to be 4 byte aligned
69 offset = RoundUp(offset, 4);
70
71 // set offset in OatDexFile to DexFile
72 oat_dex_files_[i]->dex_file_offset_ = offset;
73
74 const DexFile* dex_file = (*dex_files_)[i];
75 offset += dex_file->GetHeader().file_size_;
76 }
77 return offset;
78}
79
Brian Carlstrom389efb02012-01-11 12:06:26 -080080size_t OatWriter::InitOatClasses(size_t offset) {
81 // create the OatClasses
82 // calculate the offsets within OatDexFiles to OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -070083 size_t class_index = 0;
84 for (size_t i = 0; i != dex_files_->size(); ++i) {
85 const DexFile* dex_file = (*dex_files_)[i];
86 for (size_t class_def_index = 0;
87 class_def_index < dex_file->NumClassDefs();
88 class_def_index++, class_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -080089 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -070090 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
91 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -070092 uint32_t num_methods = 0;
93 if (class_data != NULL) { // ie not an empty class, such as a marker interface
94 ClassDataItemIterator it(*dex_file, class_data);
95 size_t num_direct_methods = it.NumDirectMethods();
96 size_t num_virtual_methods = it.NumVirtualMethods();
97 num_methods = num_direct_methods + num_virtual_methods;
98 }
Brian Carlstrom389efb02012-01-11 12:06:26 -080099 OatClass* oat_class = new OatClass(num_methods);
100 oat_classes_.push_back(oat_class);
101 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700102 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800103 oat_dex_files_[i]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700104 }
105 return offset;
106}
107
108size_t OatWriter::InitOatCode(size_t offset) {
109 // calculate the offsets within OatHeader to executable code
110 size_t old_offset = offset;
111 // required to be on a new page boundary
112 offset = RoundUp(offset, kPageSize);
113 oat_header_->SetExecutableOffset(offset);
114 executable_offset_padding_length_ = offset - old_offset;
115 return offset;
116}
117
118size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700119 size_t oat_class_index = 0;
120 for (size_t i = 0; i != dex_files_->size(); ++i) {
121 const DexFile* dex_file = (*dex_files_)[i];
122 CHECK(dex_file != NULL);
123 offset = InitOatCodeDexFile(offset, oat_class_index, *dex_file);
124 }
125 return offset;
126}
127
128size_t OatWriter::InitOatCodeDexFile(size_t offset,
129 size_t& oat_class_index,
130 const DexFile& dex_file) {
Brian Carlstrom389efb02012-01-11 12:06:26 -0800131 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700132 class_def_index < dex_file.NumClassDefs();
133 class_def_index++, oat_class_index++) {
134 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
135 offset = InitOatCodeClassDef(offset, oat_class_index, dex_file, class_def);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800136 oat_classes_[oat_class_index]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700137 }
138 return offset;
139}
140
141size_t OatWriter::InitOatCodeClassDef(size_t offset,
142 size_t oat_class_index,
143 const DexFile& dex_file,
144 const DexFile::ClassDef& class_def) {
145 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700146 if (class_data == NULL) {
147 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700148 return offset;
149 }
Ian Rogers0571d352011-11-03 19:51:38 -0700150 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800151 CHECK_EQ(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700152 it.NumDirectMethods() + it.NumVirtualMethods());
153 // Skip fields
154 while (it.HasNextStaticField()) {
155 it.Next();
156 }
157 while (it.HasNextInstanceField()) {
158 it.Next();
159 }
160 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700161 size_t class_def_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700162 while (it.HasNextDirectMethod()) {
163 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
164 offset = InitOatCodeMethod(offset, oat_class_index, class_def_method_index, is_static, true,
165 it.GetMemberIndex(), &dex_file);
166 class_def_method_index++;
167 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700168 }
Ian Rogers0571d352011-11-03 19:51:38 -0700169 while (it.HasNextVirtualMethod()) {
170 CHECK_EQ(it.GetMemberAccessFlags() & kAccStatic, 0U);
171 offset = InitOatCodeMethod(offset, oat_class_index, class_def_method_index, false, false,
172 it.GetMemberIndex(), &dex_file);
173 class_def_method_index++;
174 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700175 }
Ian Rogers0571d352011-11-03 19:51:38 -0700176 DCHECK(!it.HasNext());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700177 return offset;
178}
179
Ian Rogers0571d352011-11-03 19:51:38 -0700180size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
181 size_t class_def_method_index, bool is_static, bool is_direct,
182 uint32_t method_idx, const DexFile* dex_file) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700183 // derived from CompiledMethod if available
184 uint32_t code_offset = 0;
185 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700186 uint32_t core_spill_mask = 0;
187 uint32_t fp_spill_mask = 0;
188 uint32_t mapping_table_offset = 0;
189 uint32_t vmap_table_offset = 0;
190 // derived from CompiledInvokeStub if available
191 uint32_t invoke_stub_offset = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700192
Ian Rogers0571d352011-11-03 19:51:38 -0700193 CompiledMethod* compiled_method =
194 compiler_->GetCompiledMethod(art::Compiler::MethodReference(dex_file, method_idx));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700195 if (compiled_method != NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700196 offset = compiled_method->AlignCode(offset);
Elliott Hughes06b37d92011-10-16 11:51:29 -0700197 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700198 const std::vector<uint8_t>& code = compiled_method->GetCode();
199 size_t code_size = code.size() * sizeof(code[0]);
200 uint32_t thumb_offset = compiled_method->CodeDelta();
201 code_offset = (code_size == 0) ? 0 : offset + thumb_offset;
jeffhao55d78212011-11-02 11:41:50 -0700202
203 // Deduplicate code arrays
jeffhaof479dcc2011-11-02 15:54:15 -0700204 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
jeffhao55d78212011-11-02 11:41:50 -0700205 if (code_iter != code_offsets_.end()) {
206 code_offset = code_iter->second;
207 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700208 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&code, code_offset));
jeffhao55d78212011-11-02 11:41:50 -0700209 offset += code_size;
210 oat_header_->UpdateChecksum(&code[0], code_size);
211 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700212
213 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700214 core_spill_mask = compiled_method->GetCoreSpillMask();
215 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700216 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700217
218 offset += sizeof(frame_size_in_bytes);
219 oat_header_->UpdateChecksum(&frame_size_in_bytes, sizeof(frame_size_in_bytes));
220
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700221 offset += sizeof(core_spill_mask);
222 oat_header_->UpdateChecksum(&core_spill_mask, sizeof(core_spill_mask));
223
224 offset += sizeof(fp_spill_mask);
225 oat_header_->UpdateChecksum(&fp_spill_mask, sizeof(fp_spill_mask));
226
227 if (compiled_method != NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700228 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
229 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
230 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700231
232 // Deduplicate mapping tables
jeffhaof479dcc2011-11-02 15:54:15 -0700233 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
jeffhao55d78212011-11-02 11:41:50 -0700234 if (mapping_iter != mapping_table_offsets_.end()) {
235 mapping_table_offset = mapping_iter->second;
236 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700237 mapping_table_offsets_.insert(std::pair<const std::vector<uint32_t>*, uint32_t>(&mapping_table, mapping_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700238 offset += mapping_table_size;
239 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
240 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700241
242 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
243 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
244 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700245
246 // Deduplicate vmap tables
jeffhaof479dcc2011-11-02 15:54:15 -0700247 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
jeffhao55d78212011-11-02 11:41:50 -0700248 if (vmap_iter != vmap_table_offsets_.end()) {
249 vmap_table_offset = vmap_iter->second;
250 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700251 vmap_table_offsets_.insert(std::pair<const std::vector<uint16_t>*, uint32_t>(&vmap_table, vmap_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700252 offset += vmap_table_size;
253 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
254 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700255 }
256
Ian Rogers0571d352011-11-03 19:51:38 -0700257 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx));
258 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700259 if (compiled_invoke_stub != NULL) {
260 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
Elliott Hughes06b37d92011-10-16 11:51:29 -0700261 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700262 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
263 size_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
264 invoke_stub_offset = (invoke_stub_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700265
266 // Deduplicate invoke stubs
jeffhaof479dcc2011-11-02 15:54:15 -0700267 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&invoke_stub);
jeffhao55d78212011-11-02 11:41:50 -0700268 if (stub_iter != code_offsets_.end()) {
269 invoke_stub_offset = stub_iter->second;
270 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700271 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&invoke_stub, invoke_stub_offset));
jeffhao55d78212011-11-02 11:41:50 -0700272 offset += invoke_stub_size;
273 oat_header_->UpdateChecksum(&invoke_stub[0], invoke_stub_size);
274 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700275 }
276
Brian Carlstrom389efb02012-01-11 12:06:26 -0800277 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index]
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700278 = OatMethodOffsets(code_offset,
279 frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700280 core_spill_mask,
281 fp_spill_mask,
282 mapping_table_offset,
283 vmap_table_offset,
284 invoke_stub_offset);
285
Ian Rogers0571d352011-11-03 19:51:38 -0700286 if (compiler_->IsImage()) {
287 ClassLinker* linker = Runtime::Current()->GetClassLinker();
288 DexCache* dex_cache = linker->FindDexCache(*dex_file);
289 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader_,
290 is_direct);
291 CHECK(method != NULL);
292 method->SetFrameSizeInBytes(frame_size_in_bytes);
293 method->SetCoreSpillMask(core_spill_mask);
294 method->SetFpSpillMask(fp_spill_mask);
295 method->SetOatMappingTableOffset(mapping_table_offset);
296 method->SetOatCodeOffset(code_offset);
297 method->SetOatVmapTableOffset(vmap_table_offset);
298 method->SetOatInvokeStubOffset(invoke_stub_offset);
299 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700300 return offset;
301}
302
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700303#define DCHECK_CODE_OFFSET() \
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800304 DCHECK_EQ(static_cast<off_t>(code_offset), lseek(file->Fd(), 0, SEEK_CUR))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700305
Elliott Hughes234da572011-11-03 22:13:06 -0700306bool OatWriter::Write(File* file) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700307 if (!file->WriteFully(oat_header_, sizeof(*oat_header_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700308 PLOG(ERROR) << "Failed to write oat header to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700309 return false;
310 }
311
Elliott Hughes234da572011-11-03 22:13:06 -0700312 if (!WriteTables(file)) {
313 LOG(ERROR) << "Failed to write oat tables to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700314 return false;
315 }
316
Elliott Hughes234da572011-11-03 22:13:06 -0700317 size_t code_offset = WriteCode(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700318 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700319 LOG(ERROR) << "Failed to write oat code to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700320 return false;
321 }
322
Elliott Hughes234da572011-11-03 22:13:06 -0700323 code_offset = WriteCodeDexFiles(file, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700324 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700325 LOG(ERROR) << "Failed to write oat code for dex files to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700326 return false;
327 }
328
329 return true;
330}
331
332bool OatWriter::WriteTables(File* file) {
333 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
334 if (!oat_dex_files_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700335 PLOG(ERROR) << "Failed to write oat dex information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700336 return false;
337 }
338 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800339 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
340 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800341 off_t actual_offset = lseek(file->Fd(), expected_offset, SEEK_SET);
Brian Carlstrom89521892011-12-07 22:05:07 -0800342 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
343 const DexFile* dex_file = (*dex_files_)[i];
344 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
345 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
346 return false;
347 }
348 const DexFile* dex_file = (*dex_files_)[i];
349 if (!file->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
350 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << file->name();
351 return false;
352 }
353 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800354 for (size_t i = 0; i != oat_classes_.size(); ++i) {
355 if (!oat_classes_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700356 PLOG(ERROR) << "Failed to write oat methods information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700357 return false;
358 }
359 }
360 return true;
361}
362
363size_t OatWriter::WriteCode(File* file) {
364 uint32_t code_offset = oat_header_->GetExecutableOffset();
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800365 off_t new_offset = lseek(file->Fd(), executable_offset_padding_length_, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700366 if (static_cast<uint32_t>(new_offset) != code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700367 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700368 << " Expected: " << code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700369 return 0;
370 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700371 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700372 return code_offset;
373}
374
375size_t OatWriter::WriteCodeDexFiles(File* file, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700376 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800377 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700378 const DexFile* dex_file = (*dex_files_)[i];
379 CHECK(dex_file != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700380 code_offset = WriteCodeDexFile(file, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700381 if (code_offset == 0) {
382 return 0;
383 }
384 }
385 return code_offset;
386}
387
Ian Rogers0571d352011-11-03 19:51:38 -0700388size_t OatWriter::WriteCodeDexFile(File* file, size_t code_offset, size_t& oat_class_index,
389 const DexFile& dex_file) {
390 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
391 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700392 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700393 code_offset = WriteCodeClassDef(file, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700394 if (code_offset == 0) {
395 return 0;
396 }
397 }
398 return code_offset;
399}
400
Ian Rogers0571d352011-11-03 19:51:38 -0700401void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
402 const DexFile& dex_file, File* f) const {
403 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
404 << " to " << f->name();
Elliott Hughes234da572011-11-03 22:13:06 -0700405}
406
Brian Carlstrome24fa612011-09-29 00:53:55 -0700407size_t OatWriter::WriteCodeClassDef(File* file,
Ian Rogers0571d352011-11-03 19:51:38 -0700408 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700409 const DexFile& dex_file,
410 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700411 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700412 if (class_data == NULL) {
413 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700414 return code_offset;
415 }
Ian Rogers0571d352011-11-03 19:51:38 -0700416 ClassDataItemIterator it(dex_file, class_data);
417 // Skip fields
418 while (it.HasNextStaticField()) {
419 it.Next();
420 }
421 while (it.HasNextInstanceField()) {
422 it.Next();
423 }
424 // Process methods
425 size_t class_def_method_index = 0;
426 while (it.HasNextDirectMethod()) {
427 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
428 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
429 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700430 if (code_offset == 0) {
431 return 0;
432 }
Ian Rogers0571d352011-11-03 19:51:38 -0700433 class_def_method_index++;
434 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700435 }
Ian Rogers0571d352011-11-03 19:51:38 -0700436 while (it.HasNextVirtualMethod()) {
437 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
438 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700439 if (code_offset == 0) {
440 return 0;
441 }
Ian Rogers0571d352011-11-03 19:51:38 -0700442 class_def_method_index++;
443 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700444 }
445 return code_offset;
446}
447
Ian Rogers0571d352011-11-03 19:51:38 -0700448size_t OatWriter::WriteCodeMethod(File* file, size_t code_offset, size_t oat_class_index,
449 size_t class_def_method_index, bool is_static,
450 uint32_t method_idx, const DexFile& dex_file) {
451 const CompiledMethod* compiled_method =
452 compiler_->GetCompiledMethod(art::Compiler::MethodReference(&dex_file, method_idx));
453
454 uint32_t frame_size_in_bytes = 0;
455 uint32_t core_spill_mask = 0;
456 uint32_t fp_spill_mask = 0;
457
458 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800459 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700460
461
462 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700463 uint32_t aligned_code_offset = compiled_method->AlignCode(code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700464 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
465 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800466 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700467 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700468 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700469 << " Expected: " << aligned_code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700470 return false;
471 }
472 code_offset += aligned_code_delta;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700473 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700474 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700475 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700476 const std::vector<uint8_t>& code = compiled_method->GetCode();
477 size_t code_size = code.size() * sizeof(code[0]);
jeffhao55d78212011-11-02 11:41:50 -0700478
479 // Deduplicate code arrays
480 size_t offset = code_offset + compiled_method->CodeDelta();
jeffhaof479dcc2011-11-02 15:54:15 -0700481 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Ian Rogers0571d352011-11-03 19:51:38 -0700482 if (code_iter != code_offsets_.end() && offset != method_offsets.code_offset_) {
483 DCHECK((code_size == 0 && method_offsets.code_offset_ == 0)
484 || code_iter->second == method_offsets.code_offset_)
485 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700486 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700487 DCHECK((code_size == 0 && method_offsets.code_offset_ == 0)
488 || offset == method_offsets.code_offset_)
489 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700490 if (!file->WriteFully(&code[0], code_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700491 ReportWriteFailure("method code", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700492 return false;
493 }
494 code_offset += code_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700495 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700496 DCHECK_CODE_OFFSET();
Ian Rogers0571d352011-11-03 19:51:38 -0700497 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
498 core_spill_mask = compiled_method->GetCoreSpillMask();
499 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700500 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700501
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700502 if (!file->WriteFully(&frame_size_in_bytes, sizeof(frame_size_in_bytes))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700503 ReportWriteFailure("method frame size", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700504 return false;
505 }
506 code_offset += sizeof(frame_size_in_bytes);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700507 if (!file->WriteFully(&core_spill_mask, sizeof(core_spill_mask))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700508 ReportWriteFailure("method core spill mask", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700509 return false;
510 }
511 code_offset += sizeof(core_spill_mask);
512 if (!file->WriteFully(&fp_spill_mask, sizeof(fp_spill_mask))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700513 ReportWriteFailure("method fp spill mask", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700514 return false;
515 }
516 code_offset += sizeof(fp_spill_mask);
517
518 if (compiled_method != NULL) {
519 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
520 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700521
522 // Deduplicate mapping tables
Ian Rogers0571d352011-11-03 19:51:38 -0700523 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
524 mapping_table_offsets_.find(&mapping_table);
525 if (mapping_iter != mapping_table_offsets_.end() &&
526 code_offset != method_offsets.mapping_table_offset_) {
527 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
528 || mapping_iter->second == method_offsets.mapping_table_offset_)
529 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700530 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700531 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
532 || code_offset == method_offsets.mapping_table_offset_)
533 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700534 if (!file->WriteFully(&mapping_table[0], mapping_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700535 ReportWriteFailure("mapping table", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700536 return false;
537 }
538 code_offset += mapping_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700539 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700540 DCHECK_CODE_OFFSET();
541
542 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
543 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700544
545 // Deduplicate vmap tables
Ian Rogers0571d352011-11-03 19:51:38 -0700546 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
547 vmap_table_offsets_.find(&vmap_table);
548 if (vmap_iter != vmap_table_offsets_.end() &&
549 code_offset != method_offsets.vmap_table_offset_) {
550 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
551 || vmap_iter->second == method_offsets.vmap_table_offset_)
552 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700553 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700554 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
555 || code_offset == method_offsets.vmap_table_offset_)
556 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700557 if (!file->WriteFully(&vmap_table[0], vmap_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700558 ReportWriteFailure("vmap table", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700559 return false;
560 }
561 code_offset += vmap_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700562 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700563 DCHECK_CODE_OFFSET();
564 }
Ian Rogers0571d352011-11-03 19:51:38 -0700565 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
566 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700567 if (compiled_invoke_stub != NULL) {
568 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
569 compiler_->GetInstructionSet());
570 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
571 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800572 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700573 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
574 PLOG(ERROR) << "Failed to seek to align invoke stub code. Actual: " << new_offset
575 << " Expected: " << aligned_code_offset;
576 return false;
577 }
578 code_offset += aligned_code_delta;
579 DCHECK_CODE_OFFSET();
580 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700581 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700582 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
583 size_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
jeffhao55d78212011-11-02 11:41:50 -0700584
585 // Deduplicate invoke stubs
Ian Rogers0571d352011-11-03 19:51:38 -0700586 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter =
587 code_offsets_.find(&invoke_stub);
588 if (stub_iter != code_offsets_.end() &&
589 code_offset != method_offsets.invoke_stub_offset_) {
590 DCHECK((invoke_stub_size == 0 && method_offsets.invoke_stub_offset_ == 0)
591 || stub_iter->second == method_offsets.invoke_stub_offset_)
592 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700593 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700594 DCHECK((invoke_stub_size == 0 && method_offsets.invoke_stub_offset_ == 0)
595 || code_offset == method_offsets.invoke_stub_offset_)
596 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700597 if (!file->WriteFully(&invoke_stub[0], invoke_stub_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700598 ReportWriteFailure("invoke stub code", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700599 return false;
600 }
601 code_offset += invoke_stub_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700602 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700603 DCHECK_CODE_OFFSET();
604 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700605 return code_offset;
606}
607
Brian Carlstrome24fa612011-09-29 00:53:55 -0700608OatWriter::OatDexFile::OatDexFile(const DexFile& dex_file) {
Elliott Hughes95572412011-12-13 18:14:20 -0800609 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700610 dex_file_location_size_ = location.size();
611 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
612 dex_file_checksum_ = dex_file.GetHeader().checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800613 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800614 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700615}
616
617size_t OatWriter::OatDexFile::SizeOf() const {
618 return sizeof(dex_file_location_size_)
619 + dex_file_location_size_
620 + sizeof(dex_file_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800621 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800622 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700623}
624
625void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
626 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
627 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
628 oat_header.UpdateChecksum(&dex_file_checksum_, sizeof(dex_file_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800629 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800630 oat_header.UpdateChecksum(&methods_offsets_[0],
631 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700632}
633
634bool OatWriter::OatDexFile::Write(File* file) const {
635 if (!file->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700636 PLOG(ERROR) << "Failed to write dex file location length to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700637 return false;
638 }
639 if (!file->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700640 PLOG(ERROR) << "Failed to write dex file location data to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700641 return false;
642 }
643 if (!file->WriteFully(&dex_file_checksum_, sizeof(dex_file_checksum_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700644 PLOG(ERROR) << "Failed to write dex file checksum to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700645 return false;
646 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800647 if (!file->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
648 PLOG(ERROR) << "Failed to write dex file offset to " << file->name();
649 return false;
650 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800651 if (!file->WriteFully(&methods_offsets_[0],
652 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700653 PLOG(ERROR) << "Failed to write methods offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700654 return false;
655 }
656 return true;
657}
658
Brian Carlstrom389efb02012-01-11 12:06:26 -0800659OatWriter::OatClass::OatClass(uint32_t methods_count) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700660 method_offsets_.resize(methods_count);
661}
662
Brian Carlstrom389efb02012-01-11 12:06:26 -0800663size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700664 return (sizeof(method_offsets_[0]) * method_offsets_.size());
665}
666
Brian Carlstrom389efb02012-01-11 12:06:26 -0800667void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700668 oat_header.UpdateChecksum(&method_offsets_[0], SizeOf());
669}
670
Brian Carlstrom389efb02012-01-11 12:06:26 -0800671bool OatWriter::OatClass::Write(File* file) const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700672 if (!file->WriteFully(&method_offsets_[0], SizeOf())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700673 PLOG(ERROR) << "Failed to write method offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700674 return false;
675 }
676 return true;
677}
678
679} // namespace art