blob: 9690142ebb6c118faae75ba211ac32f8da520aac [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;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800196 uint32_t gc_map_offset = 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700197 // derived from CompiledInvokeStub if available
198 uint32_t invoke_stub_offset = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700199
Ian Rogers0571d352011-11-03 19:51:38 -0700200 CompiledMethod* compiled_method =
201 compiler_->GetCompiledMethod(art::Compiler::MethodReference(dex_file, method_idx));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700202 if (compiled_method != NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700203 offset = compiled_method->AlignCode(offset);
Elliott Hughes06b37d92011-10-16 11:51:29 -0700204 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700205 const std::vector<uint8_t>& code = compiled_method->GetCode();
206 size_t code_size = code.size() * sizeof(code[0]);
207 uint32_t thumb_offset = compiled_method->CodeDelta();
208 code_offset = (code_size == 0) ? 0 : offset + thumb_offset;
jeffhao55d78212011-11-02 11:41:50 -0700209
210 // Deduplicate code arrays
jeffhaof479dcc2011-11-02 15:54:15 -0700211 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
jeffhao55d78212011-11-02 11:41:50 -0700212 if (code_iter != code_offsets_.end()) {
213 code_offset = code_iter->second;
214 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700215 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&code, code_offset));
jeffhao55d78212011-11-02 11:41:50 -0700216 offset += code_size;
217 oat_header_->UpdateChecksum(&code[0], code_size);
218 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700219
220 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700221 core_spill_mask = compiled_method->GetCoreSpillMask();
222 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700223 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700224
225 offset += sizeof(frame_size_in_bytes);
226 oat_header_->UpdateChecksum(&frame_size_in_bytes, sizeof(frame_size_in_bytes));
227
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700228 offset += sizeof(core_spill_mask);
229 oat_header_->UpdateChecksum(&core_spill_mask, sizeof(core_spill_mask));
230
231 offset += sizeof(fp_spill_mask);
232 oat_header_->UpdateChecksum(&fp_spill_mask, sizeof(fp_spill_mask));
233
234 if (compiled_method != NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700235 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
236 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
237 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700238
239 // Deduplicate mapping tables
jeffhaof479dcc2011-11-02 15:54:15 -0700240 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
jeffhao55d78212011-11-02 11:41:50 -0700241 if (mapping_iter != mapping_table_offsets_.end()) {
242 mapping_table_offset = mapping_iter->second;
243 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700244 mapping_table_offsets_.insert(std::pair<const std::vector<uint32_t>*, uint32_t>(&mapping_table, mapping_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700245 offset += mapping_table_size;
246 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
247 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700248
249 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
250 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
251 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700252
253 // Deduplicate vmap tables
jeffhaof479dcc2011-11-02 15:54:15 -0700254 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
jeffhao55d78212011-11-02 11:41:50 -0700255 if (vmap_iter != vmap_table_offsets_.end()) {
256 vmap_table_offset = vmap_iter->second;
257 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700258 vmap_table_offsets_.insert(std::pair<const std::vector<uint16_t>*, uint32_t>(&vmap_table, vmap_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700259 offset += vmap_table_size;
260 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
261 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800262
263 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
264 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
265 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
266
267 // Deduplicate GC maps
268 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = gc_map_offsets_.find(&gc_map);
269 if (gc_map_iter != gc_map_offsets_.end()) {
270 gc_map_offset = gc_map_iter->second;
271 } else {
272 gc_map_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&gc_map, gc_map_offset));
273 offset += gc_map_size;
274 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
275 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700276 }
277
Ian Rogers0571d352011-11-03 19:51:38 -0700278 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx));
279 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700280 if (compiled_invoke_stub != NULL) {
281 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
Elliott Hughes06b37d92011-10-16 11:51:29 -0700282 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700283 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
284 size_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
285 invoke_stub_offset = (invoke_stub_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700286
287 // Deduplicate invoke stubs
jeffhaof479dcc2011-11-02 15:54:15 -0700288 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&invoke_stub);
jeffhao55d78212011-11-02 11:41:50 -0700289 if (stub_iter != code_offsets_.end()) {
290 invoke_stub_offset = stub_iter->second;
291 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700292 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&invoke_stub, invoke_stub_offset));
jeffhao55d78212011-11-02 11:41:50 -0700293 offset += invoke_stub_size;
294 oat_header_->UpdateChecksum(&invoke_stub[0], invoke_stub_size);
295 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700296 }
297
Brian Carlstrom389efb02012-01-11 12:06:26 -0800298 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index]
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700299 = OatMethodOffsets(code_offset,
300 frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700301 core_spill_mask,
302 fp_spill_mask,
303 mapping_table_offset,
304 vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800305 gc_map_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700306 invoke_stub_offset);
307
Ian Rogers0571d352011-11-03 19:51:38 -0700308 if (compiler_->IsImage()) {
309 ClassLinker* linker = Runtime::Current()->GetClassLinker();
310 DexCache* dex_cache = linker->FindDexCache(*dex_file);
311 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader_,
312 is_direct);
313 CHECK(method != NULL);
314 method->SetFrameSizeInBytes(frame_size_in_bytes);
315 method->SetCoreSpillMask(core_spill_mask);
316 method->SetFpSpillMask(fp_spill_mask);
317 method->SetOatMappingTableOffset(mapping_table_offset);
318 method->SetOatCodeOffset(code_offset);
319 method->SetOatVmapTableOffset(vmap_table_offset);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800320 method->SetOatGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700321 method->SetOatInvokeStubOffset(invoke_stub_offset);
322 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700323 return offset;
324}
325
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700326#define DCHECK_CODE_OFFSET() \
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800327 DCHECK_EQ(static_cast<off_t>(code_offset), lseek(file->Fd(), 0, SEEK_CUR))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700328
Elliott Hughes234da572011-11-03 22:13:06 -0700329bool OatWriter::Write(File* file) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700330 if (!file->WriteFully(oat_header_, sizeof(*oat_header_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700331 PLOG(ERROR) << "Failed to write oat header to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700332 return false;
333 }
334
Elliott Hughes234da572011-11-03 22:13:06 -0700335 if (!WriteTables(file)) {
336 LOG(ERROR) << "Failed to write oat tables to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700337 return false;
338 }
339
Elliott Hughes234da572011-11-03 22:13:06 -0700340 size_t code_offset = WriteCode(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700341 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700342 LOG(ERROR) << "Failed to write oat code to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700343 return false;
344 }
345
Elliott Hughes234da572011-11-03 22:13:06 -0700346 code_offset = WriteCodeDexFiles(file, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700347 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700348 LOG(ERROR) << "Failed to write oat code for dex files to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700349 return false;
350 }
351
352 return true;
353}
354
355bool OatWriter::WriteTables(File* file) {
356 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
357 if (!oat_dex_files_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700358 PLOG(ERROR) << "Failed to write oat dex information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700359 return false;
360 }
361 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800362 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
363 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800364 off_t actual_offset = lseek(file->Fd(), expected_offset, SEEK_SET);
Brian Carlstrom89521892011-12-07 22:05:07 -0800365 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
366 const DexFile* dex_file = (*dex_files_)[i];
367 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
368 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
369 return false;
370 }
371 const DexFile* dex_file = (*dex_files_)[i];
372 if (!file->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
373 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << file->name();
374 return false;
375 }
376 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800377 for (size_t i = 0; i != oat_classes_.size(); ++i) {
378 if (!oat_classes_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700379 PLOG(ERROR) << "Failed to write oat methods information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700380 return false;
381 }
382 }
383 return true;
384}
385
386size_t OatWriter::WriteCode(File* file) {
387 uint32_t code_offset = oat_header_->GetExecutableOffset();
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800388 off_t new_offset = lseek(file->Fd(), executable_offset_padding_length_, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700389 if (static_cast<uint32_t>(new_offset) != code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700390 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700391 << " Expected: " << code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700392 return 0;
393 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700394 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700395 return code_offset;
396}
397
398size_t OatWriter::WriteCodeDexFiles(File* file, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700399 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800400 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700401 const DexFile* dex_file = (*dex_files_)[i];
402 CHECK(dex_file != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700403 code_offset = WriteCodeDexFile(file, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700404 if (code_offset == 0) {
405 return 0;
406 }
407 }
408 return code_offset;
409}
410
Ian Rogers0571d352011-11-03 19:51:38 -0700411size_t OatWriter::WriteCodeDexFile(File* file, size_t code_offset, size_t& oat_class_index,
412 const DexFile& dex_file) {
413 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
414 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700415 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700416 code_offset = WriteCodeClassDef(file, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700417 if (code_offset == 0) {
418 return 0;
419 }
420 }
421 return code_offset;
422}
423
Ian Rogers0571d352011-11-03 19:51:38 -0700424void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
425 const DexFile& dex_file, File* f) const {
426 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
427 << " to " << f->name();
Elliott Hughes234da572011-11-03 22:13:06 -0700428}
429
Brian Carlstrome24fa612011-09-29 00:53:55 -0700430size_t OatWriter::WriteCodeClassDef(File* file,
Ian Rogers0571d352011-11-03 19:51:38 -0700431 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700432 const DexFile& dex_file,
433 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700434 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700435 if (class_data == NULL) {
436 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700437 return code_offset;
438 }
Ian Rogers0571d352011-11-03 19:51:38 -0700439 ClassDataItemIterator it(dex_file, class_data);
440 // Skip fields
441 while (it.HasNextStaticField()) {
442 it.Next();
443 }
444 while (it.HasNextInstanceField()) {
445 it.Next();
446 }
447 // Process methods
448 size_t class_def_method_index = 0;
449 while (it.HasNextDirectMethod()) {
450 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
451 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
452 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700453 if (code_offset == 0) {
454 return 0;
455 }
Ian Rogers0571d352011-11-03 19:51:38 -0700456 class_def_method_index++;
457 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700458 }
Ian Rogers0571d352011-11-03 19:51:38 -0700459 while (it.HasNextVirtualMethod()) {
460 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
461 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700462 if (code_offset == 0) {
463 return 0;
464 }
Ian Rogers0571d352011-11-03 19:51:38 -0700465 class_def_method_index++;
466 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700467 }
468 return code_offset;
469}
470
Ian Rogers0571d352011-11-03 19:51:38 -0700471size_t OatWriter::WriteCodeMethod(File* file, size_t code_offset, size_t oat_class_index,
472 size_t class_def_method_index, bool is_static,
473 uint32_t method_idx, const DexFile& dex_file) {
474 const CompiledMethod* compiled_method =
475 compiler_->GetCompiledMethod(art::Compiler::MethodReference(&dex_file, method_idx));
476
477 uint32_t frame_size_in_bytes = 0;
478 uint32_t core_spill_mask = 0;
479 uint32_t fp_spill_mask = 0;
480
481 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800482 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700483
484
485 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700486 uint32_t aligned_code_offset = compiled_method->AlignCode(code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700487 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
488 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800489 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700490 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700491 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700492 << " Expected: " << aligned_code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700493 return false;
494 }
495 code_offset += aligned_code_delta;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700496 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700497 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700498 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700499 const std::vector<uint8_t>& code = compiled_method->GetCode();
500 size_t code_size = code.size() * sizeof(code[0]);
jeffhao55d78212011-11-02 11:41:50 -0700501
502 // Deduplicate code arrays
503 size_t offset = code_offset + compiled_method->CodeDelta();
jeffhaof479dcc2011-11-02 15:54:15 -0700504 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Ian Rogers0571d352011-11-03 19:51:38 -0700505 if (code_iter != code_offsets_.end() && offset != method_offsets.code_offset_) {
506 DCHECK((code_size == 0 && method_offsets.code_offset_ == 0)
507 || code_iter->second == method_offsets.code_offset_)
508 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700509 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700510 DCHECK((code_size == 0 && method_offsets.code_offset_ == 0)
511 || offset == method_offsets.code_offset_)
512 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700513 if (!file->WriteFully(&code[0], code_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700514 ReportWriteFailure("method code", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700515 return false;
516 }
517 code_offset += code_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700518 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700519 DCHECK_CODE_OFFSET();
Ian Rogers0571d352011-11-03 19:51:38 -0700520 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
521 core_spill_mask = compiled_method->GetCoreSpillMask();
522 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700523 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700524
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700525 if (!file->WriteFully(&frame_size_in_bytes, sizeof(frame_size_in_bytes))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700526 ReportWriteFailure("method frame size", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700527 return false;
528 }
529 code_offset += sizeof(frame_size_in_bytes);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700530 if (!file->WriteFully(&core_spill_mask, sizeof(core_spill_mask))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700531 ReportWriteFailure("method core spill mask", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700532 return false;
533 }
534 code_offset += sizeof(core_spill_mask);
535 if (!file->WriteFully(&fp_spill_mask, sizeof(fp_spill_mask))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700536 ReportWriteFailure("method fp spill mask", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700537 return false;
538 }
539 code_offset += sizeof(fp_spill_mask);
540
541 if (compiled_method != NULL) {
542 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
543 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700544
545 // Deduplicate mapping tables
Ian Rogers0571d352011-11-03 19:51:38 -0700546 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
547 mapping_table_offsets_.find(&mapping_table);
548 if (mapping_iter != mapping_table_offsets_.end() &&
549 code_offset != method_offsets.mapping_table_offset_) {
550 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
551 || mapping_iter->second == method_offsets.mapping_table_offset_)
552 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700553 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700554 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
555 || code_offset == method_offsets.mapping_table_offset_)
556 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700557 if (!file->WriteFully(&mapping_table[0], mapping_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700558 ReportWriteFailure("mapping table", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700559 return false;
560 }
561 code_offset += mapping_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700562 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700563 DCHECK_CODE_OFFSET();
564
565 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
566 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700567
568 // Deduplicate vmap tables
Ian Rogers0571d352011-11-03 19:51:38 -0700569 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
570 vmap_table_offsets_.find(&vmap_table);
571 if (vmap_iter != vmap_table_offsets_.end() &&
572 code_offset != method_offsets.vmap_table_offset_) {
573 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
574 || vmap_iter->second == method_offsets.vmap_table_offset_)
575 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700576 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700577 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
578 || code_offset == method_offsets.vmap_table_offset_)
579 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700580 if (!file->WriteFully(&vmap_table[0], vmap_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700581 ReportWriteFailure("vmap table", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700582 return false;
583 }
584 code_offset += vmap_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700585 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700586 DCHECK_CODE_OFFSET();
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800587
588 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
589 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
590
591 // Deduplicate GC maps
592 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
593 gc_map_offsets_.find(&gc_map);
594 if (gc_map_iter != gc_map_offsets_.end() &&
595 code_offset != method_offsets.gc_map_offset_) {
596 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
597 || gc_map_iter->second == method_offsets.gc_map_offset_)
598 << PrettyMethod(method_idx, dex_file);
599 } else {
600 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
601 || code_offset == method_offsets.gc_map_offset_)
602 << PrettyMethod(method_idx, dex_file);
603 if (!file->WriteFully(&gc_map[0], gc_map_size)) {
604 ReportWriteFailure("GC map", method_idx, dex_file, file);
605 return false;
606 }
607 code_offset += gc_map_size;
608 }
609 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700610 }
Ian Rogers0571d352011-11-03 19:51:38 -0700611 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
612 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700613 if (compiled_invoke_stub != NULL) {
614 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
615 compiler_->GetInstructionSet());
616 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
617 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800618 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700619 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
620 PLOG(ERROR) << "Failed to seek to align invoke stub code. Actual: " << new_offset
621 << " Expected: " << aligned_code_offset;
622 return false;
623 }
624 code_offset += aligned_code_delta;
625 DCHECK_CODE_OFFSET();
626 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700627 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700628 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
629 size_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
jeffhao55d78212011-11-02 11:41:50 -0700630
631 // Deduplicate invoke stubs
Ian Rogers0571d352011-11-03 19:51:38 -0700632 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter =
633 code_offsets_.find(&invoke_stub);
634 if (stub_iter != code_offsets_.end() &&
635 code_offset != method_offsets.invoke_stub_offset_) {
636 DCHECK((invoke_stub_size == 0 && method_offsets.invoke_stub_offset_ == 0)
637 || stub_iter->second == method_offsets.invoke_stub_offset_)
638 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700639 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700640 DCHECK((invoke_stub_size == 0 && method_offsets.invoke_stub_offset_ == 0)
641 || code_offset == method_offsets.invoke_stub_offset_)
642 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700643 if (!file->WriteFully(&invoke_stub[0], invoke_stub_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700644 ReportWriteFailure("invoke stub code", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700645 return false;
646 }
647 code_offset += invoke_stub_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700648 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700649 DCHECK_CODE_OFFSET();
650 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700651 return code_offset;
652}
653
Brian Carlstrome24fa612011-09-29 00:53:55 -0700654OatWriter::OatDexFile::OatDexFile(const DexFile& dex_file) {
Elliott Hughes95572412011-12-13 18:14:20 -0800655 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700656 dex_file_location_size_ = location.size();
657 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
658 dex_file_checksum_ = dex_file.GetHeader().checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800659 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800660 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700661}
662
663size_t OatWriter::OatDexFile::SizeOf() const {
664 return sizeof(dex_file_location_size_)
665 + dex_file_location_size_
666 + sizeof(dex_file_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800667 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800668 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700669}
670
671void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
672 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
673 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
674 oat_header.UpdateChecksum(&dex_file_checksum_, sizeof(dex_file_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800675 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800676 oat_header.UpdateChecksum(&methods_offsets_[0],
677 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700678}
679
680bool OatWriter::OatDexFile::Write(File* file) const {
681 if (!file->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700682 PLOG(ERROR) << "Failed to write dex file location length to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700683 return false;
684 }
685 if (!file->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700686 PLOG(ERROR) << "Failed to write dex file location data to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700687 return false;
688 }
689 if (!file->WriteFully(&dex_file_checksum_, sizeof(dex_file_checksum_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700690 PLOG(ERROR) << "Failed to write dex file checksum to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700691 return false;
692 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800693 if (!file->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
694 PLOG(ERROR) << "Failed to write dex file offset to " << file->name();
695 return false;
696 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800697 if (!file->WriteFully(&methods_offsets_[0],
698 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700699 PLOG(ERROR) << "Failed to write methods offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700700 return false;
701 }
702 return true;
703}
704
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800705OatWriter::OatClass::OatClass(Class::Status status, uint32_t methods_count) {
706 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700707 method_offsets_.resize(methods_count);
708}
709
Brian Carlstrom389efb02012-01-11 12:06:26 -0800710size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800711 return sizeof(status_)
712 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700713}
714
Brian Carlstrom389efb02012-01-11 12:06:26 -0800715void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800716 oat_header.UpdateChecksum(&status_, sizeof(status_));
717 oat_header.UpdateChecksum(&method_offsets_[0],
718 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700719}
720
Brian Carlstrom389efb02012-01-11 12:06:26 -0800721bool OatWriter::OatClass::Write(File* file) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800722 if (!file->WriteFully(&status_, sizeof(status_))) {
723 PLOG(ERROR) << "Failed to write class status to " << file->name();
724 return false;
725 }
726 if (!file->WriteFully(&method_offsets_[0],
727 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700728 PLOG(ERROR) << "Failed to write method offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700729 return false;
730 }
731 return true;
732}
733
734} // namespace art