blob: 1ac247eebe5252c435a3695bc8aa315b343d8f75 [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();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700223 uint32_t code_size = code.size() * sizeof(code[0]);
224 CHECK_NE(code_size, 0U);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700225 uint32_t thumb_offset = compiled_method->CodeDelta();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700226 code_offset = offset + sizeof(code_size) + thumb_offset;
jeffhao55d78212011-11-02 11:41:50 -0700227
228 // Deduplicate code arrays
jeffhaof479dcc2011-11-02 15:54:15 -0700229 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
jeffhao55d78212011-11-02 11:41:50 -0700230 if (code_iter != code_offsets_.end()) {
231 code_offset = code_iter->second;
232 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700233 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&code, code_offset));
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700234 offset += sizeof(code_size); // code size is prepended before code
jeffhao55d78212011-11-02 11:41:50 -0700235 offset += code_size;
236 oat_header_->UpdateChecksum(&code[0], code_size);
237 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700238 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700239 core_spill_mask = compiled_method->GetCoreSpillMask();
240 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700241
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700242 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
243 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
244 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700245
246 // Deduplicate mapping tables
jeffhaof479dcc2011-11-02 15:54:15 -0700247 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
jeffhao55d78212011-11-02 11:41:50 -0700248 if (mapping_iter != mapping_table_offsets_.end()) {
249 mapping_table_offset = mapping_iter->second;
250 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700251 mapping_table_offsets_.insert(std::pair<const std::vector<uint32_t>*, uint32_t>(&mapping_table, mapping_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700252 offset += mapping_table_size;
253 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
254 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700255
256 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
257 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
258 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700259
260 // Deduplicate vmap tables
jeffhaof479dcc2011-11-02 15:54:15 -0700261 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
jeffhao55d78212011-11-02 11:41:50 -0700262 if (vmap_iter != vmap_table_offsets_.end()) {
263 vmap_table_offset = vmap_iter->second;
264 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700265 vmap_table_offsets_.insert(std::pair<const std::vector<uint16_t>*, uint32_t>(&vmap_table, vmap_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700266 offset += vmap_table_size;
267 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
268 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800269
270 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
271 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
272 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
273
Ian Rogersc20a83e2012-01-18 18:15:32 -0800274#ifndef NDEBUG
275 // We expect GC maps except when the class hasn't been verified or the method is native
276 CompiledClass* compiled_class =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800277 compiler_->GetCompiledClass(Compiler::MethodReference(dex_file, class_def_index));
Ian Rogersc20a83e2012-01-18 18:15:32 -0800278 Class::Status status =
279 (compiled_class != NULL) ? compiled_class->GetStatus() : Class::kStatusNotReady;
280 CHECK(gc_map_size != 0 || is_native || status < Class::kStatusVerified)
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800281 << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " " << (status < Class::kStatusVerified) << " " << status << " " << PrettyMethod(method_idx, *dex_file);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800282#endif
283
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800284 // Deduplicate GC maps
285 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = gc_map_offsets_.find(&gc_map);
286 if (gc_map_iter != gc_map_offsets_.end()) {
287 gc_map_offset = gc_map_iter->second;
288 } else {
289 gc_map_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&gc_map, gc_map_offset));
290 offset += gc_map_size;
291 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
292 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700293 }
294
Ian Rogers0571d352011-11-03 19:51:38 -0700295 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx));
296 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700297 if (compiled_invoke_stub != NULL) {
298 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
Elliott Hughes06b37d92011-10-16 11:51:29 -0700299 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700300 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700301 uint32_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
302 CHECK_NE(invoke_stub_size, 0U);
303 invoke_stub_offset = offset + sizeof(invoke_stub_size);
jeffhao55d78212011-11-02 11:41:50 -0700304
305 // Deduplicate invoke stubs
jeffhaof479dcc2011-11-02 15:54:15 -0700306 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&invoke_stub);
jeffhao55d78212011-11-02 11:41:50 -0700307 if (stub_iter != code_offsets_.end()) {
308 invoke_stub_offset = stub_iter->second;
309 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700310 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&invoke_stub, invoke_stub_offset));
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700311 offset += sizeof(invoke_stub_size); // invoke stub size is prepended before code
jeffhao55d78212011-11-02 11:41:50 -0700312 offset += invoke_stub_size;
313 oat_header_->UpdateChecksum(&invoke_stub[0], invoke_stub_size);
314 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700315 }
316
Brian Carlstrom389efb02012-01-11 12:06:26 -0800317 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index]
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700318 = OatMethodOffsets(code_offset,
319 frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700320 core_spill_mask,
321 fp_spill_mask,
322 mapping_table_offset,
323 vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800324 gc_map_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700325 invoke_stub_offset);
326
Ian Rogers0571d352011-11-03 19:51:38 -0700327 if (compiler_->IsImage()) {
328 ClassLinker* linker = Runtime::Current()->GetClassLinker();
329 DexCache* dex_cache = linker->FindDexCache(*dex_file);
330 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader_,
331 is_direct);
332 CHECK(method != NULL);
333 method->SetFrameSizeInBytes(frame_size_in_bytes);
334 method->SetCoreSpillMask(core_spill_mask);
335 method->SetFpSpillMask(fp_spill_mask);
336 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800337 // Don't overwrite static method trampoline
338 if (!method->IsStatic() || method->IsConstructor() ||
339 method->GetDeclaringClass()->IsInitialized()) {
340 method->SetOatCodeOffset(code_offset);
341 } else {
342 method->SetCode(Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod)->GetData());
343 }
Ian Rogers0571d352011-11-03 19:51:38 -0700344 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 }
Logan Chien8b977d32012-02-21 19:14:55 +0800348#endif
349
Brian Carlstrome24fa612011-09-29 00:53:55 -0700350 return offset;
351}
352
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700353#define DCHECK_CODE_OFFSET() \
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800354 DCHECK_EQ(static_cast<off_t>(code_offset), lseek(file->Fd(), 0, SEEK_CUR))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700355
Elliott Hughes234da572011-11-03 22:13:06 -0700356bool OatWriter::Write(File* file) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700357 if (!file->WriteFully(oat_header_, sizeof(*oat_header_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700358 PLOG(ERROR) << "Failed to write oat header to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700359 return false;
360 }
361
Elliott Hughes234da572011-11-03 22:13:06 -0700362 if (!WriteTables(file)) {
363 LOG(ERROR) << "Failed to write oat tables to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700364 return false;
365 }
366
Elliott Hughes234da572011-11-03 22:13:06 -0700367 size_t code_offset = WriteCode(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700368 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700369 LOG(ERROR) << "Failed to write oat code to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700370 return false;
371 }
372
Elliott Hughes234da572011-11-03 22:13:06 -0700373 code_offset = WriteCodeDexFiles(file, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700374 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700375 LOG(ERROR) << "Failed to write oat code for dex files to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700376 return false;
377 }
378
379 return true;
380}
381
382bool OatWriter::WriteTables(File* file) {
383 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
384 if (!oat_dex_files_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700385 PLOG(ERROR) << "Failed to write oat dex information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700386 return false;
387 }
388 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800389 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
390 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800391 off_t actual_offset = lseek(file->Fd(), expected_offset, SEEK_SET);
Brian Carlstrom89521892011-12-07 22:05:07 -0800392 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
393 const DexFile* dex_file = (*dex_files_)[i];
394 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
395 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
396 return false;
397 }
398 const DexFile* dex_file = (*dex_files_)[i];
399 if (!file->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
400 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << file->name();
401 return false;
402 }
403 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800404 for (size_t i = 0; i != oat_classes_.size(); ++i) {
405 if (!oat_classes_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700406 PLOG(ERROR) << "Failed to write oat methods information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700407 return false;
408 }
409 }
410 return true;
411}
412
413size_t OatWriter::WriteCode(File* file) {
414 uint32_t code_offset = oat_header_->GetExecutableOffset();
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800415 off_t new_offset = lseek(file->Fd(), executable_offset_padding_length_, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700416 if (static_cast<uint32_t>(new_offset) != code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700417 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700418 << " Expected: " << code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700419 return 0;
420 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700421 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700422 return code_offset;
423}
424
425size_t OatWriter::WriteCodeDexFiles(File* file, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700426 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800427 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700428 const DexFile* dex_file = (*dex_files_)[i];
429 CHECK(dex_file != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700430 code_offset = WriteCodeDexFile(file, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700431 if (code_offset == 0) {
432 return 0;
433 }
434 }
435 return code_offset;
436}
437
Ian Rogers0571d352011-11-03 19:51:38 -0700438size_t OatWriter::WriteCodeDexFile(File* file, size_t code_offset, size_t& oat_class_index,
439 const DexFile& dex_file) {
440 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
441 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700442 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700443 code_offset = WriteCodeClassDef(file, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700444 if (code_offset == 0) {
445 return 0;
446 }
447 }
448 return code_offset;
449}
450
Ian Rogers0571d352011-11-03 19:51:38 -0700451void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
452 const DexFile& dex_file, File* f) const {
453 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
454 << " to " << f->name();
Elliott Hughes234da572011-11-03 22:13:06 -0700455}
456
Brian Carlstrome24fa612011-09-29 00:53:55 -0700457size_t OatWriter::WriteCodeClassDef(File* file,
Ian Rogers0571d352011-11-03 19:51:38 -0700458 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700459 const DexFile& dex_file,
460 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700461 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700462 if (class_data == NULL) {
463 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700464 return code_offset;
465 }
Ian Rogers0571d352011-11-03 19:51:38 -0700466 ClassDataItemIterator it(dex_file, class_data);
467 // Skip fields
468 while (it.HasNextStaticField()) {
469 it.Next();
470 }
471 while (it.HasNextInstanceField()) {
472 it.Next();
473 }
474 // Process methods
475 size_t class_def_method_index = 0;
476 while (it.HasNextDirectMethod()) {
477 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
478 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
479 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700480 if (code_offset == 0) {
481 return 0;
482 }
Ian Rogers0571d352011-11-03 19:51:38 -0700483 class_def_method_index++;
484 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700485 }
Ian Rogers0571d352011-11-03 19:51:38 -0700486 while (it.HasNextVirtualMethod()) {
487 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
488 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700489 if (code_offset == 0) {
490 return 0;
491 }
Ian Rogers0571d352011-11-03 19:51:38 -0700492 class_def_method_index++;
493 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700494 }
495 return code_offset;
496}
497
Ian Rogers0571d352011-11-03 19:51:38 -0700498size_t OatWriter::WriteCodeMethod(File* file, size_t code_offset, size_t oat_class_index,
499 size_t class_def_method_index, bool is_static,
500 uint32_t method_idx, const DexFile& dex_file) {
Logan Chien8b977d32012-02-21 19:14:55 +0800501#if !defined(ART_USE_LLVM_COMPILER)
Ian Rogers0571d352011-11-03 19:51:38 -0700502 const CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800503 compiler_->GetCompiledMethod(Compiler::MethodReference(&dex_file, method_idx));
Ian Rogers0571d352011-11-03 19:51:38 -0700504
505 uint32_t frame_size_in_bytes = 0;
506 uint32_t core_spill_mask = 0;
507 uint32_t fp_spill_mask = 0;
508
509 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800510 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700511
512
513 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700514 uint32_t aligned_code_offset = compiled_method->AlignCode(code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700515 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
516 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800517 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700518 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700519 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700520 << " Expected: " << aligned_code_offset << " File: " << file->name();
Brian Carlstromf03c2882012-03-05 20:29:06 -0800521 return 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700522 }
523 code_offset += aligned_code_delta;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700524 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700525 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700526 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700527 const std::vector<uint8_t>& code = compiled_method->GetCode();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700528 uint32_t code_size = code.size() * sizeof(code[0]);
529 CHECK_NE(code_size, 0U);
jeffhao55d78212011-11-02 11:41:50 -0700530
531 // Deduplicate code arrays
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700532 size_t offset = code_offset + sizeof(code_size) + compiled_method->CodeDelta();
jeffhaof479dcc2011-11-02 15:54:15 -0700533 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Ian Rogers0571d352011-11-03 19:51:38 -0700534 if (code_iter != code_offsets_.end() && offset != method_offsets.code_offset_) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700535 DCHECK(code_iter->second == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700536 } else {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700537 DCHECK(offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
538 if (!file->WriteFully(&code_size, sizeof(code_size))) {
539 ReportWriteFailure("method code size", method_idx, dex_file, file);
540 return 0;
541 }
542 code_offset += sizeof(code_size);
543 DCHECK_CODE_OFFSET();
jeffhao55d78212011-11-02 11:41:50 -0700544 if (!file->WriteFully(&code[0], code_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700545 ReportWriteFailure("method code", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800546 return 0;
jeffhao55d78212011-11-02 11:41:50 -0700547 }
548 code_offset += code_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700549 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700550 DCHECK_CODE_OFFSET();
Ian Rogers0571d352011-11-03 19:51:38 -0700551 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
552 core_spill_mask = compiled_method->GetCoreSpillMask();
553 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700554
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700555 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
556 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700557
558 // Deduplicate mapping tables
Ian Rogers0571d352011-11-03 19:51:38 -0700559 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
560 mapping_table_offsets_.find(&mapping_table);
561 if (mapping_iter != mapping_table_offsets_.end() &&
562 code_offset != method_offsets.mapping_table_offset_) {
563 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
564 || mapping_iter->second == method_offsets.mapping_table_offset_)
565 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700566 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700567 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
568 || code_offset == method_offsets.mapping_table_offset_)
569 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700570 if (!file->WriteFully(&mapping_table[0], mapping_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700571 ReportWriteFailure("mapping table", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800572 return 0;
jeffhao55d78212011-11-02 11:41:50 -0700573 }
574 code_offset += mapping_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700575 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700576 DCHECK_CODE_OFFSET();
577
578 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
579 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700580
581 // Deduplicate vmap tables
Ian Rogers0571d352011-11-03 19:51:38 -0700582 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
583 vmap_table_offsets_.find(&vmap_table);
584 if (vmap_iter != vmap_table_offsets_.end() &&
585 code_offset != method_offsets.vmap_table_offset_) {
586 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
587 || vmap_iter->second == method_offsets.vmap_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((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
591 || code_offset == method_offsets.vmap_table_offset_)
592 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700593 if (!file->WriteFully(&vmap_table[0], vmap_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700594 ReportWriteFailure("vmap table", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800595 return 0;
jeffhao55d78212011-11-02 11:41:50 -0700596 }
597 code_offset += vmap_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700598 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700599 DCHECK_CODE_OFFSET();
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800600
601 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
602 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
603
604 // Deduplicate GC maps
605 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
606 gc_map_offsets_.find(&gc_map);
607 if (gc_map_iter != gc_map_offsets_.end() &&
608 code_offset != method_offsets.gc_map_offset_) {
609 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
610 || gc_map_iter->second == method_offsets.gc_map_offset_)
611 << PrettyMethod(method_idx, dex_file);
612 } else {
613 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
614 || code_offset == method_offsets.gc_map_offset_)
615 << PrettyMethod(method_idx, dex_file);
616 if (!file->WriteFully(&gc_map[0], gc_map_size)) {
617 ReportWriteFailure("GC map", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800618 return 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800619 }
620 code_offset += gc_map_size;
621 }
622 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700623 }
Ian Rogers0571d352011-11-03 19:51:38 -0700624 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
625 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700626 if (compiled_invoke_stub != NULL) {
627 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
628 compiler_->GetInstructionSet());
629 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
630 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800631 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700632 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
633 PLOG(ERROR) << "Failed to seek to align invoke stub code. Actual: " << new_offset
634 << " Expected: " << aligned_code_offset;
Brian Carlstromf03c2882012-03-05 20:29:06 -0800635 return 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700636 }
637 code_offset += aligned_code_delta;
638 DCHECK_CODE_OFFSET();
639 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700640 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700641 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700642 uint32_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
643 CHECK_NE(invoke_stub_size, 0U);
jeffhao55d78212011-11-02 11:41:50 -0700644
645 // Deduplicate invoke stubs
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700646 size_t offset = code_offset + sizeof(invoke_stub_size);
Ian Rogers0571d352011-11-03 19:51:38 -0700647 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter =
648 code_offsets_.find(&invoke_stub);
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700649 if (stub_iter != code_offsets_.end() && offset != method_offsets.invoke_stub_offset_) {
650 DCHECK(stub_iter->second == method_offsets.invoke_stub_offset_) << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700651 } else {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700652 DCHECK(offset == method_offsets.invoke_stub_offset_) << PrettyMethod(method_idx, dex_file);
653 if (!file->WriteFully(&invoke_stub_size, sizeof(invoke_stub_size))) {
654 ReportWriteFailure("invoke stub code size", method_idx, dex_file, file);
655 return 0;
656 }
657 code_offset += sizeof(invoke_stub_size);
658 DCHECK_CODE_OFFSET();
jeffhao55d78212011-11-02 11:41:50 -0700659 if (!file->WriteFully(&invoke_stub[0], invoke_stub_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700660 ReportWriteFailure("invoke stub code", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800661 return 0;
jeffhao55d78212011-11-02 11:41:50 -0700662 }
663 code_offset += invoke_stub_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700664 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700665 DCHECK_CODE_OFFSET();
666 }
Logan Chien8b977d32012-02-21 19:14:55 +0800667#endif
668
Brian Carlstrome24fa612011-09-29 00:53:55 -0700669 return code_offset;
670}
671
Brian Carlstrome24fa612011-09-29 00:53:55 -0700672OatWriter::OatDexFile::OatDexFile(const DexFile& dex_file) {
Elliott Hughes95572412011-12-13 18:14:20 -0800673 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700674 dex_file_location_size_ = location.size();
675 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800676 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800677 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800678 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700679}
680
681size_t OatWriter::OatDexFile::SizeOf() const {
682 return sizeof(dex_file_location_size_)
683 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800684 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800685 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800686 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700687}
688
689void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
690 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
691 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800692 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800693 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800694 oat_header.UpdateChecksum(&methods_offsets_[0],
695 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700696}
697
698bool OatWriter::OatDexFile::Write(File* file) const {
699 if (!file->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700700 PLOG(ERROR) << "Failed to write dex file location length to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700701 return false;
702 }
703 if (!file->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700704 PLOG(ERROR) << "Failed to write dex file location data to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700705 return false;
706 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800707 if (!file->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
708 PLOG(ERROR) << "Failed to write dex file location checksum to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700709 return false;
710 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800711 if (!file->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
712 PLOG(ERROR) << "Failed to write dex file offset to " << file->name();
713 return false;
714 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800715 if (!file->WriteFully(&methods_offsets_[0],
716 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700717 PLOG(ERROR) << "Failed to write methods offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700718 return false;
719 }
720 return true;
721}
722
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800723OatWriter::OatClass::OatClass(Class::Status status, uint32_t methods_count) {
724 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700725 method_offsets_.resize(methods_count);
726}
727
Brian Carlstrom389efb02012-01-11 12:06:26 -0800728size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800729 return sizeof(status_)
730 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700731}
732
Brian Carlstrom389efb02012-01-11 12:06:26 -0800733void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800734 oat_header.UpdateChecksum(&status_, sizeof(status_));
735 oat_header.UpdateChecksum(&method_offsets_[0],
736 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700737}
738
Brian Carlstrom389efb02012-01-11 12:06:26 -0800739bool OatWriter::OatClass::Write(File* file) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800740 if (!file->WriteFully(&status_, sizeof(status_))) {
741 PLOG(ERROR) << "Failed to write class status to " << file->name();
742 return false;
743 }
744 if (!file->WriteFully(&method_offsets_[0],
745 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700746 PLOG(ERROR) << "Failed to write method offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700747 return false;
748 }
749 return true;
750}
751
752} // namespace art