blob: dcb23bf1ec6895caa2e9becb2029b1dd8d1fa8fb [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 {
Vladimir Marko35831e82015-09-11 11:59:18 +0100175 static ArrayRef<const 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 {
Vladimir Marko35831e82015-09-11 11:59:18 +0100197 static ArrayRef<const 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 {
Vladimir Marko35831e82015-09-11 11:59:18 +0100219 static ArrayRef<const 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
Vladimir Marko35831e82015-09-11 11:59:18 +0100391 ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode();
392 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.
Vladimir Marko35831e82015-09-11 11:59:18 +0100431 DCHECK(!quick_code.empty() || 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 }
Vladimir Marko35831e82015-09-11 11:59:18 +0100499 ArrayRef<const uint8_t> gc_map = compiled_method->GetGcMap();
500 if (!gc_map.empty()) {
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)
Vladimir Marko35831e82015-09-11 11:59:18 +0100504 << gc_map_size << " " << (is_native ? "true" : "false") << " "
Nicolas Geoffray39468442014-09-02 15:17:15 +0100505 << (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 {
Vladimir Marko35831e82015-09-11 11:59:18 +0100522 // Code is deduplicated by CompilerDriver, compare only data pointers.
523 if (lhs->GetQuickCode().data() != rhs->GetQuickCode().data()) {
524 return lhs->GetQuickCode().data() < rhs->GetQuickCode().data();
Vladimir Marko20f85592015-03-19 10:07:02 +0000525 }
526 // If the code is the same, all other fields are likely to be the same as well.
Vladimir Marko35831e82015-09-11 11:59:18 +0100527 if (UNLIKELY(lhs->GetMappingTable().data() != rhs->GetMappingTable().data())) {
528 return lhs->GetMappingTable().data() < rhs->GetMappingTable().data();
Vladimir Marko20f85592015-03-19 10:07:02 +0000529 }
Vladimir Marko35831e82015-09-11 11:59:18 +0100530 if (UNLIKELY(lhs->GetVmapTable().data() != rhs->GetVmapTable().data())) {
531 return lhs->GetVmapTable().data() < rhs->GetVmapTable().data();
Vladimir Marko20f85592015-03-19 10:07:02 +0000532 }
Vladimir Marko35831e82015-09-11 11:59:18 +0100533 if (UNLIKELY(lhs->GetGcMap().data() != rhs->GetGcMap().data())) {
534 return lhs->GetGcMap().data() < rhs->GetGcMap().data();
Vladimir Marko20f85592015-03-19 10:07:02 +0000535 }
Vladimir Marko35831e82015-09-11 11:59:18 +0100536 if (UNLIKELY(lhs->GetPatches().data() != rhs->GetPatches().data())) {
537 return lhs->GetPatches().data() < rhs->GetPatches().data();
Vladimir Marko20f85592015-03-19 10:07:02 +0000538 }
539 return false;
540 }
541 };
542
David Srbecky009e2a62015-04-15 02:46:30 +0100543 uint32_t NewQuickCodeOffset(CompiledMethod* compiled_method,
544 const ClassDataItemIterator& it,
545 uint32_t thumb_offset) {
546 offset_ = writer_->relative_patcher_->ReserveSpace(
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100547 offset_, compiled_method, MethodReference(dex_file_, it.GetMemberIndex()));
David Srbecky009e2a62015-04-15 02:46:30 +0100548 offset_ = compiled_method->AlignCode(offset_);
549 DCHECK_ALIGNED_PARAM(offset_,
550 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
551 return offset_ + sizeof(OatQuickMethodHeader) + thumb_offset;
552 }
553
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100554 // Deduplication is already done on a pointer basis by the compiler driver,
555 // so we can simply compare the pointers to find out if things are duplicated.
Vladimir Marko8a630572014-04-09 18:45:35 +0100556 SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_;
David Srbecky2f6cdb02015-04-11 00:17:53 +0100557
David Srbecky009e2a62015-04-15 02:46:30 +0100558 // Cache of compiler's --debuggable option.
559 const bool debuggable_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100560};
561
562template <typename DataAccess>
563class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor {
564 public:
565 InitMapMethodVisitor(OatWriter* writer, size_t offset)
566 : OatDexMethodVisitor(writer, offset) {
567 }
568
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700569 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it ATTRIBUTE_UNUSED)
Mathieu Chartier90443472015-07-16 20:32:27 -0700570 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100571 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
572 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
573
574 if (compiled_method != nullptr) {
575 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
576 DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u);
577
Vladimir Marko35831e82015-09-11 11:59:18 +0100578 ArrayRef<const uint8_t> map = DataAccess::GetData(compiled_method);
579 uint32_t map_size = map.size() * sizeof(map[0]);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100580 if (map_size != 0u) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100581 auto lb = dedupe_map_.lower_bound(map.data());
582 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(map.data(), lb->first)) {
Vladimir Markobd72fc12014-07-09 16:06:40 +0100583 DataAccess::SetOffset(oat_class, method_offsets_index_, lb->second);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100584 } else {
585 DataAccess::SetOffset(oat_class, method_offsets_index_, offset_);
Vladimir Marko35831e82015-09-11 11:59:18 +0100586 dedupe_map_.PutBefore(lb, map.data(), offset_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100587 offset_ += map_size;
Vladimir Marko35831e82015-09-11 11:59:18 +0100588 writer_->oat_header_->UpdateChecksum(&map[0], map_size);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100589 }
590 }
591 ++method_offsets_index_;
592 }
593
594 return true;
595 }
596
597 private:
598 // Deduplication is already done on a pointer basis by the compiler driver,
599 // so we can simply compare the pointers to find out if things are duplicated.
Vladimir Marko35831e82015-09-11 11:59:18 +0100600 SafeMap<const uint8_t*, uint32_t> dedupe_map_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100601};
602
603class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor {
604 public:
605 InitImageMethodVisitor(OatWriter* writer, size_t offset)
Jeff Haoc7d11882015-02-03 15:08:39 -0800606 : OatDexMethodVisitor(writer, offset),
607 pointer_size_(GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet())) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100608 }
609
610 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
Mathieu Chartier90443472015-07-16 20:32:27 -0700611 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100612 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
613 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
614
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800615 OatMethodOffsets offsets(0u);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100616 if (compiled_method != nullptr) {
617 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
618 offsets = oat_class->method_offsets_[method_offsets_index_];
619 ++method_offsets_index_;
620 }
621
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100622 ClassLinker* linker = Runtime::Current()->GetClassLinker();
623 InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_));
624 // Unchecked as we hold mutator_lock_ on entry.
625 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800626 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700627 Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache(
628 Thread::Current(), *dex_file_)));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700629 ArtMethod* method = linker->ResolveMethod(
630 *dex_file_, it.GetMemberIndex(), dex_cache, NullHandle<mirror::ClassLoader>(), nullptr,
631 invoke_type);
Andreas Gamped9efea62014-07-21 22:56:08 -0700632 if (method == nullptr) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100633 LOG(INTERNAL_FATAL) << "Unexpected failure to resolve a method: "
634 << PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
Andreas Gamped9efea62014-07-21 22:56:08 -0700635 soa.Self()->AssertPendingException();
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000636 mirror::Throwable* exc = soa.Self()->GetException();
Andreas Gamped9efea62014-07-21 22:56:08 -0700637 std::string dump = exc->Dump();
638 LOG(FATAL) << dump;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100639 UNREACHABLE();
Andreas Gamped9efea62014-07-21 22:56:08 -0700640 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100641
Vladimir Marko35831e82015-09-11 11:59:18 +0100642 if (compiled_method != nullptr && compiled_method->GetQuickCode().size() != 0) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100643 method->SetEntryPointFromQuickCompiledCodePtrSize(
644 reinterpret_cast<void*>(offsets.code_offset_), pointer_size_);
645 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100646
647 return true;
648 }
Jeff Haoc7d11882015-02-03 15:08:39 -0800649
650 protected:
651 const size_t pointer_size_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100652};
653
654class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor {
655 public:
656 WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
Vladimir Markof4da6752014-08-01 19:04:18 +0100657 size_t relative_offset) SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100658 : OatDexMethodVisitor(writer, relative_offset),
659 out_(out),
Vladimir Markof4da6752014-08-01 19:04:18 +0100660 file_offset_(file_offset),
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100661 soa_(Thread::Current()),
662 no_thread_suspension_(soa_.Self(), "OatWriter patching"),
Vladimir Markof4da6752014-08-01 19:04:18 +0100663 class_linker_(Runtime::Current()->GetClassLinker()),
664 dex_cache_(nullptr) {
Vladimir Marko09d09432015-09-08 13:47:48 +0100665 patched_code_.reserve(16 * KB);
666 if (writer_->HasImage()) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100667 // If we're creating the image, the address space must be ready so that we can apply patches.
668 CHECK(writer_->image_writer_->IsImageAddressSpaceReady());
Vladimir Markof4da6752014-08-01 19:04:18 +0100669 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100670 }
671
Vladimir Markof4da6752014-08-01 19:04:18 +0100672 ~WriteCodeMethodVisitor() UNLOCK_FUNCTION(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100673 }
674
675 bool StartClass(const DexFile* dex_file, size_t class_def_index)
Mathieu Chartier90443472015-07-16 20:32:27 -0700676 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100677 OatDexMethodVisitor::StartClass(dex_file, class_def_index);
678 if (dex_cache_ == nullptr || dex_cache_->GetDexFile() != dex_file) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700679 dex_cache_ = class_linker_->FindDexCache(Thread::Current(), *dex_file);
Vladimir Markof4da6752014-08-01 19:04:18 +0100680 }
681 return true;
682 }
683
Mathieu Chartier90443472015-07-16 20:32:27 -0700684 bool EndClass() SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100685 bool result = OatDexMethodVisitor::EndClass();
686 if (oat_class_index_ == writer_->oat_classes_.size()) {
687 DCHECK(result); // OatDexMethodVisitor::EndClass() never fails.
Vladimir Marko20f85592015-03-19 10:07:02 +0000688 offset_ = writer_->relative_patcher_->WriteThunks(out_, offset_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100689 if (UNLIKELY(offset_ == 0u)) {
690 PLOG(ERROR) << "Failed to write final relative call thunks";
691 result = false;
692 }
693 }
694 return result;
695 }
696
697 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
Mathieu Chartier90443472015-07-16 20:32:27 -0700698 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100699 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
700 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
701
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700702 // No thread suspension since dex_cache_ that may get invalidated if that occurs.
703 ScopedAssertNoThreadSuspension tsc(Thread::Current(), __FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700704 if (compiled_method != nullptr) { // ie. not an abstract method
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100705 size_t file_offset = file_offset_;
706 OutputStream* out = out_;
707
Vladimir Marko35831e82015-09-11 11:59:18 +0100708 ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode();
709 uint32_t code_size = quick_code.size() * sizeof(uint8_t);
Nicolas Geoffrayf0758792015-07-13 11:56:00 +0000710
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100711 // Deduplicate code arrays.
712 const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_];
713 if (method_offsets.code_offset_ > offset_) {
714 offset_ = writer_->relative_patcher_->WriteThunks(out, offset_);
715 if (offset_ == 0u) {
716 ReportWriteFailure("relative call thunk", it);
717 return false;
Nicolas Geoffrayf0758792015-07-13 11:56:00 +0000718 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100719 uint32_t aligned_offset = compiled_method->AlignCode(offset_);
720 uint32_t aligned_code_delta = aligned_offset - offset_;
721 if (aligned_code_delta != 0) {
722 if (!writer_->WriteCodeAlignment(out, aligned_code_delta)) {
723 ReportWriteFailure("code alignment padding", it);
724 return false;
725 }
726 offset_ += aligned_code_delta;
727 DCHECK_OFFSET_();
728 }
729 DCHECK_ALIGNED_PARAM(offset_,
730 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
731 DCHECK_EQ(method_offsets.code_offset_,
732 offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta())
733 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
734 const OatQuickMethodHeader& method_header =
735 oat_class->method_headers_[method_offsets_index_];
736 writer_->oat_header_->UpdateChecksum(&method_header, sizeof(method_header));
737 if (!out->WriteFully(&method_header, sizeof(method_header))) {
738 ReportWriteFailure("method header", it);
739 return false;
740 }
741 writer_->size_method_header_ += sizeof(method_header);
742 offset_ += sizeof(method_header);
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000743 DCHECK_OFFSET_();
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100744
745 if (!compiled_method->GetPatches().empty()) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100746 patched_code_.assign(quick_code.begin(), quick_code.end());
747 quick_code = ArrayRef<const uint8_t>(patched_code_);
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100748 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
749 if (patch.Type() == kLinkerPatchCallRelative) {
750 // NOTE: Relative calls across oat files are not supported.
751 uint32_t target_offset = GetTargetOffset(patch);
752 uint32_t literal_offset = patch.LiteralOffset();
753 writer_->relative_patcher_->PatchCall(&patched_code_, literal_offset,
754 offset_ + literal_offset, target_offset);
755 } else if (patch.Type() == kLinkerPatchDexCacheArray) {
756 uint32_t target_offset = GetDexCacheOffset(patch);
757 uint32_t literal_offset = patch.LiteralOffset();
758 writer_->relative_patcher_->PatchDexCacheReference(&patched_code_, patch,
759 offset_ + literal_offset,
760 target_offset);
761 } else if (patch.Type() == kLinkerPatchCall) {
762 uint32_t target_offset = GetTargetOffset(patch);
763 PatchCodeAddress(&patched_code_, patch.LiteralOffset(), target_offset);
764 } else if (patch.Type() == kLinkerPatchMethod) {
765 ArtMethod* method = GetTargetMethod(patch);
766 PatchMethodAddress(&patched_code_, patch.LiteralOffset(), method);
767 } else if (patch.Type() == kLinkerPatchType) {
768 mirror::Class* type = GetTargetType(patch);
769 PatchObjectAddress(&patched_code_, patch.LiteralOffset(), type);
770 }
771 }
772 }
773
Vladimir Marko35831e82015-09-11 11:59:18 +0100774 writer_->oat_header_->UpdateChecksum(quick_code.data(), code_size);
775 if (!out->WriteFully(quick_code.data(), code_size)) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100776 ReportWriteFailure("method code", it);
777 return false;
778 }
779 writer_->size_code_ += code_size;
780 offset_ += code_size;
Nicolas Geoffrayf0758792015-07-13 11:56:00 +0000781 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100782 DCHECK_OFFSET_();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100783 ++method_offsets_index_;
784 }
785
786 return true;
787 }
788
789 private:
790 OutputStream* const out_;
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100791 const size_t file_offset_;
792 const ScopedObjectAccess soa_;
793 const ScopedAssertNoThreadSuspension no_thread_suspension_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100794 ClassLinker* const class_linker_;
795 mirror::DexCache* dex_cache_;
796 std::vector<uint8_t> patched_code_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100797
798 void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) {
799 PLOG(ERROR) << "Failed to write " << what << " for "
800 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
801 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100802
Mathieu Chartiere401d142015-04-22 13:56:20 -0700803 ArtMethod* GetTargetMethod(const LinkerPatch& patch)
Mathieu Chartier90443472015-07-16 20:32:27 -0700804 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100805 MethodReference ref = patch.TargetMethod();
806 mirror::DexCache* dex_cache =
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700807 (dex_file_ == ref.dex_file) ? dex_cache_ : class_linker_->FindDexCache(
808 Thread::Current(), *ref.dex_file);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700809 ArtMethod* method = dex_cache->GetResolvedMethod(
810 ref.dex_method_index, class_linker_->GetImagePointerSize());
Vladimir Markof4da6752014-08-01 19:04:18 +0100811 CHECK(method != nullptr);
812 return method;
813 }
814
Mathieu Chartier90443472015-07-16 20:32:27 -0700815 uint32_t GetTargetOffset(const LinkerPatch& patch) SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markob163bb72015-03-31 21:49:49 +0100816 auto target_it = writer_->method_offset_map_.map.find(patch.TargetMethod());
Vladimir Markof4da6752014-08-01 19:04:18 +0100817 uint32_t target_offset =
Vladimir Markob163bb72015-03-31 21:49:49 +0100818 (target_it != writer_->method_offset_map_.map.end()) ? target_it->second : 0u;
Vladimir Markof4da6752014-08-01 19:04:18 +0100819 // If there's no compiled code, point to the correct trampoline.
820 if (UNLIKELY(target_offset == 0)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700821 ArtMethod* target = GetTargetMethod(patch);
Vladimir Markof4da6752014-08-01 19:04:18 +0100822 DCHECK(target != nullptr);
Jeff Haoa0acc2d2015-01-27 11:22:04 -0800823 size_t size = GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet());
824 const void* oat_code_offset = target->GetEntryPointFromQuickCompiledCodePtrSize(size);
825 if (oat_code_offset != 0) {
826 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(oat_code_offset));
827 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(oat_code_offset));
828 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(oat_code_offset));
829 target_offset = PointerToLowMemUInt32(oat_code_offset);
830 } else {
831 target_offset = target->IsNative()
832 ? writer_->oat_header_->GetQuickGenericJniTrampolineOffset()
833 : writer_->oat_header_->GetQuickToInterpreterBridgeOffset();
834 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100835 }
836 return target_offset;
837 }
838
839 mirror::Class* GetTargetType(const LinkerPatch& patch)
Mathieu Chartier90443472015-07-16 20:32:27 -0700840 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100841 mirror::DexCache* dex_cache = (dex_file_ == patch.TargetTypeDexFile())
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700842 ? dex_cache_ : class_linker_->FindDexCache(Thread::Current(), *patch.TargetTypeDexFile());
Vladimir Markof4da6752014-08-01 19:04:18 +0100843 mirror::Class* type = dex_cache->GetResolvedType(patch.TargetTypeIndex());
844 CHECK(type != nullptr);
845 return type;
846 }
847
Mathieu Chartier90443472015-07-16 20:32:27 -0700848 uint32_t GetDexCacheOffset(const LinkerPatch& patch) SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko09d09432015-09-08 13:47:48 +0100849 if (writer_->HasImage()) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100850 auto* element = writer_->image_writer_->GetDexCacheArrayElementImageAddress<const uint8_t*>(
Vladimir Marko20f85592015-03-19 10:07:02 +0000851 patch.TargetDexCacheDexFile(), patch.TargetDexCacheElementOffset());
852 const uint8_t* oat_data = writer_->image_writer_->GetOatFileBegin() + file_offset_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100853 return element - oat_data;
Vladimir Marko20f85592015-03-19 10:07:02 +0000854 } else {
Vladimir Marko09d09432015-09-08 13:47:48 +0100855 size_t start = writer_->dex_cache_arrays_offsets_.Get(patch.TargetDexCacheDexFile());
856 return start + patch.TargetDexCacheElementOffset();
Vladimir Marko20f85592015-03-19 10:07:02 +0000857 }
858 }
859
Vladimir Markof4da6752014-08-01 19:04:18 +0100860 void PatchObjectAddress(std::vector<uint8_t>* code, uint32_t offset, mirror::Object* object)
Mathieu Chartier90443472015-07-16 20:32:27 -0700861 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko09d09432015-09-08 13:47:48 +0100862 if (writer_->HasImage()) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100863 object = writer_->image_writer_->GetImageAddress(object);
Vladimir Marko09d09432015-09-08 13:47:48 +0100864 } else {
865 // NOTE: We're using linker patches for app->boot references when the image can
866 // be relocated and therefore we need to emit .oat_patches. We're not using this
867 // for app->app references, so check that the object is in the image space.
868 DCHECK(Runtime::Current()->GetHeap()->FindSpaceFromObject(object, false)->IsImageSpace());
Vladimir Markof4da6752014-08-01 19:04:18 +0100869 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100870 // Note: We only patch targeting Objects in image which is in the low 4gb.
Vladimir Markof4da6752014-08-01 19:04:18 +0100871 uint32_t address = PointerToLowMemUInt32(object);
872 DCHECK_LE(offset + 4, code->size());
873 uint8_t* data = &(*code)[offset];
874 data[0] = address & 0xffu;
875 data[1] = (address >> 8) & 0xffu;
876 data[2] = (address >> 16) & 0xffu;
877 data[3] = (address >> 24) & 0xffu;
878 }
879
Mathieu Chartiere401d142015-04-22 13:56:20 -0700880 void PatchMethodAddress(std::vector<uint8_t>* code, uint32_t offset, ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700881 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko09d09432015-09-08 13:47:48 +0100882 if (writer_->HasImage()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700883 method = writer_->image_writer_->GetImageMethodAddress(method);
Vladimir Marko09d09432015-09-08 13:47:48 +0100884 } else if (kIsDebugBuild) {
885 // NOTE: We're using linker patches for app->boot references when the image can
886 // be relocated and therefore we need to emit .oat_patches. We're not using this
887 // for app->app references, so check that the method is an image method.
888 gc::space::ImageSpace* image_space = Runtime::Current()->GetHeap()->GetImageSpace();
889 size_t method_offset = reinterpret_cast<const uint8_t*>(method) - image_space->Begin();
890 CHECK(image_space->GetImageHeader().GetMethodsSection().Contains(method_offset));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700891 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100892 // Note: We only patch targeting ArtMethods in image which is in the low 4gb.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700893 uint32_t address = PointerToLowMemUInt32(method);
894 DCHECK_LE(offset + 4, code->size());
895 uint8_t* data = &(*code)[offset];
896 data[0] = address & 0xffu;
897 data[1] = (address >> 8) & 0xffu;
898 data[2] = (address >> 16) & 0xffu;
899 data[3] = (address >> 24) & 0xffu;
900 }
901
Vladimir Markof4da6752014-08-01 19:04:18 +0100902 void PatchCodeAddress(std::vector<uint8_t>* code, uint32_t offset, uint32_t target_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700903 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko09d09432015-09-08 13:47:48 +0100904 uint32_t address = target_offset;
905 if (writer_->HasImage()) {
906 address = PointerToLowMemUInt32(writer_->image_writer_->GetOatFileBegin() +
907 writer_->oat_data_offset_ + target_offset);
908 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100909 DCHECK_LE(offset + 4, code->size());
910 uint8_t* data = &(*code)[offset];
911 data[0] = address & 0xffu;
912 data[1] = (address >> 8) & 0xffu;
913 data[2] = (address >> 16) & 0xffu;
914 data[3] = (address >> 24) & 0xffu;
915 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100916};
917
918template <typename DataAccess>
919class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor {
920 public:
921 WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800922 size_t relative_offset)
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100923 : OatDexMethodVisitor(writer, relative_offset),
924 out_(out),
925 file_offset_(file_offset) {
926 }
927
928 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
929 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
930 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
931
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700932 if (compiled_method != nullptr) { // ie. not an abstract method
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100933 size_t file_offset = file_offset_;
934 OutputStream* out = out_;
935
936 uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_);
937 ++method_offsets_index_;
938
939 // Write deduplicated map.
Vladimir Marko35831e82015-09-11 11:59:18 +0100940 ArrayRef<const uint8_t> map = DataAccess::GetData(compiled_method);
941 size_t map_size = map.size() * sizeof(map[0]);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100942 DCHECK((map_size == 0u && map_offset == 0u) ||
943 (map_size != 0u && map_offset != 0u && map_offset <= offset_))
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800944 << map_size << " " << map_offset << " " << offset_ << " "
945 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " for " << DataAccess::Name();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100946 if (map_size != 0u && map_offset == offset_) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100947 if (UNLIKELY(!out->WriteFully(&map[0], map_size))) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100948 ReportWriteFailure(it);
949 return false;
950 }
951 offset_ += map_size;
952 }
953 DCHECK_OFFSET_();
954 }
955
956 return true;
957 }
958
959 private:
960 OutputStream* const out_;
961 size_t const file_offset_;
962
963 void ReportWriteFailure(const ClassDataItemIterator& it) {
964 PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for "
965 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
966 }
967};
968
969// Visit all methods from all classes in all dex files with the specified visitor.
970bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) {
971 for (const DexFile* dex_file : *dex_files_) {
972 const size_t class_def_count = dex_file->NumClassDefs();
973 for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) {
974 if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) {
975 return false;
976 }
977 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700978 const uint8_t* class_data = dex_file->GetClassData(class_def);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700979 if (class_data != nullptr) { // ie not an empty class, such as a marker interface
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100980 ClassDataItemIterator it(*dex_file, class_data);
981 while (it.HasNextStaticField()) {
982 it.Next();
983 }
984 while (it.HasNextInstanceField()) {
985 it.Next();
986 }
987 size_t class_def_method_index = 0u;
988 while (it.HasNextDirectMethod()) {
989 if (!visitor->VisitMethod(class_def_method_index, it)) {
990 return false;
991 }
992 ++class_def_method_index;
993 it.Next();
994 }
995 while (it.HasNextVirtualMethod()) {
996 if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) {
997 return false;
998 }
999 ++class_def_method_index;
1000 it.Next();
1001 }
1002 }
1003 if (UNLIKELY(!visitor->EndClass())) {
1004 return false;
1005 }
1006 }
1007 }
1008 return true;
1009}
1010
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001011size_t OatWriter::InitOatHeader() {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001012 oat_header_ = OatHeader::Create(compiler_driver_->GetInstructionSet(),
1013 compiler_driver_->GetInstructionSetFeatures(),
1014 dex_files_,
1015 image_file_location_oat_checksum_,
1016 image_file_location_oat_begin_,
1017 key_value_store_);
1018
1019 return oat_header_->GetHeaderSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001020}
1021
1022size_t OatWriter::InitOatDexFiles(size_t offset) {
1023 // create the OatDexFiles
1024 for (size_t i = 0; i != dex_files_->size(); ++i) {
1025 const DexFile* dex_file = (*dex_files_)[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001026 CHECK(dex_file != nullptr);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001027 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001028 oat_dex_files_.push_back(oat_dex_file);
1029 offset += oat_dex_file->SizeOf();
1030 }
1031 return offset;
1032}
1033
Brian Carlstrom89521892011-12-07 22:05:07 -08001034size_t OatWriter::InitDexFiles(size_t offset) {
1035 // calculate the offsets within OatDexFiles to the DexFiles
1036 for (size_t i = 0; i != dex_files_->size(); ++i) {
1037 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001038 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -08001039 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001040 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -08001041
1042 // set offset in OatDexFile to DexFile
1043 oat_dex_files_[i]->dex_file_offset_ = offset;
1044
1045 const DexFile* dex_file = (*dex_files_)[i];
1046 offset += dex_file->GetHeader().file_size_;
1047 }
1048 return offset;
1049}
1050
Brian Carlstrom389efb02012-01-11 12:06:26 -08001051size_t OatWriter::InitOatClasses(size_t offset) {
Brian Carlstrom389efb02012-01-11 12:06:26 -08001052 // calculate the offsets within OatDexFiles to OatClasses
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001053 InitOatClassesMethodVisitor visitor(this, offset);
1054 bool success = VisitDexMethods(&visitor);
1055 CHECK(success);
1056 offset = visitor.GetOffset();
Brian Carlstromba150c32013-08-27 17:31:03 -07001057
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001058 // Update oat_dex_files_.
1059 auto oat_class_it = oat_classes_.begin();
1060 for (OatDexFile* oat_dex_file : oat_dex_files_) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001061 for (uint32_t& method_offset : oat_dex_file->methods_offsets_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001062 DCHECK(oat_class_it != oat_classes_.end());
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001063 method_offset = (*oat_class_it)->offset_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001064 ++oat_class_it;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001065 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001066 oat_dex_file->UpdateChecksum(oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001067 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001068 CHECK(oat_class_it == oat_classes_.end());
1069
1070 return offset;
1071}
1072
1073size_t OatWriter::InitOatMaps(size_t offset) {
1074 #define VISIT(VisitorType) \
1075 do { \
1076 VisitorType visitor(this, offset); \
1077 bool success = VisitDexMethods(&visitor); \
1078 DCHECK(success); \
1079 offset = visitor.GetOffset(); \
1080 } while (false)
1081
1082 VISIT(InitMapMethodVisitor<GcMapDataAccess>);
1083 VISIT(InitMapMethodVisitor<MappingTableDataAccess>);
1084 VISIT(InitMapMethodVisitor<VmapTableDataAccess>);
1085
1086 #undef VISIT
1087
Brian Carlstrome24fa612011-09-29 00:53:55 -07001088 return offset;
1089}
1090
1091size_t OatWriter::InitOatCode(size_t offset) {
1092 // calculate the offsets within OatHeader to executable code
1093 size_t old_offset = offset;
Dave Allison50abf0a2014-06-23 13:19:59 -07001094 size_t adjusted_offset = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001095 // required to be on a new page boundary
1096 offset = RoundUp(offset, kPageSize);
1097 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001098 size_executable_offset_alignment_ = offset - old_offset;
1099 if (compiler_driver_->IsImage()) {
Alex Lighta59dd802014-07-02 16:28:08 -07001100 CHECK_EQ(image_patch_delta_, 0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001101 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001102
Ian Rogers848871b2013-08-05 10:56:33 -07001103 #define DO_TRAMPOLINE(field, fn_name) \
1104 offset = CompiledCode::AlignCode(offset, instruction_set); \
Dave Allison50abf0a2014-06-23 13:19:59 -07001105 adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \
1106 oat_header_->Set ## fn_name ## Offset(adjusted_offset); \
Ian Rogers848871b2013-08-05 10:56:33 -07001107 field.reset(compiler_driver_->Create ## fn_name()); \
1108 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001109
Ian Rogers848871b2013-08-05 10:56:33 -07001110 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Andreas Gampe2da88232014-02-27 12:26:20 -08001111 DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline);
Jeff Hao88474b42013-10-23 16:24:40 -07001112 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -07001113 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
1114 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001115
Ian Rogers848871b2013-08-05 10:56:33 -07001116 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001117 } else {
Ian Rogers848871b2013-08-05 10:56:33 -07001118 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
1119 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
1120 oat_header_->SetJniDlsymLookupOffset(0);
Andreas Gampe2da88232014-02-27 12:26:20 -08001121 oat_header_->SetQuickGenericJniTrampolineOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -07001122 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001123 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -07001124 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Alex Lighta59dd802014-07-02 16:28:08 -07001125 oat_header_->SetImagePatchDelta(image_patch_delta_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001126 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001127 return offset;
1128}
1129
1130size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001131 #define VISIT(VisitorType) \
1132 do { \
1133 VisitorType visitor(this, offset); \
1134 bool success = VisitDexMethods(&visitor); \
1135 DCHECK(success); \
1136 offset = visitor.GetOffset(); \
1137 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001138
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001139 VISIT(InitCodeMethodVisitor);
Andreas Gampe9edb5b12015-09-21 11:46:52 -07001140 if (compiler_driver_->IsImage()) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001141 VISIT(InitImageMethodVisitor);
Ian Rogers0571d352011-11-03 19:51:38 -07001142 }
Logan Chien8b977d32012-02-21 19:14:55 +08001143
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001144 #undef VISIT
1145
Brian Carlstrome24fa612011-09-29 00:53:55 -07001146 return offset;
1147}
1148
David Srbeckybc90fd02015-04-22 19:40:27 +01001149bool OatWriter::WriteRodata(OutputStream* out) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001150 const off_t raw_file_offset = out->Seek(0, kSeekCurrent);
1151 if (raw_file_offset == (off_t) -1) {
1152 LOG(ERROR) << "Failed to get file offset in " << out->GetLocation();
1153 return false;
1154 }
1155 const size_t file_offset = static_cast<size_t>(raw_file_offset);
David Srbeckybc90fd02015-04-22 19:40:27 +01001156 oat_data_offset_ = file_offset;
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001157
Vladimir Markof4da6752014-08-01 19:04:18 +01001158 // Reserve space for header. It will be written last - after updating the checksum.
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001159 size_t header_size = oat_header_->GetHeaderSize();
Vladimir Markof4da6752014-08-01 19:04:18 +01001160 if (out->Seek(header_size, kSeekCurrent) == (off_t) -1) {
1161 PLOG(ERROR) << "Failed to reserve space for oat header in " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001162 return false;
1163 }
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001164 size_oat_header_ += sizeof(OatHeader);
1165 size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001166
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001167 if (!WriteTables(out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001168 LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001169 return false;
1170 }
1171
Vladimir Markof4da6752014-08-01 19:04:18 +01001172 off_t tables_end_offset = out->Seek(0, kSeekCurrent);
1173 if (tables_end_offset == (off_t) -1) {
1174 LOG(ERROR) << "Failed to seek to oat code position in " << out->GetLocation();
1175 return false;
1176 }
1177 size_t relative_offset = static_cast<size_t>(tables_end_offset) - file_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001178 relative_offset = WriteMaps(out, file_offset, relative_offset);
1179 if (relative_offset == 0) {
1180 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
1181 return false;
1182 }
1183
David Srbeckybc90fd02015-04-22 19:40:27 +01001184 // Write padding.
1185 off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent);
1186 relative_offset += size_executable_offset_alignment_;
1187 DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset());
1188 size_t expected_file_offset = file_offset + relative_offset;
1189 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
1190 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
1191 << " Expected: " << expected_file_offset << " File: " << out->GetLocation();
1192 return 0;
1193 }
1194 DCHECK_OFFSET();
1195
1196 return true;
1197}
1198
1199bool OatWriter::WriteCode(OutputStream* out) {
1200 size_t header_size = oat_header_->GetHeaderSize();
1201 const size_t file_offset = oat_data_offset_;
1202 size_t relative_offset = oat_header_->GetExecutableOffset();
1203 DCHECK_OFFSET();
1204
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001205 relative_offset = WriteCode(out, file_offset, relative_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001206 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001207 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001208 return false;
1209 }
1210
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001211 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
1212 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001213 LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001214 return false;
1215 }
1216
Vladimir Markof4da6752014-08-01 19:04:18 +01001217 const off_t oat_end_file_offset = out->Seek(0, kSeekCurrent);
1218 if (oat_end_file_offset == (off_t) -1) {
1219 LOG(ERROR) << "Failed to get oat end file offset in " << out->GetLocation();
1220 return false;
1221 }
1222
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001223 if (kIsDebugBuild) {
1224 uint32_t size_total = 0;
1225 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001226 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001227 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001228
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001229 DO_STAT(size_dex_file_alignment_);
1230 DO_STAT(size_executable_offset_alignment_);
1231 DO_STAT(size_oat_header_);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001232 DO_STAT(size_oat_header_key_value_store_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001233 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -07001234 DO_STAT(size_interpreter_to_interpreter_bridge_);
1235 DO_STAT(size_interpreter_to_compiled_code_bridge_);
1236 DO_STAT(size_jni_dlsym_lookup_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001237 DO_STAT(size_quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001238 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001239 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001240 DO_STAT(size_quick_to_interpreter_bridge_);
1241 DO_STAT(size_trampoline_alignment_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001242 DO_STAT(size_method_header_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001243 DO_STAT(size_code_);
1244 DO_STAT(size_code_alignment_);
Vladimir Markof4da6752014-08-01 19:04:18 +01001245 DO_STAT(size_relative_call_thunks_);
Vladimir Markoc74658b2015-03-31 10:26:41 +01001246 DO_STAT(size_misc_thunks_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001247 DO_STAT(size_mapping_table_);
1248 DO_STAT(size_vmap_table_);
1249 DO_STAT(size_gc_map_);
1250 DO_STAT(size_oat_dex_file_location_size_);
1251 DO_STAT(size_oat_dex_file_location_data_);
1252 DO_STAT(size_oat_dex_file_location_checksum_);
1253 DO_STAT(size_oat_dex_file_offset_);
1254 DO_STAT(size_oat_dex_file_methods_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001255 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001256 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001257 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001258 DO_STAT(size_oat_class_method_offsets_);
1259 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001260
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001261 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Vladimir Markof4da6752014-08-01 19:04:18 +01001262 CHECK_EQ(file_offset + size_total, static_cast<size_t>(oat_end_file_offset));
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001263 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001264 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001265
Vladimir Markof4da6752014-08-01 19:04:18 +01001266 CHECK_EQ(file_offset + size_, static_cast<size_t>(oat_end_file_offset));
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001267 CHECK_EQ(size_, relative_offset);
1268
Vladimir Markof4da6752014-08-01 19:04:18 +01001269 // Write the header now that the checksum is final.
1270 if (out->Seek(file_offset, kSeekSet) == (off_t) -1) {
1271 PLOG(ERROR) << "Failed to seek to oat header position in " << out->GetLocation();
1272 return false;
1273 }
David Srbeckybc90fd02015-04-22 19:40:27 +01001274 DCHECK_EQ(file_offset, static_cast<size_t>(out->Seek(0, kSeekCurrent)));
Vladimir Markof4da6752014-08-01 19:04:18 +01001275 if (!out->WriteFully(oat_header_, header_size)) {
1276 PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation();
1277 return false;
1278 }
1279 if (out->Seek(oat_end_file_offset, kSeekSet) == (off_t) -1) {
1280 PLOG(ERROR) << "Failed to seek to end after writing oat header to " << out->GetLocation();
1281 return false;
1282 }
1283 DCHECK_EQ(oat_end_file_offset, out->Seek(0, kSeekCurrent));
1284
Brian Carlstrome24fa612011-09-29 00:53:55 -07001285 return true;
1286}
1287
Ian Rogers3d504072014-03-01 09:16:49 -08001288bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001289 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001290 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001291 PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001292 return false;
1293 }
1294 }
Brian Carlstrom89521892011-12-07 22:05:07 -08001295 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001296 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Ian Rogers3d504072014-03-01 09:16:49 -08001297 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -08001298 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
1299 const DexFile* dex_file = (*dex_files_)[i];
1300 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
1301 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
1302 return false;
1303 }
1304 const DexFile* dex_file = (*dex_files_)[i];
Ian Rogers3d504072014-03-01 09:16:49 -08001305 if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001306 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
Ian Rogers3d504072014-03-01 09:16:49 -08001307 << " to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001308 return false;
1309 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001310 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -08001311 }
Brian Carlstrom389efb02012-01-11 12:06:26 -08001312 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001313 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001314 PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001315 return false;
1316 }
1317 }
1318 return true;
1319}
1320
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001321size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) {
1322 #define VISIT(VisitorType) \
1323 do { \
1324 VisitorType visitor(this, out, file_offset, relative_offset); \
1325 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1326 return 0; \
1327 } \
1328 relative_offset = visitor.GetOffset(); \
1329 } while (false)
1330
1331 size_t gc_maps_offset = relative_offset;
1332 VISIT(WriteMapMethodVisitor<GcMapDataAccess>);
1333 size_gc_map_ = relative_offset - gc_maps_offset;
1334
1335 size_t mapping_tables_offset = relative_offset;
1336 VISIT(WriteMapMethodVisitor<MappingTableDataAccess>);
1337 size_mapping_table_ = relative_offset - mapping_tables_offset;
1338
1339 size_t vmap_tables_offset = relative_offset;
1340 VISIT(WriteMapMethodVisitor<VmapTableDataAccess>);
1341 size_vmap_table_ = relative_offset - vmap_tables_offset;
1342
1343 #undef VISIT
1344
1345 return relative_offset;
1346}
1347
1348size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001349 if (compiler_driver_->IsImage()) {
1350 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001351
Ian Rogers848871b2013-08-05 10:56:33 -07001352 #define DO_TRAMPOLINE(field) \
1353 do { \
1354 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
1355 uint32_t alignment_padding = aligned_offset - relative_offset; \
Ian Rogers3d504072014-03-01 09:16:49 -08001356 out->Seek(alignment_padding, kSeekCurrent); \
Ian Rogers848871b2013-08-05 10:56:33 -07001357 size_trampoline_alignment_ += alignment_padding; \
Ian Rogers3d504072014-03-01 09:16:49 -08001358 if (!out->WriteFully(&(*field)[0], field->size())) { \
1359 PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \
Ian Rogers848871b2013-08-05 10:56:33 -07001360 return false; \
1361 } \
1362 size_ ## field += field->size(); \
1363 relative_offset += alignment_padding + field->size(); \
1364 DCHECK_OFFSET(); \
1365 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001366
Ian Rogers848871b2013-08-05 10:56:33 -07001367 DO_TRAMPOLINE(jni_dlsym_lookup_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001368 DO_TRAMPOLINE(quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001369 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001370 DO_TRAMPOLINE(quick_resolution_trampoline_);
1371 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
1372 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001373 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001374 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001375}
1376
Ian Rogers3d504072014-03-01 09:16:49 -08001377size_t OatWriter::WriteCodeDexFiles(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001378 const size_t file_offset,
1379 size_t relative_offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001380 #define VISIT(VisitorType) \
1381 do { \
1382 VisitorType visitor(this, out, file_offset, relative_offset); \
1383 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1384 return 0; \
1385 } \
1386 relative_offset = visitor.GetOffset(); \
1387 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001388
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001389 VISIT(WriteCodeMethodVisitor);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001390
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001391 #undef VISIT
Brian Carlstrom265091e2013-01-30 14:08:26 -08001392
Vladimir Markob163bb72015-03-31 21:49:49 +01001393 size_code_alignment_ += relative_patcher_->CodeAlignmentSize();
1394 size_relative_call_thunks_ += relative_patcher_->RelativeCallThunksSize();
1395 size_misc_thunks_ += relative_patcher_->MiscThunksSize();
1396
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001397 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001398}
1399
Vladimir Markof4da6752014-08-01 19:04:18 +01001400bool OatWriter::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) {
1401 static const uint8_t kPadding[] = {
1402 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
1403 };
1404 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
1405 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
1406 return false;
1407 }
1408 size_code_alignment_ += aligned_code_delta;
1409 return true;
1410}
1411
Vladimir Markob163bb72015-03-31 21:49:49 +01001412std::pair<bool, uint32_t> OatWriter::MethodOffsetMap::FindMethodOffset(MethodReference ref) {
1413 auto it = map.find(ref);
1414 if (it == map.end()) {
1415 return std::pair<bool, uint32_t>(false, 0u);
1416 } else {
1417 return std::pair<bool, uint32_t>(true, it->second);
1418 }
1419}
1420
Brian Carlstrom265091e2013-01-30 14:08:26 -08001421OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
1422 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -08001423 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001424 dex_file_location_size_ = location.size();
1425 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001426 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -08001427 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001428 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001429}
1430
1431size_t OatWriter::OatDexFile::SizeOf() const {
1432 return sizeof(dex_file_location_size_)
1433 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001434 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -08001435 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001436 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001437}
1438
Ian Rogers3d504072014-03-01 09:16:49 -08001439void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const {
1440 oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
1441 oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
1442 oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
1443 oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
1444 oat_header->UpdateChecksum(&methods_offsets_[0],
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001445 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001446}
1447
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001448bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001449 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001450 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001451 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001452 if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
1453 PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001454 return false;
1455 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001456 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001457 if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
1458 PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001459 return false;
1460 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001461 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Ian Rogers3d504072014-03-01 09:16:49 -08001462 if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
1463 PLOG(ERROR) << "Failed to write dex file location checksum 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_checksum_ += sizeof(dex_file_location_checksum_);
Ian Rogers3d504072014-03-01 09:16:49 -08001467 if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
1468 PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001469 return false;
1470 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001471 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Ian Rogers3d504072014-03-01 09:16:49 -08001472 if (!out->WriteFully(&methods_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001473 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001474 PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001475 return false;
1476 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001477 oat_writer->size_oat_dex_file_methods_offsets_ +=
1478 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001479 return true;
1480}
1481
Brian Carlstromba150c32013-08-27 17:31:03 -07001482OatWriter::OatClass::OatClass(size_t offset,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001483 const std::vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -07001484 uint32_t num_non_null_compiled_methods,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001485 mirror::Class::Status status)
1486 : compiled_methods_(compiled_methods) {
1487 uint32_t num_methods = compiled_methods.size();
Brian Carlstromba150c32013-08-27 17:31:03 -07001488 CHECK_LE(num_non_null_compiled_methods, num_methods);
1489
Brian Carlstrom265091e2013-01-30 14:08:26 -08001490 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -07001491 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
1492
1493 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
1494 // apply when there are 0 methods, we just arbitrarily say that 0
1495 // methods means kOatClassNoneCompiled and that we won't use
1496 // kOatClassAllCompiled unless there is at least one compiled
1497 // method. This means in an interpretter only system, we can assert
1498 // that all classes are kOatClassNoneCompiled.
1499 if (num_non_null_compiled_methods == 0) {
1500 type_ = kOatClassNoneCompiled;
1501 } else if (num_non_null_compiled_methods == num_methods) {
1502 type_ = kOatClassAllCompiled;
1503 } else {
1504 type_ = kOatClassSomeCompiled;
1505 }
1506
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001507 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -07001508 method_offsets_.resize(num_non_null_compiled_methods);
Vladimir Marko8a630572014-04-09 18:45:35 +01001509 method_headers_.resize(num_non_null_compiled_methods);
Brian Carlstromba150c32013-08-27 17:31:03 -07001510
1511 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
1512 if (type_ == kOatClassSomeCompiled) {
1513 method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator());
1514 method_bitmap_size_ = method_bitmap_->GetSizeOf();
1515 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
1516 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
1517 } else {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001518 method_bitmap_ = nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001519 method_bitmap_size_ = 0;
1520 }
1521
1522 for (size_t i = 0; i < num_methods; i++) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001523 CompiledMethod* compiled_method = compiled_methods_[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001524 if (compiled_method == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001525 oat_method_offsets_offsets_from_oat_class_[i] = 0;
1526 } else {
1527 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
1528 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
1529 if (type_ == kOatClassSomeCompiled) {
1530 method_bitmap_->SetBit(i);
1531 }
1532 }
1533 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001534}
1535
Brian Carlstromba150c32013-08-27 17:31:03 -07001536OatWriter::OatClass::~OatClass() {
Mathieu Chartier661974a2014-01-09 11:23:53 -08001537 delete method_bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -07001538}
1539
Brian Carlstrom265091e2013-01-30 14:08:26 -08001540size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
1541 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001542 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
1543 if (method_offset == 0) {
1544 return 0;
1545 }
1546 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001547}
1548
1549size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
1550 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001551 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08001552}
1553
1554size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001555 return sizeof(status_)
1556 + sizeof(type_)
1557 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
1558 + method_bitmap_size_
1559 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001560}
1561
Ian Rogers3d504072014-03-01 09:16:49 -08001562void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const {
1563 oat_header->UpdateChecksum(&status_, sizeof(status_));
1564 oat_header->UpdateChecksum(&type_, sizeof(type_));
Brian Carlstromba150c32013-08-27 17:31:03 -07001565 if (method_bitmap_size_ != 0) {
1566 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001567 oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_));
1568 oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001569 }
Ian Rogers3d504072014-03-01 09:16:49 -08001570 oat_header->UpdateChecksum(&method_offsets_[0],
1571 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001572}
1573
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001574bool OatWriter::OatClass::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001575 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001576 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001577 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001578 if (!out->WriteFully(&status_, sizeof(status_))) {
1579 PLOG(ERROR) << "Failed to write class status to " << out->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001580 return false;
1581 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001582 oat_writer->size_oat_class_status_ += sizeof(status_);
Ian Rogers3d504072014-03-01 09:16:49 -08001583 if (!out->WriteFully(&type_, sizeof(type_))) {
1584 PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001585 return false;
1586 }
1587 oat_writer->size_oat_class_type_ += sizeof(type_);
1588 if (method_bitmap_size_ != 0) {
1589 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001590 if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
1591 PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001592 return false;
1593 }
1594 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001595 if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
1596 PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001597 return false;
1598 }
1599 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1600 }
Ian Rogers3d504072014-03-01 09:16:49 -08001601 if (!out->WriteFully(&method_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001602 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001603 PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001604 return false;
1605 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001606 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001607 return true;
1608}
1609
Brian Carlstrome24fa612011-09-29 00:53:55 -07001610} // namespace art