blob: 83824694ae4aa1713fb81fa5cea2fcfb16e6957c [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"
Ian Rogers1d54e732013-05-02 21:10:01 -070026#include "gc/space/space.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070027#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/array.h"
29#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070031#include "os.h"
Brian Carlstromcd60ac72013-01-20 17:09:51 -080032#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070033#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070034#include "scoped_thread_state_change.h"
jeffhaoec014232012-09-05 10:42:25 -070035#include "verifier/method_verifier.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070036
37namespace art {
38
Brian Carlstrom3320cf42011-10-04 14:58:28 -070039OatWriter::OatWriter(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 Rogersca368cb2013-11-15 15:52:08 -080043 const CompilerDriver* compiler,
44 TimingLogger* timings)
Jeff Hao0aba0ba2013-06-03 14:49:28 -070045 : compiler_driver_(compiler),
46 dex_files_(&dex_files),
47 image_file_location_oat_checksum_(image_file_location_oat_checksum),
48 image_file_location_oat_begin_(image_file_location_oat_begin),
49 image_file_location_(image_file_location),
50 oat_header_(NULL),
51 size_dex_file_alignment_(0),
52 size_executable_offset_alignment_(0),
53 size_oat_header_(0),
54 size_oat_header_image_file_location_(0),
55 size_dex_file_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070056 size_interpreter_to_interpreter_bridge_(0),
57 size_interpreter_to_compiled_code_bridge_(0),
58 size_jni_dlsym_lookup_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070059 size_portable_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070060 size_portable_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070061 size_portable_to_interpreter_bridge_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070062 size_quick_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070063 size_quick_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070064 size_quick_to_interpreter_bridge_(0),
65 size_trampoline_alignment_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070066 size_code_size_(0),
67 size_code_(0),
68 size_code_alignment_(0),
69 size_mapping_table_(0),
70 size_vmap_table_(0),
71 size_gc_map_(0),
72 size_oat_dex_file_location_size_(0),
73 size_oat_dex_file_location_data_(0),
74 size_oat_dex_file_location_checksum_(0),
75 size_oat_dex_file_offset_(0),
76 size_oat_dex_file_methods_offsets_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -070077 size_oat_class_type_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070078 size_oat_class_status_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -070079 size_oat_class_method_bitmaps_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070080 size_oat_class_method_offsets_(0) {
Ian Rogersca368cb2013-11-15 15:52:08 -080081 size_t offset;
82 {
83 TimingLogger::ScopedSplit split("InitOatHeader", timings);
84 offset = InitOatHeader();
85 }
86 {
87 TimingLogger::ScopedSplit split("InitOatDexFiles", timings);
88 offset = InitOatDexFiles(offset);
89 }
90 {
91 TimingLogger::ScopedSplit split("InitDexFiles", timings);
92 offset = InitDexFiles(offset);
93 }
94 {
95 TimingLogger::ScopedSplit split("InitOatClasses", timings);
96 offset = InitOatClasses(offset);
97 }
98 {
99 TimingLogger::ScopedSplit split("InitOatCode", timings);
100 offset = InitOatCode(offset);
101 }
102 {
103 TimingLogger::ScopedSplit split("InitOatCodeDexFiles", timings);
104 offset = InitOatCodeDexFiles(offset);
105 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700106 size_ = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700107
108 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700109 CHECK(image_file_location.empty() == compiler->IsImage());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700110}
111
Ian Rogers0571d352011-11-03 19:51:38 -0700112OatWriter::~OatWriter() {
113 delete oat_header_;
114 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800115 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -0700116}
117
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700118size_t OatWriter::InitOatHeader() {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700119 // create the OatHeader
Ian Rogers1212a022013-03-04 10:48:41 -0800120 oat_header_ = new OatHeader(compiler_driver_->GetInstructionSet(),
Dave Allison70202782013-10-22 17:52:19 -0700121 compiler_driver_->GetInstructionSetFeatures(),
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700122 dex_files_,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700123 image_file_location_oat_checksum_,
124 image_file_location_oat_begin_,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700125 image_file_location_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700126 size_t offset = sizeof(*oat_header_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700127 offset += image_file_location_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700128 return offset;
129}
130
131size_t OatWriter::InitOatDexFiles(size_t offset) {
132 // create the OatDexFiles
133 for (size_t i = 0; i != dex_files_->size(); ++i) {
134 const DexFile* dex_file = (*dex_files_)[i];
135 CHECK(dex_file != NULL);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800136 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700137 oat_dex_files_.push_back(oat_dex_file);
138 offset += oat_dex_file->SizeOf();
139 }
140 return offset;
141}
142
Brian Carlstrom89521892011-12-07 22:05:07 -0800143size_t OatWriter::InitDexFiles(size_t offset) {
144 // calculate the offsets within OatDexFiles to the DexFiles
145 for (size_t i = 0; i != dex_files_->size(); ++i) {
146 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700147 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800148 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700149 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800150
151 // set offset in OatDexFile to DexFile
152 oat_dex_files_[i]->dex_file_offset_ = offset;
153
154 const DexFile* dex_file = (*dex_files_)[i];
155 offset += dex_file->GetHeader().file_size_;
156 }
157 return offset;
158}
159
Brian Carlstrom389efb02012-01-11 12:06:26 -0800160size_t OatWriter::InitOatClasses(size_t offset) {
161 // create the OatClasses
162 // calculate the offsets within OatDexFiles to OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -0700163 for (size_t i = 0; i != dex_files_->size(); ++i) {
164 const DexFile* dex_file = (*dex_files_)[i];
165 for (size_t class_def_index = 0;
166 class_def_index < dex_file->NumClassDefs();
Ian Rogersc20a83e2012-01-18 18:15:32 -0800167 class_def_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800168 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700169 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
170 const byte* class_data = dex_file->GetClassData(class_def);
Brian Carlstromba150c32013-08-27 17:31:03 -0700171 uint32_t num_non_null_compiled_methods = 0;
172 UniquePtr<std::vector<CompiledMethod*> > compiled_methods(new std::vector<CompiledMethod*>());
Ian Rogers0571d352011-11-03 19:51:38 -0700173 if (class_data != NULL) { // ie not an empty class, such as a marker interface
174 ClassDataItemIterator it(*dex_file, class_data);
175 size_t num_direct_methods = it.NumDirectMethods();
176 size_t num_virtual_methods = it.NumVirtualMethods();
Brian Carlstromba150c32013-08-27 17:31:03 -0700177 size_t num_methods = num_direct_methods + num_virtual_methods;
178
179 // Fill in the compiled_methods_ array for methods that have a
180 // CompiledMethod. We track the number of non-null entries in
181 // num_non_null_compiled_methods since we only want to allocate
182 // OatMethodOffsets for the compiled methods.
183 compiled_methods->reserve(num_methods);
184 while (it.HasNextStaticField()) {
185 it.Next();
186 }
187 while (it.HasNextInstanceField()) {
188 it.Next();
189 }
190 size_t class_def_method_index = 0;
191 while (it.HasNextDirectMethod()) {
192 uint32_t method_idx = it.GetMemberIndex();
193 CompiledMethod* compiled_method =
194 compiler_driver_->GetCompiledMethod(MethodReference(dex_file, method_idx));
195 compiled_methods->push_back(compiled_method);
196 if (compiled_method != NULL) {
197 num_non_null_compiled_methods++;
198 }
199 class_def_method_index++;
200 it.Next();
201 }
202 while (it.HasNextVirtualMethod()) {
203 uint32_t method_idx = it.GetMemberIndex();
204 CompiledMethod* compiled_method =
205 compiler_driver_->GetCompiledMethod(MethodReference(dex_file, method_idx));
206 compiled_methods->push_back(compiled_method);
207 if (compiled_method != NULL) {
208 num_non_null_compiled_methods++;
209 }
210 class_def_method_index++;
211 it.Next();
212 }
Ian Rogers0571d352011-11-03 19:51:38 -0700213 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800214
Brian Carlstrom51c24672013-07-11 16:00:56 -0700215 ClassReference class_ref(dex_file, class_def_index);
Ian Rogers1212a022013-03-04 10:48:41 -0800216 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217 mirror::Class::Status status;
jeffhaoec014232012-09-05 10:42:25 -0700218 if (compiled_class != NULL) {
219 status = compiled_class->GetStatus();
220 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221 status = mirror::Class::kStatusError;
jeffhaoec014232012-09-05 10:42:25 -0700222 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800223 status = mirror::Class::kStatusNotReady;
jeffhaoec014232012-09-05 10:42:25 -0700224 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800225
Brian Carlstromba150c32013-08-27 17:31:03 -0700226 OatClass* oat_class = new OatClass(offset, compiled_methods.release(),
227 num_non_null_compiled_methods, status);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800228 oat_classes_.push_back(oat_class);
229 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700230 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800231 oat_dex_files_[i]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700232 }
233 return offset;
234}
235
236size_t OatWriter::InitOatCode(size_t offset) {
237 // calculate the offsets within OatHeader to executable code
238 size_t old_offset = offset;
239 // required to be on a new page boundary
240 offset = RoundUp(offset, kPageSize);
241 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700242 size_executable_offset_alignment_ = offset - old_offset;
243 if (compiler_driver_->IsImage()) {
244 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700245
Ian Rogers848871b2013-08-05 10:56:33 -0700246 #define DO_TRAMPOLINE(field, fn_name) \
247 offset = CompiledCode::AlignCode(offset, instruction_set); \
248 oat_header_->Set ## fn_name ## Offset(offset); \
249 field.reset(compiler_driver_->Create ## fn_name()); \
250 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700251
Ian Rogers848871b2013-08-05 10:56:33 -0700252 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge);
253 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge);
254 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Jeff Hao88474b42013-10-23 16:24:40 -0700255 DO_TRAMPOLINE(portable_imt_conflict_trampoline_, PortableImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700256 DO_TRAMPOLINE(portable_resolution_trampoline_, PortableResolutionTrampoline);
257 DO_TRAMPOLINE(portable_to_interpreter_bridge_, PortableToInterpreterBridge);
Jeff Hao88474b42013-10-23 16:24:40 -0700258 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700259 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
260 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700261
Ian Rogers848871b2013-08-05 10:56:33 -0700262 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700263 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700264 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
265 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
266 oat_header_->SetJniDlsymLookupOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700267 oat_header_->SetPortableImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700268 oat_header_->SetPortableResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700269 oat_header_->SetPortableToInterpreterBridgeOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700270 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700271 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700272 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700273 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700274 return offset;
275}
276
277size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700278 size_t oat_class_index = 0;
279 for (size_t i = 0; i != dex_files_->size(); ++i) {
280 const DexFile* dex_file = (*dex_files_)[i];
281 CHECK(dex_file != NULL);
Brian Carlstromba150c32013-08-27 17:31:03 -0700282 offset = InitOatCodeDexFile(offset, &oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700283 }
284 return offset;
285}
286
287size_t OatWriter::InitOatCodeDexFile(size_t offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700288 size_t* oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700289 const DexFile& dex_file) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800290 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700291 class_def_index < dex_file.NumClassDefs();
Brian Carlstromba150c32013-08-27 17:31:03 -0700292 class_def_index++, (*oat_class_index)++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700293 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstromba150c32013-08-27 17:31:03 -0700294 offset = InitOatCodeClassDef(offset, *oat_class_index, class_def_index, dex_file, class_def);
295 oat_classes_[*oat_class_index]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700296 }
297 return offset;
298}
299
300size_t OatWriter::InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800301 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700302 const DexFile& dex_file,
303 const DexFile::ClassDef& class_def) {
304 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700305 if (class_data == NULL) {
306 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700307 return offset;
308 }
Ian Rogers0571d352011-11-03 19:51:38 -0700309 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstromba150c32013-08-27 17:31:03 -0700310 CHECK_LE(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700311 it.NumDirectMethods() + it.NumVirtualMethods());
312 // Skip fields
313 while (it.HasNextStaticField()) {
314 it.Next();
315 }
316 while (it.HasNextInstanceField()) {
317 it.Next();
318 }
319 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700320 size_t class_def_method_index = 0;
Brian Carlstromba150c32013-08-27 17:31:03 -0700321 size_t method_offsets_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700322 while (it.HasNextDirectMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800323 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700324 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700325 &method_offsets_index, is_native,
326 it.GetMethodInvokeType(class_def), it.GetMemberIndex(), dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700327 class_def_method_index++;
328 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700329 }
Ian Rogers0571d352011-11-03 19:51:38 -0700330 while (it.HasNextVirtualMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800331 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700332 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700333 &method_offsets_index, is_native,
334 it.GetMethodInvokeType(class_def), it.GetMemberIndex(), dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700335 class_def_method_index++;
336 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700337 }
Ian Rogers0571d352011-11-03 19:51:38 -0700338 DCHECK(!it.HasNext());
Brian Carlstromba150c32013-08-27 17:31:03 -0700339 CHECK_LE(method_offsets_index, class_def_method_index);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700340 return offset;
341}
342
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700343size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
344 size_t __attribute__((unused)) class_def_index,
345 size_t class_def_method_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700346 size_t* method_offsets_index,
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700347 bool __attribute__((unused)) is_native,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800348 InvokeType invoke_type,
Brian Carlstromba150c32013-08-27 17:31:03 -0700349 uint32_t method_idx, const DexFile& dex_file) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700350 // derived from CompiledMethod if available
351 uint32_t code_offset = 0;
352 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700353 uint32_t core_spill_mask = 0;
354 uint32_t fp_spill_mask = 0;
355 uint32_t mapping_table_offset = 0;
356 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800357 uint32_t gc_map_offset = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700358
Brian Carlstrom265091e2013-01-30 14:08:26 -0800359 OatClass* oat_class = oat_classes_[oat_class_index];
360#if defined(ART_USE_PORTABLE_COMPILER)
361 size_t oat_method_offsets_offset =
362 oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index);
363#endif
364
Brian Carlstromba150c32013-08-27 17:31:03 -0700365 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700366 if (compiled_method != NULL) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800367#if defined(ART_USE_PORTABLE_COMPILER)
368 compiled_method->AddOatdataOffsetToCompliledCodeOffset(
369 oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_));
370#else
371 const std::vector<uint8_t>& code = compiled_method->GetCode();
Logan Chien971bf3f2012-05-01 15:47:55 +0800372 offset = compiled_method->AlignCode(offset);
373 DCHECK_ALIGNED(offset, kArmAlignment);
Logan Chien971bf3f2012-05-01 15:47:55 +0800374 uint32_t code_size = code.size() * sizeof(code[0]);
375 CHECK_NE(code_size, 0U);
376 uint32_t thumb_offset = compiled_method->CodeDelta();
377 code_offset = offset + sizeof(code_size) + thumb_offset;
378
379 // Deduplicate code arrays
380 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
381 if (code_iter != code_offsets_.end()) {
382 code_offset = code_iter->second;
jeffhao55d78212011-11-02 11:41:50 -0700383 } else {
Logan Chien971bf3f2012-05-01 15:47:55 +0800384 code_offsets_.Put(&code, code_offset);
385 offset += sizeof(code_size); // code size is prepended before code
386 offset += code_size;
387 oat_header_->UpdateChecksum(&code[0], code_size);
388 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800389#endif
Logan Chien971bf3f2012-05-01 15:47:55 +0800390 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
391 core_spill_mask = compiled_method->GetCoreSpillMask();
392 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700393
Ian Rogers1809a722013-08-09 22:05:32 -0700394 const std::vector<uint8_t>& mapping_table = compiled_method->GetMappingTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800395 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
396 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700397
Logan Chien971bf3f2012-05-01 15:47:55 +0800398 // Deduplicate mapping tables
Ian Rogers1809a722013-08-09 22:05:32 -0700399 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator mapping_iter =
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700400 mapping_table_offsets_.find(&mapping_table);
Logan Chien971bf3f2012-05-01 15:47:55 +0800401 if (mapping_iter != mapping_table_offsets_.end()) {
402 mapping_table_offset = mapping_iter->second;
403 } else {
404 mapping_table_offsets_.Put(&mapping_table, mapping_table_offset);
405 offset += mapping_table_size;
406 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
407 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700408
Ian Rogers1809a722013-08-09 22:05:32 -0700409 const std::vector<uint8_t>& vmap_table = compiled_method->GetVmapTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800410 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
411 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700412
Logan Chien971bf3f2012-05-01 15:47:55 +0800413 // Deduplicate vmap tables
Ian Rogers1809a722013-08-09 22:05:32 -0700414 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator vmap_iter =
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700415 vmap_table_offsets_.find(&vmap_table);
Logan Chien971bf3f2012-05-01 15:47:55 +0800416 if (vmap_iter != vmap_table_offsets_.end()) {
417 vmap_table_offset = vmap_iter->second;
418 } else {
419 vmap_table_offsets_.Put(&vmap_table, vmap_table_offset);
420 offset += vmap_table_size;
421 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
422 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800423
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700424 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800425 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
426 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
Logan Chienccb7bf12012-03-28 12:52:32 +0800427
Mathieu Chartier590fee92013-09-13 13:46:47 -0700428 if (kIsDebugBuild) {
429 // We expect GC maps except when the class hasn't been verified or the method is native
430 ClassReference class_ref(&dex_file, class_def_index);
431 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
432 mirror::Class::Status status;
433 if (compiled_class != NULL) {
434 status = compiled_class->GetStatus();
435 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
436 status = mirror::Class::kStatusError;
437 } else {
438 status = mirror::Class::kStatusNotReady;
439 }
440 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
441 << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
442 << (status < mirror::Class::kStatusVerified) << " " << status << " "
443 << PrettyMethod(method_idx, dex_file);
jeffhaoec014232012-09-05 10:42:25 -0700444 }
Ian Rogersc20a83e2012-01-18 18:15:32 -0800445
Logan Chien971bf3f2012-05-01 15:47:55 +0800446 // Deduplicate GC maps
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700447 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
448 gc_map_offsets_.find(&gc_map);
Logan Chien971bf3f2012-05-01 15:47:55 +0800449 if (gc_map_iter != gc_map_offsets_.end()) {
450 gc_map_offset = gc_map_iter->second;
451 } else {
452 gc_map_offsets_.Put(&gc_map, gc_map_offset);
453 offset += gc_map_size;
454 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800455 }
Brian Carlstromba150c32013-08-27 17:31:03 -0700456
457 oat_class->method_offsets_[*method_offsets_index] =
458 OatMethodOffsets(code_offset,
459 frame_size_in_bytes,
460 core_spill_mask,
461 fp_spill_mask,
462 mapping_table_offset,
463 vmap_table_offset,
464 gc_map_offset);
465 (*method_offsets_index)++;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700466 }
467
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700468
Ian Rogers1212a022013-03-04 10:48:41 -0800469 if (compiler_driver_->IsImage()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700470 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700471 // Unchecked as we hold mutator_lock_ on entry.
472 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700473 SirtRef<mirror::DexCache> dex_cache(soa.Self(), linker->FindDexCache(dex_file));
474 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700475 mirror::ArtMethod* method = linker->ResolveMethod(dex_file, method_idx, dex_cache,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700476 class_loader, nullptr, invoke_type);
Ian Rogers0571d352011-11-03 19:51:38 -0700477 CHECK(method != NULL);
478 method->SetFrameSizeInBytes(frame_size_in_bytes);
479 method->SetCoreSpillMask(core_spill_mask);
480 method->SetFpSpillMask(fp_spill_mask);
481 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800482 // Don't overwrite static method trampoline
483 if (!method->IsStatic() || method->IsConstructor() ||
484 method->GetDeclaringClass()->IsInitialized()) {
485 method->SetOatCodeOffset(code_offset);
486 } else {
Jeff Haoaa4a7932013-05-13 11:28:27 -0700487 method->SetEntryPointFromCompiledCode(NULL);
Ian Rogers19846512012-02-24 11:42:47 -0800488 }
Ian Rogers0571d352011-11-03 19:51:38 -0700489 method->SetOatVmapTableOffset(vmap_table_offset);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700490 method->SetOatNativeGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700491 }
Logan Chien8b977d32012-02-21 19:14:55 +0800492
Brian Carlstrome24fa612011-09-29 00:53:55 -0700493 return offset;
494}
495
Brian Carlstrom265091e2013-01-30 14:08:26 -0800496#define DCHECK_OFFSET() \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700497 DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out.Seek(0, kSeekCurrent)) \
498 << "file_offset=" << file_offset << " relative_offset=" << relative_offset
Brian Carlstrom265091e2013-01-30 14:08:26 -0800499
500#define DCHECK_OFFSET_() \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700501 DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out.Seek(0, kSeekCurrent)) \
502 << "file_offset=" << file_offset << " offset_=" << offset_
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700503
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800504bool OatWriter::Write(OutputStream& out) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700505 const size_t file_offset = out.Seek(0, kSeekCurrent);
506
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800507 if (!out.WriteFully(oat_header_, sizeof(*oat_header_))) {
508 PLOG(ERROR) << "Failed to write oat header to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700509 return false;
510 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700511 size_oat_header_ += sizeof(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700512
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800513 if (!out.WriteFully(image_file_location_.data(), image_file_location_.size())) {
514 PLOG(ERROR) << "Failed to write oat header image file location to " << out.GetLocation();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700515 return false;
516 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700517 size_oat_header_image_file_location_ += image_file_location_.size();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700518
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700519 if (!WriteTables(out, file_offset)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800520 LOG(ERROR) << "Failed to write oat tables to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700521 return false;
522 }
523
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700524 size_t relative_offset = WriteCode(out, file_offset);
525 if (relative_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800526 LOG(ERROR) << "Failed to write oat code to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700527 return false;
528 }
529
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700530 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
531 if (relative_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800532 LOG(ERROR) << "Failed to write oat code for dex files to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700533 return false;
534 }
535
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700536 if (kIsDebugBuild) {
537 uint32_t size_total = 0;
538 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700539 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700540 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700541
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700542 DO_STAT(size_dex_file_alignment_);
543 DO_STAT(size_executable_offset_alignment_);
544 DO_STAT(size_oat_header_);
545 DO_STAT(size_oat_header_image_file_location_);
546 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -0700547 DO_STAT(size_interpreter_to_interpreter_bridge_);
548 DO_STAT(size_interpreter_to_compiled_code_bridge_);
549 DO_STAT(size_jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -0700550 DO_STAT(size_portable_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700551 DO_STAT(size_portable_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700552 DO_STAT(size_portable_to_interpreter_bridge_);
Jeff Hao88474b42013-10-23 16:24:40 -0700553 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700554 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700555 DO_STAT(size_quick_to_interpreter_bridge_);
556 DO_STAT(size_trampoline_alignment_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700557 DO_STAT(size_code_size_);
558 DO_STAT(size_code_);
559 DO_STAT(size_code_alignment_);
560 DO_STAT(size_mapping_table_);
561 DO_STAT(size_vmap_table_);
562 DO_STAT(size_gc_map_);
563 DO_STAT(size_oat_dex_file_location_size_);
564 DO_STAT(size_oat_dex_file_location_data_);
565 DO_STAT(size_oat_dex_file_location_checksum_);
566 DO_STAT(size_oat_dex_file_offset_);
567 DO_STAT(size_oat_dex_file_methods_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700568 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700569 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700570 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700571 DO_STAT(size_oat_class_method_offsets_);
572 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700573
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700574 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700575 CHECK_EQ(file_offset + size_total, static_cast<uint32_t>(out.Seek(0, kSeekCurrent)));
576 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700577 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700578
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700579 CHECK_EQ(file_offset + size_, static_cast<uint32_t>(out.Seek(0, kSeekCurrent)));
580 CHECK_EQ(size_, relative_offset);
581
Brian Carlstrome24fa612011-09-29 00:53:55 -0700582 return true;
583}
584
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700585bool OatWriter::WriteTables(OutputStream& out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700586 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700587 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800588 PLOG(ERROR) << "Failed to write oat dex information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700589 return false;
590 }
591 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800592 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700593 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800594 off_t actual_offset = out.Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -0800595 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
596 const DexFile* dex_file = (*dex_files_)[i];
597 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
598 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
599 return false;
600 }
601 const DexFile* dex_file = (*dex_files_)[i];
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800602 if (!out.WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700603 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
604 << " to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800605 return false;
606 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700607 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800608 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800609 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700610 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800611 PLOG(ERROR) << "Failed to write oat methods information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700612 return false;
613 }
614 }
615 return true;
616}
617
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700618size_t OatWriter::WriteCode(OutputStream& out, const size_t file_offset) {
619 size_t relative_offset = oat_header_->GetExecutableOffset();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700620 off_t new_offset = out.Seek(size_executable_offset_alignment_, kSeekCurrent);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700621 size_t expected_file_offset = file_offset + relative_offset;
622 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700623 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700624 << " Expected: " << expected_file_offset << " File: " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700625 return 0;
626 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800627 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700628 if (compiler_driver_->IsImage()) {
629 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700630
Ian Rogers848871b2013-08-05 10:56:33 -0700631 #define DO_TRAMPOLINE(field) \
632 do { \
633 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
634 uint32_t alignment_padding = aligned_offset - relative_offset; \
635 out.Seek(alignment_padding, kSeekCurrent); \
636 size_trampoline_alignment_ += alignment_padding; \
637 if (!out.WriteFully(&(*field)[0], field->size())) { \
638 PLOG(ERROR) << "Failed to write " # field " to " << out.GetLocation(); \
639 return false; \
640 } \
641 size_ ## field += field->size(); \
642 relative_offset += alignment_padding + field->size(); \
643 DCHECK_OFFSET(); \
644 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700645
Ian Rogers848871b2013-08-05 10:56:33 -0700646 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_);
647 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_);
648 DO_TRAMPOLINE(jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -0700649 DO_TRAMPOLINE(portable_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700650 DO_TRAMPOLINE(portable_resolution_trampoline_);
651 DO_TRAMPOLINE(portable_to_interpreter_bridge_);
Jeff Hao88474b42013-10-23 16:24:40 -0700652 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700653 DO_TRAMPOLINE(quick_resolution_trampoline_);
654 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
655 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700656 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700657 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700658}
659
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700660size_t OatWriter::WriteCodeDexFiles(OutputStream& out,
661 const size_t file_offset,
662 size_t relative_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700663 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800664 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700665 const DexFile* dex_file = (*dex_files_)[i];
666 CHECK(dex_file != NULL);
Brian Carlstromba150c32013-08-27 17:31:03 -0700667 relative_offset = WriteCodeDexFile(out, file_offset, relative_offset, &oat_class_index,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700668 *dex_file);
669 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700670 return 0;
671 }
672 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700673 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700674}
675
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700676size_t OatWriter::WriteCodeDexFile(OutputStream& out, const size_t file_offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700677 size_t relative_offset, size_t* oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700678 const DexFile& dex_file) {
679 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
Brian Carlstromba150c32013-08-27 17:31:03 -0700680 class_def_index++, (*oat_class_index)++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700681 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstromba150c32013-08-27 17:31:03 -0700682 relative_offset = WriteCodeClassDef(out, file_offset, relative_offset, *oat_class_index,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700683 dex_file, class_def);
684 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700685 return 0;
686 }
687 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700688 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700689}
690
Ian Rogers0571d352011-11-03 19:51:38 -0700691void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800692 const DexFile& dex_file, OutputStream& out) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700693 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800694 << " to " << out.GetLocation();
Elliott Hughes234da572011-11-03 22:13:06 -0700695}
696
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800697size_t OatWriter::WriteCodeClassDef(OutputStream& out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700698 const size_t file_offset,
699 size_t relative_offset,
700 size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700701 const DexFile& dex_file,
702 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700703 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700704 if (class_data == NULL) {
705 // ie. an empty class such as a marker interface
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700706 return relative_offset;
Ian Rogers387b6992011-10-31 17:52:37 -0700707 }
Ian Rogers0571d352011-11-03 19:51:38 -0700708 ClassDataItemIterator it(dex_file, class_data);
709 // Skip fields
710 while (it.HasNextStaticField()) {
711 it.Next();
712 }
713 while (it.HasNextInstanceField()) {
714 it.Next();
715 }
716 // Process methods
717 size_t class_def_method_index = 0;
Brian Carlstromba150c32013-08-27 17:31:03 -0700718 size_t method_offsets_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700719 while (it.HasNextDirectMethod()) {
720 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700721 relative_offset = WriteCodeMethod(out, file_offset, relative_offset, oat_class_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700722 class_def_method_index, &method_offsets_index, is_static,
723 it.GetMemberIndex(), dex_file);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700724 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700725 return 0;
726 }
Ian Rogers0571d352011-11-03 19:51:38 -0700727 class_def_method_index++;
728 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700729 }
Ian Rogers0571d352011-11-03 19:51:38 -0700730 while (it.HasNextVirtualMethod()) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700731 relative_offset = WriteCodeMethod(out, file_offset, relative_offset, oat_class_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700732 class_def_method_index, &method_offsets_index, false,
733 it.GetMemberIndex(), dex_file);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700734 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700735 return 0;
736 }
Ian Rogers0571d352011-11-03 19:51:38 -0700737 class_def_method_index++;
738 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700739 }
Brian Carlstromba150c32013-08-27 17:31:03 -0700740 DCHECK(!it.HasNext());
741 CHECK_LE(method_offsets_index, class_def_method_index);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700742 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700743}
744
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700745size_t OatWriter::WriteCodeMethod(OutputStream& out, const size_t file_offset,
746 size_t relative_offset, size_t oat_class_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700747 size_t class_def_method_index, size_t* method_offsets_index,
748 bool is_static, uint32_t method_idx, const DexFile& dex_file) {
749 OatClass* oat_class = oat_classes_[oat_class_index];
750 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700751
752 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstromba150c32013-08-27 17:31:03 -0700753 const OatMethodOffsets method_offsets = oat_class->method_offsets_[*method_offsets_index];
754 (*method_offsets_index)++;
755
Brian Carlstrom265091e2013-01-30 14:08:26 -0800756#if !defined(ART_USE_PORTABLE_COMPILER)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700757 uint32_t aligned_offset = compiled_method->AlignCode(relative_offset);
758 uint32_t aligned_code_delta = aligned_offset - relative_offset;
Logan Chien971bf3f2012-05-01 15:47:55 +0800759 if (aligned_code_delta != 0) {
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800760 off_t new_offset = out.Seek(aligned_code_delta, kSeekCurrent);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700761 size_code_alignment_ += aligned_code_delta;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700762 uint32_t expected_offset = file_offset + aligned_offset;
763 if (static_cast<uint32_t>(new_offset) != expected_offset) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800764 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700765 << " Expected: " << expected_offset << " File: " << out.GetLocation();
Logan Chien971bf3f2012-05-01 15:47:55 +0800766 return 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700767 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700768 relative_offset += aligned_code_delta;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800769 DCHECK_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700770 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700771 DCHECK_ALIGNED(relative_offset, kArmAlignment);
Logan Chien971bf3f2012-05-01 15:47:55 +0800772 const std::vector<uint8_t>& code = compiled_method->GetCode();
773 uint32_t code_size = code.size() * sizeof(code[0]);
774 CHECK_NE(code_size, 0U);
775
776 // Deduplicate code arrays
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700777 size_t code_offset = relative_offset + sizeof(code_size) + compiled_method->CodeDelta();
Logan Chien971bf3f2012-05-01 15:47:55 +0800778 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800779 if (code_iter != code_offsets_.end() && code_offset != method_offsets.code_offset_) {
780 DCHECK(code_iter->second == method_offsets.code_offset_)
781 << PrettyMethod(method_idx, dex_file);
Logan Chien971bf3f2012-05-01 15:47:55 +0800782 } else {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800783 DCHECK(code_offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800784 if (!out.WriteFully(&code_size, sizeof(code_size))) {
785 ReportWriteFailure("method code size", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800786 return 0;
787 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700788 size_code_size_ += sizeof(code_size);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700789 relative_offset += sizeof(code_size);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800790 DCHECK_OFFSET();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800791 if (!out.WriteFully(&code[0], code_size)) {
792 ReportWriteFailure("method code", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800793 return 0;
794 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700795 size_code_ += code_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700796 relative_offset += code_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800797 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800798 DCHECK_OFFSET();
799#endif
Logan Chien971bf3f2012-05-01 15:47:55 +0800800
Ian Rogers1809a722013-08-09 22:05:32 -0700801 const std::vector<uint8_t>& mapping_table = compiled_method->GetMappingTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800802 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
803
804 // Deduplicate mapping tables
Ian Rogers1809a722013-08-09 22:05:32 -0700805 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator mapping_iter =
Logan Chien971bf3f2012-05-01 15:47:55 +0800806 mapping_table_offsets_.find(&mapping_table);
807 if (mapping_iter != mapping_table_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700808 relative_offset != method_offsets.mapping_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800809 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
810 || mapping_iter->second == method_offsets.mapping_table_offset_)
811 << PrettyMethod(method_idx, dex_file);
812 } else {
813 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700814 || relative_offset == method_offsets.mapping_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800815 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800816 if (!out.WriteFully(&mapping_table[0], mapping_table_size)) {
817 ReportWriteFailure("mapping table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800818 return 0;
819 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700820 size_mapping_table_ += mapping_table_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700821 relative_offset += mapping_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800822 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800823 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800824
Ian Rogers1809a722013-08-09 22:05:32 -0700825 const std::vector<uint8_t>& vmap_table = compiled_method->GetVmapTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800826 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
827
828 // Deduplicate vmap tables
Ian Rogers1809a722013-08-09 22:05:32 -0700829 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator vmap_iter =
Logan Chien971bf3f2012-05-01 15:47:55 +0800830 vmap_table_offsets_.find(&vmap_table);
831 if (vmap_iter != vmap_table_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700832 relative_offset != method_offsets.vmap_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800833 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
834 || vmap_iter->second == method_offsets.vmap_table_offset_)
835 << PrettyMethod(method_idx, dex_file);
836 } else {
837 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700838 || relative_offset == method_offsets.vmap_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800839 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800840 if (!out.WriteFully(&vmap_table[0], vmap_table_size)) {
841 ReportWriteFailure("vmap table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800842 return 0;
843 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700844 size_vmap_table_ += vmap_table_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700845 relative_offset += vmap_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800846 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800847 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800848
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700849 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800850 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
851
852 // Deduplicate GC maps
853 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
854 gc_map_offsets_.find(&gc_map);
855 if (gc_map_iter != gc_map_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700856 relative_offset != method_offsets.gc_map_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800857 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
858 || gc_map_iter->second == method_offsets.gc_map_offset_)
859 << PrettyMethod(method_idx, dex_file);
860 } else {
861 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700862 || relative_offset == method_offsets.gc_map_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800863 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800864 if (!out.WriteFully(&gc_map[0], gc_map_size)) {
865 ReportWriteFailure("GC map", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800866 return 0;
867 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700868 size_gc_map_ += gc_map_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700869 relative_offset += gc_map_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800870 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800871 DCHECK_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700872 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800873
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700874 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700875}
876
Brian Carlstrom265091e2013-01-30 14:08:26 -0800877OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
878 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -0800879 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700880 dex_file_location_size_ = location.size();
881 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800882 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800883 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800884 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700885}
886
887size_t OatWriter::OatDexFile::SizeOf() const {
888 return sizeof(dex_file_location_size_)
889 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800890 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800891 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800892 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700893}
894
895void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
896 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
897 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800898 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800899 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800900 oat_header.UpdateChecksum(&methods_offsets_[0],
901 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700902}
903
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700904bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
905 OutputStream& out,
906 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800907 DCHECK_OFFSET_();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800908 if (!out.WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
909 PLOG(ERROR) << "Failed to write dex file location length to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700910 return false;
911 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700912 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800913 if (!out.WriteFully(dex_file_location_data_, dex_file_location_size_)) {
914 PLOG(ERROR) << "Failed to write dex file location data 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_dex_file_location_data_ += dex_file_location_size_;
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800918 if (!out.WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
919 PLOG(ERROR) << "Failed to write dex file location checksum to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700920 return false;
921 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700922 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800923 if (!out.WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
924 PLOG(ERROR) << "Failed to write dex file offset to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800925 return false;
926 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700927 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800928 if (!out.WriteFully(&methods_offsets_[0],
929 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
930 PLOG(ERROR) << "Failed to write methods offsets to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700931 return false;
932 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700933 oat_writer->size_oat_dex_file_methods_offsets_ +=
934 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700935 return true;
936}
937
Brian Carlstromba150c32013-08-27 17:31:03 -0700938OatWriter::OatClass::OatClass(size_t offset,
939 std::vector<CompiledMethod*>* compiled_methods,
940 uint32_t num_non_null_compiled_methods,
941 mirror::Class::Status status) {
942 CHECK(compiled_methods != NULL);
943 uint32_t num_methods = compiled_methods->size();
944 CHECK_LE(num_non_null_compiled_methods, num_methods);
945
Brian Carlstrom265091e2013-01-30 14:08:26 -0800946 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -0700947 compiled_methods_ = compiled_methods;
948 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
949
950 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
951 // apply when there are 0 methods, we just arbitrarily say that 0
952 // methods means kOatClassNoneCompiled and that we won't use
953 // kOatClassAllCompiled unless there is at least one compiled
954 // method. This means in an interpretter only system, we can assert
955 // that all classes are kOatClassNoneCompiled.
956 if (num_non_null_compiled_methods == 0) {
957 type_ = kOatClassNoneCompiled;
958 } else if (num_non_null_compiled_methods == num_methods) {
959 type_ = kOatClassAllCompiled;
960 } else {
961 type_ = kOatClassSomeCompiled;
962 }
963
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800964 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -0700965 method_offsets_.resize(num_non_null_compiled_methods);
966
967 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
968 if (type_ == kOatClassSomeCompiled) {
969 method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator());
970 method_bitmap_size_ = method_bitmap_->GetSizeOf();
971 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
972 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
973 } else {
974 method_bitmap_ = NULL;
975 method_bitmap_size_ = 0;
976 }
977
978 for (size_t i = 0; i < num_methods; i++) {
979 CompiledMethod* compiled_method = (*compiled_methods_)[i];
980 if (compiled_method == NULL) {
981 oat_method_offsets_offsets_from_oat_class_[i] = 0;
982 } else {
983 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
984 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
985 if (type_ == kOatClassSomeCompiled) {
986 method_bitmap_->SetBit(i);
987 }
988 }
989 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700990}
991
Brian Carlstromba150c32013-08-27 17:31:03 -0700992OatWriter::OatClass::~OatClass() {
993 delete compiled_methods_;
994}
995
996#if defined(ART_USE_PORTABLE_COMPILER)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800997size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
998 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -0700999 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
1000 if (method_offset == 0) {
1001 return 0;
1002 }
1003 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001004}
1005
1006size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
1007 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001008 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08001009}
Brian Carlstromba150c32013-08-27 17:31:03 -07001010#endif
Brian Carlstrom265091e2013-01-30 14:08:26 -08001011
1012size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001013 return sizeof(status_)
1014 + sizeof(type_)
1015 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
1016 + method_bitmap_size_
1017 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001018}
1019
Brian Carlstrom389efb02012-01-11 12:06:26 -08001020void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001021 oat_header.UpdateChecksum(&status_, sizeof(status_));
Brian Carlstromba150c32013-08-27 17:31:03 -07001022 oat_header.UpdateChecksum(&type_, sizeof(type_));
1023 if (method_bitmap_size_ != 0) {
1024 CHECK_EQ(kOatClassSomeCompiled, type_);
1025 oat_header.UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_));
1026 oat_header.UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_);
1027 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001028 oat_header.UpdateChecksum(&method_offsets_[0],
1029 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001030}
1031
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001032bool OatWriter::OatClass::Write(OatWriter* oat_writer,
1033 OutputStream& out,
1034 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001035 DCHECK_OFFSET_();
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001036 if (!out.WriteFully(&status_, sizeof(status_))) {
1037 PLOG(ERROR) << "Failed to write class status to " << out.GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001038 return false;
1039 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001040 oat_writer->size_oat_class_status_ += sizeof(status_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001041 if (!out.WriteFully(&type_, sizeof(type_))) {
1042 PLOG(ERROR) << "Failed to write oat class type to " << out.GetLocation();
1043 return false;
1044 }
1045 oat_writer->size_oat_class_type_ += sizeof(type_);
1046 if (method_bitmap_size_ != 0) {
1047 CHECK_EQ(kOatClassSomeCompiled, type_);
1048 if (!out.WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
1049 PLOG(ERROR) << "Failed to write method bitmap size to " << out.GetLocation();
1050 return false;
1051 }
1052 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
1053 if (!out.WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
1054 PLOG(ERROR) << "Failed to write method bitmap to " << out.GetLocation();
1055 return false;
1056 }
1057 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1058 }
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001059 if (!out.WriteFully(&method_offsets_[0],
1060 sizeof(method_offsets_[0]) * method_offsets_.size())) {
1061 PLOG(ERROR) << "Failed to write method offsets to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001062 return false;
1063 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001064 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001065 return true;
1066}
1067
Brian Carlstrome24fa612011-09-29 00:53:55 -07001068} // namespace art