blob: f681d7da6fc18221f59e11585d03a472c9ea49af [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 Hao0aba0ba2013-06-03 14:49:28 -070058 size_portable_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070059 size_portable_to_interpreter_bridge_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070060 size_quick_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070061 size_quick_to_interpreter_bridge_(0),
62 size_trampoline_alignment_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070063 size_code_size_(0),
64 size_code_(0),
65 size_code_alignment_(0),
66 size_mapping_table_(0),
67 size_vmap_table_(0),
68 size_gc_map_(0),
69 size_oat_dex_file_location_size_(0),
70 size_oat_dex_file_location_data_(0),
71 size_oat_dex_file_location_checksum_(0),
72 size_oat_dex_file_offset_(0),
73 size_oat_dex_file_methods_offsets_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -070074 size_oat_class_type_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070075 size_oat_class_status_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -070076 size_oat_class_method_bitmaps_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070077 size_oat_class_method_offsets_(0) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070078 size_t offset = InitOatHeader();
Brian Carlstrome24fa612011-09-29 00:53:55 -070079 offset = InitOatDexFiles(offset);
Brian Carlstrom89521892011-12-07 22:05:07 -080080 offset = InitDexFiles(offset);
Brian Carlstrom389efb02012-01-11 12:06:26 -080081 offset = InitOatClasses(offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -070082 offset = InitOatCode(offset);
83 offset = InitOatCodeDexFiles(offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -070084 size_ = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -070085
86 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Jeff Hao0aba0ba2013-06-03 14:49:28 -070087 CHECK(image_file_location.empty() == compiler->IsImage());
Brian Carlstrome24fa612011-09-29 00:53:55 -070088}
89
Ian Rogers0571d352011-11-03 19:51:38 -070090OatWriter::~OatWriter() {
91 delete oat_header_;
92 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -080093 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -070094}
95
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070096size_t OatWriter::InitOatHeader() {
Brian Carlstrome24fa612011-09-29 00:53:55 -070097 // create the OatHeader
Ian Rogers1212a022013-03-04 10:48:41 -080098 oat_header_ = new OatHeader(compiler_driver_->GetInstructionSet(),
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070099 dex_files_,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700100 image_file_location_oat_checksum_,
101 image_file_location_oat_begin_,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700102 image_file_location_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700103 size_t offset = sizeof(*oat_header_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700104 offset += image_file_location_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700105 return offset;
106}
107
108size_t OatWriter::InitOatDexFiles(size_t offset) {
109 // create the OatDexFiles
110 for (size_t i = 0; i != dex_files_->size(); ++i) {
111 const DexFile* dex_file = (*dex_files_)[i];
112 CHECK(dex_file != NULL);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800113 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700114 oat_dex_files_.push_back(oat_dex_file);
115 offset += oat_dex_file->SizeOf();
116 }
117 return offset;
118}
119
Brian Carlstrom89521892011-12-07 22:05:07 -0800120size_t OatWriter::InitDexFiles(size_t offset) {
121 // calculate the offsets within OatDexFiles to the DexFiles
122 for (size_t i = 0; i != dex_files_->size(); ++i) {
123 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700124 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800125 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700126 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800127
128 // set offset in OatDexFile to DexFile
129 oat_dex_files_[i]->dex_file_offset_ = offset;
130
131 const DexFile* dex_file = (*dex_files_)[i];
132 offset += dex_file->GetHeader().file_size_;
133 }
134 return offset;
135}
136
Brian Carlstrom389efb02012-01-11 12:06:26 -0800137size_t OatWriter::InitOatClasses(size_t offset) {
138 // create the OatClasses
139 // calculate the offsets within OatDexFiles to OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -0700140 for (size_t i = 0; i != dex_files_->size(); ++i) {
141 const DexFile* dex_file = (*dex_files_)[i];
142 for (size_t class_def_index = 0;
143 class_def_index < dex_file->NumClassDefs();
Ian Rogersc20a83e2012-01-18 18:15:32 -0800144 class_def_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800145 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700146 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
147 const byte* class_data = dex_file->GetClassData(class_def);
Brian Carlstromba150c32013-08-27 17:31:03 -0700148 uint32_t num_non_null_compiled_methods = 0;
149 UniquePtr<std::vector<CompiledMethod*> > compiled_methods(new std::vector<CompiledMethod*>());
Ian Rogers0571d352011-11-03 19:51:38 -0700150 if (class_data != NULL) { // ie not an empty class, such as a marker interface
151 ClassDataItemIterator it(*dex_file, class_data);
152 size_t num_direct_methods = it.NumDirectMethods();
153 size_t num_virtual_methods = it.NumVirtualMethods();
Brian Carlstromba150c32013-08-27 17:31:03 -0700154 size_t num_methods = num_direct_methods + num_virtual_methods;
155
156 // Fill in the compiled_methods_ array for methods that have a
157 // CompiledMethod. We track the number of non-null entries in
158 // num_non_null_compiled_methods since we only want to allocate
159 // OatMethodOffsets for the compiled methods.
160 compiled_methods->reserve(num_methods);
161 while (it.HasNextStaticField()) {
162 it.Next();
163 }
164 while (it.HasNextInstanceField()) {
165 it.Next();
166 }
167 size_t class_def_method_index = 0;
168 while (it.HasNextDirectMethod()) {
169 uint32_t method_idx = it.GetMemberIndex();
170 CompiledMethod* compiled_method =
171 compiler_driver_->GetCompiledMethod(MethodReference(dex_file, method_idx));
172 compiled_methods->push_back(compiled_method);
173 if (compiled_method != NULL) {
174 num_non_null_compiled_methods++;
175 }
176 class_def_method_index++;
177 it.Next();
178 }
179 while (it.HasNextVirtualMethod()) {
180 uint32_t method_idx = it.GetMemberIndex();
181 CompiledMethod* compiled_method =
182 compiler_driver_->GetCompiledMethod(MethodReference(dex_file, method_idx));
183 compiled_methods->push_back(compiled_method);
184 if (compiled_method != NULL) {
185 num_non_null_compiled_methods++;
186 }
187 class_def_method_index++;
188 it.Next();
189 }
Ian Rogers0571d352011-11-03 19:51:38 -0700190 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800191
Brian Carlstrom51c24672013-07-11 16:00:56 -0700192 ClassReference class_ref(dex_file, class_def_index);
Ian Rogers1212a022013-03-04 10:48:41 -0800193 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800194 mirror::Class::Status status;
jeffhaoec014232012-09-05 10:42:25 -0700195 if (compiled_class != NULL) {
196 status = compiled_class->GetStatus();
197 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800198 status = mirror::Class::kStatusError;
jeffhaoec014232012-09-05 10:42:25 -0700199 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800200 status = mirror::Class::kStatusNotReady;
jeffhaoec014232012-09-05 10:42:25 -0700201 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800202
Brian Carlstromba150c32013-08-27 17:31:03 -0700203 OatClass* oat_class = new OatClass(offset, compiled_methods.release(),
204 num_non_null_compiled_methods, status);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800205 oat_classes_.push_back(oat_class);
206 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700207 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800208 oat_dex_files_[i]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700209 }
210 return offset;
211}
212
213size_t OatWriter::InitOatCode(size_t offset) {
214 // calculate the offsets within OatHeader to executable code
215 size_t old_offset = offset;
216 // required to be on a new page boundary
217 offset = RoundUp(offset, kPageSize);
218 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700219 size_executable_offset_alignment_ = offset - old_offset;
220 if (compiler_driver_->IsImage()) {
221 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700222
Ian Rogers848871b2013-08-05 10:56:33 -0700223 #define DO_TRAMPOLINE(field, fn_name) \
224 offset = CompiledCode::AlignCode(offset, instruction_set); \
225 oat_header_->Set ## fn_name ## Offset(offset); \
226 field.reset(compiler_driver_->Create ## fn_name()); \
227 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700228
Ian Rogers848871b2013-08-05 10:56:33 -0700229 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge);
230 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge);
231 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
232 DO_TRAMPOLINE(portable_resolution_trampoline_, PortableResolutionTrampoline);
233 DO_TRAMPOLINE(portable_to_interpreter_bridge_, PortableToInterpreterBridge);
234 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
235 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700236
Ian Rogers848871b2013-08-05 10:56:33 -0700237 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700238 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700239 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
240 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
241 oat_header_->SetJniDlsymLookupOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700242 oat_header_->SetPortableResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700243 oat_header_->SetPortableToInterpreterBridgeOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700244 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700245 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700246 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700247 return offset;
248}
249
250size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700251 size_t oat_class_index = 0;
252 for (size_t i = 0; i != dex_files_->size(); ++i) {
253 const DexFile* dex_file = (*dex_files_)[i];
254 CHECK(dex_file != NULL);
Brian Carlstromba150c32013-08-27 17:31:03 -0700255 offset = InitOatCodeDexFile(offset, &oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700256 }
257 return offset;
258}
259
260size_t OatWriter::InitOatCodeDexFile(size_t offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700261 size_t* oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700262 const DexFile& dex_file) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800263 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700264 class_def_index < dex_file.NumClassDefs();
Brian Carlstromba150c32013-08-27 17:31:03 -0700265 class_def_index++, (*oat_class_index)++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700266 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstromba150c32013-08-27 17:31:03 -0700267 offset = InitOatCodeClassDef(offset, *oat_class_index, class_def_index, dex_file, class_def);
268 oat_classes_[*oat_class_index]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700269 }
270 return offset;
271}
272
273size_t OatWriter::InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800274 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700275 const DexFile& dex_file,
276 const DexFile::ClassDef& class_def) {
277 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700278 if (class_data == NULL) {
279 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700280 return offset;
281 }
Ian Rogers0571d352011-11-03 19:51:38 -0700282 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstromba150c32013-08-27 17:31:03 -0700283 CHECK_LE(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700284 it.NumDirectMethods() + it.NumVirtualMethods());
285 // Skip fields
286 while (it.HasNextStaticField()) {
287 it.Next();
288 }
289 while (it.HasNextInstanceField()) {
290 it.Next();
291 }
292 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700293 size_t class_def_method_index = 0;
Brian Carlstromba150c32013-08-27 17:31:03 -0700294 size_t method_offsets_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700295 while (it.HasNextDirectMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800296 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700297 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700298 &method_offsets_index, is_native,
299 it.GetMethodInvokeType(class_def), it.GetMemberIndex(), dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700300 class_def_method_index++;
301 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700302 }
Ian Rogers0571d352011-11-03 19:51:38 -0700303 while (it.HasNextVirtualMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800304 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700305 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700306 &method_offsets_index, is_native,
307 it.GetMethodInvokeType(class_def), it.GetMemberIndex(), dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700308 class_def_method_index++;
309 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700310 }
Ian Rogers0571d352011-11-03 19:51:38 -0700311 DCHECK(!it.HasNext());
Brian Carlstromba150c32013-08-27 17:31:03 -0700312 CHECK_LE(method_offsets_index, class_def_method_index);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700313 return offset;
314}
315
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700316size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
317 size_t __attribute__((unused)) class_def_index,
318 size_t class_def_method_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700319 size_t* method_offsets_index,
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700320 bool __attribute__((unused)) is_native,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800321 InvokeType invoke_type,
Brian Carlstromba150c32013-08-27 17:31:03 -0700322 uint32_t method_idx, const DexFile& dex_file) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700323 // derived from CompiledMethod if available
324 uint32_t code_offset = 0;
325 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700326 uint32_t core_spill_mask = 0;
327 uint32_t fp_spill_mask = 0;
328 uint32_t mapping_table_offset = 0;
329 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800330 uint32_t gc_map_offset = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700331
Brian Carlstrom265091e2013-01-30 14:08:26 -0800332 OatClass* oat_class = oat_classes_[oat_class_index];
333#if defined(ART_USE_PORTABLE_COMPILER)
334 size_t oat_method_offsets_offset =
335 oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index);
336#endif
337
Brian Carlstromba150c32013-08-27 17:31:03 -0700338 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700339 if (compiled_method != NULL) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800340#if defined(ART_USE_PORTABLE_COMPILER)
341 compiled_method->AddOatdataOffsetToCompliledCodeOffset(
342 oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_));
343#else
344 const std::vector<uint8_t>& code = compiled_method->GetCode();
Logan Chien971bf3f2012-05-01 15:47:55 +0800345 offset = compiled_method->AlignCode(offset);
346 DCHECK_ALIGNED(offset, kArmAlignment);
Logan Chien971bf3f2012-05-01 15:47:55 +0800347 uint32_t code_size = code.size() * sizeof(code[0]);
348 CHECK_NE(code_size, 0U);
349 uint32_t thumb_offset = compiled_method->CodeDelta();
350 code_offset = offset + sizeof(code_size) + thumb_offset;
351
352 // Deduplicate code arrays
353 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
354 if (code_iter != code_offsets_.end()) {
355 code_offset = code_iter->second;
jeffhao55d78212011-11-02 11:41:50 -0700356 } else {
Logan Chien971bf3f2012-05-01 15:47:55 +0800357 code_offsets_.Put(&code, code_offset);
358 offset += sizeof(code_size); // code size is prepended before code
359 offset += code_size;
360 oat_header_->UpdateChecksum(&code[0], code_size);
361 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800362#endif
Logan Chien971bf3f2012-05-01 15:47:55 +0800363 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
364 core_spill_mask = compiled_method->GetCoreSpillMask();
365 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700366
Ian Rogers1809a722013-08-09 22:05:32 -0700367 const std::vector<uint8_t>& mapping_table = compiled_method->GetMappingTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800368 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
369 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700370
Logan Chien971bf3f2012-05-01 15:47:55 +0800371 // Deduplicate mapping tables
Ian Rogers1809a722013-08-09 22:05:32 -0700372 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator mapping_iter =
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700373 mapping_table_offsets_.find(&mapping_table);
Logan Chien971bf3f2012-05-01 15:47:55 +0800374 if (mapping_iter != mapping_table_offsets_.end()) {
375 mapping_table_offset = mapping_iter->second;
376 } else {
377 mapping_table_offsets_.Put(&mapping_table, mapping_table_offset);
378 offset += mapping_table_size;
379 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
380 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700381
Ian Rogers1809a722013-08-09 22:05:32 -0700382 const std::vector<uint8_t>& vmap_table = compiled_method->GetVmapTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800383 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
384 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700385
Logan Chien971bf3f2012-05-01 15:47:55 +0800386 // Deduplicate vmap tables
Ian Rogers1809a722013-08-09 22:05:32 -0700387 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator vmap_iter =
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700388 vmap_table_offsets_.find(&vmap_table);
Logan Chien971bf3f2012-05-01 15:47:55 +0800389 if (vmap_iter != vmap_table_offsets_.end()) {
390 vmap_table_offset = vmap_iter->second;
391 } else {
392 vmap_table_offsets_.Put(&vmap_table, vmap_table_offset);
393 offset += vmap_table_size;
394 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
395 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800396
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700397 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800398 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
399 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
Logan Chienccb7bf12012-03-28 12:52:32 +0800400
TDYa127ce4cc0d2012-11-18 16:59:53 -0800401#if !defined(NDEBUG)
Logan Chien971bf3f2012-05-01 15:47:55 +0800402 // We expect GC maps except when the class hasn't been verified or the method is native
Brian Carlstromba150c32013-08-27 17:31:03 -0700403 ClassReference class_ref(&dex_file, class_def_index);
Ian Rogers1212a022013-03-04 10:48:41 -0800404 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800405 mirror::Class::Status status;
jeffhaoec014232012-09-05 10:42:25 -0700406 if (compiled_class != NULL) {
407 status = compiled_class->GetStatus();
408 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800409 status = mirror::Class::kStatusError;
jeffhaoec014232012-09-05 10:42:25 -0700410 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800411 status = mirror::Class::kStatusNotReady;
jeffhaoec014232012-09-05 10:42:25 -0700412 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800413 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
414 << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
415 << (status < mirror::Class::kStatusVerified) << " " << status << " "
Brian Carlstromba150c32013-08-27 17:31:03 -0700416 << PrettyMethod(method_idx, dex_file);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800417#endif
418
Logan Chien971bf3f2012-05-01 15:47:55 +0800419 // Deduplicate GC maps
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700420 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
421 gc_map_offsets_.find(&gc_map);
Logan Chien971bf3f2012-05-01 15:47:55 +0800422 if (gc_map_iter != gc_map_offsets_.end()) {
423 gc_map_offset = gc_map_iter->second;
424 } else {
425 gc_map_offsets_.Put(&gc_map, gc_map_offset);
426 offset += gc_map_size;
427 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800428 }
Brian Carlstromba150c32013-08-27 17:31:03 -0700429
430 oat_class->method_offsets_[*method_offsets_index] =
431 OatMethodOffsets(code_offset,
432 frame_size_in_bytes,
433 core_spill_mask,
434 fp_spill_mask,
435 mapping_table_offset,
436 vmap_table_offset,
437 gc_map_offset);
438 (*method_offsets_index)++;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700439 }
440
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700441
Ian Rogers1212a022013-03-04 10:48:41 -0800442 if (compiler_driver_->IsImage()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700443 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Brian Carlstromba150c32013-08-27 17:31:03 -0700444 mirror::DexCache* dex_cache = linker->FindDexCache(dex_file);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700445 // Unchecked as we hold mutator_lock_ on entry.
446 ScopedObjectAccessUnchecked soa(Thread::Current());
Brian Carlstromba150c32013-08-27 17:31:03 -0700447 mirror::ArtMethod* method = linker->ResolveMethod(dex_file, method_idx, dex_cache,
448 NULL, NULL, invoke_type);
Ian Rogers0571d352011-11-03 19:51:38 -0700449 CHECK(method != NULL);
450 method->SetFrameSizeInBytes(frame_size_in_bytes);
451 method->SetCoreSpillMask(core_spill_mask);
452 method->SetFpSpillMask(fp_spill_mask);
453 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800454 // Don't overwrite static method trampoline
455 if (!method->IsStatic() || method->IsConstructor() ||
456 method->GetDeclaringClass()->IsInitialized()) {
457 method->SetOatCodeOffset(code_offset);
458 } else {
Jeff Haoaa4a7932013-05-13 11:28:27 -0700459 method->SetEntryPointFromCompiledCode(NULL);
Ian Rogers19846512012-02-24 11:42:47 -0800460 }
Ian Rogers0571d352011-11-03 19:51:38 -0700461 method->SetOatVmapTableOffset(vmap_table_offset);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700462 method->SetOatNativeGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700463 }
Logan Chien8b977d32012-02-21 19:14:55 +0800464
Brian Carlstrome24fa612011-09-29 00:53:55 -0700465 return offset;
466}
467
Brian Carlstrom265091e2013-01-30 14:08:26 -0800468#define DCHECK_OFFSET() \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700469 DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out.Seek(0, kSeekCurrent)) \
470 << "file_offset=" << file_offset << " relative_offset=" << relative_offset
Brian Carlstrom265091e2013-01-30 14:08:26 -0800471
472#define DCHECK_OFFSET_() \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700473 DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out.Seek(0, kSeekCurrent)) \
474 << "file_offset=" << file_offset << " offset_=" << offset_
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700475
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800476bool OatWriter::Write(OutputStream& out) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700477 const size_t file_offset = out.Seek(0, kSeekCurrent);
478
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800479 if (!out.WriteFully(oat_header_, sizeof(*oat_header_))) {
480 PLOG(ERROR) << "Failed to write oat header to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700481 return false;
482 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700483 size_oat_header_ += sizeof(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700484
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800485 if (!out.WriteFully(image_file_location_.data(), image_file_location_.size())) {
486 PLOG(ERROR) << "Failed to write oat header image file location to " << out.GetLocation();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700487 return false;
488 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700489 size_oat_header_image_file_location_ += image_file_location_.size();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700490
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700491 if (!WriteTables(out, file_offset)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800492 LOG(ERROR) << "Failed to write oat tables to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700493 return false;
494 }
495
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700496 size_t relative_offset = WriteCode(out, file_offset);
497 if (relative_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800498 LOG(ERROR) << "Failed to write oat code to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700499 return false;
500 }
501
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700502 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
503 if (relative_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800504 LOG(ERROR) << "Failed to write oat code for dex files to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700505 return false;
506 }
507
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700508 if (kIsDebugBuild) {
509 uint32_t size_total = 0;
510 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700511 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700512 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700513
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700514 DO_STAT(size_dex_file_alignment_);
515 DO_STAT(size_executable_offset_alignment_);
516 DO_STAT(size_oat_header_);
517 DO_STAT(size_oat_header_image_file_location_);
518 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -0700519 DO_STAT(size_interpreter_to_interpreter_bridge_);
520 DO_STAT(size_interpreter_to_compiled_code_bridge_);
521 DO_STAT(size_jni_dlsym_lookup_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700522 DO_STAT(size_portable_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700523 DO_STAT(size_portable_to_interpreter_bridge_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700524 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700525 DO_STAT(size_quick_to_interpreter_bridge_);
526 DO_STAT(size_trampoline_alignment_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700527 DO_STAT(size_code_size_);
528 DO_STAT(size_code_);
529 DO_STAT(size_code_alignment_);
530 DO_STAT(size_mapping_table_);
531 DO_STAT(size_vmap_table_);
532 DO_STAT(size_gc_map_);
533 DO_STAT(size_oat_dex_file_location_size_);
534 DO_STAT(size_oat_dex_file_location_data_);
535 DO_STAT(size_oat_dex_file_location_checksum_);
536 DO_STAT(size_oat_dex_file_offset_);
537 DO_STAT(size_oat_dex_file_methods_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700538 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700539 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700540 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700541 DO_STAT(size_oat_class_method_offsets_);
542 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700543
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700544 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700545 CHECK_EQ(file_offset + size_total, static_cast<uint32_t>(out.Seek(0, kSeekCurrent)));
546 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700547 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700548
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700549 CHECK_EQ(file_offset + size_, static_cast<uint32_t>(out.Seek(0, kSeekCurrent)));
550 CHECK_EQ(size_, relative_offset);
551
Brian Carlstrome24fa612011-09-29 00:53:55 -0700552 return true;
553}
554
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700555bool OatWriter::WriteTables(OutputStream& out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700556 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700557 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800558 PLOG(ERROR) << "Failed to write oat dex information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700559 return false;
560 }
561 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800562 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700563 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800564 off_t actual_offset = out.Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -0800565 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
566 const DexFile* dex_file = (*dex_files_)[i];
567 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
568 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
569 return false;
570 }
571 const DexFile* dex_file = (*dex_files_)[i];
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800572 if (!out.WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700573 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
574 << " to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800575 return false;
576 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700577 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800578 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800579 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700580 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800581 PLOG(ERROR) << "Failed to write oat methods information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700582 return false;
583 }
584 }
585 return true;
586}
587
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700588size_t OatWriter::WriteCode(OutputStream& out, const size_t file_offset) {
589 size_t relative_offset = oat_header_->GetExecutableOffset();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700590 off_t new_offset = out.Seek(size_executable_offset_alignment_, kSeekCurrent);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700591 size_t expected_file_offset = file_offset + relative_offset;
592 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700593 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700594 << " Expected: " << expected_file_offset << " File: " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700595 return 0;
596 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800597 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700598 if (compiler_driver_->IsImage()) {
599 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700600
Ian Rogers848871b2013-08-05 10:56:33 -0700601 #define DO_TRAMPOLINE(field) \
602 do { \
603 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
604 uint32_t alignment_padding = aligned_offset - relative_offset; \
605 out.Seek(alignment_padding, kSeekCurrent); \
606 size_trampoline_alignment_ += alignment_padding; \
607 if (!out.WriteFully(&(*field)[0], field->size())) { \
608 PLOG(ERROR) << "Failed to write " # field " to " << out.GetLocation(); \
609 return false; \
610 } \
611 size_ ## field += field->size(); \
612 relative_offset += alignment_padding + field->size(); \
613 DCHECK_OFFSET(); \
614 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700615
Ian Rogers848871b2013-08-05 10:56:33 -0700616 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_);
617 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_);
618 DO_TRAMPOLINE(jni_dlsym_lookup_);
619 DO_TRAMPOLINE(portable_resolution_trampoline_);
620 DO_TRAMPOLINE(portable_to_interpreter_bridge_);
621 DO_TRAMPOLINE(quick_resolution_trampoline_);
622 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
623 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700624 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700625 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700626}
627
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700628size_t OatWriter::WriteCodeDexFiles(OutputStream& out,
629 const size_t file_offset,
630 size_t relative_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700631 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800632 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700633 const DexFile* dex_file = (*dex_files_)[i];
634 CHECK(dex_file != NULL);
Brian Carlstromba150c32013-08-27 17:31:03 -0700635 relative_offset = WriteCodeDexFile(out, file_offset, relative_offset, &oat_class_index,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700636 *dex_file);
637 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700638 return 0;
639 }
640 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700641 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700642}
643
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700644size_t OatWriter::WriteCodeDexFile(OutputStream& out, const size_t file_offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700645 size_t relative_offset, size_t* oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700646 const DexFile& dex_file) {
647 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
Brian Carlstromba150c32013-08-27 17:31:03 -0700648 class_def_index++, (*oat_class_index)++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700649 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstromba150c32013-08-27 17:31:03 -0700650 relative_offset = WriteCodeClassDef(out, file_offset, relative_offset, *oat_class_index,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700651 dex_file, class_def);
652 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700653 return 0;
654 }
655 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700656 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700657}
658
Ian Rogers0571d352011-11-03 19:51:38 -0700659void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800660 const DexFile& dex_file, OutputStream& out) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700661 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800662 << " to " << out.GetLocation();
Elliott Hughes234da572011-11-03 22:13:06 -0700663}
664
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800665size_t OatWriter::WriteCodeClassDef(OutputStream& out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700666 const size_t file_offset,
667 size_t relative_offset,
668 size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700669 const DexFile& dex_file,
670 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700671 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700672 if (class_data == NULL) {
673 // ie. an empty class such as a marker interface
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700674 return relative_offset;
Ian Rogers387b6992011-10-31 17:52:37 -0700675 }
Ian Rogers0571d352011-11-03 19:51:38 -0700676 ClassDataItemIterator it(dex_file, class_data);
677 // Skip fields
678 while (it.HasNextStaticField()) {
679 it.Next();
680 }
681 while (it.HasNextInstanceField()) {
682 it.Next();
683 }
684 // Process methods
685 size_t class_def_method_index = 0;
Brian Carlstromba150c32013-08-27 17:31:03 -0700686 size_t method_offsets_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700687 while (it.HasNextDirectMethod()) {
688 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700689 relative_offset = WriteCodeMethod(out, file_offset, relative_offset, oat_class_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700690 class_def_method_index, &method_offsets_index, is_static,
691 it.GetMemberIndex(), dex_file);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700692 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700693 return 0;
694 }
Ian Rogers0571d352011-11-03 19:51:38 -0700695 class_def_method_index++;
696 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700697 }
Ian Rogers0571d352011-11-03 19:51:38 -0700698 while (it.HasNextVirtualMethod()) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700699 relative_offset = WriteCodeMethod(out, file_offset, relative_offset, oat_class_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700700 class_def_method_index, &method_offsets_index, false,
701 it.GetMemberIndex(), dex_file);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700702 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700703 return 0;
704 }
Ian Rogers0571d352011-11-03 19:51:38 -0700705 class_def_method_index++;
706 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700707 }
Brian Carlstromba150c32013-08-27 17:31:03 -0700708 DCHECK(!it.HasNext());
709 CHECK_LE(method_offsets_index, class_def_method_index);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700710 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700711}
712
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700713size_t OatWriter::WriteCodeMethod(OutputStream& out, const size_t file_offset,
714 size_t relative_offset, size_t oat_class_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700715 size_t class_def_method_index, size_t* method_offsets_index,
716 bool is_static, uint32_t method_idx, const DexFile& dex_file) {
717 OatClass* oat_class = oat_classes_[oat_class_index];
718 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700719
720 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstromba150c32013-08-27 17:31:03 -0700721 const OatMethodOffsets method_offsets = oat_class->method_offsets_[*method_offsets_index];
722 (*method_offsets_index)++;
723
Brian Carlstrom265091e2013-01-30 14:08:26 -0800724#if !defined(ART_USE_PORTABLE_COMPILER)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700725 uint32_t aligned_offset = compiled_method->AlignCode(relative_offset);
726 uint32_t aligned_code_delta = aligned_offset - relative_offset;
Logan Chien971bf3f2012-05-01 15:47:55 +0800727 if (aligned_code_delta != 0) {
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800728 off_t new_offset = out.Seek(aligned_code_delta, kSeekCurrent);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700729 size_code_alignment_ += aligned_code_delta;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700730 uint32_t expected_offset = file_offset + aligned_offset;
731 if (static_cast<uint32_t>(new_offset) != expected_offset) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800732 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700733 << " Expected: " << expected_offset << " File: " << out.GetLocation();
Logan Chien971bf3f2012-05-01 15:47:55 +0800734 return 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700735 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700736 relative_offset += aligned_code_delta;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800737 DCHECK_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700738 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700739 DCHECK_ALIGNED(relative_offset, kArmAlignment);
Logan Chien971bf3f2012-05-01 15:47:55 +0800740 const std::vector<uint8_t>& code = compiled_method->GetCode();
741 uint32_t code_size = code.size() * sizeof(code[0]);
742 CHECK_NE(code_size, 0U);
743
744 // Deduplicate code arrays
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700745 size_t code_offset = relative_offset + sizeof(code_size) + compiled_method->CodeDelta();
Logan Chien971bf3f2012-05-01 15:47:55 +0800746 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800747 if (code_iter != code_offsets_.end() && code_offset != method_offsets.code_offset_) {
748 DCHECK(code_iter->second == method_offsets.code_offset_)
749 << PrettyMethod(method_idx, dex_file);
Logan Chien971bf3f2012-05-01 15:47:55 +0800750 } else {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800751 DCHECK(code_offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800752 if (!out.WriteFully(&code_size, sizeof(code_size))) {
753 ReportWriteFailure("method code size", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800754 return 0;
755 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700756 size_code_size_ += sizeof(code_size);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700757 relative_offset += sizeof(code_size);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800758 DCHECK_OFFSET();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800759 if (!out.WriteFully(&code[0], code_size)) {
760 ReportWriteFailure("method code", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800761 return 0;
762 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700763 size_code_ += code_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700764 relative_offset += code_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800765 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800766 DCHECK_OFFSET();
767#endif
Logan Chien971bf3f2012-05-01 15:47:55 +0800768
Ian Rogers1809a722013-08-09 22:05:32 -0700769 const std::vector<uint8_t>& mapping_table = compiled_method->GetMappingTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800770 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
771
772 // Deduplicate mapping tables
Ian Rogers1809a722013-08-09 22:05:32 -0700773 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator mapping_iter =
Logan Chien971bf3f2012-05-01 15:47:55 +0800774 mapping_table_offsets_.find(&mapping_table);
775 if (mapping_iter != mapping_table_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700776 relative_offset != method_offsets.mapping_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800777 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
778 || mapping_iter->second == method_offsets.mapping_table_offset_)
779 << PrettyMethod(method_idx, dex_file);
780 } else {
781 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700782 || relative_offset == method_offsets.mapping_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800783 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800784 if (!out.WriteFully(&mapping_table[0], mapping_table_size)) {
785 ReportWriteFailure("mapping table", 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_mapping_table_ += mapping_table_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700789 relative_offset += mapping_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800790 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800791 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800792
Ian Rogers1809a722013-08-09 22:05:32 -0700793 const std::vector<uint8_t>& vmap_table = compiled_method->GetVmapTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800794 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
795
796 // Deduplicate vmap tables
Ian Rogers1809a722013-08-09 22:05:32 -0700797 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator vmap_iter =
Logan Chien971bf3f2012-05-01 15:47:55 +0800798 vmap_table_offsets_.find(&vmap_table);
799 if (vmap_iter != vmap_table_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700800 relative_offset != method_offsets.vmap_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800801 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
802 || vmap_iter->second == method_offsets.vmap_table_offset_)
803 << PrettyMethod(method_idx, dex_file);
804 } else {
805 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700806 || relative_offset == method_offsets.vmap_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800807 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800808 if (!out.WriteFully(&vmap_table[0], vmap_table_size)) {
809 ReportWriteFailure("vmap table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800810 return 0;
811 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700812 size_vmap_table_ += vmap_table_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700813 relative_offset += vmap_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800814 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800815 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800816
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700817 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800818 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
819
820 // Deduplicate GC maps
821 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
822 gc_map_offsets_.find(&gc_map);
823 if (gc_map_iter != gc_map_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700824 relative_offset != method_offsets.gc_map_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800825 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
826 || gc_map_iter->second == method_offsets.gc_map_offset_)
827 << PrettyMethod(method_idx, dex_file);
828 } else {
829 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700830 || relative_offset == method_offsets.gc_map_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800831 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800832 if (!out.WriteFully(&gc_map[0], gc_map_size)) {
833 ReportWriteFailure("GC map", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800834 return 0;
835 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700836 size_gc_map_ += gc_map_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700837 relative_offset += gc_map_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800838 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800839 DCHECK_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700840 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800841
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700842 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700843}
844
Brian Carlstrom265091e2013-01-30 14:08:26 -0800845OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
846 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -0800847 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700848 dex_file_location_size_ = location.size();
849 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800850 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800851 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800852 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700853}
854
855size_t OatWriter::OatDexFile::SizeOf() const {
856 return sizeof(dex_file_location_size_)
857 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800858 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800859 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800860 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700861}
862
863void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
864 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
865 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800866 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800867 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800868 oat_header.UpdateChecksum(&methods_offsets_[0],
869 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700870}
871
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700872bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
873 OutputStream& out,
874 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800875 DCHECK_OFFSET_();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800876 if (!out.WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
877 PLOG(ERROR) << "Failed to write dex file location length to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700878 return false;
879 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700880 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800881 if (!out.WriteFully(dex_file_location_data_, dex_file_location_size_)) {
882 PLOG(ERROR) << "Failed to write dex file location data to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700883 return false;
884 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700885 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800886 if (!out.WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
887 PLOG(ERROR) << "Failed to write dex file location checksum to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700888 return false;
889 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700890 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800891 if (!out.WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
892 PLOG(ERROR) << "Failed to write dex file offset to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800893 return false;
894 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700895 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800896 if (!out.WriteFully(&methods_offsets_[0],
897 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
898 PLOG(ERROR) << "Failed to write methods offsets 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_methods_offsets_ +=
902 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700903 return true;
904}
905
Brian Carlstromba150c32013-08-27 17:31:03 -0700906OatWriter::OatClass::OatClass(size_t offset,
907 std::vector<CompiledMethod*>* compiled_methods,
908 uint32_t num_non_null_compiled_methods,
909 mirror::Class::Status status) {
910 CHECK(compiled_methods != NULL);
911 uint32_t num_methods = compiled_methods->size();
912 CHECK_LE(num_non_null_compiled_methods, num_methods);
913
Brian Carlstrom265091e2013-01-30 14:08:26 -0800914 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -0700915 compiled_methods_ = compiled_methods;
916 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
917
918 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
919 // apply when there are 0 methods, we just arbitrarily say that 0
920 // methods means kOatClassNoneCompiled and that we won't use
921 // kOatClassAllCompiled unless there is at least one compiled
922 // method. This means in an interpretter only system, we can assert
923 // that all classes are kOatClassNoneCompiled.
924 if (num_non_null_compiled_methods == 0) {
925 type_ = kOatClassNoneCompiled;
926 } else if (num_non_null_compiled_methods == num_methods) {
927 type_ = kOatClassAllCompiled;
928 } else {
929 type_ = kOatClassSomeCompiled;
930 }
931
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800932 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -0700933 method_offsets_.resize(num_non_null_compiled_methods);
934
935 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
936 if (type_ == kOatClassSomeCompiled) {
937 method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator());
938 method_bitmap_size_ = method_bitmap_->GetSizeOf();
939 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
940 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
941 } else {
942 method_bitmap_ = NULL;
943 method_bitmap_size_ = 0;
944 }
945
946 for (size_t i = 0; i < num_methods; i++) {
947 CompiledMethod* compiled_method = (*compiled_methods_)[i];
948 if (compiled_method == NULL) {
949 oat_method_offsets_offsets_from_oat_class_[i] = 0;
950 } else {
951 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
952 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
953 if (type_ == kOatClassSomeCompiled) {
954 method_bitmap_->SetBit(i);
955 }
956 }
957 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700958}
959
Brian Carlstromba150c32013-08-27 17:31:03 -0700960OatWriter::OatClass::~OatClass() {
961 delete compiled_methods_;
962}
963
964#if defined(ART_USE_PORTABLE_COMPILER)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800965size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
966 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -0700967 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
968 if (method_offset == 0) {
969 return 0;
970 }
971 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800972}
973
974size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
975 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -0700976 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -0800977}
Brian Carlstromba150c32013-08-27 17:31:03 -0700978#endif
Brian Carlstrom265091e2013-01-30 14:08:26 -0800979
980size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -0700981 return sizeof(status_)
982 + sizeof(type_)
983 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
984 + method_bitmap_size_
985 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700986}
987
Brian Carlstrom389efb02012-01-11 12:06:26 -0800988void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800989 oat_header.UpdateChecksum(&status_, sizeof(status_));
Brian Carlstromba150c32013-08-27 17:31:03 -0700990 oat_header.UpdateChecksum(&type_, sizeof(type_));
991 if (method_bitmap_size_ != 0) {
992 CHECK_EQ(kOatClassSomeCompiled, type_);
993 oat_header.UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_));
994 oat_header.UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_);
995 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800996 oat_header.UpdateChecksum(&method_offsets_[0],
997 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700998}
999
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001000bool OatWriter::OatClass::Write(OatWriter* oat_writer,
1001 OutputStream& out,
1002 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001003 DCHECK_OFFSET_();
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001004 if (!out.WriteFully(&status_, sizeof(status_))) {
1005 PLOG(ERROR) << "Failed to write class status to " << out.GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001006 return false;
1007 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001008 oat_writer->size_oat_class_status_ += sizeof(status_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001009 if (!out.WriteFully(&type_, sizeof(type_))) {
1010 PLOG(ERROR) << "Failed to write oat class type to " << out.GetLocation();
1011 return false;
1012 }
1013 oat_writer->size_oat_class_type_ += sizeof(type_);
1014 if (method_bitmap_size_ != 0) {
1015 CHECK_EQ(kOatClassSomeCompiled, type_);
1016 if (!out.WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
1017 PLOG(ERROR) << "Failed to write method bitmap size to " << out.GetLocation();
1018 return false;
1019 }
1020 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
1021 if (!out.WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
1022 PLOG(ERROR) << "Failed to write method bitmap to " << out.GetLocation();
1023 return false;
1024 }
1025 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1026 }
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001027 if (!out.WriteFully(&method_offsets_[0],
1028 sizeof(method_offsets_[0]) * method_offsets_.size())) {
1029 PLOG(ERROR) << "Failed to write method offsets to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001030 return false;
1031 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001032 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001033 return true;
1034}
1035
Brian Carlstrome24fa612011-09-29 00:53:55 -07001036} // namespace art