blob: 8b197275436e37cfe7f4d2c2d03f9c81b4428a0d [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 for (size_t i = 0; i != dex_files_->size(); ++i) {
84 const DexFile* dex_file = (*dex_files_)[i];
85 for (size_t class_def_index = 0;
86 class_def_index < dex_file->NumClassDefs();
Ian Rogersc20a83e2012-01-18 18:15:32 -080087 class_def_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -080088 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -070089 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
90 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -070091 uint32_t num_methods = 0;
92 if (class_data != NULL) { // ie not an empty class, such as a marker interface
93 ClassDataItemIterator it(*dex_file, class_data);
94 size_t num_direct_methods = it.NumDirectMethods();
95 size_t num_virtual_methods = it.NumVirtualMethods();
96 num_methods = num_direct_methods + num_virtual_methods;
97 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -080098
99 CompiledClass* compiled_class =
100 compiler_->GetCompiledClass(art::Compiler::MethodReference(dex_file, class_def_index));
101 Class::Status status =
102 (compiled_class != NULL) ? compiled_class->GetStatus() : Class::kStatusNotReady;
103
104 OatClass* oat_class = new OatClass(status, num_methods);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800105 oat_classes_.push_back(oat_class);
106 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700107 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800108 oat_dex_files_[i]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700109 }
110 return offset;
111}
112
113size_t OatWriter::InitOatCode(size_t offset) {
114 // calculate the offsets within OatHeader to executable code
115 size_t old_offset = offset;
116 // required to be on a new page boundary
117 offset = RoundUp(offset, kPageSize);
118 oat_header_->SetExecutableOffset(offset);
119 executable_offset_padding_length_ = offset - old_offset;
120 return offset;
121}
122
123size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700124 size_t oat_class_index = 0;
125 for (size_t i = 0; i != dex_files_->size(); ++i) {
126 const DexFile* dex_file = (*dex_files_)[i];
127 CHECK(dex_file != NULL);
128 offset = InitOatCodeDexFile(offset, oat_class_index, *dex_file);
129 }
130 return offset;
131}
132
133size_t OatWriter::InitOatCodeDexFile(size_t offset,
134 size_t& oat_class_index,
135 const DexFile& dex_file) {
Brian Carlstrom389efb02012-01-11 12:06:26 -0800136 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700137 class_def_index < dex_file.NumClassDefs();
138 class_def_index++, oat_class_index++) {
139 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800140 offset = InitOatCodeClassDef(offset, oat_class_index, class_def_index, dex_file, class_def);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800141 oat_classes_[oat_class_index]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700142 }
143 return offset;
144}
145
146size_t OatWriter::InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800147 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700148 const DexFile& dex_file,
149 const DexFile::ClassDef& class_def) {
150 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700151 if (class_data == NULL) {
152 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700153 return offset;
154 }
Ian Rogers0571d352011-11-03 19:51:38 -0700155 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800156 CHECK_EQ(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700157 it.NumDirectMethods() + it.NumVirtualMethods());
158 // Skip fields
159 while (it.HasNextStaticField()) {
160 it.Next();
161 }
162 while (it.HasNextInstanceField()) {
163 it.Next();
164 }
165 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700166 size_t class_def_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700167 while (it.HasNextDirectMethod()) {
168 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
Ian Rogersc20a83e2012-01-18 18:15:32 -0800169 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
170 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index, is_native,
171 is_static, true, it.GetMemberIndex(), &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700172 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);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800177 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
178 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index, is_native,
179 false, false, it.GetMemberIndex(), &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700180 class_def_method_index++;
181 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700182 }
Ian Rogers0571d352011-11-03 19:51:38 -0700183 DCHECK(!it.HasNext());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700184 return offset;
185}
186
Ian Rogersc20a83e2012-01-18 18:15:32 -0800187size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index, size_t class_def_index,
188 size_t class_def_method_index, bool is_native, bool is_static,
189 bool is_direct, uint32_t method_idx, const DexFile* dex_file) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700190 // derived from CompiledMethod if available
191 uint32_t code_offset = 0;
192 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700193 uint32_t core_spill_mask = 0;
194 uint32_t fp_spill_mask = 0;
195 uint32_t mapping_table_offset = 0;
196 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800197 uint32_t gc_map_offset = 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700198 // derived from CompiledInvokeStub if available
199 uint32_t invoke_stub_offset = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700200
Ian Rogers0571d352011-11-03 19:51:38 -0700201 CompiledMethod* compiled_method =
202 compiler_->GetCompiledMethod(art::Compiler::MethodReference(dex_file, method_idx));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700203 if (compiled_method != NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700204 offset = compiled_method->AlignCode(offset);
Elliott Hughes06b37d92011-10-16 11:51:29 -0700205 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700206 const std::vector<uint8_t>& code = compiled_method->GetCode();
207 size_t code_size = code.size() * sizeof(code[0]);
208 uint32_t thumb_offset = compiled_method->CodeDelta();
209 code_offset = (code_size == 0) ? 0 : offset + thumb_offset;
jeffhao55d78212011-11-02 11:41:50 -0700210
211 // Deduplicate code arrays
jeffhaof479dcc2011-11-02 15:54:15 -0700212 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
jeffhao55d78212011-11-02 11:41:50 -0700213 if (code_iter != code_offsets_.end()) {
214 code_offset = code_iter->second;
215 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700216 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&code, code_offset));
jeffhao55d78212011-11-02 11:41:50 -0700217 offset += code_size;
218 oat_header_->UpdateChecksum(&code[0], code_size);
219 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700220
221 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700222 core_spill_mask = compiled_method->GetCoreSpillMask();
223 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700224 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700225
226 offset += sizeof(frame_size_in_bytes);
227 oat_header_->UpdateChecksum(&frame_size_in_bytes, sizeof(frame_size_in_bytes));
228
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700229 offset += sizeof(core_spill_mask);
230 oat_header_->UpdateChecksum(&core_spill_mask, sizeof(core_spill_mask));
231
232 offset += sizeof(fp_spill_mask);
233 oat_header_->UpdateChecksum(&fp_spill_mask, sizeof(fp_spill_mask));
234
235 if (compiled_method != NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700236 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
237 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
238 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700239
240 // Deduplicate mapping tables
jeffhaof479dcc2011-11-02 15:54:15 -0700241 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
jeffhao55d78212011-11-02 11:41:50 -0700242 if (mapping_iter != mapping_table_offsets_.end()) {
243 mapping_table_offset = mapping_iter->second;
244 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700245 mapping_table_offsets_.insert(std::pair<const std::vector<uint32_t>*, uint32_t>(&mapping_table, mapping_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700246 offset += mapping_table_size;
247 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
248 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700249
250 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
251 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
252 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700253
254 // Deduplicate vmap tables
jeffhaof479dcc2011-11-02 15:54:15 -0700255 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
jeffhao55d78212011-11-02 11:41:50 -0700256 if (vmap_iter != vmap_table_offsets_.end()) {
257 vmap_table_offset = vmap_iter->second;
258 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700259 vmap_table_offsets_.insert(std::pair<const std::vector<uint16_t>*, uint32_t>(&vmap_table, vmap_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700260 offset += vmap_table_size;
261 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
262 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800263
264 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
265 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
266 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
267
Ian Rogersc20a83e2012-01-18 18:15:32 -0800268#ifndef NDEBUG
269 // We expect GC maps except when the class hasn't been verified or the method is native
270 CompiledClass* compiled_class =
271 compiler_->GetCompiledClass(art::Compiler::MethodReference(dex_file, class_def_index));
272 Class::Status status =
273 (compiled_class != NULL) ? compiled_class->GetStatus() : Class::kStatusNotReady;
274 CHECK(gc_map_size != 0 || is_native || status < Class::kStatusVerified)
275 << PrettyMethod(method_idx, *dex_file);
276#endif
277
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800278 // Deduplicate GC maps
279 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = gc_map_offsets_.find(&gc_map);
280 if (gc_map_iter != gc_map_offsets_.end()) {
281 gc_map_offset = gc_map_iter->second;
282 } else {
283 gc_map_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&gc_map, gc_map_offset));
284 offset += gc_map_size;
285 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
286 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700287 }
288
Ian Rogers0571d352011-11-03 19:51:38 -0700289 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx));
290 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700291 if (compiled_invoke_stub != NULL) {
292 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
Elliott Hughes06b37d92011-10-16 11:51:29 -0700293 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700294 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
295 size_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
296 invoke_stub_offset = (invoke_stub_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700297
298 // Deduplicate invoke stubs
jeffhaof479dcc2011-11-02 15:54:15 -0700299 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&invoke_stub);
jeffhao55d78212011-11-02 11:41:50 -0700300 if (stub_iter != code_offsets_.end()) {
301 invoke_stub_offset = stub_iter->second;
302 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700303 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&invoke_stub, invoke_stub_offset));
jeffhao55d78212011-11-02 11:41:50 -0700304 offset += invoke_stub_size;
305 oat_header_->UpdateChecksum(&invoke_stub[0], invoke_stub_size);
306 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700307 }
308
Brian Carlstrom389efb02012-01-11 12:06:26 -0800309 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index]
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700310 = OatMethodOffsets(code_offset,
311 frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700312 core_spill_mask,
313 fp_spill_mask,
314 mapping_table_offset,
315 vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800316 gc_map_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700317 invoke_stub_offset);
318
Ian Rogers0571d352011-11-03 19:51:38 -0700319 if (compiler_->IsImage()) {
320 ClassLinker* linker = Runtime::Current()->GetClassLinker();
321 DexCache* dex_cache = linker->FindDexCache(*dex_file);
322 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader_,
323 is_direct);
324 CHECK(method != NULL);
325 method->SetFrameSizeInBytes(frame_size_in_bytes);
326 method->SetCoreSpillMask(core_spill_mask);
327 method->SetFpSpillMask(fp_spill_mask);
328 method->SetOatMappingTableOffset(mapping_table_offset);
329 method->SetOatCodeOffset(code_offset);
330 method->SetOatVmapTableOffset(vmap_table_offset);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800331 method->SetOatGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700332 method->SetOatInvokeStubOffset(invoke_stub_offset);
333 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700334 return offset;
335}
336
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700337#define DCHECK_CODE_OFFSET() \
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800338 DCHECK_EQ(static_cast<off_t>(code_offset), lseek(file->Fd(), 0, SEEK_CUR))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700339
Elliott Hughes234da572011-11-03 22:13:06 -0700340bool OatWriter::Write(File* file) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700341 if (!file->WriteFully(oat_header_, sizeof(*oat_header_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700342 PLOG(ERROR) << "Failed to write oat header to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700343 return false;
344 }
345
Elliott Hughes234da572011-11-03 22:13:06 -0700346 if (!WriteTables(file)) {
347 LOG(ERROR) << "Failed to write oat tables to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700348 return false;
349 }
350
Elliott Hughes234da572011-11-03 22:13:06 -0700351 size_t code_offset = WriteCode(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700352 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700353 LOG(ERROR) << "Failed to write oat code to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700354 return false;
355 }
356
Elliott Hughes234da572011-11-03 22:13:06 -0700357 code_offset = WriteCodeDexFiles(file, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700358 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700359 LOG(ERROR) << "Failed to write oat code for dex files to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700360 return false;
361 }
362
363 return true;
364}
365
366bool OatWriter::WriteTables(File* file) {
367 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
368 if (!oat_dex_files_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700369 PLOG(ERROR) << "Failed to write oat dex information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700370 return false;
371 }
372 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800373 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
374 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800375 off_t actual_offset = lseek(file->Fd(), expected_offset, SEEK_SET);
Brian Carlstrom89521892011-12-07 22:05:07 -0800376 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
377 const DexFile* dex_file = (*dex_files_)[i];
378 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
379 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
380 return false;
381 }
382 const DexFile* dex_file = (*dex_files_)[i];
383 if (!file->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
384 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << file->name();
385 return false;
386 }
387 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800388 for (size_t i = 0; i != oat_classes_.size(); ++i) {
389 if (!oat_classes_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700390 PLOG(ERROR) << "Failed to write oat methods information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700391 return false;
392 }
393 }
394 return true;
395}
396
397size_t OatWriter::WriteCode(File* file) {
398 uint32_t code_offset = oat_header_->GetExecutableOffset();
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800399 off_t new_offset = lseek(file->Fd(), executable_offset_padding_length_, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700400 if (static_cast<uint32_t>(new_offset) != code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700401 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700402 << " Expected: " << code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700403 return 0;
404 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700405 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700406 return code_offset;
407}
408
409size_t OatWriter::WriteCodeDexFiles(File* file, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700410 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800411 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700412 const DexFile* dex_file = (*dex_files_)[i];
413 CHECK(dex_file != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700414 code_offset = WriteCodeDexFile(file, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700415 if (code_offset == 0) {
416 return 0;
417 }
418 }
419 return code_offset;
420}
421
Ian Rogers0571d352011-11-03 19:51:38 -0700422size_t OatWriter::WriteCodeDexFile(File* file, size_t code_offset, size_t& oat_class_index,
423 const DexFile& dex_file) {
424 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
425 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700426 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700427 code_offset = WriteCodeClassDef(file, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700428 if (code_offset == 0) {
429 return 0;
430 }
431 }
432 return code_offset;
433}
434
Ian Rogers0571d352011-11-03 19:51:38 -0700435void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
436 const DexFile& dex_file, File* f) const {
437 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
438 << " to " << f->name();
Elliott Hughes234da572011-11-03 22:13:06 -0700439}
440
Brian Carlstrome24fa612011-09-29 00:53:55 -0700441size_t OatWriter::WriteCodeClassDef(File* file,
Ian Rogers0571d352011-11-03 19:51:38 -0700442 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700443 const DexFile& dex_file,
444 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700445 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700446 if (class_data == NULL) {
447 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700448 return code_offset;
449 }
Ian Rogers0571d352011-11-03 19:51:38 -0700450 ClassDataItemIterator it(dex_file, class_data);
451 // Skip fields
452 while (it.HasNextStaticField()) {
453 it.Next();
454 }
455 while (it.HasNextInstanceField()) {
456 it.Next();
457 }
458 // Process methods
459 size_t class_def_method_index = 0;
460 while (it.HasNextDirectMethod()) {
461 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
462 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
463 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700464 if (code_offset == 0) {
465 return 0;
466 }
Ian Rogers0571d352011-11-03 19:51:38 -0700467 class_def_method_index++;
468 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700469 }
Ian Rogers0571d352011-11-03 19:51:38 -0700470 while (it.HasNextVirtualMethod()) {
471 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
472 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700473 if (code_offset == 0) {
474 return 0;
475 }
Ian Rogers0571d352011-11-03 19:51:38 -0700476 class_def_method_index++;
477 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700478 }
479 return code_offset;
480}
481
Ian Rogers0571d352011-11-03 19:51:38 -0700482size_t OatWriter::WriteCodeMethod(File* file, size_t code_offset, size_t oat_class_index,
483 size_t class_def_method_index, bool is_static,
484 uint32_t method_idx, const DexFile& dex_file) {
485 const CompiledMethod* compiled_method =
486 compiler_->GetCompiledMethod(art::Compiler::MethodReference(&dex_file, method_idx));
487
488 uint32_t frame_size_in_bytes = 0;
489 uint32_t core_spill_mask = 0;
490 uint32_t fp_spill_mask = 0;
491
492 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800493 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700494
495
496 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700497 uint32_t aligned_code_offset = compiled_method->AlignCode(code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700498 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
499 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800500 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700501 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700502 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700503 << " Expected: " << aligned_code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700504 return false;
505 }
506 code_offset += aligned_code_delta;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700507 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700508 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700509 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700510 const std::vector<uint8_t>& code = compiled_method->GetCode();
511 size_t code_size = code.size() * sizeof(code[0]);
jeffhao55d78212011-11-02 11:41:50 -0700512
513 // Deduplicate code arrays
514 size_t offset = code_offset + compiled_method->CodeDelta();
jeffhaof479dcc2011-11-02 15:54:15 -0700515 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Ian Rogers0571d352011-11-03 19:51:38 -0700516 if (code_iter != code_offsets_.end() && offset != method_offsets.code_offset_) {
517 DCHECK((code_size == 0 && method_offsets.code_offset_ == 0)
518 || code_iter->second == method_offsets.code_offset_)
519 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700520 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700521 DCHECK((code_size == 0 && method_offsets.code_offset_ == 0)
522 || offset == method_offsets.code_offset_)
523 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700524 if (!file->WriteFully(&code[0], code_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700525 ReportWriteFailure("method code", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700526 return false;
527 }
528 code_offset += code_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700529 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700530 DCHECK_CODE_OFFSET();
Ian Rogers0571d352011-11-03 19:51:38 -0700531 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
532 core_spill_mask = compiled_method->GetCoreSpillMask();
533 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700534 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700535
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700536 if (!file->WriteFully(&frame_size_in_bytes, sizeof(frame_size_in_bytes))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700537 ReportWriteFailure("method frame size", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700538 return false;
539 }
540 code_offset += sizeof(frame_size_in_bytes);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700541 if (!file->WriteFully(&core_spill_mask, sizeof(core_spill_mask))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700542 ReportWriteFailure("method core spill mask", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700543 return false;
544 }
545 code_offset += sizeof(core_spill_mask);
546 if (!file->WriteFully(&fp_spill_mask, sizeof(fp_spill_mask))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700547 ReportWriteFailure("method fp spill mask", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700548 return false;
549 }
550 code_offset += sizeof(fp_spill_mask);
551
552 if (compiled_method != NULL) {
553 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
554 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700555
556 // Deduplicate mapping tables
Ian Rogers0571d352011-11-03 19:51:38 -0700557 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
558 mapping_table_offsets_.find(&mapping_table);
559 if (mapping_iter != mapping_table_offsets_.end() &&
560 code_offset != method_offsets.mapping_table_offset_) {
561 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
562 || mapping_iter->second == method_offsets.mapping_table_offset_)
563 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700564 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700565 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
566 || code_offset == method_offsets.mapping_table_offset_)
567 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700568 if (!file->WriteFully(&mapping_table[0], mapping_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700569 ReportWriteFailure("mapping table", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700570 return false;
571 }
572 code_offset += mapping_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700573 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700574 DCHECK_CODE_OFFSET();
575
576 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
577 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700578
579 // Deduplicate vmap tables
Ian Rogers0571d352011-11-03 19:51:38 -0700580 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
581 vmap_table_offsets_.find(&vmap_table);
582 if (vmap_iter != vmap_table_offsets_.end() &&
583 code_offset != method_offsets.vmap_table_offset_) {
584 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
585 || vmap_iter->second == method_offsets.vmap_table_offset_)
586 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700587 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700588 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
589 || code_offset == method_offsets.vmap_table_offset_)
590 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700591 if (!file->WriteFully(&vmap_table[0], vmap_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700592 ReportWriteFailure("vmap table", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700593 return false;
594 }
595 code_offset += vmap_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700596 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700597 DCHECK_CODE_OFFSET();
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800598
599 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
600 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
601
602 // Deduplicate GC maps
603 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
604 gc_map_offsets_.find(&gc_map);
605 if (gc_map_iter != gc_map_offsets_.end() &&
606 code_offset != method_offsets.gc_map_offset_) {
607 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
608 || gc_map_iter->second == method_offsets.gc_map_offset_)
609 << PrettyMethod(method_idx, dex_file);
610 } else {
611 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
612 || code_offset == method_offsets.gc_map_offset_)
613 << PrettyMethod(method_idx, dex_file);
614 if (!file->WriteFully(&gc_map[0], gc_map_size)) {
615 ReportWriteFailure("GC map", method_idx, dex_file, file);
616 return false;
617 }
618 code_offset += gc_map_size;
619 }
620 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700621 }
Ian Rogers0571d352011-11-03 19:51:38 -0700622 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
623 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700624 if (compiled_invoke_stub != NULL) {
625 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
626 compiler_->GetInstructionSet());
627 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
628 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800629 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700630 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
631 PLOG(ERROR) << "Failed to seek to align invoke stub code. Actual: " << new_offset
632 << " Expected: " << aligned_code_offset;
633 return false;
634 }
635 code_offset += aligned_code_delta;
636 DCHECK_CODE_OFFSET();
637 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700638 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700639 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
640 size_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
jeffhao55d78212011-11-02 11:41:50 -0700641
642 // Deduplicate invoke stubs
Ian Rogers0571d352011-11-03 19:51:38 -0700643 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter =
644 code_offsets_.find(&invoke_stub);
645 if (stub_iter != code_offsets_.end() &&
646 code_offset != method_offsets.invoke_stub_offset_) {
647 DCHECK((invoke_stub_size == 0 && method_offsets.invoke_stub_offset_ == 0)
648 || stub_iter->second == method_offsets.invoke_stub_offset_)
649 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700650 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700651 DCHECK((invoke_stub_size == 0 && method_offsets.invoke_stub_offset_ == 0)
652 || code_offset == method_offsets.invoke_stub_offset_)
653 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700654 if (!file->WriteFully(&invoke_stub[0], invoke_stub_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700655 ReportWriteFailure("invoke stub code", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700656 return false;
657 }
658 code_offset += invoke_stub_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700659 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700660 DCHECK_CODE_OFFSET();
661 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700662 return code_offset;
663}
664
Brian Carlstrome24fa612011-09-29 00:53:55 -0700665OatWriter::OatDexFile::OatDexFile(const DexFile& dex_file) {
Elliott Hughes95572412011-12-13 18:14:20 -0800666 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700667 dex_file_location_size_ = location.size();
668 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
669 dex_file_checksum_ = dex_file.GetHeader().checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800670 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800671 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700672}
673
674size_t OatWriter::OatDexFile::SizeOf() const {
675 return sizeof(dex_file_location_size_)
676 + dex_file_location_size_
677 + sizeof(dex_file_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800678 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800679 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700680}
681
682void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
683 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
684 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
685 oat_header.UpdateChecksum(&dex_file_checksum_, sizeof(dex_file_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800686 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800687 oat_header.UpdateChecksum(&methods_offsets_[0],
688 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700689}
690
691bool OatWriter::OatDexFile::Write(File* file) const {
692 if (!file->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700693 PLOG(ERROR) << "Failed to write dex file location length to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700694 return false;
695 }
696 if (!file->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700697 PLOG(ERROR) << "Failed to write dex file location data to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700698 return false;
699 }
700 if (!file->WriteFully(&dex_file_checksum_, sizeof(dex_file_checksum_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700701 PLOG(ERROR) << "Failed to write dex file checksum to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700702 return false;
703 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800704 if (!file->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
705 PLOG(ERROR) << "Failed to write dex file offset to " << file->name();
706 return false;
707 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800708 if (!file->WriteFully(&methods_offsets_[0],
709 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700710 PLOG(ERROR) << "Failed to write methods offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700711 return false;
712 }
713 return true;
714}
715
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800716OatWriter::OatClass::OatClass(Class::Status status, uint32_t methods_count) {
717 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700718 method_offsets_.resize(methods_count);
719}
720
Brian Carlstrom389efb02012-01-11 12:06:26 -0800721size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800722 return sizeof(status_)
723 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700724}
725
Brian Carlstrom389efb02012-01-11 12:06:26 -0800726void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800727 oat_header.UpdateChecksum(&status_, sizeof(status_));
728 oat_header.UpdateChecksum(&method_offsets_[0],
729 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700730}
731
Brian Carlstrom389efb02012-01-11 12:06:26 -0800732bool OatWriter::OatClass::Write(File* file) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800733 if (!file->WriteFully(&status_, sizeof(status_))) {
734 PLOG(ERROR) << "Failed to write class status to " << file->name();
735 return false;
736 }
737 if (!file->WriteFully(&method_offsets_[0],
738 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700739 PLOG(ERROR) << "Failed to write method offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700740 return false;
741 }
742 return true;
743}
744
745} // namespace art