blob: 28355bfc7461c680fdb8efa729ed559a12140149 [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(),
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700101 dex_files_,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700102 image_file_location_oat_checksum_,
103 image_file_location_oat_begin_,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700104 image_file_location_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700105 size_t offset = sizeof(*oat_header_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700106 offset += image_file_location_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700107 return offset;
108}
109
110size_t OatWriter::InitOatDexFiles(size_t offset) {
111 // create the OatDexFiles
112 for (size_t i = 0; i != dex_files_->size(); ++i) {
113 const DexFile* dex_file = (*dex_files_)[i];
114 CHECK(dex_file != NULL);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800115 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700116 oat_dex_files_.push_back(oat_dex_file);
117 offset += oat_dex_file->SizeOf();
118 }
119 return offset;
120}
121
Brian Carlstrom89521892011-12-07 22:05:07 -0800122size_t OatWriter::InitDexFiles(size_t offset) {
123 // calculate the offsets within OatDexFiles to the DexFiles
124 for (size_t i = 0; i != dex_files_->size(); ++i) {
125 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700126 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800127 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700128 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800129
130 // set offset in OatDexFile to DexFile
131 oat_dex_files_[i]->dex_file_offset_ = offset;
132
133 const DexFile* dex_file = (*dex_files_)[i];
134 offset += dex_file->GetHeader().file_size_;
135 }
136 return offset;
137}
138
Brian Carlstrom389efb02012-01-11 12:06:26 -0800139size_t OatWriter::InitOatClasses(size_t offset) {
140 // create the OatClasses
141 // calculate the offsets within OatDexFiles to OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -0700142 for (size_t i = 0; i != dex_files_->size(); ++i) {
143 const DexFile* dex_file = (*dex_files_)[i];
144 for (size_t class_def_index = 0;
145 class_def_index < dex_file->NumClassDefs();
Ian Rogersc20a83e2012-01-18 18:15:32 -0800146 class_def_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800147 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700148 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
149 const byte* class_data = dex_file->GetClassData(class_def);
Brian Carlstromba150c32013-08-27 17:31:03 -0700150 uint32_t num_non_null_compiled_methods = 0;
151 UniquePtr<std::vector<CompiledMethod*> > compiled_methods(new std::vector<CompiledMethod*>());
Ian Rogers0571d352011-11-03 19:51:38 -0700152 if (class_data != NULL) { // ie not an empty class, such as a marker interface
153 ClassDataItemIterator it(*dex_file, class_data);
154 size_t num_direct_methods = it.NumDirectMethods();
155 size_t num_virtual_methods = it.NumVirtualMethods();
Brian Carlstromba150c32013-08-27 17:31:03 -0700156 size_t num_methods = num_direct_methods + num_virtual_methods;
157
158 // Fill in the compiled_methods_ array for methods that have a
159 // CompiledMethod. We track the number of non-null entries in
160 // num_non_null_compiled_methods since we only want to allocate
161 // OatMethodOffsets for the compiled methods.
162 compiled_methods->reserve(num_methods);
163 while (it.HasNextStaticField()) {
164 it.Next();
165 }
166 while (it.HasNextInstanceField()) {
167 it.Next();
168 }
169 size_t class_def_method_index = 0;
170 while (it.HasNextDirectMethod()) {
171 uint32_t method_idx = it.GetMemberIndex();
172 CompiledMethod* compiled_method =
173 compiler_driver_->GetCompiledMethod(MethodReference(dex_file, method_idx));
174 compiled_methods->push_back(compiled_method);
175 if (compiled_method != NULL) {
176 num_non_null_compiled_methods++;
177 }
178 class_def_method_index++;
179 it.Next();
180 }
181 while (it.HasNextVirtualMethod()) {
182 uint32_t method_idx = it.GetMemberIndex();
183 CompiledMethod* compiled_method =
184 compiler_driver_->GetCompiledMethod(MethodReference(dex_file, method_idx));
185 compiled_methods->push_back(compiled_method);
186 if (compiled_method != NULL) {
187 num_non_null_compiled_methods++;
188 }
189 class_def_method_index++;
190 it.Next();
191 }
Ian Rogers0571d352011-11-03 19:51:38 -0700192 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800193
Brian Carlstrom51c24672013-07-11 16:00:56 -0700194 ClassReference class_ref(dex_file, class_def_index);
Ian Rogers1212a022013-03-04 10:48:41 -0800195 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800196 mirror::Class::Status status;
jeffhaoec014232012-09-05 10:42:25 -0700197 if (compiled_class != NULL) {
198 status = compiled_class->GetStatus();
199 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800200 status = mirror::Class::kStatusError;
jeffhaoec014232012-09-05 10:42:25 -0700201 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800202 status = mirror::Class::kStatusNotReady;
jeffhaoec014232012-09-05 10:42:25 -0700203 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800204
Brian Carlstromba150c32013-08-27 17:31:03 -0700205 OatClass* oat_class = new OatClass(offset, compiled_methods.release(),
206 num_non_null_compiled_methods, status);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800207 oat_classes_.push_back(oat_class);
208 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700209 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800210 oat_dex_files_[i]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700211 }
212 return offset;
213}
214
215size_t OatWriter::InitOatCode(size_t offset) {
216 // calculate the offsets within OatHeader to executable code
217 size_t old_offset = offset;
218 // required to be on a new page boundary
219 offset = RoundUp(offset, kPageSize);
220 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700221 size_executable_offset_alignment_ = offset - old_offset;
222 if (compiler_driver_->IsImage()) {
223 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700224
Ian Rogers848871b2013-08-05 10:56:33 -0700225 #define DO_TRAMPOLINE(field, fn_name) \
226 offset = CompiledCode::AlignCode(offset, instruction_set); \
227 oat_header_->Set ## fn_name ## Offset(offset); \
228 field.reset(compiler_driver_->Create ## fn_name()); \
229 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700230
Ian Rogers848871b2013-08-05 10:56:33 -0700231 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge);
232 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge);
233 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Jeff Hao88474b42013-10-23 16:24:40 -0700234 DO_TRAMPOLINE(portable_imt_conflict_trampoline_, PortableImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700235 DO_TRAMPOLINE(portable_resolution_trampoline_, PortableResolutionTrampoline);
236 DO_TRAMPOLINE(portable_to_interpreter_bridge_, PortableToInterpreterBridge);
Jeff Hao88474b42013-10-23 16:24:40 -0700237 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700238 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
239 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700240
Ian Rogers848871b2013-08-05 10:56:33 -0700241 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700242 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700243 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
244 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
245 oat_header_->SetJniDlsymLookupOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700246 oat_header_->SetPortableImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700247 oat_header_->SetPortableResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700248 oat_header_->SetPortableToInterpreterBridgeOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700249 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700250 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700251 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700252 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700253 return offset;
254}
255
256size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700257 size_t oat_class_index = 0;
258 for (size_t i = 0; i != dex_files_->size(); ++i) {
259 const DexFile* dex_file = (*dex_files_)[i];
260 CHECK(dex_file != NULL);
Brian Carlstromba150c32013-08-27 17:31:03 -0700261 offset = InitOatCodeDexFile(offset, &oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700262 }
263 return offset;
264}
265
266size_t OatWriter::InitOatCodeDexFile(size_t offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700267 size_t* oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700268 const DexFile& dex_file) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800269 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700270 class_def_index < dex_file.NumClassDefs();
Brian Carlstromba150c32013-08-27 17:31:03 -0700271 class_def_index++, (*oat_class_index)++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700272 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstromba150c32013-08-27 17:31:03 -0700273 offset = InitOatCodeClassDef(offset, *oat_class_index, class_def_index, dex_file, class_def);
274 oat_classes_[*oat_class_index]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700275 }
276 return offset;
277}
278
279size_t OatWriter::InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800280 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700281 const DexFile& dex_file,
282 const DexFile::ClassDef& class_def) {
283 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700284 if (class_data == NULL) {
285 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700286 return offset;
287 }
Ian Rogers0571d352011-11-03 19:51:38 -0700288 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstromba150c32013-08-27 17:31:03 -0700289 CHECK_LE(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700290 it.NumDirectMethods() + it.NumVirtualMethods());
291 // Skip fields
292 while (it.HasNextStaticField()) {
293 it.Next();
294 }
295 while (it.HasNextInstanceField()) {
296 it.Next();
297 }
298 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700299 size_t class_def_method_index = 0;
Brian Carlstromba150c32013-08-27 17:31:03 -0700300 size_t method_offsets_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700301 while (it.HasNextDirectMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800302 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700303 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700304 &method_offsets_index, is_native,
305 it.GetMethodInvokeType(class_def), it.GetMemberIndex(), dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700306 class_def_method_index++;
307 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700308 }
Ian Rogers0571d352011-11-03 19:51:38 -0700309 while (it.HasNextVirtualMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800310 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700311 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700312 &method_offsets_index, is_native,
313 it.GetMethodInvokeType(class_def), it.GetMemberIndex(), dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700314 class_def_method_index++;
315 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700316 }
Ian Rogers0571d352011-11-03 19:51:38 -0700317 DCHECK(!it.HasNext());
Brian Carlstromba150c32013-08-27 17:31:03 -0700318 CHECK_LE(method_offsets_index, class_def_method_index);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700319 return offset;
320}
321
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700322size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
323 size_t __attribute__((unused)) class_def_index,
324 size_t class_def_method_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700325 size_t* method_offsets_index,
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700326 bool __attribute__((unused)) is_native,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800327 InvokeType invoke_type,
Brian Carlstromba150c32013-08-27 17:31:03 -0700328 uint32_t method_idx, const DexFile& dex_file) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700329 // derived from CompiledMethod if available
330 uint32_t code_offset = 0;
331 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700332 uint32_t core_spill_mask = 0;
333 uint32_t fp_spill_mask = 0;
334 uint32_t mapping_table_offset = 0;
335 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800336 uint32_t gc_map_offset = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700337
Brian Carlstrom265091e2013-01-30 14:08:26 -0800338 OatClass* oat_class = oat_classes_[oat_class_index];
339#if defined(ART_USE_PORTABLE_COMPILER)
340 size_t oat_method_offsets_offset =
341 oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index);
342#endif
343
Brian Carlstromba150c32013-08-27 17:31:03 -0700344 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700345 if (compiled_method != NULL) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800346#if defined(ART_USE_PORTABLE_COMPILER)
347 compiled_method->AddOatdataOffsetToCompliledCodeOffset(
348 oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_));
349#else
350 const std::vector<uint8_t>& code = compiled_method->GetCode();
Logan Chien971bf3f2012-05-01 15:47:55 +0800351 offset = compiled_method->AlignCode(offset);
352 DCHECK_ALIGNED(offset, kArmAlignment);
Logan Chien971bf3f2012-05-01 15:47:55 +0800353 uint32_t code_size = code.size() * sizeof(code[0]);
354 CHECK_NE(code_size, 0U);
355 uint32_t thumb_offset = compiled_method->CodeDelta();
356 code_offset = offset + sizeof(code_size) + thumb_offset;
357
358 // Deduplicate code arrays
359 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
360 if (code_iter != code_offsets_.end()) {
361 code_offset = code_iter->second;
jeffhao55d78212011-11-02 11:41:50 -0700362 } else {
Logan Chien971bf3f2012-05-01 15:47:55 +0800363 code_offsets_.Put(&code, code_offset);
364 offset += sizeof(code_size); // code size is prepended before code
365 offset += code_size;
366 oat_header_->UpdateChecksum(&code[0], code_size);
367 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800368#endif
Logan Chien971bf3f2012-05-01 15:47:55 +0800369 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
370 core_spill_mask = compiled_method->GetCoreSpillMask();
371 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700372
Ian Rogers1809a722013-08-09 22:05:32 -0700373 const std::vector<uint8_t>& mapping_table = compiled_method->GetMappingTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800374 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
375 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700376
Logan Chien971bf3f2012-05-01 15:47:55 +0800377 // Deduplicate mapping tables
Ian Rogers1809a722013-08-09 22:05:32 -0700378 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator mapping_iter =
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700379 mapping_table_offsets_.find(&mapping_table);
Logan Chien971bf3f2012-05-01 15:47:55 +0800380 if (mapping_iter != mapping_table_offsets_.end()) {
381 mapping_table_offset = mapping_iter->second;
382 } else {
383 mapping_table_offsets_.Put(&mapping_table, mapping_table_offset);
384 offset += mapping_table_size;
385 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
386 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700387
Ian Rogers1809a722013-08-09 22:05:32 -0700388 const std::vector<uint8_t>& vmap_table = compiled_method->GetVmapTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800389 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
390 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700391
Logan Chien971bf3f2012-05-01 15:47:55 +0800392 // Deduplicate vmap tables
Ian Rogers1809a722013-08-09 22:05:32 -0700393 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator vmap_iter =
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700394 vmap_table_offsets_.find(&vmap_table);
Logan Chien971bf3f2012-05-01 15:47:55 +0800395 if (vmap_iter != vmap_table_offsets_.end()) {
396 vmap_table_offset = vmap_iter->second;
397 } else {
398 vmap_table_offsets_.Put(&vmap_table, vmap_table_offset);
399 offset += vmap_table_size;
400 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
401 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800402
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700403 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800404 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
405 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
Logan Chienccb7bf12012-03-28 12:52:32 +0800406
TDYa127ce4cc0d2012-11-18 16:59:53 -0800407#if !defined(NDEBUG)
Logan Chien971bf3f2012-05-01 15:47:55 +0800408 // We expect GC maps except when the class hasn't been verified or the method is native
Brian Carlstromba150c32013-08-27 17:31:03 -0700409 ClassReference class_ref(&dex_file, class_def_index);
Ian Rogers1212a022013-03-04 10:48:41 -0800410 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800411 mirror::Class::Status status;
jeffhaoec014232012-09-05 10:42:25 -0700412 if (compiled_class != NULL) {
413 status = compiled_class->GetStatus();
414 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800415 status = mirror::Class::kStatusError;
jeffhaoec014232012-09-05 10:42:25 -0700416 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800417 status = mirror::Class::kStatusNotReady;
jeffhaoec014232012-09-05 10:42:25 -0700418 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800419 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
420 << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
421 << (status < mirror::Class::kStatusVerified) << " " << status << " "
Brian Carlstromba150c32013-08-27 17:31:03 -0700422 << PrettyMethod(method_idx, dex_file);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800423#endif
424
Logan Chien971bf3f2012-05-01 15:47:55 +0800425 // Deduplicate GC maps
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700426 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
427 gc_map_offsets_.find(&gc_map);
Logan Chien971bf3f2012-05-01 15:47:55 +0800428 if (gc_map_iter != gc_map_offsets_.end()) {
429 gc_map_offset = gc_map_iter->second;
430 } else {
431 gc_map_offsets_.Put(&gc_map, gc_map_offset);
432 offset += gc_map_size;
433 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800434 }
Brian Carlstromba150c32013-08-27 17:31:03 -0700435
436 oat_class->method_offsets_[*method_offsets_index] =
437 OatMethodOffsets(code_offset,
438 frame_size_in_bytes,
439 core_spill_mask,
440 fp_spill_mask,
441 mapping_table_offset,
442 vmap_table_offset,
443 gc_map_offset);
444 (*method_offsets_index)++;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700445 }
446
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700447
Ian Rogers1212a022013-03-04 10:48:41 -0800448 if (compiler_driver_->IsImage()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700449 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Brian Carlstromba150c32013-08-27 17:31:03 -0700450 mirror::DexCache* dex_cache = linker->FindDexCache(dex_file);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700451 // Unchecked as we hold mutator_lock_ on entry.
452 ScopedObjectAccessUnchecked soa(Thread::Current());
Brian Carlstromba150c32013-08-27 17:31:03 -0700453 mirror::ArtMethod* method = linker->ResolveMethod(dex_file, method_idx, dex_cache,
454 NULL, NULL, invoke_type);
Ian Rogers0571d352011-11-03 19:51:38 -0700455 CHECK(method != NULL);
456 method->SetFrameSizeInBytes(frame_size_in_bytes);
457 method->SetCoreSpillMask(core_spill_mask);
458 method->SetFpSpillMask(fp_spill_mask);
459 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800460 // Don't overwrite static method trampoline
461 if (!method->IsStatic() || method->IsConstructor() ||
462 method->GetDeclaringClass()->IsInitialized()) {
463 method->SetOatCodeOffset(code_offset);
464 } else {
Jeff Haoaa4a7932013-05-13 11:28:27 -0700465 method->SetEntryPointFromCompiledCode(NULL);
Ian Rogers19846512012-02-24 11:42:47 -0800466 }
Ian Rogers0571d352011-11-03 19:51:38 -0700467 method->SetOatVmapTableOffset(vmap_table_offset);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700468 method->SetOatNativeGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700469 }
Logan Chien8b977d32012-02-21 19:14:55 +0800470
Brian Carlstrome24fa612011-09-29 00:53:55 -0700471 return offset;
472}
473
Brian Carlstrom265091e2013-01-30 14:08:26 -0800474#define DCHECK_OFFSET() \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700475 DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out.Seek(0, kSeekCurrent)) \
476 << "file_offset=" << file_offset << " relative_offset=" << relative_offset
Brian Carlstrom265091e2013-01-30 14:08:26 -0800477
478#define DCHECK_OFFSET_() \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700479 DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out.Seek(0, kSeekCurrent)) \
480 << "file_offset=" << file_offset << " offset_=" << offset_
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700481
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800482bool OatWriter::Write(OutputStream& out) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700483 const size_t file_offset = out.Seek(0, kSeekCurrent);
484
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800485 if (!out.WriteFully(oat_header_, sizeof(*oat_header_))) {
486 PLOG(ERROR) << "Failed to write oat header to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700487 return false;
488 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700489 size_oat_header_ += sizeof(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700490
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800491 if (!out.WriteFully(image_file_location_.data(), image_file_location_.size())) {
492 PLOG(ERROR) << "Failed to write oat header image file location to " << out.GetLocation();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700493 return false;
494 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700495 size_oat_header_image_file_location_ += image_file_location_.size();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700496
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700497 if (!WriteTables(out, file_offset)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800498 LOG(ERROR) << "Failed to write oat tables to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700499 return false;
500 }
501
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700502 size_t relative_offset = WriteCode(out, file_offset);
503 if (relative_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800504 LOG(ERROR) << "Failed to write oat code to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700505 return false;
506 }
507
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700508 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
509 if (relative_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800510 LOG(ERROR) << "Failed to write oat code for dex files to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700511 return false;
512 }
513
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700514 if (kIsDebugBuild) {
515 uint32_t size_total = 0;
516 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700517 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700518 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700519
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700520 DO_STAT(size_dex_file_alignment_);
521 DO_STAT(size_executable_offset_alignment_);
522 DO_STAT(size_oat_header_);
523 DO_STAT(size_oat_header_image_file_location_);
524 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -0700525 DO_STAT(size_interpreter_to_interpreter_bridge_);
526 DO_STAT(size_interpreter_to_compiled_code_bridge_);
527 DO_STAT(size_jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -0700528 DO_STAT(size_portable_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700529 DO_STAT(size_portable_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700530 DO_STAT(size_portable_to_interpreter_bridge_);
Jeff Hao88474b42013-10-23 16:24:40 -0700531 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700532 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700533 DO_STAT(size_quick_to_interpreter_bridge_);
534 DO_STAT(size_trampoline_alignment_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700535 DO_STAT(size_code_size_);
536 DO_STAT(size_code_);
537 DO_STAT(size_code_alignment_);
538 DO_STAT(size_mapping_table_);
539 DO_STAT(size_vmap_table_);
540 DO_STAT(size_gc_map_);
541 DO_STAT(size_oat_dex_file_location_size_);
542 DO_STAT(size_oat_dex_file_location_data_);
543 DO_STAT(size_oat_dex_file_location_checksum_);
544 DO_STAT(size_oat_dex_file_offset_);
545 DO_STAT(size_oat_dex_file_methods_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700546 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700547 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700548 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700549 DO_STAT(size_oat_class_method_offsets_);
550 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700551
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700552 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700553 CHECK_EQ(file_offset + size_total, static_cast<uint32_t>(out.Seek(0, kSeekCurrent)));
554 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700555 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700556
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700557 CHECK_EQ(file_offset + size_, static_cast<uint32_t>(out.Seek(0, kSeekCurrent)));
558 CHECK_EQ(size_, relative_offset);
559
Brian Carlstrome24fa612011-09-29 00:53:55 -0700560 return true;
561}
562
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700563bool OatWriter::WriteTables(OutputStream& out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700564 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700565 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800566 PLOG(ERROR) << "Failed to write oat dex information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700567 return false;
568 }
569 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800570 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700571 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800572 off_t actual_offset = out.Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -0800573 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
574 const DexFile* dex_file = (*dex_files_)[i];
575 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
576 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
577 return false;
578 }
579 const DexFile* dex_file = (*dex_files_)[i];
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800580 if (!out.WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700581 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
582 << " to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800583 return false;
584 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700585 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800586 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800587 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700588 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800589 PLOG(ERROR) << "Failed to write oat methods information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700590 return false;
591 }
592 }
593 return true;
594}
595
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700596size_t OatWriter::WriteCode(OutputStream& out, const size_t file_offset) {
597 size_t relative_offset = oat_header_->GetExecutableOffset();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700598 off_t new_offset = out.Seek(size_executable_offset_alignment_, kSeekCurrent);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700599 size_t expected_file_offset = file_offset + relative_offset;
600 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700601 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700602 << " Expected: " << expected_file_offset << " File: " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700603 return 0;
604 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800605 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700606 if (compiler_driver_->IsImage()) {
607 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700608
Ian Rogers848871b2013-08-05 10:56:33 -0700609 #define DO_TRAMPOLINE(field) \
610 do { \
611 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
612 uint32_t alignment_padding = aligned_offset - relative_offset; \
613 out.Seek(alignment_padding, kSeekCurrent); \
614 size_trampoline_alignment_ += alignment_padding; \
615 if (!out.WriteFully(&(*field)[0], field->size())) { \
616 PLOG(ERROR) << "Failed to write " # field " to " << out.GetLocation(); \
617 return false; \
618 } \
619 size_ ## field += field->size(); \
620 relative_offset += alignment_padding + field->size(); \
621 DCHECK_OFFSET(); \
622 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700623
Ian Rogers848871b2013-08-05 10:56:33 -0700624 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_);
625 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_);
626 DO_TRAMPOLINE(jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -0700627 DO_TRAMPOLINE(portable_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700628 DO_TRAMPOLINE(portable_resolution_trampoline_);
629 DO_TRAMPOLINE(portable_to_interpreter_bridge_);
Jeff Hao88474b42013-10-23 16:24:40 -0700630 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700631 DO_TRAMPOLINE(quick_resolution_trampoline_);
632 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
633 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700634 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700635 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700636}
637
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700638size_t OatWriter::WriteCodeDexFiles(OutputStream& out,
639 const size_t file_offset,
640 size_t relative_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700641 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800642 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700643 const DexFile* dex_file = (*dex_files_)[i];
644 CHECK(dex_file != NULL);
Brian Carlstromba150c32013-08-27 17:31:03 -0700645 relative_offset = WriteCodeDexFile(out, file_offset, relative_offset, &oat_class_index,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700646 *dex_file);
647 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700648 return 0;
649 }
650 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700651 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700652}
653
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700654size_t OatWriter::WriteCodeDexFile(OutputStream& out, const size_t file_offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700655 size_t relative_offset, size_t* oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700656 const DexFile& dex_file) {
657 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
Brian Carlstromba150c32013-08-27 17:31:03 -0700658 class_def_index++, (*oat_class_index)++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700659 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstromba150c32013-08-27 17:31:03 -0700660 relative_offset = WriteCodeClassDef(out, file_offset, relative_offset, *oat_class_index,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700661 dex_file, class_def);
662 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700663 return 0;
664 }
665 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700666 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700667}
668
Ian Rogers0571d352011-11-03 19:51:38 -0700669void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800670 const DexFile& dex_file, OutputStream& out) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700671 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800672 << " to " << out.GetLocation();
Elliott Hughes234da572011-11-03 22:13:06 -0700673}
674
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800675size_t OatWriter::WriteCodeClassDef(OutputStream& out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700676 const size_t file_offset,
677 size_t relative_offset,
678 size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700679 const DexFile& dex_file,
680 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700681 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700682 if (class_data == NULL) {
683 // ie. an empty class such as a marker interface
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700684 return relative_offset;
Ian Rogers387b6992011-10-31 17:52:37 -0700685 }
Ian Rogers0571d352011-11-03 19:51:38 -0700686 ClassDataItemIterator it(dex_file, class_data);
687 // Skip fields
688 while (it.HasNextStaticField()) {
689 it.Next();
690 }
691 while (it.HasNextInstanceField()) {
692 it.Next();
693 }
694 // Process methods
695 size_t class_def_method_index = 0;
Brian Carlstromba150c32013-08-27 17:31:03 -0700696 size_t method_offsets_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700697 while (it.HasNextDirectMethod()) {
698 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
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, is_static,
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 }
Ian Rogers0571d352011-11-03 19:51:38 -0700708 while (it.HasNextVirtualMethod()) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700709 relative_offset = WriteCodeMethod(out, file_offset, relative_offset, oat_class_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700710 class_def_method_index, &method_offsets_index, false,
711 it.GetMemberIndex(), dex_file);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700712 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700713 return 0;
714 }
Ian Rogers0571d352011-11-03 19:51:38 -0700715 class_def_method_index++;
716 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700717 }
Brian Carlstromba150c32013-08-27 17:31:03 -0700718 DCHECK(!it.HasNext());
719 CHECK_LE(method_offsets_index, class_def_method_index);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700720 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700721}
722
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700723size_t OatWriter::WriteCodeMethod(OutputStream& out, const size_t file_offset,
724 size_t relative_offset, size_t oat_class_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700725 size_t class_def_method_index, size_t* method_offsets_index,
726 bool is_static, uint32_t method_idx, const DexFile& dex_file) {
727 OatClass* oat_class = oat_classes_[oat_class_index];
728 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700729
730 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstromba150c32013-08-27 17:31:03 -0700731 const OatMethodOffsets method_offsets = oat_class->method_offsets_[*method_offsets_index];
732 (*method_offsets_index)++;
733
Brian Carlstrom265091e2013-01-30 14:08:26 -0800734#if !defined(ART_USE_PORTABLE_COMPILER)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700735 uint32_t aligned_offset = compiled_method->AlignCode(relative_offset);
736 uint32_t aligned_code_delta = aligned_offset - relative_offset;
Logan Chien971bf3f2012-05-01 15:47:55 +0800737 if (aligned_code_delta != 0) {
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800738 off_t new_offset = out.Seek(aligned_code_delta, kSeekCurrent);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700739 size_code_alignment_ += aligned_code_delta;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700740 uint32_t expected_offset = file_offset + aligned_offset;
741 if (static_cast<uint32_t>(new_offset) != expected_offset) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800742 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700743 << " Expected: " << expected_offset << " File: " << out.GetLocation();
Logan Chien971bf3f2012-05-01 15:47:55 +0800744 return 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700745 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700746 relative_offset += aligned_code_delta;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800747 DCHECK_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700748 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700749 DCHECK_ALIGNED(relative_offset, kArmAlignment);
Logan Chien971bf3f2012-05-01 15:47:55 +0800750 const std::vector<uint8_t>& code = compiled_method->GetCode();
751 uint32_t code_size = code.size() * sizeof(code[0]);
752 CHECK_NE(code_size, 0U);
753
754 // Deduplicate code arrays
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700755 size_t code_offset = relative_offset + sizeof(code_size) + compiled_method->CodeDelta();
Logan Chien971bf3f2012-05-01 15:47:55 +0800756 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800757 if (code_iter != code_offsets_.end() && code_offset != method_offsets.code_offset_) {
758 DCHECK(code_iter->second == method_offsets.code_offset_)
759 << PrettyMethod(method_idx, dex_file);
Logan Chien971bf3f2012-05-01 15:47:55 +0800760 } else {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800761 DCHECK(code_offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800762 if (!out.WriteFully(&code_size, sizeof(code_size))) {
763 ReportWriteFailure("method code size", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800764 return 0;
765 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700766 size_code_size_ += sizeof(code_size);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700767 relative_offset += sizeof(code_size);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800768 DCHECK_OFFSET();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800769 if (!out.WriteFully(&code[0], code_size)) {
770 ReportWriteFailure("method code", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800771 return 0;
772 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700773 size_code_ += code_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700774 relative_offset += code_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800775 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800776 DCHECK_OFFSET();
777#endif
Logan Chien971bf3f2012-05-01 15:47:55 +0800778
Ian Rogers1809a722013-08-09 22:05:32 -0700779 const std::vector<uint8_t>& mapping_table = compiled_method->GetMappingTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800780 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
781
782 // Deduplicate mapping tables
Ian Rogers1809a722013-08-09 22:05:32 -0700783 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator mapping_iter =
Logan Chien971bf3f2012-05-01 15:47:55 +0800784 mapping_table_offsets_.find(&mapping_table);
785 if (mapping_iter != mapping_table_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700786 relative_offset != method_offsets.mapping_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800787 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
788 || mapping_iter->second == method_offsets.mapping_table_offset_)
789 << PrettyMethod(method_idx, dex_file);
790 } else {
791 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700792 || relative_offset == method_offsets.mapping_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800793 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800794 if (!out.WriteFully(&mapping_table[0], mapping_table_size)) {
795 ReportWriteFailure("mapping table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800796 return 0;
797 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700798 size_mapping_table_ += mapping_table_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700799 relative_offset += mapping_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800800 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800801 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800802
Ian Rogers1809a722013-08-09 22:05:32 -0700803 const std::vector<uint8_t>& vmap_table = compiled_method->GetVmapTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800804 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
805
806 // Deduplicate vmap tables
Ian Rogers1809a722013-08-09 22:05:32 -0700807 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator vmap_iter =
Logan Chien971bf3f2012-05-01 15:47:55 +0800808 vmap_table_offsets_.find(&vmap_table);
809 if (vmap_iter != vmap_table_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700810 relative_offset != method_offsets.vmap_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800811 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
812 || vmap_iter->second == method_offsets.vmap_table_offset_)
813 << PrettyMethod(method_idx, dex_file);
814 } else {
815 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700816 || relative_offset == method_offsets.vmap_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800817 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800818 if (!out.WriteFully(&vmap_table[0], vmap_table_size)) {
819 ReportWriteFailure("vmap table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800820 return 0;
821 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700822 size_vmap_table_ += vmap_table_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700823 relative_offset += vmap_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800824 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800825 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800826
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700827 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800828 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
829
830 // Deduplicate GC maps
831 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
832 gc_map_offsets_.find(&gc_map);
833 if (gc_map_iter != gc_map_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700834 relative_offset != method_offsets.gc_map_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800835 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
836 || gc_map_iter->second == method_offsets.gc_map_offset_)
837 << PrettyMethod(method_idx, dex_file);
838 } else {
839 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700840 || relative_offset == method_offsets.gc_map_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800841 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800842 if (!out.WriteFully(&gc_map[0], gc_map_size)) {
843 ReportWriteFailure("GC map", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800844 return 0;
845 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700846 size_gc_map_ += gc_map_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700847 relative_offset += gc_map_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800848 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800849 DCHECK_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700850 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800851
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700852 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700853}
854
Brian Carlstrom265091e2013-01-30 14:08:26 -0800855OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
856 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -0800857 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700858 dex_file_location_size_ = location.size();
859 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800860 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800861 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800862 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700863}
864
865size_t OatWriter::OatDexFile::SizeOf() const {
866 return sizeof(dex_file_location_size_)
867 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800868 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800869 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800870 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700871}
872
873void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
874 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
875 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800876 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800877 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800878 oat_header.UpdateChecksum(&methods_offsets_[0],
879 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700880}
881
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700882bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
883 OutputStream& out,
884 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800885 DCHECK_OFFSET_();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800886 if (!out.WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
887 PLOG(ERROR) << "Failed to write dex file location length 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_size_ += sizeof(dex_file_location_size_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800891 if (!out.WriteFully(dex_file_location_data_, dex_file_location_size_)) {
892 PLOG(ERROR) << "Failed to write dex file location data to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700893 return false;
894 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700895 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800896 if (!out.WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
897 PLOG(ERROR) << "Failed to write dex file location checksum to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700898 return false;
899 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700900 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800901 if (!out.WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
902 PLOG(ERROR) << "Failed to write dex file offset to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800903 return false;
904 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700905 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800906 if (!out.WriteFully(&methods_offsets_[0],
907 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
908 PLOG(ERROR) << "Failed to write methods offsets to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700909 return false;
910 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700911 oat_writer->size_oat_dex_file_methods_offsets_ +=
912 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700913 return true;
914}
915
Brian Carlstromba150c32013-08-27 17:31:03 -0700916OatWriter::OatClass::OatClass(size_t offset,
917 std::vector<CompiledMethod*>* compiled_methods,
918 uint32_t num_non_null_compiled_methods,
919 mirror::Class::Status status) {
920 CHECK(compiled_methods != NULL);
921 uint32_t num_methods = compiled_methods->size();
922 CHECK_LE(num_non_null_compiled_methods, num_methods);
923
Brian Carlstrom265091e2013-01-30 14:08:26 -0800924 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -0700925 compiled_methods_ = compiled_methods;
926 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
927
928 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
929 // apply when there are 0 methods, we just arbitrarily say that 0
930 // methods means kOatClassNoneCompiled and that we won't use
931 // kOatClassAllCompiled unless there is at least one compiled
932 // method. This means in an interpretter only system, we can assert
933 // that all classes are kOatClassNoneCompiled.
934 if (num_non_null_compiled_methods == 0) {
935 type_ = kOatClassNoneCompiled;
936 } else if (num_non_null_compiled_methods == num_methods) {
937 type_ = kOatClassAllCompiled;
938 } else {
939 type_ = kOatClassSomeCompiled;
940 }
941
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800942 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -0700943 method_offsets_.resize(num_non_null_compiled_methods);
944
945 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
946 if (type_ == kOatClassSomeCompiled) {
947 method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator());
948 method_bitmap_size_ = method_bitmap_->GetSizeOf();
949 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
950 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
951 } else {
952 method_bitmap_ = NULL;
953 method_bitmap_size_ = 0;
954 }
955
956 for (size_t i = 0; i < num_methods; i++) {
957 CompiledMethod* compiled_method = (*compiled_methods_)[i];
958 if (compiled_method == NULL) {
959 oat_method_offsets_offsets_from_oat_class_[i] = 0;
960 } else {
961 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
962 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
963 if (type_ == kOatClassSomeCompiled) {
964 method_bitmap_->SetBit(i);
965 }
966 }
967 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700968}
969
Brian Carlstromba150c32013-08-27 17:31:03 -0700970OatWriter::OatClass::~OatClass() {
971 delete compiled_methods_;
972}
973
974#if defined(ART_USE_PORTABLE_COMPILER)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800975size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
976 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -0700977 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
978 if (method_offset == 0) {
979 return 0;
980 }
981 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800982}
983
984size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
985 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -0700986 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -0800987}
Brian Carlstromba150c32013-08-27 17:31:03 -0700988#endif
Brian Carlstrom265091e2013-01-30 14:08:26 -0800989
990size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -0700991 return sizeof(status_)
992 + sizeof(type_)
993 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
994 + method_bitmap_size_
995 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700996}
997
Brian Carlstrom389efb02012-01-11 12:06:26 -0800998void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800999 oat_header.UpdateChecksum(&status_, sizeof(status_));
Brian Carlstromba150c32013-08-27 17:31:03 -07001000 oat_header.UpdateChecksum(&type_, sizeof(type_));
1001 if (method_bitmap_size_ != 0) {
1002 CHECK_EQ(kOatClassSomeCompiled, type_);
1003 oat_header.UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_));
1004 oat_header.UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_);
1005 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001006 oat_header.UpdateChecksum(&method_offsets_[0],
1007 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001008}
1009
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001010bool OatWriter::OatClass::Write(OatWriter* oat_writer,
1011 OutputStream& out,
1012 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001013 DCHECK_OFFSET_();
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001014 if (!out.WriteFully(&status_, sizeof(status_))) {
1015 PLOG(ERROR) << "Failed to write class status to " << out.GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001016 return false;
1017 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001018 oat_writer->size_oat_class_status_ += sizeof(status_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001019 if (!out.WriteFully(&type_, sizeof(type_))) {
1020 PLOG(ERROR) << "Failed to write oat class type to " << out.GetLocation();
1021 return false;
1022 }
1023 oat_writer->size_oat_class_type_ += sizeof(type_);
1024 if (method_bitmap_size_ != 0) {
1025 CHECK_EQ(kOatClassSomeCompiled, type_);
1026 if (!out.WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
1027 PLOG(ERROR) << "Failed to write method bitmap size to " << out.GetLocation();
1028 return false;
1029 }
1030 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
1031 if (!out.WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
1032 PLOG(ERROR) << "Failed to write method bitmap to " << out.GetLocation();
1033 return false;
1034 }
1035 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1036 }
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001037 if (!out.WriteFully(&method_offsets_[0],
1038 sizeof(method_offsets_[0]) * method_offsets_.size())) {
1039 PLOG(ERROR) << "Failed to write method offsets to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001040 return false;
1041 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001042 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001043 return true;
1044}
1045
Brian Carlstrome24fa612011-09-29 00:53:55 -07001046} // namespace art