blob: 639d249404da09558c2941f63537abe37fcc1934 [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 Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/abstract_method-inl.h"
26#include "mirror/array.h"
27#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070029#include "os.h"
Brian Carlstromcd60ac72013-01-20 17:09:51 -080030#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070031#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070032#include "scoped_thread_state_change.h"
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070033#include "gc/space.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 Carlstrome24fa612011-09-29 00:53:55 -070086
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070087 size_t offset = InitOatHeader();
Brian Carlstrome24fa612011-09-29 00:53:55 -070088 offset = InitOatDexFiles(offset);
Brian Carlstrom89521892011-12-07 22:05:07 -080089 offset = InitDexFiles(offset);
Brian Carlstrom389efb02012-01-11 12:06:26 -080090 offset = InitOatClasses(offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -070091 offset = InitOatCode(offset);
92 offset = InitOatCodeDexFiles(offset);
93
94 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Jeff Hao0aba0ba2013-06-03 14:49:28 -070095 CHECK(image_file_location.empty() == compiler->IsImage());
Brian Carlstrome24fa612011-09-29 00:53:55 -070096}
97
Ian Rogers0571d352011-11-03 19:51:38 -070098OatWriter::~OatWriter() {
99 delete oat_header_;
100 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800101 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -0700102}
103
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700104size_t OatWriter::InitOatHeader() {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700105 // create the OatHeader
Ian Rogers1212a022013-03-04 10:48:41 -0800106 oat_header_ = new OatHeader(compiler_driver_->GetInstructionSet(),
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700107 dex_files_,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700108 image_file_location_oat_checksum_,
109 image_file_location_oat_begin_,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700110 image_file_location_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700111 size_t offset = sizeof(*oat_header_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700112 offset += image_file_location_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700113 return offset;
114}
115
116size_t OatWriter::InitOatDexFiles(size_t offset) {
117 // create the OatDexFiles
118 for (size_t i = 0; i != dex_files_->size(); ++i) {
119 const DexFile* dex_file = (*dex_files_)[i];
120 CHECK(dex_file != NULL);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800121 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700122 oat_dex_files_.push_back(oat_dex_file);
123 offset += oat_dex_file->SizeOf();
124 }
125 return offset;
126}
127
Brian Carlstrom89521892011-12-07 22:05:07 -0800128size_t OatWriter::InitDexFiles(size_t offset) {
129 // calculate the offsets within OatDexFiles to the DexFiles
130 for (size_t i = 0; i != dex_files_->size(); ++i) {
131 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700132 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800133 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700134 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800135
136 // set offset in OatDexFile to DexFile
137 oat_dex_files_[i]->dex_file_offset_ = offset;
138
139 const DexFile* dex_file = (*dex_files_)[i];
140 offset += dex_file->GetHeader().file_size_;
141 }
142 return offset;
143}
144
Brian Carlstrom389efb02012-01-11 12:06:26 -0800145size_t OatWriter::InitOatClasses(size_t offset) {
146 // create the OatClasses
147 // calculate the offsets within OatDexFiles to OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -0700148 for (size_t i = 0; i != dex_files_->size(); ++i) {
149 const DexFile* dex_file = (*dex_files_)[i];
150 for (size_t class_def_index = 0;
151 class_def_index < dex_file->NumClassDefs();
Ian Rogersc20a83e2012-01-18 18:15:32 -0800152 class_def_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800153 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700154 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
155 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700156 uint32_t num_methods = 0;
157 if (class_data != NULL) { // ie not an empty class, such as a marker interface
158 ClassDataItemIterator it(*dex_file, class_data);
159 size_t num_direct_methods = it.NumDirectMethods();
160 size_t num_virtual_methods = it.NumVirtualMethods();
161 num_methods = num_direct_methods + num_virtual_methods;
162 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800163
Ian Rogers1212a022013-03-04 10:48:41 -0800164 CompilerDriver::ClassReference class_ref = CompilerDriver::ClassReference(dex_file, class_def_index);
165 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800166 mirror::Class::Status status;
jeffhaoec014232012-09-05 10:42:25 -0700167 if (compiled_class != NULL) {
168 status = compiled_class->GetStatus();
169 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170 status = mirror::Class::kStatusError;
jeffhaoec014232012-09-05 10:42:25 -0700171 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800172 status = mirror::Class::kStatusNotReady;
jeffhaoec014232012-09-05 10:42:25 -0700173 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800174
Brian Carlstrom265091e2013-01-30 14:08:26 -0800175 OatClass* oat_class = new OatClass(offset, status, num_methods);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800176 oat_classes_.push_back(oat_class);
177 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700178 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800179 oat_dex_files_[i]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700180 }
181 return offset;
182}
183
184size_t OatWriter::InitOatCode(size_t offset) {
185 // calculate the offsets within OatHeader to executable code
186 size_t old_offset = offset;
187 // required to be on a new page boundary
188 offset = RoundUp(offset, kPageSize);
189 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700190 size_executable_offset_alignment_ = offset - old_offset;
191 if (compiler_driver_->IsImage()) {
192 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
193 oat_header_->SetInterpreterToInterpreterEntryOffset(offset);
194 interpreter_to_interpreter_entry_.reset(compiler_driver_->CreateInterpreterToInterpreterEntry());
195 offset += interpreter_to_interpreter_entry_->size();
196
197 offset = CompiledCode::AlignCode(offset, instruction_set);
198 oat_header_->SetInterpreterToQuickEntryOffset(offset);
199 interpreter_to_quick_entry_.reset(compiler_driver_->CreateInterpreterToQuickEntry());
200 offset += interpreter_to_quick_entry_->size();
201
202 offset = CompiledCode::AlignCode(offset, instruction_set);
203 oat_header_->SetPortableResolutionTrampolineOffset(offset);
204 portable_resolution_trampoline_.reset(compiler_driver_->CreatePortableResolutionTrampoline());
205 offset += portable_resolution_trampoline_->size();
206
207 offset = CompiledCode::AlignCode(offset, instruction_set);
208 oat_header_->SetQuickResolutionTrampolineOffset(offset);
209 quick_resolution_trampoline_.reset(compiler_driver_->CreateQuickResolutionTrampoline());
210 offset += quick_resolution_trampoline_->size();
211 } else {
212 oat_header_->SetInterpreterToInterpreterEntryOffset(0);
213 oat_header_->SetInterpreterToQuickEntryOffset(0);
214 oat_header_->SetPortableResolutionTrampolineOffset(0);
215 oat_header_->SetQuickResolutionTrampolineOffset(0);
216 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700217 return offset;
218}
219
220size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700221 size_t oat_class_index = 0;
222 for (size_t i = 0; i != dex_files_->size(); ++i) {
223 const DexFile* dex_file = (*dex_files_)[i];
224 CHECK(dex_file != NULL);
225 offset = InitOatCodeDexFile(offset, oat_class_index, *dex_file);
226 }
227 return offset;
228}
229
230size_t OatWriter::InitOatCodeDexFile(size_t offset,
231 size_t& oat_class_index,
232 const DexFile& dex_file) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800233 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700234 class_def_index < dex_file.NumClassDefs();
235 class_def_index++, oat_class_index++) {
236 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800237 offset = InitOatCodeClassDef(offset, oat_class_index, class_def_index, dex_file, class_def);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800238 oat_classes_[oat_class_index]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700239 }
240 return offset;
241}
242
243size_t OatWriter::InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800244 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700245 const DexFile& dex_file,
246 const DexFile::ClassDef& class_def) {
247 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700248 if (class_data == NULL) {
249 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700250 return offset;
251 }
Ian Rogers0571d352011-11-03 19:51:38 -0700252 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800253 CHECK_EQ(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700254 it.NumDirectMethods() + it.NumVirtualMethods());
255 // Skip fields
256 while (it.HasNextStaticField()) {
257 it.Next();
258 }
259 while (it.HasNextInstanceField()) {
260 it.Next();
261 }
262 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700263 size_t class_def_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700264 while (it.HasNextDirectMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800265 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700266 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
267 is_native, it.GetMethodInvokeType(class_def), it.GetMemberIndex(),
268 &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700269 class_def_method_index++;
270 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700271 }
Ian Rogers0571d352011-11-03 19:51:38 -0700272 while (it.HasNextVirtualMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800273 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700274 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
275 is_native, it.GetMethodInvokeType(class_def), it.GetMemberIndex(),
276 &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700277 class_def_method_index++;
278 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700279 }
Ian Rogers0571d352011-11-03 19:51:38 -0700280 DCHECK(!it.HasNext());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700281 return offset;
282}
283
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700284size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
285 size_t __attribute__((unused)) class_def_index,
286 size_t class_def_method_index,
287 bool __attribute__((unused)) is_native,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800288 InvokeType invoke_type,
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700289 uint32_t method_idx, const DexFile* dex_file) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700290 // derived from CompiledMethod if available
291 uint32_t code_offset = 0;
292 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700293 uint32_t core_spill_mask = 0;
294 uint32_t fp_spill_mask = 0;
295 uint32_t mapping_table_offset = 0;
296 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800297 uint32_t gc_map_offset = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700298
Brian Carlstrom265091e2013-01-30 14:08:26 -0800299 OatClass* oat_class = oat_classes_[oat_class_index];
300#if defined(ART_USE_PORTABLE_COMPILER)
301 size_t oat_method_offsets_offset =
302 oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index);
303#endif
304
Ian Rogers0571d352011-11-03 19:51:38 -0700305 CompiledMethod* compiled_method =
Ian Rogers1212a022013-03-04 10:48:41 -0800306 compiler_driver_->GetCompiledMethod(CompilerDriver::MethodReference(dex_file, method_idx));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700307 if (compiled_method != NULL) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800308#if defined(ART_USE_PORTABLE_COMPILER)
309 compiled_method->AddOatdataOffsetToCompliledCodeOffset(
310 oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_));
311#else
312 const std::vector<uint8_t>& code = compiled_method->GetCode();
Logan Chien971bf3f2012-05-01 15:47:55 +0800313 offset = compiled_method->AlignCode(offset);
314 DCHECK_ALIGNED(offset, kArmAlignment);
Logan Chien971bf3f2012-05-01 15:47:55 +0800315 uint32_t code_size = code.size() * sizeof(code[0]);
316 CHECK_NE(code_size, 0U);
317 uint32_t thumb_offset = compiled_method->CodeDelta();
318 code_offset = offset + sizeof(code_size) + thumb_offset;
319
320 // Deduplicate code arrays
321 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
322 if (code_iter != code_offsets_.end()) {
323 code_offset = code_iter->second;
jeffhao55d78212011-11-02 11:41:50 -0700324 } else {
Logan Chien971bf3f2012-05-01 15:47:55 +0800325 code_offsets_.Put(&code, code_offset);
326 offset += sizeof(code_size); // code size is prepended before code
327 offset += code_size;
328 oat_header_->UpdateChecksum(&code[0], code_size);
329 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800330#endif
Logan Chien971bf3f2012-05-01 15:47:55 +0800331 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
332 core_spill_mask = compiled_method->GetCoreSpillMask();
333 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700334
Logan Chien971bf3f2012-05-01 15:47:55 +0800335 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
336 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
337 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700338
Logan Chien971bf3f2012-05-01 15:47:55 +0800339 // Deduplicate mapping tables
340 SafeMap<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
341 if (mapping_iter != mapping_table_offsets_.end()) {
342 mapping_table_offset = mapping_iter->second;
343 } else {
344 mapping_table_offsets_.Put(&mapping_table, mapping_table_offset);
345 offset += mapping_table_size;
346 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
347 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700348
Logan Chien971bf3f2012-05-01 15:47:55 +0800349 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
350 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
351 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700352
Logan Chien971bf3f2012-05-01 15:47:55 +0800353 // Deduplicate vmap tables
354 SafeMap<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
355 if (vmap_iter != vmap_table_offsets_.end()) {
356 vmap_table_offset = vmap_iter->second;
357 } else {
358 vmap_table_offsets_.Put(&vmap_table, vmap_table_offset);
359 offset += vmap_table_size;
360 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
361 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800362
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700363 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800364 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
365 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
Logan Chienccb7bf12012-03-28 12:52:32 +0800366
TDYa127ce4cc0d2012-11-18 16:59:53 -0800367#if !defined(NDEBUG)
Logan Chien971bf3f2012-05-01 15:47:55 +0800368 // We expect GC maps except when the class hasn't been verified or the method is native
Ian Rogers1212a022013-03-04 10:48:41 -0800369 CompilerDriver::ClassReference class_ref = CompilerDriver::ClassReference(dex_file, class_def_index);
370 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800371 mirror::Class::Status status;
jeffhaoec014232012-09-05 10:42:25 -0700372 if (compiled_class != NULL) {
373 status = compiled_class->GetStatus();
374 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800375 status = mirror::Class::kStatusError;
jeffhaoec014232012-09-05 10:42:25 -0700376 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800377 status = mirror::Class::kStatusNotReady;
jeffhaoec014232012-09-05 10:42:25 -0700378 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800379 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
380 << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
381 << (status < mirror::Class::kStatusVerified) << " " << status << " "
382 << PrettyMethod(method_idx, *dex_file);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800383#endif
384
Logan Chien971bf3f2012-05-01 15:47:55 +0800385 // Deduplicate GC maps
386 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = gc_map_offsets_.find(&gc_map);
387 if (gc_map_iter != gc_map_offsets_.end()) {
388 gc_map_offset = gc_map_iter->second;
389 } else {
390 gc_map_offsets_.Put(&gc_map, gc_map_offset);
391 offset += gc_map_size;
392 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800393 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700394 }
395
Brian Carlstrom265091e2013-01-30 14:08:26 -0800396 oat_class->method_offsets_[class_def_method_index]
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700397 = OatMethodOffsets(code_offset,
398 frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700399 core_spill_mask,
400 fp_spill_mask,
401 mapping_table_offset,
402 vmap_table_offset,
Jeff Hao74180ca2013-03-27 15:29:11 -0700403 gc_map_offset
Logan Chienccb7bf12012-03-28 12:52:32 +0800404 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700405
Ian Rogers1212a022013-03-04 10:48:41 -0800406 if (compiler_driver_->IsImage()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700407 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800408 mirror::DexCache* dex_cache = linker->FindDexCache(*dex_file);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700409 // Unchecked as we hold mutator_lock_ on entry.
410 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800411 mirror::AbstractMethod* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800412 NULL, NULL, invoke_type);
Ian Rogers0571d352011-11-03 19:51:38 -0700413 CHECK(method != NULL);
414 method->SetFrameSizeInBytes(frame_size_in_bytes);
415 method->SetCoreSpillMask(core_spill_mask);
416 method->SetFpSpillMask(fp_spill_mask);
417 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800418 // Don't overwrite static method trampoline
419 if (!method->IsStatic() || method->IsConstructor() ||
420 method->GetDeclaringClass()->IsInitialized()) {
421 method->SetOatCodeOffset(code_offset);
422 } else {
Jeff Haoaa4a7932013-05-13 11:28:27 -0700423 method->SetEntryPointFromCompiledCode(NULL);
Ian Rogers19846512012-02-24 11:42:47 -0800424 }
Ian Rogers0571d352011-11-03 19:51:38 -0700425 method->SetOatVmapTableOffset(vmap_table_offset);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700426 method->SetOatNativeGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700427 }
Logan Chien8b977d32012-02-21 19:14:55 +0800428
Brian Carlstrome24fa612011-09-29 00:53:55 -0700429 return offset;
430}
431
Brian Carlstrom265091e2013-01-30 14:08:26 -0800432#define DCHECK_OFFSET() \
433 DCHECK_EQ(static_cast<off_t>(offset), out.Seek(0, kSeekCurrent))
434
435#define DCHECK_OFFSET_() \
436 DCHECK_EQ(static_cast<off_t>(offset_), out.Seek(0, kSeekCurrent))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700437
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800438bool OatWriter::Write(OutputStream& out) {
439 if (!out.WriteFully(oat_header_, sizeof(*oat_header_))) {
440 PLOG(ERROR) << "Failed to write oat header to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700441 return false;
442 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700443 size_oat_header_ += sizeof(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700444
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800445 if (!out.WriteFully(image_file_location_.data(), image_file_location_.size())) {
446 PLOG(ERROR) << "Failed to write oat header image file location to " << out.GetLocation();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700447 return false;
448 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700449 size_oat_header_image_file_location_ += image_file_location_.size();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700450
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800451 if (!WriteTables(out)) {
452 LOG(ERROR) << "Failed to write oat tables to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700453 return false;
454 }
455
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800456 size_t code_offset = WriteCode(out);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700457 if (code_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800458 LOG(ERROR) << "Failed to write oat code to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700459 return false;
460 }
461
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800462 code_offset = WriteCodeDexFiles(out, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700463 if (code_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800464 LOG(ERROR) << "Failed to write oat code for dex files to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700465 return false;
466 }
467
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700468 if (kIsDebugBuild) {
469 uint32_t size_total = 0;
470 #define DO_STAT(x) \
471 LOG(INFO) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
472 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700473
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700474 DO_STAT(size_dex_file_alignment_);
475 DO_STAT(size_executable_offset_alignment_);
476 DO_STAT(size_oat_header_);
477 DO_STAT(size_oat_header_image_file_location_);
478 DO_STAT(size_dex_file_);
479 DO_STAT(size_interpreter_to_interpreter_entry_);
480 DO_STAT(size_interpreter_to_quick_entry_);
481 DO_STAT(size_portable_resolution_trampoline_);
482 DO_STAT(size_quick_resolution_trampoline_);
483 DO_STAT(size_stubs_alignment_);
484 DO_STAT(size_code_size_);
485 DO_STAT(size_code_);
486 DO_STAT(size_code_alignment_);
487 DO_STAT(size_mapping_table_);
488 DO_STAT(size_vmap_table_);
489 DO_STAT(size_gc_map_);
490 DO_STAT(size_oat_dex_file_location_size_);
491 DO_STAT(size_oat_dex_file_location_data_);
492 DO_STAT(size_oat_dex_file_location_checksum_);
493 DO_STAT(size_oat_dex_file_offset_);
494 DO_STAT(size_oat_dex_file_methods_offsets_);
495 DO_STAT(size_oat_class_status_);
496 DO_STAT(size_oat_class_method_offsets_);
497 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700498
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700499 LOG(INFO) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
500 CHECK_EQ(size_total, static_cast<uint32_t>(out.Seek(0, kSeekCurrent)));
501 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700502
Brian Carlstrome24fa612011-09-29 00:53:55 -0700503 return true;
504}
505
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800506bool OatWriter::WriteTables(OutputStream& out) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700507 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700508 if (!oat_dex_files_[i]->Write(this, out)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800509 PLOG(ERROR) << "Failed to write oat dex information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700510 return false;
511 }
512 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800513 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
514 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800515 off_t actual_offset = out.Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -0800516 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
517 const DexFile* dex_file = (*dex_files_)[i];
518 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
519 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
520 return false;
521 }
522 const DexFile* dex_file = (*dex_files_)[i];
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800523 if (!out.WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
524 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800525 return false;
526 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700527 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800528 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800529 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700530 if (!oat_classes_[i]->Write(this, out)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800531 PLOG(ERROR) << "Failed to write oat methods information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700532 return false;
533 }
534 }
535 return true;
536}
537
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800538size_t OatWriter::WriteCode(OutputStream& out) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800539 uint32_t offset = oat_header_->GetExecutableOffset();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700540 off_t new_offset = out.Seek(size_executable_offset_alignment_, kSeekCurrent);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800541 if (static_cast<uint32_t>(new_offset) != offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700542 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Brian Carlstrom265091e2013-01-30 14:08:26 -0800543 << " Expected: " << offset << " File: " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700544 return 0;
545 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800546 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700547 if (compiler_driver_->IsImage()) {
548 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
549 if (!out.WriteFully(&(*interpreter_to_interpreter_entry_)[0], interpreter_to_interpreter_entry_->size())) {
550 PLOG(ERROR) << "Failed to write interpreter to interpreter entry to " << out.GetLocation();
551 return false;
552 }
553 size_interpreter_to_interpreter_entry_ += interpreter_to_interpreter_entry_->size();
554 offset += interpreter_to_interpreter_entry_->size();
555 DCHECK_OFFSET();
556
557 uint32_t aligned_offset = CompiledCode::AlignCode(offset, instruction_set);
558 uint32_t alignment_padding = aligned_offset - offset;
559 out.Seek(alignment_padding, kSeekCurrent);
560 size_stubs_alignment_ += alignment_padding;
561 if (!out.WriteFully(&(*interpreter_to_quick_entry_)[0], interpreter_to_quick_entry_->size())) {
562 PLOG(ERROR) << "Failed to write interpreter to quick entry to " << out.GetLocation();
563 return false;
564 }
565 size_interpreter_to_quick_entry_ += interpreter_to_quick_entry_->size();
566 offset += alignment_padding + interpreter_to_quick_entry_->size();
567 DCHECK_OFFSET();
568
569 aligned_offset = CompiledCode::AlignCode(offset, instruction_set);
570 alignment_padding = aligned_offset - offset;
571 out.Seek(alignment_padding, kSeekCurrent);
572 size_stubs_alignment_ += alignment_padding;
573 if (!out.WriteFully(&(*portable_resolution_trampoline_)[0], portable_resolution_trampoline_->size())) {
574 PLOG(ERROR) << "Failed to write portable resolution trampoline to " << out.GetLocation();
575 return false;
576 }
577 size_portable_resolution_trampoline_ += portable_resolution_trampoline_->size();
578 offset += alignment_padding + portable_resolution_trampoline_->size();
579 DCHECK_OFFSET();
580
581 aligned_offset = CompiledCode::AlignCode(offset, instruction_set);
582 alignment_padding = aligned_offset - offset;
583 out.Seek(alignment_padding, kSeekCurrent);
584 size_stubs_alignment_ += alignment_padding;
585 if (!out.WriteFully(&(*quick_resolution_trampoline_)[0], quick_resolution_trampoline_->size())) {
586 PLOG(ERROR) << "Failed to write quick resolution trampoline to " << out.GetLocation();
587 return false;
588 }
589 size_quick_resolution_trampoline_ += quick_resolution_trampoline_->size();
590 offset += alignment_padding + quick_resolution_trampoline_->size();
591 DCHECK_OFFSET();
592 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800593 return offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700594}
595
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800596size_t OatWriter::WriteCodeDexFiles(OutputStream& out, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700597 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800598 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700599 const DexFile* dex_file = (*dex_files_)[i];
600 CHECK(dex_file != NULL);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800601 code_offset = WriteCodeDexFile(out, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700602 if (code_offset == 0) {
603 return 0;
604 }
605 }
606 return code_offset;
607}
608
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800609size_t OatWriter::WriteCodeDexFile(OutputStream& out, size_t code_offset, size_t& oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700610 const DexFile& dex_file) {
611 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
612 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700613 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800614 code_offset = WriteCodeClassDef(out, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700615 if (code_offset == 0) {
616 return 0;
617 }
618 }
619 return code_offset;
620}
621
Ian Rogers0571d352011-11-03 19:51:38 -0700622void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800623 const DexFile& dex_file, OutputStream& out) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700624 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800625 << " to " << out.GetLocation();
Elliott Hughes234da572011-11-03 22:13:06 -0700626}
627
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800628size_t OatWriter::WriteCodeClassDef(OutputStream& out,
Ian Rogers0571d352011-11-03 19:51:38 -0700629 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700630 const DexFile& dex_file,
631 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700632 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700633 if (class_data == NULL) {
634 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700635 return code_offset;
636 }
Ian Rogers0571d352011-11-03 19:51:38 -0700637 ClassDataItemIterator it(dex_file, class_data);
638 // Skip fields
639 while (it.HasNextStaticField()) {
640 it.Next();
641 }
642 while (it.HasNextInstanceField()) {
643 it.Next();
644 }
645 // Process methods
646 size_t class_def_method_index = 0;
647 while (it.HasNextDirectMethod()) {
648 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800649 code_offset = WriteCodeMethod(out, code_offset, oat_class_index, class_def_method_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700650 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700651 if (code_offset == 0) {
652 return 0;
653 }
Ian Rogers0571d352011-11-03 19:51:38 -0700654 class_def_method_index++;
655 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700656 }
Ian Rogers0571d352011-11-03 19:51:38 -0700657 while (it.HasNextVirtualMethod()) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800658 code_offset = WriteCodeMethod(out, code_offset, oat_class_index, class_def_method_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700659 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700660 if (code_offset == 0) {
661 return 0;
662 }
Ian Rogers0571d352011-11-03 19:51:38 -0700663 class_def_method_index++;
664 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700665 }
666 return code_offset;
667}
668
Brian Carlstrom265091e2013-01-30 14:08:26 -0800669size_t OatWriter::WriteCodeMethod(OutputStream& out, size_t offset, size_t oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700670 size_t class_def_method_index, bool is_static,
671 uint32_t method_idx, const DexFile& dex_file) {
672 const CompiledMethod* compiled_method =
Ian Rogers1212a022013-03-04 10:48:41 -0800673 compiler_driver_->GetCompiledMethod(CompilerDriver::MethodReference(&dex_file, method_idx));
Ian Rogers0571d352011-11-03 19:51:38 -0700674
Ian Rogers0571d352011-11-03 19:51:38 -0700675 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800676 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700677
678
679 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstrom265091e2013-01-30 14:08:26 -0800680#if !defined(ART_USE_PORTABLE_COMPILER)
681 uint32_t aligned_offset = compiled_method->AlignCode(offset);
682 uint32_t aligned_code_delta = aligned_offset - offset;
Logan Chien971bf3f2012-05-01 15:47:55 +0800683 if (aligned_code_delta != 0) {
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800684 off_t new_offset = out.Seek(aligned_code_delta, kSeekCurrent);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700685 size_code_alignment_ += aligned_code_delta;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800686 if (static_cast<uint32_t>(new_offset) != aligned_offset) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800687 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Brian Carlstrom265091e2013-01-30 14:08:26 -0800688 << " Expected: " << aligned_offset << " File: " << out.GetLocation();
Logan Chien971bf3f2012-05-01 15:47:55 +0800689 return 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700690 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800691 offset += aligned_code_delta;
692 DCHECK_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700693 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800694 DCHECK_ALIGNED(offset, kArmAlignment);
Logan Chien971bf3f2012-05-01 15:47:55 +0800695 const std::vector<uint8_t>& code = compiled_method->GetCode();
696 uint32_t code_size = code.size() * sizeof(code[0]);
697 CHECK_NE(code_size, 0U);
698
699 // Deduplicate code arrays
Brian Carlstrom265091e2013-01-30 14:08:26 -0800700 size_t code_offset = offset + sizeof(code_size) + compiled_method->CodeDelta();
Logan Chien971bf3f2012-05-01 15:47:55 +0800701 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800702 if (code_iter != code_offsets_.end() && code_offset != method_offsets.code_offset_) {
703 DCHECK(code_iter->second == method_offsets.code_offset_)
704 << PrettyMethod(method_idx, dex_file);
Logan Chien971bf3f2012-05-01 15:47:55 +0800705 } else {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800706 DCHECK(code_offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800707 if (!out.WriteFully(&code_size, sizeof(code_size))) {
708 ReportWriteFailure("method code size", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800709 return 0;
710 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700711 size_code_size_ += sizeof(code_size);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800712 offset += sizeof(code_size);
713 DCHECK_OFFSET();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800714 if (!out.WriteFully(&code[0], code_size)) {
715 ReportWriteFailure("method code", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800716 return 0;
717 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700718 size_code_ += code_size;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800719 offset += code_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800720 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800721 DCHECK_OFFSET();
722#endif
Logan Chien971bf3f2012-05-01 15:47:55 +0800723
724 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
725 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
726
727 // Deduplicate mapping tables
728 SafeMap<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
729 mapping_table_offsets_.find(&mapping_table);
730 if (mapping_iter != mapping_table_offsets_.end() &&
Brian Carlstrom265091e2013-01-30 14:08:26 -0800731 offset != method_offsets.mapping_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800732 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
733 || mapping_iter->second == method_offsets.mapping_table_offset_)
734 << PrettyMethod(method_idx, dex_file);
735 } else {
736 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800737 || offset == method_offsets.mapping_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800738 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800739 if (!out.WriteFully(&mapping_table[0], mapping_table_size)) {
740 ReportWriteFailure("mapping table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800741 return 0;
742 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700743 size_mapping_table_ += mapping_table_size;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800744 offset += mapping_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800745 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800746 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800747
748 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
749 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
750
751 // Deduplicate vmap tables
752 SafeMap<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
753 vmap_table_offsets_.find(&vmap_table);
754 if (vmap_iter != vmap_table_offsets_.end() &&
Brian Carlstrom265091e2013-01-30 14:08:26 -0800755 offset != method_offsets.vmap_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800756 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
757 || vmap_iter->second == method_offsets.vmap_table_offset_)
758 << PrettyMethod(method_idx, dex_file);
759 } else {
760 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800761 || offset == method_offsets.vmap_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800762 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800763 if (!out.WriteFully(&vmap_table[0], vmap_table_size)) {
764 ReportWriteFailure("vmap table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800765 return 0;
766 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700767 size_vmap_table_ += vmap_table_size;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800768 offset += vmap_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800769 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800770 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800771
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700772 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800773 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
774
775 // Deduplicate GC maps
776 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
777 gc_map_offsets_.find(&gc_map);
778 if (gc_map_iter != gc_map_offsets_.end() &&
Brian Carlstrom265091e2013-01-30 14:08:26 -0800779 offset != method_offsets.gc_map_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800780 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
781 || gc_map_iter->second == method_offsets.gc_map_offset_)
782 << PrettyMethod(method_idx, dex_file);
783 } else {
784 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800785 || offset == method_offsets.gc_map_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800786 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800787 if (!out.WriteFully(&gc_map[0], gc_map_size)) {
788 ReportWriteFailure("GC map", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800789 return 0;
790 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700791 size_gc_map_ += gc_map_size;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800792 offset += gc_map_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800793 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800794 DCHECK_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700795 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800796
Brian Carlstrom265091e2013-01-30 14:08:26 -0800797 return offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700798}
799
Brian Carlstrom265091e2013-01-30 14:08:26 -0800800OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
801 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -0800802 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700803 dex_file_location_size_ = location.size();
804 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800805 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800806 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800807 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700808}
809
810size_t OatWriter::OatDexFile::SizeOf() const {
811 return sizeof(dex_file_location_size_)
812 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800813 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800814 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800815 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700816}
817
818void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
819 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
820 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800821 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800822 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800823 oat_header.UpdateChecksum(&methods_offsets_[0],
824 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700825}
826
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700827bool OatWriter::OatDexFile::Write(OatWriter* oat_writer, OutputStream& out) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800828 DCHECK_OFFSET_();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800829 if (!out.WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
830 PLOG(ERROR) << "Failed to write dex file location length to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700831 return false;
832 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700833 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800834 if (!out.WriteFully(dex_file_location_data_, dex_file_location_size_)) {
835 PLOG(ERROR) << "Failed to write dex file location data to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700836 return false;
837 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700838 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800839 if (!out.WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
840 PLOG(ERROR) << "Failed to write dex file location checksum to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700841 return false;
842 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700843 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800844 if (!out.WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
845 PLOG(ERROR) << "Failed to write dex file offset to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800846 return false;
847 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700848 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800849 if (!out.WriteFully(&methods_offsets_[0],
850 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
851 PLOG(ERROR) << "Failed to write methods offsets to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700852 return false;
853 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700854 oat_writer->size_oat_dex_file_methods_offsets_ +=
855 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700856 return true;
857}
858
Brian Carlstrom265091e2013-01-30 14:08:26 -0800859OatWriter::OatClass::OatClass(size_t offset, mirror::Class::Status status, uint32_t methods_count) {
860 offset_ = offset;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800861 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700862 method_offsets_.resize(methods_count);
863}
864
Brian Carlstrom265091e2013-01-30 14:08:26 -0800865size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
866 size_t class_def_method_index_) const {
867 return offset_ + GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
868}
869
870size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
871 size_t class_def_method_index_) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800872 return sizeof(status_)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800873 + (sizeof(method_offsets_[0]) * class_def_method_index_);
874}
875
876size_t OatWriter::OatClass::SizeOf() const {
877 return GetOatMethodOffsetsOffsetFromOatClass(method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700878}
879
Brian Carlstrom389efb02012-01-11 12:06:26 -0800880void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800881 oat_header.UpdateChecksum(&status_, sizeof(status_));
882 oat_header.UpdateChecksum(&method_offsets_[0],
883 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700884}
885
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700886bool OatWriter::OatClass::Write(OatWriter* oat_writer, OutputStream& out) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800887 DCHECK_OFFSET_();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800888 if (!out.WriteFully(&status_, sizeof(status_))) {
889 PLOG(ERROR) << "Failed to write class status to " << out.GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800890 return false;
891 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700892 oat_writer->size_oat_class_status_ += sizeof(status_);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800893 DCHECK_EQ(static_cast<off_t>(GetOatMethodOffsetsOffsetFromOatHeader(0)),
894 out.Seek(0, kSeekCurrent));
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800895 if (!out.WriteFully(&method_offsets_[0],
896 sizeof(method_offsets_[0]) * method_offsets_.size())) {
897 PLOG(ERROR) << "Failed to write method offsets to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700898 return false;
899 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700900 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrom265091e2013-01-30 14:08:26 -0800901 DCHECK_EQ(static_cast<off_t>(GetOatMethodOffsetsOffsetFromOatHeader(method_offsets_.size())),
902 out.Seek(0, kSeekCurrent));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700903 return true;
904}
905
Brian Carlstrome24fa612011-09-29 00:53:55 -0700906} // namespace art