blob: 2b2f0e8c26dd42e9a4911750c23f9212b66a5bff [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
Vladimir Markoc74658b2015-03-31 10:26:41 +010021#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Ian Rogerse77493c2014-08-20 15:08:45 -070023#include "base/allocator.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070024#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080025#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080026#include "base/unix_file/fd_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070027#include "class_linker.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070028#include "compiled_class.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000029#include "compiled_method.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "dex_file-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000031#include "dex/verification_results.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000032#include "driver/compiler_driver.h"
33#include "driver/compiler_options.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000034#include "dwarf/method_debug_info.h"
Vladimir Marko09d09432015-09-08 13:47:48 +010035#include "gc/space/image_space.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070036#include "gc/space/space.h"
Artem Udovichenkod9786b02015-10-14 16:36:55 +030037#include "handle_scope-inl.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010038#include "image_writer.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000039#include "linker/output_stream.h"
Vladimir Markob163bb72015-03-31 21:49:49 +010040#include "linker/relative_patcher.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "mirror/array.h"
42#include "mirror/class_loader.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010043#include "mirror/dex_cache-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070044#include "mirror/object-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010045#include "oat_quick_method_header.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070046#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070047#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048#include "scoped_thread_state_change.h"
Artem Udovichenkod9786b02015-10-14 16:36:55 +030049#include "type_lookup_table.h"
Vladimir Marko09d09432015-09-08 13:47:48 +010050#include "utils/dex_cache_arrays_layout-inl.h"
jeffhaoec014232012-09-05 10:42:25 -070051#include "verifier/method_verifier.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070052
53namespace art {
54
Vladimir Marko49b0f452015-12-10 13:49:19 +000055class OatWriter::OatClass {
56 public:
57 OatClass(size_t offset,
58 const dchecked_vector<CompiledMethod*>& compiled_methods,
59 uint32_t num_non_null_compiled_methods,
60 mirror::Class::Status status);
61 OatClass(OatClass&& src) = default;
62 size_t GetOatMethodOffsetsOffsetFromOatHeader(size_t class_def_method_index_) const;
63 size_t GetOatMethodOffsetsOffsetFromOatClass(size_t class_def_method_index_) const;
64 size_t SizeOf() const;
65 bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
66
67 CompiledMethod* GetCompiledMethod(size_t class_def_method_index) const {
68 return compiled_methods_[class_def_method_index];
69 }
70
71 // Offset of start of OatClass from beginning of OatHeader. It is
72 // used to validate file position when writing.
73 size_t offset_;
74
75 // CompiledMethods for each class_def_method_index, or null if no method is available.
76 dchecked_vector<CompiledMethod*> compiled_methods_;
77
78 // Offset from OatClass::offset_ to the OatMethodOffsets for the
79 // class_def_method_index. If 0, it means the corresponding
80 // CompiledMethod entry in OatClass::compiled_methods_ should be
81 // null and that the OatClass::type_ should be kOatClassBitmap.
82 dchecked_vector<uint32_t> oat_method_offsets_offsets_from_oat_class_;
83
84 // Data to write.
85
86 static_assert(mirror::Class::Status::kStatusMax < (1 << 16), "class status won't fit in 16bits");
87 int16_t status_;
88
89 static_assert(OatClassType::kOatClassMax < (1 << 16), "oat_class type won't fit in 16bits");
90 uint16_t type_;
91
92 uint32_t method_bitmap_size_;
93
94 // bit vector indexed by ClassDef method index. When
95 // OatClassType::type_ is kOatClassBitmap, a set bit indicates the
96 // method has an OatMethodOffsets in methods_offsets_, otherwise
97 // the entry was ommited to save space. If OatClassType::type_ is
98 // not is kOatClassBitmap, the bitmap will be null.
99 std::unique_ptr<BitVector> method_bitmap_;
100
101 // OatMethodOffsets and OatMethodHeaders for each CompiledMethod
102 // present in the OatClass. Note that some may be missing if
103 // OatClass::compiled_methods_ contains null values (and
104 // oat_method_offsets_offsets_from_oat_class_ should contain 0
105 // values in this case).
106 dchecked_vector<OatMethodOffsets> method_offsets_;
107 dchecked_vector<OatQuickMethodHeader> method_headers_;
108
109 private:
110 size_t GetMethodOffsetsRawSize() const {
111 return method_offsets_.size() * sizeof(method_offsets_[0]);
112 }
113
114 DISALLOW_COPY_AND_ASSIGN(OatClass);
115};
116
117class OatWriter::OatDexFile {
118 public:
119 OatDexFile(size_t offset, const DexFile& dex_file);
120 OatDexFile(OatDexFile&& src) = default;
121
122 size_t SizeOf() const;
123 bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
124
125 // Offset of start of OatDexFile from beginning of OatHeader. It is
126 // used to validate file position when writing.
127 size_t offset_;
128
129 // Data to write.
130 uint32_t dex_file_location_size_;
131 const uint8_t* dex_file_location_data_;
132 uint32_t dex_file_location_checksum_;
133 uint32_t dex_file_offset_;
134 uint32_t lookup_table_offset_;
135 TypeLookupTable* lookup_table_; // Owned by the dex file.
136 dchecked_vector<uint32_t> class_offsets_;
137
138 private:
139 size_t GetClassOffsetsRawSize() const {
140 return class_offsets_.size() * sizeof(class_offsets_[0]);
141 }
142
143 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
144};
145
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100146#define DCHECK_OFFSET() \
147 DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out->Seek(0, kSeekCurrent)) \
148 << "file_offset=" << file_offset << " relative_offset=" << relative_offset
149
150#define DCHECK_OFFSET_() \
151 DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out->Seek(0, kSeekCurrent)) \
152 << "file_offset=" << file_offset << " offset_=" << offset_
153
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700154OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -0700155 uint32_t image_file_location_oat_checksum,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800156 uintptr_t image_file_location_oat_begin,
Alex Lighta59dd802014-07-02 16:28:08 -0700157 int32_t image_patch_delta,
Ian Rogersca368cb2013-11-15 15:52:08 -0800158 const CompilerDriver* compiler,
Vladimir Markof4da6752014-08-01 19:04:18 +0100159 ImageWriter* image_writer,
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800160 bool compiling_boot_image,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700161 TimingLogger* timings,
162 SafeMap<std::string, std::string>* key_value_store)
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700163 : compiler_driver_(compiler),
Vladimir Markof4da6752014-08-01 19:04:18 +0100164 image_writer_(image_writer),
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800165 compiling_boot_image_(compiling_boot_image),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700166 dex_files_(&dex_files),
Vladimir Markof4da6752014-08-01 19:04:18 +0100167 size_(0u),
Vladimir Marko5c42c292015-02-25 12:02:49 +0000168 bss_size_(0u),
Vladimir Markof4da6752014-08-01 19:04:18 +0100169 oat_data_offset_(0u),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700170 image_file_location_oat_checksum_(image_file_location_oat_checksum),
171 image_file_location_oat_begin_(image_file_location_oat_begin),
Alex Lighta59dd802014-07-02 16:28:08 -0700172 image_patch_delta_(image_patch_delta),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700173 key_value_store_(key_value_store),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700174 oat_header_(nullptr),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700175 size_dex_file_alignment_(0),
176 size_executable_offset_alignment_(0),
177 size_oat_header_(0),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700178 size_oat_header_key_value_store_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700179 size_dex_file_(0),
Ian Rogers848871b2013-08-05 10:56:33 -0700180 size_interpreter_to_interpreter_bridge_(0),
181 size_interpreter_to_compiled_code_bridge_(0),
182 size_jni_dlsym_lookup_(0),
Andreas Gampe2da88232014-02-27 12:26:20 -0800183 size_quick_generic_jni_trampoline_(0),
Jeff Hao88474b42013-10-23 16:24:40 -0700184 size_quick_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700185 size_quick_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -0700186 size_quick_to_interpreter_bridge_(0),
187 size_trampoline_alignment_(0),
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100188 size_method_header_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700189 size_code_(0),
190 size_code_alignment_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +0100191 size_relative_call_thunks_(0),
Vladimir Markoc74658b2015-03-31 10:26:41 +0100192 size_misc_thunks_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700193 size_mapping_table_(0),
194 size_vmap_table_(0),
195 size_gc_map_(0),
196 size_oat_dex_file_location_size_(0),
197 size_oat_dex_file_location_data_(0),
198 size_oat_dex_file_location_checksum_(0),
199 size_oat_dex_file_offset_(0),
Vladimir Marko49b0f452015-12-10 13:49:19 +0000200 size_oat_dex_file_lookup_table_offset_(0),
201 size_oat_dex_file_class_offsets_(0),
202 size_oat_lookup_table_alignment_(0),
203 size_oat_lookup_table_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -0700204 size_oat_class_type_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700205 size_oat_class_status_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -0700206 size_oat_class_method_bitmaps_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +0100207 size_oat_class_method_offsets_(0),
208 method_offset_map_() {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700209 CHECK(key_value_store != nullptr);
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800210 if (compiling_boot_image) {
211 CHECK(image_writer != nullptr);
212 }
Vladimir Markob163bb72015-03-31 21:49:49 +0100213 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
214 const InstructionSetFeatures* features = compiler_driver_->GetInstructionSetFeatures();
215 relative_patcher_ = linker::RelativePatcher::Create(instruction_set, features,
216 &method_offset_map_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100217
Ian Rogersca368cb2013-11-15 15:52:08 -0800218 size_t offset;
219 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700220 TimingLogger::ScopedTiming split("InitOatHeader", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800221 offset = InitOatHeader();
222 }
223 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700224 TimingLogger::ScopedTiming split("InitOatDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800225 offset = InitOatDexFiles(offset);
226 }
227 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700228 TimingLogger::ScopedTiming split("InitDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800229 offset = InitDexFiles(offset);
230 }
231 {
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300232 TimingLogger::ScopedTiming split("InitLookupTables", timings);
233 offset = InitLookupTables(offset);
234 }
235 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700236 TimingLogger::ScopedTiming split("InitOatClasses", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800237 offset = InitOatClasses(offset);
238 }
239 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700240 TimingLogger::ScopedTiming split("InitOatMaps", timings);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100241 offset = InitOatMaps(offset);
242 }
243 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700244 TimingLogger::ScopedTiming split("InitOatCode", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800245 offset = InitOatCode(offset);
246 }
247 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700248 TimingLogger::ScopedTiming split("InitOatCodeDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800249 offset = InitOatCodeDexFiles(offset);
250 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700251 size_ = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700252
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800253 if (!HasBootImage()) {
Vladimir Marko09d09432015-09-08 13:47:48 +0100254 // Allocate space for app dex cache arrays in the .bss section.
255 size_t bss_start = RoundUp(size_, kPageSize);
256 size_t pointer_size = GetInstructionSetPointerSize(instruction_set);
257 bss_size_ = 0u;
258 for (const DexFile* dex_file : dex_files) {
259 dex_cache_arrays_offsets_.Put(dex_file, bss_start + bss_size_);
260 DexCacheArraysLayout layout(pointer_size, dex_file);
261 bss_size_ += layout.Size();
262 }
263 }
264
Brian Carlstrome24fa612011-09-29 00:53:55 -0700265 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800266 if (compiling_boot_image_) {
267 CHECK_EQ(image_writer_ != nullptr,
268 key_value_store_->find(OatHeader::kImageLocationKey) == key_value_store_->end());
269 }
Alex Lighta59dd802014-07-02 16:28:08 -0700270 CHECK_ALIGNED(image_patch_delta_, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700271}
272
Ian Rogers0571d352011-11-03 19:51:38 -0700273OatWriter::~OatWriter() {
Ian Rogers0571d352011-11-03 19:51:38 -0700274}
275
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100276struct OatWriter::GcMapDataAccess {
Vladimir Marko35831e82015-09-11 11:59:18 +0100277 static ArrayRef<const uint8_t> GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100278 return compiled_method->GetGcMap();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100279 }
280
281 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800282 uint32_t offset = oat_class->method_headers_[method_offsets_index].gc_map_offset_;
283 return offset == 0u ? 0u :
284 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100285 }
286
287 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
288 ALWAYS_INLINE {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800289 oat_class->method_headers_[method_offsets_index].gc_map_offset_ =
290 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100291 }
292
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800293 static const char* Name() {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100294 return "GC map";
295 }
296};
297
298struct OatWriter::MappingTableDataAccess {
Vladimir Marko35831e82015-09-11 11:59:18 +0100299 static ArrayRef<const uint8_t> GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000300 return compiled_method->GetMappingTable();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100301 }
302
303 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100304 uint32_t offset = oat_class->method_headers_[method_offsets_index].mapping_table_offset_;
305 return offset == 0u ? 0u :
306 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100307 }
308
309 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
310 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100311 oat_class->method_headers_[method_offsets_index].mapping_table_offset_ =
312 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100313 }
314
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800315 static const char* Name() {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100316 return "mapping table";
317 }
318};
319
320struct OatWriter::VmapTableDataAccess {
Vladimir Marko35831e82015-09-11 11:59:18 +0100321 static ArrayRef<const uint8_t> GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800322 return compiled_method->GetVmapTable();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100323 }
324
325 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100326 uint32_t offset = oat_class->method_headers_[method_offsets_index].vmap_table_offset_;
327 return offset == 0u ? 0u :
328 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100329 }
330
331 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
332 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100333 oat_class->method_headers_[method_offsets_index].vmap_table_offset_ =
334 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100335 }
336
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800337 static const char* Name() {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100338 return "vmap table";
339 }
340};
341
342class OatWriter::DexMethodVisitor {
343 public:
344 DexMethodVisitor(OatWriter* writer, size_t offset)
345 : writer_(writer),
346 offset_(offset),
347 dex_file_(nullptr),
348 class_def_index_(DexFile::kDexNoIndex) {
349 }
350
351 virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) {
352 DCHECK(dex_file_ == nullptr);
353 DCHECK_EQ(class_def_index_, DexFile::kDexNoIndex);
354 dex_file_ = dex_file;
355 class_def_index_ = class_def_index;
356 return true;
357 }
358
359 virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0;
360
361 virtual bool EndClass() {
362 if (kIsDebugBuild) {
363 dex_file_ = nullptr;
364 class_def_index_ = DexFile::kDexNoIndex;
365 }
366 return true;
367 }
368
369 size_t GetOffset() const {
370 return offset_;
371 }
372
373 protected:
374 virtual ~DexMethodVisitor() { }
375
376 OatWriter* const writer_;
377
378 // The offset is usually advanced for each visited method by the derived class.
379 size_t offset_;
380
381 // The dex file and class def index are set in StartClass().
382 const DexFile* dex_file_;
383 size_t class_def_index_;
384};
385
386class OatWriter::OatDexMethodVisitor : public DexMethodVisitor {
387 public:
388 OatDexMethodVisitor(OatWriter* writer, size_t offset)
389 : DexMethodVisitor(writer, offset),
390 oat_class_index_(0u),
391 method_offsets_index_(0u) {
392 }
393
394 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
395 DexMethodVisitor::StartClass(dex_file, class_def_index);
396 DCHECK_LT(oat_class_index_, writer_->oat_classes_.size());
397 method_offsets_index_ = 0u;
398 return true;
399 }
400
401 bool EndClass() {
402 ++oat_class_index_;
403 return DexMethodVisitor::EndClass();
404 }
405
406 protected:
407 size_t oat_class_index_;
408 size_t method_offsets_index_;
409};
410
411class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor {
412 public:
413 InitOatClassesMethodVisitor(OatWriter* writer, size_t offset)
414 : DexMethodVisitor(writer, offset),
415 compiled_methods_(),
416 num_non_null_compiled_methods_(0u) {
Vladimir Marko49b0f452015-12-10 13:49:19 +0000417 size_t num_classes = 0u;
418 for (const OatDexFile& oat_dex_file : writer_->oat_dex_files_) {
419 num_classes += oat_dex_file.class_offsets_.size();
420 }
421 writer_->oat_classes_.reserve(num_classes);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100422 compiled_methods_.reserve(256u);
423 }
424
425 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
426 DexMethodVisitor::StartClass(dex_file, class_def_index);
427 compiled_methods_.clear();
428 num_non_null_compiled_methods_ = 0u;
429 return true;
430 }
431
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300432 bool VisitMethod(size_t class_def_method_index ATTRIBUTE_UNUSED,
433 const ClassDataItemIterator& it) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100434 // Fill in the compiled_methods_ array for methods that have a
435 // CompiledMethod. We track the number of non-null entries in
436 // num_non_null_compiled_methods_ since we only want to allocate
437 // OatMethodOffsets for the compiled methods.
438 uint32_t method_idx = it.GetMemberIndex();
439 CompiledMethod* compiled_method =
440 writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx));
441 compiled_methods_.push_back(compiled_method);
442 if (compiled_method != nullptr) {
443 ++num_non_null_compiled_methods_;
444 }
445 return true;
446 }
447
448 bool EndClass() {
449 ClassReference class_ref(dex_file_, class_def_index_);
450 CompiledClass* compiled_class = writer_->compiler_driver_->GetCompiledClass(class_ref);
451 mirror::Class::Status status;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700452 if (compiled_class != nullptr) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100453 status = compiled_class->GetStatus();
454 } else if (writer_->compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) {
455 status = mirror::Class::kStatusError;
456 } else {
457 status = mirror::Class::kStatusNotReady;
458 }
459
Vladimir Marko49b0f452015-12-10 13:49:19 +0000460 writer_->oat_classes_.emplace_back(offset_,
461 compiled_methods_,
462 num_non_null_compiled_methods_,
463 status);
464 offset_ += writer_->oat_classes_.back().SizeOf();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100465 return DexMethodVisitor::EndClass();
466 }
467
468 private:
Vladimir Marko49b0f452015-12-10 13:49:19 +0000469 dchecked_vector<CompiledMethod*> compiled_methods_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100470 size_t num_non_null_compiled_methods_;
471};
472
473class OatWriter::InitCodeMethodVisitor : public OatDexMethodVisitor {
474 public:
475 InitCodeMethodVisitor(OatWriter* writer, size_t offset)
David Srbecky009e2a62015-04-15 02:46:30 +0100476 : OatDexMethodVisitor(writer, offset),
David Srbecky009e2a62015-04-15 02:46:30 +0100477 debuggable_(writer->GetCompilerDriver()->GetCompilerOptions().GetDebuggable()) {
David Srbeckyf8980872015-05-22 17:04:47 +0100478 writer_->absolute_patch_locations_.reserve(
Vladimir Markof4da6752014-08-01 19:04:18 +0100479 writer_->compiler_driver_->GetNonRelativeLinkerPatchCount());
480 }
481
482 bool EndClass() {
483 OatDexMethodVisitor::EndClass();
484 if (oat_class_index_ == writer_->oat_classes_.size()) {
Vladimir Marko71b0ddf2015-04-02 19:45:06 +0100485 offset_ = writer_->relative_patcher_->ReserveSpaceEnd(offset_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100486 }
487 return true;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100488 }
489
490 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
Mathieu Chartier90443472015-07-16 20:32:27 -0700491 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko49b0f452015-12-10 13:49:19 +0000492 OatClass* oat_class = &writer_->oat_classes_[oat_class_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100493 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
494
495 if (compiled_method != nullptr) {
496 // Derived from CompiledMethod.
497 uint32_t quick_code_offset = 0;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100498
Vladimir Marko35831e82015-09-11 11:59:18 +0100499 ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode();
500 uint32_t code_size = quick_code.size() * sizeof(uint8_t);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800501 uint32_t thumb_offset = compiled_method->CodeDelta();
Elliott Hughes956af0f2014-12-11 14:34:28 -0800502
David Srbecky009e2a62015-04-15 02:46:30 +0100503 // Deduplicate code arrays if we are not producing debuggable code.
Vladimir Markoc74658b2015-03-31 10:26:41 +0100504 bool deduped = false;
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000505 if (debuggable_) {
506 quick_code_offset = NewQuickCodeOffset(compiled_method, it, thumb_offset);
507 } else {
508 auto lb = dedupe_map_.lower_bound(compiled_method);
509 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(compiled_method, lb->first)) {
510 quick_code_offset = lb->second;
511 deduped = true;
Nicolas Geoffrayf0758792015-07-13 11:56:00 +0000512 } else {
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000513 quick_code_offset = NewQuickCodeOffset(compiled_method, it, thumb_offset);
514 dedupe_map_.PutBefore(lb, compiled_method, quick_code_offset);
David Srbecky009e2a62015-04-15 02:46:30 +0100515 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800516 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100517
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100518 if (code_size != 0) {
519 MethodReference method_ref(dex_file_, it.GetMemberIndex());
520 auto method_lb = writer_->method_offset_map_.map.lower_bound(method_ref);
521 if (method_lb != writer_->method_offset_map_.map.end() &&
522 !writer_->method_offset_map_.map.key_comp()(method_ref, method_lb->first)) {
523 // TODO: Should this be a hard failure?
524 LOG(WARNING) << "Multiple definitions of "
525 << PrettyMethod(method_ref.dex_method_index, *method_ref.dex_file)
526 << ((method_lb->second != quick_code_offset) ? "; OFFSET MISMATCH" : "");
527 } else {
528 writer_->method_offset_map_.map.PutBefore(method_lb, method_ref, quick_code_offset);
529 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800530 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100531
Elliott Hughes956af0f2014-12-11 14:34:28 -0800532 // Update quick method header.
533 DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size());
534 OatQuickMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_];
535 uint32_t mapping_table_offset = method_header->mapping_table_offset_;
536 uint32_t vmap_table_offset = method_header->vmap_table_offset_;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100537 // If we don't have quick code, then we must have a vmap, as that is how the dex2dex
538 // compiler records its transformations.
Vladimir Marko35831e82015-09-11 11:59:18 +0100539 DCHECK(!quick_code.empty() || vmap_table_offset != 0);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800540 uint32_t gc_map_offset = method_header->gc_map_offset_;
541 // The code offset was 0 when the mapping/vmap table offset was set, so it's set
542 // to 0-offset and we need to adjust it by code_offset.
543 uint32_t code_offset = quick_code_offset - thumb_offset;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100544 if (mapping_table_offset != 0u && code_offset != 0u) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800545 mapping_table_offset += code_offset;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100546 DCHECK_LT(mapping_table_offset, code_offset) << "Overflow in oat offsets";
Elliott Hughes956af0f2014-12-11 14:34:28 -0800547 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100548 if (vmap_table_offset != 0u && code_offset != 0u) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800549 vmap_table_offset += code_offset;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100550 DCHECK_LT(vmap_table_offset, code_offset) << "Overflow in oat offsets";
Elliott Hughes956af0f2014-12-11 14:34:28 -0800551 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100552 if (gc_map_offset != 0u && code_offset != 0u) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800553 gc_map_offset += code_offset;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100554 DCHECK_LT(gc_map_offset, code_offset) << "Overflow in oat offsets";
Elliott Hughes956af0f2014-12-11 14:34:28 -0800555 }
556 uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
557 uint32_t core_spill_mask = compiled_method->GetCoreSpillMask();
558 uint32_t fp_spill_mask = compiled_method->GetFpSpillMask();
559 *method_header = OatQuickMethodHeader(mapping_table_offset, vmap_table_offset,
560 gc_map_offset, frame_size_in_bytes, core_spill_mask,
561 fp_spill_mask, code_size);
Vladimir Marko7624d252014-05-02 14:40:15 +0100562
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000563 if (!deduped) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800564 // Update offsets. (Checksum is updated when writing.)
565 offset_ += sizeof(*method_header); // Method header is prepended before code.
566 offset_ += code_size;
567 // Record absolute patch locations.
568 if (!compiled_method->GetPatches().empty()) {
569 uintptr_t base_loc = offset_ - code_size - writer_->oat_header_->GetExecutableOffset();
570 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
Vladimir Marko20f85592015-03-19 10:07:02 +0000571 if (!patch.IsPcRelative()) {
David Srbeckyf8980872015-05-22 17:04:47 +0100572 writer_->absolute_patch_locations_.push_back(base_loc + patch.LiteralOffset());
Vladimir Markof4da6752014-08-01 19:04:18 +0100573 }
574 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100575 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800576 }
Alex Light78382fa2014-06-06 15:45:32 -0700577
David Srbecky8363c772015-05-28 16:12:43 +0100578 if (writer_->compiler_driver_->GetCompilerOptions().GetGenerateDebugInfo()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800579 // Record debug information for this function if we are doing that.
Elliott Hughes956af0f2014-12-11 14:34:28 -0800580 const uint32_t quick_code_start = quick_code_offset -
David Srbecky6f715892015-03-30 14:21:42 +0100581 writer_->oat_header_->GetExecutableOffset() - thumb_offset;
Vladimir Marko10c13562015-11-25 14:33:36 +0000582 writer_->method_info_.push_back(dwarf::MethodDebugInfo {
David Srbecky0df9e1f2015-04-07 19:02:58 +0100583 dex_file_,
584 class_def_index_,
585 it.GetMemberIndex(),
586 it.GetMethodAccessFlags(),
587 it.GetMethodCodeItem(),
588 deduped,
589 quick_code_start,
590 quick_code_start + code_size,
591 compiled_method});
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100592 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100593
594 if (kIsDebugBuild) {
595 // We expect GC maps except when the class hasn't been verified or the method is native.
596 const CompilerDriver* compiler_driver = writer_->compiler_driver_;
597 ClassReference class_ref(dex_file_, class_def_index_);
598 CompiledClass* compiled_class = compiler_driver->GetCompiledClass(class_ref);
599 mirror::Class::Status status;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700600 if (compiled_class != nullptr) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100601 status = compiled_class->GetStatus();
602 } else if (compiler_driver->GetVerificationResults()->IsClassRejected(class_ref)) {
603 status = mirror::Class::kStatusError;
604 } else {
605 status = mirror::Class::kStatusNotReady;
606 }
Vladimir Marko35831e82015-09-11 11:59:18 +0100607 ArrayRef<const uint8_t> gc_map = compiled_method->GetGcMap();
608 if (!gc_map.empty()) {
609 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
Andreas Gampe51829322014-08-25 15:05:04 -0700610 bool is_native = it.MemberIsNative();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100611 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
Vladimir Marko35831e82015-09-11 11:59:18 +0100612 << gc_map_size << " " << (is_native ? "true" : "false") << " "
Nicolas Geoffray39468442014-09-02 15:17:15 +0100613 << (status < mirror::Class::kStatusVerified) << " " << status << " "
614 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
615 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100616 }
617
618 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
619 OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_];
620 offsets->code_offset_ = quick_code_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100621 ++method_offsets_index_;
622 }
623
624 return true;
625 }
626
627 private:
Vladimir Marko20f85592015-03-19 10:07:02 +0000628 struct CodeOffsetsKeyComparator {
629 bool operator()(const CompiledMethod* lhs, const CompiledMethod* rhs) const {
Vladimir Marko35831e82015-09-11 11:59:18 +0100630 // Code is deduplicated by CompilerDriver, compare only data pointers.
631 if (lhs->GetQuickCode().data() != rhs->GetQuickCode().data()) {
632 return lhs->GetQuickCode().data() < rhs->GetQuickCode().data();
Vladimir Marko20f85592015-03-19 10:07:02 +0000633 }
634 // If the code is the same, all other fields are likely to be the same as well.
Vladimir Marko35831e82015-09-11 11:59:18 +0100635 if (UNLIKELY(lhs->GetMappingTable().data() != rhs->GetMappingTable().data())) {
636 return lhs->GetMappingTable().data() < rhs->GetMappingTable().data();
Vladimir Marko20f85592015-03-19 10:07:02 +0000637 }
Vladimir Marko35831e82015-09-11 11:59:18 +0100638 if (UNLIKELY(lhs->GetVmapTable().data() != rhs->GetVmapTable().data())) {
639 return lhs->GetVmapTable().data() < rhs->GetVmapTable().data();
Vladimir Marko20f85592015-03-19 10:07:02 +0000640 }
Vladimir Marko35831e82015-09-11 11:59:18 +0100641 if (UNLIKELY(lhs->GetGcMap().data() != rhs->GetGcMap().data())) {
642 return lhs->GetGcMap().data() < rhs->GetGcMap().data();
Vladimir Marko20f85592015-03-19 10:07:02 +0000643 }
Vladimir Marko35831e82015-09-11 11:59:18 +0100644 if (UNLIKELY(lhs->GetPatches().data() != rhs->GetPatches().data())) {
645 return lhs->GetPatches().data() < rhs->GetPatches().data();
Vladimir Marko20f85592015-03-19 10:07:02 +0000646 }
647 return false;
648 }
649 };
650
David Srbecky009e2a62015-04-15 02:46:30 +0100651 uint32_t NewQuickCodeOffset(CompiledMethod* compiled_method,
652 const ClassDataItemIterator& it,
653 uint32_t thumb_offset) {
654 offset_ = writer_->relative_patcher_->ReserveSpace(
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100655 offset_, compiled_method, MethodReference(dex_file_, it.GetMemberIndex()));
David Srbecky009e2a62015-04-15 02:46:30 +0100656 offset_ = compiled_method->AlignCode(offset_);
657 DCHECK_ALIGNED_PARAM(offset_,
658 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
659 return offset_ + sizeof(OatQuickMethodHeader) + thumb_offset;
660 }
661
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100662 // Deduplication is already done on a pointer basis by the compiler driver,
663 // so we can simply compare the pointers to find out if things are duplicated.
Vladimir Marko8a630572014-04-09 18:45:35 +0100664 SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_;
David Srbecky2f6cdb02015-04-11 00:17:53 +0100665
David Srbecky009e2a62015-04-15 02:46:30 +0100666 // Cache of compiler's --debuggable option.
667 const bool debuggable_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100668};
669
670template <typename DataAccess>
671class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor {
672 public:
673 InitMapMethodVisitor(OatWriter* writer, size_t offset)
674 : OatDexMethodVisitor(writer, offset) {
675 }
676
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700677 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it ATTRIBUTE_UNUSED)
Mathieu Chartier90443472015-07-16 20:32:27 -0700678 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko49b0f452015-12-10 13:49:19 +0000679 OatClass* oat_class = &writer_->oat_classes_[oat_class_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100680 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
681
682 if (compiled_method != nullptr) {
683 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
684 DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u);
685
Vladimir Marko35831e82015-09-11 11:59:18 +0100686 ArrayRef<const uint8_t> map = DataAccess::GetData(compiled_method);
687 uint32_t map_size = map.size() * sizeof(map[0]);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100688 if (map_size != 0u) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100689 auto lb = dedupe_map_.lower_bound(map.data());
690 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(map.data(), lb->first)) {
Vladimir Markobd72fc12014-07-09 16:06:40 +0100691 DataAccess::SetOffset(oat_class, method_offsets_index_, lb->second);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100692 } else {
693 DataAccess::SetOffset(oat_class, method_offsets_index_, offset_);
Vladimir Marko35831e82015-09-11 11:59:18 +0100694 dedupe_map_.PutBefore(lb, map.data(), offset_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100695 offset_ += map_size;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100696 }
697 }
698 ++method_offsets_index_;
699 }
700
701 return true;
702 }
703
704 private:
705 // Deduplication is already done on a pointer basis by the compiler driver,
706 // so we can simply compare the pointers to find out if things are duplicated.
Vladimir Marko35831e82015-09-11 11:59:18 +0100707 SafeMap<const uint8_t*, uint32_t> dedupe_map_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100708};
709
710class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor {
711 public:
712 InitImageMethodVisitor(OatWriter* writer, size_t offset)
Jeff Haoc7d11882015-02-03 15:08:39 -0800713 : OatDexMethodVisitor(writer, offset),
714 pointer_size_(GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet())) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100715 }
716
717 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
Mathieu Chartier90443472015-07-16 20:32:27 -0700718 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko49b0f452015-12-10 13:49:19 +0000719 OatClass* oat_class = &writer_->oat_classes_[oat_class_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100720 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
721
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800722 OatMethodOffsets offsets(0u);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100723 if (compiled_method != nullptr) {
724 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
725 offsets = oat_class->method_offsets_[method_offsets_index_];
726 ++method_offsets_index_;
727 }
728
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100729 ClassLinker* linker = Runtime::Current()->GetClassLinker();
730 InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_));
731 // Unchecked as we hold mutator_lock_ on entry.
732 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800733 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700734 Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache(
735 Thread::Current(), *dex_file_)));
Andreas Gampedae24142015-12-03 17:27:32 -0800736 ArtMethod* method = linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
737 *dex_file_,
738 it.GetMemberIndex(),
739 dex_cache,
740 NullHandle<mirror::ClassLoader>(),
741 nullptr,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700742 invoke_type);
Andreas Gamped9efea62014-07-21 22:56:08 -0700743 if (method == nullptr) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100744 LOG(INTERNAL_FATAL) << "Unexpected failure to resolve a method: "
745 << PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
Andreas Gamped9efea62014-07-21 22:56:08 -0700746 soa.Self()->AssertPendingException();
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000747 mirror::Throwable* exc = soa.Self()->GetException();
Andreas Gamped9efea62014-07-21 22:56:08 -0700748 std::string dump = exc->Dump();
749 LOG(FATAL) << dump;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100750 UNREACHABLE();
Andreas Gamped9efea62014-07-21 22:56:08 -0700751 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100752
Vladimir Marko35831e82015-09-11 11:59:18 +0100753 if (compiled_method != nullptr && compiled_method->GetQuickCode().size() != 0) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100754 method->SetEntryPointFromQuickCompiledCodePtrSize(
755 reinterpret_cast<void*>(offsets.code_offset_), pointer_size_);
756 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100757
758 return true;
759 }
Jeff Haoc7d11882015-02-03 15:08:39 -0800760
761 protected:
762 const size_t pointer_size_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100763};
764
765class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor {
766 public:
767 WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
Vladimir Markof4da6752014-08-01 19:04:18 +0100768 size_t relative_offset) SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100769 : OatDexMethodVisitor(writer, relative_offset),
770 out_(out),
Vladimir Markof4da6752014-08-01 19:04:18 +0100771 file_offset_(file_offset),
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100772 soa_(Thread::Current()),
773 no_thread_suspension_(soa_.Self(), "OatWriter patching"),
Vladimir Markof4da6752014-08-01 19:04:18 +0100774 class_linker_(Runtime::Current()->GetClassLinker()),
775 dex_cache_(nullptr) {
Vladimir Marko09d09432015-09-08 13:47:48 +0100776 patched_code_.reserve(16 * KB);
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800777 if (writer_->HasBootImage()) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100778 // If we're creating the image, the address space must be ready so that we can apply patches.
779 CHECK(writer_->image_writer_->IsImageAddressSpaceReady());
Vladimir Markof4da6752014-08-01 19:04:18 +0100780 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100781 }
782
Vladimir Markof4da6752014-08-01 19:04:18 +0100783 ~WriteCodeMethodVisitor() UNLOCK_FUNCTION(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100784 }
785
786 bool StartClass(const DexFile* dex_file, size_t class_def_index)
Mathieu Chartier90443472015-07-16 20:32:27 -0700787 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100788 OatDexMethodVisitor::StartClass(dex_file, class_def_index);
789 if (dex_cache_ == nullptr || dex_cache_->GetDexFile() != dex_file) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700790 dex_cache_ = class_linker_->FindDexCache(Thread::Current(), *dex_file);
Vladimir Markof4da6752014-08-01 19:04:18 +0100791 }
792 return true;
793 }
794
Mathieu Chartier90443472015-07-16 20:32:27 -0700795 bool EndClass() SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100796 bool result = OatDexMethodVisitor::EndClass();
797 if (oat_class_index_ == writer_->oat_classes_.size()) {
798 DCHECK(result); // OatDexMethodVisitor::EndClass() never fails.
Vladimir Marko20f85592015-03-19 10:07:02 +0000799 offset_ = writer_->relative_patcher_->WriteThunks(out_, offset_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100800 if (UNLIKELY(offset_ == 0u)) {
801 PLOG(ERROR) << "Failed to write final relative call thunks";
802 result = false;
803 }
804 }
805 return result;
806 }
807
808 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
Mathieu Chartier90443472015-07-16 20:32:27 -0700809 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko49b0f452015-12-10 13:49:19 +0000810 OatClass* oat_class = &writer_->oat_classes_[oat_class_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100811 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
812
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700813 // No thread suspension since dex_cache_ that may get invalidated if that occurs.
814 ScopedAssertNoThreadSuspension tsc(Thread::Current(), __FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700815 if (compiled_method != nullptr) { // ie. not an abstract method
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100816 size_t file_offset = file_offset_;
817 OutputStream* out = out_;
818
Vladimir Marko35831e82015-09-11 11:59:18 +0100819 ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode();
820 uint32_t code_size = quick_code.size() * sizeof(uint8_t);
Nicolas Geoffrayf0758792015-07-13 11:56:00 +0000821
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100822 // Deduplicate code arrays.
823 const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_];
824 if (method_offsets.code_offset_ > offset_) {
825 offset_ = writer_->relative_patcher_->WriteThunks(out, offset_);
826 if (offset_ == 0u) {
827 ReportWriteFailure("relative call thunk", it);
828 return false;
Nicolas Geoffrayf0758792015-07-13 11:56:00 +0000829 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100830 uint32_t aligned_offset = compiled_method->AlignCode(offset_);
831 uint32_t aligned_code_delta = aligned_offset - offset_;
832 if (aligned_code_delta != 0) {
833 if (!writer_->WriteCodeAlignment(out, aligned_code_delta)) {
834 ReportWriteFailure("code alignment padding", it);
835 return false;
836 }
837 offset_ += aligned_code_delta;
838 DCHECK_OFFSET_();
839 }
840 DCHECK_ALIGNED_PARAM(offset_,
841 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
842 DCHECK_EQ(method_offsets.code_offset_,
843 offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta())
844 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
845 const OatQuickMethodHeader& method_header =
846 oat_class->method_headers_[method_offsets_index_];
Vladimir Marko49b0f452015-12-10 13:49:19 +0000847 if (!writer_->WriteData(out, &method_header, sizeof(method_header))) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100848 ReportWriteFailure("method header", it);
849 return false;
850 }
851 writer_->size_method_header_ += sizeof(method_header);
852 offset_ += sizeof(method_header);
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000853 DCHECK_OFFSET_();
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100854
855 if (!compiled_method->GetPatches().empty()) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100856 patched_code_.assign(quick_code.begin(), quick_code.end());
857 quick_code = ArrayRef<const uint8_t>(patched_code_);
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100858 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
859 if (patch.Type() == kLinkerPatchCallRelative) {
860 // NOTE: Relative calls across oat files are not supported.
861 uint32_t target_offset = GetTargetOffset(patch);
862 uint32_t literal_offset = patch.LiteralOffset();
863 writer_->relative_patcher_->PatchCall(&patched_code_, literal_offset,
864 offset_ + literal_offset, target_offset);
865 } else if (patch.Type() == kLinkerPatchDexCacheArray) {
866 uint32_t target_offset = GetDexCacheOffset(patch);
867 uint32_t literal_offset = patch.LiteralOffset();
868 writer_->relative_patcher_->PatchDexCacheReference(&patched_code_, patch,
869 offset_ + literal_offset,
870 target_offset);
871 } else if (patch.Type() == kLinkerPatchCall) {
872 uint32_t target_offset = GetTargetOffset(patch);
873 PatchCodeAddress(&patched_code_, patch.LiteralOffset(), target_offset);
874 } else if (patch.Type() == kLinkerPatchMethod) {
875 ArtMethod* method = GetTargetMethod(patch);
876 PatchMethodAddress(&patched_code_, patch.LiteralOffset(), method);
877 } else if (patch.Type() == kLinkerPatchType) {
878 mirror::Class* type = GetTargetType(patch);
879 PatchObjectAddress(&patched_code_, patch.LiteralOffset(), type);
880 }
881 }
882 }
883
Vladimir Marko49b0f452015-12-10 13:49:19 +0000884 if (!writer_->WriteData(out, quick_code.data(), code_size)) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100885 ReportWriteFailure("method code", it);
886 return false;
887 }
888 writer_->size_code_ += code_size;
889 offset_ += code_size;
Nicolas Geoffrayf0758792015-07-13 11:56:00 +0000890 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100891 DCHECK_OFFSET_();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100892 ++method_offsets_index_;
893 }
894
895 return true;
896 }
897
898 private:
899 OutputStream* const out_;
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100900 const size_t file_offset_;
901 const ScopedObjectAccess soa_;
902 const ScopedAssertNoThreadSuspension no_thread_suspension_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100903 ClassLinker* const class_linker_;
904 mirror::DexCache* dex_cache_;
905 std::vector<uint8_t> patched_code_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100906
907 void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) {
908 PLOG(ERROR) << "Failed to write " << what << " for "
909 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
910 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100911
Mathieu Chartiere401d142015-04-22 13:56:20 -0700912 ArtMethod* GetTargetMethod(const LinkerPatch& patch)
Mathieu Chartier90443472015-07-16 20:32:27 -0700913 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100914 MethodReference ref = patch.TargetMethod();
915 mirror::DexCache* dex_cache =
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700916 (dex_file_ == ref.dex_file) ? dex_cache_ : class_linker_->FindDexCache(
917 Thread::Current(), *ref.dex_file);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700918 ArtMethod* method = dex_cache->GetResolvedMethod(
919 ref.dex_method_index, class_linker_->GetImagePointerSize());
Vladimir Markof4da6752014-08-01 19:04:18 +0100920 CHECK(method != nullptr);
921 return method;
922 }
923
Mathieu Chartier90443472015-07-16 20:32:27 -0700924 uint32_t GetTargetOffset(const LinkerPatch& patch) SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markob163bb72015-03-31 21:49:49 +0100925 auto target_it = writer_->method_offset_map_.map.find(patch.TargetMethod());
Vladimir Markof4da6752014-08-01 19:04:18 +0100926 uint32_t target_offset =
Vladimir Markob163bb72015-03-31 21:49:49 +0100927 (target_it != writer_->method_offset_map_.map.end()) ? target_it->second : 0u;
Vladimir Markof4da6752014-08-01 19:04:18 +0100928 // If there's no compiled code, point to the correct trampoline.
929 if (UNLIKELY(target_offset == 0)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700930 ArtMethod* target = GetTargetMethod(patch);
Vladimir Markof4da6752014-08-01 19:04:18 +0100931 DCHECK(target != nullptr);
Jeff Haoa0acc2d2015-01-27 11:22:04 -0800932 size_t size = GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet());
933 const void* oat_code_offset = target->GetEntryPointFromQuickCompiledCodePtrSize(size);
934 if (oat_code_offset != 0) {
935 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(oat_code_offset));
936 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(oat_code_offset));
937 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(oat_code_offset));
938 target_offset = PointerToLowMemUInt32(oat_code_offset);
939 } else {
940 target_offset = target->IsNative()
941 ? writer_->oat_header_->GetQuickGenericJniTrampolineOffset()
942 : writer_->oat_header_->GetQuickToInterpreterBridgeOffset();
943 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100944 }
945 return target_offset;
946 }
947
948 mirror::Class* GetTargetType(const LinkerPatch& patch)
Mathieu Chartier90443472015-07-16 20:32:27 -0700949 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100950 mirror::DexCache* dex_cache = (dex_file_ == patch.TargetTypeDexFile())
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700951 ? dex_cache_ : class_linker_->FindDexCache(Thread::Current(), *patch.TargetTypeDexFile());
Vladimir Markof4da6752014-08-01 19:04:18 +0100952 mirror::Class* type = dex_cache->GetResolvedType(patch.TargetTypeIndex());
953 CHECK(type != nullptr);
954 return type;
955 }
956
Mathieu Chartier90443472015-07-16 20:32:27 -0700957 uint32_t GetDexCacheOffset(const LinkerPatch& patch) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800958 if (writer_->HasBootImage()) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100959 auto* element = writer_->image_writer_->GetDexCacheArrayElementImageAddress<const uint8_t*>(
Vladimir Marko20f85592015-03-19 10:07:02 +0000960 patch.TargetDexCacheDexFile(), patch.TargetDexCacheElementOffset());
961 const uint8_t* oat_data = writer_->image_writer_->GetOatFileBegin() + file_offset_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100962 return element - oat_data;
Vladimir Marko20f85592015-03-19 10:07:02 +0000963 } else {
Vladimir Marko09d09432015-09-08 13:47:48 +0100964 size_t start = writer_->dex_cache_arrays_offsets_.Get(patch.TargetDexCacheDexFile());
965 return start + patch.TargetDexCacheElementOffset();
Vladimir Marko20f85592015-03-19 10:07:02 +0000966 }
967 }
968
Vladimir Markof4da6752014-08-01 19:04:18 +0100969 void PatchObjectAddress(std::vector<uint8_t>* code, uint32_t offset, mirror::Object* object)
Mathieu Chartier90443472015-07-16 20:32:27 -0700970 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800971 if (writer_->HasBootImage()) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100972 object = writer_->image_writer_->GetImageAddress(object);
Vladimir Marko09d09432015-09-08 13:47:48 +0100973 } else {
974 // NOTE: We're using linker patches for app->boot references when the image can
975 // be relocated and therefore we need to emit .oat_patches. We're not using this
976 // for app->app references, so check that the object is in the image space.
977 DCHECK(Runtime::Current()->GetHeap()->FindSpaceFromObject(object, false)->IsImageSpace());
Vladimir Markof4da6752014-08-01 19:04:18 +0100978 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100979 // Note: We only patch targeting Objects in image which is in the low 4gb.
Vladimir Markof4da6752014-08-01 19:04:18 +0100980 uint32_t address = PointerToLowMemUInt32(object);
981 DCHECK_LE(offset + 4, code->size());
982 uint8_t* data = &(*code)[offset];
983 data[0] = address & 0xffu;
984 data[1] = (address >> 8) & 0xffu;
985 data[2] = (address >> 16) & 0xffu;
986 data[3] = (address >> 24) & 0xffu;
987 }
988
Mathieu Chartiere401d142015-04-22 13:56:20 -0700989 void PatchMethodAddress(std::vector<uint8_t>* code, uint32_t offset, ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700990 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800991 if (writer_->HasBootImage()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700992 method = writer_->image_writer_->GetImageMethodAddress(method);
Vladimir Marko09d09432015-09-08 13:47:48 +0100993 } else if (kIsDebugBuild) {
994 // NOTE: We're using linker patches for app->boot references when the image can
995 // be relocated and therefore we need to emit .oat_patches. We're not using this
996 // for app->app references, so check that the method is an image method.
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800997 gc::space::ImageSpace* image_space = Runtime::Current()->GetHeap()->GetBootImageSpace();
Vladimir Marko09d09432015-09-08 13:47:48 +0100998 size_t method_offset = reinterpret_cast<const uint8_t*>(method) - image_space->Begin();
999 CHECK(image_space->GetImageHeader().GetMethodsSection().Contains(method_offset));
Mathieu Chartiere401d142015-04-22 13:56:20 -07001000 }
Vladimir Marko09d09432015-09-08 13:47:48 +01001001 // Note: We only patch targeting ArtMethods in image which is in the low 4gb.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001002 uint32_t address = PointerToLowMemUInt32(method);
1003 DCHECK_LE(offset + 4, code->size());
1004 uint8_t* data = &(*code)[offset];
1005 data[0] = address & 0xffu;
1006 data[1] = (address >> 8) & 0xffu;
1007 data[2] = (address >> 16) & 0xffu;
1008 data[3] = (address >> 24) & 0xffu;
1009 }
1010
Vladimir Markof4da6752014-08-01 19:04:18 +01001011 void PatchCodeAddress(std::vector<uint8_t>* code, uint32_t offset, uint32_t target_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -07001012 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko09d09432015-09-08 13:47:48 +01001013 uint32_t address = target_offset;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001014 if (writer_->HasBootImage()) {
Vladimir Marko09d09432015-09-08 13:47:48 +01001015 address = PointerToLowMemUInt32(writer_->image_writer_->GetOatFileBegin() +
1016 writer_->oat_data_offset_ + target_offset);
1017 }
Vladimir Markof4da6752014-08-01 19:04:18 +01001018 DCHECK_LE(offset + 4, code->size());
1019 uint8_t* data = &(*code)[offset];
1020 data[0] = address & 0xffu;
1021 data[1] = (address >> 8) & 0xffu;
1022 data[2] = (address >> 16) & 0xffu;
1023 data[3] = (address >> 24) & 0xffu;
1024 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001025};
1026
1027template <typename DataAccess>
1028class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor {
1029 public:
1030 WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001031 size_t relative_offset)
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001032 : OatDexMethodVisitor(writer, relative_offset),
1033 out_(out),
1034 file_offset_(file_offset) {
1035 }
1036
1037 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001038 OatClass* oat_class = &writer_->oat_classes_[oat_class_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001039 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
1040
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001041 if (compiled_method != nullptr) { // ie. not an abstract method
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001042 size_t file_offset = file_offset_;
1043 OutputStream* out = out_;
1044
1045 uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_);
1046 ++method_offsets_index_;
1047
1048 // Write deduplicated map.
Vladimir Marko35831e82015-09-11 11:59:18 +01001049 ArrayRef<const uint8_t> map = DataAccess::GetData(compiled_method);
1050 size_t map_size = map.size() * sizeof(map[0]);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001051 DCHECK((map_size == 0u && map_offset == 0u) ||
1052 (map_size != 0u && map_offset != 0u && map_offset <= offset_))
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001053 << map_size << " " << map_offset << " " << offset_ << " "
1054 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " for " << DataAccess::Name();
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001055 if (map_size != 0u && map_offset == offset_) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001056 if (UNLIKELY(!writer_->WriteData(out, map.data(), map_size))) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001057 ReportWriteFailure(it);
1058 return false;
1059 }
1060 offset_ += map_size;
1061 }
1062 DCHECK_OFFSET_();
1063 }
1064
1065 return true;
1066 }
1067
1068 private:
1069 OutputStream* const out_;
1070 size_t const file_offset_;
1071
1072 void ReportWriteFailure(const ClassDataItemIterator& it) {
1073 PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for "
1074 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
1075 }
1076};
1077
1078// Visit all methods from all classes in all dex files with the specified visitor.
1079bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) {
1080 for (const DexFile* dex_file : *dex_files_) {
1081 const size_t class_def_count = dex_file->NumClassDefs();
1082 for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) {
1083 if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) {
1084 return false;
1085 }
1086 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -07001087 const uint8_t* class_data = dex_file->GetClassData(class_def);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001088 if (class_data != nullptr) { // ie not an empty class, such as a marker interface
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001089 ClassDataItemIterator it(*dex_file, class_data);
1090 while (it.HasNextStaticField()) {
1091 it.Next();
1092 }
1093 while (it.HasNextInstanceField()) {
1094 it.Next();
1095 }
1096 size_t class_def_method_index = 0u;
1097 while (it.HasNextDirectMethod()) {
1098 if (!visitor->VisitMethod(class_def_method_index, it)) {
1099 return false;
1100 }
1101 ++class_def_method_index;
1102 it.Next();
1103 }
1104 while (it.HasNextVirtualMethod()) {
1105 if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) {
1106 return false;
1107 }
1108 ++class_def_method_index;
1109 it.Next();
1110 }
1111 }
1112 if (UNLIKELY(!visitor->EndClass())) {
1113 return false;
1114 }
1115 }
1116 }
1117 return true;
1118}
1119
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001120size_t OatWriter::InitOatHeader() {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001121 oat_header_.reset(OatHeader::Create(compiler_driver_->GetInstructionSet(),
1122 compiler_driver_->GetInstructionSetFeatures(),
1123 dchecked_integral_cast<uint32_t>(dex_files_->size()),
1124 key_value_store_));
1125 oat_header_->SetImageFileLocationOatChecksum(image_file_location_oat_checksum_);
1126 oat_header_->SetImageFileLocationOatDataBegin(image_file_location_oat_begin_);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001127
1128 return oat_header_->GetHeaderSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001129}
1130
1131size_t OatWriter::InitOatDexFiles(size_t offset) {
1132 // create the OatDexFiles
1133 for (size_t i = 0; i != dex_files_->size(); ++i) {
1134 const DexFile* dex_file = (*dex_files_)[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001135 CHECK(dex_file != nullptr);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001136 oat_dex_files_.emplace_back(offset, *dex_file);
1137 offset += oat_dex_files_.back().SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001138 }
1139 return offset;
1140}
1141
Brian Carlstrom89521892011-12-07 22:05:07 -08001142size_t OatWriter::InitDexFiles(size_t offset) {
1143 // calculate the offsets within OatDexFiles to the DexFiles
1144 for (size_t i = 0; i != dex_files_->size(); ++i) {
1145 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001146 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -08001147 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001148 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -08001149
1150 // set offset in OatDexFile to DexFile
Vladimir Marko49b0f452015-12-10 13:49:19 +00001151 oat_dex_files_[i].dex_file_offset_ = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -08001152
1153 const DexFile* dex_file = (*dex_files_)[i];
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001154
1155 // Initialize type lookup table
Vladimir Marko49b0f452015-12-10 13:49:19 +00001156 oat_dex_files_[i].lookup_table_ = dex_file->GetTypeLookupTable();
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001157
Brian Carlstrom89521892011-12-07 22:05:07 -08001158 offset += dex_file->GetHeader().file_size_;
1159 }
1160 return offset;
1161}
1162
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001163size_t OatWriter::InitLookupTables(size_t offset) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001164 for (OatDexFile& oat_dex_file : oat_dex_files_) {
1165 if (oat_dex_file.lookup_table_ != nullptr) {
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001166 uint32_t aligned_offset = RoundUp(offset, 4);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001167 oat_dex_file.lookup_table_offset_ = aligned_offset;
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001168 size_oat_lookup_table_alignment_ += aligned_offset - offset;
Vladimir Marko49b0f452015-12-10 13:49:19 +00001169 offset = aligned_offset + oat_dex_file.lookup_table_->RawDataLength();
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001170 } else {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001171 oat_dex_file.lookup_table_offset_ = 0;
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001172 }
1173 }
1174 return offset;
1175}
1176
Brian Carlstrom389efb02012-01-11 12:06:26 -08001177size_t OatWriter::InitOatClasses(size_t offset) {
Brian Carlstrom389efb02012-01-11 12:06:26 -08001178 // calculate the offsets within OatDexFiles to OatClasses
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001179 InitOatClassesMethodVisitor visitor(this, offset);
1180 bool success = VisitDexMethods(&visitor);
1181 CHECK(success);
1182 offset = visitor.GetOffset();
Brian Carlstromba150c32013-08-27 17:31:03 -07001183
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001184 // Update oat_dex_files_.
1185 auto oat_class_it = oat_classes_.begin();
Vladimir Marko49b0f452015-12-10 13:49:19 +00001186 for (OatDexFile& oat_dex_file : oat_dex_files_) {
1187 for (uint32_t& class_offset : oat_dex_file.class_offsets_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001188 DCHECK(oat_class_it != oat_classes_.end());
Vladimir Marko49b0f452015-12-10 13:49:19 +00001189 class_offset = oat_class_it->offset_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001190 ++oat_class_it;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001191 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001192 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001193 CHECK(oat_class_it == oat_classes_.end());
1194
1195 return offset;
1196}
1197
1198size_t OatWriter::InitOatMaps(size_t offset) {
1199 #define VISIT(VisitorType) \
1200 do { \
1201 VisitorType visitor(this, offset); \
1202 bool success = VisitDexMethods(&visitor); \
1203 DCHECK(success); \
1204 offset = visitor.GetOffset(); \
1205 } while (false)
1206
1207 VISIT(InitMapMethodVisitor<GcMapDataAccess>);
1208 VISIT(InitMapMethodVisitor<MappingTableDataAccess>);
1209 VISIT(InitMapMethodVisitor<VmapTableDataAccess>);
1210
1211 #undef VISIT
1212
Brian Carlstrome24fa612011-09-29 00:53:55 -07001213 return offset;
1214}
1215
1216size_t OatWriter::InitOatCode(size_t offset) {
1217 // calculate the offsets within OatHeader to executable code
1218 size_t old_offset = offset;
Dave Allison50abf0a2014-06-23 13:19:59 -07001219 size_t adjusted_offset = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001220 // required to be on a new page boundary
1221 offset = RoundUp(offset, kPageSize);
1222 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001223 size_executable_offset_alignment_ = offset - old_offset;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001224 if (compiler_driver_->IsBootImage()) {
Alex Lighta59dd802014-07-02 16:28:08 -07001225 CHECK_EQ(image_patch_delta_, 0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001226 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001227
Ian Rogers848871b2013-08-05 10:56:33 -07001228 #define DO_TRAMPOLINE(field, fn_name) \
1229 offset = CompiledCode::AlignCode(offset, instruction_set); \
Dave Allison50abf0a2014-06-23 13:19:59 -07001230 adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \
1231 oat_header_->Set ## fn_name ## Offset(adjusted_offset); \
Ian Rogers848871b2013-08-05 10:56:33 -07001232 field.reset(compiler_driver_->Create ## fn_name()); \
1233 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001234
Ian Rogers848871b2013-08-05 10:56:33 -07001235 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Andreas Gampe2da88232014-02-27 12:26:20 -08001236 DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline);
Jeff Hao88474b42013-10-23 16:24:40 -07001237 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -07001238 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
1239 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001240
Ian Rogers848871b2013-08-05 10:56:33 -07001241 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001242 } else {
Ian Rogers848871b2013-08-05 10:56:33 -07001243 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
1244 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
1245 oat_header_->SetJniDlsymLookupOffset(0);
Andreas Gampe2da88232014-02-27 12:26:20 -08001246 oat_header_->SetQuickGenericJniTrampolineOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -07001247 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001248 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -07001249 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Alex Lighta59dd802014-07-02 16:28:08 -07001250 oat_header_->SetImagePatchDelta(image_patch_delta_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001251 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001252 return offset;
1253}
1254
1255size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001256 #define VISIT(VisitorType) \
1257 do { \
1258 VisitorType visitor(this, offset); \
1259 bool success = VisitDexMethods(&visitor); \
1260 DCHECK(success); \
1261 offset = visitor.GetOffset(); \
1262 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001263
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001264 VISIT(InitCodeMethodVisitor);
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001265 if (compiler_driver_->IsBootImage()) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001266 VISIT(InitImageMethodVisitor);
Ian Rogers0571d352011-11-03 19:51:38 -07001267 }
Logan Chien8b977d32012-02-21 19:14:55 +08001268
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001269 #undef VISIT
1270
Brian Carlstrome24fa612011-09-29 00:53:55 -07001271 return offset;
1272}
1273
David Srbeckybc90fd02015-04-22 19:40:27 +01001274bool OatWriter::WriteRodata(OutputStream* out) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001275 if (!GetOatDataOffset(out)) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001276 return false;
1277 }
Vladimir Marko49b0f452015-12-10 13:49:19 +00001278 const size_t file_offset = oat_data_offset_;
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001279
Vladimir Markof4da6752014-08-01 19:04:18 +01001280 // Reserve space for header. It will be written last - after updating the checksum.
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001281 size_t header_size = oat_header_->GetHeaderSize();
Vladimir Marko49b0f452015-12-10 13:49:19 +00001282 if (out->Seek(header_size, kSeekCurrent) == static_cast<off_t>(-1)) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001283 PLOG(ERROR) << "Failed to reserve space for oat header in " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001284 return false;
1285 }
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001286 size_oat_header_ += sizeof(OatHeader);
1287 size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001288
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001289 if (!WriteTables(out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001290 LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001291 return false;
1292 }
1293
Vladimir Markof4da6752014-08-01 19:04:18 +01001294 off_t tables_end_offset = out->Seek(0, kSeekCurrent);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001295 if (tables_end_offset == static_cast<off_t>(-1)) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001296 LOG(ERROR) << "Failed to seek to oat code position in " << out->GetLocation();
1297 return false;
1298 }
1299 size_t relative_offset = static_cast<size_t>(tables_end_offset) - file_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001300 relative_offset = WriteMaps(out, file_offset, relative_offset);
1301 if (relative_offset == 0) {
1302 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
1303 return false;
1304 }
1305
David Srbeckybc90fd02015-04-22 19:40:27 +01001306 // Write padding.
1307 off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent);
1308 relative_offset += size_executable_offset_alignment_;
1309 DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset());
1310 size_t expected_file_offset = file_offset + relative_offset;
1311 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
1312 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
1313 << " Expected: " << expected_file_offset << " File: " << out->GetLocation();
1314 return 0;
1315 }
1316 DCHECK_OFFSET();
1317
1318 return true;
1319}
1320
1321bool OatWriter::WriteCode(OutputStream* out) {
1322 size_t header_size = oat_header_->GetHeaderSize();
1323 const size_t file_offset = oat_data_offset_;
1324 size_t relative_offset = oat_header_->GetExecutableOffset();
1325 DCHECK_OFFSET();
1326
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001327 relative_offset = WriteCode(out, file_offset, relative_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001328 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001329 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001330 return false;
1331 }
1332
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001333 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
1334 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001335 LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001336 return false;
1337 }
1338
Vladimir Markof4da6752014-08-01 19:04:18 +01001339 const off_t oat_end_file_offset = out->Seek(0, kSeekCurrent);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001340 if (oat_end_file_offset == static_cast<off_t>(-1)) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001341 LOG(ERROR) << "Failed to get oat end file offset in " << out->GetLocation();
1342 return false;
1343 }
1344
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001345 if (kIsDebugBuild) {
1346 uint32_t size_total = 0;
1347 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001348 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001349 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001350
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001351 DO_STAT(size_dex_file_alignment_);
1352 DO_STAT(size_executable_offset_alignment_);
1353 DO_STAT(size_oat_header_);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001354 DO_STAT(size_oat_header_key_value_store_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001355 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -07001356 DO_STAT(size_interpreter_to_interpreter_bridge_);
1357 DO_STAT(size_interpreter_to_compiled_code_bridge_);
1358 DO_STAT(size_jni_dlsym_lookup_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001359 DO_STAT(size_quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001360 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001361 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001362 DO_STAT(size_quick_to_interpreter_bridge_);
1363 DO_STAT(size_trampoline_alignment_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001364 DO_STAT(size_method_header_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001365 DO_STAT(size_code_);
1366 DO_STAT(size_code_alignment_);
Vladimir Markof4da6752014-08-01 19:04:18 +01001367 DO_STAT(size_relative_call_thunks_);
Vladimir Markoc74658b2015-03-31 10:26:41 +01001368 DO_STAT(size_misc_thunks_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001369 DO_STAT(size_mapping_table_);
1370 DO_STAT(size_vmap_table_);
1371 DO_STAT(size_gc_map_);
1372 DO_STAT(size_oat_dex_file_location_size_);
1373 DO_STAT(size_oat_dex_file_location_data_);
1374 DO_STAT(size_oat_dex_file_location_checksum_);
1375 DO_STAT(size_oat_dex_file_offset_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001376 DO_STAT(size_oat_dex_file_lookup_table_offset_);
1377 DO_STAT(size_oat_dex_file_class_offsets_);
1378 DO_STAT(size_oat_lookup_table_alignment_);
1379 DO_STAT(size_oat_lookup_table_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001380 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001381 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001382 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001383 DO_STAT(size_oat_class_method_offsets_);
1384 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001385
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001386 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Vladimir Markof4da6752014-08-01 19:04:18 +01001387 CHECK_EQ(file_offset + size_total, static_cast<size_t>(oat_end_file_offset));
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001388 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001389 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001390
Vladimir Markof4da6752014-08-01 19:04:18 +01001391 CHECK_EQ(file_offset + size_, static_cast<size_t>(oat_end_file_offset));
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001392 CHECK_EQ(size_, relative_offset);
1393
Vladimir Marko49b0f452015-12-10 13:49:19 +00001394 // Finalize the header checksum.
1395 oat_header_->UpdateChecksumWithHeaderData();
1396
Vladimir Markof4da6752014-08-01 19:04:18 +01001397 // Write the header now that the checksum is final.
Vladimir Marko49b0f452015-12-10 13:49:19 +00001398 if (out->Seek(file_offset, kSeekSet) == static_cast<off_t>(-1)) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001399 PLOG(ERROR) << "Failed to seek to oat header position in " << out->GetLocation();
1400 return false;
1401 }
David Srbeckybc90fd02015-04-22 19:40:27 +01001402 DCHECK_EQ(file_offset, static_cast<size_t>(out->Seek(0, kSeekCurrent)));
Vladimir Marko49b0f452015-12-10 13:49:19 +00001403 if (!out->WriteFully(oat_header_.get(), header_size)) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001404 PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation();
1405 return false;
1406 }
Vladimir Marko49b0f452015-12-10 13:49:19 +00001407 if (out->Seek(oat_end_file_offset, kSeekSet) == static_cast<off_t>(-1)) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001408 PLOG(ERROR) << "Failed to seek to end after writing oat header to " << out->GetLocation();
1409 return false;
1410 }
1411 DCHECK_EQ(oat_end_file_offset, out->Seek(0, kSeekCurrent));
1412
Brian Carlstrome24fa612011-09-29 00:53:55 -07001413 return true;
1414}
1415
Ian Rogers3d504072014-03-01 09:16:49 -08001416bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001417 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001418 if (!oat_dex_files_[i].Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001419 PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001420 return false;
1421 }
1422 }
Brian Carlstrom89521892011-12-07 22:05:07 -08001423 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001424 uint32_t expected_offset = file_offset + oat_dex_files_[i].dex_file_offset_;
Ian Rogers3d504072014-03-01 09:16:49 -08001425 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -08001426 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
1427 const DexFile* dex_file = (*dex_files_)[i];
1428 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
1429 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
1430 return false;
1431 }
1432 const DexFile* dex_file = (*dex_files_)[i];
Ian Rogers3d504072014-03-01 09:16:49 -08001433 if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001434 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
Ian Rogers3d504072014-03-01 09:16:49 -08001435 << " to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001436 return false;
1437 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001438 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -08001439 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001440 if (!WriteLookupTables(out, file_offset)) {
1441 return false;
1442 }
Brian Carlstrom389efb02012-01-11 12:06:26 -08001443 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001444 if (!oat_classes_[i].Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001445 PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001446 return false;
1447 }
1448 }
1449 return true;
1450}
1451
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001452bool OatWriter::WriteLookupTables(OutputStream* out, const size_t file_offset) {
1453 for (size_t i = 0; i < oat_dex_files_.size(); ++i) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001454 const uint32_t lookup_table_offset = oat_dex_files_[i].lookup_table_offset_;
1455 const TypeLookupTable* table = oat_dex_files_[i].lookup_table_;
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001456 DCHECK_EQ(lookup_table_offset == 0, table == nullptr);
1457 if (lookup_table_offset == 0) {
1458 continue;
1459 }
1460 const uint32_t expected_offset = file_offset + lookup_table_offset;
1461 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
1462 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
1463 const DexFile* dex_file = (*dex_files_)[i];
1464 PLOG(ERROR) << "Failed to seek to lookup table section. Actual: " << actual_offset
1465 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
1466 return false;
1467 }
1468 if (table != nullptr) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001469 if (!WriteData(out, table->RawData(), table->RawDataLength())) {
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001470 const DexFile* dex_file = (*dex_files_)[i];
1471 PLOG(ERROR) << "Failed to write lookup table for " << dex_file->GetLocation()
1472 << " to " << out->GetLocation();
1473 return false;
1474 }
1475 size_oat_lookup_table_ += table->RawDataLength();
1476 }
1477 }
1478 return true;
1479}
1480
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001481size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) {
1482 #define VISIT(VisitorType) \
1483 do { \
1484 VisitorType visitor(this, out, file_offset, relative_offset); \
1485 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1486 return 0; \
1487 } \
1488 relative_offset = visitor.GetOffset(); \
1489 } while (false)
1490
1491 size_t gc_maps_offset = relative_offset;
1492 VISIT(WriteMapMethodVisitor<GcMapDataAccess>);
1493 size_gc_map_ = relative_offset - gc_maps_offset;
1494
1495 size_t mapping_tables_offset = relative_offset;
1496 VISIT(WriteMapMethodVisitor<MappingTableDataAccess>);
1497 size_mapping_table_ = relative_offset - mapping_tables_offset;
1498
1499 size_t vmap_tables_offset = relative_offset;
1500 VISIT(WriteMapMethodVisitor<VmapTableDataAccess>);
1501 size_vmap_table_ = relative_offset - vmap_tables_offset;
1502
1503 #undef VISIT
1504
1505 return relative_offset;
1506}
1507
1508size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001509 if (compiler_driver_->IsBootImage()) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001510 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001511
Ian Rogers848871b2013-08-05 10:56:33 -07001512 #define DO_TRAMPOLINE(field) \
1513 do { \
1514 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
1515 uint32_t alignment_padding = aligned_offset - relative_offset; \
Ian Rogers3d504072014-03-01 09:16:49 -08001516 out->Seek(alignment_padding, kSeekCurrent); \
Ian Rogers848871b2013-08-05 10:56:33 -07001517 size_trampoline_alignment_ += alignment_padding; \
Vladimir Marko49b0f452015-12-10 13:49:19 +00001518 if (!WriteData(out, field->data(), field->size())) { \
Ian Rogers3d504072014-03-01 09:16:49 -08001519 PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \
Ian Rogers848871b2013-08-05 10:56:33 -07001520 return false; \
1521 } \
1522 size_ ## field += field->size(); \
1523 relative_offset += alignment_padding + field->size(); \
1524 DCHECK_OFFSET(); \
1525 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001526
Ian Rogers848871b2013-08-05 10:56:33 -07001527 DO_TRAMPOLINE(jni_dlsym_lookup_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001528 DO_TRAMPOLINE(quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001529 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001530 DO_TRAMPOLINE(quick_resolution_trampoline_);
1531 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
1532 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001533 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001534 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001535}
1536
Ian Rogers3d504072014-03-01 09:16:49 -08001537size_t OatWriter::WriteCodeDexFiles(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001538 const size_t file_offset,
1539 size_t relative_offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001540 #define VISIT(VisitorType) \
1541 do { \
1542 VisitorType visitor(this, out, file_offset, relative_offset); \
1543 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1544 return 0; \
1545 } \
1546 relative_offset = visitor.GetOffset(); \
1547 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001548
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001549 VISIT(WriteCodeMethodVisitor);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001550
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001551 #undef VISIT
Brian Carlstrom265091e2013-01-30 14:08:26 -08001552
Vladimir Markob163bb72015-03-31 21:49:49 +01001553 size_code_alignment_ += relative_patcher_->CodeAlignmentSize();
1554 size_relative_call_thunks_ += relative_patcher_->RelativeCallThunksSize();
1555 size_misc_thunks_ += relative_patcher_->MiscThunksSize();
1556
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001557 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001558}
1559
Vladimir Marko49b0f452015-12-10 13:49:19 +00001560bool OatWriter::GetOatDataOffset(OutputStream* out) {
1561 // Get the elf file offset of the oat file.
1562 const off_t raw_file_offset = out->Seek(0, kSeekCurrent);
1563 if (raw_file_offset == static_cast<off_t>(-1)) {
1564 LOG(ERROR) << "Failed to get file offset in " << out->GetLocation();
1565 return false;
1566 }
1567 oat_data_offset_ = static_cast<size_t>(raw_file_offset);
1568 return true;
1569}
1570
Vladimir Markof4da6752014-08-01 19:04:18 +01001571bool OatWriter::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) {
1572 static const uint8_t kPadding[] = {
1573 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
1574 };
1575 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
1576 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
1577 return false;
1578 }
1579 size_code_alignment_ += aligned_code_delta;
1580 return true;
1581}
1582
Vladimir Marko49b0f452015-12-10 13:49:19 +00001583bool OatWriter::WriteData(OutputStream* out, const void* data, size_t size) {
1584 oat_header_->UpdateChecksum(data, size);
1585 return out->WriteFully(data, size);
1586}
1587
Vladimir Markob163bb72015-03-31 21:49:49 +01001588std::pair<bool, uint32_t> OatWriter::MethodOffsetMap::FindMethodOffset(MethodReference ref) {
1589 auto it = map.find(ref);
1590 if (it == map.end()) {
1591 return std::pair<bool, uint32_t>(false, 0u);
1592 } else {
1593 return std::pair<bool, uint32_t>(true, it->second);
1594 }
1595}
1596
Brian Carlstrom265091e2013-01-30 14:08:26 -08001597OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
1598 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -08001599 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001600 dex_file_location_size_ = location.size();
1601 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001602 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -08001603 dex_file_offset_ = 0;
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001604 lookup_table_offset_ = 0;
Vladimir Marko49b0f452015-12-10 13:49:19 +00001605 class_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001606}
1607
1608size_t OatWriter::OatDexFile::SizeOf() const {
1609 return sizeof(dex_file_location_size_)
1610 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001611 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -08001612 + sizeof(dex_file_offset_)
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001613 + sizeof(lookup_table_offset_)
Vladimir Marko49b0f452015-12-10 13:49:19 +00001614 + (sizeof(class_offsets_[0]) * class_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001615}
1616
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001617bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001618 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001619 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001620 DCHECK_OFFSET_();
Vladimir Marko49b0f452015-12-10 13:49:19 +00001621 if (!oat_writer->WriteData(out, &dex_file_location_size_, sizeof(dex_file_location_size_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08001622 PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001623 return false;
1624 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001625 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001626 if (!oat_writer->WriteData(out, dex_file_location_data_, dex_file_location_size_)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001627 PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001628 return false;
1629 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001630 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Vladimir Marko49b0f452015-12-10 13:49:19 +00001631 if (!oat_writer->WriteData(out,
1632 &dex_file_location_checksum_,
1633 sizeof(dex_file_location_checksum_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08001634 PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001635 return false;
1636 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001637 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001638 if (!oat_writer->WriteData(out, &dex_file_offset_, sizeof(dex_file_offset_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08001639 PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001640 return false;
1641 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001642 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001643 if (!oat_writer->WriteData(out, &lookup_table_offset_, sizeof(lookup_table_offset_))) {
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001644 PLOG(ERROR) << "Failed to write lookup table offset to " << out->GetLocation();
1645 return false;
1646 }
Vladimir Marko49b0f452015-12-10 13:49:19 +00001647 oat_writer->size_oat_dex_file_lookup_table_offset_ += sizeof(lookup_table_offset_);
1648 if (!oat_writer->WriteData(out, class_offsets_.data(), GetClassOffsetsRawSize())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001649 PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001650 return false;
1651 }
Vladimir Marko49b0f452015-12-10 13:49:19 +00001652 oat_writer->size_oat_dex_file_class_offsets_ += GetClassOffsetsRawSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001653 return true;
1654}
1655
Brian Carlstromba150c32013-08-27 17:31:03 -07001656OatWriter::OatClass::OatClass(size_t offset,
Vladimir Marko49b0f452015-12-10 13:49:19 +00001657 const dchecked_vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -07001658 uint32_t num_non_null_compiled_methods,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001659 mirror::Class::Status status)
1660 : compiled_methods_(compiled_methods) {
1661 uint32_t num_methods = compiled_methods.size();
Brian Carlstromba150c32013-08-27 17:31:03 -07001662 CHECK_LE(num_non_null_compiled_methods, num_methods);
1663
Brian Carlstrom265091e2013-01-30 14:08:26 -08001664 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -07001665 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
1666
1667 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
1668 // apply when there are 0 methods, we just arbitrarily say that 0
1669 // methods means kOatClassNoneCompiled and that we won't use
1670 // kOatClassAllCompiled unless there is at least one compiled
1671 // method. This means in an interpretter only system, we can assert
1672 // that all classes are kOatClassNoneCompiled.
1673 if (num_non_null_compiled_methods == 0) {
1674 type_ = kOatClassNoneCompiled;
1675 } else if (num_non_null_compiled_methods == num_methods) {
1676 type_ = kOatClassAllCompiled;
1677 } else {
1678 type_ = kOatClassSomeCompiled;
1679 }
1680
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001681 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -07001682 method_offsets_.resize(num_non_null_compiled_methods);
Vladimir Marko8a630572014-04-09 18:45:35 +01001683 method_headers_.resize(num_non_null_compiled_methods);
Brian Carlstromba150c32013-08-27 17:31:03 -07001684
1685 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
1686 if (type_ == kOatClassSomeCompiled) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001687 method_bitmap_.reset(new BitVector(num_methods, false, Allocator::GetMallocAllocator()));
Brian Carlstromba150c32013-08-27 17:31:03 -07001688 method_bitmap_size_ = method_bitmap_->GetSizeOf();
1689 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
1690 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
1691 } else {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001692 method_bitmap_ = nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001693 method_bitmap_size_ = 0;
1694 }
1695
1696 for (size_t i = 0; i < num_methods; i++) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001697 CompiledMethod* compiled_method = compiled_methods_[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001698 if (compiled_method == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001699 oat_method_offsets_offsets_from_oat_class_[i] = 0;
1700 } else {
1701 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
1702 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
1703 if (type_ == kOatClassSomeCompiled) {
1704 method_bitmap_->SetBit(i);
1705 }
1706 }
1707 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001708}
1709
Brian Carlstrom265091e2013-01-30 14:08:26 -08001710size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
1711 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001712 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
1713 if (method_offset == 0) {
1714 return 0;
1715 }
1716 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001717}
1718
1719size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
1720 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001721 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08001722}
1723
1724size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001725 return sizeof(status_)
1726 + sizeof(type_)
1727 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
1728 + method_bitmap_size_
1729 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001730}
1731
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001732bool OatWriter::OatClass::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001733 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001734 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001735 DCHECK_OFFSET_();
Vladimir Marko49b0f452015-12-10 13:49:19 +00001736 if (!oat_writer->WriteData(out, &status_, sizeof(status_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08001737 PLOG(ERROR) << "Failed to write class status to " << out->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001738 return false;
1739 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001740 oat_writer->size_oat_class_status_ += sizeof(status_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001741
1742 if (!oat_writer->WriteData(out, &type_, sizeof(type_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08001743 PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001744 return false;
1745 }
1746 oat_writer->size_oat_class_type_ += sizeof(type_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001747
Brian Carlstromba150c32013-08-27 17:31:03 -07001748 if (method_bitmap_size_ != 0) {
1749 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001750 if (!oat_writer->WriteData(out, &method_bitmap_size_, sizeof(method_bitmap_size_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08001751 PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001752 return false;
1753 }
1754 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001755
1756 if (!oat_writer->WriteData(out, method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001757 PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001758 return false;
1759 }
1760 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1761 }
Vladimir Marko49b0f452015-12-10 13:49:19 +00001762
1763 if (!oat_writer->WriteData(out, method_offsets_.data(), GetMethodOffsetsRawSize())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001764 PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001765 return false;
1766 }
Vladimir Marko49b0f452015-12-10 13:49:19 +00001767 oat_writer->size_oat_class_method_offsets_ += GetMethodOffsetsRawSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001768 return true;
1769}
1770
Brian Carlstrome24fa612011-09-29 00:53:55 -07001771} // namespace art