blob: 8e127deb6493fcd3e7e5a8f48e73a9806386d69a [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"
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070023#include "space.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070024#include "stl_util.h"
25
26namespace art {
27
Elliott Hughes234da572011-11-03 22:13:06 -070028bool OatWriter::Create(File* file,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070029 const ClassLoader* class_loader,
jeffhao10037c82012-01-23 15:06:23 -080030 const std::vector<const DexFile*>& dex_files,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070031 uint32_t image_file_location_checksum,
32 const std::string& image_file_location,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070033 const Compiler& compiler) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070034 OatWriter oat_writer(dex_files,
35 image_file_location_checksum,
36 image_file_location,
37 class_loader,
38 compiler);
Elliott Hughes234da572011-11-03 22:13:06 -070039 return oat_writer.Write(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -070040}
41
Brian Carlstrom3320cf42011-10-04 14:58:28 -070042OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070043 uint32_t image_file_location_checksum,
44 const std::string& image_file_location,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070045 const ClassLoader* class_loader,
46 const Compiler& compiler) {
47 compiler_ = &compiler;
Brian Carlstrome24fa612011-09-29 00:53:55 -070048 class_loader_ = class_loader;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070049 image_file_location_checksum_ = image_file_location_checksum;
50 image_file_location_ = image_file_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -070051 dex_files_ = &dex_files;
Ian Rogers0571d352011-11-03 19:51:38 -070052 oat_header_ = NULL;
53 executable_offset_padding_length_ = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -070054
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070055 size_t offset = InitOatHeader();
Brian Carlstrome24fa612011-09-29 00:53:55 -070056 offset = InitOatDexFiles(offset);
Brian Carlstrom89521892011-12-07 22:05:07 -080057 offset = InitDexFiles(offset);
Brian Carlstrom389efb02012-01-11 12:06:26 -080058 offset = InitOatClasses(offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -070059 offset = InitOatCode(offset);
60 offset = InitOatCodeDexFiles(offset);
61
62 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -070063}
64
Ian Rogers0571d352011-11-03 19:51:38 -070065OatWriter::~OatWriter() {
66 delete oat_header_;
67 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -080068 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -070069}
70
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070071size_t OatWriter::InitOatHeader() {
Brian Carlstrome24fa612011-09-29 00:53:55 -070072 // create the OatHeader
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070073 oat_header_ = new OatHeader(compiler_->GetInstructionSet(),
74 dex_files_,
75 image_file_location_checksum_,
76 image_file_location_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070077 size_t offset = sizeof(*oat_header_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070078 offset += image_file_location_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -070079 return offset;
80}
81
82size_t OatWriter::InitOatDexFiles(size_t offset) {
83 // create the OatDexFiles
84 for (size_t i = 0; i != dex_files_->size(); ++i) {
85 const DexFile* dex_file = (*dex_files_)[i];
86 CHECK(dex_file != NULL);
87 OatDexFile* oat_dex_file = new OatDexFile(*dex_file);
88 oat_dex_files_.push_back(oat_dex_file);
89 offset += oat_dex_file->SizeOf();
90 }
91 return offset;
92}
93
Brian Carlstrom89521892011-12-07 22:05:07 -080094size_t OatWriter::InitDexFiles(size_t offset) {
95 // calculate the offsets within OatDexFiles to the DexFiles
96 for (size_t i = 0; i != dex_files_->size(); ++i) {
97 // dex files are required to be 4 byte aligned
98 offset = RoundUp(offset, 4);
99
100 // set offset in OatDexFile to DexFile
101 oat_dex_files_[i]->dex_file_offset_ = offset;
102
103 const DexFile* dex_file = (*dex_files_)[i];
104 offset += dex_file->GetHeader().file_size_;
105 }
106 return offset;
107}
108
Brian Carlstrom389efb02012-01-11 12:06:26 -0800109size_t OatWriter::InitOatClasses(size_t offset) {
110 // create the OatClasses
111 // calculate the offsets within OatDexFiles to OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -0700112 for (size_t i = 0; i != dex_files_->size(); ++i) {
113 const DexFile* dex_file = (*dex_files_)[i];
114 for (size_t class_def_index = 0;
115 class_def_index < dex_file->NumClassDefs();
Ian Rogersc20a83e2012-01-18 18:15:32 -0800116 class_def_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800117 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700118 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
119 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700120 uint32_t num_methods = 0;
121 if (class_data != NULL) { // ie not an empty class, such as a marker interface
122 ClassDataItemIterator it(*dex_file, class_data);
123 size_t num_direct_methods = it.NumDirectMethods();
124 size_t num_virtual_methods = it.NumVirtualMethods();
125 num_methods = num_direct_methods + num_virtual_methods;
126 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800127
128 CompiledClass* compiled_class =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800129 compiler_->GetCompiledClass(Compiler::MethodReference(dex_file, class_def_index));
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800130 Class::Status status =
131 (compiled_class != NULL) ? compiled_class->GetStatus() : Class::kStatusNotReady;
132
133 OatClass* oat_class = new OatClass(status, num_methods);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800134 oat_classes_.push_back(oat_class);
135 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700136 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800137 oat_dex_files_[i]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700138 }
139 return offset;
140}
141
142size_t OatWriter::InitOatCode(size_t offset) {
143 // calculate the offsets within OatHeader to executable code
144 size_t old_offset = offset;
145 // required to be on a new page boundary
146 offset = RoundUp(offset, kPageSize);
147 oat_header_->SetExecutableOffset(offset);
148 executable_offset_padding_length_ = offset - old_offset;
149 return offset;
150}
151
152size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700153 size_t oat_class_index = 0;
154 for (size_t i = 0; i != dex_files_->size(); ++i) {
155 const DexFile* dex_file = (*dex_files_)[i];
156 CHECK(dex_file != NULL);
157 offset = InitOatCodeDexFile(offset, oat_class_index, *dex_file);
158 }
159 return offset;
160}
161
162size_t OatWriter::InitOatCodeDexFile(size_t offset,
163 size_t& oat_class_index,
164 const DexFile& dex_file) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800165 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700166 class_def_index < dex_file.NumClassDefs();
167 class_def_index++, oat_class_index++) {
168 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800169 offset = InitOatCodeClassDef(offset, oat_class_index, class_def_index, dex_file, class_def);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800170 oat_classes_[oat_class_index]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700171 }
172 return offset;
173}
174
175size_t OatWriter::InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800176 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700177 const DexFile& dex_file,
178 const DexFile::ClassDef& class_def) {
179 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700180 if (class_data == NULL) {
181 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700182 return offset;
183 }
Ian Rogers0571d352011-11-03 19:51:38 -0700184 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800185 CHECK_EQ(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700186 it.NumDirectMethods() + it.NumVirtualMethods());
187 // Skip fields
188 while (it.HasNextStaticField()) {
189 it.Next();
190 }
191 while (it.HasNextInstanceField()) {
192 it.Next();
193 }
194 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700195 size_t class_def_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700196 while (it.HasNextDirectMethod()) {
197 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
Ian Rogersc20a83e2012-01-18 18:15:32 -0800198 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
199 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index, is_native,
200 is_static, true, it.GetMemberIndex(), &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700201 class_def_method_index++;
202 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700203 }
Ian Rogers0571d352011-11-03 19:51:38 -0700204 while (it.HasNextVirtualMethod()) {
205 CHECK_EQ(it.GetMemberAccessFlags() & kAccStatic, 0U);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800206 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
207 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index, is_native,
208 false, false, it.GetMemberIndex(), &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700209 class_def_method_index++;
210 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700211 }
Ian Rogers0571d352011-11-03 19:51:38 -0700212 DCHECK(!it.HasNext());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700213 return offset;
214}
215
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700216size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
217 size_t __attribute__((unused)) class_def_index,
218 size_t class_def_method_index,
219 bool __attribute__((unused)) is_native,
220 bool is_static, bool is_direct,
221 uint32_t method_idx, const DexFile* dex_file) {
Logan Chien8b977d32012-02-21 19:14:55 +0800222#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700223 // derived from CompiledMethod if available
224 uint32_t code_offset = 0;
225 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700226 uint32_t core_spill_mask = 0;
227 uint32_t fp_spill_mask = 0;
228 uint32_t mapping_table_offset = 0;
229 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800230 uint32_t gc_map_offset = 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700231 // derived from CompiledInvokeStub if available
232 uint32_t invoke_stub_offset = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700233
Ian Rogers0571d352011-11-03 19:51:38 -0700234 CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800235 compiler_->GetCompiledMethod(Compiler::MethodReference(dex_file, method_idx));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700236 if (compiled_method != NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700237 offset = compiled_method->AlignCode(offset);
Elliott Hughes06b37d92011-10-16 11:51:29 -0700238 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700239 const std::vector<uint8_t>& code = compiled_method->GetCode();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700240 uint32_t code_size = code.size() * sizeof(code[0]);
241 CHECK_NE(code_size, 0U);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700242 uint32_t thumb_offset = compiled_method->CodeDelta();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700243 code_offset = offset + sizeof(code_size) + thumb_offset;
jeffhao55d78212011-11-02 11:41:50 -0700244
245 // Deduplicate code arrays
jeffhaof479dcc2011-11-02 15:54:15 -0700246 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
jeffhao55d78212011-11-02 11:41:50 -0700247 if (code_iter != code_offsets_.end()) {
248 code_offset = code_iter->second;
249 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700250 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&code, code_offset));
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700251 offset += sizeof(code_size); // code size is prepended before code
jeffhao55d78212011-11-02 11:41:50 -0700252 offset += code_size;
253 oat_header_->UpdateChecksum(&code[0], code_size);
254 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700255 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700256 core_spill_mask = compiled_method->GetCoreSpillMask();
257 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700258
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700259 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
260 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
261 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700262
263 // Deduplicate mapping tables
jeffhaof479dcc2011-11-02 15:54:15 -0700264 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
jeffhao55d78212011-11-02 11:41:50 -0700265 if (mapping_iter != mapping_table_offsets_.end()) {
266 mapping_table_offset = mapping_iter->second;
267 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700268 mapping_table_offsets_.insert(std::pair<const std::vector<uint32_t>*, uint32_t>(&mapping_table, mapping_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700269 offset += mapping_table_size;
270 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
271 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700272
273 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
274 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
275 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700276
277 // Deduplicate vmap tables
jeffhaof479dcc2011-11-02 15:54:15 -0700278 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
jeffhao55d78212011-11-02 11:41:50 -0700279 if (vmap_iter != vmap_table_offsets_.end()) {
280 vmap_table_offset = vmap_iter->second;
281 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700282 vmap_table_offsets_.insert(std::pair<const std::vector<uint16_t>*, uint32_t>(&vmap_table, vmap_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700283 offset += vmap_table_size;
284 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
285 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800286
287 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
288 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
289 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
290
Ian Rogersc20a83e2012-01-18 18:15:32 -0800291#ifndef NDEBUG
292 // We expect GC maps except when the class hasn't been verified or the method is native
293 CompiledClass* compiled_class =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800294 compiler_->GetCompiledClass(Compiler::MethodReference(dex_file, class_def_index));
Ian Rogersc20a83e2012-01-18 18:15:32 -0800295 Class::Status status =
296 (compiled_class != NULL) ? compiled_class->GetStatus() : Class::kStatusNotReady;
297 CHECK(gc_map_size != 0 || is_native || status < Class::kStatusVerified)
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800298 << &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 -0800299#endif
300
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800301 // Deduplicate GC maps
302 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = gc_map_offsets_.find(&gc_map);
303 if (gc_map_iter != gc_map_offsets_.end()) {
304 gc_map_offset = gc_map_iter->second;
305 } else {
306 gc_map_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&gc_map, gc_map_offset));
307 offset += gc_map_size;
308 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
309 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700310 }
311
Ian Rogers0571d352011-11-03 19:51:38 -0700312 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx));
313 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700314 if (compiled_invoke_stub != NULL) {
315 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
Elliott Hughes06b37d92011-10-16 11:51:29 -0700316 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700317 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700318 uint32_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
319 CHECK_NE(invoke_stub_size, 0U);
320 invoke_stub_offset = offset + sizeof(invoke_stub_size);
jeffhao55d78212011-11-02 11:41:50 -0700321
322 // Deduplicate invoke stubs
jeffhaof479dcc2011-11-02 15:54:15 -0700323 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&invoke_stub);
jeffhao55d78212011-11-02 11:41:50 -0700324 if (stub_iter != code_offsets_.end()) {
325 invoke_stub_offset = stub_iter->second;
326 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700327 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&invoke_stub, invoke_stub_offset));
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700328 offset += sizeof(invoke_stub_size); // invoke stub size is prepended before code
jeffhao55d78212011-11-02 11:41:50 -0700329 offset += invoke_stub_size;
330 oat_header_->UpdateChecksum(&invoke_stub[0], invoke_stub_size);
331 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700332 }
333
Brian Carlstrom389efb02012-01-11 12:06:26 -0800334 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index]
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700335 = OatMethodOffsets(code_offset,
336 frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700337 core_spill_mask,
338 fp_spill_mask,
339 mapping_table_offset,
340 vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800341 gc_map_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700342 invoke_stub_offset);
343
Ian Rogers0571d352011-11-03 19:51:38 -0700344 if (compiler_->IsImage()) {
345 ClassLinker* linker = Runtime::Current()->GetClassLinker();
346 DexCache* dex_cache = linker->FindDexCache(*dex_file);
347 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader_,
348 is_direct);
349 CHECK(method != NULL);
350 method->SetFrameSizeInBytes(frame_size_in_bytes);
351 method->SetCoreSpillMask(core_spill_mask);
352 method->SetFpSpillMask(fp_spill_mask);
353 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800354 // Don't overwrite static method trampoline
355 if (!method->IsStatic() || method->IsConstructor() ||
356 method->GetDeclaringClass()->IsInitialized()) {
357 method->SetOatCodeOffset(code_offset);
358 } else {
359 method->SetCode(Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod)->GetData());
360 }
Ian Rogers0571d352011-11-03 19:51:38 -0700361 method->SetOatVmapTableOffset(vmap_table_offset);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800362 method->SetOatGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700363 method->SetOatInvokeStubOffset(invoke_stub_offset);
364 }
Logan Chien8b977d32012-02-21 19:14:55 +0800365#endif
366
Brian Carlstrome24fa612011-09-29 00:53:55 -0700367 return offset;
368}
369
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700370#define DCHECK_CODE_OFFSET() \
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800371 DCHECK_EQ(static_cast<off_t>(code_offset), lseek(file->Fd(), 0, SEEK_CUR))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700372
Elliott Hughes234da572011-11-03 22:13:06 -0700373bool OatWriter::Write(File* file) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700374 if (!file->WriteFully(oat_header_, sizeof(*oat_header_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700375 PLOG(ERROR) << "Failed to write oat header to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700376 return false;
377 }
378
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700379 if (!file->WriteFully(image_file_location_.data(),
380 image_file_location_.size())) {
381 PLOG(ERROR) << "Failed to write oat header image file location to " << file->name();
382 return false;
383 }
384
Elliott Hughes234da572011-11-03 22:13:06 -0700385 if (!WriteTables(file)) {
386 LOG(ERROR) << "Failed to write oat tables to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700387 return false;
388 }
389
Elliott Hughes234da572011-11-03 22:13:06 -0700390 size_t code_offset = WriteCode(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700391 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700392 LOG(ERROR) << "Failed to write oat code to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700393 return false;
394 }
395
Elliott Hughes234da572011-11-03 22:13:06 -0700396 code_offset = WriteCodeDexFiles(file, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700397 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700398 LOG(ERROR) << "Failed to write oat code for dex files to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700399 return false;
400 }
401
402 return true;
403}
404
405bool OatWriter::WriteTables(File* file) {
406 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
407 if (!oat_dex_files_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700408 PLOG(ERROR) << "Failed to write oat dex information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700409 return false;
410 }
411 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800412 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
413 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800414 off_t actual_offset = lseek(file->Fd(), expected_offset, SEEK_SET);
Brian Carlstrom89521892011-12-07 22:05:07 -0800415 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
416 const DexFile* dex_file = (*dex_files_)[i];
417 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
418 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
419 return false;
420 }
421 const DexFile* dex_file = (*dex_files_)[i];
422 if (!file->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
423 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << file->name();
424 return false;
425 }
426 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800427 for (size_t i = 0; i != oat_classes_.size(); ++i) {
428 if (!oat_classes_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700429 PLOG(ERROR) << "Failed to write oat methods information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700430 return false;
431 }
432 }
433 return true;
434}
435
436size_t OatWriter::WriteCode(File* file) {
437 uint32_t code_offset = oat_header_->GetExecutableOffset();
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800438 off_t new_offset = lseek(file->Fd(), executable_offset_padding_length_, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700439 if (static_cast<uint32_t>(new_offset) != code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700440 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700441 << " Expected: " << code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700442 return 0;
443 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700444 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700445 return code_offset;
446}
447
448size_t OatWriter::WriteCodeDexFiles(File* file, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700449 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800450 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700451 const DexFile* dex_file = (*dex_files_)[i];
452 CHECK(dex_file != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700453 code_offset = WriteCodeDexFile(file, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700454 if (code_offset == 0) {
455 return 0;
456 }
457 }
458 return code_offset;
459}
460
Ian Rogers0571d352011-11-03 19:51:38 -0700461size_t OatWriter::WriteCodeDexFile(File* file, size_t code_offset, size_t& oat_class_index,
462 const DexFile& dex_file) {
463 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
464 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700465 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700466 code_offset = WriteCodeClassDef(file, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700467 if (code_offset == 0) {
468 return 0;
469 }
470 }
471 return code_offset;
472}
473
Ian Rogers0571d352011-11-03 19:51:38 -0700474void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
475 const DexFile& dex_file, File* f) const {
476 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
477 << " to " << f->name();
Elliott Hughes234da572011-11-03 22:13:06 -0700478}
479
Brian Carlstrome24fa612011-09-29 00:53:55 -0700480size_t OatWriter::WriteCodeClassDef(File* file,
Ian Rogers0571d352011-11-03 19:51:38 -0700481 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700482 const DexFile& dex_file,
483 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700484 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700485 if (class_data == NULL) {
486 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700487 return code_offset;
488 }
Ian Rogers0571d352011-11-03 19:51:38 -0700489 ClassDataItemIterator it(dex_file, class_data);
490 // Skip fields
491 while (it.HasNextStaticField()) {
492 it.Next();
493 }
494 while (it.HasNextInstanceField()) {
495 it.Next();
496 }
497 // Process methods
498 size_t class_def_method_index = 0;
499 while (it.HasNextDirectMethod()) {
500 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
501 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
502 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700503 if (code_offset == 0) {
504 return 0;
505 }
Ian Rogers0571d352011-11-03 19:51:38 -0700506 class_def_method_index++;
507 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700508 }
Ian Rogers0571d352011-11-03 19:51:38 -0700509 while (it.HasNextVirtualMethod()) {
510 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
511 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700512 if (code_offset == 0) {
513 return 0;
514 }
Ian Rogers0571d352011-11-03 19:51:38 -0700515 class_def_method_index++;
516 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700517 }
518 return code_offset;
519}
520
Ian Rogers0571d352011-11-03 19:51:38 -0700521size_t OatWriter::WriteCodeMethod(File* file, size_t code_offset, size_t oat_class_index,
522 size_t class_def_method_index, bool is_static,
523 uint32_t method_idx, const DexFile& dex_file) {
Logan Chien8b977d32012-02-21 19:14:55 +0800524#if !defined(ART_USE_LLVM_COMPILER)
Ian Rogers0571d352011-11-03 19:51:38 -0700525 const CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800526 compiler_->GetCompiledMethod(Compiler::MethodReference(&dex_file, method_idx));
Ian Rogers0571d352011-11-03 19:51:38 -0700527
528 uint32_t frame_size_in_bytes = 0;
529 uint32_t core_spill_mask = 0;
530 uint32_t fp_spill_mask = 0;
531
532 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800533 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700534
535
536 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700537 uint32_t aligned_code_offset = compiled_method->AlignCode(code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700538 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
539 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800540 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700541 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700542 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700543 << " Expected: " << aligned_code_offset << " File: " << file->name();
Brian Carlstromf03c2882012-03-05 20:29:06 -0800544 return 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700545 }
546 code_offset += aligned_code_delta;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700547 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700548 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700549 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700550 const std::vector<uint8_t>& code = compiled_method->GetCode();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700551 uint32_t code_size = code.size() * sizeof(code[0]);
552 CHECK_NE(code_size, 0U);
jeffhao55d78212011-11-02 11:41:50 -0700553
554 // Deduplicate code arrays
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700555 size_t offset = code_offset + sizeof(code_size) + compiled_method->CodeDelta();
jeffhaof479dcc2011-11-02 15:54:15 -0700556 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Ian Rogers0571d352011-11-03 19:51:38 -0700557 if (code_iter != code_offsets_.end() && offset != method_offsets.code_offset_) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700558 DCHECK(code_iter->second == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700559 } else {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700560 DCHECK(offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
561 if (!file->WriteFully(&code_size, sizeof(code_size))) {
562 ReportWriteFailure("method code size", method_idx, dex_file, file);
563 return 0;
564 }
565 code_offset += sizeof(code_size);
566 DCHECK_CODE_OFFSET();
jeffhao55d78212011-11-02 11:41:50 -0700567 if (!file->WriteFully(&code[0], code_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700568 ReportWriteFailure("method code", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800569 return 0;
jeffhao55d78212011-11-02 11:41:50 -0700570 }
571 code_offset += code_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700572 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700573 DCHECK_CODE_OFFSET();
Ian Rogers0571d352011-11-03 19:51:38 -0700574 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
575 core_spill_mask = compiled_method->GetCoreSpillMask();
576 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700577
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700578 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
579 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700580
581 // Deduplicate mapping tables
Ian Rogers0571d352011-11-03 19:51:38 -0700582 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
583 mapping_table_offsets_.find(&mapping_table);
584 if (mapping_iter != mapping_table_offsets_.end() &&
585 code_offset != method_offsets.mapping_table_offset_) {
586 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
587 || mapping_iter->second == method_offsets.mapping_table_offset_)
588 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700589 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700590 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
591 || code_offset == method_offsets.mapping_table_offset_)
592 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700593 if (!file->WriteFully(&mapping_table[0], mapping_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700594 ReportWriteFailure("mapping table", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800595 return 0;
jeffhao55d78212011-11-02 11:41:50 -0700596 }
597 code_offset += mapping_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700598 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700599 DCHECK_CODE_OFFSET();
600
601 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
602 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700603
604 // Deduplicate vmap tables
Ian Rogers0571d352011-11-03 19:51:38 -0700605 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
606 vmap_table_offsets_.find(&vmap_table);
607 if (vmap_iter != vmap_table_offsets_.end() &&
608 code_offset != method_offsets.vmap_table_offset_) {
609 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
610 || vmap_iter->second == method_offsets.vmap_table_offset_)
611 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700612 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700613 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
614 || code_offset == method_offsets.vmap_table_offset_)
615 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700616 if (!file->WriteFully(&vmap_table[0], vmap_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700617 ReportWriteFailure("vmap table", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800618 return 0;
jeffhao55d78212011-11-02 11:41:50 -0700619 }
620 code_offset += vmap_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700621 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700622 DCHECK_CODE_OFFSET();
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800623
624 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
625 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
626
627 // Deduplicate GC maps
628 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
629 gc_map_offsets_.find(&gc_map);
630 if (gc_map_iter != gc_map_offsets_.end() &&
631 code_offset != method_offsets.gc_map_offset_) {
632 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
633 || gc_map_iter->second == method_offsets.gc_map_offset_)
634 << PrettyMethod(method_idx, dex_file);
635 } else {
636 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
637 || code_offset == method_offsets.gc_map_offset_)
638 << PrettyMethod(method_idx, dex_file);
639 if (!file->WriteFully(&gc_map[0], gc_map_size)) {
640 ReportWriteFailure("GC map", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800641 return 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800642 }
643 code_offset += gc_map_size;
644 }
645 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700646 }
Ian Rogers0571d352011-11-03 19:51:38 -0700647 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
648 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700649 if (compiled_invoke_stub != NULL) {
650 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
651 compiler_->GetInstructionSet());
652 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
653 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800654 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700655 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
656 PLOG(ERROR) << "Failed to seek to align invoke stub code. Actual: " << new_offset
657 << " Expected: " << aligned_code_offset;
Brian Carlstromf03c2882012-03-05 20:29:06 -0800658 return 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700659 }
660 code_offset += aligned_code_delta;
661 DCHECK_CODE_OFFSET();
662 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700663 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700664 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700665 uint32_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
666 CHECK_NE(invoke_stub_size, 0U);
jeffhao55d78212011-11-02 11:41:50 -0700667
668 // Deduplicate invoke stubs
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700669 size_t offset = code_offset + sizeof(invoke_stub_size);
Ian Rogers0571d352011-11-03 19:51:38 -0700670 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter =
671 code_offsets_.find(&invoke_stub);
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700672 if (stub_iter != code_offsets_.end() && offset != method_offsets.invoke_stub_offset_) {
673 DCHECK(stub_iter->second == method_offsets.invoke_stub_offset_) << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700674 } else {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700675 DCHECK(offset == method_offsets.invoke_stub_offset_) << PrettyMethod(method_idx, dex_file);
676 if (!file->WriteFully(&invoke_stub_size, sizeof(invoke_stub_size))) {
677 ReportWriteFailure("invoke stub code size", method_idx, dex_file, file);
678 return 0;
679 }
680 code_offset += sizeof(invoke_stub_size);
681 DCHECK_CODE_OFFSET();
jeffhao55d78212011-11-02 11:41:50 -0700682 if (!file->WriteFully(&invoke_stub[0], invoke_stub_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700683 ReportWriteFailure("invoke stub code", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800684 return 0;
jeffhao55d78212011-11-02 11:41:50 -0700685 }
686 code_offset += invoke_stub_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700687 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700688 DCHECK_CODE_OFFSET();
689 }
Logan Chien8b977d32012-02-21 19:14:55 +0800690#endif
691
Brian Carlstrome24fa612011-09-29 00:53:55 -0700692 return code_offset;
693}
694
Brian Carlstrome24fa612011-09-29 00:53:55 -0700695OatWriter::OatDexFile::OatDexFile(const DexFile& dex_file) {
Elliott Hughes95572412011-12-13 18:14:20 -0800696 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700697 dex_file_location_size_ = location.size();
698 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800699 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800700 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800701 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700702}
703
704size_t OatWriter::OatDexFile::SizeOf() const {
705 return sizeof(dex_file_location_size_)
706 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800707 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800708 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800709 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700710}
711
712void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
713 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
714 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800715 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800716 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800717 oat_header.UpdateChecksum(&methods_offsets_[0],
718 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700719}
720
721bool OatWriter::OatDexFile::Write(File* file) const {
722 if (!file->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700723 PLOG(ERROR) << "Failed to write dex file location length to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700724 return false;
725 }
726 if (!file->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700727 PLOG(ERROR) << "Failed to write dex file location data to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700728 return false;
729 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800730 if (!file->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
731 PLOG(ERROR) << "Failed to write dex file location checksum to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700732 return false;
733 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800734 if (!file->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
735 PLOG(ERROR) << "Failed to write dex file offset to " << file->name();
736 return false;
737 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800738 if (!file->WriteFully(&methods_offsets_[0],
739 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700740 PLOG(ERROR) << "Failed to write methods offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700741 return false;
742 }
743 return true;
744}
745
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800746OatWriter::OatClass::OatClass(Class::Status status, uint32_t methods_count) {
747 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700748 method_offsets_.resize(methods_count);
749}
750
Brian Carlstrom389efb02012-01-11 12:06:26 -0800751size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800752 return sizeof(status_)
753 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700754}
755
Brian Carlstrom389efb02012-01-11 12:06:26 -0800756void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800757 oat_header.UpdateChecksum(&status_, sizeof(status_));
758 oat_header.UpdateChecksum(&method_offsets_[0],
759 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700760}
761
Brian Carlstrom389efb02012-01-11 12:06:26 -0800762bool OatWriter::OatClass::Write(File* file) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800763 if (!file->WriteFully(&status_, sizeof(status_))) {
764 PLOG(ERROR) << "Failed to write class status to " << file->name();
765 return false;
766 }
767 if (!file->WriteFully(&method_offsets_[0],
768 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700769 PLOG(ERROR) << "Failed to write method offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700770 return false;
771 }
772 return true;
773}
774
775} // namespace art