blob: 2558ebac89c980891cc4c8f8d5429a287f0e40b5 [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
Brian Carlstrome24fa612011-09-29 00:53:55 -070021#include "class_linker.h"
22#include "class_loader.h"
Logan Chien25ae6402012-03-20 20:19:26 +080023#include "elf_image.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070024#include "file.h"
25#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070026#include "safe_map.h"
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070027#include "space.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070028#include "stl_util.h"
29
30namespace art {
31
Elliott Hughes234da572011-11-03 22:13:06 -070032bool OatWriter::Create(File* file,
Ian Rogers365c1022012-06-22 15:05:28 -070033 ClassLoader* class_loader,
jeffhao10037c82012-01-23 15:06:23 -080034 const std::vector<const DexFile*>& dex_files,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070035 uint32_t image_file_location_checksum,
36 const std::string& image_file_location,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070037 const Compiler& compiler) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070038 OatWriter oat_writer(dex_files,
39 image_file_location_checksum,
40 image_file_location,
41 class_loader,
42 compiler);
Elliott Hughes234da572011-11-03 22:13:06 -070043 return oat_writer.Write(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -070044}
45
Brian Carlstrom3320cf42011-10-04 14:58:28 -070046OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070047 uint32_t image_file_location_checksum,
48 const std::string& image_file_location,
Ian Rogers365c1022012-06-22 15:05:28 -070049 ClassLoader* class_loader,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070050 const Compiler& compiler) {
51 compiler_ = &compiler;
Brian Carlstrome24fa612011-09-29 00:53:55 -070052 class_loader_ = class_loader;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070053 image_file_location_checksum_ = image_file_location_checksum;
54 image_file_location_ = image_file_location;
Brian Carlstrome24fa612011-09-29 00:53:55 -070055 dex_files_ = &dex_files;
Logan Chien25ae6402012-03-20 20:19:26 +080056 elf_images_ = compiler_->GetElfImages();
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);
Logan Chien25ae6402012-03-20 20:19:26 +080063 offset = InitOatElfImages(offset);
64 offset = InitElfImages(offset);
Brian Carlstrom389efb02012-01-11 12:06:26 -080065 offset = InitOatClasses(offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -070066 offset = InitOatCode(offset);
67 offset = InitOatCodeDexFiles(offset);
68
69 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -070070}
71
Ian Rogers0571d352011-11-03 19:51:38 -070072OatWriter::~OatWriter() {
73 delete oat_header_;
74 STLDeleteElements(&oat_dex_files_);
Logan Chien25ae6402012-03-20 20:19:26 +080075 STLDeleteElements(&oat_elf_images_);
Brian Carlstrom389efb02012-01-11 12:06:26 -080076 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -070077}
78
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070079size_t OatWriter::InitOatHeader() {
Brian Carlstrome24fa612011-09-29 00:53:55 -070080 // create the OatHeader
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070081 oat_header_ = new OatHeader(compiler_->GetInstructionSet(),
82 dex_files_,
Logan Chien25ae6402012-03-20 20:19:26 +080083 elf_images_.size(),
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070084 image_file_location_checksum_,
85 image_file_location_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070086 size_t offset = sizeof(*oat_header_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070087 offset += image_file_location_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -070088 return offset;
89}
90
91size_t OatWriter::InitOatDexFiles(size_t offset) {
92 // create the OatDexFiles
93 for (size_t i = 0; i != dex_files_->size(); ++i) {
94 const DexFile* dex_file = (*dex_files_)[i];
95 CHECK(dex_file != NULL);
96 OatDexFile* oat_dex_file = new OatDexFile(*dex_file);
97 oat_dex_files_.push_back(oat_dex_file);
98 offset += oat_dex_file->SizeOf();
99 }
100 return offset;
101}
102
Brian Carlstrom89521892011-12-07 22:05:07 -0800103size_t OatWriter::InitDexFiles(size_t offset) {
104 // calculate the offsets within OatDexFiles to the DexFiles
105 for (size_t i = 0; i != dex_files_->size(); ++i) {
106 // dex files are required to be 4 byte aligned
107 offset = RoundUp(offset, 4);
108
109 // set offset in OatDexFile to DexFile
110 oat_dex_files_[i]->dex_file_offset_ = offset;
111
112 const DexFile* dex_file = (*dex_files_)[i];
113 offset += dex_file->GetHeader().file_size_;
114 }
115 return offset;
116}
117
Logan Chien25ae6402012-03-20 20:19:26 +0800118size_t OatWriter::InitOatElfImages(size_t offset) {
Shih-wei Liao8e5e9782012-03-24 13:18:46 -0700119 size_t n = elf_images_.size();
120 if (n != 0) {
121 // Offset to ELF image table should be rounded up to 4-byte aligned, so that
122 // we can read the uint32_t directly.
123 offset = RoundUp(offset, 4);
124 oat_header_->SetElfImageTableOffset(offset);
125 } else {
126 oat_header_->SetElfImageTableOffset(0);
127 }
Logan Chien25ae6402012-03-20 20:19:26 +0800128
Shih-wei Liao8e5e9782012-03-24 13:18:46 -0700129 for (size_t i = 0; i < n; ++i) {
Logan Chien25ae6402012-03-20 20:19:26 +0800130 OatElfImage* oat_elf_image = new OatElfImage(elf_images_[i]);
131 oat_elf_images_.push_back(oat_elf_image);
132 offset += oat_elf_image->SizeOf();
133 }
134 return offset;
135}
136
137size_t OatWriter::InitElfImages(size_t offset) {
138 for (size_t i = 0; i < oat_elf_images_.size(); ++i) {
139 offset = RoundUp(offset, 4);
140 oat_elf_images_[i]->SetElfOffset(offset);
141 offset += oat_elf_images_[i]->GetElfSize();
142 }
143 return offset;
144}
145
Brian Carlstrom389efb02012-01-11 12:06:26 -0800146size_t OatWriter::InitOatClasses(size_t offset) {
147 // create the OatClasses
148 // calculate the offsets within OatDexFiles to OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -0700149 for (size_t i = 0; i != dex_files_->size(); ++i) {
150 const DexFile* dex_file = (*dex_files_)[i];
151 for (size_t class_def_index = 0;
152 class_def_index < dex_file->NumClassDefs();
Ian Rogersc20a83e2012-01-18 18:15:32 -0800153 class_def_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800154 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700155 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
156 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700157 uint32_t num_methods = 0;
158 if (class_data != NULL) { // ie not an empty class, such as a marker interface
159 ClassDataItemIterator it(*dex_file, class_data);
160 size_t num_direct_methods = it.NumDirectMethods();
161 size_t num_virtual_methods = it.NumVirtualMethods();
162 num_methods = num_direct_methods + num_virtual_methods;
163 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800164
165 CompiledClass* compiled_class =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800166 compiler_->GetCompiledClass(Compiler::MethodReference(dex_file, class_def_index));
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800167 Class::Status status =
168 (compiled_class != NULL) ? compiled_class->GetStatus() : Class::kStatusNotReady;
169
170 OatClass* oat_class = new OatClass(status, num_methods);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800171 oat_classes_.push_back(oat_class);
172 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700173 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800174 oat_dex_files_[i]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700175 }
176 return offset;
177}
178
179size_t OatWriter::InitOatCode(size_t offset) {
180 // calculate the offsets within OatHeader to executable code
181 size_t old_offset = offset;
182 // required to be on a new page boundary
183 offset = RoundUp(offset, kPageSize);
184 oat_header_->SetExecutableOffset(offset);
185 executable_offset_padding_length_ = offset - old_offset;
186 return offset;
187}
188
189size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700190 size_t oat_class_index = 0;
191 for (size_t i = 0; i != dex_files_->size(); ++i) {
192 const DexFile* dex_file = (*dex_files_)[i];
193 CHECK(dex_file != NULL);
194 offset = InitOatCodeDexFile(offset, oat_class_index, *dex_file);
195 }
196 return offset;
197}
198
199size_t OatWriter::InitOatCodeDexFile(size_t offset,
200 size_t& oat_class_index,
201 const DexFile& dex_file) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800202 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700203 class_def_index < dex_file.NumClassDefs();
204 class_def_index++, oat_class_index++) {
205 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800206 offset = InitOatCodeClassDef(offset, oat_class_index, class_def_index, dex_file, class_def);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800207 oat_classes_[oat_class_index]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700208 }
209 return offset;
210}
211
212size_t OatWriter::InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800213 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700214 const DexFile& dex_file,
215 const DexFile::ClassDef& class_def) {
216 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700217 if (class_data == NULL) {
218 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700219 return offset;
220 }
Ian Rogers0571d352011-11-03 19:51:38 -0700221 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800222 CHECK_EQ(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700223 it.NumDirectMethods() + it.NumVirtualMethods());
224 // Skip fields
225 while (it.HasNextStaticField()) {
226 it.Next();
227 }
228 while (it.HasNextInstanceField()) {
229 it.Next();
230 }
231 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700232 size_t class_def_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700233 while (it.HasNextDirectMethod()) {
234 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
Ian Rogersc20a83e2012-01-18 18:15:32 -0800235 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
236 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index, is_native,
237 is_static, true, it.GetMemberIndex(), &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700238 class_def_method_index++;
239 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700240 }
Ian Rogers0571d352011-11-03 19:51:38 -0700241 while (it.HasNextVirtualMethod()) {
242 CHECK_EQ(it.GetMemberAccessFlags() & kAccStatic, 0U);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800243 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
244 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index, is_native,
245 false, false, it.GetMemberIndex(), &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700246 class_def_method_index++;
247 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700248 }
Ian Rogers0571d352011-11-03 19:51:38 -0700249 DCHECK(!it.HasNext());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700250 return offset;
251}
252
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700253size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
254 size_t __attribute__((unused)) class_def_index,
255 size_t class_def_method_index,
256 bool __attribute__((unused)) is_native,
257 bool is_static, bool is_direct,
258 uint32_t method_idx, const DexFile* dex_file) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700259 // derived from CompiledMethod if available
260 uint32_t code_offset = 0;
Brian Carlstromfd2ec542012-05-02 15:08:57 -0700261#if defined(ART_USE_LLVM_COMPILER)
Logan Chien937105a2012-04-02 02:37:37 +0800262 uint16_t code_elf_idx = static_cast<uint16_t>(-1u);
263 uint16_t code_elf_func_idx = static_cast<uint16_t>(-1u);
Brian Carlstromfd2ec542012-05-02 15:08:57 -0700264#endif
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700265 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700266 uint32_t core_spill_mask = 0;
267 uint32_t fp_spill_mask = 0;
268 uint32_t mapping_table_offset = 0;
269 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800270 uint32_t gc_map_offset = 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700271 // derived from CompiledInvokeStub if available
272 uint32_t invoke_stub_offset = 0;
Brian Carlstromfd2ec542012-05-02 15:08:57 -0700273#if defined(ART_USE_LLVM_COMPILER)
Logan Chien937105a2012-04-02 02:37:37 +0800274 uint16_t invoke_stub_elf_idx = static_cast<uint16_t>(-1u);
275 uint16_t invoke_stub_elf_func_idx = static_cast<uint16_t>(-1u);
Logan Chien7a2a23a2012-06-06 11:01:00 +0800276 uint16_t proxy_stub_elf_idx = static_cast<uint16_t>(-1u);
TDYa127eead4ac2012-06-03 07:15:25 -0700277 uint16_t proxy_stub_elf_func_idx = static_cast<uint16_t>(-1u);
Brian Carlstromfd2ec542012-05-02 15:08:57 -0700278#endif
Brian Carlstrome24fa612011-09-29 00:53:55 -0700279
Ian Rogers0571d352011-11-03 19:51:38 -0700280 CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800281 compiler_->GetCompiledMethod(Compiler::MethodReference(dex_file, method_idx));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700282 if (compiled_method != NULL) {
Logan Chienccb7bf12012-03-28 12:52:32 +0800283 if (compiled_method->IsExecutableInElf()) {
Brian Carlstromfd2ec542012-05-02 15:08:57 -0700284#if defined(ART_USE_LLVM_COMPILER)
Logan Chienccb7bf12012-03-28 12:52:32 +0800285 code_elf_idx = compiled_method->GetElfIndex();
Logan Chien937105a2012-04-02 02:37:37 +0800286 code_elf_func_idx = compiled_method->GetElfFuncIndex();
Brian Carlstromfd2ec542012-05-02 15:08:57 -0700287#endif
Logan Chien110bcba2012-04-16 19:11:28 +0800288 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
jeffhao55d78212011-11-02 11:41:50 -0700289 } else {
Logan Chienccb7bf12012-03-28 12:52:32 +0800290 offset = compiled_method->AlignCode(offset);
291 DCHECK_ALIGNED(offset, kArmAlignment);
292 const std::vector<uint8_t>& code = compiled_method->GetCode();
293 uint32_t code_size = code.size() * sizeof(code[0]);
294 CHECK_NE(code_size, 0U);
295 uint32_t thumb_offset = compiled_method->CodeDelta();
296 code_offset = offset + sizeof(code_size) + thumb_offset;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700297
Logan Chienccb7bf12012-03-28 12:52:32 +0800298 // Deduplicate code arrays
Elliott Hughesa0e18062012-04-13 15:59:59 -0700299 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Logan Chienccb7bf12012-03-28 12:52:32 +0800300 if (code_iter != code_offsets_.end()) {
301 code_offset = code_iter->second;
302 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700303 code_offsets_.Put(&code, code_offset);
Logan Chienccb7bf12012-03-28 12:52:32 +0800304 offset += sizeof(code_size); // code size is prepended before code
305 offset += code_size;
306 oat_header_->UpdateChecksum(&code[0], code_size);
307 }
308 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
309 core_spill_mask = compiled_method->GetCoreSpillMask();
310 fp_spill_mask = compiled_method->GetFpSpillMask();
jeffhao55d78212011-11-02 11:41:50 -0700311
Logan Chienccb7bf12012-03-28 12:52:32 +0800312 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
313 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
314 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700315
Logan Chienccb7bf12012-03-28 12:52:32 +0800316 // Deduplicate mapping tables
Elliott Hughesa0e18062012-04-13 15:59:59 -0700317 SafeMap<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
Logan Chienccb7bf12012-03-28 12:52:32 +0800318 if (mapping_iter != mapping_table_offsets_.end()) {
319 mapping_table_offset = mapping_iter->second;
320 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700321 mapping_table_offsets_.Put(&mapping_table, mapping_table_offset);
Logan Chienccb7bf12012-03-28 12:52:32 +0800322 offset += mapping_table_size;
323 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
324 }
jeffhao55d78212011-11-02 11:41:50 -0700325
Logan Chienccb7bf12012-03-28 12:52:32 +0800326 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
327 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
328 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800329
Logan Chienccb7bf12012-03-28 12:52:32 +0800330 // Deduplicate vmap tables
Elliott Hughesa0e18062012-04-13 15:59:59 -0700331 SafeMap<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
Logan Chienccb7bf12012-03-28 12:52:32 +0800332 if (vmap_iter != vmap_table_offsets_.end()) {
333 vmap_table_offset = vmap_iter->second;
334 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700335 vmap_table_offsets_.Put(&vmap_table, vmap_table_offset);
Logan Chienccb7bf12012-03-28 12:52:32 +0800336 offset += vmap_table_size;
337 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
338 }
339
340 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
341 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
342 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800343
Ian Rogersc20a83e2012-01-18 18:15:32 -0800344#ifndef NDEBUG
Logan Chienccb7bf12012-03-28 12:52:32 +0800345 // We expect GC maps except when the class hasn't been verified or the method is native
346 CompiledClass* compiled_class =
347 compiler_->GetCompiledClass(Compiler::MethodReference(dex_file, class_def_index));
348 Class::Status status =
349 (compiled_class != NULL) ? compiled_class->GetStatus() : Class::kStatusNotReady;
350 CHECK(gc_map_size != 0 || is_native || status < Class::kStatusVerified)
351 << &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 -0800352#endif
353
Logan Chienccb7bf12012-03-28 12:52:32 +0800354 // Deduplicate GC maps
Elliott Hughesa0e18062012-04-13 15:59:59 -0700355 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = gc_map_offsets_.find(&gc_map);
Logan Chienccb7bf12012-03-28 12:52:32 +0800356 if (gc_map_iter != gc_map_offsets_.end()) {
357 gc_map_offset = gc_map_iter->second;
358 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700359 gc_map_offsets_.Put(&gc_map, gc_map_offset);
Logan Chienccb7bf12012-03-28 12:52:32 +0800360 offset += gc_map_size;
361 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
362 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800363 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700364 }
365
Ian Rogers0571d352011-11-03 19:51:38 -0700366 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx));
367 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700368 if (compiled_invoke_stub != NULL) {
Logan Chienccb7bf12012-03-28 12:52:32 +0800369 if (compiled_invoke_stub->IsExecutableInElf()) {
Brian Carlstromfd2ec542012-05-02 15:08:57 -0700370#if defined(ART_USE_LLVM_COMPILER)
Logan Chien7a2a23a2012-06-06 11:01:00 +0800371 invoke_stub_elf_idx = compiled_invoke_stub->GetElfIndex();
372 invoke_stub_elf_func_idx = compiled_invoke_stub->GetElfFuncIndex();
Shih-wei Liaoe5fc3342012-06-03 11:41:12 -0700373#endif
jeffhao55d78212011-11-02 11:41:50 -0700374 } else {
Logan Chienccb7bf12012-03-28 12:52:32 +0800375 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
376 DCHECK_ALIGNED(offset, kArmAlignment);
377 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
378 uint32_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
379 CHECK_NE(invoke_stub_size, 0U);
Logan Chien4284bb92012-06-06 15:30:44 +0800380 uint32_t thumb_offset = compiled_invoke_stub->CodeDelta();
381 invoke_stub_offset = offset + sizeof(invoke_stub_size) + thumb_offset;
Logan Chienccb7bf12012-03-28 12:52:32 +0800382
383 // Deduplicate invoke stubs
Elliott Hughesa0e18062012-04-13 15:59:59 -0700384 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&invoke_stub);
Logan Chienccb7bf12012-03-28 12:52:32 +0800385 if (stub_iter != code_offsets_.end()) {
386 invoke_stub_offset = stub_iter->second;
387 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700388 code_offsets_.Put(&invoke_stub, invoke_stub_offset);
Logan Chienccb7bf12012-03-28 12:52:32 +0800389 offset += sizeof(invoke_stub_size); // invoke stub size is prepended before code
390 offset += invoke_stub_size;
391 oat_header_->UpdateChecksum(&invoke_stub[0], invoke_stub_size);
392 }
jeffhao55d78212011-11-02 11:41:50 -0700393 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700394 }
395
Logan Chien7a2a23a2012-06-06 11:01:00 +0800396#if defined(ART_USE_LLVM_COMPILER)
397 if (!is_static) {
398 const CompiledInvokeStub* compiled_proxy_stub = compiler_->FindProxyStub(shorty);
399 if (compiled_proxy_stub != NULL) {
400 DCHECK(compiled_proxy_stub->IsExecutableInElf());
401 proxy_stub_elf_idx = compiled_proxy_stub->GetElfIndex();
402 proxy_stub_elf_func_idx = compiled_proxy_stub->GetElfFuncIndex();
403 }
404 }
405#endif
406
Brian Carlstrom389efb02012-01-11 12:06:26 -0800407 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index]
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700408 = OatMethodOffsets(code_offset,
409 frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700410 core_spill_mask,
411 fp_spill_mask,
412 mapping_table_offset,
413 vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800414 gc_map_offset,
Logan Chienccb7bf12012-03-28 12:52:32 +0800415 invoke_stub_offset
416#if defined(ART_USE_LLVM_COMPILER)
417 , code_elf_idx,
Logan Chien937105a2012-04-02 02:37:37 +0800418 code_elf_func_idx,
419 invoke_stub_elf_idx,
TDYa127eead4ac2012-06-03 07:15:25 -0700420 invoke_stub_elf_func_idx,
Logan Chien7a2a23a2012-06-06 11:01:00 +0800421 proxy_stub_elf_idx,
TDYa127eead4ac2012-06-03 07:15:25 -0700422 proxy_stub_elf_func_idx
Logan Chienccb7bf12012-03-28 12:52:32 +0800423#endif
424 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700425
Ian Rogers0571d352011-11-03 19:51:38 -0700426 if (compiler_->IsImage()) {
427 ClassLinker* linker = Runtime::Current()->GetClassLinker();
428 DexCache* dex_cache = linker->FindDexCache(*dex_file);
429 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader_,
430 is_direct);
431 CHECK(method != NULL);
432 method->SetFrameSizeInBytes(frame_size_in_bytes);
433 method->SetCoreSpillMask(core_spill_mask);
434 method->SetFpSpillMask(fp_spill_mask);
435 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800436 // Don't overwrite static method trampoline
437 if (!method->IsStatic() || method->IsConstructor() ||
438 method->GetDeclaringClass()->IsInitialized()) {
439 method->SetOatCodeOffset(code_offset);
440 } else {
441 method->SetCode(Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod)->GetData());
442 }
Ian Rogers0571d352011-11-03 19:51:38 -0700443 method->SetOatVmapTableOffset(vmap_table_offset);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800444 method->SetOatGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700445 method->SetOatInvokeStubOffset(invoke_stub_offset);
446 }
Logan Chien8b977d32012-02-21 19:14:55 +0800447
Brian Carlstrome24fa612011-09-29 00:53:55 -0700448 return offset;
449}
450
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700451#define DCHECK_CODE_OFFSET() \
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800452 DCHECK_EQ(static_cast<off_t>(code_offset), lseek(file->Fd(), 0, SEEK_CUR))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700453
Elliott Hughes234da572011-11-03 22:13:06 -0700454bool OatWriter::Write(File* file) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700455 if (!file->WriteFully(oat_header_, sizeof(*oat_header_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700456 PLOG(ERROR) << "Failed to write oat header to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700457 return false;
458 }
459
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700460 if (!file->WriteFully(image_file_location_.data(),
461 image_file_location_.size())) {
462 PLOG(ERROR) << "Failed to write oat header image file location to " << file->name();
463 return false;
464 }
465
Elliott Hughes234da572011-11-03 22:13:06 -0700466 if (!WriteTables(file)) {
467 LOG(ERROR) << "Failed to write oat tables to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700468 return false;
469 }
470
Elliott Hughes234da572011-11-03 22:13:06 -0700471 size_t code_offset = WriteCode(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700472 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700473 LOG(ERROR) << "Failed to write oat code to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700474 return false;
475 }
476
Elliott Hughes234da572011-11-03 22:13:06 -0700477 code_offset = WriteCodeDexFiles(file, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700478 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700479 LOG(ERROR) << "Failed to write oat code for dex files to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700480 return false;
481 }
482
483 return true;
484}
485
486bool OatWriter::WriteTables(File* file) {
487 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
488 if (!oat_dex_files_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700489 PLOG(ERROR) << "Failed to write oat dex information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700490 return false;
491 }
492 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800493 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
494 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800495 off_t actual_offset = lseek(file->Fd(), expected_offset, SEEK_SET);
Brian Carlstrom89521892011-12-07 22:05:07 -0800496 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
497 const DexFile* dex_file = (*dex_files_)[i];
498 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
499 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
500 return false;
501 }
502 const DexFile* dex_file = (*dex_files_)[i];
503 if (!file->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
504 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << file->name();
505 return false;
506 }
507 }
Logan Chien25ae6402012-03-20 20:19:26 +0800508 for (size_t i = 0; i != oat_elf_images_.size(); ++i) {
509 if (!oat_elf_images_[i]->Write(file)) {
510 PLOG(ERROR) << "Failed to write oat elf information to " << file->name();
511 return false;
512 }
513 }
514 for (size_t i = 0; i != oat_elf_images_.size(); ++i) {
515 uint32_t expected_offset = oat_elf_images_[i]->GetElfOffset();
516 off_t actual_offset = lseek(file->Fd(), expected_offset, SEEK_SET);
517 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
518 PLOG(ERROR) << "Failed to seek to dex file section."
519 << " Actual: " << actual_offset
520 << " Expected: " << expected_offset;
521 return false;
522 }
523 if (!oat_elf_images_[i]->WriteElfImage(file)) {
524 return false;
525 }
526 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800527 for (size_t i = 0; i != oat_classes_.size(); ++i) {
528 if (!oat_classes_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700529 PLOG(ERROR) << "Failed to write oat methods information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700530 return false;
531 }
532 }
533 return true;
534}
535
536size_t OatWriter::WriteCode(File* file) {
537 uint32_t code_offset = oat_header_->GetExecutableOffset();
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800538 off_t new_offset = lseek(file->Fd(), executable_offset_padding_length_, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700539 if (static_cast<uint32_t>(new_offset) != code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700540 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700541 << " Expected: " << code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700542 return 0;
543 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700544 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700545 return code_offset;
546}
547
548size_t OatWriter::WriteCodeDexFiles(File* file, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700549 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800550 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700551 const DexFile* dex_file = (*dex_files_)[i];
552 CHECK(dex_file != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700553 code_offset = WriteCodeDexFile(file, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700554 if (code_offset == 0) {
555 return 0;
556 }
557 }
558 return code_offset;
559}
560
Ian Rogers0571d352011-11-03 19:51:38 -0700561size_t OatWriter::WriteCodeDexFile(File* file, size_t code_offset, size_t& oat_class_index,
562 const DexFile& dex_file) {
563 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
564 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700565 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700566 code_offset = WriteCodeClassDef(file, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700567 if (code_offset == 0) {
568 return 0;
569 }
570 }
571 return code_offset;
572}
573
Ian Rogers0571d352011-11-03 19:51:38 -0700574void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
575 const DexFile& dex_file, File* f) const {
576 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
577 << " to " << f->name();
Elliott Hughes234da572011-11-03 22:13:06 -0700578}
579
Brian Carlstrome24fa612011-09-29 00:53:55 -0700580size_t OatWriter::WriteCodeClassDef(File* file,
Ian Rogers0571d352011-11-03 19:51:38 -0700581 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700582 const DexFile& dex_file,
583 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700584 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700585 if (class_data == NULL) {
586 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700587 return code_offset;
588 }
Ian Rogers0571d352011-11-03 19:51:38 -0700589 ClassDataItemIterator it(dex_file, class_data);
590 // Skip fields
591 while (it.HasNextStaticField()) {
592 it.Next();
593 }
594 while (it.HasNextInstanceField()) {
595 it.Next();
596 }
597 // Process methods
598 size_t class_def_method_index = 0;
599 while (it.HasNextDirectMethod()) {
600 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
601 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
602 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700603 if (code_offset == 0) {
604 return 0;
605 }
Ian Rogers0571d352011-11-03 19:51:38 -0700606 class_def_method_index++;
607 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700608 }
Ian Rogers0571d352011-11-03 19:51:38 -0700609 while (it.HasNextVirtualMethod()) {
610 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
611 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700612 if (code_offset == 0) {
613 return 0;
614 }
Ian Rogers0571d352011-11-03 19:51:38 -0700615 class_def_method_index++;
616 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700617 }
618 return code_offset;
619}
620
Ian Rogers0571d352011-11-03 19:51:38 -0700621size_t OatWriter::WriteCodeMethod(File* file, size_t code_offset, size_t oat_class_index,
622 size_t class_def_method_index, bool is_static,
623 uint32_t method_idx, const DexFile& dex_file) {
624 const CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800625 compiler_->GetCompiledMethod(Compiler::MethodReference(&dex_file, method_idx));
Ian Rogers0571d352011-11-03 19:51:38 -0700626
Ian Rogers0571d352011-11-03 19:51:38 -0700627 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800628 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700629
630
631 if (compiled_method != NULL) { // ie. not an abstract method
Logan Chienccb7bf12012-03-28 12:52:32 +0800632 if (!compiled_method->IsExecutableInElf()) {
633 uint32_t aligned_code_offset = compiled_method->AlignCode(code_offset);
634 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
635 if (aligned_code_delta != 0) {
636 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
637 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
638 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
639 << " Expected: " << aligned_code_offset << " File: " << file->name();
640 return 0;
641 }
642 code_offset += aligned_code_delta;
643 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700644 }
Logan Chienccb7bf12012-03-28 12:52:32 +0800645 DCHECK_ALIGNED(code_offset, kArmAlignment);
646 const std::vector<uint8_t>& code = compiled_method->GetCode();
647 uint32_t code_size = code.size() * sizeof(code[0]);
648 CHECK_NE(code_size, 0U);
649
650 // Deduplicate code arrays
651 size_t offset = code_offset + sizeof(code_size) + compiled_method->CodeDelta();
Elliott Hughesa0e18062012-04-13 15:59:59 -0700652 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
Logan Chienccb7bf12012-03-28 12:52:32 +0800653 if (code_iter != code_offsets_.end() && offset != method_offsets.code_offset_) {
654 DCHECK(code_iter->second == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
655 } else {
656 DCHECK(offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
657 if (!file->WriteFully(&code_size, sizeof(code_size))) {
658 ReportWriteFailure("method code size", method_idx, dex_file, file);
659 return 0;
660 }
661 code_offset += sizeof(code_size);
662 DCHECK_CODE_OFFSET();
663 if (!file->WriteFully(&code[0], code_size)) {
664 ReportWriteFailure("method code", method_idx, dex_file, file);
665 return 0;
666 }
667 code_offset += code_size;
668 }
669 DCHECK_CODE_OFFSET();
Logan Chienccb7bf12012-03-28 12:52:32 +0800670
671 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
672 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
673
674 // Deduplicate mapping tables
Elliott Hughesa0e18062012-04-13 15:59:59 -0700675 SafeMap<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
Logan Chienccb7bf12012-03-28 12:52:32 +0800676 mapping_table_offsets_.find(&mapping_table);
677 if (mapping_iter != mapping_table_offsets_.end() &&
678 code_offset != method_offsets.mapping_table_offset_) {
679 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
680 || mapping_iter->second == method_offsets.mapping_table_offset_)
681 << PrettyMethod(method_idx, dex_file);
682 } else {
683 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
684 || code_offset == method_offsets.mapping_table_offset_)
685 << PrettyMethod(method_idx, dex_file);
686 if (!file->WriteFully(&mapping_table[0], mapping_table_size)) {
687 ReportWriteFailure("mapping table", method_idx, dex_file, file);
688 return 0;
689 }
690 code_offset += mapping_table_size;
691 }
692 DCHECK_CODE_OFFSET();
693
694 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
695 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
696
697 // Deduplicate vmap tables
Elliott Hughesa0e18062012-04-13 15:59:59 -0700698 SafeMap<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
Logan Chienccb7bf12012-03-28 12:52:32 +0800699 vmap_table_offsets_.find(&vmap_table);
700 if (vmap_iter != vmap_table_offsets_.end() &&
701 code_offset != method_offsets.vmap_table_offset_) {
702 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
703 || vmap_iter->second == method_offsets.vmap_table_offset_)
704 << PrettyMethod(method_idx, dex_file);
705 } else {
706 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
707 || code_offset == method_offsets.vmap_table_offset_)
708 << PrettyMethod(method_idx, dex_file);
709 if (!file->WriteFully(&vmap_table[0], vmap_table_size)) {
710 ReportWriteFailure("vmap table", method_idx, dex_file, file);
711 return 0;
712 }
713 code_offset += vmap_table_size;
714 }
715 DCHECK_CODE_OFFSET();
716
717 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
718 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
719
720 // Deduplicate GC maps
Elliott Hughesa0e18062012-04-13 15:59:59 -0700721 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
Logan Chienccb7bf12012-03-28 12:52:32 +0800722 gc_map_offsets_.find(&gc_map);
723 if (gc_map_iter != gc_map_offsets_.end() &&
724 code_offset != method_offsets.gc_map_offset_) {
725 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
726 || gc_map_iter->second == method_offsets.gc_map_offset_)
727 << PrettyMethod(method_idx, dex_file);
728 } else {
729 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
730 || code_offset == method_offsets.gc_map_offset_)
731 << PrettyMethod(method_idx, dex_file);
732 if (!file->WriteFully(&gc_map[0], gc_map_size)) {
733 ReportWriteFailure("GC map", method_idx, dex_file, file);
734 return 0;
735 }
736 code_offset += gc_map_size;
737 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700738 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700739 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700740 }
Ian Rogers0571d352011-11-03 19:51:38 -0700741 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
742 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700743 if (compiled_invoke_stub != NULL) {
Logan Chienccb7bf12012-03-28 12:52:32 +0800744 if (!compiled_invoke_stub->IsExecutableInElf()) {
745 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
746 compiler_->GetInstructionSet());
747 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
748 if (aligned_code_delta != 0) {
749 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
750 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
751 PLOG(ERROR) << "Failed to seek to align invoke stub code. Actual: " << new_offset
752 << " Expected: " << aligned_code_offset;
753 return 0;
754 }
755 code_offset += aligned_code_delta;
756 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700757 }
Logan Chienccb7bf12012-03-28 12:52:32 +0800758 DCHECK_ALIGNED(code_offset, kArmAlignment);
759 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
760 uint32_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
761 CHECK_NE(invoke_stub_size, 0U);
jeffhao55d78212011-11-02 11:41:50 -0700762
Logan Chienccb7bf12012-03-28 12:52:32 +0800763 // Deduplicate invoke stubs
Logan Chien4284bb92012-06-06 15:30:44 +0800764 size_t offset = code_offset + sizeof(invoke_stub_size) + compiled_invoke_stub->CodeDelta();
Elliott Hughesa0e18062012-04-13 15:59:59 -0700765 SafeMap<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter =
Logan Chienccb7bf12012-03-28 12:52:32 +0800766 code_offsets_.find(&invoke_stub);
767 if (stub_iter != code_offsets_.end() && offset != method_offsets.invoke_stub_offset_) {
768 DCHECK(stub_iter->second == method_offsets.invoke_stub_offset_) << PrettyMethod(method_idx, dex_file);
769 } else {
770 DCHECK(offset == method_offsets.invoke_stub_offset_) << PrettyMethod(method_idx, dex_file);
771 if (!file->WriteFully(&invoke_stub_size, sizeof(invoke_stub_size))) {
772 ReportWriteFailure("invoke stub code size", method_idx, dex_file, file);
773 return 0;
774 }
775 code_offset += sizeof(invoke_stub_size);
776 DCHECK_CODE_OFFSET();
777 if (!file->WriteFully(&invoke_stub[0], invoke_stub_size)) {
778 ReportWriteFailure("invoke stub code", method_idx, dex_file, file);
779 return 0;
780 }
781 code_offset += invoke_stub_size;
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700782 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700783 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700784 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700785 }
Logan Chien8b977d32012-02-21 19:14:55 +0800786
Brian Carlstrome24fa612011-09-29 00:53:55 -0700787 return code_offset;
788}
789
Brian Carlstrome24fa612011-09-29 00:53:55 -0700790OatWriter::OatDexFile::OatDexFile(const DexFile& dex_file) {
Elliott Hughes95572412011-12-13 18:14:20 -0800791 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700792 dex_file_location_size_ = location.size();
793 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800794 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800795 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800796 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700797}
798
799size_t OatWriter::OatDexFile::SizeOf() const {
800 return sizeof(dex_file_location_size_)
801 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800802 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800803 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800804 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700805}
806
807void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
808 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
809 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800810 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800811 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800812 oat_header.UpdateChecksum(&methods_offsets_[0],
813 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700814}
815
816bool OatWriter::OatDexFile::Write(File* file) const {
817 if (!file->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700818 PLOG(ERROR) << "Failed to write dex file location length to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700819 return false;
820 }
821 if (!file->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700822 PLOG(ERROR) << "Failed to write dex file location data to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700823 return false;
824 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800825 if (!file->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
826 PLOG(ERROR) << "Failed to write dex file location checksum to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700827 return false;
828 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800829 if (!file->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
830 PLOG(ERROR) << "Failed to write dex file offset to " << file->name();
831 return false;
832 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800833 if (!file->WriteFully(&methods_offsets_[0],
834 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700835 PLOG(ERROR) << "Failed to write methods offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700836 return false;
837 }
838 return true;
839}
840
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800841OatWriter::OatClass::OatClass(Class::Status status, uint32_t methods_count) {
842 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700843 method_offsets_.resize(methods_count);
844}
845
Brian Carlstrom389efb02012-01-11 12:06:26 -0800846size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800847 return sizeof(status_)
848 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700849}
850
Brian Carlstrom389efb02012-01-11 12:06:26 -0800851void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800852 oat_header.UpdateChecksum(&status_, sizeof(status_));
853 oat_header.UpdateChecksum(&method_offsets_[0],
854 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700855}
856
Brian Carlstrom389efb02012-01-11 12:06:26 -0800857bool OatWriter::OatClass::Write(File* file) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800858 if (!file->WriteFully(&status_, sizeof(status_))) {
859 PLOG(ERROR) << "Failed to write class status to " << file->name();
860 return false;
861 }
862 if (!file->WriteFully(&method_offsets_[0],
863 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700864 PLOG(ERROR) << "Failed to write method offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700865 return false;
866 }
867 return true;
868}
869
Logan Chien25ae6402012-03-20 20:19:26 +0800870OatWriter::OatElfImage::OatElfImage(const ElfImage& image)
871 : elf_offset_(0), elf_size_(image.size()), elf_addr_(image.begin()) {
872}
873
874size_t OatWriter::OatElfImage::SizeOf() const {
875 return (sizeof(elf_offset_) + sizeof(elf_size_));
876}
877
878uint32_t OatWriter::OatElfImage::GetElfSize() const {
879 return elf_size_;
880}
881
882uint32_t OatWriter::OatElfImage::GetElfOffset() const {
883 DCHECK_NE(elf_offset_, 0U);
884 return elf_offset_;
885}
886
887void OatWriter::OatElfImage::SetElfOffset(uint32_t offset) {
888 DCHECK_NE(offset, 0U);
Elliott Hughes74847412012-06-20 18:10:21 -0700889 DCHECK_EQ((offset & 0x3LU), 0U);
Logan Chien25ae6402012-03-20 20:19:26 +0800890 elf_offset_ = offset;
891}
892
893bool OatWriter::OatElfImage::Write(File* file) const {
894 DCHECK_NE(elf_offset_, 0U);
895 if (!file->WriteFully(&elf_offset_, sizeof(elf_offset_))) {
896 PLOG(ERROR) << "Failed to write ELF offset to " << file->name();
897 return false;
898 }
899 if (!file->WriteFully(&elf_size_, sizeof(elf_size_))) {
900 PLOG(ERROR) << "Failed to write ELF size to " << file->name();
901 return false;
902 }
903 return true;
904}
905
906bool OatWriter::OatElfImage::WriteElfImage(File* file) const {
907 if (!file->WriteFully(elf_addr_, elf_size_)) {
908 PLOG(ERROR) << "Failed to write ELF image to " << file->name();
909 return false;
910 }
911 return true;
912}
913
Brian Carlstrome24fa612011-09-29 00:53:55 -0700914} // namespace art