blob: 013a5619579e48056cb922e0537584b46a4c0025 [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
Elliott Hughesa0e18062012-04-13 15:59:59 -070019#include <zlib.h>
20
Brian Carlstrome24fa612011-09-29 00:53:55 -070021#include "class_linker.h"
22#include "class_loader.h"
23#include "file.h"
24#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070025#include "safe_map.h"
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070026#include "space.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070027#include "stl_util.h"
28
29namespace art {
30
Elliott Hughes234da572011-11-03 22:13:06 -070031bool OatWriter::Create(File* file,
Ian Rogers365c1022012-06-22 15:05:28 -070032 ClassLoader* class_loader,
jeffhao10037c82012-01-23 15:06:23 -080033 const std::vector<const DexFile*>& dex_files,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070034 uint32_t image_file_location_checksum,
35 const std::string& image_file_location,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070036 const Compiler& compiler) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070037 OatWriter oat_writer(dex_files,
38 image_file_location_checksum,
39 image_file_location,
40 class_loader,
41 compiler);
Elliott Hughes234da572011-11-03 22:13:06 -070042 return oat_writer.Write(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -070043}
44
Brian Carlstrom3320cf42011-10-04 14:58:28 -070045OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070046 uint32_t image_file_location_checksum,
47 const std::string& image_file_location,
Ian Rogers365c1022012-06-22 15:05:28 -070048 ClassLoader* class_loader,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070049 const Compiler& compiler) {
50 compiler_ = &compiler;
Brian Carlstrome24fa612011-09-29 00:53:55 -070051 class_loader_ = class_loader;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070052 image_file_location_checksum_ = image_file_location_checksum;
53 image_file_location_ = image_file_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -070054 dex_files_ = &dex_files;
Ian Rogers0571d352011-11-03 19:51:38 -070055 oat_header_ = NULL;
56 executable_offset_padding_length_ = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -070057
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070058 size_t offset = InitOatHeader();
Brian Carlstrome24fa612011-09-29 00:53:55 -070059 offset = InitOatDexFiles(offset);
Brian Carlstrom89521892011-12-07 22:05:07 -080060 offset = InitDexFiles(offset);
Brian Carlstrom389efb02012-01-11 12:06:26 -080061 offset = InitOatClasses(offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -070062 offset = InitOatCode(offset);
63 offset = InitOatCodeDexFiles(offset);
64
65 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -070066}
67
Ian Rogers0571d352011-11-03 19:51:38 -070068OatWriter::~OatWriter() {
69 delete oat_header_;
70 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -080071 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -070072}
73
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070074size_t OatWriter::InitOatHeader() {
Brian Carlstrome24fa612011-09-29 00:53:55 -070075 // create the OatHeader
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070076 oat_header_ = new OatHeader(compiler_->GetInstructionSet(),
77 dex_files_,
78 image_file_location_checksum_,
79 image_file_location_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070080 size_t offset = sizeof(*oat_header_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070081 offset += image_file_location_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -070082 return offset;
83}
84
85size_t OatWriter::InitOatDexFiles(size_t offset) {
86 // create the OatDexFiles
87 for (size_t i = 0; i != dex_files_->size(); ++i) {
88 const DexFile* dex_file = (*dex_files_)[i];
89 CHECK(dex_file != NULL);
90 OatDexFile* oat_dex_file = new OatDexFile(*dex_file);
91 oat_dex_files_.push_back(oat_dex_file);
92 offset += oat_dex_file->SizeOf();
93 }
94 return offset;
95}
96
Brian Carlstrom89521892011-12-07 22:05:07 -080097size_t OatWriter::InitDexFiles(size_t offset) {
98 // calculate the offsets within OatDexFiles to the DexFiles
99 for (size_t i = 0; i != dex_files_->size(); ++i) {
100 // dex files are required to be 4 byte aligned
101 offset = RoundUp(offset, 4);
102
103 // set offset in OatDexFile to DexFile
104 oat_dex_files_[i]->dex_file_offset_ = offset;
105
106 const DexFile* dex_file = (*dex_files_)[i];
107 offset += dex_file->GetHeader().file_size_;
108 }
109 return offset;
110}
111
Brian Carlstrom389efb02012-01-11 12:06:26 -0800112size_t OatWriter::InitOatClasses(size_t offset) {
113 // create the OatClasses
114 // calculate the offsets within OatDexFiles to OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -0700115 for (size_t i = 0; i != dex_files_->size(); ++i) {
116 const DexFile* dex_file = (*dex_files_)[i];
117 for (size_t class_def_index = 0;
118 class_def_index < dex_file->NumClassDefs();
Ian Rogersc20a83e2012-01-18 18:15:32 -0800119 class_def_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800120 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700121 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
122 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700123 uint32_t num_methods = 0;
124 if (class_data != NULL) { // ie not an empty class, such as a marker interface
125 ClassDataItemIterator it(*dex_file, class_data);
126 size_t num_direct_methods = it.NumDirectMethods();
127 size_t num_virtual_methods = it.NumVirtualMethods();
128 num_methods = num_direct_methods + num_virtual_methods;
129 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800130
131 CompiledClass* compiled_class =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800132 compiler_->GetCompiledClass(Compiler::MethodReference(dex_file, class_def_index));
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800133 Class::Status status =
134 (compiled_class != NULL) ? compiled_class->GetStatus() : Class::kStatusNotReady;
135
136 OatClass* oat_class = new OatClass(status, num_methods);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800137 oat_classes_.push_back(oat_class);
138 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700139 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800140 oat_dex_files_[i]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700141 }
142 return offset;
143}
144
145size_t OatWriter::InitOatCode(size_t offset) {
146 // calculate the offsets within OatHeader to executable code
147 size_t old_offset = offset;
148 // required to be on a new page boundary
149 offset = RoundUp(offset, kPageSize);
150 oat_header_->SetExecutableOffset(offset);
151 executable_offset_padding_length_ = offset - old_offset;
152 return offset;
153}
154
155size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700156 size_t oat_class_index = 0;
157 for (size_t i = 0; i != dex_files_->size(); ++i) {
158 const DexFile* dex_file = (*dex_files_)[i];
159 CHECK(dex_file != NULL);
160 offset = InitOatCodeDexFile(offset, oat_class_index, *dex_file);
161 }
162 return offset;
163}
164
165size_t OatWriter::InitOatCodeDexFile(size_t offset,
166 size_t& oat_class_index,
167 const DexFile& dex_file) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800168 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700169 class_def_index < dex_file.NumClassDefs();
170 class_def_index++, oat_class_index++) {
171 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800172 offset = InitOatCodeClassDef(offset, oat_class_index, class_def_index, dex_file, class_def);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800173 oat_classes_[oat_class_index]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700174 }
175 return offset;
176}
177
178size_t OatWriter::InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800179 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700180 const DexFile& dex_file,
181 const DexFile::ClassDef& class_def) {
182 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700183 if (class_data == NULL) {
184 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700185 return offset;
186 }
Ian Rogers0571d352011-11-03 19:51:38 -0700187 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800188 CHECK_EQ(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700189 it.NumDirectMethods() + it.NumVirtualMethods());
190 // Skip fields
191 while (it.HasNextStaticField()) {
192 it.Next();
193 }
194 while (it.HasNextInstanceField()) {
195 it.Next();
196 }
197 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700198 size_t class_def_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700199 while (it.HasNextDirectMethod()) {
200 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
Ian Rogersc20a83e2012-01-18 18:15:32 -0800201 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
202 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index, is_native,
203 is_static, true, it.GetMemberIndex(), &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700204 class_def_method_index++;
205 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700206 }
Ian Rogers0571d352011-11-03 19:51:38 -0700207 while (it.HasNextVirtualMethod()) {
208 CHECK_EQ(it.GetMemberAccessFlags() & kAccStatic, 0U);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800209 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
210 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index, is_native,
211 false, false, it.GetMemberIndex(), &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700212 class_def_method_index++;
213 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700214 }
Ian Rogers0571d352011-11-03 19:51:38 -0700215 DCHECK(!it.HasNext());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700216 return offset;
217}
218
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700219size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
220 size_t __attribute__((unused)) class_def_index,
221 size_t class_def_method_index,
222 bool __attribute__((unused)) is_native,
223 bool is_static, bool is_direct,
224 uint32_t method_idx, const DexFile* dex_file) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700225 // derived from CompiledMethod if available
226 uint32_t code_offset = 0;
227 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700228 uint32_t core_spill_mask = 0;
229 uint32_t fp_spill_mask = 0;
230 uint32_t mapping_table_offset = 0;
231 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800232 uint32_t gc_map_offset = 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700233 // derived from CompiledInvokeStub if available
234 uint32_t invoke_stub_offset = 0;
Brian Carlstromfd2ec542012-05-02 15:08:57 -0700235#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800236 uint32_t proxy_stub_offset = 0;
Brian Carlstromfd2ec542012-05-02 15:08:57 -0700237#endif
Brian Carlstrome24fa612011-09-29 00:53:55 -0700238
Ian Rogers0571d352011-11-03 19:51:38 -0700239 CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800240 compiler_->GetCompiledMethod(Compiler::MethodReference(dex_file, method_idx));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700241 if (compiled_method != NULL) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800242 offset = compiled_method->AlignCode(offset);
243 DCHECK_ALIGNED(offset, kArmAlignment);
244 const std::vector<uint8_t>& code = compiled_method->GetCode();
245 uint32_t code_size = code.size() * sizeof(code[0]);
246 CHECK_NE(code_size, 0U);
247 uint32_t thumb_offset = compiled_method->CodeDelta();
248 code_offset = offset + sizeof(code_size) + thumb_offset;
249
250 // Deduplicate code arrays
251 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
252 if (code_iter != code_offsets_.end()) {
253 code_offset = code_iter->second;
jeffhao55d78212011-11-02 11:41:50 -0700254 } else {
Logan Chien971bf3f2012-05-01 15:47:55 +0800255 code_offsets_.Put(&code, code_offset);
256 offset += sizeof(code_size); // code size is prepended before code
257 offset += code_size;
258 oat_header_->UpdateChecksum(&code[0], code_size);
259 }
260 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
261 core_spill_mask = compiled_method->GetCoreSpillMask();
262 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700263
Logan Chien971bf3f2012-05-01 15:47:55 +0800264 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
265 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
266 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700267
Logan Chien971bf3f2012-05-01 15:47:55 +0800268 // Deduplicate mapping tables
269 SafeMap<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
270 if (mapping_iter != mapping_table_offsets_.end()) {
271 mapping_table_offset = mapping_iter->second;
272 } else {
273 mapping_table_offsets_.Put(&mapping_table, mapping_table_offset);
274 offset += mapping_table_size;
275 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
276 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700277
Logan Chien971bf3f2012-05-01 15:47:55 +0800278 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
279 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
280 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700281
Logan Chien971bf3f2012-05-01 15:47:55 +0800282 // Deduplicate vmap tables
283 SafeMap<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
284 if (vmap_iter != vmap_table_offsets_.end()) {
285 vmap_table_offset = vmap_iter->second;
286 } else {
287 vmap_table_offsets_.Put(&vmap_table, vmap_table_offset);
288 offset += vmap_table_size;
289 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
290 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800291
Logan Chien971bf3f2012-05-01 15:47:55 +0800292 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
293 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
294 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
Logan Chienccb7bf12012-03-28 12:52:32 +0800295
Logan Chien971bf3f2012-05-01 15:47:55 +0800296#if !defined(NDEBUG) && !defined(ART_USE_LLVM_COMPILER)
297 // We expect GC maps except when the class hasn't been verified or the method is native
298 CompiledClass* compiled_class =
299 compiler_->GetCompiledClass(Compiler::MethodReference(dex_file, class_def_index));
300 Class::Status status =
301 (compiled_class != NULL) ? compiled_class->GetStatus() : Class::kStatusNotReady;
302 CHECK(gc_map_size != 0 || is_native || status < Class::kStatusVerified)
303 << &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 -0800304#endif
305
Logan Chien971bf3f2012-05-01 15:47:55 +0800306 // Deduplicate GC maps
307 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = gc_map_offsets_.find(&gc_map);
308 if (gc_map_iter != gc_map_offsets_.end()) {
309 gc_map_offset = gc_map_iter->second;
310 } else {
311 gc_map_offsets_.Put(&gc_map, gc_map_offset);
312 offset += gc_map_size;
313 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800314 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700315 }
316
Ian Rogers0571d352011-11-03 19:51:38 -0700317 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx));
318 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700319 if (compiled_invoke_stub != NULL) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800320 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
321 DCHECK_ALIGNED(offset, kArmAlignment);
322 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
323 uint32_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
324 CHECK_NE(invoke_stub_size, 0U);
325 uint32_t thumb_offset = compiled_invoke_stub->CodeDelta();
326 invoke_stub_offset = offset + sizeof(invoke_stub_size) + thumb_offset;
Logan Chienccb7bf12012-03-28 12:52:32 +0800327
Logan Chien971bf3f2012-05-01 15:47:55 +0800328 // Deduplicate invoke stubs
329 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&invoke_stub);
330 if (stub_iter != code_offsets_.end()) {
331 invoke_stub_offset = stub_iter->second;
332 } else {
333 code_offsets_.Put(&invoke_stub, invoke_stub_offset);
334 offset += sizeof(invoke_stub_size); // invoke stub size is prepended before code
335 offset += invoke_stub_size;
336 oat_header_->UpdateChecksum(&invoke_stub[0], invoke_stub_size);
jeffhao55d78212011-11-02 11:41:50 -0700337 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700338 }
339
Logan Chien7a2a23a2012-06-06 11:01:00 +0800340#if defined(ART_USE_LLVM_COMPILER)
341 if (!is_static) {
342 const CompiledInvokeStub* compiled_proxy_stub = compiler_->FindProxyStub(shorty);
343 if (compiled_proxy_stub != NULL) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800344 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
345 DCHECK_ALIGNED(offset, kArmAlignment);
346 const std::vector<uint8_t>& proxy_stub = compiled_proxy_stub->GetCode();
347 uint32_t proxy_stub_size = proxy_stub.size() * sizeof(proxy_stub[0]);
348 CHECK_NE(proxy_stub_size, 0U);
349 uint32_t thumb_offset = compiled_proxy_stub->CodeDelta();
350 proxy_stub_offset = offset + sizeof(proxy_stub_size) + thumb_offset;
351
352 // Deduplicate proxy stubs
353 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&proxy_stub);
354 if (stub_iter != code_offsets_.end()) {
355 proxy_stub_offset = stub_iter->second;
356 } else {
357 code_offsets_.Put(&proxy_stub, proxy_stub_offset);
358 offset += sizeof(proxy_stub_size); // proxy stub size is prepended before code
359 offset += proxy_stub_size;
360 oat_header_->UpdateChecksum(&proxy_stub[0], proxy_stub_size);
361 }
Logan Chien7a2a23a2012-06-06 11:01:00 +0800362 }
363 }
364#endif
365
Brian Carlstrom389efb02012-01-11 12:06:26 -0800366 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index]
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700367 = OatMethodOffsets(code_offset,
368 frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700369 core_spill_mask,
370 fp_spill_mask,
371 mapping_table_offset,
372 vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800373 gc_map_offset,
Logan Chienccb7bf12012-03-28 12:52:32 +0800374 invoke_stub_offset
375#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800376 , proxy_stub_offset
Logan Chienccb7bf12012-03-28 12:52:32 +0800377#endif
378 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700379
Ian Rogers0571d352011-11-03 19:51:38 -0700380 if (compiler_->IsImage()) {
381 ClassLinker* linker = Runtime::Current()->GetClassLinker();
382 DexCache* dex_cache = linker->FindDexCache(*dex_file);
383 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader_,
384 is_direct);
385 CHECK(method != NULL);
386 method->SetFrameSizeInBytes(frame_size_in_bytes);
387 method->SetCoreSpillMask(core_spill_mask);
388 method->SetFpSpillMask(fp_spill_mask);
389 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800390 // Don't overwrite static method trampoline
391 if (!method->IsStatic() || method->IsConstructor() ||
392 method->GetDeclaringClass()->IsInitialized()) {
393 method->SetOatCodeOffset(code_offset);
394 } else {
395 method->SetCode(Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod)->GetData());
396 }
Ian Rogers0571d352011-11-03 19:51:38 -0700397 method->SetOatVmapTableOffset(vmap_table_offset);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800398 method->SetOatGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700399 method->SetOatInvokeStubOffset(invoke_stub_offset);
400 }
Logan Chien8b977d32012-02-21 19:14:55 +0800401
Brian Carlstrome24fa612011-09-29 00:53:55 -0700402 return offset;
403}
404
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700405#define DCHECK_CODE_OFFSET() \
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800406 DCHECK_EQ(static_cast<off_t>(code_offset), lseek(file->Fd(), 0, SEEK_CUR))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700407
Elliott Hughes234da572011-11-03 22:13:06 -0700408bool OatWriter::Write(File* file) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700409 if (!file->WriteFully(oat_header_, sizeof(*oat_header_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700410 PLOG(ERROR) << "Failed to write oat header to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700411 return false;
412 }
413
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700414 if (!file->WriteFully(image_file_location_.data(),
415 image_file_location_.size())) {
416 PLOG(ERROR) << "Failed to write oat header image file location to " << file->name();
417 return false;
418 }
419
Elliott Hughes234da572011-11-03 22:13:06 -0700420 if (!WriteTables(file)) {
421 LOG(ERROR) << "Failed to write oat tables to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700422 return false;
423 }
424
Elliott Hughes234da572011-11-03 22:13:06 -0700425 size_t code_offset = WriteCode(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700426 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700427 LOG(ERROR) << "Failed to write oat code to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700428 return false;
429 }
430
Elliott Hughes234da572011-11-03 22:13:06 -0700431 code_offset = WriteCodeDexFiles(file, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700432 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700433 LOG(ERROR) << "Failed to write oat code for dex files to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700434 return false;
435 }
436
437 return true;
438}
439
440bool OatWriter::WriteTables(File* file) {
441 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
442 if (!oat_dex_files_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700443 PLOG(ERROR) << "Failed to write oat dex information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700444 return false;
445 }
446 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800447 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
448 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800449 off_t actual_offset = lseek(file->Fd(), expected_offset, SEEK_SET);
Brian Carlstrom89521892011-12-07 22:05:07 -0800450 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
451 const DexFile* dex_file = (*dex_files_)[i];
452 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
453 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
454 return false;
455 }
456 const DexFile* dex_file = (*dex_files_)[i];
457 if (!file->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
458 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << file->name();
459 return false;
460 }
461 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800462 for (size_t i = 0; i != oat_classes_.size(); ++i) {
463 if (!oat_classes_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700464 PLOG(ERROR) << "Failed to write oat methods information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700465 return false;
466 }
467 }
468 return true;
469}
470
471size_t OatWriter::WriteCode(File* file) {
472 uint32_t code_offset = oat_header_->GetExecutableOffset();
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800473 off_t new_offset = lseek(file->Fd(), executable_offset_padding_length_, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700474 if (static_cast<uint32_t>(new_offset) != code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700475 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700476 << " Expected: " << code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700477 return 0;
478 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700479 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700480 return code_offset;
481}
482
483size_t OatWriter::WriteCodeDexFiles(File* file, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700484 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800485 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700486 const DexFile* dex_file = (*dex_files_)[i];
487 CHECK(dex_file != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700488 code_offset = WriteCodeDexFile(file, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700489 if (code_offset == 0) {
490 return 0;
491 }
492 }
493 return code_offset;
494}
495
Ian Rogers0571d352011-11-03 19:51:38 -0700496size_t OatWriter::WriteCodeDexFile(File* file, size_t code_offset, size_t& oat_class_index,
497 const DexFile& dex_file) {
498 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
499 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700500 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700501 code_offset = WriteCodeClassDef(file, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700502 if (code_offset == 0) {
503 return 0;
504 }
505 }
506 return code_offset;
507}
508
Ian Rogers0571d352011-11-03 19:51:38 -0700509void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
510 const DexFile& dex_file, File* f) const {
511 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
512 << " to " << f->name();
Elliott Hughes234da572011-11-03 22:13:06 -0700513}
514
Brian Carlstrome24fa612011-09-29 00:53:55 -0700515size_t OatWriter::WriteCodeClassDef(File* file,
Ian Rogers0571d352011-11-03 19:51:38 -0700516 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700517 const DexFile& dex_file,
518 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700519 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700520 if (class_data == NULL) {
521 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700522 return code_offset;
523 }
Ian Rogers0571d352011-11-03 19:51:38 -0700524 ClassDataItemIterator it(dex_file, class_data);
525 // Skip fields
526 while (it.HasNextStaticField()) {
527 it.Next();
528 }
529 while (it.HasNextInstanceField()) {
530 it.Next();
531 }
532 // Process methods
533 size_t class_def_method_index = 0;
534 while (it.HasNextDirectMethod()) {
535 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
536 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
537 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700538 if (code_offset == 0) {
539 return 0;
540 }
Ian Rogers0571d352011-11-03 19:51:38 -0700541 class_def_method_index++;
542 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700543 }
Ian Rogers0571d352011-11-03 19:51:38 -0700544 while (it.HasNextVirtualMethod()) {
545 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
546 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700547 if (code_offset == 0) {
548 return 0;
549 }
Ian Rogers0571d352011-11-03 19:51:38 -0700550 class_def_method_index++;
551 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700552 }
553 return code_offset;
554}
555
Ian Rogers0571d352011-11-03 19:51:38 -0700556size_t OatWriter::WriteCodeMethod(File* file, size_t code_offset, size_t oat_class_index,
557 size_t class_def_method_index, bool is_static,
558 uint32_t method_idx, const DexFile& dex_file) {
559 const CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800560 compiler_->GetCompiledMethod(Compiler::MethodReference(&dex_file, method_idx));
Ian Rogers0571d352011-11-03 19:51:38 -0700561
Ian Rogers0571d352011-11-03 19:51:38 -0700562 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800563 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700564
565
566 if (compiled_method != NULL) { // ie. not an abstract method
Logan Chien971bf3f2012-05-01 15:47:55 +0800567 uint32_t aligned_code_offset = compiled_method->AlignCode(code_offset);
568 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
569 if (aligned_code_delta != 0) {
570 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
571 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
572 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
573 << " Expected: " << aligned_code_offset << " File: " << file->name();
574 return 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700575 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800576 code_offset += aligned_code_delta;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700577 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700578 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800579 DCHECK_ALIGNED(code_offset, kArmAlignment);
580 const std::vector<uint8_t>& code = compiled_method->GetCode();
581 uint32_t code_size = code.size() * sizeof(code[0]);
582 CHECK_NE(code_size, 0U);
583
584 // Deduplicate code arrays
585 size_t offset = code_offset + sizeof(code_size) + compiled_method->CodeDelta();
586 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
587 if (code_iter != code_offsets_.end() && offset != method_offsets.code_offset_) {
588 DCHECK(code_iter->second == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
589 } else {
590 DCHECK(offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
591 if (!file->WriteFully(&code_size, sizeof(code_size))) {
592 ReportWriteFailure("method code size", method_idx, dex_file, file);
593 return 0;
594 }
595 code_offset += sizeof(code_size);
596 DCHECK_CODE_OFFSET();
597 if (!file->WriteFully(&code[0], code_size)) {
598 ReportWriteFailure("method code", method_idx, dex_file, file);
599 return 0;
600 }
601 code_offset += code_size;
602 }
603 DCHECK_CODE_OFFSET();
604
605 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
606 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
607
608 // Deduplicate mapping tables
609 SafeMap<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
610 mapping_table_offsets_.find(&mapping_table);
611 if (mapping_iter != mapping_table_offsets_.end() &&
612 code_offset != method_offsets.mapping_table_offset_) {
613 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
614 || mapping_iter->second == method_offsets.mapping_table_offset_)
615 << PrettyMethod(method_idx, dex_file);
616 } else {
617 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
618 || code_offset == method_offsets.mapping_table_offset_)
619 << PrettyMethod(method_idx, dex_file);
620 if (!file->WriteFully(&mapping_table[0], mapping_table_size)) {
621 ReportWriteFailure("mapping table", method_idx, dex_file, file);
622 return 0;
623 }
624 code_offset += mapping_table_size;
625 }
626 DCHECK_CODE_OFFSET();
627
628 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
629 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
630
631 // Deduplicate vmap tables
632 SafeMap<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
633 vmap_table_offsets_.find(&vmap_table);
634 if (vmap_iter != vmap_table_offsets_.end() &&
635 code_offset != method_offsets.vmap_table_offset_) {
636 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
637 || vmap_iter->second == method_offsets.vmap_table_offset_)
638 << PrettyMethod(method_idx, dex_file);
639 } else {
640 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
641 || code_offset == method_offsets.vmap_table_offset_)
642 << PrettyMethod(method_idx, dex_file);
643 if (!file->WriteFully(&vmap_table[0], vmap_table_size)) {
644 ReportWriteFailure("vmap table", method_idx, dex_file, file);
645 return 0;
646 }
647 code_offset += vmap_table_size;
648 }
649 DCHECK_CODE_OFFSET();
650
651 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
652 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
653
654 // Deduplicate GC maps
655 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
656 gc_map_offsets_.find(&gc_map);
657 if (gc_map_iter != gc_map_offsets_.end() &&
658 code_offset != method_offsets.gc_map_offset_) {
659 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
660 || gc_map_iter->second == method_offsets.gc_map_offset_)
661 << PrettyMethod(method_idx, dex_file);
662 } else {
663 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
664 || code_offset == method_offsets.gc_map_offset_)
665 << PrettyMethod(method_idx, dex_file);
666 if (!file->WriteFully(&gc_map[0], gc_map_size)) {
667 ReportWriteFailure("GC map", method_idx, dex_file, file);
668 return 0;
669 }
670 code_offset += gc_map_size;
671 }
672 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700673 }
Ian Rogers0571d352011-11-03 19:51:38 -0700674 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
675 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700676 if (compiled_invoke_stub != NULL) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800677 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
678 compiler_->GetInstructionSet());
679 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
680 if (aligned_code_delta != 0) {
681 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
682 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
683 PLOG(ERROR) << "Failed to seek to align invoke stub code. Actual: " << new_offset
684 << " Expected: " << aligned_code_offset;
685 return 0;
686 }
687 code_offset += aligned_code_delta;
688 DCHECK_CODE_OFFSET();
689 }
690 DCHECK_ALIGNED(code_offset, kArmAlignment);
691 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
692 uint32_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
693 CHECK_NE(invoke_stub_size, 0U);
694
695 // Deduplicate invoke stubs
696 size_t offset = code_offset + sizeof(invoke_stub_size) + compiled_invoke_stub->CodeDelta();
697 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter =
698 code_offsets_.find(&invoke_stub);
699 if (stub_iter != code_offsets_.end() && offset != method_offsets.invoke_stub_offset_) {
700 DCHECK(stub_iter->second == method_offsets.invoke_stub_offset_) << PrettyMethod(method_idx, dex_file);
701 } else {
702 DCHECK(offset == method_offsets.invoke_stub_offset_) << PrettyMethod(method_idx, dex_file);
703 if (!file->WriteFully(&invoke_stub_size, sizeof(invoke_stub_size))) {
704 ReportWriteFailure("invoke stub code size", method_idx, dex_file, file);
705 return 0;
706 }
707 code_offset += sizeof(invoke_stub_size);
708 DCHECK_CODE_OFFSET();
709 if (!file->WriteFully(&invoke_stub[0], invoke_stub_size)) {
710 ReportWriteFailure("invoke stub code", method_idx, dex_file, file);
711 return 0;
712 }
713 code_offset += invoke_stub_size;
714 DCHECK_CODE_OFFSET();
715 }
716 }
717
718#if defined(ART_USE_LLVM_COMPILER)
719 if (!is_static) {
720 const CompiledInvokeStub* compiled_proxy_stub = compiler_->FindProxyStub(shorty);
721 if (compiled_proxy_stub != NULL) {
Logan Chienccb7bf12012-03-28 12:52:32 +0800722 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
723 compiler_->GetInstructionSet());
724 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
Logan Chien971bf3f2012-05-01 15:47:55 +0800725 CHECK(aligned_code_delta < 48u);
Logan Chienccb7bf12012-03-28 12:52:32 +0800726 if (aligned_code_delta != 0) {
727 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
728 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800729 PLOG(ERROR) << "Failed to seek to align proxy stub code. Actual: " << new_offset
Logan Chienccb7bf12012-03-28 12:52:32 +0800730 << " Expected: " << aligned_code_offset;
731 return 0;
732 }
733 code_offset += aligned_code_delta;
734 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700735 }
Logan Chienccb7bf12012-03-28 12:52:32 +0800736 DCHECK_ALIGNED(code_offset, kArmAlignment);
Logan Chien971bf3f2012-05-01 15:47:55 +0800737 const std::vector<uint8_t>& proxy_stub = compiled_proxy_stub->GetCode();
738 uint32_t proxy_stub_size = proxy_stub.size() * sizeof(proxy_stub[0]);
739 CHECK_NE(proxy_stub_size, 0U);
jeffhao55d78212011-11-02 11:41:50 -0700740
Logan Chien971bf3f2012-05-01 15:47:55 +0800741 // Deduplicate proxy stubs
742 size_t offset = code_offset + sizeof(proxy_stub_size) + compiled_proxy_stub->CodeDelta();
Elliott Hughesa0e18062012-04-13 15:59:59 -0700743 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter =
Logan Chien971bf3f2012-05-01 15:47:55 +0800744 code_offsets_.find(&proxy_stub);
745 if (stub_iter != code_offsets_.end() && offset != method_offsets.proxy_stub_offset_) {
746 DCHECK(stub_iter->second == method_offsets.proxy_stub_offset_) << PrettyMethod(method_idx, dex_file);
Logan Chienccb7bf12012-03-28 12:52:32 +0800747 } else {
Logan Chien971bf3f2012-05-01 15:47:55 +0800748 DCHECK(offset == method_offsets.proxy_stub_offset_) << PrettyMethod(method_idx, dex_file);
749 if (!file->WriteFully(&proxy_stub_size, sizeof(proxy_stub_size))) {
750 ReportWriteFailure("proxy stub code size", method_idx, dex_file, file);
Logan Chienccb7bf12012-03-28 12:52:32 +0800751 return 0;
752 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800753 code_offset += sizeof(proxy_stub_size);
Logan Chienccb7bf12012-03-28 12:52:32 +0800754 DCHECK_CODE_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800755 if (!file->WriteFully(&proxy_stub[0], proxy_stub_size)) {
756 ReportWriteFailure("proxy stub code", method_idx, dex_file, file);
Logan Chienccb7bf12012-03-28 12:52:32 +0800757 return 0;
758 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800759 code_offset += proxy_stub_size;
760 DCHECK_CODE_OFFSET();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700761 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700762 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700763 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700764 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800765#endif
Logan Chien8b977d32012-02-21 19:14:55 +0800766
Brian Carlstrome24fa612011-09-29 00:53:55 -0700767 return code_offset;
768}
769
Brian Carlstrome24fa612011-09-29 00:53:55 -0700770OatWriter::OatDexFile::OatDexFile(const DexFile& dex_file) {
Elliott Hughes95572412011-12-13 18:14:20 -0800771 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700772 dex_file_location_size_ = location.size();
773 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800774 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800775 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800776 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700777}
778
779size_t OatWriter::OatDexFile::SizeOf() const {
780 return sizeof(dex_file_location_size_)
781 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800782 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800783 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800784 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700785}
786
787void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
788 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
789 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800790 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800791 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800792 oat_header.UpdateChecksum(&methods_offsets_[0],
793 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700794}
795
796bool OatWriter::OatDexFile::Write(File* file) const {
797 if (!file->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700798 PLOG(ERROR) << "Failed to write dex file location length to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700799 return false;
800 }
801 if (!file->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700802 PLOG(ERROR) << "Failed to write dex file location data to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700803 return false;
804 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800805 if (!file->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
806 PLOG(ERROR) << "Failed to write dex file location checksum to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700807 return false;
808 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800809 if (!file->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
810 PLOG(ERROR) << "Failed to write dex file offset to " << file->name();
811 return false;
812 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800813 if (!file->WriteFully(&methods_offsets_[0],
814 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700815 PLOG(ERROR) << "Failed to write methods offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700816 return false;
817 }
818 return true;
819}
820
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800821OatWriter::OatClass::OatClass(Class::Status status, uint32_t methods_count) {
822 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700823 method_offsets_.resize(methods_count);
824}
825
Brian Carlstrom389efb02012-01-11 12:06:26 -0800826size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800827 return sizeof(status_)
828 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700829}
830
Brian Carlstrom389efb02012-01-11 12:06:26 -0800831void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800832 oat_header.UpdateChecksum(&status_, sizeof(status_));
833 oat_header.UpdateChecksum(&method_offsets_[0],
834 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700835}
836
Brian Carlstrom389efb02012-01-11 12:06:26 -0800837bool OatWriter::OatClass::Write(File* file) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800838 if (!file->WriteFully(&status_, sizeof(status_))) {
839 PLOG(ERROR) << "Failed to write class status to " << file->name();
840 return false;
841 }
842 if (!file->WriteFully(&method_offsets_[0],
843 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700844 PLOG(ERROR) << "Failed to write method offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700845 return false;
846 }
847 return true;
848}
849
Brian Carlstrome24fa612011-09-29 00:53:55 -0700850} // namespace art