blob: 3f2271ef11785be33fea49212b1811d5a965247e [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrome24fa612011-09-29 00:53:55 -070016
17#include "oat_writer.h"
18
Elliott Hughesa0e18062012-04-13 15:59:59 -070019#include <zlib.h>
20
Vladimir Markoc74658b2015-03-31 10:26:41 +010021#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Ian Rogerse77493c2014-08-20 15:08:45 -070023#include "base/allocator.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070024#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080025#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080026#include "base/unix_file/fd_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070027#include "class_linker.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070028#include "compiled_class.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000029#include "compiled_method.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "dex_file-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000031#include "dex/verification_results.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000032#include "driver/compiler_driver.h"
33#include "driver/compiler_options.h"
Vladimir Marko09d09432015-09-08 13:47:48 +010034#include "gc/space/image_space.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070035#include "gc/space/space.h"
Artem Udovichenkod9786b02015-10-14 16:36:55 +030036#include "handle_scope-inl.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010037#include "image_writer.h"
Vladimir Markob163bb72015-03-31 21:49:49 +010038#include "linker/relative_patcher.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/array.h"
40#include "mirror/class_loader.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010041#include "mirror/dex_cache-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070042#include "mirror/object-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010043#include "oat_quick_method_header.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070044#include "os.h"
Brian Carlstromcd60ac72013-01-20 17:09:51 -080045#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070046#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070047#include "scoped_thread_state_change.h"
Artem Udovichenkod9786b02015-10-14 16:36:55 +030048#include "type_lookup_table.h"
Vladimir Marko09d09432015-09-08 13:47:48 +010049#include "utils/dex_cache_arrays_layout-inl.h"
jeffhaoec014232012-09-05 10:42:25 -070050#include "verifier/method_verifier.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070051
52namespace art {
53
Vladimir Marko96c6ab92014-04-08 14:00:50 +010054#define DCHECK_OFFSET() \
55 DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out->Seek(0, kSeekCurrent)) \
56 << "file_offset=" << file_offset << " relative_offset=" << relative_offset
57
58#define DCHECK_OFFSET_() \
59 DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out->Seek(0, kSeekCurrent)) \
60 << "file_offset=" << file_offset << " offset_=" << offset_
61
Brian Carlstrom3320cf42011-10-04 14:58:28 -070062OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070063 uint32_t image_file_location_oat_checksum,
Ian Rogersef7d42f2014-01-06 12:55:46 -080064 uintptr_t image_file_location_oat_begin,
Alex Lighta59dd802014-07-02 16:28:08 -070065 int32_t image_patch_delta,
Ian Rogersca368cb2013-11-15 15:52:08 -080066 const CompilerDriver* compiler,
Vladimir Markof4da6752014-08-01 19:04:18 +010067 ImageWriter* image_writer,
Mathieu Chartierda5b28a2015-11-05 08:03:47 -080068 bool compiling_boot_image,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070069 TimingLogger* timings,
70 SafeMap<std::string, std::string>* key_value_store)
Jeff Hao0aba0ba2013-06-03 14:49:28 -070071 : compiler_driver_(compiler),
Vladimir Markof4da6752014-08-01 19:04:18 +010072 image_writer_(image_writer),
Mathieu Chartierda5b28a2015-11-05 08:03:47 -080073 compiling_boot_image_(compiling_boot_image),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070074 dex_files_(&dex_files),
Vladimir Markof4da6752014-08-01 19:04:18 +010075 size_(0u),
Vladimir Marko5c42c292015-02-25 12:02:49 +000076 bss_size_(0u),
Vladimir Markof4da6752014-08-01 19:04:18 +010077 oat_data_offset_(0u),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070078 image_file_location_oat_checksum_(image_file_location_oat_checksum),
79 image_file_location_oat_begin_(image_file_location_oat_begin),
Alex Lighta59dd802014-07-02 16:28:08 -070080 image_patch_delta_(image_patch_delta),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070081 key_value_store_(key_value_store),
Mathieu Chartier2cebb242015-04-21 16:50:40 -070082 oat_header_(nullptr),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070083 size_dex_file_alignment_(0),
84 size_executable_offset_alignment_(0),
85 size_oat_header_(0),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070086 size_oat_header_key_value_store_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070087 size_dex_file_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070088 size_interpreter_to_interpreter_bridge_(0),
89 size_interpreter_to_compiled_code_bridge_(0),
90 size_jni_dlsym_lookup_(0),
Andreas Gampe2da88232014-02-27 12:26:20 -080091 size_quick_generic_jni_trampoline_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070092 size_quick_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070093 size_quick_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070094 size_quick_to_interpreter_bridge_(0),
95 size_trampoline_alignment_(0),
Vladimir Marko96c6ab92014-04-08 14:00:50 +010096 size_method_header_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070097 size_code_(0),
98 size_code_alignment_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +010099 size_relative_call_thunks_(0),
Vladimir Markoc74658b2015-03-31 10:26:41 +0100100 size_misc_thunks_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700101 size_mapping_table_(0),
102 size_vmap_table_(0),
103 size_gc_map_(0),
104 size_oat_dex_file_location_size_(0),
105 size_oat_dex_file_location_data_(0),
106 size_oat_dex_file_location_checksum_(0),
107 size_oat_dex_file_offset_(0),
108 size_oat_dex_file_methods_offsets_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -0700109 size_oat_class_type_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700110 size_oat_class_status_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -0700111 size_oat_class_method_bitmaps_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +0100112 size_oat_class_method_offsets_(0),
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300113 size_oat_lookup_table_alignment_(0),
114 size_oat_lookup_table_offset_(0),
115 size_oat_lookup_table_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +0100116 method_offset_map_() {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700117 CHECK(key_value_store != nullptr);
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800118 if (compiling_boot_image) {
119 CHECK(image_writer != nullptr);
120 }
Vladimir Markob163bb72015-03-31 21:49:49 +0100121 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
122 const InstructionSetFeatures* features = compiler_driver_->GetInstructionSetFeatures();
123 relative_patcher_ = linker::RelativePatcher::Create(instruction_set, features,
124 &method_offset_map_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100125
Ian Rogersca368cb2013-11-15 15:52:08 -0800126 size_t offset;
127 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700128 TimingLogger::ScopedTiming split("InitOatHeader", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800129 offset = InitOatHeader();
130 }
131 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700132 TimingLogger::ScopedTiming split("InitOatDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800133 offset = InitOatDexFiles(offset);
134 }
135 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700136 TimingLogger::ScopedTiming split("InitDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800137 offset = InitDexFiles(offset);
138 }
139 {
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300140 TimingLogger::ScopedTiming split("InitLookupTables", timings);
141 offset = InitLookupTables(offset);
142 }
143 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700144 TimingLogger::ScopedTiming split("InitOatClasses", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800145 offset = InitOatClasses(offset);
146 }
147 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700148 TimingLogger::ScopedTiming split("InitOatMaps", timings);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100149 offset = InitOatMaps(offset);
150 }
151 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700152 TimingLogger::ScopedTiming split("InitOatCode", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800153 offset = InitOatCode(offset);
154 }
155 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700156 TimingLogger::ScopedTiming split("InitOatCodeDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800157 offset = InitOatCodeDexFiles(offset);
158 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700159 size_ = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700160
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800161 if (!HasBootImage()) {
Vladimir Marko09d09432015-09-08 13:47:48 +0100162 // Allocate space for app dex cache arrays in the .bss section.
163 size_t bss_start = RoundUp(size_, kPageSize);
164 size_t pointer_size = GetInstructionSetPointerSize(instruction_set);
165 bss_size_ = 0u;
166 for (const DexFile* dex_file : dex_files) {
167 dex_cache_arrays_offsets_.Put(dex_file, bss_start + bss_size_);
168 DexCacheArraysLayout layout(pointer_size, dex_file);
169 bss_size_ += layout.Size();
170 }
171 }
172
Brian Carlstrome24fa612011-09-29 00:53:55 -0700173 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800174 if (compiling_boot_image_) {
175 CHECK_EQ(image_writer_ != nullptr,
176 key_value_store_->find(OatHeader::kImageLocationKey) == key_value_store_->end());
177 }
Alex Lighta59dd802014-07-02 16:28:08 -0700178 CHECK_ALIGNED(image_patch_delta_, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700179}
180
Ian Rogers0571d352011-11-03 19:51:38 -0700181OatWriter::~OatWriter() {
182 delete oat_header_;
183 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800184 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -0700185}
186
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100187struct OatWriter::GcMapDataAccess {
Vladimir Marko35831e82015-09-11 11:59:18 +0100188 static ArrayRef<const uint8_t> GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100189 return compiled_method->GetGcMap();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100190 }
191
192 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800193 uint32_t offset = oat_class->method_headers_[method_offsets_index].gc_map_offset_;
194 return offset == 0u ? 0u :
195 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100196 }
197
198 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
199 ALWAYS_INLINE {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800200 oat_class->method_headers_[method_offsets_index].gc_map_offset_ =
201 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100202 }
203
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800204 static const char* Name() {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100205 return "GC map";
206 }
207};
208
209struct OatWriter::MappingTableDataAccess {
Vladimir Marko35831e82015-09-11 11:59:18 +0100210 static ArrayRef<const uint8_t> GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000211 return compiled_method->GetMappingTable();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100212 }
213
214 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100215 uint32_t offset = oat_class->method_headers_[method_offsets_index].mapping_table_offset_;
216 return offset == 0u ? 0u :
217 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100218 }
219
220 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
221 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100222 oat_class->method_headers_[method_offsets_index].mapping_table_offset_ =
223 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100224 }
225
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800226 static const char* Name() {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100227 return "mapping table";
228 }
229};
230
231struct OatWriter::VmapTableDataAccess {
Vladimir Marko35831e82015-09-11 11:59:18 +0100232 static ArrayRef<const uint8_t> GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800233 return compiled_method->GetVmapTable();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100234 }
235
236 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100237 uint32_t offset = oat_class->method_headers_[method_offsets_index].vmap_table_offset_;
238 return offset == 0u ? 0u :
239 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100240 }
241
242 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
243 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100244 oat_class->method_headers_[method_offsets_index].vmap_table_offset_ =
245 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100246 }
247
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800248 static const char* Name() {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100249 return "vmap table";
250 }
251};
252
253class OatWriter::DexMethodVisitor {
254 public:
255 DexMethodVisitor(OatWriter* writer, size_t offset)
256 : writer_(writer),
257 offset_(offset),
258 dex_file_(nullptr),
259 class_def_index_(DexFile::kDexNoIndex) {
260 }
261
262 virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) {
263 DCHECK(dex_file_ == nullptr);
264 DCHECK_EQ(class_def_index_, DexFile::kDexNoIndex);
265 dex_file_ = dex_file;
266 class_def_index_ = class_def_index;
267 return true;
268 }
269
270 virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0;
271
272 virtual bool EndClass() {
273 if (kIsDebugBuild) {
274 dex_file_ = nullptr;
275 class_def_index_ = DexFile::kDexNoIndex;
276 }
277 return true;
278 }
279
280 size_t GetOffset() const {
281 return offset_;
282 }
283
284 protected:
285 virtual ~DexMethodVisitor() { }
286
287 OatWriter* const writer_;
288
289 // The offset is usually advanced for each visited method by the derived class.
290 size_t offset_;
291
292 // The dex file and class def index are set in StartClass().
293 const DexFile* dex_file_;
294 size_t class_def_index_;
295};
296
297class OatWriter::OatDexMethodVisitor : public DexMethodVisitor {
298 public:
299 OatDexMethodVisitor(OatWriter* writer, size_t offset)
300 : DexMethodVisitor(writer, offset),
301 oat_class_index_(0u),
302 method_offsets_index_(0u) {
303 }
304
305 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
306 DexMethodVisitor::StartClass(dex_file, class_def_index);
307 DCHECK_LT(oat_class_index_, writer_->oat_classes_.size());
308 method_offsets_index_ = 0u;
309 return true;
310 }
311
312 bool EndClass() {
313 ++oat_class_index_;
314 return DexMethodVisitor::EndClass();
315 }
316
317 protected:
318 size_t oat_class_index_;
319 size_t method_offsets_index_;
320};
321
322class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor {
323 public:
324 InitOatClassesMethodVisitor(OatWriter* writer, size_t offset)
325 : DexMethodVisitor(writer, offset),
326 compiled_methods_(),
327 num_non_null_compiled_methods_(0u) {
328 compiled_methods_.reserve(256u);
329 }
330
331 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
332 DexMethodVisitor::StartClass(dex_file, class_def_index);
333 compiled_methods_.clear();
334 num_non_null_compiled_methods_ = 0u;
335 return true;
336 }
337
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300338 bool VisitMethod(size_t class_def_method_index ATTRIBUTE_UNUSED,
339 const ClassDataItemIterator& it) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100340 // Fill in the compiled_methods_ array for methods that have a
341 // CompiledMethod. We track the number of non-null entries in
342 // num_non_null_compiled_methods_ since we only want to allocate
343 // OatMethodOffsets for the compiled methods.
344 uint32_t method_idx = it.GetMemberIndex();
345 CompiledMethod* compiled_method =
346 writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx));
347 compiled_methods_.push_back(compiled_method);
348 if (compiled_method != nullptr) {
349 ++num_non_null_compiled_methods_;
350 }
351 return true;
352 }
353
354 bool EndClass() {
355 ClassReference class_ref(dex_file_, class_def_index_);
356 CompiledClass* compiled_class = writer_->compiler_driver_->GetCompiledClass(class_ref);
357 mirror::Class::Status status;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700358 if (compiled_class != nullptr) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100359 status = compiled_class->GetStatus();
360 } else if (writer_->compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) {
361 status = mirror::Class::kStatusError;
362 } else {
363 status = mirror::Class::kStatusNotReady;
364 }
365
366 OatClass* oat_class = new OatClass(offset_, compiled_methods_,
367 num_non_null_compiled_methods_, status);
368 writer_->oat_classes_.push_back(oat_class);
Vladimir Markof4da6752014-08-01 19:04:18 +0100369 oat_class->UpdateChecksum(writer_->oat_header_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100370 offset_ += oat_class->SizeOf();
371 return DexMethodVisitor::EndClass();
372 }
373
374 private:
375 std::vector<CompiledMethod*> compiled_methods_;
376 size_t num_non_null_compiled_methods_;
377};
378
379class OatWriter::InitCodeMethodVisitor : public OatDexMethodVisitor {
380 public:
381 InitCodeMethodVisitor(OatWriter* writer, size_t offset)
David Srbecky009e2a62015-04-15 02:46:30 +0100382 : OatDexMethodVisitor(writer, offset),
David Srbecky009e2a62015-04-15 02:46:30 +0100383 debuggable_(writer->GetCompilerDriver()->GetCompilerOptions().GetDebuggable()) {
David Srbeckyf8980872015-05-22 17:04:47 +0100384 writer_->absolute_patch_locations_.reserve(
Vladimir Markof4da6752014-08-01 19:04:18 +0100385 writer_->compiler_driver_->GetNonRelativeLinkerPatchCount());
386 }
387
388 bool EndClass() {
389 OatDexMethodVisitor::EndClass();
390 if (oat_class_index_ == writer_->oat_classes_.size()) {
Vladimir Marko71b0ddf2015-04-02 19:45:06 +0100391 offset_ = writer_->relative_patcher_->ReserveSpaceEnd(offset_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100392 }
393 return true;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100394 }
395
396 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
Mathieu Chartier90443472015-07-16 20:32:27 -0700397 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100398 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
399 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
400
401 if (compiled_method != nullptr) {
402 // Derived from CompiledMethod.
403 uint32_t quick_code_offset = 0;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100404
Vladimir Marko35831e82015-09-11 11:59:18 +0100405 ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode();
406 uint32_t code_size = quick_code.size() * sizeof(uint8_t);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800407 uint32_t thumb_offset = compiled_method->CodeDelta();
Elliott Hughes956af0f2014-12-11 14:34:28 -0800408
David Srbecky009e2a62015-04-15 02:46:30 +0100409 // Deduplicate code arrays if we are not producing debuggable code.
Vladimir Markoc74658b2015-03-31 10:26:41 +0100410 bool deduped = false;
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000411 if (debuggable_) {
412 quick_code_offset = NewQuickCodeOffset(compiled_method, it, thumb_offset);
413 } else {
414 auto lb = dedupe_map_.lower_bound(compiled_method);
415 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(compiled_method, lb->first)) {
416 quick_code_offset = lb->second;
417 deduped = true;
Nicolas Geoffrayf0758792015-07-13 11:56:00 +0000418 } else {
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000419 quick_code_offset = NewQuickCodeOffset(compiled_method, it, thumb_offset);
420 dedupe_map_.PutBefore(lb, compiled_method, quick_code_offset);
David Srbecky009e2a62015-04-15 02:46:30 +0100421 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800422 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100423
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100424 if (code_size != 0) {
425 MethodReference method_ref(dex_file_, it.GetMemberIndex());
426 auto method_lb = writer_->method_offset_map_.map.lower_bound(method_ref);
427 if (method_lb != writer_->method_offset_map_.map.end() &&
428 !writer_->method_offset_map_.map.key_comp()(method_ref, method_lb->first)) {
429 // TODO: Should this be a hard failure?
430 LOG(WARNING) << "Multiple definitions of "
431 << PrettyMethod(method_ref.dex_method_index, *method_ref.dex_file)
432 << ((method_lb->second != quick_code_offset) ? "; OFFSET MISMATCH" : "");
433 } else {
434 writer_->method_offset_map_.map.PutBefore(method_lb, method_ref, quick_code_offset);
435 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800436 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100437
Elliott Hughes956af0f2014-12-11 14:34:28 -0800438 // Update quick method header.
439 DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size());
440 OatQuickMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_];
441 uint32_t mapping_table_offset = method_header->mapping_table_offset_;
442 uint32_t vmap_table_offset = method_header->vmap_table_offset_;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100443 // If we don't have quick code, then we must have a vmap, as that is how the dex2dex
444 // compiler records its transformations.
Vladimir Marko35831e82015-09-11 11:59:18 +0100445 DCHECK(!quick_code.empty() || vmap_table_offset != 0);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800446 uint32_t gc_map_offset = method_header->gc_map_offset_;
447 // The code offset was 0 when the mapping/vmap table offset was set, so it's set
448 // to 0-offset and we need to adjust it by code_offset.
449 uint32_t code_offset = quick_code_offset - thumb_offset;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100450 if (mapping_table_offset != 0u && code_offset != 0u) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800451 mapping_table_offset += code_offset;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100452 DCHECK_LT(mapping_table_offset, code_offset) << "Overflow in oat offsets";
Elliott Hughes956af0f2014-12-11 14:34:28 -0800453 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100454 if (vmap_table_offset != 0u && code_offset != 0u) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800455 vmap_table_offset += code_offset;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100456 DCHECK_LT(vmap_table_offset, code_offset) << "Overflow in oat offsets";
Elliott Hughes956af0f2014-12-11 14:34:28 -0800457 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100458 if (gc_map_offset != 0u && code_offset != 0u) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800459 gc_map_offset += code_offset;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100460 DCHECK_LT(gc_map_offset, code_offset) << "Overflow in oat offsets";
Elliott Hughes956af0f2014-12-11 14:34:28 -0800461 }
462 uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
463 uint32_t core_spill_mask = compiled_method->GetCoreSpillMask();
464 uint32_t fp_spill_mask = compiled_method->GetFpSpillMask();
465 *method_header = OatQuickMethodHeader(mapping_table_offset, vmap_table_offset,
466 gc_map_offset, frame_size_in_bytes, core_spill_mask,
467 fp_spill_mask, code_size);
Vladimir Marko7624d252014-05-02 14:40:15 +0100468
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000469 if (!deduped) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800470 // Update offsets. (Checksum is updated when writing.)
471 offset_ += sizeof(*method_header); // Method header is prepended before code.
472 offset_ += code_size;
473 // Record absolute patch locations.
474 if (!compiled_method->GetPatches().empty()) {
475 uintptr_t base_loc = offset_ - code_size - writer_->oat_header_->GetExecutableOffset();
476 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
Vladimir Marko20f85592015-03-19 10:07:02 +0000477 if (!patch.IsPcRelative()) {
David Srbeckyf8980872015-05-22 17:04:47 +0100478 writer_->absolute_patch_locations_.push_back(base_loc + patch.LiteralOffset());
Vladimir Markof4da6752014-08-01 19:04:18 +0100479 }
480 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100481 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800482 }
Alex Light78382fa2014-06-06 15:45:32 -0700483
David Srbecky8363c772015-05-28 16:12:43 +0100484 if (writer_->compiler_driver_->GetCompilerOptions().GetGenerateDebugInfo()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800485 // Record debug information for this function if we are doing that.
Elliott Hughes956af0f2014-12-11 14:34:28 -0800486 const uint32_t quick_code_start = quick_code_offset -
David Srbecky6f715892015-03-30 14:21:42 +0100487 writer_->oat_header_->GetExecutableOffset() - thumb_offset;
David Srbecky0df9e1f2015-04-07 19:02:58 +0100488 writer_->method_info_.push_back(DebugInfo {
489 dex_file_,
490 class_def_index_,
491 it.GetMemberIndex(),
492 it.GetMethodAccessFlags(),
493 it.GetMethodCodeItem(),
494 deduped,
495 quick_code_start,
496 quick_code_start + code_size,
497 compiled_method});
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100498 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100499
500 if (kIsDebugBuild) {
501 // We expect GC maps except when the class hasn't been verified or the method is native.
502 const CompilerDriver* compiler_driver = writer_->compiler_driver_;
503 ClassReference class_ref(dex_file_, class_def_index_);
504 CompiledClass* compiled_class = compiler_driver->GetCompiledClass(class_ref);
505 mirror::Class::Status status;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700506 if (compiled_class != nullptr) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100507 status = compiled_class->GetStatus();
508 } else if (compiler_driver->GetVerificationResults()->IsClassRejected(class_ref)) {
509 status = mirror::Class::kStatusError;
510 } else {
511 status = mirror::Class::kStatusNotReady;
512 }
Vladimir Marko35831e82015-09-11 11:59:18 +0100513 ArrayRef<const uint8_t> gc_map = compiled_method->GetGcMap();
514 if (!gc_map.empty()) {
515 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
Andreas Gampe51829322014-08-25 15:05:04 -0700516 bool is_native = it.MemberIsNative();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100517 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
Vladimir Marko35831e82015-09-11 11:59:18 +0100518 << gc_map_size << " " << (is_native ? "true" : "false") << " "
Nicolas Geoffray39468442014-09-02 15:17:15 +0100519 << (status < mirror::Class::kStatusVerified) << " " << status << " "
520 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
521 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100522 }
523
524 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
525 OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_];
526 offsets->code_offset_ = quick_code_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100527 ++method_offsets_index_;
528 }
529
530 return true;
531 }
532
533 private:
Vladimir Marko20f85592015-03-19 10:07:02 +0000534 struct CodeOffsetsKeyComparator {
535 bool operator()(const CompiledMethod* lhs, const CompiledMethod* rhs) const {
Vladimir Marko35831e82015-09-11 11:59:18 +0100536 // Code is deduplicated by CompilerDriver, compare only data pointers.
537 if (lhs->GetQuickCode().data() != rhs->GetQuickCode().data()) {
538 return lhs->GetQuickCode().data() < rhs->GetQuickCode().data();
Vladimir Marko20f85592015-03-19 10:07:02 +0000539 }
540 // If the code is the same, all other fields are likely to be the same as well.
Vladimir Marko35831e82015-09-11 11:59:18 +0100541 if (UNLIKELY(lhs->GetMappingTable().data() != rhs->GetMappingTable().data())) {
542 return lhs->GetMappingTable().data() < rhs->GetMappingTable().data();
Vladimir Marko20f85592015-03-19 10:07:02 +0000543 }
Vladimir Marko35831e82015-09-11 11:59:18 +0100544 if (UNLIKELY(lhs->GetVmapTable().data() != rhs->GetVmapTable().data())) {
545 return lhs->GetVmapTable().data() < rhs->GetVmapTable().data();
Vladimir Marko20f85592015-03-19 10:07:02 +0000546 }
Vladimir Marko35831e82015-09-11 11:59:18 +0100547 if (UNLIKELY(lhs->GetGcMap().data() != rhs->GetGcMap().data())) {
548 return lhs->GetGcMap().data() < rhs->GetGcMap().data();
Vladimir Marko20f85592015-03-19 10:07:02 +0000549 }
Vladimir Marko35831e82015-09-11 11:59:18 +0100550 if (UNLIKELY(lhs->GetPatches().data() != rhs->GetPatches().data())) {
551 return lhs->GetPatches().data() < rhs->GetPatches().data();
Vladimir Marko20f85592015-03-19 10:07:02 +0000552 }
553 return false;
554 }
555 };
556
David Srbecky009e2a62015-04-15 02:46:30 +0100557 uint32_t NewQuickCodeOffset(CompiledMethod* compiled_method,
558 const ClassDataItemIterator& it,
559 uint32_t thumb_offset) {
560 offset_ = writer_->relative_patcher_->ReserveSpace(
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100561 offset_, compiled_method, MethodReference(dex_file_, it.GetMemberIndex()));
David Srbecky009e2a62015-04-15 02:46:30 +0100562 offset_ = compiled_method->AlignCode(offset_);
563 DCHECK_ALIGNED_PARAM(offset_,
564 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
565 return offset_ + sizeof(OatQuickMethodHeader) + thumb_offset;
566 }
567
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100568 // Deduplication is already done on a pointer basis by the compiler driver,
569 // so we can simply compare the pointers to find out if things are duplicated.
Vladimir Marko8a630572014-04-09 18:45:35 +0100570 SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_;
David Srbecky2f6cdb02015-04-11 00:17:53 +0100571
David Srbecky009e2a62015-04-15 02:46:30 +0100572 // Cache of compiler's --debuggable option.
573 const bool debuggable_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100574};
575
576template <typename DataAccess>
577class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor {
578 public:
579 InitMapMethodVisitor(OatWriter* writer, size_t offset)
580 : OatDexMethodVisitor(writer, offset) {
581 }
582
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700583 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it ATTRIBUTE_UNUSED)
Mathieu Chartier90443472015-07-16 20:32:27 -0700584 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100585 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
586 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
587
588 if (compiled_method != nullptr) {
589 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
590 DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u);
591
Vladimir Marko35831e82015-09-11 11:59:18 +0100592 ArrayRef<const uint8_t> map = DataAccess::GetData(compiled_method);
593 uint32_t map_size = map.size() * sizeof(map[0]);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100594 if (map_size != 0u) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100595 auto lb = dedupe_map_.lower_bound(map.data());
596 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(map.data(), lb->first)) {
Vladimir Markobd72fc12014-07-09 16:06:40 +0100597 DataAccess::SetOffset(oat_class, method_offsets_index_, lb->second);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100598 } else {
599 DataAccess::SetOffset(oat_class, method_offsets_index_, offset_);
Vladimir Marko35831e82015-09-11 11:59:18 +0100600 dedupe_map_.PutBefore(lb, map.data(), offset_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100601 offset_ += map_size;
Vladimir Marko35831e82015-09-11 11:59:18 +0100602 writer_->oat_header_->UpdateChecksum(&map[0], map_size);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100603 }
604 }
605 ++method_offsets_index_;
606 }
607
608 return true;
609 }
610
611 private:
612 // Deduplication is already done on a pointer basis by the compiler driver,
613 // so we can simply compare the pointers to find out if things are duplicated.
Vladimir Marko35831e82015-09-11 11:59:18 +0100614 SafeMap<const uint8_t*, uint32_t> dedupe_map_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100615};
616
617class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor {
618 public:
619 InitImageMethodVisitor(OatWriter* writer, size_t offset)
Jeff Haoc7d11882015-02-03 15:08:39 -0800620 : OatDexMethodVisitor(writer, offset),
621 pointer_size_(GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet())) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100622 }
623
624 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
Mathieu Chartier90443472015-07-16 20:32:27 -0700625 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100626 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
627 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
628
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800629 OatMethodOffsets offsets(0u);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100630 if (compiled_method != nullptr) {
631 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
632 offsets = oat_class->method_offsets_[method_offsets_index_];
633 ++method_offsets_index_;
634 }
635
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100636 ClassLinker* linker = Runtime::Current()->GetClassLinker();
637 InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_));
638 // Unchecked as we hold mutator_lock_ on entry.
639 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800640 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700641 Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache(
642 Thread::Current(), *dex_file_)));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700643 ArtMethod* method = linker->ResolveMethod(
644 *dex_file_, it.GetMemberIndex(), dex_cache, NullHandle<mirror::ClassLoader>(), nullptr,
645 invoke_type);
Andreas Gamped9efea62014-07-21 22:56:08 -0700646 if (method == nullptr) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100647 LOG(INTERNAL_FATAL) << "Unexpected failure to resolve a method: "
648 << PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
Andreas Gamped9efea62014-07-21 22:56:08 -0700649 soa.Self()->AssertPendingException();
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000650 mirror::Throwable* exc = soa.Self()->GetException();
Andreas Gamped9efea62014-07-21 22:56:08 -0700651 std::string dump = exc->Dump();
652 LOG(FATAL) << dump;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100653 UNREACHABLE();
Andreas Gamped9efea62014-07-21 22:56:08 -0700654 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100655
Vladimir Marko35831e82015-09-11 11:59:18 +0100656 if (compiled_method != nullptr && compiled_method->GetQuickCode().size() != 0) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100657 method->SetEntryPointFromQuickCompiledCodePtrSize(
658 reinterpret_cast<void*>(offsets.code_offset_), pointer_size_);
659 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100660
661 return true;
662 }
Jeff Haoc7d11882015-02-03 15:08:39 -0800663
664 protected:
665 const size_t pointer_size_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100666};
667
668class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor {
669 public:
670 WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
Vladimir Markof4da6752014-08-01 19:04:18 +0100671 size_t relative_offset) SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100672 : OatDexMethodVisitor(writer, relative_offset),
673 out_(out),
Vladimir Markof4da6752014-08-01 19:04:18 +0100674 file_offset_(file_offset),
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100675 soa_(Thread::Current()),
676 no_thread_suspension_(soa_.Self(), "OatWriter patching"),
Vladimir Markof4da6752014-08-01 19:04:18 +0100677 class_linker_(Runtime::Current()->GetClassLinker()),
678 dex_cache_(nullptr) {
Vladimir Marko09d09432015-09-08 13:47:48 +0100679 patched_code_.reserve(16 * KB);
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800680 if (writer_->HasBootImage()) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100681 // If we're creating the image, the address space must be ready so that we can apply patches.
682 CHECK(writer_->image_writer_->IsImageAddressSpaceReady());
Vladimir Markof4da6752014-08-01 19:04:18 +0100683 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100684 }
685
Vladimir Markof4da6752014-08-01 19:04:18 +0100686 ~WriteCodeMethodVisitor() UNLOCK_FUNCTION(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100687 }
688
689 bool StartClass(const DexFile* dex_file, size_t class_def_index)
Mathieu Chartier90443472015-07-16 20:32:27 -0700690 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100691 OatDexMethodVisitor::StartClass(dex_file, class_def_index);
692 if (dex_cache_ == nullptr || dex_cache_->GetDexFile() != dex_file) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700693 dex_cache_ = class_linker_->FindDexCache(Thread::Current(), *dex_file);
Vladimir Markof4da6752014-08-01 19:04:18 +0100694 }
695 return true;
696 }
697
Mathieu Chartier90443472015-07-16 20:32:27 -0700698 bool EndClass() SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100699 bool result = OatDexMethodVisitor::EndClass();
700 if (oat_class_index_ == writer_->oat_classes_.size()) {
701 DCHECK(result); // OatDexMethodVisitor::EndClass() never fails.
Vladimir Marko20f85592015-03-19 10:07:02 +0000702 offset_ = writer_->relative_patcher_->WriteThunks(out_, offset_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100703 if (UNLIKELY(offset_ == 0u)) {
704 PLOG(ERROR) << "Failed to write final relative call thunks";
705 result = false;
706 }
707 }
708 return result;
709 }
710
711 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
Mathieu Chartier90443472015-07-16 20:32:27 -0700712 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100713 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
714 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
715
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700716 // No thread suspension since dex_cache_ that may get invalidated if that occurs.
717 ScopedAssertNoThreadSuspension tsc(Thread::Current(), __FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700718 if (compiled_method != nullptr) { // ie. not an abstract method
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100719 size_t file_offset = file_offset_;
720 OutputStream* out = out_;
721
Vladimir Marko35831e82015-09-11 11:59:18 +0100722 ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode();
723 uint32_t code_size = quick_code.size() * sizeof(uint8_t);
Nicolas Geoffrayf0758792015-07-13 11:56:00 +0000724
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100725 // Deduplicate code arrays.
726 const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_];
727 if (method_offsets.code_offset_ > offset_) {
728 offset_ = writer_->relative_patcher_->WriteThunks(out, offset_);
729 if (offset_ == 0u) {
730 ReportWriteFailure("relative call thunk", it);
731 return false;
Nicolas Geoffrayf0758792015-07-13 11:56:00 +0000732 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100733 uint32_t aligned_offset = compiled_method->AlignCode(offset_);
734 uint32_t aligned_code_delta = aligned_offset - offset_;
735 if (aligned_code_delta != 0) {
736 if (!writer_->WriteCodeAlignment(out, aligned_code_delta)) {
737 ReportWriteFailure("code alignment padding", it);
738 return false;
739 }
740 offset_ += aligned_code_delta;
741 DCHECK_OFFSET_();
742 }
743 DCHECK_ALIGNED_PARAM(offset_,
744 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
745 DCHECK_EQ(method_offsets.code_offset_,
746 offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta())
747 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
748 const OatQuickMethodHeader& method_header =
749 oat_class->method_headers_[method_offsets_index_];
750 writer_->oat_header_->UpdateChecksum(&method_header, sizeof(method_header));
751 if (!out->WriteFully(&method_header, sizeof(method_header))) {
752 ReportWriteFailure("method header", it);
753 return false;
754 }
755 writer_->size_method_header_ += sizeof(method_header);
756 offset_ += sizeof(method_header);
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000757 DCHECK_OFFSET_();
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100758
759 if (!compiled_method->GetPatches().empty()) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100760 patched_code_.assign(quick_code.begin(), quick_code.end());
761 quick_code = ArrayRef<const uint8_t>(patched_code_);
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100762 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
763 if (patch.Type() == kLinkerPatchCallRelative) {
764 // NOTE: Relative calls across oat files are not supported.
765 uint32_t target_offset = GetTargetOffset(patch);
766 uint32_t literal_offset = patch.LiteralOffset();
767 writer_->relative_patcher_->PatchCall(&patched_code_, literal_offset,
768 offset_ + literal_offset, target_offset);
769 } else if (patch.Type() == kLinkerPatchDexCacheArray) {
770 uint32_t target_offset = GetDexCacheOffset(patch);
771 uint32_t literal_offset = patch.LiteralOffset();
772 writer_->relative_patcher_->PatchDexCacheReference(&patched_code_, patch,
773 offset_ + literal_offset,
774 target_offset);
775 } else if (patch.Type() == kLinkerPatchCall) {
776 uint32_t target_offset = GetTargetOffset(patch);
777 PatchCodeAddress(&patched_code_, patch.LiteralOffset(), target_offset);
778 } else if (patch.Type() == kLinkerPatchMethod) {
779 ArtMethod* method = GetTargetMethod(patch);
780 PatchMethodAddress(&patched_code_, patch.LiteralOffset(), method);
781 } else if (patch.Type() == kLinkerPatchType) {
782 mirror::Class* type = GetTargetType(patch);
783 PatchObjectAddress(&patched_code_, patch.LiteralOffset(), type);
784 }
785 }
786 }
787
Vladimir Marko35831e82015-09-11 11:59:18 +0100788 writer_->oat_header_->UpdateChecksum(quick_code.data(), code_size);
789 if (!out->WriteFully(quick_code.data(), code_size)) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100790 ReportWriteFailure("method code", it);
791 return false;
792 }
793 writer_->size_code_ += code_size;
794 offset_ += code_size;
Nicolas Geoffrayf0758792015-07-13 11:56:00 +0000795 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100796 DCHECK_OFFSET_();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100797 ++method_offsets_index_;
798 }
799
800 return true;
801 }
802
803 private:
804 OutputStream* const out_;
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100805 const size_t file_offset_;
806 const ScopedObjectAccess soa_;
807 const ScopedAssertNoThreadSuspension no_thread_suspension_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100808 ClassLinker* const class_linker_;
809 mirror::DexCache* dex_cache_;
810 std::vector<uint8_t> patched_code_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100811
812 void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) {
813 PLOG(ERROR) << "Failed to write " << what << " for "
814 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
815 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100816
Mathieu Chartiere401d142015-04-22 13:56:20 -0700817 ArtMethod* GetTargetMethod(const LinkerPatch& patch)
Mathieu Chartier90443472015-07-16 20:32:27 -0700818 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100819 MethodReference ref = patch.TargetMethod();
820 mirror::DexCache* dex_cache =
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700821 (dex_file_ == ref.dex_file) ? dex_cache_ : class_linker_->FindDexCache(
822 Thread::Current(), *ref.dex_file);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700823 ArtMethod* method = dex_cache->GetResolvedMethod(
824 ref.dex_method_index, class_linker_->GetImagePointerSize());
Vladimir Markof4da6752014-08-01 19:04:18 +0100825 CHECK(method != nullptr);
826 return method;
827 }
828
Mathieu Chartier90443472015-07-16 20:32:27 -0700829 uint32_t GetTargetOffset(const LinkerPatch& patch) SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markob163bb72015-03-31 21:49:49 +0100830 auto target_it = writer_->method_offset_map_.map.find(patch.TargetMethod());
Vladimir Markof4da6752014-08-01 19:04:18 +0100831 uint32_t target_offset =
Vladimir Markob163bb72015-03-31 21:49:49 +0100832 (target_it != writer_->method_offset_map_.map.end()) ? target_it->second : 0u;
Vladimir Markof4da6752014-08-01 19:04:18 +0100833 // If there's no compiled code, point to the correct trampoline.
834 if (UNLIKELY(target_offset == 0)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700835 ArtMethod* target = GetTargetMethod(patch);
Vladimir Markof4da6752014-08-01 19:04:18 +0100836 DCHECK(target != nullptr);
Jeff Haoa0acc2d2015-01-27 11:22:04 -0800837 size_t size = GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet());
838 const void* oat_code_offset = target->GetEntryPointFromQuickCompiledCodePtrSize(size);
839 if (oat_code_offset != 0) {
840 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(oat_code_offset));
841 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(oat_code_offset));
842 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(oat_code_offset));
843 target_offset = PointerToLowMemUInt32(oat_code_offset);
844 } else {
845 target_offset = target->IsNative()
846 ? writer_->oat_header_->GetQuickGenericJniTrampolineOffset()
847 : writer_->oat_header_->GetQuickToInterpreterBridgeOffset();
848 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100849 }
850 return target_offset;
851 }
852
853 mirror::Class* GetTargetType(const LinkerPatch& patch)
Mathieu Chartier90443472015-07-16 20:32:27 -0700854 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100855 mirror::DexCache* dex_cache = (dex_file_ == patch.TargetTypeDexFile())
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700856 ? dex_cache_ : class_linker_->FindDexCache(Thread::Current(), *patch.TargetTypeDexFile());
Vladimir Markof4da6752014-08-01 19:04:18 +0100857 mirror::Class* type = dex_cache->GetResolvedType(patch.TargetTypeIndex());
858 CHECK(type != nullptr);
859 return type;
860 }
861
Mathieu Chartier90443472015-07-16 20:32:27 -0700862 uint32_t GetDexCacheOffset(const LinkerPatch& patch) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800863 if (writer_->HasBootImage()) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100864 auto* element = writer_->image_writer_->GetDexCacheArrayElementImageAddress<const uint8_t*>(
Vladimir Marko20f85592015-03-19 10:07:02 +0000865 patch.TargetDexCacheDexFile(), patch.TargetDexCacheElementOffset());
866 const uint8_t* oat_data = writer_->image_writer_->GetOatFileBegin() + file_offset_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100867 return element - oat_data;
Vladimir Marko20f85592015-03-19 10:07:02 +0000868 } else {
Vladimir Marko09d09432015-09-08 13:47:48 +0100869 size_t start = writer_->dex_cache_arrays_offsets_.Get(patch.TargetDexCacheDexFile());
870 return start + patch.TargetDexCacheElementOffset();
Vladimir Marko20f85592015-03-19 10:07:02 +0000871 }
872 }
873
Vladimir Markof4da6752014-08-01 19:04:18 +0100874 void PatchObjectAddress(std::vector<uint8_t>* code, uint32_t offset, mirror::Object* object)
Mathieu Chartier90443472015-07-16 20:32:27 -0700875 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800876 if (writer_->HasBootImage()) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100877 object = writer_->image_writer_->GetImageAddress(object);
Vladimir Marko09d09432015-09-08 13:47:48 +0100878 } else {
879 // NOTE: We're using linker patches for app->boot references when the image can
880 // be relocated and therefore we need to emit .oat_patches. We're not using this
881 // for app->app references, so check that the object is in the image space.
882 DCHECK(Runtime::Current()->GetHeap()->FindSpaceFromObject(object, false)->IsImageSpace());
Vladimir Markof4da6752014-08-01 19:04:18 +0100883 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100884 // Note: We only patch targeting Objects in image which is in the low 4gb.
Vladimir Markof4da6752014-08-01 19:04:18 +0100885 uint32_t address = PointerToLowMemUInt32(object);
886 DCHECK_LE(offset + 4, code->size());
887 uint8_t* data = &(*code)[offset];
888 data[0] = address & 0xffu;
889 data[1] = (address >> 8) & 0xffu;
890 data[2] = (address >> 16) & 0xffu;
891 data[3] = (address >> 24) & 0xffu;
892 }
893
Mathieu Chartiere401d142015-04-22 13:56:20 -0700894 void PatchMethodAddress(std::vector<uint8_t>* code, uint32_t offset, ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700895 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800896 if (writer_->HasBootImage()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700897 method = writer_->image_writer_->GetImageMethodAddress(method);
Vladimir Marko09d09432015-09-08 13:47:48 +0100898 } else if (kIsDebugBuild) {
899 // NOTE: We're using linker patches for app->boot references when the image can
900 // be relocated and therefore we need to emit .oat_patches. We're not using this
901 // for app->app references, so check that the method is an image method.
902 gc::space::ImageSpace* image_space = Runtime::Current()->GetHeap()->GetImageSpace();
903 size_t method_offset = reinterpret_cast<const uint8_t*>(method) - image_space->Begin();
904 CHECK(image_space->GetImageHeader().GetMethodsSection().Contains(method_offset));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700905 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100906 // Note: We only patch targeting ArtMethods in image which is in the low 4gb.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700907 uint32_t address = PointerToLowMemUInt32(method);
908 DCHECK_LE(offset + 4, code->size());
909 uint8_t* data = &(*code)[offset];
910 data[0] = address & 0xffu;
911 data[1] = (address >> 8) & 0xffu;
912 data[2] = (address >> 16) & 0xffu;
913 data[3] = (address >> 24) & 0xffu;
914 }
915
Vladimir Markof4da6752014-08-01 19:04:18 +0100916 void PatchCodeAddress(std::vector<uint8_t>* code, uint32_t offset, uint32_t target_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700917 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko09d09432015-09-08 13:47:48 +0100918 uint32_t address = target_offset;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800919 if (writer_->HasBootImage()) {
Vladimir Marko09d09432015-09-08 13:47:48 +0100920 address = PointerToLowMemUInt32(writer_->image_writer_->GetOatFileBegin() +
921 writer_->oat_data_offset_ + target_offset);
922 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100923 DCHECK_LE(offset + 4, code->size());
924 uint8_t* data = &(*code)[offset];
925 data[0] = address & 0xffu;
926 data[1] = (address >> 8) & 0xffu;
927 data[2] = (address >> 16) & 0xffu;
928 data[3] = (address >> 24) & 0xffu;
929 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100930};
931
932template <typename DataAccess>
933class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor {
934 public:
935 WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800936 size_t relative_offset)
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100937 : OatDexMethodVisitor(writer, relative_offset),
938 out_(out),
939 file_offset_(file_offset) {
940 }
941
942 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
943 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
944 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
945
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700946 if (compiled_method != nullptr) { // ie. not an abstract method
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100947 size_t file_offset = file_offset_;
948 OutputStream* out = out_;
949
950 uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_);
951 ++method_offsets_index_;
952
953 // Write deduplicated map.
Vladimir Marko35831e82015-09-11 11:59:18 +0100954 ArrayRef<const uint8_t> map = DataAccess::GetData(compiled_method);
955 size_t map_size = map.size() * sizeof(map[0]);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100956 DCHECK((map_size == 0u && map_offset == 0u) ||
957 (map_size != 0u && map_offset != 0u && map_offset <= offset_))
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800958 << map_size << " " << map_offset << " " << offset_ << " "
959 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " for " << DataAccess::Name();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100960 if (map_size != 0u && map_offset == offset_) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100961 if (UNLIKELY(!out->WriteFully(&map[0], map_size))) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100962 ReportWriteFailure(it);
963 return false;
964 }
965 offset_ += map_size;
966 }
967 DCHECK_OFFSET_();
968 }
969
970 return true;
971 }
972
973 private:
974 OutputStream* const out_;
975 size_t const file_offset_;
976
977 void ReportWriteFailure(const ClassDataItemIterator& it) {
978 PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for "
979 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
980 }
981};
982
983// Visit all methods from all classes in all dex files with the specified visitor.
984bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) {
985 for (const DexFile* dex_file : *dex_files_) {
986 const size_t class_def_count = dex_file->NumClassDefs();
987 for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) {
988 if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) {
989 return false;
990 }
991 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700992 const uint8_t* class_data = dex_file->GetClassData(class_def);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700993 if (class_data != nullptr) { // ie not an empty class, such as a marker interface
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100994 ClassDataItemIterator it(*dex_file, class_data);
995 while (it.HasNextStaticField()) {
996 it.Next();
997 }
998 while (it.HasNextInstanceField()) {
999 it.Next();
1000 }
1001 size_t class_def_method_index = 0u;
1002 while (it.HasNextDirectMethod()) {
1003 if (!visitor->VisitMethod(class_def_method_index, it)) {
1004 return false;
1005 }
1006 ++class_def_method_index;
1007 it.Next();
1008 }
1009 while (it.HasNextVirtualMethod()) {
1010 if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) {
1011 return false;
1012 }
1013 ++class_def_method_index;
1014 it.Next();
1015 }
1016 }
1017 if (UNLIKELY(!visitor->EndClass())) {
1018 return false;
1019 }
1020 }
1021 }
1022 return true;
1023}
1024
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001025size_t OatWriter::InitOatHeader() {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001026 oat_header_ = OatHeader::Create(compiler_driver_->GetInstructionSet(),
1027 compiler_driver_->GetInstructionSetFeatures(),
1028 dex_files_,
1029 image_file_location_oat_checksum_,
1030 image_file_location_oat_begin_,
1031 key_value_store_);
1032
1033 return oat_header_->GetHeaderSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001034}
1035
1036size_t OatWriter::InitOatDexFiles(size_t offset) {
1037 // create the OatDexFiles
1038 for (size_t i = 0; i != dex_files_->size(); ++i) {
1039 const DexFile* dex_file = (*dex_files_)[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001040 CHECK(dex_file != nullptr);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001041 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001042 oat_dex_files_.push_back(oat_dex_file);
1043 offset += oat_dex_file->SizeOf();
1044 }
1045 return offset;
1046}
1047
Brian Carlstrom89521892011-12-07 22:05:07 -08001048size_t OatWriter::InitDexFiles(size_t offset) {
1049 // calculate the offsets within OatDexFiles to the DexFiles
1050 for (size_t i = 0; i != dex_files_->size(); ++i) {
1051 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001052 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -08001053 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001054 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -08001055
1056 // set offset in OatDexFile to DexFile
1057 oat_dex_files_[i]->dex_file_offset_ = offset;
1058
1059 const DexFile* dex_file = (*dex_files_)[i];
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001060
1061 // Initialize type lookup table
1062 oat_dex_files_[i]->lookup_table_ = dex_file->GetTypeLookupTable();
1063
Brian Carlstrom89521892011-12-07 22:05:07 -08001064 offset += dex_file->GetHeader().file_size_;
1065 }
1066 return offset;
1067}
1068
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001069size_t OatWriter::InitLookupTables(size_t offset) {
1070 for (OatDexFile* oat_dex_file : oat_dex_files_) {
1071 if (oat_dex_file->lookup_table_ != nullptr) {
1072 uint32_t aligned_offset = RoundUp(offset, 4);
1073 oat_dex_file->lookup_table_offset_ = aligned_offset;
1074 size_oat_lookup_table_alignment_ += aligned_offset - offset;
1075 offset = aligned_offset + oat_dex_file->lookup_table_->RawDataLength();
1076 } else {
1077 oat_dex_file->lookup_table_offset_ = 0;
1078 }
1079 }
1080 return offset;
1081}
1082
Brian Carlstrom389efb02012-01-11 12:06:26 -08001083size_t OatWriter::InitOatClasses(size_t offset) {
Brian Carlstrom389efb02012-01-11 12:06:26 -08001084 // calculate the offsets within OatDexFiles to OatClasses
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001085 InitOatClassesMethodVisitor visitor(this, offset);
1086 bool success = VisitDexMethods(&visitor);
1087 CHECK(success);
1088 offset = visitor.GetOffset();
Brian Carlstromba150c32013-08-27 17:31:03 -07001089
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001090 // Update oat_dex_files_.
1091 auto oat_class_it = oat_classes_.begin();
1092 for (OatDexFile* oat_dex_file : oat_dex_files_) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001093 for (uint32_t& method_offset : oat_dex_file->methods_offsets_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001094 DCHECK(oat_class_it != oat_classes_.end());
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001095 method_offset = (*oat_class_it)->offset_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001096 ++oat_class_it;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001097 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001098 oat_dex_file->UpdateChecksum(oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001099 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001100 CHECK(oat_class_it == oat_classes_.end());
1101
1102 return offset;
1103}
1104
1105size_t OatWriter::InitOatMaps(size_t offset) {
1106 #define VISIT(VisitorType) \
1107 do { \
1108 VisitorType visitor(this, offset); \
1109 bool success = VisitDexMethods(&visitor); \
1110 DCHECK(success); \
1111 offset = visitor.GetOffset(); \
1112 } while (false)
1113
1114 VISIT(InitMapMethodVisitor<GcMapDataAccess>);
1115 VISIT(InitMapMethodVisitor<MappingTableDataAccess>);
1116 VISIT(InitMapMethodVisitor<VmapTableDataAccess>);
1117
1118 #undef VISIT
1119
Brian Carlstrome24fa612011-09-29 00:53:55 -07001120 return offset;
1121}
1122
1123size_t OatWriter::InitOatCode(size_t offset) {
1124 // calculate the offsets within OatHeader to executable code
1125 size_t old_offset = offset;
Dave Allison50abf0a2014-06-23 13:19:59 -07001126 size_t adjusted_offset = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001127 // required to be on a new page boundary
1128 offset = RoundUp(offset, kPageSize);
1129 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001130 size_executable_offset_alignment_ = offset - old_offset;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001131 if (compiler_driver_->IsBootImage()) {
Alex Lighta59dd802014-07-02 16:28:08 -07001132 CHECK_EQ(image_patch_delta_, 0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001133 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001134
Ian Rogers848871b2013-08-05 10:56:33 -07001135 #define DO_TRAMPOLINE(field, fn_name) \
1136 offset = CompiledCode::AlignCode(offset, instruction_set); \
Dave Allison50abf0a2014-06-23 13:19:59 -07001137 adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \
1138 oat_header_->Set ## fn_name ## Offset(adjusted_offset); \
Ian Rogers848871b2013-08-05 10:56:33 -07001139 field.reset(compiler_driver_->Create ## fn_name()); \
1140 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001141
Ian Rogers848871b2013-08-05 10:56:33 -07001142 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Andreas Gampe2da88232014-02-27 12:26:20 -08001143 DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline);
Jeff Hao88474b42013-10-23 16:24:40 -07001144 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -07001145 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
1146 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001147
Ian Rogers848871b2013-08-05 10:56:33 -07001148 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001149 } else {
Ian Rogers848871b2013-08-05 10:56:33 -07001150 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
1151 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
1152 oat_header_->SetJniDlsymLookupOffset(0);
Andreas Gampe2da88232014-02-27 12:26:20 -08001153 oat_header_->SetQuickGenericJniTrampolineOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -07001154 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001155 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -07001156 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Alex Lighta59dd802014-07-02 16:28:08 -07001157 oat_header_->SetImagePatchDelta(image_patch_delta_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001158 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001159 return offset;
1160}
1161
1162size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001163 #define VISIT(VisitorType) \
1164 do { \
1165 VisitorType visitor(this, offset); \
1166 bool success = VisitDexMethods(&visitor); \
1167 DCHECK(success); \
1168 offset = visitor.GetOffset(); \
1169 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001170
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001171 VISIT(InitCodeMethodVisitor);
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001172 if (compiler_driver_->IsBootImage()) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001173 VISIT(InitImageMethodVisitor);
Ian Rogers0571d352011-11-03 19:51:38 -07001174 }
Logan Chien8b977d32012-02-21 19:14:55 +08001175
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001176 #undef VISIT
1177
Brian Carlstrome24fa612011-09-29 00:53:55 -07001178 return offset;
1179}
1180
David Srbeckybc90fd02015-04-22 19:40:27 +01001181bool OatWriter::WriteRodata(OutputStream* out) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001182 const off_t raw_file_offset = out->Seek(0, kSeekCurrent);
1183 if (raw_file_offset == (off_t) -1) {
1184 LOG(ERROR) << "Failed to get file offset in " << out->GetLocation();
1185 return false;
1186 }
1187 const size_t file_offset = static_cast<size_t>(raw_file_offset);
David Srbeckybc90fd02015-04-22 19:40:27 +01001188 oat_data_offset_ = file_offset;
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001189
Vladimir Markof4da6752014-08-01 19:04:18 +01001190 // Reserve space for header. It will be written last - after updating the checksum.
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001191 size_t header_size = oat_header_->GetHeaderSize();
Vladimir Markof4da6752014-08-01 19:04:18 +01001192 if (out->Seek(header_size, kSeekCurrent) == (off_t) -1) {
1193 PLOG(ERROR) << "Failed to reserve space for oat header in " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001194 return false;
1195 }
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001196 size_oat_header_ += sizeof(OatHeader);
1197 size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001198
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001199 if (!WriteTables(out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001200 LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001201 return false;
1202 }
1203
Vladimir Markof4da6752014-08-01 19:04:18 +01001204 off_t tables_end_offset = out->Seek(0, kSeekCurrent);
1205 if (tables_end_offset == (off_t) -1) {
1206 LOG(ERROR) << "Failed to seek to oat code position in " << out->GetLocation();
1207 return false;
1208 }
1209 size_t relative_offset = static_cast<size_t>(tables_end_offset) - file_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001210 relative_offset = WriteMaps(out, file_offset, relative_offset);
1211 if (relative_offset == 0) {
1212 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
1213 return false;
1214 }
1215
David Srbeckybc90fd02015-04-22 19:40:27 +01001216 // Write padding.
1217 off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent);
1218 relative_offset += size_executable_offset_alignment_;
1219 DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset());
1220 size_t expected_file_offset = file_offset + relative_offset;
1221 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
1222 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
1223 << " Expected: " << expected_file_offset << " File: " << out->GetLocation();
1224 return 0;
1225 }
1226 DCHECK_OFFSET();
1227
1228 return true;
1229}
1230
1231bool OatWriter::WriteCode(OutputStream* out) {
1232 size_t header_size = oat_header_->GetHeaderSize();
1233 const size_t file_offset = oat_data_offset_;
1234 size_t relative_offset = oat_header_->GetExecutableOffset();
1235 DCHECK_OFFSET();
1236
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001237 relative_offset = WriteCode(out, file_offset, relative_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001238 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001239 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001240 return false;
1241 }
1242
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001243 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
1244 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001245 LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001246 return false;
1247 }
1248
Vladimir Markof4da6752014-08-01 19:04:18 +01001249 const off_t oat_end_file_offset = out->Seek(0, kSeekCurrent);
1250 if (oat_end_file_offset == (off_t) -1) {
1251 LOG(ERROR) << "Failed to get oat end file offset in " << out->GetLocation();
1252 return false;
1253 }
1254
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001255 if (kIsDebugBuild) {
1256 uint32_t size_total = 0;
1257 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001258 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001259 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001260
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001261 DO_STAT(size_dex_file_alignment_);
1262 DO_STAT(size_executable_offset_alignment_);
1263 DO_STAT(size_oat_header_);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001264 DO_STAT(size_oat_header_key_value_store_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001265 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -07001266 DO_STAT(size_interpreter_to_interpreter_bridge_);
1267 DO_STAT(size_interpreter_to_compiled_code_bridge_);
1268 DO_STAT(size_jni_dlsym_lookup_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001269 DO_STAT(size_quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001270 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001271 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001272 DO_STAT(size_quick_to_interpreter_bridge_);
1273 DO_STAT(size_trampoline_alignment_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001274 DO_STAT(size_method_header_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001275 DO_STAT(size_code_);
1276 DO_STAT(size_code_alignment_);
Vladimir Markof4da6752014-08-01 19:04:18 +01001277 DO_STAT(size_relative_call_thunks_);
Vladimir Markoc74658b2015-03-31 10:26:41 +01001278 DO_STAT(size_misc_thunks_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001279 DO_STAT(size_mapping_table_);
1280 DO_STAT(size_vmap_table_);
1281 DO_STAT(size_gc_map_);
1282 DO_STAT(size_oat_dex_file_location_size_);
1283 DO_STAT(size_oat_dex_file_location_data_);
1284 DO_STAT(size_oat_dex_file_location_checksum_);
1285 DO_STAT(size_oat_dex_file_offset_);
1286 DO_STAT(size_oat_dex_file_methods_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001287 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001288 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001289 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001290 DO_STAT(size_oat_class_method_offsets_);
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001291 DO_STAT(size_oat_lookup_table_alignment_);
1292 DO_STAT(size_oat_lookup_table_offset_);
1293 DO_STAT(size_oat_lookup_table_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001294 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001295
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001296 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Vladimir Markof4da6752014-08-01 19:04:18 +01001297 CHECK_EQ(file_offset + size_total, static_cast<size_t>(oat_end_file_offset));
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001298 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001299 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001300
Vladimir Markof4da6752014-08-01 19:04:18 +01001301 CHECK_EQ(file_offset + size_, static_cast<size_t>(oat_end_file_offset));
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001302 CHECK_EQ(size_, relative_offset);
1303
Vladimir Markof4da6752014-08-01 19:04:18 +01001304 // Write the header now that the checksum is final.
1305 if (out->Seek(file_offset, kSeekSet) == (off_t) -1) {
1306 PLOG(ERROR) << "Failed to seek to oat header position in " << out->GetLocation();
1307 return false;
1308 }
David Srbeckybc90fd02015-04-22 19:40:27 +01001309 DCHECK_EQ(file_offset, static_cast<size_t>(out->Seek(0, kSeekCurrent)));
Vladimir Markof4da6752014-08-01 19:04:18 +01001310 if (!out->WriteFully(oat_header_, header_size)) {
1311 PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation();
1312 return false;
1313 }
1314 if (out->Seek(oat_end_file_offset, kSeekSet) == (off_t) -1) {
1315 PLOG(ERROR) << "Failed to seek to end after writing oat header to " << out->GetLocation();
1316 return false;
1317 }
1318 DCHECK_EQ(oat_end_file_offset, out->Seek(0, kSeekCurrent));
1319
Brian Carlstrome24fa612011-09-29 00:53:55 -07001320 return true;
1321}
1322
Ian Rogers3d504072014-03-01 09:16:49 -08001323bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001324 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001325 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001326 PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001327 return false;
1328 }
1329 }
Brian Carlstrom89521892011-12-07 22:05:07 -08001330 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001331 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Ian Rogers3d504072014-03-01 09:16:49 -08001332 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -08001333 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
1334 const DexFile* dex_file = (*dex_files_)[i];
1335 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
1336 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
1337 return false;
1338 }
1339 const DexFile* dex_file = (*dex_files_)[i];
Ian Rogers3d504072014-03-01 09:16:49 -08001340 if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001341 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
Ian Rogers3d504072014-03-01 09:16:49 -08001342 << " to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001343 return false;
1344 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001345 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -08001346 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001347 if (!WriteLookupTables(out, file_offset)) {
1348 return false;
1349 }
Brian Carlstrom389efb02012-01-11 12:06:26 -08001350 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001351 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001352 PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001353 return false;
1354 }
1355 }
1356 return true;
1357}
1358
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001359bool OatWriter::WriteLookupTables(OutputStream* out, const size_t file_offset) {
1360 for (size_t i = 0; i < oat_dex_files_.size(); ++i) {
1361 const uint32_t lookup_table_offset = oat_dex_files_[i]->lookup_table_offset_;
1362 const TypeLookupTable* table = oat_dex_files_[i]->lookup_table_;
1363 DCHECK_EQ(lookup_table_offset == 0, table == nullptr);
1364 if (lookup_table_offset == 0) {
1365 continue;
1366 }
1367 const uint32_t expected_offset = file_offset + lookup_table_offset;
1368 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
1369 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
1370 const DexFile* dex_file = (*dex_files_)[i];
1371 PLOG(ERROR) << "Failed to seek to lookup table section. Actual: " << actual_offset
1372 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
1373 return false;
1374 }
1375 if (table != nullptr) {
1376 if (!out->WriteFully(table->RawData(), table->RawDataLength())) {
1377 const DexFile* dex_file = (*dex_files_)[i];
1378 PLOG(ERROR) << "Failed to write lookup table for " << dex_file->GetLocation()
1379 << " to " << out->GetLocation();
1380 return false;
1381 }
1382 size_oat_lookup_table_ += table->RawDataLength();
1383 }
1384 }
1385 return true;
1386}
1387
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001388size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) {
1389 #define VISIT(VisitorType) \
1390 do { \
1391 VisitorType visitor(this, out, file_offset, relative_offset); \
1392 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1393 return 0; \
1394 } \
1395 relative_offset = visitor.GetOffset(); \
1396 } while (false)
1397
1398 size_t gc_maps_offset = relative_offset;
1399 VISIT(WriteMapMethodVisitor<GcMapDataAccess>);
1400 size_gc_map_ = relative_offset - gc_maps_offset;
1401
1402 size_t mapping_tables_offset = relative_offset;
1403 VISIT(WriteMapMethodVisitor<MappingTableDataAccess>);
1404 size_mapping_table_ = relative_offset - mapping_tables_offset;
1405
1406 size_t vmap_tables_offset = relative_offset;
1407 VISIT(WriteMapMethodVisitor<VmapTableDataAccess>);
1408 size_vmap_table_ = relative_offset - vmap_tables_offset;
1409
1410 #undef VISIT
1411
1412 return relative_offset;
1413}
1414
1415size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) {
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08001416 if (compiler_driver_->IsBootImage()) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001417 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001418
Ian Rogers848871b2013-08-05 10:56:33 -07001419 #define DO_TRAMPOLINE(field) \
1420 do { \
1421 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
1422 uint32_t alignment_padding = aligned_offset - relative_offset; \
Ian Rogers3d504072014-03-01 09:16:49 -08001423 out->Seek(alignment_padding, kSeekCurrent); \
Ian Rogers848871b2013-08-05 10:56:33 -07001424 size_trampoline_alignment_ += alignment_padding; \
Ian Rogers3d504072014-03-01 09:16:49 -08001425 if (!out->WriteFully(&(*field)[0], field->size())) { \
1426 PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \
Ian Rogers848871b2013-08-05 10:56:33 -07001427 return false; \
1428 } \
1429 size_ ## field += field->size(); \
1430 relative_offset += alignment_padding + field->size(); \
1431 DCHECK_OFFSET(); \
1432 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001433
Ian Rogers848871b2013-08-05 10:56:33 -07001434 DO_TRAMPOLINE(jni_dlsym_lookup_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001435 DO_TRAMPOLINE(quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001436 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001437 DO_TRAMPOLINE(quick_resolution_trampoline_);
1438 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
1439 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001440 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001441 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001442}
1443
Ian Rogers3d504072014-03-01 09:16:49 -08001444size_t OatWriter::WriteCodeDexFiles(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001445 const size_t file_offset,
1446 size_t relative_offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001447 #define VISIT(VisitorType) \
1448 do { \
1449 VisitorType visitor(this, out, file_offset, relative_offset); \
1450 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1451 return 0; \
1452 } \
1453 relative_offset = visitor.GetOffset(); \
1454 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001455
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001456 VISIT(WriteCodeMethodVisitor);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001457
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001458 #undef VISIT
Brian Carlstrom265091e2013-01-30 14:08:26 -08001459
Vladimir Markob163bb72015-03-31 21:49:49 +01001460 size_code_alignment_ += relative_patcher_->CodeAlignmentSize();
1461 size_relative_call_thunks_ += relative_patcher_->RelativeCallThunksSize();
1462 size_misc_thunks_ += relative_patcher_->MiscThunksSize();
1463
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001464 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001465}
1466
Vladimir Markof4da6752014-08-01 19:04:18 +01001467bool OatWriter::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) {
1468 static const uint8_t kPadding[] = {
1469 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
1470 };
1471 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
1472 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
1473 return false;
1474 }
1475 size_code_alignment_ += aligned_code_delta;
1476 return true;
1477}
1478
Vladimir Markob163bb72015-03-31 21:49:49 +01001479std::pair<bool, uint32_t> OatWriter::MethodOffsetMap::FindMethodOffset(MethodReference ref) {
1480 auto it = map.find(ref);
1481 if (it == map.end()) {
1482 return std::pair<bool, uint32_t>(false, 0u);
1483 } else {
1484 return std::pair<bool, uint32_t>(true, it->second);
1485 }
1486}
1487
Brian Carlstrom265091e2013-01-30 14:08:26 -08001488OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
1489 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -08001490 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001491 dex_file_location_size_ = location.size();
1492 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001493 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -08001494 dex_file_offset_ = 0;
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001495 lookup_table_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001496 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001497}
1498
1499size_t OatWriter::OatDexFile::SizeOf() const {
1500 return sizeof(dex_file_location_size_)
1501 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001502 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -08001503 + sizeof(dex_file_offset_)
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001504 + sizeof(lookup_table_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001505 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001506}
1507
Ian Rogers3d504072014-03-01 09:16:49 -08001508void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const {
1509 oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
1510 oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
1511 oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
1512 oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001513 oat_header->UpdateChecksum(&lookup_table_offset_, sizeof(lookup_table_offset_));
1514 if (lookup_table_ != nullptr) {
1515 oat_header->UpdateChecksum(lookup_table_->RawData(), lookup_table_->RawDataLength());
1516 }
Ian Rogers3d504072014-03-01 09:16:49 -08001517 oat_header->UpdateChecksum(&methods_offsets_[0],
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001518 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001519}
1520
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001521bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001522 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001523 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001524 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001525 if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
1526 PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001527 return false;
1528 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001529 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001530 if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
1531 PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001532 return false;
1533 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001534 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Ian Rogers3d504072014-03-01 09:16:49 -08001535 if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
1536 PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001537 return false;
1538 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001539 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Ian Rogers3d504072014-03-01 09:16:49 -08001540 if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
1541 PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001542 return false;
1543 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001544 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001545 if (!out->WriteFully(&lookup_table_offset_, sizeof(lookup_table_offset_))) {
1546 PLOG(ERROR) << "Failed to write lookup table offset to " << out->GetLocation();
1547 return false;
1548 }
1549 oat_writer->size_oat_lookup_table_offset_ += sizeof(lookup_table_offset_);
Ian Rogers3d504072014-03-01 09:16:49 -08001550 if (!out->WriteFully(&methods_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001551 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001552 PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001553 return false;
1554 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001555 oat_writer->size_oat_dex_file_methods_offsets_ +=
1556 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001557 return true;
1558}
1559
Brian Carlstromba150c32013-08-27 17:31:03 -07001560OatWriter::OatClass::OatClass(size_t offset,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001561 const std::vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -07001562 uint32_t num_non_null_compiled_methods,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001563 mirror::Class::Status status)
1564 : compiled_methods_(compiled_methods) {
1565 uint32_t num_methods = compiled_methods.size();
Brian Carlstromba150c32013-08-27 17:31:03 -07001566 CHECK_LE(num_non_null_compiled_methods, num_methods);
1567
Brian Carlstrom265091e2013-01-30 14:08:26 -08001568 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -07001569 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
1570
1571 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
1572 // apply when there are 0 methods, we just arbitrarily say that 0
1573 // methods means kOatClassNoneCompiled and that we won't use
1574 // kOatClassAllCompiled unless there is at least one compiled
1575 // method. This means in an interpretter only system, we can assert
1576 // that all classes are kOatClassNoneCompiled.
1577 if (num_non_null_compiled_methods == 0) {
1578 type_ = kOatClassNoneCompiled;
1579 } else if (num_non_null_compiled_methods == num_methods) {
1580 type_ = kOatClassAllCompiled;
1581 } else {
1582 type_ = kOatClassSomeCompiled;
1583 }
1584
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001585 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -07001586 method_offsets_.resize(num_non_null_compiled_methods);
Vladimir Marko8a630572014-04-09 18:45:35 +01001587 method_headers_.resize(num_non_null_compiled_methods);
Brian Carlstromba150c32013-08-27 17:31:03 -07001588
1589 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
1590 if (type_ == kOatClassSomeCompiled) {
1591 method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator());
1592 method_bitmap_size_ = method_bitmap_->GetSizeOf();
1593 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
1594 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
1595 } else {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001596 method_bitmap_ = nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001597 method_bitmap_size_ = 0;
1598 }
1599
1600 for (size_t i = 0; i < num_methods; i++) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001601 CompiledMethod* compiled_method = compiled_methods_[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001602 if (compiled_method == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001603 oat_method_offsets_offsets_from_oat_class_[i] = 0;
1604 } else {
1605 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
1606 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
1607 if (type_ == kOatClassSomeCompiled) {
1608 method_bitmap_->SetBit(i);
1609 }
1610 }
1611 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001612}
1613
Brian Carlstromba150c32013-08-27 17:31:03 -07001614OatWriter::OatClass::~OatClass() {
Mathieu Chartier661974a2014-01-09 11:23:53 -08001615 delete method_bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -07001616}
1617
Brian Carlstrom265091e2013-01-30 14:08:26 -08001618size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
1619 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001620 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
1621 if (method_offset == 0) {
1622 return 0;
1623 }
1624 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001625}
1626
1627size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
1628 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001629 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08001630}
1631
1632size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001633 return sizeof(status_)
1634 + sizeof(type_)
1635 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
1636 + method_bitmap_size_
1637 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001638}
1639
Ian Rogers3d504072014-03-01 09:16:49 -08001640void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const {
1641 oat_header->UpdateChecksum(&status_, sizeof(status_));
1642 oat_header->UpdateChecksum(&type_, sizeof(type_));
Brian Carlstromba150c32013-08-27 17:31:03 -07001643 if (method_bitmap_size_ != 0) {
1644 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001645 oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_));
1646 oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001647 }
Ian Rogers3d504072014-03-01 09:16:49 -08001648 oat_header->UpdateChecksum(&method_offsets_[0],
1649 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001650}
1651
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001652bool OatWriter::OatClass::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001653 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001654 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001655 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001656 if (!out->WriteFully(&status_, sizeof(status_))) {
1657 PLOG(ERROR) << "Failed to write class status to " << out->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001658 return false;
1659 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001660 oat_writer->size_oat_class_status_ += sizeof(status_);
Ian Rogers3d504072014-03-01 09:16:49 -08001661 if (!out->WriteFully(&type_, sizeof(type_))) {
1662 PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001663 return false;
1664 }
1665 oat_writer->size_oat_class_type_ += sizeof(type_);
1666 if (method_bitmap_size_ != 0) {
1667 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001668 if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
1669 PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001670 return false;
1671 }
1672 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001673 if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
1674 PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001675 return false;
1676 }
1677 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1678 }
Ian Rogers3d504072014-03-01 09:16:49 -08001679 if (!out->WriteFully(&method_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001680 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001681 PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001682 return false;
1683 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001684 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001685 return true;
1686}
1687
Brian Carlstrome24fa612011-09-29 00:53:55 -07001688} // namespace art