blob: 5394bbc42ee4884e661969d64e348bde26226bf1 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrome24fa612011-09-29 00:53:55 -070016
17#include "oat_writer.h"
18
Elliott Hughesa0e18062012-04-13 15:59:59 -070019#include <zlib.h>
20
Brian Carlstromba150c32013-08-27 17:31:03 -070021#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080022#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080023#include "base/unix_file/fd_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070024#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000026#include "dex/verification_results.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070027#include "gc/space/space.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070028#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/array.h"
30#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070031#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070032#include "os.h"
Brian Carlstromcd60ac72013-01-20 17:09:51 -080033#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070034#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070035#include "scoped_thread_state_change.h"
jeffhaoec014232012-09-05 10:42:25 -070036#include "verifier/method_verifier.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070037
38namespace art {
39
Brian Carlstrom3320cf42011-10-04 14:58:28 -070040OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070041 uint32_t image_file_location_oat_checksum,
Ian Rogersef7d42f2014-01-06 12:55:46 -080042 uintptr_t image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070043 const std::string& image_file_location,
Ian Rogersca368cb2013-11-15 15:52:08 -080044 const CompilerDriver* compiler,
45 TimingLogger* timings)
Jeff Hao0aba0ba2013-06-03 14:49:28 -070046 : compiler_driver_(compiler),
47 dex_files_(&dex_files),
48 image_file_location_oat_checksum_(image_file_location_oat_checksum),
49 image_file_location_oat_begin_(image_file_location_oat_begin),
50 image_file_location_(image_file_location),
51 oat_header_(NULL),
52 size_dex_file_alignment_(0),
53 size_executable_offset_alignment_(0),
54 size_oat_header_(0),
55 size_oat_header_image_file_location_(0),
56 size_dex_file_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070057 size_interpreter_to_interpreter_bridge_(0),
58 size_interpreter_to_compiled_code_bridge_(0),
59 size_jni_dlsym_lookup_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070060 size_portable_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070061 size_portable_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070062 size_portable_to_interpreter_bridge_(0),
Andreas Gampe2da88232014-02-27 12:26:20 -080063 size_quick_generic_jni_trampoline_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070064 size_quick_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070065 size_quick_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070066 size_quick_to_interpreter_bridge_(0),
67 size_trampoline_alignment_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070068 size_code_size_(0),
69 size_code_(0),
70 size_code_alignment_(0),
71 size_mapping_table_(0),
72 size_vmap_table_(0),
73 size_gc_map_(0),
74 size_oat_dex_file_location_size_(0),
75 size_oat_dex_file_location_data_(0),
76 size_oat_dex_file_location_checksum_(0),
77 size_oat_dex_file_offset_(0),
78 size_oat_dex_file_methods_offsets_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -070079 size_oat_class_type_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070080 size_oat_class_status_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -070081 size_oat_class_method_bitmaps_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070082 size_oat_class_method_offsets_(0) {
Ian Rogersca368cb2013-11-15 15:52:08 -080083 size_t offset;
84 {
85 TimingLogger::ScopedSplit split("InitOatHeader", timings);
86 offset = InitOatHeader();
87 }
88 {
89 TimingLogger::ScopedSplit split("InitOatDexFiles", timings);
90 offset = InitOatDexFiles(offset);
91 }
92 {
93 TimingLogger::ScopedSplit split("InitDexFiles", timings);
94 offset = InitDexFiles(offset);
95 }
96 {
97 TimingLogger::ScopedSplit split("InitOatClasses", timings);
98 offset = InitOatClasses(offset);
99 }
100 {
101 TimingLogger::ScopedSplit split("InitOatCode", timings);
102 offset = InitOatCode(offset);
103 }
104 {
105 TimingLogger::ScopedSplit split("InitOatCodeDexFiles", timings);
106 offset = InitOatCodeDexFiles(offset);
107 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700108 size_ = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700109
110 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700111 CHECK(image_file_location.empty() == compiler->IsImage());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700112}
113
Ian Rogers0571d352011-11-03 19:51:38 -0700114OatWriter::~OatWriter() {
115 delete oat_header_;
116 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800117 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -0700118}
119
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700120size_t OatWriter::InitOatHeader() {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700121 // create the OatHeader
Ian Rogers1212a022013-03-04 10:48:41 -0800122 oat_header_ = new OatHeader(compiler_driver_->GetInstructionSet(),
Dave Allison70202782013-10-22 17:52:19 -0700123 compiler_driver_->GetInstructionSetFeatures(),
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700124 dex_files_,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700125 image_file_location_oat_checksum_,
126 image_file_location_oat_begin_,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700127 image_file_location_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700128 size_t offset = sizeof(*oat_header_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700129 offset += image_file_location_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700130 return offset;
131}
132
133size_t OatWriter::InitOatDexFiles(size_t offset) {
134 // create the OatDexFiles
135 for (size_t i = 0; i != dex_files_->size(); ++i) {
136 const DexFile* dex_file = (*dex_files_)[i];
137 CHECK(dex_file != NULL);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800138 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700139 oat_dex_files_.push_back(oat_dex_file);
140 offset += oat_dex_file->SizeOf();
141 }
142 return offset;
143}
144
Brian Carlstrom89521892011-12-07 22:05:07 -0800145size_t OatWriter::InitDexFiles(size_t offset) {
146 // calculate the offsets within OatDexFiles to the DexFiles
147 for (size_t i = 0; i != dex_files_->size(); ++i) {
148 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700149 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800150 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700151 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800152
153 // set offset in OatDexFile to DexFile
154 oat_dex_files_[i]->dex_file_offset_ = offset;
155
156 const DexFile* dex_file = (*dex_files_)[i];
157 offset += dex_file->GetHeader().file_size_;
158 }
159 return offset;
160}
161
Brian Carlstrom389efb02012-01-11 12:06:26 -0800162size_t OatWriter::InitOatClasses(size_t offset) {
163 // create the OatClasses
164 // calculate the offsets within OatDexFiles to OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -0700165 for (size_t i = 0; i != dex_files_->size(); ++i) {
166 const DexFile* dex_file = (*dex_files_)[i];
167 for (size_t class_def_index = 0;
168 class_def_index < dex_file->NumClassDefs();
Ian Rogersc20a83e2012-01-18 18:15:32 -0800169 class_def_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800170 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700171 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
172 const byte* class_data = dex_file->GetClassData(class_def);
Brian Carlstromba150c32013-08-27 17:31:03 -0700173 uint32_t num_non_null_compiled_methods = 0;
174 UniquePtr<std::vector<CompiledMethod*> > compiled_methods(new std::vector<CompiledMethod*>());
Ian Rogers0571d352011-11-03 19:51:38 -0700175 if (class_data != NULL) { // ie not an empty class, such as a marker interface
176 ClassDataItemIterator it(*dex_file, class_data);
177 size_t num_direct_methods = it.NumDirectMethods();
178 size_t num_virtual_methods = it.NumVirtualMethods();
Brian Carlstromba150c32013-08-27 17:31:03 -0700179 size_t num_methods = num_direct_methods + num_virtual_methods;
180
181 // Fill in the compiled_methods_ array for methods that have a
182 // CompiledMethod. We track the number of non-null entries in
183 // num_non_null_compiled_methods since we only want to allocate
184 // OatMethodOffsets for the compiled methods.
185 compiled_methods->reserve(num_methods);
186 while (it.HasNextStaticField()) {
187 it.Next();
188 }
189 while (it.HasNextInstanceField()) {
190 it.Next();
191 }
192 size_t class_def_method_index = 0;
193 while (it.HasNextDirectMethod()) {
194 uint32_t method_idx = it.GetMemberIndex();
195 CompiledMethod* compiled_method =
196 compiler_driver_->GetCompiledMethod(MethodReference(dex_file, method_idx));
197 compiled_methods->push_back(compiled_method);
198 if (compiled_method != NULL) {
199 num_non_null_compiled_methods++;
200 }
201 class_def_method_index++;
202 it.Next();
203 }
204 while (it.HasNextVirtualMethod()) {
205 uint32_t method_idx = it.GetMemberIndex();
206 CompiledMethod* compiled_method =
207 compiler_driver_->GetCompiledMethod(MethodReference(dex_file, method_idx));
208 compiled_methods->push_back(compiled_method);
209 if (compiled_method != NULL) {
210 num_non_null_compiled_methods++;
211 }
212 class_def_method_index++;
213 it.Next();
214 }
Ian Rogers0571d352011-11-03 19:51:38 -0700215 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800216
Brian Carlstrom51c24672013-07-11 16:00:56 -0700217 ClassReference class_ref(dex_file, class_def_index);
Ian Rogers1212a022013-03-04 10:48:41 -0800218 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800219 mirror::Class::Status status;
jeffhaoec014232012-09-05 10:42:25 -0700220 if (compiled_class != NULL) {
221 status = compiled_class->GetStatus();
Vladimir Markoc7f83202014-01-24 17:55:18 +0000222 } else if (compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800223 status = mirror::Class::kStatusError;
jeffhaoec014232012-09-05 10:42:25 -0700224 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800225 status = mirror::Class::kStatusNotReady;
jeffhaoec014232012-09-05 10:42:25 -0700226 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800227
Brian Carlstromba150c32013-08-27 17:31:03 -0700228 OatClass* oat_class = new OatClass(offset, compiled_methods.release(),
229 num_non_null_compiled_methods, status);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800230 oat_classes_.push_back(oat_class);
231 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700232 }
Ian Rogers3d504072014-03-01 09:16:49 -0800233 oat_dex_files_[i]->UpdateChecksum(oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700234 }
235 return offset;
236}
237
238size_t OatWriter::InitOatCode(size_t offset) {
239 // calculate the offsets within OatHeader to executable code
240 size_t old_offset = offset;
241 // required to be on a new page boundary
242 offset = RoundUp(offset, kPageSize);
243 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700244 size_executable_offset_alignment_ = offset - old_offset;
245 if (compiler_driver_->IsImage()) {
246 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700247
Ian Rogers848871b2013-08-05 10:56:33 -0700248 #define DO_TRAMPOLINE(field, fn_name) \
249 offset = CompiledCode::AlignCode(offset, instruction_set); \
250 oat_header_->Set ## fn_name ## Offset(offset); \
251 field.reset(compiler_driver_->Create ## fn_name()); \
252 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700253
Ian Rogers848871b2013-08-05 10:56:33 -0700254 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge);
255 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge);
256 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Jeff Hao88474b42013-10-23 16:24:40 -0700257 DO_TRAMPOLINE(portable_imt_conflict_trampoline_, PortableImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700258 DO_TRAMPOLINE(portable_resolution_trampoline_, PortableResolutionTrampoline);
259 DO_TRAMPOLINE(portable_to_interpreter_bridge_, PortableToInterpreterBridge);
Andreas Gampe2da88232014-02-27 12:26:20 -0800260 DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline);
Jeff Hao88474b42013-10-23 16:24:40 -0700261 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700262 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
263 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700264
Ian Rogers848871b2013-08-05 10:56:33 -0700265 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700266 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700267 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
268 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
269 oat_header_->SetJniDlsymLookupOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700270 oat_header_->SetPortableImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700271 oat_header_->SetPortableResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700272 oat_header_->SetPortableToInterpreterBridgeOffset(0);
Andreas Gampe2da88232014-02-27 12:26:20 -0800273 oat_header_->SetQuickGenericJniTrampolineOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700274 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700275 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700276 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700277 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700278 return offset;
279}
280
281size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700282 size_t oat_class_index = 0;
283 for (size_t i = 0; i != dex_files_->size(); ++i) {
284 const DexFile* dex_file = (*dex_files_)[i];
285 CHECK(dex_file != NULL);
Brian Carlstromba150c32013-08-27 17:31:03 -0700286 offset = InitOatCodeDexFile(offset, &oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700287 }
288 return offset;
289}
290
291size_t OatWriter::InitOatCodeDexFile(size_t offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700292 size_t* oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700293 const DexFile& dex_file) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800294 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700295 class_def_index < dex_file.NumClassDefs();
Brian Carlstromba150c32013-08-27 17:31:03 -0700296 class_def_index++, (*oat_class_index)++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700297 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstromba150c32013-08-27 17:31:03 -0700298 offset = InitOatCodeClassDef(offset, *oat_class_index, class_def_index, dex_file, class_def);
Ian Rogers3d504072014-03-01 09:16:49 -0800299 oat_classes_[*oat_class_index]->UpdateChecksum(oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700300 }
301 return offset;
302}
303
304size_t OatWriter::InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800305 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700306 const DexFile& dex_file,
307 const DexFile::ClassDef& class_def) {
308 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700309 if (class_data == NULL) {
310 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700311 return offset;
312 }
Ian Rogers0571d352011-11-03 19:51:38 -0700313 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstromba150c32013-08-27 17:31:03 -0700314 CHECK_LE(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700315 it.NumDirectMethods() + it.NumVirtualMethods());
316 // Skip fields
317 while (it.HasNextStaticField()) {
318 it.Next();
319 }
320 while (it.HasNextInstanceField()) {
321 it.Next();
322 }
323 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700324 size_t class_def_method_index = 0;
Brian Carlstromba150c32013-08-27 17:31:03 -0700325 size_t method_offsets_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700326 while (it.HasNextDirectMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800327 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700328 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700329 &method_offsets_index, is_native,
330 it.GetMethodInvokeType(class_def), it.GetMemberIndex(), dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700331 class_def_method_index++;
332 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700333 }
Ian Rogers0571d352011-11-03 19:51:38 -0700334 while (it.HasNextVirtualMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800335 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700336 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700337 &method_offsets_index, is_native,
338 it.GetMethodInvokeType(class_def), it.GetMemberIndex(), dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700339 class_def_method_index++;
340 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700341 }
Ian Rogers0571d352011-11-03 19:51:38 -0700342 DCHECK(!it.HasNext());
Brian Carlstromba150c32013-08-27 17:31:03 -0700343 CHECK_LE(method_offsets_index, class_def_method_index);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700344 return offset;
345}
346
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700347size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
348 size_t __attribute__((unused)) class_def_index,
349 size_t class_def_method_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700350 size_t* method_offsets_index,
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700351 bool __attribute__((unused)) is_native,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800352 InvokeType invoke_type,
Brian Carlstromba150c32013-08-27 17:31:03 -0700353 uint32_t method_idx, const DexFile& dex_file) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800354 // Derived from CompiledMethod if available.
355 uint32_t quick_code_offset = 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700356 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700357 uint32_t core_spill_mask = 0;
358 uint32_t fp_spill_mask = 0;
359 uint32_t mapping_table_offset = 0;
360 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800361 uint32_t gc_map_offset = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700362
Brian Carlstrom265091e2013-01-30 14:08:26 -0800363 OatClass* oat_class = oat_classes_[oat_class_index];
Brian Carlstromba150c32013-08-27 17:31:03 -0700364 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
Logan Chien971bf3f2012-05-01 15:47:55 +0800365
Ian Rogersef7d42f2014-01-06 12:55:46 -0800366 if (compiled_method != NULL) {
367 const std::vector<uint8_t>* portable_code = compiled_method->GetPortableCode();
368 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode();
369 if (portable_code != nullptr) {
370 CHECK(quick_code == nullptr);
371 size_t oat_method_offsets_offset =
372 oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index);
373 compiled_method->AddOatdataOffsetToCompliledCodeOffset(
374 oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_));
jeffhao55d78212011-11-02 11:41:50 -0700375 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800376 CHECK(quick_code != nullptr);
377 offset = compiled_method->AlignCode(offset);
378 DCHECK_ALIGNED(offset, kArmAlignment);
379 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
380 CHECK_NE(code_size, 0U);
381 uint32_t thumb_offset = compiled_method->CodeDelta();
382 quick_code_offset = offset + sizeof(code_size) + thumb_offset;
383
Mark Mendellae9fd932014-02-10 16:14:35 -0800384 std::vector<uint8_t>* cfi_info = compiler_driver_->GetCallFrameInformation();
385 if (cfi_info != nullptr) {
386 // Copy in the FDE, if present
387 const std::vector<uint8_t>* fde = compiled_method->GetCFIInfo();
388 if (fde != nullptr) {
389 // Copy the information into cfi_info and then fix the address in the new copy.
390 int cur_offset = cfi_info->size();
391 cfi_info->insert(cfi_info->end(), fde->begin(), fde->end());
392
393 // Set the 'initial_location' field to address the start of the method.
394 uint32_t new_value = quick_code_offset - oat_header_->GetExecutableOffset();
395 uint32_t offset_to_update = cur_offset + 2*sizeof(uint32_t);
396 (*cfi_info)[offset_to_update+0] = new_value;
397 (*cfi_info)[offset_to_update+1] = new_value >> 8;
398 (*cfi_info)[offset_to_update+2] = new_value >> 16;
399 (*cfi_info)[offset_to_update+3] = new_value >> 24;
400 method_info_.push_back(DebugInfo(PrettyMethod(class_def_method_index, dex_file, false),
401 new_value, new_value + code_size));
402 }
403 }
404
Ian Rogersef7d42f2014-01-06 12:55:46 -0800405 // Deduplicate code arrays
406 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter =
407 code_offsets_.find(quick_code);
408 if (code_iter != code_offsets_.end()) {
409 quick_code_offset = code_iter->second;
410 } else {
411 code_offsets_.Put(quick_code, quick_code_offset);
412 offset += sizeof(code_size); // code size is prepended before code
413 offset += code_size;
414 oat_header_->UpdateChecksum(&(*quick_code)[0], code_size);
415 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800416 }
417 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
418 core_spill_mask = compiled_method->GetCoreSpillMask();
419 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700420
Ian Rogers1809a722013-08-09 22:05:32 -0700421 const std::vector<uint8_t>& mapping_table = compiled_method->GetMappingTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800422 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
423 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700424
Logan Chien971bf3f2012-05-01 15:47:55 +0800425 // Deduplicate mapping tables
Ian Rogers1809a722013-08-09 22:05:32 -0700426 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator mapping_iter =
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700427 mapping_table_offsets_.find(&mapping_table);
Logan Chien971bf3f2012-05-01 15:47:55 +0800428 if (mapping_iter != mapping_table_offsets_.end()) {
429 mapping_table_offset = mapping_iter->second;
430 } else {
431 mapping_table_offsets_.Put(&mapping_table, mapping_table_offset);
432 offset += mapping_table_size;
433 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
434 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700435
Ian Rogers1809a722013-08-09 22:05:32 -0700436 const std::vector<uint8_t>& vmap_table = compiled_method->GetVmapTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800437 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
438 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700439
Logan Chien971bf3f2012-05-01 15:47:55 +0800440 // Deduplicate vmap tables
Ian Rogers1809a722013-08-09 22:05:32 -0700441 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator vmap_iter =
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700442 vmap_table_offsets_.find(&vmap_table);
Logan Chien971bf3f2012-05-01 15:47:55 +0800443 if (vmap_iter != vmap_table_offsets_.end()) {
444 vmap_table_offset = vmap_iter->second;
445 } else {
446 vmap_table_offsets_.Put(&vmap_table, vmap_table_offset);
447 offset += vmap_table_size;
448 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
449 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800450
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700451 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800452 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
453 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
Logan Chienccb7bf12012-03-28 12:52:32 +0800454
Mathieu Chartier590fee92013-09-13 13:46:47 -0700455 if (kIsDebugBuild) {
456 // We expect GC maps except when the class hasn't been verified or the method is native
457 ClassReference class_ref(&dex_file, class_def_index);
458 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
459 mirror::Class::Status status;
460 if (compiled_class != NULL) {
461 status = compiled_class->GetStatus();
Vladimir Markoc7f83202014-01-24 17:55:18 +0000462 } else if (compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700463 status = mirror::Class::kStatusError;
464 } else {
465 status = mirror::Class::kStatusNotReady;
466 }
467 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
468 << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
469 << (status < mirror::Class::kStatusVerified) << " " << status << " "
470 << PrettyMethod(method_idx, dex_file);
jeffhaoec014232012-09-05 10:42:25 -0700471 }
Ian Rogersc20a83e2012-01-18 18:15:32 -0800472
Logan Chien971bf3f2012-05-01 15:47:55 +0800473 // Deduplicate GC maps
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700474 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
475 gc_map_offsets_.find(&gc_map);
Logan Chien971bf3f2012-05-01 15:47:55 +0800476 if (gc_map_iter != gc_map_offsets_.end()) {
477 gc_map_offset = gc_map_iter->second;
478 } else {
479 gc_map_offsets_.Put(&gc_map, gc_map_offset);
480 offset += gc_map_size;
481 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800482 }
Brian Carlstromba150c32013-08-27 17:31:03 -0700483
484 oat_class->method_offsets_[*method_offsets_index] =
Ian Rogersef7d42f2014-01-06 12:55:46 -0800485 OatMethodOffsets(quick_code_offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700486 frame_size_in_bytes,
487 core_spill_mask,
488 fp_spill_mask,
489 mapping_table_offset,
490 vmap_table_offset,
491 gc_map_offset);
492 (*method_offsets_index)++;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700493 }
494
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700495
Ian Rogers1212a022013-03-04 10:48:41 -0800496 if (compiler_driver_->IsImage()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700497 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700498 // Unchecked as we hold mutator_lock_ on entry.
499 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700500 SirtRef<mirror::DexCache> dex_cache(soa.Self(), linker->FindDexCache(dex_file));
501 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700502 mirror::ArtMethod* method = linker->ResolveMethod(dex_file, method_idx, dex_cache,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700503 class_loader, nullptr, invoke_type);
Ian Rogers0571d352011-11-03 19:51:38 -0700504 CHECK(method != NULL);
505 method->SetFrameSizeInBytes(frame_size_in_bytes);
506 method->SetCoreSpillMask(core_spill_mask);
507 method->SetFpSpillMask(fp_spill_mask);
508 method->SetOatMappingTableOffset(mapping_table_offset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800509 // Portable code offsets are set by ElfWriterMclinker::FixupCompiledCodeOffset after linking.
510 method->SetQuickOatCodeOffset(quick_code_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700511 method->SetOatVmapTableOffset(vmap_table_offset);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700512 method->SetOatNativeGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700513 }
Logan Chien8b977d32012-02-21 19:14:55 +0800514
Brian Carlstrome24fa612011-09-29 00:53:55 -0700515 return offset;
516}
517
Brian Carlstrom265091e2013-01-30 14:08:26 -0800518#define DCHECK_OFFSET() \
Ian Rogers3d504072014-03-01 09:16:49 -0800519 DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out->Seek(0, kSeekCurrent)) \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700520 << "file_offset=" << file_offset << " relative_offset=" << relative_offset
Brian Carlstrom265091e2013-01-30 14:08:26 -0800521
522#define DCHECK_OFFSET_() \
Ian Rogers3d504072014-03-01 09:16:49 -0800523 DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out->Seek(0, kSeekCurrent)) \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700524 << "file_offset=" << file_offset << " offset_=" << offset_
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700525
Ian Rogers3d504072014-03-01 09:16:49 -0800526bool OatWriter::Write(OutputStream* out) {
527 const size_t file_offset = out->Seek(0, kSeekCurrent);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700528
Ian Rogers3d504072014-03-01 09:16:49 -0800529 if (!out->WriteFully(oat_header_, sizeof(*oat_header_))) {
530 PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700531 return false;
532 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700533 size_oat_header_ += sizeof(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700534
Ian Rogers3d504072014-03-01 09:16:49 -0800535 if (!out->WriteFully(image_file_location_.data(), image_file_location_.size())) {
536 PLOG(ERROR) << "Failed to write oat header image file location to " << out->GetLocation();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700537 return false;
538 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700539 size_oat_header_image_file_location_ += image_file_location_.size();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700540
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700541 if (!WriteTables(out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800542 LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700543 return false;
544 }
545
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700546 size_t relative_offset = WriteCode(out, file_offset);
547 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800548 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700549 return false;
550 }
551
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700552 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
553 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800554 LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700555 return false;
556 }
557
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700558 if (kIsDebugBuild) {
559 uint32_t size_total = 0;
560 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700561 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700562 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700563
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700564 DO_STAT(size_dex_file_alignment_);
565 DO_STAT(size_executable_offset_alignment_);
566 DO_STAT(size_oat_header_);
567 DO_STAT(size_oat_header_image_file_location_);
568 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -0700569 DO_STAT(size_interpreter_to_interpreter_bridge_);
570 DO_STAT(size_interpreter_to_compiled_code_bridge_);
571 DO_STAT(size_jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -0700572 DO_STAT(size_portable_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700573 DO_STAT(size_portable_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700574 DO_STAT(size_portable_to_interpreter_bridge_);
Andreas Gampe2da88232014-02-27 12:26:20 -0800575 DO_STAT(size_quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -0700576 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700577 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700578 DO_STAT(size_quick_to_interpreter_bridge_);
579 DO_STAT(size_trampoline_alignment_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700580 DO_STAT(size_code_size_);
581 DO_STAT(size_code_);
582 DO_STAT(size_code_alignment_);
583 DO_STAT(size_mapping_table_);
584 DO_STAT(size_vmap_table_);
585 DO_STAT(size_gc_map_);
586 DO_STAT(size_oat_dex_file_location_size_);
587 DO_STAT(size_oat_dex_file_location_data_);
588 DO_STAT(size_oat_dex_file_location_checksum_);
589 DO_STAT(size_oat_dex_file_offset_);
590 DO_STAT(size_oat_dex_file_methods_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700591 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700592 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700593 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700594 DO_STAT(size_oat_class_method_offsets_);
595 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700596
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700597 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Ian Rogers3d504072014-03-01 09:16:49 -0800598 CHECK_EQ(file_offset + size_total, static_cast<uint32_t>(out->Seek(0, kSeekCurrent)));
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700599 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700600 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700601
Ian Rogers3d504072014-03-01 09:16:49 -0800602 CHECK_EQ(file_offset + size_, static_cast<uint32_t>(out->Seek(0, kSeekCurrent)));
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700603 CHECK_EQ(size_, relative_offset);
604
Brian Carlstrome24fa612011-09-29 00:53:55 -0700605 return true;
606}
607
Ian Rogers3d504072014-03-01 09:16:49 -0800608bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700609 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700610 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800611 PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700612 return false;
613 }
614 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800615 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700616 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Ian Rogers3d504072014-03-01 09:16:49 -0800617 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -0800618 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
619 const DexFile* dex_file = (*dex_files_)[i];
620 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
621 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
622 return false;
623 }
624 const DexFile* dex_file = (*dex_files_)[i];
Ian Rogers3d504072014-03-01 09:16:49 -0800625 if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700626 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
Ian Rogers3d504072014-03-01 09:16:49 -0800627 << " to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800628 return false;
629 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700630 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800631 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800632 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700633 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800634 PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700635 return false;
636 }
637 }
638 return true;
639}
640
Ian Rogers3d504072014-03-01 09:16:49 -0800641size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700642 size_t relative_offset = oat_header_->GetExecutableOffset();
Ian Rogers3d504072014-03-01 09:16:49 -0800643 off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700644 size_t expected_file_offset = file_offset + relative_offset;
645 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700646 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Ian Rogers3d504072014-03-01 09:16:49 -0800647 << " Expected: " << expected_file_offset << " File: " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700648 return 0;
649 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800650 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700651 if (compiler_driver_->IsImage()) {
652 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700653
Ian Rogers848871b2013-08-05 10:56:33 -0700654 #define DO_TRAMPOLINE(field) \
655 do { \
656 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
657 uint32_t alignment_padding = aligned_offset - relative_offset; \
Ian Rogers3d504072014-03-01 09:16:49 -0800658 out->Seek(alignment_padding, kSeekCurrent); \
Ian Rogers848871b2013-08-05 10:56:33 -0700659 size_trampoline_alignment_ += alignment_padding; \
Ian Rogers3d504072014-03-01 09:16:49 -0800660 if (!out->WriteFully(&(*field)[0], field->size())) { \
661 PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \
Ian Rogers848871b2013-08-05 10:56:33 -0700662 return false; \
663 } \
664 size_ ## field += field->size(); \
665 relative_offset += alignment_padding + field->size(); \
666 DCHECK_OFFSET(); \
667 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700668
Ian Rogers848871b2013-08-05 10:56:33 -0700669 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_);
670 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_);
671 DO_TRAMPOLINE(jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -0700672 DO_TRAMPOLINE(portable_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700673 DO_TRAMPOLINE(portable_resolution_trampoline_);
674 DO_TRAMPOLINE(portable_to_interpreter_bridge_);
Andreas Gampe2da88232014-02-27 12:26:20 -0800675 DO_TRAMPOLINE(quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -0700676 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700677 DO_TRAMPOLINE(quick_resolution_trampoline_);
678 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
679 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700680 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700681 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700682}
683
Ian Rogers3d504072014-03-01 09:16:49 -0800684size_t OatWriter::WriteCodeDexFiles(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700685 const size_t file_offset,
686 size_t relative_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700687 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800688 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700689 const DexFile* dex_file = (*dex_files_)[i];
690 CHECK(dex_file != NULL);
Brian Carlstromba150c32013-08-27 17:31:03 -0700691 relative_offset = WriteCodeDexFile(out, file_offset, relative_offset, &oat_class_index,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700692 *dex_file);
693 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700694 return 0;
695 }
696 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700697 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700698}
699
Ian Rogers3d504072014-03-01 09:16:49 -0800700size_t OatWriter::WriteCodeDexFile(OutputStream* out, const size_t file_offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700701 size_t relative_offset, size_t* oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700702 const DexFile& dex_file) {
703 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
Brian Carlstromba150c32013-08-27 17:31:03 -0700704 class_def_index++, (*oat_class_index)++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700705 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstromba150c32013-08-27 17:31:03 -0700706 relative_offset = WriteCodeClassDef(out, file_offset, relative_offset, *oat_class_index,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700707 dex_file, class_def);
708 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700709 return 0;
710 }
711 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700712 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700713}
714
Ian Rogers0571d352011-11-03 19:51:38 -0700715void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
Ian Rogers3d504072014-03-01 09:16:49 -0800716 const DexFile& dex_file, const OutputStream& out) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700717 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800718 << " to " << out.GetLocation();
Elliott Hughes234da572011-11-03 22:13:06 -0700719}
720
Ian Rogers3d504072014-03-01 09:16:49 -0800721size_t OatWriter::WriteCodeClassDef(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700722 const size_t file_offset,
723 size_t relative_offset,
724 size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700725 const DexFile& dex_file,
726 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700727 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700728 if (class_data == NULL) {
729 // ie. an empty class such as a marker interface
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700730 return relative_offset;
Ian Rogers387b6992011-10-31 17:52:37 -0700731 }
Ian Rogers0571d352011-11-03 19:51:38 -0700732 ClassDataItemIterator it(dex_file, class_data);
733 // Skip fields
734 while (it.HasNextStaticField()) {
735 it.Next();
736 }
737 while (it.HasNextInstanceField()) {
738 it.Next();
739 }
740 // Process methods
741 size_t class_def_method_index = 0;
Brian Carlstromba150c32013-08-27 17:31:03 -0700742 size_t method_offsets_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700743 while (it.HasNextDirectMethod()) {
744 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700745 relative_offset = WriteCodeMethod(out, file_offset, relative_offset, oat_class_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700746 class_def_method_index, &method_offsets_index, is_static,
747 it.GetMemberIndex(), dex_file);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700748 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700749 return 0;
750 }
Ian Rogers0571d352011-11-03 19:51:38 -0700751 class_def_method_index++;
752 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700753 }
Ian Rogers0571d352011-11-03 19:51:38 -0700754 while (it.HasNextVirtualMethod()) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700755 relative_offset = WriteCodeMethod(out, file_offset, relative_offset, oat_class_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700756 class_def_method_index, &method_offsets_index, false,
757 it.GetMemberIndex(), dex_file);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700758 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700759 return 0;
760 }
Ian Rogers0571d352011-11-03 19:51:38 -0700761 class_def_method_index++;
762 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700763 }
Brian Carlstromba150c32013-08-27 17:31:03 -0700764 DCHECK(!it.HasNext());
765 CHECK_LE(method_offsets_index, class_def_method_index);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700766 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700767}
768
Ian Rogers3d504072014-03-01 09:16:49 -0800769size_t OatWriter::WriteCodeMethod(OutputStream* out, const size_t file_offset,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700770 size_t relative_offset, size_t oat_class_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700771 size_t class_def_method_index, size_t* method_offsets_index,
772 bool is_static, uint32_t method_idx, const DexFile& dex_file) {
773 OatClass* oat_class = oat_classes_[oat_class_index];
774 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700775
776 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstromba150c32013-08-27 17:31:03 -0700777 const OatMethodOffsets method_offsets = oat_class->method_offsets_[*method_offsets_index];
778 (*method_offsets_index)++;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800779 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode();
780 if (quick_code != nullptr) {
781 CHECK(compiled_method->GetPortableCode() == nullptr);
782 uint32_t aligned_offset = compiled_method->AlignCode(relative_offset);
783 uint32_t aligned_code_delta = aligned_offset - relative_offset;
784 if (aligned_code_delta != 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800785 off_t new_offset = out->Seek(aligned_code_delta, kSeekCurrent);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800786 size_code_alignment_ += aligned_code_delta;
787 uint32_t expected_offset = file_offset + aligned_offset;
788 if (static_cast<uint32_t>(new_offset) != expected_offset) {
789 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Ian Rogers3d504072014-03-01 09:16:49 -0800790 << " Expected: " << expected_offset << " File: " << out->GetLocation();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800791 return 0;
792 }
793 relative_offset += aligned_code_delta;
794 DCHECK_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700795 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800796 DCHECK_ALIGNED(relative_offset, kArmAlignment);
797 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
798 CHECK_NE(code_size, 0U);
799
800 // Deduplicate code arrays
801 size_t code_offset = relative_offset + sizeof(code_size) + compiled_method->CodeDelta();
802 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter =
803 code_offsets_.find(quick_code);
804 if (code_iter != code_offsets_.end() && code_offset != method_offsets.code_offset_) {
805 DCHECK(code_iter->second == method_offsets.code_offset_)
806 << PrettyMethod(method_idx, dex_file);
807 } else {
808 DCHECK(code_offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
Ian Rogers3d504072014-03-01 09:16:49 -0800809 if (!out->WriteFully(&code_size, sizeof(code_size))) {
810 ReportWriteFailure("method code size", method_idx, dex_file, *out);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800811 return 0;
812 }
813 size_code_size_ += sizeof(code_size);
814 relative_offset += sizeof(code_size);
815 DCHECK_OFFSET();
Ian Rogers3d504072014-03-01 09:16:49 -0800816 if (!out->WriteFully(&(*quick_code)[0], code_size)) {
817 ReportWriteFailure("method code", method_idx, dex_file, *out);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800818 return 0;
819 }
820 size_code_ += code_size;
821 relative_offset += code_size;
822 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800823 DCHECK_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700824 }
Ian Rogers1809a722013-08-09 22:05:32 -0700825 const std::vector<uint8_t>& mapping_table = compiled_method->GetMappingTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800826 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
827
828 // Deduplicate mapping tables
Ian Rogers1809a722013-08-09 22:05:32 -0700829 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator mapping_iter =
Logan Chien971bf3f2012-05-01 15:47:55 +0800830 mapping_table_offsets_.find(&mapping_table);
831 if (mapping_iter != mapping_table_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700832 relative_offset != method_offsets.mapping_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800833 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
834 || mapping_iter->second == method_offsets.mapping_table_offset_)
835 << PrettyMethod(method_idx, dex_file);
836 } else {
837 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700838 || relative_offset == method_offsets.mapping_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800839 << PrettyMethod(method_idx, dex_file);
Ian Rogers3d504072014-03-01 09:16:49 -0800840 if (!out->WriteFully(&mapping_table[0], mapping_table_size)) {
841 ReportWriteFailure("mapping table", method_idx, dex_file, *out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800842 return 0;
843 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700844 size_mapping_table_ += mapping_table_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700845 relative_offset += mapping_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800846 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800847 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800848
Ian Rogers1809a722013-08-09 22:05:32 -0700849 const std::vector<uint8_t>& vmap_table = compiled_method->GetVmapTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800850 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
851
852 // Deduplicate vmap tables
Ian Rogers1809a722013-08-09 22:05:32 -0700853 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator vmap_iter =
Logan Chien971bf3f2012-05-01 15:47:55 +0800854 vmap_table_offsets_.find(&vmap_table);
855 if (vmap_iter != vmap_table_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700856 relative_offset != method_offsets.vmap_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800857 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
858 || vmap_iter->second == method_offsets.vmap_table_offset_)
859 << PrettyMethod(method_idx, dex_file);
860 } else {
861 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700862 || relative_offset == method_offsets.vmap_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800863 << PrettyMethod(method_idx, dex_file);
Ian Rogers3d504072014-03-01 09:16:49 -0800864 if (!out->WriteFully(&vmap_table[0], vmap_table_size)) {
865 ReportWriteFailure("vmap table", method_idx, dex_file, *out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800866 return 0;
867 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700868 size_vmap_table_ += vmap_table_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700869 relative_offset += vmap_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800870 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800871 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800872
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700873 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800874 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
875
876 // Deduplicate GC maps
877 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
878 gc_map_offsets_.find(&gc_map);
879 if (gc_map_iter != gc_map_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700880 relative_offset != method_offsets.gc_map_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800881 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
882 || gc_map_iter->second == method_offsets.gc_map_offset_)
883 << PrettyMethod(method_idx, dex_file);
884 } else {
885 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700886 || relative_offset == method_offsets.gc_map_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800887 << PrettyMethod(method_idx, dex_file);
Ian Rogers3d504072014-03-01 09:16:49 -0800888 if (!out->WriteFully(&gc_map[0], gc_map_size)) {
889 ReportWriteFailure("GC map", method_idx, dex_file, *out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800890 return 0;
891 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700892 size_gc_map_ += gc_map_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700893 relative_offset += gc_map_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800894 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800895 DCHECK_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700896 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800897
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700898 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700899}
900
Brian Carlstrom265091e2013-01-30 14:08:26 -0800901OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
902 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -0800903 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700904 dex_file_location_size_ = location.size();
905 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800906 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800907 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800908 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700909}
910
911size_t OatWriter::OatDexFile::SizeOf() const {
912 return sizeof(dex_file_location_size_)
913 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800914 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800915 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800916 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700917}
918
Ian Rogers3d504072014-03-01 09:16:49 -0800919void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const {
920 oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
921 oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
922 oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
923 oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
924 oat_header->UpdateChecksum(&methods_offsets_[0],
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800925 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700926}
927
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700928bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -0800929 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700930 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800931 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -0800932 if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
933 PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700934 return false;
935 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700936 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Ian Rogers3d504072014-03-01 09:16:49 -0800937 if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
938 PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700939 return false;
940 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700941 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Ian Rogers3d504072014-03-01 09:16:49 -0800942 if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
943 PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700944 return false;
945 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700946 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Ian Rogers3d504072014-03-01 09:16:49 -0800947 if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
948 PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800949 return false;
950 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700951 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Ian Rogers3d504072014-03-01 09:16:49 -0800952 if (!out->WriteFully(&methods_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800953 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -0800954 PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700955 return false;
956 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700957 oat_writer->size_oat_dex_file_methods_offsets_ +=
958 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700959 return true;
960}
961
Brian Carlstromba150c32013-08-27 17:31:03 -0700962OatWriter::OatClass::OatClass(size_t offset,
963 std::vector<CompiledMethod*>* compiled_methods,
964 uint32_t num_non_null_compiled_methods,
965 mirror::Class::Status status) {
966 CHECK(compiled_methods != NULL);
967 uint32_t num_methods = compiled_methods->size();
968 CHECK_LE(num_non_null_compiled_methods, num_methods);
969
Brian Carlstrom265091e2013-01-30 14:08:26 -0800970 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -0700971 compiled_methods_ = compiled_methods;
972 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
973
974 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
975 // apply when there are 0 methods, we just arbitrarily say that 0
976 // methods means kOatClassNoneCompiled and that we won't use
977 // kOatClassAllCompiled unless there is at least one compiled
978 // method. This means in an interpretter only system, we can assert
979 // that all classes are kOatClassNoneCompiled.
980 if (num_non_null_compiled_methods == 0) {
981 type_ = kOatClassNoneCompiled;
982 } else if (num_non_null_compiled_methods == num_methods) {
983 type_ = kOatClassAllCompiled;
984 } else {
985 type_ = kOatClassSomeCompiled;
986 }
987
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800988 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -0700989 method_offsets_.resize(num_non_null_compiled_methods);
990
991 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
992 if (type_ == kOatClassSomeCompiled) {
993 method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator());
994 method_bitmap_size_ = method_bitmap_->GetSizeOf();
995 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
996 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
997 } else {
998 method_bitmap_ = NULL;
999 method_bitmap_size_ = 0;
1000 }
1001
1002 for (size_t i = 0; i < num_methods; i++) {
1003 CompiledMethod* compiled_method = (*compiled_methods_)[i];
1004 if (compiled_method == NULL) {
1005 oat_method_offsets_offsets_from_oat_class_[i] = 0;
1006 } else {
1007 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
1008 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
1009 if (type_ == kOatClassSomeCompiled) {
1010 method_bitmap_->SetBit(i);
1011 }
1012 }
1013 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001014}
1015
Brian Carlstromba150c32013-08-27 17:31:03 -07001016OatWriter::OatClass::~OatClass() {
Mathieu Chartier661974a2014-01-09 11:23:53 -08001017 delete method_bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -07001018 delete compiled_methods_;
1019}
1020
Brian Carlstrom265091e2013-01-30 14:08:26 -08001021size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
1022 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001023 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
1024 if (method_offset == 0) {
1025 return 0;
1026 }
1027 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001028}
1029
1030size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
1031 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001032 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08001033}
1034
1035size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001036 return sizeof(status_)
1037 + sizeof(type_)
1038 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
1039 + method_bitmap_size_
1040 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001041}
1042
Ian Rogers3d504072014-03-01 09:16:49 -08001043void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const {
1044 oat_header->UpdateChecksum(&status_, sizeof(status_));
1045 oat_header->UpdateChecksum(&type_, sizeof(type_));
Brian Carlstromba150c32013-08-27 17:31:03 -07001046 if (method_bitmap_size_ != 0) {
1047 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001048 oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_));
1049 oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001050 }
Ian Rogers3d504072014-03-01 09:16:49 -08001051 oat_header->UpdateChecksum(&method_offsets_[0],
1052 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001053}
1054
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001055bool OatWriter::OatClass::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001056 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001057 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001058 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001059 if (!out->WriteFully(&status_, sizeof(status_))) {
1060 PLOG(ERROR) << "Failed to write class status to " << out->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001061 return false;
1062 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001063 oat_writer->size_oat_class_status_ += sizeof(status_);
Ian Rogers3d504072014-03-01 09:16:49 -08001064 if (!out->WriteFully(&type_, sizeof(type_))) {
1065 PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001066 return false;
1067 }
1068 oat_writer->size_oat_class_type_ += sizeof(type_);
1069 if (method_bitmap_size_ != 0) {
1070 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001071 if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
1072 PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001073 return false;
1074 }
1075 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001076 if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
1077 PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001078 return false;
1079 }
1080 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1081 }
Ian Rogers3d504072014-03-01 09:16:49 -08001082 if (!out->WriteFully(&method_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001083 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001084 PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001085 return false;
1086 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001087 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001088 return true;
1089}
1090
Brian Carlstrome24fa612011-09-29 00:53:55 -07001091} // namespace art