blob: f08aa8d773d1587b95f9cf6cb69f193d9505a48a [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) {
Shih-wei Liao8e5e9782012-03-24 13:18:46 -0700118 size_t n = elf_images_.size();
119 if (n != 0) {
120 // Offset to ELF image table should be rounded up to 4-byte aligned, so that
121 // we can read the uint32_t directly.
122 offset = RoundUp(offset, 4);
123 oat_header_->SetElfImageTableOffset(offset);
124 } else {
125 oat_header_->SetElfImageTableOffset(0);
126 }
Logan Chien25ae6402012-03-20 20:19:26 +0800127
Shih-wei Liao8e5e9782012-03-24 13:18:46 -0700128 for (size_t i = 0; i < n; ++i) {
Logan Chien25ae6402012-03-20 20:19:26 +0800129 OatElfImage* oat_elf_image = new OatElfImage(elf_images_[i]);
130 oat_elf_images_.push_back(oat_elf_image);
131 offset += oat_elf_image->SizeOf();
132 }
133 return offset;
134}
135
136size_t OatWriter::InitElfImages(size_t offset) {
137 for (size_t i = 0; i < oat_elf_images_.size(); ++i) {
138 offset = RoundUp(offset, 4);
139 oat_elf_images_[i]->SetElfOffset(offset);
140 offset += oat_elf_images_[i]->GetElfSize();
141 }
142 return offset;
143}
144
Brian Carlstrom389efb02012-01-11 12:06:26 -0800145size_t OatWriter::InitOatClasses(size_t offset) {
146 // create the OatClasses
147 // calculate the offsets within OatDexFiles to OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -0700148 for (size_t i = 0; i != dex_files_->size(); ++i) {
149 const DexFile* dex_file = (*dex_files_)[i];
150 for (size_t class_def_index = 0;
151 class_def_index < dex_file->NumClassDefs();
Ian Rogersc20a83e2012-01-18 18:15:32 -0800152 class_def_index++) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800153 oat_dex_files_[i]->methods_offsets_[class_def_index] = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700154 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
155 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700156 uint32_t num_methods = 0;
157 if (class_data != NULL) { // ie not an empty class, such as a marker interface
158 ClassDataItemIterator it(*dex_file, class_data);
159 size_t num_direct_methods = it.NumDirectMethods();
160 size_t num_virtual_methods = it.NumVirtualMethods();
161 num_methods = num_direct_methods + num_virtual_methods;
162 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800163
164 CompiledClass* compiled_class =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800165 compiler_->GetCompiledClass(Compiler::MethodReference(dex_file, class_def_index));
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800166 Class::Status status =
167 (compiled_class != NULL) ? compiled_class->GetStatus() : Class::kStatusNotReady;
168
169 OatClass* oat_class = new OatClass(status, num_methods);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800170 oat_classes_.push_back(oat_class);
171 offset += oat_class->SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700172 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800173 oat_dex_files_[i]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700174 }
175 return offset;
176}
177
178size_t OatWriter::InitOatCode(size_t offset) {
179 // calculate the offsets within OatHeader to executable code
180 size_t old_offset = offset;
181 // required to be on a new page boundary
182 offset = RoundUp(offset, kPageSize);
183 oat_header_->SetExecutableOffset(offset);
184 executable_offset_padding_length_ = offset - old_offset;
185 return offset;
186}
187
188size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700189 size_t oat_class_index = 0;
190 for (size_t i = 0; i != dex_files_->size(); ++i) {
191 const DexFile* dex_file = (*dex_files_)[i];
192 CHECK(dex_file != NULL);
193 offset = InitOatCodeDexFile(offset, oat_class_index, *dex_file);
194 }
195 return offset;
196}
197
198size_t OatWriter::InitOatCodeDexFile(size_t offset,
199 size_t& oat_class_index,
200 const DexFile& dex_file) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800201 for (size_t class_def_index = 0;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700202 class_def_index < dex_file.NumClassDefs();
203 class_def_index++, oat_class_index++) {
204 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800205 offset = InitOatCodeClassDef(offset, oat_class_index, class_def_index, dex_file, class_def);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800206 oat_classes_[oat_class_index]->UpdateChecksum(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700207 }
208 return offset;
209}
210
211size_t OatWriter::InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800212 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700213 const DexFile& dex_file,
214 const DexFile::ClassDef& class_def) {
215 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700216 if (class_data == NULL) {
217 // empty class, such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700218 return offset;
219 }
Ian Rogers0571d352011-11-03 19:51:38 -0700220 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800221 CHECK_EQ(oat_classes_[oat_class_index]->method_offsets_.size(),
Ian Rogers0571d352011-11-03 19:51:38 -0700222 it.NumDirectMethods() + it.NumVirtualMethods());
223 // Skip fields
224 while (it.HasNextStaticField()) {
225 it.Next();
226 }
227 while (it.HasNextInstanceField()) {
228 it.Next();
229 }
230 // Process methods
Brian Carlstrome24fa612011-09-29 00:53:55 -0700231 size_t class_def_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700232 while (it.HasNextDirectMethod()) {
233 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
Ian Rogersc20a83e2012-01-18 18:15:32 -0800234 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
235 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index, is_native,
236 is_static, true, it.GetMemberIndex(), &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700237 class_def_method_index++;
238 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700239 }
Ian Rogers0571d352011-11-03 19:51:38 -0700240 while (it.HasNextVirtualMethod()) {
241 CHECK_EQ(it.GetMemberAccessFlags() & kAccStatic, 0U);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800242 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
243 offset = InitOatCodeMethod(offset, oat_class_index, class_def_index, class_def_method_index, is_native,
244 false, false, it.GetMemberIndex(), &dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700245 class_def_method_index++;
246 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700247 }
Ian Rogers0571d352011-11-03 19:51:38 -0700248 DCHECK(!it.HasNext());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700249 return offset;
250}
251
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700252size_t OatWriter::InitOatCodeMethod(size_t offset, size_t oat_class_index,
253 size_t __attribute__((unused)) class_def_index,
254 size_t class_def_method_index,
255 bool __attribute__((unused)) is_native,
256 bool is_static, bool is_direct,
257 uint32_t method_idx, const DexFile* dex_file) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700258 // derived from CompiledMethod if available
259 uint32_t code_offset = 0;
Logan Chien937105a2012-04-02 02:37:37 +0800260 uint16_t code_elf_idx = static_cast<uint16_t>(-1u);
261 uint16_t code_elf_func_idx = static_cast<uint16_t>(-1u);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700262 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700263 uint32_t core_spill_mask = 0;
264 uint32_t fp_spill_mask = 0;
265 uint32_t mapping_table_offset = 0;
266 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800267 uint32_t gc_map_offset = 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700268 // derived from CompiledInvokeStub if available
269 uint32_t invoke_stub_offset = 0;
Logan Chien937105a2012-04-02 02:37:37 +0800270 uint16_t invoke_stub_elf_idx = static_cast<uint16_t>(-1u);
271 uint16_t invoke_stub_elf_func_idx = static_cast<uint16_t>(-1u);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700272
Ian Rogers0571d352011-11-03 19:51:38 -0700273 CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800274 compiler_->GetCompiledMethod(Compiler::MethodReference(dex_file, method_idx));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700275 if (compiled_method != NULL) {
Logan Chienccb7bf12012-03-28 12:52:32 +0800276 if (compiled_method->IsExecutableInElf()) {
277 code_elf_idx = compiled_method->GetElfIndex();
Logan Chien937105a2012-04-02 02:37:37 +0800278 code_elf_func_idx = compiled_method->GetElfFuncIndex();
jeffhao55d78212011-11-02 11:41:50 -0700279 } else {
Logan Chienccb7bf12012-03-28 12:52:32 +0800280 offset = compiled_method->AlignCode(offset);
281 DCHECK_ALIGNED(offset, kArmAlignment);
282 const std::vector<uint8_t>& code = compiled_method->GetCode();
283 uint32_t code_size = code.size() * sizeof(code[0]);
284 CHECK_NE(code_size, 0U);
285 uint32_t thumb_offset = compiled_method->CodeDelta();
286 code_offset = offset + sizeof(code_size) + thumb_offset;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700287
Logan Chienccb7bf12012-03-28 12:52:32 +0800288 // Deduplicate code arrays
289 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
290 if (code_iter != code_offsets_.end()) {
291 code_offset = code_iter->second;
292 } else {
293 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&code, code_offset));
294 offset += sizeof(code_size); // code size is prepended before code
295 offset += code_size;
296 oat_header_->UpdateChecksum(&code[0], code_size);
297 }
298 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
299 core_spill_mask = compiled_method->GetCoreSpillMask();
300 fp_spill_mask = compiled_method->GetFpSpillMask();
jeffhao55d78212011-11-02 11:41:50 -0700301
Logan Chienccb7bf12012-03-28 12:52:32 +0800302 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
303 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
304 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700305
Logan Chienccb7bf12012-03-28 12:52:32 +0800306 // Deduplicate mapping tables
307 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
308 if (mapping_iter != mapping_table_offsets_.end()) {
309 mapping_table_offset = mapping_iter->second;
310 } else {
311 mapping_table_offsets_.insert(std::pair<const std::vector<uint32_t>*, uint32_t>(&mapping_table, mapping_table_offset));
312 offset += mapping_table_size;
313 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
314 }
jeffhao55d78212011-11-02 11:41:50 -0700315
Logan Chienccb7bf12012-03-28 12:52:32 +0800316 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
317 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
318 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800319
Logan Chienccb7bf12012-03-28 12:52:32 +0800320 // Deduplicate vmap tables
321 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
322 if (vmap_iter != vmap_table_offsets_.end()) {
323 vmap_table_offset = vmap_iter->second;
324 } else {
325 vmap_table_offsets_.insert(std::pair<const std::vector<uint16_t>*, uint32_t>(&vmap_table, vmap_table_offset));
326 offset += vmap_table_size;
327 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
328 }
329
330 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
331 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
332 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800333
Ian Rogersc20a83e2012-01-18 18:15:32 -0800334#ifndef NDEBUG
Logan Chienccb7bf12012-03-28 12:52:32 +0800335 // We expect GC maps except when the class hasn't been verified or the method is native
336 CompiledClass* compiled_class =
337 compiler_->GetCompiledClass(Compiler::MethodReference(dex_file, class_def_index));
338 Class::Status status =
339 (compiled_class != NULL) ? compiled_class->GetStatus() : Class::kStatusNotReady;
340 CHECK(gc_map_size != 0 || is_native || status < Class::kStatusVerified)
341 << &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 -0800342#endif
343
Logan Chienccb7bf12012-03-28 12:52:32 +0800344 // Deduplicate GC maps
345 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = gc_map_offsets_.find(&gc_map);
346 if (gc_map_iter != gc_map_offsets_.end()) {
347 gc_map_offset = gc_map_iter->second;
348 } else {
349 gc_map_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&gc_map, gc_map_offset));
350 offset += gc_map_size;
351 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
352 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800353 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700354 }
355
Ian Rogers0571d352011-11-03 19:51:38 -0700356 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx));
357 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700358 if (compiled_invoke_stub != NULL) {
Logan Chienccb7bf12012-03-28 12:52:32 +0800359 if (compiled_invoke_stub->IsExecutableInElf()) {
360 invoke_stub_elf_idx = compiled_invoke_stub->GetElfIndex();
Logan Chien937105a2012-04-02 02:37:37 +0800361 invoke_stub_elf_func_idx = compiled_invoke_stub->GetElfFuncIndex();
jeffhao55d78212011-11-02 11:41:50 -0700362 } else {
Logan Chienccb7bf12012-03-28 12:52:32 +0800363 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
364 DCHECK_ALIGNED(offset, kArmAlignment);
365 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
366 uint32_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
367 CHECK_NE(invoke_stub_size, 0U);
368 invoke_stub_offset = offset + sizeof(invoke_stub_size);
369
370 // Deduplicate invoke stubs
371 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&invoke_stub);
372 if (stub_iter != code_offsets_.end()) {
373 invoke_stub_offset = stub_iter->second;
374 } else {
375 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&invoke_stub, invoke_stub_offset));
376 offset += sizeof(invoke_stub_size); // invoke stub size is prepended before code
377 offset += invoke_stub_size;
378 oat_header_->UpdateChecksum(&invoke_stub[0], invoke_stub_size);
379 }
jeffhao55d78212011-11-02 11:41:50 -0700380 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700381 }
382
Brian Carlstrom389efb02012-01-11 12:06:26 -0800383 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index]
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700384 = OatMethodOffsets(code_offset,
385 frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700386 core_spill_mask,
387 fp_spill_mask,
388 mapping_table_offset,
389 vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800390 gc_map_offset,
Logan Chienccb7bf12012-03-28 12:52:32 +0800391 invoke_stub_offset
392#if defined(ART_USE_LLVM_COMPILER)
393 , code_elf_idx,
Logan Chien937105a2012-04-02 02:37:37 +0800394 code_elf_func_idx,
395 invoke_stub_elf_idx,
396 invoke_stub_elf_func_idx
Logan Chienccb7bf12012-03-28 12:52:32 +0800397#endif
398 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700399
Ian Rogers0571d352011-11-03 19:51:38 -0700400 if (compiler_->IsImage()) {
401 ClassLinker* linker = Runtime::Current()->GetClassLinker();
402 DexCache* dex_cache = linker->FindDexCache(*dex_file);
403 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader_,
404 is_direct);
405 CHECK(method != NULL);
406 method->SetFrameSizeInBytes(frame_size_in_bytes);
407 method->SetCoreSpillMask(core_spill_mask);
408 method->SetFpSpillMask(fp_spill_mask);
409 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800410 // Don't overwrite static method trampoline
411 if (!method->IsStatic() || method->IsConstructor() ||
412 method->GetDeclaringClass()->IsInitialized()) {
413 method->SetOatCodeOffset(code_offset);
414 } else {
415 method->SetCode(Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod)->GetData());
416 }
Ian Rogers0571d352011-11-03 19:51:38 -0700417 method->SetOatVmapTableOffset(vmap_table_offset);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800418 method->SetOatGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700419 method->SetOatInvokeStubOffset(invoke_stub_offset);
420 }
Logan Chien8b977d32012-02-21 19:14:55 +0800421
Brian Carlstrome24fa612011-09-29 00:53:55 -0700422 return offset;
423}
424
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700425#define DCHECK_CODE_OFFSET() \
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800426 DCHECK_EQ(static_cast<off_t>(code_offset), lseek(file->Fd(), 0, SEEK_CUR))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700427
Elliott Hughes234da572011-11-03 22:13:06 -0700428bool OatWriter::Write(File* file) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700429 if (!file->WriteFully(oat_header_, sizeof(*oat_header_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700430 PLOG(ERROR) << "Failed to write oat header to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700431 return false;
432 }
433
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700434 if (!file->WriteFully(image_file_location_.data(),
435 image_file_location_.size())) {
436 PLOG(ERROR) << "Failed to write oat header image file location to " << file->name();
437 return false;
438 }
439
Elliott Hughes234da572011-11-03 22:13:06 -0700440 if (!WriteTables(file)) {
441 LOG(ERROR) << "Failed to write oat tables to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700442 return false;
443 }
444
Elliott Hughes234da572011-11-03 22:13:06 -0700445 size_t code_offset = WriteCode(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700446 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700447 LOG(ERROR) << "Failed to write oat code to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700448 return false;
449 }
450
Elliott Hughes234da572011-11-03 22:13:06 -0700451 code_offset = WriteCodeDexFiles(file, code_offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700452 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700453 LOG(ERROR) << "Failed to write oat code for dex files to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700454 return false;
455 }
456
457 return true;
458}
459
460bool OatWriter::WriteTables(File* file) {
461 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
462 if (!oat_dex_files_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700463 PLOG(ERROR) << "Failed to write oat dex information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700464 return false;
465 }
466 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800467 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
468 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800469 off_t actual_offset = lseek(file->Fd(), expected_offset, SEEK_SET);
Brian Carlstrom89521892011-12-07 22:05:07 -0800470 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
471 const DexFile* dex_file = (*dex_files_)[i];
472 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
473 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
474 return false;
475 }
476 const DexFile* dex_file = (*dex_files_)[i];
477 if (!file->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
478 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << file->name();
479 return false;
480 }
481 }
Logan Chien25ae6402012-03-20 20:19:26 +0800482 for (size_t i = 0; i != oat_elf_images_.size(); ++i) {
483 if (!oat_elf_images_[i]->Write(file)) {
484 PLOG(ERROR) << "Failed to write oat elf information to " << file->name();
485 return false;
486 }
487 }
488 for (size_t i = 0; i != oat_elf_images_.size(); ++i) {
489 uint32_t expected_offset = oat_elf_images_[i]->GetElfOffset();
490 off_t actual_offset = lseek(file->Fd(), expected_offset, SEEK_SET);
491 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
492 PLOG(ERROR) << "Failed to seek to dex file section."
493 << " Actual: " << actual_offset
494 << " Expected: " << expected_offset;
495 return false;
496 }
497 if (!oat_elf_images_[i]->WriteElfImage(file)) {
498 return false;
499 }
500 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800501 for (size_t i = 0; i != oat_classes_.size(); ++i) {
502 if (!oat_classes_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700503 PLOG(ERROR) << "Failed to write oat methods information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700504 return false;
505 }
506 }
507 return true;
508}
509
510size_t OatWriter::WriteCode(File* file) {
511 uint32_t code_offset = oat_header_->GetExecutableOffset();
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800512 off_t new_offset = lseek(file->Fd(), executable_offset_padding_length_, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700513 if (static_cast<uint32_t>(new_offset) != code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700514 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700515 << " Expected: " << code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700516 return 0;
517 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700518 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700519 return code_offset;
520}
521
522size_t OatWriter::WriteCodeDexFiles(File* file, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700523 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800524 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700525 const DexFile* dex_file = (*dex_files_)[i];
526 CHECK(dex_file != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700527 code_offset = WriteCodeDexFile(file, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700528 if (code_offset == 0) {
529 return 0;
530 }
531 }
532 return code_offset;
533}
534
Ian Rogers0571d352011-11-03 19:51:38 -0700535size_t OatWriter::WriteCodeDexFile(File* file, size_t code_offset, size_t& oat_class_index,
536 const DexFile& dex_file) {
537 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
538 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700539 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700540 code_offset = WriteCodeClassDef(file, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700541 if (code_offset == 0) {
542 return 0;
543 }
544 }
545 return code_offset;
546}
547
Ian Rogers0571d352011-11-03 19:51:38 -0700548void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
549 const DexFile& dex_file, File* f) const {
550 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
551 << " to " << f->name();
Elliott Hughes234da572011-11-03 22:13:06 -0700552}
553
Brian Carlstrome24fa612011-09-29 00:53:55 -0700554size_t OatWriter::WriteCodeClassDef(File* file,
Ian Rogers0571d352011-11-03 19:51:38 -0700555 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700556 const DexFile& dex_file,
557 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700558 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700559 if (class_data == NULL) {
560 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700561 return code_offset;
562 }
Ian Rogers0571d352011-11-03 19:51:38 -0700563 ClassDataItemIterator it(dex_file, class_data);
564 // Skip fields
565 while (it.HasNextStaticField()) {
566 it.Next();
567 }
568 while (it.HasNextInstanceField()) {
569 it.Next();
570 }
571 // Process methods
572 size_t class_def_method_index = 0;
573 while (it.HasNextDirectMethod()) {
574 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
575 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
576 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700577 if (code_offset == 0) {
578 return 0;
579 }
Ian Rogers0571d352011-11-03 19:51:38 -0700580 class_def_method_index++;
581 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700582 }
Ian Rogers0571d352011-11-03 19:51:38 -0700583 while (it.HasNextVirtualMethod()) {
584 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
585 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700586 if (code_offset == 0) {
587 return 0;
588 }
Ian Rogers0571d352011-11-03 19:51:38 -0700589 class_def_method_index++;
590 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700591 }
592 return code_offset;
593}
594
Ian Rogers0571d352011-11-03 19:51:38 -0700595size_t OatWriter::WriteCodeMethod(File* file, size_t code_offset, size_t oat_class_index,
596 size_t class_def_method_index, bool is_static,
597 uint32_t method_idx, const DexFile& dex_file) {
598 const CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800599 compiler_->GetCompiledMethod(Compiler::MethodReference(&dex_file, method_idx));
Ian Rogers0571d352011-11-03 19:51:38 -0700600
601 uint32_t frame_size_in_bytes = 0;
602 uint32_t core_spill_mask = 0;
603 uint32_t fp_spill_mask = 0;
604
605 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800606 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700607
608
609 if (compiled_method != NULL) { // ie. not an abstract method
Logan Chienccb7bf12012-03-28 12:52:32 +0800610 if (!compiled_method->IsExecutableInElf()) {
611 uint32_t aligned_code_offset = compiled_method->AlignCode(code_offset);
612 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
613 if (aligned_code_delta != 0) {
614 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
615 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
616 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
617 << " Expected: " << aligned_code_offset << " File: " << file->name();
618 return 0;
619 }
620 code_offset += aligned_code_delta;
621 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700622 }
Logan Chienccb7bf12012-03-28 12:52:32 +0800623 DCHECK_ALIGNED(code_offset, kArmAlignment);
624 const std::vector<uint8_t>& code = compiled_method->GetCode();
625 uint32_t code_size = code.size() * sizeof(code[0]);
626 CHECK_NE(code_size, 0U);
627
628 // Deduplicate code arrays
629 size_t offset = code_offset + sizeof(code_size) + compiled_method->CodeDelta();
630 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
631 if (code_iter != code_offsets_.end() && offset != method_offsets.code_offset_) {
632 DCHECK(code_iter->second == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
633 } else {
634 DCHECK(offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
635 if (!file->WriteFully(&code_size, sizeof(code_size))) {
636 ReportWriteFailure("method code size", method_idx, dex_file, file);
637 return 0;
638 }
639 code_offset += sizeof(code_size);
640 DCHECK_CODE_OFFSET();
641 if (!file->WriteFully(&code[0], code_size)) {
642 ReportWriteFailure("method code", method_idx, dex_file, file);
643 return 0;
644 }
645 code_offset += code_size;
646 }
647 DCHECK_CODE_OFFSET();
648 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
649 core_spill_mask = compiled_method->GetCoreSpillMask();
650 fp_spill_mask = compiled_method->GetFpSpillMask();
651
652 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
653 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
654
655 // Deduplicate mapping tables
656 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
657 mapping_table_offsets_.find(&mapping_table);
658 if (mapping_iter != mapping_table_offsets_.end() &&
659 code_offset != method_offsets.mapping_table_offset_) {
660 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
661 || mapping_iter->second == method_offsets.mapping_table_offset_)
662 << PrettyMethod(method_idx, dex_file);
663 } else {
664 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
665 || code_offset == method_offsets.mapping_table_offset_)
666 << PrettyMethod(method_idx, dex_file);
667 if (!file->WriteFully(&mapping_table[0], mapping_table_size)) {
668 ReportWriteFailure("mapping table", method_idx, dex_file, file);
669 return 0;
670 }
671 code_offset += mapping_table_size;
672 }
673 DCHECK_CODE_OFFSET();
674
675 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
676 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
677
678 // Deduplicate vmap tables
679 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
680 vmap_table_offsets_.find(&vmap_table);
681 if (vmap_iter != vmap_table_offsets_.end() &&
682 code_offset != method_offsets.vmap_table_offset_) {
683 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
684 || vmap_iter->second == method_offsets.vmap_table_offset_)
685 << PrettyMethod(method_idx, dex_file);
686 } else {
687 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
688 || code_offset == method_offsets.vmap_table_offset_)
689 << PrettyMethod(method_idx, dex_file);
690 if (!file->WriteFully(&vmap_table[0], vmap_table_size)) {
691 ReportWriteFailure("vmap table", method_idx, dex_file, file);
692 return 0;
693 }
694 code_offset += vmap_table_size;
695 }
696 DCHECK_CODE_OFFSET();
697
698 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
699 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
700
701 // Deduplicate GC maps
702 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
703 gc_map_offsets_.find(&gc_map);
704 if (gc_map_iter != gc_map_offsets_.end() &&
705 code_offset != method_offsets.gc_map_offset_) {
706 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
707 || gc_map_iter->second == method_offsets.gc_map_offset_)
708 << PrettyMethod(method_idx, dex_file);
709 } else {
710 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
711 || code_offset == method_offsets.gc_map_offset_)
712 << PrettyMethod(method_idx, dex_file);
713 if (!file->WriteFully(&gc_map[0], gc_map_size)) {
714 ReportWriteFailure("GC map", method_idx, dex_file, file);
715 return 0;
716 }
717 code_offset += gc_map_size;
718 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700719 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700720 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700721 }
Ian Rogers0571d352011-11-03 19:51:38 -0700722 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
723 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700724 if (compiled_invoke_stub != NULL) {
Logan Chienccb7bf12012-03-28 12:52:32 +0800725 if (!compiled_invoke_stub->IsExecutableInElf()) {
726 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
727 compiler_->GetInstructionSet());
728 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
729 if (aligned_code_delta != 0) {
730 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
731 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
732 PLOG(ERROR) << "Failed to seek to align invoke stub code. Actual: " << new_offset
733 << " Expected: " << aligned_code_offset;
734 return 0;
735 }
736 code_offset += aligned_code_delta;
737 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700738 }
Logan Chienccb7bf12012-03-28 12:52:32 +0800739 DCHECK_ALIGNED(code_offset, kArmAlignment);
740 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
741 uint32_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
742 CHECK_NE(invoke_stub_size, 0U);
jeffhao55d78212011-11-02 11:41:50 -0700743
Logan Chienccb7bf12012-03-28 12:52:32 +0800744 // Deduplicate invoke stubs
745 size_t offset = code_offset + sizeof(invoke_stub_size);
746 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter =
747 code_offsets_.find(&invoke_stub);
748 if (stub_iter != code_offsets_.end() && offset != method_offsets.invoke_stub_offset_) {
749 DCHECK(stub_iter->second == method_offsets.invoke_stub_offset_) << PrettyMethod(method_idx, dex_file);
750 } else {
751 DCHECK(offset == method_offsets.invoke_stub_offset_) << PrettyMethod(method_idx, dex_file);
752 if (!file->WriteFully(&invoke_stub_size, sizeof(invoke_stub_size))) {
753 ReportWriteFailure("invoke stub code size", method_idx, dex_file, file);
754 return 0;
755 }
756 code_offset += sizeof(invoke_stub_size);
757 DCHECK_CODE_OFFSET();
758 if (!file->WriteFully(&invoke_stub[0], invoke_stub_size)) {
759 ReportWriteFailure("invoke stub code", method_idx, dex_file, file);
760 return 0;
761 }
762 code_offset += invoke_stub_size;
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700763 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700764 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700765 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700766 }
Logan Chien8b977d32012-02-21 19:14:55 +0800767
Brian Carlstrome24fa612011-09-29 00:53:55 -0700768 return code_offset;
769}
770
Brian Carlstrome24fa612011-09-29 00:53:55 -0700771OatWriter::OatDexFile::OatDexFile(const DexFile& dex_file) {
Elliott Hughes95572412011-12-13 18:14:20 -0800772 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700773 dex_file_location_size_ = location.size();
774 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800775 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800776 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800777 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700778}
779
780size_t OatWriter::OatDexFile::SizeOf() const {
781 return sizeof(dex_file_location_size_)
782 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800783 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800784 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800785 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700786}
787
788void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
789 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
790 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800791 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800792 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800793 oat_header.UpdateChecksum(&methods_offsets_[0],
794 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700795}
796
797bool OatWriter::OatDexFile::Write(File* file) const {
798 if (!file->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700799 PLOG(ERROR) << "Failed to write dex file location length to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700800 return false;
801 }
802 if (!file->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700803 PLOG(ERROR) << "Failed to write dex file location data to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700804 return false;
805 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800806 if (!file->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
807 PLOG(ERROR) << "Failed to write dex file location checksum to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700808 return false;
809 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800810 if (!file->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
811 PLOG(ERROR) << "Failed to write dex file offset to " << file->name();
812 return false;
813 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800814 if (!file->WriteFully(&methods_offsets_[0],
815 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700816 PLOG(ERROR) << "Failed to write methods offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700817 return false;
818 }
819 return true;
820}
821
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800822OatWriter::OatClass::OatClass(Class::Status status, uint32_t methods_count) {
823 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700824 method_offsets_.resize(methods_count);
825}
826
Brian Carlstrom389efb02012-01-11 12:06:26 -0800827size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800828 return sizeof(status_)
829 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700830}
831
Brian Carlstrom389efb02012-01-11 12:06:26 -0800832void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800833 oat_header.UpdateChecksum(&status_, sizeof(status_));
834 oat_header.UpdateChecksum(&method_offsets_[0],
835 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700836}
837
Brian Carlstrom389efb02012-01-11 12:06:26 -0800838bool OatWriter::OatClass::Write(File* file) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800839 if (!file->WriteFully(&status_, sizeof(status_))) {
840 PLOG(ERROR) << "Failed to write class status to " << file->name();
841 return false;
842 }
843 if (!file->WriteFully(&method_offsets_[0],
844 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700845 PLOG(ERROR) << "Failed to write method offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700846 return false;
847 }
848 return true;
849}
850
Logan Chien25ae6402012-03-20 20:19:26 +0800851OatWriter::OatElfImage::OatElfImage(const ElfImage& image)
852 : elf_offset_(0), elf_size_(image.size()), elf_addr_(image.begin()) {
853}
854
855size_t OatWriter::OatElfImage::SizeOf() const {
856 return (sizeof(elf_offset_) + sizeof(elf_size_));
857}
858
859uint32_t OatWriter::OatElfImage::GetElfSize() const {
860 return elf_size_;
861}
862
863uint32_t OatWriter::OatElfImage::GetElfOffset() const {
864 DCHECK_NE(elf_offset_, 0U);
865 return elf_offset_;
866}
867
868void OatWriter::OatElfImage::SetElfOffset(uint32_t offset) {
869 DCHECK_NE(offset, 0U);
870 DCHECK((offset & 0x3LU) == 0);
871 elf_offset_ = offset;
872}
873
874bool OatWriter::OatElfImage::Write(File* file) const {
875 DCHECK_NE(elf_offset_, 0U);
876 if (!file->WriteFully(&elf_offset_, sizeof(elf_offset_))) {
877 PLOG(ERROR) << "Failed to write ELF offset to " << file->name();
878 return false;
879 }
880 if (!file->WriteFully(&elf_size_, sizeof(elf_size_))) {
881 PLOG(ERROR) << "Failed to write ELF size to " << file->name();
882 return false;
883 }
884 return true;
885}
886
887bool OatWriter::OatElfImage::WriteElfImage(File* file) const {
888 if (!file->WriteFully(elf_addr_, elf_size_)) {
889 PLOG(ERROR) << "Failed to write ELF image to " << file->name();
890 return false;
891 }
892 return true;
893}
894
Brian Carlstrome24fa612011-09-29 00:53:55 -0700895} // namespace art