blob: 6fcea5d8e25abf7234adc7cca80713d6449c5b0f [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 Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "mirror/abstract_method-inl.h"
25#include "mirror/array.h"
26#include "mirror/class_loader.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070027#include "os.h"
Brian Carlstromcd60ac72013-01-20 17:09:51 -080028#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070029#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070030#include "scoped_thread_state_change.h"
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070031#include "gc/space.h"
jeffhaoec014232012-09-05 10:42:25 -070032#include "verifier/method_verifier.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070033
34namespace art {
35
Brian Carlstromcd60ac72013-01-20 17:09:51 -080036bool OatWriter::Create(OutputStream& output_stream,
jeffhao10037c82012-01-23 15:06:23 -080037 const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070038 uint32_t image_file_location_oat_checksum,
39 uint32_t image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070040 const std::string& image_file_location,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070041 const Compiler& compiler) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070042 OatWriter oat_writer(dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070043 image_file_location_oat_checksum,
44 image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070045 image_file_location,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070046 compiler);
Brian Carlstromcd60ac72013-01-20 17:09:51 -080047 return oat_writer.Write(output_stream);
Brian Carlstrome24fa612011-09-29 00:53:55 -070048}
49
Brian Carlstrom3320cf42011-10-04 14:58:28 -070050OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070051 uint32_t image_file_location_oat_checksum,
52 uint32_t image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070053 const std::string& image_file_location,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070054 const Compiler& compiler) {
55 compiler_ = &compiler;
Brian Carlstrom28db0122012-10-18 16:20:41 -070056 image_file_location_oat_checksum_ = image_file_location_oat_checksum;
57 image_file_location_oat_begin_ = image_file_location_oat_begin;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070058 image_file_location_ = image_file_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -070059 dex_files_ = &dex_files;
Ian Rogers0571d352011-11-03 19:51:38 -070060 oat_header_ = NULL;
61 executable_offset_padding_length_ = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -070062
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070063 size_t offset = InitOatHeader();
Brian Carlstrome24fa612011-09-29 00:53:55 -070064 offset = InitOatDexFiles(offset);
Brian Carlstrom89521892011-12-07 22:05:07 -080065 offset = InitDexFiles(offset);
Brian Carlstrom389efb02012-01-11 12:06:26 -080066 offset = InitOatClasses(offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -070067 offset = InitOatCode(offset);
68 offset = InitOatCodeDexFiles(offset);
69
70 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -070071}
72
Ian Rogers0571d352011-11-03 19:51:38 -070073OatWriter::~OatWriter() {
74 delete oat_header_;
75 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -080076 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -070077}
78
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070079size_t OatWriter::InitOatHeader() {
Brian Carlstrome24fa612011-09-29 00:53:55 -070080 // create the OatHeader
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070081 oat_header_ = new OatHeader(compiler_->GetInstructionSet(),
82 dex_files_,
Brian Carlstrom28db0122012-10-18 16:20:41 -070083 image_file_location_oat_checksum_,
84 image_file_location_oat_begin_,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070085 image_file_location_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070086 size_t offset = sizeof(*oat_header_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070087 offset += image_file_location_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -070088 return offset;
89}
90
91size_t OatWriter::InitOatDexFiles(size_t offset) {
92 // create the OatDexFiles
93 for (size_t i = 0; i != dex_files_->size(); ++i) {
94 const DexFile* dex_file = (*dex_files_)[i];
95 CHECK(dex_file != NULL);
96 OatDexFile* oat_dex_file = new OatDexFile(*dex_file);
97 oat_dex_files_.push_back(oat_dex_file);
98 offset += oat_dex_file->SizeOf();
99 }
100 return offset;
101}
102
Brian Carlstrom89521892011-12-07 22:05:07 -0800103size_t OatWriter::InitDexFiles(size_t offset) {
104 // calculate the offsets within OatDexFiles to the DexFiles
105 for (size_t i = 0; i != dex_files_->size(); ++i) {
106 // dex files are required to be 4 byte aligned
107 offset = RoundUp(offset, 4);
108
109 // set offset in OatDexFile to DexFile
110 oat_dex_files_[i]->dex_file_offset_ = offset;
111
112 const DexFile* dex_file = (*dex_files_)[i];
113 offset += dex_file->GetHeader().file_size_;
114 }
115 return offset;
116}
117
Brian Carlstrom389efb02012-01-11 12:06:26 -0800118size_t OatWriter::InitOatClasses(size_t offset) {
119 // create the OatClasses
120 // calculate the offsets within OatDexFiles to OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -0700121 for (size_t i = 0; i != dex_files_->size(); ++i) {
122 const DexFile* dex_file = (*dex_files_)[i];
123 for (size_t class_def_index = 0;
124 class_def_index < dex_file->NumClassDefs();
Ian Rogersc20a83e2012-01-18 18:15:32 -0800125 class_def_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800126 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700127 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
128 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700129 uint32_t num_methods = 0;
130 if (class_data != NULL) { // ie not an empty class, such as a marker interface
131 ClassDataItemIterator it(*dex_file, class_data);
132 size_t num_direct_methods = it.NumDirectMethods();
133 size_t num_virtual_methods = it.NumVirtualMethods();
134 num_methods = num_direct_methods + num_virtual_methods;
135 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800136
jeffhaoec014232012-09-05 10:42:25 -0700137 Compiler::ClassReference class_ref = Compiler::ClassReference(dex_file, class_def_index);
138 CompiledClass* compiled_class = compiler_->GetCompiledClass(class_ref);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139 mirror::Class::Status status;
jeffhaoec014232012-09-05 10:42:25 -0700140 if (compiled_class != NULL) {
141 status = compiled_class->GetStatus();
142 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143 status = mirror::Class::kStatusError;
jeffhaoec014232012-09-05 10:42:25 -0700144 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145 status = mirror::Class::kStatusNotReady;
jeffhaoec014232012-09-05 10:42:25 -0700146 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800147
148 OatClass* oat_class = new OatClass(status, num_methods);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800149 oat_classes_.push_back(oat_class);
150 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700151 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800152 oat_dex_files_[i]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700153 }
154 return offset;
155}
156
157size_t OatWriter::InitOatCode(size_t offset) {
158 // calculate the offsets within OatHeader to executable code
159 size_t old_offset = offset;
160 // required to be on a new page boundary
161 offset = RoundUp(offset, kPageSize);
162 oat_header_->SetExecutableOffset(offset);
163 executable_offset_padding_length_ = offset - old_offset;
164 return offset;
165}
166
167size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700168 size_t oat_class_index = 0;
169 for (size_t i = 0; i != dex_files_->size(); ++i) {
170 const DexFile* dex_file = (*dex_files_)[i];
171 CHECK(dex_file != NULL);
172 offset = InitOatCodeDexFile(offset, oat_class_index, *dex_file);
173 }
174 return offset;
175}
176
177size_t OatWriter::InitOatCodeDexFile(size_t offset,
178 size_t& oat_class_index,
179 const DexFile& dex_file) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800180 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700181 class_def_index < dex_file.NumClassDefs();
182 class_def_index++, oat_class_index++) {
183 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800184 offset = InitOatCodeClassDef(offset, oat_class_index, class_def_index, dex_file, class_def);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800185 oat_classes_[oat_class_index]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700186 }
187 return offset;
188}
189
190size_t OatWriter::InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800191 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700192 const DexFile& dex_file,
193 const DexFile::ClassDef& class_def) {
194 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700195 if (class_data == NULL) {
196 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700197 return offset;
198 }
Ian Rogers0571d352011-11-03 19:51:38 -0700199 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800200 CHECK_EQ(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700201 it.NumDirectMethods() + it.NumVirtualMethods());
202 // Skip fields
203 while (it.HasNextStaticField()) {
204 it.Next();
205 }
206 while (it.HasNextInstanceField()) {
207 it.Next();
208 }
209 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700210 size_t class_def_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700211 while (it.HasNextDirectMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800212 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700213 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
214 is_native, it.GetMethodInvokeType(class_def), it.GetMemberIndex(),
215 &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700216 class_def_method_index++;
217 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700218 }
Ian Rogers0571d352011-11-03 19:51:38 -0700219 while (it.HasNextVirtualMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800220 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700221 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
222 is_native, it.GetMethodInvokeType(class_def), it.GetMemberIndex(),
223 &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700224 class_def_method_index++;
225 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700226 }
Ian Rogers0571d352011-11-03 19:51:38 -0700227 DCHECK(!it.HasNext());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700228 return offset;
229}
230
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700231size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
232 size_t __attribute__((unused)) class_def_index,
233 size_t class_def_method_index,
234 bool __attribute__((unused)) is_native,
Ian Rogers08f753d2012-08-24 14:35:25 -0700235 InvokeType type,
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700236 uint32_t method_idx, const DexFile* dex_file) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700237 // derived from CompiledMethod if available
238 uint32_t code_offset = 0;
239 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700240 uint32_t core_spill_mask = 0;
241 uint32_t fp_spill_mask = 0;
242 uint32_t mapping_table_offset = 0;
243 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800244 uint32_t gc_map_offset = 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700245 // derived from CompiledInvokeStub if available
246 uint32_t invoke_stub_offset = 0;
Brian Carlstromfd2ec542012-05-02 15:08:57 -0700247#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800248 uint32_t proxy_stub_offset = 0;
Brian Carlstromfd2ec542012-05-02 15:08:57 -0700249#endif
Brian Carlstrome24fa612011-09-29 00:53:55 -0700250
Ian Rogers0571d352011-11-03 19:51:38 -0700251 CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800252 compiler_->GetCompiledMethod(Compiler::MethodReference(dex_file, method_idx));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700253 if (compiled_method != NULL) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800254 offset = compiled_method->AlignCode(offset);
255 DCHECK_ALIGNED(offset, kArmAlignment);
256 const std::vector<uint8_t>& code = compiled_method->GetCode();
257 uint32_t code_size = code.size() * sizeof(code[0]);
258 CHECK_NE(code_size, 0U);
259 uint32_t thumb_offset = compiled_method->CodeDelta();
260 code_offset = offset + sizeof(code_size) + thumb_offset;
261
262 // Deduplicate code arrays
263 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
264 if (code_iter != code_offsets_.end()) {
265 code_offset = code_iter->second;
jeffhao55d78212011-11-02 11:41:50 -0700266 } else {
Logan Chien971bf3f2012-05-01 15:47:55 +0800267 code_offsets_.Put(&code, code_offset);
268 offset += sizeof(code_size); // code size is prepended before code
269 offset += code_size;
270 oat_header_->UpdateChecksum(&code[0], code_size);
271 }
272 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
273 core_spill_mask = compiled_method->GetCoreSpillMask();
274 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700275
Logan Chien971bf3f2012-05-01 15:47:55 +0800276 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
277 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
278 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700279
Logan Chien971bf3f2012-05-01 15:47:55 +0800280 // Deduplicate mapping tables
281 SafeMap<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
282 if (mapping_iter != mapping_table_offsets_.end()) {
283 mapping_table_offset = mapping_iter->second;
284 } else {
285 mapping_table_offsets_.Put(&mapping_table, mapping_table_offset);
286 offset += mapping_table_size;
287 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
288 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700289
Logan Chien971bf3f2012-05-01 15:47:55 +0800290 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
291 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
292 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700293
Logan Chien971bf3f2012-05-01 15:47:55 +0800294 // Deduplicate vmap tables
295 SafeMap<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
296 if (vmap_iter != vmap_table_offsets_.end()) {
297 vmap_table_offset = vmap_iter->second;
298 } else {
299 vmap_table_offsets_.Put(&vmap_table, vmap_table_offset);
300 offset += vmap_table_size;
301 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
302 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800303
Ian Rogers0c7abda2012-09-19 13:33:42 -0700304 const std::vector<uint8_t>& gc_map = compiled_method->GetNativeGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800305 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
306 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
Logan Chienccb7bf12012-03-28 12:52:32 +0800307
TDYa127ce4cc0d2012-11-18 16:59:53 -0800308#if !defined(NDEBUG)
Logan Chien971bf3f2012-05-01 15:47:55 +0800309 // We expect GC maps except when the class hasn't been verified or the method is native
jeffhaoec014232012-09-05 10:42:25 -0700310 Compiler::ClassReference class_ref = Compiler::ClassReference(dex_file, class_def_index);
311 CompiledClass* compiled_class = compiler_->GetCompiledClass(class_ref);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800312 mirror::Class::Status status;
jeffhaoec014232012-09-05 10:42:25 -0700313 if (compiled_class != NULL) {
314 status = compiled_class->GetStatus();
315 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800316 status = mirror::Class::kStatusError;
jeffhaoec014232012-09-05 10:42:25 -0700317 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800318 status = mirror::Class::kStatusNotReady;
jeffhaoec014232012-09-05 10:42:25 -0700319 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800320 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
321 << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
322 << (status < mirror::Class::kStatusVerified) << " " << status << " "
323 << PrettyMethod(method_idx, *dex_file);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800324#endif
325
Logan Chien971bf3f2012-05-01 15:47:55 +0800326 // Deduplicate GC maps
327 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = gc_map_offsets_.find(&gc_map);
328 if (gc_map_iter != gc_map_offsets_.end()) {
329 gc_map_offset = gc_map_iter->second;
330 } else {
331 gc_map_offsets_.Put(&gc_map, gc_map_offset);
332 offset += gc_map_size;
333 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800334 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700335 }
336
Ian Rogers0571d352011-11-03 19:51:38 -0700337 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx));
Ian Rogers08f753d2012-08-24 14:35:25 -0700338 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(type == kStatic,
339 shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700340 if (compiled_invoke_stub != NULL) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800341 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
342 DCHECK_ALIGNED(offset, kArmAlignment);
343 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
344 uint32_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
345 CHECK_NE(invoke_stub_size, 0U);
346 uint32_t thumb_offset = compiled_invoke_stub->CodeDelta();
347 invoke_stub_offset = offset + sizeof(invoke_stub_size) + thumb_offset;
Logan Chienccb7bf12012-03-28 12:52:32 +0800348
Logan Chien971bf3f2012-05-01 15:47:55 +0800349 // Deduplicate invoke stubs
350 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&invoke_stub);
351 if (stub_iter != code_offsets_.end()) {
352 invoke_stub_offset = stub_iter->second;
353 } else {
354 code_offsets_.Put(&invoke_stub, invoke_stub_offset);
355 offset += sizeof(invoke_stub_size); // invoke stub size is prepended before code
356 offset += invoke_stub_size;
357 oat_header_->UpdateChecksum(&invoke_stub[0], invoke_stub_size);
jeffhao55d78212011-11-02 11:41:50 -0700358 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700359 }
360
Logan Chien7a2a23a2012-06-06 11:01:00 +0800361#if defined(ART_USE_LLVM_COMPILER)
Logan Chien99bc2172012-10-18 10:03:36 +0800362 if (type != kStatic) {
Logan Chien7a2a23a2012-06-06 11:01:00 +0800363 const CompiledInvokeStub* compiled_proxy_stub = compiler_->FindProxyStub(shorty);
364 if (compiled_proxy_stub != NULL) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800365 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
366 DCHECK_ALIGNED(offset, kArmAlignment);
367 const std::vector<uint8_t>& proxy_stub = compiled_proxy_stub->GetCode();
368 uint32_t proxy_stub_size = proxy_stub.size() * sizeof(proxy_stub[0]);
369 CHECK_NE(proxy_stub_size, 0U);
370 uint32_t thumb_offset = compiled_proxy_stub->CodeDelta();
371 proxy_stub_offset = offset + sizeof(proxy_stub_size) + thumb_offset;
372
373 // Deduplicate proxy stubs
374 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&proxy_stub);
375 if (stub_iter != code_offsets_.end()) {
376 proxy_stub_offset = stub_iter->second;
377 } else {
378 code_offsets_.Put(&proxy_stub, proxy_stub_offset);
379 offset += sizeof(proxy_stub_size); // proxy stub size is prepended before code
380 offset += proxy_stub_size;
381 oat_header_->UpdateChecksum(&proxy_stub[0], proxy_stub_size);
382 }
Logan Chien7a2a23a2012-06-06 11:01:00 +0800383 }
384 }
385#endif
386
Brian Carlstrom389efb02012-01-11 12:06:26 -0800387 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index]
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700388 = OatMethodOffsets(code_offset,
389 frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700390 core_spill_mask,
391 fp_spill_mask,
392 mapping_table_offset,
393 vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800394 gc_map_offset,
Logan Chienccb7bf12012-03-28 12:52:32 +0800395 invoke_stub_offset
396#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800397 , proxy_stub_offset
Logan Chienccb7bf12012-03-28 12:52:32 +0800398#endif
399 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700400
Ian Rogers0571d352011-11-03 19:51:38 -0700401 if (compiler_->IsImage()) {
402 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800403 mirror::DexCache* dex_cache = linker->FindDexCache(*dex_file);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700404 // Unchecked as we hold mutator_lock_ on entry.
405 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800406 mirror::AbstractMethod* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache,
407 NULL, NULL, type);
Ian Rogers0571d352011-11-03 19:51:38 -0700408 CHECK(method != NULL);
409 method->SetFrameSizeInBytes(frame_size_in_bytes);
410 method->SetCoreSpillMask(core_spill_mask);
411 method->SetFpSpillMask(fp_spill_mask);
412 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800413 // Don't overwrite static method trampoline
414 if (!method->IsStatic() || method->IsConstructor() ||
415 method->GetDeclaringClass()->IsInitialized()) {
416 method->SetOatCodeOffset(code_offset);
417 } else {
418 method->SetCode(Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod)->GetData());
419 }
Ian Rogers0571d352011-11-03 19:51:38 -0700420 method->SetOatVmapTableOffset(vmap_table_offset);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700421 method->SetOatNativeGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700422 method->SetOatInvokeStubOffset(invoke_stub_offset);
423 }
Logan Chien8b977d32012-02-21 19:14:55 +0800424
Brian Carlstrome24fa612011-09-29 00:53:55 -0700425 return offset;
426}
427
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700428#define DCHECK_CODE_OFFSET() \
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800429 DCHECK_EQ(static_cast<off_t>(code_offset), out.Seek(0, kSeekCurrent))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700430
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800431bool OatWriter::Write(OutputStream& out) {
432 if (!out.WriteFully(oat_header_, sizeof(*oat_header_))) {
433 PLOG(ERROR) << "Failed to write oat header to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700434 return false;
435 }
436
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800437 if (!out.WriteFully(image_file_location_.data(), image_file_location_.size())) {
438 PLOG(ERROR) << "Failed to write oat header image file location to " << out.GetLocation();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700439 return false;
440 }
441
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800442 if (!WriteTables(out)) {
443 LOG(ERROR) << "Failed to write oat tables to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700444 return false;
445 }
446
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800447 size_t code_offset = WriteCode(out);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700448 if (code_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800449 LOG(ERROR) << "Failed to write oat code to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700450 return false;
451 }
452
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800453 code_offset = WriteCodeDexFiles(out, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700454 if (code_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800455 LOG(ERROR) << "Failed to write oat code for dex files to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700456 return false;
457 }
458
459 return true;
460}
461
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800462bool OatWriter::WriteTables(OutputStream& out) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700463 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800464 if (!oat_dex_files_[i]->Write(out)) {
465 PLOG(ERROR) << "Failed to write oat dex information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700466 return false;
467 }
468 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800469 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
470 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800471 off_t actual_offset = out.Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -0800472 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
473 const DexFile* dex_file = (*dex_files_)[i];
474 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
475 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
476 return false;
477 }
478 const DexFile* dex_file = (*dex_files_)[i];
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800479 if (!out.WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
480 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800481 return false;
482 }
483 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800484 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800485 if (!oat_classes_[i]->Write(out)) {
486 PLOG(ERROR) << "Failed to write oat methods information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700487 return false;
488 }
489 }
490 return true;
491}
492
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800493size_t OatWriter::WriteCode(OutputStream& out) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700494 uint32_t code_offset = oat_header_->GetExecutableOffset();
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800495 off_t new_offset = out.Seek(executable_offset_padding_length_, kSeekCurrent);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700496 if (static_cast<uint32_t>(new_offset) != code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700497 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800498 << " Expected: " << code_offset << " File: " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700499 return 0;
500 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700501 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700502 return code_offset;
503}
504
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800505size_t OatWriter::WriteCodeDexFiles(OutputStream& out, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700506 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800507 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700508 const DexFile* dex_file = (*dex_files_)[i];
509 CHECK(dex_file != NULL);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800510 code_offset = WriteCodeDexFile(out, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700511 if (code_offset == 0) {
512 return 0;
513 }
514 }
515 return code_offset;
516}
517
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800518size_t OatWriter::WriteCodeDexFile(OutputStream& out, size_t code_offset, size_t& oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700519 const DexFile& dex_file) {
520 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
521 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700522 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800523 code_offset = WriteCodeClassDef(out, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700524 if (code_offset == 0) {
525 return 0;
526 }
527 }
528 return code_offset;
529}
530
Ian Rogers0571d352011-11-03 19:51:38 -0700531void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800532 const DexFile& dex_file, OutputStream& out) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700533 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800534 << " to " << out.GetLocation();
Elliott Hughes234da572011-11-03 22:13:06 -0700535}
536
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800537size_t OatWriter::WriteCodeClassDef(OutputStream& out,
Ian Rogers0571d352011-11-03 19:51:38 -0700538 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700539 const DexFile& dex_file,
540 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700541 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700542 if (class_data == NULL) {
543 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700544 return code_offset;
545 }
Ian Rogers0571d352011-11-03 19:51:38 -0700546 ClassDataItemIterator it(dex_file, class_data);
547 // Skip fields
548 while (it.HasNextStaticField()) {
549 it.Next();
550 }
551 while (it.HasNextInstanceField()) {
552 it.Next();
553 }
554 // Process methods
555 size_t class_def_method_index = 0;
556 while (it.HasNextDirectMethod()) {
557 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800558 code_offset = WriteCodeMethod(out, code_offset, oat_class_index, class_def_method_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700559 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700560 if (code_offset == 0) {
561 return 0;
562 }
Ian Rogers0571d352011-11-03 19:51:38 -0700563 class_def_method_index++;
564 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700565 }
Ian Rogers0571d352011-11-03 19:51:38 -0700566 while (it.HasNextVirtualMethod()) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800567 code_offset = WriteCodeMethod(out, code_offset, oat_class_index, class_def_method_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700568 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700569 if (code_offset == 0) {
570 return 0;
571 }
Ian Rogers0571d352011-11-03 19:51:38 -0700572 class_def_method_index++;
573 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700574 }
575 return code_offset;
576}
577
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800578size_t OatWriter::WriteCodeMethod(OutputStream& out, size_t code_offset, size_t oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700579 size_t class_def_method_index, bool is_static,
580 uint32_t method_idx, const DexFile& dex_file) {
581 const CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800582 compiler_->GetCompiledMethod(Compiler::MethodReference(&dex_file, method_idx));
Ian Rogers0571d352011-11-03 19:51:38 -0700583
Ian Rogers0571d352011-11-03 19:51:38 -0700584 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800585 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700586
587
588 if (compiled_method != NULL) { // ie. not an abstract method
Logan Chien971bf3f2012-05-01 15:47:55 +0800589 uint32_t aligned_code_offset = compiled_method->AlignCode(code_offset);
590 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
591 if (aligned_code_delta != 0) {
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800592 off_t new_offset = out.Seek(aligned_code_delta, kSeekCurrent);
Logan Chien971bf3f2012-05-01 15:47:55 +0800593 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
594 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800595 << " Expected: " << aligned_code_offset << " File: " << out.GetLocation();
Logan Chien971bf3f2012-05-01 15:47:55 +0800596 return 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700597 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800598 code_offset += aligned_code_delta;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700599 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700600 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800601 DCHECK_ALIGNED(code_offset, kArmAlignment);
602 const std::vector<uint8_t>& code = compiled_method->GetCode();
603 uint32_t code_size = code.size() * sizeof(code[0]);
604 CHECK_NE(code_size, 0U);
605
606 // Deduplicate code arrays
607 size_t offset = code_offset + sizeof(code_size) + compiled_method->CodeDelta();
608 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
609 if (code_iter != code_offsets_.end() && offset != method_offsets.code_offset_) {
610 DCHECK(code_iter->second == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
611 } else {
612 DCHECK(offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800613 if (!out.WriteFully(&code_size, sizeof(code_size))) {
614 ReportWriteFailure("method code size", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800615 return 0;
616 }
617 code_offset += sizeof(code_size);
618 DCHECK_CODE_OFFSET();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800619 if (!out.WriteFully(&code[0], code_size)) {
620 ReportWriteFailure("method code", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800621 return 0;
622 }
623 code_offset += code_size;
624 }
625 DCHECK_CODE_OFFSET();
626
627 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
628 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
629
630 // Deduplicate mapping tables
631 SafeMap<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
632 mapping_table_offsets_.find(&mapping_table);
633 if (mapping_iter != mapping_table_offsets_.end() &&
634 code_offset != method_offsets.mapping_table_offset_) {
635 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
636 || mapping_iter->second == method_offsets.mapping_table_offset_)
637 << PrettyMethod(method_idx, dex_file);
638 } else {
639 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
640 || code_offset == method_offsets.mapping_table_offset_)
641 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800642 if (!out.WriteFully(&mapping_table[0], mapping_table_size)) {
643 ReportWriteFailure("mapping table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800644 return 0;
645 }
646 code_offset += mapping_table_size;
647 }
648 DCHECK_CODE_OFFSET();
649
650 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
651 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
652
653 // Deduplicate vmap tables
654 SafeMap<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
655 vmap_table_offsets_.find(&vmap_table);
656 if (vmap_iter != vmap_table_offsets_.end() &&
657 code_offset != method_offsets.vmap_table_offset_) {
658 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
659 || vmap_iter->second == method_offsets.vmap_table_offset_)
660 << PrettyMethod(method_idx, dex_file);
661 } else {
662 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
663 || code_offset == method_offsets.vmap_table_offset_)
664 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800665 if (!out.WriteFully(&vmap_table[0], vmap_table_size)) {
666 ReportWriteFailure("vmap table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800667 return 0;
668 }
669 code_offset += vmap_table_size;
670 }
671 DCHECK_CODE_OFFSET();
672
Ian Rogers0c7abda2012-09-19 13:33:42 -0700673 const std::vector<uint8_t>& gc_map = compiled_method->GetNativeGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800674 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
675
676 // Deduplicate GC maps
677 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
678 gc_map_offsets_.find(&gc_map);
679 if (gc_map_iter != gc_map_offsets_.end() &&
680 code_offset != method_offsets.gc_map_offset_) {
681 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
682 || gc_map_iter->second == method_offsets.gc_map_offset_)
683 << PrettyMethod(method_idx, dex_file);
684 } else {
685 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
686 || code_offset == method_offsets.gc_map_offset_)
687 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800688 if (!out.WriteFully(&gc_map[0], gc_map_size)) {
689 ReportWriteFailure("GC map", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800690 return 0;
691 }
692 code_offset += gc_map_size;
693 }
694 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700695 }
Ian Rogers0571d352011-11-03 19:51:38 -0700696 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
697 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700698 if (compiled_invoke_stub != NULL) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800699 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
700 compiler_->GetInstructionSet());
701 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
702 if (aligned_code_delta != 0) {
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800703 off_t new_offset = out.Seek(aligned_code_delta, kSeekCurrent);
Logan Chien971bf3f2012-05-01 15:47:55 +0800704 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
705 PLOG(ERROR) << "Failed to seek to align invoke stub code. Actual: " << new_offset
706 << " Expected: " << aligned_code_offset;
707 return 0;
708 }
709 code_offset += aligned_code_delta;
710 DCHECK_CODE_OFFSET();
711 }
712 DCHECK_ALIGNED(code_offset, kArmAlignment);
713 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
714 uint32_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
715 CHECK_NE(invoke_stub_size, 0U);
716
717 // Deduplicate invoke stubs
718 size_t offset = code_offset + sizeof(invoke_stub_size) + compiled_invoke_stub->CodeDelta();
719 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter =
720 code_offsets_.find(&invoke_stub);
721 if (stub_iter != code_offsets_.end() && offset != method_offsets.invoke_stub_offset_) {
722 DCHECK(stub_iter->second == method_offsets.invoke_stub_offset_) << PrettyMethod(method_idx, dex_file);
723 } else {
724 DCHECK(offset == method_offsets.invoke_stub_offset_) << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800725 if (!out.WriteFully(&invoke_stub_size, sizeof(invoke_stub_size))) {
726 ReportWriteFailure("invoke stub code size", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800727 return 0;
728 }
729 code_offset += sizeof(invoke_stub_size);
730 DCHECK_CODE_OFFSET();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800731 if (!out.WriteFully(&invoke_stub[0], invoke_stub_size)) {
732 ReportWriteFailure("invoke stub code", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800733 return 0;
734 }
735 code_offset += invoke_stub_size;
736 DCHECK_CODE_OFFSET();
737 }
738 }
739
740#if defined(ART_USE_LLVM_COMPILER)
Logan Chien99bc2172012-10-18 10:03:36 +0800741 if (!is_static) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800742 const CompiledInvokeStub* compiled_proxy_stub = compiler_->FindProxyStub(shorty);
743 if (compiled_proxy_stub != NULL) {
Logan Chienccb7bf12012-03-28 12:52:32 +0800744 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
745 compiler_->GetInstructionSet());
746 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
Logan Chien971bf3f2012-05-01 15:47:55 +0800747 CHECK(aligned_code_delta < 48u);
Logan Chienccb7bf12012-03-28 12:52:32 +0800748 if (aligned_code_delta != 0) {
Brian Carlstrom49a0f152013-01-22 17:17:36 -0800749 off_t new_offset = out.Seek(aligned_code_delta, kSeekCurrent);
Logan Chienccb7bf12012-03-28 12:52:32 +0800750 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800751 PLOG(ERROR) << "Failed to seek to align proxy stub code. Actual: " << new_offset
Logan Chienccb7bf12012-03-28 12:52:32 +0800752 << " Expected: " << aligned_code_offset;
753 return 0;
754 }
755 code_offset += aligned_code_delta;
756 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700757 }
Logan Chienccb7bf12012-03-28 12:52:32 +0800758 DCHECK_ALIGNED(code_offset, kArmAlignment);
Logan Chien971bf3f2012-05-01 15:47:55 +0800759 const std::vector<uint8_t>& proxy_stub = compiled_proxy_stub->GetCode();
760 uint32_t proxy_stub_size = proxy_stub.size() * sizeof(proxy_stub[0]);
761 CHECK_NE(proxy_stub_size, 0U);
jeffhao55d78212011-11-02 11:41:50 -0700762
Logan Chien971bf3f2012-05-01 15:47:55 +0800763 // Deduplicate proxy stubs
764 size_t offset = code_offset + sizeof(proxy_stub_size) + compiled_proxy_stub->CodeDelta();
Elliott Hughesa0e18062012-04-13 15:59:59 -0700765 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter =
Logan Chien971bf3f2012-05-01 15:47:55 +0800766 code_offsets_.find(&proxy_stub);
767 if (stub_iter != code_offsets_.end() && offset != method_offsets.proxy_stub_offset_) {
768 DCHECK(stub_iter->second == method_offsets.proxy_stub_offset_) << PrettyMethod(method_idx, dex_file);
Logan Chienccb7bf12012-03-28 12:52:32 +0800769 } else {
Logan Chien971bf3f2012-05-01 15:47:55 +0800770 DCHECK(offset == method_offsets.proxy_stub_offset_) << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800771 if (!out.WriteFully(&proxy_stub_size, sizeof(proxy_stub_size))) {
772 ReportWriteFailure("proxy stub code size", method_idx, dex_file, out);
Logan Chienccb7bf12012-03-28 12:52:32 +0800773 return 0;
774 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800775 code_offset += sizeof(proxy_stub_size);
Logan Chienccb7bf12012-03-28 12:52:32 +0800776 DCHECK_CODE_OFFSET();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800777 if (!out.WriteFully(&proxy_stub[0], proxy_stub_size)) {
778 ReportWriteFailure("proxy stub code", method_idx, dex_file, out);
Logan Chienccb7bf12012-03-28 12:52:32 +0800779 return 0;
780 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800781 code_offset += proxy_stub_size;
782 DCHECK_CODE_OFFSET();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700783 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700784 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700785 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700786 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800787#endif
Logan Chien8b977d32012-02-21 19:14:55 +0800788
Brian Carlstrome24fa612011-09-29 00:53:55 -0700789 return code_offset;
790}
791
Brian Carlstrome24fa612011-09-29 00:53:55 -0700792OatWriter::OatDexFile::OatDexFile(const DexFile& dex_file) {
Elliott Hughes95572412011-12-13 18:14:20 -0800793 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700794 dex_file_location_size_ = location.size();
795 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800796 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800797 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800798 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700799}
800
801size_t OatWriter::OatDexFile::SizeOf() const {
802 return sizeof(dex_file_location_size_)
803 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800804 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800805 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800806 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700807}
808
809void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
810 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
811 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800812 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800813 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800814 oat_header.UpdateChecksum(&methods_offsets_[0],
815 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700816}
817
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800818bool OatWriter::OatDexFile::Write(OutputStream& out) const {
819 if (!out.WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
820 PLOG(ERROR) << "Failed to write dex file location length to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700821 return false;
822 }
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800823 if (!out.WriteFully(dex_file_location_data_, dex_file_location_size_)) {
824 PLOG(ERROR) << "Failed to write dex file location data to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700825 return false;
826 }
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800827 if (!out.WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
828 PLOG(ERROR) << "Failed to write dex file location checksum to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700829 return false;
830 }
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800831 if (!out.WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
832 PLOG(ERROR) << "Failed to write dex file offset to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800833 return false;
834 }
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800835 if (!out.WriteFully(&methods_offsets_[0],
836 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
837 PLOG(ERROR) << "Failed to write methods offsets to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700838 return false;
839 }
840 return true;
841}
842
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800843OatWriter::OatClass::OatClass(mirror::Class::Status status, uint32_t methods_count) {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800844 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700845 method_offsets_.resize(methods_count);
846}
847
Brian Carlstrom389efb02012-01-11 12:06:26 -0800848size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800849 return sizeof(status_)
850 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700851}
852
Brian Carlstrom389efb02012-01-11 12:06:26 -0800853void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800854 oat_header.UpdateChecksum(&status_, sizeof(status_));
855 oat_header.UpdateChecksum(&method_offsets_[0],
856 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700857}
858
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800859bool OatWriter::OatClass::Write(OutputStream& out) const {
860 if (!out.WriteFully(&status_, sizeof(status_))) {
861 PLOG(ERROR) << "Failed to write class status to " << out.GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800862 return false;
863 }
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800864 if (!out.WriteFully(&method_offsets_[0],
865 sizeof(method_offsets_[0]) * method_offsets_.size())) {
866 PLOG(ERROR) << "Failed to write method offsets to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700867 return false;
868 }
869 return true;
870}
871
Brian Carlstrome24fa612011-09-29 00:53:55 -0700872} // namespace art