blob: a400bdde6cd9b81d16a976fcf16d20f795735596 [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
Brian Carlstromba150c32013-08-27 17:31:03 -070021#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080022#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080023#include "base/unix_file/fd_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070024#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000026#include "dex/verification_results.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070027#include "gc/space/space.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070028#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/array.h"
30#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070031#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070032#include "os.h"
Brian Carlstromcd60ac72013-01-20 17:09:51 -080033#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070034#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070035#include "scoped_thread_state_change.h"
jeffhaoec014232012-09-05 10:42:25 -070036#include "verifier/method_verifier.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070037
38namespace art {
39
Brian Carlstrom3320cf42011-10-04 14:58:28 -070040OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070041 uint32_t image_file_location_oat_checksum,
Ian Rogersef7d42f2014-01-06 12:55:46 -080042 uintptr_t image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070043 const std::string& image_file_location,
Ian Rogersca368cb2013-11-15 15:52:08 -080044 const CompilerDriver* compiler,
45 TimingLogger* timings)
Jeff Hao0aba0ba2013-06-03 14:49:28 -070046 : compiler_driver_(compiler),
47 dex_files_(&dex_files),
48 image_file_location_oat_checksum_(image_file_location_oat_checksum),
49 image_file_location_oat_begin_(image_file_location_oat_begin),
50 image_file_location_(image_file_location),
51 oat_header_(NULL),
52 size_dex_file_alignment_(0),
53 size_executable_offset_alignment_(0),
54 size_oat_header_(0),
55 size_oat_header_image_file_location_(0),
56 size_dex_file_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070057 size_interpreter_to_interpreter_bridge_(0),
58 size_interpreter_to_compiled_code_bridge_(0),
59 size_jni_dlsym_lookup_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070060 size_portable_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070061 size_portable_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070062 size_portable_to_interpreter_bridge_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070063 size_quick_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070064 size_quick_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070065 size_quick_to_interpreter_bridge_(0),
66 size_trampoline_alignment_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070067 size_code_size_(0),
68 size_code_(0),
69 size_code_alignment_(0),
70 size_mapping_table_(0),
71 size_vmap_table_(0),
72 size_gc_map_(0),
73 size_oat_dex_file_location_size_(0),
74 size_oat_dex_file_location_data_(0),
75 size_oat_dex_file_location_checksum_(0),
76 size_oat_dex_file_offset_(0),
77 size_oat_dex_file_methods_offsets_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -070078 size_oat_class_type_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070079 size_oat_class_status_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -070080 size_oat_class_method_bitmaps_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070081 size_oat_class_method_offsets_(0) {
Ian Rogersca368cb2013-11-15 15:52:08 -080082 size_t offset;
83 {
84 TimingLogger::ScopedSplit split("InitOatHeader", timings);
85 offset = InitOatHeader();
86 }
87 {
88 TimingLogger::ScopedSplit split("InitOatDexFiles", timings);
89 offset = InitOatDexFiles(offset);
90 }
91 {
92 TimingLogger::ScopedSplit split("InitDexFiles", timings);
93 offset = InitDexFiles(offset);
94 }
95 {
96 TimingLogger::ScopedSplit split("InitOatClasses", timings);
97 offset = InitOatClasses(offset);
98 }
99 {
100 TimingLogger::ScopedSplit split("InitOatCode", timings);
101 offset = InitOatCode(offset);
102 }
103 {
104 TimingLogger::ScopedSplit split("InitOatCodeDexFiles", timings);
105 offset = InitOatCodeDexFiles(offset);
106 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700107 size_ = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700108
109 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700110 CHECK(image_file_location.empty() == compiler->IsImage());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700111}
112
Ian Rogers0571d352011-11-03 19:51:38 -0700113OatWriter::~OatWriter() {
114 delete oat_header_;
115 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800116 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -0700117}
118
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700119size_t OatWriter::InitOatHeader() {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700120 // create the OatHeader
Ian Rogers1212a022013-03-04 10:48:41 -0800121 oat_header_ = new OatHeader(compiler_driver_->GetInstructionSet(),
Dave Allison70202782013-10-22 17:52:19 -0700122 compiler_driver_->GetInstructionSetFeatures(),
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700123 dex_files_,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700124 image_file_location_oat_checksum_,
125 image_file_location_oat_begin_,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700126 image_file_location_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700127 size_t offset = sizeof(*oat_header_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700128 offset += image_file_location_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700129 return offset;
130}
131
132size_t OatWriter::InitOatDexFiles(size_t offset) {
133 // create the OatDexFiles
134 for (size_t i = 0; i != dex_files_->size(); ++i) {
135 const DexFile* dex_file = (*dex_files_)[i];
136 CHECK(dex_file != NULL);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800137 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700138 oat_dex_files_.push_back(oat_dex_file);
139 offset += oat_dex_file->SizeOf();
140 }
141 return offset;
142}
143
Brian Carlstrom89521892011-12-07 22:05:07 -0800144size_t OatWriter::InitDexFiles(size_t offset) {
145 // calculate the offsets within OatDexFiles to the DexFiles
146 for (size_t i = 0; i != dex_files_->size(); ++i) {
147 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700148 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800149 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700150 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800151
152 // set offset in OatDexFile to DexFile
153 oat_dex_files_[i]->dex_file_offset_ = offset;
154
155 const DexFile* dex_file = (*dex_files_)[i];
156 offset += dex_file->GetHeader().file_size_;
157 }
158 return offset;
159}
160
Brian Carlstrom389efb02012-01-11 12:06:26 -0800161size_t OatWriter::InitOatClasses(size_t offset) {
162 // create the OatClasses
163 // calculate the offsets within OatDexFiles to OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -0700164 for (size_t i = 0; i != dex_files_->size(); ++i) {
165 const DexFile* dex_file = (*dex_files_)[i];
166 for (size_t class_def_index = 0;
167 class_def_index < dex_file->NumClassDefs();
Ian Rogersc20a83e2012-01-18 18:15:32 -0800168 class_def_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800169 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700170 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
171 const byte* class_data = dex_file->GetClassData(class_def);
Brian Carlstromba150c32013-08-27 17:31:03 -0700172 uint32_t num_non_null_compiled_methods = 0;
173 UniquePtr<std::vector<CompiledMethod*> > compiled_methods(new std::vector<CompiledMethod*>());
Ian Rogers0571d352011-11-03 19:51:38 -0700174 if (class_data != NULL) { // ie not an empty class, such as a marker interface
175 ClassDataItemIterator it(*dex_file, class_data);
176 size_t num_direct_methods = it.NumDirectMethods();
177 size_t num_virtual_methods = it.NumVirtualMethods();
Brian Carlstromba150c32013-08-27 17:31:03 -0700178 size_t num_methods = num_direct_methods + num_virtual_methods;
179
180 // Fill in the compiled_methods_ array for methods that have a
181 // CompiledMethod. We track the number of non-null entries in
182 // num_non_null_compiled_methods since we only want to allocate
183 // OatMethodOffsets for the compiled methods.
184 compiled_methods->reserve(num_methods);
185 while (it.HasNextStaticField()) {
186 it.Next();
187 }
188 while (it.HasNextInstanceField()) {
189 it.Next();
190 }
191 size_t class_def_method_index = 0;
192 while (it.HasNextDirectMethod()) {
193 uint32_t method_idx = it.GetMemberIndex();
194 CompiledMethod* compiled_method =
195 compiler_driver_->GetCompiledMethod(MethodReference(dex_file, method_idx));
196 compiled_methods->push_back(compiled_method);
197 if (compiled_method != NULL) {
198 num_non_null_compiled_methods++;
199 }
200 class_def_method_index++;
201 it.Next();
202 }
203 while (it.HasNextVirtualMethod()) {
204 uint32_t method_idx = it.GetMemberIndex();
205 CompiledMethod* compiled_method =
206 compiler_driver_->GetCompiledMethod(MethodReference(dex_file, method_idx));
207 compiled_methods->push_back(compiled_method);
208 if (compiled_method != NULL) {
209 num_non_null_compiled_methods++;
210 }
211 class_def_method_index++;
212 it.Next();
213 }
Ian Rogers0571d352011-11-03 19:51:38 -0700214 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800215
Brian Carlstrom51c24672013-07-11 16:00:56 -0700216 ClassReference class_ref(dex_file, class_def_index);
Ian Rogers1212a022013-03-04 10:48:41 -0800217 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 mirror::Class::Status status;
jeffhaoec014232012-09-05 10:42:25 -0700219 if (compiled_class != NULL) {
220 status = compiled_class->GetStatus();
Vladimir Markoc7f83202014-01-24 17:55:18 +0000221 } else if (compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800222 status = mirror::Class::kStatusError;
jeffhaoec014232012-09-05 10:42:25 -0700223 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224 status = mirror::Class::kStatusNotReady;
jeffhaoec014232012-09-05 10:42:25 -0700225 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800226
Brian Carlstromba150c32013-08-27 17:31:03 -0700227 OatClass* oat_class = new OatClass(offset, compiled_methods.release(),
228 num_non_null_compiled_methods, status);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800229 oat_classes_.push_back(oat_class);
230 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700231 }
Ian Rogers3d504072014-03-01 09:16:49 -0800232 oat_dex_files_[i]->UpdateChecksum(oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700233 }
234 return offset;
235}
236
237size_t OatWriter::InitOatCode(size_t offset) {
238 // calculate the offsets within OatHeader to executable code
239 size_t old_offset = offset;
240 // required to be on a new page boundary
241 offset = RoundUp(offset, kPageSize);
242 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700243 size_executable_offset_alignment_ = offset - old_offset;
244 if (compiler_driver_->IsImage()) {
245 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700246
Ian Rogers848871b2013-08-05 10:56:33 -0700247 #define DO_TRAMPOLINE(field, fn_name) \
248 offset = CompiledCode::AlignCode(offset, instruction_set); \
249 oat_header_->Set ## fn_name ## Offset(offset); \
250 field.reset(compiler_driver_->Create ## fn_name()); \
251 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700252
Ian Rogers848871b2013-08-05 10:56:33 -0700253 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge);
254 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge);
255 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Jeff Hao88474b42013-10-23 16:24:40 -0700256 DO_TRAMPOLINE(portable_imt_conflict_trampoline_, PortableImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700257 DO_TRAMPOLINE(portable_resolution_trampoline_, PortableResolutionTrampoline);
258 DO_TRAMPOLINE(portable_to_interpreter_bridge_, PortableToInterpreterBridge);
Jeff Hao88474b42013-10-23 16:24:40 -0700259 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700260 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
261 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700262
Ian Rogers848871b2013-08-05 10:56:33 -0700263 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700264 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700265 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
266 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
267 oat_header_->SetJniDlsymLookupOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700268 oat_header_->SetPortableImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700269 oat_header_->SetPortableResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700270 oat_header_->SetPortableToInterpreterBridgeOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700271 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700272 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700273 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700274 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700275 return offset;
276}
277
278size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700279 size_t oat_class_index = 0;
280 for (size_t i = 0; i != dex_files_->size(); ++i) {
281 const DexFile* dex_file = (*dex_files_)[i];
282 CHECK(dex_file != NULL);
Brian Carlstromba150c32013-08-27 17:31:03 -0700283 offset = InitOatCodeDexFile(offset, &oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700284 }
285 return offset;
286}
287
288size_t OatWriter::InitOatCodeDexFile(size_t offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700289 size_t* oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700290 const DexFile& dex_file) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800291 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700292 class_def_index < dex_file.NumClassDefs();
Brian Carlstromba150c32013-08-27 17:31:03 -0700293 class_def_index++, (*oat_class_index)++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700294 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstromba150c32013-08-27 17:31:03 -0700295 offset = InitOatCodeClassDef(offset, *oat_class_index, class_def_index, dex_file, class_def);
Ian Rogers3d504072014-03-01 09:16:49 -0800296 oat_classes_[*oat_class_index]->UpdateChecksum(oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700297 }
298 return offset;
299}
300
301size_t OatWriter::InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800302 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700303 const DexFile& dex_file,
304 const DexFile::ClassDef& class_def) {
305 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700306 if (class_data == NULL) {
307 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700308 return offset;
309 }
Ian Rogers0571d352011-11-03 19:51:38 -0700310 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstromba150c32013-08-27 17:31:03 -0700311 CHECK_LE(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700312 it.NumDirectMethods() + it.NumVirtualMethods());
313 // Skip fields
314 while (it.HasNextStaticField()) {
315 it.Next();
316 }
317 while (it.HasNextInstanceField()) {
318 it.Next();
319 }
320 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700321 size_t class_def_method_index = 0;
Brian Carlstromba150c32013-08-27 17:31:03 -0700322 size_t method_offsets_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700323 while (it.HasNextDirectMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800324 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700325 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700326 &method_offsets_index, is_native,
327 it.GetMethodInvokeType(class_def), it.GetMemberIndex(), dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700328 class_def_method_index++;
329 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700330 }
Ian Rogers0571d352011-11-03 19:51:38 -0700331 while (it.HasNextVirtualMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800332 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700333 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700334 &method_offsets_index, is_native,
335 it.GetMethodInvokeType(class_def), it.GetMemberIndex(), dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700336 class_def_method_index++;
337 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700338 }
Ian Rogers0571d352011-11-03 19:51:38 -0700339 DCHECK(!it.HasNext());
Brian Carlstromba150c32013-08-27 17:31:03 -0700340 CHECK_LE(method_offsets_index, class_def_method_index);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700341 return offset;
342}
343
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700344size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
345 size_t __attribute__((unused)) class_def_index,
346 size_t class_def_method_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700347 size_t* method_offsets_index,
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700348 bool __attribute__((unused)) is_native,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800349 InvokeType invoke_type,
Brian Carlstromba150c32013-08-27 17:31:03 -0700350 uint32_t method_idx, const DexFile& dex_file) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800351 // Derived from CompiledMethod if available.
352 uint32_t quick_code_offset = 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700353 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700354 uint32_t core_spill_mask = 0;
355 uint32_t fp_spill_mask = 0;
356 uint32_t mapping_table_offset = 0;
357 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800358 uint32_t gc_map_offset = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700359
Brian Carlstrom265091e2013-01-30 14:08:26 -0800360 OatClass* oat_class = oat_classes_[oat_class_index];
Brian Carlstromba150c32013-08-27 17:31:03 -0700361 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
Logan Chien971bf3f2012-05-01 15:47:55 +0800362
Ian Rogersef7d42f2014-01-06 12:55:46 -0800363 if (compiled_method != NULL) {
364 const std::vector<uint8_t>* portable_code = compiled_method->GetPortableCode();
365 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode();
366 if (portable_code != nullptr) {
367 CHECK(quick_code == nullptr);
368 size_t oat_method_offsets_offset =
369 oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index);
370 compiled_method->AddOatdataOffsetToCompliledCodeOffset(
371 oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_));
jeffhao55d78212011-11-02 11:41:50 -0700372 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800373 CHECK(quick_code != nullptr);
374 offset = compiled_method->AlignCode(offset);
375 DCHECK_ALIGNED(offset, kArmAlignment);
376 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
377 CHECK_NE(code_size, 0U);
378 uint32_t thumb_offset = compiled_method->CodeDelta();
379 quick_code_offset = offset + sizeof(code_size) + thumb_offset;
380
Mark Mendellae9fd932014-02-10 16:14:35 -0800381 std::vector<uint8_t>* cfi_info = compiler_driver_->GetCallFrameInformation();
382 if (cfi_info != nullptr) {
383 // Copy in the FDE, if present
384 const std::vector<uint8_t>* fde = compiled_method->GetCFIInfo();
385 if (fde != nullptr) {
386 // Copy the information into cfi_info and then fix the address in the new copy.
387 int cur_offset = cfi_info->size();
388 cfi_info->insert(cfi_info->end(), fde->begin(), fde->end());
389
390 // Set the 'initial_location' field to address the start of the method.
391 uint32_t new_value = quick_code_offset - oat_header_->GetExecutableOffset();
392 uint32_t offset_to_update = cur_offset + 2*sizeof(uint32_t);
393 (*cfi_info)[offset_to_update+0] = new_value;
394 (*cfi_info)[offset_to_update+1] = new_value >> 8;
395 (*cfi_info)[offset_to_update+2] = new_value >> 16;
396 (*cfi_info)[offset_to_update+3] = new_value >> 24;
397 method_info_.push_back(DebugInfo(PrettyMethod(class_def_method_index, dex_file, false),
398 new_value, new_value + code_size));
399 }
400 }
401
Ian Rogersef7d42f2014-01-06 12:55:46 -0800402 // Deduplicate code arrays
403 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter =
404 code_offsets_.find(quick_code);
405 if (code_iter != code_offsets_.end()) {
406 quick_code_offset = code_iter->second;
407 } else {
408 code_offsets_.Put(quick_code, quick_code_offset);
409 offset += sizeof(code_size); // code size is prepended before code
410 offset += code_size;
411 oat_header_->UpdateChecksum(&(*quick_code)[0], code_size);
412 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800413 }
414 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
415 core_spill_mask = compiled_method->GetCoreSpillMask();
416 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700417
Ian Rogers1809a722013-08-09 22:05:32 -0700418 const std::vector<uint8_t>& mapping_table = compiled_method->GetMappingTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800419 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
420 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700421
Logan Chien971bf3f2012-05-01 15:47:55 +0800422 // Deduplicate mapping tables
Ian Rogers1809a722013-08-09 22:05:32 -0700423 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator mapping_iter =
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700424 mapping_table_offsets_.find(&mapping_table);
Logan Chien971bf3f2012-05-01 15:47:55 +0800425 if (mapping_iter != mapping_table_offsets_.end()) {
426 mapping_table_offset = mapping_iter->second;
427 } else {
428 mapping_table_offsets_.Put(&mapping_table, mapping_table_offset);
429 offset += mapping_table_size;
430 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
431 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700432
Ian Rogers1809a722013-08-09 22:05:32 -0700433 const std::vector<uint8_t>& vmap_table = compiled_method->GetVmapTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800434 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
435 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700436
Logan Chien971bf3f2012-05-01 15:47:55 +0800437 // Deduplicate vmap tables
Ian Rogers1809a722013-08-09 22:05:32 -0700438 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator vmap_iter =
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700439 vmap_table_offsets_.find(&vmap_table);
Logan Chien971bf3f2012-05-01 15:47:55 +0800440 if (vmap_iter != vmap_table_offsets_.end()) {
441 vmap_table_offset = vmap_iter->second;
442 } else {
443 vmap_table_offsets_.Put(&vmap_table, vmap_table_offset);
444 offset += vmap_table_size;
445 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
446 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800447
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700448 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800449 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
450 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
Logan Chienccb7bf12012-03-28 12:52:32 +0800451
Mathieu Chartier590fee92013-09-13 13:46:47 -0700452 if (kIsDebugBuild) {
453 // We expect GC maps except when the class hasn't been verified or the method is native
454 ClassReference class_ref(&dex_file, class_def_index);
455 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
456 mirror::Class::Status status;
457 if (compiled_class != NULL) {
458 status = compiled_class->GetStatus();
Vladimir Markoc7f83202014-01-24 17:55:18 +0000459 } else if (compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700460 status = mirror::Class::kStatusError;
461 } else {
462 status = mirror::Class::kStatusNotReady;
463 }
464 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
465 << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
466 << (status < mirror::Class::kStatusVerified) << " " << status << " "
467 << PrettyMethod(method_idx, dex_file);
jeffhaoec014232012-09-05 10:42:25 -0700468 }
Ian Rogersc20a83e2012-01-18 18:15:32 -0800469
Logan Chien971bf3f2012-05-01 15:47:55 +0800470 // Deduplicate GC maps
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700471 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
472 gc_map_offsets_.find(&gc_map);
Logan Chien971bf3f2012-05-01 15:47:55 +0800473 if (gc_map_iter != gc_map_offsets_.end()) {
474 gc_map_offset = gc_map_iter->second;
475 } else {
476 gc_map_offsets_.Put(&gc_map, gc_map_offset);
477 offset += gc_map_size;
478 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800479 }
Brian Carlstromba150c32013-08-27 17:31:03 -0700480
481 oat_class->method_offsets_[*method_offsets_index] =
Ian Rogersef7d42f2014-01-06 12:55:46 -0800482 OatMethodOffsets(quick_code_offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700483 frame_size_in_bytes,
484 core_spill_mask,
485 fp_spill_mask,
486 mapping_table_offset,
487 vmap_table_offset,
488 gc_map_offset);
489 (*method_offsets_index)++;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700490 }
491
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700492
Ian Rogers1212a022013-03-04 10:48:41 -0800493 if (compiler_driver_->IsImage()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700494 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700495 // Unchecked as we hold mutator_lock_ on entry.
496 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700497 SirtRef<mirror::DexCache> dex_cache(soa.Self(), linker->FindDexCache(dex_file));
498 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700499 mirror::ArtMethod* method = linker->ResolveMethod(dex_file, method_idx, dex_cache,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700500 class_loader, nullptr, invoke_type);
Ian Rogers0571d352011-11-03 19:51:38 -0700501 CHECK(method != NULL);
502 method->SetFrameSizeInBytes(frame_size_in_bytes);
503 method->SetCoreSpillMask(core_spill_mask);
504 method->SetFpSpillMask(fp_spill_mask);
505 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800506 // Don't overwrite static method trampoline
507 if (!method->IsStatic() || method->IsConstructor() ||
508 method->GetDeclaringClass()->IsInitialized()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800509 // TODO: record portable code offsets: method->SetPortableOatCodeOffset(portable_code_offset);
510 method->SetQuickOatCodeOffset(quick_code_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800511 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800512 method->SetEntryPointFromPortableCompiledCode(nullptr);
513 method->SetEntryPointFromQuickCompiledCode(nullptr);
Ian Rogers19846512012-02-24 11:42:47 -0800514 }
Ian Rogers0571d352011-11-03 19:51:38 -0700515 method->SetOatVmapTableOffset(vmap_table_offset);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700516 method->SetOatNativeGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700517 }
Logan Chien8b977d32012-02-21 19:14:55 +0800518
Brian Carlstrome24fa612011-09-29 00:53:55 -0700519 return offset;
520}
521
Brian Carlstrom265091e2013-01-30 14:08:26 -0800522#define DCHECK_OFFSET() \
Ian Rogers3d504072014-03-01 09:16:49 -0800523 DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out->Seek(0, kSeekCurrent)) \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700524 << "file_offset=" << file_offset << " relative_offset=" << relative_offset
Brian Carlstrom265091e2013-01-30 14:08:26 -0800525
526#define DCHECK_OFFSET_() \
Ian Rogers3d504072014-03-01 09:16:49 -0800527 DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out->Seek(0, kSeekCurrent)) \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700528 << "file_offset=" << file_offset << " offset_=" << offset_
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700529
Ian Rogers3d504072014-03-01 09:16:49 -0800530bool OatWriter::Write(OutputStream* out) {
531 const size_t file_offset = out->Seek(0, kSeekCurrent);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700532
Ian Rogers3d504072014-03-01 09:16:49 -0800533 if (!out->WriteFully(oat_header_, sizeof(*oat_header_))) {
534 PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700535 return false;
536 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700537 size_oat_header_ += sizeof(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700538
Ian Rogers3d504072014-03-01 09:16:49 -0800539 if (!out->WriteFully(image_file_location_.data(), image_file_location_.size())) {
540 PLOG(ERROR) << "Failed to write oat header image file location to " << out->GetLocation();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700541 return false;
542 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700543 size_oat_header_image_file_location_ += image_file_location_.size();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700544
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700545 if (!WriteTables(out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800546 LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700547 return false;
548 }
549
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700550 size_t relative_offset = WriteCode(out, file_offset);
551 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800552 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700553 return false;
554 }
555
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700556 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
557 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800558 LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700559 return false;
560 }
561
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700562 if (kIsDebugBuild) {
563 uint32_t size_total = 0;
564 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700565 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700566 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700567
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700568 DO_STAT(size_dex_file_alignment_);
569 DO_STAT(size_executable_offset_alignment_);
570 DO_STAT(size_oat_header_);
571 DO_STAT(size_oat_header_image_file_location_);
572 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -0700573 DO_STAT(size_interpreter_to_interpreter_bridge_);
574 DO_STAT(size_interpreter_to_compiled_code_bridge_);
575 DO_STAT(size_jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -0700576 DO_STAT(size_portable_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700577 DO_STAT(size_portable_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700578 DO_STAT(size_portable_to_interpreter_bridge_);
Jeff Hao88474b42013-10-23 16:24:40 -0700579 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700580 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700581 DO_STAT(size_quick_to_interpreter_bridge_);
582 DO_STAT(size_trampoline_alignment_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700583 DO_STAT(size_code_size_);
584 DO_STAT(size_code_);
585 DO_STAT(size_code_alignment_);
586 DO_STAT(size_mapping_table_);
587 DO_STAT(size_vmap_table_);
588 DO_STAT(size_gc_map_);
589 DO_STAT(size_oat_dex_file_location_size_);
590 DO_STAT(size_oat_dex_file_location_data_);
591 DO_STAT(size_oat_dex_file_location_checksum_);
592 DO_STAT(size_oat_dex_file_offset_);
593 DO_STAT(size_oat_dex_file_methods_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700594 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700595 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700596 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700597 DO_STAT(size_oat_class_method_offsets_);
598 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700599
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700600 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Ian Rogers3d504072014-03-01 09:16:49 -0800601 CHECK_EQ(file_offset + size_total, static_cast<uint32_t>(out->Seek(0, kSeekCurrent)));
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700602 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700603 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700604
Ian Rogers3d504072014-03-01 09:16:49 -0800605 CHECK_EQ(file_offset + size_, static_cast<uint32_t>(out->Seek(0, kSeekCurrent)));
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700606 CHECK_EQ(size_, relative_offset);
607
Brian Carlstrome24fa612011-09-29 00:53:55 -0700608 return true;
609}
610
Ian Rogers3d504072014-03-01 09:16:49 -0800611bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700612 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700613 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800614 PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700615 return false;
616 }
617 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800618 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700619 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Ian Rogers3d504072014-03-01 09:16:49 -0800620 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -0800621 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
622 const DexFile* dex_file = (*dex_files_)[i];
623 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
624 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
625 return false;
626 }
627 const DexFile* dex_file = (*dex_files_)[i];
Ian Rogers3d504072014-03-01 09:16:49 -0800628 if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700629 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
Ian Rogers3d504072014-03-01 09:16:49 -0800630 << " to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800631 return false;
632 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700633 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800634 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800635 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700636 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800637 PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700638 return false;
639 }
640 }
641 return true;
642}
643
Ian Rogers3d504072014-03-01 09:16:49 -0800644size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700645 size_t relative_offset = oat_header_->GetExecutableOffset();
Ian Rogers3d504072014-03-01 09:16:49 -0800646 off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700647 size_t expected_file_offset = file_offset + relative_offset;
648 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700649 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Ian Rogers3d504072014-03-01 09:16:49 -0800650 << " Expected: " << expected_file_offset << " File: " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700651 return 0;
652 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800653 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700654 if (compiler_driver_->IsImage()) {
655 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700656
Ian Rogers848871b2013-08-05 10:56:33 -0700657 #define DO_TRAMPOLINE(field) \
658 do { \
659 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
660 uint32_t alignment_padding = aligned_offset - relative_offset; \
Ian Rogers3d504072014-03-01 09:16:49 -0800661 out->Seek(alignment_padding, kSeekCurrent); \
Ian Rogers848871b2013-08-05 10:56:33 -0700662 size_trampoline_alignment_ += alignment_padding; \
Ian Rogers3d504072014-03-01 09:16:49 -0800663 if (!out->WriteFully(&(*field)[0], field->size())) { \
664 PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \
Ian Rogers848871b2013-08-05 10:56:33 -0700665 return false; \
666 } \
667 size_ ## field += field->size(); \
668 relative_offset += alignment_padding + field->size(); \
669 DCHECK_OFFSET(); \
670 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700671
Ian Rogers848871b2013-08-05 10:56:33 -0700672 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_);
673 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_);
674 DO_TRAMPOLINE(jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -0700675 DO_TRAMPOLINE(portable_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700676 DO_TRAMPOLINE(portable_resolution_trampoline_);
677 DO_TRAMPOLINE(portable_to_interpreter_bridge_);
Jeff Hao88474b42013-10-23 16:24:40 -0700678 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700679 DO_TRAMPOLINE(quick_resolution_trampoline_);
680 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
681 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700682 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700683 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700684}
685
Ian Rogers3d504072014-03-01 09:16:49 -0800686size_t OatWriter::WriteCodeDexFiles(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700687 const size_t file_offset,
688 size_t relative_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700689 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800690 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700691 const DexFile* dex_file = (*dex_files_)[i];
692 CHECK(dex_file != NULL);
Brian Carlstromba150c32013-08-27 17:31:03 -0700693 relative_offset = WriteCodeDexFile(out, file_offset, relative_offset, &oat_class_index,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700694 *dex_file);
695 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700696 return 0;
697 }
698 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700699 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700700}
701
Ian Rogers3d504072014-03-01 09:16:49 -0800702size_t OatWriter::WriteCodeDexFile(OutputStream* out, const size_t file_offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700703 size_t relative_offset, size_t* oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700704 const DexFile& dex_file) {
705 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
Brian Carlstromba150c32013-08-27 17:31:03 -0700706 class_def_index++, (*oat_class_index)++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700707 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstromba150c32013-08-27 17:31:03 -0700708 relative_offset = WriteCodeClassDef(out, file_offset, relative_offset, *oat_class_index,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700709 dex_file, class_def);
710 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700711 return 0;
712 }
713 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700714 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700715}
716
Ian Rogers0571d352011-11-03 19:51:38 -0700717void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
Ian Rogers3d504072014-03-01 09:16:49 -0800718 const DexFile& dex_file, const OutputStream& out) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700719 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800720 << " to " << out.GetLocation();
Elliott Hughes234da572011-11-03 22:13:06 -0700721}
722
Ian Rogers3d504072014-03-01 09:16:49 -0800723size_t OatWriter::WriteCodeClassDef(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700724 const size_t file_offset,
725 size_t relative_offset,
726 size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700727 const DexFile& dex_file,
728 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700729 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700730 if (class_data == NULL) {
731 // ie. an empty class such as a marker interface
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700732 return relative_offset;
Ian Rogers387b6992011-10-31 17:52:37 -0700733 }
Ian Rogers0571d352011-11-03 19:51:38 -0700734 ClassDataItemIterator it(dex_file, class_data);
735 // Skip fields
736 while (it.HasNextStaticField()) {
737 it.Next();
738 }
739 while (it.HasNextInstanceField()) {
740 it.Next();
741 }
742 // Process methods
743 size_t class_def_method_index = 0;
Brian Carlstromba150c32013-08-27 17:31:03 -0700744 size_t method_offsets_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700745 while (it.HasNextDirectMethod()) {
746 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700747 relative_offset = WriteCodeMethod(out, file_offset, relative_offset, oat_class_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700748 class_def_method_index, &method_offsets_index, is_static,
749 it.GetMemberIndex(), dex_file);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700750 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700751 return 0;
752 }
Ian Rogers0571d352011-11-03 19:51:38 -0700753 class_def_method_index++;
754 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700755 }
Ian Rogers0571d352011-11-03 19:51:38 -0700756 while (it.HasNextVirtualMethod()) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700757 relative_offset = WriteCodeMethod(out, file_offset, relative_offset, oat_class_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700758 class_def_method_index, &method_offsets_index, false,
759 it.GetMemberIndex(), dex_file);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700760 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700761 return 0;
762 }
Ian Rogers0571d352011-11-03 19:51:38 -0700763 class_def_method_index++;
764 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700765 }
Brian Carlstromba150c32013-08-27 17:31:03 -0700766 DCHECK(!it.HasNext());
767 CHECK_LE(method_offsets_index, class_def_method_index);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700768 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700769}
770
Ian Rogers3d504072014-03-01 09:16:49 -0800771size_t OatWriter::WriteCodeMethod(OutputStream* out, const size_t file_offset,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700772 size_t relative_offset, size_t oat_class_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700773 size_t class_def_method_index, size_t* method_offsets_index,
774 bool is_static, uint32_t method_idx, const DexFile& dex_file) {
775 OatClass* oat_class = oat_classes_[oat_class_index];
776 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700777
778 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstromba150c32013-08-27 17:31:03 -0700779 const OatMethodOffsets method_offsets = oat_class->method_offsets_[*method_offsets_index];
780 (*method_offsets_index)++;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800781 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode();
782 if (quick_code != nullptr) {
783 CHECK(compiled_method->GetPortableCode() == nullptr);
784 uint32_t aligned_offset = compiled_method->AlignCode(relative_offset);
785 uint32_t aligned_code_delta = aligned_offset - relative_offset;
786 if (aligned_code_delta != 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800787 off_t new_offset = out->Seek(aligned_code_delta, kSeekCurrent);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800788 size_code_alignment_ += aligned_code_delta;
789 uint32_t expected_offset = file_offset + aligned_offset;
790 if (static_cast<uint32_t>(new_offset) != expected_offset) {
791 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Ian Rogers3d504072014-03-01 09:16:49 -0800792 << " Expected: " << expected_offset << " File: " << out->GetLocation();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800793 return 0;
794 }
795 relative_offset += aligned_code_delta;
796 DCHECK_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700797 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800798 DCHECK_ALIGNED(relative_offset, kArmAlignment);
799 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
800 CHECK_NE(code_size, 0U);
801
802 // Deduplicate code arrays
803 size_t code_offset = relative_offset + sizeof(code_size) + compiled_method->CodeDelta();
804 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter =
805 code_offsets_.find(quick_code);
806 if (code_iter != code_offsets_.end() && code_offset != method_offsets.code_offset_) {
807 DCHECK(code_iter->second == method_offsets.code_offset_)
808 << PrettyMethod(method_idx, dex_file);
809 } else {
810 DCHECK(code_offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
Ian Rogers3d504072014-03-01 09:16:49 -0800811 if (!out->WriteFully(&code_size, sizeof(code_size))) {
812 ReportWriteFailure("method code size", method_idx, dex_file, *out);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800813 return 0;
814 }
815 size_code_size_ += sizeof(code_size);
816 relative_offset += sizeof(code_size);
817 DCHECK_OFFSET();
Ian Rogers3d504072014-03-01 09:16:49 -0800818 if (!out->WriteFully(&(*quick_code)[0], code_size)) {
819 ReportWriteFailure("method code", method_idx, dex_file, *out);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800820 return 0;
821 }
822 size_code_ += code_size;
823 relative_offset += code_size;
824 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800825 DCHECK_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700826 }
Ian Rogers1809a722013-08-09 22:05:32 -0700827 const std::vector<uint8_t>& mapping_table = compiled_method->GetMappingTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800828 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
829
830 // Deduplicate mapping tables
Ian Rogers1809a722013-08-09 22:05:32 -0700831 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator mapping_iter =
Logan Chien971bf3f2012-05-01 15:47:55 +0800832 mapping_table_offsets_.find(&mapping_table);
833 if (mapping_iter != mapping_table_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700834 relative_offset != method_offsets.mapping_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800835 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
836 || mapping_iter->second == method_offsets.mapping_table_offset_)
837 << PrettyMethod(method_idx, dex_file);
838 } else {
839 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700840 || relative_offset == method_offsets.mapping_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800841 << PrettyMethod(method_idx, dex_file);
Ian Rogers3d504072014-03-01 09:16:49 -0800842 if (!out->WriteFully(&mapping_table[0], mapping_table_size)) {
843 ReportWriteFailure("mapping table", method_idx, dex_file, *out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800844 return 0;
845 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700846 size_mapping_table_ += mapping_table_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700847 relative_offset += mapping_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800848 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800849 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800850
Ian Rogers1809a722013-08-09 22:05:32 -0700851 const std::vector<uint8_t>& vmap_table = compiled_method->GetVmapTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800852 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
853
854 // Deduplicate vmap tables
Ian Rogers1809a722013-08-09 22:05:32 -0700855 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator vmap_iter =
Logan Chien971bf3f2012-05-01 15:47:55 +0800856 vmap_table_offsets_.find(&vmap_table);
857 if (vmap_iter != vmap_table_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700858 relative_offset != method_offsets.vmap_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800859 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
860 || vmap_iter->second == method_offsets.vmap_table_offset_)
861 << PrettyMethod(method_idx, dex_file);
862 } else {
863 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700864 || relative_offset == method_offsets.vmap_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800865 << PrettyMethod(method_idx, dex_file);
Ian Rogers3d504072014-03-01 09:16:49 -0800866 if (!out->WriteFully(&vmap_table[0], vmap_table_size)) {
867 ReportWriteFailure("vmap table", method_idx, dex_file, *out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800868 return 0;
869 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700870 size_vmap_table_ += vmap_table_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700871 relative_offset += vmap_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800872 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800873 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800874
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700875 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800876 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
877
878 // Deduplicate GC maps
879 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
880 gc_map_offsets_.find(&gc_map);
881 if (gc_map_iter != gc_map_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700882 relative_offset != method_offsets.gc_map_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800883 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
884 || gc_map_iter->second == method_offsets.gc_map_offset_)
885 << PrettyMethod(method_idx, dex_file);
886 } else {
887 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700888 || relative_offset == method_offsets.gc_map_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800889 << PrettyMethod(method_idx, dex_file);
Ian Rogers3d504072014-03-01 09:16:49 -0800890 if (!out->WriteFully(&gc_map[0], gc_map_size)) {
891 ReportWriteFailure("GC map", method_idx, dex_file, *out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800892 return 0;
893 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700894 size_gc_map_ += gc_map_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700895 relative_offset += gc_map_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800896 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800897 DCHECK_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700898 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800899
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700900 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700901}
902
Brian Carlstrom265091e2013-01-30 14:08:26 -0800903OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
904 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -0800905 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700906 dex_file_location_size_ = location.size();
907 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800908 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800909 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800910 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700911}
912
913size_t OatWriter::OatDexFile::SizeOf() const {
914 return sizeof(dex_file_location_size_)
915 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800916 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800917 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800918 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700919}
920
Ian Rogers3d504072014-03-01 09:16:49 -0800921void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const {
922 oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
923 oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
924 oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
925 oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
926 oat_header->UpdateChecksum(&methods_offsets_[0],
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800927 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700928}
929
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700930bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -0800931 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700932 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800933 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -0800934 if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
935 PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700936 return false;
937 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700938 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Ian Rogers3d504072014-03-01 09:16:49 -0800939 if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
940 PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700941 return false;
942 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700943 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Ian Rogers3d504072014-03-01 09:16:49 -0800944 if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
945 PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700946 return false;
947 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700948 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Ian Rogers3d504072014-03-01 09:16:49 -0800949 if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
950 PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800951 return false;
952 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700953 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Ian Rogers3d504072014-03-01 09:16:49 -0800954 if (!out->WriteFully(&methods_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800955 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -0800956 PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700957 return false;
958 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700959 oat_writer->size_oat_dex_file_methods_offsets_ +=
960 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700961 return true;
962}
963
Brian Carlstromba150c32013-08-27 17:31:03 -0700964OatWriter::OatClass::OatClass(size_t offset,
965 std::vector<CompiledMethod*>* compiled_methods,
966 uint32_t num_non_null_compiled_methods,
967 mirror::Class::Status status) {
968 CHECK(compiled_methods != NULL);
969 uint32_t num_methods = compiled_methods->size();
970 CHECK_LE(num_non_null_compiled_methods, num_methods);
971
Brian Carlstrom265091e2013-01-30 14:08:26 -0800972 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -0700973 compiled_methods_ = compiled_methods;
974 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
975
976 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
977 // apply when there are 0 methods, we just arbitrarily say that 0
978 // methods means kOatClassNoneCompiled and that we won't use
979 // kOatClassAllCompiled unless there is at least one compiled
980 // method. This means in an interpretter only system, we can assert
981 // that all classes are kOatClassNoneCompiled.
982 if (num_non_null_compiled_methods == 0) {
983 type_ = kOatClassNoneCompiled;
984 } else if (num_non_null_compiled_methods == num_methods) {
985 type_ = kOatClassAllCompiled;
986 } else {
987 type_ = kOatClassSomeCompiled;
988 }
989
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800990 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -0700991 method_offsets_.resize(num_non_null_compiled_methods);
992
993 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
994 if (type_ == kOatClassSomeCompiled) {
995 method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator());
996 method_bitmap_size_ = method_bitmap_->GetSizeOf();
997 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
998 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
999 } else {
1000 method_bitmap_ = NULL;
1001 method_bitmap_size_ = 0;
1002 }
1003
1004 for (size_t i = 0; i < num_methods; i++) {
1005 CompiledMethod* compiled_method = (*compiled_methods_)[i];
1006 if (compiled_method == NULL) {
1007 oat_method_offsets_offsets_from_oat_class_[i] = 0;
1008 } else {
1009 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
1010 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
1011 if (type_ == kOatClassSomeCompiled) {
1012 method_bitmap_->SetBit(i);
1013 }
1014 }
1015 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001016}
1017
Brian Carlstromba150c32013-08-27 17:31:03 -07001018OatWriter::OatClass::~OatClass() {
1019 delete compiled_methods_;
1020}
1021
Brian Carlstrom265091e2013-01-30 14:08:26 -08001022size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
1023 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001024 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
1025 if (method_offset == 0) {
1026 return 0;
1027 }
1028 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001029}
1030
1031size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
1032 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001033 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08001034}
1035
1036size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001037 return sizeof(status_)
1038 + sizeof(type_)
1039 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
1040 + method_bitmap_size_
1041 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001042}
1043
Ian Rogers3d504072014-03-01 09:16:49 -08001044void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const {
1045 oat_header->UpdateChecksum(&status_, sizeof(status_));
1046 oat_header->UpdateChecksum(&type_, sizeof(type_));
Brian Carlstromba150c32013-08-27 17:31:03 -07001047 if (method_bitmap_size_ != 0) {
1048 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001049 oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_));
1050 oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001051 }
Ian Rogers3d504072014-03-01 09:16:49 -08001052 oat_header->UpdateChecksum(&method_offsets_[0],
1053 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001054}
1055
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001056bool OatWriter::OatClass::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001057 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001058 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001059 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001060 if (!out->WriteFully(&status_, sizeof(status_))) {
1061 PLOG(ERROR) << "Failed to write class status to " << out->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001062 return false;
1063 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001064 oat_writer->size_oat_class_status_ += sizeof(status_);
Ian Rogers3d504072014-03-01 09:16:49 -08001065 if (!out->WriteFully(&type_, sizeof(type_))) {
1066 PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001067 return false;
1068 }
1069 oat_writer->size_oat_class_type_ += sizeof(type_);
1070 if (method_bitmap_size_ != 0) {
1071 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001072 if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
1073 PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001074 return false;
1075 }
1076 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001077 if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
1078 PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001079 return false;
1080 }
1081 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1082 }
Ian Rogers3d504072014-03-01 09:16:49 -08001083 if (!out->WriteFully(&method_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001084 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001085 PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001086 return false;
1087 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001088 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001089 return true;
1090}
1091
Brian Carlstrome24fa612011-09-29 00:53:55 -07001092} // namespace art