blob: df809d5d33cdff72889dabcade74dfcf41db685c [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
19#include "class_linker.h"
20#include "class_loader.h"
Logan Chien25ae6402012-03-20 20:19:26 +080021#include "elf_image.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070022#include "file.h"
23#include "os.h"
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070024#include "space.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070025#include "stl_util.h"
26
Logan Chien25ae6402012-03-20 20:19:26 +080027#include <zlib.h>
28
Brian Carlstrome24fa612011-09-29 00:53:55 -070029namespace art {
30
Elliott Hughes234da572011-11-03 22:13:06 -070031bool OatWriter::Create(File* file,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070032 const ClassLoader* class_loader,
jeffhao10037c82012-01-23 15:06:23 -080033 const std::vector<const DexFile*>& dex_files,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070034 uint32_t image_file_location_checksum,
35 const std::string& image_file_location,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070036 const Compiler& compiler) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070037 OatWriter oat_writer(dex_files,
38 image_file_location_checksum,
39 image_file_location,
40 class_loader,
41 compiler);
Elliott Hughes234da572011-11-03 22:13:06 -070042 return oat_writer.Write(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -070043}
44
Brian Carlstrom3320cf42011-10-04 14:58:28 -070045OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070046 uint32_t image_file_location_checksum,
47 const std::string& image_file_location,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070048 const ClassLoader* class_loader,
49 const Compiler& compiler) {
50 compiler_ = &compiler;
Brian Carlstrome24fa612011-09-29 00:53:55 -070051 class_loader_ = class_loader;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070052 image_file_location_checksum_ = image_file_location_checksum;
53 image_file_location_ = image_file_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -070054 dex_files_ = &dex_files;
Logan Chien25ae6402012-03-20 20:19:26 +080055 elf_images_ = compiler_->GetElfImages();
Ian Rogers0571d352011-11-03 19:51:38 -070056 oat_header_ = NULL;
57 executable_offset_padding_length_ = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -070058
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070059 size_t offset = InitOatHeader();
Brian Carlstrome24fa612011-09-29 00:53:55 -070060 offset = InitOatDexFiles(offset);
Brian Carlstrom89521892011-12-07 22:05:07 -080061 offset = InitDexFiles(offset);
Logan Chien25ae6402012-03-20 20:19:26 +080062 offset = InitOatElfImages(offset);
63 offset = InitElfImages(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_);
Logan Chien25ae6402012-03-20 20:19:26 +080074 STLDeleteElements(&oat_elf_images_);
Brian Carlstrom389efb02012-01-11 12:06:26 -080075 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -070076}
77
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070078size_t OatWriter::InitOatHeader() {
Brian Carlstrome24fa612011-09-29 00:53:55 -070079 // create the OatHeader
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070080 oat_header_ = new OatHeader(compiler_->GetInstructionSet(),
81 dex_files_,
Logan Chien25ae6402012-03-20 20:19:26 +080082 elf_images_.size(),
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070083 image_file_location_checksum_,
84 image_file_location_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070085 size_t offset = sizeof(*oat_header_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070086 offset += image_file_location_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -070087 return offset;
88}
89
90size_t OatWriter::InitOatDexFiles(size_t offset) {
91 // create the OatDexFiles
92 for (size_t i = 0; i != dex_files_->size(); ++i) {
93 const DexFile* dex_file = (*dex_files_)[i];
94 CHECK(dex_file != NULL);
95 OatDexFile* oat_dex_file = new OatDexFile(*dex_file);
96 oat_dex_files_.push_back(oat_dex_file);
97 offset += oat_dex_file->SizeOf();
98 }
99 return offset;
100}
101
Brian Carlstrom89521892011-12-07 22:05:07 -0800102size_t OatWriter::InitDexFiles(size_t offset) {
103 // calculate the offsets within OatDexFiles to the DexFiles
104 for (size_t i = 0; i != dex_files_->size(); ++i) {
105 // dex files are required to be 4 byte aligned
106 offset = RoundUp(offset, 4);
107
108 // set offset in OatDexFile to DexFile
109 oat_dex_files_[i]->dex_file_offset_ = offset;
110
111 const DexFile* dex_file = (*dex_files_)[i];
112 offset += dex_file->GetHeader().file_size_;
113 }
114 return offset;
115}
116
Logan Chien25ae6402012-03-20 20:19:26 +0800117size_t OatWriter::InitOatElfImages(size_t offset) {
118 // Offset to ELF image table should be rounded up to 4-byte aligned, so that
119 // we can read the uint32_t directly.
120 offset = RoundUp(offset, 4);
121 oat_header_->SetElfImageTableOffset(offset);
122
123 for (size_t i = 0, n = elf_images_.size(); i < n; ++i) {
124 OatElfImage* oat_elf_image = new OatElfImage(elf_images_[i]);
125 oat_elf_images_.push_back(oat_elf_image);
126 offset += oat_elf_image->SizeOf();
127 }
128 return offset;
129}
130
131size_t OatWriter::InitElfImages(size_t offset) {
132 for (size_t i = 0; i < oat_elf_images_.size(); ++i) {
133 offset = RoundUp(offset, 4);
134 oat_elf_images_[i]->SetElfOffset(offset);
135 offset += oat_elf_images_[i]->GetElfSize();
136 }
137 return offset;
138}
139
Brian Carlstrom389efb02012-01-11 12:06:26 -0800140size_t OatWriter::InitOatClasses(size_t offset) {
141 // create the OatClasses
142 // calculate the offsets within OatDexFiles to OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -0700143 for (size_t i = 0; i != dex_files_->size(); ++i) {
144 const DexFile* dex_file = (*dex_files_)[i];
145 for (size_t class_def_index = 0;
146 class_def_index < dex_file->NumClassDefs();
Ian Rogersc20a83e2012-01-18 18:15:32 -0800147 class_def_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800148 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700149 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
150 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700151 uint32_t num_methods = 0;
152 if (class_data != NULL) { // ie not an empty class, such as a marker interface
153 ClassDataItemIterator it(*dex_file, class_data);
154 size_t num_direct_methods = it.NumDirectMethods();
155 size_t num_virtual_methods = it.NumVirtualMethods();
156 num_methods = num_direct_methods + num_virtual_methods;
157 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800158
159 CompiledClass* compiled_class =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800160 compiler_->GetCompiledClass(Compiler::MethodReference(dex_file, class_def_index));
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800161 Class::Status status =
162 (compiled_class != NULL) ? compiled_class->GetStatus() : Class::kStatusNotReady;
163
164 OatClass* oat_class = new OatClass(status, num_methods);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800165 oat_classes_.push_back(oat_class);
166 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700167 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800168 oat_dex_files_[i]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700169 }
170 return offset;
171}
172
173size_t OatWriter::InitOatCode(size_t offset) {
174 // calculate the offsets within OatHeader to executable code
175 size_t old_offset = offset;
176 // required to be on a new page boundary
177 offset = RoundUp(offset, kPageSize);
178 oat_header_->SetExecutableOffset(offset);
179 executable_offset_padding_length_ = offset - old_offset;
180 return offset;
181}
182
183size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700184 size_t oat_class_index = 0;
185 for (size_t i = 0; i != dex_files_->size(); ++i) {
186 const DexFile* dex_file = (*dex_files_)[i];
187 CHECK(dex_file != NULL);
188 offset = InitOatCodeDexFile(offset, oat_class_index, *dex_file);
189 }
190 return offset;
191}
192
193size_t OatWriter::InitOatCodeDexFile(size_t offset,
194 size_t& oat_class_index,
195 const DexFile& dex_file) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800196 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700197 class_def_index < dex_file.NumClassDefs();
198 class_def_index++, oat_class_index++) {
199 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800200 offset = InitOatCodeClassDef(offset, oat_class_index, class_def_index, dex_file, class_def);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800201 oat_classes_[oat_class_index]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700202 }
203 return offset;
204}
205
206size_t OatWriter::InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800207 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700208 const DexFile& dex_file,
209 const DexFile::ClassDef& class_def) {
210 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700211 if (class_data == NULL) {
212 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700213 return offset;
214 }
Ian Rogers0571d352011-11-03 19:51:38 -0700215 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800216 CHECK_EQ(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700217 it.NumDirectMethods() + it.NumVirtualMethods());
218 // Skip fields
219 while (it.HasNextStaticField()) {
220 it.Next();
221 }
222 while (it.HasNextInstanceField()) {
223 it.Next();
224 }
225 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700226 size_t class_def_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700227 while (it.HasNextDirectMethod()) {
228 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
Ian Rogersc20a83e2012-01-18 18:15:32 -0800229 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
230 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index, is_native,
231 is_static, true, it.GetMemberIndex(), &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700232 class_def_method_index++;
233 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700234 }
Ian Rogers0571d352011-11-03 19:51:38 -0700235 while (it.HasNextVirtualMethod()) {
236 CHECK_EQ(it.GetMemberAccessFlags() & kAccStatic, 0U);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800237 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
238 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index, is_native,
239 false, false, it.GetMemberIndex(), &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700240 class_def_method_index++;
241 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700242 }
Ian Rogers0571d352011-11-03 19:51:38 -0700243 DCHECK(!it.HasNext());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700244 return offset;
245}
246
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700247size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
248 size_t __attribute__((unused)) class_def_index,
249 size_t class_def_method_index,
250 bool __attribute__((unused)) is_native,
251 bool is_static, bool is_direct,
252 uint32_t method_idx, const DexFile* dex_file) {
Logan Chien8b977d32012-02-21 19:14:55 +0800253#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700254 // derived from CompiledMethod if available
255 uint32_t code_offset = 0;
256 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700257 uint32_t core_spill_mask = 0;
258 uint32_t fp_spill_mask = 0;
259 uint32_t mapping_table_offset = 0;
260 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800261 uint32_t gc_map_offset = 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700262 // derived from CompiledInvokeStub if available
263 uint32_t invoke_stub_offset = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700264
Ian Rogers0571d352011-11-03 19:51:38 -0700265 CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800266 compiler_->GetCompiledMethod(Compiler::MethodReference(dex_file, method_idx));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700267 if (compiled_method != NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700268 offset = compiled_method->AlignCode(offset);
Elliott Hughes06b37d92011-10-16 11:51:29 -0700269 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700270 const std::vector<uint8_t>& code = compiled_method->GetCode();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700271 uint32_t code_size = code.size() * sizeof(code[0]);
272 CHECK_NE(code_size, 0U);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700273 uint32_t thumb_offset = compiled_method->CodeDelta();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700274 code_offset = offset + sizeof(code_size) + thumb_offset;
jeffhao55d78212011-11-02 11:41:50 -0700275
276 // Deduplicate code arrays
jeffhaof479dcc2011-11-02 15:54:15 -0700277 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
jeffhao55d78212011-11-02 11:41:50 -0700278 if (code_iter != code_offsets_.end()) {
279 code_offset = code_iter->second;
280 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700281 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&code, code_offset));
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700282 offset += sizeof(code_size); // code size is prepended before code
jeffhao55d78212011-11-02 11:41:50 -0700283 offset += code_size;
284 oat_header_->UpdateChecksum(&code[0], code_size);
285 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700286 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700287 core_spill_mask = compiled_method->GetCoreSpillMask();
288 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700289
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700290 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
291 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
292 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700293
294 // Deduplicate mapping tables
jeffhaof479dcc2011-11-02 15:54:15 -0700295 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
jeffhao55d78212011-11-02 11:41:50 -0700296 if (mapping_iter != mapping_table_offsets_.end()) {
297 mapping_table_offset = mapping_iter->second;
298 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700299 mapping_table_offsets_.insert(std::pair<const std::vector<uint32_t>*, uint32_t>(&mapping_table, mapping_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700300 offset += mapping_table_size;
301 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
302 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700303
304 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
305 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
306 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
jeffhao55d78212011-11-02 11:41:50 -0700307
308 // Deduplicate vmap tables
jeffhaof479dcc2011-11-02 15:54:15 -0700309 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
jeffhao55d78212011-11-02 11:41:50 -0700310 if (vmap_iter != vmap_table_offsets_.end()) {
311 vmap_table_offset = vmap_iter->second;
312 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700313 vmap_table_offsets_.insert(std::pair<const std::vector<uint16_t>*, uint32_t>(&vmap_table, vmap_table_offset));
jeffhao55d78212011-11-02 11:41:50 -0700314 offset += vmap_table_size;
315 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
316 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800317
318 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
319 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
320 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
321
Ian Rogersc20a83e2012-01-18 18:15:32 -0800322#ifndef NDEBUG
323 // We expect GC maps except when the class hasn't been verified or the method is native
324 CompiledClass* compiled_class =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800325 compiler_->GetCompiledClass(Compiler::MethodReference(dex_file, class_def_index));
Ian Rogersc20a83e2012-01-18 18:15:32 -0800326 Class::Status status =
327 (compiled_class != NULL) ? compiled_class->GetStatus() : Class::kStatusNotReady;
328 CHECK(gc_map_size != 0 || is_native || status < Class::kStatusVerified)
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800329 << &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 -0800330#endif
331
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800332 // Deduplicate GC maps
333 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = gc_map_offsets_.find(&gc_map);
334 if (gc_map_iter != gc_map_offsets_.end()) {
335 gc_map_offset = gc_map_iter->second;
336 } else {
337 gc_map_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&gc_map, gc_map_offset));
338 offset += gc_map_size;
339 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
340 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700341 }
342
Ian Rogers0571d352011-11-03 19:51:38 -0700343 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx));
344 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700345 if (compiled_invoke_stub != NULL) {
346 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
Elliott Hughes06b37d92011-10-16 11:51:29 -0700347 DCHECK_ALIGNED(offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700348 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700349 uint32_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
350 CHECK_NE(invoke_stub_size, 0U);
351 invoke_stub_offset = offset + sizeof(invoke_stub_size);
jeffhao55d78212011-11-02 11:41:50 -0700352
353 // Deduplicate invoke stubs
jeffhaof479dcc2011-11-02 15:54:15 -0700354 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&invoke_stub);
jeffhao55d78212011-11-02 11:41:50 -0700355 if (stub_iter != code_offsets_.end()) {
356 invoke_stub_offset = stub_iter->second;
357 } else {
jeffhaof479dcc2011-11-02 15:54:15 -0700358 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&invoke_stub, invoke_stub_offset));
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700359 offset += sizeof(invoke_stub_size); // invoke stub size is prepended before code
jeffhao55d78212011-11-02 11:41:50 -0700360 offset += invoke_stub_size;
361 oat_header_->UpdateChecksum(&invoke_stub[0], invoke_stub_size);
362 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700363 }
364
Brian Carlstrom389efb02012-01-11 12:06:26 -0800365 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index]
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700366 = OatMethodOffsets(code_offset,
367 frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700368 core_spill_mask,
369 fp_spill_mask,
370 mapping_table_offset,
371 vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800372 gc_map_offset,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700373 invoke_stub_offset);
374
Ian Rogers0571d352011-11-03 19:51:38 -0700375 if (compiler_->IsImage()) {
376 ClassLinker* linker = Runtime::Current()->GetClassLinker();
377 DexCache* dex_cache = linker->FindDexCache(*dex_file);
378 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader_,
379 is_direct);
380 CHECK(method != NULL);
381 method->SetFrameSizeInBytes(frame_size_in_bytes);
382 method->SetCoreSpillMask(core_spill_mask);
383 method->SetFpSpillMask(fp_spill_mask);
384 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800385 // Don't overwrite static method trampoline
386 if (!method->IsStatic() || method->IsConstructor() ||
387 method->GetDeclaringClass()->IsInitialized()) {
388 method->SetOatCodeOffset(code_offset);
389 } else {
390 method->SetCode(Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod)->GetData());
391 }
Ian Rogers0571d352011-11-03 19:51:38 -0700392 method->SetOatVmapTableOffset(vmap_table_offset);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800393 method->SetOatGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700394 method->SetOatInvokeStubOffset(invoke_stub_offset);
395 }
Logan Chien8b977d32012-02-21 19:14:55 +0800396#endif
397
Brian Carlstrome24fa612011-09-29 00:53:55 -0700398 return offset;
399}
400
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700401#define DCHECK_CODE_OFFSET() \
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800402 DCHECK_EQ(static_cast<off_t>(code_offset), lseek(file->Fd(), 0, SEEK_CUR))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700403
Elliott Hughes234da572011-11-03 22:13:06 -0700404bool OatWriter::Write(File* file) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700405 if (!file->WriteFully(oat_header_, sizeof(*oat_header_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700406 PLOG(ERROR) << "Failed to write oat header to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700407 return false;
408 }
409
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700410 if (!file->WriteFully(image_file_location_.data(),
411 image_file_location_.size())) {
412 PLOG(ERROR) << "Failed to write oat header image file location to " << file->name();
413 return false;
414 }
415
Elliott Hughes234da572011-11-03 22:13:06 -0700416 if (!WriteTables(file)) {
417 LOG(ERROR) << "Failed to write oat tables to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700418 return false;
419 }
420
Elliott Hughes234da572011-11-03 22:13:06 -0700421 size_t code_offset = WriteCode(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700422 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700423 LOG(ERROR) << "Failed to write oat code to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700424 return false;
425 }
426
Elliott Hughes234da572011-11-03 22:13:06 -0700427 code_offset = WriteCodeDexFiles(file, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700428 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700429 LOG(ERROR) << "Failed to write oat code for dex files to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700430 return false;
431 }
432
433 return true;
434}
435
436bool OatWriter::WriteTables(File* file) {
437 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
438 if (!oat_dex_files_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700439 PLOG(ERROR) << "Failed to write oat dex information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700440 return false;
441 }
442 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800443 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
444 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800445 off_t actual_offset = lseek(file->Fd(), expected_offset, SEEK_SET);
Brian Carlstrom89521892011-12-07 22:05:07 -0800446 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
447 const DexFile* dex_file = (*dex_files_)[i];
448 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
449 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
450 return false;
451 }
452 const DexFile* dex_file = (*dex_files_)[i];
453 if (!file->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
454 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << file->name();
455 return false;
456 }
457 }
Logan Chien25ae6402012-03-20 20:19:26 +0800458 for (size_t i = 0; i != oat_elf_images_.size(); ++i) {
459 if (!oat_elf_images_[i]->Write(file)) {
460 PLOG(ERROR) << "Failed to write oat elf information to " << file->name();
461 return false;
462 }
463 }
464 for (size_t i = 0; i != oat_elf_images_.size(); ++i) {
465 uint32_t expected_offset = oat_elf_images_[i]->GetElfOffset();
466 off_t actual_offset = lseek(file->Fd(), expected_offset, SEEK_SET);
467 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
468 PLOG(ERROR) << "Failed to seek to dex file section."
469 << " Actual: " << actual_offset
470 << " Expected: " << expected_offset;
471 return false;
472 }
473 if (!oat_elf_images_[i]->WriteElfImage(file)) {
474 return false;
475 }
476 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800477 for (size_t i = 0; i != oat_classes_.size(); ++i) {
478 if (!oat_classes_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700479 PLOG(ERROR) << "Failed to write oat methods information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700480 return false;
481 }
482 }
483 return true;
484}
485
486size_t OatWriter::WriteCode(File* file) {
487 uint32_t code_offset = oat_header_->GetExecutableOffset();
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800488 off_t new_offset = lseek(file->Fd(), executable_offset_padding_length_, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700489 if (static_cast<uint32_t>(new_offset) != code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700490 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700491 << " Expected: " << code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700492 return 0;
493 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700494 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700495 return code_offset;
496}
497
498size_t OatWriter::WriteCodeDexFiles(File* file, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700499 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800500 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700501 const DexFile* dex_file = (*dex_files_)[i];
502 CHECK(dex_file != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700503 code_offset = WriteCodeDexFile(file, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700504 if (code_offset == 0) {
505 return 0;
506 }
507 }
508 return code_offset;
509}
510
Ian Rogers0571d352011-11-03 19:51:38 -0700511size_t OatWriter::WriteCodeDexFile(File* file, size_t code_offset, size_t& oat_class_index,
512 const DexFile& dex_file) {
513 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
514 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700515 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700516 code_offset = WriteCodeClassDef(file, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700517 if (code_offset == 0) {
518 return 0;
519 }
520 }
521 return code_offset;
522}
523
Ian Rogers0571d352011-11-03 19:51:38 -0700524void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
525 const DexFile& dex_file, File* f) const {
526 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
527 << " to " << f->name();
Elliott Hughes234da572011-11-03 22:13:06 -0700528}
529
Brian Carlstrome24fa612011-09-29 00:53:55 -0700530size_t OatWriter::WriteCodeClassDef(File* file,
Ian Rogers0571d352011-11-03 19:51:38 -0700531 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700532 const DexFile& dex_file,
533 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700534 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700535 if (class_data == NULL) {
536 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700537 return code_offset;
538 }
Ian Rogers0571d352011-11-03 19:51:38 -0700539 ClassDataItemIterator it(dex_file, class_data);
540 // Skip fields
541 while (it.HasNextStaticField()) {
542 it.Next();
543 }
544 while (it.HasNextInstanceField()) {
545 it.Next();
546 }
547 // Process methods
548 size_t class_def_method_index = 0;
549 while (it.HasNextDirectMethod()) {
550 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
551 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
552 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700553 if (code_offset == 0) {
554 return 0;
555 }
Ian Rogers0571d352011-11-03 19:51:38 -0700556 class_def_method_index++;
557 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700558 }
Ian Rogers0571d352011-11-03 19:51:38 -0700559 while (it.HasNextVirtualMethod()) {
560 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
561 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700562 if (code_offset == 0) {
563 return 0;
564 }
Ian Rogers0571d352011-11-03 19:51:38 -0700565 class_def_method_index++;
566 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700567 }
568 return code_offset;
569}
570
Ian Rogers0571d352011-11-03 19:51:38 -0700571size_t OatWriter::WriteCodeMethod(File* file, size_t code_offset, size_t oat_class_index,
572 size_t class_def_method_index, bool is_static,
573 uint32_t method_idx, const DexFile& dex_file) {
Logan Chien8b977d32012-02-21 19:14:55 +0800574#if !defined(ART_USE_LLVM_COMPILER)
Ian Rogers0571d352011-11-03 19:51:38 -0700575 const CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800576 compiler_->GetCompiledMethod(Compiler::MethodReference(&dex_file, method_idx));
Ian Rogers0571d352011-11-03 19:51:38 -0700577
578 uint32_t frame_size_in_bytes = 0;
579 uint32_t core_spill_mask = 0;
580 uint32_t fp_spill_mask = 0;
581
582 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800583 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700584
585
586 if (compiled_method != NULL) { // ie. not an abstract method
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700587 uint32_t aligned_code_offset = compiled_method->AlignCode(code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700588 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
589 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800590 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700591 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700592 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700593 << " Expected: " << aligned_code_offset << " File: " << file->name();
Brian Carlstromf03c2882012-03-05 20:29:06 -0800594 return 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700595 }
596 code_offset += aligned_code_delta;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700597 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700598 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700599 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700600 const std::vector<uint8_t>& code = compiled_method->GetCode();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700601 uint32_t code_size = code.size() * sizeof(code[0]);
602 CHECK_NE(code_size, 0U);
jeffhao55d78212011-11-02 11:41:50 -0700603
604 // Deduplicate code arrays
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700605 size_t offset = code_offset + sizeof(code_size) + compiled_method->CodeDelta();
jeffhaof479dcc2011-11-02 15:54:15 -0700606 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Ian Rogers0571d352011-11-03 19:51:38 -0700607 if (code_iter != code_offsets_.end() && offset != method_offsets.code_offset_) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700608 DCHECK(code_iter->second == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700609 } else {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700610 DCHECK(offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
611 if (!file->WriteFully(&code_size, sizeof(code_size))) {
612 ReportWriteFailure("method code size", method_idx, dex_file, file);
613 return 0;
614 }
615 code_offset += sizeof(code_size);
616 DCHECK_CODE_OFFSET();
jeffhao55d78212011-11-02 11:41:50 -0700617 if (!file->WriteFully(&code[0], code_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700618 ReportWriteFailure("method code", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800619 return 0;
jeffhao55d78212011-11-02 11:41:50 -0700620 }
621 code_offset += code_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700622 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700623 DCHECK_CODE_OFFSET();
Ian Rogers0571d352011-11-03 19:51:38 -0700624 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
625 core_spill_mask = compiled_method->GetCoreSpillMask();
626 fp_spill_mask = compiled_method->GetFpSpillMask();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700627
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700628 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
629 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700630
631 // Deduplicate mapping tables
Ian Rogers0571d352011-11-03 19:51:38 -0700632 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
633 mapping_table_offsets_.find(&mapping_table);
634 if (mapping_iter != mapping_table_offsets_.end() &&
635 code_offset != method_offsets.mapping_table_offset_) {
636 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
637 || mapping_iter->second == method_offsets.mapping_table_offset_)
638 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700639 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700640 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
641 || code_offset == method_offsets.mapping_table_offset_)
642 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700643 if (!file->WriteFully(&mapping_table[0], mapping_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700644 ReportWriteFailure("mapping table", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800645 return 0;
jeffhao55d78212011-11-02 11:41:50 -0700646 }
647 code_offset += mapping_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700648 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700649 DCHECK_CODE_OFFSET();
650
651 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
652 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
jeffhao55d78212011-11-02 11:41:50 -0700653
654 // Deduplicate vmap tables
Ian Rogers0571d352011-11-03 19:51:38 -0700655 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
656 vmap_table_offsets_.find(&vmap_table);
657 if (vmap_iter != vmap_table_offsets_.end() &&
658 code_offset != method_offsets.vmap_table_offset_) {
659 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
660 || vmap_iter->second == method_offsets.vmap_table_offset_)
661 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700662 } else {
Ian Rogers0571d352011-11-03 19:51:38 -0700663 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
664 || code_offset == method_offsets.vmap_table_offset_)
665 << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700666 if (!file->WriteFully(&vmap_table[0], vmap_table_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700667 ReportWriteFailure("vmap table", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800668 return 0;
jeffhao55d78212011-11-02 11:41:50 -0700669 }
670 code_offset += vmap_table_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700671 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700672 DCHECK_CODE_OFFSET();
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800673
674 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
675 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
676
677 // Deduplicate GC maps
678 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
679 gc_map_offsets_.find(&gc_map);
680 if (gc_map_iter != gc_map_offsets_.end() &&
681 code_offset != method_offsets.gc_map_offset_) {
682 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
683 || gc_map_iter->second == method_offsets.gc_map_offset_)
684 << PrettyMethod(method_idx, dex_file);
685 } else {
686 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
687 || code_offset == method_offsets.gc_map_offset_)
688 << PrettyMethod(method_idx, dex_file);
689 if (!file->WriteFully(&gc_map[0], gc_map_size)) {
690 ReportWriteFailure("GC map", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800691 return 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800692 }
693 code_offset += gc_map_size;
694 }
695 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700696 }
Ian Rogers0571d352011-11-03 19:51:38 -0700697 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
698 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700699 if (compiled_invoke_stub != NULL) {
700 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
701 compiler_->GetInstructionSet());
702 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
703 if (aligned_code_delta != 0) {
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800704 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700705 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
706 PLOG(ERROR) << "Failed to seek to align invoke stub code. Actual: " << new_offset
707 << " Expected: " << aligned_code_offset;
Brian Carlstromf03c2882012-03-05 20:29:06 -0800708 return 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700709 }
710 code_offset += aligned_code_delta;
711 DCHECK_CODE_OFFSET();
712 }
Elliott Hughes06b37d92011-10-16 11:51:29 -0700713 DCHECK_ALIGNED(code_offset, kArmAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700714 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700715 uint32_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
716 CHECK_NE(invoke_stub_size, 0U);
jeffhao55d78212011-11-02 11:41:50 -0700717
718 // Deduplicate invoke stubs
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700719 size_t offset = code_offset + sizeof(invoke_stub_size);
Ian Rogers0571d352011-11-03 19:51:38 -0700720 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter =
721 code_offsets_.find(&invoke_stub);
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700722 if (stub_iter != code_offsets_.end() && offset != method_offsets.invoke_stub_offset_) {
723 DCHECK(stub_iter->second == method_offsets.invoke_stub_offset_) << PrettyMethod(method_idx, dex_file);
jeffhao55d78212011-11-02 11:41:50 -0700724 } else {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700725 DCHECK(offset == method_offsets.invoke_stub_offset_) << PrettyMethod(method_idx, dex_file);
726 if (!file->WriteFully(&invoke_stub_size, sizeof(invoke_stub_size))) {
727 ReportWriteFailure("invoke stub code size", method_idx, dex_file, file);
728 return 0;
729 }
730 code_offset += sizeof(invoke_stub_size);
731 DCHECK_CODE_OFFSET();
jeffhao55d78212011-11-02 11:41:50 -0700732 if (!file->WriteFully(&invoke_stub[0], invoke_stub_size)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700733 ReportWriteFailure("invoke stub code", method_idx, dex_file, file);
Brian Carlstromf03c2882012-03-05 20:29:06 -0800734 return 0;
jeffhao55d78212011-11-02 11:41:50 -0700735 }
736 code_offset += invoke_stub_size;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700737 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700738 DCHECK_CODE_OFFSET();
739 }
Logan Chien8b977d32012-02-21 19:14:55 +0800740#endif
741
Brian Carlstrome24fa612011-09-29 00:53:55 -0700742 return code_offset;
743}
744
Brian Carlstrome24fa612011-09-29 00:53:55 -0700745OatWriter::OatDexFile::OatDexFile(const DexFile& dex_file) {
Elliott Hughes95572412011-12-13 18:14:20 -0800746 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700747 dex_file_location_size_ = location.size();
748 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800749 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800750 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800751 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700752}
753
754size_t OatWriter::OatDexFile::SizeOf() const {
755 return sizeof(dex_file_location_size_)
756 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800757 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800758 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800759 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700760}
761
762void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
763 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
764 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800765 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800766 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800767 oat_header.UpdateChecksum(&methods_offsets_[0],
768 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700769}
770
771bool OatWriter::OatDexFile::Write(File* file) const {
772 if (!file->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700773 PLOG(ERROR) << "Failed to write dex file location length to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700774 return false;
775 }
776 if (!file->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700777 PLOG(ERROR) << "Failed to write dex file location data to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700778 return false;
779 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800780 if (!file->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
781 PLOG(ERROR) << "Failed to write dex file location checksum to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700782 return false;
783 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800784 if (!file->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
785 PLOG(ERROR) << "Failed to write dex file offset to " << file->name();
786 return false;
787 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800788 if (!file->WriteFully(&methods_offsets_[0],
789 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700790 PLOG(ERROR) << "Failed to write methods offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700791 return false;
792 }
793 return true;
794}
795
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800796OatWriter::OatClass::OatClass(Class::Status status, uint32_t methods_count) {
797 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700798 method_offsets_.resize(methods_count);
799}
800
Brian Carlstrom389efb02012-01-11 12:06:26 -0800801size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800802 return sizeof(status_)
803 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700804}
805
Brian Carlstrom389efb02012-01-11 12:06:26 -0800806void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800807 oat_header.UpdateChecksum(&status_, sizeof(status_));
808 oat_header.UpdateChecksum(&method_offsets_[0],
809 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700810}
811
Brian Carlstrom389efb02012-01-11 12:06:26 -0800812bool OatWriter::OatClass::Write(File* file) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800813 if (!file->WriteFully(&status_, sizeof(status_))) {
814 PLOG(ERROR) << "Failed to write class status to " << file->name();
815 return false;
816 }
817 if (!file->WriteFully(&method_offsets_[0],
818 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700819 PLOG(ERROR) << "Failed to write method offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700820 return false;
821 }
822 return true;
823}
824
Logan Chien25ae6402012-03-20 20:19:26 +0800825OatWriter::OatElfImage::OatElfImage(const ElfImage& image)
826 : elf_offset_(0), elf_size_(image.size()), elf_addr_(image.begin()) {
827}
828
829size_t OatWriter::OatElfImage::SizeOf() const {
830 return (sizeof(elf_offset_) + sizeof(elf_size_));
831}
832
833uint32_t OatWriter::OatElfImage::GetElfSize() const {
834 return elf_size_;
835}
836
837uint32_t OatWriter::OatElfImage::GetElfOffset() const {
838 DCHECK_NE(elf_offset_, 0U);
839 return elf_offset_;
840}
841
842void OatWriter::OatElfImage::SetElfOffset(uint32_t offset) {
843 DCHECK_NE(offset, 0U);
844 DCHECK((offset & 0x3LU) == 0);
845 elf_offset_ = offset;
846}
847
848bool OatWriter::OatElfImage::Write(File* file) const {
849 DCHECK_NE(elf_offset_, 0U);
850 if (!file->WriteFully(&elf_offset_, sizeof(elf_offset_))) {
851 PLOG(ERROR) << "Failed to write ELF offset to " << file->name();
852 return false;
853 }
854 if (!file->WriteFully(&elf_size_, sizeof(elf_size_))) {
855 PLOG(ERROR) << "Failed to write ELF size to " << file->name();
856 return false;
857 }
858 return true;
859}
860
861bool OatWriter::OatElfImage::WriteElfImage(File* file) const {
862 if (!file->WriteFully(elf_addr_, elf_size_)) {
863 PLOG(ERROR) << "Failed to write ELF image to " << file->name();
864 return false;
865 }
866 return true;
867}
868
Brian Carlstrome24fa612011-09-29 00:53:55 -0700869} // namespace art