blob: da05c49e0e5726e169b7576fa467128ad3101179 [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,
Brian Carlstromdf629502013-07-17 22:39:56 -0700402 gc_map_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700403
Ian Rogers1212a022013-03-04 10:48:41 -0800404 if (compiler_driver_->IsImage()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700405 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800406 mirror::DexCache* dex_cache = linker->FindDexCache(*dex_file);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700407 // Unchecked as we hold mutator_lock_ on entry.
408 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800409 mirror::AbstractMethod* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800410 NULL, NULL, invoke_type);
Ian Rogers0571d352011-11-03 19:51:38 -0700411 CHECK(method != NULL);
412 method->SetFrameSizeInBytes(frame_size_in_bytes);
413 method->SetCoreSpillMask(core_spill_mask);
414 method->SetFpSpillMask(fp_spill_mask);
415 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800416 // Don't overwrite static method trampoline
417 if (!method->IsStatic() || method->IsConstructor() ||
418 method->GetDeclaringClass()->IsInitialized()) {
419 method->SetOatCodeOffset(code_offset);
420 } else {
Jeff Haoaa4a7932013-05-13 11:28:27 -0700421 method->SetEntryPointFromCompiledCode(NULL);
Ian Rogers19846512012-02-24 11:42:47 -0800422 }
Ian Rogers0571d352011-11-03 19:51:38 -0700423 method->SetOatVmapTableOffset(vmap_table_offset);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700424 method->SetOatNativeGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700425 }
Logan Chien8b977d32012-02-21 19:14:55 +0800426
Brian Carlstrome24fa612011-09-29 00:53:55 -0700427 return offset;
428}
429
Brian Carlstrom265091e2013-01-30 14:08:26 -0800430#define DCHECK_OFFSET() \
431 DCHECK_EQ(static_cast<off_t>(offset), out.Seek(0, kSeekCurrent))
432
433#define DCHECK_OFFSET_() \
434 DCHECK_EQ(static_cast<off_t>(offset_), out.Seek(0, kSeekCurrent))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700435
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800436bool OatWriter::Write(OutputStream& out) {
437 if (!out.WriteFully(oat_header_, sizeof(*oat_header_))) {
438 PLOG(ERROR) << "Failed to write oat header to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700439 return false;
440 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700441 size_oat_header_ += sizeof(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700442
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800443 if (!out.WriteFully(image_file_location_.data(), image_file_location_.size())) {
444 PLOG(ERROR) << "Failed to write oat header image file location to " << out.GetLocation();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700445 return false;
446 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700447 size_oat_header_image_file_location_ += image_file_location_.size();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700448
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800449 if (!WriteTables(out)) {
450 LOG(ERROR) << "Failed to write oat tables to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700451 return false;
452 }
453
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800454 size_t code_offset = WriteCode(out);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700455 if (code_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800456 LOG(ERROR) << "Failed to write oat code to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700457 return false;
458 }
459
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800460 code_offset = WriteCodeDexFiles(out, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700461 if (code_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800462 LOG(ERROR) << "Failed to write oat code for dex files to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700463 return false;
464 }
465
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700466 if (kIsDebugBuild) {
467 uint32_t size_total = 0;
468 #define DO_STAT(x) \
469 LOG(INFO) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
470 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700471
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700472 DO_STAT(size_dex_file_alignment_);
473 DO_STAT(size_executable_offset_alignment_);
474 DO_STAT(size_oat_header_);
475 DO_STAT(size_oat_header_image_file_location_);
476 DO_STAT(size_dex_file_);
477 DO_STAT(size_interpreter_to_interpreter_entry_);
478 DO_STAT(size_interpreter_to_quick_entry_);
479 DO_STAT(size_portable_resolution_trampoline_);
480 DO_STAT(size_quick_resolution_trampoline_);
481 DO_STAT(size_stubs_alignment_);
482 DO_STAT(size_code_size_);
483 DO_STAT(size_code_);
484 DO_STAT(size_code_alignment_);
485 DO_STAT(size_mapping_table_);
486 DO_STAT(size_vmap_table_);
487 DO_STAT(size_gc_map_);
488 DO_STAT(size_oat_dex_file_location_size_);
489 DO_STAT(size_oat_dex_file_location_data_);
490 DO_STAT(size_oat_dex_file_location_checksum_);
491 DO_STAT(size_oat_dex_file_offset_);
492 DO_STAT(size_oat_dex_file_methods_offsets_);
493 DO_STAT(size_oat_class_status_);
494 DO_STAT(size_oat_class_method_offsets_);
495 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700496
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700497 LOG(INFO) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
498 CHECK_EQ(size_total, static_cast<uint32_t>(out.Seek(0, kSeekCurrent)));
499 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700500
Brian Carlstrome24fa612011-09-29 00:53:55 -0700501 return true;
502}
503
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800504bool OatWriter::WriteTables(OutputStream& out) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700505 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700506 if (!oat_dex_files_[i]->Write(this, out)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800507 PLOG(ERROR) << "Failed to write oat dex information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700508 return false;
509 }
510 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800511 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
512 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800513 off_t actual_offset = out.Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -0800514 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
515 const DexFile* dex_file = (*dex_files_)[i];
516 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
517 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
518 return false;
519 }
520 const DexFile* dex_file = (*dex_files_)[i];
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800521 if (!out.WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
522 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800523 return false;
524 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700525 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800526 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800527 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700528 if (!oat_classes_[i]->Write(this, out)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800529 PLOG(ERROR) << "Failed to write oat methods information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700530 return false;
531 }
532 }
533 return true;
534}
535
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800536size_t OatWriter::WriteCode(OutputStream& out) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800537 uint32_t offset = oat_header_->GetExecutableOffset();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700538 off_t new_offset = out.Seek(size_executable_offset_alignment_, kSeekCurrent);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800539 if (static_cast<uint32_t>(new_offset) != offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700540 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Brian Carlstrom265091e2013-01-30 14:08:26 -0800541 << " Expected: " << offset << " File: " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700542 return 0;
543 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800544 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700545 if (compiler_driver_->IsImage()) {
546 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
547 if (!out.WriteFully(&(*interpreter_to_interpreter_entry_)[0], interpreter_to_interpreter_entry_->size())) {
548 PLOG(ERROR) << "Failed to write interpreter to interpreter entry to " << out.GetLocation();
549 return false;
550 }
551 size_interpreter_to_interpreter_entry_ += interpreter_to_interpreter_entry_->size();
552 offset += interpreter_to_interpreter_entry_->size();
553 DCHECK_OFFSET();
554
555 uint32_t aligned_offset = CompiledCode::AlignCode(offset, instruction_set);
556 uint32_t alignment_padding = aligned_offset - offset;
557 out.Seek(alignment_padding, kSeekCurrent);
558 size_stubs_alignment_ += alignment_padding;
559 if (!out.WriteFully(&(*interpreter_to_quick_entry_)[0], interpreter_to_quick_entry_->size())) {
560 PLOG(ERROR) << "Failed to write interpreter to quick entry to " << out.GetLocation();
561 return false;
562 }
563 size_interpreter_to_quick_entry_ += interpreter_to_quick_entry_->size();
564 offset += alignment_padding + interpreter_to_quick_entry_->size();
565 DCHECK_OFFSET();
566
567 aligned_offset = CompiledCode::AlignCode(offset, instruction_set);
568 alignment_padding = aligned_offset - offset;
569 out.Seek(alignment_padding, kSeekCurrent);
570 size_stubs_alignment_ += alignment_padding;
571 if (!out.WriteFully(&(*portable_resolution_trampoline_)[0], portable_resolution_trampoline_->size())) {
572 PLOG(ERROR) << "Failed to write portable resolution trampoline to " << out.GetLocation();
573 return false;
574 }
575 size_portable_resolution_trampoline_ += portable_resolution_trampoline_->size();
576 offset += alignment_padding + portable_resolution_trampoline_->size();
577 DCHECK_OFFSET();
578
579 aligned_offset = CompiledCode::AlignCode(offset, instruction_set);
580 alignment_padding = aligned_offset - offset;
581 out.Seek(alignment_padding, kSeekCurrent);
582 size_stubs_alignment_ += alignment_padding;
583 if (!out.WriteFully(&(*quick_resolution_trampoline_)[0], quick_resolution_trampoline_->size())) {
584 PLOG(ERROR) << "Failed to write quick resolution trampoline to " << out.GetLocation();
585 return false;
586 }
587 size_quick_resolution_trampoline_ += quick_resolution_trampoline_->size();
588 offset += alignment_padding + quick_resolution_trampoline_->size();
589 DCHECK_OFFSET();
590 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800591 return offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700592}
593
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800594size_t OatWriter::WriteCodeDexFiles(OutputStream& out, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700595 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800596 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700597 const DexFile* dex_file = (*dex_files_)[i];
598 CHECK(dex_file != NULL);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800599 code_offset = WriteCodeDexFile(out, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700600 if (code_offset == 0) {
601 return 0;
602 }
603 }
604 return code_offset;
605}
606
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800607size_t OatWriter::WriteCodeDexFile(OutputStream& out, size_t code_offset, size_t& oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700608 const DexFile& dex_file) {
609 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
610 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700611 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800612 code_offset = WriteCodeClassDef(out, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700613 if (code_offset == 0) {
614 return 0;
615 }
616 }
617 return code_offset;
618}
619
Ian Rogers0571d352011-11-03 19:51:38 -0700620void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800621 const DexFile& dex_file, OutputStream& out) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700622 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800623 << " to " << out.GetLocation();
Elliott Hughes234da572011-11-03 22:13:06 -0700624}
625
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800626size_t OatWriter::WriteCodeClassDef(OutputStream& out,
Ian Rogers0571d352011-11-03 19:51:38 -0700627 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700628 const DexFile& dex_file,
629 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700630 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700631 if (class_data == NULL) {
632 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700633 return code_offset;
634 }
Ian Rogers0571d352011-11-03 19:51:38 -0700635 ClassDataItemIterator it(dex_file, class_data);
636 // Skip fields
637 while (it.HasNextStaticField()) {
638 it.Next();
639 }
640 while (it.HasNextInstanceField()) {
641 it.Next();
642 }
643 // Process methods
644 size_t class_def_method_index = 0;
645 while (it.HasNextDirectMethod()) {
646 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800647 code_offset = WriteCodeMethod(out, code_offset, oat_class_index, class_def_method_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700648 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700649 if (code_offset == 0) {
650 return 0;
651 }
Ian Rogers0571d352011-11-03 19:51:38 -0700652 class_def_method_index++;
653 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700654 }
Ian Rogers0571d352011-11-03 19:51:38 -0700655 while (it.HasNextVirtualMethod()) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800656 code_offset = WriteCodeMethod(out, code_offset, oat_class_index, class_def_method_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700657 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700658 if (code_offset == 0) {
659 return 0;
660 }
Ian Rogers0571d352011-11-03 19:51:38 -0700661 class_def_method_index++;
662 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700663 }
664 return code_offset;
665}
666
Brian Carlstrom265091e2013-01-30 14:08:26 -0800667size_t OatWriter::WriteCodeMethod(OutputStream& out, size_t offset, size_t oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700668 size_t class_def_method_index, bool is_static,
669 uint32_t method_idx, const DexFile& dex_file) {
670 const CompiledMethod* compiled_method =
Brian Carlstrom51c24672013-07-11 16:00:56 -0700671 compiler_driver_->GetCompiledMethod(MethodReference(&dex_file, method_idx));
Ian Rogers0571d352011-11-03 19:51:38 -0700672
Ian Rogers0571d352011-11-03 19:51:38 -0700673 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800674 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700675
676
677 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstrom265091e2013-01-30 14:08:26 -0800678#if !defined(ART_USE_PORTABLE_COMPILER)
679 uint32_t aligned_offset = compiled_method->AlignCode(offset);
680 uint32_t aligned_code_delta = aligned_offset - offset;
Logan Chien971bf3f2012-05-01 15:47:55 +0800681 if (aligned_code_delta != 0) {
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800682 off_t new_offset = out.Seek(aligned_code_delta, kSeekCurrent);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700683 size_code_alignment_ += aligned_code_delta;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800684 if (static_cast<uint32_t>(new_offset) != aligned_offset) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800685 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Brian Carlstrom265091e2013-01-30 14:08:26 -0800686 << " Expected: " << aligned_offset << " File: " << out.GetLocation();
Logan Chien971bf3f2012-05-01 15:47:55 +0800687 return 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700688 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800689 offset += aligned_code_delta;
690 DCHECK_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700691 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800692 DCHECK_ALIGNED(offset, kArmAlignment);
Logan Chien971bf3f2012-05-01 15:47:55 +0800693 const std::vector<uint8_t>& code = compiled_method->GetCode();
694 uint32_t code_size = code.size() * sizeof(code[0]);
695 CHECK_NE(code_size, 0U);
696
697 // Deduplicate code arrays
Brian Carlstrom265091e2013-01-30 14:08:26 -0800698 size_t code_offset = offset + sizeof(code_size) + compiled_method->CodeDelta();
Logan Chien971bf3f2012-05-01 15:47:55 +0800699 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800700 if (code_iter != code_offsets_.end() && code_offset != method_offsets.code_offset_) {
701 DCHECK(code_iter->second == method_offsets.code_offset_)
702 << PrettyMethod(method_idx, dex_file);
Logan Chien971bf3f2012-05-01 15:47:55 +0800703 } else {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800704 DCHECK(code_offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800705 if (!out.WriteFully(&code_size, sizeof(code_size))) {
706 ReportWriteFailure("method code size", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800707 return 0;
708 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700709 size_code_size_ += sizeof(code_size);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800710 offset += sizeof(code_size);
711 DCHECK_OFFSET();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800712 if (!out.WriteFully(&code[0], code_size)) {
713 ReportWriteFailure("method code", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800714 return 0;
715 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700716 size_code_ += code_size;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800717 offset += code_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800718 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800719 DCHECK_OFFSET();
720#endif
Logan Chien971bf3f2012-05-01 15:47:55 +0800721
722 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
723 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
724
725 // Deduplicate mapping tables
726 SafeMap<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
727 mapping_table_offsets_.find(&mapping_table);
728 if (mapping_iter != mapping_table_offsets_.end() &&
Brian Carlstrom265091e2013-01-30 14:08:26 -0800729 offset != method_offsets.mapping_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800730 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
731 || mapping_iter->second == method_offsets.mapping_table_offset_)
732 << PrettyMethod(method_idx, dex_file);
733 } else {
734 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800735 || offset == method_offsets.mapping_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800736 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800737 if (!out.WriteFully(&mapping_table[0], mapping_table_size)) {
738 ReportWriteFailure("mapping table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800739 return 0;
740 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700741 size_mapping_table_ += mapping_table_size;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800742 offset += mapping_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800743 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800744 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800745
746 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
747 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
748
749 // Deduplicate vmap tables
750 SafeMap<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
751 vmap_table_offsets_.find(&vmap_table);
752 if (vmap_iter != vmap_table_offsets_.end() &&
Brian Carlstrom265091e2013-01-30 14:08:26 -0800753 offset != method_offsets.vmap_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800754 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
755 || vmap_iter->second == method_offsets.vmap_table_offset_)
756 << PrettyMethod(method_idx, dex_file);
757 } else {
758 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800759 || offset == method_offsets.vmap_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800760 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800761 if (!out.WriteFully(&vmap_table[0], vmap_table_size)) {
762 ReportWriteFailure("vmap table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800763 return 0;
764 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700765 size_vmap_table_ += vmap_table_size;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800766 offset += vmap_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800767 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800768 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800769
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700770 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800771 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
772
773 // Deduplicate GC maps
774 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
775 gc_map_offsets_.find(&gc_map);
776 if (gc_map_iter != gc_map_offsets_.end() &&
Brian Carlstrom265091e2013-01-30 14:08:26 -0800777 offset != method_offsets.gc_map_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800778 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
779 || gc_map_iter->second == method_offsets.gc_map_offset_)
780 << PrettyMethod(method_idx, dex_file);
781 } else {
782 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800783 || offset == method_offsets.gc_map_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800784 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800785 if (!out.WriteFully(&gc_map[0], gc_map_size)) {
786 ReportWriteFailure("GC map", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800787 return 0;
788 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700789 size_gc_map_ += gc_map_size;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800790 offset += gc_map_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800791 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800792 DCHECK_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700793 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800794
Brian Carlstrom265091e2013-01-30 14:08:26 -0800795 return offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700796}
797
Brian Carlstrom265091e2013-01-30 14:08:26 -0800798OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
799 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -0800800 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700801 dex_file_location_size_ = location.size();
802 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800803 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800804 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800805 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700806}
807
808size_t OatWriter::OatDexFile::SizeOf() const {
809 return sizeof(dex_file_location_size_)
810 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800811 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800812 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800813 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700814}
815
816void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
817 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
818 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800819 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800820 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800821 oat_header.UpdateChecksum(&methods_offsets_[0],
822 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700823}
824
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700825bool OatWriter::OatDexFile::Write(OatWriter* oat_writer, OutputStream& out) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800826 DCHECK_OFFSET_();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800827 if (!out.WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
828 PLOG(ERROR) << "Failed to write dex file location length to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700829 return false;
830 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700831 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800832 if (!out.WriteFully(dex_file_location_data_, dex_file_location_size_)) {
833 PLOG(ERROR) << "Failed to write dex file location data to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700834 return false;
835 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700836 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800837 if (!out.WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
838 PLOG(ERROR) << "Failed to write dex file location checksum to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700839 return false;
840 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700841 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800842 if (!out.WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
843 PLOG(ERROR) << "Failed to write dex file offset to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800844 return false;
845 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700846 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800847 if (!out.WriteFully(&methods_offsets_[0],
848 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
849 PLOG(ERROR) << "Failed to write methods offsets to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700850 return false;
851 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700852 oat_writer->size_oat_dex_file_methods_offsets_ +=
853 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700854 return true;
855}
856
Brian Carlstrom265091e2013-01-30 14:08:26 -0800857OatWriter::OatClass::OatClass(size_t offset, mirror::Class::Status status, uint32_t methods_count) {
858 offset_ = offset;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800859 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700860 method_offsets_.resize(methods_count);
861}
862
Brian Carlstrom265091e2013-01-30 14:08:26 -0800863size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
864 size_t class_def_method_index_) const {
865 return offset_ + GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
866}
867
868size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
869 size_t class_def_method_index_) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800870 return sizeof(status_)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800871 + (sizeof(method_offsets_[0]) * class_def_method_index_);
872}
873
874size_t OatWriter::OatClass::SizeOf() const {
875 return GetOatMethodOffsetsOffsetFromOatClass(method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700876}
877
Brian Carlstrom389efb02012-01-11 12:06:26 -0800878void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800879 oat_header.UpdateChecksum(&status_, sizeof(status_));
880 oat_header.UpdateChecksum(&method_offsets_[0],
881 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700882}
883
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700884bool OatWriter::OatClass::Write(OatWriter* oat_writer, OutputStream& out) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800885 DCHECK_OFFSET_();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800886 if (!out.WriteFully(&status_, sizeof(status_))) {
887 PLOG(ERROR) << "Failed to write class status to " << out.GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800888 return false;
889 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700890 oat_writer->size_oat_class_status_ += sizeof(status_);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800891 DCHECK_EQ(static_cast<off_t>(GetOatMethodOffsetsOffsetFromOatHeader(0)),
892 out.Seek(0, kSeekCurrent));
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800893 if (!out.WriteFully(&method_offsets_[0],
894 sizeof(method_offsets_[0]) * method_offsets_.size())) {
895 PLOG(ERROR) << "Failed to write method offsets to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700896 return false;
897 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700898 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrom265091e2013-01-30 14:08:26 -0800899 DCHECK_EQ(static_cast<off_t>(GetOatMethodOffsetsOffsetFromOatHeader(method_offsets_.size())),
900 out.Seek(0, kSeekCurrent));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700901 return true;
902}
903
Brian Carlstrome24fa612011-09-29 00:53:55 -0700904} // namespace art