blob: 21c5317b69b212817bd4244acd9203d22fb3df3a [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
Elliott Hughes1aa246d2012-12-13 09:29:36 -080021#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080022#include "base/unix_file/fd_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070023#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070024#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070025#include "gc/space/space.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "mirror/abstract_method-inl.h"
27#include "mirror/array.h"
28#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070030#include "os.h"
Brian Carlstromcd60ac72013-01-20 17:09:51 -080031#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070032#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070033#include "scoped_thread_state_change.h"
jeffhaoec014232012-09-05 10:42:25 -070034#include "verifier/method_verifier.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070035
36namespace art {
37
Brian Carlstrom3320cf42011-10-04 14:58:28 -070038OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070039 uint32_t image_file_location_oat_checksum,
40 uint32_t image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070041 const std::string& image_file_location,
Ian Rogers1212a022013-03-04 10:48:41 -080042 const CompilerDriver* compiler)
Jeff Hao0aba0ba2013-06-03 14:49:28 -070043 : compiler_driver_(compiler),
44 dex_files_(&dex_files),
45 image_file_location_oat_checksum_(image_file_location_oat_checksum),
46 image_file_location_oat_begin_(image_file_location_oat_begin),
47 image_file_location_(image_file_location),
48 oat_header_(NULL),
49 size_dex_file_alignment_(0),
50 size_executable_offset_alignment_(0),
51 size_oat_header_(0),
52 size_oat_header_image_file_location_(0),
53 size_dex_file_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070054 size_interpreter_to_interpreter_bridge_(0),
55 size_interpreter_to_compiled_code_bridge_(0),
56 size_jni_dlsym_lookup_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070057 size_portable_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070058 size_portable_to_interpreter_bridge_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070059 size_quick_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070060 size_quick_to_interpreter_bridge_(0),
61 size_trampoline_alignment_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070062 size_code_size_(0),
63 size_code_(0),
64 size_code_alignment_(0),
65 size_mapping_table_(0),
66 size_vmap_table_(0),
67 size_gc_map_(0),
68 size_oat_dex_file_location_size_(0),
69 size_oat_dex_file_location_data_(0),
70 size_oat_dex_file_location_checksum_(0),
71 size_oat_dex_file_offset_(0),
72 size_oat_dex_file_methods_offsets_(0),
73 size_oat_class_status_(0),
74 size_oat_class_method_offsets_(0) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070075 size_t offset = InitOatHeader();
Brian Carlstrome24fa612011-09-29 00:53:55 -070076 offset = InitOatDexFiles(offset);
Brian Carlstrom89521892011-12-07 22:05:07 -080077 offset = InitDexFiles(offset);
Brian Carlstrom389efb02012-01-11 12:06:26 -080078 offset = InitOatClasses(offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -070079 offset = InitOatCode(offset);
80 offset = InitOatCodeDexFiles(offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -070081 size_ = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -070082
83 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Jeff Hao0aba0ba2013-06-03 14:49:28 -070084 CHECK(image_file_location.empty() == compiler->IsImage());
Brian Carlstrome24fa612011-09-29 00:53:55 -070085}
86
Ian Rogers0571d352011-11-03 19:51:38 -070087OatWriter::~OatWriter() {
88 delete oat_header_;
89 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -080090 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -070091}
92
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070093size_t OatWriter::InitOatHeader() {
Brian Carlstrome24fa612011-09-29 00:53:55 -070094 // create the OatHeader
Ian Rogers1212a022013-03-04 10:48:41 -080095 oat_header_ = new OatHeader(compiler_driver_->GetInstructionSet(),
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070096 dex_files_,
Brian Carlstrom28db0122012-10-18 16:20:41 -070097 image_file_location_oat_checksum_,
98 image_file_location_oat_begin_,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070099 image_file_location_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700100 size_t offset = sizeof(*oat_header_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700101 offset += image_file_location_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700102 return offset;
103}
104
105size_t OatWriter::InitOatDexFiles(size_t offset) {
106 // create the OatDexFiles
107 for (size_t i = 0; i != dex_files_->size(); ++i) {
108 const DexFile* dex_file = (*dex_files_)[i];
109 CHECK(dex_file != NULL);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800110 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700111 oat_dex_files_.push_back(oat_dex_file);
112 offset += oat_dex_file->SizeOf();
113 }
114 return offset;
115}
116
Brian Carlstrom89521892011-12-07 22:05:07 -0800117size_t OatWriter::InitDexFiles(size_t offset) {
118 // calculate the offsets within OatDexFiles to the DexFiles
119 for (size_t i = 0; i != dex_files_->size(); ++i) {
120 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700121 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800122 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700123 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800124
125 // set offset in OatDexFile to DexFile
126 oat_dex_files_[i]->dex_file_offset_ = offset;
127
128 const DexFile* dex_file = (*dex_files_)[i];
129 offset += dex_file->GetHeader().file_size_;
130 }
131 return offset;
132}
133
Brian Carlstrom389efb02012-01-11 12:06:26 -0800134size_t OatWriter::InitOatClasses(size_t offset) {
135 // create the OatClasses
136 // calculate the offsets within OatDexFiles to OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -0700137 for (size_t i = 0; i != dex_files_->size(); ++i) {
138 const DexFile* dex_file = (*dex_files_)[i];
139 for (size_t class_def_index = 0;
140 class_def_index < dex_file->NumClassDefs();
Ian Rogersc20a83e2012-01-18 18:15:32 -0800141 class_def_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800142 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700143 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
144 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700145 uint32_t num_methods = 0;
146 if (class_data != NULL) { // ie not an empty class, such as a marker interface
147 ClassDataItemIterator it(*dex_file, class_data);
148 size_t num_direct_methods = it.NumDirectMethods();
149 size_t num_virtual_methods = it.NumVirtualMethods();
150 num_methods = num_direct_methods + num_virtual_methods;
151 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800152
Brian Carlstrom51c24672013-07-11 16:00:56 -0700153 ClassReference class_ref(dex_file, class_def_index);
Ian Rogers1212a022013-03-04 10:48:41 -0800154 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155 mirror::Class::Status status;
jeffhaoec014232012-09-05 10:42:25 -0700156 if (compiled_class != NULL) {
157 status = compiled_class->GetStatus();
158 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800159 status = mirror::Class::kStatusError;
jeffhaoec014232012-09-05 10:42:25 -0700160 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161 status = mirror::Class::kStatusNotReady;
jeffhaoec014232012-09-05 10:42:25 -0700162 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800163
Brian Carlstrom265091e2013-01-30 14:08:26 -0800164 OatClass* oat_class = new OatClass(offset, status, num_methods);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800165 oat_classes_.push_back(oat_class);
166 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700167 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800168 oat_dex_files_[i]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700169 }
170 return offset;
171}
172
173size_t OatWriter::InitOatCode(size_t offset) {
174 // calculate the offsets within OatHeader to executable code
175 size_t old_offset = offset;
176 // required to be on a new page boundary
177 offset = RoundUp(offset, kPageSize);
178 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700179 size_executable_offset_alignment_ = offset - old_offset;
180 if (compiler_driver_->IsImage()) {
181 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700182
Ian Rogers848871b2013-08-05 10:56:33 -0700183 #define DO_TRAMPOLINE(field, fn_name) \
184 offset = CompiledCode::AlignCode(offset, instruction_set); \
185 oat_header_->Set ## fn_name ## Offset(offset); \
186 field.reset(compiler_driver_->Create ## fn_name()); \
187 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700188
Ian Rogers848871b2013-08-05 10:56:33 -0700189 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge);
190 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge);
191 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
192 DO_TRAMPOLINE(portable_resolution_trampoline_, PortableResolutionTrampoline);
193 DO_TRAMPOLINE(portable_to_interpreter_bridge_, PortableToInterpreterBridge);
194 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
195 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700196
Ian Rogers848871b2013-08-05 10:56:33 -0700197 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700198 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700199 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
200 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
201 oat_header_->SetJniDlsymLookupOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700202 oat_header_->SetPortableResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700203 oat_header_->SetPortableToInterpreterBridgeOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700204 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700205 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700206 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700207 return offset;
208}
209
210size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700211 size_t oat_class_index = 0;
212 for (size_t i = 0; i != dex_files_->size(); ++i) {
213 const DexFile* dex_file = (*dex_files_)[i];
214 CHECK(dex_file != NULL);
215 offset = InitOatCodeDexFile(offset, oat_class_index, *dex_file);
216 }
217 return offset;
218}
219
220size_t OatWriter::InitOatCodeDexFile(size_t offset,
221 size_t& oat_class_index,
222 const DexFile& dex_file) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800223 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700224 class_def_index < dex_file.NumClassDefs();
225 class_def_index++, oat_class_index++) {
226 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800227 offset = InitOatCodeClassDef(offset, oat_class_index, class_def_index, dex_file, class_def);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800228 oat_classes_[oat_class_index]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700229 }
230 return offset;
231}
232
233size_t OatWriter::InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800234 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700235 const DexFile& dex_file,
236 const DexFile::ClassDef& class_def) {
237 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700238 if (class_data == NULL) {
239 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700240 return offset;
241 }
Ian Rogers0571d352011-11-03 19:51:38 -0700242 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800243 CHECK_EQ(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700244 it.NumDirectMethods() + it.NumVirtualMethods());
245 // Skip fields
246 while (it.HasNextStaticField()) {
247 it.Next();
248 }
249 while (it.HasNextInstanceField()) {
250 it.Next();
251 }
252 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700253 size_t class_def_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700254 while (it.HasNextDirectMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800255 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700256 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
257 is_native, it.GetMethodInvokeType(class_def), it.GetMemberIndex(),
258 &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700259 class_def_method_index++;
260 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700261 }
Ian Rogers0571d352011-11-03 19:51:38 -0700262 while (it.HasNextVirtualMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800263 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700264 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
265 is_native, it.GetMethodInvokeType(class_def), it.GetMemberIndex(),
266 &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700267 class_def_method_index++;
268 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700269 }
Ian Rogers0571d352011-11-03 19:51:38 -0700270 DCHECK(!it.HasNext());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700271 return offset;
272}
273
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700274size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
275 size_t __attribute__((unused)) class_def_index,
276 size_t class_def_method_index,
277 bool __attribute__((unused)) is_native,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800278 InvokeType invoke_type,
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700279 uint32_t method_idx, const DexFile* dex_file) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700280 // derived from CompiledMethod if available
281 uint32_t code_offset = 0;
282 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700283 uint32_t core_spill_mask = 0;
284 uint32_t fp_spill_mask = 0;
285 uint32_t mapping_table_offset = 0;
286 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800287 uint32_t gc_map_offset = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700288
Brian Carlstrom265091e2013-01-30 14:08:26 -0800289 OatClass* oat_class = oat_classes_[oat_class_index];
290#if defined(ART_USE_PORTABLE_COMPILER)
291 size_t oat_method_offsets_offset =
292 oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index);
293#endif
294
Ian Rogers0571d352011-11-03 19:51:38 -0700295 CompiledMethod* compiled_method =
Brian Carlstrom51c24672013-07-11 16:00:56 -0700296 compiler_driver_->GetCompiledMethod(MethodReference(dex_file, method_idx));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700297 if (compiled_method != NULL) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800298#if defined(ART_USE_PORTABLE_COMPILER)
299 compiled_method->AddOatdataOffsetToCompliledCodeOffset(
300 oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_));
301#else
302 const std::vector<uint8_t>& code = compiled_method->GetCode();
Logan Chien971bf3f2012-05-01 15:47:55 +0800303 offset = compiled_method->AlignCode(offset);
304 DCHECK_ALIGNED(offset, kArmAlignment);
Logan Chien971bf3f2012-05-01 15:47:55 +0800305 uint32_t code_size = code.size() * sizeof(code[0]);
306 CHECK_NE(code_size, 0U);
307 uint32_t thumb_offset = compiled_method->CodeDelta();
308 code_offset = offset + sizeof(code_size) + thumb_offset;
309
310 // Deduplicate code arrays
311 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
312 if (code_iter != code_offsets_.end()) {
313 code_offset = code_iter->second;
jeffhao55d78212011-11-02 11:41:50 -0700314 } else {
Logan Chien971bf3f2012-05-01 15:47:55 +0800315 code_offsets_.Put(&code, code_offset);
316 offset += sizeof(code_size); // code size is prepended before code
317 offset += code_size;
318 oat_header_->UpdateChecksum(&code[0], code_size);
319 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800320#endif
Logan Chien971bf3f2012-05-01 15:47:55 +0800321 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
322 core_spill_mask = compiled_method->GetCoreSpillMask();
323 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700324
Logan Chien971bf3f2012-05-01 15:47:55 +0800325 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
326 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
327 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700328
Logan Chien971bf3f2012-05-01 15:47:55 +0800329 // Deduplicate mapping tables
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700330 SafeMap<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
331 mapping_table_offsets_.find(&mapping_table);
Logan Chien971bf3f2012-05-01 15:47:55 +0800332 if (mapping_iter != mapping_table_offsets_.end()) {
333 mapping_table_offset = mapping_iter->second;
334 } else {
335 mapping_table_offsets_.Put(&mapping_table, mapping_table_offset);
336 offset += mapping_table_size;
337 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
338 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700339
Logan Chien971bf3f2012-05-01 15:47:55 +0800340 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
341 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
342 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700343
Logan Chien971bf3f2012-05-01 15:47:55 +0800344 // Deduplicate vmap tables
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700345 SafeMap<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
346 vmap_table_offsets_.find(&vmap_table);
Logan Chien971bf3f2012-05-01 15:47:55 +0800347 if (vmap_iter != vmap_table_offsets_.end()) {
348 vmap_table_offset = vmap_iter->second;
349 } else {
350 vmap_table_offsets_.Put(&vmap_table, vmap_table_offset);
351 offset += vmap_table_size;
352 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
353 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800354
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700355 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800356 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
357 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
Logan Chienccb7bf12012-03-28 12:52:32 +0800358
TDYa127ce4cc0d2012-11-18 16:59:53 -0800359#if !defined(NDEBUG)
Logan Chien971bf3f2012-05-01 15:47:55 +0800360 // We expect GC maps except when the class hasn't been verified or the method is native
Brian Carlstrom51c24672013-07-11 16:00:56 -0700361 ClassReference class_ref(dex_file, class_def_index);
Ian Rogers1212a022013-03-04 10:48:41 -0800362 CompiledClass* compiled_class = compiler_driver_->GetCompiledClass(class_ref);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800363 mirror::Class::Status status;
jeffhaoec014232012-09-05 10:42:25 -0700364 if (compiled_class != NULL) {
365 status = compiled_class->GetStatus();
366 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800367 status = mirror::Class::kStatusError;
jeffhaoec014232012-09-05 10:42:25 -0700368 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800369 status = mirror::Class::kStatusNotReady;
jeffhaoec014232012-09-05 10:42:25 -0700370 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800371 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
372 << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
373 << (status < mirror::Class::kStatusVerified) << " " << status << " "
374 << PrettyMethod(method_idx, *dex_file);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800375#endif
376
Logan Chien971bf3f2012-05-01 15:47:55 +0800377 // Deduplicate GC maps
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700378 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
379 gc_map_offsets_.find(&gc_map);
Logan Chien971bf3f2012-05-01 15:47:55 +0800380 if (gc_map_iter != gc_map_offsets_.end()) {
381 gc_map_offset = gc_map_iter->second;
382 } else {
383 gc_map_offsets_.Put(&gc_map, gc_map_offset);
384 offset += gc_map_size;
385 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800386 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700387 }
388
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700389 oat_class->method_offsets_[class_def_method_index] =
390 OatMethodOffsets(code_offset,
391 frame_size_in_bytes,
392 core_spill_mask,
393 fp_spill_mask,
394 mapping_table_offset,
395 vmap_table_offset,
396 gc_map_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700397
Ian Rogers1212a022013-03-04 10:48:41 -0800398 if (compiler_driver_->IsImage()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700399 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800400 mirror::DexCache* dex_cache = linker->FindDexCache(*dex_file);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700401 // Unchecked as we hold mutator_lock_ on entry.
402 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800403 mirror::AbstractMethod* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800404 NULL, NULL, invoke_type);
Ian Rogers0571d352011-11-03 19:51:38 -0700405 CHECK(method != NULL);
406 method->SetFrameSizeInBytes(frame_size_in_bytes);
407 method->SetCoreSpillMask(core_spill_mask);
408 method->SetFpSpillMask(fp_spill_mask);
409 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800410 // Don't overwrite static method trampoline
411 if (!method->IsStatic() || method->IsConstructor() ||
412 method->GetDeclaringClass()->IsInitialized()) {
413 method->SetOatCodeOffset(code_offset);
414 } else {
Jeff Haoaa4a7932013-05-13 11:28:27 -0700415 method->SetEntryPointFromCompiledCode(NULL);
Ian Rogers19846512012-02-24 11:42:47 -0800416 }
Ian Rogers0571d352011-11-03 19:51:38 -0700417 method->SetOatVmapTableOffset(vmap_table_offset);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700418 method->SetOatNativeGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700419 }
Logan Chien8b977d32012-02-21 19:14:55 +0800420
Brian Carlstrome24fa612011-09-29 00:53:55 -0700421 return offset;
422}
423
Brian Carlstrom265091e2013-01-30 14:08:26 -0800424#define DCHECK_OFFSET() \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700425 DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out.Seek(0, kSeekCurrent)) \
426 << "file_offset=" << file_offset << " relative_offset=" << relative_offset
Brian Carlstrom265091e2013-01-30 14:08:26 -0800427
428#define DCHECK_OFFSET_() \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700429 DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out.Seek(0, kSeekCurrent)) \
430 << "file_offset=" << file_offset << " offset_=" << offset_
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700431
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800432bool OatWriter::Write(OutputStream& out) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700433 const size_t file_offset = out.Seek(0, kSeekCurrent);
434
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800435 if (!out.WriteFully(oat_header_, sizeof(*oat_header_))) {
436 PLOG(ERROR) << "Failed to write oat header to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700437 return false;
438 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700439 size_oat_header_ += sizeof(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700440
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800441 if (!out.WriteFully(image_file_location_.data(), image_file_location_.size())) {
442 PLOG(ERROR) << "Failed to write oat header image file location to " << out.GetLocation();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700443 return false;
444 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700445 size_oat_header_image_file_location_ += image_file_location_.size();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700446
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700447 if (!WriteTables(out, file_offset)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800448 LOG(ERROR) << "Failed to write oat tables to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700449 return false;
450 }
451
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700452 size_t relative_offset = WriteCode(out, file_offset);
453 if (relative_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800454 LOG(ERROR) << "Failed to write oat code to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700455 return false;
456 }
457
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700458 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
459 if (relative_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800460 LOG(ERROR) << "Failed to write oat code for dex files to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700461 return false;
462 }
463
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700464 if (kIsDebugBuild) {
465 uint32_t size_total = 0;
466 #define DO_STAT(x) \
467 LOG(INFO) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
468 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700469
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700470 DO_STAT(size_dex_file_alignment_);
471 DO_STAT(size_executable_offset_alignment_);
472 DO_STAT(size_oat_header_);
473 DO_STAT(size_oat_header_image_file_location_);
474 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -0700475 DO_STAT(size_interpreter_to_interpreter_bridge_);
476 DO_STAT(size_interpreter_to_compiled_code_bridge_);
477 DO_STAT(size_jni_dlsym_lookup_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700478 DO_STAT(size_portable_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700479 DO_STAT(size_portable_to_interpreter_bridge_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700480 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700481 DO_STAT(size_quick_to_interpreter_bridge_);
482 DO_STAT(size_trampoline_alignment_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700483 DO_STAT(size_code_size_);
484 DO_STAT(size_code_);
485 DO_STAT(size_code_alignment_);
486 DO_STAT(size_mapping_table_);
487 DO_STAT(size_vmap_table_);
488 DO_STAT(size_gc_map_);
489 DO_STAT(size_oat_dex_file_location_size_);
490 DO_STAT(size_oat_dex_file_location_data_);
491 DO_STAT(size_oat_dex_file_location_checksum_);
492 DO_STAT(size_oat_dex_file_offset_);
493 DO_STAT(size_oat_dex_file_methods_offsets_);
494 DO_STAT(size_oat_class_status_);
495 DO_STAT(size_oat_class_method_offsets_);
496 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700497
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700498 LOG(INFO) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700499 CHECK_EQ(file_offset + size_total, static_cast<uint32_t>(out.Seek(0, kSeekCurrent)));
500 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700501 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700502
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700503 CHECK_EQ(file_offset + size_, static_cast<uint32_t>(out.Seek(0, kSeekCurrent)));
504 CHECK_EQ(size_, relative_offset);
505
Brian Carlstrome24fa612011-09-29 00:53:55 -0700506 return true;
507}
508
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700509bool OatWriter::WriteTables(OutputStream& out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700510 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700511 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800512 PLOG(ERROR) << "Failed to write oat dex information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700513 return false;
514 }
515 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800516 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700517 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800518 off_t actual_offset = out.Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -0800519 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
520 const DexFile* dex_file = (*dex_files_)[i];
521 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
522 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
523 return false;
524 }
525 const DexFile* dex_file = (*dex_files_)[i];
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800526 if (!out.WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700527 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
528 << " to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800529 return false;
530 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700531 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800532 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800533 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700534 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800535 PLOG(ERROR) << "Failed to write oat methods information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700536 return false;
537 }
538 }
539 return true;
540}
541
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700542size_t OatWriter::WriteCode(OutputStream& out, const size_t file_offset) {
543 size_t relative_offset = oat_header_->GetExecutableOffset();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700544 off_t new_offset = out.Seek(size_executable_offset_alignment_, kSeekCurrent);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700545 size_t expected_file_offset = file_offset + relative_offset;
546 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700547 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700548 << " Expected: " << expected_file_offset << " File: " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700549 return 0;
550 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800551 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700552 if (compiler_driver_->IsImage()) {
553 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700554
Ian Rogers848871b2013-08-05 10:56:33 -0700555 #define DO_TRAMPOLINE(field) \
556 do { \
557 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
558 uint32_t alignment_padding = aligned_offset - relative_offset; \
559 out.Seek(alignment_padding, kSeekCurrent); \
560 size_trampoline_alignment_ += alignment_padding; \
561 if (!out.WriteFully(&(*field)[0], field->size())) { \
562 PLOG(ERROR) << "Failed to write " # field " to " << out.GetLocation(); \
563 return false; \
564 } \
565 size_ ## field += field->size(); \
566 relative_offset += alignment_padding + field->size(); \
567 DCHECK_OFFSET(); \
568 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700569
Ian Rogers848871b2013-08-05 10:56:33 -0700570 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_);
571 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_);
572 DO_TRAMPOLINE(jni_dlsym_lookup_);
573 DO_TRAMPOLINE(portable_resolution_trampoline_);
574 DO_TRAMPOLINE(portable_to_interpreter_bridge_);
575 DO_TRAMPOLINE(quick_resolution_trampoline_);
576 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
577 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700578 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700579 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700580}
581
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700582size_t OatWriter::WriteCodeDexFiles(OutputStream& out,
583 const size_t file_offset,
584 size_t relative_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700585 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800586 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700587 const DexFile* dex_file = (*dex_files_)[i];
588 CHECK(dex_file != NULL);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700589 relative_offset = WriteCodeDexFile(out, file_offset, relative_offset, oat_class_index,
590 *dex_file);
591 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700592 return 0;
593 }
594 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700595 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700596}
597
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700598size_t OatWriter::WriteCodeDexFile(OutputStream& out, const size_t file_offset,
599 size_t relative_offset, size_t& oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700600 const DexFile& dex_file) {
601 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
602 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700603 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700604 relative_offset = WriteCodeClassDef(out, file_offset, relative_offset, oat_class_index,
605 dex_file, class_def);
606 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700607 return 0;
608 }
609 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700610 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700611}
612
Ian Rogers0571d352011-11-03 19:51:38 -0700613void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800614 const DexFile& dex_file, OutputStream& out) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700615 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800616 << " to " << out.GetLocation();
Elliott Hughes234da572011-11-03 22:13:06 -0700617}
618
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800619size_t OatWriter::WriteCodeClassDef(OutputStream& out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700620 const size_t file_offset,
621 size_t relative_offset,
622 size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700623 const DexFile& dex_file,
624 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700625 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700626 if (class_data == NULL) {
627 // ie. an empty class such as a marker interface
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700628 return relative_offset;
Ian Rogers387b6992011-10-31 17:52:37 -0700629 }
Ian Rogers0571d352011-11-03 19:51:38 -0700630 ClassDataItemIterator it(dex_file, class_data);
631 // Skip fields
632 while (it.HasNextStaticField()) {
633 it.Next();
634 }
635 while (it.HasNextInstanceField()) {
636 it.Next();
637 }
638 // Process methods
639 size_t class_def_method_index = 0;
640 while (it.HasNextDirectMethod()) {
641 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700642 relative_offset = WriteCodeMethod(out, file_offset, relative_offset, oat_class_index,
643 class_def_method_index, is_static, it.GetMemberIndex(),
644 dex_file);
645 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700646 return 0;
647 }
Ian Rogers0571d352011-11-03 19:51:38 -0700648 class_def_method_index++;
649 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700650 }
Ian Rogers0571d352011-11-03 19:51:38 -0700651 while (it.HasNextVirtualMethod()) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700652 relative_offset = WriteCodeMethod(out, file_offset, relative_offset, oat_class_index,
653 class_def_method_index, false, it.GetMemberIndex(), dex_file);
654 if (relative_offset == 0) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700655 return 0;
656 }
Ian Rogers0571d352011-11-03 19:51:38 -0700657 class_def_method_index++;
658 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700659 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700660 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700661}
662
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700663size_t OatWriter::WriteCodeMethod(OutputStream& out, const size_t file_offset,
664 size_t relative_offset, size_t oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700665 size_t class_def_method_index, bool is_static,
666 uint32_t method_idx, const DexFile& dex_file) {
667 const CompiledMethod* compiled_method =
Brian Carlstrom51c24672013-07-11 16:00:56 -0700668 compiler_driver_->GetCompiledMethod(MethodReference(&dex_file, method_idx));
Ian Rogers0571d352011-11-03 19:51:38 -0700669
Ian Rogers0571d352011-11-03 19:51:38 -0700670 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800671 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700672
673
674 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstrom265091e2013-01-30 14:08:26 -0800675#if !defined(ART_USE_PORTABLE_COMPILER)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700676 uint32_t aligned_offset = compiled_method->AlignCode(relative_offset);
677 uint32_t aligned_code_delta = aligned_offset - relative_offset;
Logan Chien971bf3f2012-05-01 15:47:55 +0800678 if (aligned_code_delta != 0) {
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800679 off_t new_offset = out.Seek(aligned_code_delta, kSeekCurrent);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700680 size_code_alignment_ += aligned_code_delta;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700681 uint32_t expected_offset = file_offset + aligned_offset;
682 if (static_cast<uint32_t>(new_offset) != expected_offset) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800683 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700684 << " Expected: " << expected_offset << " File: " << out.GetLocation();
Logan Chien971bf3f2012-05-01 15:47:55 +0800685 return 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700686 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700687 relative_offset += aligned_code_delta;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800688 DCHECK_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700689 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700690 DCHECK_ALIGNED(relative_offset, kArmAlignment);
Logan Chien971bf3f2012-05-01 15:47:55 +0800691 const std::vector<uint8_t>& code = compiled_method->GetCode();
692 uint32_t code_size = code.size() * sizeof(code[0]);
693 CHECK_NE(code_size, 0U);
694
695 // Deduplicate code arrays
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700696 size_t code_offset = relative_offset + sizeof(code_size) + compiled_method->CodeDelta();
Logan Chien971bf3f2012-05-01 15:47:55 +0800697 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800698 if (code_iter != code_offsets_.end() && code_offset != method_offsets.code_offset_) {
699 DCHECK(code_iter->second == method_offsets.code_offset_)
700 << PrettyMethod(method_idx, dex_file);
Logan Chien971bf3f2012-05-01 15:47:55 +0800701 } else {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800702 DCHECK(code_offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800703 if (!out.WriteFully(&code_size, sizeof(code_size))) {
704 ReportWriteFailure("method code size", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800705 return 0;
706 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700707 size_code_size_ += sizeof(code_size);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700708 relative_offset += sizeof(code_size);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800709 DCHECK_OFFSET();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800710 if (!out.WriteFully(&code[0], code_size)) {
711 ReportWriteFailure("method code", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800712 return 0;
713 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700714 size_code_ += code_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700715 relative_offset += code_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800716 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800717 DCHECK_OFFSET();
718#endif
Logan Chien971bf3f2012-05-01 15:47:55 +0800719
720 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
721 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
722
723 // Deduplicate mapping tables
724 SafeMap<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
725 mapping_table_offsets_.find(&mapping_table);
726 if (mapping_iter != mapping_table_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700727 relative_offset != method_offsets.mapping_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800728 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
729 || mapping_iter->second == method_offsets.mapping_table_offset_)
730 << PrettyMethod(method_idx, dex_file);
731 } else {
732 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700733 || relative_offset == method_offsets.mapping_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800734 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800735 if (!out.WriteFully(&mapping_table[0], mapping_table_size)) {
736 ReportWriteFailure("mapping table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800737 return 0;
738 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700739 size_mapping_table_ += mapping_table_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700740 relative_offset += mapping_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800741 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800742 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800743
744 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
745 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
746
747 // Deduplicate vmap tables
748 SafeMap<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
749 vmap_table_offsets_.find(&vmap_table);
750 if (vmap_iter != vmap_table_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700751 relative_offset != method_offsets.vmap_table_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800752 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
753 || vmap_iter->second == method_offsets.vmap_table_offset_)
754 << PrettyMethod(method_idx, dex_file);
755 } else {
756 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700757 || relative_offset == method_offsets.vmap_table_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800758 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800759 if (!out.WriteFully(&vmap_table[0], vmap_table_size)) {
760 ReportWriteFailure("vmap table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800761 return 0;
762 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700763 size_vmap_table_ += vmap_table_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700764 relative_offset += vmap_table_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800765 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800766 DCHECK_OFFSET();
Logan Chien971bf3f2012-05-01 15:47:55 +0800767
Ian Rogersfbdc0fa2013-04-18 16:48:17 -0700768 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800769 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
770
771 // Deduplicate GC maps
772 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
773 gc_map_offsets_.find(&gc_map);
774 if (gc_map_iter != gc_map_offsets_.end() &&
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700775 relative_offset != method_offsets.gc_map_offset_) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800776 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
777 || gc_map_iter->second == method_offsets.gc_map_offset_)
778 << PrettyMethod(method_idx, dex_file);
779 } else {
780 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700781 || relative_offset == method_offsets.gc_map_offset_)
Logan Chien971bf3f2012-05-01 15:47:55 +0800782 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800783 if (!out.WriteFully(&gc_map[0], gc_map_size)) {
784 ReportWriteFailure("GC map", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800785 return 0;
786 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700787 size_gc_map_ += gc_map_size;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700788 relative_offset += gc_map_size;
Logan Chien971bf3f2012-05-01 15:47:55 +0800789 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800790 DCHECK_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700791 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800792
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700793 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700794}
795
Brian Carlstrom265091e2013-01-30 14:08:26 -0800796OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
797 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -0800798 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700799 dex_file_location_size_ = location.size();
800 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800801 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800802 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800803 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700804}
805
806size_t OatWriter::OatDexFile::SizeOf() const {
807 return sizeof(dex_file_location_size_)
808 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800809 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800810 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800811 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700812}
813
814void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
815 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
816 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800817 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800818 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800819 oat_header.UpdateChecksum(&methods_offsets_[0],
820 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700821}
822
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700823bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
824 OutputStream& out,
825 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800826 DCHECK_OFFSET_();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800827 if (!out.WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
828 PLOG(ERROR) << "Failed to write dex file location length to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700829 return false;
830 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700831 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800832 if (!out.WriteFully(dex_file_location_data_, dex_file_location_size_)) {
833 PLOG(ERROR) << "Failed to write dex file location data to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700834 return false;
835 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700836 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800837 if (!out.WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
838 PLOG(ERROR) << "Failed to write dex file location checksum to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700839 return false;
840 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700841 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800842 if (!out.WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
843 PLOG(ERROR) << "Failed to write dex file offset to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800844 return false;
845 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700846 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800847 if (!out.WriteFully(&methods_offsets_[0],
848 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
849 PLOG(ERROR) << "Failed to write methods offsets to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700850 return false;
851 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700852 oat_writer->size_oat_dex_file_methods_offsets_ +=
853 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700854 return true;
855}
856
Brian Carlstrom265091e2013-01-30 14:08:26 -0800857OatWriter::OatClass::OatClass(size_t offset, mirror::Class::Status status, uint32_t methods_count) {
858 offset_ = offset;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800859 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700860 method_offsets_.resize(methods_count);
861}
862
Brian Carlstrom265091e2013-01-30 14:08:26 -0800863size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
864 size_t class_def_method_index_) const {
865 return offset_ + GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
866}
867
868size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
869 size_t class_def_method_index_) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800870 return sizeof(status_)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800871 + (sizeof(method_offsets_[0]) * class_def_method_index_);
872}
873
874size_t OatWriter::OatClass::SizeOf() const {
875 return GetOatMethodOffsetsOffsetFromOatClass(method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700876}
877
Brian Carlstrom389efb02012-01-11 12:06:26 -0800878void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800879 oat_header.UpdateChecksum(&status_, sizeof(status_));
880 oat_header.UpdateChecksum(&method_offsets_[0],
881 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700882}
883
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700884bool OatWriter::OatClass::Write(OatWriter* oat_writer,
885 OutputStream& out,
886 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800887 DCHECK_OFFSET_();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800888 if (!out.WriteFully(&status_, sizeof(status_))) {
889 PLOG(ERROR) << "Failed to write class status to " << out.GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800890 return false;
891 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700892 oat_writer->size_oat_class_status_ += sizeof(status_);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700893 DCHECK_EQ(static_cast<off_t>(file_offset + GetOatMethodOffsetsOffsetFromOatHeader(0)),
Brian Carlstrom265091e2013-01-30 14:08:26 -0800894 out.Seek(0, kSeekCurrent));
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800895 if (!out.WriteFully(&method_offsets_[0],
896 sizeof(method_offsets_[0]) * method_offsets_.size())) {
897 PLOG(ERROR) << "Failed to write method offsets to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700898 return false;
899 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700900 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700901 DCHECK_EQ(static_cast<off_t>(file_offset +
902 GetOatMethodOffsetsOffsetFromOatHeader(method_offsets_.size())),
Brian Carlstrom265091e2013-01-30 14:08:26 -0800903 out.Seek(0, kSeekCurrent));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700904 return true;
905}
906
Brian Carlstrome24fa612011-09-29 00:53:55 -0700907} // namespace art