blob: 640698b0e99a242d6ea223d31aca00867b976c87 [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"
Vladimir Markof4da6752014-08-01 19:04:18 +010036#include "image_writer.h"
Vladimir Markob163bb72015-03-31 21:49:49 +010037#include "linker/relative_patcher.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/array.h"
39#include "mirror/class_loader.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010040#include "mirror/dex_cache-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070041#include "mirror/object-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010042#include "oat_quick_method_header.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070043#include "os.h"
Brian Carlstromcd60ac72013-01-20 17:09:51 -080044#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070045#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070046#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070047#include "handle_scope-inl.h"
Vladimir Marko09d09432015-09-08 13:47:48 +010048#include "utils/dex_cache_arrays_layout-inl.h"
jeffhaoec014232012-09-05 10:42:25 -070049#include "verifier/method_verifier.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070050
51namespace art {
52
Vladimir Marko96c6ab92014-04-08 14:00:50 +010053#define DCHECK_OFFSET() \
54 DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out->Seek(0, kSeekCurrent)) \
55 << "file_offset=" << file_offset << " relative_offset=" << relative_offset
56
57#define DCHECK_OFFSET_() \
58 DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out->Seek(0, kSeekCurrent)) \
59 << "file_offset=" << file_offset << " offset_=" << offset_
60
Brian Carlstrom3320cf42011-10-04 14:58:28 -070061OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070062 uint32_t image_file_location_oat_checksum,
Ian Rogersef7d42f2014-01-06 12:55:46 -080063 uintptr_t image_file_location_oat_begin,
Alex Lighta59dd802014-07-02 16:28:08 -070064 int32_t image_patch_delta,
Ian Rogersca368cb2013-11-15 15:52:08 -080065 const CompilerDriver* compiler,
Vladimir Markof4da6752014-08-01 19:04:18 +010066 ImageWriter* image_writer,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070067 TimingLogger* timings,
68 SafeMap<std::string, std::string>* key_value_store)
Jeff Hao0aba0ba2013-06-03 14:49:28 -070069 : compiler_driver_(compiler),
Vladimir Markof4da6752014-08-01 19:04:18 +010070 image_writer_(image_writer),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070071 dex_files_(&dex_files),
Vladimir Markof4da6752014-08-01 19:04:18 +010072 size_(0u),
Vladimir Marko5c42c292015-02-25 12:02:49 +000073 bss_size_(0u),
Vladimir Markof4da6752014-08-01 19:04:18 +010074 oat_data_offset_(0u),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070075 image_file_location_oat_checksum_(image_file_location_oat_checksum),
76 image_file_location_oat_begin_(image_file_location_oat_begin),
Alex Lighta59dd802014-07-02 16:28:08 -070077 image_patch_delta_(image_patch_delta),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070078 key_value_store_(key_value_store),
Mathieu Chartier2cebb242015-04-21 16:50:40 -070079 oat_header_(nullptr),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070080 size_dex_file_alignment_(0),
81 size_executable_offset_alignment_(0),
82 size_oat_header_(0),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070083 size_oat_header_key_value_store_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070084 size_dex_file_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070085 size_interpreter_to_interpreter_bridge_(0),
86 size_interpreter_to_compiled_code_bridge_(0),
87 size_jni_dlsym_lookup_(0),
Andreas Gampe2da88232014-02-27 12:26:20 -080088 size_quick_generic_jni_trampoline_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070089 size_quick_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070090 size_quick_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070091 size_quick_to_interpreter_bridge_(0),
92 size_trampoline_alignment_(0),
Vladimir Marko96c6ab92014-04-08 14:00:50 +010093 size_method_header_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070094 size_code_(0),
95 size_code_alignment_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +010096 size_relative_call_thunks_(0),
Vladimir Markoc74658b2015-03-31 10:26:41 +010097 size_misc_thunks_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070098 size_mapping_table_(0),
99 size_vmap_table_(0),
100 size_gc_map_(0),
101 size_oat_dex_file_location_size_(0),
102 size_oat_dex_file_location_data_(0),
103 size_oat_dex_file_location_checksum_(0),
104 size_oat_dex_file_offset_(0),
105 size_oat_dex_file_methods_offsets_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -0700106 size_oat_class_type_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700107 size_oat_class_status_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -0700108 size_oat_class_method_bitmaps_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +0100109 size_oat_class_method_offsets_(0),
110 method_offset_map_() {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700111 CHECK(key_value_store != nullptr);
112
Vladimir Markob163bb72015-03-31 21:49:49 +0100113 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
114 const InstructionSetFeatures* features = compiler_driver_->GetInstructionSetFeatures();
115 relative_patcher_ = linker::RelativePatcher::Create(instruction_set, features,
116 &method_offset_map_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100117
Ian Rogersca368cb2013-11-15 15:52:08 -0800118 size_t offset;
119 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700120 TimingLogger::ScopedTiming split("InitOatHeader", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800121 offset = InitOatHeader();
122 }
123 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700124 TimingLogger::ScopedTiming split("InitOatDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800125 offset = InitOatDexFiles(offset);
126 }
127 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700128 TimingLogger::ScopedTiming split("InitDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800129 offset = InitDexFiles(offset);
130 }
131 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700132 TimingLogger::ScopedTiming split("InitOatClasses", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800133 offset = InitOatClasses(offset);
134 }
135 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700136 TimingLogger::ScopedTiming split("InitOatMaps", timings);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100137 offset = InitOatMaps(offset);
138 }
139 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700140 TimingLogger::ScopedTiming split("InitOatCode", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800141 offset = InitOatCode(offset);
142 }
143 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700144 TimingLogger::ScopedTiming split("InitOatCodeDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800145 offset = InitOatCodeDexFiles(offset);
146 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700147 size_ = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700148
Vladimir Marko09d09432015-09-08 13:47:48 +0100149 if (!HasImage()) {
150 // Allocate space for app dex cache arrays in the .bss section.
151 size_t bss_start = RoundUp(size_, kPageSize);
152 size_t pointer_size = GetInstructionSetPointerSize(instruction_set);
153 bss_size_ = 0u;
154 for (const DexFile* dex_file : dex_files) {
155 dex_cache_arrays_offsets_.Put(dex_file, bss_start + bss_size_);
156 DexCacheArraysLayout layout(pointer_size, dex_file);
157 bss_size_ += layout.Size();
158 }
159 }
160
Brian Carlstrome24fa612011-09-29 00:53:55 -0700161 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Vladimir Markof4da6752014-08-01 19:04:18 +0100162 CHECK_EQ(compiler->IsImage(), image_writer_ != nullptr);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700163 CHECK_EQ(compiler->IsImage(),
164 key_value_store_->find(OatHeader::kImageLocationKey) == key_value_store_->end());
Alex Lighta59dd802014-07-02 16:28:08 -0700165 CHECK_ALIGNED(image_patch_delta_, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700166}
167
Ian Rogers0571d352011-11-03 19:51:38 -0700168OatWriter::~OatWriter() {
169 delete oat_header_;
170 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800171 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -0700172}
173
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100174struct OatWriter::GcMapDataAccess {
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800175 static const SwapVector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100176 return compiled_method->GetGcMap();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100177 }
178
179 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800180 uint32_t offset = oat_class->method_headers_[method_offsets_index].gc_map_offset_;
181 return offset == 0u ? 0u :
182 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100183 }
184
185 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
186 ALWAYS_INLINE {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800187 oat_class->method_headers_[method_offsets_index].gc_map_offset_ =
188 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100189 }
190
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800191 static const char* Name() {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100192 return "GC map";
193 }
194};
195
196struct OatWriter::MappingTableDataAccess {
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800197 static const SwapVector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000198 return compiled_method->GetMappingTable();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100199 }
200
201 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100202 uint32_t offset = oat_class->method_headers_[method_offsets_index].mapping_table_offset_;
203 return offset == 0u ? 0u :
204 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100205 }
206
207 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
208 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100209 oat_class->method_headers_[method_offsets_index].mapping_table_offset_ =
210 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100211 }
212
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800213 static const char* Name() {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100214 return "mapping table";
215 }
216};
217
218struct OatWriter::VmapTableDataAccess {
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800219 static const SwapVector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800220 return compiled_method->GetVmapTable();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100221 }
222
223 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100224 uint32_t offset = oat_class->method_headers_[method_offsets_index].vmap_table_offset_;
225 return offset == 0u ? 0u :
226 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100227 }
228
229 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
230 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100231 oat_class->method_headers_[method_offsets_index].vmap_table_offset_ =
232 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100233 }
234
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800235 static const char* Name() {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100236 return "vmap table";
237 }
238};
239
240class OatWriter::DexMethodVisitor {
241 public:
242 DexMethodVisitor(OatWriter* writer, size_t offset)
243 : writer_(writer),
244 offset_(offset),
245 dex_file_(nullptr),
246 class_def_index_(DexFile::kDexNoIndex) {
247 }
248
249 virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) {
250 DCHECK(dex_file_ == nullptr);
251 DCHECK_EQ(class_def_index_, DexFile::kDexNoIndex);
252 dex_file_ = dex_file;
253 class_def_index_ = class_def_index;
254 return true;
255 }
256
257 virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0;
258
259 virtual bool EndClass() {
260 if (kIsDebugBuild) {
261 dex_file_ = nullptr;
262 class_def_index_ = DexFile::kDexNoIndex;
263 }
264 return true;
265 }
266
267 size_t GetOffset() const {
268 return offset_;
269 }
270
271 protected:
272 virtual ~DexMethodVisitor() { }
273
274 OatWriter* const writer_;
275
276 // The offset is usually advanced for each visited method by the derived class.
277 size_t offset_;
278
279 // The dex file and class def index are set in StartClass().
280 const DexFile* dex_file_;
281 size_t class_def_index_;
282};
283
284class OatWriter::OatDexMethodVisitor : public DexMethodVisitor {
285 public:
286 OatDexMethodVisitor(OatWriter* writer, size_t offset)
287 : DexMethodVisitor(writer, offset),
288 oat_class_index_(0u),
289 method_offsets_index_(0u) {
290 }
291
292 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
293 DexMethodVisitor::StartClass(dex_file, class_def_index);
294 DCHECK_LT(oat_class_index_, writer_->oat_classes_.size());
295 method_offsets_index_ = 0u;
296 return true;
297 }
298
299 bool EndClass() {
300 ++oat_class_index_;
301 return DexMethodVisitor::EndClass();
302 }
303
304 protected:
305 size_t oat_class_index_;
306 size_t method_offsets_index_;
307};
308
309class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor {
310 public:
311 InitOatClassesMethodVisitor(OatWriter* writer, size_t offset)
312 : DexMethodVisitor(writer, offset),
313 compiled_methods_(),
314 num_non_null_compiled_methods_(0u) {
315 compiled_methods_.reserve(256u);
316 }
317
318 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
319 DexMethodVisitor::StartClass(dex_file, class_def_index);
320 compiled_methods_.clear();
321 num_non_null_compiled_methods_ = 0u;
322 return true;
323 }
324
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700325 bool VisitMethod(size_t class_def_method_index ATTRIBUTE_UNUSED, const ClassDataItemIterator& it) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100326 // Fill in the compiled_methods_ array for methods that have a
327 // CompiledMethod. We track the number of non-null entries in
328 // num_non_null_compiled_methods_ since we only want to allocate
329 // OatMethodOffsets for the compiled methods.
330 uint32_t method_idx = it.GetMemberIndex();
331 CompiledMethod* compiled_method =
332 writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx));
333 compiled_methods_.push_back(compiled_method);
334 if (compiled_method != nullptr) {
335 ++num_non_null_compiled_methods_;
336 }
337 return true;
338 }
339
340 bool EndClass() {
341 ClassReference class_ref(dex_file_, class_def_index_);
342 CompiledClass* compiled_class = writer_->compiler_driver_->GetCompiledClass(class_ref);
343 mirror::Class::Status status;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700344 if (compiled_class != nullptr) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100345 status = compiled_class->GetStatus();
346 } else if (writer_->compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) {
347 status = mirror::Class::kStatusError;
348 } else {
349 status = mirror::Class::kStatusNotReady;
350 }
351
352 OatClass* oat_class = new OatClass(offset_, compiled_methods_,
353 num_non_null_compiled_methods_, status);
354 writer_->oat_classes_.push_back(oat_class);
Vladimir Markof4da6752014-08-01 19:04:18 +0100355 oat_class->UpdateChecksum(writer_->oat_header_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100356 offset_ += oat_class->SizeOf();
357 return DexMethodVisitor::EndClass();
358 }
359
360 private:
361 std::vector<CompiledMethod*> compiled_methods_;
362 size_t num_non_null_compiled_methods_;
363};
364
365class OatWriter::InitCodeMethodVisitor : public OatDexMethodVisitor {
366 public:
367 InitCodeMethodVisitor(OatWriter* writer, size_t offset)
David Srbecky009e2a62015-04-15 02:46:30 +0100368 : OatDexMethodVisitor(writer, offset),
David Srbecky009e2a62015-04-15 02:46:30 +0100369 debuggable_(writer->GetCompilerDriver()->GetCompilerOptions().GetDebuggable()) {
David Srbeckyf8980872015-05-22 17:04:47 +0100370 writer_->absolute_patch_locations_.reserve(
Vladimir Markof4da6752014-08-01 19:04:18 +0100371 writer_->compiler_driver_->GetNonRelativeLinkerPatchCount());
372 }
373
374 bool EndClass() {
375 OatDexMethodVisitor::EndClass();
376 if (oat_class_index_ == writer_->oat_classes_.size()) {
Vladimir Marko71b0ddf2015-04-02 19:45:06 +0100377 offset_ = writer_->relative_patcher_->ReserveSpaceEnd(offset_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100378 }
379 return true;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100380 }
381
382 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
Mathieu Chartier90443472015-07-16 20:32:27 -0700383 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100384 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
385 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
386
387 if (compiled_method != nullptr) {
388 // Derived from CompiledMethod.
389 uint32_t quick_code_offset = 0;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100390
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800391 const SwapVector<uint8_t>* quick_code = compiled_method->GetQuickCode();
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000392 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800393 uint32_t thumb_offset = compiled_method->CodeDelta();
Elliott Hughes956af0f2014-12-11 14:34:28 -0800394
David Srbecky009e2a62015-04-15 02:46:30 +0100395 // Deduplicate code arrays if we are not producing debuggable code.
Vladimir Markoc74658b2015-03-31 10:26:41 +0100396 bool deduped = false;
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000397 if (debuggable_) {
398 quick_code_offset = NewQuickCodeOffset(compiled_method, it, thumb_offset);
399 } else {
400 auto lb = dedupe_map_.lower_bound(compiled_method);
401 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(compiled_method, lb->first)) {
402 quick_code_offset = lb->second;
403 deduped = true;
Nicolas Geoffrayf0758792015-07-13 11:56:00 +0000404 } else {
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000405 quick_code_offset = NewQuickCodeOffset(compiled_method, it, thumb_offset);
406 dedupe_map_.PutBefore(lb, compiled_method, quick_code_offset);
David Srbecky009e2a62015-04-15 02:46:30 +0100407 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800408 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100409
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100410 if (code_size != 0) {
411 MethodReference method_ref(dex_file_, it.GetMemberIndex());
412 auto method_lb = writer_->method_offset_map_.map.lower_bound(method_ref);
413 if (method_lb != writer_->method_offset_map_.map.end() &&
414 !writer_->method_offset_map_.map.key_comp()(method_ref, method_lb->first)) {
415 // TODO: Should this be a hard failure?
416 LOG(WARNING) << "Multiple definitions of "
417 << PrettyMethod(method_ref.dex_method_index, *method_ref.dex_file)
418 << ((method_lb->second != quick_code_offset) ? "; OFFSET MISMATCH" : "");
419 } else {
420 writer_->method_offset_map_.map.PutBefore(method_lb, method_ref, quick_code_offset);
421 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800422 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100423
Elliott Hughes956af0f2014-12-11 14:34:28 -0800424 // Update quick method header.
425 DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size());
426 OatQuickMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_];
427 uint32_t mapping_table_offset = method_header->mapping_table_offset_;
428 uint32_t vmap_table_offset = method_header->vmap_table_offset_;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100429 // If we don't have quick code, then we must have a vmap, as that is how the dex2dex
430 // compiler records its transformations.
431 DCHECK(quick_code != nullptr || vmap_table_offset != 0);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800432 uint32_t gc_map_offset = method_header->gc_map_offset_;
433 // The code offset was 0 when the mapping/vmap table offset was set, so it's set
434 // to 0-offset and we need to adjust it by code_offset.
435 uint32_t code_offset = quick_code_offset - thumb_offset;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100436 if (mapping_table_offset != 0u && code_offset != 0u) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800437 mapping_table_offset += code_offset;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100438 DCHECK_LT(mapping_table_offset, code_offset) << "Overflow in oat offsets";
Elliott Hughes956af0f2014-12-11 14:34:28 -0800439 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100440 if (vmap_table_offset != 0u && code_offset != 0u) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800441 vmap_table_offset += code_offset;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100442 DCHECK_LT(vmap_table_offset, code_offset) << "Overflow in oat offsets";
Elliott Hughes956af0f2014-12-11 14:34:28 -0800443 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100444 if (gc_map_offset != 0u && code_offset != 0u) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800445 gc_map_offset += code_offset;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100446 DCHECK_LT(gc_map_offset, code_offset) << "Overflow in oat offsets";
Elliott Hughes956af0f2014-12-11 14:34:28 -0800447 }
448 uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
449 uint32_t core_spill_mask = compiled_method->GetCoreSpillMask();
450 uint32_t fp_spill_mask = compiled_method->GetFpSpillMask();
451 *method_header = OatQuickMethodHeader(mapping_table_offset, vmap_table_offset,
452 gc_map_offset, frame_size_in_bytes, core_spill_mask,
453 fp_spill_mask, code_size);
Vladimir Marko7624d252014-05-02 14:40:15 +0100454
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000455 if (!deduped) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800456 // Update offsets. (Checksum is updated when writing.)
457 offset_ += sizeof(*method_header); // Method header is prepended before code.
458 offset_ += code_size;
459 // Record absolute patch locations.
460 if (!compiled_method->GetPatches().empty()) {
461 uintptr_t base_loc = offset_ - code_size - writer_->oat_header_->GetExecutableOffset();
462 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
Vladimir Marko20f85592015-03-19 10:07:02 +0000463 if (!patch.IsPcRelative()) {
David Srbeckyf8980872015-05-22 17:04:47 +0100464 writer_->absolute_patch_locations_.push_back(base_loc + patch.LiteralOffset());
Vladimir Markof4da6752014-08-01 19:04:18 +0100465 }
466 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100467 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800468 }
Alex Light78382fa2014-06-06 15:45:32 -0700469
David Srbecky8363c772015-05-28 16:12:43 +0100470 if (writer_->compiler_driver_->GetCompilerOptions().GetGenerateDebugInfo()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800471 // Record debug information for this function if we are doing that.
Elliott Hughes956af0f2014-12-11 14:34:28 -0800472 const uint32_t quick_code_start = quick_code_offset -
David Srbecky6f715892015-03-30 14:21:42 +0100473 writer_->oat_header_->GetExecutableOffset() - thumb_offset;
David Srbecky0df9e1f2015-04-07 19:02:58 +0100474 writer_->method_info_.push_back(DebugInfo {
475 dex_file_,
476 class_def_index_,
477 it.GetMemberIndex(),
478 it.GetMethodAccessFlags(),
479 it.GetMethodCodeItem(),
480 deduped,
481 quick_code_start,
482 quick_code_start + code_size,
483 compiled_method});
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100484 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100485
486 if (kIsDebugBuild) {
487 // We expect GC maps except when the class hasn't been verified or the method is native.
488 const CompilerDriver* compiler_driver = writer_->compiler_driver_;
489 ClassReference class_ref(dex_file_, class_def_index_);
490 CompiledClass* compiled_class = compiler_driver->GetCompiledClass(class_ref);
491 mirror::Class::Status status;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700492 if (compiled_class != nullptr) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100493 status = compiled_class->GetStatus();
494 } else if (compiler_driver->GetVerificationResults()->IsClassRejected(class_ref)) {
495 status = mirror::Class::kStatusError;
496 } else {
497 status = mirror::Class::kStatusNotReady;
498 }
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800499 const SwapVector<uint8_t>* gc_map = compiled_method->GetGcMap();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100500 if (gc_map != nullptr) {
501 size_t gc_map_size = gc_map->size() * sizeof(gc_map[0]);
Andreas Gampe51829322014-08-25 15:05:04 -0700502 bool is_native = it.MemberIsNative();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100503 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
504 << gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
505 << (status < mirror::Class::kStatusVerified) << " " << status << " "
506 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
507 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100508 }
509
510 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
511 OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_];
512 offsets->code_offset_ = quick_code_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100513 ++method_offsets_index_;
514 }
515
516 return true;
517 }
518
519 private:
Vladimir Marko20f85592015-03-19 10:07:02 +0000520 struct CodeOffsetsKeyComparator {
521 bool operator()(const CompiledMethod* lhs, const CompiledMethod* rhs) const {
522 if (lhs->GetQuickCode() != rhs->GetQuickCode()) {
523 return lhs->GetQuickCode() < rhs->GetQuickCode();
524 }
525 // If the code is the same, all other fields are likely to be the same as well.
526 if (UNLIKELY(lhs->GetMappingTable() != rhs->GetMappingTable())) {
527 return lhs->GetMappingTable() < rhs->GetMappingTable();
528 }
529 if (UNLIKELY(lhs->GetVmapTable() != rhs->GetVmapTable())) {
530 return lhs->GetVmapTable() < rhs->GetVmapTable();
531 }
532 if (UNLIKELY(lhs->GetGcMap() != rhs->GetGcMap())) {
533 return lhs->GetGcMap() < rhs->GetGcMap();
534 }
535 const auto& lhs_patches = lhs->GetPatches();
536 const auto& rhs_patches = rhs->GetPatches();
537 if (UNLIKELY(lhs_patches.size() != rhs_patches.size())) {
538 return lhs_patches.size() < rhs_patches.size();
539 }
540 auto rit = rhs_patches.begin();
541 for (const LinkerPatch& lpatch : lhs_patches) {
542 if (UNLIKELY(!(lpatch == *rit))) {
543 return lpatch < *rit;
544 }
545 ++rit;
546 }
547 return false;
548 }
549 };
550
David Srbecky009e2a62015-04-15 02:46:30 +0100551 uint32_t NewQuickCodeOffset(CompiledMethod* compiled_method,
552 const ClassDataItemIterator& it,
553 uint32_t thumb_offset) {
554 offset_ = writer_->relative_patcher_->ReserveSpace(
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100555 offset_, compiled_method, MethodReference(dex_file_, it.GetMemberIndex()));
David Srbecky009e2a62015-04-15 02:46:30 +0100556 offset_ = compiled_method->AlignCode(offset_);
557 DCHECK_ALIGNED_PARAM(offset_,
558 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
559 return offset_ + sizeof(OatQuickMethodHeader) + thumb_offset;
560 }
561
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100562 // Deduplication is already done on a pointer basis by the compiler driver,
563 // so we can simply compare the pointers to find out if things are duplicated.
Vladimir Marko8a630572014-04-09 18:45:35 +0100564 SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_;
David Srbecky2f6cdb02015-04-11 00:17:53 +0100565
David Srbecky009e2a62015-04-15 02:46:30 +0100566 // Cache of compiler's --debuggable option.
567 const bool debuggable_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100568};
569
570template <typename DataAccess>
571class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor {
572 public:
573 InitMapMethodVisitor(OatWriter* writer, size_t offset)
574 : OatDexMethodVisitor(writer, offset) {
575 }
576
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700577 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it ATTRIBUTE_UNUSED)
Mathieu Chartier90443472015-07-16 20:32:27 -0700578 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100579 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
580 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
581
582 if (compiled_method != nullptr) {
583 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
584 DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u);
585
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800586 const SwapVector<uint8_t>* map = DataAccess::GetData(compiled_method);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100587 uint32_t map_size = map == nullptr ? 0 : map->size() * sizeof((*map)[0]);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100588 if (map_size != 0u) {
Vladimir Markobd72fc12014-07-09 16:06:40 +0100589 auto lb = dedupe_map_.lower_bound(map);
590 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(map, lb->first)) {
591 DataAccess::SetOffset(oat_class, method_offsets_index_, lb->second);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100592 } else {
593 DataAccess::SetOffset(oat_class, method_offsets_index_, offset_);
Vladimir Markobd72fc12014-07-09 16:06:40 +0100594 dedupe_map_.PutBefore(lb, map, offset_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100595 offset_ += map_size;
596 writer_->oat_header_->UpdateChecksum(&(*map)[0], map_size);
597 }
598 }
599 ++method_offsets_index_;
600 }
601
602 return true;
603 }
604
605 private:
606 // Deduplication is already done on a pointer basis by the compiler driver,
607 // so we can simply compare the pointers to find out if things are duplicated.
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800608 SafeMap<const SwapVector<uint8_t>*, uint32_t> dedupe_map_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100609};
610
611class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor {
612 public:
613 InitImageMethodVisitor(OatWriter* writer, size_t offset)
Jeff Haoc7d11882015-02-03 15:08:39 -0800614 : OatDexMethodVisitor(writer, offset),
615 pointer_size_(GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet())) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100616 }
617
618 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
Mathieu Chartier90443472015-07-16 20:32:27 -0700619 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100620 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
621 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
622
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800623 OatMethodOffsets offsets(0u);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100624 if (compiled_method != nullptr) {
625 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
626 offsets = oat_class->method_offsets_[method_offsets_index_];
627 ++method_offsets_index_;
628 }
629
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100630 ClassLinker* linker = Runtime::Current()->GetClassLinker();
631 InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_));
632 // Unchecked as we hold mutator_lock_ on entry.
633 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800634 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700635 Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache(
636 Thread::Current(), *dex_file_)));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700637 ArtMethod* method = linker->ResolveMethod(
638 *dex_file_, it.GetMemberIndex(), dex_cache, NullHandle<mirror::ClassLoader>(), nullptr,
639 invoke_type);
Andreas Gamped9efea62014-07-21 22:56:08 -0700640 if (method == nullptr) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100641 LOG(INTERNAL_FATAL) << "Unexpected failure to resolve a method: "
642 << PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
Andreas Gamped9efea62014-07-21 22:56:08 -0700643 soa.Self()->AssertPendingException();
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000644 mirror::Throwable* exc = soa.Self()->GetException();
Andreas Gamped9efea62014-07-21 22:56:08 -0700645 std::string dump = exc->Dump();
646 LOG(FATAL) << dump;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100647 UNREACHABLE();
Andreas Gamped9efea62014-07-21 22:56:08 -0700648 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100649
650 if (compiled_method != nullptr && compiled_method->GetQuickCode()->size() != 0) {
651 method->SetEntryPointFromQuickCompiledCodePtrSize(
652 reinterpret_cast<void*>(offsets.code_offset_), pointer_size_);
653 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100654
655 return true;
656 }
Jeff Haoc7d11882015-02-03 15:08:39 -0800657
658 protected:
659 const size_t pointer_size_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100660};
661
662class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor {
663 public:
664 WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
Vladimir Markof4da6752014-08-01 19:04:18 +0100665 size_t relative_offset) SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100666 : OatDexMethodVisitor(writer, relative_offset),
667 out_(out),
Vladimir Markof4da6752014-08-01 19:04:18 +0100668 file_offset_(file_offset),
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100669 soa_(Thread::Current()),
670 no_thread_suspension_(soa_.Self(), "OatWriter patching"),
Vladimir Markof4da6752014-08-01 19:04:18 +0100671 class_linker_(Runtime::Current()->GetClassLinker()),
672 dex_cache_(nullptr) {
Vladimir Marko09d09432015-09-08 13:47:48 +0100673 patched_code_.reserve(16 * KB);
674 if (writer_->HasImage()) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100675 // If we're creating the image, the address space must be ready so that we can apply patches.
676 CHECK(writer_->image_writer_->IsImageAddressSpaceReady());
Vladimir Markof4da6752014-08-01 19:04:18 +0100677 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100678 }
679
Vladimir Markof4da6752014-08-01 19:04:18 +0100680 ~WriteCodeMethodVisitor() UNLOCK_FUNCTION(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100681 }
682
683 bool StartClass(const DexFile* dex_file, size_t class_def_index)
Mathieu Chartier90443472015-07-16 20:32:27 -0700684 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100685 OatDexMethodVisitor::StartClass(dex_file, class_def_index);
686 if (dex_cache_ == nullptr || dex_cache_->GetDexFile() != dex_file) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700687 dex_cache_ = class_linker_->FindDexCache(Thread::Current(), *dex_file);
Vladimir Markof4da6752014-08-01 19:04:18 +0100688 }
689 return true;
690 }
691
Mathieu Chartier90443472015-07-16 20:32:27 -0700692 bool EndClass() SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100693 bool result = OatDexMethodVisitor::EndClass();
694 if (oat_class_index_ == writer_->oat_classes_.size()) {
695 DCHECK(result); // OatDexMethodVisitor::EndClass() never fails.
Vladimir Marko20f85592015-03-19 10:07:02 +0000696 offset_ = writer_->relative_patcher_->WriteThunks(out_, offset_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100697 if (UNLIKELY(offset_ == 0u)) {
698 PLOG(ERROR) << "Failed to write final relative call thunks";
699 result = false;
700 }
701 }
702 return result;
703 }
704
705 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
Mathieu Chartier90443472015-07-16 20:32:27 -0700706 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100707 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
708 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
709
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700710 // No thread suspension since dex_cache_ that may get invalidated if that occurs.
711 ScopedAssertNoThreadSuspension tsc(Thread::Current(), __FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700712 if (compiled_method != nullptr) { // ie. not an abstract method
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100713 size_t file_offset = file_offset_;
714 OutputStream* out = out_;
715
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800716 const SwapVector<uint8_t>* quick_code = compiled_method->GetQuickCode();
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100717 // Need a wrapper if we create a copy for patching.
718 ArrayRef<const uint8_t> wrapped(*quick_code);
719 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
Nicolas Geoffrayf0758792015-07-13 11:56:00 +0000720
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100721 // Deduplicate code arrays.
722 const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_];
723 if (method_offsets.code_offset_ > offset_) {
724 offset_ = writer_->relative_patcher_->WriteThunks(out, offset_);
725 if (offset_ == 0u) {
726 ReportWriteFailure("relative call thunk", it);
727 return false;
Nicolas Geoffrayf0758792015-07-13 11:56:00 +0000728 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100729 uint32_t aligned_offset = compiled_method->AlignCode(offset_);
730 uint32_t aligned_code_delta = aligned_offset - offset_;
731 if (aligned_code_delta != 0) {
732 if (!writer_->WriteCodeAlignment(out, aligned_code_delta)) {
733 ReportWriteFailure("code alignment padding", it);
734 return false;
735 }
736 offset_ += aligned_code_delta;
737 DCHECK_OFFSET_();
738 }
739 DCHECK_ALIGNED_PARAM(offset_,
740 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
741 DCHECK_EQ(method_offsets.code_offset_,
742 offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta())
743 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
744 const OatQuickMethodHeader& method_header =
745 oat_class->method_headers_[method_offsets_index_];
746 writer_->oat_header_->UpdateChecksum(&method_header, sizeof(method_header));
747 if (!out->WriteFully(&method_header, sizeof(method_header))) {
748 ReportWriteFailure("method header", it);
749 return false;
750 }
751 writer_->size_method_header_ += sizeof(method_header);
752 offset_ += sizeof(method_header);
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000753 DCHECK_OFFSET_();
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100754
755 if (!compiled_method->GetPatches().empty()) {
756 patched_code_.assign(quick_code->begin(), quick_code->end());
757 wrapped = ArrayRef<const uint8_t>(patched_code_);
758 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
759 if (patch.Type() == kLinkerPatchCallRelative) {
760 // NOTE: Relative calls across oat files are not supported.
761 uint32_t target_offset = GetTargetOffset(patch);
762 uint32_t literal_offset = patch.LiteralOffset();
763 writer_->relative_patcher_->PatchCall(&patched_code_, literal_offset,
764 offset_ + literal_offset, target_offset);
765 } else if (patch.Type() == kLinkerPatchDexCacheArray) {
766 uint32_t target_offset = GetDexCacheOffset(patch);
767 uint32_t literal_offset = patch.LiteralOffset();
768 writer_->relative_patcher_->PatchDexCacheReference(&patched_code_, patch,
769 offset_ + literal_offset,
770 target_offset);
771 } else if (patch.Type() == kLinkerPatchCall) {
772 uint32_t target_offset = GetTargetOffset(patch);
773 PatchCodeAddress(&patched_code_, patch.LiteralOffset(), target_offset);
774 } else if (patch.Type() == kLinkerPatchMethod) {
775 ArtMethod* method = GetTargetMethod(patch);
776 PatchMethodAddress(&patched_code_, patch.LiteralOffset(), method);
777 } else if (patch.Type() == kLinkerPatchType) {
778 mirror::Class* type = GetTargetType(patch);
779 PatchObjectAddress(&patched_code_, patch.LiteralOffset(), type);
780 }
781 }
782 }
783
784 writer_->oat_header_->UpdateChecksum(wrapped.data(), code_size);
785 if (!out->WriteFully(wrapped.data(), code_size)) {
786 ReportWriteFailure("method code", it);
787 return false;
788 }
789 writer_->size_code_ += code_size;
790 offset_ += code_size;
Nicolas Geoffrayf0758792015-07-13 11:56:00 +0000791 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100792 DCHECK_OFFSET_();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100793 ++method_offsets_index_;
794 }
795
796 return true;
797 }
798
799 private:
800 OutputStream* const out_;
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100801 const size_t file_offset_;
802 const ScopedObjectAccess soa_;
803 const ScopedAssertNoThreadSuspension no_thread_suspension_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100804 ClassLinker* const class_linker_;
805 mirror::DexCache* dex_cache_;
806 std::vector<uint8_t> patched_code_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100807
808 void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) {
809 PLOG(ERROR) << "Failed to write " << what << " for "
810 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
811 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100812
Mathieu Chartiere401d142015-04-22 13:56:20 -0700813 ArtMethod* GetTargetMethod(const LinkerPatch& patch)
Mathieu Chartier90443472015-07-16 20:32:27 -0700814 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100815 MethodReference ref = patch.TargetMethod();
816 mirror::DexCache* dex_cache =
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700817 (dex_file_ == ref.dex_file) ? dex_cache_ : class_linker_->FindDexCache(
818 Thread::Current(), *ref.dex_file);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700819 ArtMethod* method = dex_cache->GetResolvedMethod(
820 ref.dex_method_index, class_linker_->GetImagePointerSize());
Vladimir Markof4da6752014-08-01 19:04:18 +0100821 CHECK(method != nullptr);
822 return method;
823 }
824
Mathieu Chartier90443472015-07-16 20:32:27 -0700825 uint32_t GetTargetOffset(const LinkerPatch& patch) SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markob163bb72015-03-31 21:49:49 +0100826 auto target_it = writer_->method_offset_map_.map.find(patch.TargetMethod());
Vladimir Markof4da6752014-08-01 19:04:18 +0100827 uint32_t target_offset =
Vladimir Markob163bb72015-03-31 21:49:49 +0100828 (target_it != writer_->method_offset_map_.map.end()) ? target_it->second : 0u;
Vladimir Markof4da6752014-08-01 19:04:18 +0100829 // If there's no compiled code, point to the correct trampoline.
830 if (UNLIKELY(target_offset == 0)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700831 ArtMethod* target = GetTargetMethod(patch);
Vladimir Markof4da6752014-08-01 19:04:18 +0100832 DCHECK(target != nullptr);
Jeff Haoa0acc2d2015-01-27 11:22:04 -0800833 size_t size = GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet());
834 const void* oat_code_offset = target->GetEntryPointFromQuickCompiledCodePtrSize(size);
835 if (oat_code_offset != 0) {
836 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(oat_code_offset));
837 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(oat_code_offset));
838 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(oat_code_offset));
839 target_offset = PointerToLowMemUInt32(oat_code_offset);
840 } else {
841 target_offset = target->IsNative()
842 ? writer_->oat_header_->GetQuickGenericJniTrampolineOffset()
843 : writer_->oat_header_->GetQuickToInterpreterBridgeOffset();
844 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100845 }
846 return target_offset;
847 }
848
849 mirror::Class* GetTargetType(const LinkerPatch& patch)
Mathieu Chartier90443472015-07-16 20:32:27 -0700850 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100851 mirror::DexCache* dex_cache = (dex_file_ == patch.TargetTypeDexFile())
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700852 ? dex_cache_ : class_linker_->FindDexCache(Thread::Current(), *patch.TargetTypeDexFile());
Vladimir Markof4da6752014-08-01 19:04:18 +0100853 mirror::Class* type = dex_cache->GetResolvedType(patch.TargetTypeIndex());
854 CHECK(type != nullptr);
855 return type;
856 }
857
Mathieu Chartier90443472015-07-16 20:32:27 -0700858 uint32_t GetDexCacheOffset(const LinkerPatch& patch) SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko09d09432015-09-08 13:47:48 +0100859 if (writer_->HasImage()) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100860 auto* element = writer_->image_writer_->GetDexCacheArrayElementImageAddress<const uint8_t*>(
Vladimir Marko20f85592015-03-19 10:07:02 +0000861 patch.TargetDexCacheDexFile(), patch.TargetDexCacheElementOffset());
862 const uint8_t* oat_data = writer_->image_writer_->GetOatFileBegin() + file_offset_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100863 return element - oat_data;
Vladimir Marko20f85592015-03-19 10:07:02 +0000864 } else {
Vladimir Marko09d09432015-09-08 13:47:48 +0100865 size_t start = writer_->dex_cache_arrays_offsets_.Get(patch.TargetDexCacheDexFile());
866 return start + patch.TargetDexCacheElementOffset();
Vladimir Marko20f85592015-03-19 10:07:02 +0000867 }
868 }
869
Vladimir Markof4da6752014-08-01 19:04:18 +0100870 void PatchObjectAddress(std::vector<uint8_t>* code, uint32_t offset, mirror::Object* object)
Mathieu Chartier90443472015-07-16 20:32:27 -0700871 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko09d09432015-09-08 13:47:48 +0100872 if (writer_->HasImage()) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100873 object = writer_->image_writer_->GetImageAddress(object);
Vladimir Marko09d09432015-09-08 13:47:48 +0100874 } else {
875 // NOTE: We're using linker patches for app->boot references when the image can
876 // be relocated and therefore we need to emit .oat_patches. We're not using this
877 // for app->app references, so check that the object is in the image space.
878 DCHECK(Runtime::Current()->GetHeap()->FindSpaceFromObject(object, false)->IsImageSpace());
Vladimir Markof4da6752014-08-01 19:04:18 +0100879 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100880 // Note: We only patch targeting Objects in image which is in the low 4gb.
Vladimir Markof4da6752014-08-01 19:04:18 +0100881 uint32_t address = PointerToLowMemUInt32(object);
882 DCHECK_LE(offset + 4, code->size());
883 uint8_t* data = &(*code)[offset];
884 data[0] = address & 0xffu;
885 data[1] = (address >> 8) & 0xffu;
886 data[2] = (address >> 16) & 0xffu;
887 data[3] = (address >> 24) & 0xffu;
888 }
889
Mathieu Chartiere401d142015-04-22 13:56:20 -0700890 void PatchMethodAddress(std::vector<uint8_t>* code, uint32_t offset, ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700891 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko09d09432015-09-08 13:47:48 +0100892 if (writer_->HasImage()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700893 method = writer_->image_writer_->GetImageMethodAddress(method);
Vladimir Marko09d09432015-09-08 13:47:48 +0100894 } else if (kIsDebugBuild) {
895 // NOTE: We're using linker patches for app->boot references when the image can
896 // be relocated and therefore we need to emit .oat_patches. We're not using this
897 // for app->app references, so check that the method is an image method.
898 gc::space::ImageSpace* image_space = Runtime::Current()->GetHeap()->GetImageSpace();
899 size_t method_offset = reinterpret_cast<const uint8_t*>(method) - image_space->Begin();
900 CHECK(image_space->GetImageHeader().GetMethodsSection().Contains(method_offset));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700901 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100902 // Note: We only patch targeting ArtMethods in image which is in the low 4gb.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700903 uint32_t address = PointerToLowMemUInt32(method);
904 DCHECK_LE(offset + 4, code->size());
905 uint8_t* data = &(*code)[offset];
906 data[0] = address & 0xffu;
907 data[1] = (address >> 8) & 0xffu;
908 data[2] = (address >> 16) & 0xffu;
909 data[3] = (address >> 24) & 0xffu;
910 }
911
Vladimir Markof4da6752014-08-01 19:04:18 +0100912 void PatchCodeAddress(std::vector<uint8_t>* code, uint32_t offset, uint32_t target_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700913 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko09d09432015-09-08 13:47:48 +0100914 uint32_t address = target_offset;
915 if (writer_->HasImage()) {
916 address = PointerToLowMemUInt32(writer_->image_writer_->GetOatFileBegin() +
917 writer_->oat_data_offset_ + target_offset);
918 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100919 DCHECK_LE(offset + 4, code->size());
920 uint8_t* data = &(*code)[offset];
921 data[0] = address & 0xffu;
922 data[1] = (address >> 8) & 0xffu;
923 data[2] = (address >> 16) & 0xffu;
924 data[3] = (address >> 24) & 0xffu;
925 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100926};
927
928template <typename DataAccess>
929class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor {
930 public:
931 WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800932 size_t relative_offset)
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100933 : OatDexMethodVisitor(writer, relative_offset),
934 out_(out),
935 file_offset_(file_offset) {
936 }
937
938 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
939 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
940 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
941
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700942 if (compiled_method != nullptr) { // ie. not an abstract method
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100943 size_t file_offset = file_offset_;
944 OutputStream* out = out_;
945
946 uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_);
947 ++method_offsets_index_;
948
949 // Write deduplicated map.
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800950 const SwapVector<uint8_t>* map = DataAccess::GetData(compiled_method);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100951 size_t map_size = map == nullptr ? 0 : map->size() * sizeof((*map)[0]);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100952 DCHECK((map_size == 0u && map_offset == 0u) ||
953 (map_size != 0u && map_offset != 0u && map_offset <= offset_))
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800954 << map_size << " " << map_offset << " " << offset_ << " "
955 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " for " << DataAccess::Name();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100956 if (map_size != 0u && map_offset == offset_) {
957 if (UNLIKELY(!out->WriteFully(&(*map)[0], map_size))) {
958 ReportWriteFailure(it);
959 return false;
960 }
961 offset_ += map_size;
962 }
963 DCHECK_OFFSET_();
964 }
965
966 return true;
967 }
968
969 private:
970 OutputStream* const out_;
971 size_t const file_offset_;
972
973 void ReportWriteFailure(const ClassDataItemIterator& it) {
974 PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for "
975 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
976 }
977};
978
979// Visit all methods from all classes in all dex files with the specified visitor.
980bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) {
981 for (const DexFile* dex_file : *dex_files_) {
982 const size_t class_def_count = dex_file->NumClassDefs();
983 for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) {
984 if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) {
985 return false;
986 }
987 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700988 const uint8_t* class_data = dex_file->GetClassData(class_def);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700989 if (class_data != nullptr) { // ie not an empty class, such as a marker interface
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100990 ClassDataItemIterator it(*dex_file, class_data);
991 while (it.HasNextStaticField()) {
992 it.Next();
993 }
994 while (it.HasNextInstanceField()) {
995 it.Next();
996 }
997 size_t class_def_method_index = 0u;
998 while (it.HasNextDirectMethod()) {
999 if (!visitor->VisitMethod(class_def_method_index, it)) {
1000 return false;
1001 }
1002 ++class_def_method_index;
1003 it.Next();
1004 }
1005 while (it.HasNextVirtualMethod()) {
1006 if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) {
1007 return false;
1008 }
1009 ++class_def_method_index;
1010 it.Next();
1011 }
1012 }
1013 if (UNLIKELY(!visitor->EndClass())) {
1014 return false;
1015 }
1016 }
1017 }
1018 return true;
1019}
1020
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001021size_t OatWriter::InitOatHeader() {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001022 oat_header_ = OatHeader::Create(compiler_driver_->GetInstructionSet(),
1023 compiler_driver_->GetInstructionSetFeatures(),
1024 dex_files_,
1025 image_file_location_oat_checksum_,
1026 image_file_location_oat_begin_,
1027 key_value_store_);
1028
1029 return oat_header_->GetHeaderSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001030}
1031
1032size_t OatWriter::InitOatDexFiles(size_t offset) {
1033 // create the OatDexFiles
1034 for (size_t i = 0; i != dex_files_->size(); ++i) {
1035 const DexFile* dex_file = (*dex_files_)[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001036 CHECK(dex_file != nullptr);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001037 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001038 oat_dex_files_.push_back(oat_dex_file);
1039 offset += oat_dex_file->SizeOf();
1040 }
1041 return offset;
1042}
1043
Brian Carlstrom89521892011-12-07 22:05:07 -08001044size_t OatWriter::InitDexFiles(size_t offset) {
1045 // calculate the offsets within OatDexFiles to the DexFiles
1046 for (size_t i = 0; i != dex_files_->size(); ++i) {
1047 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001048 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -08001049 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001050 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -08001051
1052 // set offset in OatDexFile to DexFile
1053 oat_dex_files_[i]->dex_file_offset_ = offset;
1054
1055 const DexFile* dex_file = (*dex_files_)[i];
1056 offset += dex_file->GetHeader().file_size_;
1057 }
1058 return offset;
1059}
1060
Brian Carlstrom389efb02012-01-11 12:06:26 -08001061size_t OatWriter::InitOatClasses(size_t offset) {
Brian Carlstrom389efb02012-01-11 12:06:26 -08001062 // calculate the offsets within OatDexFiles to OatClasses
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001063 InitOatClassesMethodVisitor visitor(this, offset);
1064 bool success = VisitDexMethods(&visitor);
1065 CHECK(success);
1066 offset = visitor.GetOffset();
Brian Carlstromba150c32013-08-27 17:31:03 -07001067
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001068 // Update oat_dex_files_.
1069 auto oat_class_it = oat_classes_.begin();
1070 for (OatDexFile* oat_dex_file : oat_dex_files_) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001071 for (uint32_t& method_offset : oat_dex_file->methods_offsets_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001072 DCHECK(oat_class_it != oat_classes_.end());
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001073 method_offset = (*oat_class_it)->offset_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001074 ++oat_class_it;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001075 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001076 oat_dex_file->UpdateChecksum(oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001077 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001078 CHECK(oat_class_it == oat_classes_.end());
1079
1080 return offset;
1081}
1082
1083size_t OatWriter::InitOatMaps(size_t offset) {
1084 #define VISIT(VisitorType) \
1085 do { \
1086 VisitorType visitor(this, offset); \
1087 bool success = VisitDexMethods(&visitor); \
1088 DCHECK(success); \
1089 offset = visitor.GetOffset(); \
1090 } while (false)
1091
1092 VISIT(InitMapMethodVisitor<GcMapDataAccess>);
1093 VISIT(InitMapMethodVisitor<MappingTableDataAccess>);
1094 VISIT(InitMapMethodVisitor<VmapTableDataAccess>);
1095
1096 #undef VISIT
1097
Brian Carlstrome24fa612011-09-29 00:53:55 -07001098 return offset;
1099}
1100
1101size_t OatWriter::InitOatCode(size_t offset) {
1102 // calculate the offsets within OatHeader to executable code
1103 size_t old_offset = offset;
Dave Allison50abf0a2014-06-23 13:19:59 -07001104 size_t adjusted_offset = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001105 // required to be on a new page boundary
1106 offset = RoundUp(offset, kPageSize);
1107 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001108 size_executable_offset_alignment_ = offset - old_offset;
1109 if (compiler_driver_->IsImage()) {
Alex Lighta59dd802014-07-02 16:28:08 -07001110 CHECK_EQ(image_patch_delta_, 0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001111 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001112
Ian Rogers848871b2013-08-05 10:56:33 -07001113 #define DO_TRAMPOLINE(field, fn_name) \
1114 offset = CompiledCode::AlignCode(offset, instruction_set); \
Dave Allison50abf0a2014-06-23 13:19:59 -07001115 adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \
1116 oat_header_->Set ## fn_name ## Offset(adjusted_offset); \
Ian Rogers848871b2013-08-05 10:56:33 -07001117 field.reset(compiler_driver_->Create ## fn_name()); \
1118 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001119
Ian Rogers848871b2013-08-05 10:56:33 -07001120 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Andreas Gampe2da88232014-02-27 12:26:20 -08001121 DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline);
Jeff Hao88474b42013-10-23 16:24:40 -07001122 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -07001123 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
1124 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001125
Ian Rogers848871b2013-08-05 10:56:33 -07001126 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001127 } else {
Ian Rogers848871b2013-08-05 10:56:33 -07001128 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
1129 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
1130 oat_header_->SetJniDlsymLookupOffset(0);
Andreas Gampe2da88232014-02-27 12:26:20 -08001131 oat_header_->SetQuickGenericJniTrampolineOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -07001132 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001133 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -07001134 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Alex Lighta59dd802014-07-02 16:28:08 -07001135 oat_header_->SetImagePatchDelta(image_patch_delta_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001136 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001137 return offset;
1138}
1139
1140size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001141 #define VISIT(VisitorType) \
1142 do { \
1143 VisitorType visitor(this, offset); \
1144 bool success = VisitDexMethods(&visitor); \
1145 DCHECK(success); \
1146 offset = visitor.GetOffset(); \
1147 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001148
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001149 VISIT(InitCodeMethodVisitor);
Andreas Gampe9edb5b12015-09-21 11:46:52 -07001150 if (compiler_driver_->IsImage()) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001151 VISIT(InitImageMethodVisitor);
Ian Rogers0571d352011-11-03 19:51:38 -07001152 }
Logan Chien8b977d32012-02-21 19:14:55 +08001153
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001154 #undef VISIT
1155
Brian Carlstrome24fa612011-09-29 00:53:55 -07001156 return offset;
1157}
1158
David Srbeckybc90fd02015-04-22 19:40:27 +01001159bool OatWriter::WriteRodata(OutputStream* out) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001160 const off_t raw_file_offset = out->Seek(0, kSeekCurrent);
1161 if (raw_file_offset == (off_t) -1) {
1162 LOG(ERROR) << "Failed to get file offset in " << out->GetLocation();
1163 return false;
1164 }
1165 const size_t file_offset = static_cast<size_t>(raw_file_offset);
David Srbeckybc90fd02015-04-22 19:40:27 +01001166 oat_data_offset_ = file_offset;
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001167
Vladimir Markof4da6752014-08-01 19:04:18 +01001168 // Reserve space for header. It will be written last - after updating the checksum.
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001169 size_t header_size = oat_header_->GetHeaderSize();
Vladimir Markof4da6752014-08-01 19:04:18 +01001170 if (out->Seek(header_size, kSeekCurrent) == (off_t) -1) {
1171 PLOG(ERROR) << "Failed to reserve space for oat header in " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001172 return false;
1173 }
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001174 size_oat_header_ += sizeof(OatHeader);
1175 size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001176
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001177 if (!WriteTables(out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001178 LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001179 return false;
1180 }
1181
Vladimir Markof4da6752014-08-01 19:04:18 +01001182 off_t tables_end_offset = out->Seek(0, kSeekCurrent);
1183 if (tables_end_offset == (off_t) -1) {
1184 LOG(ERROR) << "Failed to seek to oat code position in " << out->GetLocation();
1185 return false;
1186 }
1187 size_t relative_offset = static_cast<size_t>(tables_end_offset) - file_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001188 relative_offset = WriteMaps(out, file_offset, relative_offset);
1189 if (relative_offset == 0) {
1190 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
1191 return false;
1192 }
1193
David Srbeckybc90fd02015-04-22 19:40:27 +01001194 // Write padding.
1195 off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent);
1196 relative_offset += size_executable_offset_alignment_;
1197 DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset());
1198 size_t expected_file_offset = file_offset + relative_offset;
1199 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
1200 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
1201 << " Expected: " << expected_file_offset << " File: " << out->GetLocation();
1202 return 0;
1203 }
1204 DCHECK_OFFSET();
1205
1206 return true;
1207}
1208
1209bool OatWriter::WriteCode(OutputStream* out) {
1210 size_t header_size = oat_header_->GetHeaderSize();
1211 const size_t file_offset = oat_data_offset_;
1212 size_t relative_offset = oat_header_->GetExecutableOffset();
1213 DCHECK_OFFSET();
1214
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001215 relative_offset = WriteCode(out, file_offset, relative_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001216 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001217 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001218 return false;
1219 }
1220
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001221 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
1222 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001223 LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001224 return false;
1225 }
1226
Vladimir Markof4da6752014-08-01 19:04:18 +01001227 const off_t oat_end_file_offset = out->Seek(0, kSeekCurrent);
1228 if (oat_end_file_offset == (off_t) -1) {
1229 LOG(ERROR) << "Failed to get oat end file offset in " << out->GetLocation();
1230 return false;
1231 }
1232
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001233 if (kIsDebugBuild) {
1234 uint32_t size_total = 0;
1235 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001236 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001237 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001238
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001239 DO_STAT(size_dex_file_alignment_);
1240 DO_STAT(size_executable_offset_alignment_);
1241 DO_STAT(size_oat_header_);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001242 DO_STAT(size_oat_header_key_value_store_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001243 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -07001244 DO_STAT(size_interpreter_to_interpreter_bridge_);
1245 DO_STAT(size_interpreter_to_compiled_code_bridge_);
1246 DO_STAT(size_jni_dlsym_lookup_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001247 DO_STAT(size_quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001248 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001249 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001250 DO_STAT(size_quick_to_interpreter_bridge_);
1251 DO_STAT(size_trampoline_alignment_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001252 DO_STAT(size_method_header_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001253 DO_STAT(size_code_);
1254 DO_STAT(size_code_alignment_);
Vladimir Markof4da6752014-08-01 19:04:18 +01001255 DO_STAT(size_relative_call_thunks_);
Vladimir Markoc74658b2015-03-31 10:26:41 +01001256 DO_STAT(size_misc_thunks_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001257 DO_STAT(size_mapping_table_);
1258 DO_STAT(size_vmap_table_);
1259 DO_STAT(size_gc_map_);
1260 DO_STAT(size_oat_dex_file_location_size_);
1261 DO_STAT(size_oat_dex_file_location_data_);
1262 DO_STAT(size_oat_dex_file_location_checksum_);
1263 DO_STAT(size_oat_dex_file_offset_);
1264 DO_STAT(size_oat_dex_file_methods_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001265 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001266 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001267 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001268 DO_STAT(size_oat_class_method_offsets_);
1269 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001270
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001271 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Vladimir Markof4da6752014-08-01 19:04:18 +01001272 CHECK_EQ(file_offset + size_total, static_cast<size_t>(oat_end_file_offset));
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001273 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001274 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001275
Vladimir Markof4da6752014-08-01 19:04:18 +01001276 CHECK_EQ(file_offset + size_, static_cast<size_t>(oat_end_file_offset));
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001277 CHECK_EQ(size_, relative_offset);
1278
Vladimir Markof4da6752014-08-01 19:04:18 +01001279 // Write the header now that the checksum is final.
1280 if (out->Seek(file_offset, kSeekSet) == (off_t) -1) {
1281 PLOG(ERROR) << "Failed to seek to oat header position in " << out->GetLocation();
1282 return false;
1283 }
David Srbeckybc90fd02015-04-22 19:40:27 +01001284 DCHECK_EQ(file_offset, static_cast<size_t>(out->Seek(0, kSeekCurrent)));
Vladimir Markof4da6752014-08-01 19:04:18 +01001285 if (!out->WriteFully(oat_header_, header_size)) {
1286 PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation();
1287 return false;
1288 }
1289 if (out->Seek(oat_end_file_offset, kSeekSet) == (off_t) -1) {
1290 PLOG(ERROR) << "Failed to seek to end after writing oat header to " << out->GetLocation();
1291 return false;
1292 }
1293 DCHECK_EQ(oat_end_file_offset, out->Seek(0, kSeekCurrent));
1294
Brian Carlstrome24fa612011-09-29 00:53:55 -07001295 return true;
1296}
1297
Ian Rogers3d504072014-03-01 09:16:49 -08001298bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001299 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001300 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001301 PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001302 return false;
1303 }
1304 }
Brian Carlstrom89521892011-12-07 22:05:07 -08001305 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001306 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Ian Rogers3d504072014-03-01 09:16:49 -08001307 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -08001308 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
1309 const DexFile* dex_file = (*dex_files_)[i];
1310 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
1311 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
1312 return false;
1313 }
1314 const DexFile* dex_file = (*dex_files_)[i];
Ian Rogers3d504072014-03-01 09:16:49 -08001315 if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001316 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
Ian Rogers3d504072014-03-01 09:16:49 -08001317 << " to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001318 return false;
1319 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001320 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -08001321 }
Brian Carlstrom389efb02012-01-11 12:06:26 -08001322 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001323 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001324 PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001325 return false;
1326 }
1327 }
1328 return true;
1329}
1330
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001331size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) {
1332 #define VISIT(VisitorType) \
1333 do { \
1334 VisitorType visitor(this, out, file_offset, relative_offset); \
1335 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1336 return 0; \
1337 } \
1338 relative_offset = visitor.GetOffset(); \
1339 } while (false)
1340
1341 size_t gc_maps_offset = relative_offset;
1342 VISIT(WriteMapMethodVisitor<GcMapDataAccess>);
1343 size_gc_map_ = relative_offset - gc_maps_offset;
1344
1345 size_t mapping_tables_offset = relative_offset;
1346 VISIT(WriteMapMethodVisitor<MappingTableDataAccess>);
1347 size_mapping_table_ = relative_offset - mapping_tables_offset;
1348
1349 size_t vmap_tables_offset = relative_offset;
1350 VISIT(WriteMapMethodVisitor<VmapTableDataAccess>);
1351 size_vmap_table_ = relative_offset - vmap_tables_offset;
1352
1353 #undef VISIT
1354
1355 return relative_offset;
1356}
1357
1358size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001359 if (compiler_driver_->IsImage()) {
1360 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001361
Ian Rogers848871b2013-08-05 10:56:33 -07001362 #define DO_TRAMPOLINE(field) \
1363 do { \
1364 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
1365 uint32_t alignment_padding = aligned_offset - relative_offset; \
Ian Rogers3d504072014-03-01 09:16:49 -08001366 out->Seek(alignment_padding, kSeekCurrent); \
Ian Rogers848871b2013-08-05 10:56:33 -07001367 size_trampoline_alignment_ += alignment_padding; \
Ian Rogers3d504072014-03-01 09:16:49 -08001368 if (!out->WriteFully(&(*field)[0], field->size())) { \
1369 PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \
Ian Rogers848871b2013-08-05 10:56:33 -07001370 return false; \
1371 } \
1372 size_ ## field += field->size(); \
1373 relative_offset += alignment_padding + field->size(); \
1374 DCHECK_OFFSET(); \
1375 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001376
Ian Rogers848871b2013-08-05 10:56:33 -07001377 DO_TRAMPOLINE(jni_dlsym_lookup_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001378 DO_TRAMPOLINE(quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001379 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001380 DO_TRAMPOLINE(quick_resolution_trampoline_);
1381 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
1382 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001383 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001384 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001385}
1386
Ian Rogers3d504072014-03-01 09:16:49 -08001387size_t OatWriter::WriteCodeDexFiles(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001388 const size_t file_offset,
1389 size_t relative_offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001390 #define VISIT(VisitorType) \
1391 do { \
1392 VisitorType visitor(this, out, file_offset, relative_offset); \
1393 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1394 return 0; \
1395 } \
1396 relative_offset = visitor.GetOffset(); \
1397 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001398
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001399 VISIT(WriteCodeMethodVisitor);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001400
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001401 #undef VISIT
Brian Carlstrom265091e2013-01-30 14:08:26 -08001402
Vladimir Markob163bb72015-03-31 21:49:49 +01001403 size_code_alignment_ += relative_patcher_->CodeAlignmentSize();
1404 size_relative_call_thunks_ += relative_patcher_->RelativeCallThunksSize();
1405 size_misc_thunks_ += relative_patcher_->MiscThunksSize();
1406
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001407 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001408}
1409
Vladimir Markof4da6752014-08-01 19:04:18 +01001410bool OatWriter::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) {
1411 static const uint8_t kPadding[] = {
1412 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
1413 };
1414 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
1415 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
1416 return false;
1417 }
1418 size_code_alignment_ += aligned_code_delta;
1419 return true;
1420}
1421
Vladimir Markob163bb72015-03-31 21:49:49 +01001422std::pair<bool, uint32_t> OatWriter::MethodOffsetMap::FindMethodOffset(MethodReference ref) {
1423 auto it = map.find(ref);
1424 if (it == map.end()) {
1425 return std::pair<bool, uint32_t>(false, 0u);
1426 } else {
1427 return std::pair<bool, uint32_t>(true, it->second);
1428 }
1429}
1430
Brian Carlstrom265091e2013-01-30 14:08:26 -08001431OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
1432 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -08001433 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001434 dex_file_location_size_ = location.size();
1435 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001436 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -08001437 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001438 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001439}
1440
1441size_t OatWriter::OatDexFile::SizeOf() const {
1442 return sizeof(dex_file_location_size_)
1443 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001444 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -08001445 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001446 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001447}
1448
Ian Rogers3d504072014-03-01 09:16:49 -08001449void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const {
1450 oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
1451 oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
1452 oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
1453 oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
1454 oat_header->UpdateChecksum(&methods_offsets_[0],
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001455 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001456}
1457
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001458bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001459 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001460 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001461 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001462 if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
1463 PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001464 return false;
1465 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001466 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001467 if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
1468 PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001469 return false;
1470 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001471 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Ian Rogers3d504072014-03-01 09:16:49 -08001472 if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
1473 PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001474 return false;
1475 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001476 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Ian Rogers3d504072014-03-01 09:16:49 -08001477 if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
1478 PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001479 return false;
1480 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001481 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Ian Rogers3d504072014-03-01 09:16:49 -08001482 if (!out->WriteFully(&methods_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001483 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001484 PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001485 return false;
1486 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001487 oat_writer->size_oat_dex_file_methods_offsets_ +=
1488 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001489 return true;
1490}
1491
Brian Carlstromba150c32013-08-27 17:31:03 -07001492OatWriter::OatClass::OatClass(size_t offset,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001493 const std::vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -07001494 uint32_t num_non_null_compiled_methods,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001495 mirror::Class::Status status)
1496 : compiled_methods_(compiled_methods) {
1497 uint32_t num_methods = compiled_methods.size();
Brian Carlstromba150c32013-08-27 17:31:03 -07001498 CHECK_LE(num_non_null_compiled_methods, num_methods);
1499
Brian Carlstrom265091e2013-01-30 14:08:26 -08001500 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -07001501 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
1502
1503 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
1504 // apply when there are 0 methods, we just arbitrarily say that 0
1505 // methods means kOatClassNoneCompiled and that we won't use
1506 // kOatClassAllCompiled unless there is at least one compiled
1507 // method. This means in an interpretter only system, we can assert
1508 // that all classes are kOatClassNoneCompiled.
1509 if (num_non_null_compiled_methods == 0) {
1510 type_ = kOatClassNoneCompiled;
1511 } else if (num_non_null_compiled_methods == num_methods) {
1512 type_ = kOatClassAllCompiled;
1513 } else {
1514 type_ = kOatClassSomeCompiled;
1515 }
1516
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001517 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -07001518 method_offsets_.resize(num_non_null_compiled_methods);
Vladimir Marko8a630572014-04-09 18:45:35 +01001519 method_headers_.resize(num_non_null_compiled_methods);
Brian Carlstromba150c32013-08-27 17:31:03 -07001520
1521 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
1522 if (type_ == kOatClassSomeCompiled) {
1523 method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator());
1524 method_bitmap_size_ = method_bitmap_->GetSizeOf();
1525 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
1526 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
1527 } else {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001528 method_bitmap_ = nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001529 method_bitmap_size_ = 0;
1530 }
1531
1532 for (size_t i = 0; i < num_methods; i++) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001533 CompiledMethod* compiled_method = compiled_methods_[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001534 if (compiled_method == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001535 oat_method_offsets_offsets_from_oat_class_[i] = 0;
1536 } else {
1537 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
1538 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
1539 if (type_ == kOatClassSomeCompiled) {
1540 method_bitmap_->SetBit(i);
1541 }
1542 }
1543 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001544}
1545
Brian Carlstromba150c32013-08-27 17:31:03 -07001546OatWriter::OatClass::~OatClass() {
Mathieu Chartier661974a2014-01-09 11:23:53 -08001547 delete method_bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -07001548}
1549
Brian Carlstrom265091e2013-01-30 14:08:26 -08001550size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
1551 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001552 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
1553 if (method_offset == 0) {
1554 return 0;
1555 }
1556 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001557}
1558
1559size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
1560 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001561 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08001562}
1563
1564size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001565 return sizeof(status_)
1566 + sizeof(type_)
1567 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
1568 + method_bitmap_size_
1569 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001570}
1571
Ian Rogers3d504072014-03-01 09:16:49 -08001572void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const {
1573 oat_header->UpdateChecksum(&status_, sizeof(status_));
1574 oat_header->UpdateChecksum(&type_, sizeof(type_));
Brian Carlstromba150c32013-08-27 17:31:03 -07001575 if (method_bitmap_size_ != 0) {
1576 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001577 oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_));
1578 oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001579 }
Ian Rogers3d504072014-03-01 09:16:49 -08001580 oat_header->UpdateChecksum(&method_offsets_[0],
1581 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001582}
1583
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001584bool OatWriter::OatClass::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001585 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001586 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001587 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001588 if (!out->WriteFully(&status_, sizeof(status_))) {
1589 PLOG(ERROR) << "Failed to write class status to " << out->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001590 return false;
1591 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001592 oat_writer->size_oat_class_status_ += sizeof(status_);
Ian Rogers3d504072014-03-01 09:16:49 -08001593 if (!out->WriteFully(&type_, sizeof(type_))) {
1594 PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001595 return false;
1596 }
1597 oat_writer->size_oat_class_type_ += sizeof(type_);
1598 if (method_bitmap_size_ != 0) {
1599 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001600 if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
1601 PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001602 return false;
1603 }
1604 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001605 if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
1606 PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001607 return false;
1608 }
1609 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1610 }
Ian Rogers3d504072014-03-01 09:16:49 -08001611 if (!out->WriteFully(&method_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001612 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001613 PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001614 return false;
1615 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001616 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001617 return true;
1618}
1619
Brian Carlstrome24fa612011-09-29 00:53:55 -07001620} // namespace art