blob: 99c42f7aa13d5e618176faf36c95cab78f7b91a1 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrome24fa612011-09-29 00:53:55 -070016
17#include "oat_writer.h"
18
19#include "class_linker.h"
20#include "class_loader.h"
21#include "file.h"
22#include "os.h"
23#include "stl_util.h"
24
25namespace art {
26
Elliott Hughes234da572011-11-03 22:13:06 -070027bool OatWriter::Create(File* file,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070028 const ClassLoader* class_loader,
jeffhao10037c82012-01-23 15:06:23 -080029 const std::vector<const DexFile*>& dex_files,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070030 const Compiler& compiler) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070031 OatWriter oat_writer(dex_files, class_loader, compiler);
Elliott Hughes234da572011-11-03 22:13:06 -070032 return oat_writer.Write(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -070033}
34
Brian Carlstrom3320cf42011-10-04 14:58:28 -070035OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
36 const ClassLoader* class_loader,
37 const Compiler& compiler) {
38 compiler_ = &compiler;
Brian Carlstrome24fa612011-09-29 00:53:55 -070039 class_loader_ = class_loader;
40 dex_files_ = &dex_files;
Ian Rogers0571d352011-11-03 19:51:38 -070041 oat_header_ = NULL;
42 executable_offset_padding_length_ = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -070043
44 size_t offset = InitOatHeader();
45 offset = InitOatDexFiles(offset);
Brian Carlstrom89521892011-12-07 22:05:07 -080046 offset = InitDexFiles(offset);
Brian Carlstrom389efb02012-01-11 12:06:26 -080047 offset = InitOatClasses(offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -070048 offset = InitOatCode(offset);
49 offset = InitOatCodeDexFiles(offset);
50
51 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -070052}
53
Ian Rogers0571d352011-11-03 19:51:38 -070054OatWriter::~OatWriter() {
55 delete oat_header_;
56 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -080057 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -070058}
59
Brian Carlstrome24fa612011-09-29 00:53:55 -070060size_t OatWriter::InitOatHeader() {
61 // create the OatHeader
62 oat_header_ = new OatHeader(dex_files_);
63 size_t offset = sizeof(*oat_header_);
64 return offset;
65}
66
67size_t OatWriter::InitOatDexFiles(size_t offset) {
68 // create the OatDexFiles
69 for (size_t i = 0; i != dex_files_->size(); ++i) {
70 const DexFile* dex_file = (*dex_files_)[i];
71 CHECK(dex_file != NULL);
72 OatDexFile* oat_dex_file = new OatDexFile(*dex_file);
73 oat_dex_files_.push_back(oat_dex_file);
74 offset += oat_dex_file->SizeOf();
75 }
76 return offset;
77}
78
Brian Carlstrom89521892011-12-07 22:05:07 -080079size_t OatWriter::InitDexFiles(size_t offset) {
80 // calculate the offsets within OatDexFiles to the DexFiles
81 for (size_t i = 0; i != dex_files_->size(); ++i) {
82 // dex files are required to be 4 byte aligned
83 offset = RoundUp(offset, 4);
84
85 // set offset in OatDexFile to DexFile
86 oat_dex_files_[i]->dex_file_offset_ = offset;
87
88 const DexFile* dex_file = (*dex_files_)[i];
89 offset += dex_file->GetHeader().file_size_;
90 }
91 return offset;
92}
93
Brian Carlstrom389efb02012-01-11 12:06:26 -080094size_t OatWriter::InitOatClasses(size_t offset) {
95 // create the OatClasses
96 // calculate the offsets within OatDexFiles to OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -070097 for (size_t i = 0; i != dex_files_->size(); ++i) {
98 const DexFile* dex_file = (*dex_files_)[i];
99 for (size_t class_def_index = 0;
100 class_def_index < dex_file->NumClassDefs();
Ian Rogersc20a83e2012-01-18 18:15:32 -0800101 class_def_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800102 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700103 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
104 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700105 uint32_t num_methods = 0;
106 if (class_data != NULL) { // ie not an empty class, such as a marker interface
107 ClassDataItemIterator it(*dex_file, class_data);
108 size_t num_direct_methods = it.NumDirectMethods();
109 size_t num_virtual_methods = it.NumVirtualMethods();
110 num_methods = num_direct_methods + num_virtual_methods;
111 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800112
113 CompiledClass* compiled_class =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800114 compiler_->GetCompiledClass(Compiler::MethodReference(dex_file, class_def_index));
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800115 Class::Status status =
116 (compiled_class != NULL) ? compiled_class->GetStatus() : Class::kStatusNotReady;
117
118 OatClass* oat_class = new OatClass(status, num_methods);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800119 oat_classes_.push_back(oat_class);
120 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700121 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800122 oat_dex_files_[i]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700123 }
124 return offset;
125}
126
127size_t OatWriter::InitOatCode(size_t offset) {
128 // calculate the offsets within OatHeader to executable code
129 size_t old_offset = offset;
130 // required to be on a new page boundary
131 offset = RoundUp(offset, kPageSize);
132 oat_header_->SetExecutableOffset(offset);
133 executable_offset_padding_length_ = offset - old_offset;
134 return offset;
135}
136
137size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700138 size_t oat_class_index = 0;
139 for (size_t i = 0; i != dex_files_->size(); ++i) {
140 const DexFile* dex_file = (*dex_files_)[i];
141 CHECK(dex_file != NULL);
142 offset = InitOatCodeDexFile(offset, oat_class_index, *dex_file);
143 }
144 return offset;
145}
146
147size_t OatWriter::InitOatCodeDexFile(size_t offset,
148 size_t& oat_class_index,
149 const DexFile& dex_file) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800150 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700151 class_def_index < dex_file.NumClassDefs();
152 class_def_index++, oat_class_index++) {
153 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800154 offset = InitOatCodeClassDef(offset, oat_class_index, class_def_index, dex_file, class_def);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800155 oat_classes_[oat_class_index]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700156 }
157 return offset;
158}
159
160size_t OatWriter::InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800161 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700162 const DexFile& dex_file,
163 const DexFile::ClassDef& class_def) {
164 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700165 if (class_data == NULL) {
166 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700167 return offset;
168 }
Ian Rogers0571d352011-11-03 19:51:38 -0700169 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800170 CHECK_EQ(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700171 it.NumDirectMethods() + it.NumVirtualMethods());
172 // Skip fields
173 while (it.HasNextStaticField()) {
174 it.Next();
175 }
176 while (it.HasNextInstanceField()) {
177 it.Next();
178 }
179 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700180 size_t class_def_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700181 while (it.HasNextDirectMethod()) {
182 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
Ian Rogersc20a83e2012-01-18 18:15:32 -0800183 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
184 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index, is_native,
185 is_static, true, it.GetMemberIndex(), &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700186 class_def_method_index++;
187 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700188 }
Ian Rogers0571d352011-11-03 19:51:38 -0700189 while (it.HasNextVirtualMethod()) {
190 CHECK_EQ(it.GetMemberAccessFlags() & kAccStatic, 0U);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800191 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
192 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index, is_native,
193 false, false, it.GetMemberIndex(), &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700194 class_def_method_index++;
195 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700196 }
Ian Rogers0571d352011-11-03 19:51:38 -0700197 DCHECK(!it.HasNext());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700198 return offset;
199}
200
Ian Rogersc20a83e2012-01-18 18:15:32 -0800201size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index, size_t class_def_index,
202 size_t class_def_method_index, bool is_native, bool is_static,
203 bool is_direct, uint32_t method_idx, const DexFile* dex_file) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700204 // derived from CompiledMethod if available
205 uint32_t code_offset = 0;
206 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700207 uint32_t core_spill_mask = 0;
208 uint32_t fp_spill_mask = 0;
209 uint32_t mapping_table_offset = 0;
210 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800211 uint32_t gc_map_offset = 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700212 // derived from CompiledInvokeStub if available
213 uint32_t invoke_stub_offset = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700214
Ian Rogers0571d352011-11-03 19:51:38 -0700215 CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800216 compiler_->GetCompiledMethod(Compiler::MethodReference(dex_file, method_idx));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700217 if (compiled_method != NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700218 offset = compiled_method->AlignCode(offset);
Elliott Hughes06b37d92011-10-16 11:51:29 -0700219 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700220 const std::vector<uint8_t>& code = compiled_method->GetCode();
221 size_t code_size = code.size() * sizeof(code[0]);
222 uint32_t thumb_offset = compiled_method->CodeDelta();
223 code_offset = (code_size == 0) ? 0 : offset + thumb_offset;
jeffhao55d78212011-11-02 11:41:50 -0700224
225 // Deduplicate code arrays
jeffhaof479dcc2011-11-02 15:54:15 -0700226 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
jeffhao55d78212011-11-02 11:41:50 -0700227 if (code_iter != code_offsets_.end()) {
228 code_offset = code_iter->second;
229 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700230 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&code, code_offset));
jeffhao55d78212011-11-02 11:41:50 -0700231 offset += code_size;
232 oat_header_->UpdateChecksum(&code[0], code_size);
233 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700234
235 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700236 core_spill_mask = compiled_method->GetCoreSpillMask();
237 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700238 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700239
240 offset += sizeof(frame_size_in_bytes);
241 oat_header_->UpdateChecksum(&frame_size_in_bytes, sizeof(frame_size_in_bytes));
242
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700243 offset += sizeof(core_spill_mask);
244 oat_header_->UpdateChecksum(&core_spill_mask, sizeof(core_spill_mask));
245
246 offset += sizeof(fp_spill_mask);
247 oat_header_->UpdateChecksum(&fp_spill_mask, sizeof(fp_spill_mask));
248
249 if (compiled_method != NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700250 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
251 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
252 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700253
254 // Deduplicate mapping tables
jeffhaof479dcc2011-11-02 15:54:15 -0700255 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
jeffhao55d78212011-11-02 11:41:50 -0700256 if (mapping_iter != mapping_table_offsets_.end()) {
257 mapping_table_offset = mapping_iter->second;
258 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700259 mapping_table_offsets_.insert(std::pair<const std::vector<uint32_t>*, uint32_t>(&mapping_table, mapping_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700260 offset += mapping_table_size;
261 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
262 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700263
264 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
265 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
266 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700267
268 // Deduplicate vmap tables
jeffhaof479dcc2011-11-02 15:54:15 -0700269 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
jeffhao55d78212011-11-02 11:41:50 -0700270 if (vmap_iter != vmap_table_offsets_.end()) {
271 vmap_table_offset = vmap_iter->second;
272 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700273 vmap_table_offsets_.insert(std::pair<const std::vector<uint16_t>*, uint32_t>(&vmap_table, vmap_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700274 offset += vmap_table_size;
275 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
276 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800277
278 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
279 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
280 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
281
Ian Rogersc20a83e2012-01-18 18:15:32 -0800282#ifndef NDEBUG
283 // We expect GC maps except when the class hasn't been verified or the method is native
284 CompiledClass* compiled_class =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800285 compiler_->GetCompiledClass(Compiler::MethodReference(dex_file, class_def_index));
Ian Rogersc20a83e2012-01-18 18:15:32 -0800286 Class::Status status =
287 (compiled_class != NULL) ? compiled_class->GetStatus() : Class::kStatusNotReady;
288 CHECK(gc_map_size != 0 || is_native || status < Class::kStatusVerified)
289 << PrettyMethod(method_idx, *dex_file);
290#endif
291
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800292 // Deduplicate GC maps
293 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = gc_map_offsets_.find(&gc_map);
294 if (gc_map_iter != gc_map_offsets_.end()) {
295 gc_map_offset = gc_map_iter->second;
296 } else {
297 gc_map_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&gc_map, gc_map_offset));
298 offset += gc_map_size;
299 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
300 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700301 }
302
Ian Rogers0571d352011-11-03 19:51:38 -0700303 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx));
304 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700305 if (compiled_invoke_stub != NULL) {
306 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
Elliott Hughes06b37d92011-10-16 11:51:29 -0700307 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700308 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
309 size_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
310 invoke_stub_offset = (invoke_stub_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700311
312 // Deduplicate invoke stubs
jeffhaof479dcc2011-11-02 15:54:15 -0700313 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&invoke_stub);
jeffhao55d78212011-11-02 11:41:50 -0700314 if (stub_iter != code_offsets_.end()) {
315 invoke_stub_offset = stub_iter->second;
316 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700317 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&invoke_stub, invoke_stub_offset));
jeffhao55d78212011-11-02 11:41:50 -0700318 offset += invoke_stub_size;
319 oat_header_->UpdateChecksum(&invoke_stub[0], invoke_stub_size);
320 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700321 }
322
Brian Carlstrom389efb02012-01-11 12:06:26 -0800323 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index]
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700324 = OatMethodOffsets(code_offset,
325 frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700326 core_spill_mask,
327 fp_spill_mask,
328 mapping_table_offset,
329 vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800330 gc_map_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700331 invoke_stub_offset);
332
Ian Rogers0571d352011-11-03 19:51:38 -0700333 if (compiler_->IsImage()) {
334 ClassLinker* linker = Runtime::Current()->GetClassLinker();
335 DexCache* dex_cache = linker->FindDexCache(*dex_file);
336 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader_,
337 is_direct);
338 CHECK(method != NULL);
339 method->SetFrameSizeInBytes(frame_size_in_bytes);
340 method->SetCoreSpillMask(core_spill_mask);
341 method->SetFpSpillMask(fp_spill_mask);
342 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800343 // Don't overwrite static method trampoline
344 if (!method->IsStatic() || method->IsConstructor() ||
345 method->GetDeclaringClass()->IsInitialized()) {
346 method->SetOatCodeOffset(code_offset);
347 } else {
348 method->SetCode(Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod)->GetData());
349 }
Ian Rogers0571d352011-11-03 19:51:38 -0700350 method->SetOatVmapTableOffset(vmap_table_offset);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800351 method->SetOatGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700352 method->SetOatInvokeStubOffset(invoke_stub_offset);
353 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700354 return offset;
355}
356
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700357#define DCHECK_CODE_OFFSET() \
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800358 DCHECK_EQ(static_cast<off_t>(code_offset), lseek(file->Fd(), 0, SEEK_CUR))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700359
Elliott Hughes234da572011-11-03 22:13:06 -0700360bool OatWriter::Write(File* file) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700361 if (!file->WriteFully(oat_header_, sizeof(*oat_header_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700362 PLOG(ERROR) << "Failed to write oat header to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700363 return false;
364 }
365
Elliott Hughes234da572011-11-03 22:13:06 -0700366 if (!WriteTables(file)) {
367 LOG(ERROR) << "Failed to write oat tables to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700368 return false;
369 }
370
Elliott Hughes234da572011-11-03 22:13:06 -0700371 size_t code_offset = WriteCode(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700372 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700373 LOG(ERROR) << "Failed to write oat code to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700374 return false;
375 }
376
Elliott Hughes234da572011-11-03 22:13:06 -0700377 code_offset = WriteCodeDexFiles(file, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700378 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700379 LOG(ERROR) << "Failed to write oat code for dex files to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700380 return false;
381 }
382
383 return true;
384}
385
386bool OatWriter::WriteTables(File* file) {
387 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
388 if (!oat_dex_files_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700389 PLOG(ERROR) << "Failed to write oat dex information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700390 return false;
391 }
392 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800393 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
394 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800395 off_t actual_offset = lseek(file->Fd(), expected_offset, SEEK_SET);
Brian Carlstrom89521892011-12-07 22:05:07 -0800396 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
397 const DexFile* dex_file = (*dex_files_)[i];
398 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
399 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
400 return false;
401 }
402 const DexFile* dex_file = (*dex_files_)[i];
403 if (!file->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
404 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << file->name();
405 return false;
406 }
407 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800408 for (size_t i = 0; i != oat_classes_.size(); ++i) {
409 if (!oat_classes_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700410 PLOG(ERROR) << "Failed to write oat methods information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700411 return false;
412 }
413 }
414 return true;
415}
416
417size_t OatWriter::WriteCode(File* file) {
418 uint32_t code_offset = oat_header_->GetExecutableOffset();
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800419 off_t new_offset = lseek(file->Fd(), executable_offset_padding_length_, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700420 if (static_cast<uint32_t>(new_offset) != code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700421 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700422 << " Expected: " << code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700423 return 0;
424 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700425 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700426 return code_offset;
427}
428
429size_t OatWriter::WriteCodeDexFiles(File* file, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700430 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800431 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700432 const DexFile* dex_file = (*dex_files_)[i];
433 CHECK(dex_file != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700434 code_offset = WriteCodeDexFile(file, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700435 if (code_offset == 0) {
436 return 0;
437 }
438 }
439 return code_offset;
440}
441
Ian Rogers0571d352011-11-03 19:51:38 -0700442size_t OatWriter::WriteCodeDexFile(File* file, size_t code_offset, size_t& oat_class_index,
443 const DexFile& dex_file) {
444 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
445 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700446 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700447 code_offset = WriteCodeClassDef(file, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700448 if (code_offset == 0) {
449 return 0;
450 }
451 }
452 return code_offset;
453}
454
Ian Rogers0571d352011-11-03 19:51:38 -0700455void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
456 const DexFile& dex_file, File* f) const {
457 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
458 << " to " << f->name();
Elliott Hughes234da572011-11-03 22:13:06 -0700459}
460
Brian Carlstrome24fa612011-09-29 00:53:55 -0700461size_t OatWriter::WriteCodeClassDef(File* file,
Ian Rogers0571d352011-11-03 19:51:38 -0700462 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700463 const DexFile& dex_file,
464 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700465 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700466 if (class_data == NULL) {
467 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700468 return code_offset;
469 }
Ian Rogers0571d352011-11-03 19:51:38 -0700470 ClassDataItemIterator it(dex_file, class_data);
471 // Skip fields
472 while (it.HasNextStaticField()) {
473 it.Next();
474 }
475 while (it.HasNextInstanceField()) {
476 it.Next();
477 }
478 // Process methods
479 size_t class_def_method_index = 0;
480 while (it.HasNextDirectMethod()) {
481 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
482 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
483 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700484 if (code_offset == 0) {
485 return 0;
486 }
Ian Rogers0571d352011-11-03 19:51:38 -0700487 class_def_method_index++;
488 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700489 }
Ian Rogers0571d352011-11-03 19:51:38 -0700490 while (it.HasNextVirtualMethod()) {
491 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
492 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700493 if (code_offset == 0) {
494 return 0;
495 }
Ian Rogers0571d352011-11-03 19:51:38 -0700496 class_def_method_index++;
497 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700498 }
499 return code_offset;
500}
501
Ian Rogers0571d352011-11-03 19:51:38 -0700502size_t OatWriter::WriteCodeMethod(File* file, size_t code_offset, size_t oat_class_index,
503 size_t class_def_method_index, bool is_static,
504 uint32_t method_idx, const DexFile& dex_file) {
505 const CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800506 compiler_->GetCompiledMethod(Compiler::MethodReference(&dex_file, method_idx));
Ian Rogers0571d352011-11-03 19:51:38 -0700507
508 uint32_t frame_size_in_bytes = 0;
509 uint32_t core_spill_mask = 0;
510 uint32_t fp_spill_mask = 0;
511
512 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800513 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700514
515
516 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700517 uint32_t aligned_code_offset = compiled_method->AlignCode(code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700518 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
519 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800520 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700521 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700522 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700523 << " Expected: " << aligned_code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700524 return false;
525 }
526 code_offset += aligned_code_delta;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700527 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700528 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700529 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700530 const std::vector<uint8_t>& code = compiled_method->GetCode();
531 size_t code_size = code.size() * sizeof(code[0]);
jeffhao55d78212011-11-02 11:41:50 -0700532
533 // Deduplicate code arrays
534 size_t offset = code_offset + compiled_method->CodeDelta();
jeffhaof479dcc2011-11-02 15:54:15 -0700535 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Ian Rogers0571d352011-11-03 19:51:38 -0700536 if (code_iter != code_offsets_.end() && offset != method_offsets.code_offset_) {
537 DCHECK((code_size == 0 && method_offsets.code_offset_ == 0)
538 || code_iter->second == method_offsets.code_offset_)
539 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700540 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700541 DCHECK((code_size == 0 && method_offsets.code_offset_ == 0)
542 || offset == method_offsets.code_offset_)
543 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700544 if (!file->WriteFully(&code[0], code_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700545 ReportWriteFailure("method code", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700546 return false;
547 }
548 code_offset += code_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700549 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700550 DCHECK_CODE_OFFSET();
Ian Rogers0571d352011-11-03 19:51:38 -0700551 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
552 core_spill_mask = compiled_method->GetCoreSpillMask();
553 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700554 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700555
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700556 if (!file->WriteFully(&frame_size_in_bytes, sizeof(frame_size_in_bytes))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700557 ReportWriteFailure("method frame size", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700558 return false;
559 }
560 code_offset += sizeof(frame_size_in_bytes);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700561 if (!file->WriteFully(&core_spill_mask, sizeof(core_spill_mask))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700562 ReportWriteFailure("method core spill mask", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700563 return false;
564 }
565 code_offset += sizeof(core_spill_mask);
566 if (!file->WriteFully(&fp_spill_mask, sizeof(fp_spill_mask))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700567 ReportWriteFailure("method fp spill mask", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700568 return false;
569 }
570 code_offset += sizeof(fp_spill_mask);
571
572 if (compiled_method != NULL) {
573 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
574 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700575
576 // Deduplicate mapping tables
Ian Rogers0571d352011-11-03 19:51:38 -0700577 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
578 mapping_table_offsets_.find(&mapping_table);
579 if (mapping_iter != mapping_table_offsets_.end() &&
580 code_offset != method_offsets.mapping_table_offset_) {
581 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
582 || mapping_iter->second == method_offsets.mapping_table_offset_)
583 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700584 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700585 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
586 || code_offset == method_offsets.mapping_table_offset_)
587 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700588 if (!file->WriteFully(&mapping_table[0], mapping_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700589 ReportWriteFailure("mapping table", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700590 return false;
591 }
592 code_offset += mapping_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700593 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700594 DCHECK_CODE_OFFSET();
595
596 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
597 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700598
599 // Deduplicate vmap tables
Ian Rogers0571d352011-11-03 19:51:38 -0700600 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
601 vmap_table_offsets_.find(&vmap_table);
602 if (vmap_iter != vmap_table_offsets_.end() &&
603 code_offset != method_offsets.vmap_table_offset_) {
604 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
605 || vmap_iter->second == method_offsets.vmap_table_offset_)
606 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700607 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700608 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
609 || code_offset == method_offsets.vmap_table_offset_)
610 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700611 if (!file->WriteFully(&vmap_table[0], vmap_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700612 ReportWriteFailure("vmap table", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700613 return false;
614 }
615 code_offset += vmap_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700616 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700617 DCHECK_CODE_OFFSET();
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800618
619 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
620 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
621
622 // Deduplicate GC maps
623 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
624 gc_map_offsets_.find(&gc_map);
625 if (gc_map_iter != gc_map_offsets_.end() &&
626 code_offset != method_offsets.gc_map_offset_) {
627 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
628 || gc_map_iter->second == method_offsets.gc_map_offset_)
629 << PrettyMethod(method_idx, dex_file);
630 } else {
631 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
632 || code_offset == method_offsets.gc_map_offset_)
633 << PrettyMethod(method_idx, dex_file);
634 if (!file->WriteFully(&gc_map[0], gc_map_size)) {
635 ReportWriteFailure("GC map", method_idx, dex_file, file);
636 return false;
637 }
638 code_offset += gc_map_size;
639 }
640 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700641 }
Ian Rogers0571d352011-11-03 19:51:38 -0700642 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
643 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700644 if (compiled_invoke_stub != NULL) {
645 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
646 compiler_->GetInstructionSet());
647 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
648 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800649 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700650 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
651 PLOG(ERROR) << "Failed to seek to align invoke stub code. Actual: " << new_offset
652 << " Expected: " << aligned_code_offset;
653 return false;
654 }
655 code_offset += aligned_code_delta;
656 DCHECK_CODE_OFFSET();
657 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700658 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700659 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
660 size_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
jeffhao55d78212011-11-02 11:41:50 -0700661
662 // Deduplicate invoke stubs
Ian Rogers0571d352011-11-03 19:51:38 -0700663 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter =
664 code_offsets_.find(&invoke_stub);
665 if (stub_iter != code_offsets_.end() &&
666 code_offset != method_offsets.invoke_stub_offset_) {
667 DCHECK((invoke_stub_size == 0 && method_offsets.invoke_stub_offset_ == 0)
668 || stub_iter->second == method_offsets.invoke_stub_offset_)
669 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700670 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700671 DCHECK((invoke_stub_size == 0 && method_offsets.invoke_stub_offset_ == 0)
672 || code_offset == method_offsets.invoke_stub_offset_)
673 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700674 if (!file->WriteFully(&invoke_stub[0], invoke_stub_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700675 ReportWriteFailure("invoke stub code", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700676 return false;
677 }
678 code_offset += invoke_stub_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700679 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700680 DCHECK_CODE_OFFSET();
681 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700682 return code_offset;
683}
684
Brian Carlstrome24fa612011-09-29 00:53:55 -0700685OatWriter::OatDexFile::OatDexFile(const DexFile& dex_file) {
Elliott Hughes95572412011-12-13 18:14:20 -0800686 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700687 dex_file_location_size_ = location.size();
688 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800689 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800690 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800691 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700692}
693
694size_t OatWriter::OatDexFile::SizeOf() const {
695 return sizeof(dex_file_location_size_)
696 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800697 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800698 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800699 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700700}
701
702void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
703 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
704 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800705 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800706 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800707 oat_header.UpdateChecksum(&methods_offsets_[0],
708 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700709}
710
711bool OatWriter::OatDexFile::Write(File* file) const {
712 if (!file->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700713 PLOG(ERROR) << "Failed to write dex file location length to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700714 return false;
715 }
716 if (!file->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700717 PLOG(ERROR) << "Failed to write dex file location data to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700718 return false;
719 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800720 if (!file->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
721 PLOG(ERROR) << "Failed to write dex file location checksum to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700722 return false;
723 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800724 if (!file->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
725 PLOG(ERROR) << "Failed to write dex file offset to " << file->name();
726 return false;
727 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800728 if (!file->WriteFully(&methods_offsets_[0],
729 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700730 PLOG(ERROR) << "Failed to write methods offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700731 return false;
732 }
733 return true;
734}
735
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800736OatWriter::OatClass::OatClass(Class::Status status, uint32_t methods_count) {
737 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700738 method_offsets_.resize(methods_count);
739}
740
Brian Carlstrom389efb02012-01-11 12:06:26 -0800741size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800742 return sizeof(status_)
743 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700744}
745
Brian Carlstrom389efb02012-01-11 12:06:26 -0800746void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800747 oat_header.UpdateChecksum(&status_, sizeof(status_));
748 oat_header.UpdateChecksum(&method_offsets_[0],
749 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700750}
751
Brian Carlstrom389efb02012-01-11 12:06:26 -0800752bool OatWriter::OatClass::Write(File* file) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800753 if (!file->WriteFully(&status_, sizeof(status_))) {
754 PLOG(ERROR) << "Failed to write class status to " << file->name();
755 return false;
756 }
757 if (!file->WriteFully(&method_offsets_[0],
758 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700759 PLOG(ERROR) << "Failed to write method offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700760 return false;
761 }
762 return true;
763}
764
765} // namespace art