blob: 025e35e178b48fd16ff42ecbbfb5e58b88badc37 [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_) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800719 const DexFile::TypeId& type_id =
720 dex_file_->GetTypeId(dex_file_->GetClassDef(class_def_index_).class_idx_);
721 const char* class_descriptor = dex_file_->GetTypeDescriptor(type_id);
722 // Skip methods that are not in the image.
723 if (!writer_->GetCompilerDriver()->IsImageClass(class_descriptor)) {
724 return true;
725 }
726
Vladimir Marko49b0f452015-12-10 13:49:19 +0000727 OatClass* oat_class = &writer_->oat_classes_[oat_class_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100728 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
729
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800730 OatMethodOffsets offsets(0u);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100731 if (compiled_method != nullptr) {
732 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
733 offsets = oat_class->method_offsets_[method_offsets_index_];
734 ++method_offsets_index_;
735 }
736
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100737 ClassLinker* linker = Runtime::Current()->GetClassLinker();
738 InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_));
739 // Unchecked as we hold mutator_lock_ on entry.
740 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800741 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700742 Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache(
743 Thread::Current(), *dex_file_)));
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800744 ArtMethod* method = linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
745 *dex_file_,
746 it.GetMemberIndex(),
747 dex_cache,
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800748 ScopedNullHandle<mirror::ClassLoader>(),
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800749 nullptr,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700750 invoke_type);
Andreas Gamped9efea62014-07-21 22:56:08 -0700751 if (method == nullptr) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100752 LOG(INTERNAL_FATAL) << "Unexpected failure to resolve a method: "
753 << PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
Andreas Gamped9efea62014-07-21 22:56:08 -0700754 soa.Self()->AssertPendingException();
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000755 mirror::Throwable* exc = soa.Self()->GetException();
Andreas Gamped9efea62014-07-21 22:56:08 -0700756 std::string dump = exc->Dump();
757 LOG(FATAL) << dump;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100758 UNREACHABLE();
Andreas Gamped9efea62014-07-21 22:56:08 -0700759 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100760
Vladimir Marko35831e82015-09-11 11:59:18 +0100761 if (compiled_method != nullptr && compiled_method->GetQuickCode().size() != 0) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100762 method->SetEntryPointFromQuickCompiledCodePtrSize(
763 reinterpret_cast<void*>(offsets.code_offset_), pointer_size_);
764 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100765
766 return true;
767 }
Jeff Haoc7d11882015-02-03 15:08:39 -0800768
769 protected:
770 const size_t pointer_size_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100771};
772
773class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor {
774 public:
775 WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
Vladimir Markof4da6752014-08-01 19:04:18 +0100776 size_t relative_offset) SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100777 : OatDexMethodVisitor(writer, relative_offset),
778 out_(out),
Vladimir Markof4da6752014-08-01 19:04:18 +0100779 file_offset_(file_offset),
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100780 soa_(Thread::Current()),
781 no_thread_suspension_(soa_.Self(), "OatWriter patching"),
Vladimir Markof4da6752014-08-01 19:04:18 +0100782 class_linker_(Runtime::Current()->GetClassLinker()),
783 dex_cache_(nullptr) {
Vladimir Marko09d09432015-09-08 13:47:48 +0100784 patched_code_.reserve(16 * KB);
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800785 if (writer_->HasBootImage()) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100786 // If we're creating the image, the address space must be ready so that we can apply patches.
787 CHECK(writer_->image_writer_->IsImageAddressSpaceReady());
Vladimir Markof4da6752014-08-01 19:04:18 +0100788 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100789 }
790
Vladimir Markof4da6752014-08-01 19:04:18 +0100791 ~WriteCodeMethodVisitor() UNLOCK_FUNCTION(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100792 }
793
794 bool StartClass(const DexFile* dex_file, size_t class_def_index)
Mathieu Chartier90443472015-07-16 20:32:27 -0700795 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100796 OatDexMethodVisitor::StartClass(dex_file, class_def_index);
797 if (dex_cache_ == nullptr || dex_cache_->GetDexFile() != dex_file) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700798 dex_cache_ = class_linker_->FindDexCache(Thread::Current(), *dex_file);
Vladimir Markof4da6752014-08-01 19:04:18 +0100799 }
800 return true;
801 }
802
Mathieu Chartier90443472015-07-16 20:32:27 -0700803 bool EndClass() SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100804 bool result = OatDexMethodVisitor::EndClass();
805 if (oat_class_index_ == writer_->oat_classes_.size()) {
806 DCHECK(result); // OatDexMethodVisitor::EndClass() never fails.
Vladimir Marko20f85592015-03-19 10:07:02 +0000807 offset_ = writer_->relative_patcher_->WriteThunks(out_, offset_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100808 if (UNLIKELY(offset_ == 0u)) {
809 PLOG(ERROR) << "Failed to write final relative call thunks";
810 result = false;
811 }
812 }
813 return result;
814 }
815
816 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
Mathieu Chartier90443472015-07-16 20:32:27 -0700817 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko49b0f452015-12-10 13:49:19 +0000818 OatClass* oat_class = &writer_->oat_classes_[oat_class_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100819 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
820
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700821 // No thread suspension since dex_cache_ that may get invalidated if that occurs.
822 ScopedAssertNoThreadSuspension tsc(Thread::Current(), __FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700823 if (compiled_method != nullptr) { // ie. not an abstract method
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100824 size_t file_offset = file_offset_;
825 OutputStream* out = out_;
826
Vladimir Marko35831e82015-09-11 11:59:18 +0100827 ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode();
828 uint32_t code_size = quick_code.size() * sizeof(uint8_t);
Nicolas Geoffrayf0758792015-07-13 11:56:00 +0000829
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100830 // Deduplicate code arrays.
831 const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_];
832 if (method_offsets.code_offset_ > offset_) {
833 offset_ = writer_->relative_patcher_->WriteThunks(out, offset_);
834 if (offset_ == 0u) {
835 ReportWriteFailure("relative call thunk", it);
836 return false;
Nicolas Geoffrayf0758792015-07-13 11:56:00 +0000837 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100838 uint32_t aligned_offset = compiled_method->AlignCode(offset_);
839 uint32_t aligned_code_delta = aligned_offset - offset_;
840 if (aligned_code_delta != 0) {
841 if (!writer_->WriteCodeAlignment(out, aligned_code_delta)) {
842 ReportWriteFailure("code alignment padding", it);
843 return false;
844 }
845 offset_ += aligned_code_delta;
846 DCHECK_OFFSET_();
847 }
848 DCHECK_ALIGNED_PARAM(offset_,
849 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
850 DCHECK_EQ(method_offsets.code_offset_,
851 offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta())
852 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
853 const OatQuickMethodHeader& method_header =
854 oat_class->method_headers_[method_offsets_index_];
Vladimir Marko49b0f452015-12-10 13:49:19 +0000855 if (!writer_->WriteData(out, &method_header, sizeof(method_header))) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100856 ReportWriteFailure("method header", it);
857 return false;
858 }
859 writer_->size_method_header_ += sizeof(method_header);
860 offset_ += sizeof(method_header);
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000861 DCHECK_OFFSET_();
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100862
863 if (!compiled_method->GetPatches().empty()) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100864 patched_code_.assign(quick_code.begin(), quick_code.end());
865 quick_code = ArrayRef<const uint8_t>(patched_code_);
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100866 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
867 if (patch.Type() == kLinkerPatchCallRelative) {
868 // NOTE: Relative calls across oat files are not supported.
869 uint32_t target_offset = GetTargetOffset(patch);
870 uint32_t literal_offset = patch.LiteralOffset();
871 writer_->relative_patcher_->PatchCall(&patched_code_, literal_offset,
872 offset_ + literal_offset, target_offset);
873 } else if (patch.Type() == kLinkerPatchDexCacheArray) {
874 uint32_t target_offset = GetDexCacheOffset(patch);
875 uint32_t literal_offset = patch.LiteralOffset();
876 writer_->relative_patcher_->PatchDexCacheReference(&patched_code_, patch,
877 offset_ + literal_offset,
878 target_offset);
879 } else if (patch.Type() == kLinkerPatchCall) {
880 uint32_t target_offset = GetTargetOffset(patch);
881 PatchCodeAddress(&patched_code_, patch.LiteralOffset(), target_offset);
882 } else if (patch.Type() == kLinkerPatchMethod) {
883 ArtMethod* method = GetTargetMethod(patch);
884 PatchMethodAddress(&patched_code_, patch.LiteralOffset(), method);
885 } else if (patch.Type() == kLinkerPatchType) {
886 mirror::Class* type = GetTargetType(patch);
887 PatchObjectAddress(&patched_code_, patch.LiteralOffset(), type);
888 }
889 }
890 }
891
Vladimir Marko49b0f452015-12-10 13:49:19 +0000892 if (!writer_->WriteData(out, quick_code.data(), code_size)) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100893 ReportWriteFailure("method code", it);
894 return false;
895 }
896 writer_->size_code_ += code_size;
897 offset_ += code_size;
Nicolas Geoffrayf0758792015-07-13 11:56:00 +0000898 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100899 DCHECK_OFFSET_();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100900 ++method_offsets_index_;
901 }
902
903 return true;
904 }
905
906 private:
907 OutputStream* const out_;
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100908 const size_t file_offset_;
909 const ScopedObjectAccess soa_;
910 const ScopedAssertNoThreadSuspension no_thread_suspension_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100911 ClassLinker* const class_linker_;
912 mirror::DexCache* dex_cache_;
913 std::vector<uint8_t> patched_code_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100914
915 void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) {
916 PLOG(ERROR) << "Failed to write " << what << " for "
917 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
918 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100919
Mathieu Chartiere401d142015-04-22 13:56:20 -0700920 ArtMethod* GetTargetMethod(const LinkerPatch& patch)
Mathieu Chartier90443472015-07-16 20:32:27 -0700921 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100922 MethodReference ref = patch.TargetMethod();
923 mirror::DexCache* dex_cache =
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700924 (dex_file_ == ref.dex_file) ? dex_cache_ : class_linker_->FindDexCache(
925 Thread::Current(), *ref.dex_file);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700926 ArtMethod* method = dex_cache->GetResolvedMethod(
927 ref.dex_method_index, class_linker_->GetImagePointerSize());
Vladimir Markof4da6752014-08-01 19:04:18 +0100928 CHECK(method != nullptr);
929 return method;
930 }
931
Mathieu Chartier90443472015-07-16 20:32:27 -0700932 uint32_t GetTargetOffset(const LinkerPatch& patch) SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markob163bb72015-03-31 21:49:49 +0100933 auto target_it = writer_->method_offset_map_.map.find(patch.TargetMethod());
Vladimir Markof4da6752014-08-01 19:04:18 +0100934 uint32_t target_offset =
Vladimir Markob163bb72015-03-31 21:49:49 +0100935 (target_it != writer_->method_offset_map_.map.end()) ? target_it->second : 0u;
Vladimir Markof4da6752014-08-01 19:04:18 +0100936 // If there's no compiled code, point to the correct trampoline.
937 if (UNLIKELY(target_offset == 0)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700938 ArtMethod* target = GetTargetMethod(patch);
Vladimir Markof4da6752014-08-01 19:04:18 +0100939 DCHECK(target != nullptr);
Jeff Haoa0acc2d2015-01-27 11:22:04 -0800940 size_t size = GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet());
941 const void* oat_code_offset = target->GetEntryPointFromQuickCompiledCodePtrSize(size);
942 if (oat_code_offset != 0) {
943 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(oat_code_offset));
944 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(oat_code_offset));
945 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(oat_code_offset));
946 target_offset = PointerToLowMemUInt32(oat_code_offset);
947 } else {
948 target_offset = target->IsNative()
949 ? writer_->oat_header_->GetQuickGenericJniTrampolineOffset()
950 : writer_->oat_header_->GetQuickToInterpreterBridgeOffset();
951 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100952 }
953 return target_offset;
954 }
955
956 mirror::Class* GetTargetType(const LinkerPatch& patch)
Mathieu Chartier90443472015-07-16 20:32:27 -0700957 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100958 mirror::DexCache* dex_cache = (dex_file_ == patch.TargetTypeDexFile())
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700959 ? dex_cache_ : class_linker_->FindDexCache(Thread::Current(), *patch.TargetTypeDexFile());
Vladimir Markof4da6752014-08-01 19:04:18 +0100960 mirror::Class* type = dex_cache->GetResolvedType(patch.TargetTypeIndex());
961 CHECK(type != nullptr);
962 return type;
963 }
964
Mathieu Chartier90443472015-07-16 20:32:27 -0700965 uint32_t GetDexCacheOffset(const LinkerPatch& patch) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800966 if (writer_->HasBootImage()) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100967 auto* element = writer_->image_writer_->GetDexCacheArrayElementImageAddress<const uint8_t*>(
Vladimir Marko20f85592015-03-19 10:07:02 +0000968 patch.TargetDexCacheDexFile(), patch.TargetDexCacheElementOffset());
Jeff Haodcdc85b2015-12-04 14:06:18 -0800969 const char* oat_filename = writer_->image_writer_->GetOatFilenameForDexCache(dex_cache_);
970 const uint8_t* oat_data =
971 writer_->image_writer_->GetOatFileBegin(oat_filename) + file_offset_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100972 return element - oat_data;
Vladimir Marko20f85592015-03-19 10:07:02 +0000973 } else {
Vladimir Marko09d09432015-09-08 13:47:48 +0100974 size_t start = writer_->dex_cache_arrays_offsets_.Get(patch.TargetDexCacheDexFile());
975 return start + patch.TargetDexCacheElementOffset();
Vladimir Marko20f85592015-03-19 10:07:02 +0000976 }
977 }
978
Vladimir Markof4da6752014-08-01 19:04:18 +0100979 void PatchObjectAddress(std::vector<uint8_t>* code, uint32_t offset, mirror::Object* object)
Mathieu Chartier90443472015-07-16 20:32:27 -0700980 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800981 if (writer_->HasBootImage()) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100982 object = writer_->image_writer_->GetImageAddress(object);
Vladimir Marko09d09432015-09-08 13:47:48 +0100983 } else {
984 // NOTE: We're using linker patches for app->boot references when the image can
985 // be relocated and therefore we need to emit .oat_patches. We're not using this
986 // for app->app references, so check that the object is in the image space.
987 DCHECK(Runtime::Current()->GetHeap()->FindSpaceFromObject(object, false)->IsImageSpace());
Vladimir Markof4da6752014-08-01 19:04:18 +0100988 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100989 // Note: We only patch targeting Objects in image which is in the low 4gb.
Vladimir Markof4da6752014-08-01 19:04:18 +0100990 uint32_t address = PointerToLowMemUInt32(object);
991 DCHECK_LE(offset + 4, code->size());
992 uint8_t* data = &(*code)[offset];
993 data[0] = address & 0xffu;
994 data[1] = (address >> 8) & 0xffu;
995 data[2] = (address >> 16) & 0xffu;
996 data[3] = (address >> 24) & 0xffu;
997 }
998
Mathieu Chartiere401d142015-04-22 13:56:20 -0700999 void PatchMethodAddress(std::vector<uint8_t>* code, uint32_t offset, ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -07001000 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001001 if (writer_->HasBootImage()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001002 method = writer_->image_writer_->GetImageMethodAddress(method);
Vladimir Marko09d09432015-09-08 13:47:48 +01001003 } else if (kIsDebugBuild) {
1004 // NOTE: We're using linker patches for app->boot references when the image can
1005 // be relocated and therefore we need to emit .oat_patches. We're not using this
1006 // for app->app references, so check that the method is an image method.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001007 std::vector<gc::space::ImageSpace*> image_spaces =
1008 Runtime::Current()->GetHeap()->GetBootImageSpaces();
1009 bool contains_method = false;
1010 for (gc::space::ImageSpace* image_space : image_spaces) {
1011 size_t method_offset = reinterpret_cast<const uint8_t*>(method) - image_space->Begin();
1012 contains_method |=
1013 image_space->GetImageHeader().GetMethodsSection().Contains(method_offset);
1014 }
1015 CHECK(contains_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001016 }
Vladimir Marko09d09432015-09-08 13:47:48 +01001017 // Note: We only patch targeting ArtMethods in image which is in the low 4gb.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001018 uint32_t address = PointerToLowMemUInt32(method);
1019 DCHECK_LE(offset + 4, code->size());
1020 uint8_t* data = &(*code)[offset];
1021 data[0] = address & 0xffu;
1022 data[1] = (address >> 8) & 0xffu;
1023 data[2] = (address >> 16) & 0xffu;
1024 data[3] = (address >> 24) & 0xffu;
1025 }
1026
Vladimir Markof4da6752014-08-01 19:04:18 +01001027 void PatchCodeAddress(std::vector<uint8_t>* code, uint32_t offset, uint32_t target_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -07001028 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko09d09432015-09-08 13:47:48 +01001029 uint32_t address = target_offset;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001030 if (writer_->HasBootImage()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08001031 const char* oat_filename = writer_->image_writer_->GetOatFilenameForDexCache(dex_cache_);
1032 address = PointerToLowMemUInt32(writer_->image_writer_->GetOatFileBegin(oat_filename) +
Vladimir Marko09d09432015-09-08 13:47:48 +01001033 writer_->oat_data_offset_ + target_offset);
1034 }
Vladimir Markof4da6752014-08-01 19:04:18 +01001035 DCHECK_LE(offset + 4, code->size());
1036 uint8_t* data = &(*code)[offset];
1037 data[0] = address & 0xffu;
1038 data[1] = (address >> 8) & 0xffu;
1039 data[2] = (address >> 16) & 0xffu;
1040 data[3] = (address >> 24) & 0xffu;
1041 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001042};
1043
1044template <typename DataAccess>
1045class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor {
1046 public:
1047 WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001048 size_t relative_offset)
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001049 : OatDexMethodVisitor(writer, relative_offset),
1050 out_(out),
1051 file_offset_(file_offset) {
1052 }
1053
1054 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001055 OatClass* oat_class = &writer_->oat_classes_[oat_class_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001056 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
1057
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001058 if (compiled_method != nullptr) { // ie. not an abstract method
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001059 size_t file_offset = file_offset_;
1060 OutputStream* out = out_;
1061
1062 uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_);
1063 ++method_offsets_index_;
1064
1065 // Write deduplicated map.
Vladimir Marko35831e82015-09-11 11:59:18 +01001066 ArrayRef<const uint8_t> map = DataAccess::GetData(compiled_method);
1067 size_t map_size = map.size() * sizeof(map[0]);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001068 DCHECK((map_size == 0u && map_offset == 0u) ||
1069 (map_size != 0u && map_offset != 0u && map_offset <= offset_))
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001070 << map_size << " " << map_offset << " " << offset_ << " "
1071 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " for " << DataAccess::Name();
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001072 if (map_size != 0u && map_offset == offset_) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001073 if (UNLIKELY(!writer_->WriteData(out, map.data(), map_size))) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001074 ReportWriteFailure(it);
1075 return false;
1076 }
1077 offset_ += map_size;
1078 }
1079 DCHECK_OFFSET_();
1080 }
1081
1082 return true;
1083 }
1084
1085 private:
1086 OutputStream* const out_;
1087 size_t const file_offset_;
1088
1089 void ReportWriteFailure(const ClassDataItemIterator& it) {
1090 PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for "
1091 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
1092 }
1093};
1094
1095// Visit all methods from all classes in all dex files with the specified visitor.
1096bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) {
1097 for (const DexFile* dex_file : *dex_files_) {
1098 const size_t class_def_count = dex_file->NumClassDefs();
1099 for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) {
1100 if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) {
1101 return false;
1102 }
1103 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -07001104 const uint8_t* class_data = dex_file->GetClassData(class_def);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001105 if (class_data != nullptr) { // ie not an empty class, such as a marker interface
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001106 ClassDataItemIterator it(*dex_file, class_data);
1107 while (it.HasNextStaticField()) {
1108 it.Next();
1109 }
1110 while (it.HasNextInstanceField()) {
1111 it.Next();
1112 }
1113 size_t class_def_method_index = 0u;
1114 while (it.HasNextDirectMethod()) {
1115 if (!visitor->VisitMethod(class_def_method_index, it)) {
1116 return false;
1117 }
1118 ++class_def_method_index;
1119 it.Next();
1120 }
1121 while (it.HasNextVirtualMethod()) {
1122 if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) {
1123 return false;
1124 }
1125 ++class_def_method_index;
1126 it.Next();
1127 }
1128 }
1129 if (UNLIKELY(!visitor->EndClass())) {
1130 return false;
1131 }
1132 }
1133 }
1134 return true;
1135}
1136
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001137size_t OatWriter::InitOatHeader() {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001138 oat_header_.reset(OatHeader::Create(compiler_driver_->GetInstructionSet(),
1139 compiler_driver_->GetInstructionSetFeatures(),
1140 dchecked_integral_cast<uint32_t>(dex_files_->size()),
1141 key_value_store_));
1142 oat_header_->SetImageFileLocationOatChecksum(image_file_location_oat_checksum_);
1143 oat_header_->SetImageFileLocationOatDataBegin(image_file_location_oat_begin_);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001144
1145 return oat_header_->GetHeaderSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001146}
1147
1148size_t OatWriter::InitOatDexFiles(size_t offset) {
1149 // create the OatDexFiles
1150 for (size_t i = 0; i != dex_files_->size(); ++i) {
1151 const DexFile* dex_file = (*dex_files_)[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001152 CHECK(dex_file != nullptr);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001153 oat_dex_files_.emplace_back(offset, *dex_file);
1154 offset += oat_dex_files_.back().SizeOf();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001155 }
1156 return offset;
1157}
1158
Brian Carlstrom89521892011-12-07 22:05:07 -08001159size_t OatWriter::InitDexFiles(size_t offset) {
1160 // calculate the offsets within OatDexFiles to the DexFiles
1161 for (size_t i = 0; i != dex_files_->size(); ++i) {
1162 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001163 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -08001164 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001165 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -08001166
1167 // set offset in OatDexFile to DexFile
Vladimir Marko49b0f452015-12-10 13:49:19 +00001168 oat_dex_files_[i].dex_file_offset_ = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -08001169
1170 const DexFile* dex_file = (*dex_files_)[i];
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001171
1172 // Initialize type lookup table
Vladimir Marko49b0f452015-12-10 13:49:19 +00001173 oat_dex_files_[i].lookup_table_ = dex_file->GetTypeLookupTable();
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001174
Brian Carlstrom89521892011-12-07 22:05:07 -08001175 offset += dex_file->GetHeader().file_size_;
1176 }
1177 return offset;
1178}
1179
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001180size_t OatWriter::InitLookupTables(size_t offset) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001181 for (OatDexFile& oat_dex_file : oat_dex_files_) {
1182 if (oat_dex_file.lookup_table_ != nullptr) {
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001183 uint32_t aligned_offset = RoundUp(offset, 4);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001184 oat_dex_file.lookup_table_offset_ = aligned_offset;
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001185 size_oat_lookup_table_alignment_ += aligned_offset - offset;
Vladimir Marko49b0f452015-12-10 13:49:19 +00001186 offset = aligned_offset + oat_dex_file.lookup_table_->RawDataLength();
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001187 } else {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001188 oat_dex_file.lookup_table_offset_ = 0;
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001189 }
1190 }
1191 return offset;
1192}
1193
Brian Carlstrom389efb02012-01-11 12:06:26 -08001194size_t OatWriter::InitOatClasses(size_t offset) {
Brian Carlstrom389efb02012-01-11 12:06:26 -08001195 // calculate the offsets within OatDexFiles to OatClasses
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001196 InitOatClassesMethodVisitor visitor(this, offset);
1197 bool success = VisitDexMethods(&visitor);
1198 CHECK(success);
1199 offset = visitor.GetOffset();
Brian Carlstromba150c32013-08-27 17:31:03 -07001200
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001201 // Update oat_dex_files_.
1202 auto oat_class_it = oat_classes_.begin();
Vladimir Marko49b0f452015-12-10 13:49:19 +00001203 for (OatDexFile& oat_dex_file : oat_dex_files_) {
1204 for (uint32_t& class_offset : oat_dex_file.class_offsets_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001205 DCHECK(oat_class_it != oat_classes_.end());
Vladimir Marko49b0f452015-12-10 13:49:19 +00001206 class_offset = oat_class_it->offset_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001207 ++oat_class_it;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001208 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001209 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001210 CHECK(oat_class_it == oat_classes_.end());
1211
1212 return offset;
1213}
1214
1215size_t OatWriter::InitOatMaps(size_t offset) {
1216 #define VISIT(VisitorType) \
1217 do { \
1218 VisitorType visitor(this, offset); \
1219 bool success = VisitDexMethods(&visitor); \
1220 DCHECK(success); \
1221 offset = visitor.GetOffset(); \
1222 } while (false)
1223
1224 VISIT(InitMapMethodVisitor<GcMapDataAccess>);
1225 VISIT(InitMapMethodVisitor<MappingTableDataAccess>);
1226 VISIT(InitMapMethodVisitor<VmapTableDataAccess>);
1227
1228 #undef VISIT
1229
Brian Carlstrome24fa612011-09-29 00:53:55 -07001230 return offset;
1231}
1232
1233size_t OatWriter::InitOatCode(size_t offset) {
1234 // calculate the offsets within OatHeader to executable code
1235 size_t old_offset = offset;
Dave Allison50abf0a2014-06-23 13:19:59 -07001236 size_t adjusted_offset = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001237 // required to be on a new page boundary
1238 offset = RoundUp(offset, kPageSize);
1239 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001240 size_executable_offset_alignment_ = offset - old_offset;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001241 if (compiler_driver_->IsBootImage()) {
Alex Lighta59dd802014-07-02 16:28:08 -07001242 CHECK_EQ(image_patch_delta_, 0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001243 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001244
Ian Rogers848871b2013-08-05 10:56:33 -07001245 #define DO_TRAMPOLINE(field, fn_name) \
1246 offset = CompiledCode::AlignCode(offset, instruction_set); \
Dave Allison50abf0a2014-06-23 13:19:59 -07001247 adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \
1248 oat_header_->Set ## fn_name ## Offset(adjusted_offset); \
Ian Rogers848871b2013-08-05 10:56:33 -07001249 field.reset(compiler_driver_->Create ## fn_name()); \
1250 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001251
Ian Rogers848871b2013-08-05 10:56:33 -07001252 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Andreas Gampe2da88232014-02-27 12:26:20 -08001253 DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline);
Jeff Hao88474b42013-10-23 16:24:40 -07001254 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -07001255 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
1256 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001257
Ian Rogers848871b2013-08-05 10:56:33 -07001258 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001259 } else {
Ian Rogers848871b2013-08-05 10:56:33 -07001260 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
1261 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
1262 oat_header_->SetJniDlsymLookupOffset(0);
Andreas Gampe2da88232014-02-27 12:26:20 -08001263 oat_header_->SetQuickGenericJniTrampolineOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -07001264 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001265 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -07001266 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Alex Lighta59dd802014-07-02 16:28:08 -07001267 oat_header_->SetImagePatchDelta(image_patch_delta_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001268 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001269 return offset;
1270}
1271
1272size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001273 #define VISIT(VisitorType) \
1274 do { \
1275 VisitorType visitor(this, offset); \
1276 bool success = VisitDexMethods(&visitor); \
1277 DCHECK(success); \
1278 offset = visitor.GetOffset(); \
1279 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001280
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001281 VISIT(InitCodeMethodVisitor);
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001282 if (compiler_driver_->IsBootImage()) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001283 VISIT(InitImageMethodVisitor);
Ian Rogers0571d352011-11-03 19:51:38 -07001284 }
Logan Chien8b977d32012-02-21 19:14:55 +08001285
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001286 #undef VISIT
1287
Brian Carlstrome24fa612011-09-29 00:53:55 -07001288 return offset;
1289}
1290
David Srbeckybc90fd02015-04-22 19:40:27 +01001291bool OatWriter::WriteRodata(OutputStream* out) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001292 if (!GetOatDataOffset(out)) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001293 return false;
1294 }
Vladimir Marko49b0f452015-12-10 13:49:19 +00001295 const size_t file_offset = oat_data_offset_;
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001296
Vladimir Markof4da6752014-08-01 19:04:18 +01001297 // Reserve space for header. It will be written last - after updating the checksum.
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001298 size_t header_size = oat_header_->GetHeaderSize();
Vladimir Marko49b0f452015-12-10 13:49:19 +00001299 if (out->Seek(header_size, kSeekCurrent) == static_cast<off_t>(-1)) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001300 PLOG(ERROR) << "Failed to reserve space for oat header in " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001301 return false;
1302 }
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001303 size_oat_header_ += sizeof(OatHeader);
1304 size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001305
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001306 if (!WriteTables(out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001307 LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001308 return false;
1309 }
1310
Vladimir Markof4da6752014-08-01 19:04:18 +01001311 off_t tables_end_offset = out->Seek(0, kSeekCurrent);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001312 if (tables_end_offset == static_cast<off_t>(-1)) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001313 LOG(ERROR) << "Failed to seek to oat code position in " << out->GetLocation();
1314 return false;
1315 }
1316 size_t relative_offset = static_cast<size_t>(tables_end_offset) - file_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001317 relative_offset = WriteMaps(out, file_offset, relative_offset);
1318 if (relative_offset == 0) {
1319 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
1320 return false;
1321 }
1322
David Srbeckybc90fd02015-04-22 19:40:27 +01001323 // Write padding.
1324 off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent);
1325 relative_offset += size_executable_offset_alignment_;
1326 DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset());
1327 size_t expected_file_offset = file_offset + relative_offset;
1328 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
1329 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
1330 << " Expected: " << expected_file_offset << " File: " << out->GetLocation();
1331 return 0;
1332 }
1333 DCHECK_OFFSET();
1334
1335 return true;
1336}
1337
1338bool OatWriter::WriteCode(OutputStream* out) {
1339 size_t header_size = oat_header_->GetHeaderSize();
1340 const size_t file_offset = oat_data_offset_;
1341 size_t relative_offset = oat_header_->GetExecutableOffset();
1342 DCHECK_OFFSET();
1343
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001344 relative_offset = WriteCode(out, file_offset, relative_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001345 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001346 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001347 return false;
1348 }
1349
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001350 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
1351 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001352 LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001353 return false;
1354 }
1355
Vladimir Markof4da6752014-08-01 19:04:18 +01001356 const off_t oat_end_file_offset = out->Seek(0, kSeekCurrent);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001357 if (oat_end_file_offset == static_cast<off_t>(-1)) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001358 LOG(ERROR) << "Failed to get oat end file offset in " << out->GetLocation();
1359 return false;
1360 }
1361
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001362 if (kIsDebugBuild) {
1363 uint32_t size_total = 0;
1364 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001365 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001366 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001367
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001368 DO_STAT(size_dex_file_alignment_);
1369 DO_STAT(size_executable_offset_alignment_);
1370 DO_STAT(size_oat_header_);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001371 DO_STAT(size_oat_header_key_value_store_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001372 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -07001373 DO_STAT(size_interpreter_to_interpreter_bridge_);
1374 DO_STAT(size_interpreter_to_compiled_code_bridge_);
1375 DO_STAT(size_jni_dlsym_lookup_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001376 DO_STAT(size_quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001377 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001378 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001379 DO_STAT(size_quick_to_interpreter_bridge_);
1380 DO_STAT(size_trampoline_alignment_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001381 DO_STAT(size_method_header_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001382 DO_STAT(size_code_);
1383 DO_STAT(size_code_alignment_);
Vladimir Markof4da6752014-08-01 19:04:18 +01001384 DO_STAT(size_relative_call_thunks_);
Vladimir Markoc74658b2015-03-31 10:26:41 +01001385 DO_STAT(size_misc_thunks_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001386 DO_STAT(size_mapping_table_);
1387 DO_STAT(size_vmap_table_);
1388 DO_STAT(size_gc_map_);
1389 DO_STAT(size_oat_dex_file_location_size_);
1390 DO_STAT(size_oat_dex_file_location_data_);
1391 DO_STAT(size_oat_dex_file_location_checksum_);
1392 DO_STAT(size_oat_dex_file_offset_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001393 DO_STAT(size_oat_dex_file_lookup_table_offset_);
1394 DO_STAT(size_oat_dex_file_class_offsets_);
1395 DO_STAT(size_oat_lookup_table_alignment_);
1396 DO_STAT(size_oat_lookup_table_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001397 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001398 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001399 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001400 DO_STAT(size_oat_class_method_offsets_);
1401 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001402
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001403 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Vladimir Markof4da6752014-08-01 19:04:18 +01001404 CHECK_EQ(file_offset + size_total, static_cast<size_t>(oat_end_file_offset));
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001405 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001406 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001407
Vladimir Markof4da6752014-08-01 19:04:18 +01001408 CHECK_EQ(file_offset + size_, static_cast<size_t>(oat_end_file_offset));
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001409 CHECK_EQ(size_, relative_offset);
1410
Vladimir Marko49b0f452015-12-10 13:49:19 +00001411 // Finalize the header checksum.
1412 oat_header_->UpdateChecksumWithHeaderData();
1413
Vladimir Markof4da6752014-08-01 19:04:18 +01001414 // Write the header now that the checksum is final.
Vladimir Marko49b0f452015-12-10 13:49:19 +00001415 if (out->Seek(file_offset, kSeekSet) == static_cast<off_t>(-1)) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001416 PLOG(ERROR) << "Failed to seek to oat header position in " << out->GetLocation();
1417 return false;
1418 }
David Srbeckybc90fd02015-04-22 19:40:27 +01001419 DCHECK_EQ(file_offset, static_cast<size_t>(out->Seek(0, kSeekCurrent)));
Vladimir Marko49b0f452015-12-10 13:49:19 +00001420 if (!out->WriteFully(oat_header_.get(), header_size)) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001421 PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation();
1422 return false;
1423 }
Vladimir Marko49b0f452015-12-10 13:49:19 +00001424 if (out->Seek(oat_end_file_offset, kSeekSet) == static_cast<off_t>(-1)) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001425 PLOG(ERROR) << "Failed to seek to end after writing oat header to " << out->GetLocation();
1426 return false;
1427 }
1428 DCHECK_EQ(oat_end_file_offset, out->Seek(0, kSeekCurrent));
1429
Brian Carlstrome24fa612011-09-29 00:53:55 -07001430 return true;
1431}
1432
Ian Rogers3d504072014-03-01 09:16:49 -08001433bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001434 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001435 if (!oat_dex_files_[i].Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001436 PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001437 return false;
1438 }
1439 }
Brian Carlstrom89521892011-12-07 22:05:07 -08001440 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001441 uint32_t expected_offset = file_offset + oat_dex_files_[i].dex_file_offset_;
Ian Rogers3d504072014-03-01 09:16:49 -08001442 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -08001443 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
1444 const DexFile* dex_file = (*dex_files_)[i];
1445 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
1446 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
1447 return false;
1448 }
1449 const DexFile* dex_file = (*dex_files_)[i];
Ian Rogers3d504072014-03-01 09:16:49 -08001450 if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001451 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
Ian Rogers3d504072014-03-01 09:16:49 -08001452 << " to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001453 return false;
1454 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001455 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -08001456 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001457 if (!WriteLookupTables(out, file_offset)) {
1458 return false;
1459 }
Brian Carlstrom389efb02012-01-11 12:06:26 -08001460 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001461 if (!oat_classes_[i].Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001462 PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001463 return false;
1464 }
1465 }
1466 return true;
1467}
1468
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001469bool OatWriter::WriteLookupTables(OutputStream* out, const size_t file_offset) {
1470 for (size_t i = 0; i < oat_dex_files_.size(); ++i) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001471 const uint32_t lookup_table_offset = oat_dex_files_[i].lookup_table_offset_;
1472 const TypeLookupTable* table = oat_dex_files_[i].lookup_table_;
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001473 DCHECK_EQ(lookup_table_offset == 0, table == nullptr);
1474 if (lookup_table_offset == 0) {
1475 continue;
1476 }
1477 const uint32_t expected_offset = file_offset + lookup_table_offset;
1478 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
1479 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
1480 const DexFile* dex_file = (*dex_files_)[i];
1481 PLOG(ERROR) << "Failed to seek to lookup table section. Actual: " << actual_offset
1482 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
1483 return false;
1484 }
1485 if (table != nullptr) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001486 if (!WriteData(out, table->RawData(), table->RawDataLength())) {
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001487 const DexFile* dex_file = (*dex_files_)[i];
1488 PLOG(ERROR) << "Failed to write lookup table for " << dex_file->GetLocation()
1489 << " to " << out->GetLocation();
1490 return false;
1491 }
1492 size_oat_lookup_table_ += table->RawDataLength();
1493 }
1494 }
1495 return true;
1496}
1497
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001498size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) {
1499 #define VISIT(VisitorType) \
1500 do { \
1501 VisitorType visitor(this, out, file_offset, relative_offset); \
1502 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1503 return 0; \
1504 } \
1505 relative_offset = visitor.GetOffset(); \
1506 } while (false)
1507
1508 size_t gc_maps_offset = relative_offset;
1509 VISIT(WriteMapMethodVisitor<GcMapDataAccess>);
1510 size_gc_map_ = relative_offset - gc_maps_offset;
1511
1512 size_t mapping_tables_offset = relative_offset;
1513 VISIT(WriteMapMethodVisitor<MappingTableDataAccess>);
1514 size_mapping_table_ = relative_offset - mapping_tables_offset;
1515
1516 size_t vmap_tables_offset = relative_offset;
1517 VISIT(WriteMapMethodVisitor<VmapTableDataAccess>);
1518 size_vmap_table_ = relative_offset - vmap_tables_offset;
1519
1520 #undef VISIT
1521
1522 return relative_offset;
1523}
1524
1525size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001526 if (compiler_driver_->IsBootImage()) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001527 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001528
Ian Rogers848871b2013-08-05 10:56:33 -07001529 #define DO_TRAMPOLINE(field) \
1530 do { \
1531 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
1532 uint32_t alignment_padding = aligned_offset - relative_offset; \
Ian Rogers3d504072014-03-01 09:16:49 -08001533 out->Seek(alignment_padding, kSeekCurrent); \
Ian Rogers848871b2013-08-05 10:56:33 -07001534 size_trampoline_alignment_ += alignment_padding; \
Vladimir Marko49b0f452015-12-10 13:49:19 +00001535 if (!WriteData(out, field->data(), field->size())) { \
Ian Rogers3d504072014-03-01 09:16:49 -08001536 PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \
Ian Rogers848871b2013-08-05 10:56:33 -07001537 return false; \
1538 } \
1539 size_ ## field += field->size(); \
1540 relative_offset += alignment_padding + field->size(); \
1541 DCHECK_OFFSET(); \
1542 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001543
Ian Rogers848871b2013-08-05 10:56:33 -07001544 DO_TRAMPOLINE(jni_dlsym_lookup_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001545 DO_TRAMPOLINE(quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001546 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001547 DO_TRAMPOLINE(quick_resolution_trampoline_);
1548 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
1549 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001550 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001551 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001552}
1553
Ian Rogers3d504072014-03-01 09:16:49 -08001554size_t OatWriter::WriteCodeDexFiles(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001555 const size_t file_offset,
1556 size_t relative_offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001557 #define VISIT(VisitorType) \
1558 do { \
1559 VisitorType visitor(this, out, file_offset, relative_offset); \
1560 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1561 return 0; \
1562 } \
1563 relative_offset = visitor.GetOffset(); \
1564 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001565
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001566 VISIT(WriteCodeMethodVisitor);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001567
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001568 #undef VISIT
Brian Carlstrom265091e2013-01-30 14:08:26 -08001569
Vladimir Markob163bb72015-03-31 21:49:49 +01001570 size_code_alignment_ += relative_patcher_->CodeAlignmentSize();
1571 size_relative_call_thunks_ += relative_patcher_->RelativeCallThunksSize();
1572 size_misc_thunks_ += relative_patcher_->MiscThunksSize();
1573
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001574 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001575}
1576
Vladimir Marko49b0f452015-12-10 13:49:19 +00001577bool OatWriter::GetOatDataOffset(OutputStream* out) {
1578 // Get the elf file offset of the oat file.
1579 const off_t raw_file_offset = out->Seek(0, kSeekCurrent);
1580 if (raw_file_offset == static_cast<off_t>(-1)) {
1581 LOG(ERROR) << "Failed to get file offset in " << out->GetLocation();
1582 return false;
1583 }
1584 oat_data_offset_ = static_cast<size_t>(raw_file_offset);
1585 return true;
1586}
1587
Vladimir Markof4da6752014-08-01 19:04:18 +01001588bool OatWriter::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) {
1589 static const uint8_t kPadding[] = {
1590 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
1591 };
1592 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
1593 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
1594 return false;
1595 }
1596 size_code_alignment_ += aligned_code_delta;
1597 return true;
1598}
1599
Vladimir Marko49b0f452015-12-10 13:49:19 +00001600bool OatWriter::WriteData(OutputStream* out, const void* data, size_t size) {
1601 oat_header_->UpdateChecksum(data, size);
1602 return out->WriteFully(data, size);
1603}
1604
Vladimir Markob163bb72015-03-31 21:49:49 +01001605std::pair<bool, uint32_t> OatWriter::MethodOffsetMap::FindMethodOffset(MethodReference ref) {
1606 auto it = map.find(ref);
1607 if (it == map.end()) {
1608 return std::pair<bool, uint32_t>(false, 0u);
1609 } else {
1610 return std::pair<bool, uint32_t>(true, it->second);
1611 }
1612}
1613
Brian Carlstrom265091e2013-01-30 14:08:26 -08001614OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
1615 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -08001616 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001617 dex_file_location_size_ = location.size();
1618 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001619 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -08001620 dex_file_offset_ = 0;
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001621 lookup_table_offset_ = 0;
Vladimir Marko49b0f452015-12-10 13:49:19 +00001622 class_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001623}
1624
1625size_t OatWriter::OatDexFile::SizeOf() const {
1626 return sizeof(dex_file_location_size_)
1627 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001628 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -08001629 + sizeof(dex_file_offset_)
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001630 + sizeof(lookup_table_offset_)
Vladimir Marko49b0f452015-12-10 13:49:19 +00001631 + (sizeof(class_offsets_[0]) * class_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001632}
1633
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001634bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001635 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001636 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001637 DCHECK_OFFSET_();
Vladimir Marko49b0f452015-12-10 13:49:19 +00001638 if (!oat_writer->WriteData(out, &dex_file_location_size_, sizeof(dex_file_location_size_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08001639 PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001640 return false;
1641 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001642 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001643 if (!oat_writer->WriteData(out, dex_file_location_data_, dex_file_location_size_)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001644 PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001645 return false;
1646 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001647 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Vladimir Marko49b0f452015-12-10 13:49:19 +00001648 if (!oat_writer->WriteData(out,
1649 &dex_file_location_checksum_,
1650 sizeof(dex_file_location_checksum_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08001651 PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001652 return false;
1653 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001654 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001655 if (!oat_writer->WriteData(out, &dex_file_offset_, sizeof(dex_file_offset_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08001656 PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001657 return false;
1658 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001659 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001660 if (!oat_writer->WriteData(out, &lookup_table_offset_, sizeof(lookup_table_offset_))) {
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001661 PLOG(ERROR) << "Failed to write lookup table offset to " << out->GetLocation();
1662 return false;
1663 }
Vladimir Marko49b0f452015-12-10 13:49:19 +00001664 oat_writer->size_oat_dex_file_lookup_table_offset_ += sizeof(lookup_table_offset_);
1665 if (!oat_writer->WriteData(out, class_offsets_.data(), GetClassOffsetsRawSize())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001666 PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001667 return false;
1668 }
Vladimir Marko49b0f452015-12-10 13:49:19 +00001669 oat_writer->size_oat_dex_file_class_offsets_ += GetClassOffsetsRawSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001670 return true;
1671}
1672
Brian Carlstromba150c32013-08-27 17:31:03 -07001673OatWriter::OatClass::OatClass(size_t offset,
Vladimir Marko49b0f452015-12-10 13:49:19 +00001674 const dchecked_vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -07001675 uint32_t num_non_null_compiled_methods,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001676 mirror::Class::Status status)
1677 : compiled_methods_(compiled_methods) {
1678 uint32_t num_methods = compiled_methods.size();
Brian Carlstromba150c32013-08-27 17:31:03 -07001679 CHECK_LE(num_non_null_compiled_methods, num_methods);
1680
Brian Carlstrom265091e2013-01-30 14:08:26 -08001681 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -07001682 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
1683
1684 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
1685 // apply when there are 0 methods, we just arbitrarily say that 0
1686 // methods means kOatClassNoneCompiled and that we won't use
1687 // kOatClassAllCompiled unless there is at least one compiled
1688 // method. This means in an interpretter only system, we can assert
1689 // that all classes are kOatClassNoneCompiled.
1690 if (num_non_null_compiled_methods == 0) {
1691 type_ = kOatClassNoneCompiled;
1692 } else if (num_non_null_compiled_methods == num_methods) {
1693 type_ = kOatClassAllCompiled;
1694 } else {
1695 type_ = kOatClassSomeCompiled;
1696 }
1697
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001698 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -07001699 method_offsets_.resize(num_non_null_compiled_methods);
Vladimir Marko8a630572014-04-09 18:45:35 +01001700 method_headers_.resize(num_non_null_compiled_methods);
Brian Carlstromba150c32013-08-27 17:31:03 -07001701
1702 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
1703 if (type_ == kOatClassSomeCompiled) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001704 method_bitmap_.reset(new BitVector(num_methods, false, Allocator::GetMallocAllocator()));
Brian Carlstromba150c32013-08-27 17:31:03 -07001705 method_bitmap_size_ = method_bitmap_->GetSizeOf();
1706 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
1707 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
1708 } else {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001709 method_bitmap_ = nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001710 method_bitmap_size_ = 0;
1711 }
1712
1713 for (size_t i = 0; i < num_methods; i++) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001714 CompiledMethod* compiled_method = compiled_methods_[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001715 if (compiled_method == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001716 oat_method_offsets_offsets_from_oat_class_[i] = 0;
1717 } else {
1718 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
1719 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
1720 if (type_ == kOatClassSomeCompiled) {
1721 method_bitmap_->SetBit(i);
1722 }
1723 }
1724 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001725}
1726
Brian Carlstrom265091e2013-01-30 14:08:26 -08001727size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
1728 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001729 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
1730 if (method_offset == 0) {
1731 return 0;
1732 }
1733 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001734}
1735
1736size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
1737 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001738 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08001739}
1740
1741size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001742 return sizeof(status_)
1743 + sizeof(type_)
1744 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
1745 + method_bitmap_size_
1746 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001747}
1748
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001749bool OatWriter::OatClass::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001750 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001751 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001752 DCHECK_OFFSET_();
Vladimir Marko49b0f452015-12-10 13:49:19 +00001753 if (!oat_writer->WriteData(out, &status_, sizeof(status_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08001754 PLOG(ERROR) << "Failed to write class status to " << out->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001755 return false;
1756 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001757 oat_writer->size_oat_class_status_ += sizeof(status_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001758
1759 if (!oat_writer->WriteData(out, &type_, sizeof(type_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08001760 PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001761 return false;
1762 }
1763 oat_writer->size_oat_class_type_ += sizeof(type_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001764
Brian Carlstromba150c32013-08-27 17:31:03 -07001765 if (method_bitmap_size_ != 0) {
1766 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001767 if (!oat_writer->WriteData(out, &method_bitmap_size_, sizeof(method_bitmap_size_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08001768 PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001769 return false;
1770 }
1771 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001772
1773 if (!oat_writer->WriteData(out, method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001774 PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001775 return false;
1776 }
1777 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1778 }
Vladimir Marko49b0f452015-12-10 13:49:19 +00001779
1780 if (!oat_writer->WriteData(out, method_offsets_.data(), GetMethodOffsetsRawSize())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001781 PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001782 return false;
1783 }
Vladimir Marko49b0f452015-12-10 13:49:19 +00001784 oat_writer->size_oat_class_method_offsets_ += GetMethodOffsetsRawSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001785 return true;
1786}
1787
Brian Carlstrome24fa612011-09-29 00:53:55 -07001788} // namespace art