blob: e4c8135fecdaf9729b83fa4d6ce429927fe9efe7 [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);
343 method->SetOatCodeOffset(code_offset);
344 method->SetOatVmapTableOffset(vmap_table_offset);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800345 method->SetOatGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700346 method->SetOatInvokeStubOffset(invoke_stub_offset);
347 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700348 return offset;
349}
350
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700351#define DCHECK_CODE_OFFSET() \
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800352 DCHECK_EQ(static_cast<off_t>(code_offset), lseek(file->Fd(), 0, SEEK_CUR))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700353
Elliott Hughes234da572011-11-03 22:13:06 -0700354bool OatWriter::Write(File* file) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700355 if (!file->WriteFully(oat_header_, sizeof(*oat_header_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700356 PLOG(ERROR) << "Failed to write oat header to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700357 return false;
358 }
359
Elliott Hughes234da572011-11-03 22:13:06 -0700360 if (!WriteTables(file)) {
361 LOG(ERROR) << "Failed to write oat tables to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700362 return false;
363 }
364
Elliott Hughes234da572011-11-03 22:13:06 -0700365 size_t code_offset = WriteCode(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700366 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700367 LOG(ERROR) << "Failed to write oat code to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700368 return false;
369 }
370
Elliott Hughes234da572011-11-03 22:13:06 -0700371 code_offset = WriteCodeDexFiles(file, code_offset);
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 for dex files to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700374 return false;
375 }
376
377 return true;
378}
379
380bool OatWriter::WriteTables(File* file) {
381 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
382 if (!oat_dex_files_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700383 PLOG(ERROR) << "Failed to write oat dex information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700384 return false;
385 }
386 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800387 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
388 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800389 off_t actual_offset = lseek(file->Fd(), expected_offset, SEEK_SET);
Brian Carlstrom89521892011-12-07 22:05:07 -0800390 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
391 const DexFile* dex_file = (*dex_files_)[i];
392 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
393 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
394 return false;
395 }
396 const DexFile* dex_file = (*dex_files_)[i];
397 if (!file->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
398 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << file->name();
399 return false;
400 }
401 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800402 for (size_t i = 0; i != oat_classes_.size(); ++i) {
403 if (!oat_classes_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700404 PLOG(ERROR) << "Failed to write oat methods information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700405 return false;
406 }
407 }
408 return true;
409}
410
411size_t OatWriter::WriteCode(File* file) {
412 uint32_t code_offset = oat_header_->GetExecutableOffset();
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800413 off_t new_offset = lseek(file->Fd(), executable_offset_padding_length_, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700414 if (static_cast<uint32_t>(new_offset) != code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700415 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700416 << " Expected: " << code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700417 return 0;
418 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700419 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700420 return code_offset;
421}
422
423size_t OatWriter::WriteCodeDexFiles(File* file, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700424 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800425 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700426 const DexFile* dex_file = (*dex_files_)[i];
427 CHECK(dex_file != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700428 code_offset = WriteCodeDexFile(file, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700429 if (code_offset == 0) {
430 return 0;
431 }
432 }
433 return code_offset;
434}
435
Ian Rogers0571d352011-11-03 19:51:38 -0700436size_t OatWriter::WriteCodeDexFile(File* file, size_t code_offset, size_t& oat_class_index,
437 const DexFile& dex_file) {
438 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
439 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700440 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700441 code_offset = WriteCodeClassDef(file, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700442 if (code_offset == 0) {
443 return 0;
444 }
445 }
446 return code_offset;
447}
448
Ian Rogers0571d352011-11-03 19:51:38 -0700449void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
450 const DexFile& dex_file, File* f) const {
451 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
452 << " to " << f->name();
Elliott Hughes234da572011-11-03 22:13:06 -0700453}
454
Brian Carlstrome24fa612011-09-29 00:53:55 -0700455size_t OatWriter::WriteCodeClassDef(File* file,
Ian Rogers0571d352011-11-03 19:51:38 -0700456 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700457 const DexFile& dex_file,
458 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700459 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700460 if (class_data == NULL) {
461 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700462 return code_offset;
463 }
Ian Rogers0571d352011-11-03 19:51:38 -0700464 ClassDataItemIterator it(dex_file, class_data);
465 // Skip fields
466 while (it.HasNextStaticField()) {
467 it.Next();
468 }
469 while (it.HasNextInstanceField()) {
470 it.Next();
471 }
472 // Process methods
473 size_t class_def_method_index = 0;
474 while (it.HasNextDirectMethod()) {
475 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
476 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
477 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700478 if (code_offset == 0) {
479 return 0;
480 }
Ian Rogers0571d352011-11-03 19:51:38 -0700481 class_def_method_index++;
482 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700483 }
Ian Rogers0571d352011-11-03 19:51:38 -0700484 while (it.HasNextVirtualMethod()) {
485 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
486 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700487 if (code_offset == 0) {
488 return 0;
489 }
Ian Rogers0571d352011-11-03 19:51:38 -0700490 class_def_method_index++;
491 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700492 }
493 return code_offset;
494}
495
Ian Rogers0571d352011-11-03 19:51:38 -0700496size_t OatWriter::WriteCodeMethod(File* file, size_t code_offset, size_t oat_class_index,
497 size_t class_def_method_index, bool is_static,
498 uint32_t method_idx, const DexFile& dex_file) {
499 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 Carlstrome24fa612011-09-29 00:53:55 -0700518 return false;
519 }
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);
jeffhao55d78212011-11-02 11:41:50 -0700540 return false;
541 }
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 Carlstrome24fa612011-09-29 00:53:55 -0700548 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700549
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700550 if (!file->WriteFully(&frame_size_in_bytes, sizeof(frame_size_in_bytes))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700551 ReportWriteFailure("method frame size", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700552 return false;
553 }
554 code_offset += sizeof(frame_size_in_bytes);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700555 if (!file->WriteFully(&core_spill_mask, sizeof(core_spill_mask))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700556 ReportWriteFailure("method core spill mask", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700557 return false;
558 }
559 code_offset += sizeof(core_spill_mask);
560 if (!file->WriteFully(&fp_spill_mask, sizeof(fp_spill_mask))) {
Ian Rogers0571d352011-11-03 19:51:38 -0700561 ReportWriteFailure("method fp spill mask", method_idx, dex_file, file);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700562 return false;
563 }
564 code_offset += sizeof(fp_spill_mask);
565
566 if (compiled_method != NULL) {
567 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
568 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700569
570 // Deduplicate mapping tables
Ian Rogers0571d352011-11-03 19:51:38 -0700571 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
572 mapping_table_offsets_.find(&mapping_table);
573 if (mapping_iter != mapping_table_offsets_.end() &&
574 code_offset != method_offsets.mapping_table_offset_) {
575 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
576 || mapping_iter->second == method_offsets.mapping_table_offset_)
577 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700578 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700579 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
580 || code_offset == method_offsets.mapping_table_offset_)
581 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700582 if (!file->WriteFully(&mapping_table[0], mapping_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700583 ReportWriteFailure("mapping table", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700584 return false;
585 }
586 code_offset += mapping_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700587 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700588 DCHECK_CODE_OFFSET();
589
590 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
591 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700592
593 // Deduplicate vmap tables
Ian Rogers0571d352011-11-03 19:51:38 -0700594 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
595 vmap_table_offsets_.find(&vmap_table);
596 if (vmap_iter != vmap_table_offsets_.end() &&
597 code_offset != method_offsets.vmap_table_offset_) {
598 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
599 || vmap_iter->second == method_offsets.vmap_table_offset_)
600 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700601 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700602 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
603 || code_offset == method_offsets.vmap_table_offset_)
604 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700605 if (!file->WriteFully(&vmap_table[0], vmap_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700606 ReportWriteFailure("vmap table", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700607 return false;
608 }
609 code_offset += vmap_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700610 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700611 DCHECK_CODE_OFFSET();
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800612
613 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
614 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
615
616 // Deduplicate GC maps
617 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
618 gc_map_offsets_.find(&gc_map);
619 if (gc_map_iter != gc_map_offsets_.end() &&
620 code_offset != method_offsets.gc_map_offset_) {
621 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
622 || gc_map_iter->second == method_offsets.gc_map_offset_)
623 << PrettyMethod(method_idx, dex_file);
624 } else {
625 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
626 || code_offset == method_offsets.gc_map_offset_)
627 << PrettyMethod(method_idx, dex_file);
628 if (!file->WriteFully(&gc_map[0], gc_map_size)) {
629 ReportWriteFailure("GC map", method_idx, dex_file, file);
630 return false;
631 }
632 code_offset += gc_map_size;
633 }
634 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700635 }
Ian Rogers0571d352011-11-03 19:51:38 -0700636 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
637 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700638 if (compiled_invoke_stub != NULL) {
639 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
640 compiler_->GetInstructionSet());
641 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
642 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800643 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700644 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
645 PLOG(ERROR) << "Failed to seek to align invoke stub code. Actual: " << new_offset
646 << " Expected: " << aligned_code_offset;
647 return false;
648 }
649 code_offset += aligned_code_delta;
650 DCHECK_CODE_OFFSET();
651 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700652 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700653 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
654 size_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
jeffhao55d78212011-11-02 11:41:50 -0700655
656 // Deduplicate invoke stubs
Ian Rogers0571d352011-11-03 19:51:38 -0700657 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter =
658 code_offsets_.find(&invoke_stub);
659 if (stub_iter != code_offsets_.end() &&
660 code_offset != method_offsets.invoke_stub_offset_) {
661 DCHECK((invoke_stub_size == 0 && method_offsets.invoke_stub_offset_ == 0)
662 || stub_iter->second == method_offsets.invoke_stub_offset_)
663 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700664 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700665 DCHECK((invoke_stub_size == 0 && method_offsets.invoke_stub_offset_ == 0)
666 || code_offset == method_offsets.invoke_stub_offset_)
667 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700668 if (!file->WriteFully(&invoke_stub[0], invoke_stub_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700669 ReportWriteFailure("invoke stub code", method_idx, dex_file, file);
jeffhao55d78212011-11-02 11:41:50 -0700670 return false;
671 }
672 code_offset += invoke_stub_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700673 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700674 DCHECK_CODE_OFFSET();
675 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700676 return code_offset;
677}
678
Brian Carlstrome24fa612011-09-29 00:53:55 -0700679OatWriter::OatDexFile::OatDexFile(const DexFile& dex_file) {
Elliott Hughes95572412011-12-13 18:14:20 -0800680 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700681 dex_file_location_size_ = location.size();
682 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800683 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800684 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800685 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700686}
687
688size_t OatWriter::OatDexFile::SizeOf() const {
689 return sizeof(dex_file_location_size_)
690 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800691 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800692 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800693 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700694}
695
696void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
697 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
698 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800699 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800700 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800701 oat_header.UpdateChecksum(&methods_offsets_[0],
702 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700703}
704
705bool OatWriter::OatDexFile::Write(File* file) const {
706 if (!file->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700707 PLOG(ERROR) << "Failed to write dex file location length to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700708 return false;
709 }
710 if (!file->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700711 PLOG(ERROR) << "Failed to write dex file location data to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700712 return false;
713 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800714 if (!file->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
715 PLOG(ERROR) << "Failed to write dex file location checksum to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700716 return false;
717 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800718 if (!file->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
719 PLOG(ERROR) << "Failed to write dex file offset to " << file->name();
720 return false;
721 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800722 if (!file->WriteFully(&methods_offsets_[0],
723 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700724 PLOG(ERROR) << "Failed to write methods offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700725 return false;
726 }
727 return true;
728}
729
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800730OatWriter::OatClass::OatClass(Class::Status status, uint32_t methods_count) {
731 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700732 method_offsets_.resize(methods_count);
733}
734
Brian Carlstrom389efb02012-01-11 12:06:26 -0800735size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800736 return sizeof(status_)
737 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700738}
739
Brian Carlstrom389efb02012-01-11 12:06:26 -0800740void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800741 oat_header.UpdateChecksum(&status_, sizeof(status_));
742 oat_header.UpdateChecksum(&method_offsets_[0],
743 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700744}
745
Brian Carlstrom389efb02012-01-11 12:06:26 -0800746bool OatWriter::OatClass::Write(File* file) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800747 if (!file->WriteFully(&status_, sizeof(status_))) {
748 PLOG(ERROR) << "Failed to write class status to " << file->name();
749 return false;
750 }
751 if (!file->WriteFully(&method_offsets_[0],
752 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700753 PLOG(ERROR) << "Failed to write method offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700754 return false;
755 }
756 return true;
757}
758
759} // namespace art