blob: 49d4e253bd78276232965fbfee63ae7b2e6ebd50 [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 Hughes76160052012-12-12 16:31:20 -080021#include "base/unix_file/fd_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070022#include "class_linker.h"
23#include "class_loader.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070024#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070025#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070026#include "scoped_thread_state_change.h"
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070027#include "gc/space.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070028#include "stl_util.h"
jeffhaoec014232012-09-05 10:42:25 -070029#include "verifier/method_verifier.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070030
31namespace art {
32
Elliott Hughes234da572011-11-03 22:13:06 -070033bool OatWriter::Create(File* file,
jeffhao10037c82012-01-23 15:06:23 -080034 const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070035 uint32_t image_file_location_oat_checksum,
36 uint32_t image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070037 const std::string& image_file_location,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070038 const Compiler& compiler) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070039 OatWriter oat_writer(dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070040 image_file_location_oat_checksum,
41 image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070042 image_file_location,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070043 compiler);
Elliott Hughes234da572011-11-03 22:13:06 -070044 return oat_writer.Write(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -070045}
46
Brian Carlstrom3320cf42011-10-04 14:58:28 -070047OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070048 uint32_t image_file_location_oat_checksum,
49 uint32_t image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070050 const std::string& image_file_location,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070051 const Compiler& compiler) {
52 compiler_ = &compiler;
Brian Carlstrom28db0122012-10-18 16:20:41 -070053 image_file_location_oat_checksum_ = image_file_location_oat_checksum;
54 image_file_location_oat_begin_ = image_file_location_oat_begin;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070055 image_file_location_ = image_file_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -070056 dex_files_ = &dex_files;
Ian Rogers0571d352011-11-03 19:51:38 -070057 oat_header_ = NULL;
58 executable_offset_padding_length_ = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -070059
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070060 size_t offset = InitOatHeader();
Brian Carlstrome24fa612011-09-29 00:53:55 -070061 offset = InitOatDexFiles(offset);
Brian Carlstrom89521892011-12-07 22:05:07 -080062 offset = InitDexFiles(offset);
Brian Carlstrom389efb02012-01-11 12:06:26 -080063 offset = InitOatClasses(offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -070064 offset = InitOatCode(offset);
65 offset = InitOatCodeDexFiles(offset);
66
67 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -070068}
69
Ian Rogers0571d352011-11-03 19:51:38 -070070OatWriter::~OatWriter() {
71 delete oat_header_;
72 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -080073 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -070074}
75
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070076size_t OatWriter::InitOatHeader() {
Brian Carlstrome24fa612011-09-29 00:53:55 -070077 // create the OatHeader
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070078 oat_header_ = new OatHeader(compiler_->GetInstructionSet(),
79 dex_files_,
Brian Carlstrom28db0122012-10-18 16:20:41 -070080 image_file_location_oat_checksum_,
81 image_file_location_oat_begin_,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070082 image_file_location_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070083 size_t offset = sizeof(*oat_header_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070084 offset += image_file_location_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -070085 return offset;
86}
87
88size_t OatWriter::InitOatDexFiles(size_t offset) {
89 // create the OatDexFiles
90 for (size_t i = 0; i != dex_files_->size(); ++i) {
91 const DexFile* dex_file = (*dex_files_)[i];
92 CHECK(dex_file != NULL);
93 OatDexFile* oat_dex_file = new OatDexFile(*dex_file);
94 oat_dex_files_.push_back(oat_dex_file);
95 offset += oat_dex_file->SizeOf();
96 }
97 return offset;
98}
99
Brian Carlstrom89521892011-12-07 22:05:07 -0800100size_t OatWriter::InitDexFiles(size_t offset) {
101 // calculate the offsets within OatDexFiles to the DexFiles
102 for (size_t i = 0; i != dex_files_->size(); ++i) {
103 // dex files are required to be 4 byte aligned
104 offset = RoundUp(offset, 4);
105
106 // set offset in OatDexFile to DexFile
107 oat_dex_files_[i]->dex_file_offset_ = offset;
108
109 const DexFile* dex_file = (*dex_files_)[i];
110 offset += dex_file->GetHeader().file_size_;
111 }
112 return offset;
113}
114
Brian Carlstrom389efb02012-01-11 12:06:26 -0800115size_t OatWriter::InitOatClasses(size_t offset) {
116 // create the OatClasses
117 // calculate the offsets within OatDexFiles to OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -0700118 for (size_t i = 0; i != dex_files_->size(); ++i) {
119 const DexFile* dex_file = (*dex_files_)[i];
120 for (size_t class_def_index = 0;
121 class_def_index < dex_file->NumClassDefs();
Ian Rogersc20a83e2012-01-18 18:15:32 -0800122 class_def_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800123 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700124 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
125 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700126 uint32_t num_methods = 0;
127 if (class_data != NULL) { // ie not an empty class, such as a marker interface
128 ClassDataItemIterator it(*dex_file, class_data);
129 size_t num_direct_methods = it.NumDirectMethods();
130 size_t num_virtual_methods = it.NumVirtualMethods();
131 num_methods = num_direct_methods + num_virtual_methods;
132 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800133
jeffhaoec014232012-09-05 10:42:25 -0700134 Compiler::ClassReference class_ref = Compiler::ClassReference(dex_file, class_def_index);
135 CompiledClass* compiled_class = compiler_->GetCompiledClass(class_ref);
136 Class::Status status;
137 if (compiled_class != NULL) {
138 status = compiled_class->GetStatus();
139 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
140 status = Class::kStatusError;
141 } else {
142 status = Class::kStatusNotReady;
143 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800144
145 OatClass* oat_class = new OatClass(status, num_methods);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800146 oat_classes_.push_back(oat_class);
147 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700148 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800149 oat_dex_files_[i]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700150 }
151 return offset;
152}
153
154size_t OatWriter::InitOatCode(size_t offset) {
155 // calculate the offsets within OatHeader to executable code
156 size_t old_offset = offset;
157 // required to be on a new page boundary
158 offset = RoundUp(offset, kPageSize);
159 oat_header_->SetExecutableOffset(offset);
160 executable_offset_padding_length_ = offset - old_offset;
161 return offset;
162}
163
164size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700165 size_t oat_class_index = 0;
166 for (size_t i = 0; i != dex_files_->size(); ++i) {
167 const DexFile* dex_file = (*dex_files_)[i];
168 CHECK(dex_file != NULL);
169 offset = InitOatCodeDexFile(offset, oat_class_index, *dex_file);
170 }
171 return offset;
172}
173
174size_t OatWriter::InitOatCodeDexFile(size_t offset,
175 size_t& oat_class_index,
176 const DexFile& dex_file) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800177 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700178 class_def_index < dex_file.NumClassDefs();
179 class_def_index++, oat_class_index++) {
180 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800181 offset = InitOatCodeClassDef(offset, oat_class_index, class_def_index, dex_file, class_def);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800182 oat_classes_[oat_class_index]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700183 }
184 return offset;
185}
186
187size_t OatWriter::InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800188 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700189 const DexFile& dex_file,
190 const DexFile::ClassDef& class_def) {
191 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700192 if (class_data == NULL) {
193 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700194 return offset;
195 }
Ian Rogers0571d352011-11-03 19:51:38 -0700196 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800197 CHECK_EQ(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700198 it.NumDirectMethods() + it.NumVirtualMethods());
199 // Skip fields
200 while (it.HasNextStaticField()) {
201 it.Next();
202 }
203 while (it.HasNextInstanceField()) {
204 it.Next();
205 }
206 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700207 size_t class_def_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700208 while (it.HasNextDirectMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800209 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700210 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
211 is_native, it.GetMethodInvokeType(class_def), it.GetMemberIndex(),
212 &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700213 class_def_method_index++;
214 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700215 }
Ian Rogers0571d352011-11-03 19:51:38 -0700216 while (it.HasNextVirtualMethod()) {
Ian Rogersc20a83e2012-01-18 18:15:32 -0800217 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
Ian Rogers08f753d2012-08-24 14:35:25 -0700218 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index,
219 is_native, it.GetMethodInvokeType(class_def), it.GetMemberIndex(),
220 &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700221 class_def_method_index++;
222 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700223 }
Ian Rogers0571d352011-11-03 19:51:38 -0700224 DCHECK(!it.HasNext());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700225 return offset;
226}
227
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700228size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
229 size_t __attribute__((unused)) class_def_index,
230 size_t class_def_method_index,
231 bool __attribute__((unused)) is_native,
Ian Rogers08f753d2012-08-24 14:35:25 -0700232 InvokeType type,
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700233 uint32_t method_idx, const DexFile* dex_file) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700234 // derived from CompiledMethod if available
235 uint32_t code_offset = 0;
236 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700237 uint32_t core_spill_mask = 0;
238 uint32_t fp_spill_mask = 0;
239 uint32_t mapping_table_offset = 0;
240 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800241 uint32_t gc_map_offset = 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700242 // derived from CompiledInvokeStub if available
243 uint32_t invoke_stub_offset = 0;
Brian Carlstromfd2ec542012-05-02 15:08:57 -0700244#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800245 uint32_t proxy_stub_offset = 0;
Brian Carlstromfd2ec542012-05-02 15:08:57 -0700246#endif
Brian Carlstrome24fa612011-09-29 00:53:55 -0700247
Ian Rogers0571d352011-11-03 19:51:38 -0700248 CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800249 compiler_->GetCompiledMethod(Compiler::MethodReference(dex_file, method_idx));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700250 if (compiled_method != NULL) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800251 offset = compiled_method->AlignCode(offset);
252 DCHECK_ALIGNED(offset, kArmAlignment);
253 const std::vector<uint8_t>& code = compiled_method->GetCode();
254 uint32_t code_size = code.size() * sizeof(code[0]);
255 CHECK_NE(code_size, 0U);
256 uint32_t thumb_offset = compiled_method->CodeDelta();
257 code_offset = offset + sizeof(code_size) + thumb_offset;
258
259 // Deduplicate code arrays
260 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
261 if (code_iter != code_offsets_.end()) {
262 code_offset = code_iter->second;
jeffhao55d78212011-11-02 11:41:50 -0700263 } else {
Logan Chien971bf3f2012-05-01 15:47:55 +0800264 code_offsets_.Put(&code, code_offset);
265 offset += sizeof(code_size); // code size is prepended before code
266 offset += code_size;
267 oat_header_->UpdateChecksum(&code[0], code_size);
268 }
269 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
270 core_spill_mask = compiled_method->GetCoreSpillMask();
271 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700272
Logan Chien971bf3f2012-05-01 15:47:55 +0800273 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
274 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
275 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700276
Logan Chien971bf3f2012-05-01 15:47:55 +0800277 // Deduplicate mapping tables
278 SafeMap<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
279 if (mapping_iter != mapping_table_offsets_.end()) {
280 mapping_table_offset = mapping_iter->second;
281 } else {
282 mapping_table_offsets_.Put(&mapping_table, mapping_table_offset);
283 offset += mapping_table_size;
284 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
285 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700286
Logan Chien971bf3f2012-05-01 15:47:55 +0800287 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
288 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
289 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700290
Logan Chien971bf3f2012-05-01 15:47:55 +0800291 // Deduplicate vmap tables
292 SafeMap<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
293 if (vmap_iter != vmap_table_offsets_.end()) {
294 vmap_table_offset = vmap_iter->second;
295 } else {
296 vmap_table_offsets_.Put(&vmap_table, vmap_table_offset);
297 offset += vmap_table_size;
298 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
299 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800300
Ian Rogers0c7abda2012-09-19 13:33:42 -0700301 const std::vector<uint8_t>& gc_map = compiled_method->GetNativeGcMap();
Logan Chien971bf3f2012-05-01 15:47:55 +0800302 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
303 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
Logan Chienccb7bf12012-03-28 12:52:32 +0800304
TDYa127ce4cc0d2012-11-18 16:59:53 -0800305#if !defined(NDEBUG)
Logan Chien971bf3f2012-05-01 15:47:55 +0800306 // We expect GC maps except when the class hasn't been verified or the method is native
jeffhaoec014232012-09-05 10:42:25 -0700307 Compiler::ClassReference class_ref = Compiler::ClassReference(dex_file, class_def_index);
308 CompiledClass* compiled_class = compiler_->GetCompiledClass(class_ref);
309 Class::Status status;
310 if (compiled_class != NULL) {
311 status = compiled_class->GetStatus();
312 } else if (verifier::MethodVerifier::IsClassRejected(class_ref)) {
313 status = Class::kStatusError;
314 } else {
315 status = Class::kStatusNotReady;
316 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800317 CHECK(gc_map_size != 0 || is_native || status < Class::kStatusVerified)
318 << &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 -0800319#endif
320
Logan Chien971bf3f2012-05-01 15:47:55 +0800321 // Deduplicate GC maps
322 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = gc_map_offsets_.find(&gc_map);
323 if (gc_map_iter != gc_map_offsets_.end()) {
324 gc_map_offset = gc_map_iter->second;
325 } else {
326 gc_map_offsets_.Put(&gc_map, gc_map_offset);
327 offset += gc_map_size;
328 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800329 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700330 }
331
Ian Rogers0571d352011-11-03 19:51:38 -0700332 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx));
Ian Rogers08f753d2012-08-24 14:35:25 -0700333 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(type == kStatic,
334 shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700335 if (compiled_invoke_stub != NULL) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800336 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
337 DCHECK_ALIGNED(offset, kArmAlignment);
338 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
339 uint32_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
340 CHECK_NE(invoke_stub_size, 0U);
341 uint32_t thumb_offset = compiled_invoke_stub->CodeDelta();
342 invoke_stub_offset = offset + sizeof(invoke_stub_size) + thumb_offset;
Logan Chienccb7bf12012-03-28 12:52:32 +0800343
Logan Chien971bf3f2012-05-01 15:47:55 +0800344 // Deduplicate invoke stubs
345 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&invoke_stub);
346 if (stub_iter != code_offsets_.end()) {
347 invoke_stub_offset = stub_iter->second;
348 } else {
349 code_offsets_.Put(&invoke_stub, invoke_stub_offset);
350 offset += sizeof(invoke_stub_size); // invoke stub size is prepended before code
351 offset += invoke_stub_size;
352 oat_header_->UpdateChecksum(&invoke_stub[0], invoke_stub_size);
jeffhao55d78212011-11-02 11:41:50 -0700353 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700354 }
355
Logan Chien7a2a23a2012-06-06 11:01:00 +0800356#if defined(ART_USE_LLVM_COMPILER)
Logan Chien99bc2172012-10-18 10:03:36 +0800357 if (type != kStatic) {
Logan Chien7a2a23a2012-06-06 11:01:00 +0800358 const CompiledInvokeStub* compiled_proxy_stub = compiler_->FindProxyStub(shorty);
359 if (compiled_proxy_stub != NULL) {
Logan Chien971bf3f2012-05-01 15:47:55 +0800360 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
361 DCHECK_ALIGNED(offset, kArmAlignment);
362 const std::vector<uint8_t>& proxy_stub = compiled_proxy_stub->GetCode();
363 uint32_t proxy_stub_size = proxy_stub.size() * sizeof(proxy_stub[0]);
364 CHECK_NE(proxy_stub_size, 0U);
365 uint32_t thumb_offset = compiled_proxy_stub->CodeDelta();
366 proxy_stub_offset = offset + sizeof(proxy_stub_size) + thumb_offset;
367
368 // Deduplicate proxy stubs
369 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&proxy_stub);
370 if (stub_iter != code_offsets_.end()) {
371 proxy_stub_offset = stub_iter->second;
372 } else {
373 code_offsets_.Put(&proxy_stub, proxy_stub_offset);
374 offset += sizeof(proxy_stub_size); // proxy stub size is prepended before code
375 offset += proxy_stub_size;
376 oat_header_->UpdateChecksum(&proxy_stub[0], proxy_stub_size);
377 }
Logan Chien7a2a23a2012-06-06 11:01:00 +0800378 }
379 }
380#endif
381
Brian Carlstrom389efb02012-01-11 12:06:26 -0800382 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index]
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700383 = OatMethodOffsets(code_offset,
384 frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700385 core_spill_mask,
386 fp_spill_mask,
387 mapping_table_offset,
388 vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800389 gc_map_offset,
Logan Chienccb7bf12012-03-28 12:52:32 +0800390 invoke_stub_offset
391#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800392 , proxy_stub_offset
Logan Chienccb7bf12012-03-28 12:52:32 +0800393#endif
394 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700395
Ian Rogers0571d352011-11-03 19:51:38 -0700396 if (compiler_->IsImage()) {
397 ClassLinker* linker = Runtime::Current()->GetClassLinker();
398 DexCache* dex_cache = linker->FindDexCache(*dex_file);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700399 // Unchecked as we hold mutator_lock_ on entry.
400 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700401 AbstractMethod* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache,
Brian Carlstromff2d2c52012-10-30 14:15:34 -0700402 NULL, NULL, type);
Ian Rogers0571d352011-11-03 19:51:38 -0700403 CHECK(method != NULL);
404 method->SetFrameSizeInBytes(frame_size_in_bytes);
405 method->SetCoreSpillMask(core_spill_mask);
406 method->SetFpSpillMask(fp_spill_mask);
407 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800408 // Don't overwrite static method trampoline
409 if (!method->IsStatic() || method->IsConstructor() ||
410 method->GetDeclaringClass()->IsInitialized()) {
411 method->SetOatCodeOffset(code_offset);
412 } else {
413 method->SetCode(Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod)->GetData());
414 }
Ian Rogers0571d352011-11-03 19:51:38 -0700415 method->SetOatVmapTableOffset(vmap_table_offset);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700416 method->SetOatNativeGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700417 method->SetOatInvokeStubOffset(invoke_stub_offset);
418 }
Logan Chien8b977d32012-02-21 19:14:55 +0800419
Brian Carlstrome24fa612011-09-29 00:53:55 -0700420 return offset;
421}
422
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700423#define DCHECK_CODE_OFFSET() \
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800424 DCHECK_EQ(static_cast<off_t>(code_offset), lseek(file->Fd(), 0, SEEK_CUR))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700425
Elliott Hughes234da572011-11-03 22:13:06 -0700426bool OatWriter::Write(File* file) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700427 if (!file->WriteFully(oat_header_, sizeof(*oat_header_))) {
Elliott Hughes76160052012-12-12 16:31:20 -0800428 PLOG(ERROR) << "Failed to write oat header to " << file->GetPath();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700429 return false;
430 }
431
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700432 if (!file->WriteFully(image_file_location_.data(),
433 image_file_location_.size())) {
Elliott Hughes76160052012-12-12 16:31:20 -0800434 PLOG(ERROR) << "Failed to write oat header image file location to " << file->GetPath();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700435 return false;
436 }
437
Elliott Hughes234da572011-11-03 22:13:06 -0700438 if (!WriteTables(file)) {
Elliott Hughes76160052012-12-12 16:31:20 -0800439 LOG(ERROR) << "Failed to write oat tables to " << file->GetPath();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700440 return false;
441 }
442
Elliott Hughes234da572011-11-03 22:13:06 -0700443 size_t code_offset = WriteCode(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700444 if (code_offset == 0) {
Elliott Hughes76160052012-12-12 16:31:20 -0800445 LOG(ERROR) << "Failed to write oat code to " << file->GetPath();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700446 return false;
447 }
448
Elliott Hughes234da572011-11-03 22:13:06 -0700449 code_offset = WriteCodeDexFiles(file, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700450 if (code_offset == 0) {
Elliott Hughes76160052012-12-12 16:31:20 -0800451 LOG(ERROR) << "Failed to write oat code for dex files to " << file->GetPath();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700452 return false;
453 }
454
455 return true;
456}
457
458bool OatWriter::WriteTables(File* file) {
459 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
460 if (!oat_dex_files_[i]->Write(file)) {
Elliott Hughes76160052012-12-12 16:31:20 -0800461 PLOG(ERROR) << "Failed to write oat dex information to " << file->GetPath();
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_;
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800467 off_t actual_offset = lseek(file->Fd(), 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];
475 if (!file->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Elliott Hughes76160052012-12-12 16:31:20 -0800476 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << file->GetPath();
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) {
481 if (!oat_classes_[i]->Write(file)) {
Elliott Hughes76160052012-12-12 16:31:20 -0800482 PLOG(ERROR) << "Failed to write oat methods information to " << file->GetPath();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700483 return false;
484 }
485 }
486 return true;
487}
488
489size_t OatWriter::WriteCode(File* file) {
490 uint32_t code_offset = oat_header_->GetExecutableOffset();
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800491 off_t new_offset = lseek(file->Fd(), 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
Elliott Hughes76160052012-12-12 16:31:20 -0800494 << " Expected: " << code_offset << " File: " << file->GetPath();
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
501size_t OatWriter::WriteCodeDexFiles(File* file, 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);
Ian Rogers0571d352011-11-03 19:51:38 -0700506 code_offset = WriteCodeDexFile(file, 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
Ian Rogers0571d352011-11-03 19:51:38 -0700514size_t OatWriter::WriteCodeDexFile(File* file, size_t code_offset, size_t& oat_class_index,
515 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);
Ian Rogers0571d352011-11-03 19:51:38 -0700519 code_offset = WriteCodeClassDef(file, 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,
528 const DexFile& dex_file, File* f) const {
529 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
Elliott Hughes76160052012-12-12 16:31:20 -0800530 << " to " << f->GetPath();
Elliott Hughes234da572011-11-03 22:13:06 -0700531}
532
Brian Carlstrome24fa612011-09-29 00:53:55 -0700533size_t OatWriter::WriteCodeClassDef(File* file,
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;
554 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
555 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()) {
563 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
564 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
Ian Rogers0571d352011-11-03 19:51:38 -0700574size_t OatWriter::WriteCodeMethod(File* file, size_t code_offset, size_t oat_class_index,
575 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) {
588 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
589 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
590 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Elliott Hughes76160052012-12-12 16:31:20 -0800591 << " Expected: " << aligned_code_offset << " File: " << file->GetPath();
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);
609 if (!file->WriteFully(&code_size, sizeof(code_size))) {
610 ReportWriteFailure("method code size", method_idx, dex_file, file);
611 return 0;
612 }
613 code_offset += sizeof(code_size);
614 DCHECK_CODE_OFFSET();
615 if (!file->WriteFully(&code[0], code_size)) {
616 ReportWriteFailure("method code", method_idx, dex_file, file);
617 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);
638 if (!file->WriteFully(&mapping_table[0], mapping_table_size)) {
639 ReportWriteFailure("mapping table", method_idx, dex_file, file);
640 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);
661 if (!file->WriteFully(&vmap_table[0], vmap_table_size)) {
662 ReportWriteFailure("vmap table", method_idx, dex_file, file);
663 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);
684 if (!file->WriteFully(&gc_map[0], gc_map_size)) {
685 ReportWriteFailure("GC map", method_idx, dex_file, file);
686 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) {
699 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
700 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);
721 if (!file->WriteFully(&invoke_stub_size, sizeof(invoke_stub_size))) {
722 ReportWriteFailure("invoke stub code size", method_idx, dex_file, file);
723 return 0;
724 }
725 code_offset += sizeof(invoke_stub_size);
726 DCHECK_CODE_OFFSET();
727 if (!file->WriteFully(&invoke_stub[0], invoke_stub_size)) {
728 ReportWriteFailure("invoke stub code", method_idx, dex_file, file);
729 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) {
745 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
746 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);
767 if (!file->WriteFully(&proxy_stub_size, sizeof(proxy_stub_size))) {
768 ReportWriteFailure("proxy stub code size", method_idx, dex_file, file);
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();
Logan Chien971bf3f2012-05-01 15:47:55 +0800773 if (!file->WriteFully(&proxy_stub[0], proxy_stub_size)) {
774 ReportWriteFailure("proxy stub code", method_idx, dex_file, file);
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
814bool OatWriter::OatDexFile::Write(File* file) const {
815 if (!file->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
Elliott Hughes76160052012-12-12 16:31:20 -0800816 PLOG(ERROR) << "Failed to write dex file location length to " << file->GetPath();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700817 return false;
818 }
819 if (!file->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
Elliott Hughes76160052012-12-12 16:31:20 -0800820 PLOG(ERROR) << "Failed to write dex file location data to " << file->GetPath();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700821 return false;
822 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800823 if (!file->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
Elliott Hughes76160052012-12-12 16:31:20 -0800824 PLOG(ERROR) << "Failed to write dex file location checksum to " << file->GetPath();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700825 return false;
826 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800827 if (!file->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
Elliott Hughes76160052012-12-12 16:31:20 -0800828 PLOG(ERROR) << "Failed to write dex file offset to " << file->GetPath();
Brian Carlstrom89521892011-12-07 22:05:07 -0800829 return false;
830 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800831 if (!file->WriteFully(&methods_offsets_[0],
832 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Elliott Hughes76160052012-12-12 16:31:20 -0800833 PLOG(ERROR) << "Failed to write methods offsets to " << file->GetPath();
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 Carlstrom389efb02012-01-11 12:06:26 -0800855bool OatWriter::OatClass::Write(File* file) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800856 if (!file->WriteFully(&status_, sizeof(status_))) {
Elliott Hughes76160052012-12-12 16:31:20 -0800857 PLOG(ERROR) << "Failed to write class status to " << file->GetPath();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800858 return false;
859 }
860 if (!file->WriteFully(&method_offsets_[0],
861 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Elliott Hughes76160052012-12-12 16:31:20 -0800862 PLOG(ERROR) << "Failed to write method offsets to " << file->GetPath();
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