blob: 806ac71e7e7b5ecbf5e2634a2c4cf5dfb5fbf21a [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 Carlstrom0755ec52012-01-11 15:19:46 -080099
100 CompiledClass* compiled_class =
101 compiler_->GetCompiledClass(art::Compiler::MethodReference(dex_file, class_def_index));
102 Class::Status status =
103 (compiled_class != NULL) ? compiled_class->GetStatus() : Class::kStatusNotReady;
104
105 OatClass* oat_class = new OatClass(status, num_methods);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800106 oat_classes_.push_back(oat_class);
107 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700108 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800109 oat_dex_files_[i]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700110 }
111 return offset;
112}
113
114size_t OatWriter::InitOatCode(size_t offset) {
115 // calculate the offsets within OatHeader to executable code
116 size_t old_offset = offset;
117 // required to be on a new page boundary
118 offset = RoundUp(offset, kPageSize);
119 oat_header_->SetExecutableOffset(offset);
120 executable_offset_padding_length_ = offset - old_offset;
121 return offset;
122}
123
124size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700125 size_t oat_class_index = 0;
126 for (size_t i = 0; i != dex_files_->size(); ++i) {
127 const DexFile* dex_file = (*dex_files_)[i];
128 CHECK(dex_file != NULL);
129 offset = InitOatCodeDexFile(offset, oat_class_index, *dex_file);
130 }
131 return offset;
132}
133
134size_t OatWriter::InitOatCodeDexFile(size_t offset,
135 size_t& oat_class_index,
136 const DexFile& dex_file) {
Brian Carlstrom389efb02012-01-11 12:06:26 -0800137 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700138 class_def_index < dex_file.NumClassDefs();
139 class_def_index++, oat_class_index++) {
140 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
141 offset = InitOatCodeClassDef(offset, oat_class_index, dex_file, class_def);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800142 oat_classes_[oat_class_index]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700143 }
144 return offset;
145}
146
147size_t OatWriter::InitOatCodeClassDef(size_t offset,
148 size_t oat_class_index,
149 const DexFile& dex_file,
150 const DexFile::ClassDef& class_def) {
151 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700152 if (class_data == NULL) {
153 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700154 return offset;
155 }
Ian Rogers0571d352011-11-03 19:51:38 -0700156 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800157 CHECK_EQ(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700158 it.NumDirectMethods() + it.NumVirtualMethods());
159 // Skip fields
160 while (it.HasNextStaticField()) {
161 it.Next();
162 }
163 while (it.HasNextInstanceField()) {
164 it.Next();
165 }
166 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700167 size_t class_def_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700168 while (it.HasNextDirectMethod()) {
169 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
170 offset = InitOatCodeMethod(offset, oat_class_index, class_def_method_index, is_static, true,
171 it.GetMemberIndex(), &dex_file);
172 class_def_method_index++;
173 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700174 }
Ian Rogers0571d352011-11-03 19:51:38 -0700175 while (it.HasNextVirtualMethod()) {
176 CHECK_EQ(it.GetMemberAccessFlags() & kAccStatic, 0U);
177 offset = InitOatCodeMethod(offset, oat_class_index, class_def_method_index, false, false,
178 it.GetMemberIndex(), &dex_file);
179 class_def_method_index++;
180 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700181 }
Ian Rogers0571d352011-11-03 19:51:38 -0700182 DCHECK(!it.HasNext());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700183 return offset;
184}
185
Ian Rogers0571d352011-11-03 19:51:38 -0700186size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
187 size_t class_def_method_index, bool is_static, bool is_direct,
188 uint32_t method_idx, const DexFile* dex_file) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700189 // derived from CompiledMethod if available
190 uint32_t code_offset = 0;
191 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700192 uint32_t core_spill_mask = 0;
193 uint32_t fp_spill_mask = 0;
194 uint32_t mapping_table_offset = 0;
195 uint32_t vmap_table_offset = 0;
196 // derived from CompiledInvokeStub if available
197 uint32_t invoke_stub_offset = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700198
Ian Rogers0571d352011-11-03 19:51:38 -0700199 CompiledMethod* compiled_method =
200 compiler_->GetCompiledMethod(art::Compiler::MethodReference(dex_file, method_idx));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700201 if (compiled_method != NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700202 offset = compiled_method->AlignCode(offset);
Elliott Hughes06b37d92011-10-16 11:51:29 -0700203 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700204 const std::vector<uint8_t>& code = compiled_method->GetCode();
205 size_t code_size = code.size() * sizeof(code[0]);
206 uint32_t thumb_offset = compiled_method->CodeDelta();
207 code_offset = (code_size == 0) ? 0 : offset + thumb_offset;
jeffhao55d78212011-11-02 11:41:50 -0700208
209 // Deduplicate code arrays
jeffhaof479dcc2011-11-02 15:54:15 -0700210 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
jeffhao55d78212011-11-02 11:41:50 -0700211 if (code_iter != code_offsets_.end()) {
212 code_offset = code_iter->second;
213 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700214 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&code, code_offset));
jeffhao55d78212011-11-02 11:41:50 -0700215 offset += code_size;
216 oat_header_->UpdateChecksum(&code[0], code_size);
217 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700218
219 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700220 core_spill_mask = compiled_method->GetCoreSpillMask();
221 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700222 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700223
224 offset += sizeof(frame_size_in_bytes);
225 oat_header_->UpdateChecksum(&frame_size_in_bytes, sizeof(frame_size_in_bytes));
226
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700227 offset += sizeof(core_spill_mask);
228 oat_header_->UpdateChecksum(&core_spill_mask, sizeof(core_spill_mask));
229
230 offset += sizeof(fp_spill_mask);
231 oat_header_->UpdateChecksum(&fp_spill_mask, sizeof(fp_spill_mask));
232
233 if (compiled_method != NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700234 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
235 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
236 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700237
238 // Deduplicate mapping tables
jeffhaof479dcc2011-11-02 15:54:15 -0700239 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
jeffhao55d78212011-11-02 11:41:50 -0700240 if (mapping_iter != mapping_table_offsets_.end()) {
241 mapping_table_offset = mapping_iter->second;
242 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700243 mapping_table_offsets_.insert(std::pair<const std::vector<uint32_t>*, uint32_t>(&mapping_table, mapping_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700244 offset += mapping_table_size;
245 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
246 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700247
248 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
249 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
250 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700251
252 // Deduplicate vmap tables
jeffhaof479dcc2011-11-02 15:54:15 -0700253 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
jeffhao55d78212011-11-02 11:41:50 -0700254 if (vmap_iter != vmap_table_offsets_.end()) {
255 vmap_table_offset = vmap_iter->second;
256 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700257 vmap_table_offsets_.insert(std::pair<const std::vector<uint16_t>*, uint32_t>(&vmap_table, vmap_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700258 offset += vmap_table_size;
259 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
260 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700261 }
262
Ian Rogers0571d352011-11-03 19:51:38 -0700263 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx));
264 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700265 if (compiled_invoke_stub != NULL) {
266 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
Elliott Hughes06b37d92011-10-16 11:51:29 -0700267 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700268 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
269 size_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
270 invoke_stub_offset = (invoke_stub_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700271
272 // Deduplicate invoke stubs
jeffhaof479dcc2011-11-02 15:54:15 -0700273 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&invoke_stub);
jeffhao55d78212011-11-02 11:41:50 -0700274 if (stub_iter != code_offsets_.end()) {
275 invoke_stub_offset = stub_iter->second;
276 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700277 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&invoke_stub, invoke_stub_offset));
jeffhao55d78212011-11-02 11:41:50 -0700278 offset += invoke_stub_size;
279 oat_header_->UpdateChecksum(&invoke_stub[0], invoke_stub_size);
280 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700281 }
282
Brian Carlstrom389efb02012-01-11 12:06:26 -0800283 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index]
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700284 = OatMethodOffsets(code_offset,
285 frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700286 core_spill_mask,
287 fp_spill_mask,
288 mapping_table_offset,
289 vmap_table_offset,
290 invoke_stub_offset);
291
Ian Rogers0571d352011-11-03 19:51:38 -0700292 if (compiler_->IsImage()) {
293 ClassLinker* linker = Runtime::Current()->GetClassLinker();
294 DexCache* dex_cache = linker->FindDexCache(*dex_file);
295 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader_,
296 is_direct);
297 CHECK(method != NULL);
298 method->SetFrameSizeInBytes(frame_size_in_bytes);
299 method->SetCoreSpillMask(core_spill_mask);
300 method->SetFpSpillMask(fp_spill_mask);
301 method->SetOatMappingTableOffset(mapping_table_offset);
302 method->SetOatCodeOffset(code_offset);
303 method->SetOatVmapTableOffset(vmap_table_offset);
304 method->SetOatInvokeStubOffset(invoke_stub_offset);
305 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700306 return offset;
307}
308
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700309#define DCHECK_CODE_OFFSET() \
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800310 DCHECK_EQ(static_cast<off_t>(code_offset), lseek(file->Fd(), 0, SEEK_CUR))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700311
Elliott Hughes234da572011-11-03 22:13:06 -0700312bool OatWriter::Write(File* file) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700313 if (!file->WriteFully(oat_header_, sizeof(*oat_header_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700314 PLOG(ERROR) << "Failed to write oat header to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700315 return false;
316 }
317
Elliott Hughes234da572011-11-03 22:13:06 -0700318 if (!WriteTables(file)) {
319 LOG(ERROR) << "Failed to write oat tables to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700320 return false;
321 }
322
Elliott Hughes234da572011-11-03 22:13:06 -0700323 size_t code_offset = WriteCode(file);
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 to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700326 return false;
327 }
328
Elliott Hughes234da572011-11-03 22:13:06 -0700329 code_offset = WriteCodeDexFiles(file, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700330 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700331 LOG(ERROR) << "Failed to write oat code for dex files to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700332 return false;
333 }
334
335 return true;
336}
337
338bool OatWriter::WriteTables(File* file) {
339 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
340 if (!oat_dex_files_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700341 PLOG(ERROR) << "Failed to write oat dex information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700342 return false;
343 }
344 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800345 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
346 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800347 off_t actual_offset = lseek(file->Fd(), expected_offset, SEEK_SET);
Brian Carlstrom89521892011-12-07 22:05:07 -0800348 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
349 const DexFile* dex_file = (*dex_files_)[i];
350 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
351 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
352 return false;
353 }
354 const DexFile* dex_file = (*dex_files_)[i];
355 if (!file->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
356 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << file->name();
357 return false;
358 }
359 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800360 for (size_t i = 0; i != oat_classes_.size(); ++i) {
361 if (!oat_classes_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700362 PLOG(ERROR) << "Failed to write oat methods information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700363 return false;
364 }
365 }
366 return true;
367}
368
369size_t OatWriter::WriteCode(File* file) {
370 uint32_t code_offset = oat_header_->GetExecutableOffset();
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800371 off_t new_offset = lseek(file->Fd(), executable_offset_padding_length_, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700372 if (static_cast<uint32_t>(new_offset) != code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700373 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700374 << " Expected: " << code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700375 return 0;
376 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700377 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700378 return code_offset;
379}
380
381size_t OatWriter::WriteCodeDexFiles(File* file, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700382 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800383 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700384 const DexFile* dex_file = (*dex_files_)[i];
385 CHECK(dex_file != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700386 code_offset = WriteCodeDexFile(file, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700387 if (code_offset == 0) {
388 return 0;
389 }
390 }
391 return code_offset;
392}
393
Ian Rogers0571d352011-11-03 19:51:38 -0700394size_t OatWriter::WriteCodeDexFile(File* file, size_t code_offset, size_t& oat_class_index,
395 const DexFile& dex_file) {
396 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
397 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700398 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700399 code_offset = WriteCodeClassDef(file, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700400 if (code_offset == 0) {
401 return 0;
402 }
403 }
404 return code_offset;
405}
406
Ian Rogers0571d352011-11-03 19:51:38 -0700407void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
408 const DexFile& dex_file, File* f) const {
409 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
410 << " to " << f->name();
Elliott Hughes234da572011-11-03 22:13:06 -0700411}
412
Brian Carlstrome24fa612011-09-29 00:53:55 -0700413size_t OatWriter::WriteCodeClassDef(File* file,
Ian Rogers0571d352011-11-03 19:51:38 -0700414 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700415 const DexFile& dex_file,
416 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700417 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700418 if (class_data == NULL) {
419 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700420 return code_offset;
421 }
Ian Rogers0571d352011-11-03 19:51:38 -0700422 ClassDataItemIterator it(dex_file, class_data);
423 // Skip fields
424 while (it.HasNextStaticField()) {
425 it.Next();
426 }
427 while (it.HasNextInstanceField()) {
428 it.Next();
429 }
430 // Process methods
431 size_t class_def_method_index = 0;
432 while (it.HasNextDirectMethod()) {
433 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
434 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
435 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700436 if (code_offset == 0) {
437 return 0;
438 }
Ian Rogers0571d352011-11-03 19:51:38 -0700439 class_def_method_index++;
440 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700441 }
Ian Rogers0571d352011-11-03 19:51:38 -0700442 while (it.HasNextVirtualMethod()) {
443 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
444 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700445 if (code_offset == 0) {
446 return 0;
447 }
Ian Rogers0571d352011-11-03 19:51:38 -0700448 class_def_method_index++;
449 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700450 }
451 return code_offset;
452}
453
Ian Rogers0571d352011-11-03 19:51:38 -0700454size_t OatWriter::WriteCodeMethod(File* file, size_t code_offset, size_t oat_class_index,
455 size_t class_def_method_index, bool is_static,
456 uint32_t method_idx, const DexFile& dex_file) {
457 const CompiledMethod* compiled_method =
458 compiler_->GetCompiledMethod(art::Compiler::MethodReference(&dex_file, method_idx));
459
460 uint32_t frame_size_in_bytes = 0;
461 uint32_t core_spill_mask = 0;
462 uint32_t fp_spill_mask = 0;
463
464 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800465 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700466
467
468 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700469 uint32_t aligned_code_offset = compiled_method->AlignCode(code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700470 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
471 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800472 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700473 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700474 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700475 << " Expected: " << aligned_code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700476 return false;
477 }
478 code_offset += aligned_code_delta;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700479 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700480 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700481 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700482 const std::vector<uint8_t>& code = compiled_method->GetCode();
483 size_t code_size = code.size() * sizeof(code[0]);
jeffhao55d78212011-11-02 11:41:50 -0700484
485 // Deduplicate code arrays
486 size_t offset = code_offset + compiled_method->CodeDelta();
jeffhaof479dcc2011-11-02 15:54:15 -0700487 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Ian Rogers0571d352011-11-03 19:51:38 -0700488 if (code_iter != code_offsets_.end() && offset != method_offsets.code_offset_) {
489 DCHECK((code_size == 0 && method_offsets.code_offset_ == 0)
490 || code_iter->second == method_offsets.code_offset_)
491 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700492 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700493 DCHECK((code_size == 0 && method_offsets.code_offset_ == 0)
494 || offset == method_offsets.code_offset_)
495 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700496 if (!file->WriteFully(&code[0], code_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700497 ReportWriteFailure("method code", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700498 return false;
499 }
500 code_offset += code_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700501 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700502 DCHECK_CODE_OFFSET();
Ian Rogers0571d352011-11-03 19:51:38 -0700503 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
504 core_spill_mask = compiled_method->GetCoreSpillMask();
505 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700506 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700507
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700508 if (!file->WriteFully(&frame_size_in_bytes, sizeof(frame_size_in_bytes))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700509 ReportWriteFailure("method frame size", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700510 return false;
511 }
512 code_offset += sizeof(frame_size_in_bytes);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700513 if (!file->WriteFully(&core_spill_mask, sizeof(core_spill_mask))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700514 ReportWriteFailure("method core spill mask", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700515 return false;
516 }
517 code_offset += sizeof(core_spill_mask);
518 if (!file->WriteFully(&fp_spill_mask, sizeof(fp_spill_mask))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700519 ReportWriteFailure("method fp spill mask", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700520 return false;
521 }
522 code_offset += sizeof(fp_spill_mask);
523
524 if (compiled_method != NULL) {
525 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
526 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700527
528 // Deduplicate mapping tables
Ian Rogers0571d352011-11-03 19:51:38 -0700529 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
530 mapping_table_offsets_.find(&mapping_table);
531 if (mapping_iter != mapping_table_offsets_.end() &&
532 code_offset != method_offsets.mapping_table_offset_) {
533 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
534 || mapping_iter->second == method_offsets.mapping_table_offset_)
535 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700536 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700537 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
538 || code_offset == method_offsets.mapping_table_offset_)
539 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700540 if (!file->WriteFully(&mapping_table[0], mapping_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700541 ReportWriteFailure("mapping table", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700542 return false;
543 }
544 code_offset += mapping_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700545 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700546 DCHECK_CODE_OFFSET();
547
548 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
549 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700550
551 // Deduplicate vmap tables
Ian Rogers0571d352011-11-03 19:51:38 -0700552 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
553 vmap_table_offsets_.find(&vmap_table);
554 if (vmap_iter != vmap_table_offsets_.end() &&
555 code_offset != method_offsets.vmap_table_offset_) {
556 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
557 || vmap_iter->second == method_offsets.vmap_table_offset_)
558 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700559 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700560 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
561 || code_offset == method_offsets.vmap_table_offset_)
562 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700563 if (!file->WriteFully(&vmap_table[0], vmap_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700564 ReportWriteFailure("vmap table", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700565 return false;
566 }
567 code_offset += vmap_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700568 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700569 DCHECK_CODE_OFFSET();
570 }
Ian Rogers0571d352011-11-03 19:51:38 -0700571 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
572 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700573 if (compiled_invoke_stub != NULL) {
574 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
575 compiler_->GetInstructionSet());
576 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
577 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800578 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700579 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
580 PLOG(ERROR) << "Failed to seek to align invoke stub code. Actual: " << new_offset
581 << " Expected: " << aligned_code_offset;
582 return false;
583 }
584 code_offset += aligned_code_delta;
585 DCHECK_CODE_OFFSET();
586 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700587 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700588 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
589 size_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
jeffhao55d78212011-11-02 11:41:50 -0700590
591 // Deduplicate invoke stubs
Ian Rogers0571d352011-11-03 19:51:38 -0700592 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter =
593 code_offsets_.find(&invoke_stub);
594 if (stub_iter != code_offsets_.end() &&
595 code_offset != method_offsets.invoke_stub_offset_) {
596 DCHECK((invoke_stub_size == 0 && method_offsets.invoke_stub_offset_ == 0)
597 || stub_iter->second == method_offsets.invoke_stub_offset_)
598 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700599 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700600 DCHECK((invoke_stub_size == 0 && method_offsets.invoke_stub_offset_ == 0)
601 || code_offset == method_offsets.invoke_stub_offset_)
602 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700603 if (!file->WriteFully(&invoke_stub[0], invoke_stub_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700604 ReportWriteFailure("invoke stub code", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700605 return false;
606 }
607 code_offset += invoke_stub_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700608 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700609 DCHECK_CODE_OFFSET();
610 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700611 return code_offset;
612}
613
Brian Carlstrome24fa612011-09-29 00:53:55 -0700614OatWriter::OatDexFile::OatDexFile(const DexFile& dex_file) {
Elliott Hughes95572412011-12-13 18:14:20 -0800615 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700616 dex_file_location_size_ = location.size();
617 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
618 dex_file_checksum_ = dex_file.GetHeader().checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800619 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800620 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700621}
622
623size_t OatWriter::OatDexFile::SizeOf() const {
624 return sizeof(dex_file_location_size_)
625 + dex_file_location_size_
626 + sizeof(dex_file_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800627 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800628 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700629}
630
631void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
632 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
633 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
634 oat_header.UpdateChecksum(&dex_file_checksum_, sizeof(dex_file_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800635 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800636 oat_header.UpdateChecksum(&methods_offsets_[0],
637 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700638}
639
640bool OatWriter::OatDexFile::Write(File* file) const {
641 if (!file->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700642 PLOG(ERROR) << "Failed to write dex file location length to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700643 return false;
644 }
645 if (!file->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700646 PLOG(ERROR) << "Failed to write dex file location data to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700647 return false;
648 }
649 if (!file->WriteFully(&dex_file_checksum_, sizeof(dex_file_checksum_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700650 PLOG(ERROR) << "Failed to write dex file checksum to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700651 return false;
652 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800653 if (!file->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
654 PLOG(ERROR) << "Failed to write dex file offset to " << file->name();
655 return false;
656 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800657 if (!file->WriteFully(&methods_offsets_[0],
658 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700659 PLOG(ERROR) << "Failed to write methods offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700660 return false;
661 }
662 return true;
663}
664
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800665OatWriter::OatClass::OatClass(Class::Status status, uint32_t methods_count) {
666 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700667 method_offsets_.resize(methods_count);
668}
669
Brian Carlstrom389efb02012-01-11 12:06:26 -0800670size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800671 return sizeof(status_)
672 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700673}
674
Brian Carlstrom389efb02012-01-11 12:06:26 -0800675void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800676 oat_header.UpdateChecksum(&status_, sizeof(status_));
677 oat_header.UpdateChecksum(&method_offsets_[0],
678 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700679}
680
Brian Carlstrom389efb02012-01-11 12:06:26 -0800681bool OatWriter::OatClass::Write(File* file) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800682 if (!file->WriteFully(&status_, sizeof(status_))) {
683 PLOG(ERROR) << "Failed to write class status to " << file->name();
684 return false;
685 }
686 if (!file->WriteFully(&method_offsets_[0],
687 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700688 PLOG(ERROR) << "Failed to write method offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700689 return false;
690 }
691 return true;
692}
693
694} // namespace art