blob: 44c26edd715bb146df764fbce367cac812b1e23b [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
Vladimir Marko9bdf1082016-01-21 12:15:52 +000019#include <unistd.h>
Elliott Hughesa0e18062012-04-13 15:59:59 -070020#include <zlib.h>
21
Vladimir Markoc74658b2015-03-31 10:26:41 +010022#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070023#include "art_method-inl.h"
Ian Rogerse77493c2014-08-20 15:08:45 -070024#include "base/allocator.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070025#include "base/bit_vector.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070026#include "base/enums.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000027#include "base/file_magic.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080028#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080029#include "base/unix_file/fd_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070030#include "class_linker.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070031#include "compiled_class.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000032#include "compiled_method.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000033#include "debug/method_debug_info.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000034#include "dex/verification_results.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000035#include "dex_file-inl.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000036#include "driver/compiler_driver.h"
37#include "driver/compiler_options.h"
Vladimir Marko09d09432015-09-08 13:47:48 +010038#include "gc/space/image_space.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070039#include "gc/space/space.h"
Artem Udovichenkod9786b02015-10-14 16:36:55 +030040#include "handle_scope-inl.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010041#include "image_writer.h"
David Brazdil7b49e6c2016-09-01 11:06:18 +010042#include "linker/buffered_output_stream.h"
43#include "linker/file_output_stream.h"
Vladimir Marko944da602016-02-19 12:27:55 +000044#include "linker/multi_oat_relative_patcher.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000045#include "linker/output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046#include "mirror/array.h"
47#include "mirror/class_loader.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010048#include "mirror/dex_cache-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070049#include "mirror/object-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010050#include "oat_quick_method_header.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070051#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070052#include "safe_map.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070053#include "scoped_thread_state_change-inl.h"
Artem Udovichenkod9786b02015-10-14 16:36:55 +030054#include "type_lookup_table.h"
Vladimir Marko09d09432015-09-08 13:47:48 +010055#include "utils/dex_cache_arrays_layout-inl.h"
David Brazdil7b49e6c2016-09-01 11:06:18 +010056#include "vdex_file.h"
jeffhaoec014232012-09-05 10:42:25 -070057#include "verifier/method_verifier.h"
David Brazdil5d5a36b2016-09-14 15:34:10 +010058#include "verifier/verifier_deps.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000059#include "zip_archive.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070060
61namespace art {
62
Vladimir Marko9bdf1082016-01-21 12:15:52 +000063namespace { // anonymous namespace
64
65typedef DexFile::Header __attribute__((aligned(1))) UnalignedDexFileHeader;
66
67const UnalignedDexFileHeader* AsUnalignedDexFileHeader(const uint8_t* raw_data) {
68 return reinterpret_cast<const UnalignedDexFileHeader*>(raw_data);
69}
70
Vladimir Markoe079e212016-05-25 12:49:49 +010071class ChecksumUpdatingOutputStream : public OutputStream {
72 public:
73 ChecksumUpdatingOutputStream(OutputStream* out, OatHeader* oat_header)
74 : OutputStream(out->GetLocation()), out_(out), oat_header_(oat_header) { }
75
76 bool WriteFully(const void* buffer, size_t byte_count) OVERRIDE {
77 oat_header_->UpdateChecksum(buffer, byte_count);
78 return out_->WriteFully(buffer, byte_count);
79 }
80
81 off_t Seek(off_t offset, Whence whence) OVERRIDE {
82 return out_->Seek(offset, whence);
83 }
84
85 bool Flush() OVERRIDE {
86 return out_->Flush();
87 }
88
89 private:
90 OutputStream* const out_;
91 OatHeader* const oat_header_;
92};
93
Vladimir Marko0c737df2016-08-01 16:33:16 +010094inline uint32_t CodeAlignmentSize(uint32_t header_offset, const CompiledMethod& compiled_method) {
95 // We want to align the code rather than the preheader.
96 uint32_t unaligned_code_offset = header_offset + sizeof(OatQuickMethodHeader);
97 uint32_t aligned_code_offset = compiled_method.AlignCode(unaligned_code_offset);
98 return aligned_code_offset - unaligned_code_offset;
99}
100
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000101} // anonymous namespace
102
103// Defines the location of the raw dex file to write.
104class OatWriter::DexFileSource {
105 public:
106 explicit DexFileSource(ZipEntry* zip_entry)
107 : type_(kZipEntry), source_(zip_entry) {
108 DCHECK(source_ != nullptr);
109 }
110
111 explicit DexFileSource(File* raw_file)
112 : type_(kRawFile), source_(raw_file) {
113 DCHECK(source_ != nullptr);
114 }
115
116 explicit DexFileSource(const uint8_t* dex_file)
117 : type_(kRawData), source_(dex_file) {
118 DCHECK(source_ != nullptr);
119 }
120
121 bool IsZipEntry() const { return type_ == kZipEntry; }
122 bool IsRawFile() const { return type_ == kRawFile; }
123 bool IsRawData() const { return type_ == kRawData; }
124
125 ZipEntry* GetZipEntry() const {
126 DCHECK(IsZipEntry());
127 DCHECK(source_ != nullptr);
128 return static_cast<ZipEntry*>(const_cast<void*>(source_));
129 }
130
131 File* GetRawFile() const {
132 DCHECK(IsRawFile());
133 DCHECK(source_ != nullptr);
134 return static_cast<File*>(const_cast<void*>(source_));
135 }
136
137 const uint8_t* GetRawData() const {
138 DCHECK(IsRawData());
139 DCHECK(source_ != nullptr);
140 return static_cast<const uint8_t*>(source_);
141 }
142
143 void Clear() {
144 type_ = kNone;
145 source_ = nullptr;
146 }
147
148 private:
149 enum Type {
150 kNone,
151 kZipEntry,
152 kRawFile,
153 kRawData,
154 };
155
156 Type type_;
157 const void* source_;
158};
159
Vladimir Marko49b0f452015-12-10 13:49:19 +0000160class OatWriter::OatClass {
161 public:
162 OatClass(size_t offset,
163 const dchecked_vector<CompiledMethod*>& compiled_methods,
164 uint32_t num_non_null_compiled_methods,
165 mirror::Class::Status status);
166 OatClass(OatClass&& src) = default;
167 size_t GetOatMethodOffsetsOffsetFromOatHeader(size_t class_def_method_index_) const;
168 size_t GetOatMethodOffsetsOffsetFromOatClass(size_t class_def_method_index_) const;
169 size_t SizeOf() const;
170 bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
171
172 CompiledMethod* GetCompiledMethod(size_t class_def_method_index) const {
173 return compiled_methods_[class_def_method_index];
174 }
175
176 // Offset of start of OatClass from beginning of OatHeader. It is
177 // used to validate file position when writing.
178 size_t offset_;
179
180 // CompiledMethods for each class_def_method_index, or null if no method is available.
181 dchecked_vector<CompiledMethod*> compiled_methods_;
182
183 // Offset from OatClass::offset_ to the OatMethodOffsets for the
184 // class_def_method_index. If 0, it means the corresponding
185 // CompiledMethod entry in OatClass::compiled_methods_ should be
186 // null and that the OatClass::type_ should be kOatClassBitmap.
187 dchecked_vector<uint32_t> oat_method_offsets_offsets_from_oat_class_;
188
189 // Data to write.
190
191 static_assert(mirror::Class::Status::kStatusMax < (1 << 16), "class status won't fit in 16bits");
192 int16_t status_;
193
194 static_assert(OatClassType::kOatClassMax < (1 << 16), "oat_class type won't fit in 16bits");
195 uint16_t type_;
196
197 uint32_t method_bitmap_size_;
198
199 // bit vector indexed by ClassDef method index. When
200 // OatClassType::type_ is kOatClassBitmap, a set bit indicates the
201 // method has an OatMethodOffsets in methods_offsets_, otherwise
202 // the entry was ommited to save space. If OatClassType::type_ is
203 // not is kOatClassBitmap, the bitmap will be null.
204 std::unique_ptr<BitVector> method_bitmap_;
205
206 // OatMethodOffsets and OatMethodHeaders for each CompiledMethod
207 // present in the OatClass. Note that some may be missing if
208 // OatClass::compiled_methods_ contains null values (and
209 // oat_method_offsets_offsets_from_oat_class_ should contain 0
210 // values in this case).
211 dchecked_vector<OatMethodOffsets> method_offsets_;
212 dchecked_vector<OatQuickMethodHeader> method_headers_;
213
214 private:
215 size_t GetMethodOffsetsRawSize() const {
216 return method_offsets_.size() * sizeof(method_offsets_[0]);
217 }
218
219 DISALLOW_COPY_AND_ASSIGN(OatClass);
220};
221
222class OatWriter::OatDexFile {
223 public:
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000224 OatDexFile(const char* dex_file_location,
225 DexFileSource source,
226 CreateTypeLookupTable create_type_lookup_table);
Vladimir Marko49b0f452015-12-10 13:49:19 +0000227 OatDexFile(OatDexFile&& src) = default;
228
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000229 const char* GetLocation() const {
230 return dex_file_location_data_;
231 }
232
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000233 void ReserveClassOffsets(OatWriter* oat_writer);
234
Vladimir Marko49b0f452015-12-10 13:49:19 +0000235 size_t SizeOf() const;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000236 bool Write(OatWriter* oat_writer, OutputStream* out) const;
237 bool WriteClassOffsets(OatWriter* oat_writer, OutputStream* out);
238
239 // The source of the dex file.
240 DexFileSource source_;
241
242 // Whether to create the type lookup table.
243 CreateTypeLookupTable create_type_lookup_table_;
244
245 // Dex file size. Initialized when writing the dex file.
246 size_t dex_file_size_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000247
248 // Offset of start of OatDexFile from beginning of OatHeader. It is
249 // used to validate file position when writing.
250 size_t offset_;
251
252 // Data to write.
253 uint32_t dex_file_location_size_;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000254 const char* dex_file_location_data_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000255 uint32_t dex_file_location_checksum_;
256 uint32_t dex_file_offset_;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000257 uint32_t class_offsets_offset_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000258 uint32_t lookup_table_offset_;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000259
260 // Data to write to a separate section.
Vladimir Marko49b0f452015-12-10 13:49:19 +0000261 dchecked_vector<uint32_t> class_offsets_;
262
David Sehr9aa352e2016-09-15 18:13:52 -0700263 void InitTypeLookupTable(const DexFile& dex_file, uint8_t* storage) const {
264 lookup_table_.reset(TypeLookupTable::Create(dex_file, storage));
265 }
266
267 TypeLookupTable* GetTypeLookupTable() const {
268 return lookup_table_.get();
269 }
270
Vladimir Marko49b0f452015-12-10 13:49:19 +0000271 private:
David Sehr9aa352e2016-09-15 18:13:52 -0700272 mutable std::unique_ptr<TypeLookupTable> lookup_table_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000273 size_t GetClassOffsetsRawSize() const {
274 return class_offsets_.size() * sizeof(class_offsets_[0]);
275 }
276
277 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
278};
279
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100280#define DCHECK_OFFSET() \
281 DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out->Seek(0, kSeekCurrent)) \
282 << "file_offset=" << file_offset << " relative_offset=" << relative_offset
283
284#define DCHECK_OFFSET_() \
285 DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out->Seek(0, kSeekCurrent)) \
286 << "file_offset=" << file_offset << " offset_=" << offset_
287
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000288OatWriter::OatWriter(bool compiling_boot_image, TimingLogger* timings)
289 : write_state_(WriteState::kAddingDexFileSources),
290 timings_(timings),
291 raw_dex_files_(),
292 zip_archives_(),
293 zipped_dex_files_(),
294 zipped_dex_file_locations_(),
295 compiler_driver_(nullptr),
296 image_writer_(nullptr),
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800297 compiling_boot_image_(compiling_boot_image),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000298 dex_files_(nullptr),
David Brazdil7b49e6c2016-09-01 11:06:18 +0100299 vdex_size_(0u),
300 vdex_dex_files_offset_(0u),
David Brazdil5d5a36b2016-09-14 15:34:10 +0100301 vdex_verifier_deps_offset_(0u),
David Brazdil7b49e6c2016-09-01 11:06:18 +0100302 oat_size_(0u),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000303 bss_start_(0u),
Vladimir Marko5c42c292015-02-25 12:02:49 +0000304 bss_size_(0u),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000305 bss_roots_offset_(0u),
306 bss_string_entries_(),
Vladimir Markof4da6752014-08-01 19:04:18 +0100307 oat_data_offset_(0u),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700308 oat_header_(nullptr),
David Brazdil7b49e6c2016-09-01 11:06:18 +0100309 size_vdex_header_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700310 size_dex_file_alignment_(0),
311 size_executable_offset_alignment_(0),
312 size_oat_header_(0),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700313 size_oat_header_key_value_store_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700314 size_dex_file_(0),
David Brazdil5d5a36b2016-09-14 15:34:10 +0100315 size_verifier_deps_(0),
316 size_verifier_deps_alignment_(0),
Ian Rogers848871b2013-08-05 10:56:33 -0700317 size_interpreter_to_interpreter_bridge_(0),
318 size_interpreter_to_compiled_code_bridge_(0),
319 size_jni_dlsym_lookup_(0),
Andreas Gampe2da88232014-02-27 12:26:20 -0800320 size_quick_generic_jni_trampoline_(0),
Jeff Hao88474b42013-10-23 16:24:40 -0700321 size_quick_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700322 size_quick_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -0700323 size_quick_to_interpreter_bridge_(0),
324 size_trampoline_alignment_(0),
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100325 size_method_header_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700326 size_code_(0),
327 size_code_alignment_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +0100328 size_relative_call_thunks_(0),
Vladimir Markoc74658b2015-03-31 10:26:41 +0100329 size_misc_thunks_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700330 size_vmap_table_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700331 size_oat_dex_file_location_size_(0),
332 size_oat_dex_file_location_data_(0),
333 size_oat_dex_file_location_checksum_(0),
334 size_oat_dex_file_offset_(0),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000335 size_oat_dex_file_class_offsets_offset_(0),
Vladimir Marko49b0f452015-12-10 13:49:19 +0000336 size_oat_dex_file_lookup_table_offset_(0),
Vladimir Marko49b0f452015-12-10 13:49:19 +0000337 size_oat_lookup_table_alignment_(0),
338 size_oat_lookup_table_(0),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000339 size_oat_class_offsets_alignment_(0),
340 size_oat_class_offsets_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -0700341 size_oat_class_type_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700342 size_oat_class_status_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -0700343 size_oat_class_method_bitmaps_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +0100344 size_oat_class_method_offsets_(0),
Vladimir Marko944da602016-02-19 12:27:55 +0000345 relative_patcher_(nullptr),
346 absolute_patch_locations_() {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000347}
348
349bool OatWriter::AddDexFileSource(const char* filename,
350 const char* location,
351 CreateTypeLookupTable create_type_lookup_table) {
352 DCHECK(write_state_ == WriteState::kAddingDexFileSources);
353 uint32_t magic;
354 std::string error_msg;
Andreas Gampe43e10b02016-07-15 17:17:34 -0700355 File fd = OpenAndReadMagic(filename, &magic, &error_msg);
356 if (fd.Fd() == -1) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000357 PLOG(ERROR) << "Failed to read magic number from dex file: '" << filename << "'";
358 return false;
359 } else if (IsDexMagic(magic)) {
360 // The file is open for reading, not writing, so it's OK to let the File destructor
361 // close it without checking for explicit Close(), so pass checkUsage = false.
Andreas Gampe43e10b02016-07-15 17:17:34 -0700362 raw_dex_files_.emplace_back(new File(fd.Release(), location, /* checkUsage */ false));
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000363 oat_dex_files_.emplace_back(location,
364 DexFileSource(raw_dex_files_.back().get()),
365 create_type_lookup_table);
366 } else if (IsZipMagic(magic)) {
367 if (!AddZippedDexFilesSource(std::move(fd), location, create_type_lookup_table)) {
368 return false;
369 }
370 } else {
371 LOG(ERROR) << "Expected valid zip or dex file: '" << filename << "'";
372 return false;
373 }
374 return true;
375}
376
377// Add dex file source(s) from a zip file specified by a file handle.
Andreas Gampe43e10b02016-07-15 17:17:34 -0700378bool OatWriter::AddZippedDexFilesSource(File&& zip_fd,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000379 const char* location,
380 CreateTypeLookupTable create_type_lookup_table) {
381 DCHECK(write_state_ == WriteState::kAddingDexFileSources);
382 std::string error_msg;
Andreas Gampe43e10b02016-07-15 17:17:34 -0700383 zip_archives_.emplace_back(ZipArchive::OpenFromFd(zip_fd.Release(), location, &error_msg));
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000384 ZipArchive* zip_archive = zip_archives_.back().get();
385 if (zip_archive == nullptr) {
386 LOG(ERROR) << "Failed to open zip from file descriptor for '" << location << "': "
387 << error_msg;
388 return false;
389 }
390 for (size_t i = 0; ; ++i) {
391 std::string entry_name = DexFile::GetMultiDexClassesDexName(i);
392 std::unique_ptr<ZipEntry> entry(zip_archive->Find(entry_name.c_str(), &error_msg));
393 if (entry == nullptr) {
394 break;
395 }
396 zipped_dex_files_.push_back(std::move(entry));
397 zipped_dex_file_locations_.push_back(DexFile::GetMultiDexLocation(i, location));
398 const char* full_location = zipped_dex_file_locations_.back().c_str();
399 oat_dex_files_.emplace_back(full_location,
400 DexFileSource(zipped_dex_files_.back().get()),
401 create_type_lookup_table);
402 }
403 if (zipped_dex_file_locations_.empty()) {
404 LOG(ERROR) << "No dex files in zip file '" << location << "': " << error_msg;
405 return false;
406 }
407 return true;
408}
409
410// Add dex file source from raw memory.
411bool OatWriter::AddRawDexFileSource(const ArrayRef<const uint8_t>& data,
412 const char* location,
413 uint32_t location_checksum,
414 CreateTypeLookupTable create_type_lookup_table) {
415 DCHECK(write_state_ == WriteState::kAddingDexFileSources);
416 if (data.size() < sizeof(DexFile::Header)) {
417 LOG(ERROR) << "Provided data is shorter than dex file header. size: "
418 << data.size() << " File: " << location;
419 return false;
420 }
421 if (!ValidateDexFileHeader(data.data(), location)) {
422 return false;
423 }
424 const UnalignedDexFileHeader* header = AsUnalignedDexFileHeader(data.data());
425 if (data.size() < header->file_size_) {
426 LOG(ERROR) << "Truncated dex file data. Data size: " << data.size()
427 << " file size from header: " << header->file_size_ << " File: " << location;
428 return false;
429 }
430
431 oat_dex_files_.emplace_back(location, DexFileSource(data.data()), create_type_lookup_table);
432 oat_dex_files_.back().dex_file_location_checksum_ = location_checksum;
433 return true;
434}
435
436dchecked_vector<const char*> OatWriter::GetSourceLocations() const {
437 dchecked_vector<const char*> locations;
438 locations.reserve(oat_dex_files_.size());
439 for (const OatDexFile& oat_dex_file : oat_dex_files_) {
440 locations.push_back(oat_dex_file.GetLocation());
441 }
442 return locations;
443}
444
445bool OatWriter::WriteAndOpenDexFiles(
David Brazdil7b49e6c2016-09-01 11:06:18 +0100446 File* vdex_file,
447 OutputStream* oat_rodata,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000448 InstructionSet instruction_set,
449 const InstructionSetFeatures* instruction_set_features,
450 SafeMap<std::string, std::string>* key_value_store,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800451 bool verify,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000452 /*out*/ std::unique_ptr<MemMap>* opened_dex_files_map,
453 /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files) {
454 CHECK(write_state_ == WriteState::kAddingDexFileSources);
455
David Brazdil7b49e6c2016-09-01 11:06:18 +0100456 // Record the ELF rodata section offset, i.e. the beginning of the OAT data.
457 if (!RecordOatDataOffset(oat_rodata)) {
458 return false;
459 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000460
461 std::unique_ptr<MemMap> dex_files_map;
462 std::vector<std::unique_ptr<const DexFile>> dex_files;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100463
464 // Initialize VDEX and OAT headers.
465 if (kIsVdexEnabled) {
466 size_vdex_header_ = sizeof(VdexFile::Header);
467 vdex_size_ = size_vdex_header_;
468 }
469 size_t oat_data_offset = InitOatHeader(instruction_set,
470 instruction_set_features,
471 dchecked_integral_cast<uint32_t>(oat_dex_files_.size()),
472 key_value_store);
473 oat_size_ = InitOatDexFiles(oat_data_offset);
474
475 ChecksumUpdatingOutputStream checksum_updating_rodata(oat_rodata, oat_header_.get());
476
477 if (kIsVdexEnabled) {
478 std::unique_ptr<BufferedOutputStream> vdex_out(
479 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(vdex_file)));
480
481 // Write DEX files into VDEX, mmap and open them.
482 if (!WriteDexFiles(vdex_out.get(), vdex_file) ||
483 !OpenDexFiles(vdex_file, verify, &dex_files_map, &dex_files)) {
484 return false;
485 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100486 } else {
487 // Write DEX files into OAT, mmap and open them.
488 if (!WriteDexFiles(oat_rodata, vdex_file) ||
489 !OpenDexFiles(vdex_file, verify, &dex_files_map, &dex_files)) {
490 return false;
491 }
492
493 // Do a bulk checksum update for Dex[]. Doing it piece by piece would be
494 // difficult because we're not using the OutputStream directly.
495 if (!oat_dex_files_.empty()) {
496 size_t size = oat_size_ - oat_dex_files_[0].dex_file_offset_;
497 oat_header_->UpdateChecksum(dex_files_map->Begin(), size);
498 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000499 }
David Brazdil181e1cc2016-09-01 16:38:47 +0000500
David Brazdil7b49e6c2016-09-01 11:06:18 +0100501 // Write TypeLookupTables into OAT.
David Brazdil181e1cc2016-09-01 16:38:47 +0000502 if (!WriteTypeLookupTables(&checksum_updating_rodata, dex_files)) {
503 return false;
504 }
505
David Brazdil7b49e6c2016-09-01 11:06:18 +0100506 // Reserve space for class offsets in OAT and update class_offsets_offset_.
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000507 for (OatDexFile& oat_dex_file : oat_dex_files_) {
508 oat_dex_file.ReserveClassOffsets(this);
509 }
Vladimir Markoe079e212016-05-25 12:49:49 +0100510
David Brazdil7b49e6c2016-09-01 11:06:18 +0100511 // Write OatDexFiles into OAT. Needs to be done last, once offsets are collected.
David Brazdil181e1cc2016-09-01 16:38:47 +0000512 if (!WriteOatDexFiles(&checksum_updating_rodata)) {
513 return false;
David Brazdilb92ba622016-09-01 16:00:30 +0000514 }
515
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000516 *opened_dex_files_map = std::move(dex_files_map);
517 *opened_dex_files = std::move(dex_files);
518 write_state_ = WriteState::kPrepareLayout;
519 return true;
520}
521
522void OatWriter::PrepareLayout(const CompilerDriver* compiler,
523 ImageWriter* image_writer,
Vladimir Marko944da602016-02-19 12:27:55 +0000524 const std::vector<const DexFile*>& dex_files,
525 linker::MultiOatRelativePatcher* relative_patcher) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000526 CHECK(write_state_ == WriteState::kPrepareLayout);
527
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000528 compiler_driver_ = compiler;
529 image_writer_ = image_writer;
Vladimir Marko944da602016-02-19 12:27:55 +0000530 dex_files_ = &dex_files;
531 relative_patcher_ = relative_patcher;
532 SetMultiOatRelativePatcherAdjustment();
533
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000534 if (compiling_boot_image_) {
535 CHECK(image_writer_ != nullptr);
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800536 }
Vladimir Markob163bb72015-03-31 21:49:49 +0100537 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000538 CHECK_EQ(instruction_set, oat_header_->GetInstructionSet());
Vladimir Markof4da6752014-08-01 19:04:18 +0100539
David Brazdil7b49e6c2016-09-01 11:06:18 +0100540 uint32_t offset = oat_size_;
Ian Rogersca368cb2013-11-15 15:52:08 -0800541 {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000542 TimingLogger::ScopedTiming split("InitOatClasses", timings_);
Ian Rogersca368cb2013-11-15 15:52:08 -0800543 offset = InitOatClasses(offset);
544 }
545 {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000546 TimingLogger::ScopedTiming split("InitOatMaps", timings_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100547 offset = InitOatMaps(offset);
548 }
549 {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000550 TimingLogger::ScopedTiming split("InitOatCode", timings_);
Ian Rogersca368cb2013-11-15 15:52:08 -0800551 offset = InitOatCode(offset);
552 }
553 {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000554 TimingLogger::ScopedTiming split("InitOatCodeDexFiles", timings_);
Ian Rogersca368cb2013-11-15 15:52:08 -0800555 offset = InitOatCodeDexFiles(offset);
556 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100557 oat_size_ = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700558
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800559 if (!HasBootImage()) {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000560 TimingLogger::ScopedTiming split("InitBssLayout", timings_);
561 InitBssLayout(instruction_set);
Vladimir Marko09d09432015-09-08 13:47:48 +0100562 }
563
Brian Carlstrome24fa612011-09-29 00:53:55 -0700564 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800565 if (compiling_boot_image_) {
566 CHECK_EQ(image_writer_ != nullptr,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000567 oat_header_->GetStoreValueByKey(OatHeader::kImageLocationKey) == nullptr);
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800568 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000569
570 write_state_ = WriteState::kWriteRoData;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700571}
572
Ian Rogers0571d352011-11-03 19:51:38 -0700573OatWriter::~OatWriter() {
Ian Rogers0571d352011-11-03 19:51:38 -0700574}
575
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100576class OatWriter::DexMethodVisitor {
577 public:
578 DexMethodVisitor(OatWriter* writer, size_t offset)
579 : writer_(writer),
580 offset_(offset),
581 dex_file_(nullptr),
582 class_def_index_(DexFile::kDexNoIndex) {
583 }
584
585 virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) {
586 DCHECK(dex_file_ == nullptr);
587 DCHECK_EQ(class_def_index_, DexFile::kDexNoIndex);
588 dex_file_ = dex_file;
589 class_def_index_ = class_def_index;
590 return true;
591 }
592
593 virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0;
594
595 virtual bool EndClass() {
596 if (kIsDebugBuild) {
597 dex_file_ = nullptr;
598 class_def_index_ = DexFile::kDexNoIndex;
599 }
600 return true;
601 }
602
603 size_t GetOffset() const {
604 return offset_;
605 }
606
607 protected:
608 virtual ~DexMethodVisitor() { }
609
610 OatWriter* const writer_;
611
612 // The offset is usually advanced for each visited method by the derived class.
613 size_t offset_;
614
615 // The dex file and class def index are set in StartClass().
616 const DexFile* dex_file_;
617 size_t class_def_index_;
618};
619
620class OatWriter::OatDexMethodVisitor : public DexMethodVisitor {
621 public:
622 OatDexMethodVisitor(OatWriter* writer, size_t offset)
623 : DexMethodVisitor(writer, offset),
624 oat_class_index_(0u),
625 method_offsets_index_(0u) {
626 }
627
628 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
629 DexMethodVisitor::StartClass(dex_file, class_def_index);
630 DCHECK_LT(oat_class_index_, writer_->oat_classes_.size());
631 method_offsets_index_ = 0u;
632 return true;
633 }
634
635 bool EndClass() {
636 ++oat_class_index_;
637 return DexMethodVisitor::EndClass();
638 }
639
640 protected:
641 size_t oat_class_index_;
642 size_t method_offsets_index_;
643};
644
645class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor {
646 public:
647 InitOatClassesMethodVisitor(OatWriter* writer, size_t offset)
648 : DexMethodVisitor(writer, offset),
649 compiled_methods_(),
650 num_non_null_compiled_methods_(0u) {
Vladimir Marko49b0f452015-12-10 13:49:19 +0000651 size_t num_classes = 0u;
652 for (const OatDexFile& oat_dex_file : writer_->oat_dex_files_) {
653 num_classes += oat_dex_file.class_offsets_.size();
654 }
655 writer_->oat_classes_.reserve(num_classes);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100656 compiled_methods_.reserve(256u);
657 }
658
659 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
660 DexMethodVisitor::StartClass(dex_file, class_def_index);
661 compiled_methods_.clear();
662 num_non_null_compiled_methods_ = 0u;
663 return true;
664 }
665
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300666 bool VisitMethod(size_t class_def_method_index ATTRIBUTE_UNUSED,
667 const ClassDataItemIterator& it) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100668 // Fill in the compiled_methods_ array for methods that have a
669 // CompiledMethod. We track the number of non-null entries in
670 // num_non_null_compiled_methods_ since we only want to allocate
671 // OatMethodOffsets for the compiled methods.
672 uint32_t method_idx = it.GetMemberIndex();
673 CompiledMethod* compiled_method =
674 writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx));
675 compiled_methods_.push_back(compiled_method);
676 if (compiled_method != nullptr) {
677 ++num_non_null_compiled_methods_;
678 }
679 return true;
680 }
681
682 bool EndClass() {
683 ClassReference class_ref(dex_file_, class_def_index_);
684 CompiledClass* compiled_class = writer_->compiler_driver_->GetCompiledClass(class_ref);
685 mirror::Class::Status status;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700686 if (compiled_class != nullptr) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100687 status = compiled_class->GetStatus();
688 } else if (writer_->compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) {
689 status = mirror::Class::kStatusError;
690 } else {
691 status = mirror::Class::kStatusNotReady;
692 }
693
Vladimir Marko49b0f452015-12-10 13:49:19 +0000694 writer_->oat_classes_.emplace_back(offset_,
695 compiled_methods_,
696 num_non_null_compiled_methods_,
697 status);
698 offset_ += writer_->oat_classes_.back().SizeOf();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100699 return DexMethodVisitor::EndClass();
700 }
701
702 private:
Vladimir Marko49b0f452015-12-10 13:49:19 +0000703 dchecked_vector<CompiledMethod*> compiled_methods_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100704 size_t num_non_null_compiled_methods_;
705};
706
707class OatWriter::InitCodeMethodVisitor : public OatDexMethodVisitor {
708 public:
709 InitCodeMethodVisitor(OatWriter* writer, size_t offset)
David Srbecky009e2a62015-04-15 02:46:30 +0100710 : OatDexMethodVisitor(writer, offset),
David Srbecky009e2a62015-04-15 02:46:30 +0100711 debuggable_(writer->GetCompilerDriver()->GetCompilerOptions().GetDebuggable()) {
David Srbeckyf8980872015-05-22 17:04:47 +0100712 writer_->absolute_patch_locations_.reserve(
Vladimir Markof4da6752014-08-01 19:04:18 +0100713 writer_->compiler_driver_->GetNonRelativeLinkerPatchCount());
714 }
715
716 bool EndClass() {
717 OatDexMethodVisitor::EndClass();
718 if (oat_class_index_ == writer_->oat_classes_.size()) {
Vladimir Marko71b0ddf2015-04-02 19:45:06 +0100719 offset_ = writer_->relative_patcher_->ReserveSpaceEnd(offset_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100720 }
721 return true;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100722 }
723
724 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700725 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko49b0f452015-12-10 13:49:19 +0000726 OatClass* oat_class = &writer_->oat_classes_[oat_class_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100727 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
728
729 if (compiled_method != nullptr) {
730 // Derived from CompiledMethod.
731 uint32_t quick_code_offset = 0;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100732
Vladimir Marko35831e82015-09-11 11:59:18 +0100733 ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode();
734 uint32_t code_size = quick_code.size() * sizeof(uint8_t);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800735 uint32_t thumb_offset = compiled_method->CodeDelta();
Elliott Hughes956af0f2014-12-11 14:34:28 -0800736
David Srbecky009e2a62015-04-15 02:46:30 +0100737 // Deduplicate code arrays if we are not producing debuggable code.
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100738 bool deduped = true;
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800739 MethodReference method_ref(dex_file_, it.GetMemberIndex());
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000740 if (debuggable_) {
Vladimir Marko944da602016-02-19 12:27:55 +0000741 quick_code_offset = writer_->relative_patcher_->GetOffset(method_ref);
742 if (quick_code_offset != 0u) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800743 // Duplicate methods, we want the same code for both of them so that the oat writer puts
744 // the same code in both ArtMethods so that we do not get different oat code at runtime.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800745 } else {
746 quick_code_offset = NewQuickCodeOffset(compiled_method, it, thumb_offset);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100747 deduped = false;
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800748 }
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000749 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100750 quick_code_offset = dedupe_map_.GetOrCreate(
751 compiled_method,
752 [this, &deduped, compiled_method, &it, thumb_offset]() {
753 deduped = false;
754 return NewQuickCodeOffset(compiled_method, it, thumb_offset);
755 });
Elliott Hughes956af0f2014-12-11 14:34:28 -0800756 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100757
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100758 if (code_size != 0) {
Vladimir Marko944da602016-02-19 12:27:55 +0000759 if (writer_->relative_patcher_->GetOffset(method_ref) != 0u) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100760 // TODO: Should this be a hard failure?
761 LOG(WARNING) << "Multiple definitions of "
762 << PrettyMethod(method_ref.dex_method_index, *method_ref.dex_file)
Vladimir Marko944da602016-02-19 12:27:55 +0000763 << " offsets " << writer_->relative_patcher_->GetOffset(method_ref)
764 << " " << quick_code_offset;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100765 } else {
Vladimir Marko944da602016-02-19 12:27:55 +0000766 writer_->relative_patcher_->SetOffset(method_ref, quick_code_offset);
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100767 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800768 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100769
Elliott Hughes956af0f2014-12-11 14:34:28 -0800770 // Update quick method header.
771 DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size());
772 OatQuickMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_];
Elliott Hughes956af0f2014-12-11 14:34:28 -0800773 uint32_t vmap_table_offset = method_header->vmap_table_offset_;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100774 // If we don't have quick code, then we must have a vmap, as that is how the dex2dex
775 // compiler records its transformations.
Vladimir Marko35831e82015-09-11 11:59:18 +0100776 DCHECK(!quick_code.empty() || vmap_table_offset != 0);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800777 // The code offset was 0 when the mapping/vmap table offset was set, so it's set
778 // to 0-offset and we need to adjust it by code_offset.
779 uint32_t code_offset = quick_code_offset - thumb_offset;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100780 if (vmap_table_offset != 0u && code_offset != 0u) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800781 vmap_table_offset += code_offset;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100782 DCHECK_LT(vmap_table_offset, code_offset) << "Overflow in oat offsets";
Elliott Hughes956af0f2014-12-11 14:34:28 -0800783 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800784 uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
785 uint32_t core_spill_mask = compiled_method->GetCoreSpillMask();
786 uint32_t fp_spill_mask = compiled_method->GetFpSpillMask();
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100787 *method_header = OatQuickMethodHeader(vmap_table_offset,
788 frame_size_in_bytes,
789 core_spill_mask,
790 fp_spill_mask,
791 code_size);
Vladimir Marko7624d252014-05-02 14:40:15 +0100792
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000793 if (!deduped) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800794 // Update offsets. (Checksum is updated when writing.)
795 offset_ += sizeof(*method_header); // Method header is prepended before code.
796 offset_ += code_size;
797 // Record absolute patch locations.
798 if (!compiled_method->GetPatches().empty()) {
799 uintptr_t base_loc = offset_ - code_size - writer_->oat_header_->GetExecutableOffset();
800 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
Vladimir Marko20f85592015-03-19 10:07:02 +0000801 if (!patch.IsPcRelative()) {
David Srbeckyf8980872015-05-22 17:04:47 +0100802 writer_->absolute_patch_locations_.push_back(base_loc + patch.LiteralOffset());
Vladimir Markof4da6752014-08-01 19:04:18 +0100803 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000804 if (patch.GetType() == LinkerPatch::Type::kStringBssEntry) {
805 StringReference ref(patch.TargetStringDexFile(), patch.TargetStringIndex());
806 writer_->bss_string_entries_.Overwrite(ref, /* placeholder */ 0u);
807 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100808 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100809 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800810 }
Alex Light78382fa2014-06-06 15:45:32 -0700811
David Srbecky91cc06c2016-03-07 16:13:58 +0000812 const CompilerOptions& compiler_options = writer_->compiler_driver_->GetCompilerOptions();
David Srbecky197160d2016-03-07 17:33:57 +0000813 // Exclude quickened dex methods (code_size == 0) since they have no native code.
814 if (compiler_options.GenerateAnyDebugInfo() && code_size != 0) {
815 bool has_code_info = method_header->IsOptimized();
Elliott Hughes956af0f2014-12-11 14:34:28 -0800816 // Record debug information for this function if we are doing that.
David Srbecky09c2a6b2016-03-11 17:11:44 +0000817 debug::MethodDebugInfo info = debug::MethodDebugInfo();
818 info.trampoline_name = nullptr;
David Srbecky197160d2016-03-07 17:33:57 +0000819 info.dex_file = dex_file_;
820 info.class_def_index = class_def_index_;
821 info.dex_method_index = it.GetMemberIndex();
822 info.access_flags = it.GetMethodAccessFlags();
823 info.code_item = it.GetMethodCodeItem();
824 info.isa = compiled_method->GetInstructionSet();
825 info.deduped = deduped;
826 info.is_native_debuggable = compiler_options.GetNativeDebuggable();
827 info.is_optimized = method_header->IsOptimized();
828 info.is_code_address_text_relative = true;
829 info.code_address = code_offset - writer_->oat_header_->GetExecutableOffset();
830 info.code_size = code_size;
831 info.frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
832 info.code_info = has_code_info ? compiled_method->GetVmapTable().data() : nullptr;
833 info.cfi = compiled_method->GetCFIInfo();
834 writer_->method_info_.push_back(info);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100835 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100836
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100837 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
838 OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_];
839 offsets->code_offset_ = quick_code_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100840 ++method_offsets_index_;
841 }
842
843 return true;
844 }
845
846 private:
Vladimir Marko20f85592015-03-19 10:07:02 +0000847 struct CodeOffsetsKeyComparator {
848 bool operator()(const CompiledMethod* lhs, const CompiledMethod* rhs) const {
Vladimir Marko35831e82015-09-11 11:59:18 +0100849 // Code is deduplicated by CompilerDriver, compare only data pointers.
850 if (lhs->GetQuickCode().data() != rhs->GetQuickCode().data()) {
851 return lhs->GetQuickCode().data() < rhs->GetQuickCode().data();
Vladimir Marko20f85592015-03-19 10:07:02 +0000852 }
853 // If the code is the same, all other fields are likely to be the same as well.
Vladimir Marko35831e82015-09-11 11:59:18 +0100854 if (UNLIKELY(lhs->GetVmapTable().data() != rhs->GetVmapTable().data())) {
855 return lhs->GetVmapTable().data() < rhs->GetVmapTable().data();
Vladimir Marko20f85592015-03-19 10:07:02 +0000856 }
Vladimir Marko35831e82015-09-11 11:59:18 +0100857 if (UNLIKELY(lhs->GetPatches().data() != rhs->GetPatches().data())) {
858 return lhs->GetPatches().data() < rhs->GetPatches().data();
Vladimir Marko20f85592015-03-19 10:07:02 +0000859 }
860 return false;
861 }
862 };
863
David Srbecky009e2a62015-04-15 02:46:30 +0100864 uint32_t NewQuickCodeOffset(CompiledMethod* compiled_method,
865 const ClassDataItemIterator& it,
866 uint32_t thumb_offset) {
867 offset_ = writer_->relative_patcher_->ReserveSpace(
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100868 offset_, compiled_method, MethodReference(dex_file_, it.GetMemberIndex()));
Vladimir Marko0c737df2016-08-01 16:33:16 +0100869 offset_ += CodeAlignmentSize(offset_, *compiled_method);
870 DCHECK_ALIGNED_PARAM(offset_ + sizeof(OatQuickMethodHeader),
David Srbecky009e2a62015-04-15 02:46:30 +0100871 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
872 return offset_ + sizeof(OatQuickMethodHeader) + thumb_offset;
873 }
874
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100875 // Deduplication is already done on a pointer basis by the compiler driver,
876 // so we can simply compare the pointers to find out if things are duplicated.
Vladimir Marko8a630572014-04-09 18:45:35 +0100877 SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_;
David Srbecky2f6cdb02015-04-11 00:17:53 +0100878
David Srbecky009e2a62015-04-15 02:46:30 +0100879 // Cache of compiler's --debuggable option.
880 const bool debuggable_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100881};
882
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100883class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor {
884 public:
885 InitMapMethodVisitor(OatWriter* writer, size_t offset)
886 : OatDexMethodVisitor(writer, offset) {
887 }
888
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700889 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700890 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko49b0f452015-12-10 13:49:19 +0000891 OatClass* oat_class = &writer_->oat_classes_[oat_class_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100892 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
893
894 if (compiled_method != nullptr) {
895 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100896 DCHECK_EQ(oat_class->method_headers_[method_offsets_index_].vmap_table_offset_, 0u);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100897
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100898 ArrayRef<const uint8_t> map = compiled_method->GetVmapTable();
Vladimir Marko35831e82015-09-11 11:59:18 +0100899 uint32_t map_size = map.size() * sizeof(map[0]);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100900 if (map_size != 0u) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100901 size_t offset = dedupe_map_.GetOrCreate(
902 map.data(),
903 [this, map_size]() {
904 uint32_t new_offset = offset_;
905 offset_ += map_size;
906 return new_offset;
907 });
908 // Code offset is not initialized yet, so set the map offset to 0u-offset.
909 DCHECK_EQ(oat_class->method_offsets_[method_offsets_index_].code_offset_, 0u);
910 oat_class->method_headers_[method_offsets_index_].vmap_table_offset_ = 0u - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100911 }
912 ++method_offsets_index_;
913 }
914
915 return true;
916 }
917
918 private:
919 // Deduplication is already done on a pointer basis by the compiler driver,
920 // so we can simply compare the pointers to find out if things are duplicated.
Vladimir Marko35831e82015-09-11 11:59:18 +0100921 SafeMap<const uint8_t*, uint32_t> dedupe_map_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100922};
923
924class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor {
925 public:
926 InitImageMethodVisitor(OatWriter* writer, size_t offset)
Jeff Haoc7d11882015-02-03 15:08:39 -0800927 : OatDexMethodVisitor(writer, offset),
928 pointer_size_(GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet())) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100929 }
930
931 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700932 REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800933 const DexFile::TypeId& type_id =
934 dex_file_->GetTypeId(dex_file_->GetClassDef(class_def_index_).class_idx_);
935 const char* class_descriptor = dex_file_->GetTypeDescriptor(type_id);
936 // Skip methods that are not in the image.
937 if (!writer_->GetCompilerDriver()->IsImageClass(class_descriptor)) {
938 return true;
939 }
940
Vladimir Marko49b0f452015-12-10 13:49:19 +0000941 OatClass* oat_class = &writer_->oat_classes_[oat_class_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100942 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
943
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800944 OatMethodOffsets offsets(0u);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100945 if (compiled_method != nullptr) {
946 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
947 offsets = oat_class->method_offsets_[method_offsets_index_];
948 ++method_offsets_index_;
949 }
950
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100951 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100952 // Unchecked as we hold mutator_lock_ on entry.
953 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800954 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700955 Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache(
956 Thread::Current(), *dex_file_)));
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800957 ArtMethod* method;
958 if (writer_->HasBootImage()) {
959 const InvokeType invoke_type = it.GetMethodInvokeType(
960 dex_file_->GetClassDef(class_def_index_));
961 method = linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
962 *dex_file_,
963 it.GetMemberIndex(),
964 dex_cache,
965 ScopedNullHandle<mirror::ClassLoader>(),
966 nullptr,
967 invoke_type);
968 if (method == nullptr) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700969 LOG(FATAL_WITHOUT_ABORT) << "Unexpected failure to resolve a method: "
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800970 << PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
971 soa.Self()->AssertPendingException();
972 mirror::Throwable* exc = soa.Self()->GetException();
973 std::string dump = exc->Dump();
974 LOG(FATAL) << dump;
975 UNREACHABLE();
976 }
977 } else {
978 // Should already have been resolved by the compiler, just peek into the dex cache.
979 // It may not be resolved if the class failed to verify, in this case, don't set the
980 // entrypoint. This is not fatal since the dex cache will contain a resolution method.
981 method = dex_cache->GetResolvedMethod(it.GetMemberIndex(), linker->GetImagePointerSize());
Andreas Gamped9efea62014-07-21 22:56:08 -0700982 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800983 if (method != nullptr &&
984 compiled_method != nullptr &&
985 compiled_method->GetQuickCode().size() != 0) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100986 method->SetEntryPointFromQuickCompiledCodePtrSize(
987 reinterpret_cast<void*>(offsets.code_offset_), pointer_size_);
988 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100989
990 return true;
991 }
Jeff Haoc7d11882015-02-03 15:08:39 -0800992
993 protected:
Andreas Gampe542451c2016-07-26 09:02:02 -0700994 const PointerSize pointer_size_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100995};
996
997class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor {
998 public:
999 WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
Vladimir Markof4da6752014-08-01 19:04:18 +01001000 size_t relative_offset) SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001001 : OatDexMethodVisitor(writer, relative_offset),
1002 out_(out),
Vladimir Markof4da6752014-08-01 19:04:18 +01001003 file_offset_(file_offset),
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +01001004 soa_(Thread::Current()),
Mathieu Chartier268764d2016-09-13 12:09:38 -07001005 no_thread_suspension_("OatWriter patching"),
Vladimir Markof4da6752014-08-01 19:04:18 +01001006 class_linker_(Runtime::Current()->GetClassLinker()),
1007 dex_cache_(nullptr) {
Vladimir Marko09d09432015-09-08 13:47:48 +01001008 patched_code_.reserve(16 * KB);
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001009 if (writer_->HasBootImage()) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001010 // If we're creating the image, the address space must be ready so that we can apply patches.
1011 CHECK(writer_->image_writer_->IsImageAddressSpaceReady());
Vladimir Markof4da6752014-08-01 19:04:18 +01001012 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001013 }
1014
Vladimir Markof4da6752014-08-01 19:04:18 +01001015 ~WriteCodeMethodVisitor() UNLOCK_FUNCTION(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001016 }
1017
1018 bool StartClass(const DexFile* dex_file, size_t class_def_index)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001019 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001020 OatDexMethodVisitor::StartClass(dex_file, class_def_index);
1021 if (dex_cache_ == nullptr || dex_cache_->GetDexFile() != dex_file) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001022 dex_cache_ = class_linker_->FindDexCache(Thread::Current(), *dex_file);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001023 DCHECK(dex_cache_ != nullptr);
Vladimir Markof4da6752014-08-01 19:04:18 +01001024 }
1025 return true;
1026 }
1027
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001028 bool EndClass() REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001029 bool result = OatDexMethodVisitor::EndClass();
1030 if (oat_class_index_ == writer_->oat_classes_.size()) {
1031 DCHECK(result); // OatDexMethodVisitor::EndClass() never fails.
Vladimir Marko20f85592015-03-19 10:07:02 +00001032 offset_ = writer_->relative_patcher_->WriteThunks(out_, offset_);
Vladimir Markof4da6752014-08-01 19:04:18 +01001033 if (UNLIKELY(offset_ == 0u)) {
1034 PLOG(ERROR) << "Failed to write final relative call thunks";
1035 result = false;
1036 }
1037 }
1038 return result;
1039 }
1040
1041 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001042 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001043 OatClass* oat_class = &writer_->oat_classes_[oat_class_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001044 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
1045
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001046 // No thread suspension since dex_cache_ that may get invalidated if that occurs.
Mathieu Chartier268764d2016-09-13 12:09:38 -07001047 ScopedAssertNoThreadSuspension tsc(__FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001048 if (compiled_method != nullptr) { // ie. not an abstract method
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001049 size_t file_offset = file_offset_;
1050 OutputStream* out = out_;
1051
Vladimir Marko35831e82015-09-11 11:59:18 +01001052 ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode();
1053 uint32_t code_size = quick_code.size() * sizeof(uint8_t);
Nicolas Geoffrayf0758792015-07-13 11:56:00 +00001054
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +01001055 // Deduplicate code arrays.
1056 const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_];
1057 if (method_offsets.code_offset_ > offset_) {
1058 offset_ = writer_->relative_patcher_->WriteThunks(out, offset_);
1059 if (offset_ == 0u) {
1060 ReportWriteFailure("relative call thunk", it);
1061 return false;
Nicolas Geoffrayf0758792015-07-13 11:56:00 +00001062 }
Vladimir Marko0c737df2016-08-01 16:33:16 +01001063 uint32_t alignment_size = CodeAlignmentSize(offset_, *compiled_method);
1064 if (alignment_size != 0) {
1065 if (!writer_->WriteCodeAlignment(out, alignment_size)) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +01001066 ReportWriteFailure("code alignment padding", it);
1067 return false;
1068 }
Vladimir Marko0c737df2016-08-01 16:33:16 +01001069 offset_ += alignment_size;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +01001070 DCHECK_OFFSET_();
1071 }
Vladimir Marko0c737df2016-08-01 16:33:16 +01001072 DCHECK_ALIGNED_PARAM(offset_ + sizeof(OatQuickMethodHeader),
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +01001073 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
1074 DCHECK_EQ(method_offsets.code_offset_,
1075 offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta())
1076 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
1077 const OatQuickMethodHeader& method_header =
1078 oat_class->method_headers_[method_offsets_index_];
Vladimir Markoe079e212016-05-25 12:49:49 +01001079 if (!out->WriteFully(&method_header, sizeof(method_header))) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +01001080 ReportWriteFailure("method header", it);
1081 return false;
1082 }
1083 writer_->size_method_header_ += sizeof(method_header);
1084 offset_ += sizeof(method_header);
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +00001085 DCHECK_OFFSET_();
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +01001086
1087 if (!compiled_method->GetPatches().empty()) {
Vladimir Marko35831e82015-09-11 11:59:18 +01001088 patched_code_.assign(quick_code.begin(), quick_code.end());
1089 quick_code = ArrayRef<const uint8_t>(patched_code_);
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +01001090 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
Vladimir Marko944da602016-02-19 12:27:55 +00001091 uint32_t literal_offset = patch.LiteralOffset();
Vladimir Markodb8e62d2016-03-30 16:30:21 +01001092 switch (patch.GetType()) {
1093 case LinkerPatch::Type::kCallRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001094 // NOTE: Relative calls across oat files are not supported.
1095 uint32_t target_offset = GetTargetOffset(patch);
1096 writer_->relative_patcher_->PatchCall(&patched_code_,
1097 literal_offset,
1098 offset_ + literal_offset,
1099 target_offset);
1100 break;
1101 }
Vladimir Markodb8e62d2016-03-30 16:30:21 +01001102 case LinkerPatch::Type::kDexCacheArray: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001103 uint32_t target_offset = GetDexCacheOffset(patch);
1104 writer_->relative_patcher_->PatchPcRelativeReference(&patched_code_,
1105 patch,
1106 offset_ + literal_offset,
1107 target_offset);
1108 break;
1109 }
Vladimir Markodb8e62d2016-03-30 16:30:21 +01001110 case LinkerPatch::Type::kStringRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001111 uint32_t target_offset = GetTargetObjectOffset(GetTargetString(patch));
1112 writer_->relative_patcher_->PatchPcRelativeReference(&patched_code_,
1113 patch,
1114 offset_ + literal_offset,
1115 target_offset);
1116 break;
1117 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001118 case LinkerPatch::Type::kStringBssEntry: {
1119 StringReference ref(patch.TargetStringDexFile(), patch.TargetStringIndex());
1120 uint32_t target_offset = writer_->bss_string_entries_.Get(ref);
1121 writer_->relative_patcher_->PatchPcRelativeReference(&patched_code_,
1122 patch,
1123 offset_ + literal_offset,
1124 target_offset);
1125 break;
1126 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001127 case LinkerPatch::Type::kTypeRelative: {
1128 uint32_t target_offset = GetTargetObjectOffset(GetTargetType(patch));
1129 writer_->relative_patcher_->PatchPcRelativeReference(&patched_code_,
1130 patch,
1131 offset_ + literal_offset,
1132 target_offset);
1133 break;
1134 }
Vladimir Markodb8e62d2016-03-30 16:30:21 +01001135 case LinkerPatch::Type::kCall: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001136 uint32_t target_offset = GetTargetOffset(patch);
1137 PatchCodeAddress(&patched_code_, literal_offset, target_offset);
1138 break;
1139 }
Vladimir Markodb8e62d2016-03-30 16:30:21 +01001140 case LinkerPatch::Type::kMethod: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001141 ArtMethod* method = GetTargetMethod(patch);
1142 PatchMethodAddress(&patched_code_, literal_offset, method);
1143 break;
1144 }
Vladimir Markodb8e62d2016-03-30 16:30:21 +01001145 case LinkerPatch::Type::kString: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001146 mirror::String* string = GetTargetString(patch);
1147 PatchObjectAddress(&patched_code_, literal_offset, string);
1148 break;
1149 }
Vladimir Markodb8e62d2016-03-30 16:30:21 +01001150 case LinkerPatch::Type::kType: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001151 mirror::Class* type = GetTargetType(patch);
1152 PatchObjectAddress(&patched_code_, literal_offset, type);
1153 break;
1154 }
1155 default: {
Vladimir Markodb8e62d2016-03-30 16:30:21 +01001156 DCHECK_EQ(patch.GetType(), LinkerPatch::Type::kRecordPosition);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001157 break;
1158 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +01001159 }
1160 }
1161 }
1162
Vladimir Markoe079e212016-05-25 12:49:49 +01001163 if (!out->WriteFully(quick_code.data(), code_size)) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +01001164 ReportWriteFailure("method code", it);
1165 return false;
1166 }
1167 writer_->size_code_ += code_size;
1168 offset_ += code_size;
Nicolas Geoffrayf0758792015-07-13 11:56:00 +00001169 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +01001170 DCHECK_OFFSET_();
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001171 ++method_offsets_index_;
1172 }
1173
1174 return true;
1175 }
1176
1177 private:
1178 OutputStream* const out_;
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +01001179 const size_t file_offset_;
1180 const ScopedObjectAccess soa_;
1181 const ScopedAssertNoThreadSuspension no_thread_suspension_;
Vladimir Markof4da6752014-08-01 19:04:18 +01001182 ClassLinker* const class_linker_;
1183 mirror::DexCache* dex_cache_;
1184 std::vector<uint8_t> patched_code_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001185
1186 void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) {
1187 PLOG(ERROR) << "Failed to write " << what << " for "
1188 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
1189 }
Vladimir Markof4da6752014-08-01 19:04:18 +01001190
Mathieu Chartiere401d142015-04-22 13:56:20 -07001191 ArtMethod* GetTargetMethod(const LinkerPatch& patch)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001192 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001193 MethodReference ref = patch.TargetMethod();
1194 mirror::DexCache* dex_cache =
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001195 (dex_file_ == ref.dex_file) ? dex_cache_ : class_linker_->FindDexCache(
1196 Thread::Current(), *ref.dex_file);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001197 ArtMethod* method = dex_cache->GetResolvedMethod(
1198 ref.dex_method_index, class_linker_->GetImagePointerSize());
Vladimir Markof4da6752014-08-01 19:04:18 +01001199 CHECK(method != nullptr);
1200 return method;
1201 }
1202
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001203 uint32_t GetTargetOffset(const LinkerPatch& patch) REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko944da602016-02-19 12:27:55 +00001204 uint32_t target_offset = writer_->relative_patcher_->GetOffset(patch.TargetMethod());
1205 // If there's no new compiled code, either we're compiling an app and the target method
1206 // is in the boot image, or we need to point to the correct trampoline.
Vladimir Markof4da6752014-08-01 19:04:18 +01001207 if (UNLIKELY(target_offset == 0)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001208 ArtMethod* target = GetTargetMethod(patch);
Vladimir Markof4da6752014-08-01 19:04:18 +01001209 DCHECK(target != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001210 PointerSize size =
1211 GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet());
Jeff Haoa0acc2d2015-01-27 11:22:04 -08001212 const void* oat_code_offset = target->GetEntryPointFromQuickCompiledCodePtrSize(size);
1213 if (oat_code_offset != 0) {
Vladimir Marko944da602016-02-19 12:27:55 +00001214 DCHECK(!writer_->HasBootImage());
Jeff Haoa0acc2d2015-01-27 11:22:04 -08001215 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(oat_code_offset));
1216 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(oat_code_offset));
1217 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(oat_code_offset));
1218 target_offset = PointerToLowMemUInt32(oat_code_offset);
1219 } else {
1220 target_offset = target->IsNative()
1221 ? writer_->oat_header_->GetQuickGenericJniTrampolineOffset()
1222 : writer_->oat_header_->GetQuickToInterpreterBridgeOffset();
1223 }
Vladimir Markof4da6752014-08-01 19:04:18 +01001224 }
1225 return target_offset;
1226 }
1227
Vladimir Marko052164a2016-04-27 13:54:18 +01001228 mirror::DexCache* GetDexCache(const DexFile* target_dex_file)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001229 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko052164a2016-04-27 13:54:18 +01001230 return (target_dex_file == dex_file_)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001231 ? dex_cache_
Vladimir Marko052164a2016-04-27 13:54:18 +01001232 : class_linker_->FindDexCache(Thread::Current(), *target_dex_file);
1233 }
1234
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001235 mirror::Class* GetTargetType(const LinkerPatch& patch) REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko052164a2016-04-27 13:54:18 +01001236 mirror::DexCache* dex_cache = GetDexCache(patch.TargetTypeDexFile());
Vladimir Markof4da6752014-08-01 19:04:18 +01001237 mirror::Class* type = dex_cache->GetResolvedType(patch.TargetTypeIndex());
1238 CHECK(type != nullptr);
1239 return type;
1240 }
1241
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001242 mirror::String* GetTargetString(const LinkerPatch& patch) REQUIRES_SHARED(Locks::mutator_lock_) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07001243 ScopedObjectAccessUnchecked soa(Thread::Current());
1244 StackHandleScope<1> hs(soa.Self());
1245 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1246 Handle<mirror::DexCache> dex_cache(hs.NewHandle(GetDexCache(patch.TargetStringDexFile())));
1247 mirror::String* string = linker->LookupString(*patch.TargetStringDexFile(),
1248 patch.TargetStringIndex(),
1249 dex_cache);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001250 DCHECK(string != nullptr);
1251 DCHECK(writer_->HasBootImage() ||
1252 Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(string));
1253 return string;
1254 }
1255
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001256 uint32_t GetDexCacheOffset(const LinkerPatch& patch) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001257 if (writer_->HasBootImage()) {
Vladimir Marko944da602016-02-19 12:27:55 +00001258 uintptr_t element = writer_->image_writer_->GetDexCacheArrayElementImageAddress<uintptr_t>(
1259 patch.TargetDexCacheDexFile(), patch.TargetDexCacheElementOffset());
1260 size_t oat_index = writer_->image_writer_->GetOatIndexForDexCache(dex_cache_);
1261 uintptr_t oat_data = writer_->image_writer_->GetOatDataBegin(oat_index);
Vladimir Marko05792b92015-08-03 11:56:49 +01001262 return element - oat_data;
Vladimir Marko20f85592015-03-19 10:07:02 +00001263 } else {
Vladimir Marko09d09432015-09-08 13:47:48 +01001264 size_t start = writer_->dex_cache_arrays_offsets_.Get(patch.TargetDexCacheDexFile());
1265 return start + patch.TargetDexCacheElementOffset();
Vladimir Marko20f85592015-03-19 10:07:02 +00001266 }
1267 }
1268
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001269 uint32_t GetTargetObjectOffset(mirror::Object* object) REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001270 DCHECK(writer_->HasBootImage());
1271 object = writer_->image_writer_->GetImageAddress(object);
1272 size_t oat_index = writer_->image_writer_->GetOatIndexForDexFile(dex_file_);
1273 uintptr_t oat_data_begin = writer_->image_writer_->GetOatDataBegin(oat_index);
1274 // TODO: Clean up offset types. The target offset must be treated as signed.
1275 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(object) - oat_data_begin);
1276 }
1277
Vladimir Markof4da6752014-08-01 19:04:18 +01001278 void PatchObjectAddress(std::vector<uint8_t>* code, uint32_t offset, mirror::Object* object)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001279 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001280 if (writer_->HasBootImage()) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001281 object = writer_->image_writer_->GetImageAddress(object);
Vladimir Marko09d09432015-09-08 13:47:48 +01001282 } else {
1283 // NOTE: We're using linker patches for app->boot references when the image can
1284 // be relocated and therefore we need to emit .oat_patches. We're not using this
1285 // for app->app references, so check that the object is in the image space.
1286 DCHECK(Runtime::Current()->GetHeap()->FindSpaceFromObject(object, false)->IsImageSpace());
Vladimir Markof4da6752014-08-01 19:04:18 +01001287 }
Vladimir Marko09d09432015-09-08 13:47:48 +01001288 // Note: We only patch targeting Objects in image which is in the low 4gb.
Vladimir Markof4da6752014-08-01 19:04:18 +01001289 uint32_t address = PointerToLowMemUInt32(object);
1290 DCHECK_LE(offset + 4, code->size());
1291 uint8_t* data = &(*code)[offset];
1292 data[0] = address & 0xffu;
1293 data[1] = (address >> 8) & 0xffu;
1294 data[2] = (address >> 16) & 0xffu;
1295 data[3] = (address >> 24) & 0xffu;
1296 }
1297
Mathieu Chartiere401d142015-04-22 13:56:20 -07001298 void PatchMethodAddress(std::vector<uint8_t>* code, uint32_t offset, ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001299 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001300 if (writer_->HasBootImage()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001301 method = writer_->image_writer_->GetImageMethodAddress(method);
Vladimir Marko09d09432015-09-08 13:47:48 +01001302 } else if (kIsDebugBuild) {
1303 // NOTE: We're using linker patches for app->boot references when the image can
1304 // be relocated and therefore we need to emit .oat_patches. We're not using this
1305 // for app->app references, so check that the method is an image method.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001306 std::vector<gc::space::ImageSpace*> image_spaces =
1307 Runtime::Current()->GetHeap()->GetBootImageSpaces();
1308 bool contains_method = false;
1309 for (gc::space::ImageSpace* image_space : image_spaces) {
1310 size_t method_offset = reinterpret_cast<const uint8_t*>(method) - image_space->Begin();
1311 contains_method |=
1312 image_space->GetImageHeader().GetMethodsSection().Contains(method_offset);
1313 }
1314 CHECK(contains_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001315 }
Vladimir Marko09d09432015-09-08 13:47:48 +01001316 // Note: We only patch targeting ArtMethods in image which is in the low 4gb.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001317 uint32_t address = PointerToLowMemUInt32(method);
1318 DCHECK_LE(offset + 4, code->size());
1319 uint8_t* data = &(*code)[offset];
1320 data[0] = address & 0xffu;
1321 data[1] = (address >> 8) & 0xffu;
1322 data[2] = (address >> 16) & 0xffu;
1323 data[3] = (address >> 24) & 0xffu;
1324 }
1325
Vladimir Markof4da6752014-08-01 19:04:18 +01001326 void PatchCodeAddress(std::vector<uint8_t>* code, uint32_t offset, uint32_t target_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001327 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko09d09432015-09-08 13:47:48 +01001328 uint32_t address = target_offset;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001329 if (writer_->HasBootImage()) {
Vladimir Marko944da602016-02-19 12:27:55 +00001330 size_t oat_index = writer_->image_writer_->GetOatIndexForDexCache(dex_cache_);
1331 // TODO: Clean up offset types.
1332 // The target_offset must be treated as signed for cross-oat patching.
1333 const void* target = reinterpret_cast<const void*>(
1334 writer_->image_writer_->GetOatDataBegin(oat_index) +
1335 static_cast<int32_t>(target_offset));
1336 address = PointerToLowMemUInt32(target);
Vladimir Marko09d09432015-09-08 13:47:48 +01001337 }
Vladimir Markof4da6752014-08-01 19:04:18 +01001338 DCHECK_LE(offset + 4, code->size());
1339 uint8_t* data = &(*code)[offset];
1340 data[0] = address & 0xffu;
1341 data[1] = (address >> 8) & 0xffu;
1342 data[2] = (address >> 16) & 0xffu;
1343 data[3] = (address >> 24) & 0xffu;
1344 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001345};
1346
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001347class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor {
1348 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001349 WriteMapMethodVisitor(OatWriter* writer,
1350 OutputStream* out,
1351 const size_t file_offset,
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001352 size_t relative_offset)
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001353 : OatDexMethodVisitor(writer, relative_offset),
1354 out_(out),
1355 file_offset_(file_offset) {
1356 }
1357
1358 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001359 OatClass* oat_class = &writer_->oat_classes_[oat_class_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001360 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
1361
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001362 if (compiled_method != nullptr) { // ie. not an abstract method
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001363 size_t file_offset = file_offset_;
1364 OutputStream* out = out_;
1365
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001366 uint32_t map_offset = oat_class->method_headers_[method_offsets_index_].vmap_table_offset_;
1367 uint32_t code_offset = oat_class->method_offsets_[method_offsets_index_].code_offset_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001368 ++method_offsets_index_;
1369
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001370 DCHECK((compiled_method->GetVmapTable().size() == 0u && map_offset == 0u) ||
1371 (compiled_method->GetVmapTable().size() != 0u && map_offset != 0u))
1372 << compiled_method->GetVmapTable().size() << " " << map_offset << " "
1373 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
1374
1375 if (map_offset != 0u) {
1376 // Transform map_offset to actual oat data offset.
1377 map_offset = (code_offset - compiled_method->CodeDelta()) - map_offset;
1378 DCHECK_NE(map_offset, 0u);
1379 DCHECK_LE(map_offset, offset_) << PrettyMethod(it.GetMemberIndex(), *dex_file_);
1380
1381 ArrayRef<const uint8_t> map = compiled_method->GetVmapTable();
1382 size_t map_size = map.size() * sizeof(map[0]);
1383 if (map_offset == offset_) {
1384 // Write deduplicated map (code info for Optimizing or transformation info for dex2dex).
Vladimir Markoe079e212016-05-25 12:49:49 +01001385 if (UNLIKELY(!out->WriteFully(map.data(), map_size))) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001386 ReportWriteFailure(it);
1387 return false;
1388 }
1389 offset_ += map_size;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001390 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001391 }
1392 DCHECK_OFFSET_();
1393 }
1394
1395 return true;
1396 }
1397
1398 private:
1399 OutputStream* const out_;
1400 size_t const file_offset_;
1401
1402 void ReportWriteFailure(const ClassDataItemIterator& it) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001403 PLOG(ERROR) << "Failed to write map for "
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001404 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
1405 }
1406};
1407
1408// Visit all methods from all classes in all dex files with the specified visitor.
1409bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) {
1410 for (const DexFile* dex_file : *dex_files_) {
1411 const size_t class_def_count = dex_file->NumClassDefs();
1412 for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) {
1413 if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) {
1414 return false;
1415 }
1416 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -07001417 const uint8_t* class_data = dex_file->GetClassData(class_def);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001418 if (class_data != nullptr) { // ie not an empty class, such as a marker interface
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001419 ClassDataItemIterator it(*dex_file, class_data);
1420 while (it.HasNextStaticField()) {
1421 it.Next();
1422 }
1423 while (it.HasNextInstanceField()) {
1424 it.Next();
1425 }
1426 size_t class_def_method_index = 0u;
1427 while (it.HasNextDirectMethod()) {
1428 if (!visitor->VisitMethod(class_def_method_index, it)) {
1429 return false;
1430 }
1431 ++class_def_method_index;
1432 it.Next();
1433 }
1434 while (it.HasNextVirtualMethod()) {
1435 if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) {
1436 return false;
1437 }
1438 ++class_def_method_index;
1439 it.Next();
1440 }
1441 }
1442 if (UNLIKELY(!visitor->EndClass())) {
1443 return false;
1444 }
1445 }
1446 }
1447 return true;
1448}
1449
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001450size_t OatWriter::InitOatHeader(InstructionSet instruction_set,
1451 const InstructionSetFeatures* instruction_set_features,
1452 uint32_t num_dex_files,
1453 SafeMap<std::string, std::string>* key_value_store) {
1454 TimingLogger::ScopedTiming split("InitOatHeader", timings_);
1455 oat_header_.reset(OatHeader::Create(instruction_set,
1456 instruction_set_features,
1457 num_dex_files,
1458 key_value_store));
1459 size_oat_header_ += sizeof(OatHeader);
1460 size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001461 return oat_header_->GetHeaderSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001462}
1463
1464size_t OatWriter::InitOatDexFiles(size_t offset) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001465 TimingLogger::ScopedTiming split("InitOatDexFiles", timings_);
1466 // Initialize offsets of dex files.
Vladimir Marko49b0f452015-12-10 13:49:19 +00001467 for (OatDexFile& oat_dex_file : oat_dex_files_) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001468 oat_dex_file.offset_ = offset;
1469 offset += oat_dex_file.SizeOf();
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001470 }
1471 return offset;
1472}
1473
Brian Carlstrom389efb02012-01-11 12:06:26 -08001474size_t OatWriter::InitOatClasses(size_t offset) {
Brian Carlstrom389efb02012-01-11 12:06:26 -08001475 // calculate the offsets within OatDexFiles to OatClasses
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001476 InitOatClassesMethodVisitor visitor(this, offset);
1477 bool success = VisitDexMethods(&visitor);
1478 CHECK(success);
1479 offset = visitor.GetOffset();
Brian Carlstromba150c32013-08-27 17:31:03 -07001480
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001481 // Update oat_dex_files_.
1482 auto oat_class_it = oat_classes_.begin();
Vladimir Marko49b0f452015-12-10 13:49:19 +00001483 for (OatDexFile& oat_dex_file : oat_dex_files_) {
1484 for (uint32_t& class_offset : oat_dex_file.class_offsets_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001485 DCHECK(oat_class_it != oat_classes_.end());
Vladimir Marko49b0f452015-12-10 13:49:19 +00001486 class_offset = oat_class_it->offset_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001487 ++oat_class_it;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001488 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001489 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001490 CHECK(oat_class_it == oat_classes_.end());
1491
1492 return offset;
1493}
1494
1495size_t OatWriter::InitOatMaps(size_t offset) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001496 InitMapMethodVisitor visitor(this, offset);
1497 bool success = VisitDexMethods(&visitor);
1498 DCHECK(success);
1499 offset = visitor.GetOffset();
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001500
Brian Carlstrome24fa612011-09-29 00:53:55 -07001501 return offset;
1502}
1503
1504size_t OatWriter::InitOatCode(size_t offset) {
1505 // calculate the offsets within OatHeader to executable code
1506 size_t old_offset = offset;
Dave Allison50abf0a2014-06-23 13:19:59 -07001507 size_t adjusted_offset = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001508 // required to be on a new page boundary
1509 offset = RoundUp(offset, kPageSize);
1510 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001511 size_executable_offset_alignment_ = offset - old_offset;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001512 if (compiler_driver_->GetCompilerOptions().IsBootImage()) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001513 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001514
Ian Rogers848871b2013-08-05 10:56:33 -07001515 #define DO_TRAMPOLINE(field, fn_name) \
1516 offset = CompiledCode::AlignCode(offset, instruction_set); \
Dave Allison50abf0a2014-06-23 13:19:59 -07001517 adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \
1518 oat_header_->Set ## fn_name ## Offset(adjusted_offset); \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -07001519 (field) = compiler_driver_->Create ## fn_name(); \
1520 offset += (field)->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001521
Ian Rogers848871b2013-08-05 10:56:33 -07001522 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Andreas Gampe2da88232014-02-27 12:26:20 -08001523 DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline);
Jeff Hao88474b42013-10-23 16:24:40 -07001524 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -07001525 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
1526 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001527
Ian Rogers848871b2013-08-05 10:56:33 -07001528 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001529 } else {
Ian Rogers848871b2013-08-05 10:56:33 -07001530 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
1531 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
1532 oat_header_->SetJniDlsymLookupOffset(0);
Andreas Gampe2da88232014-02-27 12:26:20 -08001533 oat_header_->SetQuickGenericJniTrampolineOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -07001534 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001535 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -07001536 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001537 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001538 return offset;
1539}
1540
1541size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001542 #define VISIT(VisitorType) \
1543 do { \
1544 VisitorType visitor(this, offset); \
1545 bool success = VisitDexMethods(&visitor); \
1546 DCHECK(success); \
1547 offset = visitor.GetOffset(); \
1548 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001549
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001550 VISIT(InitCodeMethodVisitor);
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001551 if (HasImage()) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001552 VISIT(InitImageMethodVisitor);
Ian Rogers0571d352011-11-03 19:51:38 -07001553 }
Logan Chien8b977d32012-02-21 19:14:55 +08001554
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001555 #undef VISIT
1556
Brian Carlstrome24fa612011-09-29 00:53:55 -07001557 return offset;
1558}
1559
Vladimir Markoaad75c62016-10-03 08:46:48 +00001560void OatWriter::InitBssLayout(InstructionSet instruction_set) {
1561 DCHECK(!HasBootImage());
1562
1563 // Allocate space for app dex cache arrays in the .bss section.
1564 bss_start_ = RoundUp(oat_size_, kPageSize);
1565 PointerSize pointer_size = GetInstructionSetPointerSize(instruction_set);
1566 bss_size_ = 0u;
1567 for (const DexFile* dex_file : *dex_files_) {
1568 dex_cache_arrays_offsets_.Put(dex_file, bss_start_ + bss_size_);
1569 DexCacheArraysLayout layout(pointer_size, dex_file);
1570 bss_size_ += layout.Size();
1571 }
1572
1573 bss_roots_offset_ = bss_size_;
1574
1575 // Prepare offsets for .bss String entries.
1576 for (auto& entry : bss_string_entries_) {
1577 DCHECK_EQ(entry.second, 0u);
1578 entry.second = bss_start_ + bss_size_;
1579 bss_size_ += sizeof(GcRoot<mirror::String>);
1580 }
1581}
1582
David Srbeckybc90fd02015-04-22 19:40:27 +01001583bool OatWriter::WriteRodata(OutputStream* out) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001584 CHECK(write_state_ == WriteState::kWriteRoData);
1585
Vladimir Markoe079e212016-05-25 12:49:49 +01001586 // Wrap out to update checksum with each write.
1587 ChecksumUpdatingOutputStream checksum_updating_out(out, oat_header_.get());
1588 out = &checksum_updating_out;
1589
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001590 if (!WriteClassOffsets(out)) {
1591 LOG(ERROR) << "Failed to write class offsets to " << out->GetLocation();
Vladimir Markof4da6752014-08-01 19:04:18 +01001592 return false;
1593 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001594
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001595 if (!WriteClasses(out)) {
1596 LOG(ERROR) << "Failed to write classes to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001597 return false;
1598 }
1599
Vladimir Markof4da6752014-08-01 19:04:18 +01001600 off_t tables_end_offset = out->Seek(0, kSeekCurrent);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001601 if (tables_end_offset == static_cast<off_t>(-1)) {
David Brazdil181e1cc2016-09-01 16:38:47 +00001602 LOG(ERROR) << "Failed to get oat code position in " << out->GetLocation();
Vladimir Markof4da6752014-08-01 19:04:18 +01001603 return false;
1604 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001605 size_t file_offset = oat_data_offset_;
Vladimir Markof4da6752014-08-01 19:04:18 +01001606 size_t relative_offset = static_cast<size_t>(tables_end_offset) - file_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001607 relative_offset = WriteMaps(out, file_offset, relative_offset);
1608 if (relative_offset == 0) {
1609 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
1610 return false;
1611 }
1612
David Srbeckybc90fd02015-04-22 19:40:27 +01001613 // Write padding.
1614 off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent);
1615 relative_offset += size_executable_offset_alignment_;
1616 DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset());
1617 size_t expected_file_offset = file_offset + relative_offset;
1618 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
1619 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
1620 << " Expected: " << expected_file_offset << " File: " << out->GetLocation();
1621 return 0;
1622 }
1623 DCHECK_OFFSET();
1624
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001625 write_state_ = WriteState::kWriteText;
David Srbeckybc90fd02015-04-22 19:40:27 +01001626 return true;
1627}
1628
David Brazdil5d5a36b2016-09-14 15:34:10 +01001629bool OatWriter::WriteVerifierDeps(OutputStream* vdex_out, verifier::VerifierDeps* verifier_deps) {
1630 if (!kIsVdexEnabled) {
1631 return true;
1632 }
1633
1634 if (verifier_deps == nullptr) {
1635 // Nothing to write. Record the offset, but no need
1636 // for alignment.
1637 vdex_verifier_deps_offset_ = vdex_size_;
1638 return true;
1639 }
1640
1641 size_t initial_offset = vdex_size_;
1642 size_t start_offset = RoundUp(initial_offset, 4u);
1643
1644 vdex_size_ = start_offset;
1645 vdex_verifier_deps_offset_ = vdex_size_;
1646 size_verifier_deps_alignment_ = start_offset - initial_offset;
1647
1648 off_t actual_offset = vdex_out->Seek(start_offset, kSeekSet);
1649 if (actual_offset != static_cast<off_t>(start_offset)) {
1650 PLOG(ERROR) << "Failed to seek to verifier deps section. Actual: " << actual_offset
1651 << " Expected: " << start_offset
1652 << " Output: " << vdex_out->GetLocation();
1653 return false;
1654 }
1655
1656 std::vector<uint8_t> buffer;
1657 verifier_deps->Encode(&buffer);
1658
1659 if (!vdex_out->WriteFully(buffer.data(), buffer.size())) {
1660 PLOG(ERROR) << "Failed to write verifier deps."
1661 << " File: " << vdex_out->GetLocation();
1662 return false;
1663 }
1664 if (!vdex_out->Flush()) {
1665 PLOG(ERROR) << "Failed to flush stream after writing verifier deps."
1666 << " File: " << vdex_out->GetLocation();
1667 return false;
1668 }
1669
1670 size_verifier_deps_ = buffer.size();
1671 vdex_size_ += size_verifier_deps_;
1672 return true;
1673}
1674
David Srbeckybc90fd02015-04-22 19:40:27 +01001675bool OatWriter::WriteCode(OutputStream* out) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001676 CHECK(write_state_ == WriteState::kWriteText);
1677
Vladimir Markoe079e212016-05-25 12:49:49 +01001678 // Wrap out to update checksum with each write.
1679 ChecksumUpdatingOutputStream checksum_updating_out(out, oat_header_.get());
1680 out = &checksum_updating_out;
1681
Vladimir Marko944da602016-02-19 12:27:55 +00001682 SetMultiOatRelativePatcherAdjustment();
1683
David Srbeckybc90fd02015-04-22 19:40:27 +01001684 const size_t file_offset = oat_data_offset_;
1685 size_t relative_offset = oat_header_->GetExecutableOffset();
1686 DCHECK_OFFSET();
1687
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001688 relative_offset = WriteCode(out, file_offset, relative_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001689 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001690 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001691 return false;
1692 }
1693
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001694 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
1695 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001696 LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001697 return false;
1698 }
1699
Vladimir Markof4da6752014-08-01 19:04:18 +01001700 const off_t oat_end_file_offset = out->Seek(0, kSeekCurrent);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001701 if (oat_end_file_offset == static_cast<off_t>(-1)) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001702 LOG(ERROR) << "Failed to get oat end file offset in " << out->GetLocation();
1703 return false;
1704 }
1705
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001706 if (kIsDebugBuild) {
1707 uint32_t size_total = 0;
1708 #define DO_STAT(x) \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -07001709 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << (x) << "B)"; \
1710 size_total += (x);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001711
David Brazdil7b49e6c2016-09-01 11:06:18 +01001712 DO_STAT(size_vdex_header_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001713 DO_STAT(size_dex_file_alignment_);
1714 DO_STAT(size_executable_offset_alignment_);
1715 DO_STAT(size_oat_header_);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001716 DO_STAT(size_oat_header_key_value_store_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001717 DO_STAT(size_dex_file_);
David Brazdil5d5a36b2016-09-14 15:34:10 +01001718 DO_STAT(size_verifier_deps_);
1719 DO_STAT(size_verifier_deps_alignment_);
Ian Rogers848871b2013-08-05 10:56:33 -07001720 DO_STAT(size_interpreter_to_interpreter_bridge_);
1721 DO_STAT(size_interpreter_to_compiled_code_bridge_);
1722 DO_STAT(size_jni_dlsym_lookup_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001723 DO_STAT(size_quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001724 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001725 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001726 DO_STAT(size_quick_to_interpreter_bridge_);
1727 DO_STAT(size_trampoline_alignment_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001728 DO_STAT(size_method_header_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001729 DO_STAT(size_code_);
1730 DO_STAT(size_code_alignment_);
Vladimir Markof4da6752014-08-01 19:04:18 +01001731 DO_STAT(size_relative_call_thunks_);
Vladimir Markoc74658b2015-03-31 10:26:41 +01001732 DO_STAT(size_misc_thunks_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001733 DO_STAT(size_vmap_table_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001734 DO_STAT(size_oat_dex_file_location_size_);
1735 DO_STAT(size_oat_dex_file_location_data_);
1736 DO_STAT(size_oat_dex_file_location_checksum_);
1737 DO_STAT(size_oat_dex_file_offset_);
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001738 DO_STAT(size_oat_dex_file_class_offsets_offset_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001739 DO_STAT(size_oat_dex_file_lookup_table_offset_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001740 DO_STAT(size_oat_lookup_table_alignment_);
1741 DO_STAT(size_oat_lookup_table_);
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001742 DO_STAT(size_oat_class_offsets_alignment_);
1743 DO_STAT(size_oat_class_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001744 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001745 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001746 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001747 DO_STAT(size_oat_class_method_offsets_);
1748 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001749
David Brazdil7b49e6c2016-09-01 11:06:18 +01001750 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)";
1751
1752 CHECK_EQ(vdex_size_ + oat_size_, size_total);
1753 CHECK_EQ(file_offset + size_total - vdex_size_, static_cast<size_t>(oat_end_file_offset));
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001754 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001755
David Brazdil7b49e6c2016-09-01 11:06:18 +01001756 CHECK_EQ(file_offset + oat_size_, static_cast<size_t>(oat_end_file_offset));
1757 CHECK_EQ(oat_size_, relative_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001758
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001759 write_state_ = WriteState::kWriteHeader;
1760 return true;
1761}
1762
1763bool OatWriter::WriteHeader(OutputStream* out,
1764 uint32_t image_file_location_oat_checksum,
1765 uintptr_t image_file_location_oat_begin,
1766 int32_t image_patch_delta) {
1767 CHECK(write_state_ == WriteState::kWriteHeader);
1768
1769 oat_header_->SetImageFileLocationOatChecksum(image_file_location_oat_checksum);
1770 oat_header_->SetImageFileLocationOatDataBegin(image_file_location_oat_begin);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001771 if (compiler_driver_->GetCompilerOptions().IsBootImage()) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001772 CHECK_EQ(image_patch_delta, 0);
1773 CHECK_EQ(oat_header_->GetImagePatchDelta(), 0);
1774 } else {
1775 CHECK_ALIGNED(image_patch_delta, kPageSize);
1776 oat_header_->SetImagePatchDelta(image_patch_delta);
1777 }
Vladimir Marko49b0f452015-12-10 13:49:19 +00001778 oat_header_->UpdateChecksumWithHeaderData();
1779
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001780 const size_t file_offset = oat_data_offset_;
1781
1782 off_t current_offset = out->Seek(0, kSeekCurrent);
1783 if (current_offset == static_cast<off_t>(-1)) {
1784 PLOG(ERROR) << "Failed to get current offset from " << out->GetLocation();
1785 return false;
1786 }
Vladimir Marko49b0f452015-12-10 13:49:19 +00001787 if (out->Seek(file_offset, kSeekSet) == static_cast<off_t>(-1)) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001788 PLOG(ERROR) << "Failed to seek to oat header position in " << out->GetLocation();
1789 return false;
1790 }
David Srbeckybc90fd02015-04-22 19:40:27 +01001791 DCHECK_EQ(file_offset, static_cast<size_t>(out->Seek(0, kSeekCurrent)));
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001792
1793 // Flush all other data before writing the header.
1794 if (!out->Flush()) {
1795 PLOG(ERROR) << "Failed to flush before writing oat header to " << out->GetLocation();
1796 return false;
1797 }
1798 // Write the header.
1799 size_t header_size = oat_header_->GetHeaderSize();
Vladimir Marko49b0f452015-12-10 13:49:19 +00001800 if (!out->WriteFully(oat_header_.get(), header_size)) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001801 PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation();
1802 return false;
1803 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001804 // Flush the header data.
1805 if (!out->Flush()) {
1806 PLOG(ERROR) << "Failed to flush after writing oat header to " << out->GetLocation();
Vladimir Markof4da6752014-08-01 19:04:18 +01001807 return false;
1808 }
Vladimir Markof4da6752014-08-01 19:04:18 +01001809
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001810 if (out->Seek(current_offset, kSeekSet) == static_cast<off_t>(-1)) {
1811 PLOG(ERROR) << "Failed to seek back after writing oat header to " << out->GetLocation();
1812 return false;
1813 }
1814 DCHECK_EQ(current_offset, out->Seek(0, kSeekCurrent));
1815
1816 write_state_ = WriteState::kDone;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001817 return true;
1818}
1819
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001820bool OatWriter::WriteClassOffsets(OutputStream* out) {
1821 for (OatDexFile& oat_dex_file : oat_dex_files_) {
1822 if (oat_dex_file.class_offsets_offset_ != 0u) {
1823 uint32_t expected_offset = oat_data_offset_ + oat_dex_file.class_offsets_offset_;
1824 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
1825 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
1826 PLOG(ERROR) << "Failed to seek to oat class offsets section. Actual: " << actual_offset
1827 << " Expected: " << expected_offset << " File: " << oat_dex_file.GetLocation();
Vladimir Marko919f5532016-01-20 19:13:01 +00001828 return false;
1829 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001830 if (!oat_dex_file.WriteClassOffsets(this, out)) {
1831 return false;
1832 }
1833 }
1834 }
1835 return true;
1836}
1837
1838bool OatWriter::WriteClasses(OutputStream* out) {
1839 for (OatClass& oat_class : oat_classes_) {
1840 if (!oat_class.Write(this, out, oat_data_offset_)) {
1841 PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation();
1842 return false;
Vladimir Marko919f5532016-01-20 19:13:01 +00001843 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001844 }
1845 return true;
1846}
1847
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001848size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001849 size_t vmap_tables_offset = relative_offset;
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001850 WriteMapMethodVisitor visitor(this, out, file_offset, relative_offset);
1851 if (UNLIKELY(!VisitDexMethods(&visitor))) {
1852 return 0;
1853 }
1854 relative_offset = visitor.GetOffset();
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001855 size_vmap_table_ = relative_offset - vmap_tables_offset;
1856
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001857 return relative_offset;
1858}
1859
1860size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001861 if (compiler_driver_->GetCompilerOptions().IsBootImage()) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001862 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001863
Ian Rogers848871b2013-08-05 10:56:33 -07001864 #define DO_TRAMPOLINE(field) \
1865 do { \
1866 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
1867 uint32_t alignment_padding = aligned_offset - relative_offset; \
Ian Rogers3d504072014-03-01 09:16:49 -08001868 out->Seek(alignment_padding, kSeekCurrent); \
Ian Rogers848871b2013-08-05 10:56:33 -07001869 size_trampoline_alignment_ += alignment_padding; \
Vladimir Markoe079e212016-05-25 12:49:49 +01001870 if (!out->WriteFully((field)->data(), (field)->size())) { \
Ian Rogers3d504072014-03-01 09:16:49 -08001871 PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \
Ian Rogers848871b2013-08-05 10:56:33 -07001872 return false; \
1873 } \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -07001874 size_ ## field += (field)->size(); \
1875 relative_offset += alignment_padding + (field)->size(); \
Ian Rogers848871b2013-08-05 10:56:33 -07001876 DCHECK_OFFSET(); \
1877 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001878
Ian Rogers848871b2013-08-05 10:56:33 -07001879 DO_TRAMPOLINE(jni_dlsym_lookup_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001880 DO_TRAMPOLINE(quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001881 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001882 DO_TRAMPOLINE(quick_resolution_trampoline_);
1883 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
1884 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001885 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001886 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001887}
1888
Ian Rogers3d504072014-03-01 09:16:49 -08001889size_t OatWriter::WriteCodeDexFiles(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001890 const size_t file_offset,
1891 size_t relative_offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001892 #define VISIT(VisitorType) \
1893 do { \
1894 VisitorType visitor(this, out, file_offset, relative_offset); \
1895 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1896 return 0; \
1897 } \
1898 relative_offset = visitor.GetOffset(); \
1899 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001900
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001901 VISIT(WriteCodeMethodVisitor);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001902
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001903 #undef VISIT
Brian Carlstrom265091e2013-01-30 14:08:26 -08001904
Vladimir Markob163bb72015-03-31 21:49:49 +01001905 size_code_alignment_ += relative_patcher_->CodeAlignmentSize();
1906 size_relative_call_thunks_ += relative_patcher_->RelativeCallThunksSize();
1907 size_misc_thunks_ += relative_patcher_->MiscThunksSize();
1908
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001909 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001910}
1911
Vladimir Marko944da602016-02-19 12:27:55 +00001912bool OatWriter::RecordOatDataOffset(OutputStream* out) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001913 // Get the elf file offset of the oat file.
1914 const off_t raw_file_offset = out->Seek(0, kSeekCurrent);
1915 if (raw_file_offset == static_cast<off_t>(-1)) {
1916 LOG(ERROR) << "Failed to get file offset in " << out->GetLocation();
1917 return false;
1918 }
1919 oat_data_offset_ = static_cast<size_t>(raw_file_offset);
1920 return true;
1921}
1922
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001923bool OatWriter::ReadDexFileHeader(File* file, OatDexFile* oat_dex_file) {
1924 // Read the dex file header and perform minimal verification.
1925 uint8_t raw_header[sizeof(DexFile::Header)];
1926 if (!file->ReadFully(&raw_header, sizeof(DexFile::Header))) {
1927 PLOG(ERROR) << "Failed to read dex file header. Actual: "
1928 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
1929 return false;
1930 }
1931 if (!ValidateDexFileHeader(raw_header, oat_dex_file->GetLocation())) {
1932 return false;
1933 }
1934
1935 const UnalignedDexFileHeader* header = AsUnalignedDexFileHeader(raw_header);
1936 oat_dex_file->dex_file_size_ = header->file_size_;
1937 oat_dex_file->dex_file_location_checksum_ = header->checksum_;
1938 oat_dex_file->class_offsets_.resize(header->class_defs_size_);
1939 return true;
1940}
1941
1942bool OatWriter::ValidateDexFileHeader(const uint8_t* raw_header, const char* location) {
1943 if (!DexFile::IsMagicValid(raw_header)) {
1944 LOG(ERROR) << "Invalid magic number in dex file header. " << " File: " << location;
1945 return false;
1946 }
1947 if (!DexFile::IsVersionValid(raw_header)) {
1948 LOG(ERROR) << "Invalid version number in dex file header. " << " File: " << location;
1949 return false;
1950 }
1951 const UnalignedDexFileHeader* header = AsUnalignedDexFileHeader(raw_header);
1952 if (header->file_size_ < sizeof(DexFile::Header)) {
1953 LOG(ERROR) << "Dex file header specifies file size insufficient to contain the header."
1954 << " File: " << location;
1955 return false;
1956 }
1957 return true;
1958}
1959
David Brazdil7b49e6c2016-09-01 11:06:18 +01001960bool OatWriter::WriteDexFiles(OutputStream* out, File* file) {
1961 TimingLogger::ScopedTiming split("Write Dex files", timings_);
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001962
David Brazdil7b49e6c2016-09-01 11:06:18 +01001963 vdex_dex_files_offset_ = vdex_size_;
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001964
1965 // Write dex files.
1966 for (OatDexFile& oat_dex_file : oat_dex_files_) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001967 if (!WriteDexFile(out, file, &oat_dex_file)) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001968 return false;
1969 }
1970 }
1971
1972 // Close sources.
1973 for (OatDexFile& oat_dex_file : oat_dex_files_) {
1974 oat_dex_file.source_.Clear(); // Get rid of the reference, it's about to be invalidated.
1975 }
1976 zipped_dex_files_.clear();
1977 zip_archives_.clear();
1978 raw_dex_files_.clear();
1979 return true;
1980}
1981
David Brazdil7b49e6c2016-09-01 11:06:18 +01001982bool OatWriter::WriteDexFile(OutputStream* out, File* file, OatDexFile* oat_dex_file) {
1983 if (!SeekToDexFile(out, file, oat_dex_file)) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001984 return false;
1985 }
1986 if (oat_dex_file->source_.IsZipEntry()) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001987 if (!WriteDexFile(out, file, oat_dex_file, oat_dex_file->source_.GetZipEntry())) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001988 return false;
1989 }
1990 } else if (oat_dex_file->source_.IsRawFile()) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001991 if (!WriteDexFile(out, file, oat_dex_file, oat_dex_file->source_.GetRawFile())) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001992 return false;
1993 }
1994 } else {
1995 DCHECK(oat_dex_file->source_.IsRawData());
David Brazdil7b49e6c2016-09-01 11:06:18 +01001996 if (!WriteDexFile(out, oat_dex_file, oat_dex_file->source_.GetRawData())) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001997 return false;
1998 }
1999 }
2000
2001 // Update current size and account for the written data.
David Brazdil7b49e6c2016-09-01 11:06:18 +01002002 if (kIsVdexEnabled) {
2003 DCHECK_EQ(vdex_size_, oat_dex_file->dex_file_offset_);
2004 vdex_size_ += oat_dex_file->dex_file_size_;
2005 } else {
2006 DCHECK_EQ(oat_size_, oat_dex_file->dex_file_offset_);
2007 oat_size_ += oat_dex_file->dex_file_size_;
2008 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002009 size_dex_file_ += oat_dex_file->dex_file_size_;
2010 return true;
2011}
2012
2013bool OatWriter::SeekToDexFile(OutputStream* out, File* file, OatDexFile* oat_dex_file) {
2014 // Dex files are required to be 4 byte aligned.
David Brazdil7b49e6c2016-09-01 11:06:18 +01002015 size_t initial_offset = kIsVdexEnabled ? vdex_size_ : oat_size_;
2016 size_t start_offset = RoundUp(initial_offset, 4);
2017 size_t file_offset = kIsVdexEnabled ? start_offset : (oat_data_offset_ + start_offset);
2018 size_dex_file_alignment_ += start_offset - initial_offset;
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002019
2020 // Seek to the start of the dex file and flush any pending operations in the stream.
2021 // Verify that, after flushing the stream, the file is at the same offset as the stream.
David Brazdil7b49e6c2016-09-01 11:06:18 +01002022 off_t actual_offset = out->Seek(file_offset, kSeekSet);
2023 if (actual_offset != static_cast<off_t>(file_offset)) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002024 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
David Brazdil7b49e6c2016-09-01 11:06:18 +01002025 << " Expected: " << file_offset
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002026 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2027 return false;
2028 }
2029 if (!out->Flush()) {
2030 PLOG(ERROR) << "Failed to flush before writing dex file."
2031 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2032 return false;
2033 }
2034 actual_offset = lseek(file->Fd(), 0, SEEK_CUR);
David Brazdil7b49e6c2016-09-01 11:06:18 +01002035 if (actual_offset != static_cast<off_t>(file_offset)) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002036 PLOG(ERROR) << "Stream/file position mismatch! Actual: " << actual_offset
David Brazdil7b49e6c2016-09-01 11:06:18 +01002037 << " Expected: " << file_offset
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002038 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2039 return false;
2040 }
2041
David Brazdil7b49e6c2016-09-01 11:06:18 +01002042 if (kIsVdexEnabled) {
2043 vdex_size_ = start_offset;
2044 } else {
2045 oat_size_ = start_offset;
2046 }
2047 oat_dex_file->dex_file_offset_ = start_offset;
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002048 return true;
2049}
2050
David Brazdil7b49e6c2016-09-01 11:06:18 +01002051bool OatWriter::WriteDexFile(OutputStream* out,
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002052 File* file,
2053 OatDexFile* oat_dex_file,
2054 ZipEntry* dex_file) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01002055 size_t start_offset = kIsVdexEnabled ? vdex_size_ : oat_data_offset_ + oat_size_;
2056 DCHECK_EQ(static_cast<off_t>(start_offset), out->Seek(0, kSeekCurrent));
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002057
2058 // Extract the dex file and get the extracted size.
2059 std::string error_msg;
2060 if (!dex_file->ExtractToFile(*file, &error_msg)) {
2061 LOG(ERROR) << "Failed to extract dex file from ZIP entry: " << error_msg
2062 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2063 return false;
2064 }
2065 if (file->Flush() != 0) {
2066 PLOG(ERROR) << "Failed to flush dex file from ZIP entry."
2067 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2068 return false;
2069 }
2070 off_t extracted_end = lseek(file->Fd(), 0, SEEK_CUR);
2071 if (extracted_end == static_cast<off_t>(-1)) {
2072 PLOG(ERROR) << "Failed get end offset after writing dex file from ZIP entry."
2073 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2074 return false;
2075 }
2076 if (extracted_end < static_cast<off_t>(start_offset)) {
2077 LOG(ERROR) << "Dex file end position is before start position! End: " << extracted_end
2078 << " Start: " << start_offset
2079 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2080 return false;
2081 }
2082 uint64_t extracted_size = static_cast<uint64_t>(extracted_end - start_offset);
2083 if (extracted_size < sizeof(DexFile::Header)) {
2084 LOG(ERROR) << "Extracted dex file is shorter than dex file header. size: "
2085 << extracted_size << " File: " << oat_dex_file->GetLocation();
2086 return false;
2087 }
2088
2089 // Read the dex file header and extract required data to OatDexFile.
2090 off_t actual_offset = lseek(file->Fd(), start_offset, SEEK_SET);
2091 if (actual_offset != static_cast<off_t>(start_offset)) {
2092 PLOG(ERROR) << "Failed to seek back to dex file header. Actual: " << actual_offset
2093 << " Expected: " << start_offset
2094 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2095 return false;
2096 }
2097 if (!ReadDexFileHeader(file, oat_dex_file)) {
2098 return false;
2099 }
2100 if (extracted_size < oat_dex_file->dex_file_size_) {
2101 LOG(ERROR) << "Extracted truncated dex file. Extracted size: " << extracted_size
2102 << " file size from header: " << oat_dex_file->dex_file_size_
2103 << " File: " << oat_dex_file->GetLocation();
2104 return false;
2105 }
2106
2107 // Override the checksum from header with the CRC from ZIP entry.
2108 oat_dex_file->dex_file_location_checksum_ = dex_file->GetCrc32();
2109
2110 // Seek both file and stream to the end offset.
2111 size_t end_offset = start_offset + oat_dex_file->dex_file_size_;
2112 actual_offset = lseek(file->Fd(), end_offset, SEEK_SET);
2113 if (actual_offset != static_cast<off_t>(end_offset)) {
2114 PLOG(ERROR) << "Failed to seek to end of dex file. Actual: " << actual_offset
2115 << " Expected: " << end_offset
2116 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2117 return false;
2118 }
David Brazdil7b49e6c2016-09-01 11:06:18 +01002119 actual_offset = out->Seek(end_offset, kSeekSet);
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002120 if (actual_offset != static_cast<off_t>(end_offset)) {
2121 PLOG(ERROR) << "Failed to seek stream to end of dex file. Actual: " << actual_offset
2122 << " Expected: " << end_offset << " File: " << oat_dex_file->GetLocation();
2123 return false;
2124 }
David Brazdil7b49e6c2016-09-01 11:06:18 +01002125 if (!out->Flush()) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002126 PLOG(ERROR) << "Failed to flush stream after seeking over dex file."
2127 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2128 return false;
2129 }
2130
2131 // If we extracted more than the size specified in the header, truncate the file.
2132 if (extracted_size > oat_dex_file->dex_file_size_) {
2133 if (file->SetLength(end_offset) != 0) {
2134 PLOG(ERROR) << "Failed to truncate excessive dex file length."
David Brazdil7b49e6c2016-09-01 11:06:18 +01002135 << " File: " << oat_dex_file->GetLocation()
2136 << " Output: " << file->GetPath();
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002137 return false;
2138 }
2139 }
2140
2141 return true;
2142}
2143
David Brazdil7b49e6c2016-09-01 11:06:18 +01002144bool OatWriter::WriteDexFile(OutputStream* out,
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002145 File* file,
2146 OatDexFile* oat_dex_file,
2147 File* dex_file) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01002148 size_t start_offset = kIsVdexEnabled ? vdex_size_ : oat_data_offset_ + oat_size_;
2149 DCHECK_EQ(static_cast<off_t>(start_offset), out->Seek(0, kSeekCurrent));
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002150
2151 off_t input_offset = lseek(dex_file->Fd(), 0, SEEK_SET);
2152 if (input_offset != static_cast<off_t>(0)) {
2153 PLOG(ERROR) << "Failed to seek to dex file header. Actual: " << input_offset
2154 << " Expected: 0"
2155 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2156 return false;
2157 }
2158 if (!ReadDexFileHeader(dex_file, oat_dex_file)) {
2159 return false;
2160 }
2161
2162 // Copy the input dex file using sendfile().
2163 if (!file->Copy(dex_file, 0, oat_dex_file->dex_file_size_)) {
2164 PLOG(ERROR) << "Failed to copy dex file to oat file."
2165 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2166 return false;
2167 }
2168 if (file->Flush() != 0) {
2169 PLOG(ERROR) << "Failed to flush dex file."
2170 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2171 return false;
2172 }
2173
2174 // Check file position and seek the stream to the end offset.
2175 size_t end_offset = start_offset + oat_dex_file->dex_file_size_;
2176 off_t actual_offset = lseek(file->Fd(), 0, SEEK_CUR);
2177 if (actual_offset != static_cast<off_t>(end_offset)) {
2178 PLOG(ERROR) << "Unexpected file position after copying dex file. Actual: " << actual_offset
2179 << " Expected: " << end_offset
2180 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2181 return false;
2182 }
David Brazdil7b49e6c2016-09-01 11:06:18 +01002183 actual_offset = out->Seek(end_offset, kSeekSet);
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002184 if (actual_offset != static_cast<off_t>(end_offset)) {
2185 PLOG(ERROR) << "Failed to seek stream to end of dex file. Actual: " << actual_offset
2186 << " Expected: " << end_offset << " File: " << oat_dex_file->GetLocation();
2187 return false;
2188 }
David Brazdil7b49e6c2016-09-01 11:06:18 +01002189 if (!out->Flush()) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002190 PLOG(ERROR) << "Failed to flush stream after seeking over dex file."
2191 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2192 return false;
2193 }
2194
2195 return true;
2196}
2197
David Brazdil7b49e6c2016-09-01 11:06:18 +01002198bool OatWriter::WriteDexFile(OutputStream* out,
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002199 OatDexFile* oat_dex_file,
2200 const uint8_t* dex_file) {
2201 // Note: The raw data has already been checked to contain the header
2202 // and all the data that the header specifies as the file size.
2203 DCHECK(dex_file != nullptr);
2204 DCHECK(ValidateDexFileHeader(dex_file, oat_dex_file->GetLocation()));
2205 const UnalignedDexFileHeader* header = AsUnalignedDexFileHeader(dex_file);
2206
David Brazdil7b49e6c2016-09-01 11:06:18 +01002207 if (!out->WriteFully(dex_file, header->file_size_)) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002208 PLOG(ERROR) << "Failed to write dex file " << oat_dex_file->GetLocation()
David Brazdil7b49e6c2016-09-01 11:06:18 +01002209 << " to " << out->GetLocation();
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002210 return false;
2211 }
David Brazdil7b49e6c2016-09-01 11:06:18 +01002212 if (!out->Flush()) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002213 PLOG(ERROR) << "Failed to flush stream after writing dex file."
2214 << " File: " << oat_dex_file->GetLocation();
2215 return false;
2216 }
2217
2218 // Update dex file size and resize class offsets in the OatDexFile.
2219 // Note: For raw data, the checksum is passed directly to AddRawDexFileSource().
2220 oat_dex_file->dex_file_size_ = header->file_size_;
2221 oat_dex_file->class_offsets_.resize(header->class_defs_size_);
2222 return true;
2223}
2224
2225bool OatWriter::WriteOatDexFiles(OutputStream* rodata) {
2226 TimingLogger::ScopedTiming split("WriteOatDexFiles", timings_);
2227
David Brazdil181e1cc2016-09-01 16:38:47 +00002228 off_t initial_offset = rodata->Seek(0, kSeekCurrent);
2229 if (initial_offset == static_cast<off_t>(-1)) {
2230 LOG(ERROR) << "Failed to get current position in " << rodata->GetLocation();
2231 return false;
2232 }
2233
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002234 // Seek to the start of OatDexFiles, i.e. to the end of the OatHeader. If there are
2235 // no OatDexFiles, no data is actually written to .rodata before WriteHeader() and
2236 // this Seek() ensures that we reserve the space for OatHeader in .rodata.
2237 DCHECK(oat_dex_files_.empty() || oat_dex_files_[0u].offset_ == oat_header_->GetHeaderSize());
2238 uint32_t expected_offset = oat_data_offset_ + oat_header_->GetHeaderSize();
2239 off_t actual_offset = rodata->Seek(expected_offset, kSeekSet);
2240 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
2241 PLOG(ERROR) << "Failed to seek to OatDexFile table section. Actual: " << actual_offset
2242 << " Expected: " << expected_offset << " File: " << rodata->GetLocation();
2243 return false;
2244 }
2245
2246 for (size_t i = 0, size = oat_dex_files_.size(); i != size; ++i) {
2247 OatDexFile* oat_dex_file = &oat_dex_files_[i];
2248
2249 DCHECK_EQ(oat_data_offset_ + oat_dex_file->offset_,
2250 static_cast<size_t>(rodata->Seek(0, kSeekCurrent)));
2251
2252 // Write OatDexFile.
2253 if (!oat_dex_file->Write(this, rodata)) {
2254 PLOG(ERROR) << "Failed to write oat dex information to " << rodata->GetLocation();
2255 return false;
2256 }
2257 }
2258
David Brazdil181e1cc2016-09-01 16:38:47 +00002259 // Seek back to the initial position.
2260 if (rodata->Seek(initial_offset, kSeekSet) != initial_offset) {
2261 PLOG(ERROR) << "Failed to seek to initial position. Actual: " << actual_offset
2262 << " Expected: " << initial_offset << " File: " << rodata->GetLocation();
2263 return false;
2264 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002265
David Brazdilb92ba622016-09-01 16:00:30 +00002266 return true;
2267}
2268
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002269bool OatWriter::OpenDexFiles(
2270 File* file,
Andreas Gampe3a2bd292016-01-26 17:23:47 -08002271 bool verify,
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002272 /*out*/ std::unique_ptr<MemMap>* opened_dex_files_map,
2273 /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files) {
2274 TimingLogger::ScopedTiming split("OpenDexFiles", timings_);
2275
2276 if (oat_dex_files_.empty()) {
2277 // Nothing to do.
2278 return true;
2279 }
2280
2281 size_t map_offset = oat_dex_files_[0].dex_file_offset_;
David Brazdil7b49e6c2016-09-01 11:06:18 +01002282 size_t length = kIsVdexEnabled ? (vdex_size_ - map_offset) : (oat_size_ - map_offset);
2283
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002284 std::string error_msg;
David Brazdil7b49e6c2016-09-01 11:06:18 +01002285 std::unique_ptr<MemMap> dex_files_map(MemMap::MapFile(
2286 length,
2287 PROT_READ | PROT_WRITE,
2288 MAP_SHARED,
2289 file->Fd(),
2290 kIsVdexEnabled ? map_offset : (oat_data_offset_ + map_offset),
2291 /* low_4gb */ false,
2292 file->GetPath().c_str(),
2293 &error_msg));
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002294 if (dex_files_map == nullptr) {
2295 LOG(ERROR) << "Failed to mmap() dex files from oat file. File: " << file->GetPath()
2296 << " error: " << error_msg;
2297 return false;
2298 }
2299 std::vector<std::unique_ptr<const DexFile>> dex_files;
2300 for (OatDexFile& oat_dex_file : oat_dex_files_) {
2301 // Make sure no one messed with input files while we were copying data.
2302 // At the very least we need consistent file size and number of class definitions.
2303 const uint8_t* raw_dex_file =
2304 dex_files_map->Begin() + oat_dex_file.dex_file_offset_ - map_offset;
2305 if (!ValidateDexFileHeader(raw_dex_file, oat_dex_file.GetLocation())) {
2306 // Note: ValidateDexFileHeader() already logged an error message.
2307 LOG(ERROR) << "Failed to verify written dex file header!"
2308 << " Output: " << file->GetPath() << " ~ " << std::hex << map_offset
2309 << " ~ " << static_cast<const void*>(raw_dex_file);
2310 return false;
2311 }
2312 const UnalignedDexFileHeader* header = AsUnalignedDexFileHeader(raw_dex_file);
2313 if (header->file_size_ != oat_dex_file.dex_file_size_) {
2314 LOG(ERROR) << "File size mismatch in written dex file header! Expected: "
2315 << oat_dex_file.dex_file_size_ << " Actual: " << header->file_size_
2316 << " Output: " << file->GetPath();
2317 return false;
2318 }
2319 if (header->class_defs_size_ != oat_dex_file.class_offsets_.size()) {
2320 LOG(ERROR) << "Class defs size mismatch in written dex file header! Expected: "
2321 << oat_dex_file.class_offsets_.size() << " Actual: " << header->class_defs_size_
2322 << " Output: " << file->GetPath();
2323 return false;
2324 }
2325
2326 // Now, open the dex file.
2327 dex_files.emplace_back(DexFile::Open(raw_dex_file,
2328 oat_dex_file.dex_file_size_,
2329 oat_dex_file.GetLocation(),
2330 oat_dex_file.dex_file_location_checksum_,
2331 /* oat_dex_file */ nullptr,
Andreas Gampe3a2bd292016-01-26 17:23:47 -08002332 verify,
Aart Bik37d6a3b2016-06-21 18:30:10 -07002333 verify,
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002334 &error_msg));
2335 if (dex_files.back() == nullptr) {
Andreas Gampe3a2bd292016-01-26 17:23:47 -08002336 LOG(ERROR) << "Failed to open dex file from oat file. File: " << oat_dex_file.GetLocation()
2337 << " Error: " << error_msg;
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002338 return false;
2339 }
2340 }
2341
2342 *opened_dex_files_map = std::move(dex_files_map);
2343 *opened_dex_files = std::move(dex_files);
2344 return true;
2345}
2346
2347bool OatWriter::WriteTypeLookupTables(
David Brazdil7b49e6c2016-09-01 11:06:18 +01002348 OutputStream* oat_rodata,
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002349 const std::vector<std::unique_ptr<const DexFile>>& opened_dex_files) {
2350 TimingLogger::ScopedTiming split("WriteTypeLookupTables", timings_);
2351
David Brazdil7b49e6c2016-09-01 11:06:18 +01002352 uint32_t expected_offset = oat_data_offset_ + oat_size_;
2353 off_t actual_offset = oat_rodata->Seek(expected_offset, kSeekSet);
2354 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
2355 PLOG(ERROR) << "Failed to seek to TypeLookupTable section. Actual: " << actual_offset
2356 << " Expected: " << expected_offset << " File: " << oat_rodata->GetLocation();
2357 return false;
2358 }
2359
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002360 DCHECK_EQ(opened_dex_files.size(), oat_dex_files_.size());
2361 for (size_t i = 0, size = opened_dex_files.size(); i != size; ++i) {
2362 OatDexFile* oat_dex_file = &oat_dex_files_[i];
David Brazdil181e1cc2016-09-01 16:38:47 +00002363 DCHECK_EQ(oat_dex_file->lookup_table_offset_, 0u);
2364
2365 if (oat_dex_file->create_type_lookup_table_ != CreateTypeLookupTable::kCreate ||
2366 oat_dex_file->class_offsets_.empty()) {
2367 continue;
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002368 }
David Brazdil181e1cc2016-09-01 16:38:47 +00002369
2370 size_t table_size = TypeLookupTable::RawDataLength(oat_dex_file->class_offsets_.size());
2371 if (table_size == 0u) {
2372 continue;
2373 }
2374
2375 // Create the lookup table. When `nullptr` is given as the storage buffer,
David Sehr9aa352e2016-09-15 18:13:52 -07002376 // TypeLookupTable allocates its own and OatDexFile takes ownership.
2377 oat_dex_file->InitTypeLookupTable(*opened_dex_files[i], /* storage */ nullptr);
2378 TypeLookupTable* table = oat_dex_file->GetTypeLookupTable();
David Brazdil181e1cc2016-09-01 16:38:47 +00002379
2380 // Type tables are required to be 4 byte aligned.
David Brazdil7b49e6c2016-09-01 11:06:18 +01002381 size_t initial_offset = oat_size_;
2382 size_t rodata_offset = RoundUp(initial_offset, 4);
2383 size_t padding_size = rodata_offset - initial_offset;
David Brazdil181e1cc2016-09-01 16:38:47 +00002384
2385 if (padding_size != 0u) {
2386 std::vector<uint8_t> buffer(padding_size, 0u);
David Brazdil7b49e6c2016-09-01 11:06:18 +01002387 if (!oat_rodata->WriteFully(buffer.data(), padding_size)) {
David Brazdil181e1cc2016-09-01 16:38:47 +00002388 PLOG(ERROR) << "Failed to write lookup table alignment padding."
2389 << " File: " << oat_dex_file->GetLocation()
David Brazdil7b49e6c2016-09-01 11:06:18 +01002390 << " Output: " << oat_rodata->GetLocation();
David Brazdil181e1cc2016-09-01 16:38:47 +00002391 return false;
2392 }
2393 }
2394
2395 DCHECK_EQ(oat_data_offset_ + rodata_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +01002396 static_cast<size_t>(oat_rodata->Seek(0u, kSeekCurrent)));
David Brazdil181e1cc2016-09-01 16:38:47 +00002397 DCHECK_EQ(table_size, table->RawDataLength());
2398
David Brazdil7b49e6c2016-09-01 11:06:18 +01002399 if (!oat_rodata->WriteFully(table->RawData(), table_size)) {
David Brazdil181e1cc2016-09-01 16:38:47 +00002400 PLOG(ERROR) << "Failed to write lookup table."
2401 << " File: " << oat_dex_file->GetLocation()
David Brazdil7b49e6c2016-09-01 11:06:18 +01002402 << " Output: " << oat_rodata->GetLocation();
David Brazdil181e1cc2016-09-01 16:38:47 +00002403 return false;
2404 }
2405
2406 oat_dex_file->lookup_table_offset_ = rodata_offset;
2407
David Brazdil7b49e6c2016-09-01 11:06:18 +01002408 oat_size_ += padding_size + table_size;
David Brazdil181e1cc2016-09-01 16:38:47 +00002409 size_oat_lookup_table_ += table_size;
2410 size_oat_lookup_table_alignment_ += padding_size;
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002411 }
2412
David Brazdil7b49e6c2016-09-01 11:06:18 +01002413 if (!oat_rodata->Flush()) {
David Brazdil181e1cc2016-09-01 16:38:47 +00002414 PLOG(ERROR) << "Failed to flush stream after writing type lookup tables."
David Brazdil7b49e6c2016-09-01 11:06:18 +01002415 << " File: " << oat_rodata->GetLocation();
2416 return false;
2417 }
2418
2419 return true;
2420}
2421
2422bool OatWriter::WriteVdexHeader(OutputStream* vdex_out) {
David Brazdil5d5a36b2016-09-14 15:34:10 +01002423 if (!kIsVdexEnabled) {
2424 return true;
2425 }
David Brazdil7b49e6c2016-09-01 11:06:18 +01002426 off_t actual_offset = vdex_out->Seek(0, kSeekSet);
2427 if (actual_offset != 0) {
2428 PLOG(ERROR) << "Failed to seek to the beginning of vdex file. Actual: " << actual_offset
2429 << " File: " << vdex_out->GetLocation();
2430 return false;
2431 }
2432
David Brazdil5d5a36b2016-09-14 15:34:10 +01002433 DCHECK_NE(vdex_dex_files_offset_, 0u);
2434 DCHECK_NE(vdex_verifier_deps_offset_, 0u);
2435
2436 size_t dex_section_size = vdex_verifier_deps_offset_ - vdex_dex_files_offset_;
2437 size_t verifier_deps_section_size = vdex_size_ - vdex_verifier_deps_offset_;
2438
2439 VdexFile::Header vdex_header(dex_section_size, verifier_deps_section_size);
David Brazdil7b49e6c2016-09-01 11:06:18 +01002440 if (!vdex_out->WriteFully(&vdex_header, sizeof(VdexFile::Header))) {
2441 PLOG(ERROR) << "Failed to write vdex header. File: " << vdex_out->GetLocation();
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002442 return false;
2443 }
2444
David Brazdil5d5a36b2016-09-14 15:34:10 +01002445 if (!vdex_out->Flush()) {
2446 PLOG(ERROR) << "Failed to flush stream after writing to vdex file."
2447 << " File: " << vdex_out->GetLocation();
2448 return false;
2449 }
2450
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002451 return true;
2452}
2453
Vladimir Markof4da6752014-08-01 19:04:18 +01002454bool OatWriter::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) {
2455 static const uint8_t kPadding[] = {
2456 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
2457 };
2458 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
Vladimir Markoe079e212016-05-25 12:49:49 +01002459 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
Vladimir Markof4da6752014-08-01 19:04:18 +01002460 return false;
2461 }
2462 size_code_alignment_ += aligned_code_delta;
2463 return true;
2464}
2465
Vladimir Marko944da602016-02-19 12:27:55 +00002466void OatWriter::SetMultiOatRelativePatcherAdjustment() {
2467 DCHECK(dex_files_ != nullptr);
2468 DCHECK(relative_patcher_ != nullptr);
2469 DCHECK_NE(oat_data_offset_, 0u);
2470 if (image_writer_ != nullptr && !dex_files_->empty()) {
2471 // The oat data begin may not be initialized yet but the oat file offset is ready.
2472 size_t oat_index = image_writer_->GetOatIndexForDexFile(dex_files_->front());
2473 size_t elf_file_offset = image_writer_->GetOatFileOffset(oat_index);
2474 relative_patcher_->StartOatFile(elf_file_offset + oat_data_offset_);
Vladimir Markob163bb72015-03-31 21:49:49 +01002475 }
2476}
2477
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002478OatWriter::OatDexFile::OatDexFile(const char* dex_file_location,
2479 DexFileSource source,
2480 CreateTypeLookupTable create_type_lookup_table)
2481 : source_(source),
2482 create_type_lookup_table_(create_type_lookup_table),
2483 dex_file_size_(0),
2484 offset_(0),
2485 dex_file_location_size_(strlen(dex_file_location)),
2486 dex_file_location_data_(dex_file_location),
2487 dex_file_location_checksum_(0u),
2488 dex_file_offset_(0u),
2489 class_offsets_offset_(0u),
2490 lookup_table_offset_(0u),
2491 class_offsets_() {
Brian Carlstrome24fa612011-09-29 00:53:55 -07002492}
2493
2494size_t OatWriter::OatDexFile::SizeOf() const {
2495 return sizeof(dex_file_location_size_)
2496 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -08002497 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -08002498 + sizeof(dex_file_offset_)
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002499 + sizeof(class_offsets_offset_)
2500 + sizeof(lookup_table_offset_);
Brian Carlstrome24fa612011-09-29 00:53:55 -07002501}
2502
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002503void OatWriter::OatDexFile::ReserveClassOffsets(OatWriter* oat_writer) {
2504 DCHECK_EQ(class_offsets_offset_, 0u);
2505 if (!class_offsets_.empty()) {
2506 // Class offsets are required to be 4 byte aligned.
David Brazdil7b49e6c2016-09-01 11:06:18 +01002507 size_t initial_offset = oat_writer->oat_size_;
2508 size_t offset = RoundUp(initial_offset, 4);
2509 oat_writer->size_oat_class_offsets_alignment_ += offset - initial_offset;
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002510 class_offsets_offset_ = offset;
David Brazdil7b49e6c2016-09-01 11:06:18 +01002511 oat_writer->oat_size_ = offset + GetClassOffsetsRawSize();
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002512 }
2513}
2514
2515bool OatWriter::OatDexFile::Write(OatWriter* oat_writer, OutputStream* out) const {
2516 const size_t file_offset = oat_writer->oat_data_offset_;
Brian Carlstrom265091e2013-01-30 14:08:26 -08002517 DCHECK_OFFSET_();
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002518
Vladimir Markoe079e212016-05-25 12:49:49 +01002519 if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08002520 PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07002521 return false;
2522 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07002523 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002524
Vladimir Markoe079e212016-05-25 12:49:49 +01002525 if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
Ian Rogers3d504072014-03-01 09:16:49 -08002526 PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07002527 return false;
2528 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07002529 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002530
Vladimir Markoe079e212016-05-25 12:49:49 +01002531 if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08002532 PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07002533 return false;
2534 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07002535 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002536
Vladimir Markoe079e212016-05-25 12:49:49 +01002537 if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08002538 PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08002539 return false;
2540 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07002541 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002542
Vladimir Markoe079e212016-05-25 12:49:49 +01002543 if (!out->WriteFully(&class_offsets_offset_, sizeof(class_offsets_offset_))) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002544 PLOG(ERROR) << "Failed to write class offsets offset to " << out->GetLocation();
2545 return false;
2546 }
2547 oat_writer->size_oat_dex_file_class_offsets_offset_ += sizeof(class_offsets_offset_);
2548
Vladimir Markoe079e212016-05-25 12:49:49 +01002549 if (!out->WriteFully(&lookup_table_offset_, sizeof(lookup_table_offset_))) {
Artem Udovichenkod9786b02015-10-14 16:36:55 +03002550 PLOG(ERROR) << "Failed to write lookup table offset to " << out->GetLocation();
2551 return false;
2552 }
Vladimir Marko49b0f452015-12-10 13:49:19 +00002553 oat_writer->size_oat_dex_file_lookup_table_offset_ += sizeof(lookup_table_offset_);
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002554
2555 return true;
2556}
2557
2558bool OatWriter::OatDexFile::WriteClassOffsets(OatWriter* oat_writer, OutputStream* out) {
Vladimir Markoe079e212016-05-25 12:49:49 +01002559 if (!out->WriteFully(class_offsets_.data(), GetClassOffsetsRawSize())) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002560 PLOG(ERROR) << "Failed to write oat class offsets for " << GetLocation()
2561 << " to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07002562 return false;
2563 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002564 oat_writer->size_oat_class_offsets_ += GetClassOffsetsRawSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -07002565 return true;
2566}
2567
Brian Carlstromba150c32013-08-27 17:31:03 -07002568OatWriter::OatClass::OatClass(size_t offset,
Vladimir Marko49b0f452015-12-10 13:49:19 +00002569 const dchecked_vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -07002570 uint32_t num_non_null_compiled_methods,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01002571 mirror::Class::Status status)
2572 : compiled_methods_(compiled_methods) {
2573 uint32_t num_methods = compiled_methods.size();
Brian Carlstromba150c32013-08-27 17:31:03 -07002574 CHECK_LE(num_non_null_compiled_methods, num_methods);
2575
Brian Carlstrom265091e2013-01-30 14:08:26 -08002576 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -07002577 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
2578
2579 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
2580 // apply when there are 0 methods, we just arbitrarily say that 0
2581 // methods means kOatClassNoneCompiled and that we won't use
2582 // kOatClassAllCompiled unless there is at least one compiled
2583 // method. This means in an interpretter only system, we can assert
2584 // that all classes are kOatClassNoneCompiled.
2585 if (num_non_null_compiled_methods == 0) {
2586 type_ = kOatClassNoneCompiled;
2587 } else if (num_non_null_compiled_methods == num_methods) {
2588 type_ = kOatClassAllCompiled;
2589 } else {
2590 type_ = kOatClassSomeCompiled;
2591 }
2592
Brian Carlstrom0755ec52012-01-11 15:19:46 -08002593 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -07002594 method_offsets_.resize(num_non_null_compiled_methods);
Vladimir Marko8a630572014-04-09 18:45:35 +01002595 method_headers_.resize(num_non_null_compiled_methods);
Brian Carlstromba150c32013-08-27 17:31:03 -07002596
2597 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
2598 if (type_ == kOatClassSomeCompiled) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00002599 method_bitmap_.reset(new BitVector(num_methods, false, Allocator::GetMallocAllocator()));
Brian Carlstromba150c32013-08-27 17:31:03 -07002600 method_bitmap_size_ = method_bitmap_->GetSizeOf();
2601 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
2602 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
2603 } else {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002604 method_bitmap_ = nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07002605 method_bitmap_size_ = 0;
2606 }
2607
2608 for (size_t i = 0; i < num_methods; i++) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01002609 CompiledMethod* compiled_method = compiled_methods_[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002610 if (compiled_method == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07002611 oat_method_offsets_offsets_from_oat_class_[i] = 0;
2612 } else {
2613 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
2614 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
2615 if (type_ == kOatClassSomeCompiled) {
2616 method_bitmap_->SetBit(i);
2617 }
2618 }
2619 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07002620}
2621
Brian Carlstrom265091e2013-01-30 14:08:26 -08002622size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
2623 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07002624 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
2625 if (method_offset == 0) {
2626 return 0;
2627 }
2628 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08002629}
2630
2631size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
2632 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07002633 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08002634}
2635
2636size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07002637 return sizeof(status_)
2638 + sizeof(type_)
2639 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
2640 + method_bitmap_size_
2641 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07002642}
2643
Brian Carlstromc50d8e12013-07-23 22:35:16 -07002644bool OatWriter::OatClass::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08002645 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07002646 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08002647 DCHECK_OFFSET_();
Vladimir Markoe079e212016-05-25 12:49:49 +01002648 if (!out->WriteFully(&status_, sizeof(status_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08002649 PLOG(ERROR) << "Failed to write class status to " << out->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08002650 return false;
2651 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07002652 oat_writer->size_oat_class_status_ += sizeof(status_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00002653
Vladimir Markoe079e212016-05-25 12:49:49 +01002654 if (!out->WriteFully(&type_, sizeof(type_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08002655 PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07002656 return false;
2657 }
2658 oat_writer->size_oat_class_type_ += sizeof(type_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00002659
Brian Carlstromba150c32013-08-27 17:31:03 -07002660 if (method_bitmap_size_ != 0) {
2661 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markoe079e212016-05-25 12:49:49 +01002662 if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08002663 PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07002664 return false;
2665 }
2666 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00002667
Vladimir Markoe079e212016-05-25 12:49:49 +01002668 if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
Ian Rogers3d504072014-03-01 09:16:49 -08002669 PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07002670 return false;
2671 }
2672 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
2673 }
Vladimir Marko49b0f452015-12-10 13:49:19 +00002674
Vladimir Markoe079e212016-05-25 12:49:49 +01002675 if (!out->WriteFully(method_offsets_.data(), GetMethodOffsetsRawSize())) {
Ian Rogers3d504072014-03-01 09:16:49 -08002676 PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07002677 return false;
2678 }
Vladimir Marko49b0f452015-12-10 13:49:19 +00002679 oat_writer->size_oat_class_method_offsets_ += GetMethodOffsetsRawSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -07002680 return true;
2681}
2682
Brian Carlstrome24fa612011-09-29 00:53:55 -07002683} // namespace art