blob: a4bd87d4c00ec2ca4f1e9e02dec9b4948e8f9f7a [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrome24fa612011-09-29 00:53:55 -070016
17#include "oat_writer.h"
18
Elliott Hughesa0e18062012-04-13 15:59:59 -070019#include <zlib.h>
20
Elliott Hughes1aa246d2012-12-13 09:29:36 -080021#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080022#include "base/unix_file/fd_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070023#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070024#include "dex_file-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/abstract_method-inl.h"
26#include "mirror/array.h"
27#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070029#include "os.h"
Brian Carlstromcd60ac72013-01-20 17:09:51 -080030#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070031#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070032#include "scoped_thread_state_change.h"
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070033#include "gc/space.h"
jeffhaoec014232012-09-05 10:42:25 -070034#include "verifier/method_verifier.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070035
36namespace art {
37
Brian Carlstromcd60ac72013-01-20 17:09:51 -080038bool OatWriter::Create(OutputStream& output_stream,
jeffhao10037c82012-01-23 15:06:23 -080039 const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070040 uint32_t image_file_location_oat_checksum,
41 uint32_t image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070042 const std::string& image_file_location,
Ian Rogers1212a022013-03-04 10:48:41 -080043 const CompilerDriver& driver) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070044 OatWriter oat_writer(dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070045 image_file_location_oat_checksum,
46 image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070047 image_file_location,
Ian Rogers1212a022013-03-04 10:48:41 -080048 &driver);
Brian Carlstromcd60ac72013-01-20 17:09:51 -080049 return oat_writer.Write(output_stream);
Brian Carlstrome24fa612011-09-29 00:53:55 -070050}
51
Brian Carlstrom3320cf42011-10-04 14:58:28 -070052OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070053 uint32_t image_file_location_oat_checksum,
54 uint32_t image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070055 const std::string& image_file_location,
Ian Rogers1212a022013-03-04 10:48:41 -080056 const CompilerDriver* compiler)
Jeff Hao0aba0ba2013-06-03 14:49:28 -070057 : compiler_driver_(compiler),
58 dex_files_(&dex_files),
59 image_file_location_oat_checksum_(image_file_location_oat_checksum),
60 image_file_location_oat_begin_(image_file_location_oat_begin),
61 image_file_location_(image_file_location),
62 oat_header_(NULL),
63 size_dex_file_alignment_(0),
64 size_executable_offset_alignment_(0),
65 size_oat_header_(0),
66 size_oat_header_image_file_location_(0),
67 size_dex_file_(0),
68 size_interpreter_to_interpreter_entry_(0),
69 size_interpreter_to_quick_entry_(0),
70 size_portable_resolution_trampoline_(0),
71 size_quick_resolution_trampoline_(0),
72 size_stubs_alignment_(0),
73 size_code_size_(0),
74 size_code_(0),
75 size_code_alignment_(0),
76 size_mapping_table_(0),
77 size_vmap_table_(0),
78 size_gc_map_(0),
79 size_oat_dex_file_location_size_(0),
80 size_oat_dex_file_location_data_(0),
81 size_oat_dex_file_location_checksum_(0),
82 size_oat_dex_file_offset_(0),
83 size_oat_dex_file_methods_offsets_(0),
84 size_oat_class_status_(0),
85 size_oat_class_method_offsets_(0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070086
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070087 size_t offset = InitOatHeader();
Brian Carlstrome24fa612011-09-29 00:53:55 -070088 offset = InitOatDexFiles(offset);
Brian Carlstrom89521892011-12-07 22:05:07 -080089 offset = InitDexFiles(offset);
Brian Carlstrom389efb02012-01-11 12:06:26 -080090 offset = InitOatClasses(offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -070091 offset = InitOatCode(offset);
92 offset = InitOatCodeDexFiles(offset);
93
94 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Jeff Hao0aba0ba2013-06-03 14:49:28 -070095 CHECK(image_file_location.empty() == compiler->IsImage());
Brian Carlstrome24fa612011-09-29 00:53:55 -070096}
97
Ian Rogers0571d352011-11-03 19:51:38 -070098OatWriter::~OatWriter() {
99 delete oat_header_;
100 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800101 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -0700102}
103
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700104size_t OatWriter::InitOatHeader() {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700105 // create the OatHeader
Ian Rogers1212a022013-03-04 10:48:41 -0800106 oat_header_ = new OatHeader(compiler_driver_->GetInstructionSet(),
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700107 dex_files_,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700108 image_file_location_oat_checksum_,
109 image_file_location_oat_begin_,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700110 image_file_location_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700111 size_t offset = sizeof(*oat_header_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700112 offset += image_file_location_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700113 return offset;
114}
115
116size_t OatWriter::InitOatDexFiles(size_t offset) {
117 // create the OatDexFiles
118 for (size_t i = 0; i != dex_files_->size(); ++i) {
119 const DexFile* dex_file = (*dex_files_)[i];
120 CHECK(dex_file != NULL);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800121 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700122 oat_dex_files_.push_back(oat_dex_file);
123 offset += oat_dex_file->SizeOf();
124 }
125 return offset;
126}
127
Brian Carlstrom89521892011-12-07 22:05:07 -0800128size_t OatWriter::InitDexFiles(size_t offset) {
129 // calculate the offsets within OatDexFiles to the DexFiles
130 for (size_t i = 0; i != dex_files_->size(); ++i) {
131 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700132 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800133 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700134 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800135
136 // set offset in OatDexFile to DexFile
137 oat_dex_files_[i]->dex_file_offset_ = offset;
138
139 const DexFile* dex_file = (*dex_files_)[i];
140 offset += dex_file->GetHeader().file_size_;
141 }
142 return offset;
143}
144
Brian Carlstrom389efb02012-01-11 12:06:26 -0800145size_t OatWriter::InitOatClasses(size_t offset) {
146 // create the OatClasses
147 // calculate the offsets within OatDexFiles to OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -0700148 for (size_t i = 0; i != dex_files_->size(); ++i) {
149 const DexFile* dex_file = (*dex_files_)[i];
150 for (size_t class_def_index = 0;
151 class_def_index < dex_file->NumClassDefs();
Ian Rogersc20a83e2012-01-18 18:15:32 -0800152 class_def_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800153 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700154 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
155 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700156 uint32_t num_methods = 0;
157 if (class_data != NULL) { // ie not an empty class, such as a marker interface
158 ClassDataItemIterator it(*dex_file, class_data);
159 size_t num_direct_methods = it.NumDirectMethods();
160 size_t num_virtual_methods = it.NumVirtualMethods();
161 num_methods = num_direct_methods + num_virtual_methods;
162 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800163
Ian Rogers1212a022013-03-04 10:48:41 -0800164 CompilerDriver::ClassReference class_ref = CompilerDriver::ClassReference(dex_file, class_def_index);
165 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800166 mirror::Class::Status status;
jeffhaoec014232012-09-05 10:42:25 -0700167 if (compiled_class != NULL) {
168 status = compiled_class->GetStatus();
169 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170 status = mirror::Class::kStatusError;
jeffhaoec014232012-09-05 10:42:25 -0700171 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800172 status = mirror::Class::kStatusNotReady;
jeffhaoec014232012-09-05 10:42:25 -0700173 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800174
Brian Carlstrom265091e2013-01-30 14:08:26 -0800175 OatClass* oat_class = new OatClass(offset, status, num_methods);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800176 oat_classes_.push_back(oat_class);
177 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700178 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800179 oat_dex_files_[i]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700180 }
181 return offset;
182}
183
184size_t OatWriter::InitOatCode(size_t offset) {
185 // calculate the offsets within OatHeader to executable code
186 size_t old_offset = offset;
187 // required to be on a new page boundary
188 offset = RoundUp(offset, kPageSize);
189 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700190 size_executable_offset_alignment_ = offset - old_offset;
191 if (compiler_driver_->IsImage()) {
192 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
193 oat_header_->SetInterpreterToInterpreterEntryOffset(offset);
194 interpreter_to_interpreter_entry_.reset(compiler_driver_->CreateInterpreterToInterpreterEntry());
195 offset += interpreter_to_interpreter_entry_->size();
196
197 offset = CompiledCode::AlignCode(offset, instruction_set);
198 oat_header_->SetInterpreterToQuickEntryOffset(offset);
199 interpreter_to_quick_entry_.reset(compiler_driver_->CreateInterpreterToQuickEntry());
200 offset += interpreter_to_quick_entry_->size();
201
202 offset = CompiledCode::AlignCode(offset, instruction_set);
203 oat_header_->SetPortableResolutionTrampolineOffset(offset);
204 portable_resolution_trampoline_.reset(compiler_driver_->CreatePortableResolutionTrampoline());
205 offset += portable_resolution_trampoline_->size();
206
207 offset = CompiledCode::AlignCode(offset, instruction_set);
208 oat_header_->SetQuickResolutionTrampolineOffset(offset);
209 quick_resolution_trampoline_.reset(compiler_driver_->CreateQuickResolutionTrampoline());
210 offset += quick_resolution_trampoline_->size();
211 } else {
212 oat_header_->SetInterpreterToInterpreterEntryOffset(0);
213 oat_header_->SetInterpreterToQuickEntryOffset(0);
214 oat_header_->SetPortableResolutionTrampolineOffset(0);
215 oat_header_->SetQuickResolutionTrampolineOffset(0);
216 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700217 return offset;
218}
219
220size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700221 size_t oat_class_index = 0;
222 for (size_t i = 0; i != dex_files_->size(); ++i) {
223 const DexFile* dex_file = (*dex_files_)[i];
224 CHECK(dex_file != NULL);
225 offset = InitOatCodeDexFile(offset, oat_class_index, *dex_file);
226 }
227 return offset;
228}
229
230size_t OatWriter::InitOatCodeDexFile(size_t offset,
231 size_t& oat_class_index,
232 const DexFile& dex_file) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800233 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700234 class_def_index < dex_file.NumClassDefs();
235 class_def_index++, oat_class_index++) {
236 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800237 offset = InitOatCodeClassDef(offset, oat_class_index, class_def_index, dex_file, class_def);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800238 oat_classes_[oat_class_index]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700239 }
240 return offset;
241}
242
243size_t OatWriter::InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800244 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700245 const DexFile& dex_file,
246 const DexFile::ClassDef& class_def) {
247 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700248 if (class_data == NULL) {
249 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700250 return offset;
251 }
Ian Rogers0571d352011-11-03 19:51:38 -0700252 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800253 CHECK_EQ(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700254 it.NumDirectMethods() + it.NumVirtualMethods());
255 // Skip fields
256 while (it.HasNextStaticField()) {
257 it.Next();
258 }
259 while (it.HasNextInstanceField()) {
260 it.Next();
261 }
262 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700263 size_t class_def_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700264 while (it.HasNextDirectMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800265 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700266 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
267 is_native, it.GetMethodInvokeType(class_def), it.GetMemberIndex(),
268 &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700269 class_def_method_index++;
270 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700271 }
Ian Rogers0571d352011-11-03 19:51:38 -0700272 while (it.HasNextVirtualMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800273 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700274 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
275 is_native, it.GetMethodInvokeType(class_def), it.GetMemberIndex(),
276 &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700277 class_def_method_index++;
278 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700279 }
Ian Rogers0571d352011-11-03 19:51:38 -0700280 DCHECK(!it.HasNext());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700281 return offset;
282}
283
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700284size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
285 size_t __attribute__((unused)) class_def_index,
286 size_t class_def_method_index,
287 bool __attribute__((unused)) is_native,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800288 InvokeType invoke_type,
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700289 uint32_t method_idx, const DexFile* dex_file) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700290 // derived from CompiledMethod if available
291 uint32_t code_offset = 0;
292 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700293 uint32_t core_spill_mask = 0;
294 uint32_t fp_spill_mask = 0;
295 uint32_t mapping_table_offset = 0;
296 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800297 uint32_t gc_map_offset = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700298
Brian Carlstrom265091e2013-01-30 14:08:26 -0800299 OatClass* oat_class = oat_classes_[oat_class_index];
300#if defined(ART_USE_PORTABLE_COMPILER)
301 size_t oat_method_offsets_offset =
302 oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index);
303#endif
304
Ian Rogers0571d352011-11-03 19:51:38 -0700305 CompiledMethod* compiled_method =
Ian Rogers1212a022013-03-04 10:48:41 -0800306 compiler_driver_->GetCompiledMethod(CompilerDriver::MethodReference(dex_file, method_idx));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700307 if (compiled_method != NULL) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800308#if defined(ART_USE_PORTABLE_COMPILER)
309 compiled_method->AddOatdataOffsetToCompliledCodeOffset(
310 oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_));
311#else
312 const std::vector<uint8_t>& code = compiled_method->GetCode();
Logan Chien971bf3f2012-05-01 15:47:55 +0800313 offset = compiled_method->AlignCode(offset);
314 DCHECK_ALIGNED(offset, kArmAlignment);
Logan Chien971bf3f2012-05-01 15:47:55 +0800315 uint32_t code_size = code.size() * sizeof(code[0]);
316 CHECK_NE(code_size, 0U);
317 uint32_t thumb_offset = compiled_method->CodeDelta();
318 code_offset = offset + sizeof(code_size) + thumb_offset;
319
320 // Deduplicate code arrays
321 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
322 if (code_iter != code_offsets_.end()) {
323 code_offset = code_iter->second;
jeffhao55d78212011-11-02 11:41:50 -0700324 } else {
Logan Chien971bf3f2012-05-01 15:47:55 +0800325 code_offsets_.Put(&code, code_offset);
326 offset += sizeof(code_size); // code size is prepended before code
327 offset += code_size;
328 oat_header_->UpdateChecksum(&code[0], code_size);
329 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800330#endif
Logan Chien971bf3f2012-05-01 15:47:55 +0800331 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
332 core_spill_mask = compiled_method->GetCoreSpillMask();
333 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700334
Logan Chien971bf3f2012-05-01 15:47:55 +0800335 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
336 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
337 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700338
Logan Chien971bf3f2012-05-01 15:47:55 +0800339 // Deduplicate mapping tables
340 SafeMap<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
341 if (mapping_iter != mapping_table_offsets_.end()) {
342 mapping_table_offset = mapping_iter->second;
343 } else {
344 mapping_table_offsets_.Put(&mapping_table, mapping_table_offset);
345 offset += mapping_table_size;
346 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
347 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700348
Logan Chien971bf3f2012-05-01 15:47:55 +0800349 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
350 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
351 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700352
Logan Chien971bf3f2012-05-01 15:47:55 +0800353 // Deduplicate vmap tables
354 SafeMap<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
355 if (vmap_iter != vmap_table_offsets_.end()) {
356 vmap_table_offset = vmap_iter->second;
357 } else {
358 vmap_table_offsets_.Put(&vmap_table, vmap_table_offset);
359 offset += vmap_table_size;
360 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
361 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800362
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700363 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800364 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
365 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
Logan Chienccb7bf12012-03-28 12:52:32 +0800366
TDYa127ce4cc0d2012-11-18 16:59:53 -0800367#if !defined(NDEBUG)
Logan Chien971bf3f2012-05-01 15:47:55 +0800368 // We expect GC maps except when the class hasn't been verified or the method is native
Ian Rogers1212a022013-03-04 10:48:41 -0800369 CompilerDriver::ClassReference class_ref = CompilerDriver::ClassReference(dex_file, class_def_index);
370 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800371 mirror::Class::Status status;
jeffhaoec014232012-09-05 10:42:25 -0700372 if (compiled_class != NULL) {
373 status = compiled_class->GetStatus();
374 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800375 status = mirror::Class::kStatusError;
jeffhaoec014232012-09-05 10:42:25 -0700376 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800377 status = mirror::Class::kStatusNotReady;
jeffhaoec014232012-09-05 10:42:25 -0700378 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800379 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
380 << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
381 << (status < mirror::Class::kStatusVerified) << " " << status << " "
382 << PrettyMethod(method_idx, *dex_file);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800383#endif
384
Logan Chien971bf3f2012-05-01 15:47:55 +0800385 // Deduplicate GC maps
386 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = gc_map_offsets_.find(&gc_map);
387 if (gc_map_iter != gc_map_offsets_.end()) {
388 gc_map_offset = gc_map_iter->second;
389 } else {
390 gc_map_offsets_.Put(&gc_map, gc_map_offset);
391 offset += gc_map_size;
392 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800393 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700394 }
395
Brian Carlstrom265091e2013-01-30 14:08:26 -0800396 oat_class->method_offsets_[class_def_method_index]
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700397 = OatMethodOffsets(code_offset,
398 frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700399 core_spill_mask,
400 fp_spill_mask,
401 mapping_table_offset,
402 vmap_table_offset,
Jeff Hao74180ca2013-03-27 15:29:11 -0700403 gc_map_offset
Logan Chienccb7bf12012-03-28 12:52:32 +0800404 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700405
Ian Rogers1212a022013-03-04 10:48:41 -0800406 if (compiler_driver_->IsImage()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700407 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800408 mirror::DexCache* dex_cache = linker->FindDexCache(*dex_file);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700409 // Unchecked as we hold mutator_lock_ on entry.
410 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800411 mirror::AbstractMethod* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800412 NULL, NULL, invoke_type);
Ian Rogers0571d352011-11-03 19:51:38 -0700413 CHECK(method != NULL);
414 method->SetFrameSizeInBytes(frame_size_in_bytes);
415 method->SetCoreSpillMask(core_spill_mask);
416 method->SetFpSpillMask(fp_spill_mask);
417 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800418 // Don't overwrite static method trampoline
419 if (!method->IsStatic() || method->IsConstructor() ||
420 method->GetDeclaringClass()->IsInitialized()) {
421 method->SetOatCodeOffset(code_offset);
422 } else {
Jeff Haoaa4a7932013-05-13 11:28:27 -0700423 method->SetEntryPointFromCompiledCode(NULL);
Ian Rogers19846512012-02-24 11:42:47 -0800424 }
Ian Rogers0571d352011-11-03 19:51:38 -0700425 method->SetOatVmapTableOffset(vmap_table_offset);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700426 method->SetOatNativeGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700427 }
Logan Chien8b977d32012-02-21 19:14:55 +0800428
Brian Carlstrome24fa612011-09-29 00:53:55 -0700429 return offset;
430}
431
Brian Carlstrom265091e2013-01-30 14:08:26 -0800432#define DCHECK_OFFSET() \
433 DCHECK_EQ(static_cast<off_t>(offset), out.Seek(0, kSeekCurrent))
434
435#define DCHECK_OFFSET_() \
436 DCHECK_EQ(static_cast<off_t>(offset_), out.Seek(0, kSeekCurrent))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700437
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800438bool OatWriter::Write(OutputStream& out) {
439 if (!out.WriteFully(oat_header_, sizeof(*oat_header_))) {
440 PLOG(ERROR) << "Failed to write oat header to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700441 return false;
442 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700443 size_oat_header_ += sizeof(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700444
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800445 if (!out.WriteFully(image_file_location_.data(), image_file_location_.size())) {
446 PLOG(ERROR) << "Failed to write oat header image file location to " << out.GetLocation();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700447 return false;
448 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700449 size_oat_header_image_file_location_ += image_file_location_.size();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700450
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800451 if (!WriteTables(out)) {
452 LOG(ERROR) << "Failed to write oat tables to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700453 return false;
454 }
455
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800456 size_t code_offset = WriteCode(out);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700457 if (code_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800458 LOG(ERROR) << "Failed to write oat code to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700459 return false;
460 }
461
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800462 code_offset = WriteCodeDexFiles(out, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700463 if (code_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800464 LOG(ERROR) << "Failed to write oat code for dex files to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700465 return false;
466 }
467
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700468 LOG(INFO) << "size_dex_file_alignment_=" << size_dex_file_alignment_;
469 LOG(INFO) << "size_executable_offset_alignment_=" << size_executable_offset_alignment_;
470 LOG(INFO) << "size_oat_header_=" << size_oat_header_;
471 LOG(INFO) << "size_oat_header_image_file_location_=" << size_oat_header_image_file_location_;
472 LOG(INFO) << "size_dex_file_=" << size_dex_file_;
473 LOG(INFO) << "size_interpreter_to_interpreter_entry_=" << size_interpreter_to_interpreter_entry_;
474 LOG(INFO) << "size_interpreter_to_quick_entry_=" << size_interpreter_to_quick_entry_;
475 LOG(INFO) << "size_portable_resolution_trampoline_=" << size_portable_resolution_trampoline_;
476 LOG(INFO) << "size_quick_resolution_trampoline_=" << size_quick_resolution_trampoline_;
477 LOG(INFO) << "size_stubs_alignment_=" << size_stubs_alignment_;
478 LOG(INFO) << "size_code_size_=" << size_code_size_;
479 LOG(INFO) << "size_code_=" << size_code_;
480 LOG(INFO) << "size_code_alignment_=" << size_code_alignment_;
481 LOG(INFO) << "size_mapping_table_=" << size_mapping_table_;
482 LOG(INFO) << "size_vmap_table_=" << size_vmap_table_;
483 LOG(INFO) << "size_gc_map_=" << size_gc_map_;
484 LOG(INFO) << "size_oat_dex_file_location_size_=" << size_oat_dex_file_location_size_;
485 LOG(INFO) << "size_oat_dex_file_location_data=" << size_oat_dex_file_location_data_;
486 LOG(INFO) << "size_oat_dex_file_location_checksum_=" << size_oat_dex_file_location_checksum_;
487 LOG(INFO) << "size_oat_dex_file_offset_=" << size_oat_dex_file_offset_;
488 LOG(INFO) << "size_oat_dex_file_methods_offsets_=" << size_oat_dex_file_methods_offsets_;
489 LOG(INFO) << "size_oat_class_status_=" << size_oat_class_status_;
490 LOG(INFO) << "size_oat_class_method_offsets=" << size_oat_class_method_offsets_;
491
492 uint32_t size_total =
493 size_dex_file_alignment_ +
494 size_executable_offset_alignment_ +
495 size_oat_header_ +
496 size_oat_header_image_file_location_ +
497 size_dex_file_ +
498 size_interpreter_to_interpreter_entry_ +
499 size_interpreter_to_quick_entry_ +
500 size_portable_resolution_trampoline_ +
501 size_quick_resolution_trampoline_ +
502 size_stubs_alignment_ +
503 size_code_size_ +
504 size_code_ +
505 size_code_alignment_ +
506 size_mapping_table_ +
507 size_vmap_table_ +
508 size_gc_map_ +
509 size_oat_dex_file_location_size_ +
510 size_oat_dex_file_location_data_ +
511 size_oat_dex_file_location_checksum_ +
512 size_oat_dex_file_offset_ +
513 size_oat_dex_file_methods_offsets_ +
514 size_oat_class_status_ +
515 size_oat_class_method_offsets_;
516
517 LOG(INFO) << "size_total=" << size_total;
518 DCHECK_EQ(size_total, static_cast<uint32_t>(out.Seek(0, kSeekCurrent)));
519
Brian Carlstrome24fa612011-09-29 00:53:55 -0700520 return true;
521}
522
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800523bool OatWriter::WriteTables(OutputStream& out) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700524 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700525 if (!oat_dex_files_[i]->Write(this, out)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800526 PLOG(ERROR) << "Failed to write oat dex information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700527 return false;
528 }
529 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800530 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
531 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800532 off_t actual_offset = out.Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -0800533 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
534 const DexFile* dex_file = (*dex_files_)[i];
535 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
536 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
537 return false;
538 }
539 const DexFile* dex_file = (*dex_files_)[i];
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800540 if (!out.WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
541 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800542 return false;
543 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700544 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800545 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800546 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700547 if (!oat_classes_[i]->Write(this, out)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800548 PLOG(ERROR) << "Failed to write oat methods information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700549 return false;
550 }
551 }
552 return true;
553}
554
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800555size_t OatWriter::WriteCode(OutputStream& out) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800556 uint32_t offset = oat_header_->GetExecutableOffset();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700557 off_t new_offset = out.Seek(size_executable_offset_alignment_, kSeekCurrent);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800558 if (static_cast<uint32_t>(new_offset) != offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700559 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Brian Carlstrom265091e2013-01-30 14:08:26 -0800560 << " Expected: " << offset << " File: " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700561 return 0;
562 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800563 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700564 if (compiler_driver_->IsImage()) {
565 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
566 if (!out.WriteFully(&(*interpreter_to_interpreter_entry_)[0], interpreter_to_interpreter_entry_->size())) {
567 PLOG(ERROR) << "Failed to write interpreter to interpreter entry to " << out.GetLocation();
568 return false;
569 }
570 size_interpreter_to_interpreter_entry_ += interpreter_to_interpreter_entry_->size();
571 offset += interpreter_to_interpreter_entry_->size();
572 DCHECK_OFFSET();
573
574 uint32_t aligned_offset = CompiledCode::AlignCode(offset, instruction_set);
575 uint32_t alignment_padding = aligned_offset - offset;
576 out.Seek(alignment_padding, kSeekCurrent);
577 size_stubs_alignment_ += alignment_padding;
578 if (!out.WriteFully(&(*interpreter_to_quick_entry_)[0], interpreter_to_quick_entry_->size())) {
579 PLOG(ERROR) << "Failed to write interpreter to quick entry to " << out.GetLocation();
580 return false;
581 }
582 size_interpreter_to_quick_entry_ += interpreter_to_quick_entry_->size();
583 offset += alignment_padding + interpreter_to_quick_entry_->size();
584 DCHECK_OFFSET();
585
586 aligned_offset = CompiledCode::AlignCode(offset, instruction_set);
587 alignment_padding = aligned_offset - offset;
588 out.Seek(alignment_padding, kSeekCurrent);
589 size_stubs_alignment_ += alignment_padding;
590 if (!out.WriteFully(&(*portable_resolution_trampoline_)[0], portable_resolution_trampoline_->size())) {
591 PLOG(ERROR) << "Failed to write portable resolution trampoline to " << out.GetLocation();
592 return false;
593 }
594 size_portable_resolution_trampoline_ += portable_resolution_trampoline_->size();
595 offset += alignment_padding + portable_resolution_trampoline_->size();
596 DCHECK_OFFSET();
597
598 aligned_offset = CompiledCode::AlignCode(offset, instruction_set);
599 alignment_padding = aligned_offset - offset;
600 out.Seek(alignment_padding, kSeekCurrent);
601 size_stubs_alignment_ += alignment_padding;
602 if (!out.WriteFully(&(*quick_resolution_trampoline_)[0], quick_resolution_trampoline_->size())) {
603 PLOG(ERROR) << "Failed to write quick resolution trampoline to " << out.GetLocation();
604 return false;
605 }
606 size_quick_resolution_trampoline_ += quick_resolution_trampoline_->size();
607 offset += alignment_padding + quick_resolution_trampoline_->size();
608 DCHECK_OFFSET();
609 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800610 return offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700611}
612
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800613size_t OatWriter::WriteCodeDexFiles(OutputStream& out, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700614 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800615 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700616 const DexFile* dex_file = (*dex_files_)[i];
617 CHECK(dex_file != NULL);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800618 code_offset = WriteCodeDexFile(out, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700619 if (code_offset == 0) {
620 return 0;
621 }
622 }
623 return code_offset;
624}
625
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800626size_t OatWriter::WriteCodeDexFile(OutputStream& out, size_t code_offset, size_t& oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700627 const DexFile& dex_file) {
628 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
629 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700630 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800631 code_offset = WriteCodeClassDef(out, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700632 if (code_offset == 0) {
633 return 0;
634 }
635 }
636 return code_offset;
637}
638
Ian Rogers0571d352011-11-03 19:51:38 -0700639void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800640 const DexFile& dex_file, OutputStream& out) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700641 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800642 << " to " << out.GetLocation();
Elliott Hughes234da572011-11-03 22:13:06 -0700643}
644
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800645size_t OatWriter::WriteCodeClassDef(OutputStream& out,
Ian Rogers0571d352011-11-03 19:51:38 -0700646 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700647 const DexFile& dex_file,
648 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700649 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700650 if (class_data == NULL) {
651 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700652 return code_offset;
653 }
Ian Rogers0571d352011-11-03 19:51:38 -0700654 ClassDataItemIterator it(dex_file, class_data);
655 // Skip fields
656 while (it.HasNextStaticField()) {
657 it.Next();
658 }
659 while (it.HasNextInstanceField()) {
660 it.Next();
661 }
662 // Process methods
663 size_t class_def_method_index = 0;
664 while (it.HasNextDirectMethod()) {
665 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800666 code_offset = WriteCodeMethod(out, code_offset, oat_class_index, class_def_method_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700667 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700668 if (code_offset == 0) {
669 return 0;
670 }
Ian Rogers0571d352011-11-03 19:51:38 -0700671 class_def_method_index++;
672 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700673 }
Ian Rogers0571d352011-11-03 19:51:38 -0700674 while (it.HasNextVirtualMethod()) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800675 code_offset = WriteCodeMethod(out, code_offset, oat_class_index, class_def_method_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700676 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700677 if (code_offset == 0) {
678 return 0;
679 }
Ian Rogers0571d352011-11-03 19:51:38 -0700680 class_def_method_index++;
681 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700682 }
683 return code_offset;
684}
685
Brian Carlstrom265091e2013-01-30 14:08:26 -0800686size_t OatWriter::WriteCodeMethod(OutputStream& out, size_t offset, size_t oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700687 size_t class_def_method_index, bool is_static,
688 uint32_t method_idx, const DexFile& dex_file) {
689 const CompiledMethod* compiled_method =
Ian Rogers1212a022013-03-04 10:48:41 -0800690 compiler_driver_->GetCompiledMethod(CompilerDriver::MethodReference(&dex_file, method_idx));
Ian Rogers0571d352011-11-03 19:51:38 -0700691
Ian Rogers0571d352011-11-03 19:51:38 -0700692 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800693 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700694
695
696 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstrom265091e2013-01-30 14:08:26 -0800697#if !defined(ART_USE_PORTABLE_COMPILER)
698 uint32_t aligned_offset = compiled_method->AlignCode(offset);
699 uint32_t aligned_code_delta = aligned_offset - offset;
Logan Chien971bf3f2012-05-01 15:47:55 +0800700 if (aligned_code_delta != 0) {
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800701 off_t new_offset = out.Seek(aligned_code_delta, kSeekCurrent);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700702 size_code_alignment_ += aligned_code_delta;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800703 if (static_cast<uint32_t>(new_offset) != aligned_offset) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800704 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Brian Carlstrom265091e2013-01-30 14:08:26 -0800705 << " Expected: " << aligned_offset << " File: " << out.GetLocation();
Logan Chien971bf3f2012-05-01 15:47:55 +0800706 return 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700707 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800708 offset += aligned_code_delta;
709 DCHECK_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700710 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800711 DCHECK_ALIGNED(offset, kArmAlignment);
Logan Chien971bf3f2012-05-01 15:47:55 +0800712 const std::vector<uint8_t>& code = compiled_method->GetCode();
713 uint32_t code_size = code.size() * sizeof(code[0]);
714 CHECK_NE(code_size, 0U);
715
716 // Deduplicate code arrays
Brian Carlstrom265091e2013-01-30 14:08:26 -0800717 size_t code_offset = offset + sizeof(code_size) + compiled_method->CodeDelta();
Logan Chien971bf3f2012-05-01 15:47:55 +0800718 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800719 if (code_iter != code_offsets_.end() && code_offset != method_offsets.code_offset_) {
720 DCHECK(code_iter->second == method_offsets.code_offset_)
721 << PrettyMethod(method_idx, dex_file);
Logan Chien971bf3f2012-05-01 15:47:55 +0800722 } else {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800723 DCHECK(code_offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800724 if (!out.WriteFully(&code_size, sizeof(code_size))) {
725 ReportWriteFailure("method code size", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800726 return 0;
727 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700728 size_code_size_ += sizeof(code_size);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800729 offset += sizeof(code_size);
730 DCHECK_OFFSET();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800731 if (!out.WriteFully(&code[0], code_size)) {
732 ReportWriteFailure("method code", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800733 return 0;
734 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700735 size_code_ += code_size;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800736 offset += code_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800737 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800738 DCHECK_OFFSET();
739#endif
Logan Chien971bf3f2012-05-01 15:47:55 +0800740
741 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
742 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
743
744 // Deduplicate mapping tables
745 SafeMap<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
746 mapping_table_offsets_.find(&mapping_table);
747 if (mapping_iter != mapping_table_offsets_.end() &&
Brian Carlstrom265091e2013-01-30 14:08:26 -0800748 offset != method_offsets.mapping_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800749 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
750 || mapping_iter->second == method_offsets.mapping_table_offset_)
751 << PrettyMethod(method_idx, dex_file);
752 } else {
753 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800754 || offset == method_offsets.mapping_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800755 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800756 if (!out.WriteFully(&mapping_table[0], mapping_table_size)) {
757 ReportWriteFailure("mapping table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800758 return 0;
759 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700760 size_mapping_table_ += mapping_table_size;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800761 offset += mapping_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800762 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800763 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800764
765 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
766 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
767
768 // Deduplicate vmap tables
769 SafeMap<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
770 vmap_table_offsets_.find(&vmap_table);
771 if (vmap_iter != vmap_table_offsets_.end() &&
Brian Carlstrom265091e2013-01-30 14:08:26 -0800772 offset != method_offsets.vmap_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800773 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
774 || vmap_iter->second == method_offsets.vmap_table_offset_)
775 << PrettyMethod(method_idx, dex_file);
776 } else {
777 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800778 || offset == method_offsets.vmap_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800779 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800780 if (!out.WriteFully(&vmap_table[0], vmap_table_size)) {
781 ReportWriteFailure("vmap table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800782 return 0;
783 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700784 size_vmap_table_ += vmap_table_size;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800785 offset += vmap_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800786 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800787 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800788
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700789 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800790 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
791
792 // Deduplicate GC maps
793 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
794 gc_map_offsets_.find(&gc_map);
795 if (gc_map_iter != gc_map_offsets_.end() &&
Brian Carlstrom265091e2013-01-30 14:08:26 -0800796 offset != method_offsets.gc_map_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800797 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
798 || gc_map_iter->second == method_offsets.gc_map_offset_)
799 << PrettyMethod(method_idx, dex_file);
800 } else {
801 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800802 || offset == method_offsets.gc_map_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800803 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800804 if (!out.WriteFully(&gc_map[0], gc_map_size)) {
805 ReportWriteFailure("GC map", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800806 return 0;
807 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700808 size_gc_map_ += gc_map_size;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800809 offset += gc_map_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800810 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800811 DCHECK_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700812 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800813
Brian Carlstrom265091e2013-01-30 14:08:26 -0800814 return offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700815}
816
Brian Carlstrom265091e2013-01-30 14:08:26 -0800817OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
818 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -0800819 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700820 dex_file_location_size_ = location.size();
821 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800822 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800823 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800824 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700825}
826
827size_t OatWriter::OatDexFile::SizeOf() const {
828 return sizeof(dex_file_location_size_)
829 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800830 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800831 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800832 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700833}
834
835void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
836 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
837 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800838 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800839 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800840 oat_header.UpdateChecksum(&methods_offsets_[0],
841 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700842}
843
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700844bool OatWriter::OatDexFile::Write(OatWriter* oat_writer, OutputStream& out) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800845 DCHECK_OFFSET_();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800846 if (!out.WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
847 PLOG(ERROR) << "Failed to write dex file location length to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700848 return false;
849 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700850 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800851 if (!out.WriteFully(dex_file_location_data_, dex_file_location_size_)) {
852 PLOG(ERROR) << "Failed to write dex file location data to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700853 return false;
854 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700855 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800856 if (!out.WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
857 PLOG(ERROR) << "Failed to write dex file location checksum to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700858 return false;
859 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700860 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800861 if (!out.WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
862 PLOG(ERROR) << "Failed to write dex file offset to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800863 return false;
864 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700865 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800866 if (!out.WriteFully(&methods_offsets_[0],
867 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
868 PLOG(ERROR) << "Failed to write methods offsets to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700869 return false;
870 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700871 oat_writer->size_oat_dex_file_methods_offsets_ +=
872 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700873 return true;
874}
875
Brian Carlstrom265091e2013-01-30 14:08:26 -0800876OatWriter::OatClass::OatClass(size_t offset, mirror::Class::Status status, uint32_t methods_count) {
877 offset_ = offset;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800878 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700879 method_offsets_.resize(methods_count);
880}
881
Brian Carlstrom265091e2013-01-30 14:08:26 -0800882size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
883 size_t class_def_method_index_) const {
884 return offset_ + GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
885}
886
887size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
888 size_t class_def_method_index_) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800889 return sizeof(status_)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800890 + (sizeof(method_offsets_[0]) * class_def_method_index_);
891}
892
893size_t OatWriter::OatClass::SizeOf() const {
894 return GetOatMethodOffsetsOffsetFromOatClass(method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700895}
896
Brian Carlstrom389efb02012-01-11 12:06:26 -0800897void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800898 oat_header.UpdateChecksum(&status_, sizeof(status_));
899 oat_header.UpdateChecksum(&method_offsets_[0],
900 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700901}
902
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700903bool OatWriter::OatClass::Write(OatWriter* oat_writer, OutputStream& out) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800904 DCHECK_OFFSET_();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800905 if (!out.WriteFully(&status_, sizeof(status_))) {
906 PLOG(ERROR) << "Failed to write class status to " << out.GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800907 return false;
908 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700909 oat_writer->size_oat_class_status_ += sizeof(status_);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800910 DCHECK_EQ(static_cast<off_t>(GetOatMethodOffsetsOffsetFromOatHeader(0)),
911 out.Seek(0, kSeekCurrent));
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800912 if (!out.WriteFully(&method_offsets_[0],
913 sizeof(method_offsets_[0]) * method_offsets_.size())) {
914 PLOG(ERROR) << "Failed to write method offsets to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700915 return false;
916 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700917 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrom265091e2013-01-30 14:08:26 -0800918 DCHECK_EQ(static_cast<off_t>(GetOatMethodOffsetsOffsetFromOatHeader(method_offsets_.size())),
919 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