blob: 69aebb8272123d0edb1f9d8b53f1ad9a4c85ba1e [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 Carlstrome24fa612011-09-29 00:53:55 -070033 offset = InitOatMethods(offset);
34 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_);
Ian Rogers0571d352011-11-03 19:51:38 -070043 STLDeleteElements(&oat_methods_);
44}
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 Carlstrome24fa612011-09-29 00:53:55 -070080size_t OatWriter::InitOatMethods(size_t offset) {
81 // create the OatMethods
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -080082 // calculate the offsets within OatDexFiles to OatMethods
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 Carlstrome24fa612011-09-29 00:53:55 -070099 OatMethods* oat_methods = new OatMethods(num_methods);
100 oat_methods_.push_back(oat_methods);
101 offset += oat_methods->SizeOf();
102 }
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) {
119 // calculate the offsets within OatMethods
120 size_t oat_class_index = 0;
121 for (size_t i = 0; i != dex_files_->size(); ++i) {
122 const DexFile* dex_file = (*dex_files_)[i];
123 CHECK(dex_file != NULL);
124 offset = InitOatCodeDexFile(offset, oat_class_index, *dex_file);
125 }
126 return offset;
127}
128
129size_t OatWriter::InitOatCodeDexFile(size_t offset,
130 size_t& oat_class_index,
131 const DexFile& dex_file) {
132 for (size_t class_def_index = 0;
133 class_def_index < dex_file.NumClassDefs();
134 class_def_index++, oat_class_index++) {
135 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
136 offset = InitOatCodeClassDef(offset, oat_class_index, dex_file, class_def);
137 oat_methods_[oat_class_index]->UpdateChecksum(*oat_header_);
138 }
139 return offset;
140}
141
142size_t OatWriter::InitOatCodeClassDef(size_t offset,
143 size_t oat_class_index,
144 const DexFile& dex_file,
145 const DexFile::ClassDef& class_def) {
146 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700147 if (class_data == NULL) {
148 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700149 return offset;
150 }
Ian Rogers0571d352011-11-03 19:51:38 -0700151 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700152 CHECK_EQ(oat_methods_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700153 it.NumDirectMethods() + it.NumVirtualMethods());
154 // Skip fields
155 while (it.HasNextStaticField()) {
156 it.Next();
157 }
158 while (it.HasNextInstanceField()) {
159 it.Next();
160 }
161 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700162 size_t class_def_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700163 while (it.HasNextDirectMethod()) {
164 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
165 offset = InitOatCodeMethod(offset, oat_class_index, class_def_method_index, is_static, true,
166 it.GetMemberIndex(), &dex_file);
167 class_def_method_index++;
168 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700169 }
Ian Rogers0571d352011-11-03 19:51:38 -0700170 while (it.HasNextVirtualMethod()) {
171 CHECK_EQ(it.GetMemberAccessFlags() & kAccStatic, 0U);
172 offset = InitOatCodeMethod(offset, oat_class_index, class_def_method_index, false, false,
173 it.GetMemberIndex(), &dex_file);
174 class_def_method_index++;
175 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700176 }
Ian Rogers0571d352011-11-03 19:51:38 -0700177 DCHECK(!it.HasNext());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700178 return offset;
179}
180
Ian Rogers0571d352011-11-03 19:51:38 -0700181size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
182 size_t class_def_method_index, bool is_static, bool is_direct,
183 uint32_t method_idx, const DexFile* dex_file) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700184 // derived from CompiledMethod if available
185 uint32_t code_offset = 0;
186 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700187 uint32_t core_spill_mask = 0;
188 uint32_t fp_spill_mask = 0;
189 uint32_t mapping_table_offset = 0;
190 uint32_t vmap_table_offset = 0;
191 // derived from CompiledInvokeStub if available
192 uint32_t invoke_stub_offset = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700193
Ian Rogers0571d352011-11-03 19:51:38 -0700194 CompiledMethod* compiled_method =
195 compiler_->GetCompiledMethod(art::Compiler::MethodReference(dex_file, method_idx));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700196 if (compiled_method != NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700197 offset = compiled_method->AlignCode(offset);
Elliott Hughes06b37d92011-10-16 11:51:29 -0700198 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700199 const std::vector<uint8_t>& code = compiled_method->GetCode();
200 size_t code_size = code.size() * sizeof(code[0]);
201 uint32_t thumb_offset = compiled_method->CodeDelta();
202 code_offset = (code_size == 0) ? 0 : offset + thumb_offset;
jeffhao55d78212011-11-02 11:41:50 -0700203
204 // Deduplicate code arrays
jeffhaof479dcc2011-11-02 15:54:15 -0700205 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
jeffhao55d78212011-11-02 11:41:50 -0700206 if (code_iter != code_offsets_.end()) {
207 code_offset = code_iter->second;
208 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700209 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&code, code_offset));
jeffhao55d78212011-11-02 11:41:50 -0700210 offset += code_size;
211 oat_header_->UpdateChecksum(&code[0], code_size);
212 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700213
214 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700215 core_spill_mask = compiled_method->GetCoreSpillMask();
216 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700217 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700218
219 offset += sizeof(frame_size_in_bytes);
220 oat_header_->UpdateChecksum(&frame_size_in_bytes, sizeof(frame_size_in_bytes));
221
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700222 offset += sizeof(core_spill_mask);
223 oat_header_->UpdateChecksum(&core_spill_mask, sizeof(core_spill_mask));
224
225 offset += sizeof(fp_spill_mask);
226 oat_header_->UpdateChecksum(&fp_spill_mask, sizeof(fp_spill_mask));
227
228 if (compiled_method != NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700229 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
230 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
231 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700232
233 // Deduplicate mapping tables
jeffhaof479dcc2011-11-02 15:54:15 -0700234 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
jeffhao55d78212011-11-02 11:41:50 -0700235 if (mapping_iter != mapping_table_offsets_.end()) {
236 mapping_table_offset = mapping_iter->second;
237 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700238 mapping_table_offsets_.insert(std::pair<const std::vector<uint32_t>*, uint32_t>(&mapping_table, mapping_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700239 offset += mapping_table_size;
240 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
241 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700242
243 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
244 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
245 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700246
247 // Deduplicate vmap tables
jeffhaof479dcc2011-11-02 15:54:15 -0700248 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
jeffhao55d78212011-11-02 11:41:50 -0700249 if (vmap_iter != vmap_table_offsets_.end()) {
250 vmap_table_offset = vmap_iter->second;
251 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700252 vmap_table_offsets_.insert(std::pair<const std::vector<uint16_t>*, uint32_t>(&vmap_table, vmap_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700253 offset += vmap_table_size;
254 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
255 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700256 }
257
Ian Rogers0571d352011-11-03 19:51:38 -0700258 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx));
259 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700260 if (compiled_invoke_stub != NULL) {
261 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
Elliott Hughes06b37d92011-10-16 11:51:29 -0700262 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700263 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
264 size_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
265 invoke_stub_offset = (invoke_stub_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700266
267 // Deduplicate invoke stubs
jeffhaof479dcc2011-11-02 15:54:15 -0700268 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&invoke_stub);
jeffhao55d78212011-11-02 11:41:50 -0700269 if (stub_iter != code_offsets_.end()) {
270 invoke_stub_offset = stub_iter->second;
271 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700272 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&invoke_stub, invoke_stub_offset));
jeffhao55d78212011-11-02 11:41:50 -0700273 offset += invoke_stub_size;
274 oat_header_->UpdateChecksum(&invoke_stub[0], invoke_stub_size);
275 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700276 }
277
278 oat_methods_[oat_class_index]->method_offsets_[class_def_method_index]
279 = OatMethodOffsets(code_offset,
280 frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700281 core_spill_mask,
282 fp_spill_mask,
283 mapping_table_offset,
284 vmap_table_offset,
285 invoke_stub_offset);
286
Ian Rogers0571d352011-11-03 19:51:38 -0700287 if (compiler_->IsImage()) {
288 ClassLinker* linker = Runtime::Current()->GetClassLinker();
289 DexCache* dex_cache = linker->FindDexCache(*dex_file);
290 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader_,
291 is_direct);
292 CHECK(method != NULL);
293 method->SetFrameSizeInBytes(frame_size_in_bytes);
294 method->SetCoreSpillMask(core_spill_mask);
295 method->SetFpSpillMask(fp_spill_mask);
296 method->SetOatMappingTableOffset(mapping_table_offset);
297 method->SetOatCodeOffset(code_offset);
298 method->SetOatVmapTableOffset(vmap_table_offset);
299 method->SetOatInvokeStubOffset(invoke_stub_offset);
300 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700301 return offset;
302}
303
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700304#define DCHECK_CODE_OFFSET() \
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800305 DCHECK_EQ(static_cast<off_t>(code_offset), lseek(file->Fd(), 0, SEEK_CUR))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700306
Elliott Hughes234da572011-11-03 22:13:06 -0700307bool OatWriter::Write(File* file) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700308 if (!file->WriteFully(oat_header_, sizeof(*oat_header_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700309 PLOG(ERROR) << "Failed to write oat header to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700310 return false;
311 }
312
Elliott Hughes234da572011-11-03 22:13:06 -0700313 if (!WriteTables(file)) {
314 LOG(ERROR) << "Failed to write oat tables to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700315 return false;
316 }
317
Elliott Hughes234da572011-11-03 22:13:06 -0700318 size_t code_offset = WriteCode(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700319 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700320 LOG(ERROR) << "Failed to write oat code to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700321 return false;
322 }
323
Elliott Hughes234da572011-11-03 22:13:06 -0700324 code_offset = WriteCodeDexFiles(file, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700325 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700326 LOG(ERROR) << "Failed to write oat code for dex files to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700327 return false;
328 }
329
330 return true;
331}
332
333bool OatWriter::WriteTables(File* file) {
334 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
335 if (!oat_dex_files_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700336 PLOG(ERROR) << "Failed to write oat dex information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700337 return false;
338 }
339 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800340 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
341 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800342 off_t actual_offset = lseek(file->Fd(), expected_offset, SEEK_SET);
Brian Carlstrom89521892011-12-07 22:05:07 -0800343 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
344 const DexFile* dex_file = (*dex_files_)[i];
345 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
346 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
347 return false;
348 }
349 const DexFile* dex_file = (*dex_files_)[i];
350 if (!file->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
351 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << file->name();
352 return false;
353 }
354 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700355 for (size_t i = 0; i != oat_methods_.size(); ++i) {
356 if (!oat_methods_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700357 PLOG(ERROR) << "Failed to write oat methods information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700358 return false;
359 }
360 }
361 return true;
362}
363
364size_t OatWriter::WriteCode(File* file) {
365 uint32_t code_offset = oat_header_->GetExecutableOffset();
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800366 off_t new_offset = lseek(file->Fd(), executable_offset_padding_length_, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700367 if (static_cast<uint32_t>(new_offset) != code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700368 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700369 << " Expected: " << code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700370 return 0;
371 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700372 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700373 return code_offset;
374}
375
376size_t OatWriter::WriteCodeDexFiles(File* file, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700377 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800378 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700379 const DexFile* dex_file = (*dex_files_)[i];
380 CHECK(dex_file != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700381 code_offset = WriteCodeDexFile(file, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700382 if (code_offset == 0) {
383 return 0;
384 }
385 }
386 return code_offset;
387}
388
Ian Rogers0571d352011-11-03 19:51:38 -0700389size_t OatWriter::WriteCodeDexFile(File* file, size_t code_offset, size_t& oat_class_index,
390 const DexFile& dex_file) {
391 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
392 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700393 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700394 code_offset = WriteCodeClassDef(file, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700395 if (code_offset == 0) {
396 return 0;
397 }
398 }
399 return code_offset;
400}
401
Ian Rogers0571d352011-11-03 19:51:38 -0700402void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
403 const DexFile& dex_file, File* f) const {
404 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
405 << " to " << f->name();
Elliott Hughes234da572011-11-03 22:13:06 -0700406}
407
Brian Carlstrome24fa612011-09-29 00:53:55 -0700408size_t OatWriter::WriteCodeClassDef(File* file,
Ian Rogers0571d352011-11-03 19:51:38 -0700409 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700410 const DexFile& dex_file,
411 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700412 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700413 if (class_data == NULL) {
414 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700415 return code_offset;
416 }
Ian Rogers0571d352011-11-03 19:51:38 -0700417 ClassDataItemIterator it(dex_file, class_data);
418 // Skip fields
419 while (it.HasNextStaticField()) {
420 it.Next();
421 }
422 while (it.HasNextInstanceField()) {
423 it.Next();
424 }
425 // Process methods
426 size_t class_def_method_index = 0;
427 while (it.HasNextDirectMethod()) {
428 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
429 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
430 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700431 if (code_offset == 0) {
432 return 0;
433 }
Ian Rogers0571d352011-11-03 19:51:38 -0700434 class_def_method_index++;
435 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700436 }
Ian Rogers0571d352011-11-03 19:51:38 -0700437 while (it.HasNextVirtualMethod()) {
438 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
439 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700440 if (code_offset == 0) {
441 return 0;
442 }
Ian Rogers0571d352011-11-03 19:51:38 -0700443 class_def_method_index++;
444 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700445 }
446 return code_offset;
447}
448
Ian Rogers0571d352011-11-03 19:51:38 -0700449size_t OatWriter::WriteCodeMethod(File* file, size_t code_offset, size_t oat_class_index,
450 size_t class_def_method_index, bool is_static,
451 uint32_t method_idx, const DexFile& dex_file) {
452 const CompiledMethod* compiled_method =
453 compiler_->GetCompiledMethod(art::Compiler::MethodReference(&dex_file, method_idx));
454
455 uint32_t frame_size_in_bytes = 0;
456 uint32_t core_spill_mask = 0;
457 uint32_t fp_spill_mask = 0;
458
459 OatMethodOffsets method_offsets =
460 oat_methods_[oat_class_index]->method_offsets_[class_def_method_index];
461
462
463 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700464 uint32_t aligned_code_offset = compiled_method->AlignCode(code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700465 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
466 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800467 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700468 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700469 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700470 << " Expected: " << aligned_code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700471 return false;
472 }
473 code_offset += aligned_code_delta;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700474 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700475 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700476 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700477 const std::vector<uint8_t>& code = compiled_method->GetCode();
478 size_t code_size = code.size() * sizeof(code[0]);
jeffhao55d78212011-11-02 11:41:50 -0700479
480 // Deduplicate code arrays
481 size_t offset = code_offset + compiled_method->CodeDelta();
jeffhaof479dcc2011-11-02 15:54:15 -0700482 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Ian Rogers0571d352011-11-03 19:51:38 -0700483 if (code_iter != code_offsets_.end() && offset != method_offsets.code_offset_) {
484 DCHECK((code_size == 0 && method_offsets.code_offset_ == 0)
485 || code_iter->second == method_offsets.code_offset_)
486 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700487 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700488 DCHECK((code_size == 0 && method_offsets.code_offset_ == 0)
489 || offset == method_offsets.code_offset_)
490 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700491 if (!file->WriteFully(&code[0], code_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700492 ReportWriteFailure("method code", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700493 return false;
494 }
495 code_offset += code_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700496 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700497 DCHECK_CODE_OFFSET();
Ian Rogers0571d352011-11-03 19:51:38 -0700498 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
499 core_spill_mask = compiled_method->GetCoreSpillMask();
500 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700501 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700502
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700503 if (!file->WriteFully(&frame_size_in_bytes, sizeof(frame_size_in_bytes))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700504 ReportWriteFailure("method frame size", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700505 return false;
506 }
507 code_offset += sizeof(frame_size_in_bytes);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700508 if (!file->WriteFully(&core_spill_mask, sizeof(core_spill_mask))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700509 ReportWriteFailure("method core spill mask", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700510 return false;
511 }
512 code_offset += sizeof(core_spill_mask);
513 if (!file->WriteFully(&fp_spill_mask, sizeof(fp_spill_mask))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700514 ReportWriteFailure("method fp spill mask", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700515 return false;
516 }
517 code_offset += sizeof(fp_spill_mask);
518
519 if (compiled_method != NULL) {
520 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
521 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700522
523 // Deduplicate mapping tables
Ian Rogers0571d352011-11-03 19:51:38 -0700524 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
525 mapping_table_offsets_.find(&mapping_table);
526 if (mapping_iter != mapping_table_offsets_.end() &&
527 code_offset != method_offsets.mapping_table_offset_) {
528 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
529 || mapping_iter->second == method_offsets.mapping_table_offset_)
530 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700531 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700532 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
533 || code_offset == method_offsets.mapping_table_offset_)
534 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700535 if (!file->WriteFully(&mapping_table[0], mapping_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700536 ReportWriteFailure("mapping table", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700537 return false;
538 }
539 code_offset += mapping_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700540 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700541 DCHECK_CODE_OFFSET();
542
543 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
544 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700545
546 // Deduplicate vmap tables
Ian Rogers0571d352011-11-03 19:51:38 -0700547 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
548 vmap_table_offsets_.find(&vmap_table);
549 if (vmap_iter != vmap_table_offsets_.end() &&
550 code_offset != method_offsets.vmap_table_offset_) {
551 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
552 || vmap_iter->second == method_offsets.vmap_table_offset_)
553 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700554 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700555 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
556 || code_offset == method_offsets.vmap_table_offset_)
557 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700558 if (!file->WriteFully(&vmap_table[0], vmap_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700559 ReportWriteFailure("vmap table", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700560 return false;
561 }
562 code_offset += vmap_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700563 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700564 DCHECK_CODE_OFFSET();
565 }
Ian Rogers0571d352011-11-03 19:51:38 -0700566 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
567 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700568 if (compiled_invoke_stub != NULL) {
569 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
570 compiler_->GetInstructionSet());
571 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
572 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800573 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700574 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
575 PLOG(ERROR) << "Failed to seek to align invoke stub code. Actual: " << new_offset
576 << " Expected: " << aligned_code_offset;
577 return false;
578 }
579 code_offset += aligned_code_delta;
580 DCHECK_CODE_OFFSET();
581 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700582 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700583 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
584 size_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
jeffhao55d78212011-11-02 11:41:50 -0700585
586 // Deduplicate invoke stubs
Ian Rogers0571d352011-11-03 19:51:38 -0700587 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter =
588 code_offsets_.find(&invoke_stub);
589 if (stub_iter != code_offsets_.end() &&
590 code_offset != method_offsets.invoke_stub_offset_) {
591 DCHECK((invoke_stub_size == 0 && method_offsets.invoke_stub_offset_ == 0)
592 || stub_iter->second == method_offsets.invoke_stub_offset_)
593 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700594 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700595 DCHECK((invoke_stub_size == 0 && method_offsets.invoke_stub_offset_ == 0)
596 || code_offset == method_offsets.invoke_stub_offset_)
597 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700598 if (!file->WriteFully(&invoke_stub[0], invoke_stub_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700599 ReportWriteFailure("invoke stub code", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700600 return false;
601 }
602 code_offset += invoke_stub_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700603 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700604 DCHECK_CODE_OFFSET();
605 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700606 return code_offset;
607}
608
Brian Carlstrome24fa612011-09-29 00:53:55 -0700609OatWriter::OatDexFile::OatDexFile(const DexFile& dex_file) {
Elliott Hughes95572412011-12-13 18:14:20 -0800610 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700611 dex_file_location_size_ = location.size();
612 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
613 dex_file_checksum_ = dex_file.GetHeader().checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800614 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800615 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700616}
617
618size_t OatWriter::OatDexFile::SizeOf() const {
619 return sizeof(dex_file_location_size_)
620 + dex_file_location_size_
621 + sizeof(dex_file_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800622 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800623 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700624}
625
626void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
627 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
628 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
629 oat_header.UpdateChecksum(&dex_file_checksum_, sizeof(dex_file_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800630 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800631 oat_header.UpdateChecksum(&methods_offsets_[0],
632 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700633}
634
635bool OatWriter::OatDexFile::Write(File* file) const {
636 if (!file->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700637 PLOG(ERROR) << "Failed to write dex file location length to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700638 return false;
639 }
640 if (!file->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700641 PLOG(ERROR) << "Failed to write dex file location data to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700642 return false;
643 }
644 if (!file->WriteFully(&dex_file_checksum_, sizeof(dex_file_checksum_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700645 PLOG(ERROR) << "Failed to write dex file checksum to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700646 return false;
647 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800648 if (!file->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
649 PLOG(ERROR) << "Failed to write dex file offset to " << file->name();
650 return false;
651 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800652 if (!file->WriteFully(&methods_offsets_[0],
653 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700654 PLOG(ERROR) << "Failed to write methods offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700655 return false;
656 }
657 return true;
658}
659
660OatWriter::OatMethods::OatMethods(uint32_t methods_count) {
661 method_offsets_.resize(methods_count);
662}
663
664size_t OatWriter::OatMethods::SizeOf() const {
665 return (sizeof(method_offsets_[0]) * method_offsets_.size());
666}
667
668void OatWriter::OatMethods::UpdateChecksum(OatHeader& oat_header) const {
669 oat_header.UpdateChecksum(&method_offsets_[0], SizeOf());
670}
671
672bool OatWriter::OatMethods::Write(File* file) const {
673 if (!file->WriteFully(&method_offsets_[0], SizeOf())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700674 PLOG(ERROR) << "Failed to write method offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700675 return false;
676 }
677 return true;
678}
679
680} // namespace art