blob: f3bb11272ec345c7a7641e845bea0915f41f2934 [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 Rogers1212a022013-03-04 10:48:41 -080043 const CompilerDriver* compiler)
Jeff Hao0aba0ba2013-06-03 14:49:28 -070044 : compiler_driver_(compiler),
45 dex_files_(&dex_files),
46 image_file_location_oat_checksum_(image_file_location_oat_checksum),
47 image_file_location_oat_begin_(image_file_location_oat_begin),
48 image_file_location_(image_file_location),
49 oat_header_(NULL),
50 size_dex_file_alignment_(0),
51 size_executable_offset_alignment_(0),
52 size_oat_header_(0),
53 size_oat_header_image_file_location_(0),
54 size_dex_file_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070055 size_interpreter_to_interpreter_bridge_(0),
56 size_interpreter_to_compiled_code_bridge_(0),
57 size_jni_dlsym_lookup_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070058 size_portable_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070059 size_portable_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070060 size_portable_to_interpreter_bridge_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070061 size_quick_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070062 size_quick_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070063 size_quick_to_interpreter_bridge_(0),
64 size_trampoline_alignment_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070065 size_code_size_(0),
66 size_code_(0),
67 size_code_alignment_(0),
68 size_mapping_table_(0),
69 size_vmap_table_(0),
70 size_gc_map_(0),
71 size_oat_dex_file_location_size_(0),
72 size_oat_dex_file_location_data_(0),
73 size_oat_dex_file_location_checksum_(0),
74 size_oat_dex_file_offset_(0),
75 size_oat_dex_file_methods_offsets_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -070076 size_oat_class_type_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070077 size_oat_class_status_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -070078 size_oat_class_method_bitmaps_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070079 size_oat_class_method_offsets_(0) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070080 size_t offset = InitOatHeader();
Brian Carlstrome24fa612011-09-29 00:53:55 -070081 offset = InitOatDexFiles(offset);
Brian Carlstrom89521892011-12-07 22:05:07 -080082 offset = InitDexFiles(offset);
Brian Carlstrom389efb02012-01-11 12:06:26 -080083 offset = InitOatClasses(offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -070084 offset = InitOatCode(offset);
85 offset = InitOatCodeDexFiles(offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -070086 size_ = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -070087
88 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Jeff Hao0aba0ba2013-06-03 14:49:28 -070089 CHECK(image_file_location.empty() == compiler->IsImage());
Brian Carlstrome24fa612011-09-29 00:53:55 -070090}
91
Ian Rogers0571d352011-11-03 19:51:38 -070092OatWriter::~OatWriter() {
93 delete oat_header_;
94 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -080095 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -070096}
97
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070098size_t OatWriter::InitOatHeader() {
Brian Carlstrome24fa612011-09-29 00:53:55 -070099 // create the OatHeader
Ian Rogers1212a022013-03-04 10:48:41 -0800100 oat_header_ = new OatHeader(compiler_driver_->GetInstructionSet(),
Dave Allison70202782013-10-22 17:52:19 -0700101 compiler_driver_->GetInstructionSetFeatures(),
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700102 dex_files_,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700103 image_file_location_oat_checksum_,
104 image_file_location_oat_begin_,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700105 image_file_location_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700106 size_t offset = sizeof(*oat_header_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700107 offset += image_file_location_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700108 return offset;
109}
110
111size_t OatWriter::InitOatDexFiles(size_t offset) {
112 // create the OatDexFiles
113 for (size_t i = 0; i != dex_files_->size(); ++i) {
114 const DexFile* dex_file = (*dex_files_)[i];
115 CHECK(dex_file != NULL);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800116 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700117 oat_dex_files_.push_back(oat_dex_file);
118 offset += oat_dex_file->SizeOf();
119 }
120 return offset;
121}
122
Brian Carlstrom89521892011-12-07 22:05:07 -0800123size_t OatWriter::InitDexFiles(size_t offset) {
124 // calculate the offsets within OatDexFiles to the DexFiles
125 for (size_t i = 0; i != dex_files_->size(); ++i) {
126 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700127 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800128 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700129 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800130
131 // set offset in OatDexFile to DexFile
132 oat_dex_files_[i]->dex_file_offset_ = offset;
133
134 const DexFile* dex_file = (*dex_files_)[i];
135 offset += dex_file->GetHeader().file_size_;
136 }
137 return offset;
138}
139
Brian Carlstrom389efb02012-01-11 12:06:26 -0800140size_t OatWriter::InitOatClasses(size_t offset) {
141 // create the OatClasses
142 // calculate the offsets within OatDexFiles to OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -0700143 for (size_t i = 0; i != dex_files_->size(); ++i) {
144 const DexFile* dex_file = (*dex_files_)[i];
145 for (size_t class_def_index = 0;
146 class_def_index < dex_file->NumClassDefs();
Ian Rogersc20a83e2012-01-18 18:15:32 -0800147 class_def_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800148 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700149 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
150 const byte* class_data = dex_file->GetClassData(class_def);
Brian Carlstromba150c32013-08-27 17:31:03 -0700151 uint32_t num_non_null_compiled_methods = 0;
152 UniquePtr<std::vector<CompiledMethod*> > compiled_methods(new std::vector<CompiledMethod*>());
Ian Rogers0571d352011-11-03 19:51:38 -0700153 if (class_data != NULL) { // ie not an empty class, such as a marker interface
154 ClassDataItemIterator it(*dex_file, class_data);
155 size_t num_direct_methods = it.NumDirectMethods();
156 size_t num_virtual_methods = it.NumVirtualMethods();
Brian Carlstromba150c32013-08-27 17:31:03 -0700157 size_t num_methods = num_direct_methods + num_virtual_methods;
158
159 // Fill in the compiled_methods_ array for methods that have a
160 // CompiledMethod. We track the number of non-null entries in
161 // num_non_null_compiled_methods since we only want to allocate
162 // OatMethodOffsets for the compiled methods.
163 compiled_methods->reserve(num_methods);
164 while (it.HasNextStaticField()) {
165 it.Next();
166 }
167 while (it.HasNextInstanceField()) {
168 it.Next();
169 }
170 size_t class_def_method_index = 0;
171 while (it.HasNextDirectMethod()) {
172 uint32_t method_idx = it.GetMemberIndex();
173 CompiledMethod* compiled_method =
174 compiler_driver_->GetCompiledMethod(MethodReference(dex_file, method_idx));
175 compiled_methods->push_back(compiled_method);
176 if (compiled_method != NULL) {
177 num_non_null_compiled_methods++;
178 }
179 class_def_method_index++;
180 it.Next();
181 }
182 while (it.HasNextVirtualMethod()) {
183 uint32_t method_idx = it.GetMemberIndex();
184 CompiledMethod* compiled_method =
185 compiler_driver_->GetCompiledMethod(MethodReference(dex_file, method_idx));
186 compiled_methods->push_back(compiled_method);
187 if (compiled_method != NULL) {
188 num_non_null_compiled_methods++;
189 }
190 class_def_method_index++;
191 it.Next();
192 }
Ian Rogers0571d352011-11-03 19:51:38 -0700193 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800194
Brian Carlstrom51c24672013-07-11 16:00:56 -0700195 ClassReference class_ref(dex_file, class_def_index);
Ian Rogers1212a022013-03-04 10:48:41 -0800196 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800197 mirror::Class::Status status;
jeffhaoec014232012-09-05 10:42:25 -0700198 if (compiled_class != NULL) {
199 status = compiled_class->GetStatus();
200 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800201 status = mirror::Class::kStatusError;
jeffhaoec014232012-09-05 10:42:25 -0700202 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800203 status = mirror::Class::kStatusNotReady;
jeffhaoec014232012-09-05 10:42:25 -0700204 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800205
Brian Carlstromba150c32013-08-27 17:31:03 -0700206 OatClass* oat_class = new OatClass(offset, compiled_methods.release(),
207 num_non_null_compiled_methods, status);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800208 oat_classes_.push_back(oat_class);
209 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700210 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800211 oat_dex_files_[i]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700212 }
213 return offset;
214}
215
216size_t OatWriter::InitOatCode(size_t offset) {
217 // calculate the offsets within OatHeader to executable code
218 size_t old_offset = offset;
219 // required to be on a new page boundary
220 offset = RoundUp(offset, kPageSize);
221 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700222 size_executable_offset_alignment_ = offset - old_offset;
223 if (compiler_driver_->IsImage()) {
224 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700225
Ian Rogers848871b2013-08-05 10:56:33 -0700226 #define DO_TRAMPOLINE(field, fn_name) \
227 offset = CompiledCode::AlignCode(offset, instruction_set); \
228 oat_header_->Set ## fn_name ## Offset(offset); \
229 field.reset(compiler_driver_->Create ## fn_name()); \
230 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700231
Ian Rogers848871b2013-08-05 10:56:33 -0700232 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge);
233 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge);
234 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Jeff Hao88474b42013-10-23 16:24:40 -0700235 DO_TRAMPOLINE(portable_imt_conflict_trampoline_, PortableImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700236 DO_TRAMPOLINE(portable_resolution_trampoline_, PortableResolutionTrampoline);
237 DO_TRAMPOLINE(portable_to_interpreter_bridge_, PortableToInterpreterBridge);
Jeff Hao88474b42013-10-23 16:24:40 -0700238 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700239 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
240 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700241
Ian Rogers848871b2013-08-05 10:56:33 -0700242 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700243 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700244 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
245 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
246 oat_header_->SetJniDlsymLookupOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700247 oat_header_->SetPortableImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700248 oat_header_->SetPortableResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700249 oat_header_->SetPortableToInterpreterBridgeOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700250 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700251 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700252 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700253 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700254 return offset;
255}
256
257size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700258 size_t oat_class_index = 0;
259 for (size_t i = 0; i != dex_files_->size(); ++i) {
260 const DexFile* dex_file = (*dex_files_)[i];
261 CHECK(dex_file != NULL);
Brian Carlstromba150c32013-08-27 17:31:03 -0700262 offset = InitOatCodeDexFile(offset, &oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700263 }
264 return offset;
265}
266
267size_t OatWriter::InitOatCodeDexFile(size_t offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700268 size_t* oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700269 const DexFile& dex_file) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800270 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700271 class_def_index < dex_file.NumClassDefs();
Brian Carlstromba150c32013-08-27 17:31:03 -0700272 class_def_index++, (*oat_class_index)++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700273 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstromba150c32013-08-27 17:31:03 -0700274 offset = InitOatCodeClassDef(offset, *oat_class_index, class_def_index, dex_file, class_def);
275 oat_classes_[*oat_class_index]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700276 }
277 return offset;
278}
279
280size_t OatWriter::InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800281 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700282 const DexFile& dex_file,
283 const DexFile::ClassDef& class_def) {
284 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700285 if (class_data == NULL) {
286 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700287 return offset;
288 }
Ian Rogers0571d352011-11-03 19:51:38 -0700289 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstromba150c32013-08-27 17:31:03 -0700290 CHECK_LE(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700291 it.NumDirectMethods() + it.NumVirtualMethods());
292 // Skip fields
293 while (it.HasNextStaticField()) {
294 it.Next();
295 }
296 while (it.HasNextInstanceField()) {
297 it.Next();
298 }
299 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700300 size_t class_def_method_index = 0;
Brian Carlstromba150c32013-08-27 17:31:03 -0700301 size_t method_offsets_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700302 while (it.HasNextDirectMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800303 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700304 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700305 &method_offsets_index, is_native,
306 it.GetMethodInvokeType(class_def), it.GetMemberIndex(), dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700307 class_def_method_index++;
308 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700309 }
Ian Rogers0571d352011-11-03 19:51:38 -0700310 while (it.HasNextVirtualMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800311 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700312 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700313 &method_offsets_index, is_native,
314 it.GetMethodInvokeType(class_def), it.GetMemberIndex(), dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700315 class_def_method_index++;
316 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700317 }
Ian Rogers0571d352011-11-03 19:51:38 -0700318 DCHECK(!it.HasNext());
Brian Carlstromba150c32013-08-27 17:31:03 -0700319 CHECK_LE(method_offsets_index, class_def_method_index);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700320 return offset;
321}
322
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700323size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
324 size_t __attribute__((unused)) class_def_index,
325 size_t class_def_method_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700326 size_t* method_offsets_index,
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700327 bool __attribute__((unused)) is_native,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800328 InvokeType invoke_type,
Brian Carlstromba150c32013-08-27 17:31:03 -0700329 uint32_t method_idx, const DexFile& dex_file) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700330 // derived from CompiledMethod if available
331 uint32_t code_offset = 0;
332 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700333 uint32_t core_spill_mask = 0;
334 uint32_t fp_spill_mask = 0;
335 uint32_t mapping_table_offset = 0;
336 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800337 uint32_t gc_map_offset = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700338
Brian Carlstrom265091e2013-01-30 14:08:26 -0800339 OatClass* oat_class = oat_classes_[oat_class_index];
340#if defined(ART_USE_PORTABLE_COMPILER)
341 size_t oat_method_offsets_offset =
342 oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index);
343#endif
344
Brian Carlstromba150c32013-08-27 17:31:03 -0700345 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700346 if (compiled_method != NULL) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800347#if defined(ART_USE_PORTABLE_COMPILER)
348 compiled_method->AddOatdataOffsetToCompliledCodeOffset(
349 oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_));
350#else
351 const std::vector<uint8_t>& code = compiled_method->GetCode();
Logan Chien971bf3f2012-05-01 15:47:55 +0800352 offset = compiled_method->AlignCode(offset);
353 DCHECK_ALIGNED(offset, kArmAlignment);
Logan Chien971bf3f2012-05-01 15:47:55 +0800354 uint32_t code_size = code.size() * sizeof(code[0]);
355 CHECK_NE(code_size, 0U);
356 uint32_t thumb_offset = compiled_method->CodeDelta();
357 code_offset = offset + sizeof(code_size) + thumb_offset;
358
359 // Deduplicate code arrays
360 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
361 if (code_iter != code_offsets_.end()) {
362 code_offset = code_iter->second;
jeffhao55d78212011-11-02 11:41:50 -0700363 } else {
Logan Chien971bf3f2012-05-01 15:47:55 +0800364 code_offsets_.Put(&code, code_offset);
365 offset += sizeof(code_size); // code size is prepended before code
366 offset += code_size;
367 oat_header_->UpdateChecksum(&code[0], code_size);
368 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800369#endif
Logan Chien971bf3f2012-05-01 15:47:55 +0800370 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
371 core_spill_mask = compiled_method->GetCoreSpillMask();
372 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700373
Ian Rogers1809a722013-08-09 22:05:32 -0700374 const std::vector<uint8_t>& mapping_table = compiled_method->GetMappingTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800375 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
376 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700377
Logan Chien971bf3f2012-05-01 15:47:55 +0800378 // Deduplicate mapping tables
Ian Rogers1809a722013-08-09 22:05:32 -0700379 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator mapping_iter =
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700380 mapping_table_offsets_.find(&mapping_table);
Logan Chien971bf3f2012-05-01 15:47:55 +0800381 if (mapping_iter != mapping_table_offsets_.end()) {
382 mapping_table_offset = mapping_iter->second;
383 } else {
384 mapping_table_offsets_.Put(&mapping_table, mapping_table_offset);
385 offset += mapping_table_size;
386 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
387 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700388
Ian Rogers1809a722013-08-09 22:05:32 -0700389 const std::vector<uint8_t>& vmap_table = compiled_method->GetVmapTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800390 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
391 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700392
Logan Chien971bf3f2012-05-01 15:47:55 +0800393 // Deduplicate vmap tables
Ian Rogers1809a722013-08-09 22:05:32 -0700394 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator vmap_iter =
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700395 vmap_table_offsets_.find(&vmap_table);
Logan Chien971bf3f2012-05-01 15:47:55 +0800396 if (vmap_iter != vmap_table_offsets_.end()) {
397 vmap_table_offset = vmap_iter->second;
398 } else {
399 vmap_table_offsets_.Put(&vmap_table, vmap_table_offset);
400 offset += vmap_table_size;
401 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
402 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800403
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700404 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800405 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
406 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
Logan Chienccb7bf12012-03-28 12:52:32 +0800407
TDYa127ce4cc0d2012-11-18 16:59:53 -0800408#if !defined(NDEBUG)
Logan Chien971bf3f2012-05-01 15:47:55 +0800409 // We expect GC maps except when the class hasn't been verified or the method is native
Brian Carlstromba150c32013-08-27 17:31:03 -0700410 ClassReference class_ref(&dex_file, class_def_index);
Ian Rogers1212a022013-03-04 10:48:41 -0800411 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800412 mirror::Class::Status status;
jeffhaoec014232012-09-05 10:42:25 -0700413 if (compiled_class != NULL) {
414 status = compiled_class->GetStatus();
415 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800416 status = mirror::Class::kStatusError;
jeffhaoec014232012-09-05 10:42:25 -0700417 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800418 status = mirror::Class::kStatusNotReady;
jeffhaoec014232012-09-05 10:42:25 -0700419 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800420 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
421 << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
422 << (status < mirror::Class::kStatusVerified) << " " << status << " "
Brian Carlstromba150c32013-08-27 17:31:03 -0700423 << PrettyMethod(method_idx, dex_file);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800424#endif
425
Logan Chien971bf3f2012-05-01 15:47:55 +0800426 // Deduplicate GC maps
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700427 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
428 gc_map_offsets_.find(&gc_map);
Logan Chien971bf3f2012-05-01 15:47:55 +0800429 if (gc_map_iter != gc_map_offsets_.end()) {
430 gc_map_offset = gc_map_iter->second;
431 } else {
432 gc_map_offsets_.Put(&gc_map, gc_map_offset);
433 offset += gc_map_size;
434 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800435 }
Brian Carlstromba150c32013-08-27 17:31:03 -0700436
437 oat_class->method_offsets_[*method_offsets_index] =
438 OatMethodOffsets(code_offset,
439 frame_size_in_bytes,
440 core_spill_mask,
441 fp_spill_mask,
442 mapping_table_offset,
443 vmap_table_offset,
444 gc_map_offset);
445 (*method_offsets_index)++;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700446 }
447
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700448
Ian Rogers1212a022013-03-04 10:48:41 -0800449 if (compiler_driver_->IsImage()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700450 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Brian Carlstromba150c32013-08-27 17:31:03 -0700451 mirror::DexCache* dex_cache = linker->FindDexCache(dex_file);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700452 // Unchecked as we hold mutator_lock_ on entry.
453 ScopedObjectAccessUnchecked soa(Thread::Current());
Brian Carlstromba150c32013-08-27 17:31:03 -0700454 mirror::ArtMethod* method = linker->ResolveMethod(dex_file, method_idx, dex_cache,
455 NULL, NULL, invoke_type);
Ian Rogers0571d352011-11-03 19:51:38 -0700456 CHECK(method != NULL);
457 method->SetFrameSizeInBytes(frame_size_in_bytes);
458 method->SetCoreSpillMask(core_spill_mask);
459 method->SetFpSpillMask(fp_spill_mask);
460 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800461 // Don't overwrite static method trampoline
462 if (!method->IsStatic() || method->IsConstructor() ||
463 method->GetDeclaringClass()->IsInitialized()) {
464 method->SetOatCodeOffset(code_offset);
465 } else {
Jeff Haoaa4a7932013-05-13 11:28:27 -0700466 method->SetEntryPointFromCompiledCode(NULL);
Ian Rogers19846512012-02-24 11:42:47 -0800467 }
Ian Rogers0571d352011-11-03 19:51:38 -0700468 method->SetOatVmapTableOffset(vmap_table_offset);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700469 method->SetOatNativeGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700470 }
Logan Chien8b977d32012-02-21 19:14:55 +0800471
Brian Carlstrome24fa612011-09-29 00:53:55 -0700472 return offset;
473}
474
Brian Carlstrom265091e2013-01-30 14:08:26 -0800475#define DCHECK_OFFSET() \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700476 DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out.Seek(0, kSeekCurrent)) \
477 << "file_offset=" << file_offset << " relative_offset=" << relative_offset
Brian Carlstrom265091e2013-01-30 14:08:26 -0800478
479#define DCHECK_OFFSET_() \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700480 DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out.Seek(0, kSeekCurrent)) \
481 << "file_offset=" << file_offset << " offset_=" << offset_
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700482
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800483bool OatWriter::Write(OutputStream& out) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700484 const size_t file_offset = out.Seek(0, kSeekCurrent);
485
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800486 if (!out.WriteFully(oat_header_, sizeof(*oat_header_))) {
487 PLOG(ERROR) << "Failed to write oat header to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700488 return false;
489 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700490 size_oat_header_ += sizeof(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700491
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800492 if (!out.WriteFully(image_file_location_.data(), image_file_location_.size())) {
493 PLOG(ERROR) << "Failed to write oat header image file location to " << out.GetLocation();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700494 return false;
495 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700496 size_oat_header_image_file_location_ += image_file_location_.size();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700497
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700498 if (!WriteTables(out, file_offset)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800499 LOG(ERROR) << "Failed to write oat tables to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700500 return false;
501 }
502
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700503 size_t relative_offset = WriteCode(out, file_offset);
504 if (relative_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800505 LOG(ERROR) << "Failed to write oat code to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700506 return false;
507 }
508
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700509 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
510 if (relative_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800511 LOG(ERROR) << "Failed to write oat code for dex files to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700512 return false;
513 }
514
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700515 if (kIsDebugBuild) {
516 uint32_t size_total = 0;
517 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700518 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700519 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700520
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700521 DO_STAT(size_dex_file_alignment_);
522 DO_STAT(size_executable_offset_alignment_);
523 DO_STAT(size_oat_header_);
524 DO_STAT(size_oat_header_image_file_location_);
525 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -0700526 DO_STAT(size_interpreter_to_interpreter_bridge_);
527 DO_STAT(size_interpreter_to_compiled_code_bridge_);
528 DO_STAT(size_jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -0700529 DO_STAT(size_portable_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700530 DO_STAT(size_portable_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700531 DO_STAT(size_portable_to_interpreter_bridge_);
Jeff Hao88474b42013-10-23 16:24:40 -0700532 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700533 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700534 DO_STAT(size_quick_to_interpreter_bridge_);
535 DO_STAT(size_trampoline_alignment_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700536 DO_STAT(size_code_size_);
537 DO_STAT(size_code_);
538 DO_STAT(size_code_alignment_);
539 DO_STAT(size_mapping_table_);
540 DO_STAT(size_vmap_table_);
541 DO_STAT(size_gc_map_);
542 DO_STAT(size_oat_dex_file_location_size_);
543 DO_STAT(size_oat_dex_file_location_data_);
544 DO_STAT(size_oat_dex_file_location_checksum_);
545 DO_STAT(size_oat_dex_file_offset_);
546 DO_STAT(size_oat_dex_file_methods_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700547 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700548 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700549 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700550 DO_STAT(size_oat_class_method_offsets_);
551 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700552
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700553 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700554 CHECK_EQ(file_offset + size_total, static_cast<uint32_t>(out.Seek(0, kSeekCurrent)));
555 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700556 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700557
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700558 CHECK_EQ(file_offset + size_, static_cast<uint32_t>(out.Seek(0, kSeekCurrent)));
559 CHECK_EQ(size_, relative_offset);
560
Brian Carlstrome24fa612011-09-29 00:53:55 -0700561 return true;
562}
563
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700564bool OatWriter::WriteTables(OutputStream& out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700565 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700566 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800567 PLOG(ERROR) << "Failed to write oat dex information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700568 return false;
569 }
570 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800571 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700572 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800573 off_t actual_offset = out.Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -0800574 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
575 const DexFile* dex_file = (*dex_files_)[i];
576 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
577 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
578 return false;
579 }
580 const DexFile* dex_file = (*dex_files_)[i];
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800581 if (!out.WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700582 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
583 << " to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800584 return false;
585 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700586 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800587 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800588 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700589 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800590 PLOG(ERROR) << "Failed to write oat methods information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700591 return false;
592 }
593 }
594 return true;
595}
596
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700597size_t OatWriter::WriteCode(OutputStream& out, const size_t file_offset) {
598 size_t relative_offset = oat_header_->GetExecutableOffset();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700599 off_t new_offset = out.Seek(size_executable_offset_alignment_, kSeekCurrent);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700600 size_t expected_file_offset = file_offset + relative_offset;
601 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700602 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700603 << " Expected: " << expected_file_offset << " File: " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700604 return 0;
605 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800606 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700607 if (compiler_driver_->IsImage()) {
608 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700609
Ian Rogers848871b2013-08-05 10:56:33 -0700610 #define DO_TRAMPOLINE(field) \
611 do { \
612 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
613 uint32_t alignment_padding = aligned_offset - relative_offset; \
614 out.Seek(alignment_padding, kSeekCurrent); \
615 size_trampoline_alignment_ += alignment_padding; \
616 if (!out.WriteFully(&(*field)[0], field->size())) { \
617 PLOG(ERROR) << "Failed to write " # field " to " << out.GetLocation(); \
618 return false; \
619 } \
620 size_ ## field += field->size(); \
621 relative_offset += alignment_padding + field->size(); \
622 DCHECK_OFFSET(); \
623 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700624
Ian Rogers848871b2013-08-05 10:56:33 -0700625 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_);
626 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_);
627 DO_TRAMPOLINE(jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -0700628 DO_TRAMPOLINE(portable_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700629 DO_TRAMPOLINE(portable_resolution_trampoline_);
630 DO_TRAMPOLINE(portable_to_interpreter_bridge_);
Jeff Hao88474b42013-10-23 16:24:40 -0700631 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700632 DO_TRAMPOLINE(quick_resolution_trampoline_);
633 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
634 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700635 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700636 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700637}
638
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700639size_t OatWriter::WriteCodeDexFiles(OutputStream& out,
640 const size_t file_offset,
641 size_t relative_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700642 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800643 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700644 const DexFile* dex_file = (*dex_files_)[i];
645 CHECK(dex_file != NULL);
Brian Carlstromba150c32013-08-27 17:31:03 -0700646 relative_offset = WriteCodeDexFile(out, file_offset, relative_offset, &oat_class_index,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700647 *dex_file);
648 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700649 return 0;
650 }
651 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700652 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700653}
654
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700655size_t OatWriter::WriteCodeDexFile(OutputStream& out, const size_t file_offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700656 size_t relative_offset, size_t* oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700657 const DexFile& dex_file) {
658 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
Brian Carlstromba150c32013-08-27 17:31:03 -0700659 class_def_index++, (*oat_class_index)++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700660 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstromba150c32013-08-27 17:31:03 -0700661 relative_offset = WriteCodeClassDef(out, file_offset, relative_offset, *oat_class_index,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700662 dex_file, class_def);
663 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700664 return 0;
665 }
666 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700667 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700668}
669
Ian Rogers0571d352011-11-03 19:51:38 -0700670void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800671 const DexFile& dex_file, OutputStream& out) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700672 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800673 << " to " << out.GetLocation();
Elliott Hughes234da572011-11-03 22:13:06 -0700674}
675
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800676size_t OatWriter::WriteCodeClassDef(OutputStream& out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700677 const size_t file_offset,
678 size_t relative_offset,
679 size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700680 const DexFile& dex_file,
681 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700682 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700683 if (class_data == NULL) {
684 // ie. an empty class such as a marker interface
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700685 return relative_offset;
Ian Rogers387b6992011-10-31 17:52:37 -0700686 }
Ian Rogers0571d352011-11-03 19:51:38 -0700687 ClassDataItemIterator it(dex_file, class_data);
688 // Skip fields
689 while (it.HasNextStaticField()) {
690 it.Next();
691 }
692 while (it.HasNextInstanceField()) {
693 it.Next();
694 }
695 // Process methods
696 size_t class_def_method_index = 0;
Brian Carlstromba150c32013-08-27 17:31:03 -0700697 size_t method_offsets_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700698 while (it.HasNextDirectMethod()) {
699 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700700 relative_offset = WriteCodeMethod(out, file_offset, relative_offset, oat_class_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700701 class_def_method_index, &method_offsets_index, is_static,
702 it.GetMemberIndex(), dex_file);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700703 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700704 return 0;
705 }
Ian Rogers0571d352011-11-03 19:51:38 -0700706 class_def_method_index++;
707 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700708 }
Ian Rogers0571d352011-11-03 19:51:38 -0700709 while (it.HasNextVirtualMethod()) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700710 relative_offset = WriteCodeMethod(out, file_offset, relative_offset, oat_class_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700711 class_def_method_index, &method_offsets_index, false,
712 it.GetMemberIndex(), dex_file);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700713 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700714 return 0;
715 }
Ian Rogers0571d352011-11-03 19:51:38 -0700716 class_def_method_index++;
717 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700718 }
Brian Carlstromba150c32013-08-27 17:31:03 -0700719 DCHECK(!it.HasNext());
720 CHECK_LE(method_offsets_index, class_def_method_index);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700721 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700722}
723
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700724size_t OatWriter::WriteCodeMethod(OutputStream& out, const size_t file_offset,
725 size_t relative_offset, size_t oat_class_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700726 size_t class_def_method_index, size_t* method_offsets_index,
727 bool is_static, uint32_t method_idx, const DexFile& dex_file) {
728 OatClass* oat_class = oat_classes_[oat_class_index];
729 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700730
731 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstromba150c32013-08-27 17:31:03 -0700732 const OatMethodOffsets method_offsets = oat_class->method_offsets_[*method_offsets_index];
733 (*method_offsets_index)++;
734
Brian Carlstrom265091e2013-01-30 14:08:26 -0800735#if !defined(ART_USE_PORTABLE_COMPILER)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700736 uint32_t aligned_offset = compiled_method->AlignCode(relative_offset);
737 uint32_t aligned_code_delta = aligned_offset - relative_offset;
Logan Chien971bf3f2012-05-01 15:47:55 +0800738 if (aligned_code_delta != 0) {
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800739 off_t new_offset = out.Seek(aligned_code_delta, kSeekCurrent);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700740 size_code_alignment_ += aligned_code_delta;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700741 uint32_t expected_offset = file_offset + aligned_offset;
742 if (static_cast<uint32_t>(new_offset) != expected_offset) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800743 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700744 << " Expected: " << expected_offset << " File: " << out.GetLocation();
Logan Chien971bf3f2012-05-01 15:47:55 +0800745 return 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700746 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700747 relative_offset += aligned_code_delta;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800748 DCHECK_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700749 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700750 DCHECK_ALIGNED(relative_offset, kArmAlignment);
Logan Chien971bf3f2012-05-01 15:47:55 +0800751 const std::vector<uint8_t>& code = compiled_method->GetCode();
752 uint32_t code_size = code.size() * sizeof(code[0]);
753 CHECK_NE(code_size, 0U);
754
755 // Deduplicate code arrays
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700756 size_t code_offset = relative_offset + sizeof(code_size) + compiled_method->CodeDelta();
Logan Chien971bf3f2012-05-01 15:47:55 +0800757 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800758 if (code_iter != code_offsets_.end() && code_offset != method_offsets.code_offset_) {
759 DCHECK(code_iter->second == method_offsets.code_offset_)
760 << PrettyMethod(method_idx, dex_file);
Logan Chien971bf3f2012-05-01 15:47:55 +0800761 } else {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800762 DCHECK(code_offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800763 if (!out.WriteFully(&code_size, sizeof(code_size))) {
764 ReportWriteFailure("method code size", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800765 return 0;
766 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700767 size_code_size_ += sizeof(code_size);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700768 relative_offset += sizeof(code_size);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800769 DCHECK_OFFSET();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800770 if (!out.WriteFully(&code[0], code_size)) {
771 ReportWriteFailure("method code", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800772 return 0;
773 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700774 size_code_ += code_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700775 relative_offset += code_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800776 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800777 DCHECK_OFFSET();
778#endif
Logan Chien971bf3f2012-05-01 15:47:55 +0800779
Ian Rogers1809a722013-08-09 22:05:32 -0700780 const std::vector<uint8_t>& mapping_table = compiled_method->GetMappingTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800781 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
782
783 // Deduplicate mapping tables
Ian Rogers1809a722013-08-09 22:05:32 -0700784 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator mapping_iter =
Logan Chien971bf3f2012-05-01 15:47:55 +0800785 mapping_table_offsets_.find(&mapping_table);
786 if (mapping_iter != mapping_table_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700787 relative_offset != method_offsets.mapping_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800788 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
789 || mapping_iter->second == method_offsets.mapping_table_offset_)
790 << PrettyMethod(method_idx, dex_file);
791 } else {
792 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700793 || relative_offset == method_offsets.mapping_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800794 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800795 if (!out.WriteFully(&mapping_table[0], mapping_table_size)) {
796 ReportWriteFailure("mapping table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800797 return 0;
798 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700799 size_mapping_table_ += mapping_table_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700800 relative_offset += mapping_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800801 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800802 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800803
Ian Rogers1809a722013-08-09 22:05:32 -0700804 const std::vector<uint8_t>& vmap_table = compiled_method->GetVmapTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800805 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
806
807 // Deduplicate vmap tables
Ian Rogers1809a722013-08-09 22:05:32 -0700808 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator vmap_iter =
Logan Chien971bf3f2012-05-01 15:47:55 +0800809 vmap_table_offsets_.find(&vmap_table);
810 if (vmap_iter != vmap_table_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700811 relative_offset != method_offsets.vmap_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800812 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
813 || vmap_iter->second == method_offsets.vmap_table_offset_)
814 << PrettyMethod(method_idx, dex_file);
815 } else {
816 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700817 || relative_offset == method_offsets.vmap_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800818 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800819 if (!out.WriteFully(&vmap_table[0], vmap_table_size)) {
820 ReportWriteFailure("vmap table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800821 return 0;
822 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700823 size_vmap_table_ += vmap_table_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700824 relative_offset += vmap_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800825 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800826 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800827
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700828 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800829 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
830
831 // Deduplicate GC maps
832 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
833 gc_map_offsets_.find(&gc_map);
834 if (gc_map_iter != gc_map_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700835 relative_offset != method_offsets.gc_map_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800836 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
837 || gc_map_iter->second == method_offsets.gc_map_offset_)
838 << PrettyMethod(method_idx, dex_file);
839 } else {
840 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700841 || relative_offset == method_offsets.gc_map_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800842 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800843 if (!out.WriteFully(&gc_map[0], gc_map_size)) {
844 ReportWriteFailure("GC map", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800845 return 0;
846 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700847 size_gc_map_ += gc_map_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700848 relative_offset += gc_map_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800849 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800850 DCHECK_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700851 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800852
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700853 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700854}
855
Brian Carlstrom265091e2013-01-30 14:08:26 -0800856OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
857 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -0800858 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700859 dex_file_location_size_ = location.size();
860 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800861 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800862 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800863 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700864}
865
866size_t OatWriter::OatDexFile::SizeOf() const {
867 return sizeof(dex_file_location_size_)
868 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800869 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800870 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800871 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700872}
873
874void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
875 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
876 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800877 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800878 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800879 oat_header.UpdateChecksum(&methods_offsets_[0],
880 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700881}
882
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700883bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
884 OutputStream& out,
885 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800886 DCHECK_OFFSET_();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800887 if (!out.WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
888 PLOG(ERROR) << "Failed to write dex file location length to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700889 return false;
890 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700891 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800892 if (!out.WriteFully(dex_file_location_data_, dex_file_location_size_)) {
893 PLOG(ERROR) << "Failed to write dex file location data to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700894 return false;
895 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700896 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800897 if (!out.WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
898 PLOG(ERROR) << "Failed to write dex file location checksum to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700899 return false;
900 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700901 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800902 if (!out.WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
903 PLOG(ERROR) << "Failed to write dex file offset to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800904 return false;
905 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700906 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800907 if (!out.WriteFully(&methods_offsets_[0],
908 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
909 PLOG(ERROR) << "Failed to write methods offsets 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_methods_offsets_ +=
913 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700914 return true;
915}
916
Brian Carlstromba150c32013-08-27 17:31:03 -0700917OatWriter::OatClass::OatClass(size_t offset,
918 std::vector<CompiledMethod*>* compiled_methods,
919 uint32_t num_non_null_compiled_methods,
920 mirror::Class::Status status) {
921 CHECK(compiled_methods != NULL);
922 uint32_t num_methods = compiled_methods->size();
923 CHECK_LE(num_non_null_compiled_methods, num_methods);
924
Brian Carlstrom265091e2013-01-30 14:08:26 -0800925 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -0700926 compiled_methods_ = compiled_methods;
927 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
928
929 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
930 // apply when there are 0 methods, we just arbitrarily say that 0
931 // methods means kOatClassNoneCompiled and that we won't use
932 // kOatClassAllCompiled unless there is at least one compiled
933 // method. This means in an interpretter only system, we can assert
934 // that all classes are kOatClassNoneCompiled.
935 if (num_non_null_compiled_methods == 0) {
936 type_ = kOatClassNoneCompiled;
937 } else if (num_non_null_compiled_methods == num_methods) {
938 type_ = kOatClassAllCompiled;
939 } else {
940 type_ = kOatClassSomeCompiled;
941 }
942
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800943 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -0700944 method_offsets_.resize(num_non_null_compiled_methods);
945
946 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
947 if (type_ == kOatClassSomeCompiled) {
948 method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator());
949 method_bitmap_size_ = method_bitmap_->GetSizeOf();
950 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
951 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
952 } else {
953 method_bitmap_ = NULL;
954 method_bitmap_size_ = 0;
955 }
956
957 for (size_t i = 0; i < num_methods; i++) {
958 CompiledMethod* compiled_method = (*compiled_methods_)[i];
959 if (compiled_method == NULL) {
960 oat_method_offsets_offsets_from_oat_class_[i] = 0;
961 } else {
962 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
963 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
964 if (type_ == kOatClassSomeCompiled) {
965 method_bitmap_->SetBit(i);
966 }
967 }
968 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700969}
970
Brian Carlstromba150c32013-08-27 17:31:03 -0700971OatWriter::OatClass::~OatClass() {
972 delete compiled_methods_;
973}
974
975#if defined(ART_USE_PORTABLE_COMPILER)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800976size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
977 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -0700978 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
979 if (method_offset == 0) {
980 return 0;
981 }
982 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800983}
984
985size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
986 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -0700987 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -0800988}
Brian Carlstromba150c32013-08-27 17:31:03 -0700989#endif
Brian Carlstrom265091e2013-01-30 14:08:26 -0800990
991size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -0700992 return sizeof(status_)
993 + sizeof(type_)
994 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
995 + method_bitmap_size_
996 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700997}
998
Brian Carlstrom389efb02012-01-11 12:06:26 -0800999void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001000 oat_header.UpdateChecksum(&status_, sizeof(status_));
Brian Carlstromba150c32013-08-27 17:31:03 -07001001 oat_header.UpdateChecksum(&type_, sizeof(type_));
1002 if (method_bitmap_size_ != 0) {
1003 CHECK_EQ(kOatClassSomeCompiled, type_);
1004 oat_header.UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_));
1005 oat_header.UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_);
1006 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001007 oat_header.UpdateChecksum(&method_offsets_[0],
1008 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001009}
1010
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001011bool OatWriter::OatClass::Write(OatWriter* oat_writer,
1012 OutputStream& out,
1013 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001014 DCHECK_OFFSET_();
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001015 if (!out.WriteFully(&status_, sizeof(status_))) {
1016 PLOG(ERROR) << "Failed to write class status to " << out.GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001017 return false;
1018 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001019 oat_writer->size_oat_class_status_ += sizeof(status_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001020 if (!out.WriteFully(&type_, sizeof(type_))) {
1021 PLOG(ERROR) << "Failed to write oat class type to " << out.GetLocation();
1022 return false;
1023 }
1024 oat_writer->size_oat_class_type_ += sizeof(type_);
1025 if (method_bitmap_size_ != 0) {
1026 CHECK_EQ(kOatClassSomeCompiled, type_);
1027 if (!out.WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
1028 PLOG(ERROR) << "Failed to write method bitmap size to " << out.GetLocation();
1029 return false;
1030 }
1031 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
1032 if (!out.WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
1033 PLOG(ERROR) << "Failed to write method bitmap to " << out.GetLocation();
1034 return false;
1035 }
1036 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1037 }
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001038 if (!out.WriteFully(&method_offsets_[0],
1039 sizeof(method_offsets_[0]) * method_offsets_.size())) {
1040 PLOG(ERROR) << "Failed to write method offsets to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001041 return false;
1042 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001043 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001044 return true;
1045}
1046
Brian Carlstrome24fa612011-09-29 00:53:55 -07001047} // namespace art