blob: a1b0f9deb9f02168f8069bf4b3a560709e530eef [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
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700201size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
202 size_t __attribute__((unused)) class_def_index,
203 size_t class_def_method_index,
204 bool __attribute__((unused)) is_native,
205 bool is_static, bool is_direct,
206 uint32_t method_idx, const DexFile* dex_file) {
Logan Chien8b977d32012-02-21 19:14:55 +0800207#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700208 // derived from CompiledMethod if available
209 uint32_t code_offset = 0;
210 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700211 uint32_t core_spill_mask = 0;
212 uint32_t fp_spill_mask = 0;
213 uint32_t mapping_table_offset = 0;
214 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800215 uint32_t gc_map_offset = 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700216 // derived from CompiledInvokeStub if available
217 uint32_t invoke_stub_offset = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700218
Ian Rogers0571d352011-11-03 19:51:38 -0700219 CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800220 compiler_->GetCompiledMethod(Compiler::MethodReference(dex_file, method_idx));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700221 if (compiled_method != NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700222 offset = compiled_method->AlignCode(offset);
Elliott Hughes06b37d92011-10-16 11:51:29 -0700223 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700224 const std::vector<uint8_t>& code = compiled_method->GetCode();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700225 uint32_t code_size = code.size() * sizeof(code[0]);
226 CHECK_NE(code_size, 0U);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700227 uint32_t thumb_offset = compiled_method->CodeDelta();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700228 code_offset = offset + sizeof(code_size) + thumb_offset;
jeffhao55d78212011-11-02 11:41:50 -0700229
230 // Deduplicate code arrays
jeffhaof479dcc2011-11-02 15:54:15 -0700231 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
jeffhao55d78212011-11-02 11:41:50 -0700232 if (code_iter != code_offsets_.end()) {
233 code_offset = code_iter->second;
234 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700235 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&code, code_offset));
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700236 offset += sizeof(code_size); // code size is prepended before code
jeffhao55d78212011-11-02 11:41:50 -0700237 offset += code_size;
238 oat_header_->UpdateChecksum(&code[0], code_size);
239 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700240 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700241 core_spill_mask = compiled_method->GetCoreSpillMask();
242 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700243
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700244 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
245 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
246 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700247
248 // Deduplicate mapping tables
jeffhaof479dcc2011-11-02 15:54:15 -0700249 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
jeffhao55d78212011-11-02 11:41:50 -0700250 if (mapping_iter != mapping_table_offsets_.end()) {
251 mapping_table_offset = mapping_iter->second;
252 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700253 mapping_table_offsets_.insert(std::pair<const std::vector<uint32_t>*, uint32_t>(&mapping_table, mapping_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700254 offset += mapping_table_size;
255 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
256 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700257
258 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
259 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
260 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700261
262 // Deduplicate vmap tables
jeffhaof479dcc2011-11-02 15:54:15 -0700263 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
jeffhao55d78212011-11-02 11:41:50 -0700264 if (vmap_iter != vmap_table_offsets_.end()) {
265 vmap_table_offset = vmap_iter->second;
266 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700267 vmap_table_offsets_.insert(std::pair<const std::vector<uint16_t>*, uint32_t>(&vmap_table, vmap_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700268 offset += vmap_table_size;
269 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
270 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800271
272 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
273 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
274 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
275
Ian Rogersc20a83e2012-01-18 18:15:32 -0800276#ifndef NDEBUG
277 // We expect GC maps except when the class hasn't been verified or the method is native
278 CompiledClass* compiled_class =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800279 compiler_->GetCompiledClass(Compiler::MethodReference(dex_file, class_def_index));
Ian Rogersc20a83e2012-01-18 18:15:32 -0800280 Class::Status status =
281 (compiled_class != NULL) ? compiled_class->GetStatus() : Class::kStatusNotReady;
282 CHECK(gc_map_size != 0 || is_native || status < Class::kStatusVerified)
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800283 << &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 -0800284#endif
285
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800286 // Deduplicate GC maps
287 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = gc_map_offsets_.find(&gc_map);
288 if (gc_map_iter != gc_map_offsets_.end()) {
289 gc_map_offset = gc_map_iter->second;
290 } else {
291 gc_map_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&gc_map, gc_map_offset));
292 offset += gc_map_size;
293 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
294 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700295 }
296
Ian Rogers0571d352011-11-03 19:51:38 -0700297 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx));
298 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700299 if (compiled_invoke_stub != NULL) {
300 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
Elliott Hughes06b37d92011-10-16 11:51:29 -0700301 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700302 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700303 uint32_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
304 CHECK_NE(invoke_stub_size, 0U);
305 invoke_stub_offset = offset + sizeof(invoke_stub_size);
jeffhao55d78212011-11-02 11:41:50 -0700306
307 // Deduplicate invoke stubs
jeffhaof479dcc2011-11-02 15:54:15 -0700308 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&invoke_stub);
jeffhao55d78212011-11-02 11:41:50 -0700309 if (stub_iter != code_offsets_.end()) {
310 invoke_stub_offset = stub_iter->second;
311 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700312 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&invoke_stub, invoke_stub_offset));
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700313 offset += sizeof(invoke_stub_size); // invoke stub size is prepended before code
jeffhao55d78212011-11-02 11:41:50 -0700314 offset += invoke_stub_size;
315 oat_header_->UpdateChecksum(&invoke_stub[0], invoke_stub_size);
316 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700317 }
318
Brian Carlstrom389efb02012-01-11 12:06:26 -0800319 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index]
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700320 = OatMethodOffsets(code_offset,
321 frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700322 core_spill_mask,
323 fp_spill_mask,
324 mapping_table_offset,
325 vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800326 gc_map_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700327 invoke_stub_offset);
328
Ian Rogers0571d352011-11-03 19:51:38 -0700329 if (compiler_->IsImage()) {
330 ClassLinker* linker = Runtime::Current()->GetClassLinker();
331 DexCache* dex_cache = linker->FindDexCache(*dex_file);
332 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader_,
333 is_direct);
334 CHECK(method != NULL);
335 method->SetFrameSizeInBytes(frame_size_in_bytes);
336 method->SetCoreSpillMask(core_spill_mask);
337 method->SetFpSpillMask(fp_spill_mask);
338 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800339 // Don't overwrite static method trampoline
340 if (!method->IsStatic() || method->IsConstructor() ||
341 method->GetDeclaringClass()->IsInitialized()) {
342 method->SetOatCodeOffset(code_offset);
343 } else {
344 method->SetCode(Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod)->GetData());
345 }
Ian Rogers0571d352011-11-03 19:51:38 -0700346 method->SetOatVmapTableOffset(vmap_table_offset);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800347 method->SetOatGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700348 method->SetOatInvokeStubOffset(invoke_stub_offset);
349 }
Logan Chien8b977d32012-02-21 19:14:55 +0800350#endif
351
Brian Carlstrome24fa612011-09-29 00:53:55 -0700352 return offset;
353}
354
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700355#define DCHECK_CODE_OFFSET() \
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800356 DCHECK_EQ(static_cast<off_t>(code_offset), lseek(file->Fd(), 0, SEEK_CUR))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700357
Elliott Hughes234da572011-11-03 22:13:06 -0700358bool OatWriter::Write(File* file) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700359 if (!file->WriteFully(oat_header_, sizeof(*oat_header_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700360 PLOG(ERROR) << "Failed to write oat header to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700361 return false;
362 }
363
Elliott Hughes234da572011-11-03 22:13:06 -0700364 if (!WriteTables(file)) {
365 LOG(ERROR) << "Failed to write oat tables to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700366 return false;
367 }
368
Elliott Hughes234da572011-11-03 22:13:06 -0700369 size_t code_offset = WriteCode(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700370 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700371 LOG(ERROR) << "Failed to write oat code to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700372 return false;
373 }
374
Elliott Hughes234da572011-11-03 22:13:06 -0700375 code_offset = WriteCodeDexFiles(file, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700376 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700377 LOG(ERROR) << "Failed to write oat code for dex files to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700378 return false;
379 }
380
381 return true;
382}
383
384bool OatWriter::WriteTables(File* file) {
385 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
386 if (!oat_dex_files_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700387 PLOG(ERROR) << "Failed to write oat dex information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700388 return false;
389 }
390 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800391 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
392 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800393 off_t actual_offset = lseek(file->Fd(), expected_offset, SEEK_SET);
Brian Carlstrom89521892011-12-07 22:05:07 -0800394 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
395 const DexFile* dex_file = (*dex_files_)[i];
396 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
397 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
398 return false;
399 }
400 const DexFile* dex_file = (*dex_files_)[i];
401 if (!file->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
402 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << file->name();
403 return false;
404 }
405 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800406 for (size_t i = 0; i != oat_classes_.size(); ++i) {
407 if (!oat_classes_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700408 PLOG(ERROR) << "Failed to write oat methods information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700409 return false;
410 }
411 }
412 return true;
413}
414
415size_t OatWriter::WriteCode(File* file) {
416 uint32_t code_offset = oat_header_->GetExecutableOffset();
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800417 off_t new_offset = lseek(file->Fd(), executable_offset_padding_length_, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700418 if (static_cast<uint32_t>(new_offset) != code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700419 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700420 << " Expected: " << code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700421 return 0;
422 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700423 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700424 return code_offset;
425}
426
427size_t OatWriter::WriteCodeDexFiles(File* file, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700428 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800429 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700430 const DexFile* dex_file = (*dex_files_)[i];
431 CHECK(dex_file != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700432 code_offset = WriteCodeDexFile(file, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700433 if (code_offset == 0) {
434 return 0;
435 }
436 }
437 return code_offset;
438}
439
Ian Rogers0571d352011-11-03 19:51:38 -0700440size_t OatWriter::WriteCodeDexFile(File* file, size_t code_offset, size_t& oat_class_index,
441 const DexFile& dex_file) {
442 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
443 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700444 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700445 code_offset = WriteCodeClassDef(file, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700446 if (code_offset == 0) {
447 return 0;
448 }
449 }
450 return code_offset;
451}
452
Ian Rogers0571d352011-11-03 19:51:38 -0700453void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
454 const DexFile& dex_file, File* f) const {
455 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
456 << " to " << f->name();
Elliott Hughes234da572011-11-03 22:13:06 -0700457}
458
Brian Carlstrome24fa612011-09-29 00:53:55 -0700459size_t OatWriter::WriteCodeClassDef(File* file,
Ian Rogers0571d352011-11-03 19:51:38 -0700460 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700461 const DexFile& dex_file,
462 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700463 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700464 if (class_data == NULL) {
465 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700466 return code_offset;
467 }
Ian Rogers0571d352011-11-03 19:51:38 -0700468 ClassDataItemIterator it(dex_file, class_data);
469 // Skip fields
470 while (it.HasNextStaticField()) {
471 it.Next();
472 }
473 while (it.HasNextInstanceField()) {
474 it.Next();
475 }
476 // Process methods
477 size_t class_def_method_index = 0;
478 while (it.HasNextDirectMethod()) {
479 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
480 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
481 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700482 if (code_offset == 0) {
483 return 0;
484 }
Ian Rogers0571d352011-11-03 19:51:38 -0700485 class_def_method_index++;
486 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700487 }
Ian Rogers0571d352011-11-03 19:51:38 -0700488 while (it.HasNextVirtualMethod()) {
489 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
490 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700491 if (code_offset == 0) {
492 return 0;
493 }
Ian Rogers0571d352011-11-03 19:51:38 -0700494 class_def_method_index++;
495 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700496 }
497 return code_offset;
498}
499
Ian Rogers0571d352011-11-03 19:51:38 -0700500size_t OatWriter::WriteCodeMethod(File* file, size_t code_offset, size_t oat_class_index,
501 size_t class_def_method_index, bool is_static,
502 uint32_t method_idx, const DexFile& dex_file) {
Logan Chien8b977d32012-02-21 19:14:55 +0800503#if !defined(ART_USE_LLVM_COMPILER)
Ian Rogers0571d352011-11-03 19:51:38 -0700504 const CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800505 compiler_->GetCompiledMethod(Compiler::MethodReference(&dex_file, method_idx));
Ian Rogers0571d352011-11-03 19:51:38 -0700506
507 uint32_t frame_size_in_bytes = 0;
508 uint32_t core_spill_mask = 0;
509 uint32_t fp_spill_mask = 0;
510
511 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800512 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700513
514
515 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700516 uint32_t aligned_code_offset = compiled_method->AlignCode(code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700517 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
518 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800519 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700520 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700521 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700522 << " Expected: " << aligned_code_offset << " File: " << file->name();
Brian Carlstromf03c2882012-03-05 20:29:06 -0800523 return 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700524 }
525 code_offset += aligned_code_delta;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700526 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700527 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700528 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700529 const std::vector<uint8_t>& code = compiled_method->GetCode();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700530 uint32_t code_size = code.size() * sizeof(code[0]);
531 CHECK_NE(code_size, 0U);
jeffhao55d78212011-11-02 11:41:50 -0700532
533 // Deduplicate code arrays
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700534 size_t offset = code_offset + sizeof(code_size) + compiled_method->CodeDelta();
jeffhaof479dcc2011-11-02 15:54:15 -0700535 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Ian Rogers0571d352011-11-03 19:51:38 -0700536 if (code_iter != code_offsets_.end() && offset != method_offsets.code_offset_) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700537 DCHECK(code_iter->second == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700538 } else {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700539 DCHECK(offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
540 if (!file->WriteFully(&code_size, sizeof(code_size))) {
541 ReportWriteFailure("method code size", method_idx, dex_file, file);
542 return 0;
543 }
544 code_offset += sizeof(code_size);
545 DCHECK_CODE_OFFSET();
jeffhao55d78212011-11-02 11:41:50 -0700546 if (!file->WriteFully(&code[0], code_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700547 ReportWriteFailure("method code", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800548 return 0;
jeffhao55d78212011-11-02 11:41:50 -0700549 }
550 code_offset += code_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700551 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700552 DCHECK_CODE_OFFSET();
Ian Rogers0571d352011-11-03 19:51:38 -0700553 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
554 core_spill_mask = compiled_method->GetCoreSpillMask();
555 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700556
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700557 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
558 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700559
560 // Deduplicate mapping tables
Ian Rogers0571d352011-11-03 19:51:38 -0700561 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
562 mapping_table_offsets_.find(&mapping_table);
563 if (mapping_iter != mapping_table_offsets_.end() &&
564 code_offset != method_offsets.mapping_table_offset_) {
565 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
566 || mapping_iter->second == method_offsets.mapping_table_offset_)
567 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700568 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700569 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
570 || code_offset == method_offsets.mapping_table_offset_)
571 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700572 if (!file->WriteFully(&mapping_table[0], mapping_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700573 ReportWriteFailure("mapping table", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800574 return 0;
jeffhao55d78212011-11-02 11:41:50 -0700575 }
576 code_offset += mapping_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700577 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700578 DCHECK_CODE_OFFSET();
579
580 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
581 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700582
583 // Deduplicate vmap tables
Ian Rogers0571d352011-11-03 19:51:38 -0700584 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
585 vmap_table_offsets_.find(&vmap_table);
586 if (vmap_iter != vmap_table_offsets_.end() &&
587 code_offset != method_offsets.vmap_table_offset_) {
588 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
589 || vmap_iter->second == method_offsets.vmap_table_offset_)
590 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700591 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700592 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
593 || code_offset == method_offsets.vmap_table_offset_)
594 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700595 if (!file->WriteFully(&vmap_table[0], vmap_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700596 ReportWriteFailure("vmap table", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800597 return 0;
jeffhao55d78212011-11-02 11:41:50 -0700598 }
599 code_offset += vmap_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700600 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700601 DCHECK_CODE_OFFSET();
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800602
603 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
604 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
605
606 // Deduplicate GC maps
607 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
608 gc_map_offsets_.find(&gc_map);
609 if (gc_map_iter != gc_map_offsets_.end() &&
610 code_offset != method_offsets.gc_map_offset_) {
611 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
612 || gc_map_iter->second == method_offsets.gc_map_offset_)
613 << PrettyMethod(method_idx, dex_file);
614 } else {
615 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
616 || code_offset == method_offsets.gc_map_offset_)
617 << PrettyMethod(method_idx, dex_file);
618 if (!file->WriteFully(&gc_map[0], gc_map_size)) {
619 ReportWriteFailure("GC map", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800620 return 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800621 }
622 code_offset += gc_map_size;
623 }
624 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700625 }
Ian Rogers0571d352011-11-03 19:51:38 -0700626 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
627 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700628 if (compiled_invoke_stub != NULL) {
629 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
630 compiler_->GetInstructionSet());
631 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
632 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800633 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700634 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
635 PLOG(ERROR) << "Failed to seek to align invoke stub code. Actual: " << new_offset
636 << " Expected: " << aligned_code_offset;
Brian Carlstromf03c2882012-03-05 20:29:06 -0800637 return 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700638 }
639 code_offset += aligned_code_delta;
640 DCHECK_CODE_OFFSET();
641 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700642 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700643 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700644 uint32_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
645 CHECK_NE(invoke_stub_size, 0U);
jeffhao55d78212011-11-02 11:41:50 -0700646
647 // Deduplicate invoke stubs
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700648 size_t offset = code_offset + sizeof(invoke_stub_size);
Ian Rogers0571d352011-11-03 19:51:38 -0700649 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter =
650 code_offsets_.find(&invoke_stub);
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700651 if (stub_iter != code_offsets_.end() && offset != method_offsets.invoke_stub_offset_) {
652 DCHECK(stub_iter->second == method_offsets.invoke_stub_offset_) << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700653 } else {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700654 DCHECK(offset == method_offsets.invoke_stub_offset_) << PrettyMethod(method_idx, dex_file);
655 if (!file->WriteFully(&invoke_stub_size, sizeof(invoke_stub_size))) {
656 ReportWriteFailure("invoke stub code size", method_idx, dex_file, file);
657 return 0;
658 }
659 code_offset += sizeof(invoke_stub_size);
660 DCHECK_CODE_OFFSET();
jeffhao55d78212011-11-02 11:41:50 -0700661 if (!file->WriteFully(&invoke_stub[0], invoke_stub_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700662 ReportWriteFailure("invoke stub code", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800663 return 0;
jeffhao55d78212011-11-02 11:41:50 -0700664 }
665 code_offset += invoke_stub_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700666 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700667 DCHECK_CODE_OFFSET();
668 }
Logan Chien8b977d32012-02-21 19:14:55 +0800669#endif
670
Brian Carlstrome24fa612011-09-29 00:53:55 -0700671 return code_offset;
672}
673
Brian Carlstrome24fa612011-09-29 00:53:55 -0700674OatWriter::OatDexFile::OatDexFile(const DexFile& dex_file) {
Elliott Hughes95572412011-12-13 18:14:20 -0800675 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700676 dex_file_location_size_ = location.size();
677 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800678 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800679 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800680 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700681}
682
683size_t OatWriter::OatDexFile::SizeOf() const {
684 return sizeof(dex_file_location_size_)
685 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800686 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800687 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800688 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700689}
690
691void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
692 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
693 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800694 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800695 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800696 oat_header.UpdateChecksum(&methods_offsets_[0],
697 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700698}
699
700bool OatWriter::OatDexFile::Write(File* file) const {
701 if (!file->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700702 PLOG(ERROR) << "Failed to write dex file location length to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700703 return false;
704 }
705 if (!file->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700706 PLOG(ERROR) << "Failed to write dex file location data to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700707 return false;
708 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800709 if (!file->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
710 PLOG(ERROR) << "Failed to write dex file location checksum to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700711 return false;
712 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800713 if (!file->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
714 PLOG(ERROR) << "Failed to write dex file offset to " << file->name();
715 return false;
716 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800717 if (!file->WriteFully(&methods_offsets_[0],
718 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700719 PLOG(ERROR) << "Failed to write methods offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700720 return false;
721 }
722 return true;
723}
724
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800725OatWriter::OatClass::OatClass(Class::Status status, uint32_t methods_count) {
726 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700727 method_offsets_.resize(methods_count);
728}
729
Brian Carlstrom389efb02012-01-11 12:06:26 -0800730size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800731 return sizeof(status_)
732 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700733}
734
Brian Carlstrom389efb02012-01-11 12:06:26 -0800735void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800736 oat_header.UpdateChecksum(&status_, sizeof(status_));
737 oat_header.UpdateChecksum(&method_offsets_[0],
738 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700739}
740
Brian Carlstrom389efb02012-01-11 12:06:26 -0800741bool OatWriter::OatClass::Write(File* file) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800742 if (!file->WriteFully(&status_, sizeof(status_))) {
743 PLOG(ERROR) << "Failed to write class status to " << file->name();
744 return false;
745 }
746 if (!file->WriteFully(&method_offsets_[0],
747 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700748 PLOG(ERROR) << "Failed to write method offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700749 return false;
750 }
751 return true;
752}
753
754} // namespace art