blob: c639271b06f9ba2a83386ae455e3c813a2174356 [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 Chienccb7bf12012-03-28 12:52:32 +0800260 uint32_t code_elf_idx = -1u;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700261 uint32_t frame_size_in_bytes = kStackAlignment;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700262 uint32_t core_spill_mask = 0;
263 uint32_t fp_spill_mask = 0;
264 uint32_t mapping_table_offset = 0;
265 uint32_t vmap_table_offset = 0;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800266 uint32_t gc_map_offset = 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700267 // derived from CompiledInvokeStub if available
268 uint32_t invoke_stub_offset = 0;
Logan Chienccb7bf12012-03-28 12:52:32 +0800269 uint32_t invoke_stub_elf_idx = -1u;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700270
Ian Rogers0571d352011-11-03 19:51:38 -0700271 CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800272 compiler_->GetCompiledMethod(Compiler::MethodReference(dex_file, method_idx));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700273 if (compiled_method != NULL) {
Logan Chienccb7bf12012-03-28 12:52:32 +0800274 if (compiled_method->IsExecutableInElf()) {
275 code_elf_idx = compiled_method->GetElfIndex();
jeffhao55d78212011-11-02 11:41:50 -0700276 } else {
Logan Chienccb7bf12012-03-28 12:52:32 +0800277 offset = compiled_method->AlignCode(offset);
278 DCHECK_ALIGNED(offset, kArmAlignment);
279 const std::vector<uint8_t>& code = compiled_method->GetCode();
280 uint32_t code_size = code.size() * sizeof(code[0]);
281 CHECK_NE(code_size, 0U);
282 uint32_t thumb_offset = compiled_method->CodeDelta();
283 code_offset = offset + sizeof(code_size) + thumb_offset;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700284
Logan Chienccb7bf12012-03-28 12:52:32 +0800285 // Deduplicate code arrays
286 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
287 if (code_iter != code_offsets_.end()) {
288 code_offset = code_iter->second;
289 } else {
290 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&code, code_offset));
291 offset += sizeof(code_size); // code size is prepended before code
292 offset += code_size;
293 oat_header_->UpdateChecksum(&code[0], code_size);
294 }
295 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
296 core_spill_mask = compiled_method->GetCoreSpillMask();
297 fp_spill_mask = compiled_method->GetFpSpillMask();
jeffhao55d78212011-11-02 11:41:50 -0700298
Logan Chienccb7bf12012-03-28 12:52:32 +0800299 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
300 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
301 mapping_table_offset = (mapping_table_size == 0) ? 0 : offset;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700302
Logan Chienccb7bf12012-03-28 12:52:32 +0800303 // Deduplicate mapping tables
304 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter = mapping_table_offsets_.find(&mapping_table);
305 if (mapping_iter != mapping_table_offsets_.end()) {
306 mapping_table_offset = mapping_iter->second;
307 } else {
308 mapping_table_offsets_.insert(std::pair<const std::vector<uint32_t>*, uint32_t>(&mapping_table, mapping_table_offset));
309 offset += mapping_table_size;
310 oat_header_->UpdateChecksum(&mapping_table[0], mapping_table_size);
311 }
jeffhao55d78212011-11-02 11:41:50 -0700312
Logan Chienccb7bf12012-03-28 12:52:32 +0800313 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
314 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
315 vmap_table_offset = (vmap_table_size == 0) ? 0 : offset;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800316
Logan Chienccb7bf12012-03-28 12:52:32 +0800317 // Deduplicate vmap tables
318 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter = vmap_table_offsets_.find(&vmap_table);
319 if (vmap_iter != vmap_table_offsets_.end()) {
320 vmap_table_offset = vmap_iter->second;
321 } else {
322 vmap_table_offsets_.insert(std::pair<const std::vector<uint16_t>*, uint32_t>(&vmap_table, vmap_table_offset));
323 offset += vmap_table_size;
324 oat_header_->UpdateChecksum(&vmap_table[0], vmap_table_size);
325 }
326
327 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
328 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
329 gc_map_offset = (gc_map_size == 0) ? 0 : offset;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800330
Ian Rogersc20a83e2012-01-18 18:15:32 -0800331#ifndef NDEBUG
Logan Chienccb7bf12012-03-28 12:52:32 +0800332 // We expect GC maps except when the class hasn't been verified or the method is native
333 CompiledClass* compiled_class =
334 compiler_->GetCompiledClass(Compiler::MethodReference(dex_file, class_def_index));
335 Class::Status status =
336 (compiled_class != NULL) ? compiled_class->GetStatus() : Class::kStatusNotReady;
337 CHECK(gc_map_size != 0 || is_native || status < Class::kStatusVerified)
338 << &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 -0800339#endif
340
Logan Chienccb7bf12012-03-28 12:52:32 +0800341 // Deduplicate GC maps
342 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter = gc_map_offsets_.find(&gc_map);
343 if (gc_map_iter != gc_map_offsets_.end()) {
344 gc_map_offset = gc_map_iter->second;
345 } else {
346 gc_map_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&gc_map, gc_map_offset));
347 offset += gc_map_size;
348 oat_header_->UpdateChecksum(&gc_map[0], gc_map_size);
349 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800350 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700351 }
352
Ian Rogers0571d352011-11-03 19:51:38 -0700353 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx));
354 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700355 if (compiled_invoke_stub != NULL) {
Logan Chienccb7bf12012-03-28 12:52:32 +0800356 if (compiled_invoke_stub->IsExecutableInElf()) {
357 invoke_stub_elf_idx = compiled_invoke_stub->GetElfIndex();
jeffhao55d78212011-11-02 11:41:50 -0700358 } else {
Logan Chienccb7bf12012-03-28 12:52:32 +0800359 offset = CompiledMethod::AlignCode(offset, compiler_->GetInstructionSet());
360 DCHECK_ALIGNED(offset, kArmAlignment);
361 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
362 uint32_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
363 CHECK_NE(invoke_stub_size, 0U);
364 invoke_stub_offset = offset + sizeof(invoke_stub_size);
365
366 // Deduplicate invoke stubs
367 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter = code_offsets_.find(&invoke_stub);
368 if (stub_iter != code_offsets_.end()) {
369 invoke_stub_offset = stub_iter->second;
370 } else {
371 code_offsets_.insert(std::pair<const std::vector<uint8_t>*, uint32_t>(&invoke_stub, invoke_stub_offset));
372 offset += sizeof(invoke_stub_size); // invoke stub size is prepended before code
373 offset += invoke_stub_size;
374 oat_header_->UpdateChecksum(&invoke_stub[0], invoke_stub_size);
375 }
jeffhao55d78212011-11-02 11:41:50 -0700376 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700377 }
378
Brian Carlstrom389efb02012-01-11 12:06:26 -0800379 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index]
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700380 = OatMethodOffsets(code_offset,
381 frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700382 core_spill_mask,
383 fp_spill_mask,
384 mapping_table_offset,
385 vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800386 gc_map_offset,
Logan Chienccb7bf12012-03-28 12:52:32 +0800387 invoke_stub_offset
388#if defined(ART_USE_LLVM_COMPILER)
389 , code_elf_idx,
390 invoke_stub_elf_idx
391#endif
392 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700393
Ian Rogers0571d352011-11-03 19:51:38 -0700394 if (compiler_->IsImage()) {
395 ClassLinker* linker = Runtime::Current()->GetClassLinker();
396 DexCache* dex_cache = linker->FindDexCache(*dex_file);
397 Method* method = linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader_,
398 is_direct);
399 CHECK(method != NULL);
400 method->SetFrameSizeInBytes(frame_size_in_bytes);
401 method->SetCoreSpillMask(core_spill_mask);
402 method->SetFpSpillMask(fp_spill_mask);
403 method->SetOatMappingTableOffset(mapping_table_offset);
Ian Rogers19846512012-02-24 11:42:47 -0800404 // Don't overwrite static method trampoline
405 if (!method->IsStatic() || method->IsConstructor() ||
406 method->GetDeclaringClass()->IsInitialized()) {
407 method->SetOatCodeOffset(code_offset);
408 } else {
409 method->SetCode(Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod)->GetData());
410 }
Ian Rogers0571d352011-11-03 19:51:38 -0700411 method->SetOatVmapTableOffset(vmap_table_offset);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800412 method->SetOatGcMapOffset(gc_map_offset);
Ian Rogers0571d352011-11-03 19:51:38 -0700413 method->SetOatInvokeStubOffset(invoke_stub_offset);
414 }
Logan Chien8b977d32012-02-21 19:14:55 +0800415
Brian Carlstrome24fa612011-09-29 00:53:55 -0700416 return offset;
417}
418
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700419#define DCHECK_CODE_OFFSET() \
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800420 DCHECK_EQ(static_cast<off_t>(code_offset), lseek(file->Fd(), 0, SEEK_CUR))
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700421
Elliott Hughes234da572011-11-03 22:13:06 -0700422bool OatWriter::Write(File* file) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700423 if (!file->WriteFully(oat_header_, sizeof(*oat_header_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700424 PLOG(ERROR) << "Failed to write oat header to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700425 return false;
426 }
427
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700428 if (!file->WriteFully(image_file_location_.data(),
429 image_file_location_.size())) {
430 PLOG(ERROR) << "Failed to write oat header image file location to " << file->name();
431 return false;
432 }
433
Elliott Hughes234da572011-11-03 22:13:06 -0700434 if (!WriteTables(file)) {
435 LOG(ERROR) << "Failed to write oat tables to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700436 return false;
437 }
438
Elliott Hughes234da572011-11-03 22:13:06 -0700439 size_t code_offset = WriteCode(file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700440 if (code_offset == 0) {
Elliott Hughes234da572011-11-03 22:13:06 -0700441 LOG(ERROR) << "Failed to write oat code to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700442 return false;
443 }
444
Elliott Hughes234da572011-11-03 22:13:06 -0700445 code_offset = WriteCodeDexFiles(file, code_offset);
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 for dex files to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700448 return false;
449 }
450
451 return true;
452}
453
454bool OatWriter::WriteTables(File* file) {
455 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
456 if (!oat_dex_files_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700457 PLOG(ERROR) << "Failed to write oat dex information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700458 return false;
459 }
460 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800461 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
462 uint32_t expected_offset = oat_dex_files_[i]->dex_file_offset_;
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800463 off_t actual_offset = lseek(file->Fd(), expected_offset, SEEK_SET);
Brian Carlstrom89521892011-12-07 22:05:07 -0800464 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
465 const DexFile* dex_file = (*dex_files_)[i];
466 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
467 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
468 return false;
469 }
470 const DexFile* dex_file = (*dex_files_)[i];
471 if (!file->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
472 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() << " to " << file->name();
473 return false;
474 }
475 }
Logan Chien25ae6402012-03-20 20:19:26 +0800476 for (size_t i = 0; i != oat_elf_images_.size(); ++i) {
477 if (!oat_elf_images_[i]->Write(file)) {
478 PLOG(ERROR) << "Failed to write oat elf information to " << file->name();
479 return false;
480 }
481 }
482 for (size_t i = 0; i != oat_elf_images_.size(); ++i) {
483 uint32_t expected_offset = oat_elf_images_[i]->GetElfOffset();
484 off_t actual_offset = lseek(file->Fd(), expected_offset, SEEK_SET);
485 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
486 PLOG(ERROR) << "Failed to seek to dex file section."
487 << " Actual: " << actual_offset
488 << " Expected: " << expected_offset;
489 return false;
490 }
491 if (!oat_elf_images_[i]->WriteElfImage(file)) {
492 return false;
493 }
494 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800495 for (size_t i = 0; i != oat_classes_.size(); ++i) {
496 if (!oat_classes_[i]->Write(file)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700497 PLOG(ERROR) << "Failed to write oat methods information to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700498 return false;
499 }
500 }
501 return true;
502}
503
504size_t OatWriter::WriteCode(File* file) {
505 uint32_t code_offset = oat_header_->GetExecutableOffset();
Elliott Hughes2a2ff562012-01-06 18:07:59 -0800506 off_t new_offset = lseek(file->Fd(), executable_offset_padding_length_, SEEK_CUR);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700507 if (static_cast<uint32_t>(new_offset) != code_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700508 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Elliott Hughes234da572011-11-03 22:13:06 -0700509 << " Expected: " << code_offset << " File: " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700510 return 0;
511 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700512 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700513 return code_offset;
514}
515
516size_t OatWriter::WriteCodeDexFiles(File* file, size_t code_offset) {
Ian Rogers0571d352011-11-03 19:51:38 -0700517 size_t oat_class_index = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800518 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700519 const DexFile* dex_file = (*dex_files_)[i];
520 CHECK(dex_file != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700521 code_offset = WriteCodeDexFile(file, code_offset, oat_class_index, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700522 if (code_offset == 0) {
523 return 0;
524 }
525 }
526 return code_offset;
527}
528
Ian Rogers0571d352011-11-03 19:51:38 -0700529size_t OatWriter::WriteCodeDexFile(File* file, size_t code_offset, size_t& oat_class_index,
530 const DexFile& dex_file) {
531 for (size_t class_def_index = 0; class_def_index < dex_file.NumClassDefs();
532 class_def_index++, oat_class_index++) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700533 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers0571d352011-11-03 19:51:38 -0700534 code_offset = WriteCodeClassDef(file, code_offset, oat_class_index, dex_file, class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700535 if (code_offset == 0) {
536 return 0;
537 }
538 }
539 return code_offset;
540}
541
Ian Rogers0571d352011-11-03 19:51:38 -0700542void OatWriter::ReportWriteFailure(const char* what, uint32_t method_idx,
543 const DexFile& dex_file, File* f) const {
544 PLOG(ERROR) << "Failed to write " << what << " for " << PrettyMethod(method_idx, dex_file)
545 << " to " << f->name();
Elliott Hughes234da572011-11-03 22:13:06 -0700546}
547
Brian Carlstrome24fa612011-09-29 00:53:55 -0700548size_t OatWriter::WriteCodeClassDef(File* file,
Ian Rogers0571d352011-11-03 19:51:38 -0700549 size_t code_offset, size_t oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700550 const DexFile& dex_file,
551 const DexFile::ClassDef& class_def) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700552 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700553 if (class_data == NULL) {
554 // ie. an empty class such as a marker interface
Ian Rogers387b6992011-10-31 17:52:37 -0700555 return code_offset;
556 }
Ian Rogers0571d352011-11-03 19:51:38 -0700557 ClassDataItemIterator it(dex_file, class_data);
558 // Skip fields
559 while (it.HasNextStaticField()) {
560 it.Next();
561 }
562 while (it.HasNextInstanceField()) {
563 it.Next();
564 }
565 // Process methods
566 size_t class_def_method_index = 0;
567 while (it.HasNextDirectMethod()) {
568 bool is_static = (it.GetMemberAccessFlags() & kAccStatic) != 0;
569 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
570 is_static, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700571 if (code_offset == 0) {
572 return 0;
573 }
Ian Rogers0571d352011-11-03 19:51:38 -0700574 class_def_method_index++;
575 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700576 }
Ian Rogers0571d352011-11-03 19:51:38 -0700577 while (it.HasNextVirtualMethod()) {
578 code_offset = WriteCodeMethod(file, code_offset, oat_class_index, class_def_method_index,
579 false, it.GetMemberIndex(), dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700580 if (code_offset == 0) {
581 return 0;
582 }
Ian Rogers0571d352011-11-03 19:51:38 -0700583 class_def_method_index++;
584 it.Next();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700585 }
586 return code_offset;
587}
588
Ian Rogers0571d352011-11-03 19:51:38 -0700589size_t OatWriter::WriteCodeMethod(File* file, size_t code_offset, size_t oat_class_index,
590 size_t class_def_method_index, bool is_static,
591 uint32_t method_idx, const DexFile& dex_file) {
592 const CompiledMethod* compiled_method =
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800593 compiler_->GetCompiledMethod(Compiler::MethodReference(&dex_file, method_idx));
Ian Rogers0571d352011-11-03 19:51:38 -0700594
595 uint32_t frame_size_in_bytes = 0;
596 uint32_t core_spill_mask = 0;
597 uint32_t fp_spill_mask = 0;
598
599 OatMethodOffsets method_offsets =
Brian Carlstrom389efb02012-01-11 12:06:26 -0800600 oat_classes_[oat_class_index]->method_offsets_[class_def_method_index];
Ian Rogers0571d352011-11-03 19:51:38 -0700601
602
603 if (compiled_method != NULL) { // ie. not an abstract method
Logan Chienccb7bf12012-03-28 12:52:32 +0800604 if (!compiled_method->IsExecutableInElf()) {
605 uint32_t aligned_code_offset = compiled_method->AlignCode(code_offset);
606 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
607 if (aligned_code_delta != 0) {
608 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
609 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
610 PLOG(ERROR) << "Failed to seek to align oat code. Actual: " << new_offset
611 << " Expected: " << aligned_code_offset << " File: " << file->name();
612 return 0;
613 }
614 code_offset += aligned_code_delta;
615 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700616 }
Logan Chienccb7bf12012-03-28 12:52:32 +0800617 DCHECK_ALIGNED(code_offset, kArmAlignment);
618 const std::vector<uint8_t>& code = compiled_method->GetCode();
619 uint32_t code_size = code.size() * sizeof(code[0]);
620 CHECK_NE(code_size, 0U);
621
622 // Deduplicate code arrays
623 size_t offset = code_offset + sizeof(code_size) + compiled_method->CodeDelta();
624 std::map<const std::vector<uint8_t>*, uint32_t>::iterator code_iter = code_offsets_.find(&code);
625 if (code_iter != code_offsets_.end() && offset != method_offsets.code_offset_) {
626 DCHECK(code_iter->second == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
627 } else {
628 DCHECK(offset == method_offsets.code_offset_) << PrettyMethod(method_idx, dex_file);
629 if (!file->WriteFully(&code_size, sizeof(code_size))) {
630 ReportWriteFailure("method code size", method_idx, dex_file, file);
631 return 0;
632 }
633 code_offset += sizeof(code_size);
634 DCHECK_CODE_OFFSET();
635 if (!file->WriteFully(&code[0], code_size)) {
636 ReportWriteFailure("method code", method_idx, dex_file, file);
637 return 0;
638 }
639 code_offset += code_size;
640 }
641 DCHECK_CODE_OFFSET();
642 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
643 core_spill_mask = compiled_method->GetCoreSpillMask();
644 fp_spill_mask = compiled_method->GetFpSpillMask();
645
646 const std::vector<uint32_t>& mapping_table = compiled_method->GetMappingTable();
647 size_t mapping_table_size = mapping_table.size() * sizeof(mapping_table[0]);
648
649 // Deduplicate mapping tables
650 std::map<const std::vector<uint32_t>*, uint32_t>::iterator mapping_iter =
651 mapping_table_offsets_.find(&mapping_table);
652 if (mapping_iter != mapping_table_offsets_.end() &&
653 code_offset != method_offsets.mapping_table_offset_) {
654 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
655 || mapping_iter->second == method_offsets.mapping_table_offset_)
656 << PrettyMethod(method_idx, dex_file);
657 } else {
658 DCHECK((mapping_table_size == 0 && method_offsets.mapping_table_offset_ == 0)
659 || code_offset == method_offsets.mapping_table_offset_)
660 << PrettyMethod(method_idx, dex_file);
661 if (!file->WriteFully(&mapping_table[0], mapping_table_size)) {
662 ReportWriteFailure("mapping table", method_idx, dex_file, file);
663 return 0;
664 }
665 code_offset += mapping_table_size;
666 }
667 DCHECK_CODE_OFFSET();
668
669 const std::vector<uint16_t>& vmap_table = compiled_method->GetVmapTable();
670 size_t vmap_table_size = vmap_table.size() * sizeof(vmap_table[0]);
671
672 // Deduplicate vmap tables
673 std::map<const std::vector<uint16_t>*, uint32_t>::iterator vmap_iter =
674 vmap_table_offsets_.find(&vmap_table);
675 if (vmap_iter != vmap_table_offsets_.end() &&
676 code_offset != method_offsets.vmap_table_offset_) {
677 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
678 || vmap_iter->second == method_offsets.vmap_table_offset_)
679 << PrettyMethod(method_idx, dex_file);
680 } else {
681 DCHECK((vmap_table_size == 0 && method_offsets.vmap_table_offset_ == 0)
682 || code_offset == method_offsets.vmap_table_offset_)
683 << PrettyMethod(method_idx, dex_file);
684 if (!file->WriteFully(&vmap_table[0], vmap_table_size)) {
685 ReportWriteFailure("vmap table", method_idx, dex_file, file);
686 return 0;
687 }
688 code_offset += vmap_table_size;
689 }
690 DCHECK_CODE_OFFSET();
691
692 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
693 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
694
695 // Deduplicate GC maps
696 std::map<const std::vector<uint8_t>*, uint32_t>::iterator gc_map_iter =
697 gc_map_offsets_.find(&gc_map);
698 if (gc_map_iter != gc_map_offsets_.end() &&
699 code_offset != method_offsets.gc_map_offset_) {
700 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
701 || gc_map_iter->second == method_offsets.gc_map_offset_)
702 << PrettyMethod(method_idx, dex_file);
703 } else {
704 DCHECK((gc_map_size == 0 && method_offsets.gc_map_offset_ == 0)
705 || code_offset == method_offsets.gc_map_offset_)
706 << PrettyMethod(method_idx, dex_file);
707 if (!file->WriteFully(&gc_map[0], gc_map_size)) {
708 ReportWriteFailure("GC map", method_idx, dex_file, file);
709 return 0;
710 }
711 code_offset += gc_map_size;
712 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700713 DCHECK_CODE_OFFSET();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700714 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700715 }
Ian Rogers0571d352011-11-03 19:51:38 -0700716 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
717 const CompiledInvokeStub* compiled_invoke_stub = compiler_->FindInvokeStub(is_static, shorty);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700718 if (compiled_invoke_stub != NULL) {
Logan Chienccb7bf12012-03-28 12:52:32 +0800719 if (!compiled_invoke_stub->IsExecutableInElf()) {
720 uint32_t aligned_code_offset = CompiledMethod::AlignCode(code_offset,
721 compiler_->GetInstructionSet());
722 uint32_t aligned_code_delta = aligned_code_offset - code_offset;
723 if (aligned_code_delta != 0) {
724 off_t new_offset = lseek(file->Fd(), aligned_code_delta, SEEK_CUR);
725 if (static_cast<uint32_t>(new_offset) != aligned_code_offset) {
726 PLOG(ERROR) << "Failed to seek to align invoke stub code. Actual: " << new_offset
727 << " Expected: " << aligned_code_offset;
728 return 0;
729 }
730 code_offset += aligned_code_delta;
731 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700732 }
Logan Chienccb7bf12012-03-28 12:52:32 +0800733 DCHECK_ALIGNED(code_offset, kArmAlignment);
734 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
735 uint32_t invoke_stub_size = invoke_stub.size() * sizeof(invoke_stub[0]);
736 CHECK_NE(invoke_stub_size, 0U);
jeffhao55d78212011-11-02 11:41:50 -0700737
Logan Chienccb7bf12012-03-28 12:52:32 +0800738 // Deduplicate invoke stubs
739 size_t offset = code_offset + sizeof(invoke_stub_size);
740 std::map<const std::vector<uint8_t>*, uint32_t>::iterator stub_iter =
741 code_offsets_.find(&invoke_stub);
742 if (stub_iter != code_offsets_.end() && offset != method_offsets.invoke_stub_offset_) {
743 DCHECK(stub_iter->second == method_offsets.invoke_stub_offset_) << PrettyMethod(method_idx, dex_file);
744 } else {
745 DCHECK(offset == method_offsets.invoke_stub_offset_) << PrettyMethod(method_idx, dex_file);
746 if (!file->WriteFully(&invoke_stub_size, sizeof(invoke_stub_size))) {
747 ReportWriteFailure("invoke stub code size", method_idx, dex_file, file);
748 return 0;
749 }
750 code_offset += sizeof(invoke_stub_size);
751 DCHECK_CODE_OFFSET();
752 if (!file->WriteFully(&invoke_stub[0], invoke_stub_size)) {
753 ReportWriteFailure("invoke stub code", method_idx, dex_file, file);
754 return 0;
755 }
756 code_offset += invoke_stub_size;
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700757 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700758 DCHECK_CODE_OFFSET();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700759 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700760 }
Logan Chien8b977d32012-02-21 19:14:55 +0800761
Brian Carlstrome24fa612011-09-29 00:53:55 -0700762 return code_offset;
763}
764
Brian Carlstrome24fa612011-09-29 00:53:55 -0700765OatWriter::OatDexFile::OatDexFile(const DexFile& dex_file) {
Elliott Hughes95572412011-12-13 18:14:20 -0800766 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700767 dex_file_location_size_ = location.size();
768 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800769 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -0800770 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800771 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700772}
773
774size_t OatWriter::OatDexFile::SizeOf() const {
775 return sizeof(dex_file_location_size_)
776 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800777 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -0800778 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800779 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700780}
781
782void OatWriter::OatDexFile::UpdateChecksum(OatHeader& oat_header) const {
783 oat_header.UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
784 oat_header.UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800785 oat_header.UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
Brian Carlstrom89521892011-12-07 22:05:07 -0800786 oat_header.UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800787 oat_header.UpdateChecksum(&methods_offsets_[0],
788 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700789}
790
791bool OatWriter::OatDexFile::Write(File* file) const {
792 if (!file->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
Elliott Hughes234da572011-11-03 22:13:06 -0700793 PLOG(ERROR) << "Failed to write dex file location length to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700794 return false;
795 }
796 if (!file->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
Elliott Hughes234da572011-11-03 22:13:06 -0700797 PLOG(ERROR) << "Failed to write dex file location data to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700798 return false;
799 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800800 if (!file->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
801 PLOG(ERROR) << "Failed to write dex file location checksum to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700802 return false;
803 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800804 if (!file->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
805 PLOG(ERROR) << "Failed to write dex file offset to " << file->name();
806 return false;
807 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800808 if (!file->WriteFully(&methods_offsets_[0],
809 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700810 PLOG(ERROR) << "Failed to write methods offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700811 return false;
812 }
813 return true;
814}
815
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800816OatWriter::OatClass::OatClass(Class::Status status, uint32_t methods_count) {
817 status_ = status;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700818 method_offsets_.resize(methods_count);
819}
820
Brian Carlstrom389efb02012-01-11 12:06:26 -0800821size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800822 return sizeof(status_)
823 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700824}
825
Brian Carlstrom389efb02012-01-11 12:06:26 -0800826void OatWriter::OatClass::UpdateChecksum(OatHeader& oat_header) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800827 oat_header.UpdateChecksum(&status_, sizeof(status_));
828 oat_header.UpdateChecksum(&method_offsets_[0],
829 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700830}
831
Brian Carlstrom389efb02012-01-11 12:06:26 -0800832bool OatWriter::OatClass::Write(File* file) const {
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800833 if (!file->WriteFully(&status_, sizeof(status_))) {
834 PLOG(ERROR) << "Failed to write class status to " << file->name();
835 return false;
836 }
837 if (!file->WriteFully(&method_offsets_[0],
838 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Elliott Hughes234da572011-11-03 22:13:06 -0700839 PLOG(ERROR) << "Failed to write method offsets to " << file->name();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700840 return false;
841 }
842 return true;
843}
844
Logan Chien25ae6402012-03-20 20:19:26 +0800845OatWriter::OatElfImage::OatElfImage(const ElfImage& image)
846 : elf_offset_(0), elf_size_(image.size()), elf_addr_(image.begin()) {
847}
848
849size_t OatWriter::OatElfImage::SizeOf() const {
850 return (sizeof(elf_offset_) + sizeof(elf_size_));
851}
852
853uint32_t OatWriter::OatElfImage::GetElfSize() const {
854 return elf_size_;
855}
856
857uint32_t OatWriter::OatElfImage::GetElfOffset() const {
858 DCHECK_NE(elf_offset_, 0U);
859 return elf_offset_;
860}
861
862void OatWriter::OatElfImage::SetElfOffset(uint32_t offset) {
863 DCHECK_NE(offset, 0U);
864 DCHECK((offset & 0x3LU) == 0);
865 elf_offset_ = offset;
866}
867
868bool OatWriter::OatElfImage::Write(File* file) const {
869 DCHECK_NE(elf_offset_, 0U);
870 if (!file->WriteFully(&elf_offset_, sizeof(elf_offset_))) {
871 PLOG(ERROR) << "Failed to write ELF offset to " << file->name();
872 return false;
873 }
874 if (!file->WriteFully(&elf_size_, sizeof(elf_size_))) {
875 PLOG(ERROR) << "Failed to write ELF size to " << file->name();
876 return false;
877 }
878 return true;
879}
880
881bool OatWriter::OatElfImage::WriteElfImage(File* file) const {
882 if (!file->WriteFully(elf_addr_, elf_size_)) {
883 PLOG(ERROR) << "Failed to write ELF image to " << file->name();
884 return false;
885 }
886 return true;
887}
888
Brian Carlstrome24fa612011-09-29 00:53:55 -0700889} // namespace art