blob: 5eb837b25cff0867cc52208a249ec880af6a2cfe [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 Carlstrom3320cf42011-10-04 14:58:28 -070038OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070039 uint32_t image_file_location_oat_checksum,
40 uint32_t image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070041 const std::string& image_file_location,
Ian Rogers1212a022013-03-04 10:48:41 -080042 const CompilerDriver* compiler)
Jeff Hao0aba0ba2013-06-03 14:49:28 -070043 : compiler_driver_(compiler),
44 dex_files_(&dex_files),
45 image_file_location_oat_checksum_(image_file_location_oat_checksum),
46 image_file_location_oat_begin_(image_file_location_oat_begin),
47 image_file_location_(image_file_location),
48 oat_header_(NULL),
49 size_dex_file_alignment_(0),
50 size_executable_offset_alignment_(0),
51 size_oat_header_(0),
52 size_oat_header_image_file_location_(0),
53 size_dex_file_(0),
54 size_interpreter_to_interpreter_entry_(0),
55 size_interpreter_to_quick_entry_(0),
56 size_portable_resolution_trampoline_(0),
57 size_quick_resolution_trampoline_(0),
58 size_stubs_alignment_(0),
59 size_code_size_(0),
60 size_code_(0),
61 size_code_alignment_(0),
62 size_mapping_table_(0),
63 size_vmap_table_(0),
64 size_gc_map_(0),
65 size_oat_dex_file_location_size_(0),
66 size_oat_dex_file_location_data_(0),
67 size_oat_dex_file_location_checksum_(0),
68 size_oat_dex_file_offset_(0),
69 size_oat_dex_file_methods_offsets_(0),
70 size_oat_class_status_(0),
71 size_oat_class_method_offsets_(0) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070072 size_t offset = InitOatHeader();
Brian Carlstrome24fa612011-09-29 00:53:55 -070073 offset = InitOatDexFiles(offset);
Brian Carlstrom89521892011-12-07 22:05:07 -080074 offset = InitDexFiles(offset);
Brian Carlstrom389efb02012-01-11 12:06:26 -080075 offset = InitOatClasses(offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -070076 offset = InitOatCode(offset);
77 offset = InitOatCodeDexFiles(offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -070078 size_ = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -070079
80 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Jeff Hao0aba0ba2013-06-03 14:49:28 -070081 CHECK(image_file_location.empty() == compiler->IsImage());
Brian Carlstrome24fa612011-09-29 00:53:55 -070082}
83
Ian Rogers0571d352011-11-03 19:51:38 -070084OatWriter::~OatWriter() {
85 delete oat_header_;
86 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -080087 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -070088}
89
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070090size_t OatWriter::InitOatHeader() {
Brian Carlstrome24fa612011-09-29 00:53:55 -070091 // create the OatHeader
Ian Rogers1212a022013-03-04 10:48:41 -080092 oat_header_ = new OatHeader(compiler_driver_->GetInstructionSet(),
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070093 dex_files_,
Brian Carlstrom28db0122012-10-18 16:20:41 -070094 image_file_location_oat_checksum_,
95 image_file_location_oat_begin_,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070096 image_file_location_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070097 size_t offset = sizeof(*oat_header_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070098 offset += image_file_location_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -070099 return offset;
100}
101
102size_t OatWriter::InitOatDexFiles(size_t offset) {
103 // create the OatDexFiles
104 for (size_t i = 0; i != dex_files_->size(); ++i) {
105 const DexFile* dex_file = (*dex_files_)[i];
106 CHECK(dex_file != NULL);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800107 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700108 oat_dex_files_.push_back(oat_dex_file);
109 offset += oat_dex_file->SizeOf();
110 }
111 return offset;
112}
113
Brian Carlstrom89521892011-12-07 22:05:07 -0800114size_t OatWriter::InitDexFiles(size_t offset) {
115 // calculate the offsets within OatDexFiles to the DexFiles
116 for (size_t i = 0; i != dex_files_->size(); ++i) {
117 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700118 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800119 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700120 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800121
122 // set offset in OatDexFile to DexFile
123 oat_dex_files_[i]->dex_file_offset_ = offset;
124
125 const DexFile* dex_file = (*dex_files_)[i];
126 offset += dex_file->GetHeader().file_size_;
127 }
128 return offset;
129}
130
Brian Carlstrom389efb02012-01-11 12:06:26 -0800131size_t OatWriter::InitOatClasses(size_t offset) {
132 // create the OatClasses
133 // calculate the offsets within OatDexFiles to OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -0700134 for (size_t i = 0; i != dex_files_->size(); ++i) {
135 const DexFile* dex_file = (*dex_files_)[i];
136 for (size_t class_def_index = 0;
137 class_def_index < dex_file->NumClassDefs();
Ian Rogersc20a83e2012-01-18 18:15:32 -0800138 class_def_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800139 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700140 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
141 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700142 uint32_t num_methods = 0;
143 if (class_data != NULL) { // ie not an empty class, such as a marker interface
144 ClassDataItemIterator it(*dex_file, class_data);
145 size_t num_direct_methods = it.NumDirectMethods();
146 size_t num_virtual_methods = it.NumVirtualMethods();
147 num_methods = num_direct_methods + num_virtual_methods;
148 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800149
Brian Carlstrom51c24672013-07-11 16:00:56 -0700150 ClassReference class_ref(dex_file, class_def_index);
Ian Rogers1212a022013-03-04 10:48:41 -0800151 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800152 mirror::Class::Status status;
jeffhaoec014232012-09-05 10:42:25 -0700153 if (compiled_class != NULL) {
154 status = compiled_class->GetStatus();
155 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800156 status = mirror::Class::kStatusError;
jeffhaoec014232012-09-05 10:42:25 -0700157 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158 status = mirror::Class::kStatusNotReady;
jeffhaoec014232012-09-05 10:42:25 -0700159 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800160
Brian Carlstrom265091e2013-01-30 14:08:26 -0800161 OatClass* oat_class = new OatClass(offset, status, num_methods);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800162 oat_classes_.push_back(oat_class);
163 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700164 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800165 oat_dex_files_[i]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700166 }
167 return offset;
168}
169
170size_t OatWriter::InitOatCode(size_t offset) {
171 // calculate the offsets within OatHeader to executable code
172 size_t old_offset = offset;
173 // required to be on a new page boundary
174 offset = RoundUp(offset, kPageSize);
175 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700176 size_executable_offset_alignment_ = offset - old_offset;
177 if (compiler_driver_->IsImage()) {
178 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
179 oat_header_->SetInterpreterToInterpreterEntryOffset(offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700180 interpreter_to_interpreter_entry_.reset(
181 compiler_driver_->CreateInterpreterToInterpreterEntry());
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700182 offset += interpreter_to_interpreter_entry_->size();
183
184 offset = CompiledCode::AlignCode(offset, instruction_set);
185 oat_header_->SetInterpreterToQuickEntryOffset(offset);
186 interpreter_to_quick_entry_.reset(compiler_driver_->CreateInterpreterToQuickEntry());
187 offset += interpreter_to_quick_entry_->size();
188
189 offset = CompiledCode::AlignCode(offset, instruction_set);
190 oat_header_->SetPortableResolutionTrampolineOffset(offset);
191 portable_resolution_trampoline_.reset(compiler_driver_->CreatePortableResolutionTrampoline());
192 offset += portable_resolution_trampoline_->size();
193
194 offset = CompiledCode::AlignCode(offset, instruction_set);
195 oat_header_->SetQuickResolutionTrampolineOffset(offset);
196 quick_resolution_trampoline_.reset(compiler_driver_->CreateQuickResolutionTrampoline());
197 offset += quick_resolution_trampoline_->size();
198 } else {
199 oat_header_->SetInterpreterToInterpreterEntryOffset(0);
200 oat_header_->SetInterpreterToQuickEntryOffset(0);
201 oat_header_->SetPortableResolutionTrampolineOffset(0);
202 oat_header_->SetQuickResolutionTrampolineOffset(0);
203 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700204 return offset;
205}
206
207size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700208 size_t oat_class_index = 0;
209 for (size_t i = 0; i != dex_files_->size(); ++i) {
210 const DexFile* dex_file = (*dex_files_)[i];
211 CHECK(dex_file != NULL);
212 offset = InitOatCodeDexFile(offset, oat_class_index, *dex_file);
213 }
214 return offset;
215}
216
217size_t OatWriter::InitOatCodeDexFile(size_t offset,
218 size_t& oat_class_index,
219 const DexFile& dex_file) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800220 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700221 class_def_index < dex_file.NumClassDefs();
222 class_def_index++, oat_class_index++) {
223 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800224 offset = InitOatCodeClassDef(offset, oat_class_index, class_def_index, dex_file, class_def);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800225 oat_classes_[oat_class_index]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700226 }
227 return offset;
228}
229
230size_t OatWriter::InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800231 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700232 const DexFile& dex_file,
233 const DexFile::ClassDef& class_def) {
234 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700235 if (class_data == NULL) {
236 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700237 return offset;
238 }
Ian Rogers0571d352011-11-03 19:51:38 -0700239 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800240 CHECK_EQ(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700241 it.NumDirectMethods() + it.NumVirtualMethods());
242 // Skip fields
243 while (it.HasNextStaticField()) {
244 it.Next();
245 }
246 while (it.HasNextInstanceField()) {
247 it.Next();
248 }
249 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700250 size_t class_def_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700251 while (it.HasNextDirectMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800252 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700253 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
254 is_native, it.GetMethodInvokeType(class_def), it.GetMemberIndex(),
255 &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700256 class_def_method_index++;
257 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700258 }
Ian Rogers0571d352011-11-03 19:51:38 -0700259 while (it.HasNextVirtualMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800260 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700261 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
262 is_native, it.GetMethodInvokeType(class_def), it.GetMemberIndex(),
263 &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700264 class_def_method_index++;
265 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700266 }
Ian Rogers0571d352011-11-03 19:51:38 -0700267 DCHECK(!it.HasNext());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700268 return offset;
269}
270
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700271size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
272 size_t __attribute__((unused)) class_def_index,
273 size_t class_def_method_index,
274 bool __attribute__((unused)) is_native,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800275 InvokeType invoke_type,
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700276 uint32_t method_idx, const DexFile* dex_file) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700277 // derived from CompiledMethod if available
278 uint32_t code_offset = 0;
279 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700280 uint32_t core_spill_mask = 0;
281 uint32_t fp_spill_mask = 0;
282 uint32_t mapping_table_offset = 0;
283 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800284 uint32_t gc_map_offset = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700285
Brian Carlstrom265091e2013-01-30 14:08:26 -0800286 OatClass* oat_class = oat_classes_[oat_class_index];
287#if defined(ART_USE_PORTABLE_COMPILER)
288 size_t oat_method_offsets_offset =
289 oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index);
290#endif
291
Ian Rogers0571d352011-11-03 19:51:38 -0700292 CompiledMethod* compiled_method =
Brian Carlstrom51c24672013-07-11 16:00:56 -0700293 compiler_driver_->GetCompiledMethod(MethodReference(dex_file, method_idx));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700294 if (compiled_method != NULL) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800295#if defined(ART_USE_PORTABLE_COMPILER)
296 compiled_method->AddOatdataOffsetToCompliledCodeOffset(
297 oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_));
298#else
299 const std::vector<uint8_t>& code = compiled_method->GetCode();
Logan Chien971bf3f2012-05-01 15:47:55 +0800300 offset = compiled_method->AlignCode(offset);
301 DCHECK_ALIGNED(offset, kArmAlignment);
Logan Chien971bf3f2012-05-01 15:47:55 +0800302 uint32_t code_size = code.size() * sizeof(code[0]);
303 CHECK_NE(code_size, 0U);
304 uint32_t thumb_offset = compiled_method->CodeDelta();
305 code_offset = offset + sizeof(code_size) + thumb_offset;
306
307 // Deduplicate code arrays
308 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
309 if (code_iter != code_offsets_.end()) {
310 code_offset = code_iter->second;
jeffhao55d78212011-11-02 11:41:50 -0700311 } else {
Logan Chien971bf3f2012-05-01 15:47:55 +0800312 code_offsets_.Put(&code, code_offset);
313 offset += sizeof(code_size); // code size is prepended before code
314 offset += code_size;
315 oat_header_->UpdateChecksum(&code[0], code_size);
316 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800317#endif
Logan Chien971bf3f2012-05-01 15:47:55 +0800318 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
319 core_spill_mask = compiled_method->GetCoreSpillMask();
320 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700321
Logan Chien971bf3f2012-05-01 15:47:55 +0800322 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
323 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
324 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700325
Logan Chien971bf3f2012-05-01 15:47:55 +0800326 // Deduplicate mapping tables
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700327 SafeMap<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
328 mapping_table_offsets_.find(&mapping_table);
Logan Chien971bf3f2012-05-01 15:47:55 +0800329 if (mapping_iter != mapping_table_offsets_.end()) {
330 mapping_table_offset = mapping_iter->second;
331 } else {
332 mapping_table_offsets_.Put(&mapping_table, mapping_table_offset);
333 offset += mapping_table_size;
334 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
335 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700336
Logan Chien971bf3f2012-05-01 15:47:55 +0800337 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
338 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
339 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700340
Logan Chien971bf3f2012-05-01 15:47:55 +0800341 // Deduplicate vmap tables
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700342 SafeMap<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
343 vmap_table_offsets_.find(&vmap_table);
Logan Chien971bf3f2012-05-01 15:47:55 +0800344 if (vmap_iter != vmap_table_offsets_.end()) {
345 vmap_table_offset = vmap_iter->second;
346 } else {
347 vmap_table_offsets_.Put(&vmap_table, vmap_table_offset);
348 offset += vmap_table_size;
349 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
350 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800351
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700352 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800353 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
354 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
Logan Chienccb7bf12012-03-28 12:52:32 +0800355
TDYa127ce4cc0d2012-11-18 16:59:53 -0800356#if !defined(NDEBUG)
Logan Chien971bf3f2012-05-01 15:47:55 +0800357 // We expect GC maps except when the class hasn't been verified or the method is native
Brian Carlstrom51c24672013-07-11 16:00:56 -0700358 ClassReference class_ref(dex_file, class_def_index);
Ian Rogers1212a022013-03-04 10:48:41 -0800359 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800360 mirror::Class::Status status;
jeffhaoec014232012-09-05 10:42:25 -0700361 if (compiled_class != NULL) {
362 status = compiled_class->GetStatus();
363 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800364 status = mirror::Class::kStatusError;
jeffhaoec014232012-09-05 10:42:25 -0700365 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800366 status = mirror::Class::kStatusNotReady;
jeffhaoec014232012-09-05 10:42:25 -0700367 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800368 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
369 << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
370 << (status < mirror::Class::kStatusVerified) << " " << status << " "
371 << PrettyMethod(method_idx, *dex_file);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800372#endif
373
Logan Chien971bf3f2012-05-01 15:47:55 +0800374 // Deduplicate GC maps
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700375 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
376 gc_map_offsets_.find(&gc_map);
Logan Chien971bf3f2012-05-01 15:47:55 +0800377 if (gc_map_iter != gc_map_offsets_.end()) {
378 gc_map_offset = gc_map_iter->second;
379 } else {
380 gc_map_offsets_.Put(&gc_map, gc_map_offset);
381 offset += gc_map_size;
382 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800383 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700384 }
385
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700386 oat_class->method_offsets_[class_def_method_index] =
387 OatMethodOffsets(code_offset,
388 frame_size_in_bytes,
389 core_spill_mask,
390 fp_spill_mask,
391 mapping_table_offset,
392 vmap_table_offset,
393 gc_map_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700394
Ian Rogers1212a022013-03-04 10:48:41 -0800395 if (compiler_driver_->IsImage()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700396 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800397 mirror::DexCache* dex_cache = linker->FindDexCache(*dex_file);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700398 // Unchecked as we hold mutator_lock_ on entry.
399 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800400 mirror::AbstractMethod* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800401 NULL, NULL, invoke_type);
Ian Rogers0571d352011-11-03 19:51:38 -0700402 CHECK(method != NULL);
403 method->SetFrameSizeInBytes(frame_size_in_bytes);
404 method->SetCoreSpillMask(core_spill_mask);
405 method->SetFpSpillMask(fp_spill_mask);
406 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800407 // Don't overwrite static method trampoline
408 if (!method->IsStatic() || method->IsConstructor() ||
409 method->GetDeclaringClass()->IsInitialized()) {
410 method->SetOatCodeOffset(code_offset);
411 } else {
Jeff Haoaa4a7932013-05-13 11:28:27 -0700412 method->SetEntryPointFromCompiledCode(NULL);
Ian Rogers19846512012-02-24 11:42:47 -0800413 }
Ian Rogers0571d352011-11-03 19:51:38 -0700414 method->SetOatVmapTableOffset(vmap_table_offset);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700415 method->SetOatNativeGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700416 }
Logan Chien8b977d32012-02-21 19:14:55 +0800417
Brian Carlstrome24fa612011-09-29 00:53:55 -0700418 return offset;
419}
420
Brian Carlstrom265091e2013-01-30 14:08:26 -0800421#define DCHECK_OFFSET() \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700422 DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out.Seek(0, kSeekCurrent)) \
423 << "file_offset=" << file_offset << " relative_offset=" << relative_offset
Brian Carlstrom265091e2013-01-30 14:08:26 -0800424
425#define DCHECK_OFFSET_() \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700426 DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out.Seek(0, kSeekCurrent)) \
427 << "file_offset=" << file_offset << " offset_=" << offset_
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700428
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800429bool OatWriter::Write(OutputStream& out) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700430 const size_t file_offset = out.Seek(0, kSeekCurrent);
431
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800432 if (!out.WriteFully(oat_header_, sizeof(*oat_header_))) {
433 PLOG(ERROR) << "Failed to write oat header to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700434 return false;
435 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700436 size_oat_header_ += sizeof(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700437
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800438 if (!out.WriteFully(image_file_location_.data(), image_file_location_.size())) {
439 PLOG(ERROR) << "Failed to write oat header image file location to " << out.GetLocation();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700440 return false;
441 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700442 size_oat_header_image_file_location_ += image_file_location_.size();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700443
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700444 if (!WriteTables(out, file_offset)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800445 LOG(ERROR) << "Failed to write oat tables to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700446 return false;
447 }
448
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700449 size_t relative_offset = WriteCode(out, file_offset);
450 if (relative_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800451 LOG(ERROR) << "Failed to write oat code to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700452 return false;
453 }
454
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700455 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
456 if (relative_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800457 LOG(ERROR) << "Failed to write oat code for dex files to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700458 return false;
459 }
460
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700461 if (kIsDebugBuild) {
462 uint32_t size_total = 0;
463 #define DO_STAT(x) \
464 LOG(INFO) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
465 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700466
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700467 DO_STAT(size_dex_file_alignment_);
468 DO_STAT(size_executable_offset_alignment_);
469 DO_STAT(size_oat_header_);
470 DO_STAT(size_oat_header_image_file_location_);
471 DO_STAT(size_dex_file_);
472 DO_STAT(size_interpreter_to_interpreter_entry_);
473 DO_STAT(size_interpreter_to_quick_entry_);
474 DO_STAT(size_portable_resolution_trampoline_);
475 DO_STAT(size_quick_resolution_trampoline_);
476 DO_STAT(size_stubs_alignment_);
477 DO_STAT(size_code_size_);
478 DO_STAT(size_code_);
479 DO_STAT(size_code_alignment_);
480 DO_STAT(size_mapping_table_);
481 DO_STAT(size_vmap_table_);
482 DO_STAT(size_gc_map_);
483 DO_STAT(size_oat_dex_file_location_size_);
484 DO_STAT(size_oat_dex_file_location_data_);
485 DO_STAT(size_oat_dex_file_location_checksum_);
486 DO_STAT(size_oat_dex_file_offset_);
487 DO_STAT(size_oat_dex_file_methods_offsets_);
488 DO_STAT(size_oat_class_status_);
489 DO_STAT(size_oat_class_method_offsets_);
490 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700491
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700492 LOG(INFO) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700493 CHECK_EQ(file_offset + size_total, static_cast<uint32_t>(out.Seek(0, kSeekCurrent)));
494 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700495 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700496
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700497 CHECK_EQ(file_offset + size_, static_cast<uint32_t>(out.Seek(0, kSeekCurrent)));
498 CHECK_EQ(size_, relative_offset);
499
Brian Carlstrome24fa612011-09-29 00:53:55 -0700500 return true;
501}
502
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700503bool OatWriter::WriteTables(OutputStream& out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700504 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700505 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800506 PLOG(ERROR) << "Failed to write oat dex information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700507 return false;
508 }
509 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800510 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700511 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800512 off_t actual_offset = out.Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -0800513 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
514 const DexFile* dex_file = (*dex_files_)[i];
515 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
516 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
517 return false;
518 }
519 const DexFile* dex_file = (*dex_files_)[i];
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800520 if (!out.WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700521 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
522 << " 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) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700528 if (!oat_classes_[i]->Write(this, out, file_offset)) {
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 Carlstromc50d8e12013-07-23 22:35:16 -0700536size_t OatWriter::WriteCode(OutputStream& out, const size_t file_offset) {
537 size_t relative_offset = oat_header_->GetExecutableOffset();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700538 off_t new_offset = out.Seek(size_executable_offset_alignment_, kSeekCurrent);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700539 size_t expected_file_offset = file_offset + relative_offset;
540 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700541 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700542 << " Expected: " << expected_file_offset << " File: " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700543 return 0;
544 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800545 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700546 if (compiler_driver_->IsImage()) {
547 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700548 if (!out.WriteFully(&(*interpreter_to_interpreter_entry_)[0],
549 interpreter_to_interpreter_entry_->size())) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700550 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();
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700554 relative_offset += interpreter_to_interpreter_entry_->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700555 DCHECK_OFFSET();
556
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700557 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set);
558 uint32_t alignment_padding = aligned_offset - relative_offset;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700559 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();
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700566 relative_offset += alignment_padding + interpreter_to_quick_entry_->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700567 DCHECK_OFFSET();
568
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700569 aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set);
570 alignment_padding = aligned_offset - relative_offset;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700571 out.Seek(alignment_padding, kSeekCurrent);
572 size_stubs_alignment_ += alignment_padding;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700573 if (!out.WriteFully(&(*portable_resolution_trampoline_)[0],
574 portable_resolution_trampoline_->size())) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700575 PLOG(ERROR) << "Failed to write portable resolution trampoline to " << out.GetLocation();
576 return false;
577 }
578 size_portable_resolution_trampoline_ += portable_resolution_trampoline_->size();
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700579 relative_offset += alignment_padding + portable_resolution_trampoline_->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700580 DCHECK_OFFSET();
581
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700582 aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set);
583 alignment_padding = aligned_offset - relative_offset;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700584 out.Seek(alignment_padding, kSeekCurrent);
585 size_stubs_alignment_ += alignment_padding;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700586 if (!out.WriteFully(&(*quick_resolution_trampoline_)[0],
587 quick_resolution_trampoline_->size())) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700588 PLOG(ERROR) << "Failed to write quick resolution trampoline to " << out.GetLocation();
589 return false;
590 }
591 size_quick_resolution_trampoline_ += quick_resolution_trampoline_->size();
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700592 relative_offset += alignment_padding + quick_resolution_trampoline_->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700593 DCHECK_OFFSET();
594 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700595 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700596}
597
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700598size_t OatWriter::WriteCodeDexFiles(OutputStream& out,
599 const size_t file_offset,
600 size_t relative_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700601 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800602 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700603 const DexFile* dex_file = (*dex_files_)[i];
604 CHECK(dex_file != NULL);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700605 relative_offset = WriteCodeDexFile(out, file_offset, relative_offset, oat_class_index,
606 *dex_file);
607 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700608 return 0;
609 }
610 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700611 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700612}
613
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700614size_t OatWriter::WriteCodeDexFile(OutputStream& out, const size_t file_offset,
615 size_t relative_offset, size_t& oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700616 const DexFile& dex_file) {
617 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
618 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700619 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700620 relative_offset = WriteCodeClassDef(out, file_offset, relative_offset, oat_class_index,
621 dex_file, class_def);
622 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700623 return 0;
624 }
625 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700626 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700627}
628
Ian Rogers0571d352011-11-03 19:51:38 -0700629void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800630 const DexFile& dex_file, OutputStream& out) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700631 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800632 << " to " << out.GetLocation();
Elliott Hughes234da572011-11-03 22:13:06 -0700633}
634
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800635size_t OatWriter::WriteCodeClassDef(OutputStream& out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700636 const size_t file_offset,
637 size_t relative_offset,
638 size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700639 const DexFile& dex_file,
640 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700641 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700642 if (class_data == NULL) {
643 // ie. an empty class such as a marker interface
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700644 return relative_offset;
Ian Rogers387b6992011-10-31 17:52:37 -0700645 }
Ian Rogers0571d352011-11-03 19:51:38 -0700646 ClassDataItemIterator it(dex_file, class_data);
647 // Skip fields
648 while (it.HasNextStaticField()) {
649 it.Next();
650 }
651 while (it.HasNextInstanceField()) {
652 it.Next();
653 }
654 // Process methods
655 size_t class_def_method_index = 0;
656 while (it.HasNextDirectMethod()) {
657 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700658 relative_offset = WriteCodeMethod(out, file_offset, relative_offset, oat_class_index,
659 class_def_method_index, is_static, it.GetMemberIndex(),
660 dex_file);
661 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700662 return 0;
663 }
Ian Rogers0571d352011-11-03 19:51:38 -0700664 class_def_method_index++;
665 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700666 }
Ian Rogers0571d352011-11-03 19:51:38 -0700667 while (it.HasNextVirtualMethod()) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700668 relative_offset = WriteCodeMethod(out, file_offset, relative_offset, oat_class_index,
669 class_def_method_index, false, it.GetMemberIndex(), dex_file);
670 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700671 return 0;
672 }
Ian Rogers0571d352011-11-03 19:51:38 -0700673 class_def_method_index++;
674 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700675 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700676 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700677}
678
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700679size_t OatWriter::WriteCodeMethod(OutputStream& out, const size_t file_offset,
680 size_t relative_offset, size_t oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700681 size_t class_def_method_index, bool is_static,
682 uint32_t method_idx, const DexFile& dex_file) {
683 const CompiledMethod* compiled_method =
Brian Carlstrom51c24672013-07-11 16:00:56 -0700684 compiler_driver_->GetCompiledMethod(MethodReference(&dex_file, method_idx));
Ian Rogers0571d352011-11-03 19:51:38 -0700685
Ian Rogers0571d352011-11-03 19:51:38 -0700686 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800687 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700688
689
690 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstrom265091e2013-01-30 14:08:26 -0800691#if !defined(ART_USE_PORTABLE_COMPILER)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700692 uint32_t aligned_offset = compiled_method->AlignCode(relative_offset);
693 uint32_t aligned_code_delta = aligned_offset - relative_offset;
Logan Chien971bf3f2012-05-01 15:47:55 +0800694 if (aligned_code_delta != 0) {
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800695 off_t new_offset = out.Seek(aligned_code_delta, kSeekCurrent);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700696 size_code_alignment_ += aligned_code_delta;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700697 uint32_t expected_offset = file_offset + aligned_offset;
698 if (static_cast<uint32_t>(new_offset) != expected_offset) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800699 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700700 << " Expected: " << expected_offset << " File: " << out.GetLocation();
Logan Chien971bf3f2012-05-01 15:47:55 +0800701 return 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700702 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700703 relative_offset += aligned_code_delta;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800704 DCHECK_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700705 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700706 DCHECK_ALIGNED(relative_offset, kArmAlignment);
Logan Chien971bf3f2012-05-01 15:47:55 +0800707 const std::vector<uint8_t>& code = compiled_method->GetCode();
708 uint32_t code_size = code.size() * sizeof(code[0]);
709 CHECK_NE(code_size, 0U);
710
711 // Deduplicate code arrays
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700712 size_t code_offset = relative_offset + sizeof(code_size) + compiled_method->CodeDelta();
Logan Chien971bf3f2012-05-01 15:47:55 +0800713 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800714 if (code_iter != code_offsets_.end() && code_offset != method_offsets.code_offset_) {
715 DCHECK(code_iter->second == method_offsets.code_offset_)
716 << PrettyMethod(method_idx, dex_file);
Logan Chien971bf3f2012-05-01 15:47:55 +0800717 } else {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800718 DCHECK(code_offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800719 if (!out.WriteFully(&code_size, sizeof(code_size))) {
720 ReportWriteFailure("method code size", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800721 return 0;
722 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700723 size_code_size_ += sizeof(code_size);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700724 relative_offset += sizeof(code_size);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800725 DCHECK_OFFSET();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800726 if (!out.WriteFully(&code[0], code_size)) {
727 ReportWriteFailure("method code", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800728 return 0;
729 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700730 size_code_ += code_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700731 relative_offset += code_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800732 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800733 DCHECK_OFFSET();
734#endif
Logan Chien971bf3f2012-05-01 15:47:55 +0800735
736 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
737 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
738
739 // Deduplicate mapping tables
740 SafeMap<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
741 mapping_table_offsets_.find(&mapping_table);
742 if (mapping_iter != mapping_table_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700743 relative_offset != method_offsets.mapping_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800744 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
745 || mapping_iter->second == method_offsets.mapping_table_offset_)
746 << PrettyMethod(method_idx, dex_file);
747 } else {
748 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700749 || relative_offset == method_offsets.mapping_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800750 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800751 if (!out.WriteFully(&mapping_table[0], mapping_table_size)) {
752 ReportWriteFailure("mapping table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800753 return 0;
754 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700755 size_mapping_table_ += mapping_table_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700756 relative_offset += mapping_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800757 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800758 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800759
760 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
761 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
762
763 // Deduplicate vmap tables
764 SafeMap<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
765 vmap_table_offsets_.find(&vmap_table);
766 if (vmap_iter != vmap_table_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700767 relative_offset != method_offsets.vmap_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800768 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
769 || vmap_iter->second == method_offsets.vmap_table_offset_)
770 << PrettyMethod(method_idx, dex_file);
771 } else {
772 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700773 || relative_offset == method_offsets.vmap_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800774 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800775 if (!out.WriteFully(&vmap_table[0], vmap_table_size)) {
776 ReportWriteFailure("vmap table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800777 return 0;
778 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700779 size_vmap_table_ += vmap_table_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700780 relative_offset += vmap_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800781 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800782 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800783
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700784 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800785 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
786
787 // Deduplicate GC maps
788 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
789 gc_map_offsets_.find(&gc_map);
790 if (gc_map_iter != gc_map_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700791 relative_offset != method_offsets.gc_map_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800792 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
793 || gc_map_iter->second == method_offsets.gc_map_offset_)
794 << PrettyMethod(method_idx, dex_file);
795 } else {
796 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700797 || relative_offset == method_offsets.gc_map_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800798 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800799 if (!out.WriteFully(&gc_map[0], gc_map_size)) {
800 ReportWriteFailure("GC map", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800801 return 0;
802 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700803 size_gc_map_ += gc_map_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700804 relative_offset += gc_map_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800805 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800806 DCHECK_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700807 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800808
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700809 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700810}
811
Brian Carlstrom265091e2013-01-30 14:08:26 -0800812OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
813 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -0800814 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700815 dex_file_location_size_ = location.size();
816 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800817 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800818 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800819 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700820}
821
822size_t OatWriter::OatDexFile::SizeOf() const {
823 return sizeof(dex_file_location_size_)
824 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800825 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800826 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800827 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700828}
829
830void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
831 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
832 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800833 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800834 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800835 oat_header.UpdateChecksum(&methods_offsets_[0],
836 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700837}
838
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700839bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
840 OutputStream& out,
841 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800842 DCHECK_OFFSET_();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800843 if (!out.WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
844 PLOG(ERROR) << "Failed to write dex file location length to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700845 return false;
846 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700847 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800848 if (!out.WriteFully(dex_file_location_data_, dex_file_location_size_)) {
849 PLOG(ERROR) << "Failed to write dex file location data 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_location_data_ += dex_file_location_size_;
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800853 if (!out.WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
854 PLOG(ERROR) << "Failed to write dex file location checksum to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700855 return false;
856 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700857 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800858 if (!out.WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
859 PLOG(ERROR) << "Failed to write dex file offset to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800860 return false;
861 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700862 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800863 if (!out.WriteFully(&methods_offsets_[0],
864 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
865 PLOG(ERROR) << "Failed to write methods offsets to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700866 return false;
867 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700868 oat_writer->size_oat_dex_file_methods_offsets_ +=
869 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700870 return true;
871}
872
Brian Carlstrom265091e2013-01-30 14:08:26 -0800873OatWriter::OatClass::OatClass(size_t offset, mirror::Class::Status status, uint32_t methods_count) {
874 offset_ = offset;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800875 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700876 method_offsets_.resize(methods_count);
877}
878
Brian Carlstrom265091e2013-01-30 14:08:26 -0800879size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
880 size_t class_def_method_index_) const {
881 return offset_ + GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
882}
883
884size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
885 size_t class_def_method_index_) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800886 return sizeof(status_)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800887 + (sizeof(method_offsets_[0]) * class_def_method_index_);
888}
889
890size_t OatWriter::OatClass::SizeOf() const {
891 return GetOatMethodOffsetsOffsetFromOatClass(method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700892}
893
Brian Carlstrom389efb02012-01-11 12:06:26 -0800894void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800895 oat_header.UpdateChecksum(&status_, sizeof(status_));
896 oat_header.UpdateChecksum(&method_offsets_[0],
897 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700898}
899
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700900bool OatWriter::OatClass::Write(OatWriter* oat_writer,
901 OutputStream& out,
902 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800903 DCHECK_OFFSET_();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800904 if (!out.WriteFully(&status_, sizeof(status_))) {
905 PLOG(ERROR) << "Failed to write class status to " << out.GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800906 return false;
907 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700908 oat_writer->size_oat_class_status_ += sizeof(status_);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700909 DCHECK_EQ(static_cast<off_t>(file_offset + GetOatMethodOffsetsOffsetFromOatHeader(0)),
Brian Carlstrom265091e2013-01-30 14:08:26 -0800910 out.Seek(0, kSeekCurrent));
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800911 if (!out.WriteFully(&method_offsets_[0],
912 sizeof(method_offsets_[0]) * method_offsets_.size())) {
913 PLOG(ERROR) << "Failed to write method offsets to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700914 return false;
915 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700916 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700917 DCHECK_EQ(static_cast<off_t>(file_offset +
918 GetOatMethodOffsetsOffsetFromOatHeader(method_offsets_.size())),
Brian Carlstrom265091e2013-01-30 14:08:26 -0800919 out.Seek(0, kSeekCurrent));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700920 return true;
921}
922
Brian Carlstrome24fa612011-09-29 00:53:55 -0700923} // namespace art