blob: 5f979270800134bb18b25011903ecd03e6da24f9 [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
Elliott Hughesa72ec822012-03-05 17:12:22 -080044 size_t offset = InitOatHeader(compiler.GetInstructionSet());
Brian Carlstrome24fa612011-09-29 00:53:55 -070045 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
Elliott Hughesa72ec822012-03-05 17:12:22 -080060size_t OatWriter::InitOatHeader(InstructionSet instruction_set) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070061 // create the OatHeader
Elliott Hughesa72ec822012-03-05 17:12:22 -080062 oat_header_ = new OatHeader(instruction_set, dex_files_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070063 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 Carlstrom3320cf42011-10-04 14:58:28 -0700240
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700241 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
242 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
243 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700244
245 // Deduplicate mapping tables
jeffhaof479dcc2011-11-02 15:54:15 -0700246 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
jeffhao55d78212011-11-02 11:41:50 -0700247 if (mapping_iter != mapping_table_offsets_.end()) {
248 mapping_table_offset = mapping_iter->second;
249 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700250 mapping_table_offsets_.insert(std::pair<const std::vector<uint32_t>*, uint32_t>(&mapping_table, mapping_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700251 offset += mapping_table_size;
252 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
253 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700254
255 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
256 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
257 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700258
259 // Deduplicate vmap tables
jeffhaof479dcc2011-11-02 15:54:15 -0700260 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
jeffhao55d78212011-11-02 11:41:50 -0700261 if (vmap_iter != vmap_table_offsets_.end()) {
262 vmap_table_offset = vmap_iter->second;
263 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700264 vmap_table_offsets_.insert(std::pair<const std::vector<uint16_t>*, uint32_t>(&vmap_table, vmap_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700265 offset += vmap_table_size;
266 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
267 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800268
269 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
270 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
271 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
272
Ian Rogersc20a83e2012-01-18 18:15:32 -0800273#ifndef NDEBUG
274 // We expect GC maps except when the class hasn't been verified or the method is native
275 CompiledClass* compiled_class =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800276 compiler_->GetCompiledClass(Compiler::MethodReference(dex_file, class_def_index));
Ian Rogersc20a83e2012-01-18 18:15:32 -0800277 Class::Status status =
278 (compiled_class != NULL) ? compiled_class->GetStatus() : Class::kStatusNotReady;
279 CHECK(gc_map_size != 0 || is_native || status < Class::kStatusVerified)
280 << PrettyMethod(method_idx, *dex_file);
281#endif
282
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800283 // Deduplicate GC maps
284 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = gc_map_offsets_.find(&gc_map);
285 if (gc_map_iter != gc_map_offsets_.end()) {
286 gc_map_offset = gc_map_iter->second;
287 } else {
288 gc_map_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&gc_map, gc_map_offset));
289 offset += gc_map_size;
290 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
291 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700292 }
293
Ian Rogers0571d352011-11-03 19:51:38 -0700294 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx));
295 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700296 if (compiled_invoke_stub != NULL) {
297 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
Elliott Hughes06b37d92011-10-16 11:51:29 -0700298 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700299 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
300 size_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
301 invoke_stub_offset = (invoke_stub_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700302
303 // Deduplicate invoke stubs
jeffhaof479dcc2011-11-02 15:54:15 -0700304 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&invoke_stub);
jeffhao55d78212011-11-02 11:41:50 -0700305 if (stub_iter != code_offsets_.end()) {
306 invoke_stub_offset = stub_iter->second;
307 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700308 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&invoke_stub, invoke_stub_offset));
jeffhao55d78212011-11-02 11:41:50 -0700309 offset += invoke_stub_size;
310 oat_header_->UpdateChecksum(&invoke_stub[0], invoke_stub_size);
311 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700312 }
313
Brian Carlstrom389efb02012-01-11 12:06:26 -0800314 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index]
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700315 = OatMethodOffsets(code_offset,
316 frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700317 core_spill_mask,
318 fp_spill_mask,
319 mapping_table_offset,
320 vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800321 gc_map_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700322 invoke_stub_offset);
323
Ian Rogers0571d352011-11-03 19:51:38 -0700324 if (compiler_->IsImage()) {
325 ClassLinker* linker = Runtime::Current()->GetClassLinker();
326 DexCache* dex_cache = linker->FindDexCache(*dex_file);
327 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader_,
328 is_direct);
329 CHECK(method != NULL);
330 method->SetFrameSizeInBytes(frame_size_in_bytes);
331 method->SetCoreSpillMask(core_spill_mask);
332 method->SetFpSpillMask(fp_spill_mask);
333 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800334 // Don't overwrite static method trampoline
335 if (!method->IsStatic() || method->IsConstructor() ||
336 method->GetDeclaringClass()->IsInitialized()) {
337 method->SetOatCodeOffset(code_offset);
338 } else {
339 method->SetCode(Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod)->GetData());
340 }
Ian Rogers0571d352011-11-03 19:51:38 -0700341 method->SetOatVmapTableOffset(vmap_table_offset);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800342 method->SetOatGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700343 method->SetOatInvokeStubOffset(invoke_stub_offset);
344 }
Logan Chien8b977d32012-02-21 19:14:55 +0800345#endif
346
Brian Carlstrome24fa612011-09-29 00:53:55 -0700347 return offset;
348}
349
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700350#define DCHECK_CODE_OFFSET() \
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800351 DCHECK_EQ(static_cast<off_t>(code_offset), lseek(file->Fd(), 0, SEEK_CUR))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700352
Elliott Hughes234da572011-11-03 22:13:06 -0700353bool OatWriter::Write(File* file) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700354 if (!file->WriteFully(oat_header_, sizeof(*oat_header_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700355 PLOG(ERROR) << "Failed to write oat header to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700356 return false;
357 }
358
Elliott Hughes234da572011-11-03 22:13:06 -0700359 if (!WriteTables(file)) {
360 LOG(ERROR) << "Failed to write oat tables to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700361 return false;
362 }
363
Elliott Hughes234da572011-11-03 22:13:06 -0700364 size_t code_offset = WriteCode(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700365 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700366 LOG(ERROR) << "Failed to write oat code to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700367 return false;
368 }
369
Elliott Hughes234da572011-11-03 22:13:06 -0700370 code_offset = WriteCodeDexFiles(file, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700371 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700372 LOG(ERROR) << "Failed to write oat code for dex files to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700373 return false;
374 }
375
376 return true;
377}
378
379bool OatWriter::WriteTables(File* file) {
380 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
381 if (!oat_dex_files_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700382 PLOG(ERROR) << "Failed to write oat dex information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700383 return false;
384 }
385 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800386 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
387 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800388 off_t actual_offset = lseek(file->Fd(), expected_offset, SEEK_SET);
Brian Carlstrom89521892011-12-07 22:05:07 -0800389 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
390 const DexFile* dex_file = (*dex_files_)[i];
391 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
392 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
393 return false;
394 }
395 const DexFile* dex_file = (*dex_files_)[i];
396 if (!file->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
397 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << file->name();
398 return false;
399 }
400 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800401 for (size_t i = 0; i != oat_classes_.size(); ++i) {
402 if (!oat_classes_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700403 PLOG(ERROR) << "Failed to write oat methods information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700404 return false;
405 }
406 }
407 return true;
408}
409
410size_t OatWriter::WriteCode(File* file) {
411 uint32_t code_offset = oat_header_->GetExecutableOffset();
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800412 off_t new_offset = lseek(file->Fd(), executable_offset_padding_length_, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700413 if (static_cast<uint32_t>(new_offset) != code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700414 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700415 << " Expected: " << code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700416 return 0;
417 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700418 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700419 return code_offset;
420}
421
422size_t OatWriter::WriteCodeDexFiles(File* file, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700423 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800424 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700425 const DexFile* dex_file = (*dex_files_)[i];
426 CHECK(dex_file != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700427 code_offset = WriteCodeDexFile(file, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700428 if (code_offset == 0) {
429 return 0;
430 }
431 }
432 return code_offset;
433}
434
Ian Rogers0571d352011-11-03 19:51:38 -0700435size_t OatWriter::WriteCodeDexFile(File* file, size_t code_offset, size_t& oat_class_index,
436 const DexFile& dex_file) {
437 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
438 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700439 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700440 code_offset = WriteCodeClassDef(file, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700441 if (code_offset == 0) {
442 return 0;
443 }
444 }
445 return code_offset;
446}
447
Ian Rogers0571d352011-11-03 19:51:38 -0700448void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
449 const DexFile& dex_file, File* f) const {
450 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
451 << " to " << f->name();
Elliott Hughes234da572011-11-03 22:13:06 -0700452}
453
Brian Carlstrome24fa612011-09-29 00:53:55 -0700454size_t OatWriter::WriteCodeClassDef(File* file,
Ian Rogers0571d352011-11-03 19:51:38 -0700455 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700456 const DexFile& dex_file,
457 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700458 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700459 if (class_data == NULL) {
460 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700461 return code_offset;
462 }
Ian Rogers0571d352011-11-03 19:51:38 -0700463 ClassDataItemIterator it(dex_file, class_data);
464 // Skip fields
465 while (it.HasNextStaticField()) {
466 it.Next();
467 }
468 while (it.HasNextInstanceField()) {
469 it.Next();
470 }
471 // Process methods
472 size_t class_def_method_index = 0;
473 while (it.HasNextDirectMethod()) {
474 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
475 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
476 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700477 if (code_offset == 0) {
478 return 0;
479 }
Ian Rogers0571d352011-11-03 19:51:38 -0700480 class_def_method_index++;
481 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700482 }
Ian Rogers0571d352011-11-03 19:51:38 -0700483 while (it.HasNextVirtualMethod()) {
484 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
485 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700486 if (code_offset == 0) {
487 return 0;
488 }
Ian Rogers0571d352011-11-03 19:51:38 -0700489 class_def_method_index++;
490 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700491 }
492 return code_offset;
493}
494
Ian Rogers0571d352011-11-03 19:51:38 -0700495size_t OatWriter::WriteCodeMethod(File* file, size_t code_offset, size_t oat_class_index,
496 size_t class_def_method_index, bool is_static,
497 uint32_t method_idx, const DexFile& dex_file) {
Logan Chien8b977d32012-02-21 19:14:55 +0800498#if !defined(ART_USE_LLVM_COMPILER)
Ian Rogers0571d352011-11-03 19:51:38 -0700499 const CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800500 compiler_->GetCompiledMethod(Compiler::MethodReference(&dex_file, method_idx));
Ian Rogers0571d352011-11-03 19:51:38 -0700501
502 uint32_t frame_size_in_bytes = 0;
503 uint32_t core_spill_mask = 0;
504 uint32_t fp_spill_mask = 0;
505
506 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800507 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700508
509
510 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700511 uint32_t aligned_code_offset = compiled_method->AlignCode(code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700512 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
513 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800514 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700515 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700516 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700517 << " Expected: " << aligned_code_offset << " File: " << file->name();
Brian Carlstromf03c2882012-03-05 20:29:06 -0800518 return 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700519 }
520 code_offset += aligned_code_delta;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700521 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700522 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700523 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700524 const std::vector<uint8_t>& code = compiled_method->GetCode();
525 size_t code_size = code.size() * sizeof(code[0]);
jeffhao55d78212011-11-02 11:41:50 -0700526
527 // Deduplicate code arrays
528 size_t offset = code_offset + compiled_method->CodeDelta();
jeffhaof479dcc2011-11-02 15:54:15 -0700529 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Ian Rogers0571d352011-11-03 19:51:38 -0700530 if (code_iter != code_offsets_.end() && offset != method_offsets.code_offset_) {
531 DCHECK((code_size == 0 && method_offsets.code_offset_ == 0)
532 || code_iter->second == method_offsets.code_offset_)
533 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700534 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700535 DCHECK((code_size == 0 && method_offsets.code_offset_ == 0)
536 || offset == method_offsets.code_offset_)
537 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700538 if (!file->WriteFully(&code[0], code_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700539 ReportWriteFailure("method code", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800540 return 0;
jeffhao55d78212011-11-02 11:41:50 -0700541 }
542 code_offset += code_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700543 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700544 DCHECK_CODE_OFFSET();
Ian Rogers0571d352011-11-03 19:51:38 -0700545 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
546 core_spill_mask = compiled_method->GetCoreSpillMask();
547 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700548
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700549 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
550 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700551
552 // Deduplicate mapping tables
Ian Rogers0571d352011-11-03 19:51:38 -0700553 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
554 mapping_table_offsets_.find(&mapping_table);
555 if (mapping_iter != mapping_table_offsets_.end() &&
556 code_offset != method_offsets.mapping_table_offset_) {
557 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
558 || mapping_iter->second == method_offsets.mapping_table_offset_)
559 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700560 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700561 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
562 || code_offset == method_offsets.mapping_table_offset_)
563 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700564 if (!file->WriteFully(&mapping_table[0], mapping_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700565 ReportWriteFailure("mapping table", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800566 return 0;
jeffhao55d78212011-11-02 11:41:50 -0700567 }
568 code_offset += mapping_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700569 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700570 DCHECK_CODE_OFFSET();
571
572 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
573 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700574
575 // Deduplicate vmap tables
Ian Rogers0571d352011-11-03 19:51:38 -0700576 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
577 vmap_table_offsets_.find(&vmap_table);
578 if (vmap_iter != vmap_table_offsets_.end() &&
579 code_offset != method_offsets.vmap_table_offset_) {
580 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
581 || vmap_iter->second == method_offsets.vmap_table_offset_)
582 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700583 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700584 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
585 || code_offset == method_offsets.vmap_table_offset_)
586 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700587 if (!file->WriteFully(&vmap_table[0], vmap_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700588 ReportWriteFailure("vmap table", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800589 return 0;
jeffhao55d78212011-11-02 11:41:50 -0700590 }
591 code_offset += vmap_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700592 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700593 DCHECK_CODE_OFFSET();
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800594
595 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
596 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
597
598 // Deduplicate GC maps
599 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
600 gc_map_offsets_.find(&gc_map);
601 if (gc_map_iter != gc_map_offsets_.end() &&
602 code_offset != method_offsets.gc_map_offset_) {
603 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
604 || gc_map_iter->second == method_offsets.gc_map_offset_)
605 << PrettyMethod(method_idx, dex_file);
606 } else {
607 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
608 || code_offset == method_offsets.gc_map_offset_)
609 << PrettyMethod(method_idx, dex_file);
610 if (!file->WriteFully(&gc_map[0], gc_map_size)) {
611 ReportWriteFailure("GC map", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800612 return 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800613 }
614 code_offset += gc_map_size;
615 }
616 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700617 }
Ian Rogers0571d352011-11-03 19:51:38 -0700618 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
619 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700620 if (compiled_invoke_stub != NULL) {
621 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
622 compiler_->GetInstructionSet());
623 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
624 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800625 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700626 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
627 PLOG(ERROR) << "Failed to seek to align invoke stub code. Actual: " << new_offset
628 << " Expected: " << aligned_code_offset;
Brian Carlstromf03c2882012-03-05 20:29:06 -0800629 return 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700630 }
631 code_offset += aligned_code_delta;
632 DCHECK_CODE_OFFSET();
633 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700634 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700635 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
636 size_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
jeffhao55d78212011-11-02 11:41:50 -0700637
638 // Deduplicate invoke stubs
Ian Rogers0571d352011-11-03 19:51:38 -0700639 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter =
640 code_offsets_.find(&invoke_stub);
641 if (stub_iter != code_offsets_.end() &&
642 code_offset != method_offsets.invoke_stub_offset_) {
643 DCHECK((invoke_stub_size == 0 && method_offsets.invoke_stub_offset_ == 0)
644 || stub_iter->second == method_offsets.invoke_stub_offset_)
645 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700646 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700647 DCHECK((invoke_stub_size == 0 && method_offsets.invoke_stub_offset_ == 0)
648 || code_offset == method_offsets.invoke_stub_offset_)
649 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700650 if (!file->WriteFully(&invoke_stub[0], invoke_stub_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700651 ReportWriteFailure("invoke stub code", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800652 return 0;
jeffhao55d78212011-11-02 11:41:50 -0700653 }
654 code_offset += invoke_stub_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700655 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700656 DCHECK_CODE_OFFSET();
657 }
Logan Chien8b977d32012-02-21 19:14:55 +0800658#endif
659
Brian Carlstrome24fa612011-09-29 00:53:55 -0700660 return code_offset;
661}
662
Brian Carlstrome24fa612011-09-29 00:53:55 -0700663OatWriter::OatDexFile::OatDexFile(const DexFile& dex_file) {
Elliott Hughes95572412011-12-13 18:14:20 -0800664 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700665 dex_file_location_size_ = location.size();
666 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800667 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800668 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800669 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700670}
671
672size_t OatWriter::OatDexFile::SizeOf() const {
673 return sizeof(dex_file_location_size_)
674 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800675 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800676 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800677 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700678}
679
680void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
681 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
682 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800683 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800684 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800685 oat_header.UpdateChecksum(&methods_offsets_[0],
686 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700687}
688
689bool OatWriter::OatDexFile::Write(File* file) const {
690 if (!file->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700691 PLOG(ERROR) << "Failed to write dex file location length to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700692 return false;
693 }
694 if (!file->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700695 PLOG(ERROR) << "Failed to write dex file location data to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700696 return false;
697 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800698 if (!file->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
699 PLOG(ERROR) << "Failed to write dex file location checksum to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700700 return false;
701 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800702 if (!file->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
703 PLOG(ERROR) << "Failed to write dex file offset to " << file->name();
704 return false;
705 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800706 if (!file->WriteFully(&methods_offsets_[0],
707 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700708 PLOG(ERROR) << "Failed to write methods offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700709 return false;
710 }
711 return true;
712}
713
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800714OatWriter::OatClass::OatClass(Class::Status status, uint32_t methods_count) {
715 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700716 method_offsets_.resize(methods_count);
717}
718
Brian Carlstrom389efb02012-01-11 12:06:26 -0800719size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800720 return sizeof(status_)
721 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700722}
723
Brian Carlstrom389efb02012-01-11 12:06:26 -0800724void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800725 oat_header.UpdateChecksum(&status_, sizeof(status_));
726 oat_header.UpdateChecksum(&method_offsets_[0],
727 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700728}
729
Brian Carlstrom389efb02012-01-11 12:06:26 -0800730bool OatWriter::OatClass::Write(File* file) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800731 if (!file->WriteFully(&status_, sizeof(status_))) {
732 PLOG(ERROR) << "Failed to write class status to " << file->name();
733 return false;
734 }
735 if (!file->WriteFully(&method_offsets_[0],
736 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700737 PLOG(ERROR) << "Failed to write method offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700738 return false;
739 }
740 return true;
741}
742
743} // namespace art