blob: 2114fe921d543ade3f9d7a6dc1d689b3e2749942 [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 Chartierc645f1d2014-03-06 18:11:53 -080036#include "sirt_ref-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 {
158 return oat_class->method_offsets_[method_offsets_index].mapping_table_offset_;
159 }
160
161 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
162 ALWAYS_INLINE {
163 oat_class->method_offsets_[method_offsets_index].mapping_table_offset_ = offset;
164 }
165
166 static const char* Name() ALWAYS_INLINE {
167 return "mapping table";
168 }
169};
170
171struct OatWriter::VmapTableDataAccess {
172 static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
173 return &compiled_method->GetVmapTable();
174 }
175
176 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
177 return oat_class->method_offsets_[method_offsets_index].vmap_table_offset_;
178 }
179
180 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
181 ALWAYS_INLINE {
182 oat_class->method_offsets_[method_offsets_index].vmap_table_offset_ = offset;
183 }
184
185 static const char* Name() ALWAYS_INLINE {
186 return "vmap table";
187 }
188};
189
190class OatWriter::DexMethodVisitor {
191 public:
192 DexMethodVisitor(OatWriter* writer, size_t offset)
193 : writer_(writer),
194 offset_(offset),
195 dex_file_(nullptr),
196 class_def_index_(DexFile::kDexNoIndex) {
197 }
198
199 virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) {
200 DCHECK(dex_file_ == nullptr);
201 DCHECK_EQ(class_def_index_, DexFile::kDexNoIndex);
202 dex_file_ = dex_file;
203 class_def_index_ = class_def_index;
204 return true;
205 }
206
207 virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0;
208
209 virtual bool EndClass() {
210 if (kIsDebugBuild) {
211 dex_file_ = nullptr;
212 class_def_index_ = DexFile::kDexNoIndex;
213 }
214 return true;
215 }
216
217 size_t GetOffset() const {
218 return offset_;
219 }
220
221 protected:
222 virtual ~DexMethodVisitor() { }
223
224 OatWriter* const writer_;
225
226 // The offset is usually advanced for each visited method by the derived class.
227 size_t offset_;
228
229 // The dex file and class def index are set in StartClass().
230 const DexFile* dex_file_;
231 size_t class_def_index_;
232};
233
234class OatWriter::OatDexMethodVisitor : public DexMethodVisitor {
235 public:
236 OatDexMethodVisitor(OatWriter* writer, size_t offset)
237 : DexMethodVisitor(writer, offset),
238 oat_class_index_(0u),
239 method_offsets_index_(0u) {
240 }
241
242 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
243 DexMethodVisitor::StartClass(dex_file, class_def_index);
244 DCHECK_LT(oat_class_index_, writer_->oat_classes_.size());
245 method_offsets_index_ = 0u;
246 return true;
247 }
248
249 bool EndClass() {
250 ++oat_class_index_;
251 return DexMethodVisitor::EndClass();
252 }
253
254 protected:
255 size_t oat_class_index_;
256 size_t method_offsets_index_;
257};
258
259class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor {
260 public:
261 InitOatClassesMethodVisitor(OatWriter* writer, size_t offset)
262 : DexMethodVisitor(writer, offset),
263 compiled_methods_(),
264 num_non_null_compiled_methods_(0u) {
265 compiled_methods_.reserve(256u);
266 }
267
268 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
269 DexMethodVisitor::StartClass(dex_file, class_def_index);
270 compiled_methods_.clear();
271 num_non_null_compiled_methods_ = 0u;
272 return true;
273 }
274
275 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
276 // Fill in the compiled_methods_ array for methods that have a
277 // CompiledMethod. We track the number of non-null entries in
278 // num_non_null_compiled_methods_ since we only want to allocate
279 // OatMethodOffsets for the compiled methods.
280 uint32_t method_idx = it.GetMemberIndex();
281 CompiledMethod* compiled_method =
282 writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx));
283 compiled_methods_.push_back(compiled_method);
284 if (compiled_method != nullptr) {
285 ++num_non_null_compiled_methods_;
286 }
287 return true;
288 }
289
290 bool EndClass() {
291 ClassReference class_ref(dex_file_, class_def_index_);
292 CompiledClass* compiled_class = writer_->compiler_driver_->GetCompiledClass(class_ref);
293 mirror::Class::Status status;
294 if (compiled_class != NULL) {
295 status = compiled_class->GetStatus();
296 } else if (writer_->compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) {
297 status = mirror::Class::kStatusError;
298 } else {
299 status = mirror::Class::kStatusNotReady;
300 }
301
302 OatClass* oat_class = new OatClass(offset_, compiled_methods_,
303 num_non_null_compiled_methods_, status);
304 writer_->oat_classes_.push_back(oat_class);
305 offset_ += oat_class->SizeOf();
306 return DexMethodVisitor::EndClass();
307 }
308
309 private:
310 std::vector<CompiledMethod*> compiled_methods_;
311 size_t num_non_null_compiled_methods_;
312};
313
314class OatWriter::InitCodeMethodVisitor : public OatDexMethodVisitor {
315 public:
316 InitCodeMethodVisitor(OatWriter* writer, size_t offset)
317 : OatDexMethodVisitor(writer, offset) {
318 }
319
320 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
321 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
322 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
323 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
324
325 if (compiled_method != nullptr) {
326 // Derived from CompiledMethod.
327 uint32_t quick_code_offset = 0;
328 uint32_t frame_size_in_bytes = kStackAlignment;
329 uint32_t core_spill_mask = 0;
330 uint32_t fp_spill_mask = 0;
331
332 const std::vector<uint8_t>* portable_code = compiled_method->GetPortableCode();
333 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode();
334 if (portable_code != nullptr) {
335 CHECK(quick_code == nullptr);
336 size_t oat_method_offsets_offset =
337 oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index);
338 compiled_method->AddOatdataOffsetToCompliledCodeOffset(
339 oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_));
340 } else {
341 CHECK(quick_code != nullptr);
342 offset_ = compiled_method->AlignCode(offset_);
343 DCHECK_ALIGNED_PARAM(offset_,
344 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
345 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
346 CHECK_NE(code_size, 0U);
347 uint32_t thumb_offset = compiled_method->CodeDelta();
348 quick_code_offset = offset_ + sizeof(OatMethodHeader) + thumb_offset;
349
350 std::vector<uint8_t>* cfi_info = writer_->compiler_driver_->GetCallFrameInformation();
351 if (cfi_info != nullptr) {
352 // Copy in the FDE, if present
353 const std::vector<uint8_t>* fde = compiled_method->GetCFIInfo();
354 if (fde != nullptr) {
355 // Copy the information into cfi_info and then fix the address in the new copy.
356 int cur_offset = cfi_info->size();
357 cfi_info->insert(cfi_info->end(), fde->begin(), fde->end());
358
359 // Set the 'initial_location' field to address the start of the method.
360 uint32_t new_value = quick_code_offset - writer_->oat_header_->GetExecutableOffset();
361 uint32_t offset_to_update = cur_offset + 2*sizeof(uint32_t);
362 (*cfi_info)[offset_to_update+0] = new_value;
363 (*cfi_info)[offset_to_update+1] = new_value >> 8;
364 (*cfi_info)[offset_to_update+2] = new_value >> 16;
365 (*cfi_info)[offset_to_update+3] = new_value >> 24;
366 std::string name = PrettyMethod(it.GetMemberIndex(), *dex_file_, false);
367 writer_->method_info_.push_back(DebugInfo(name, new_value, new_value + code_size));
368 }
369 }
370
371 // Deduplicate code arrays.
372 auto code_iter = dedupe_map_.find(quick_code);
373 if (code_iter != dedupe_map_.end()) {
374 quick_code_offset = code_iter->second;
375 } else {
376 dedupe_map_.Put(quick_code, quick_code_offset);
377 OatMethodHeader method_header(code_size);
378 offset_ += sizeof(method_header); // Method header is prepended before code.
379 writer_->oat_header_->UpdateChecksum(&method_header, sizeof(method_header));
380 offset_ += code_size;
381 writer_->oat_header_->UpdateChecksum(&(*quick_code)[0], code_size);
382 }
383 }
384 frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
385 core_spill_mask = compiled_method->GetCoreSpillMask();
386 fp_spill_mask = compiled_method->GetFpSpillMask();
387
388 if (kIsDebugBuild) {
389 // We expect GC maps except when the class hasn't been verified or the method is native.
390 const CompilerDriver* compiler_driver = writer_->compiler_driver_;
391 ClassReference class_ref(dex_file_, class_def_index_);
392 CompiledClass* compiled_class = compiler_driver->GetCompiledClass(class_ref);
393 mirror::Class::Status status;
394 if (compiled_class != NULL) {
395 status = compiled_class->GetStatus();
396 } else if (compiler_driver->GetVerificationResults()->IsClassRejected(class_ref)) {
397 status = mirror::Class::kStatusError;
398 } else {
399 status = mirror::Class::kStatusNotReady;
400 }
401 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
402 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
403 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
404 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
405 << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
406 << (status < mirror::Class::kStatusVerified) << " " << status << " "
407 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
408 }
409
410 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
411 OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_];
412 offsets->code_offset_ = quick_code_offset;
413 offsets->frame_size_in_bytes_ = frame_size_in_bytes;
414 offsets->core_spill_mask_ = core_spill_mask;
415 offsets->fp_spill_mask_ = fp_spill_mask;
416 ++method_offsets_index_;
417 }
418
419 return true;
420 }
421
422 private:
423 // Deduplication is already done on a pointer basis by the compiler driver,
424 // so we can simply compare the pointers to find out if things are duplicated.
425 SafeMap<const std::vector<uint8_t>*, uint32_t> dedupe_map_;
426};
427
428template <typename DataAccess>
429class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor {
430 public:
431 InitMapMethodVisitor(OatWriter* writer, size_t offset)
432 : OatDexMethodVisitor(writer, offset) {
433 }
434
435 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
436 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
437 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
438 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
439
440 if (compiled_method != nullptr) {
441 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
442 DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u);
443
444 const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method);
445 uint32_t map_size = map->size() * sizeof((*map)[0]);
446 if (map_size != 0u) {
447 auto it = dedupe_map_.find(map);
448 if (it != dedupe_map_.end()) {
449 DataAccess::SetOffset(oat_class, method_offsets_index_, it->second);
450 } else {
451 DataAccess::SetOffset(oat_class, method_offsets_index_, offset_);
452 dedupe_map_.Put(map, offset_);
453 offset_ += map_size;
454 writer_->oat_header_->UpdateChecksum(&(*map)[0], map_size);
455 }
456 }
457 ++method_offsets_index_;
458 }
459
460 return true;
461 }
462
463 private:
464 // Deduplication is already done on a pointer basis by the compiler driver,
465 // so we can simply compare the pointers to find out if things are duplicated.
466 SafeMap<const std::vector<uint8_t>*, uint32_t> dedupe_map_;
467};
468
469class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor {
470 public:
471 InitImageMethodVisitor(OatWriter* writer, size_t offset)
472 : OatDexMethodVisitor(writer, offset) {
473 }
474
475 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
476 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
477 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
478 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
479
480 OatMethodOffsets offsets(0u, kStackAlignment, 0u, 0u, 0u, 0u, 0u);
481 if (compiled_method != nullptr) {
482 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
483 offsets = oat_class->method_offsets_[method_offsets_index_];
484 ++method_offsets_index_;
485 }
486
487 // Derive frame size and spill masks for native methods without code:
488 // These are generic JNI methods...
489 uint32_t method_idx = it.GetMemberIndex();
490 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
491 if (is_native && compiled_method == nullptr) {
492 // Compute Sirt size as putting _every_ reference into it, even null ones.
493 uint32_t s_len;
494 const char* shorty = dex_file_->GetMethodShorty(dex_file_->GetMethodId(method_idx),
495 &s_len);
496 DCHECK(shorty != nullptr);
497 uint32_t refs = 1; // Native method always has "this" or class.
498 for (uint32_t i = 1; i < s_len; ++i) {
499 if (shorty[i] == 'L') {
500 refs++;
501 }
502 }
503 size_t pointer_size = GetInstructionSetPointerSize(
504 writer_->compiler_driver_->GetInstructionSet());
505 size_t sirt_size = StackIndirectReferenceTable::GetAlignedSirtSizeTarget(pointer_size, refs);
506
507 // Get the generic spill masks and base frame size.
508 mirror::ArtMethod* callee_save_method =
509 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs);
510
511 offsets.frame_size_in_bytes_ = callee_save_method->GetFrameSizeInBytes() + sirt_size;
512 offsets.core_spill_mask_ = callee_save_method->GetCoreSpillMask();
513 offsets.fp_spill_mask_ = callee_save_method->GetFpSpillMask();
514 DCHECK_EQ(offsets.mapping_table_offset_, 0u);
515 DCHECK_EQ(offsets.vmap_table_offset_, 0u);
516 DCHECK_EQ(offsets.gc_map_offset_, 0u);
517 }
518
519 ClassLinker* linker = Runtime::Current()->GetClassLinker();
520 InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_));
521 // Unchecked as we hold mutator_lock_ on entry.
522 ScopedObjectAccessUnchecked soa(Thread::Current());
523 SirtRef<mirror::DexCache> dex_cache(soa.Self(), linker->FindDexCache(*dex_file_));
524 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), nullptr);
525 mirror::ArtMethod* method = linker->ResolveMethod(*dex_file_, method_idx, dex_cache,
526 class_loader, nullptr, invoke_type);
527 CHECK(method != NULL);
528 method->SetFrameSizeInBytes(offsets.frame_size_in_bytes_);
529 method->SetCoreSpillMask(offsets.core_spill_mask_);
530 method->SetFpSpillMask(offsets.fp_spill_mask_);
531 method->SetOatMappingTableOffset(offsets.mapping_table_offset_);
532 // Portable code offsets are set by ElfWriterMclinker::FixupCompiledCodeOffset after linking.
533 method->SetQuickOatCodeOffset(offsets.code_offset_);
534 method->SetOatVmapTableOffset(offsets.vmap_table_offset_);
535 method->SetOatNativeGcMapOffset(offsets.gc_map_offset_);
536
537 return true;
538 }
539};
540
541class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor {
542 public:
543 WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
544 size_t relative_offset)
545 : OatDexMethodVisitor(writer, relative_offset),
546 out_(out),
547 file_offset_(file_offset) {
548 }
549
550 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
551 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
552 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
553
554 if (compiled_method != NULL) { // ie. not an abstract method
555 size_t file_offset = file_offset_;
556 OutputStream* out = out_;
557
558 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode();
559 if (quick_code != nullptr) {
560 CHECK(compiled_method->GetPortableCode() == nullptr);
561 uint32_t aligned_offset = compiled_method->AlignCode(offset_);
562 uint32_t aligned_code_delta = aligned_offset - offset_;
563 if (aligned_code_delta != 0) {
564 static const uint8_t kPadding[] = {
565 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
566 };
567 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
568 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
569 ReportWriteFailure("code alignment padding", it);
570 return false;
571 }
572 writer_->size_code_alignment_ += aligned_code_delta;
573 offset_ += aligned_code_delta;
574 DCHECK_OFFSET_();
575 }
576 DCHECK_ALIGNED_PARAM(offset_,
577 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
578 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
579 CHECK_NE(code_size, 0U);
580
581 // Deduplicate code arrays.
582 const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_];
583 DCHECK(method_offsets.code_offset_ < offset_ || method_offsets.code_offset_ ==
584 offset_ + sizeof(OatMethodHeader) + compiled_method->CodeDelta())
585 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
586 if (method_offsets.code_offset_ >= offset_) {
587 OatMethodHeader method_header(code_size);
588 if (!out->WriteFully(&method_header, sizeof(method_header))) {
589 ReportWriteFailure("method header", it);
590 return false;
591 }
592 writer_->size_method_header_ += sizeof(method_header);
593 offset_ += sizeof(method_header);
594 DCHECK_OFFSET_();
595 if (!out->WriteFully(&(*quick_code)[0], code_size)) {
596 ReportWriteFailure("method code", it);
597 return false;
598 }
599 writer_->size_code_ += code_size;
600 offset_ += code_size;
601 }
602 DCHECK_OFFSET_();
603 }
604 ++method_offsets_index_;
605 }
606
607 return true;
608 }
609
610 private:
611 OutputStream* const out_;
612 size_t const file_offset_;
613
614 void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) {
615 PLOG(ERROR) << "Failed to write " << what << " for "
616 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
617 }
618};
619
620template <typename DataAccess>
621class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor {
622 public:
623 WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
624 size_t relative_offset)
625 : OatDexMethodVisitor(writer, relative_offset),
626 out_(out),
627 file_offset_(file_offset) {
628 }
629
630 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
631 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
632 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
633
634 if (compiled_method != NULL) { // ie. not an abstract method
635 size_t file_offset = file_offset_;
636 OutputStream* out = out_;
637
638 uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_);
639 ++method_offsets_index_;
640
641 // Write deduplicated map.
642 const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method);
643 size_t map_size = map->size() * sizeof((*map)[0]);
644 DCHECK((map_size == 0u && map_offset == 0u) ||
645 (map_size != 0u && map_offset != 0u && map_offset <= offset_))
646 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
647 if (map_size != 0u && map_offset == offset_) {
648 if (UNLIKELY(!out->WriteFully(&(*map)[0], map_size))) {
649 ReportWriteFailure(it);
650 return false;
651 }
652 offset_ += map_size;
653 }
654 DCHECK_OFFSET_();
655 }
656
657 return true;
658 }
659
660 private:
661 OutputStream* const out_;
662 size_t const file_offset_;
663
664 void ReportWriteFailure(const ClassDataItemIterator& it) {
665 PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for "
666 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
667 }
668};
669
670// Visit all methods from all classes in all dex files with the specified visitor.
671bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) {
672 for (const DexFile* dex_file : *dex_files_) {
673 const size_t class_def_count = dex_file->NumClassDefs();
674 for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) {
675 if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) {
676 return false;
677 }
678 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
679 const byte* class_data = dex_file->GetClassData(class_def);
680 if (class_data != NULL) { // ie not an empty class, such as a marker interface
681 ClassDataItemIterator it(*dex_file, class_data);
682 while (it.HasNextStaticField()) {
683 it.Next();
684 }
685 while (it.HasNextInstanceField()) {
686 it.Next();
687 }
688 size_t class_def_method_index = 0u;
689 while (it.HasNextDirectMethod()) {
690 if (!visitor->VisitMethod(class_def_method_index, it)) {
691 return false;
692 }
693 ++class_def_method_index;
694 it.Next();
695 }
696 while (it.HasNextVirtualMethod()) {
697 if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) {
698 return false;
699 }
700 ++class_def_method_index;
701 it.Next();
702 }
703 }
704 if (UNLIKELY(!visitor->EndClass())) {
705 return false;
706 }
707 }
708 }
709 return true;
710}
711
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700712size_t OatWriter::InitOatHeader() {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700713 // create the OatHeader
Ian Rogers1212a022013-03-04 10:48:41 -0800714 oat_header_ = new OatHeader(compiler_driver_->GetInstructionSet(),
Dave Allison70202782013-10-22 17:52:19 -0700715 compiler_driver_->GetInstructionSetFeatures(),
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700716 dex_files_,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700717 image_file_location_oat_checksum_,
718 image_file_location_oat_begin_,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700719 image_file_location_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700720 size_t offset = sizeof(*oat_header_);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700721 offset += image_file_location_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700722 return offset;
723}
724
725size_t OatWriter::InitOatDexFiles(size_t offset) {
726 // create the OatDexFiles
727 for (size_t i = 0; i != dex_files_->size(); ++i) {
728 const DexFile* dex_file = (*dex_files_)[i];
729 CHECK(dex_file != NULL);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800730 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700731 oat_dex_files_.push_back(oat_dex_file);
732 offset += oat_dex_file->SizeOf();
733 }
734 return offset;
735}
736
Brian Carlstrom89521892011-12-07 22:05:07 -0800737size_t OatWriter::InitDexFiles(size_t offset) {
738 // calculate the offsets within OatDexFiles to the DexFiles
739 for (size_t i = 0; i != dex_files_->size(); ++i) {
740 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700741 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800742 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700743 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800744
745 // set offset in OatDexFile to DexFile
746 oat_dex_files_[i]->dex_file_offset_ = offset;
747
748 const DexFile* dex_file = (*dex_files_)[i];
749 offset += dex_file->GetHeader().file_size_;
750 }
751 return offset;
752}
753
Brian Carlstrom389efb02012-01-11 12:06:26 -0800754size_t OatWriter::InitOatClasses(size_t offset) {
Brian Carlstrom389efb02012-01-11 12:06:26 -0800755 // calculate the offsets within OatDexFiles to OatClasses
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100756 InitOatClassesMethodVisitor visitor(this, offset);
757 bool success = VisitDexMethods(&visitor);
758 CHECK(success);
759 offset = visitor.GetOffset();
Brian Carlstromba150c32013-08-27 17:31:03 -0700760
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100761 // Update oat_dex_files_.
762 auto oat_class_it = oat_classes_.begin();
763 for (OatDexFile* oat_dex_file : oat_dex_files_) {
764 for (uint32_t& offset : oat_dex_file->methods_offsets_) {
765 DCHECK(oat_class_it != oat_classes_.end());
766 offset = (*oat_class_it)->offset_;
767 ++oat_class_it;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700768 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100769 oat_dex_file->UpdateChecksum(oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700770 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100771 CHECK(oat_class_it == oat_classes_.end());
772
773 return offset;
774}
775
776size_t OatWriter::InitOatMaps(size_t offset) {
777 #define VISIT(VisitorType) \
778 do { \
779 VisitorType visitor(this, offset); \
780 bool success = VisitDexMethods(&visitor); \
781 DCHECK(success); \
782 offset = visitor.GetOffset(); \
783 } while (false)
784
785 VISIT(InitMapMethodVisitor<GcMapDataAccess>);
786 VISIT(InitMapMethodVisitor<MappingTableDataAccess>);
787 VISIT(InitMapMethodVisitor<VmapTableDataAccess>);
788
789 #undef VISIT
790
Brian Carlstrome24fa612011-09-29 00:53:55 -0700791 return offset;
792}
793
794size_t OatWriter::InitOatCode(size_t offset) {
795 // calculate the offsets within OatHeader to executable code
796 size_t old_offset = offset;
797 // required to be on a new page boundary
798 offset = RoundUp(offset, kPageSize);
799 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700800 size_executable_offset_alignment_ = offset - old_offset;
801 if (compiler_driver_->IsImage()) {
802 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700803
Ian Rogers848871b2013-08-05 10:56:33 -0700804 #define DO_TRAMPOLINE(field, fn_name) \
805 offset = CompiledCode::AlignCode(offset, instruction_set); \
806 oat_header_->Set ## fn_name ## Offset(offset); \
807 field.reset(compiler_driver_->Create ## fn_name()); \
808 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700809
Ian Rogers848871b2013-08-05 10:56:33 -0700810 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge);
811 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge);
812 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Jeff Hao88474b42013-10-23 16:24:40 -0700813 DO_TRAMPOLINE(portable_imt_conflict_trampoline_, PortableImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700814 DO_TRAMPOLINE(portable_resolution_trampoline_, PortableResolutionTrampoline);
815 DO_TRAMPOLINE(portable_to_interpreter_bridge_, PortableToInterpreterBridge);
Andreas Gampe2da88232014-02-27 12:26:20 -0800816 DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline);
Jeff Hao88474b42013-10-23 16:24:40 -0700817 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700818 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
819 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700820
Ian Rogers848871b2013-08-05 10:56:33 -0700821 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700822 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700823 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
824 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
825 oat_header_->SetJniDlsymLookupOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700826 oat_header_->SetPortableImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700827 oat_header_->SetPortableResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700828 oat_header_->SetPortableToInterpreterBridgeOffset(0);
Andreas Gampe2da88232014-02-27 12:26:20 -0800829 oat_header_->SetQuickGenericJniTrampolineOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700830 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700831 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700832 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700833 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700834 return offset;
835}
836
837size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100838 #define VISIT(VisitorType) \
839 do { \
840 VisitorType visitor(this, offset); \
841 bool success = VisitDexMethods(&visitor); \
842 DCHECK(success); \
843 offset = visitor.GetOffset(); \
844 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700845
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100846 VISIT(InitCodeMethodVisitor);
Ian Rogers1212a022013-03-04 10:48:41 -0800847 if (compiler_driver_->IsImage()) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100848 VISIT(InitImageMethodVisitor);
Ian Rogers0571d352011-11-03 19:51:38 -0700849 }
Logan Chien8b977d32012-02-21 19:14:55 +0800850
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100851 #undef VISIT
852
Brian Carlstrome24fa612011-09-29 00:53:55 -0700853 return offset;
854}
855
Ian Rogers3d504072014-03-01 09:16:49 -0800856bool OatWriter::Write(OutputStream* out) {
857 const size_t file_offset = out->Seek(0, kSeekCurrent);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700858
Ian Rogers3d504072014-03-01 09:16:49 -0800859 if (!out->WriteFully(oat_header_, sizeof(*oat_header_))) {
860 PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700861 return false;
862 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700863 size_oat_header_ += sizeof(*oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700864
Ian Rogers3d504072014-03-01 09:16:49 -0800865 if (!out->WriteFully(image_file_location_.data(), image_file_location_.size())) {
866 PLOG(ERROR) << "Failed to write oat header image file location to " << out->GetLocation();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700867 return false;
868 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700869 size_oat_header_image_file_location_ += image_file_location_.size();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700870
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700871 if (!WriteTables(out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800872 LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700873 return false;
874 }
875
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100876 size_t relative_offset = out->Seek(0, kSeekCurrent) - file_offset;
877 relative_offset = WriteMaps(out, file_offset, relative_offset);
878 if (relative_offset == 0) {
879 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
880 return false;
881 }
882
883 relative_offset = WriteCode(out, file_offset, relative_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700884 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800885 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700886 return false;
887 }
888
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700889 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
890 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800891 LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700892 return false;
893 }
894
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700895 if (kIsDebugBuild) {
896 uint32_t size_total = 0;
897 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700898 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700899 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700900
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700901 DO_STAT(size_dex_file_alignment_);
902 DO_STAT(size_executable_offset_alignment_);
903 DO_STAT(size_oat_header_);
904 DO_STAT(size_oat_header_image_file_location_);
905 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -0700906 DO_STAT(size_interpreter_to_interpreter_bridge_);
907 DO_STAT(size_interpreter_to_compiled_code_bridge_);
908 DO_STAT(size_jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -0700909 DO_STAT(size_portable_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700910 DO_STAT(size_portable_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700911 DO_STAT(size_portable_to_interpreter_bridge_);
Andreas Gampe2da88232014-02-27 12:26:20 -0800912 DO_STAT(size_quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -0700913 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700914 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700915 DO_STAT(size_quick_to_interpreter_bridge_);
916 DO_STAT(size_trampoline_alignment_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100917 DO_STAT(size_method_header_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700918 DO_STAT(size_code_);
919 DO_STAT(size_code_alignment_);
920 DO_STAT(size_mapping_table_);
921 DO_STAT(size_vmap_table_);
922 DO_STAT(size_gc_map_);
923 DO_STAT(size_oat_dex_file_location_size_);
924 DO_STAT(size_oat_dex_file_location_data_);
925 DO_STAT(size_oat_dex_file_location_checksum_);
926 DO_STAT(size_oat_dex_file_offset_);
927 DO_STAT(size_oat_dex_file_methods_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700928 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700929 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700930 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700931 DO_STAT(size_oat_class_method_offsets_);
932 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700933
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700934 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Ian Rogers3d504072014-03-01 09:16:49 -0800935 CHECK_EQ(file_offset + size_total, static_cast<uint32_t>(out->Seek(0, kSeekCurrent)));
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700936 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700937 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700938
Ian Rogers3d504072014-03-01 09:16:49 -0800939 CHECK_EQ(file_offset + size_, static_cast<uint32_t>(out->Seek(0, kSeekCurrent)));
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700940 CHECK_EQ(size_, relative_offset);
941
Brian Carlstrome24fa612011-09-29 00:53:55 -0700942 return true;
943}
944
Ian Rogers3d504072014-03-01 09:16:49 -0800945bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700946 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700947 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800948 PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700949 return false;
950 }
951 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800952 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700953 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Ian Rogers3d504072014-03-01 09:16:49 -0800954 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -0800955 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
956 const DexFile* dex_file = (*dex_files_)[i];
957 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
958 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
959 return false;
960 }
961 const DexFile* dex_file = (*dex_files_)[i];
Ian Rogers3d504072014-03-01 09:16:49 -0800962 if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700963 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
Ian Rogers3d504072014-03-01 09:16:49 -0800964 << " to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800965 return false;
966 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700967 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800968 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800969 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700970 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800971 PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700972 return false;
973 }
974 }
975 return true;
976}
977
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100978size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) {
979 #define VISIT(VisitorType) \
980 do { \
981 VisitorType visitor(this, out, file_offset, relative_offset); \
982 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
983 return 0; \
984 } \
985 relative_offset = visitor.GetOffset(); \
986 } while (false)
987
988 size_t gc_maps_offset = relative_offset;
989 VISIT(WriteMapMethodVisitor<GcMapDataAccess>);
990 size_gc_map_ = relative_offset - gc_maps_offset;
991
992 size_t mapping_tables_offset = relative_offset;
993 VISIT(WriteMapMethodVisitor<MappingTableDataAccess>);
994 size_mapping_table_ = relative_offset - mapping_tables_offset;
995
996 size_t vmap_tables_offset = relative_offset;
997 VISIT(WriteMapMethodVisitor<VmapTableDataAccess>);
998 size_vmap_table_ = relative_offset - vmap_tables_offset;
999
1000 #undef VISIT
1001
1002 return relative_offset;
1003}
1004
1005size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) {
Ian Rogers3d504072014-03-01 09:16:49 -08001006 off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001007 relative_offset += size_executable_offset_alignment_;
1008 DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset());
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001009 size_t expected_file_offset = file_offset + relative_offset;
1010 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001011 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Ian Rogers3d504072014-03-01 09:16:49 -08001012 << " Expected: " << expected_file_offset << " File: " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001013 return 0;
1014 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001015 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001016 if (compiler_driver_->IsImage()) {
1017 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001018
Ian Rogers848871b2013-08-05 10:56:33 -07001019 #define DO_TRAMPOLINE(field) \
1020 do { \
1021 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
1022 uint32_t alignment_padding = aligned_offset - relative_offset; \
Ian Rogers3d504072014-03-01 09:16:49 -08001023 out->Seek(alignment_padding, kSeekCurrent); \
Ian Rogers848871b2013-08-05 10:56:33 -07001024 size_trampoline_alignment_ += alignment_padding; \
Ian Rogers3d504072014-03-01 09:16:49 -08001025 if (!out->WriteFully(&(*field)[0], field->size())) { \
1026 PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \
Ian Rogers848871b2013-08-05 10:56:33 -07001027 return false; \
1028 } \
1029 size_ ## field += field->size(); \
1030 relative_offset += alignment_padding + field->size(); \
1031 DCHECK_OFFSET(); \
1032 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001033
Ian Rogers848871b2013-08-05 10:56:33 -07001034 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_);
1035 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_);
1036 DO_TRAMPOLINE(jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -07001037 DO_TRAMPOLINE(portable_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001038 DO_TRAMPOLINE(portable_resolution_trampoline_);
1039 DO_TRAMPOLINE(portable_to_interpreter_bridge_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001040 DO_TRAMPOLINE(quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001041 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001042 DO_TRAMPOLINE(quick_resolution_trampoline_);
1043 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
1044 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001045 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001046 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001047}
1048
Ian Rogers3d504072014-03-01 09:16:49 -08001049size_t OatWriter::WriteCodeDexFiles(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001050 const size_t file_offset,
1051 size_t relative_offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001052 #define VISIT(VisitorType) \
1053 do { \
1054 VisitorType visitor(this, out, file_offset, relative_offset); \
1055 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1056 return 0; \
1057 } \
1058 relative_offset = visitor.GetOffset(); \
1059 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001060
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001061 VISIT(WriteCodeMethodVisitor);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001062
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001063 #undef VISIT
Brian Carlstrom265091e2013-01-30 14:08:26 -08001064
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001065 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001066}
1067
Brian Carlstrom265091e2013-01-30 14:08:26 -08001068OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
1069 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -08001070 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001071 dex_file_location_size_ = location.size();
1072 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001073 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -08001074 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001075 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001076}
1077
1078size_t OatWriter::OatDexFile::SizeOf() const {
1079 return sizeof(dex_file_location_size_)
1080 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001081 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -08001082 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001083 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001084}
1085
Ian Rogers3d504072014-03-01 09:16:49 -08001086void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const {
1087 oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
1088 oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
1089 oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
1090 oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
1091 oat_header->UpdateChecksum(&methods_offsets_[0],
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001092 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001093}
1094
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001095bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001096 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001097 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001098 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001099 if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
1100 PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001101 return false;
1102 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001103 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001104 if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
1105 PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001106 return false;
1107 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001108 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Ian Rogers3d504072014-03-01 09:16:49 -08001109 if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
1110 PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001111 return false;
1112 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001113 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Ian Rogers3d504072014-03-01 09:16:49 -08001114 if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
1115 PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001116 return false;
1117 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001118 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Ian Rogers3d504072014-03-01 09:16:49 -08001119 if (!out->WriteFully(&methods_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001120 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001121 PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001122 return false;
1123 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001124 oat_writer->size_oat_dex_file_methods_offsets_ +=
1125 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001126 return true;
1127}
1128
Brian Carlstromba150c32013-08-27 17:31:03 -07001129OatWriter::OatClass::OatClass(size_t offset,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001130 const std::vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -07001131 uint32_t num_non_null_compiled_methods,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001132 mirror::Class::Status status)
1133 : compiled_methods_(compiled_methods) {
1134 uint32_t num_methods = compiled_methods.size();
Brian Carlstromba150c32013-08-27 17:31:03 -07001135 CHECK_LE(num_non_null_compiled_methods, num_methods);
1136
Brian Carlstrom265091e2013-01-30 14:08:26 -08001137 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -07001138 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
1139
1140 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
1141 // apply when there are 0 methods, we just arbitrarily say that 0
1142 // methods means kOatClassNoneCompiled and that we won't use
1143 // kOatClassAllCompiled unless there is at least one compiled
1144 // method. This means in an interpretter only system, we can assert
1145 // that all classes are kOatClassNoneCompiled.
1146 if (num_non_null_compiled_methods == 0) {
1147 type_ = kOatClassNoneCompiled;
1148 } else if (num_non_null_compiled_methods == num_methods) {
1149 type_ = kOatClassAllCompiled;
1150 } else {
1151 type_ = kOatClassSomeCompiled;
1152 }
1153
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001154 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -07001155 method_offsets_.resize(num_non_null_compiled_methods);
1156
1157 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
1158 if (type_ == kOatClassSomeCompiled) {
1159 method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator());
1160 method_bitmap_size_ = method_bitmap_->GetSizeOf();
1161 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
1162 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
1163 } else {
1164 method_bitmap_ = NULL;
1165 method_bitmap_size_ = 0;
1166 }
1167
1168 for (size_t i = 0; i < num_methods; i++) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001169 CompiledMethod* compiled_method = compiled_methods_[i];
Brian Carlstromba150c32013-08-27 17:31:03 -07001170 if (compiled_method == NULL) {
1171 oat_method_offsets_offsets_from_oat_class_[i] = 0;
1172 } else {
1173 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
1174 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
1175 if (type_ == kOatClassSomeCompiled) {
1176 method_bitmap_->SetBit(i);
1177 }
1178 }
1179 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001180}
1181
Brian Carlstromba150c32013-08-27 17:31:03 -07001182OatWriter::OatClass::~OatClass() {
Mathieu Chartier661974a2014-01-09 11:23:53 -08001183 delete method_bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -07001184}
1185
Brian Carlstrom265091e2013-01-30 14:08:26 -08001186size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
1187 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001188 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
1189 if (method_offset == 0) {
1190 return 0;
1191 }
1192 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001193}
1194
1195size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
1196 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001197 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08001198}
1199
1200size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001201 return sizeof(status_)
1202 + sizeof(type_)
1203 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
1204 + method_bitmap_size_
1205 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001206}
1207
Ian Rogers3d504072014-03-01 09:16:49 -08001208void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const {
1209 oat_header->UpdateChecksum(&status_, sizeof(status_));
1210 oat_header->UpdateChecksum(&type_, sizeof(type_));
Brian Carlstromba150c32013-08-27 17:31:03 -07001211 if (method_bitmap_size_ != 0) {
1212 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001213 oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_));
1214 oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001215 }
Ian Rogers3d504072014-03-01 09:16:49 -08001216 oat_header->UpdateChecksum(&method_offsets_[0],
1217 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001218}
1219
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001220bool OatWriter::OatClass::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001221 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001222 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001223 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001224 if (!out->WriteFully(&status_, sizeof(status_))) {
1225 PLOG(ERROR) << "Failed to write class status to " << out->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001226 return false;
1227 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001228 oat_writer->size_oat_class_status_ += sizeof(status_);
Ian Rogers3d504072014-03-01 09:16:49 -08001229 if (!out->WriteFully(&type_, sizeof(type_))) {
1230 PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001231 return false;
1232 }
1233 oat_writer->size_oat_class_type_ += sizeof(type_);
1234 if (method_bitmap_size_ != 0) {
1235 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001236 if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
1237 PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001238 return false;
1239 }
1240 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001241 if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
1242 PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001243 return false;
1244 }
1245 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1246 }
Ian Rogers3d504072014-03-01 09:16:49 -08001247 if (!out->WriteFully(&method_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001248 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001249 PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001250 return false;
1251 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001252 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001253 return true;
1254}
1255
Brian Carlstrome24fa612011-09-29 00:53:55 -07001256} // namespace art