blob: 4c32506d433e52f9784339373835e7922ecdcb35 [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
Elliott Hughes1aa246d2012-12-13 09:29:36 -080021#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080022#include "base/unix_file/fd_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070023#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070024#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070025#include "gc/space/space.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "mirror/abstract_method-inl.h"
27#include "mirror/array.h"
28#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070030#include "os.h"
Brian Carlstromcd60ac72013-01-20 17:09:51 -080031#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070032#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070033#include "scoped_thread_state_change.h"
jeffhaoec014232012-09-05 10:42:25 -070034#include "verifier/method_verifier.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070035
36namespace art {
37
Brian Carlstromcd60ac72013-01-20 17:09:51 -080038bool OatWriter::Create(OutputStream& output_stream,
jeffhao10037c82012-01-23 15:06:23 -080039 const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070040 uint32_t image_file_location_oat_checksum,
41 uint32_t image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070042 const std::string& image_file_location,
Ian Rogers1212a022013-03-04 10:48:41 -080043 const CompilerDriver& driver) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070044 OatWriter oat_writer(dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070045 image_file_location_oat_checksum,
46 image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070047 image_file_location,
Ian Rogers1212a022013-03-04 10:48:41 -080048 &driver);
Brian Carlstromcd60ac72013-01-20 17:09:51 -080049 return oat_writer.Write(output_stream);
Brian Carlstrome24fa612011-09-29 00:53:55 -070050}
51
Brian Carlstrom3320cf42011-10-04 14:58:28 -070052OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070053 uint32_t image_file_location_oat_checksum,
54 uint32_t image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070055 const std::string& image_file_location,
Ian Rogers1212a022013-03-04 10:48:41 -080056 const CompilerDriver* compiler)
Jeff Hao0aba0ba2013-06-03 14:49:28 -070057 : compiler_driver_(compiler),
58 dex_files_(&dex_files),
59 image_file_location_oat_checksum_(image_file_location_oat_checksum),
60 image_file_location_oat_begin_(image_file_location_oat_begin),
61 image_file_location_(image_file_location),
62 oat_header_(NULL),
63 size_dex_file_alignment_(0),
64 size_executable_offset_alignment_(0),
65 size_oat_header_(0),
66 size_oat_header_image_file_location_(0),
67 size_dex_file_(0),
68 size_interpreter_to_interpreter_entry_(0),
69 size_interpreter_to_quick_entry_(0),
70 size_portable_resolution_trampoline_(0),
71 size_quick_resolution_trampoline_(0),
72 size_stubs_alignment_(0),
73 size_code_size_(0),
74 size_code_(0),
75 size_code_alignment_(0),
76 size_mapping_table_(0),
77 size_vmap_table_(0),
78 size_gc_map_(0),
79 size_oat_dex_file_location_size_(0),
80 size_oat_dex_file_location_data_(0),
81 size_oat_dex_file_location_checksum_(0),
82 size_oat_dex_file_offset_(0),
83 size_oat_dex_file_methods_offsets_(0),
84 size_oat_class_status_(0),
85 size_oat_class_method_offsets_(0) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070086 size_t offset = InitOatHeader();
Brian Carlstrome24fa612011-09-29 00:53:55 -070087 offset = InitOatDexFiles(offset);
Brian Carlstrom89521892011-12-07 22:05:07 -080088 offset = InitDexFiles(offset);
Brian Carlstrom389efb02012-01-11 12:06:26 -080089 offset = InitOatClasses(offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -070090 offset = InitOatCode(offset);
91 offset = InitOatCodeDexFiles(offset);
92
93 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Jeff Hao0aba0ba2013-06-03 14:49:28 -070094 CHECK(image_file_location.empty() == compiler->IsImage());
Brian Carlstrome24fa612011-09-29 00:53:55 -070095}
96
Ian Rogers0571d352011-11-03 19:51:38 -070097OatWriter::~OatWriter() {
98 delete oat_header_;
99 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800100 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -0700101}
102
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700103size_t OatWriter::InitOatHeader() {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700104 // create the OatHeader
Ian Rogers1212a022013-03-04 10:48:41 -0800105 oat_header_ = new OatHeader(compiler_driver_->GetInstructionSet(),
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700106 dex_files_,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700107 image_file_location_oat_checksum_,
108 image_file_location_oat_begin_,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700109 image_file_location_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700110 size_t offset = sizeof(*oat_header_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700111 offset += image_file_location_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700112 return offset;
113}
114
115size_t OatWriter::InitOatDexFiles(size_t offset) {
116 // create the OatDexFiles
117 for (size_t i = 0; i != dex_files_->size(); ++i) {
118 const DexFile* dex_file = (*dex_files_)[i];
119 CHECK(dex_file != NULL);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800120 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700121 oat_dex_files_.push_back(oat_dex_file);
122 offset += oat_dex_file->SizeOf();
123 }
124 return offset;
125}
126
Brian Carlstrom89521892011-12-07 22:05:07 -0800127size_t OatWriter::InitDexFiles(size_t offset) {
128 // calculate the offsets within OatDexFiles to the DexFiles
129 for (size_t i = 0; i != dex_files_->size(); ++i) {
130 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700131 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800132 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700133 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800134
135 // set offset in OatDexFile to DexFile
136 oat_dex_files_[i]->dex_file_offset_ = offset;
137
138 const DexFile* dex_file = (*dex_files_)[i];
139 offset += dex_file->GetHeader().file_size_;
140 }
141 return offset;
142}
143
Brian Carlstrom389efb02012-01-11 12:06:26 -0800144size_t OatWriter::InitOatClasses(size_t offset) {
145 // create the OatClasses
146 // calculate the offsets within OatDexFiles to OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -0700147 for (size_t i = 0; i != dex_files_->size(); ++i) {
148 const DexFile* dex_file = (*dex_files_)[i];
149 for (size_t class_def_index = 0;
150 class_def_index < dex_file->NumClassDefs();
Ian Rogersc20a83e2012-01-18 18:15:32 -0800151 class_def_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800152 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700153 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
154 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700155 uint32_t num_methods = 0;
156 if (class_data != NULL) { // ie not an empty class, such as a marker interface
157 ClassDataItemIterator it(*dex_file, class_data);
158 size_t num_direct_methods = it.NumDirectMethods();
159 size_t num_virtual_methods = it.NumVirtualMethods();
160 num_methods = num_direct_methods + num_virtual_methods;
161 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800162
Brian Carlstrom51c24672013-07-11 16:00:56 -0700163 ClassReference class_ref(dex_file, class_def_index);
Ian Rogers1212a022013-03-04 10:48:41 -0800164 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800165 mirror::Class::Status status;
jeffhaoec014232012-09-05 10:42:25 -0700166 if (compiled_class != NULL) {
167 status = compiled_class->GetStatus();
168 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 status = mirror::Class::kStatusError;
jeffhaoec014232012-09-05 10:42:25 -0700170 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171 status = mirror::Class::kStatusNotReady;
jeffhaoec014232012-09-05 10:42:25 -0700172 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800173
Brian Carlstrom265091e2013-01-30 14:08:26 -0800174 OatClass* oat_class = new OatClass(offset, status, num_methods);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800175 oat_classes_.push_back(oat_class);
176 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700177 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800178 oat_dex_files_[i]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700179 }
180 return offset;
181}
182
183size_t OatWriter::InitOatCode(size_t offset) {
184 // calculate the offsets within OatHeader to executable code
185 size_t old_offset = offset;
186 // required to be on a new page boundary
187 offset = RoundUp(offset, kPageSize);
188 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700189 size_executable_offset_alignment_ = offset - old_offset;
190 if (compiler_driver_->IsImage()) {
191 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
192 oat_header_->SetInterpreterToInterpreterEntryOffset(offset);
193 interpreter_to_interpreter_entry_.reset(compiler_driver_->CreateInterpreterToInterpreterEntry());
194 offset += interpreter_to_interpreter_entry_->size();
195
196 offset = CompiledCode::AlignCode(offset, instruction_set);
197 oat_header_->SetInterpreterToQuickEntryOffset(offset);
198 interpreter_to_quick_entry_.reset(compiler_driver_->CreateInterpreterToQuickEntry());
199 offset += interpreter_to_quick_entry_->size();
200
201 offset = CompiledCode::AlignCode(offset, instruction_set);
202 oat_header_->SetPortableResolutionTrampolineOffset(offset);
203 portable_resolution_trampoline_.reset(compiler_driver_->CreatePortableResolutionTrampoline());
204 offset += portable_resolution_trampoline_->size();
205
206 offset = CompiledCode::AlignCode(offset, instruction_set);
207 oat_header_->SetQuickResolutionTrampolineOffset(offset);
208 quick_resolution_trampoline_.reset(compiler_driver_->CreateQuickResolutionTrampoline());
209 offset += quick_resolution_trampoline_->size();
210 } else {
211 oat_header_->SetInterpreterToInterpreterEntryOffset(0);
212 oat_header_->SetInterpreterToQuickEntryOffset(0);
213 oat_header_->SetPortableResolutionTrampolineOffset(0);
214 oat_header_->SetQuickResolutionTrampolineOffset(0);
215 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700216 return offset;
217}
218
219size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700220 size_t oat_class_index = 0;
221 for (size_t i = 0; i != dex_files_->size(); ++i) {
222 const DexFile* dex_file = (*dex_files_)[i];
223 CHECK(dex_file != NULL);
224 offset = InitOatCodeDexFile(offset, oat_class_index, *dex_file);
225 }
226 return offset;
227}
228
229size_t OatWriter::InitOatCodeDexFile(size_t offset,
230 size_t& oat_class_index,
231 const DexFile& dex_file) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800232 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700233 class_def_index < dex_file.NumClassDefs();
234 class_def_index++, oat_class_index++) {
235 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800236 offset = InitOatCodeClassDef(offset, oat_class_index, class_def_index, dex_file, class_def);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800237 oat_classes_[oat_class_index]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700238 }
239 return offset;
240}
241
242size_t OatWriter::InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800243 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700244 const DexFile& dex_file,
245 const DexFile::ClassDef& class_def) {
246 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700247 if (class_data == NULL) {
248 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700249 return offset;
250 }
Ian Rogers0571d352011-11-03 19:51:38 -0700251 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800252 CHECK_EQ(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700253 it.NumDirectMethods() + it.NumVirtualMethods());
254 // Skip fields
255 while (it.HasNextStaticField()) {
256 it.Next();
257 }
258 while (it.HasNextInstanceField()) {
259 it.Next();
260 }
261 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700262 size_t class_def_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700263 while (it.HasNextDirectMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800264 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700265 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
266 is_native, it.GetMethodInvokeType(class_def), it.GetMemberIndex(),
267 &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700268 class_def_method_index++;
269 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700270 }
Ian Rogers0571d352011-11-03 19:51:38 -0700271 while (it.HasNextVirtualMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800272 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700273 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
274 is_native, it.GetMethodInvokeType(class_def), it.GetMemberIndex(),
275 &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700276 class_def_method_index++;
277 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700278 }
Ian Rogers0571d352011-11-03 19:51:38 -0700279 DCHECK(!it.HasNext());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700280 return offset;
281}
282
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700283size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
284 size_t __attribute__((unused)) class_def_index,
285 size_t class_def_method_index,
286 bool __attribute__((unused)) is_native,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800287 InvokeType invoke_type,
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700288 uint32_t method_idx, const DexFile* dex_file) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700289 // derived from CompiledMethod if available
290 uint32_t code_offset = 0;
291 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700292 uint32_t core_spill_mask = 0;
293 uint32_t fp_spill_mask = 0;
294 uint32_t mapping_table_offset = 0;
295 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800296 uint32_t gc_map_offset = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700297
Brian Carlstrom265091e2013-01-30 14:08:26 -0800298 OatClass* oat_class = oat_classes_[oat_class_index];
299#if defined(ART_USE_PORTABLE_COMPILER)
300 size_t oat_method_offsets_offset =
301 oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index);
302#endif
303
Ian Rogers0571d352011-11-03 19:51:38 -0700304 CompiledMethod* compiled_method =
Brian Carlstrom51c24672013-07-11 16:00:56 -0700305 compiler_driver_->GetCompiledMethod(MethodReference(dex_file, method_idx));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700306 if (compiled_method != NULL) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800307#if defined(ART_USE_PORTABLE_COMPILER)
308 compiled_method->AddOatdataOffsetToCompliledCodeOffset(
309 oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_));
310#else
311 const std::vector<uint8_t>& code = compiled_method->GetCode();
Logan Chien971bf3f2012-05-01 15:47:55 +0800312 offset = compiled_method->AlignCode(offset);
313 DCHECK_ALIGNED(offset, kArmAlignment);
Logan Chien971bf3f2012-05-01 15:47:55 +0800314 uint32_t code_size = code.size() * sizeof(code[0]);
315 CHECK_NE(code_size, 0U);
316 uint32_t thumb_offset = compiled_method->CodeDelta();
317 code_offset = offset + sizeof(code_size) + thumb_offset;
318
319 // Deduplicate code arrays
320 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
321 if (code_iter != code_offsets_.end()) {
322 code_offset = code_iter->second;
jeffhao55d78212011-11-02 11:41:50 -0700323 } else {
Logan Chien971bf3f2012-05-01 15:47:55 +0800324 code_offsets_.Put(&code, code_offset);
325 offset += sizeof(code_size); // code size is prepended before code
326 offset += code_size;
327 oat_header_->UpdateChecksum(&code[0], code_size);
328 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800329#endif
Logan Chien971bf3f2012-05-01 15:47:55 +0800330 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
331 core_spill_mask = compiled_method->GetCoreSpillMask();
332 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700333
Logan Chien971bf3f2012-05-01 15:47:55 +0800334 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
335 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
336 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700337
Logan Chien971bf3f2012-05-01 15:47:55 +0800338 // Deduplicate mapping tables
339 SafeMap<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
340 if (mapping_iter != mapping_table_offsets_.end()) {
341 mapping_table_offset = mapping_iter->second;
342 } else {
343 mapping_table_offsets_.Put(&mapping_table, mapping_table_offset);
344 offset += mapping_table_size;
345 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
346 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700347
Logan Chien971bf3f2012-05-01 15:47:55 +0800348 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
349 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
350 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700351
Logan Chien971bf3f2012-05-01 15:47:55 +0800352 // Deduplicate vmap tables
353 SafeMap<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
354 if (vmap_iter != vmap_table_offsets_.end()) {
355 vmap_table_offset = vmap_iter->second;
356 } else {
357 vmap_table_offsets_.Put(&vmap_table, vmap_table_offset);
358 offset += vmap_table_size;
359 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
360 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800361
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700362 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800363 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
364 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
Logan Chienccb7bf12012-03-28 12:52:32 +0800365
TDYa127ce4cc0d2012-11-18 16:59:53 -0800366#if !defined(NDEBUG)
Logan Chien971bf3f2012-05-01 15:47:55 +0800367 // We expect GC maps except when the class hasn't been verified or the method is native
Brian Carlstrom51c24672013-07-11 16:00:56 -0700368 ClassReference class_ref(dex_file, class_def_index);
Ian Rogers1212a022013-03-04 10:48:41 -0800369 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800370 mirror::Class::Status status;
jeffhaoec014232012-09-05 10:42:25 -0700371 if (compiled_class != NULL) {
372 status = compiled_class->GetStatus();
373 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800374 status = mirror::Class::kStatusError;
jeffhaoec014232012-09-05 10:42:25 -0700375 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800376 status = mirror::Class::kStatusNotReady;
jeffhaoec014232012-09-05 10:42:25 -0700377 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800378 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
379 << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
380 << (status < mirror::Class::kStatusVerified) << " " << status << " "
381 << PrettyMethod(method_idx, *dex_file);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800382#endif
383
Logan Chien971bf3f2012-05-01 15:47:55 +0800384 // Deduplicate GC maps
385 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = gc_map_offsets_.find(&gc_map);
386 if (gc_map_iter != gc_map_offsets_.end()) {
387 gc_map_offset = gc_map_iter->second;
388 } else {
389 gc_map_offsets_.Put(&gc_map, gc_map_offset);
390 offset += gc_map_size;
391 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800392 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700393 }
394
Brian Carlstrom265091e2013-01-30 14:08:26 -0800395 oat_class->method_offsets_[class_def_method_index]
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700396 = OatMethodOffsets(code_offset,
397 frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700398 core_spill_mask,
399 fp_spill_mask,
400 mapping_table_offset,
401 vmap_table_offset,
Jeff Hao74180ca2013-03-27 15:29:11 -0700402 gc_map_offset
Logan Chienccb7bf12012-03-28 12:52:32 +0800403 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700404
Ian Rogers1212a022013-03-04 10:48:41 -0800405 if (compiler_driver_->IsImage()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700406 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800407 mirror::DexCache* dex_cache = linker->FindDexCache(*dex_file);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700408 // Unchecked as we hold mutator_lock_ on entry.
409 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800410 mirror::AbstractMethod* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800411 NULL, NULL, invoke_type);
Ian Rogers0571d352011-11-03 19:51:38 -0700412 CHECK(method != NULL);
413 method->SetFrameSizeInBytes(frame_size_in_bytes);
414 method->SetCoreSpillMask(core_spill_mask);
415 method->SetFpSpillMask(fp_spill_mask);
416 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800417 // Don't overwrite static method trampoline
418 if (!method->IsStatic() || method->IsConstructor() ||
419 method->GetDeclaringClass()->IsInitialized()) {
420 method->SetOatCodeOffset(code_offset);
421 } else {
Jeff Haoaa4a7932013-05-13 11:28:27 -0700422 method->SetEntryPointFromCompiledCode(NULL);
Ian Rogers19846512012-02-24 11:42:47 -0800423 }
Ian Rogers0571d352011-11-03 19:51:38 -0700424 method->SetOatVmapTableOffset(vmap_table_offset);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700425 method->SetOatNativeGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700426 }
Logan Chien8b977d32012-02-21 19:14:55 +0800427
Brian Carlstrome24fa612011-09-29 00:53:55 -0700428 return offset;
429}
430
Brian Carlstrom265091e2013-01-30 14:08:26 -0800431#define DCHECK_OFFSET() \
432 DCHECK_EQ(static_cast<off_t>(offset), out.Seek(0, kSeekCurrent))
433
434#define DCHECK_OFFSET_() \
435 DCHECK_EQ(static_cast<off_t>(offset_), out.Seek(0, kSeekCurrent))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700436
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800437bool OatWriter::Write(OutputStream& out) {
438 if (!out.WriteFully(oat_header_, sizeof(*oat_header_))) {
439 PLOG(ERROR) << "Failed to write oat header to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700440 return false;
441 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700442 size_oat_header_ += sizeof(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700443
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800444 if (!out.WriteFully(image_file_location_.data(), image_file_location_.size())) {
445 PLOG(ERROR) << "Failed to write oat header image file location to " << out.GetLocation();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700446 return false;
447 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700448 size_oat_header_image_file_location_ += image_file_location_.size();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700449
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800450 if (!WriteTables(out)) {
451 LOG(ERROR) << "Failed to write oat tables to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700452 return false;
453 }
454
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800455 size_t code_offset = WriteCode(out);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700456 if (code_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800457 LOG(ERROR) << "Failed to write oat code to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700458 return false;
459 }
460
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800461 code_offset = WriteCodeDexFiles(out, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700462 if (code_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800463 LOG(ERROR) << "Failed to write oat code for dex files to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700464 return false;
465 }
466
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700467 if (kIsDebugBuild) {
468 uint32_t size_total = 0;
469 #define DO_STAT(x) \
470 LOG(INFO) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
471 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700472
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700473 DO_STAT(size_dex_file_alignment_);
474 DO_STAT(size_executable_offset_alignment_);
475 DO_STAT(size_oat_header_);
476 DO_STAT(size_oat_header_image_file_location_);
477 DO_STAT(size_dex_file_);
478 DO_STAT(size_interpreter_to_interpreter_entry_);
479 DO_STAT(size_interpreter_to_quick_entry_);
480 DO_STAT(size_portable_resolution_trampoline_);
481 DO_STAT(size_quick_resolution_trampoline_);
482 DO_STAT(size_stubs_alignment_);
483 DO_STAT(size_code_size_);
484 DO_STAT(size_code_);
485 DO_STAT(size_code_alignment_);
486 DO_STAT(size_mapping_table_);
487 DO_STAT(size_vmap_table_);
488 DO_STAT(size_gc_map_);
489 DO_STAT(size_oat_dex_file_location_size_);
490 DO_STAT(size_oat_dex_file_location_data_);
491 DO_STAT(size_oat_dex_file_location_checksum_);
492 DO_STAT(size_oat_dex_file_offset_);
493 DO_STAT(size_oat_dex_file_methods_offsets_);
494 DO_STAT(size_oat_class_status_);
495 DO_STAT(size_oat_class_method_offsets_);
496 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700497
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700498 LOG(INFO) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
499 CHECK_EQ(size_total, static_cast<uint32_t>(out.Seek(0, kSeekCurrent)));
500 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700501
Brian Carlstrome24fa612011-09-29 00:53:55 -0700502 return true;
503}
504
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800505bool OatWriter::WriteTables(OutputStream& out) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700506 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700507 if (!oat_dex_files_[i]->Write(this, out)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800508 PLOG(ERROR) << "Failed to write oat dex information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700509 return false;
510 }
511 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800512 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
513 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800514 off_t actual_offset = out.Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -0800515 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
516 const DexFile* dex_file = (*dex_files_)[i];
517 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
518 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
519 return false;
520 }
521 const DexFile* dex_file = (*dex_files_)[i];
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800522 if (!out.WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
523 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800524 return false;
525 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700526 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800527 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800528 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700529 if (!oat_classes_[i]->Write(this, out)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800530 PLOG(ERROR) << "Failed to write oat methods information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700531 return false;
532 }
533 }
534 return true;
535}
536
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800537size_t OatWriter::WriteCode(OutputStream& out) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800538 uint32_t offset = oat_header_->GetExecutableOffset();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700539 off_t new_offset = out.Seek(size_executable_offset_alignment_, kSeekCurrent);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800540 if (static_cast<uint32_t>(new_offset) != offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700541 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Brian Carlstrom265091e2013-01-30 14:08:26 -0800542 << " Expected: " << offset << " File: " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700543 return 0;
544 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800545 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700546 if (compiler_driver_->IsImage()) {
547 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
548 if (!out.WriteFully(&(*interpreter_to_interpreter_entry_)[0], interpreter_to_interpreter_entry_->size())) {
549 PLOG(ERROR) << "Failed to write interpreter to interpreter entry to " << out.GetLocation();
550 return false;
551 }
552 size_interpreter_to_interpreter_entry_ += interpreter_to_interpreter_entry_->size();
553 offset += interpreter_to_interpreter_entry_->size();
554 DCHECK_OFFSET();
555
556 uint32_t aligned_offset = CompiledCode::AlignCode(offset, instruction_set);
557 uint32_t alignment_padding = aligned_offset - offset;
558 out.Seek(alignment_padding, kSeekCurrent);
559 size_stubs_alignment_ += alignment_padding;
560 if (!out.WriteFully(&(*interpreter_to_quick_entry_)[0], interpreter_to_quick_entry_->size())) {
561 PLOG(ERROR) << "Failed to write interpreter to quick entry to " << out.GetLocation();
562 return false;
563 }
564 size_interpreter_to_quick_entry_ += interpreter_to_quick_entry_->size();
565 offset += alignment_padding + interpreter_to_quick_entry_->size();
566 DCHECK_OFFSET();
567
568 aligned_offset = CompiledCode::AlignCode(offset, instruction_set);
569 alignment_padding = aligned_offset - offset;
570 out.Seek(alignment_padding, kSeekCurrent);
571 size_stubs_alignment_ += alignment_padding;
572 if (!out.WriteFully(&(*portable_resolution_trampoline_)[0], portable_resolution_trampoline_->size())) {
573 PLOG(ERROR) << "Failed to write portable resolution trampoline to " << out.GetLocation();
574 return false;
575 }
576 size_portable_resolution_trampoline_ += portable_resolution_trampoline_->size();
577 offset += alignment_padding + portable_resolution_trampoline_->size();
578 DCHECK_OFFSET();
579
580 aligned_offset = CompiledCode::AlignCode(offset, instruction_set);
581 alignment_padding = aligned_offset - offset;
582 out.Seek(alignment_padding, kSeekCurrent);
583 size_stubs_alignment_ += alignment_padding;
584 if (!out.WriteFully(&(*quick_resolution_trampoline_)[0], quick_resolution_trampoline_->size())) {
585 PLOG(ERROR) << "Failed to write quick resolution trampoline to " << out.GetLocation();
586 return false;
587 }
588 size_quick_resolution_trampoline_ += quick_resolution_trampoline_->size();
589 offset += alignment_padding + quick_resolution_trampoline_->size();
590 DCHECK_OFFSET();
591 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800592 return offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700593}
594
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800595size_t OatWriter::WriteCodeDexFiles(OutputStream& out, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700596 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800597 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700598 const DexFile* dex_file = (*dex_files_)[i];
599 CHECK(dex_file != NULL);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800600 code_offset = WriteCodeDexFile(out, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700601 if (code_offset == 0) {
602 return 0;
603 }
604 }
605 return code_offset;
606}
607
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800608size_t OatWriter::WriteCodeDexFile(OutputStream& out, size_t code_offset, size_t& oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700609 const DexFile& dex_file) {
610 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
611 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700612 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800613 code_offset = WriteCodeClassDef(out, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700614 if (code_offset == 0) {
615 return 0;
616 }
617 }
618 return code_offset;
619}
620
Ian Rogers0571d352011-11-03 19:51:38 -0700621void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800622 const DexFile& dex_file, OutputStream& out) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700623 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800624 << " to " << out.GetLocation();
Elliott Hughes234da572011-11-03 22:13:06 -0700625}
626
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800627size_t OatWriter::WriteCodeClassDef(OutputStream& out,
Ian Rogers0571d352011-11-03 19:51:38 -0700628 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700629 const DexFile& dex_file,
630 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700631 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700632 if (class_data == NULL) {
633 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700634 return code_offset;
635 }
Ian Rogers0571d352011-11-03 19:51:38 -0700636 ClassDataItemIterator it(dex_file, class_data);
637 // Skip fields
638 while (it.HasNextStaticField()) {
639 it.Next();
640 }
641 while (it.HasNextInstanceField()) {
642 it.Next();
643 }
644 // Process methods
645 size_t class_def_method_index = 0;
646 while (it.HasNextDirectMethod()) {
647 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800648 code_offset = WriteCodeMethod(out, code_offset, oat_class_index, class_def_method_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700649 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700650 if (code_offset == 0) {
651 return 0;
652 }
Ian Rogers0571d352011-11-03 19:51:38 -0700653 class_def_method_index++;
654 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700655 }
Ian Rogers0571d352011-11-03 19:51:38 -0700656 while (it.HasNextVirtualMethod()) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800657 code_offset = WriteCodeMethod(out, code_offset, oat_class_index, class_def_method_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700658 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700659 if (code_offset == 0) {
660 return 0;
661 }
Ian Rogers0571d352011-11-03 19:51:38 -0700662 class_def_method_index++;
663 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700664 }
665 return code_offset;
666}
667
Brian Carlstrom265091e2013-01-30 14:08:26 -0800668size_t OatWriter::WriteCodeMethod(OutputStream& out, size_t offset, size_t oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700669 size_t class_def_method_index, bool is_static,
670 uint32_t method_idx, const DexFile& dex_file) {
671 const CompiledMethod* compiled_method =
Brian Carlstrom51c24672013-07-11 16:00:56 -0700672 compiler_driver_->GetCompiledMethod(MethodReference(&dex_file, method_idx));
Ian Rogers0571d352011-11-03 19:51:38 -0700673
Ian Rogers0571d352011-11-03 19:51:38 -0700674 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800675 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700676
677
678 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstrom265091e2013-01-30 14:08:26 -0800679#if !defined(ART_USE_PORTABLE_COMPILER)
680 uint32_t aligned_offset = compiled_method->AlignCode(offset);
681 uint32_t aligned_code_delta = aligned_offset - offset;
Logan Chien971bf3f2012-05-01 15:47:55 +0800682 if (aligned_code_delta != 0) {
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800683 off_t new_offset = out.Seek(aligned_code_delta, kSeekCurrent);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700684 size_code_alignment_ += aligned_code_delta;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800685 if (static_cast<uint32_t>(new_offset) != aligned_offset) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800686 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Brian Carlstrom265091e2013-01-30 14:08:26 -0800687 << " Expected: " << aligned_offset << " File: " << out.GetLocation();
Logan Chien971bf3f2012-05-01 15:47:55 +0800688 return 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700689 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800690 offset += aligned_code_delta;
691 DCHECK_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700692 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800693 DCHECK_ALIGNED(offset, kArmAlignment);
Logan Chien971bf3f2012-05-01 15:47:55 +0800694 const std::vector<uint8_t>& code = compiled_method->GetCode();
695 uint32_t code_size = code.size() * sizeof(code[0]);
696 CHECK_NE(code_size, 0U);
697
698 // Deduplicate code arrays
Brian Carlstrom265091e2013-01-30 14:08:26 -0800699 size_t code_offset = offset + sizeof(code_size) + compiled_method->CodeDelta();
Logan Chien971bf3f2012-05-01 15:47:55 +0800700 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800701 if (code_iter != code_offsets_.end() && code_offset != method_offsets.code_offset_) {
702 DCHECK(code_iter->second == method_offsets.code_offset_)
703 << PrettyMethod(method_idx, dex_file);
Logan Chien971bf3f2012-05-01 15:47:55 +0800704 } else {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800705 DCHECK(code_offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800706 if (!out.WriteFully(&code_size, sizeof(code_size))) {
707 ReportWriteFailure("method code size", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800708 return 0;
709 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700710 size_code_size_ += sizeof(code_size);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800711 offset += sizeof(code_size);
712 DCHECK_OFFSET();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800713 if (!out.WriteFully(&code[0], code_size)) {
714 ReportWriteFailure("method code", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800715 return 0;
716 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700717 size_code_ += code_size;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800718 offset += code_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800719 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800720 DCHECK_OFFSET();
721#endif
Logan Chien971bf3f2012-05-01 15:47:55 +0800722
723 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
724 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
725
726 // Deduplicate mapping tables
727 SafeMap<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
728 mapping_table_offsets_.find(&mapping_table);
729 if (mapping_iter != mapping_table_offsets_.end() &&
Brian Carlstrom265091e2013-01-30 14:08:26 -0800730 offset != method_offsets.mapping_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800731 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
732 || mapping_iter->second == method_offsets.mapping_table_offset_)
733 << PrettyMethod(method_idx, dex_file);
734 } else {
735 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800736 || offset == method_offsets.mapping_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800737 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800738 if (!out.WriteFully(&mapping_table[0], mapping_table_size)) {
739 ReportWriteFailure("mapping table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800740 return 0;
741 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700742 size_mapping_table_ += mapping_table_size;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800743 offset += mapping_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800744 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800745 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800746
747 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
748 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
749
750 // Deduplicate vmap tables
751 SafeMap<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
752 vmap_table_offsets_.find(&vmap_table);
753 if (vmap_iter != vmap_table_offsets_.end() &&
Brian Carlstrom265091e2013-01-30 14:08:26 -0800754 offset != method_offsets.vmap_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800755 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
756 || vmap_iter->second == method_offsets.vmap_table_offset_)
757 << PrettyMethod(method_idx, dex_file);
758 } else {
759 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800760 || offset == method_offsets.vmap_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800761 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800762 if (!out.WriteFully(&vmap_table[0], vmap_table_size)) {
763 ReportWriteFailure("vmap table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800764 return 0;
765 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700766 size_vmap_table_ += vmap_table_size;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800767 offset += vmap_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800768 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800769 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800770
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700771 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800772 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
773
774 // Deduplicate GC maps
775 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
776 gc_map_offsets_.find(&gc_map);
777 if (gc_map_iter != gc_map_offsets_.end() &&
Brian Carlstrom265091e2013-01-30 14:08:26 -0800778 offset != method_offsets.gc_map_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800779 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
780 || gc_map_iter->second == method_offsets.gc_map_offset_)
781 << PrettyMethod(method_idx, dex_file);
782 } else {
783 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800784 || offset == method_offsets.gc_map_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800785 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800786 if (!out.WriteFully(&gc_map[0], gc_map_size)) {
787 ReportWriteFailure("GC map", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800788 return 0;
789 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700790 size_gc_map_ += gc_map_size;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800791 offset += gc_map_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800792 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800793 DCHECK_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700794 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800795
Brian Carlstrom265091e2013-01-30 14:08:26 -0800796 return offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700797}
798
Brian Carlstrom265091e2013-01-30 14:08:26 -0800799OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
800 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -0800801 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700802 dex_file_location_size_ = location.size();
803 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800804 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800805 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800806 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700807}
808
809size_t OatWriter::OatDexFile::SizeOf() const {
810 return sizeof(dex_file_location_size_)
811 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800812 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800813 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800814 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700815}
816
817void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
818 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
819 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800820 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800821 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800822 oat_header.UpdateChecksum(&methods_offsets_[0],
823 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700824}
825
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700826bool OatWriter::OatDexFile::Write(OatWriter* oat_writer, OutputStream& out) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800827 DCHECK_OFFSET_();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800828 if (!out.WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
829 PLOG(ERROR) << "Failed to write dex file location length to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700830 return false;
831 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700832 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800833 if (!out.WriteFully(dex_file_location_data_, dex_file_location_size_)) {
834 PLOG(ERROR) << "Failed to write dex file location data to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700835 return false;
836 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700837 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800838 if (!out.WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
839 PLOG(ERROR) << "Failed to write dex file location checksum to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700840 return false;
841 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700842 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800843 if (!out.WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
844 PLOG(ERROR) << "Failed to write dex file offset to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800845 return false;
846 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700847 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800848 if (!out.WriteFully(&methods_offsets_[0],
849 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
850 PLOG(ERROR) << "Failed to write methods offsets to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700851 return false;
852 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700853 oat_writer->size_oat_dex_file_methods_offsets_ +=
854 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700855 return true;
856}
857
Brian Carlstrom265091e2013-01-30 14:08:26 -0800858OatWriter::OatClass::OatClass(size_t offset, mirror::Class::Status status, uint32_t methods_count) {
859 offset_ = offset;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800860 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700861 method_offsets_.resize(methods_count);
862}
863
Brian Carlstrom265091e2013-01-30 14:08:26 -0800864size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
865 size_t class_def_method_index_) const {
866 return offset_ + GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
867}
868
869size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
870 size_t class_def_method_index_) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800871 return sizeof(status_)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800872 + (sizeof(method_offsets_[0]) * class_def_method_index_);
873}
874
875size_t OatWriter::OatClass::SizeOf() const {
876 return GetOatMethodOffsetsOffsetFromOatClass(method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700877}
878
Brian Carlstrom389efb02012-01-11 12:06:26 -0800879void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800880 oat_header.UpdateChecksum(&status_, sizeof(status_));
881 oat_header.UpdateChecksum(&method_offsets_[0],
882 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700883}
884
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700885bool OatWriter::OatClass::Write(OatWriter* oat_writer, OutputStream& out) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800886 DCHECK_OFFSET_();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800887 if (!out.WriteFully(&status_, sizeof(status_))) {
888 PLOG(ERROR) << "Failed to write class status to " << out.GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800889 return false;
890 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700891 oat_writer->size_oat_class_status_ += sizeof(status_);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800892 DCHECK_EQ(static_cast<off_t>(GetOatMethodOffsetsOffsetFromOatHeader(0)),
893 out.Seek(0, kSeekCurrent));
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800894 if (!out.WriteFully(&method_offsets_[0],
895 sizeof(method_offsets_[0]) * method_offsets_.size())) {
896 PLOG(ERROR) << "Failed to write method offsets to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700897 return false;
898 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700899 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrom265091e2013-01-30 14:08:26 -0800900 DCHECK_EQ(static_cast<off_t>(GetOatMethodOffsetsOffsetFromOatHeader(method_offsets_.size())),
901 out.Seek(0, kSeekCurrent));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700902 return true;
903}
904
Brian Carlstrome24fa612011-09-29 00:53:55 -0700905} // namespace art