blob: eff2425bb7b301d0e33a789a3b582f2f2aec7612 [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"
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080036#include "sirt_ref-inl.h"
jeffhaoec014232012-09-05 10:42:25 -070037#include "verifier/method_verifier.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070038
39namespace art {
40
Brian Carlstrom3320cf42011-10-04 14:58:28 -070041OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070042 uint32_t image_file_location_oat_checksum,
Ian Rogersef7d42f2014-01-06 12:55:46 -080043 uintptr_t image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070044 const std::string& image_file_location,
Ian Rogersca368cb2013-11-15 15:52:08 -080045 const CompilerDriver* compiler,
46 TimingLogger* timings)
Jeff Hao0aba0ba2013-06-03 14:49:28 -070047 : compiler_driver_(compiler),
48 dex_files_(&dex_files),
49 image_file_location_oat_checksum_(image_file_location_oat_checksum),
50 image_file_location_oat_begin_(image_file_location_oat_begin),
51 image_file_location_(image_file_location),
52 oat_header_(NULL),
53 size_dex_file_alignment_(0),
54 size_executable_offset_alignment_(0),
55 size_oat_header_(0),
56 size_oat_header_image_file_location_(0),
57 size_dex_file_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070058 size_interpreter_to_interpreter_bridge_(0),
59 size_interpreter_to_compiled_code_bridge_(0),
60 size_jni_dlsym_lookup_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070061 size_portable_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070062 size_portable_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070063 size_portable_to_interpreter_bridge_(0),
Andreas Gampe2da88232014-02-27 12:26:20 -080064 size_quick_generic_jni_trampoline_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070065 size_quick_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070066 size_quick_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070067 size_quick_to_interpreter_bridge_(0),
68 size_trampoline_alignment_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070069 size_code_size_(0),
70 size_code_(0),
71 size_code_alignment_(0),
72 size_mapping_table_(0),
73 size_vmap_table_(0),
74 size_gc_map_(0),
75 size_oat_dex_file_location_size_(0),
76 size_oat_dex_file_location_data_(0),
77 size_oat_dex_file_location_checksum_(0),
78 size_oat_dex_file_offset_(0),
79 size_oat_dex_file_methods_offsets_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -070080 size_oat_class_type_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070081 size_oat_class_status_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -070082 size_oat_class_method_bitmaps_(0),
Dave Allisond6ed6422014-04-09 23:36:15 +000083 size_oat_class_method_offsets_(0) {
Ian Rogersca368cb2013-11-15 15:52:08 -080084 size_t offset;
85 {
86 TimingLogger::ScopedSplit split("InitOatHeader", timings);
87 offset = InitOatHeader();
88 }
89 {
90 TimingLogger::ScopedSplit split("InitOatDexFiles", timings);
91 offset = InitOatDexFiles(offset);
92 }
93 {
94 TimingLogger::ScopedSplit split("InitDexFiles", timings);
95 offset = InitDexFiles(offset);
96 }
97 {
98 TimingLogger::ScopedSplit split("InitOatClasses", timings);
99 offset = InitOatClasses(offset);
100 }
101 {
102 TimingLogger::ScopedSplit split("InitOatCode", timings);
103 offset = InitOatCode(offset);
104 }
105 {
106 TimingLogger::ScopedSplit split("InitOatCodeDexFiles", timings);
107 offset = InitOatCodeDexFiles(offset);
108 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700109 size_ = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700110
111 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700112 CHECK(image_file_location.empty() == compiler->IsImage());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700113}
114
Ian Rogers0571d352011-11-03 19:51:38 -0700115OatWriter::~OatWriter() {
116 delete oat_header_;
117 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800118 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -0700119}
120
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700121size_t OatWriter::InitOatHeader() {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700122 // create the OatHeader
Ian Rogers1212a022013-03-04 10:48:41 -0800123 oat_header_ = new OatHeader(compiler_driver_->GetInstructionSet(),
Dave Allison70202782013-10-22 17:52:19 -0700124 compiler_driver_->GetInstructionSetFeatures(),
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700125 dex_files_,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700126 image_file_location_oat_checksum_,
127 image_file_location_oat_begin_,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700128 image_file_location_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700129 size_t offset = sizeof(*oat_header_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700130 offset += image_file_location_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700131 return offset;
132}
133
134size_t OatWriter::InitOatDexFiles(size_t offset) {
135 // create the OatDexFiles
136 for (size_t i = 0; i != dex_files_->size(); ++i) {
137 const DexFile* dex_file = (*dex_files_)[i];
138 CHECK(dex_file != NULL);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800139 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700140 oat_dex_files_.push_back(oat_dex_file);
141 offset += oat_dex_file->SizeOf();
142 }
143 return offset;
144}
145
Brian Carlstrom89521892011-12-07 22:05:07 -0800146size_t OatWriter::InitDexFiles(size_t offset) {
147 // calculate the offsets within OatDexFiles to the DexFiles
148 for (size_t i = 0; i != dex_files_->size(); ++i) {
149 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700150 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800151 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700152 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800153
154 // set offset in OatDexFile to DexFile
155 oat_dex_files_[i]->dex_file_offset_ = offset;
156
157 const DexFile* dex_file = (*dex_files_)[i];
158 offset += dex_file->GetHeader().file_size_;
159 }
160 return offset;
161}
162
Brian Carlstrom389efb02012-01-11 12:06:26 -0800163size_t OatWriter::InitOatClasses(size_t offset) {
164 // create the OatClasses
165 // calculate the offsets within OatDexFiles to OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -0700166 for (size_t i = 0; i != dex_files_->size(); ++i) {
167 const DexFile* dex_file = (*dex_files_)[i];
168 for (size_t class_def_index = 0;
169 class_def_index < dex_file->NumClassDefs();
Ian Rogersc20a83e2012-01-18 18:15:32 -0800170 class_def_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800171 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700172 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
173 const byte* class_data = dex_file->GetClassData(class_def);
Brian Carlstromba150c32013-08-27 17:31:03 -0700174 uint32_t num_non_null_compiled_methods = 0;
175 UniquePtr<std::vector<CompiledMethod*> > compiled_methods(new std::vector<CompiledMethod*>());
Ian Rogers0571d352011-11-03 19:51:38 -0700176 if (class_data != NULL) { // ie not an empty class, such as a marker interface
177 ClassDataItemIterator it(*dex_file, class_data);
178 size_t num_direct_methods = it.NumDirectMethods();
179 size_t num_virtual_methods = it.NumVirtualMethods();
Brian Carlstromba150c32013-08-27 17:31:03 -0700180 size_t num_methods = num_direct_methods + num_virtual_methods;
181
182 // Fill in the compiled_methods_ array for methods that have a
183 // CompiledMethod. We track the number of non-null entries in
184 // num_non_null_compiled_methods since we only want to allocate
185 // OatMethodOffsets for the compiled methods.
186 compiled_methods->reserve(num_methods);
187 while (it.HasNextStaticField()) {
188 it.Next();
189 }
190 while (it.HasNextInstanceField()) {
191 it.Next();
192 }
193 size_t class_def_method_index = 0;
194 while (it.HasNextDirectMethod()) {
195 uint32_t method_idx = it.GetMemberIndex();
196 CompiledMethod* compiled_method =
197 compiler_driver_->GetCompiledMethod(MethodReference(dex_file, method_idx));
198 compiled_methods->push_back(compiled_method);
199 if (compiled_method != NULL) {
200 num_non_null_compiled_methods++;
201 }
202 class_def_method_index++;
203 it.Next();
204 }
205 while (it.HasNextVirtualMethod()) {
206 uint32_t method_idx = it.GetMemberIndex();
207 CompiledMethod* compiled_method =
208 compiler_driver_->GetCompiledMethod(MethodReference(dex_file, method_idx));
209 compiled_methods->push_back(compiled_method);
210 if (compiled_method != NULL) {
211 num_non_null_compiled_methods++;
212 }
213 class_def_method_index++;
214 it.Next();
215 }
Ian Rogers0571d352011-11-03 19:51:38 -0700216 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800217
Brian Carlstrom51c24672013-07-11 16:00:56 -0700218 ClassReference class_ref(dex_file, class_def_index);
Ian Rogers1212a022013-03-04 10:48:41 -0800219 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 mirror::Class::Status status;
jeffhaoec014232012-09-05 10:42:25 -0700221 if (compiled_class != NULL) {
222 status = compiled_class->GetStatus();
Vladimir Markoc7f83202014-01-24 17:55:18 +0000223 } else if (compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224 status = mirror::Class::kStatusError;
jeffhaoec014232012-09-05 10:42:25 -0700225 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800226 status = mirror::Class::kStatusNotReady;
jeffhaoec014232012-09-05 10:42:25 -0700227 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800228
Brian Carlstromba150c32013-08-27 17:31:03 -0700229 OatClass* oat_class = new OatClass(offset, compiled_methods.release(),
230 num_non_null_compiled_methods, status);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800231 oat_classes_.push_back(oat_class);
232 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700233 }
Ian Rogers3d504072014-03-01 09:16:49 -0800234 oat_dex_files_[i]->UpdateChecksum(oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700235 }
236 return offset;
237}
238
239size_t OatWriter::InitOatCode(size_t offset) {
240 // calculate the offsets within OatHeader to executable code
241 size_t old_offset = offset;
242 // required to be on a new page boundary
243 offset = RoundUp(offset, kPageSize);
244 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700245 size_executable_offset_alignment_ = offset - old_offset;
246 if (compiler_driver_->IsImage()) {
247 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700248
Ian Rogers848871b2013-08-05 10:56:33 -0700249 #define DO_TRAMPOLINE(field, fn_name) \
250 offset = CompiledCode::AlignCode(offset, instruction_set); \
251 oat_header_->Set ## fn_name ## Offset(offset); \
252 field.reset(compiler_driver_->Create ## fn_name()); \
253 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700254
Ian Rogers848871b2013-08-05 10:56:33 -0700255 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge);
256 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge);
257 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Jeff Hao88474b42013-10-23 16:24:40 -0700258 DO_TRAMPOLINE(portable_imt_conflict_trampoline_, PortableImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700259 DO_TRAMPOLINE(portable_resolution_trampoline_, PortableResolutionTrampoline);
260 DO_TRAMPOLINE(portable_to_interpreter_bridge_, PortableToInterpreterBridge);
Andreas Gampe2da88232014-02-27 12:26:20 -0800261 DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline);
Jeff Hao88474b42013-10-23 16:24:40 -0700262 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700263 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
264 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700265
Ian Rogers848871b2013-08-05 10:56:33 -0700266 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700267 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700268 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
269 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
270 oat_header_->SetJniDlsymLookupOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700271 oat_header_->SetPortableImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700272 oat_header_->SetPortableResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700273 oat_header_->SetPortableToInterpreterBridgeOffset(0);
Andreas Gampe2da88232014-02-27 12:26:20 -0800274 oat_header_->SetQuickGenericJniTrampolineOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700275 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700276 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700277 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700278 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700279 return offset;
280}
281
282size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700283 size_t oat_class_index = 0;
284 for (size_t i = 0; i != dex_files_->size(); ++i) {
285 const DexFile* dex_file = (*dex_files_)[i];
286 CHECK(dex_file != NULL);
Brian Carlstromba150c32013-08-27 17:31:03 -0700287 offset = InitOatCodeDexFile(offset, &oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700288 }
289 return offset;
290}
291
292size_t OatWriter::InitOatCodeDexFile(size_t offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700293 size_t* oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700294 const DexFile& dex_file) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800295 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700296 class_def_index < dex_file.NumClassDefs();
Brian Carlstromba150c32013-08-27 17:31:03 -0700297 class_def_index++, (*oat_class_index)++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700298 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstromba150c32013-08-27 17:31:03 -0700299 offset = InitOatCodeClassDef(offset, *oat_class_index, class_def_index, dex_file, class_def);
Ian Rogers3d504072014-03-01 09:16:49 -0800300 oat_classes_[*oat_class_index]->UpdateChecksum(oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700301 }
302 return offset;
303}
304
305size_t OatWriter::InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800306 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700307 const DexFile& dex_file,
308 const DexFile::ClassDef& class_def) {
309 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700310 if (class_data == NULL) {
311 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700312 return offset;
313 }
Ian Rogers0571d352011-11-03 19:51:38 -0700314 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstromba150c32013-08-27 17:31:03 -0700315 CHECK_LE(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700316 it.NumDirectMethods() + it.NumVirtualMethods());
317 // Skip fields
318 while (it.HasNextStaticField()) {
319 it.Next();
320 }
321 while (it.HasNextInstanceField()) {
322 it.Next();
323 }
324 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700325 size_t class_def_method_index = 0;
Brian Carlstromba150c32013-08-27 17:31:03 -0700326 size_t method_offsets_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700327 while (it.HasNextDirectMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800328 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700329 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700330 &method_offsets_index, is_native,
331 it.GetMethodInvokeType(class_def), it.GetMemberIndex(), dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700332 class_def_method_index++;
333 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700334 }
Ian Rogers0571d352011-11-03 19:51:38 -0700335 while (it.HasNextVirtualMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800336 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700337 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700338 &method_offsets_index, is_native,
339 it.GetMethodInvokeType(class_def), it.GetMemberIndex(), dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700340 class_def_method_index++;
341 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700342 }
Ian Rogers0571d352011-11-03 19:51:38 -0700343 DCHECK(!it.HasNext());
Brian Carlstromba150c32013-08-27 17:31:03 -0700344 CHECK_LE(method_offsets_index, class_def_method_index);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700345 return offset;
346}
347
Andreas Gampe08f068c2014-04-09 17:28:11 -0700348static void DCheckCodeAlignment(size_t offset, InstructionSet isa) {
349 switch (isa) {
350 case kArm:
351 // Fall-through.
352 case kThumb2:
353 DCHECK_ALIGNED(offset, kArmAlignment);
354 break;
355
356 case kArm64:
357 DCHECK_ALIGNED(offset, kArm64Alignment);
358 break;
359
360 case kMips:
361 DCHECK_ALIGNED(offset, kMipsAlignment);
362 break;
363
364 case kX86_64:
365 // Fall-through.
366 case kX86:
367 DCHECK_ALIGNED(offset, kX86Alignment);
368 break;
369
370 case kNone:
371 // Use a DCHECK instead of FATAL so that in the non-debug case the whole switch can
372 // be optimized away.
373 DCHECK(false);
374 break;
375 }
376}
377
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700378size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
379 size_t __attribute__((unused)) class_def_index,
380 size_t class_def_method_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700381 size_t* method_offsets_index,
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700382 bool __attribute__((unused)) is_native,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800383 InvokeType invoke_type,
Brian Carlstromba150c32013-08-27 17:31:03 -0700384 uint32_t method_idx, const DexFile& dex_file) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800385 // Derived from CompiledMethod if available.
386 uint32_t quick_code_offset = 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700387 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700388 uint32_t core_spill_mask = 0;
389 uint32_t fp_spill_mask = 0;
390 uint32_t mapping_table_offset = 0;
391 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800392 uint32_t gc_map_offset = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700393
Brian Carlstrom265091e2013-01-30 14:08:26 -0800394 OatClass* oat_class = oat_classes_[oat_class_index];
Brian Carlstromba150c32013-08-27 17:31:03 -0700395 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
Logan Chien971bf3f2012-05-01 15:47:55 +0800396
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700397 if (compiled_method != nullptr) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800398 const std::vector<uint8_t>* portable_code = compiled_method->GetPortableCode();
399 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode();
400 if (portable_code != nullptr) {
401 CHECK(quick_code == nullptr);
402 size_t oat_method_offsets_offset =
403 oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index);
404 compiled_method->AddOatdataOffsetToCompliledCodeOffset(
405 oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_));
jeffhao55d78212011-11-02 11:41:50 -0700406 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800407 CHECK(quick_code != nullptr);
408 offset = compiled_method->AlignCode(offset);
Andreas Gampe08f068c2014-04-09 17:28:11 -0700409 DCheckCodeAlignment(offset, compiled_method->GetInstructionSet());
410
Ian Rogersef7d42f2014-01-06 12:55:46 -0800411 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
412 CHECK_NE(code_size, 0U);
Dave Allisond6ed6422014-04-09 23:36:15 +0000413 uint32_t thumb_offset = compiled_method->CodeDelta();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800414 quick_code_offset = offset + sizeof(code_size) + thumb_offset;
415
Mark Mendellae9fd932014-02-10 16:14:35 -0800416 std::vector<uint8_t>* cfi_info = compiler_driver_->GetCallFrameInformation();
417 if (cfi_info != nullptr) {
Dave Allisond6ed6422014-04-09 23:36:15 +0000418 // Copy in the FDE, if present
419 const std::vector<uint8_t>* fde = compiled_method->GetCFIInfo();
Mark Mendellae9fd932014-02-10 16:14:35 -0800420 if (fde != nullptr) {
421 // Copy the information into cfi_info and then fix the address in the new copy.
422 int cur_offset = cfi_info->size();
423 cfi_info->insert(cfi_info->end(), fde->begin(), fde->end());
424
425 // Set the 'initial_location' field to address the start of the method.
426 uint32_t new_value = quick_code_offset - oat_header_->GetExecutableOffset();
427 uint32_t offset_to_update = cur_offset + 2*sizeof(uint32_t);
428 (*cfi_info)[offset_to_update+0] = new_value;
429 (*cfi_info)[offset_to_update+1] = new_value >> 8;
430 (*cfi_info)[offset_to_update+2] = new_value >> 16;
431 (*cfi_info)[offset_to_update+3] = new_value >> 24;
Mark Mendell1180d772014-03-21 08:00:18 -0700432 method_info_.push_back(DebugInfo(PrettyMethod(method_idx, dex_file, false),
Mark Mendellae9fd932014-02-10 16:14:35 -0800433 new_value, new_value + code_size));
434 }
435 }
436
Ian Rogersef7d42f2014-01-06 12:55:46 -0800437 // Deduplicate code arrays
438 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter =
439 code_offsets_.find(quick_code);
440 if (code_iter != code_offsets_.end()) {
441 quick_code_offset = code_iter->second;
442 } else {
443 code_offsets_.Put(quick_code, quick_code_offset);
444 offset += sizeof(code_size); // code size is prepended before code
445 offset += code_size;
446 oat_header_->UpdateChecksum(&(*quick_code)[0], code_size);
447 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800448 }
449 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
450 core_spill_mask = compiled_method->GetCoreSpillMask();
451 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700452
Ian Rogers1809a722013-08-09 22:05:32 -0700453 const std::vector<uint8_t>& mapping_table = compiled_method->GetMappingTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800454 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
455 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700456
Logan Chien971bf3f2012-05-01 15:47:55 +0800457 // Deduplicate mapping tables
Ian Rogers1809a722013-08-09 22:05:32 -0700458 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator mapping_iter =
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700459 mapping_table_offsets_.find(&mapping_table);
Logan Chien971bf3f2012-05-01 15:47:55 +0800460 if (mapping_iter != mapping_table_offsets_.end()) {
461 mapping_table_offset = mapping_iter->second;
462 } else {
463 mapping_table_offsets_.Put(&mapping_table, mapping_table_offset);
464 offset += mapping_table_size;
465 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
466 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700467
Ian Rogers1809a722013-08-09 22:05:32 -0700468 const std::vector<uint8_t>& vmap_table = compiled_method->GetVmapTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800469 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
470 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700471
Logan Chien971bf3f2012-05-01 15:47:55 +0800472 // Deduplicate vmap tables
Ian Rogers1809a722013-08-09 22:05:32 -0700473 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator vmap_iter =
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700474 vmap_table_offsets_.find(&vmap_table);
Logan Chien971bf3f2012-05-01 15:47:55 +0800475 if (vmap_iter != vmap_table_offsets_.end()) {
476 vmap_table_offset = vmap_iter->second;
477 } else {
478 vmap_table_offsets_.Put(&vmap_table, vmap_table_offset);
479 offset += vmap_table_size;
480 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
481 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800482
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700483 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800484 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
485 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
Logan Chienccb7bf12012-03-28 12:52:32 +0800486
Mathieu Chartier590fee92013-09-13 13:46:47 -0700487 if (kIsDebugBuild) {
488 // We expect GC maps except when the class hasn't been verified or the method is native
489 ClassReference class_ref(&dex_file, class_def_index);
490 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
491 mirror::Class::Status status;
492 if (compiled_class != NULL) {
493 status = compiled_class->GetStatus();
Vladimir Markoc7f83202014-01-24 17:55:18 +0000494 } else if (compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700495 status = mirror::Class::kStatusError;
496 } else {
497 status = mirror::Class::kStatusNotReady;
498 }
499 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
500 << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
501 << (status < mirror::Class::kStatusVerified) << " " << status << " "
502 << PrettyMethod(method_idx, dex_file);
jeffhaoec014232012-09-05 10:42:25 -0700503 }
Ian Rogersc20a83e2012-01-18 18:15:32 -0800504
Logan Chien971bf3f2012-05-01 15:47:55 +0800505 // Deduplicate GC maps
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700506 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
507 gc_map_offsets_.find(&gc_map);
Logan Chien971bf3f2012-05-01 15:47:55 +0800508 if (gc_map_iter != gc_map_offsets_.end()) {
509 gc_map_offset = gc_map_iter->second;
510 } else {
511 gc_map_offsets_.Put(&gc_map, gc_map_offset);
512 offset += gc_map_size;
513 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800514 }
Brian Carlstromba150c32013-08-27 17:31:03 -0700515
516 oat_class->method_offsets_[*method_offsets_index] =
Ian Rogersef7d42f2014-01-06 12:55:46 -0800517 OatMethodOffsets(quick_code_offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700518 frame_size_in_bytes,
519 core_spill_mask,
520 fp_spill_mask,
521 mapping_table_offset,
522 vmap_table_offset,
523 gc_map_offset);
524 (*method_offsets_index)++;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700525 }
526
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700527
Ian Rogers1212a022013-03-04 10:48:41 -0800528 if (compiler_driver_->IsImage()) {
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700529 // Derive frame size and spill masks for native methods without code:
530 // These are generic JNI methods...
531 if (is_native && compiled_method == nullptr) {
532 // Compute Sirt size as putting _every_ reference into it, even null ones.
533 uint32_t s_len;
534 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx), &s_len);
535 DCHECK(shorty != nullptr);
536 uint32_t refs = 1; // Native method always has "this" or class.
537 for (uint32_t i = 1; i < s_len; ++i) {
538 if (shorty[i] == 'L') {
539 refs++;
540 }
541 }
Andreas Gampe242947d2014-04-03 13:31:32 -0700542 InstructionSet trg_isa = compiler_driver_->GetInstructionSet();
543 size_t pointer_size = 4;
544 if (trg_isa == kArm64 || trg_isa == kX86_64) {
545 pointer_size = 8;
546 }
547 size_t sirt_size = StackIndirectReferenceTable::GetAlignedSirtSizeTarget(pointer_size, refs);
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700548
549 // Get the generic spill masks and base frame size.
550 mirror::ArtMethod* callee_save_method =
551 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs);
552
553 frame_size_in_bytes = callee_save_method->GetFrameSizeInBytes() + sirt_size;
554 core_spill_mask = callee_save_method->GetCoreSpillMask();
555 fp_spill_mask = callee_save_method->GetFpSpillMask();
556 mapping_table_offset = 0;
557 vmap_table_offset = 0;
558 gc_map_offset = 0;
559 }
560
Ian Rogers0571d352011-11-03 19:51:38 -0700561 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700562 // Unchecked as we hold mutator_lock_ on entry.
563 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700564 SirtRef<mirror::DexCache> dex_cache(soa.Self(), linker->FindDexCache(dex_file));
565 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700566 mirror::ArtMethod* method = linker->ResolveMethod(dex_file, method_idx, dex_cache,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700567 class_loader, nullptr, invoke_type);
Ian Rogers0571d352011-11-03 19:51:38 -0700568 CHECK(method != NULL);
569 method->SetFrameSizeInBytes(frame_size_in_bytes);
570 method->SetCoreSpillMask(core_spill_mask);
571 method->SetFpSpillMask(fp_spill_mask);
572 method->SetOatMappingTableOffset(mapping_table_offset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800573 // Portable code offsets are set by ElfWriterMclinker::FixupCompiledCodeOffset after linking.
574 method->SetQuickOatCodeOffset(quick_code_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700575 method->SetOatVmapTableOffset(vmap_table_offset);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700576 method->SetOatNativeGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700577 }
Logan Chien8b977d32012-02-21 19:14:55 +0800578
Brian Carlstrome24fa612011-09-29 00:53:55 -0700579 return offset;
580}
581
Brian Carlstrom265091e2013-01-30 14:08:26 -0800582#define DCHECK_OFFSET() \
Ian Rogers3d504072014-03-01 09:16:49 -0800583 DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out->Seek(0, kSeekCurrent)) \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700584 << "file_offset=" << file_offset << " relative_offset=" << relative_offset
Brian Carlstrom265091e2013-01-30 14:08:26 -0800585
586#define DCHECK_OFFSET_() \
Ian Rogers3d504072014-03-01 09:16:49 -0800587 DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out->Seek(0, kSeekCurrent)) \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700588 << "file_offset=" << file_offset << " offset_=" << offset_
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700589
Ian Rogers3d504072014-03-01 09:16:49 -0800590bool OatWriter::Write(OutputStream* out) {
591 const size_t file_offset = out->Seek(0, kSeekCurrent);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700592
Ian Rogers3d504072014-03-01 09:16:49 -0800593 if (!out->WriteFully(oat_header_, sizeof(*oat_header_))) {
594 PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700595 return false;
596 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700597 size_oat_header_ += sizeof(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700598
Ian Rogers3d504072014-03-01 09:16:49 -0800599 if (!out->WriteFully(image_file_location_.data(), image_file_location_.size())) {
600 PLOG(ERROR) << "Failed to write oat header image file location to " << out->GetLocation();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700601 return false;
602 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700603 size_oat_header_image_file_location_ += image_file_location_.size();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700604
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700605 if (!WriteTables(out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800606 LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700607 return false;
608 }
609
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700610 size_t relative_offset = WriteCode(out, file_offset);
611 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800612 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700613 return false;
614 }
615
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700616 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
617 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800618 LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700619 return false;
620 }
621
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700622 if (kIsDebugBuild) {
623 uint32_t size_total = 0;
624 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700625 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700626 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700627
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700628 DO_STAT(size_dex_file_alignment_);
629 DO_STAT(size_executable_offset_alignment_);
630 DO_STAT(size_oat_header_);
631 DO_STAT(size_oat_header_image_file_location_);
632 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -0700633 DO_STAT(size_interpreter_to_interpreter_bridge_);
634 DO_STAT(size_interpreter_to_compiled_code_bridge_);
635 DO_STAT(size_jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -0700636 DO_STAT(size_portable_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700637 DO_STAT(size_portable_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700638 DO_STAT(size_portable_to_interpreter_bridge_);
Andreas Gampe2da88232014-02-27 12:26:20 -0800639 DO_STAT(size_quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -0700640 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700641 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700642 DO_STAT(size_quick_to_interpreter_bridge_);
643 DO_STAT(size_trampoline_alignment_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700644 DO_STAT(size_code_size_);
645 DO_STAT(size_code_);
646 DO_STAT(size_code_alignment_);
647 DO_STAT(size_mapping_table_);
648 DO_STAT(size_vmap_table_);
649 DO_STAT(size_gc_map_);
650 DO_STAT(size_oat_dex_file_location_size_);
651 DO_STAT(size_oat_dex_file_location_data_);
652 DO_STAT(size_oat_dex_file_location_checksum_);
653 DO_STAT(size_oat_dex_file_offset_);
654 DO_STAT(size_oat_dex_file_methods_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700655 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700656 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700657 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700658 DO_STAT(size_oat_class_method_offsets_);
659 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700660
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700661 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Ian Rogers3d504072014-03-01 09:16:49 -0800662 CHECK_EQ(file_offset + size_total, static_cast<uint32_t>(out->Seek(0, kSeekCurrent)));
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700663 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700664 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700665
Ian Rogers3d504072014-03-01 09:16:49 -0800666 CHECK_EQ(file_offset + size_, static_cast<uint32_t>(out->Seek(0, kSeekCurrent)));
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700667 CHECK_EQ(size_, relative_offset);
668
Brian Carlstrome24fa612011-09-29 00:53:55 -0700669 return true;
670}
671
Ian Rogers3d504072014-03-01 09:16:49 -0800672bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700673 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700674 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800675 PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700676 return false;
677 }
678 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800679 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700680 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Ian Rogers3d504072014-03-01 09:16:49 -0800681 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -0800682 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
683 const DexFile* dex_file = (*dex_files_)[i];
684 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
685 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
686 return false;
687 }
688 const DexFile* dex_file = (*dex_files_)[i];
Ian Rogers3d504072014-03-01 09:16:49 -0800689 if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700690 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
Ian Rogers3d504072014-03-01 09:16:49 -0800691 << " to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800692 return false;
693 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700694 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800695 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800696 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700697 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800698 PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700699 return false;
700 }
701 }
702 return true;
703}
704
Ian Rogers3d504072014-03-01 09:16:49 -0800705size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700706 size_t relative_offset = oat_header_->GetExecutableOffset();
Ian Rogers3d504072014-03-01 09:16:49 -0800707 off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700708 size_t expected_file_offset = file_offset + relative_offset;
709 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700710 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Ian Rogers3d504072014-03-01 09:16:49 -0800711 << " Expected: " << expected_file_offset << " File: " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700712 return 0;
713 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800714 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700715 if (compiler_driver_->IsImage()) {
716 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700717
Ian Rogers848871b2013-08-05 10:56:33 -0700718 #define DO_TRAMPOLINE(field) \
719 do { \
720 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
721 uint32_t alignment_padding = aligned_offset - relative_offset; \
Ian Rogers3d504072014-03-01 09:16:49 -0800722 out->Seek(alignment_padding, kSeekCurrent); \
Ian Rogers848871b2013-08-05 10:56:33 -0700723 size_trampoline_alignment_ += alignment_padding; \
Ian Rogers3d504072014-03-01 09:16:49 -0800724 if (!out->WriteFully(&(*field)[0], field->size())) { \
725 PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \
Ian Rogers848871b2013-08-05 10:56:33 -0700726 return false; \
727 } \
728 size_ ## field += field->size(); \
729 relative_offset += alignment_padding + field->size(); \
730 DCHECK_OFFSET(); \
731 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700732
Ian Rogers848871b2013-08-05 10:56:33 -0700733 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_);
734 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_);
735 DO_TRAMPOLINE(jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -0700736 DO_TRAMPOLINE(portable_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700737 DO_TRAMPOLINE(portable_resolution_trampoline_);
738 DO_TRAMPOLINE(portable_to_interpreter_bridge_);
Andreas Gampe2da88232014-02-27 12:26:20 -0800739 DO_TRAMPOLINE(quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -0700740 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700741 DO_TRAMPOLINE(quick_resolution_trampoline_);
742 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
743 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700744 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700745 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700746}
747
Ian Rogers3d504072014-03-01 09:16:49 -0800748size_t OatWriter::WriteCodeDexFiles(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700749 const size_t file_offset,
750 size_t relative_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700751 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800752 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700753 const DexFile* dex_file = (*dex_files_)[i];
754 CHECK(dex_file != NULL);
Brian Carlstromba150c32013-08-27 17:31:03 -0700755 relative_offset = WriteCodeDexFile(out, file_offset, relative_offset, &oat_class_index,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700756 *dex_file);
757 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700758 return 0;
759 }
760 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700761 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700762}
763
Ian Rogers3d504072014-03-01 09:16:49 -0800764size_t OatWriter::WriteCodeDexFile(OutputStream* out, const size_t file_offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700765 size_t relative_offset, size_t* oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700766 const DexFile& dex_file) {
767 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
Brian Carlstromba150c32013-08-27 17:31:03 -0700768 class_def_index++, (*oat_class_index)++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700769 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstromba150c32013-08-27 17:31:03 -0700770 relative_offset = WriteCodeClassDef(out, file_offset, relative_offset, *oat_class_index,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700771 dex_file, class_def);
772 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700773 return 0;
774 }
775 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700776 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700777}
778
Ian Rogers0571d352011-11-03 19:51:38 -0700779void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
Ian Rogers3d504072014-03-01 09:16:49 -0800780 const DexFile& dex_file, const OutputStream& out) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700781 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800782 << " to " << out.GetLocation();
Elliott Hughes234da572011-11-03 22:13:06 -0700783}
784
Ian Rogers3d504072014-03-01 09:16:49 -0800785size_t OatWriter::WriteCodeClassDef(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700786 const size_t file_offset,
787 size_t relative_offset,
788 size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700789 const DexFile& dex_file,
790 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700791 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700792 if (class_data == NULL) {
793 // ie. an empty class such as a marker interface
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700794 return relative_offset;
Ian Rogers387b6992011-10-31 17:52:37 -0700795 }
Ian Rogers0571d352011-11-03 19:51:38 -0700796 ClassDataItemIterator it(dex_file, class_data);
797 // Skip fields
798 while (it.HasNextStaticField()) {
799 it.Next();
800 }
801 while (it.HasNextInstanceField()) {
802 it.Next();
803 }
804 // Process methods
805 size_t class_def_method_index = 0;
Brian Carlstromba150c32013-08-27 17:31:03 -0700806 size_t method_offsets_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700807 while (it.HasNextDirectMethod()) {
808 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700809 relative_offset = WriteCodeMethod(out, file_offset, relative_offset, oat_class_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700810 class_def_method_index, &method_offsets_index, is_static,
811 it.GetMemberIndex(), dex_file);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700812 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700813 return 0;
814 }
Ian Rogers0571d352011-11-03 19:51:38 -0700815 class_def_method_index++;
816 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700817 }
Ian Rogers0571d352011-11-03 19:51:38 -0700818 while (it.HasNextVirtualMethod()) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700819 relative_offset = WriteCodeMethod(out, file_offset, relative_offset, oat_class_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700820 class_def_method_index, &method_offsets_index, false,
821 it.GetMemberIndex(), dex_file);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700822 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700823 return 0;
824 }
Ian Rogers0571d352011-11-03 19:51:38 -0700825 class_def_method_index++;
826 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700827 }
Brian Carlstromba150c32013-08-27 17:31:03 -0700828 DCHECK(!it.HasNext());
829 CHECK_LE(method_offsets_index, class_def_method_index);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700830 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700831}
832
Ian Rogers3d504072014-03-01 09:16:49 -0800833size_t OatWriter::WriteCodeMethod(OutputStream* out, const size_t file_offset,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700834 size_t relative_offset, size_t oat_class_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700835 size_t class_def_method_index, size_t* method_offsets_index,
836 bool is_static, uint32_t method_idx, const DexFile& dex_file) {
837 OatClass* oat_class = oat_classes_[oat_class_index];
838 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700839
840 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstromba150c32013-08-27 17:31:03 -0700841 const OatMethodOffsets method_offsets = oat_class->method_offsets_[*method_offsets_index];
842 (*method_offsets_index)++;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800843 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode();
844 if (quick_code != nullptr) {
845 CHECK(compiled_method->GetPortableCode() == nullptr);
846 uint32_t aligned_offset = compiled_method->AlignCode(relative_offset);
847 uint32_t aligned_code_delta = aligned_offset - relative_offset;
848 if (aligned_code_delta != 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800849 off_t new_offset = out->Seek(aligned_code_delta, kSeekCurrent);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800850 size_code_alignment_ += aligned_code_delta;
851 uint32_t expected_offset = file_offset + aligned_offset;
852 if (static_cast<uint32_t>(new_offset) != expected_offset) {
853 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Ian Rogers3d504072014-03-01 09:16:49 -0800854 << " Expected: " << expected_offset << " File: " << out->GetLocation();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800855 return 0;
856 }
857 relative_offset += aligned_code_delta;
858 DCHECK_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700859 }
Andreas Gampe08f068c2014-04-09 17:28:11 -0700860 DCheckCodeAlignment(relative_offset, compiled_method->GetInstructionSet());
861
Ian Rogersef7d42f2014-01-06 12:55:46 -0800862 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
863 CHECK_NE(code_size, 0U);
864
865 // Deduplicate code arrays
866 size_t code_offset = relative_offset + sizeof(code_size) + compiled_method->CodeDelta();
867 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter =
868 code_offsets_.find(quick_code);
869 if (code_iter != code_offsets_.end() && code_offset != method_offsets.code_offset_) {
870 DCHECK(code_iter->second == method_offsets.code_offset_)
871 << PrettyMethod(method_idx, dex_file);
872 } else {
873 DCHECK(code_offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
Ian Rogers3d504072014-03-01 09:16:49 -0800874 if (!out->WriteFully(&code_size, sizeof(code_size))) {
875 ReportWriteFailure("method code size", method_idx, dex_file, *out);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800876 return 0;
877 }
878 size_code_size_ += sizeof(code_size);
879 relative_offset += sizeof(code_size);
880 DCHECK_OFFSET();
Ian Rogers3d504072014-03-01 09:16:49 -0800881 if (!out->WriteFully(&(*quick_code)[0], code_size)) {
882 ReportWriteFailure("method code", method_idx, dex_file, *out);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800883 return 0;
884 }
885 size_code_ += code_size;
886 relative_offset += code_size;
887 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800888 DCHECK_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700889 }
Ian Rogers1809a722013-08-09 22:05:32 -0700890 const std::vector<uint8_t>& mapping_table = compiled_method->GetMappingTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800891 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
892
893 // Deduplicate mapping tables
Ian Rogers1809a722013-08-09 22:05:32 -0700894 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator mapping_iter =
Logan Chien971bf3f2012-05-01 15:47:55 +0800895 mapping_table_offsets_.find(&mapping_table);
896 if (mapping_iter != mapping_table_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700897 relative_offset != method_offsets.mapping_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800898 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
899 || mapping_iter->second == method_offsets.mapping_table_offset_)
900 << PrettyMethod(method_idx, dex_file);
901 } else {
902 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700903 || relative_offset == method_offsets.mapping_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800904 << PrettyMethod(method_idx, dex_file);
Ian Rogers3d504072014-03-01 09:16:49 -0800905 if (!out->WriteFully(&mapping_table[0], mapping_table_size)) {
906 ReportWriteFailure("mapping table", method_idx, dex_file, *out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800907 return 0;
908 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700909 size_mapping_table_ += mapping_table_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700910 relative_offset += mapping_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800911 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800912 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800913
Ian Rogers1809a722013-08-09 22:05:32 -0700914 const std::vector<uint8_t>& vmap_table = compiled_method->GetVmapTable();
Logan Chien971bf3f2012-05-01 15:47:55 +0800915 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
916
917 // Deduplicate vmap tables
Ian Rogers1809a722013-08-09 22:05:32 -0700918 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator vmap_iter =
Logan Chien971bf3f2012-05-01 15:47:55 +0800919 vmap_table_offsets_.find(&vmap_table);
920 if (vmap_iter != vmap_table_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700921 relative_offset != method_offsets.vmap_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800922 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
923 || vmap_iter->second == method_offsets.vmap_table_offset_)
924 << PrettyMethod(method_idx, dex_file);
925 } else {
926 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700927 || relative_offset == method_offsets.vmap_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800928 << PrettyMethod(method_idx, dex_file);
Ian Rogers3d504072014-03-01 09:16:49 -0800929 if (!out->WriteFully(&vmap_table[0], vmap_table_size)) {
930 ReportWriteFailure("vmap table", method_idx, dex_file, *out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800931 return 0;
932 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700933 size_vmap_table_ += vmap_table_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700934 relative_offset += vmap_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800935 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800936 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800937
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700938 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800939 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
940
941 // Deduplicate GC maps
942 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
943 gc_map_offsets_.find(&gc_map);
944 if (gc_map_iter != gc_map_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700945 relative_offset != method_offsets.gc_map_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800946 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
947 || gc_map_iter->second == method_offsets.gc_map_offset_)
948 << PrettyMethod(method_idx, dex_file);
949 } else {
950 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700951 || relative_offset == method_offsets.gc_map_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800952 << PrettyMethod(method_idx, dex_file);
Ian Rogers3d504072014-03-01 09:16:49 -0800953 if (!out->WriteFully(&gc_map[0], gc_map_size)) {
954 ReportWriteFailure("GC map", method_idx, dex_file, *out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800955 return 0;
956 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700957 size_gc_map_ += gc_map_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700958 relative_offset += gc_map_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800959 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800960 DCHECK_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700961 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800962
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700963 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700964}
965
Brian Carlstrom265091e2013-01-30 14:08:26 -0800966OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
967 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -0800968 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700969 dex_file_location_size_ = location.size();
970 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800971 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800972 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800973 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700974}
975
976size_t OatWriter::OatDexFile::SizeOf() const {
977 return sizeof(dex_file_location_size_)
978 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800979 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800980 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800981 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700982}
983
Ian Rogers3d504072014-03-01 09:16:49 -0800984void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const {
985 oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
986 oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
987 oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
988 oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
989 oat_header->UpdateChecksum(&methods_offsets_[0],
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800990 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700991}
992
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700993bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -0800994 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700995 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800996 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -0800997 if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
998 PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700999 return false;
1000 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001001 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001002 if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
1003 PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001004 return false;
1005 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001006 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Ian Rogers3d504072014-03-01 09:16:49 -08001007 if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
1008 PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001009 return false;
1010 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001011 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Ian Rogers3d504072014-03-01 09:16:49 -08001012 if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
1013 PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001014 return false;
1015 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001016 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Ian Rogers3d504072014-03-01 09:16:49 -08001017 if (!out->WriteFully(&methods_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001018 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001019 PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001020 return false;
1021 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001022 oat_writer->size_oat_dex_file_methods_offsets_ +=
1023 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001024 return true;
1025}
1026
Brian Carlstromba150c32013-08-27 17:31:03 -07001027OatWriter::OatClass::OatClass(size_t offset,
1028 std::vector<CompiledMethod*>* compiled_methods,
1029 uint32_t num_non_null_compiled_methods,
1030 mirror::Class::Status status) {
1031 CHECK(compiled_methods != NULL);
1032 uint32_t num_methods = compiled_methods->size();
1033 CHECK_LE(num_non_null_compiled_methods, num_methods);
1034
Brian Carlstrom265091e2013-01-30 14:08:26 -08001035 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -07001036 compiled_methods_ = compiled_methods;
1037 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
1038
1039 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
1040 // apply when there are 0 methods, we just arbitrarily say that 0
1041 // methods means kOatClassNoneCompiled and that we won't use
1042 // kOatClassAllCompiled unless there is at least one compiled
1043 // method. This means in an interpretter only system, we can assert
1044 // that all classes are kOatClassNoneCompiled.
1045 if (num_non_null_compiled_methods == 0) {
1046 type_ = kOatClassNoneCompiled;
1047 } else if (num_non_null_compiled_methods == num_methods) {
1048 type_ = kOatClassAllCompiled;
1049 } else {
1050 type_ = kOatClassSomeCompiled;
1051 }
1052
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001053 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -07001054 method_offsets_.resize(num_non_null_compiled_methods);
1055
1056 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
1057 if (type_ == kOatClassSomeCompiled) {
1058 method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator());
1059 method_bitmap_size_ = method_bitmap_->GetSizeOf();
1060 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
1061 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
1062 } else {
1063 method_bitmap_ = NULL;
1064 method_bitmap_size_ = 0;
1065 }
1066
1067 for (size_t i = 0; i < num_methods; i++) {
1068 CompiledMethod* compiled_method = (*compiled_methods_)[i];
1069 if (compiled_method == NULL) {
1070 oat_method_offsets_offsets_from_oat_class_[i] = 0;
1071 } else {
1072 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
1073 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
1074 if (type_ == kOatClassSomeCompiled) {
1075 method_bitmap_->SetBit(i);
1076 }
1077 }
1078 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001079}
1080
Brian Carlstromba150c32013-08-27 17:31:03 -07001081OatWriter::OatClass::~OatClass() {
Mathieu Chartier661974a2014-01-09 11:23:53 -08001082 delete method_bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -07001083 delete compiled_methods_;
1084}
1085
Brian Carlstrom265091e2013-01-30 14:08:26 -08001086size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
1087 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001088 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
1089 if (method_offset == 0) {
1090 return 0;
1091 }
1092 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001093}
1094
1095size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
1096 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001097 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08001098}
1099
1100size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001101 return sizeof(status_)
1102 + sizeof(type_)
1103 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
1104 + method_bitmap_size_
1105 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001106}
1107
Ian Rogers3d504072014-03-01 09:16:49 -08001108void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const {
1109 oat_header->UpdateChecksum(&status_, sizeof(status_));
1110 oat_header->UpdateChecksum(&type_, sizeof(type_));
Brian Carlstromba150c32013-08-27 17:31:03 -07001111 if (method_bitmap_size_ != 0) {
1112 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001113 oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_));
1114 oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001115 }
Ian Rogers3d504072014-03-01 09:16:49 -08001116 oat_header->UpdateChecksum(&method_offsets_[0],
1117 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001118}
1119
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001120bool OatWriter::OatClass::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001121 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001122 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001123 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001124 if (!out->WriteFully(&status_, sizeof(status_))) {
1125 PLOG(ERROR) << "Failed to write class status to " << out->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001126 return false;
1127 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001128 oat_writer->size_oat_class_status_ += sizeof(status_);
Ian Rogers3d504072014-03-01 09:16:49 -08001129 if (!out->WriteFully(&type_, sizeof(type_))) {
1130 PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001131 return false;
1132 }
1133 oat_writer->size_oat_class_type_ += sizeof(type_);
1134 if (method_bitmap_size_ != 0) {
1135 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001136 if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
1137 PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001138 return false;
1139 }
1140 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001141 if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
1142 PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001143 return false;
1144 }
1145 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1146 }
Ian Rogers3d504072014-03-01 09:16:49 -08001147 if (!out->WriteFully(&method_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001148 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001149 PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001150 return false;
1151 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001152 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001153 return true;
1154}
1155
Brian Carlstrome24fa612011-09-29 00:53:55 -07001156} // namespace art