blob: c5b698ca8bf967b0690804236c5a43fb56639972 [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"
24#include "class_loader.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070025#include "os.h"
Brian Carlstromcd60ac72013-01-20 17:09:51 -080026#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070027#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070028#include "scoped_thread_state_change.h"
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070029#include "gc/space.h"
jeffhaoec014232012-09-05 10:42:25 -070030#include "verifier/method_verifier.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070031
32namespace art {
33
Brian Carlstromcd60ac72013-01-20 17:09:51 -080034bool OatWriter::Create(OutputStream& output_stream,
jeffhao10037c82012-01-23 15:06:23 -080035 const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070036 uint32_t image_file_location_oat_checksum,
37 uint32_t image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070038 const std::string& image_file_location,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070039 const Compiler& compiler) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070040 OatWriter oat_writer(dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070041 image_file_location_oat_checksum,
42 image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070043 image_file_location,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070044 compiler);
Brian Carlstromcd60ac72013-01-20 17:09:51 -080045 return oat_writer.Write(output_stream);
Brian Carlstrome24fa612011-09-29 00:53:55 -070046}
47
Brian Carlstrom3320cf42011-10-04 14:58:28 -070048OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070049 uint32_t image_file_location_oat_checksum,
50 uint32_t image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070051 const std::string& image_file_location,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070052 const Compiler& compiler) {
53 compiler_ = &compiler;
Brian Carlstrom28db0122012-10-18 16:20:41 -070054 image_file_location_oat_checksum_ = image_file_location_oat_checksum;
55 image_file_location_oat_begin_ = image_file_location_oat_begin;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070056 image_file_location_ = image_file_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -070057 dex_files_ = &dex_files;
Ian Rogers0571d352011-11-03 19:51:38 -070058 oat_header_ = NULL;
59 executable_offset_padding_length_ = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -070060
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070061 size_t offset = InitOatHeader();
Brian Carlstrome24fa612011-09-29 00:53:55 -070062 offset = InitOatDexFiles(offset);
Brian Carlstrom89521892011-12-07 22:05:07 -080063 offset = InitDexFiles(offset);
Brian Carlstrom389efb02012-01-11 12:06:26 -080064 offset = InitOatClasses(offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -070065 offset = InitOatCode(offset);
66 offset = InitOatCodeDexFiles(offset);
67
68 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -070069}
70
Ian Rogers0571d352011-11-03 19:51:38 -070071OatWriter::~OatWriter() {
72 delete oat_header_;
73 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -080074 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -070075}
76
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070077size_t OatWriter::InitOatHeader() {
Brian Carlstrome24fa612011-09-29 00:53:55 -070078 // create the OatHeader
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070079 oat_header_ = new OatHeader(compiler_->GetInstructionSet(),
80 dex_files_,
Brian Carlstrom28db0122012-10-18 16:20:41 -070081 image_file_location_oat_checksum_,
82 image_file_location_oat_begin_,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070083 image_file_location_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070084 size_t offset = sizeof(*oat_header_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070085 offset += image_file_location_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -070086 return offset;
87}
88
89size_t OatWriter::InitOatDexFiles(size_t offset) {
90 // create the OatDexFiles
91 for (size_t i = 0; i != dex_files_->size(); ++i) {
92 const DexFile* dex_file = (*dex_files_)[i];
93 CHECK(dex_file != NULL);
94 OatDexFile* oat_dex_file = new OatDexFile(*dex_file);
95 oat_dex_files_.push_back(oat_dex_file);
96 offset += oat_dex_file->SizeOf();
97 }
98 return offset;
99}
100
Brian Carlstrom89521892011-12-07 22:05:07 -0800101size_t OatWriter::InitDexFiles(size_t offset) {
102 // calculate the offsets within OatDexFiles to the DexFiles
103 for (size_t i = 0; i != dex_files_->size(); ++i) {
104 // dex files are required to be 4 byte aligned
105 offset = RoundUp(offset, 4);
106
107 // set offset in OatDexFile to DexFile
108 oat_dex_files_[i]->dex_file_offset_ = offset;
109
110 const DexFile* dex_file = (*dex_files_)[i];
111 offset += dex_file->GetHeader().file_size_;
112 }
113 return offset;
114}
115
Brian Carlstrom389efb02012-01-11 12:06:26 -0800116size_t OatWriter::InitOatClasses(size_t offset) {
117 // create the OatClasses
118 // calculate the offsets within OatDexFiles to OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -0700119 for (size_t i = 0; i != dex_files_->size(); ++i) {
120 const DexFile* dex_file = (*dex_files_)[i];
121 for (size_t class_def_index = 0;
122 class_def_index < dex_file->NumClassDefs();
Ian Rogersc20a83e2012-01-18 18:15:32 -0800123 class_def_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800124 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700125 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
126 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700127 uint32_t num_methods = 0;
128 if (class_data != NULL) { // ie not an empty class, such as a marker interface
129 ClassDataItemIterator it(*dex_file, class_data);
130 size_t num_direct_methods = it.NumDirectMethods();
131 size_t num_virtual_methods = it.NumVirtualMethods();
132 num_methods = num_direct_methods + num_virtual_methods;
133 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800134
jeffhaoec014232012-09-05 10:42:25 -0700135 Compiler::ClassReference class_ref = Compiler::ClassReference(dex_file, class_def_index);
136 CompiledClass* compiled_class = compiler_->GetCompiledClass(class_ref);
137 Class::Status status;
138 if (compiled_class != NULL) {
139 status = compiled_class->GetStatus();
140 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
141 status = Class::kStatusError;
142 } else {
143 status = Class::kStatusNotReady;
144 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800145
146 OatClass* oat_class = new OatClass(status, num_methods);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800147 oat_classes_.push_back(oat_class);
148 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700149 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800150 oat_dex_files_[i]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700151 }
152 return offset;
153}
154
155size_t OatWriter::InitOatCode(size_t offset) {
156 // calculate the offsets within OatHeader to executable code
157 size_t old_offset = offset;
158 // required to be on a new page boundary
159 offset = RoundUp(offset, kPageSize);
160 oat_header_->SetExecutableOffset(offset);
161 executable_offset_padding_length_ = offset - old_offset;
162 return offset;
163}
164
165size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700166 size_t oat_class_index = 0;
167 for (size_t i = 0; i != dex_files_->size(); ++i) {
168 const DexFile* dex_file = (*dex_files_)[i];
169 CHECK(dex_file != NULL);
170 offset = InitOatCodeDexFile(offset, oat_class_index, *dex_file);
171 }
172 return offset;
173}
174
175size_t OatWriter::InitOatCodeDexFile(size_t offset,
176 size_t& oat_class_index,
177 const DexFile& dex_file) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800178 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700179 class_def_index < dex_file.NumClassDefs();
180 class_def_index++, oat_class_index++) {
181 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800182 offset = InitOatCodeClassDef(offset, oat_class_index, class_def_index, dex_file, class_def);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800183 oat_classes_[oat_class_index]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700184 }
185 return offset;
186}
187
188size_t OatWriter::InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800189 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700190 const DexFile& dex_file,
191 const DexFile::ClassDef& class_def) {
192 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700193 if (class_data == NULL) {
194 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700195 return offset;
196 }
Ian Rogers0571d352011-11-03 19:51:38 -0700197 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800198 CHECK_EQ(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700199 it.NumDirectMethods() + it.NumVirtualMethods());
200 // Skip fields
201 while (it.HasNextStaticField()) {
202 it.Next();
203 }
204 while (it.HasNextInstanceField()) {
205 it.Next();
206 }
207 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700208 size_t class_def_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700209 while (it.HasNextDirectMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800210 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700211 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
212 is_native, it.GetMethodInvokeType(class_def), it.GetMemberIndex(),
213 &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700214 class_def_method_index++;
215 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700216 }
Ian Rogers0571d352011-11-03 19:51:38 -0700217 while (it.HasNextVirtualMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800218 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700219 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
220 is_native, it.GetMethodInvokeType(class_def), it.GetMemberIndex(),
221 &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700222 class_def_method_index++;
223 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700224 }
Ian Rogers0571d352011-11-03 19:51:38 -0700225 DCHECK(!it.HasNext());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700226 return offset;
227}
228
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700229size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
230 size_t __attribute__((unused)) class_def_index,
231 size_t class_def_method_index,
232 bool __attribute__((unused)) is_native,
Ian Rogers08f753d2012-08-24 14:35:25 -0700233 InvokeType type,
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700234 uint32_t method_idx, const DexFile* dex_file) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700235 // derived from CompiledMethod if available
236 uint32_t code_offset = 0;
237 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700238 uint32_t core_spill_mask = 0;
239 uint32_t fp_spill_mask = 0;
240 uint32_t mapping_table_offset = 0;
241 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800242 uint32_t gc_map_offset = 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700243 // derived from CompiledInvokeStub if available
244 uint32_t invoke_stub_offset = 0;
Brian Carlstromfd2ec542012-05-02 15:08:57 -0700245#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800246 uint32_t proxy_stub_offset = 0;
Brian Carlstromfd2ec542012-05-02 15:08:57 -0700247#endif
Brian Carlstrome24fa612011-09-29 00:53:55 -0700248
Ian Rogers0571d352011-11-03 19:51:38 -0700249 CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800250 compiler_->GetCompiledMethod(Compiler::MethodReference(dex_file, method_idx));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700251 if (compiled_method != NULL) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800252 offset = compiled_method->AlignCode(offset);
253 DCHECK_ALIGNED(offset, kArmAlignment);
254 const std::vector<uint8_t>& code = compiled_method->GetCode();
255 uint32_t code_size = code.size() * sizeof(code[0]);
256 CHECK_NE(code_size, 0U);
257 uint32_t thumb_offset = compiled_method->CodeDelta();
258 code_offset = offset + sizeof(code_size) + thumb_offset;
259
260 // Deduplicate code arrays
261 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
262 if (code_iter != code_offsets_.end()) {
263 code_offset = code_iter->second;
jeffhao55d78212011-11-02 11:41:50 -0700264 } else {
Logan Chien971bf3f2012-05-01 15:47:55 +0800265 code_offsets_.Put(&code, code_offset);
266 offset += sizeof(code_size); // code size is prepended before code
267 offset += code_size;
268 oat_header_->UpdateChecksum(&code[0], code_size);
269 }
270 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
271 core_spill_mask = compiled_method->GetCoreSpillMask();
272 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700273
Logan Chien971bf3f2012-05-01 15:47:55 +0800274 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
275 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
276 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700277
Logan Chien971bf3f2012-05-01 15:47:55 +0800278 // Deduplicate mapping tables
279 SafeMap<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
280 if (mapping_iter != mapping_table_offsets_.end()) {
281 mapping_table_offset = mapping_iter->second;
282 } else {
283 mapping_table_offsets_.Put(&mapping_table, mapping_table_offset);
284 offset += mapping_table_size;
285 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
286 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700287
Logan Chien971bf3f2012-05-01 15:47:55 +0800288 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
289 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
290 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700291
Logan Chien971bf3f2012-05-01 15:47:55 +0800292 // Deduplicate vmap tables
293 SafeMap<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
294 if (vmap_iter != vmap_table_offsets_.end()) {
295 vmap_table_offset = vmap_iter->second;
296 } else {
297 vmap_table_offsets_.Put(&vmap_table, vmap_table_offset);
298 offset += vmap_table_size;
299 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
300 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800301
Ian Rogers0c7abda2012-09-19 13:33:42 -0700302 const std::vector<uint8_t>& gc_map = compiled_method->GetNativeGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800303 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
304 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
Logan Chienccb7bf12012-03-28 12:52:32 +0800305
TDYa127ce4cc0d2012-11-18 16:59:53 -0800306#if !defined(NDEBUG)
Logan Chien971bf3f2012-05-01 15:47:55 +0800307 // We expect GC maps except when the class hasn't been verified or the method is native
jeffhaoec014232012-09-05 10:42:25 -0700308 Compiler::ClassReference class_ref = Compiler::ClassReference(dex_file, class_def_index);
309 CompiledClass* compiled_class = compiler_->GetCompiledClass(class_ref);
310 Class::Status status;
311 if (compiled_class != NULL) {
312 status = compiled_class->GetStatus();
313 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
314 status = Class::kStatusError;
315 } else {
316 status = Class::kStatusNotReady;
317 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800318 CHECK(gc_map_size != 0 || is_native || status < Class::kStatusVerified)
319 << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " " << (status < Class::kStatusVerified) << " " << status << " " << PrettyMethod(method_idx, *dex_file);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800320#endif
321
Logan Chien971bf3f2012-05-01 15:47:55 +0800322 // Deduplicate GC maps
323 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = gc_map_offsets_.find(&gc_map);
324 if (gc_map_iter != gc_map_offsets_.end()) {
325 gc_map_offset = gc_map_iter->second;
326 } else {
327 gc_map_offsets_.Put(&gc_map, gc_map_offset);
328 offset += gc_map_size;
329 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800330 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700331 }
332
Ian Rogers0571d352011-11-03 19:51:38 -0700333 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx));
Ian Rogers08f753d2012-08-24 14:35:25 -0700334 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(type == kStatic,
335 shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700336 if (compiled_invoke_stub != NULL) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800337 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
338 DCHECK_ALIGNED(offset, kArmAlignment);
339 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
340 uint32_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
341 CHECK_NE(invoke_stub_size, 0U);
342 uint32_t thumb_offset = compiled_invoke_stub->CodeDelta();
343 invoke_stub_offset = offset + sizeof(invoke_stub_size) + thumb_offset;
Logan Chienccb7bf12012-03-28 12:52:32 +0800344
Logan Chien971bf3f2012-05-01 15:47:55 +0800345 // Deduplicate invoke stubs
346 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&invoke_stub);
347 if (stub_iter != code_offsets_.end()) {
348 invoke_stub_offset = stub_iter->second;
349 } else {
350 code_offsets_.Put(&invoke_stub, invoke_stub_offset);
351 offset += sizeof(invoke_stub_size); // invoke stub size is prepended before code
352 offset += invoke_stub_size;
353 oat_header_->UpdateChecksum(&invoke_stub[0], invoke_stub_size);
jeffhao55d78212011-11-02 11:41:50 -0700354 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700355 }
356
Logan Chien7a2a23a2012-06-06 11:01:00 +0800357#if defined(ART_USE_LLVM_COMPILER)
Logan Chien99bc2172012-10-18 10:03:36 +0800358 if (type != kStatic) {
Logan Chien7a2a23a2012-06-06 11:01:00 +0800359 const CompiledInvokeStub* compiled_proxy_stub = compiler_->FindProxyStub(shorty);
360 if (compiled_proxy_stub != NULL) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800361 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
362 DCHECK_ALIGNED(offset, kArmAlignment);
363 const std::vector<uint8_t>& proxy_stub = compiled_proxy_stub->GetCode();
364 uint32_t proxy_stub_size = proxy_stub.size() * sizeof(proxy_stub[0]);
365 CHECK_NE(proxy_stub_size, 0U);
366 uint32_t thumb_offset = compiled_proxy_stub->CodeDelta();
367 proxy_stub_offset = offset + sizeof(proxy_stub_size) + thumb_offset;
368
369 // Deduplicate proxy stubs
370 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&proxy_stub);
371 if (stub_iter != code_offsets_.end()) {
372 proxy_stub_offset = stub_iter->second;
373 } else {
374 code_offsets_.Put(&proxy_stub, proxy_stub_offset);
375 offset += sizeof(proxy_stub_size); // proxy stub size is prepended before code
376 offset += proxy_stub_size;
377 oat_header_->UpdateChecksum(&proxy_stub[0], proxy_stub_size);
378 }
Logan Chien7a2a23a2012-06-06 11:01:00 +0800379 }
380 }
381#endif
382
Brian Carlstrom389efb02012-01-11 12:06:26 -0800383 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index]
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700384 = OatMethodOffsets(code_offset,
385 frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700386 core_spill_mask,
387 fp_spill_mask,
388 mapping_table_offset,
389 vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800390 gc_map_offset,
Logan Chienccb7bf12012-03-28 12:52:32 +0800391 invoke_stub_offset
392#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800393 , proxy_stub_offset
Logan Chienccb7bf12012-03-28 12:52:32 +0800394#endif
395 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700396
Ian Rogers0571d352011-11-03 19:51:38 -0700397 if (compiler_->IsImage()) {
398 ClassLinker* linker = Runtime::Current()->GetClassLinker();
399 DexCache* dex_cache = linker->FindDexCache(*dex_file);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700400 // Unchecked as we hold mutator_lock_ on entry.
401 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700402 AbstractMethod* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache,
Brian Carlstromff2d2c52012-10-30 14:15:34 -0700403 NULL, NULL, type);
Ian Rogers0571d352011-11-03 19:51:38 -0700404 CHECK(method != NULL);
405 method->SetFrameSizeInBytes(frame_size_in_bytes);
406 method->SetCoreSpillMask(core_spill_mask);
407 method->SetFpSpillMask(fp_spill_mask);
408 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800409 // Don't overwrite static method trampoline
410 if (!method->IsStatic() || method->IsConstructor() ||
411 method->GetDeclaringClass()->IsInitialized()) {
412 method->SetOatCodeOffset(code_offset);
413 } else {
414 method->SetCode(Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod)->GetData());
415 }
Ian Rogers0571d352011-11-03 19:51:38 -0700416 method->SetOatVmapTableOffset(vmap_table_offset);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700417 method->SetOatNativeGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700418 method->SetOatInvokeStubOffset(invoke_stub_offset);
419 }
Logan Chien8b977d32012-02-21 19:14:55 +0800420
Brian Carlstrome24fa612011-09-29 00:53:55 -0700421 return offset;
422}
423
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700424#define DCHECK_CODE_OFFSET() \
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800425 DCHECK_EQ(static_cast<off_t>(code_offset), out.lseek(0, SEEK_CUR))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700426
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800427bool OatWriter::Write(OutputStream& out) {
428 if (!out.WriteFully(oat_header_, sizeof(*oat_header_))) {
429 PLOG(ERROR) << "Failed to write oat header to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700430 return false;
431 }
432
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800433 if (!out.WriteFully(image_file_location_.data(), image_file_location_.size())) {
434 PLOG(ERROR) << "Failed to write oat header image file location to " << out.GetLocation();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700435 return false;
436 }
437
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800438 if (!WriteTables(out)) {
439 LOG(ERROR) << "Failed to write oat tables to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700440 return false;
441 }
442
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800443 size_t code_offset = WriteCode(out);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700444 if (code_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800445 LOG(ERROR) << "Failed to write oat code to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700446 return false;
447 }
448
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800449 code_offset = WriteCodeDexFiles(out, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700450 if (code_offset == 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800451 LOG(ERROR) << "Failed to write oat code for dex files to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700452 return false;
453 }
454
455 return true;
456}
457
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800458bool OatWriter::WriteTables(OutputStream& out) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700459 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800460 if (!oat_dex_files_[i]->Write(out)) {
461 PLOG(ERROR) << "Failed to write oat dex information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700462 return false;
463 }
464 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800465 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
466 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800467 off_t actual_offset = out.lseek(expected_offset, SEEK_SET);
Brian Carlstrom89521892011-12-07 22:05:07 -0800468 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
469 const DexFile* dex_file = (*dex_files_)[i];
470 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
471 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
472 return false;
473 }
474 const DexFile* dex_file = (*dex_files_)[i];
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800475 if (!out.WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
476 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800477 return false;
478 }
479 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800480 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800481 if (!oat_classes_[i]->Write(out)) {
482 PLOG(ERROR) << "Failed to write oat methods information to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700483 return false;
484 }
485 }
486 return true;
487}
488
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800489size_t OatWriter::WriteCode(OutputStream& out) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700490 uint32_t code_offset = oat_header_->GetExecutableOffset();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800491 off_t new_offset = out.lseek(executable_offset_padding_length_, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700492 if (static_cast<uint32_t>(new_offset) != code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700493 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800494 << " Expected: " << code_offset << " File: " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700495 return 0;
496 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700497 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700498 return code_offset;
499}
500
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800501size_t OatWriter::WriteCodeDexFiles(OutputStream& out, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700502 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800503 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700504 const DexFile* dex_file = (*dex_files_)[i];
505 CHECK(dex_file != NULL);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800506 code_offset = WriteCodeDexFile(out, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700507 if (code_offset == 0) {
508 return 0;
509 }
510 }
511 return code_offset;
512}
513
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800514size_t OatWriter::WriteCodeDexFile(OutputStream& out, size_t code_offset, size_t& oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700515 const DexFile& dex_file) {
516 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
517 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700518 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800519 code_offset = WriteCodeClassDef(out, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700520 if (code_offset == 0) {
521 return 0;
522 }
523 }
524 return code_offset;
525}
526
Ian Rogers0571d352011-11-03 19:51:38 -0700527void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800528 const DexFile& dex_file, OutputStream& out) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700529 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800530 << " to " << out.GetLocation();
Elliott Hughes234da572011-11-03 22:13:06 -0700531}
532
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800533size_t OatWriter::WriteCodeClassDef(OutputStream& out,
Ian Rogers0571d352011-11-03 19:51:38 -0700534 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700535 const DexFile& dex_file,
536 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700537 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700538 if (class_data == NULL) {
539 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700540 return code_offset;
541 }
Ian Rogers0571d352011-11-03 19:51:38 -0700542 ClassDataItemIterator it(dex_file, class_data);
543 // Skip fields
544 while (it.HasNextStaticField()) {
545 it.Next();
546 }
547 while (it.HasNextInstanceField()) {
548 it.Next();
549 }
550 // Process methods
551 size_t class_def_method_index = 0;
552 while (it.HasNextDirectMethod()) {
553 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800554 code_offset = WriteCodeMethod(out, code_offset, oat_class_index, class_def_method_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700555 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700556 if (code_offset == 0) {
557 return 0;
558 }
Ian Rogers0571d352011-11-03 19:51:38 -0700559 class_def_method_index++;
560 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700561 }
Ian Rogers0571d352011-11-03 19:51:38 -0700562 while (it.HasNextVirtualMethod()) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800563 code_offset = WriteCodeMethod(out, code_offset, oat_class_index, class_def_method_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700564 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700565 if (code_offset == 0) {
566 return 0;
567 }
Ian Rogers0571d352011-11-03 19:51:38 -0700568 class_def_method_index++;
569 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700570 }
571 return code_offset;
572}
573
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800574size_t OatWriter::WriteCodeMethod(OutputStream& out, size_t code_offset, size_t oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700575 size_t class_def_method_index, bool is_static,
576 uint32_t method_idx, const DexFile& dex_file) {
577 const CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800578 compiler_->GetCompiledMethod(Compiler::MethodReference(&dex_file, method_idx));
Ian Rogers0571d352011-11-03 19:51:38 -0700579
Ian Rogers0571d352011-11-03 19:51:38 -0700580 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800581 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700582
583
584 if (compiled_method != NULL) { // ie. not an abstract method
Logan Chien971bf3f2012-05-01 15:47:55 +0800585 uint32_t aligned_code_offset = compiled_method->AlignCode(code_offset);
586 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
587 if (aligned_code_delta != 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800588 off_t new_offset = out.lseek(aligned_code_delta, SEEK_CUR);
Logan Chien971bf3f2012-05-01 15:47:55 +0800589 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
590 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800591 << " Expected: " << aligned_code_offset << " File: " << out.GetLocation();
Logan Chien971bf3f2012-05-01 15:47:55 +0800592 return 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700593 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800594 code_offset += aligned_code_delta;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700595 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700596 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800597 DCHECK_ALIGNED(code_offset, kArmAlignment);
598 const std::vector<uint8_t>& code = compiled_method->GetCode();
599 uint32_t code_size = code.size() * sizeof(code[0]);
600 CHECK_NE(code_size, 0U);
601
602 // Deduplicate code arrays
603 size_t offset = code_offset + sizeof(code_size) + compiled_method->CodeDelta();
604 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
605 if (code_iter != code_offsets_.end() && offset != method_offsets.code_offset_) {
606 DCHECK(code_iter->second == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
607 } else {
608 DCHECK(offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800609 if (!out.WriteFully(&code_size, sizeof(code_size))) {
610 ReportWriteFailure("method code size", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800611 return 0;
612 }
613 code_offset += sizeof(code_size);
614 DCHECK_CODE_OFFSET();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800615 if (!out.WriteFully(&code[0], code_size)) {
616 ReportWriteFailure("method code", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800617 return 0;
618 }
619 code_offset += code_size;
620 }
621 DCHECK_CODE_OFFSET();
622
623 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
624 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
625
626 // Deduplicate mapping tables
627 SafeMap<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
628 mapping_table_offsets_.find(&mapping_table);
629 if (mapping_iter != mapping_table_offsets_.end() &&
630 code_offset != method_offsets.mapping_table_offset_) {
631 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
632 || mapping_iter->second == method_offsets.mapping_table_offset_)
633 << PrettyMethod(method_idx, dex_file);
634 } else {
635 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
636 || code_offset == method_offsets.mapping_table_offset_)
637 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800638 if (!out.WriteFully(&mapping_table[0], mapping_table_size)) {
639 ReportWriteFailure("mapping table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800640 return 0;
641 }
642 code_offset += mapping_table_size;
643 }
644 DCHECK_CODE_OFFSET();
645
646 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
647 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
648
649 // Deduplicate vmap tables
650 SafeMap<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
651 vmap_table_offsets_.find(&vmap_table);
652 if (vmap_iter != vmap_table_offsets_.end() &&
653 code_offset != method_offsets.vmap_table_offset_) {
654 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
655 || vmap_iter->second == method_offsets.vmap_table_offset_)
656 << PrettyMethod(method_idx, dex_file);
657 } else {
658 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
659 || code_offset == method_offsets.vmap_table_offset_)
660 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800661 if (!out.WriteFully(&vmap_table[0], vmap_table_size)) {
662 ReportWriteFailure("vmap table", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800663 return 0;
664 }
665 code_offset += vmap_table_size;
666 }
667 DCHECK_CODE_OFFSET();
668
Ian Rogers0c7abda2012-09-19 13:33:42 -0700669 const std::vector<uint8_t>& gc_map = compiled_method->GetNativeGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800670 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
671
672 // Deduplicate GC maps
673 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
674 gc_map_offsets_.find(&gc_map);
675 if (gc_map_iter != gc_map_offsets_.end() &&
676 code_offset != method_offsets.gc_map_offset_) {
677 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
678 || gc_map_iter->second == method_offsets.gc_map_offset_)
679 << PrettyMethod(method_idx, dex_file);
680 } else {
681 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
682 || code_offset == method_offsets.gc_map_offset_)
683 << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800684 if (!out.WriteFully(&gc_map[0], gc_map_size)) {
685 ReportWriteFailure("GC map", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800686 return 0;
687 }
688 code_offset += gc_map_size;
689 }
690 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700691 }
Ian Rogers0571d352011-11-03 19:51:38 -0700692 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
693 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700694 if (compiled_invoke_stub != NULL) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800695 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
696 compiler_->GetInstructionSet());
697 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
698 if (aligned_code_delta != 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800699 off_t new_offset = out.lseek(aligned_code_delta, SEEK_CUR);
Logan Chien971bf3f2012-05-01 15:47:55 +0800700 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
701 PLOG(ERROR) << "Failed to seek to align invoke stub code. Actual: " << new_offset
702 << " Expected: " << aligned_code_offset;
703 return 0;
704 }
705 code_offset += aligned_code_delta;
706 DCHECK_CODE_OFFSET();
707 }
708 DCHECK_ALIGNED(code_offset, kArmAlignment);
709 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
710 uint32_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
711 CHECK_NE(invoke_stub_size, 0U);
712
713 // Deduplicate invoke stubs
714 size_t offset = code_offset + sizeof(invoke_stub_size) + compiled_invoke_stub->CodeDelta();
715 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter =
716 code_offsets_.find(&invoke_stub);
717 if (stub_iter != code_offsets_.end() && offset != method_offsets.invoke_stub_offset_) {
718 DCHECK(stub_iter->second == method_offsets.invoke_stub_offset_) << PrettyMethod(method_idx, dex_file);
719 } else {
720 DCHECK(offset == method_offsets.invoke_stub_offset_) << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800721 if (!out.WriteFully(&invoke_stub_size, sizeof(invoke_stub_size))) {
722 ReportWriteFailure("invoke stub code size", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800723 return 0;
724 }
725 code_offset += sizeof(invoke_stub_size);
726 DCHECK_CODE_OFFSET();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800727 if (!out.WriteFully(&invoke_stub[0], invoke_stub_size)) {
728 ReportWriteFailure("invoke stub code", method_idx, dex_file, out);
Logan Chien971bf3f2012-05-01 15:47:55 +0800729 return 0;
730 }
731 code_offset += invoke_stub_size;
732 DCHECK_CODE_OFFSET();
733 }
734 }
735
736#if defined(ART_USE_LLVM_COMPILER)
Logan Chien99bc2172012-10-18 10:03:36 +0800737 if (!is_static) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800738 const CompiledInvokeStub* compiled_proxy_stub = compiler_->FindProxyStub(shorty);
739 if (compiled_proxy_stub != NULL) {
Logan Chienccb7bf12012-03-28 12:52:32 +0800740 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
741 compiler_->GetInstructionSet());
742 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
Logan Chien971bf3f2012-05-01 15:47:55 +0800743 CHECK(aligned_code_delta < 48u);
Logan Chienccb7bf12012-03-28 12:52:32 +0800744 if (aligned_code_delta != 0) {
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800745 off_t new_offset = out.lseek(aligned_code_delta, SEEK_CUR);
Logan Chienccb7bf12012-03-28 12:52:32 +0800746 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800747 PLOG(ERROR) << "Failed to seek to align proxy stub code. Actual: " << new_offset
Logan Chienccb7bf12012-03-28 12:52:32 +0800748 << " Expected: " << aligned_code_offset;
749 return 0;
750 }
751 code_offset += aligned_code_delta;
752 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700753 }
Logan Chienccb7bf12012-03-28 12:52:32 +0800754 DCHECK_ALIGNED(code_offset, kArmAlignment);
Logan Chien971bf3f2012-05-01 15:47:55 +0800755 const std::vector<uint8_t>& proxy_stub = compiled_proxy_stub->GetCode();
756 uint32_t proxy_stub_size = proxy_stub.size() * sizeof(proxy_stub[0]);
757 CHECK_NE(proxy_stub_size, 0U);
jeffhao55d78212011-11-02 11:41:50 -0700758
Logan Chien971bf3f2012-05-01 15:47:55 +0800759 // Deduplicate proxy stubs
760 size_t offset = code_offset + sizeof(proxy_stub_size) + compiled_proxy_stub->CodeDelta();
Elliott Hughesa0e18062012-04-13 15:59:59 -0700761 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter =
Logan Chien971bf3f2012-05-01 15:47:55 +0800762 code_offsets_.find(&proxy_stub);
763 if (stub_iter != code_offsets_.end() && offset != method_offsets.proxy_stub_offset_) {
764 DCHECK(stub_iter->second == method_offsets.proxy_stub_offset_) << PrettyMethod(method_idx, dex_file);
Logan Chienccb7bf12012-03-28 12:52:32 +0800765 } else {
Logan Chien971bf3f2012-05-01 15:47:55 +0800766 DCHECK(offset == method_offsets.proxy_stub_offset_) << PrettyMethod(method_idx, dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800767 if (!out.WriteFully(&proxy_stub_size, sizeof(proxy_stub_size))) {
768 ReportWriteFailure("proxy stub code size", method_idx, dex_file, out);
Logan Chienccb7bf12012-03-28 12:52:32 +0800769 return 0;
770 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800771 code_offset += sizeof(proxy_stub_size);
Logan Chienccb7bf12012-03-28 12:52:32 +0800772 DCHECK_CODE_OFFSET();
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800773 if (!out.WriteFully(&proxy_stub[0], proxy_stub_size)) {
774 ReportWriteFailure("proxy stub code", method_idx, dex_file, out);
Logan Chienccb7bf12012-03-28 12:52:32 +0800775 return 0;
776 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800777 code_offset += proxy_stub_size;
778 DCHECK_CODE_OFFSET();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700779 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700780 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700781 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700782 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800783#endif
Logan Chien8b977d32012-02-21 19:14:55 +0800784
Brian Carlstrome24fa612011-09-29 00:53:55 -0700785 return code_offset;
786}
787
Brian Carlstrome24fa612011-09-29 00:53:55 -0700788OatWriter::OatDexFile::OatDexFile(const DexFile& dex_file) {
Elliott Hughes95572412011-12-13 18:14:20 -0800789 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700790 dex_file_location_size_ = location.size();
791 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800792 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800793 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800794 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700795}
796
797size_t OatWriter::OatDexFile::SizeOf() const {
798 return sizeof(dex_file_location_size_)
799 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800800 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800801 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800802 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700803}
804
805void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
806 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
807 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800808 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800809 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800810 oat_header.UpdateChecksum(&methods_offsets_[0],
811 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700812}
813
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800814bool OatWriter::OatDexFile::Write(OutputStream& out) const {
815 if (!out.WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
816 PLOG(ERROR) << "Failed to write dex file location length to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700817 return false;
818 }
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800819 if (!out.WriteFully(dex_file_location_data_, dex_file_location_size_)) {
820 PLOG(ERROR) << "Failed to write dex file location data 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_checksum_, sizeof(dex_file_location_checksum_))) {
824 PLOG(ERROR) << "Failed to write dex file location checksum 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_offset_, sizeof(dex_file_offset_))) {
828 PLOG(ERROR) << "Failed to write dex file offset to " << out.GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800829 return false;
830 }
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800831 if (!out.WriteFully(&methods_offsets_[0],
832 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
833 PLOG(ERROR) << "Failed to write methods offsets to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700834 return false;
835 }
836 return true;
837}
838
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800839OatWriter::OatClass::OatClass(Class::Status status, uint32_t methods_count) {
840 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700841 method_offsets_.resize(methods_count);
842}
843
Brian Carlstrom389efb02012-01-11 12:06:26 -0800844size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800845 return sizeof(status_)
846 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700847}
848
Brian Carlstrom389efb02012-01-11 12:06:26 -0800849void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800850 oat_header.UpdateChecksum(&status_, sizeof(status_));
851 oat_header.UpdateChecksum(&method_offsets_[0],
852 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700853}
854
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800855bool OatWriter::OatClass::Write(OutputStream& out) const {
856 if (!out.WriteFully(&status_, sizeof(status_))) {
857 PLOG(ERROR) << "Failed to write class status to " << out.GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800858 return false;
859 }
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800860 if (!out.WriteFully(&method_offsets_[0],
861 sizeof(method_offsets_[0]) * method_offsets_.size())) {
862 PLOG(ERROR) << "Failed to write method offsets to " << out.GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700863 return false;
864 }
865 return true;
866}
867
Brian Carlstrome24fa612011-09-29 00:53:55 -0700868} // namespace art