blob: a4a4a2c03e2615dbad9ad19bc4d53fcef28bcf4c [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) {
Logan Chien8b977d32012-02-21 19:14:55 +0800204
205#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700206 // derived from CompiledMethod if available
207 uint32_t code_offset = 0;
208 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700209 uint32_t core_spill_mask = 0;
210 uint32_t fp_spill_mask = 0;
211 uint32_t mapping_table_offset = 0;
212 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800213 uint32_t gc_map_offset = 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700214 // derived from CompiledInvokeStub if available
215 uint32_t invoke_stub_offset = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700216
Ian Rogers0571d352011-11-03 19:51:38 -0700217 CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800218 compiler_->GetCompiledMethod(Compiler::MethodReference(dex_file, method_idx));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700219 if (compiled_method != NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700220 offset = compiled_method->AlignCode(offset);
Elliott Hughes06b37d92011-10-16 11:51:29 -0700221 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700222 const std::vector<uint8_t>& code = compiled_method->GetCode();
223 size_t code_size = code.size() * sizeof(code[0]);
224 uint32_t thumb_offset = compiled_method->CodeDelta();
225 code_offset = (code_size == 0) ? 0 : offset + thumb_offset;
jeffhao55d78212011-11-02 11:41:50 -0700226
227 // Deduplicate code arrays
jeffhaof479dcc2011-11-02 15:54:15 -0700228 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
jeffhao55d78212011-11-02 11:41:50 -0700229 if (code_iter != code_offsets_.end()) {
230 code_offset = code_iter->second;
231 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700232 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&code, code_offset));
jeffhao55d78212011-11-02 11:41:50 -0700233 offset += code_size;
234 oat_header_->UpdateChecksum(&code[0], code_size);
235 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700236
237 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700238 core_spill_mask = compiled_method->GetCoreSpillMask();
239 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700240 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700241
242 offset += sizeof(frame_size_in_bytes);
243 oat_header_->UpdateChecksum(&frame_size_in_bytes, sizeof(frame_size_in_bytes));
244
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700245 offset += sizeof(core_spill_mask);
246 oat_header_->UpdateChecksum(&core_spill_mask, sizeof(core_spill_mask));
247
248 offset += sizeof(fp_spill_mask);
249 oat_header_->UpdateChecksum(&fp_spill_mask, sizeof(fp_spill_mask));
250
251 if (compiled_method != NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700252 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
253 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
254 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700255
256 // Deduplicate mapping tables
jeffhaof479dcc2011-11-02 15:54:15 -0700257 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
jeffhao55d78212011-11-02 11:41:50 -0700258 if (mapping_iter != mapping_table_offsets_.end()) {
259 mapping_table_offset = mapping_iter->second;
260 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700261 mapping_table_offsets_.insert(std::pair<const std::vector<uint32_t>*, uint32_t>(&mapping_table, mapping_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700262 offset += mapping_table_size;
263 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
264 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700265
266 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
267 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
268 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700269
270 // Deduplicate vmap tables
jeffhaof479dcc2011-11-02 15:54:15 -0700271 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
jeffhao55d78212011-11-02 11:41:50 -0700272 if (vmap_iter != vmap_table_offsets_.end()) {
273 vmap_table_offset = vmap_iter->second;
274 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700275 vmap_table_offsets_.insert(std::pair<const std::vector<uint16_t>*, uint32_t>(&vmap_table, vmap_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700276 offset += vmap_table_size;
277 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
278 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800279
280 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
281 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
282 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
283
Ian Rogersc20a83e2012-01-18 18:15:32 -0800284#ifndef NDEBUG
285 // We expect GC maps except when the class hasn't been verified or the method is native
286 CompiledClass* compiled_class =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800287 compiler_->GetCompiledClass(Compiler::MethodReference(dex_file, class_def_index));
Ian Rogersc20a83e2012-01-18 18:15:32 -0800288 Class::Status status =
289 (compiled_class != NULL) ? compiled_class->GetStatus() : Class::kStatusNotReady;
290 CHECK(gc_map_size != 0 || is_native || status < Class::kStatusVerified)
291 << PrettyMethod(method_idx, *dex_file);
292#endif
293
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800294 // Deduplicate GC maps
295 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = gc_map_offsets_.find(&gc_map);
296 if (gc_map_iter != gc_map_offsets_.end()) {
297 gc_map_offset = gc_map_iter->second;
298 } else {
299 gc_map_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&gc_map, gc_map_offset));
300 offset += gc_map_size;
301 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
302 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700303 }
304
Ian Rogers0571d352011-11-03 19:51:38 -0700305 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx));
306 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700307 if (compiled_invoke_stub != NULL) {
308 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
Elliott Hughes06b37d92011-10-16 11:51:29 -0700309 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700310 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
311 size_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
312 invoke_stub_offset = (invoke_stub_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700313
314 // Deduplicate invoke stubs
jeffhaof479dcc2011-11-02 15:54:15 -0700315 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&invoke_stub);
jeffhao55d78212011-11-02 11:41:50 -0700316 if (stub_iter != code_offsets_.end()) {
317 invoke_stub_offset = stub_iter->second;
318 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700319 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&invoke_stub, invoke_stub_offset));
jeffhao55d78212011-11-02 11:41:50 -0700320 offset += invoke_stub_size;
321 oat_header_->UpdateChecksum(&invoke_stub[0], invoke_stub_size);
322 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700323 }
324
Brian Carlstrom389efb02012-01-11 12:06:26 -0800325 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index]
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700326 = OatMethodOffsets(code_offset,
327 frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700328 core_spill_mask,
329 fp_spill_mask,
330 mapping_table_offset,
331 vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800332 gc_map_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700333 invoke_stub_offset);
334
Ian Rogers0571d352011-11-03 19:51:38 -0700335 if (compiler_->IsImage()) {
336 ClassLinker* linker = Runtime::Current()->GetClassLinker();
337 DexCache* dex_cache = linker->FindDexCache(*dex_file);
338 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader_,
339 is_direct);
340 CHECK(method != NULL);
341 method->SetFrameSizeInBytes(frame_size_in_bytes);
342 method->SetCoreSpillMask(core_spill_mask);
343 method->SetFpSpillMask(fp_spill_mask);
344 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800345 // Don't overwrite static method trampoline
346 if (!method->IsStatic() || method->IsConstructor() ||
347 method->GetDeclaringClass()->IsInitialized()) {
348 method->SetOatCodeOffset(code_offset);
349 } else {
350 method->SetCode(Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod)->GetData());
351 }
Ian Rogers0571d352011-11-03 19:51:38 -0700352 method->SetOatVmapTableOffset(vmap_table_offset);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800353 method->SetOatGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700354 method->SetOatInvokeStubOffset(invoke_stub_offset);
355 }
Logan Chien8b977d32012-02-21 19:14:55 +0800356#endif
357
Brian Carlstrome24fa612011-09-29 00:53:55 -0700358 return offset;
359}
360
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700361#define DCHECK_CODE_OFFSET() \
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800362 DCHECK_EQ(static_cast<off_t>(code_offset), lseek(file->Fd(), 0, SEEK_CUR))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700363
Elliott Hughes234da572011-11-03 22:13:06 -0700364bool OatWriter::Write(File* file) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700365 if (!file->WriteFully(oat_header_, sizeof(*oat_header_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700366 PLOG(ERROR) << "Failed to write oat header to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700367 return false;
368 }
369
Elliott Hughes234da572011-11-03 22:13:06 -0700370 if (!WriteTables(file)) {
371 LOG(ERROR) << "Failed to write oat tables to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700372 return false;
373 }
374
Elliott Hughes234da572011-11-03 22:13:06 -0700375 size_t code_offset = WriteCode(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700376 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700377 LOG(ERROR) << "Failed to write oat code to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700378 return false;
379 }
380
Elliott Hughes234da572011-11-03 22:13:06 -0700381 code_offset = WriteCodeDexFiles(file, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700382 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700383 LOG(ERROR) << "Failed to write oat code for dex files to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700384 return false;
385 }
386
387 return true;
388}
389
390bool OatWriter::WriteTables(File* file) {
391 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
392 if (!oat_dex_files_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700393 PLOG(ERROR) << "Failed to write oat dex information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700394 return false;
395 }
396 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800397 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
398 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800399 off_t actual_offset = lseek(file->Fd(), expected_offset, SEEK_SET);
Brian Carlstrom89521892011-12-07 22:05:07 -0800400 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
401 const DexFile* dex_file = (*dex_files_)[i];
402 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
403 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
404 return false;
405 }
406 const DexFile* dex_file = (*dex_files_)[i];
407 if (!file->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
408 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << file->name();
409 return false;
410 }
411 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800412 for (size_t i = 0; i != oat_classes_.size(); ++i) {
413 if (!oat_classes_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700414 PLOG(ERROR) << "Failed to write oat methods information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700415 return false;
416 }
417 }
418 return true;
419}
420
421size_t OatWriter::WriteCode(File* file) {
422 uint32_t code_offset = oat_header_->GetExecutableOffset();
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800423 off_t new_offset = lseek(file->Fd(), executable_offset_padding_length_, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700424 if (static_cast<uint32_t>(new_offset) != code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700425 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700426 << " Expected: " << code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700427 return 0;
428 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700429 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700430 return code_offset;
431}
432
433size_t OatWriter::WriteCodeDexFiles(File* file, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700434 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800435 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700436 const DexFile* dex_file = (*dex_files_)[i];
437 CHECK(dex_file != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700438 code_offset = WriteCodeDexFile(file, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700439 if (code_offset == 0) {
440 return 0;
441 }
442 }
443 return code_offset;
444}
445
Ian Rogers0571d352011-11-03 19:51:38 -0700446size_t OatWriter::WriteCodeDexFile(File* file, size_t code_offset, size_t& oat_class_index,
447 const DexFile& dex_file) {
448 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
449 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700450 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700451 code_offset = WriteCodeClassDef(file, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700452 if (code_offset == 0) {
453 return 0;
454 }
455 }
456 return code_offset;
457}
458
Ian Rogers0571d352011-11-03 19:51:38 -0700459void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
460 const DexFile& dex_file, File* f) const {
461 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
462 << " to " << f->name();
Elliott Hughes234da572011-11-03 22:13:06 -0700463}
464
Brian Carlstrome24fa612011-09-29 00:53:55 -0700465size_t OatWriter::WriteCodeClassDef(File* file,
Ian Rogers0571d352011-11-03 19:51:38 -0700466 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700467 const DexFile& dex_file,
468 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700469 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700470 if (class_data == NULL) {
471 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700472 return code_offset;
473 }
Ian Rogers0571d352011-11-03 19:51:38 -0700474 ClassDataItemIterator it(dex_file, class_data);
475 // Skip fields
476 while (it.HasNextStaticField()) {
477 it.Next();
478 }
479 while (it.HasNextInstanceField()) {
480 it.Next();
481 }
482 // Process methods
483 size_t class_def_method_index = 0;
484 while (it.HasNextDirectMethod()) {
485 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
486 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
487 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700488 if (code_offset == 0) {
489 return 0;
490 }
Ian Rogers0571d352011-11-03 19:51:38 -0700491 class_def_method_index++;
492 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700493 }
Ian Rogers0571d352011-11-03 19:51:38 -0700494 while (it.HasNextVirtualMethod()) {
495 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
496 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700497 if (code_offset == 0) {
498 return 0;
499 }
Ian Rogers0571d352011-11-03 19:51:38 -0700500 class_def_method_index++;
501 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700502 }
503 return code_offset;
504}
505
Ian Rogers0571d352011-11-03 19:51:38 -0700506size_t OatWriter::WriteCodeMethod(File* file, size_t code_offset, size_t oat_class_index,
507 size_t class_def_method_index, bool is_static,
508 uint32_t method_idx, const DexFile& dex_file) {
Logan Chien8b977d32012-02-21 19:14:55 +0800509#if !defined(ART_USE_LLVM_COMPILER)
Ian Rogers0571d352011-11-03 19:51:38 -0700510 const CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800511 compiler_->GetCompiledMethod(Compiler::MethodReference(&dex_file, method_idx));
Ian Rogers0571d352011-11-03 19:51:38 -0700512
513 uint32_t frame_size_in_bytes = 0;
514 uint32_t core_spill_mask = 0;
515 uint32_t fp_spill_mask = 0;
516
517 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800518 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700519
520
521 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700522 uint32_t aligned_code_offset = compiled_method->AlignCode(code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700523 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
524 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800525 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700526 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700527 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700528 << " Expected: " << aligned_code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700529 return false;
530 }
531 code_offset += aligned_code_delta;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700532 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700533 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700534 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700535 const std::vector<uint8_t>& code = compiled_method->GetCode();
536 size_t code_size = code.size() * sizeof(code[0]);
jeffhao55d78212011-11-02 11:41:50 -0700537
538 // Deduplicate code arrays
539 size_t offset = code_offset + compiled_method->CodeDelta();
jeffhaof479dcc2011-11-02 15:54:15 -0700540 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Ian Rogers0571d352011-11-03 19:51:38 -0700541 if (code_iter != code_offsets_.end() && offset != method_offsets.code_offset_) {
542 DCHECK((code_size == 0 && method_offsets.code_offset_ == 0)
543 || code_iter->second == method_offsets.code_offset_)
544 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700545 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700546 DCHECK((code_size == 0 && method_offsets.code_offset_ == 0)
547 || offset == method_offsets.code_offset_)
548 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700549 if (!file->WriteFully(&code[0], code_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700550 ReportWriteFailure("method code", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700551 return false;
552 }
553 code_offset += code_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700554 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700555 DCHECK_CODE_OFFSET();
Ian Rogers0571d352011-11-03 19:51:38 -0700556 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
557 core_spill_mask = compiled_method->GetCoreSpillMask();
558 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700559 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700560
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700561 if (!file->WriteFully(&frame_size_in_bytes, sizeof(frame_size_in_bytes))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700562 ReportWriteFailure("method frame size", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700563 return false;
564 }
565 code_offset += sizeof(frame_size_in_bytes);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700566 if (!file->WriteFully(&core_spill_mask, sizeof(core_spill_mask))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700567 ReportWriteFailure("method core spill mask", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700568 return false;
569 }
570 code_offset += sizeof(core_spill_mask);
571 if (!file->WriteFully(&fp_spill_mask, sizeof(fp_spill_mask))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700572 ReportWriteFailure("method fp spill mask", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700573 return false;
574 }
575 code_offset += sizeof(fp_spill_mask);
576
577 if (compiled_method != NULL) {
578 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
579 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700580
581 // Deduplicate mapping tables
Ian Rogers0571d352011-11-03 19:51:38 -0700582 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
583 mapping_table_offsets_.find(&mapping_table);
584 if (mapping_iter != mapping_table_offsets_.end() &&
585 code_offset != method_offsets.mapping_table_offset_) {
586 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
587 || mapping_iter->second == method_offsets.mapping_table_offset_)
588 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700589 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700590 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
591 || code_offset == method_offsets.mapping_table_offset_)
592 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700593 if (!file->WriteFully(&mapping_table[0], mapping_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700594 ReportWriteFailure("mapping table", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700595 return false;
596 }
597 code_offset += mapping_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700598 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700599 DCHECK_CODE_OFFSET();
600
601 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
602 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700603
604 // Deduplicate vmap tables
Ian Rogers0571d352011-11-03 19:51:38 -0700605 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
606 vmap_table_offsets_.find(&vmap_table);
607 if (vmap_iter != vmap_table_offsets_.end() &&
608 code_offset != method_offsets.vmap_table_offset_) {
609 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
610 || vmap_iter->second == method_offsets.vmap_table_offset_)
611 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700612 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700613 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
614 || code_offset == method_offsets.vmap_table_offset_)
615 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700616 if (!file->WriteFully(&vmap_table[0], vmap_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700617 ReportWriteFailure("vmap table", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700618 return false;
619 }
620 code_offset += vmap_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700621 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700622 DCHECK_CODE_OFFSET();
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800623
624 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
625 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
626
627 // Deduplicate GC maps
628 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
629 gc_map_offsets_.find(&gc_map);
630 if (gc_map_iter != gc_map_offsets_.end() &&
631 code_offset != method_offsets.gc_map_offset_) {
632 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
633 || gc_map_iter->second == method_offsets.gc_map_offset_)
634 << PrettyMethod(method_idx, dex_file);
635 } else {
636 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
637 || code_offset == method_offsets.gc_map_offset_)
638 << PrettyMethod(method_idx, dex_file);
639 if (!file->WriteFully(&gc_map[0], gc_map_size)) {
640 ReportWriteFailure("GC map", method_idx, dex_file, file);
641 return false;
642 }
643 code_offset += gc_map_size;
644 }
645 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700646 }
Ian Rogers0571d352011-11-03 19:51:38 -0700647 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
648 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700649 if (compiled_invoke_stub != NULL) {
650 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
651 compiler_->GetInstructionSet());
652 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
653 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800654 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700655 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
656 PLOG(ERROR) << "Failed to seek to align invoke stub code. Actual: " << new_offset
657 << " Expected: " << aligned_code_offset;
658 return false;
659 }
660 code_offset += aligned_code_delta;
661 DCHECK_CODE_OFFSET();
662 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700663 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700664 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
665 size_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
jeffhao55d78212011-11-02 11:41:50 -0700666
667 // Deduplicate invoke stubs
Ian Rogers0571d352011-11-03 19:51:38 -0700668 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter =
669 code_offsets_.find(&invoke_stub);
670 if (stub_iter != code_offsets_.end() &&
671 code_offset != method_offsets.invoke_stub_offset_) {
672 DCHECK((invoke_stub_size == 0 && method_offsets.invoke_stub_offset_ == 0)
673 || stub_iter->second == method_offsets.invoke_stub_offset_)
674 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700675 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700676 DCHECK((invoke_stub_size == 0 && method_offsets.invoke_stub_offset_ == 0)
677 || code_offset == method_offsets.invoke_stub_offset_)
678 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700679 if (!file->WriteFully(&invoke_stub[0], invoke_stub_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700680 ReportWriteFailure("invoke stub code", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700681 return false;
682 }
683 code_offset += invoke_stub_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700684 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700685 DCHECK_CODE_OFFSET();
686 }
Logan Chien8b977d32012-02-21 19:14:55 +0800687#endif
688
Brian Carlstrome24fa612011-09-29 00:53:55 -0700689 return code_offset;
690}
691
Brian Carlstrome24fa612011-09-29 00:53:55 -0700692OatWriter::OatDexFile::OatDexFile(const DexFile& dex_file) {
Elliott Hughes95572412011-12-13 18:14:20 -0800693 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700694 dex_file_location_size_ = location.size();
695 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800696 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800697 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800698 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700699}
700
701size_t OatWriter::OatDexFile::SizeOf() const {
702 return sizeof(dex_file_location_size_)
703 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800704 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800705 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800706 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700707}
708
709void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
710 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
711 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800712 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800713 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800714 oat_header.UpdateChecksum(&methods_offsets_[0],
715 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700716}
717
718bool OatWriter::OatDexFile::Write(File* file) const {
719 if (!file->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700720 PLOG(ERROR) << "Failed to write dex file location length to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700721 return false;
722 }
723 if (!file->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700724 PLOG(ERROR) << "Failed to write dex file location data to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700725 return false;
726 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800727 if (!file->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
728 PLOG(ERROR) << "Failed to write dex file location checksum to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700729 return false;
730 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800731 if (!file->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
732 PLOG(ERROR) << "Failed to write dex file offset to " << file->name();
733 return false;
734 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800735 if (!file->WriteFully(&methods_offsets_[0],
736 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700737 PLOG(ERROR) << "Failed to write methods offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700738 return false;
739 }
740 return true;
741}
742
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800743OatWriter::OatClass::OatClass(Class::Status status, uint32_t methods_count) {
744 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700745 method_offsets_.resize(methods_count);
746}
747
Brian Carlstrom389efb02012-01-11 12:06:26 -0800748size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800749 return sizeof(status_)
750 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700751}
752
Brian Carlstrom389efb02012-01-11 12:06:26 -0800753void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800754 oat_header.UpdateChecksum(&status_, sizeof(status_));
755 oat_header.UpdateChecksum(&method_offsets_[0],
756 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700757}
758
Brian Carlstrom389efb02012-01-11 12:06:26 -0800759bool OatWriter::OatClass::Write(File* file) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800760 if (!file->WriteFully(&status_, sizeof(status_))) {
761 PLOG(ERROR) << "Failed to write class status to " << file->name();
762 return false;
763 }
764 if (!file->WriteFully(&method_offsets_[0],
765 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700766 PLOG(ERROR) << "Failed to write method offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700767 return false;
768 }
769 return true;
770}
771
772} // namespace art