blob: 5d532abdd68f4b2539d272830507434202f37f02 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrome24fa612011-09-29 00:53:55 -070016
17#include "oat_writer.h"
18
Elliott Hughesa0e18062012-04-13 15:59:59 -070019#include <zlib.h>
20
Brian Carlstromba150c32013-08-27 17:31:03 -070021#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080022#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080023#include "base/unix_file/fd_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070024#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000026#include "dex/verification_results.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070027#include "gc/space/space.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070028#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/array.h"
30#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070031#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070032#include "os.h"
Brian Carlstromcd60ac72013-01-20 17:09:51 -080033#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070034#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070035#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070036#include "handle_scope-inl.h"
jeffhaoec014232012-09-05 10:42:25 -070037#include "verifier/method_verifier.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070038
39namespace art {
40
Vladimir Marko96c6ab92014-04-08 14:00:50 +010041#define DCHECK_OFFSET() \
42 DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out->Seek(0, kSeekCurrent)) \
43 << "file_offset=" << file_offset << " relative_offset=" << relative_offset
44
45#define DCHECK_OFFSET_() \
46 DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out->Seek(0, kSeekCurrent)) \
47 << "file_offset=" << file_offset << " offset_=" << offset_
48
Brian Carlstrom3320cf42011-10-04 14:58:28 -070049OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070050 uint32_t image_file_location_oat_checksum,
Ian Rogersef7d42f2014-01-06 12:55:46 -080051 uintptr_t image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070052 const std::string& image_file_location,
Ian Rogersca368cb2013-11-15 15:52:08 -080053 const CompilerDriver* compiler,
54 TimingLogger* timings)
Jeff Hao0aba0ba2013-06-03 14:49:28 -070055 : compiler_driver_(compiler),
56 dex_files_(&dex_files),
57 image_file_location_oat_checksum_(image_file_location_oat_checksum),
58 image_file_location_oat_begin_(image_file_location_oat_begin),
59 image_file_location_(image_file_location),
60 oat_header_(NULL),
61 size_dex_file_alignment_(0),
62 size_executable_offset_alignment_(0),
63 size_oat_header_(0),
64 size_oat_header_image_file_location_(0),
65 size_dex_file_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070066 size_interpreter_to_interpreter_bridge_(0),
67 size_interpreter_to_compiled_code_bridge_(0),
68 size_jni_dlsym_lookup_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070069 size_portable_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070070 size_portable_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070071 size_portable_to_interpreter_bridge_(0),
Andreas Gampe2da88232014-02-27 12:26:20 -080072 size_quick_generic_jni_trampoline_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070073 size_quick_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070074 size_quick_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070075 size_quick_to_interpreter_bridge_(0),
76 size_trampoline_alignment_(0),
Vladimir Marko96c6ab92014-04-08 14:00:50 +010077 size_method_header_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070078 size_code_(0),
79 size_code_alignment_(0),
80 size_mapping_table_(0),
81 size_vmap_table_(0),
82 size_gc_map_(0),
83 size_oat_dex_file_location_size_(0),
84 size_oat_dex_file_location_data_(0),
85 size_oat_dex_file_location_checksum_(0),
86 size_oat_dex_file_offset_(0),
87 size_oat_dex_file_methods_offsets_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -070088 size_oat_class_type_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070089 size_oat_class_status_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -070090 size_oat_class_method_bitmaps_(0),
Dave Allisond6ed6422014-04-09 23:36:15 +000091 size_oat_class_method_offsets_(0) {
Ian Rogersca368cb2013-11-15 15:52:08 -080092 size_t offset;
93 {
94 TimingLogger::ScopedSplit split("InitOatHeader", timings);
95 offset = InitOatHeader();
96 }
97 {
98 TimingLogger::ScopedSplit split("InitOatDexFiles", timings);
99 offset = InitOatDexFiles(offset);
100 }
101 {
102 TimingLogger::ScopedSplit split("InitDexFiles", timings);
103 offset = InitDexFiles(offset);
104 }
105 {
106 TimingLogger::ScopedSplit split("InitOatClasses", timings);
107 offset = InitOatClasses(offset);
108 }
109 {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100110 TimingLogger::ScopedSplit split("InitOatMaps", timings);
111 offset = InitOatMaps(offset);
112 }
113 {
Ian Rogersca368cb2013-11-15 15:52:08 -0800114 TimingLogger::ScopedSplit split("InitOatCode", timings);
115 offset = InitOatCode(offset);
116 }
117 {
118 TimingLogger::ScopedSplit split("InitOatCodeDexFiles", timings);
119 offset = InitOatCodeDexFiles(offset);
120 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700121 size_ = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700122
123 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700124 CHECK(image_file_location.empty() == compiler->IsImage());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700125}
126
Ian Rogers0571d352011-11-03 19:51:38 -0700127OatWriter::~OatWriter() {
128 delete oat_header_;
129 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800130 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -0700131}
132
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100133struct OatWriter::GcMapDataAccess {
134 static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
135 return &compiled_method->GetGcMap();
136 }
137
138 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
139 return oat_class->method_offsets_[method_offsets_index].gc_map_offset_;
140 }
141
142 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
143 ALWAYS_INLINE {
144 oat_class->method_offsets_[method_offsets_index].gc_map_offset_ = offset;
145 }
146
147 static const char* Name() ALWAYS_INLINE {
148 return "GC map";
149 }
150};
151
152struct OatWriter::MappingTableDataAccess {
153 static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
154 return &compiled_method->GetMappingTable();
155 }
156
157 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100158 uint32_t offset = oat_class->method_headers_[method_offsets_index].mapping_table_offset_;
159 return offset == 0u ? 0u :
160 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100161 }
162
163 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
164 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100165 oat_class->method_headers_[method_offsets_index].mapping_table_offset_ =
166 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100167 }
168
169 static const char* Name() ALWAYS_INLINE {
170 return "mapping table";
171 }
172};
173
174struct OatWriter::VmapTableDataAccess {
175 static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
176 return &compiled_method->GetVmapTable();
177 }
178
179 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100180 uint32_t offset = oat_class->method_headers_[method_offsets_index].vmap_table_offset_;
181 return offset == 0u ? 0u :
182 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100183 }
184
185 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
186 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100187 oat_class->method_headers_[method_offsets_index].vmap_table_offset_ =
188 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100189 }
190
191 static const char* Name() ALWAYS_INLINE {
192 return "vmap table";
193 }
194};
195
196class OatWriter::DexMethodVisitor {
197 public:
198 DexMethodVisitor(OatWriter* writer, size_t offset)
199 : writer_(writer),
200 offset_(offset),
201 dex_file_(nullptr),
202 class_def_index_(DexFile::kDexNoIndex) {
203 }
204
205 virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) {
206 DCHECK(dex_file_ == nullptr);
207 DCHECK_EQ(class_def_index_, DexFile::kDexNoIndex);
208 dex_file_ = dex_file;
209 class_def_index_ = class_def_index;
210 return true;
211 }
212
213 virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0;
214
215 virtual bool EndClass() {
216 if (kIsDebugBuild) {
217 dex_file_ = nullptr;
218 class_def_index_ = DexFile::kDexNoIndex;
219 }
220 return true;
221 }
222
223 size_t GetOffset() const {
224 return offset_;
225 }
226
227 protected:
228 virtual ~DexMethodVisitor() { }
229
230 OatWriter* const writer_;
231
232 // The offset is usually advanced for each visited method by the derived class.
233 size_t offset_;
234
235 // The dex file and class def index are set in StartClass().
236 const DexFile* dex_file_;
237 size_t class_def_index_;
238};
239
240class OatWriter::OatDexMethodVisitor : public DexMethodVisitor {
241 public:
242 OatDexMethodVisitor(OatWriter* writer, size_t offset)
243 : DexMethodVisitor(writer, offset),
244 oat_class_index_(0u),
245 method_offsets_index_(0u) {
246 }
247
248 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
249 DexMethodVisitor::StartClass(dex_file, class_def_index);
250 DCHECK_LT(oat_class_index_, writer_->oat_classes_.size());
251 method_offsets_index_ = 0u;
252 return true;
253 }
254
255 bool EndClass() {
256 ++oat_class_index_;
257 return DexMethodVisitor::EndClass();
258 }
259
260 protected:
261 size_t oat_class_index_;
262 size_t method_offsets_index_;
263};
264
265class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor {
266 public:
267 InitOatClassesMethodVisitor(OatWriter* writer, size_t offset)
268 : DexMethodVisitor(writer, offset),
269 compiled_methods_(),
270 num_non_null_compiled_methods_(0u) {
271 compiled_methods_.reserve(256u);
272 }
273
274 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
275 DexMethodVisitor::StartClass(dex_file, class_def_index);
276 compiled_methods_.clear();
277 num_non_null_compiled_methods_ = 0u;
278 return true;
279 }
280
281 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
282 // Fill in the compiled_methods_ array for methods that have a
283 // CompiledMethod. We track the number of non-null entries in
284 // num_non_null_compiled_methods_ since we only want to allocate
285 // OatMethodOffsets for the compiled methods.
286 uint32_t method_idx = it.GetMemberIndex();
287 CompiledMethod* compiled_method =
288 writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx));
289 compiled_methods_.push_back(compiled_method);
290 if (compiled_method != nullptr) {
291 ++num_non_null_compiled_methods_;
292 }
293 return true;
294 }
295
296 bool EndClass() {
297 ClassReference class_ref(dex_file_, class_def_index_);
298 CompiledClass* compiled_class = writer_->compiler_driver_->GetCompiledClass(class_ref);
299 mirror::Class::Status status;
300 if (compiled_class != NULL) {
301 status = compiled_class->GetStatus();
302 } else if (writer_->compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) {
303 status = mirror::Class::kStatusError;
304 } else {
305 status = mirror::Class::kStatusNotReady;
306 }
307
308 OatClass* oat_class = new OatClass(offset_, compiled_methods_,
309 num_non_null_compiled_methods_, status);
310 writer_->oat_classes_.push_back(oat_class);
311 offset_ += oat_class->SizeOf();
312 return DexMethodVisitor::EndClass();
313 }
314
315 private:
316 std::vector<CompiledMethod*> compiled_methods_;
317 size_t num_non_null_compiled_methods_;
318};
319
320class OatWriter::InitCodeMethodVisitor : public OatDexMethodVisitor {
321 public:
322 InitCodeMethodVisitor(OatWriter* writer, size_t offset)
323 : OatDexMethodVisitor(writer, offset) {
324 }
325
326 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
327 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
328 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
329 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
330
331 if (compiled_method != nullptr) {
332 // Derived from CompiledMethod.
333 uint32_t quick_code_offset = 0;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100334
335 const std::vector<uint8_t>* portable_code = compiled_method->GetPortableCode();
336 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode();
337 if (portable_code != nullptr) {
338 CHECK(quick_code == nullptr);
339 size_t oat_method_offsets_offset =
340 oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index);
341 compiled_method->AddOatdataOffsetToCompliledCodeOffset(
342 oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_));
343 } else {
344 CHECK(quick_code != nullptr);
345 offset_ = compiled_method->AlignCode(offset_);
346 DCHECK_ALIGNED_PARAM(offset_,
347 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
348 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
349 CHECK_NE(code_size, 0U);
350 uint32_t thumb_offset = compiled_method->CodeDelta();
Vladimir Marko7624d252014-05-02 14:40:15 +0100351 quick_code_offset = offset_ + sizeof(OatQuickMethodHeader) + thumb_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100352
353 std::vector<uint8_t>* cfi_info = writer_->compiler_driver_->GetCallFrameInformation();
354 if (cfi_info != nullptr) {
355 // Copy in the FDE, if present
356 const std::vector<uint8_t>* fde = compiled_method->GetCFIInfo();
357 if (fde != nullptr) {
358 // Copy the information into cfi_info and then fix the address in the new copy.
359 int cur_offset = cfi_info->size();
360 cfi_info->insert(cfi_info->end(), fde->begin(), fde->end());
361
362 // Set the 'initial_location' field to address the start of the method.
363 uint32_t new_value = quick_code_offset - writer_->oat_header_->GetExecutableOffset();
364 uint32_t offset_to_update = cur_offset + 2*sizeof(uint32_t);
365 (*cfi_info)[offset_to_update+0] = new_value;
366 (*cfi_info)[offset_to_update+1] = new_value >> 8;
367 (*cfi_info)[offset_to_update+2] = new_value >> 16;
368 (*cfi_info)[offset_to_update+3] = new_value >> 24;
369 std::string name = PrettyMethod(it.GetMemberIndex(), *dex_file_, false);
370 writer_->method_info_.push_back(DebugInfo(name, new_value, new_value + code_size));
371 }
372 }
373
374 // Deduplicate code arrays.
Vladimir Marko8a630572014-04-09 18:45:35 +0100375 auto code_iter = dedupe_map_.find(compiled_method);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100376 if (code_iter != dedupe_map_.end()) {
377 quick_code_offset = code_iter->second;
378 } else {
Vladimir Marko8a630572014-04-09 18:45:35 +0100379 dedupe_map_.Put(compiled_method, quick_code_offset);
Vladimir Marko7624d252014-05-02 14:40:15 +0100380 }
381
382 // Update quick method header.
383 DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size());
384 OatQuickMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_];
385 uint32_t mapping_table_offset = method_header->mapping_table_offset_;
386 uint32_t vmap_table_offset = method_header->vmap_table_offset_;
387 // The code offset was 0 when the mapping/vmap table offset was set, so it's set
388 // to 0-offset and we need to adjust it by code_offset.
389 uint32_t code_offset = quick_code_offset - thumb_offset;
390 if (mapping_table_offset != 0u) {
391 mapping_table_offset += code_offset;
392 DCHECK_LT(mapping_table_offset, code_offset);
393 }
394 if (vmap_table_offset != 0u) {
395 vmap_table_offset += code_offset;
396 DCHECK_LT(vmap_table_offset, code_offset);
397 }
398 uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
399 uint32_t core_spill_mask = compiled_method->GetCoreSpillMask();
400 uint32_t fp_spill_mask = compiled_method->GetFpSpillMask();
401 *method_header = OatQuickMethodHeader(mapping_table_offset, vmap_table_offset,
402 frame_size_in_bytes, core_spill_mask, fp_spill_mask,
403 code_size);
404
405 // Update checksum if this wasn't a duplicate.
406 if (code_iter == dedupe_map_.end()) {
Vladimir Marko8a630572014-04-09 18:45:35 +0100407 writer_->oat_header_->UpdateChecksum(method_header, sizeof(*method_header));
408 offset_ += sizeof(*method_header); // Method header is prepended before code.
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100409 writer_->oat_header_->UpdateChecksum(&(*quick_code)[0], code_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100410 offset_ += code_size;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100411 }
412 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100413
414 if (kIsDebugBuild) {
415 // We expect GC maps except when the class hasn't been verified or the method is native.
416 const CompilerDriver* compiler_driver = writer_->compiler_driver_;
417 ClassReference class_ref(dex_file_, class_def_index_);
418 CompiledClass* compiled_class = compiler_driver->GetCompiledClass(class_ref);
419 mirror::Class::Status status;
420 if (compiled_class != NULL) {
421 status = compiled_class->GetStatus();
422 } else if (compiler_driver->GetVerificationResults()->IsClassRejected(class_ref)) {
423 status = mirror::Class::kStatusError;
424 } else {
425 status = mirror::Class::kStatusNotReady;
426 }
427 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
428 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
429 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
430 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
431 << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
432 << (status < mirror::Class::kStatusVerified) << " " << status << " "
433 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
434 }
435
436 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
437 OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_];
438 offsets->code_offset_ = quick_code_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100439 ++method_offsets_index_;
440 }
441
442 return true;
443 }
444
445 private:
446 // Deduplication is already done on a pointer basis by the compiler driver,
447 // so we can simply compare the pointers to find out if things are duplicated.
Vladimir Marko8a630572014-04-09 18:45:35 +0100448 SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100449};
450
451template <typename DataAccess>
452class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor {
453 public:
454 InitMapMethodVisitor(OatWriter* writer, size_t offset)
455 : OatDexMethodVisitor(writer, offset) {
456 }
457
458 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
459 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
460 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
461 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
462
463 if (compiled_method != nullptr) {
464 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
465 DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u);
466
467 const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method);
468 uint32_t map_size = map->size() * sizeof((*map)[0]);
469 if (map_size != 0u) {
470 auto it = dedupe_map_.find(map);
471 if (it != dedupe_map_.end()) {
472 DataAccess::SetOffset(oat_class, method_offsets_index_, it->second);
473 } else {
474 DataAccess::SetOffset(oat_class, method_offsets_index_, offset_);
475 dedupe_map_.Put(map, offset_);
476 offset_ += map_size;
477 writer_->oat_header_->UpdateChecksum(&(*map)[0], map_size);
478 }
479 }
480 ++method_offsets_index_;
481 }
482
483 return true;
484 }
485
486 private:
487 // Deduplication is already done on a pointer basis by the compiler driver,
488 // so we can simply compare the pointers to find out if things are duplicated.
489 SafeMap<const std::vector<uint8_t>*, uint32_t> dedupe_map_;
490};
491
492class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor {
493 public:
494 InitImageMethodVisitor(OatWriter* writer, size_t offset)
495 : OatDexMethodVisitor(writer, offset) {
496 }
497
498 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
499 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
500 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
501 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
502
Vladimir Marko7624d252014-05-02 14:40:15 +0100503 OatMethodOffsets offsets(0u, 0u);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100504 if (compiled_method != nullptr) {
505 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
506 offsets = oat_class->method_offsets_[method_offsets_index_];
507 ++method_offsets_index_;
508 }
509
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100510 ClassLinker* linker = Runtime::Current()->GetClassLinker();
511 InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_));
512 // Unchecked as we hold mutator_lock_ on entry.
513 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700514 StackHandleScope<2> hs(soa.Self());
515 Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache(*dex_file_)));
Vladimir Marko7624d252014-05-02 14:40:15 +0100516 mirror::ArtMethod* method = linker->ResolveMethod(*dex_file_, it.GetMemberIndex(), dex_cache,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700517 NullHandle<mirror::ClassLoader>(),
518 NullHandle<mirror::ArtMethod>(),
519 invoke_type);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100520 CHECK(method != NULL);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100521 // Portable code offsets are set by ElfWriterMclinker::FixupCompiledCodeOffset after linking.
522 method->SetQuickOatCodeOffset(offsets.code_offset_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100523 method->SetOatNativeGcMapOffset(offsets.gc_map_offset_);
524
525 return true;
526 }
527};
528
529class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor {
530 public:
531 WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
532 size_t relative_offset)
533 : OatDexMethodVisitor(writer, relative_offset),
534 out_(out),
535 file_offset_(file_offset) {
536 }
537
538 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
539 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
540 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
541
542 if (compiled_method != NULL) { // ie. not an abstract method
543 size_t file_offset = file_offset_;
544 OutputStream* out = out_;
545
546 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode();
547 if (quick_code != nullptr) {
548 CHECK(compiled_method->GetPortableCode() == nullptr);
549 uint32_t aligned_offset = compiled_method->AlignCode(offset_);
550 uint32_t aligned_code_delta = aligned_offset - offset_;
551 if (aligned_code_delta != 0) {
552 static const uint8_t kPadding[] = {
553 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
554 };
555 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
556 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
557 ReportWriteFailure("code alignment padding", it);
558 return false;
559 }
560 writer_->size_code_alignment_ += aligned_code_delta;
561 offset_ += aligned_code_delta;
562 DCHECK_OFFSET_();
563 }
564 DCHECK_ALIGNED_PARAM(offset_,
565 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
566 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
567 CHECK_NE(code_size, 0U);
568
569 // Deduplicate code arrays.
570 const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_];
571 DCHECK(method_offsets.code_offset_ < offset_ || method_offsets.code_offset_ ==
Vladimir Marko7624d252014-05-02 14:40:15 +0100572 offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta())
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100573 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
574 if (method_offsets.code_offset_ >= offset_) {
Vladimir Marko7624d252014-05-02 14:40:15 +0100575 const OatQuickMethodHeader& method_header = oat_class->method_headers_[method_offsets_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100576 if (!out->WriteFully(&method_header, sizeof(method_header))) {
577 ReportWriteFailure("method header", it);
578 return false;
579 }
580 writer_->size_method_header_ += sizeof(method_header);
581 offset_ += sizeof(method_header);
582 DCHECK_OFFSET_();
583 if (!out->WriteFully(&(*quick_code)[0], code_size)) {
584 ReportWriteFailure("method code", it);
585 return false;
586 }
587 writer_->size_code_ += code_size;
588 offset_ += code_size;
589 }
590 DCHECK_OFFSET_();
591 }
592 ++method_offsets_index_;
593 }
594
595 return true;
596 }
597
598 private:
599 OutputStream* const out_;
600 size_t const file_offset_;
601
602 void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) {
603 PLOG(ERROR) << "Failed to write " << what << " for "
604 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
605 }
606};
607
608template <typename DataAccess>
609class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor {
610 public:
611 WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
612 size_t relative_offset)
613 : OatDexMethodVisitor(writer, relative_offset),
614 out_(out),
615 file_offset_(file_offset) {
616 }
617
618 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
619 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
620 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
621
622 if (compiled_method != NULL) { // ie. not an abstract method
623 size_t file_offset = file_offset_;
624 OutputStream* out = out_;
625
626 uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_);
627 ++method_offsets_index_;
628
629 // Write deduplicated map.
630 const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method);
631 size_t map_size = map->size() * sizeof((*map)[0]);
632 DCHECK((map_size == 0u && map_offset == 0u) ||
633 (map_size != 0u && map_offset != 0u && map_offset <= offset_))
634 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
635 if (map_size != 0u && map_offset == offset_) {
636 if (UNLIKELY(!out->WriteFully(&(*map)[0], map_size))) {
637 ReportWriteFailure(it);
638 return false;
639 }
640 offset_ += map_size;
641 }
642 DCHECK_OFFSET_();
643 }
644
645 return true;
646 }
647
648 private:
649 OutputStream* const out_;
650 size_t const file_offset_;
651
652 void ReportWriteFailure(const ClassDataItemIterator& it) {
653 PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for "
654 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
655 }
656};
657
658// Visit all methods from all classes in all dex files with the specified visitor.
659bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) {
660 for (const DexFile* dex_file : *dex_files_) {
661 const size_t class_def_count = dex_file->NumClassDefs();
662 for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) {
663 if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) {
664 return false;
665 }
666 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
667 const byte* class_data = dex_file->GetClassData(class_def);
668 if (class_data != NULL) { // ie not an empty class, such as a marker interface
669 ClassDataItemIterator it(*dex_file, class_data);
670 while (it.HasNextStaticField()) {
671 it.Next();
672 }
673 while (it.HasNextInstanceField()) {
674 it.Next();
675 }
676 size_t class_def_method_index = 0u;
677 while (it.HasNextDirectMethod()) {
678 if (!visitor->VisitMethod(class_def_method_index, it)) {
679 return false;
680 }
681 ++class_def_method_index;
682 it.Next();
683 }
684 while (it.HasNextVirtualMethod()) {
685 if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) {
686 return false;
687 }
688 ++class_def_method_index;
689 it.Next();
690 }
691 }
692 if (UNLIKELY(!visitor->EndClass())) {
693 return false;
694 }
695 }
696 }
697 return true;
698}
699
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700700size_t OatWriter::InitOatHeader() {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700701 // create the OatHeader
Ian Rogers1212a022013-03-04 10:48:41 -0800702 oat_header_ = new OatHeader(compiler_driver_->GetInstructionSet(),
Dave Allison70202782013-10-22 17:52:19 -0700703 compiler_driver_->GetInstructionSetFeatures(),
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700704 dex_files_,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700705 image_file_location_oat_checksum_,
706 image_file_location_oat_begin_,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700707 image_file_location_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700708 size_t offset = sizeof(*oat_header_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700709 offset += image_file_location_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700710 return offset;
711}
712
713size_t OatWriter::InitOatDexFiles(size_t offset) {
714 // create the OatDexFiles
715 for (size_t i = 0; i != dex_files_->size(); ++i) {
716 const DexFile* dex_file = (*dex_files_)[i];
717 CHECK(dex_file != NULL);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800718 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700719 oat_dex_files_.push_back(oat_dex_file);
720 offset += oat_dex_file->SizeOf();
721 }
722 return offset;
723}
724
Brian Carlstrom89521892011-12-07 22:05:07 -0800725size_t OatWriter::InitDexFiles(size_t offset) {
726 // calculate the offsets within OatDexFiles to the DexFiles
727 for (size_t i = 0; i != dex_files_->size(); ++i) {
728 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700729 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800730 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700731 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800732
733 // set offset in OatDexFile to DexFile
734 oat_dex_files_[i]->dex_file_offset_ = offset;
735
736 const DexFile* dex_file = (*dex_files_)[i];
737 offset += dex_file->GetHeader().file_size_;
738 }
739 return offset;
740}
741
Brian Carlstrom389efb02012-01-11 12:06:26 -0800742size_t OatWriter::InitOatClasses(size_t offset) {
Brian Carlstrom389efb02012-01-11 12:06:26 -0800743 // calculate the offsets within OatDexFiles to OatClasses
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100744 InitOatClassesMethodVisitor visitor(this, offset);
745 bool success = VisitDexMethods(&visitor);
746 CHECK(success);
747 offset = visitor.GetOffset();
Brian Carlstromba150c32013-08-27 17:31:03 -0700748
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100749 // Update oat_dex_files_.
750 auto oat_class_it = oat_classes_.begin();
751 for (OatDexFile* oat_dex_file : oat_dex_files_) {
752 for (uint32_t& offset : oat_dex_file->methods_offsets_) {
753 DCHECK(oat_class_it != oat_classes_.end());
754 offset = (*oat_class_it)->offset_;
755 ++oat_class_it;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700756 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100757 oat_dex_file->UpdateChecksum(oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700758 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100759 CHECK(oat_class_it == oat_classes_.end());
760
761 return offset;
762}
763
764size_t OatWriter::InitOatMaps(size_t offset) {
765 #define VISIT(VisitorType) \
766 do { \
767 VisitorType visitor(this, offset); \
768 bool success = VisitDexMethods(&visitor); \
769 DCHECK(success); \
770 offset = visitor.GetOffset(); \
771 } while (false)
772
773 VISIT(InitMapMethodVisitor<GcMapDataAccess>);
774 VISIT(InitMapMethodVisitor<MappingTableDataAccess>);
775 VISIT(InitMapMethodVisitor<VmapTableDataAccess>);
776
777 #undef VISIT
778
Brian Carlstrome24fa612011-09-29 00:53:55 -0700779 return offset;
780}
781
782size_t OatWriter::InitOatCode(size_t offset) {
783 // calculate the offsets within OatHeader to executable code
784 size_t old_offset = offset;
785 // required to be on a new page boundary
786 offset = RoundUp(offset, kPageSize);
787 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700788 size_executable_offset_alignment_ = offset - old_offset;
789 if (compiler_driver_->IsImage()) {
790 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700791
Ian Rogers848871b2013-08-05 10:56:33 -0700792 #define DO_TRAMPOLINE(field, fn_name) \
793 offset = CompiledCode::AlignCode(offset, instruction_set); \
794 oat_header_->Set ## fn_name ## Offset(offset); \
795 field.reset(compiler_driver_->Create ## fn_name()); \
796 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700797
Ian Rogers848871b2013-08-05 10:56:33 -0700798 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge);
799 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge);
800 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Jeff Hao88474b42013-10-23 16:24:40 -0700801 DO_TRAMPOLINE(portable_imt_conflict_trampoline_, PortableImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700802 DO_TRAMPOLINE(portable_resolution_trampoline_, PortableResolutionTrampoline);
803 DO_TRAMPOLINE(portable_to_interpreter_bridge_, PortableToInterpreterBridge);
Andreas Gampe2da88232014-02-27 12:26:20 -0800804 DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline);
Jeff Hao88474b42013-10-23 16:24:40 -0700805 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700806 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
807 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700808
Ian Rogers848871b2013-08-05 10:56:33 -0700809 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700810 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700811 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
812 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
813 oat_header_->SetJniDlsymLookupOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700814 oat_header_->SetPortableImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700815 oat_header_->SetPortableResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700816 oat_header_->SetPortableToInterpreterBridgeOffset(0);
Andreas Gampe2da88232014-02-27 12:26:20 -0800817 oat_header_->SetQuickGenericJniTrampolineOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700818 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700819 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700820 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700821 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700822 return offset;
823}
824
825size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100826 #define VISIT(VisitorType) \
827 do { \
828 VisitorType visitor(this, offset); \
829 bool success = VisitDexMethods(&visitor); \
830 DCHECK(success); \
831 offset = visitor.GetOffset(); \
832 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700833
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100834 VISIT(InitCodeMethodVisitor);
Ian Rogers1212a022013-03-04 10:48:41 -0800835 if (compiler_driver_->IsImage()) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100836 VISIT(InitImageMethodVisitor);
Ian Rogers0571d352011-11-03 19:51:38 -0700837 }
Logan Chien8b977d32012-02-21 19:14:55 +0800838
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100839 #undef VISIT
840
Brian Carlstrome24fa612011-09-29 00:53:55 -0700841 return offset;
842}
843
Ian Rogers3d504072014-03-01 09:16:49 -0800844bool OatWriter::Write(OutputStream* out) {
845 const size_t file_offset = out->Seek(0, kSeekCurrent);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700846
Ian Rogers3d504072014-03-01 09:16:49 -0800847 if (!out->WriteFully(oat_header_, sizeof(*oat_header_))) {
848 PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700849 return false;
850 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700851 size_oat_header_ += sizeof(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700852
Ian Rogers3d504072014-03-01 09:16:49 -0800853 if (!out->WriteFully(image_file_location_.data(), image_file_location_.size())) {
854 PLOG(ERROR) << "Failed to write oat header image file location to " << out->GetLocation();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700855 return false;
856 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700857 size_oat_header_image_file_location_ += image_file_location_.size();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700858
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700859 if (!WriteTables(out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800860 LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700861 return false;
862 }
863
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100864 size_t relative_offset = out->Seek(0, kSeekCurrent) - file_offset;
865 relative_offset = WriteMaps(out, file_offset, relative_offset);
866 if (relative_offset == 0) {
867 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
868 return false;
869 }
870
871 relative_offset = WriteCode(out, file_offset, relative_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700872 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800873 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700874 return false;
875 }
876
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700877 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
878 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800879 LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700880 return false;
881 }
882
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700883 if (kIsDebugBuild) {
884 uint32_t size_total = 0;
885 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700886 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700887 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700888
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700889 DO_STAT(size_dex_file_alignment_);
890 DO_STAT(size_executable_offset_alignment_);
891 DO_STAT(size_oat_header_);
892 DO_STAT(size_oat_header_image_file_location_);
893 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -0700894 DO_STAT(size_interpreter_to_interpreter_bridge_);
895 DO_STAT(size_interpreter_to_compiled_code_bridge_);
896 DO_STAT(size_jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -0700897 DO_STAT(size_portable_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700898 DO_STAT(size_portable_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700899 DO_STAT(size_portable_to_interpreter_bridge_);
Andreas Gampe2da88232014-02-27 12:26:20 -0800900 DO_STAT(size_quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -0700901 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700902 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700903 DO_STAT(size_quick_to_interpreter_bridge_);
904 DO_STAT(size_trampoline_alignment_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100905 DO_STAT(size_method_header_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700906 DO_STAT(size_code_);
907 DO_STAT(size_code_alignment_);
908 DO_STAT(size_mapping_table_);
909 DO_STAT(size_vmap_table_);
910 DO_STAT(size_gc_map_);
911 DO_STAT(size_oat_dex_file_location_size_);
912 DO_STAT(size_oat_dex_file_location_data_);
913 DO_STAT(size_oat_dex_file_location_checksum_);
914 DO_STAT(size_oat_dex_file_offset_);
915 DO_STAT(size_oat_dex_file_methods_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700916 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700917 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700918 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700919 DO_STAT(size_oat_class_method_offsets_);
920 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700921
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700922 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Ian Rogers3d504072014-03-01 09:16:49 -0800923 CHECK_EQ(file_offset + size_total, static_cast<uint32_t>(out->Seek(0, kSeekCurrent)));
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700924 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700925 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700926
Ian Rogers3d504072014-03-01 09:16:49 -0800927 CHECK_EQ(file_offset + size_, static_cast<uint32_t>(out->Seek(0, kSeekCurrent)));
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700928 CHECK_EQ(size_, relative_offset);
929
Brian Carlstrome24fa612011-09-29 00:53:55 -0700930 return true;
931}
932
Ian Rogers3d504072014-03-01 09:16:49 -0800933bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700934 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700935 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800936 PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700937 return false;
938 }
939 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800940 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700941 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Ian Rogers3d504072014-03-01 09:16:49 -0800942 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -0800943 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
944 const DexFile* dex_file = (*dex_files_)[i];
945 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
946 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
947 return false;
948 }
949 const DexFile* dex_file = (*dex_files_)[i];
Ian Rogers3d504072014-03-01 09:16:49 -0800950 if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700951 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
Ian Rogers3d504072014-03-01 09:16:49 -0800952 << " to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800953 return false;
954 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700955 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800956 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800957 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700958 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800959 PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700960 return false;
961 }
962 }
963 return true;
964}
965
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100966size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) {
967 #define VISIT(VisitorType) \
968 do { \
969 VisitorType visitor(this, out, file_offset, relative_offset); \
970 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
971 return 0; \
972 } \
973 relative_offset = visitor.GetOffset(); \
974 } while (false)
975
976 size_t gc_maps_offset = relative_offset;
977 VISIT(WriteMapMethodVisitor<GcMapDataAccess>);
978 size_gc_map_ = relative_offset - gc_maps_offset;
979
980 size_t mapping_tables_offset = relative_offset;
981 VISIT(WriteMapMethodVisitor<MappingTableDataAccess>);
982 size_mapping_table_ = relative_offset - mapping_tables_offset;
983
984 size_t vmap_tables_offset = relative_offset;
985 VISIT(WriteMapMethodVisitor<VmapTableDataAccess>);
986 size_vmap_table_ = relative_offset - vmap_tables_offset;
987
988 #undef VISIT
989
990 return relative_offset;
991}
992
993size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) {
Ian Rogers3d504072014-03-01 09:16:49 -0800994 off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100995 relative_offset += size_executable_offset_alignment_;
996 DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset());
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700997 size_t expected_file_offset = file_offset + relative_offset;
998 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700999 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Ian Rogers3d504072014-03-01 09:16:49 -08001000 << " Expected: " << expected_file_offset << " File: " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001001 return 0;
1002 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001003 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001004 if (compiler_driver_->IsImage()) {
1005 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001006
Ian Rogers848871b2013-08-05 10:56:33 -07001007 #define DO_TRAMPOLINE(field) \
1008 do { \
1009 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
1010 uint32_t alignment_padding = aligned_offset - relative_offset; \
Ian Rogers3d504072014-03-01 09:16:49 -08001011 out->Seek(alignment_padding, kSeekCurrent); \
Ian Rogers848871b2013-08-05 10:56:33 -07001012 size_trampoline_alignment_ += alignment_padding; \
Ian Rogers3d504072014-03-01 09:16:49 -08001013 if (!out->WriteFully(&(*field)[0], field->size())) { \
1014 PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \
Ian Rogers848871b2013-08-05 10:56:33 -07001015 return false; \
1016 } \
1017 size_ ## field += field->size(); \
1018 relative_offset += alignment_padding + field->size(); \
1019 DCHECK_OFFSET(); \
1020 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001021
Ian Rogers848871b2013-08-05 10:56:33 -07001022 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_);
1023 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_);
1024 DO_TRAMPOLINE(jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -07001025 DO_TRAMPOLINE(portable_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001026 DO_TRAMPOLINE(portable_resolution_trampoline_);
1027 DO_TRAMPOLINE(portable_to_interpreter_bridge_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001028 DO_TRAMPOLINE(quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001029 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001030 DO_TRAMPOLINE(quick_resolution_trampoline_);
1031 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
1032 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001033 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001034 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001035}
1036
Ian Rogers3d504072014-03-01 09:16:49 -08001037size_t OatWriter::WriteCodeDexFiles(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001038 const size_t file_offset,
1039 size_t relative_offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001040 #define VISIT(VisitorType) \
1041 do { \
1042 VisitorType visitor(this, out, file_offset, relative_offset); \
1043 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1044 return 0; \
1045 } \
1046 relative_offset = visitor.GetOffset(); \
1047 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001048
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001049 VISIT(WriteCodeMethodVisitor);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001050
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001051 #undef VISIT
Brian Carlstrom265091e2013-01-30 14:08:26 -08001052
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001053 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001054}
1055
Brian Carlstrom265091e2013-01-30 14:08:26 -08001056OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
1057 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -08001058 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001059 dex_file_location_size_ = location.size();
1060 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001061 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -08001062 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001063 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001064}
1065
1066size_t OatWriter::OatDexFile::SizeOf() const {
1067 return sizeof(dex_file_location_size_)
1068 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001069 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -08001070 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001071 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001072}
1073
Ian Rogers3d504072014-03-01 09:16:49 -08001074void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const {
1075 oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
1076 oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
1077 oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
1078 oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
1079 oat_header->UpdateChecksum(&methods_offsets_[0],
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001080 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001081}
1082
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001083bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001084 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001085 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001086 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001087 if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
1088 PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001089 return false;
1090 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001091 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001092 if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
1093 PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001094 return false;
1095 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001096 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Ian Rogers3d504072014-03-01 09:16:49 -08001097 if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
1098 PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001099 return false;
1100 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001101 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Ian Rogers3d504072014-03-01 09:16:49 -08001102 if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
1103 PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001104 return false;
1105 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001106 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Ian Rogers3d504072014-03-01 09:16:49 -08001107 if (!out->WriteFully(&methods_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001108 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001109 PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001110 return false;
1111 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001112 oat_writer->size_oat_dex_file_methods_offsets_ +=
1113 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001114 return true;
1115}
1116
Brian Carlstromba150c32013-08-27 17:31:03 -07001117OatWriter::OatClass::OatClass(size_t offset,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001118 const std::vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -07001119 uint32_t num_non_null_compiled_methods,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001120 mirror::Class::Status status)
1121 : compiled_methods_(compiled_methods) {
1122 uint32_t num_methods = compiled_methods.size();
Brian Carlstromba150c32013-08-27 17:31:03 -07001123 CHECK_LE(num_non_null_compiled_methods, num_methods);
1124
Brian Carlstrom265091e2013-01-30 14:08:26 -08001125 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -07001126 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
1127
1128 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
1129 // apply when there are 0 methods, we just arbitrarily say that 0
1130 // methods means kOatClassNoneCompiled and that we won't use
1131 // kOatClassAllCompiled unless there is at least one compiled
1132 // method. This means in an interpretter only system, we can assert
1133 // that all classes are kOatClassNoneCompiled.
1134 if (num_non_null_compiled_methods == 0) {
1135 type_ = kOatClassNoneCompiled;
1136 } else if (num_non_null_compiled_methods == num_methods) {
1137 type_ = kOatClassAllCompiled;
1138 } else {
1139 type_ = kOatClassSomeCompiled;
1140 }
1141
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001142 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -07001143 method_offsets_.resize(num_non_null_compiled_methods);
Vladimir Marko8a630572014-04-09 18:45:35 +01001144 method_headers_.resize(num_non_null_compiled_methods);
Brian Carlstromba150c32013-08-27 17:31:03 -07001145
1146 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
1147 if (type_ == kOatClassSomeCompiled) {
1148 method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator());
1149 method_bitmap_size_ = method_bitmap_->GetSizeOf();
1150 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
1151 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
1152 } else {
1153 method_bitmap_ = NULL;
1154 method_bitmap_size_ = 0;
1155 }
1156
1157 for (size_t i = 0; i < num_methods; i++) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001158 CompiledMethod* compiled_method = compiled_methods_[i];
Brian Carlstromba150c32013-08-27 17:31:03 -07001159 if (compiled_method == NULL) {
1160 oat_method_offsets_offsets_from_oat_class_[i] = 0;
1161 } else {
1162 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
1163 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
1164 if (type_ == kOatClassSomeCompiled) {
1165 method_bitmap_->SetBit(i);
1166 }
1167 }
1168 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001169}
1170
Brian Carlstromba150c32013-08-27 17:31:03 -07001171OatWriter::OatClass::~OatClass() {
Mathieu Chartier661974a2014-01-09 11:23:53 -08001172 delete method_bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -07001173}
1174
Brian Carlstrom265091e2013-01-30 14:08:26 -08001175size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
1176 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001177 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
1178 if (method_offset == 0) {
1179 return 0;
1180 }
1181 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001182}
1183
1184size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
1185 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001186 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08001187}
1188
1189size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001190 return sizeof(status_)
1191 + sizeof(type_)
1192 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
1193 + method_bitmap_size_
1194 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001195}
1196
Ian Rogers3d504072014-03-01 09:16:49 -08001197void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const {
1198 oat_header->UpdateChecksum(&status_, sizeof(status_));
1199 oat_header->UpdateChecksum(&type_, sizeof(type_));
Brian Carlstromba150c32013-08-27 17:31:03 -07001200 if (method_bitmap_size_ != 0) {
1201 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001202 oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_));
1203 oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001204 }
Ian Rogers3d504072014-03-01 09:16:49 -08001205 oat_header->UpdateChecksum(&method_offsets_[0],
1206 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001207}
1208
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001209bool OatWriter::OatClass::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001210 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001211 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001212 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001213 if (!out->WriteFully(&status_, sizeof(status_))) {
1214 PLOG(ERROR) << "Failed to write class status to " << out->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001215 return false;
1216 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001217 oat_writer->size_oat_class_status_ += sizeof(status_);
Ian Rogers3d504072014-03-01 09:16:49 -08001218 if (!out->WriteFully(&type_, sizeof(type_))) {
1219 PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001220 return false;
1221 }
1222 oat_writer->size_oat_class_type_ += sizeof(type_);
1223 if (method_bitmap_size_ != 0) {
1224 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001225 if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
1226 PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001227 return false;
1228 }
1229 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001230 if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
1231 PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001232 return false;
1233 }
1234 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1235 }
Ian Rogers3d504072014-03-01 09:16:49 -08001236 if (!out->WriteFully(&method_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001237 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001238 PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001239 return false;
1240 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001241 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001242 return true;
1243}
1244
Brian Carlstrome24fa612011-09-29 00:53:55 -07001245} // namespace art