blob: 904581783263ac8822c2e64a497609235f72689f [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"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070053#include "scoped_thread_state_change.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 Marko5c42c292015-02-25 12:02:49 +0000303 bss_size_(0u),
Vladimir Markof4da6752014-08-01 19:04:18 +0100304 oat_data_offset_(0u),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700305 oat_header_(nullptr),
David Brazdil7b49e6c2016-09-01 11:06:18 +0100306 size_vdex_header_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700307 size_dex_file_alignment_(0),
308 size_executable_offset_alignment_(0),
309 size_oat_header_(0),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700310 size_oat_header_key_value_store_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700311 size_dex_file_(0),
David Brazdil5d5a36b2016-09-14 15:34:10 +0100312 size_verifier_deps_(0),
313 size_verifier_deps_alignment_(0),
Ian Rogers848871b2013-08-05 10:56:33 -0700314 size_interpreter_to_interpreter_bridge_(0),
315 size_interpreter_to_compiled_code_bridge_(0),
316 size_jni_dlsym_lookup_(0),
Andreas Gampe2da88232014-02-27 12:26:20 -0800317 size_quick_generic_jni_trampoline_(0),
Jeff Hao88474b42013-10-23 16:24:40 -0700318 size_quick_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700319 size_quick_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -0700320 size_quick_to_interpreter_bridge_(0),
321 size_trampoline_alignment_(0),
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100322 size_method_header_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700323 size_code_(0),
324 size_code_alignment_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +0100325 size_relative_call_thunks_(0),
Vladimir Markoc74658b2015-03-31 10:26:41 +0100326 size_misc_thunks_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700327 size_vmap_table_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700328 size_oat_dex_file_location_size_(0),
329 size_oat_dex_file_location_data_(0),
330 size_oat_dex_file_location_checksum_(0),
331 size_oat_dex_file_offset_(0),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000332 size_oat_dex_file_class_offsets_offset_(0),
Vladimir Marko49b0f452015-12-10 13:49:19 +0000333 size_oat_dex_file_lookup_table_offset_(0),
Vladimir Marko49b0f452015-12-10 13:49:19 +0000334 size_oat_lookup_table_alignment_(0),
335 size_oat_lookup_table_(0),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000336 size_oat_class_offsets_alignment_(0),
337 size_oat_class_offsets_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -0700338 size_oat_class_type_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700339 size_oat_class_status_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -0700340 size_oat_class_method_bitmaps_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +0100341 size_oat_class_method_offsets_(0),
Vladimir Marko944da602016-02-19 12:27:55 +0000342 relative_patcher_(nullptr),
343 absolute_patch_locations_() {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000344}
345
346bool OatWriter::AddDexFileSource(const char* filename,
347 const char* location,
348 CreateTypeLookupTable create_type_lookup_table) {
349 DCHECK(write_state_ == WriteState::kAddingDexFileSources);
350 uint32_t magic;
351 std::string error_msg;
Andreas Gampe43e10b02016-07-15 17:17:34 -0700352 File fd = OpenAndReadMagic(filename, &magic, &error_msg);
353 if (fd.Fd() == -1) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000354 PLOG(ERROR) << "Failed to read magic number from dex file: '" << filename << "'";
355 return false;
356 } else if (IsDexMagic(magic)) {
357 // The file is open for reading, not writing, so it's OK to let the File destructor
358 // close it without checking for explicit Close(), so pass checkUsage = false.
Andreas Gampe43e10b02016-07-15 17:17:34 -0700359 raw_dex_files_.emplace_back(new File(fd.Release(), location, /* checkUsage */ false));
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000360 oat_dex_files_.emplace_back(location,
361 DexFileSource(raw_dex_files_.back().get()),
362 create_type_lookup_table);
363 } else if (IsZipMagic(magic)) {
364 if (!AddZippedDexFilesSource(std::move(fd), location, create_type_lookup_table)) {
365 return false;
366 }
367 } else {
368 LOG(ERROR) << "Expected valid zip or dex file: '" << filename << "'";
369 return false;
370 }
371 return true;
372}
373
374// Add dex file source(s) from a zip file specified by a file handle.
Andreas Gampe43e10b02016-07-15 17:17:34 -0700375bool OatWriter::AddZippedDexFilesSource(File&& zip_fd,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000376 const char* location,
377 CreateTypeLookupTable create_type_lookup_table) {
378 DCHECK(write_state_ == WriteState::kAddingDexFileSources);
379 std::string error_msg;
Andreas Gampe43e10b02016-07-15 17:17:34 -0700380 zip_archives_.emplace_back(ZipArchive::OpenFromFd(zip_fd.Release(), location, &error_msg));
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000381 ZipArchive* zip_archive = zip_archives_.back().get();
382 if (zip_archive == nullptr) {
383 LOG(ERROR) << "Failed to open zip from file descriptor for '" << location << "': "
384 << error_msg;
385 return false;
386 }
387 for (size_t i = 0; ; ++i) {
388 std::string entry_name = DexFile::GetMultiDexClassesDexName(i);
389 std::unique_ptr<ZipEntry> entry(zip_archive->Find(entry_name.c_str(), &error_msg));
390 if (entry == nullptr) {
391 break;
392 }
393 zipped_dex_files_.push_back(std::move(entry));
394 zipped_dex_file_locations_.push_back(DexFile::GetMultiDexLocation(i, location));
395 const char* full_location = zipped_dex_file_locations_.back().c_str();
396 oat_dex_files_.emplace_back(full_location,
397 DexFileSource(zipped_dex_files_.back().get()),
398 create_type_lookup_table);
399 }
400 if (zipped_dex_file_locations_.empty()) {
401 LOG(ERROR) << "No dex files in zip file '" << location << "': " << error_msg;
402 return false;
403 }
404 return true;
405}
406
407// Add dex file source from raw memory.
408bool OatWriter::AddRawDexFileSource(const ArrayRef<const uint8_t>& data,
409 const char* location,
410 uint32_t location_checksum,
411 CreateTypeLookupTable create_type_lookup_table) {
412 DCHECK(write_state_ == WriteState::kAddingDexFileSources);
413 if (data.size() < sizeof(DexFile::Header)) {
414 LOG(ERROR) << "Provided data is shorter than dex file header. size: "
415 << data.size() << " File: " << location;
416 return false;
417 }
418 if (!ValidateDexFileHeader(data.data(), location)) {
419 return false;
420 }
421 const UnalignedDexFileHeader* header = AsUnalignedDexFileHeader(data.data());
422 if (data.size() < header->file_size_) {
423 LOG(ERROR) << "Truncated dex file data. Data size: " << data.size()
424 << " file size from header: " << header->file_size_ << " File: " << location;
425 return false;
426 }
427
428 oat_dex_files_.emplace_back(location, DexFileSource(data.data()), create_type_lookup_table);
429 oat_dex_files_.back().dex_file_location_checksum_ = location_checksum;
430 return true;
431}
432
433dchecked_vector<const char*> OatWriter::GetSourceLocations() const {
434 dchecked_vector<const char*> locations;
435 locations.reserve(oat_dex_files_.size());
436 for (const OatDexFile& oat_dex_file : oat_dex_files_) {
437 locations.push_back(oat_dex_file.GetLocation());
438 }
439 return locations;
440}
441
442bool OatWriter::WriteAndOpenDexFiles(
David Brazdil7b49e6c2016-09-01 11:06:18 +0100443 File* vdex_file,
444 OutputStream* oat_rodata,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000445 InstructionSet instruction_set,
446 const InstructionSetFeatures* instruction_set_features,
447 SafeMap<std::string, std::string>* key_value_store,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800448 bool verify,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000449 /*out*/ std::unique_ptr<MemMap>* opened_dex_files_map,
450 /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files) {
451 CHECK(write_state_ == WriteState::kAddingDexFileSources);
452
David Brazdil7b49e6c2016-09-01 11:06:18 +0100453 // Record the ELF rodata section offset, i.e. the beginning of the OAT data.
454 if (!RecordOatDataOffset(oat_rodata)) {
455 return false;
456 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000457
458 std::unique_ptr<MemMap> dex_files_map;
459 std::vector<std::unique_ptr<const DexFile>> dex_files;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100460
461 // Initialize VDEX and OAT headers.
462 if (kIsVdexEnabled) {
463 size_vdex_header_ = sizeof(VdexFile::Header);
464 vdex_size_ = size_vdex_header_;
465 }
466 size_t oat_data_offset = InitOatHeader(instruction_set,
467 instruction_set_features,
468 dchecked_integral_cast<uint32_t>(oat_dex_files_.size()),
469 key_value_store);
470 oat_size_ = InitOatDexFiles(oat_data_offset);
471
472 ChecksumUpdatingOutputStream checksum_updating_rodata(oat_rodata, oat_header_.get());
473
474 if (kIsVdexEnabled) {
475 std::unique_ptr<BufferedOutputStream> vdex_out(
476 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(vdex_file)));
477
478 // Write DEX files into VDEX, mmap and open them.
479 if (!WriteDexFiles(vdex_out.get(), vdex_file) ||
480 !OpenDexFiles(vdex_file, verify, &dex_files_map, &dex_files)) {
481 return false;
482 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100483 } else {
484 // Write DEX files into OAT, mmap and open them.
485 if (!WriteDexFiles(oat_rodata, vdex_file) ||
486 !OpenDexFiles(vdex_file, verify, &dex_files_map, &dex_files)) {
487 return false;
488 }
489
490 // Do a bulk checksum update for Dex[]. Doing it piece by piece would be
491 // difficult because we're not using the OutputStream directly.
492 if (!oat_dex_files_.empty()) {
493 size_t size = oat_size_ - oat_dex_files_[0].dex_file_offset_;
494 oat_header_->UpdateChecksum(dex_files_map->Begin(), size);
495 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000496 }
David Brazdil181e1cc2016-09-01 16:38:47 +0000497
David Brazdil7b49e6c2016-09-01 11:06:18 +0100498 // Write TypeLookupTables into OAT.
David Brazdil181e1cc2016-09-01 16:38:47 +0000499 if (!WriteTypeLookupTables(&checksum_updating_rodata, dex_files)) {
500 return false;
501 }
502
David Brazdil7b49e6c2016-09-01 11:06:18 +0100503 // Reserve space for class offsets in OAT and update class_offsets_offset_.
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000504 for (OatDexFile& oat_dex_file : oat_dex_files_) {
505 oat_dex_file.ReserveClassOffsets(this);
506 }
Vladimir Markoe079e212016-05-25 12:49:49 +0100507
David Brazdil7b49e6c2016-09-01 11:06:18 +0100508 // Write OatDexFiles into OAT. Needs to be done last, once offsets are collected.
David Brazdil181e1cc2016-09-01 16:38:47 +0000509 if (!WriteOatDexFiles(&checksum_updating_rodata)) {
510 return false;
David Brazdilb92ba622016-09-01 16:00:30 +0000511 }
512
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000513 *opened_dex_files_map = std::move(dex_files_map);
514 *opened_dex_files = std::move(dex_files);
515 write_state_ = WriteState::kPrepareLayout;
516 return true;
517}
518
519void OatWriter::PrepareLayout(const CompilerDriver* compiler,
520 ImageWriter* image_writer,
Vladimir Marko944da602016-02-19 12:27:55 +0000521 const std::vector<const DexFile*>& dex_files,
522 linker::MultiOatRelativePatcher* relative_patcher) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000523 CHECK(write_state_ == WriteState::kPrepareLayout);
524
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000525 compiler_driver_ = compiler;
526 image_writer_ = image_writer;
Vladimir Marko944da602016-02-19 12:27:55 +0000527 dex_files_ = &dex_files;
528 relative_patcher_ = relative_patcher;
529 SetMultiOatRelativePatcherAdjustment();
530
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000531 if (compiling_boot_image_) {
532 CHECK(image_writer_ != nullptr);
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800533 }
Vladimir Markob163bb72015-03-31 21:49:49 +0100534 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000535 CHECK_EQ(instruction_set, oat_header_->GetInstructionSet());
Vladimir Markof4da6752014-08-01 19:04:18 +0100536
David Brazdil7b49e6c2016-09-01 11:06:18 +0100537 uint32_t offset = oat_size_;
Ian Rogersca368cb2013-11-15 15:52:08 -0800538 {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000539 TimingLogger::ScopedTiming split("InitOatClasses", timings_);
Ian Rogersca368cb2013-11-15 15:52:08 -0800540 offset = InitOatClasses(offset);
541 }
542 {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000543 TimingLogger::ScopedTiming split("InitOatMaps", timings_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100544 offset = InitOatMaps(offset);
545 }
546 {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000547 TimingLogger::ScopedTiming split("InitOatCode", timings_);
Ian Rogersca368cb2013-11-15 15:52:08 -0800548 offset = InitOatCode(offset);
549 }
550 {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000551 TimingLogger::ScopedTiming split("InitOatCodeDexFiles", timings_);
Ian Rogersca368cb2013-11-15 15:52:08 -0800552 offset = InitOatCodeDexFiles(offset);
553 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100554 oat_size_ = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700555
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800556 if (!HasBootImage()) {
Vladimir Marko09d09432015-09-08 13:47:48 +0100557 // Allocate space for app dex cache arrays in the .bss section.
David Brazdil7b49e6c2016-09-01 11:06:18 +0100558 size_t bss_start = RoundUp(oat_size_, kPageSize);
Andreas Gampe542451c2016-07-26 09:02:02 -0700559 PointerSize pointer_size = GetInstructionSetPointerSize(instruction_set);
Vladimir Marko09d09432015-09-08 13:47:48 +0100560 bss_size_ = 0u;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000561 for (const DexFile* dex_file : *dex_files_) {
Vladimir Marko09d09432015-09-08 13:47:48 +0100562 dex_cache_arrays_offsets_.Put(dex_file, bss_start + bss_size_);
563 DexCacheArraysLayout layout(pointer_size, dex_file);
564 bss_size_ += layout.Size();
565 }
566 }
567
Brian Carlstrome24fa612011-09-29 00:53:55 -0700568 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800569 if (compiling_boot_image_) {
570 CHECK_EQ(image_writer_ != nullptr,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000571 oat_header_->GetStoreValueByKey(OatHeader::kImageLocationKey) == nullptr);
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800572 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000573
574 write_state_ = WriteState::kWriteRoData;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700575}
576
Ian Rogers0571d352011-11-03 19:51:38 -0700577OatWriter::~OatWriter() {
Ian Rogers0571d352011-11-03 19:51:38 -0700578}
579
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100580class OatWriter::DexMethodVisitor {
581 public:
582 DexMethodVisitor(OatWriter* writer, size_t offset)
583 : writer_(writer),
584 offset_(offset),
585 dex_file_(nullptr),
586 class_def_index_(DexFile::kDexNoIndex) {
587 }
588
589 virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) {
590 DCHECK(dex_file_ == nullptr);
591 DCHECK_EQ(class_def_index_, DexFile::kDexNoIndex);
592 dex_file_ = dex_file;
593 class_def_index_ = class_def_index;
594 return true;
595 }
596
597 virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0;
598
599 virtual bool EndClass() {
600 if (kIsDebugBuild) {
601 dex_file_ = nullptr;
602 class_def_index_ = DexFile::kDexNoIndex;
603 }
604 return true;
605 }
606
607 size_t GetOffset() const {
608 return offset_;
609 }
610
611 protected:
612 virtual ~DexMethodVisitor() { }
613
614 OatWriter* const writer_;
615
616 // The offset is usually advanced for each visited method by the derived class.
617 size_t offset_;
618
619 // The dex file and class def index are set in StartClass().
620 const DexFile* dex_file_;
621 size_t class_def_index_;
622};
623
624class OatWriter::OatDexMethodVisitor : public DexMethodVisitor {
625 public:
626 OatDexMethodVisitor(OatWriter* writer, size_t offset)
627 : DexMethodVisitor(writer, offset),
628 oat_class_index_(0u),
629 method_offsets_index_(0u) {
630 }
631
632 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
633 DexMethodVisitor::StartClass(dex_file, class_def_index);
634 DCHECK_LT(oat_class_index_, writer_->oat_classes_.size());
635 method_offsets_index_ = 0u;
636 return true;
637 }
638
639 bool EndClass() {
640 ++oat_class_index_;
641 return DexMethodVisitor::EndClass();
642 }
643
644 protected:
645 size_t oat_class_index_;
646 size_t method_offsets_index_;
647};
648
649class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor {
650 public:
651 InitOatClassesMethodVisitor(OatWriter* writer, size_t offset)
652 : DexMethodVisitor(writer, offset),
653 compiled_methods_(),
654 num_non_null_compiled_methods_(0u) {
Vladimir Marko49b0f452015-12-10 13:49:19 +0000655 size_t num_classes = 0u;
656 for (const OatDexFile& oat_dex_file : writer_->oat_dex_files_) {
657 num_classes += oat_dex_file.class_offsets_.size();
658 }
659 writer_->oat_classes_.reserve(num_classes);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100660 compiled_methods_.reserve(256u);
661 }
662
663 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
664 DexMethodVisitor::StartClass(dex_file, class_def_index);
665 compiled_methods_.clear();
666 num_non_null_compiled_methods_ = 0u;
667 return true;
668 }
669
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300670 bool VisitMethod(size_t class_def_method_index ATTRIBUTE_UNUSED,
671 const ClassDataItemIterator& it) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100672 // Fill in the compiled_methods_ array for methods that have a
673 // CompiledMethod. We track the number of non-null entries in
674 // num_non_null_compiled_methods_ since we only want to allocate
675 // OatMethodOffsets for the compiled methods.
676 uint32_t method_idx = it.GetMemberIndex();
677 CompiledMethod* compiled_method =
678 writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx));
679 compiled_methods_.push_back(compiled_method);
680 if (compiled_method != nullptr) {
681 ++num_non_null_compiled_methods_;
682 }
683 return true;
684 }
685
686 bool EndClass() {
687 ClassReference class_ref(dex_file_, class_def_index_);
688 CompiledClass* compiled_class = writer_->compiler_driver_->GetCompiledClass(class_ref);
689 mirror::Class::Status status;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700690 if (compiled_class != nullptr) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100691 status = compiled_class->GetStatus();
692 } else if (writer_->compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) {
693 status = mirror::Class::kStatusError;
694 } else {
695 status = mirror::Class::kStatusNotReady;
696 }
697
Vladimir Marko49b0f452015-12-10 13:49:19 +0000698 writer_->oat_classes_.emplace_back(offset_,
699 compiled_methods_,
700 num_non_null_compiled_methods_,
701 status);
702 offset_ += writer_->oat_classes_.back().SizeOf();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100703 return DexMethodVisitor::EndClass();
704 }
705
706 private:
Vladimir Marko49b0f452015-12-10 13:49:19 +0000707 dchecked_vector<CompiledMethod*> compiled_methods_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100708 size_t num_non_null_compiled_methods_;
709};
710
711class OatWriter::InitCodeMethodVisitor : public OatDexMethodVisitor {
712 public:
713 InitCodeMethodVisitor(OatWriter* writer, size_t offset)
David Srbecky009e2a62015-04-15 02:46:30 +0100714 : OatDexMethodVisitor(writer, offset),
David Srbecky009e2a62015-04-15 02:46:30 +0100715 debuggable_(writer->GetCompilerDriver()->GetCompilerOptions().GetDebuggable()) {
David Srbeckyf8980872015-05-22 17:04:47 +0100716 writer_->absolute_patch_locations_.reserve(
Vladimir Markof4da6752014-08-01 19:04:18 +0100717 writer_->compiler_driver_->GetNonRelativeLinkerPatchCount());
718 }
719
720 bool EndClass() {
721 OatDexMethodVisitor::EndClass();
722 if (oat_class_index_ == writer_->oat_classes_.size()) {
Vladimir Marko71b0ddf2015-04-02 19:45:06 +0100723 offset_ = writer_->relative_patcher_->ReserveSpaceEnd(offset_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100724 }
725 return true;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100726 }
727
728 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700729 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko49b0f452015-12-10 13:49:19 +0000730 OatClass* oat_class = &writer_->oat_classes_[oat_class_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100731 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
732
733 if (compiled_method != nullptr) {
734 // Derived from CompiledMethod.
735 uint32_t quick_code_offset = 0;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100736
Vladimir Marko35831e82015-09-11 11:59:18 +0100737 ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode();
738 uint32_t code_size = quick_code.size() * sizeof(uint8_t);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800739 uint32_t thumb_offset = compiled_method->CodeDelta();
Elliott Hughes956af0f2014-12-11 14:34:28 -0800740
David Srbecky009e2a62015-04-15 02:46:30 +0100741 // Deduplicate code arrays if we are not producing debuggable code.
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100742 bool deduped = true;
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800743 MethodReference method_ref(dex_file_, it.GetMemberIndex());
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000744 if (debuggable_) {
Vladimir Marko944da602016-02-19 12:27:55 +0000745 quick_code_offset = writer_->relative_patcher_->GetOffset(method_ref);
746 if (quick_code_offset != 0u) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800747 // Duplicate methods, we want the same code for both of them so that the oat writer puts
748 // the same code in both ArtMethods so that we do not get different oat code at runtime.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800749 } else {
750 quick_code_offset = NewQuickCodeOffset(compiled_method, it, thumb_offset);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100751 deduped = false;
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800752 }
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000753 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100754 quick_code_offset = dedupe_map_.GetOrCreate(
755 compiled_method,
756 [this, &deduped, compiled_method, &it, thumb_offset]() {
757 deduped = false;
758 return NewQuickCodeOffset(compiled_method, it, thumb_offset);
759 });
Elliott Hughes956af0f2014-12-11 14:34:28 -0800760 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100761
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100762 if (code_size != 0) {
Vladimir Marko944da602016-02-19 12:27:55 +0000763 if (writer_->relative_patcher_->GetOffset(method_ref) != 0u) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100764 // TODO: Should this be a hard failure?
765 LOG(WARNING) << "Multiple definitions of "
766 << PrettyMethod(method_ref.dex_method_index, *method_ref.dex_file)
Vladimir Marko944da602016-02-19 12:27:55 +0000767 << " offsets " << writer_->relative_patcher_->GetOffset(method_ref)
768 << " " << quick_code_offset;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100769 } else {
Vladimir Marko944da602016-02-19 12:27:55 +0000770 writer_->relative_patcher_->SetOffset(method_ref, quick_code_offset);
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100771 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800772 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100773
Elliott Hughes956af0f2014-12-11 14:34:28 -0800774 // Update quick method header.
775 DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size());
776 OatQuickMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_];
Elliott Hughes956af0f2014-12-11 14:34:28 -0800777 uint32_t vmap_table_offset = method_header->vmap_table_offset_;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100778 // If we don't have quick code, then we must have a vmap, as that is how the dex2dex
779 // compiler records its transformations.
Vladimir Marko35831e82015-09-11 11:59:18 +0100780 DCHECK(!quick_code.empty() || vmap_table_offset != 0);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800781 // The code offset was 0 when the mapping/vmap table offset was set, so it's set
782 // to 0-offset and we need to adjust it by code_offset.
783 uint32_t code_offset = quick_code_offset - thumb_offset;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100784 if (vmap_table_offset != 0u && code_offset != 0u) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800785 vmap_table_offset += code_offset;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100786 DCHECK_LT(vmap_table_offset, code_offset) << "Overflow in oat offsets";
Elliott Hughes956af0f2014-12-11 14:34:28 -0800787 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800788 uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
789 uint32_t core_spill_mask = compiled_method->GetCoreSpillMask();
790 uint32_t fp_spill_mask = compiled_method->GetFpSpillMask();
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100791 *method_header = OatQuickMethodHeader(vmap_table_offset,
792 frame_size_in_bytes,
793 core_spill_mask,
794 fp_spill_mask,
795 code_size);
Vladimir Marko7624d252014-05-02 14:40:15 +0100796
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000797 if (!deduped) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800798 // Update offsets. (Checksum is updated when writing.)
799 offset_ += sizeof(*method_header); // Method header is prepended before code.
800 offset_ += code_size;
801 // Record absolute patch locations.
802 if (!compiled_method->GetPatches().empty()) {
803 uintptr_t base_loc = offset_ - code_size - writer_->oat_header_->GetExecutableOffset();
804 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
Vladimir Marko20f85592015-03-19 10:07:02 +0000805 if (!patch.IsPcRelative()) {
David Srbeckyf8980872015-05-22 17:04:47 +0100806 writer_->absolute_patch_locations_.push_back(base_loc + patch.LiteralOffset());
Vladimir Markof4da6752014-08-01 19:04:18 +0100807 }
808 }
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) {
969 LOG(INTERNAL_FATAL) << "Unexpected failure to resolve a method: "
970 << 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 Markodbb7f5b2016-03-30 13:23:58 +01001118 case LinkerPatch::Type::kTypeRelative: {
1119 uint32_t target_offset = GetTargetObjectOffset(GetTargetType(patch));
1120 writer_->relative_patcher_->PatchPcRelativeReference(&patched_code_,
1121 patch,
1122 offset_ + literal_offset,
1123 target_offset);
1124 break;
1125 }
Vladimir Markodb8e62d2016-03-30 16:30:21 +01001126 case LinkerPatch::Type::kCall: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001127 uint32_t target_offset = GetTargetOffset(patch);
1128 PatchCodeAddress(&patched_code_, literal_offset, target_offset);
1129 break;
1130 }
Vladimir Markodb8e62d2016-03-30 16:30:21 +01001131 case LinkerPatch::Type::kMethod: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001132 ArtMethod* method = GetTargetMethod(patch);
1133 PatchMethodAddress(&patched_code_, literal_offset, method);
1134 break;
1135 }
Vladimir Markodb8e62d2016-03-30 16:30:21 +01001136 case LinkerPatch::Type::kString: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001137 mirror::String* string = GetTargetString(patch);
1138 PatchObjectAddress(&patched_code_, literal_offset, string);
1139 break;
1140 }
Vladimir Markodb8e62d2016-03-30 16:30:21 +01001141 case LinkerPatch::Type::kType: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001142 mirror::Class* type = GetTargetType(patch);
1143 PatchObjectAddress(&patched_code_, literal_offset, type);
1144 break;
1145 }
1146 default: {
Vladimir Markodb8e62d2016-03-30 16:30:21 +01001147 DCHECK_EQ(patch.GetType(), LinkerPatch::Type::kRecordPosition);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001148 break;
1149 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +01001150 }
1151 }
1152 }
1153
Vladimir Markoe079e212016-05-25 12:49:49 +01001154 if (!out->WriteFully(quick_code.data(), code_size)) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +01001155 ReportWriteFailure("method code", it);
1156 return false;
1157 }
1158 writer_->size_code_ += code_size;
1159 offset_ += code_size;
Nicolas Geoffrayf0758792015-07-13 11:56:00 +00001160 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +01001161 DCHECK_OFFSET_();
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001162 ++method_offsets_index_;
1163 }
1164
1165 return true;
1166 }
1167
1168 private:
1169 OutputStream* const out_;
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +01001170 const size_t file_offset_;
1171 const ScopedObjectAccess soa_;
1172 const ScopedAssertNoThreadSuspension no_thread_suspension_;
Vladimir Markof4da6752014-08-01 19:04:18 +01001173 ClassLinker* const class_linker_;
1174 mirror::DexCache* dex_cache_;
1175 std::vector<uint8_t> patched_code_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001176
1177 void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) {
1178 PLOG(ERROR) << "Failed to write " << what << " for "
1179 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
1180 }
Vladimir Markof4da6752014-08-01 19:04:18 +01001181
Mathieu Chartiere401d142015-04-22 13:56:20 -07001182 ArtMethod* GetTargetMethod(const LinkerPatch& patch)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001183 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001184 MethodReference ref = patch.TargetMethod();
1185 mirror::DexCache* dex_cache =
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001186 (dex_file_ == ref.dex_file) ? dex_cache_ : class_linker_->FindDexCache(
1187 Thread::Current(), *ref.dex_file);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001188 ArtMethod* method = dex_cache->GetResolvedMethod(
1189 ref.dex_method_index, class_linker_->GetImagePointerSize());
Vladimir Markof4da6752014-08-01 19:04:18 +01001190 CHECK(method != nullptr);
1191 return method;
1192 }
1193
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001194 uint32_t GetTargetOffset(const LinkerPatch& patch) REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko944da602016-02-19 12:27:55 +00001195 uint32_t target_offset = writer_->relative_patcher_->GetOffset(patch.TargetMethod());
1196 // If there's no new compiled code, either we're compiling an app and the target method
1197 // is in the boot image, or we need to point to the correct trampoline.
Vladimir Markof4da6752014-08-01 19:04:18 +01001198 if (UNLIKELY(target_offset == 0)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001199 ArtMethod* target = GetTargetMethod(patch);
Vladimir Markof4da6752014-08-01 19:04:18 +01001200 DCHECK(target != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07001201 PointerSize size =
1202 GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet());
Jeff Haoa0acc2d2015-01-27 11:22:04 -08001203 const void* oat_code_offset = target->GetEntryPointFromQuickCompiledCodePtrSize(size);
1204 if (oat_code_offset != 0) {
Vladimir Marko944da602016-02-19 12:27:55 +00001205 DCHECK(!writer_->HasBootImage());
Jeff Haoa0acc2d2015-01-27 11:22:04 -08001206 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(oat_code_offset));
1207 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(oat_code_offset));
1208 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(oat_code_offset));
1209 target_offset = PointerToLowMemUInt32(oat_code_offset);
1210 } else {
1211 target_offset = target->IsNative()
1212 ? writer_->oat_header_->GetQuickGenericJniTrampolineOffset()
1213 : writer_->oat_header_->GetQuickToInterpreterBridgeOffset();
1214 }
Vladimir Markof4da6752014-08-01 19:04:18 +01001215 }
1216 return target_offset;
1217 }
1218
Vladimir Marko052164a2016-04-27 13:54:18 +01001219 mirror::DexCache* GetDexCache(const DexFile* target_dex_file)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001220 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko052164a2016-04-27 13:54:18 +01001221 return (target_dex_file == dex_file_)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001222 ? dex_cache_
Vladimir Marko052164a2016-04-27 13:54:18 +01001223 : class_linker_->FindDexCache(Thread::Current(), *target_dex_file);
1224 }
1225
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001226 mirror::Class* GetTargetType(const LinkerPatch& patch) REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko052164a2016-04-27 13:54:18 +01001227 mirror::DexCache* dex_cache = GetDexCache(patch.TargetTypeDexFile());
Vladimir Markof4da6752014-08-01 19:04:18 +01001228 mirror::Class* type = dex_cache->GetResolvedType(patch.TargetTypeIndex());
1229 CHECK(type != nullptr);
1230 return type;
1231 }
1232
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001233 mirror::String* GetTargetString(const LinkerPatch& patch) REQUIRES_SHARED(Locks::mutator_lock_) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07001234 ScopedObjectAccessUnchecked soa(Thread::Current());
1235 StackHandleScope<1> hs(soa.Self());
1236 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1237 Handle<mirror::DexCache> dex_cache(hs.NewHandle(GetDexCache(patch.TargetStringDexFile())));
1238 mirror::String* string = linker->LookupString(*patch.TargetStringDexFile(),
1239 patch.TargetStringIndex(),
1240 dex_cache);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001241 DCHECK(string != nullptr);
1242 DCHECK(writer_->HasBootImage() ||
1243 Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(string));
1244 return string;
1245 }
1246
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001247 uint32_t GetDexCacheOffset(const LinkerPatch& patch) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001248 if (writer_->HasBootImage()) {
Vladimir Marko944da602016-02-19 12:27:55 +00001249 uintptr_t element = writer_->image_writer_->GetDexCacheArrayElementImageAddress<uintptr_t>(
1250 patch.TargetDexCacheDexFile(), patch.TargetDexCacheElementOffset());
1251 size_t oat_index = writer_->image_writer_->GetOatIndexForDexCache(dex_cache_);
1252 uintptr_t oat_data = writer_->image_writer_->GetOatDataBegin(oat_index);
Vladimir Marko05792b92015-08-03 11:56:49 +01001253 return element - oat_data;
Vladimir Marko20f85592015-03-19 10:07:02 +00001254 } else {
Vladimir Marko09d09432015-09-08 13:47:48 +01001255 size_t start = writer_->dex_cache_arrays_offsets_.Get(patch.TargetDexCacheDexFile());
1256 return start + patch.TargetDexCacheElementOffset();
Vladimir Marko20f85592015-03-19 10:07:02 +00001257 }
1258 }
1259
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001260 uint32_t GetTargetObjectOffset(mirror::Object* object) REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001261 DCHECK(writer_->HasBootImage());
1262 object = writer_->image_writer_->GetImageAddress(object);
1263 size_t oat_index = writer_->image_writer_->GetOatIndexForDexFile(dex_file_);
1264 uintptr_t oat_data_begin = writer_->image_writer_->GetOatDataBegin(oat_index);
1265 // TODO: Clean up offset types. The target offset must be treated as signed.
1266 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(object) - oat_data_begin);
1267 }
1268
Vladimir Markof4da6752014-08-01 19:04:18 +01001269 void PatchObjectAddress(std::vector<uint8_t>* code, uint32_t offset, mirror::Object* object)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001270 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001271 if (writer_->HasBootImage()) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001272 object = writer_->image_writer_->GetImageAddress(object);
Vladimir Marko09d09432015-09-08 13:47:48 +01001273 } else {
1274 // NOTE: We're using linker patches for app->boot references when the image can
1275 // be relocated and therefore we need to emit .oat_patches. We're not using this
1276 // for app->app references, so check that the object is in the image space.
1277 DCHECK(Runtime::Current()->GetHeap()->FindSpaceFromObject(object, false)->IsImageSpace());
Vladimir Markof4da6752014-08-01 19:04:18 +01001278 }
Vladimir Marko09d09432015-09-08 13:47:48 +01001279 // Note: We only patch targeting Objects in image which is in the low 4gb.
Vladimir Markof4da6752014-08-01 19:04:18 +01001280 uint32_t address = PointerToLowMemUInt32(object);
1281 DCHECK_LE(offset + 4, code->size());
1282 uint8_t* data = &(*code)[offset];
1283 data[0] = address & 0xffu;
1284 data[1] = (address >> 8) & 0xffu;
1285 data[2] = (address >> 16) & 0xffu;
1286 data[3] = (address >> 24) & 0xffu;
1287 }
1288
Mathieu Chartiere401d142015-04-22 13:56:20 -07001289 void PatchMethodAddress(std::vector<uint8_t>* code, uint32_t offset, ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001290 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001291 if (writer_->HasBootImage()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001292 method = writer_->image_writer_->GetImageMethodAddress(method);
Vladimir Marko09d09432015-09-08 13:47:48 +01001293 } else if (kIsDebugBuild) {
1294 // NOTE: We're using linker patches for app->boot references when the image can
1295 // be relocated and therefore we need to emit .oat_patches. We're not using this
1296 // for app->app references, so check that the method is an image method.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001297 std::vector<gc::space::ImageSpace*> image_spaces =
1298 Runtime::Current()->GetHeap()->GetBootImageSpaces();
1299 bool contains_method = false;
1300 for (gc::space::ImageSpace* image_space : image_spaces) {
1301 size_t method_offset = reinterpret_cast<const uint8_t*>(method) - image_space->Begin();
1302 contains_method |=
1303 image_space->GetImageHeader().GetMethodsSection().Contains(method_offset);
1304 }
1305 CHECK(contains_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001306 }
Vladimir Marko09d09432015-09-08 13:47:48 +01001307 // Note: We only patch targeting ArtMethods in image which is in the low 4gb.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001308 uint32_t address = PointerToLowMemUInt32(method);
1309 DCHECK_LE(offset + 4, code->size());
1310 uint8_t* data = &(*code)[offset];
1311 data[0] = address & 0xffu;
1312 data[1] = (address >> 8) & 0xffu;
1313 data[2] = (address >> 16) & 0xffu;
1314 data[3] = (address >> 24) & 0xffu;
1315 }
1316
Vladimir Markof4da6752014-08-01 19:04:18 +01001317 void PatchCodeAddress(std::vector<uint8_t>* code, uint32_t offset, uint32_t target_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001318 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko09d09432015-09-08 13:47:48 +01001319 uint32_t address = target_offset;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001320 if (writer_->HasBootImage()) {
Vladimir Marko944da602016-02-19 12:27:55 +00001321 size_t oat_index = writer_->image_writer_->GetOatIndexForDexCache(dex_cache_);
1322 // TODO: Clean up offset types.
1323 // The target_offset must be treated as signed for cross-oat patching.
1324 const void* target = reinterpret_cast<const void*>(
1325 writer_->image_writer_->GetOatDataBegin(oat_index) +
1326 static_cast<int32_t>(target_offset));
1327 address = PointerToLowMemUInt32(target);
Vladimir Marko09d09432015-09-08 13:47:48 +01001328 }
Vladimir Markof4da6752014-08-01 19:04:18 +01001329 DCHECK_LE(offset + 4, code->size());
1330 uint8_t* data = &(*code)[offset];
1331 data[0] = address & 0xffu;
1332 data[1] = (address >> 8) & 0xffu;
1333 data[2] = (address >> 16) & 0xffu;
1334 data[3] = (address >> 24) & 0xffu;
1335 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001336};
1337
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001338class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor {
1339 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001340 WriteMapMethodVisitor(OatWriter* writer,
1341 OutputStream* out,
1342 const size_t file_offset,
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001343 size_t relative_offset)
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001344 : OatDexMethodVisitor(writer, relative_offset),
1345 out_(out),
1346 file_offset_(file_offset) {
1347 }
1348
1349 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001350 OatClass* oat_class = &writer_->oat_classes_[oat_class_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001351 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
1352
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001353 if (compiled_method != nullptr) { // ie. not an abstract method
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001354 size_t file_offset = file_offset_;
1355 OutputStream* out = out_;
1356
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001357 uint32_t map_offset = oat_class->method_headers_[method_offsets_index_].vmap_table_offset_;
1358 uint32_t code_offset = oat_class->method_offsets_[method_offsets_index_].code_offset_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001359 ++method_offsets_index_;
1360
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001361 DCHECK((compiled_method->GetVmapTable().size() == 0u && map_offset == 0u) ||
1362 (compiled_method->GetVmapTable().size() != 0u && map_offset != 0u))
1363 << compiled_method->GetVmapTable().size() << " " << map_offset << " "
1364 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
1365
1366 if (map_offset != 0u) {
1367 // Transform map_offset to actual oat data offset.
1368 map_offset = (code_offset - compiled_method->CodeDelta()) - map_offset;
1369 DCHECK_NE(map_offset, 0u);
1370 DCHECK_LE(map_offset, offset_) << PrettyMethod(it.GetMemberIndex(), *dex_file_);
1371
1372 ArrayRef<const uint8_t> map = compiled_method->GetVmapTable();
1373 size_t map_size = map.size() * sizeof(map[0]);
1374 if (map_offset == offset_) {
1375 // Write deduplicated map (code info for Optimizing or transformation info for dex2dex).
Vladimir Markoe079e212016-05-25 12:49:49 +01001376 if (UNLIKELY(!out->WriteFully(map.data(), map_size))) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001377 ReportWriteFailure(it);
1378 return false;
1379 }
1380 offset_ += map_size;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001381 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001382 }
1383 DCHECK_OFFSET_();
1384 }
1385
1386 return true;
1387 }
1388
1389 private:
1390 OutputStream* const out_;
1391 size_t const file_offset_;
1392
1393 void ReportWriteFailure(const ClassDataItemIterator& it) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001394 PLOG(ERROR) << "Failed to write map for "
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001395 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
1396 }
1397};
1398
1399// Visit all methods from all classes in all dex files with the specified visitor.
1400bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) {
1401 for (const DexFile* dex_file : *dex_files_) {
1402 const size_t class_def_count = dex_file->NumClassDefs();
1403 for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) {
1404 if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) {
1405 return false;
1406 }
1407 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -07001408 const uint8_t* class_data = dex_file->GetClassData(class_def);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001409 if (class_data != nullptr) { // ie not an empty class, such as a marker interface
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001410 ClassDataItemIterator it(*dex_file, class_data);
1411 while (it.HasNextStaticField()) {
1412 it.Next();
1413 }
1414 while (it.HasNextInstanceField()) {
1415 it.Next();
1416 }
1417 size_t class_def_method_index = 0u;
1418 while (it.HasNextDirectMethod()) {
1419 if (!visitor->VisitMethod(class_def_method_index, it)) {
1420 return false;
1421 }
1422 ++class_def_method_index;
1423 it.Next();
1424 }
1425 while (it.HasNextVirtualMethod()) {
1426 if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) {
1427 return false;
1428 }
1429 ++class_def_method_index;
1430 it.Next();
1431 }
1432 }
1433 if (UNLIKELY(!visitor->EndClass())) {
1434 return false;
1435 }
1436 }
1437 }
1438 return true;
1439}
1440
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001441size_t OatWriter::InitOatHeader(InstructionSet instruction_set,
1442 const InstructionSetFeatures* instruction_set_features,
1443 uint32_t num_dex_files,
1444 SafeMap<std::string, std::string>* key_value_store) {
1445 TimingLogger::ScopedTiming split("InitOatHeader", timings_);
1446 oat_header_.reset(OatHeader::Create(instruction_set,
1447 instruction_set_features,
1448 num_dex_files,
1449 key_value_store));
1450 size_oat_header_ += sizeof(OatHeader);
1451 size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001452 return oat_header_->GetHeaderSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001453}
1454
1455size_t OatWriter::InitOatDexFiles(size_t offset) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001456 TimingLogger::ScopedTiming split("InitOatDexFiles", timings_);
1457 // Initialize offsets of dex files.
Vladimir Marko49b0f452015-12-10 13:49:19 +00001458 for (OatDexFile& oat_dex_file : oat_dex_files_) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001459 oat_dex_file.offset_ = offset;
1460 offset += oat_dex_file.SizeOf();
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001461 }
1462 return offset;
1463}
1464
Brian Carlstrom389efb02012-01-11 12:06:26 -08001465size_t OatWriter::InitOatClasses(size_t offset) {
Brian Carlstrom389efb02012-01-11 12:06:26 -08001466 // calculate the offsets within OatDexFiles to OatClasses
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001467 InitOatClassesMethodVisitor visitor(this, offset);
1468 bool success = VisitDexMethods(&visitor);
1469 CHECK(success);
1470 offset = visitor.GetOffset();
Brian Carlstromba150c32013-08-27 17:31:03 -07001471
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001472 // Update oat_dex_files_.
1473 auto oat_class_it = oat_classes_.begin();
Vladimir Marko49b0f452015-12-10 13:49:19 +00001474 for (OatDexFile& oat_dex_file : oat_dex_files_) {
1475 for (uint32_t& class_offset : oat_dex_file.class_offsets_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001476 DCHECK(oat_class_it != oat_classes_.end());
Vladimir Marko49b0f452015-12-10 13:49:19 +00001477 class_offset = oat_class_it->offset_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001478 ++oat_class_it;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001479 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001480 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001481 CHECK(oat_class_it == oat_classes_.end());
1482
1483 return offset;
1484}
1485
1486size_t OatWriter::InitOatMaps(size_t offset) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001487 InitMapMethodVisitor visitor(this, offset);
1488 bool success = VisitDexMethods(&visitor);
1489 DCHECK(success);
1490 offset = visitor.GetOffset();
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001491
Brian Carlstrome24fa612011-09-29 00:53:55 -07001492 return offset;
1493}
1494
1495size_t OatWriter::InitOatCode(size_t offset) {
1496 // calculate the offsets within OatHeader to executable code
1497 size_t old_offset = offset;
Dave Allison50abf0a2014-06-23 13:19:59 -07001498 size_t adjusted_offset = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001499 // required to be on a new page boundary
1500 offset = RoundUp(offset, kPageSize);
1501 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001502 size_executable_offset_alignment_ = offset - old_offset;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001503 if (compiler_driver_->IsBootImage()) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001504 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001505
Ian Rogers848871b2013-08-05 10:56:33 -07001506 #define DO_TRAMPOLINE(field, fn_name) \
1507 offset = CompiledCode::AlignCode(offset, instruction_set); \
Dave Allison50abf0a2014-06-23 13:19:59 -07001508 adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \
1509 oat_header_->Set ## fn_name ## Offset(adjusted_offset); \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -07001510 (field) = compiler_driver_->Create ## fn_name(); \
1511 offset += (field)->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001512
Ian Rogers848871b2013-08-05 10:56:33 -07001513 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Andreas Gampe2da88232014-02-27 12:26:20 -08001514 DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline);
Jeff Hao88474b42013-10-23 16:24:40 -07001515 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -07001516 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
1517 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001518
Ian Rogers848871b2013-08-05 10:56:33 -07001519 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001520 } else {
Ian Rogers848871b2013-08-05 10:56:33 -07001521 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
1522 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
1523 oat_header_->SetJniDlsymLookupOffset(0);
Andreas Gampe2da88232014-02-27 12:26:20 -08001524 oat_header_->SetQuickGenericJniTrampolineOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -07001525 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001526 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -07001527 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001528 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001529 return offset;
1530}
1531
1532size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001533 #define VISIT(VisitorType) \
1534 do { \
1535 VisitorType visitor(this, offset); \
1536 bool success = VisitDexMethods(&visitor); \
1537 DCHECK(success); \
1538 offset = visitor.GetOffset(); \
1539 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001540
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001541 VISIT(InitCodeMethodVisitor);
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001542 if (HasImage()) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001543 VISIT(InitImageMethodVisitor);
Ian Rogers0571d352011-11-03 19:51:38 -07001544 }
Logan Chien8b977d32012-02-21 19:14:55 +08001545
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001546 #undef VISIT
1547
Brian Carlstrome24fa612011-09-29 00:53:55 -07001548 return offset;
1549}
1550
David Srbeckybc90fd02015-04-22 19:40:27 +01001551bool OatWriter::WriteRodata(OutputStream* out) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001552 CHECK(write_state_ == WriteState::kWriteRoData);
1553
Vladimir Markoe079e212016-05-25 12:49:49 +01001554 // Wrap out to update checksum with each write.
1555 ChecksumUpdatingOutputStream checksum_updating_out(out, oat_header_.get());
1556 out = &checksum_updating_out;
1557
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001558 if (!WriteClassOffsets(out)) {
1559 LOG(ERROR) << "Failed to write class offsets to " << out->GetLocation();
Vladimir Markof4da6752014-08-01 19:04:18 +01001560 return false;
1561 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001562
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001563 if (!WriteClasses(out)) {
1564 LOG(ERROR) << "Failed to write classes to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001565 return false;
1566 }
1567
Vladimir Markof4da6752014-08-01 19:04:18 +01001568 off_t tables_end_offset = out->Seek(0, kSeekCurrent);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001569 if (tables_end_offset == static_cast<off_t>(-1)) {
David Brazdil181e1cc2016-09-01 16:38:47 +00001570 LOG(ERROR) << "Failed to get oat code position in " << out->GetLocation();
Vladimir Markof4da6752014-08-01 19:04:18 +01001571 return false;
1572 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001573 size_t file_offset = oat_data_offset_;
Vladimir Markof4da6752014-08-01 19:04:18 +01001574 size_t relative_offset = static_cast<size_t>(tables_end_offset) - file_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001575 relative_offset = WriteMaps(out, file_offset, relative_offset);
1576 if (relative_offset == 0) {
1577 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
1578 return false;
1579 }
1580
David Srbeckybc90fd02015-04-22 19:40:27 +01001581 // Write padding.
1582 off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent);
1583 relative_offset += size_executable_offset_alignment_;
1584 DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset());
1585 size_t expected_file_offset = file_offset + relative_offset;
1586 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
1587 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
1588 << " Expected: " << expected_file_offset << " File: " << out->GetLocation();
1589 return 0;
1590 }
1591 DCHECK_OFFSET();
1592
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001593 write_state_ = WriteState::kWriteText;
David Srbeckybc90fd02015-04-22 19:40:27 +01001594 return true;
1595}
1596
David Brazdil5d5a36b2016-09-14 15:34:10 +01001597bool OatWriter::WriteVerifierDeps(OutputStream* vdex_out, verifier::VerifierDeps* verifier_deps) {
1598 if (!kIsVdexEnabled) {
1599 return true;
1600 }
1601
1602 if (verifier_deps == nullptr) {
1603 // Nothing to write. Record the offset, but no need
1604 // for alignment.
1605 vdex_verifier_deps_offset_ = vdex_size_;
1606 return true;
1607 }
1608
1609 size_t initial_offset = vdex_size_;
1610 size_t start_offset = RoundUp(initial_offset, 4u);
1611
1612 vdex_size_ = start_offset;
1613 vdex_verifier_deps_offset_ = vdex_size_;
1614 size_verifier_deps_alignment_ = start_offset - initial_offset;
1615
1616 off_t actual_offset = vdex_out->Seek(start_offset, kSeekSet);
1617 if (actual_offset != static_cast<off_t>(start_offset)) {
1618 PLOG(ERROR) << "Failed to seek to verifier deps section. Actual: " << actual_offset
1619 << " Expected: " << start_offset
1620 << " Output: " << vdex_out->GetLocation();
1621 return false;
1622 }
1623
1624 std::vector<uint8_t> buffer;
1625 verifier_deps->Encode(&buffer);
1626
1627 if (!vdex_out->WriteFully(buffer.data(), buffer.size())) {
1628 PLOG(ERROR) << "Failed to write verifier deps."
1629 << " File: " << vdex_out->GetLocation();
1630 return false;
1631 }
1632 if (!vdex_out->Flush()) {
1633 PLOG(ERROR) << "Failed to flush stream after writing verifier deps."
1634 << " File: " << vdex_out->GetLocation();
1635 return false;
1636 }
1637
1638 size_verifier_deps_ = buffer.size();
1639 vdex_size_ += size_verifier_deps_;
1640 return true;
1641}
1642
David Srbeckybc90fd02015-04-22 19:40:27 +01001643bool OatWriter::WriteCode(OutputStream* out) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001644 CHECK(write_state_ == WriteState::kWriteText);
1645
Vladimir Markoe079e212016-05-25 12:49:49 +01001646 // Wrap out to update checksum with each write.
1647 ChecksumUpdatingOutputStream checksum_updating_out(out, oat_header_.get());
1648 out = &checksum_updating_out;
1649
Vladimir Marko944da602016-02-19 12:27:55 +00001650 SetMultiOatRelativePatcherAdjustment();
1651
David Srbeckybc90fd02015-04-22 19:40:27 +01001652 const size_t file_offset = oat_data_offset_;
1653 size_t relative_offset = oat_header_->GetExecutableOffset();
1654 DCHECK_OFFSET();
1655
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001656 relative_offset = WriteCode(out, file_offset, relative_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001657 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001658 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001659 return false;
1660 }
1661
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001662 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
1663 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001664 LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001665 return false;
1666 }
1667
Vladimir Markof4da6752014-08-01 19:04:18 +01001668 const off_t oat_end_file_offset = out->Seek(0, kSeekCurrent);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001669 if (oat_end_file_offset == static_cast<off_t>(-1)) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001670 LOG(ERROR) << "Failed to get oat end file offset in " << out->GetLocation();
1671 return false;
1672 }
1673
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001674 if (kIsDebugBuild) {
1675 uint32_t size_total = 0;
1676 #define DO_STAT(x) \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -07001677 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << (x) << "B)"; \
1678 size_total += (x);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001679
David Brazdil7b49e6c2016-09-01 11:06:18 +01001680 DO_STAT(size_vdex_header_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001681 DO_STAT(size_dex_file_alignment_);
1682 DO_STAT(size_executable_offset_alignment_);
1683 DO_STAT(size_oat_header_);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001684 DO_STAT(size_oat_header_key_value_store_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001685 DO_STAT(size_dex_file_);
David Brazdil5d5a36b2016-09-14 15:34:10 +01001686 DO_STAT(size_verifier_deps_);
1687 DO_STAT(size_verifier_deps_alignment_);
Ian Rogers848871b2013-08-05 10:56:33 -07001688 DO_STAT(size_interpreter_to_interpreter_bridge_);
1689 DO_STAT(size_interpreter_to_compiled_code_bridge_);
1690 DO_STAT(size_jni_dlsym_lookup_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001691 DO_STAT(size_quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001692 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001693 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001694 DO_STAT(size_quick_to_interpreter_bridge_);
1695 DO_STAT(size_trampoline_alignment_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001696 DO_STAT(size_method_header_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001697 DO_STAT(size_code_);
1698 DO_STAT(size_code_alignment_);
Vladimir Markof4da6752014-08-01 19:04:18 +01001699 DO_STAT(size_relative_call_thunks_);
Vladimir Markoc74658b2015-03-31 10:26:41 +01001700 DO_STAT(size_misc_thunks_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001701 DO_STAT(size_vmap_table_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001702 DO_STAT(size_oat_dex_file_location_size_);
1703 DO_STAT(size_oat_dex_file_location_data_);
1704 DO_STAT(size_oat_dex_file_location_checksum_);
1705 DO_STAT(size_oat_dex_file_offset_);
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001706 DO_STAT(size_oat_dex_file_class_offsets_offset_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001707 DO_STAT(size_oat_dex_file_lookup_table_offset_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001708 DO_STAT(size_oat_lookup_table_alignment_);
1709 DO_STAT(size_oat_lookup_table_);
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001710 DO_STAT(size_oat_class_offsets_alignment_);
1711 DO_STAT(size_oat_class_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001712 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001713 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001714 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001715 DO_STAT(size_oat_class_method_offsets_);
1716 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001717
David Brazdil7b49e6c2016-09-01 11:06:18 +01001718 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)";
1719
1720 CHECK_EQ(vdex_size_ + oat_size_, size_total);
1721 CHECK_EQ(file_offset + size_total - vdex_size_, static_cast<size_t>(oat_end_file_offset));
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001722 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001723
David Brazdil7b49e6c2016-09-01 11:06:18 +01001724 CHECK_EQ(file_offset + oat_size_, static_cast<size_t>(oat_end_file_offset));
1725 CHECK_EQ(oat_size_, relative_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001726
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001727 write_state_ = WriteState::kWriteHeader;
1728 return true;
1729}
1730
1731bool OatWriter::WriteHeader(OutputStream* out,
1732 uint32_t image_file_location_oat_checksum,
1733 uintptr_t image_file_location_oat_begin,
1734 int32_t image_patch_delta) {
1735 CHECK(write_state_ == WriteState::kWriteHeader);
1736
1737 oat_header_->SetImageFileLocationOatChecksum(image_file_location_oat_checksum);
1738 oat_header_->SetImageFileLocationOatDataBegin(image_file_location_oat_begin);
1739 if (compiler_driver_->IsBootImage()) {
1740 CHECK_EQ(image_patch_delta, 0);
1741 CHECK_EQ(oat_header_->GetImagePatchDelta(), 0);
1742 } else {
1743 CHECK_ALIGNED(image_patch_delta, kPageSize);
1744 oat_header_->SetImagePatchDelta(image_patch_delta);
1745 }
Vladimir Marko49b0f452015-12-10 13:49:19 +00001746 oat_header_->UpdateChecksumWithHeaderData();
1747
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001748 const size_t file_offset = oat_data_offset_;
1749
1750 off_t current_offset = out->Seek(0, kSeekCurrent);
1751 if (current_offset == static_cast<off_t>(-1)) {
1752 PLOG(ERROR) << "Failed to get current offset from " << out->GetLocation();
1753 return false;
1754 }
Vladimir Marko49b0f452015-12-10 13:49:19 +00001755 if (out->Seek(file_offset, kSeekSet) == static_cast<off_t>(-1)) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001756 PLOG(ERROR) << "Failed to seek to oat header position in " << out->GetLocation();
1757 return false;
1758 }
David Srbeckybc90fd02015-04-22 19:40:27 +01001759 DCHECK_EQ(file_offset, static_cast<size_t>(out->Seek(0, kSeekCurrent)));
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001760
1761 // Flush all other data before writing the header.
1762 if (!out->Flush()) {
1763 PLOG(ERROR) << "Failed to flush before writing oat header to " << out->GetLocation();
1764 return false;
1765 }
1766 // Write the header.
1767 size_t header_size = oat_header_->GetHeaderSize();
Vladimir Marko49b0f452015-12-10 13:49:19 +00001768 if (!out->WriteFully(oat_header_.get(), header_size)) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001769 PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation();
1770 return false;
1771 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001772 // Flush the header data.
1773 if (!out->Flush()) {
1774 PLOG(ERROR) << "Failed to flush after writing oat header to " << out->GetLocation();
Vladimir Markof4da6752014-08-01 19:04:18 +01001775 return false;
1776 }
Vladimir Markof4da6752014-08-01 19:04:18 +01001777
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001778 if (out->Seek(current_offset, kSeekSet) == static_cast<off_t>(-1)) {
1779 PLOG(ERROR) << "Failed to seek back after writing oat header to " << out->GetLocation();
1780 return false;
1781 }
1782 DCHECK_EQ(current_offset, out->Seek(0, kSeekCurrent));
1783
1784 write_state_ = WriteState::kDone;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001785 return true;
1786}
1787
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001788bool OatWriter::WriteClassOffsets(OutputStream* out) {
1789 for (OatDexFile& oat_dex_file : oat_dex_files_) {
1790 if (oat_dex_file.class_offsets_offset_ != 0u) {
1791 uint32_t expected_offset = oat_data_offset_ + oat_dex_file.class_offsets_offset_;
1792 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
1793 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
1794 PLOG(ERROR) << "Failed to seek to oat class offsets section. Actual: " << actual_offset
1795 << " Expected: " << expected_offset << " File: " << oat_dex_file.GetLocation();
Vladimir Marko919f5532016-01-20 19:13:01 +00001796 return false;
1797 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001798 if (!oat_dex_file.WriteClassOffsets(this, out)) {
1799 return false;
1800 }
1801 }
1802 }
1803 return true;
1804}
1805
1806bool OatWriter::WriteClasses(OutputStream* out) {
1807 for (OatClass& oat_class : oat_classes_) {
1808 if (!oat_class.Write(this, out, oat_data_offset_)) {
1809 PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation();
1810 return false;
Vladimir Marko919f5532016-01-20 19:13:01 +00001811 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001812 }
1813 return true;
1814}
1815
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001816size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001817 size_t vmap_tables_offset = relative_offset;
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001818 WriteMapMethodVisitor visitor(this, out, file_offset, relative_offset);
1819 if (UNLIKELY(!VisitDexMethods(&visitor))) {
1820 return 0;
1821 }
1822 relative_offset = visitor.GetOffset();
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001823 size_vmap_table_ = relative_offset - vmap_tables_offset;
1824
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001825 return relative_offset;
1826}
1827
1828size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001829 if (compiler_driver_->IsBootImage()) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001830 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001831
Ian Rogers848871b2013-08-05 10:56:33 -07001832 #define DO_TRAMPOLINE(field) \
1833 do { \
1834 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
1835 uint32_t alignment_padding = aligned_offset - relative_offset; \
Ian Rogers3d504072014-03-01 09:16:49 -08001836 out->Seek(alignment_padding, kSeekCurrent); \
Ian Rogers848871b2013-08-05 10:56:33 -07001837 size_trampoline_alignment_ += alignment_padding; \
Vladimir Markoe079e212016-05-25 12:49:49 +01001838 if (!out->WriteFully((field)->data(), (field)->size())) { \
Ian Rogers3d504072014-03-01 09:16:49 -08001839 PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \
Ian Rogers848871b2013-08-05 10:56:33 -07001840 return false; \
1841 } \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -07001842 size_ ## field += (field)->size(); \
1843 relative_offset += alignment_padding + (field)->size(); \
Ian Rogers848871b2013-08-05 10:56:33 -07001844 DCHECK_OFFSET(); \
1845 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001846
Ian Rogers848871b2013-08-05 10:56:33 -07001847 DO_TRAMPOLINE(jni_dlsym_lookup_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001848 DO_TRAMPOLINE(quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001849 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001850 DO_TRAMPOLINE(quick_resolution_trampoline_);
1851 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
1852 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001853 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001854 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001855}
1856
Ian Rogers3d504072014-03-01 09:16:49 -08001857size_t OatWriter::WriteCodeDexFiles(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001858 const size_t file_offset,
1859 size_t relative_offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001860 #define VISIT(VisitorType) \
1861 do { \
1862 VisitorType visitor(this, out, file_offset, relative_offset); \
1863 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1864 return 0; \
1865 } \
1866 relative_offset = visitor.GetOffset(); \
1867 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001868
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001869 VISIT(WriteCodeMethodVisitor);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001870
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001871 #undef VISIT
Brian Carlstrom265091e2013-01-30 14:08:26 -08001872
Vladimir Markob163bb72015-03-31 21:49:49 +01001873 size_code_alignment_ += relative_patcher_->CodeAlignmentSize();
1874 size_relative_call_thunks_ += relative_patcher_->RelativeCallThunksSize();
1875 size_misc_thunks_ += relative_patcher_->MiscThunksSize();
1876
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001877 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001878}
1879
Vladimir Marko944da602016-02-19 12:27:55 +00001880bool OatWriter::RecordOatDataOffset(OutputStream* out) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001881 // Get the elf file offset of the oat file.
1882 const off_t raw_file_offset = out->Seek(0, kSeekCurrent);
1883 if (raw_file_offset == static_cast<off_t>(-1)) {
1884 LOG(ERROR) << "Failed to get file offset in " << out->GetLocation();
1885 return false;
1886 }
1887 oat_data_offset_ = static_cast<size_t>(raw_file_offset);
1888 return true;
1889}
1890
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001891bool OatWriter::ReadDexFileHeader(File* file, OatDexFile* oat_dex_file) {
1892 // Read the dex file header and perform minimal verification.
1893 uint8_t raw_header[sizeof(DexFile::Header)];
1894 if (!file->ReadFully(&raw_header, sizeof(DexFile::Header))) {
1895 PLOG(ERROR) << "Failed to read dex file header. Actual: "
1896 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
1897 return false;
1898 }
1899 if (!ValidateDexFileHeader(raw_header, oat_dex_file->GetLocation())) {
1900 return false;
1901 }
1902
1903 const UnalignedDexFileHeader* header = AsUnalignedDexFileHeader(raw_header);
1904 oat_dex_file->dex_file_size_ = header->file_size_;
1905 oat_dex_file->dex_file_location_checksum_ = header->checksum_;
1906 oat_dex_file->class_offsets_.resize(header->class_defs_size_);
1907 return true;
1908}
1909
1910bool OatWriter::ValidateDexFileHeader(const uint8_t* raw_header, const char* location) {
1911 if (!DexFile::IsMagicValid(raw_header)) {
1912 LOG(ERROR) << "Invalid magic number in dex file header. " << " File: " << location;
1913 return false;
1914 }
1915 if (!DexFile::IsVersionValid(raw_header)) {
1916 LOG(ERROR) << "Invalid version number in dex file header. " << " File: " << location;
1917 return false;
1918 }
1919 const UnalignedDexFileHeader* header = AsUnalignedDexFileHeader(raw_header);
1920 if (header->file_size_ < sizeof(DexFile::Header)) {
1921 LOG(ERROR) << "Dex file header specifies file size insufficient to contain the header."
1922 << " File: " << location;
1923 return false;
1924 }
1925 return true;
1926}
1927
David Brazdil7b49e6c2016-09-01 11:06:18 +01001928bool OatWriter::WriteDexFiles(OutputStream* out, File* file) {
1929 TimingLogger::ScopedTiming split("Write Dex files", timings_);
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001930
David Brazdil7b49e6c2016-09-01 11:06:18 +01001931 vdex_dex_files_offset_ = vdex_size_;
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001932
1933 // Write dex files.
1934 for (OatDexFile& oat_dex_file : oat_dex_files_) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001935 if (!WriteDexFile(out, file, &oat_dex_file)) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001936 return false;
1937 }
1938 }
1939
1940 // Close sources.
1941 for (OatDexFile& oat_dex_file : oat_dex_files_) {
1942 oat_dex_file.source_.Clear(); // Get rid of the reference, it's about to be invalidated.
1943 }
1944 zipped_dex_files_.clear();
1945 zip_archives_.clear();
1946 raw_dex_files_.clear();
1947 return true;
1948}
1949
David Brazdil7b49e6c2016-09-01 11:06:18 +01001950bool OatWriter::WriteDexFile(OutputStream* out, File* file, OatDexFile* oat_dex_file) {
1951 if (!SeekToDexFile(out, file, oat_dex_file)) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001952 return false;
1953 }
1954 if (oat_dex_file->source_.IsZipEntry()) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001955 if (!WriteDexFile(out, file, oat_dex_file, oat_dex_file->source_.GetZipEntry())) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001956 return false;
1957 }
1958 } else if (oat_dex_file->source_.IsRawFile()) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001959 if (!WriteDexFile(out, file, oat_dex_file, oat_dex_file->source_.GetRawFile())) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001960 return false;
1961 }
1962 } else {
1963 DCHECK(oat_dex_file->source_.IsRawData());
David Brazdil7b49e6c2016-09-01 11:06:18 +01001964 if (!WriteDexFile(out, oat_dex_file, oat_dex_file->source_.GetRawData())) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001965 return false;
1966 }
1967 }
1968
1969 // Update current size and account for the written data.
David Brazdil7b49e6c2016-09-01 11:06:18 +01001970 if (kIsVdexEnabled) {
1971 DCHECK_EQ(vdex_size_, oat_dex_file->dex_file_offset_);
1972 vdex_size_ += oat_dex_file->dex_file_size_;
1973 } else {
1974 DCHECK_EQ(oat_size_, oat_dex_file->dex_file_offset_);
1975 oat_size_ += oat_dex_file->dex_file_size_;
1976 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001977 size_dex_file_ += oat_dex_file->dex_file_size_;
1978 return true;
1979}
1980
1981bool OatWriter::SeekToDexFile(OutputStream* out, File* file, OatDexFile* oat_dex_file) {
1982 // Dex files are required to be 4 byte aligned.
David Brazdil7b49e6c2016-09-01 11:06:18 +01001983 size_t initial_offset = kIsVdexEnabled ? vdex_size_ : oat_size_;
1984 size_t start_offset = RoundUp(initial_offset, 4);
1985 size_t file_offset = kIsVdexEnabled ? start_offset : (oat_data_offset_ + start_offset);
1986 size_dex_file_alignment_ += start_offset - initial_offset;
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001987
1988 // Seek to the start of the dex file and flush any pending operations in the stream.
1989 // Verify that, after flushing the stream, the file is at the same offset as the stream.
David Brazdil7b49e6c2016-09-01 11:06:18 +01001990 off_t actual_offset = out->Seek(file_offset, kSeekSet);
1991 if (actual_offset != static_cast<off_t>(file_offset)) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001992 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
David Brazdil7b49e6c2016-09-01 11:06:18 +01001993 << " Expected: " << file_offset
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001994 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
1995 return false;
1996 }
1997 if (!out->Flush()) {
1998 PLOG(ERROR) << "Failed to flush before writing dex file."
1999 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2000 return false;
2001 }
2002 actual_offset = lseek(file->Fd(), 0, SEEK_CUR);
David Brazdil7b49e6c2016-09-01 11:06:18 +01002003 if (actual_offset != static_cast<off_t>(file_offset)) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002004 PLOG(ERROR) << "Stream/file position mismatch! Actual: " << actual_offset
David Brazdil7b49e6c2016-09-01 11:06:18 +01002005 << " Expected: " << file_offset
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002006 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2007 return false;
2008 }
2009
David Brazdil7b49e6c2016-09-01 11:06:18 +01002010 if (kIsVdexEnabled) {
2011 vdex_size_ = start_offset;
2012 } else {
2013 oat_size_ = start_offset;
2014 }
2015 oat_dex_file->dex_file_offset_ = start_offset;
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002016 return true;
2017}
2018
David Brazdil7b49e6c2016-09-01 11:06:18 +01002019bool OatWriter::WriteDexFile(OutputStream* out,
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002020 File* file,
2021 OatDexFile* oat_dex_file,
2022 ZipEntry* dex_file) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01002023 size_t start_offset = kIsVdexEnabled ? vdex_size_ : oat_data_offset_ + oat_size_;
2024 DCHECK_EQ(static_cast<off_t>(start_offset), out->Seek(0, kSeekCurrent));
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002025
2026 // Extract the dex file and get the extracted size.
2027 std::string error_msg;
2028 if (!dex_file->ExtractToFile(*file, &error_msg)) {
2029 LOG(ERROR) << "Failed to extract dex file from ZIP entry: " << error_msg
2030 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2031 return false;
2032 }
2033 if (file->Flush() != 0) {
2034 PLOG(ERROR) << "Failed to flush dex file from ZIP entry."
2035 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2036 return false;
2037 }
2038 off_t extracted_end = lseek(file->Fd(), 0, SEEK_CUR);
2039 if (extracted_end == static_cast<off_t>(-1)) {
2040 PLOG(ERROR) << "Failed get end offset after writing dex file from ZIP entry."
2041 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2042 return false;
2043 }
2044 if (extracted_end < static_cast<off_t>(start_offset)) {
2045 LOG(ERROR) << "Dex file end position is before start position! End: " << extracted_end
2046 << " Start: " << start_offset
2047 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2048 return false;
2049 }
2050 uint64_t extracted_size = static_cast<uint64_t>(extracted_end - start_offset);
2051 if (extracted_size < sizeof(DexFile::Header)) {
2052 LOG(ERROR) << "Extracted dex file is shorter than dex file header. size: "
2053 << extracted_size << " File: " << oat_dex_file->GetLocation();
2054 return false;
2055 }
2056
2057 // Read the dex file header and extract required data to OatDexFile.
2058 off_t actual_offset = lseek(file->Fd(), start_offset, SEEK_SET);
2059 if (actual_offset != static_cast<off_t>(start_offset)) {
2060 PLOG(ERROR) << "Failed to seek back to dex file header. Actual: " << actual_offset
2061 << " Expected: " << start_offset
2062 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2063 return false;
2064 }
2065 if (!ReadDexFileHeader(file, oat_dex_file)) {
2066 return false;
2067 }
2068 if (extracted_size < oat_dex_file->dex_file_size_) {
2069 LOG(ERROR) << "Extracted truncated dex file. Extracted size: " << extracted_size
2070 << " file size from header: " << oat_dex_file->dex_file_size_
2071 << " File: " << oat_dex_file->GetLocation();
2072 return false;
2073 }
2074
2075 // Override the checksum from header with the CRC from ZIP entry.
2076 oat_dex_file->dex_file_location_checksum_ = dex_file->GetCrc32();
2077
2078 // Seek both file and stream to the end offset.
2079 size_t end_offset = start_offset + oat_dex_file->dex_file_size_;
2080 actual_offset = lseek(file->Fd(), end_offset, SEEK_SET);
2081 if (actual_offset != static_cast<off_t>(end_offset)) {
2082 PLOG(ERROR) << "Failed to seek to end of dex file. Actual: " << actual_offset
2083 << " Expected: " << end_offset
2084 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2085 return false;
2086 }
David Brazdil7b49e6c2016-09-01 11:06:18 +01002087 actual_offset = out->Seek(end_offset, kSeekSet);
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002088 if (actual_offset != static_cast<off_t>(end_offset)) {
2089 PLOG(ERROR) << "Failed to seek stream to end of dex file. Actual: " << actual_offset
2090 << " Expected: " << end_offset << " File: " << oat_dex_file->GetLocation();
2091 return false;
2092 }
David Brazdil7b49e6c2016-09-01 11:06:18 +01002093 if (!out->Flush()) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002094 PLOG(ERROR) << "Failed to flush stream after seeking over dex file."
2095 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2096 return false;
2097 }
2098
2099 // If we extracted more than the size specified in the header, truncate the file.
2100 if (extracted_size > oat_dex_file->dex_file_size_) {
2101 if (file->SetLength(end_offset) != 0) {
2102 PLOG(ERROR) << "Failed to truncate excessive dex file length."
David Brazdil7b49e6c2016-09-01 11:06:18 +01002103 << " File: " << oat_dex_file->GetLocation()
2104 << " Output: " << file->GetPath();
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002105 return false;
2106 }
2107 }
2108
2109 return true;
2110}
2111
David Brazdil7b49e6c2016-09-01 11:06:18 +01002112bool OatWriter::WriteDexFile(OutputStream* out,
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002113 File* file,
2114 OatDexFile* oat_dex_file,
2115 File* dex_file) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01002116 size_t start_offset = kIsVdexEnabled ? vdex_size_ : oat_data_offset_ + oat_size_;
2117 DCHECK_EQ(static_cast<off_t>(start_offset), out->Seek(0, kSeekCurrent));
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002118
2119 off_t input_offset = lseek(dex_file->Fd(), 0, SEEK_SET);
2120 if (input_offset != static_cast<off_t>(0)) {
2121 PLOG(ERROR) << "Failed to seek to dex file header. Actual: " << input_offset
2122 << " Expected: 0"
2123 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2124 return false;
2125 }
2126 if (!ReadDexFileHeader(dex_file, oat_dex_file)) {
2127 return false;
2128 }
2129
2130 // Copy the input dex file using sendfile().
2131 if (!file->Copy(dex_file, 0, oat_dex_file->dex_file_size_)) {
2132 PLOG(ERROR) << "Failed to copy dex file to oat file."
2133 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2134 return false;
2135 }
2136 if (file->Flush() != 0) {
2137 PLOG(ERROR) << "Failed to flush dex file."
2138 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2139 return false;
2140 }
2141
2142 // Check file position and seek the stream to the end offset.
2143 size_t end_offset = start_offset + oat_dex_file->dex_file_size_;
2144 off_t actual_offset = lseek(file->Fd(), 0, SEEK_CUR);
2145 if (actual_offset != static_cast<off_t>(end_offset)) {
2146 PLOG(ERROR) << "Unexpected file position after copying dex file. Actual: " << actual_offset
2147 << " Expected: " << end_offset
2148 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2149 return false;
2150 }
David Brazdil7b49e6c2016-09-01 11:06:18 +01002151 actual_offset = out->Seek(end_offset, kSeekSet);
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002152 if (actual_offset != static_cast<off_t>(end_offset)) {
2153 PLOG(ERROR) << "Failed to seek stream to end of dex file. Actual: " << actual_offset
2154 << " Expected: " << end_offset << " File: " << oat_dex_file->GetLocation();
2155 return false;
2156 }
David Brazdil7b49e6c2016-09-01 11:06:18 +01002157 if (!out->Flush()) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002158 PLOG(ERROR) << "Failed to flush stream after seeking over dex file."
2159 << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath();
2160 return false;
2161 }
2162
2163 return true;
2164}
2165
David Brazdil7b49e6c2016-09-01 11:06:18 +01002166bool OatWriter::WriteDexFile(OutputStream* out,
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002167 OatDexFile* oat_dex_file,
2168 const uint8_t* dex_file) {
2169 // Note: The raw data has already been checked to contain the header
2170 // and all the data that the header specifies as the file size.
2171 DCHECK(dex_file != nullptr);
2172 DCHECK(ValidateDexFileHeader(dex_file, oat_dex_file->GetLocation()));
2173 const UnalignedDexFileHeader* header = AsUnalignedDexFileHeader(dex_file);
2174
David Brazdil7b49e6c2016-09-01 11:06:18 +01002175 if (!out->WriteFully(dex_file, header->file_size_)) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002176 PLOG(ERROR) << "Failed to write dex file " << oat_dex_file->GetLocation()
David Brazdil7b49e6c2016-09-01 11:06:18 +01002177 << " to " << out->GetLocation();
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002178 return false;
2179 }
David Brazdil7b49e6c2016-09-01 11:06:18 +01002180 if (!out->Flush()) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002181 PLOG(ERROR) << "Failed to flush stream after writing dex file."
2182 << " File: " << oat_dex_file->GetLocation();
2183 return false;
2184 }
2185
2186 // Update dex file size and resize class offsets in the OatDexFile.
2187 // Note: For raw data, the checksum is passed directly to AddRawDexFileSource().
2188 oat_dex_file->dex_file_size_ = header->file_size_;
2189 oat_dex_file->class_offsets_.resize(header->class_defs_size_);
2190 return true;
2191}
2192
2193bool OatWriter::WriteOatDexFiles(OutputStream* rodata) {
2194 TimingLogger::ScopedTiming split("WriteOatDexFiles", timings_);
2195
David Brazdil181e1cc2016-09-01 16:38:47 +00002196 off_t initial_offset = rodata->Seek(0, kSeekCurrent);
2197 if (initial_offset == static_cast<off_t>(-1)) {
2198 LOG(ERROR) << "Failed to get current position in " << rodata->GetLocation();
2199 return false;
2200 }
2201
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002202 // Seek to the start of OatDexFiles, i.e. to the end of the OatHeader. If there are
2203 // no OatDexFiles, no data is actually written to .rodata before WriteHeader() and
2204 // this Seek() ensures that we reserve the space for OatHeader in .rodata.
2205 DCHECK(oat_dex_files_.empty() || oat_dex_files_[0u].offset_ == oat_header_->GetHeaderSize());
2206 uint32_t expected_offset = oat_data_offset_ + oat_header_->GetHeaderSize();
2207 off_t actual_offset = rodata->Seek(expected_offset, kSeekSet);
2208 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
2209 PLOG(ERROR) << "Failed to seek to OatDexFile table section. Actual: " << actual_offset
2210 << " Expected: " << expected_offset << " File: " << rodata->GetLocation();
2211 return false;
2212 }
2213
2214 for (size_t i = 0, size = oat_dex_files_.size(); i != size; ++i) {
2215 OatDexFile* oat_dex_file = &oat_dex_files_[i];
2216
2217 DCHECK_EQ(oat_data_offset_ + oat_dex_file->offset_,
2218 static_cast<size_t>(rodata->Seek(0, kSeekCurrent)));
2219
2220 // Write OatDexFile.
2221 if (!oat_dex_file->Write(this, rodata)) {
2222 PLOG(ERROR) << "Failed to write oat dex information to " << rodata->GetLocation();
2223 return false;
2224 }
2225 }
2226
David Brazdil181e1cc2016-09-01 16:38:47 +00002227 // Seek back to the initial position.
2228 if (rodata->Seek(initial_offset, kSeekSet) != initial_offset) {
2229 PLOG(ERROR) << "Failed to seek to initial position. Actual: " << actual_offset
2230 << " Expected: " << initial_offset << " File: " << rodata->GetLocation();
2231 return false;
2232 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002233
David Brazdilb92ba622016-09-01 16:00:30 +00002234 return true;
2235}
2236
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002237bool OatWriter::OpenDexFiles(
2238 File* file,
Andreas Gampe3a2bd292016-01-26 17:23:47 -08002239 bool verify,
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002240 /*out*/ std::unique_ptr<MemMap>* opened_dex_files_map,
2241 /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files) {
2242 TimingLogger::ScopedTiming split("OpenDexFiles", timings_);
2243
2244 if (oat_dex_files_.empty()) {
2245 // Nothing to do.
2246 return true;
2247 }
2248
2249 size_t map_offset = oat_dex_files_[0].dex_file_offset_;
David Brazdil7b49e6c2016-09-01 11:06:18 +01002250 size_t length = kIsVdexEnabled ? (vdex_size_ - map_offset) : (oat_size_ - map_offset);
2251
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002252 std::string error_msg;
David Brazdil7b49e6c2016-09-01 11:06:18 +01002253 std::unique_ptr<MemMap> dex_files_map(MemMap::MapFile(
2254 length,
2255 PROT_READ | PROT_WRITE,
2256 MAP_SHARED,
2257 file->Fd(),
2258 kIsVdexEnabled ? map_offset : (oat_data_offset_ + map_offset),
2259 /* low_4gb */ false,
2260 file->GetPath().c_str(),
2261 &error_msg));
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002262 if (dex_files_map == nullptr) {
2263 LOG(ERROR) << "Failed to mmap() dex files from oat file. File: " << file->GetPath()
2264 << " error: " << error_msg;
2265 return false;
2266 }
2267 std::vector<std::unique_ptr<const DexFile>> dex_files;
2268 for (OatDexFile& oat_dex_file : oat_dex_files_) {
2269 // Make sure no one messed with input files while we were copying data.
2270 // At the very least we need consistent file size and number of class definitions.
2271 const uint8_t* raw_dex_file =
2272 dex_files_map->Begin() + oat_dex_file.dex_file_offset_ - map_offset;
2273 if (!ValidateDexFileHeader(raw_dex_file, oat_dex_file.GetLocation())) {
2274 // Note: ValidateDexFileHeader() already logged an error message.
2275 LOG(ERROR) << "Failed to verify written dex file header!"
2276 << " Output: " << file->GetPath() << " ~ " << std::hex << map_offset
2277 << " ~ " << static_cast<const void*>(raw_dex_file);
2278 return false;
2279 }
2280 const UnalignedDexFileHeader* header = AsUnalignedDexFileHeader(raw_dex_file);
2281 if (header->file_size_ != oat_dex_file.dex_file_size_) {
2282 LOG(ERROR) << "File size mismatch in written dex file header! Expected: "
2283 << oat_dex_file.dex_file_size_ << " Actual: " << header->file_size_
2284 << " Output: " << file->GetPath();
2285 return false;
2286 }
2287 if (header->class_defs_size_ != oat_dex_file.class_offsets_.size()) {
2288 LOG(ERROR) << "Class defs size mismatch in written dex file header! Expected: "
2289 << oat_dex_file.class_offsets_.size() << " Actual: " << header->class_defs_size_
2290 << " Output: " << file->GetPath();
2291 return false;
2292 }
2293
2294 // Now, open the dex file.
2295 dex_files.emplace_back(DexFile::Open(raw_dex_file,
2296 oat_dex_file.dex_file_size_,
2297 oat_dex_file.GetLocation(),
2298 oat_dex_file.dex_file_location_checksum_,
2299 /* oat_dex_file */ nullptr,
Andreas Gampe3a2bd292016-01-26 17:23:47 -08002300 verify,
Aart Bik37d6a3b2016-06-21 18:30:10 -07002301 verify,
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002302 &error_msg));
2303 if (dex_files.back() == nullptr) {
Andreas Gampe3a2bd292016-01-26 17:23:47 -08002304 LOG(ERROR) << "Failed to open dex file from oat file. File: " << oat_dex_file.GetLocation()
2305 << " Error: " << error_msg;
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002306 return false;
2307 }
2308 }
2309
2310 *opened_dex_files_map = std::move(dex_files_map);
2311 *opened_dex_files = std::move(dex_files);
2312 return true;
2313}
2314
2315bool OatWriter::WriteTypeLookupTables(
David Brazdil7b49e6c2016-09-01 11:06:18 +01002316 OutputStream* oat_rodata,
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002317 const std::vector<std::unique_ptr<const DexFile>>& opened_dex_files) {
2318 TimingLogger::ScopedTiming split("WriteTypeLookupTables", timings_);
2319
David Brazdil7b49e6c2016-09-01 11:06:18 +01002320 uint32_t expected_offset = oat_data_offset_ + oat_size_;
2321 off_t actual_offset = oat_rodata->Seek(expected_offset, kSeekSet);
2322 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
2323 PLOG(ERROR) << "Failed to seek to TypeLookupTable section. Actual: " << actual_offset
2324 << " Expected: " << expected_offset << " File: " << oat_rodata->GetLocation();
2325 return false;
2326 }
2327
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002328 DCHECK_EQ(opened_dex_files.size(), oat_dex_files_.size());
2329 for (size_t i = 0, size = opened_dex_files.size(); i != size; ++i) {
2330 OatDexFile* oat_dex_file = &oat_dex_files_[i];
David Brazdil181e1cc2016-09-01 16:38:47 +00002331 DCHECK_EQ(oat_dex_file->lookup_table_offset_, 0u);
2332
2333 if (oat_dex_file->create_type_lookup_table_ != CreateTypeLookupTable::kCreate ||
2334 oat_dex_file->class_offsets_.empty()) {
2335 continue;
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002336 }
David Brazdil181e1cc2016-09-01 16:38:47 +00002337
2338 size_t table_size = TypeLookupTable::RawDataLength(oat_dex_file->class_offsets_.size());
2339 if (table_size == 0u) {
2340 continue;
2341 }
2342
2343 // Create the lookup table. When `nullptr` is given as the storage buffer,
David Sehr9aa352e2016-09-15 18:13:52 -07002344 // TypeLookupTable allocates its own and OatDexFile takes ownership.
2345 oat_dex_file->InitTypeLookupTable(*opened_dex_files[i], /* storage */ nullptr);
2346 TypeLookupTable* table = oat_dex_file->GetTypeLookupTable();
David Brazdil181e1cc2016-09-01 16:38:47 +00002347
2348 // Type tables are required to be 4 byte aligned.
David Brazdil7b49e6c2016-09-01 11:06:18 +01002349 size_t initial_offset = oat_size_;
2350 size_t rodata_offset = RoundUp(initial_offset, 4);
2351 size_t padding_size = rodata_offset - initial_offset;
David Brazdil181e1cc2016-09-01 16:38:47 +00002352
2353 if (padding_size != 0u) {
2354 std::vector<uint8_t> buffer(padding_size, 0u);
David Brazdil7b49e6c2016-09-01 11:06:18 +01002355 if (!oat_rodata->WriteFully(buffer.data(), padding_size)) {
David Brazdil181e1cc2016-09-01 16:38:47 +00002356 PLOG(ERROR) << "Failed to write lookup table alignment padding."
2357 << " File: " << oat_dex_file->GetLocation()
David Brazdil7b49e6c2016-09-01 11:06:18 +01002358 << " Output: " << oat_rodata->GetLocation();
David Brazdil181e1cc2016-09-01 16:38:47 +00002359 return false;
2360 }
2361 }
2362
2363 DCHECK_EQ(oat_data_offset_ + rodata_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +01002364 static_cast<size_t>(oat_rodata->Seek(0u, kSeekCurrent)));
David Brazdil181e1cc2016-09-01 16:38:47 +00002365 DCHECK_EQ(table_size, table->RawDataLength());
2366
David Brazdil7b49e6c2016-09-01 11:06:18 +01002367 if (!oat_rodata->WriteFully(table->RawData(), table_size)) {
David Brazdil181e1cc2016-09-01 16:38:47 +00002368 PLOG(ERROR) << "Failed to write lookup table."
2369 << " File: " << oat_dex_file->GetLocation()
David Brazdil7b49e6c2016-09-01 11:06:18 +01002370 << " Output: " << oat_rodata->GetLocation();
David Brazdil181e1cc2016-09-01 16:38:47 +00002371 return false;
2372 }
2373
2374 oat_dex_file->lookup_table_offset_ = rodata_offset;
2375
David Brazdil7b49e6c2016-09-01 11:06:18 +01002376 oat_size_ += padding_size + table_size;
David Brazdil181e1cc2016-09-01 16:38:47 +00002377 size_oat_lookup_table_ += table_size;
2378 size_oat_lookup_table_alignment_ += padding_size;
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002379 }
2380
David Brazdil7b49e6c2016-09-01 11:06:18 +01002381 if (!oat_rodata->Flush()) {
David Brazdil181e1cc2016-09-01 16:38:47 +00002382 PLOG(ERROR) << "Failed to flush stream after writing type lookup tables."
David Brazdil7b49e6c2016-09-01 11:06:18 +01002383 << " File: " << oat_rodata->GetLocation();
2384 return false;
2385 }
2386
2387 return true;
2388}
2389
2390bool OatWriter::WriteVdexHeader(OutputStream* vdex_out) {
David Brazdil5d5a36b2016-09-14 15:34:10 +01002391 if (!kIsVdexEnabled) {
2392 return true;
2393 }
David Brazdil7b49e6c2016-09-01 11:06:18 +01002394 off_t actual_offset = vdex_out->Seek(0, kSeekSet);
2395 if (actual_offset != 0) {
2396 PLOG(ERROR) << "Failed to seek to the beginning of vdex file. Actual: " << actual_offset
2397 << " File: " << vdex_out->GetLocation();
2398 return false;
2399 }
2400
David Brazdil5d5a36b2016-09-14 15:34:10 +01002401 DCHECK_NE(vdex_dex_files_offset_, 0u);
2402 DCHECK_NE(vdex_verifier_deps_offset_, 0u);
2403
2404 size_t dex_section_size = vdex_verifier_deps_offset_ - vdex_dex_files_offset_;
2405 size_t verifier_deps_section_size = vdex_size_ - vdex_verifier_deps_offset_;
2406
2407 VdexFile::Header vdex_header(dex_section_size, verifier_deps_section_size);
David Brazdil7b49e6c2016-09-01 11:06:18 +01002408 if (!vdex_out->WriteFully(&vdex_header, sizeof(VdexFile::Header))) {
2409 PLOG(ERROR) << "Failed to write vdex header. File: " << vdex_out->GetLocation();
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002410 return false;
2411 }
2412
David Brazdil5d5a36b2016-09-14 15:34:10 +01002413 if (!vdex_out->Flush()) {
2414 PLOG(ERROR) << "Failed to flush stream after writing to vdex file."
2415 << " File: " << vdex_out->GetLocation();
2416 return false;
2417 }
2418
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002419 return true;
2420}
2421
Vladimir Markof4da6752014-08-01 19:04:18 +01002422bool OatWriter::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) {
2423 static const uint8_t kPadding[] = {
2424 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
2425 };
2426 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
Vladimir Markoe079e212016-05-25 12:49:49 +01002427 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
Vladimir Markof4da6752014-08-01 19:04:18 +01002428 return false;
2429 }
2430 size_code_alignment_ += aligned_code_delta;
2431 return true;
2432}
2433
Vladimir Marko944da602016-02-19 12:27:55 +00002434void OatWriter::SetMultiOatRelativePatcherAdjustment() {
2435 DCHECK(dex_files_ != nullptr);
2436 DCHECK(relative_patcher_ != nullptr);
2437 DCHECK_NE(oat_data_offset_, 0u);
2438 if (image_writer_ != nullptr && !dex_files_->empty()) {
2439 // The oat data begin may not be initialized yet but the oat file offset is ready.
2440 size_t oat_index = image_writer_->GetOatIndexForDexFile(dex_files_->front());
2441 size_t elf_file_offset = image_writer_->GetOatFileOffset(oat_index);
2442 relative_patcher_->StartOatFile(elf_file_offset + oat_data_offset_);
Vladimir Markob163bb72015-03-31 21:49:49 +01002443 }
2444}
2445
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002446OatWriter::OatDexFile::OatDexFile(const char* dex_file_location,
2447 DexFileSource source,
2448 CreateTypeLookupTable create_type_lookup_table)
2449 : source_(source),
2450 create_type_lookup_table_(create_type_lookup_table),
2451 dex_file_size_(0),
2452 offset_(0),
2453 dex_file_location_size_(strlen(dex_file_location)),
2454 dex_file_location_data_(dex_file_location),
2455 dex_file_location_checksum_(0u),
2456 dex_file_offset_(0u),
2457 class_offsets_offset_(0u),
2458 lookup_table_offset_(0u),
2459 class_offsets_() {
Brian Carlstrome24fa612011-09-29 00:53:55 -07002460}
2461
2462size_t OatWriter::OatDexFile::SizeOf() const {
2463 return sizeof(dex_file_location_size_)
2464 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -08002465 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -08002466 + sizeof(dex_file_offset_)
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002467 + sizeof(class_offsets_offset_)
2468 + sizeof(lookup_table_offset_);
Brian Carlstrome24fa612011-09-29 00:53:55 -07002469}
2470
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002471void OatWriter::OatDexFile::ReserveClassOffsets(OatWriter* oat_writer) {
2472 DCHECK_EQ(class_offsets_offset_, 0u);
2473 if (!class_offsets_.empty()) {
2474 // Class offsets are required to be 4 byte aligned.
David Brazdil7b49e6c2016-09-01 11:06:18 +01002475 size_t initial_offset = oat_writer->oat_size_;
2476 size_t offset = RoundUp(initial_offset, 4);
2477 oat_writer->size_oat_class_offsets_alignment_ += offset - initial_offset;
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002478 class_offsets_offset_ = offset;
David Brazdil7b49e6c2016-09-01 11:06:18 +01002479 oat_writer->oat_size_ = offset + GetClassOffsetsRawSize();
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002480 }
2481}
2482
2483bool OatWriter::OatDexFile::Write(OatWriter* oat_writer, OutputStream* out) const {
2484 const size_t file_offset = oat_writer->oat_data_offset_;
Brian Carlstrom265091e2013-01-30 14:08:26 -08002485 DCHECK_OFFSET_();
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002486
Vladimir Markoe079e212016-05-25 12:49:49 +01002487 if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08002488 PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07002489 return false;
2490 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07002491 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002492
Vladimir Markoe079e212016-05-25 12:49:49 +01002493 if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
Ian Rogers3d504072014-03-01 09:16:49 -08002494 PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07002495 return false;
2496 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07002497 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002498
Vladimir Markoe079e212016-05-25 12:49:49 +01002499 if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08002500 PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07002501 return false;
2502 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07002503 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002504
Vladimir Markoe079e212016-05-25 12:49:49 +01002505 if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08002506 PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08002507 return false;
2508 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07002509 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002510
Vladimir Markoe079e212016-05-25 12:49:49 +01002511 if (!out->WriteFully(&class_offsets_offset_, sizeof(class_offsets_offset_))) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002512 PLOG(ERROR) << "Failed to write class offsets offset to " << out->GetLocation();
2513 return false;
2514 }
2515 oat_writer->size_oat_dex_file_class_offsets_offset_ += sizeof(class_offsets_offset_);
2516
Vladimir Markoe079e212016-05-25 12:49:49 +01002517 if (!out->WriteFully(&lookup_table_offset_, sizeof(lookup_table_offset_))) {
Artem Udovichenkod9786b02015-10-14 16:36:55 +03002518 PLOG(ERROR) << "Failed to write lookup table offset to " << out->GetLocation();
2519 return false;
2520 }
Vladimir Marko49b0f452015-12-10 13:49:19 +00002521 oat_writer->size_oat_dex_file_lookup_table_offset_ += sizeof(lookup_table_offset_);
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002522
2523 return true;
2524}
2525
2526bool OatWriter::OatDexFile::WriteClassOffsets(OatWriter* oat_writer, OutputStream* out) {
Vladimir Markoe079e212016-05-25 12:49:49 +01002527 if (!out->WriteFully(class_offsets_.data(), GetClassOffsetsRawSize())) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002528 PLOG(ERROR) << "Failed to write oat class offsets for " << GetLocation()
2529 << " to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07002530 return false;
2531 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002532 oat_writer->size_oat_class_offsets_ += GetClassOffsetsRawSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -07002533 return true;
2534}
2535
Brian Carlstromba150c32013-08-27 17:31:03 -07002536OatWriter::OatClass::OatClass(size_t offset,
Vladimir Marko49b0f452015-12-10 13:49:19 +00002537 const dchecked_vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -07002538 uint32_t num_non_null_compiled_methods,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01002539 mirror::Class::Status status)
2540 : compiled_methods_(compiled_methods) {
2541 uint32_t num_methods = compiled_methods.size();
Brian Carlstromba150c32013-08-27 17:31:03 -07002542 CHECK_LE(num_non_null_compiled_methods, num_methods);
2543
Brian Carlstrom265091e2013-01-30 14:08:26 -08002544 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -07002545 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
2546
2547 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
2548 // apply when there are 0 methods, we just arbitrarily say that 0
2549 // methods means kOatClassNoneCompiled and that we won't use
2550 // kOatClassAllCompiled unless there is at least one compiled
2551 // method. This means in an interpretter only system, we can assert
2552 // that all classes are kOatClassNoneCompiled.
2553 if (num_non_null_compiled_methods == 0) {
2554 type_ = kOatClassNoneCompiled;
2555 } else if (num_non_null_compiled_methods == num_methods) {
2556 type_ = kOatClassAllCompiled;
2557 } else {
2558 type_ = kOatClassSomeCompiled;
2559 }
2560
Brian Carlstrom0755ec52012-01-11 15:19:46 -08002561 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -07002562 method_offsets_.resize(num_non_null_compiled_methods);
Vladimir Marko8a630572014-04-09 18:45:35 +01002563 method_headers_.resize(num_non_null_compiled_methods);
Brian Carlstromba150c32013-08-27 17:31:03 -07002564
2565 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
2566 if (type_ == kOatClassSomeCompiled) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00002567 method_bitmap_.reset(new BitVector(num_methods, false, Allocator::GetMallocAllocator()));
Brian Carlstromba150c32013-08-27 17:31:03 -07002568 method_bitmap_size_ = method_bitmap_->GetSizeOf();
2569 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
2570 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
2571 } else {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002572 method_bitmap_ = nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07002573 method_bitmap_size_ = 0;
2574 }
2575
2576 for (size_t i = 0; i < num_methods; i++) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01002577 CompiledMethod* compiled_method = compiled_methods_[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002578 if (compiled_method == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07002579 oat_method_offsets_offsets_from_oat_class_[i] = 0;
2580 } else {
2581 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
2582 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
2583 if (type_ == kOatClassSomeCompiled) {
2584 method_bitmap_->SetBit(i);
2585 }
2586 }
2587 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07002588}
2589
Brian Carlstrom265091e2013-01-30 14:08:26 -08002590size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
2591 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07002592 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
2593 if (method_offset == 0) {
2594 return 0;
2595 }
2596 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08002597}
2598
2599size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
2600 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07002601 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08002602}
2603
2604size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07002605 return sizeof(status_)
2606 + sizeof(type_)
2607 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
2608 + method_bitmap_size_
2609 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07002610}
2611
Brian Carlstromc50d8e12013-07-23 22:35:16 -07002612bool OatWriter::OatClass::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08002613 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07002614 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08002615 DCHECK_OFFSET_();
Vladimir Markoe079e212016-05-25 12:49:49 +01002616 if (!out->WriteFully(&status_, sizeof(status_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08002617 PLOG(ERROR) << "Failed to write class status to " << out->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08002618 return false;
2619 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07002620 oat_writer->size_oat_class_status_ += sizeof(status_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00002621
Vladimir Markoe079e212016-05-25 12:49:49 +01002622 if (!out->WriteFully(&type_, sizeof(type_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08002623 PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07002624 return false;
2625 }
2626 oat_writer->size_oat_class_type_ += sizeof(type_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00002627
Brian Carlstromba150c32013-08-27 17:31:03 -07002628 if (method_bitmap_size_ != 0) {
2629 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markoe079e212016-05-25 12:49:49 +01002630 if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
Ian Rogers3d504072014-03-01 09:16:49 -08002631 PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07002632 return false;
2633 }
2634 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00002635
Vladimir Markoe079e212016-05-25 12:49:49 +01002636 if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
Ian Rogers3d504072014-03-01 09:16:49 -08002637 PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07002638 return false;
2639 }
2640 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
2641 }
Vladimir Marko49b0f452015-12-10 13:49:19 +00002642
Vladimir Markoe079e212016-05-25 12:49:49 +01002643 if (!out->WriteFully(method_offsets_.data(), GetMethodOffsetsRawSize())) {
Ian Rogers3d504072014-03-01 09:16:49 -08002644 PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07002645 return false;
2646 }
Vladimir Marko49b0f452015-12-10 13:49:19 +00002647 oat_writer->size_oat_class_method_offsets_ += GetMethodOffsetsRawSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -07002648 return true;
2649}
2650
Brian Carlstrome24fa612011-09-29 00:53:55 -07002651} // namespace art