blob: d2a0f4fec4d74fc83c0934f0c303ac490748f9f8 [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"
Ian Rogerse77493c2014-08-20 15:08:45 -070022#include "base/allocator.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070023#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080024#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080025#include "base/unix_file/fd_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070026#include "class_linker.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070027#include "compiled_class.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000028#include "compiled_method.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "dex_file-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000030#include "dex/verification_results.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000031#include "driver/compiler_driver.h"
32#include "driver/compiler_options.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070033#include "gc/space/space.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010034#include "image_writer.h"
Vladimir Markob163bb72015-03-31 21:49:49 +010035#include "linker/relative_patcher.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070036#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/array.h"
38#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070039#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070040#include "os.h"
Brian Carlstromcd60ac72013-01-20 17:09:51 -080041#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070042#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070043#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070044#include "handle_scope-inl.h"
jeffhaoec014232012-09-05 10:42:25 -070045#include "verifier/method_verifier.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070046
47namespace art {
48
Vladimir Marko96c6ab92014-04-08 14:00:50 +010049#define DCHECK_OFFSET() \
50 DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out->Seek(0, kSeekCurrent)) \
51 << "file_offset=" << file_offset << " relative_offset=" << relative_offset
52
53#define DCHECK_OFFSET_() \
54 DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out->Seek(0, kSeekCurrent)) \
55 << "file_offset=" << file_offset << " offset_=" << offset_
56
Brian Carlstrom3320cf42011-10-04 14:58:28 -070057OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070058 uint32_t image_file_location_oat_checksum,
Ian Rogersef7d42f2014-01-06 12:55:46 -080059 uintptr_t image_file_location_oat_begin,
Alex Lighta59dd802014-07-02 16:28:08 -070060 int32_t image_patch_delta,
Ian Rogersca368cb2013-11-15 15:52:08 -080061 const CompilerDriver* compiler,
Vladimir Markof4da6752014-08-01 19:04:18 +010062 ImageWriter* image_writer,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070063 TimingLogger* timings,
64 SafeMap<std::string, std::string>* key_value_store)
Jeff Hao0aba0ba2013-06-03 14:49:28 -070065 : compiler_driver_(compiler),
Vladimir Markof4da6752014-08-01 19:04:18 +010066 image_writer_(image_writer),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070067 dex_files_(&dex_files),
Vladimir Markof4da6752014-08-01 19:04:18 +010068 size_(0u),
Vladimir Marko5c42c292015-02-25 12:02:49 +000069 bss_size_(0u),
Vladimir Markof4da6752014-08-01 19:04:18 +010070 oat_data_offset_(0u),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070071 image_file_location_oat_checksum_(image_file_location_oat_checksum),
72 image_file_location_oat_begin_(image_file_location_oat_begin),
Alex Lighta59dd802014-07-02 16:28:08 -070073 image_patch_delta_(image_patch_delta),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070074 key_value_store_(key_value_store),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070075 oat_header_(NULL),
76 size_dex_file_alignment_(0),
77 size_executable_offset_alignment_(0),
78 size_oat_header_(0),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070079 size_oat_header_key_value_store_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070080 size_dex_file_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070081 size_interpreter_to_interpreter_bridge_(0),
82 size_interpreter_to_compiled_code_bridge_(0),
83 size_jni_dlsym_lookup_(0),
Andreas Gampe2da88232014-02-27 12:26:20 -080084 size_quick_generic_jni_trampoline_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070085 size_quick_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070086 size_quick_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070087 size_quick_to_interpreter_bridge_(0),
88 size_trampoline_alignment_(0),
Vladimir Marko96c6ab92014-04-08 14:00:50 +010089 size_method_header_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070090 size_code_(0),
91 size_code_alignment_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +010092 size_relative_call_thunks_(0),
Vladimir Markoc74658b2015-03-31 10:26:41 +010093 size_misc_thunks_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070094 size_mapping_table_(0),
95 size_vmap_table_(0),
96 size_gc_map_(0),
97 size_oat_dex_file_location_size_(0),
98 size_oat_dex_file_location_data_(0),
99 size_oat_dex_file_location_checksum_(0),
100 size_oat_dex_file_offset_(0),
101 size_oat_dex_file_methods_offsets_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -0700102 size_oat_class_type_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700103 size_oat_class_status_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -0700104 size_oat_class_method_bitmaps_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +0100105 size_oat_class_method_offsets_(0),
106 method_offset_map_() {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700107 CHECK(key_value_store != nullptr);
108
Vladimir Markob163bb72015-03-31 21:49:49 +0100109 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
110 const InstructionSetFeatures* features = compiler_driver_->GetInstructionSetFeatures();
111 relative_patcher_ = linker::RelativePatcher::Create(instruction_set, features,
112 &method_offset_map_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100113
Ian Rogersca368cb2013-11-15 15:52:08 -0800114 size_t offset;
115 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700116 TimingLogger::ScopedTiming split("InitOatHeader", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800117 offset = InitOatHeader();
118 }
119 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700120 TimingLogger::ScopedTiming split("InitOatDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800121 offset = InitOatDexFiles(offset);
122 }
123 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700124 TimingLogger::ScopedTiming split("InitDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800125 offset = InitDexFiles(offset);
126 }
127 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700128 TimingLogger::ScopedTiming split("InitOatClasses", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800129 offset = InitOatClasses(offset);
130 }
131 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700132 TimingLogger::ScopedTiming split("InitOatMaps", timings);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100133 offset = InitOatMaps(offset);
134 }
135 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700136 TimingLogger::ScopedTiming split("InitOatCode", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800137 offset = InitOatCode(offset);
138 }
139 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700140 TimingLogger::ScopedTiming split("InitOatCodeDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800141 offset = InitOatCodeDexFiles(offset);
142 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700143 size_ = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700144
145 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Vladimir Markof4da6752014-08-01 19:04:18 +0100146 CHECK_EQ(compiler->IsImage(), image_writer_ != nullptr);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700147 CHECK_EQ(compiler->IsImage(),
148 key_value_store_->find(OatHeader::kImageLocationKey) == key_value_store_->end());
Alex Lighta59dd802014-07-02 16:28:08 -0700149 CHECK_ALIGNED(image_patch_delta_, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700150}
151
Ian Rogers0571d352011-11-03 19:51:38 -0700152OatWriter::~OatWriter() {
153 delete oat_header_;
154 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800155 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -0700156}
157
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100158struct OatWriter::GcMapDataAccess {
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800159 static const SwapVector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100160 return compiled_method->GetGcMap();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100161 }
162
163 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800164 uint32_t offset = oat_class->method_headers_[method_offsets_index].gc_map_offset_;
165 return offset == 0u ? 0u :
166 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100167 }
168
169 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
170 ALWAYS_INLINE {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800171 oat_class->method_headers_[method_offsets_index].gc_map_offset_ =
172 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100173 }
174
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800175 static const char* Name() {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100176 return "GC map";
177 }
178};
179
180struct OatWriter::MappingTableDataAccess {
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800181 static const SwapVector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000182 return compiled_method->GetMappingTable();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100183 }
184
185 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100186 uint32_t offset = oat_class->method_headers_[method_offsets_index].mapping_table_offset_;
187 return offset == 0u ? 0u :
188 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100189 }
190
191 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
192 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100193 oat_class->method_headers_[method_offsets_index].mapping_table_offset_ =
194 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100195 }
196
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800197 static const char* Name() {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100198 return "mapping table";
199 }
200};
201
202struct OatWriter::VmapTableDataAccess {
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800203 static const SwapVector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800204 return compiled_method->GetVmapTable();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100205 }
206
207 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100208 uint32_t offset = oat_class->method_headers_[method_offsets_index].vmap_table_offset_;
209 return offset == 0u ? 0u :
210 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100211 }
212
213 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
214 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100215 oat_class->method_headers_[method_offsets_index].vmap_table_offset_ =
216 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100217 }
218
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800219 static const char* Name() {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100220 return "vmap table";
221 }
222};
223
224class OatWriter::DexMethodVisitor {
225 public:
226 DexMethodVisitor(OatWriter* writer, size_t offset)
227 : writer_(writer),
228 offset_(offset),
229 dex_file_(nullptr),
230 class_def_index_(DexFile::kDexNoIndex) {
231 }
232
233 virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) {
234 DCHECK(dex_file_ == nullptr);
235 DCHECK_EQ(class_def_index_, DexFile::kDexNoIndex);
236 dex_file_ = dex_file;
237 class_def_index_ = class_def_index;
238 return true;
239 }
240
241 virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0;
242
243 virtual bool EndClass() {
244 if (kIsDebugBuild) {
245 dex_file_ = nullptr;
246 class_def_index_ = DexFile::kDexNoIndex;
247 }
248 return true;
249 }
250
251 size_t GetOffset() const {
252 return offset_;
253 }
254
255 protected:
256 virtual ~DexMethodVisitor() { }
257
258 OatWriter* const writer_;
259
260 // The offset is usually advanced for each visited method by the derived class.
261 size_t offset_;
262
263 // The dex file and class def index are set in StartClass().
264 const DexFile* dex_file_;
265 size_t class_def_index_;
266};
267
268class OatWriter::OatDexMethodVisitor : public DexMethodVisitor {
269 public:
270 OatDexMethodVisitor(OatWriter* writer, size_t offset)
271 : DexMethodVisitor(writer, offset),
272 oat_class_index_(0u),
273 method_offsets_index_(0u) {
274 }
275
276 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
277 DexMethodVisitor::StartClass(dex_file, class_def_index);
278 DCHECK_LT(oat_class_index_, writer_->oat_classes_.size());
279 method_offsets_index_ = 0u;
280 return true;
281 }
282
283 bool EndClass() {
284 ++oat_class_index_;
285 return DexMethodVisitor::EndClass();
286 }
287
288 protected:
289 size_t oat_class_index_;
290 size_t method_offsets_index_;
291};
292
293class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor {
294 public:
295 InitOatClassesMethodVisitor(OatWriter* writer, size_t offset)
296 : DexMethodVisitor(writer, offset),
297 compiled_methods_(),
298 num_non_null_compiled_methods_(0u) {
299 compiled_methods_.reserve(256u);
300 }
301
302 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
303 DexMethodVisitor::StartClass(dex_file, class_def_index);
304 compiled_methods_.clear();
305 num_non_null_compiled_methods_ = 0u;
306 return true;
307 }
308
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700309 bool VisitMethod(size_t class_def_method_index ATTRIBUTE_UNUSED, const ClassDataItemIterator& it) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100310 // Fill in the compiled_methods_ array for methods that have a
311 // CompiledMethod. We track the number of non-null entries in
312 // num_non_null_compiled_methods_ since we only want to allocate
313 // OatMethodOffsets for the compiled methods.
314 uint32_t method_idx = it.GetMemberIndex();
315 CompiledMethod* compiled_method =
316 writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx));
317 compiled_methods_.push_back(compiled_method);
318 if (compiled_method != nullptr) {
319 ++num_non_null_compiled_methods_;
320 }
321 return true;
322 }
323
324 bool EndClass() {
325 ClassReference class_ref(dex_file_, class_def_index_);
326 CompiledClass* compiled_class = writer_->compiler_driver_->GetCompiledClass(class_ref);
327 mirror::Class::Status status;
328 if (compiled_class != NULL) {
329 status = compiled_class->GetStatus();
330 } else if (writer_->compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) {
331 status = mirror::Class::kStatusError;
332 } else {
333 status = mirror::Class::kStatusNotReady;
334 }
335
336 OatClass* oat_class = new OatClass(offset_, compiled_methods_,
337 num_non_null_compiled_methods_, status);
338 writer_->oat_classes_.push_back(oat_class);
Vladimir Markof4da6752014-08-01 19:04:18 +0100339 oat_class->UpdateChecksum(writer_->oat_header_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100340 offset_ += oat_class->SizeOf();
341 return DexMethodVisitor::EndClass();
342 }
343
344 private:
345 std::vector<CompiledMethod*> compiled_methods_;
346 size_t num_non_null_compiled_methods_;
347};
348
349class OatWriter::InitCodeMethodVisitor : public OatDexMethodVisitor {
350 public:
351 InitCodeMethodVisitor(OatWriter* writer, size_t offset)
352 : OatDexMethodVisitor(writer, offset) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100353 writer_->absolute_patch_locations_.reserve(
354 writer_->compiler_driver_->GetNonRelativeLinkerPatchCount());
355 }
356
357 bool EndClass() {
358 OatDexMethodVisitor::EndClass();
359 if (oat_class_index_ == writer_->oat_classes_.size()) {
Vladimir Marko20f85592015-03-19 10:07:02 +0000360 offset_ = writer_->relative_patcher_->ReserveSpace(offset_, nullptr);
Vladimir Markof4da6752014-08-01 19:04:18 +0100361 }
362 return true;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100363 }
364
365 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
366 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
367 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
368 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
369
370 if (compiled_method != nullptr) {
371 // Derived from CompiledMethod.
372 uint32_t quick_code_offset = 0;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100373
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800374 const SwapVector<uint8_t>* quick_code = compiled_method->GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -0800375 CHECK(quick_code != nullptr);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800376 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
377 CHECK_NE(code_size, 0U);
378 uint32_t thumb_offset = compiled_method->CodeDelta();
Elliott Hughes956af0f2014-12-11 14:34:28 -0800379
380 // Deduplicate code arrays.
Vladimir Markoc74658b2015-03-31 10:26:41 +0100381 bool deduped = false;
Elliott Hughes956af0f2014-12-11 14:34:28 -0800382 auto lb = dedupe_map_.lower_bound(compiled_method);
383 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(compiled_method, lb->first)) {
384 quick_code_offset = lb->second;
385 deduped = true;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100386 } else {
Vladimir Markoc74658b2015-03-31 10:26:41 +0100387 offset_ = writer_->relative_patcher_->ReserveSpace(offset_, compiled_method);
388 offset_ = compiled_method->AlignCode(offset_);
389 DCHECK_ALIGNED_PARAM(offset_,
390 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
391 quick_code_offset = offset_ + sizeof(OatQuickMethodHeader) + thumb_offset;
Elliott Hughes956af0f2014-12-11 14:34:28 -0800392 dedupe_map_.PutBefore(lb, compiled_method, quick_code_offset);
393 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100394
Elliott Hughes956af0f2014-12-11 14:34:28 -0800395 MethodReference method_ref(dex_file_, it.GetMemberIndex());
Vladimir Markob163bb72015-03-31 21:49:49 +0100396 auto method_lb = writer_->method_offset_map_.map.lower_bound(method_ref);
397 if (method_lb != writer_->method_offset_map_.map.end() &&
398 !writer_->method_offset_map_.map.key_comp()(method_ref, method_lb->first)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800399 // TODO: Should this be a hard failure?
400 LOG(WARNING) << "Multiple definitions of "
401 << PrettyMethod(method_ref.dex_method_index, *method_ref.dex_file)
402 << ((method_lb->second != quick_code_offset) ? "; OFFSET MISMATCH" : "");
403 } else {
Vladimir Markob163bb72015-03-31 21:49:49 +0100404 writer_->method_offset_map_.map.PutBefore(method_lb, method_ref, quick_code_offset);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800405 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100406
Elliott Hughes956af0f2014-12-11 14:34:28 -0800407 // Update quick method header.
408 DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size());
409 OatQuickMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_];
410 uint32_t mapping_table_offset = method_header->mapping_table_offset_;
411 uint32_t vmap_table_offset = method_header->vmap_table_offset_;
412 uint32_t gc_map_offset = method_header->gc_map_offset_;
413 // The code offset was 0 when the mapping/vmap table offset was set, so it's set
414 // to 0-offset and we need to adjust it by code_offset.
415 uint32_t code_offset = quick_code_offset - thumb_offset;
416 if (mapping_table_offset != 0u) {
417 mapping_table_offset += code_offset;
418 DCHECK_LT(mapping_table_offset, code_offset);
419 }
420 if (vmap_table_offset != 0u) {
421 vmap_table_offset += code_offset;
422 DCHECK_LT(vmap_table_offset, code_offset);
423 }
424 if (gc_map_offset != 0u) {
425 gc_map_offset += code_offset;
426 DCHECK_LT(gc_map_offset, code_offset);
427 }
428 uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
429 uint32_t core_spill_mask = compiled_method->GetCoreSpillMask();
430 uint32_t fp_spill_mask = compiled_method->GetFpSpillMask();
431 *method_header = OatQuickMethodHeader(mapping_table_offset, vmap_table_offset,
432 gc_map_offset, frame_size_in_bytes, core_spill_mask,
433 fp_spill_mask, code_size);
Vladimir Marko7624d252014-05-02 14:40:15 +0100434
Elliott Hughes956af0f2014-12-11 14:34:28 -0800435 if (!deduped) {
436 // Update offsets. (Checksum is updated when writing.)
437 offset_ += sizeof(*method_header); // Method header is prepended before code.
438 offset_ += code_size;
439 // Record absolute patch locations.
440 if (!compiled_method->GetPatches().empty()) {
441 uintptr_t base_loc = offset_ - code_size - writer_->oat_header_->GetExecutableOffset();
442 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
Vladimir Marko20f85592015-03-19 10:07:02 +0000443 if (!patch.IsPcRelative()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800444 writer_->absolute_patch_locations_.push_back(base_loc + patch.LiteralOffset());
Vladimir Markof4da6752014-08-01 19:04:18 +0100445 }
446 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100447 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800448 }
Alex Light78382fa2014-06-06 15:45:32 -0700449
Elliott Hughes956af0f2014-12-11 14:34:28 -0800450 if (writer_->compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols()) {
451 // Record debug information for this function if we are doing that.
Alex Light78382fa2014-06-06 15:45:32 -0700452
Elliott Hughes956af0f2014-12-11 14:34:28 -0800453 std::string name = PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
454 if (deduped) {
455 // TODO We should place the DEDUPED tag on the first instance of a deduplicated symbol
456 // so that it will show up in a debuggerd crash report.
457 name += " [ DEDUPED ]";
Alex Light78382fa2014-06-06 15:45:32 -0700458 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800459
460 const uint32_t quick_code_start = quick_code_offset -
461 writer_->oat_header_->GetExecutableOffset();
462 const DexFile::CodeItem *code_item = it.GetMethodCodeItem();
463 writer_->method_info_.push_back(DebugInfo(name,
464 dex_file_->GetSourceFile(dex_file_->GetClassDef(class_def_index_)),
465 quick_code_start, quick_code_start + code_size,
466 code_item == nullptr ? nullptr : dex_file_->GetDebugInfoStream(code_item),
467 compiled_method));
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100468 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100469
470 if (kIsDebugBuild) {
471 // We expect GC maps except when the class hasn't been verified or the method is native.
472 const CompilerDriver* compiler_driver = writer_->compiler_driver_;
473 ClassReference class_ref(dex_file_, class_def_index_);
474 CompiledClass* compiled_class = compiler_driver->GetCompiledClass(class_ref);
475 mirror::Class::Status status;
476 if (compiled_class != NULL) {
477 status = compiled_class->GetStatus();
478 } else if (compiler_driver->GetVerificationResults()->IsClassRejected(class_ref)) {
479 status = mirror::Class::kStatusError;
480 } else {
481 status = mirror::Class::kStatusNotReady;
482 }
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800483 const SwapVector<uint8_t>* gc_map = compiled_method->GetGcMap();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100484 if (gc_map != nullptr) {
485 size_t gc_map_size = gc_map->size() * sizeof(gc_map[0]);
Andreas Gampe51829322014-08-25 15:05:04 -0700486 bool is_native = it.MemberIsNative();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100487 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
488 << gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
489 << (status < mirror::Class::kStatusVerified) << " " << status << " "
490 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
491 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100492 }
493
494 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
495 OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_];
496 offsets->code_offset_ = quick_code_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100497 ++method_offsets_index_;
498 }
499
500 return true;
501 }
502
503 private:
Vladimir Marko20f85592015-03-19 10:07:02 +0000504 struct CodeOffsetsKeyComparator {
505 bool operator()(const CompiledMethod* lhs, const CompiledMethod* rhs) const {
506 if (lhs->GetQuickCode() != rhs->GetQuickCode()) {
507 return lhs->GetQuickCode() < rhs->GetQuickCode();
508 }
509 // If the code is the same, all other fields are likely to be the same as well.
510 if (UNLIKELY(lhs->GetMappingTable() != rhs->GetMappingTable())) {
511 return lhs->GetMappingTable() < rhs->GetMappingTable();
512 }
513 if (UNLIKELY(lhs->GetVmapTable() != rhs->GetVmapTable())) {
514 return lhs->GetVmapTable() < rhs->GetVmapTable();
515 }
516 if (UNLIKELY(lhs->GetGcMap() != rhs->GetGcMap())) {
517 return lhs->GetGcMap() < rhs->GetGcMap();
518 }
519 const auto& lhs_patches = lhs->GetPatches();
520 const auto& rhs_patches = rhs->GetPatches();
521 if (UNLIKELY(lhs_patches.size() != rhs_patches.size())) {
522 return lhs_patches.size() < rhs_patches.size();
523 }
524 auto rit = rhs_patches.begin();
525 for (const LinkerPatch& lpatch : lhs_patches) {
526 if (UNLIKELY(!(lpatch == *rit))) {
527 return lpatch < *rit;
528 }
529 ++rit;
530 }
531 return false;
532 }
533 };
534
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100535 // Deduplication is already done on a pointer basis by the compiler driver,
536 // so we can simply compare the pointers to find out if things are duplicated.
Vladimir Marko8a630572014-04-09 18:45:35 +0100537 SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100538};
539
540template <typename DataAccess>
541class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor {
542 public:
543 InitMapMethodVisitor(OatWriter* writer, size_t offset)
544 : OatDexMethodVisitor(writer, offset) {
545 }
546
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700547 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it ATTRIBUTE_UNUSED)
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100548 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
549 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
550 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
551
552 if (compiled_method != nullptr) {
553 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
554 DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u);
555
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800556 const SwapVector<uint8_t>* map = DataAccess::GetData(compiled_method);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100557 uint32_t map_size = map == nullptr ? 0 : map->size() * sizeof((*map)[0]);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100558 if (map_size != 0u) {
Vladimir Markobd72fc12014-07-09 16:06:40 +0100559 auto lb = dedupe_map_.lower_bound(map);
560 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(map, lb->first)) {
561 DataAccess::SetOffset(oat_class, method_offsets_index_, lb->second);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100562 } else {
563 DataAccess::SetOffset(oat_class, method_offsets_index_, offset_);
Vladimir Markobd72fc12014-07-09 16:06:40 +0100564 dedupe_map_.PutBefore(lb, map, offset_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100565 offset_ += map_size;
566 writer_->oat_header_->UpdateChecksum(&(*map)[0], map_size);
567 }
568 }
569 ++method_offsets_index_;
570 }
571
572 return true;
573 }
574
575 private:
576 // Deduplication is already done on a pointer basis by the compiler driver,
577 // so we can simply compare the pointers to find out if things are duplicated.
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800578 SafeMap<const SwapVector<uint8_t>*, uint32_t> dedupe_map_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100579};
580
581class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor {
582 public:
583 InitImageMethodVisitor(OatWriter* writer, size_t offset)
Jeff Haoc7d11882015-02-03 15:08:39 -0800584 : OatDexMethodVisitor(writer, offset),
585 pointer_size_(GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet())) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100586 }
587
588 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
589 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
590 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
591 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
592
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800593 OatMethodOffsets offsets(0u);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100594 if (compiled_method != nullptr) {
595 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
596 offsets = oat_class->method_offsets_[method_offsets_index_];
597 ++method_offsets_index_;
598 }
599
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100600 ClassLinker* linker = Runtime::Current()->GetClassLinker();
601 InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_));
602 // Unchecked as we hold mutator_lock_ on entry.
603 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800604 StackHandleScope<1> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700605 Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache(*dex_file_)));
Vladimir Marko7624d252014-05-02 14:40:15 +0100606 mirror::ArtMethod* method = linker->ResolveMethod(*dex_file_, it.GetMemberIndex(), dex_cache,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700607 NullHandle<mirror::ClassLoader>(),
608 NullHandle<mirror::ArtMethod>(),
609 invoke_type);
Andreas Gamped9efea62014-07-21 22:56:08 -0700610 if (method == nullptr) {
611 LOG(ERROR) << "Unexpected failure to resolve a method: "
612 << PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
613 soa.Self()->AssertPendingException();
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000614 mirror::Throwable* exc = soa.Self()->GetException();
Andreas Gamped9efea62014-07-21 22:56:08 -0700615 std::string dump = exc->Dump();
616 LOG(FATAL) << dump;
617 }
Jeff Haoc7d11882015-02-03 15:08:39 -0800618 method->SetEntryPointFromQuickCompiledCodePtrSize(reinterpret_cast<void*>(offsets.code_offset_),
619 pointer_size_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100620
621 return true;
622 }
Jeff Haoc7d11882015-02-03 15:08:39 -0800623
624 protected:
625 const size_t pointer_size_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100626};
627
628class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor {
629 public:
630 WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
Vladimir Markof4da6752014-08-01 19:04:18 +0100631 size_t relative_offset) SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100632 : OatDexMethodVisitor(writer, relative_offset),
633 out_(out),
Vladimir Markof4da6752014-08-01 19:04:18 +0100634 file_offset_(file_offset),
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100635 soa_(Thread::Current()),
636 no_thread_suspension_(soa_.Self(), "OatWriter patching"),
Vladimir Markof4da6752014-08-01 19:04:18 +0100637 class_linker_(Runtime::Current()->GetClassLinker()),
638 dex_cache_(nullptr) {
639 if (writer_->image_writer_ != nullptr) {
640 // If we're creating the image, the address space must be ready so that we can apply patches.
641 CHECK(writer_->image_writer_->IsImageAddressSpaceReady());
642 patched_code_.reserve(16 * KB);
643 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100644 }
645
Vladimir Markof4da6752014-08-01 19:04:18 +0100646 ~WriteCodeMethodVisitor() UNLOCK_FUNCTION(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100647 }
648
649 bool StartClass(const DexFile* dex_file, size_t class_def_index)
650 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
651 OatDexMethodVisitor::StartClass(dex_file, class_def_index);
652 if (dex_cache_ == nullptr || dex_cache_->GetDexFile() != dex_file) {
653 dex_cache_ = class_linker_->FindDexCache(*dex_file);
654 }
655 return true;
656 }
657
658 bool EndClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
659 bool result = OatDexMethodVisitor::EndClass();
660 if (oat_class_index_ == writer_->oat_classes_.size()) {
661 DCHECK(result); // OatDexMethodVisitor::EndClass() never fails.
Vladimir Marko20f85592015-03-19 10:07:02 +0000662 offset_ = writer_->relative_patcher_->WriteThunks(out_, offset_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100663 if (UNLIKELY(offset_ == 0u)) {
664 PLOG(ERROR) << "Failed to write final relative call thunks";
665 result = false;
666 }
667 }
668 return result;
669 }
670
671 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
672 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100673 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
674 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
675
676 if (compiled_method != NULL) { // ie. not an abstract method
677 size_t file_offset = file_offset_;
678 OutputStream* out = out_;
679
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800680 const SwapVector<uint8_t>* quick_code = compiled_method->GetQuickCode();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100681 if (quick_code != nullptr) {
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800682 // Need a wrapper if we create a copy for patching.
683 ArrayRef<const uint8_t> wrapped(*quick_code);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100684 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
685 CHECK_NE(code_size, 0U);
686
687 // Deduplicate code arrays.
688 const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100689 if (method_offsets.code_offset_ >= offset_) {
Vladimir Markoc74658b2015-03-31 10:26:41 +0100690 offset_ = writer_->relative_patcher_->WriteThunks(out, offset_);
691 if (offset_ == 0u) {
692 ReportWriteFailure("relative call thunk", it);
693 return false;
694 }
695 uint32_t aligned_offset = compiled_method->AlignCode(offset_);
696 uint32_t aligned_code_delta = aligned_offset - offset_;
697 if (aligned_code_delta != 0) {
698 if (!writer_->WriteCodeAlignment(out, aligned_code_delta)) {
699 ReportWriteFailure("code alignment padding", it);
700 return false;
701 }
702 offset_ += aligned_code_delta;
703 DCHECK_OFFSET_();
704 }
705 DCHECK_ALIGNED_PARAM(offset_,
706 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
707 DCHECK_EQ(method_offsets.code_offset_,
708 offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta())
709 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100710 const OatQuickMethodHeader& method_header =
711 oat_class->method_headers_[method_offsets_index_];
712 writer_->oat_header_->UpdateChecksum(&method_header, sizeof(method_header));
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100713 if (!out->WriteFully(&method_header, sizeof(method_header))) {
714 ReportWriteFailure("method header", it);
715 return false;
716 }
717 writer_->size_method_header_ += sizeof(method_header);
718 offset_ += sizeof(method_header);
719 DCHECK_OFFSET_();
Vladimir Markof4da6752014-08-01 19:04:18 +0100720
721 if (!compiled_method->GetPatches().empty()) {
Vladimir Marko20f85592015-03-19 10:07:02 +0000722 patched_code_.assign(quick_code->begin(), quick_code->end());
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800723 wrapped = ArrayRef<const uint8_t>(patched_code_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100724 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
725 if (patch.Type() == kLinkerPatchCallRelative) {
726 // NOTE: Relative calls across oat files are not supported.
727 uint32_t target_offset = GetTargetOffset(patch);
728 uint32_t literal_offset = patch.LiteralOffset();
Vladimir Marko20f85592015-03-19 10:07:02 +0000729 writer_->relative_patcher_->PatchCall(&patched_code_, literal_offset,
Vladimir Markof4da6752014-08-01 19:04:18 +0100730 offset_ + literal_offset, target_offset);
Vladimir Marko20f85592015-03-19 10:07:02 +0000731 } else if (patch.Type() == kLinkerPatchDexCacheArray) {
732 uint32_t target_offset = GetDexCacheOffset(patch);
733 uint32_t literal_offset = patch.LiteralOffset();
734 writer_->relative_patcher_->PatchDexCacheReference(&patched_code_, patch,
735 offset_ + literal_offset,
736 target_offset);
Vladimir Markof4da6752014-08-01 19:04:18 +0100737 } else if (patch.Type() == kLinkerPatchCall) {
738 uint32_t target_offset = GetTargetOffset(patch);
739 PatchCodeAddress(&patched_code_, patch.LiteralOffset(), target_offset);
740 } else if (patch.Type() == kLinkerPatchMethod) {
741 mirror::ArtMethod* method = GetTargetMethod(patch);
742 PatchObjectAddress(&patched_code_, patch.LiteralOffset(), method);
743 } else if (patch.Type() == kLinkerPatchType) {
744 mirror::Class* type = GetTargetType(patch);
745 PatchObjectAddress(&patched_code_, patch.LiteralOffset(), type);
746 }
747 }
748 }
749
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800750 writer_->oat_header_->UpdateChecksum(wrapped.data(), code_size);
751 if (!out->WriteFully(wrapped.data(), code_size)) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100752 ReportWriteFailure("method code", it);
753 return false;
754 }
755 writer_->size_code_ += code_size;
756 offset_ += code_size;
757 }
758 DCHECK_OFFSET_();
759 }
760 ++method_offsets_index_;
761 }
762
763 return true;
764 }
765
766 private:
767 OutputStream* const out_;
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100768 const size_t file_offset_;
769 const ScopedObjectAccess soa_;
770 const ScopedAssertNoThreadSuspension no_thread_suspension_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100771 ClassLinker* const class_linker_;
772 mirror::DexCache* dex_cache_;
773 std::vector<uint8_t> patched_code_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100774
775 void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) {
776 PLOG(ERROR) << "Failed to write " << what << " for "
777 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
778 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100779
780 mirror::ArtMethod* GetTargetMethod(const LinkerPatch& patch)
781 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
782 MethodReference ref = patch.TargetMethod();
783 mirror::DexCache* dex_cache =
784 (dex_file_ == ref.dex_file) ? dex_cache_ : class_linker_->FindDexCache(*ref.dex_file);
785 mirror::ArtMethod* method = dex_cache->GetResolvedMethod(ref.dex_method_index);
786 CHECK(method != nullptr);
787 return method;
788 }
789
790 uint32_t GetTargetOffset(const LinkerPatch& patch) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Vladimir Markob163bb72015-03-31 21:49:49 +0100791 auto target_it = writer_->method_offset_map_.map.find(patch.TargetMethod());
Vladimir Markof4da6752014-08-01 19:04:18 +0100792 uint32_t target_offset =
Vladimir Markob163bb72015-03-31 21:49:49 +0100793 (target_it != writer_->method_offset_map_.map.end()) ? target_it->second : 0u;
Vladimir Markof4da6752014-08-01 19:04:18 +0100794 // If there's no compiled code, point to the correct trampoline.
795 if (UNLIKELY(target_offset == 0)) {
796 mirror::ArtMethod* target = GetTargetMethod(patch);
797 DCHECK(target != nullptr);
Jeff Haoa0acc2d2015-01-27 11:22:04 -0800798 size_t size = GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet());
799 const void* oat_code_offset = target->GetEntryPointFromQuickCompiledCodePtrSize(size);
800 if (oat_code_offset != 0) {
801 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(oat_code_offset));
802 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(oat_code_offset));
803 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(oat_code_offset));
804 target_offset = PointerToLowMemUInt32(oat_code_offset);
805 } else {
806 target_offset = target->IsNative()
807 ? writer_->oat_header_->GetQuickGenericJniTrampolineOffset()
808 : writer_->oat_header_->GetQuickToInterpreterBridgeOffset();
809 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100810 }
811 return target_offset;
812 }
813
814 mirror::Class* GetTargetType(const LinkerPatch& patch)
815 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
816 mirror::DexCache* dex_cache = (dex_file_ == patch.TargetTypeDexFile())
817 ? dex_cache_ : class_linker_->FindDexCache(*patch.TargetTypeDexFile());
818 mirror::Class* type = dex_cache->GetResolvedType(patch.TargetTypeIndex());
819 CHECK(type != nullptr);
820 return type;
821 }
822
Vladimir Marko20f85592015-03-19 10:07:02 +0000823 uint32_t GetDexCacheOffset(const LinkerPatch& patch) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
824 if (writer_->image_writer_ != nullptr) {
825 auto* element = writer_->image_writer_->GetDexCacheArrayElementImageAddress(
826 patch.TargetDexCacheDexFile(), patch.TargetDexCacheElementOffset());
827 const uint8_t* oat_data = writer_->image_writer_->GetOatFileBegin() + file_offset_;
828 return reinterpret_cast<const uint8_t*>(element) - oat_data;
829 } else {
830 LOG(FATAL) << "Unimplemented.";
831 UNREACHABLE();
832 }
833 }
834
Vladimir Markof4da6752014-08-01 19:04:18 +0100835 void PatchObjectAddress(std::vector<uint8_t>* code, uint32_t offset, mirror::Object* object)
836 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
837 // NOTE: Direct method pointers across oat files don't use linker patches. However, direct
838 // type pointers across oat files do. (TODO: Investigate why.)
839 if (writer_->image_writer_ != nullptr) {
840 object = writer_->image_writer_->GetImageAddress(object);
841 }
842 uint32_t address = PointerToLowMemUInt32(object);
843 DCHECK_LE(offset + 4, code->size());
844 uint8_t* data = &(*code)[offset];
845 data[0] = address & 0xffu;
846 data[1] = (address >> 8) & 0xffu;
847 data[2] = (address >> 16) & 0xffu;
848 data[3] = (address >> 24) & 0xffu;
849 }
850
851 void PatchCodeAddress(std::vector<uint8_t>* code, uint32_t offset, uint32_t target_offset)
852 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Jeff Haoa0acc2d2015-01-27 11:22:04 -0800853 uint32_t address = writer_->image_writer_ == nullptr ? target_offset :
854 PointerToLowMemUInt32(writer_->image_writer_->GetOatFileBegin() +
855 writer_->oat_data_offset_ + target_offset);
Vladimir Markof4da6752014-08-01 19:04:18 +0100856 DCHECK_LE(offset + 4, code->size());
857 uint8_t* data = &(*code)[offset];
858 data[0] = address & 0xffu;
859 data[1] = (address >> 8) & 0xffu;
860 data[2] = (address >> 16) & 0xffu;
861 data[3] = (address >> 24) & 0xffu;
862 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100863};
864
865template <typename DataAccess>
866class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor {
867 public:
868 WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800869 size_t relative_offset)
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100870 : OatDexMethodVisitor(writer, relative_offset),
871 out_(out),
872 file_offset_(file_offset) {
873 }
874
875 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
876 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
877 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
878
879 if (compiled_method != NULL) { // ie. not an abstract method
880 size_t file_offset = file_offset_;
881 OutputStream* out = out_;
882
883 uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_);
884 ++method_offsets_index_;
885
886 // Write deduplicated map.
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800887 const SwapVector<uint8_t>* map = DataAccess::GetData(compiled_method);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100888 size_t map_size = map == nullptr ? 0 : map->size() * sizeof((*map)[0]);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100889 DCHECK((map_size == 0u && map_offset == 0u) ||
890 (map_size != 0u && map_offset != 0u && map_offset <= offset_))
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800891 << map_size << " " << map_offset << " " << offset_ << " "
892 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " for " << DataAccess::Name();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100893 if (map_size != 0u && map_offset == offset_) {
894 if (UNLIKELY(!out->WriteFully(&(*map)[0], map_size))) {
895 ReportWriteFailure(it);
896 return false;
897 }
898 offset_ += map_size;
899 }
900 DCHECK_OFFSET_();
901 }
902
903 return true;
904 }
905
906 private:
907 OutputStream* const out_;
908 size_t const file_offset_;
909
910 void ReportWriteFailure(const ClassDataItemIterator& it) {
911 PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for "
912 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
913 }
914};
915
916// Visit all methods from all classes in all dex files with the specified visitor.
917bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) {
918 for (const DexFile* dex_file : *dex_files_) {
919 const size_t class_def_count = dex_file->NumClassDefs();
920 for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) {
921 if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) {
922 return false;
923 }
924 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700925 const uint8_t* class_data = dex_file->GetClassData(class_def);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100926 if (class_data != NULL) { // ie not an empty class, such as a marker interface
927 ClassDataItemIterator it(*dex_file, class_data);
928 while (it.HasNextStaticField()) {
929 it.Next();
930 }
931 while (it.HasNextInstanceField()) {
932 it.Next();
933 }
934 size_t class_def_method_index = 0u;
935 while (it.HasNextDirectMethod()) {
936 if (!visitor->VisitMethod(class_def_method_index, it)) {
937 return false;
938 }
939 ++class_def_method_index;
940 it.Next();
941 }
942 while (it.HasNextVirtualMethod()) {
943 if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) {
944 return false;
945 }
946 ++class_def_method_index;
947 it.Next();
948 }
949 }
950 if (UNLIKELY(!visitor->EndClass())) {
951 return false;
952 }
953 }
954 }
955 return true;
956}
957
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700958size_t OatWriter::InitOatHeader() {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700959 oat_header_ = OatHeader::Create(compiler_driver_->GetInstructionSet(),
960 compiler_driver_->GetInstructionSetFeatures(),
961 dex_files_,
962 image_file_location_oat_checksum_,
963 image_file_location_oat_begin_,
964 key_value_store_);
965
966 return oat_header_->GetHeaderSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700967}
968
969size_t OatWriter::InitOatDexFiles(size_t offset) {
970 // create the OatDexFiles
971 for (size_t i = 0; i != dex_files_->size(); ++i) {
972 const DexFile* dex_file = (*dex_files_)[i];
973 CHECK(dex_file != NULL);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800974 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700975 oat_dex_files_.push_back(oat_dex_file);
976 offset += oat_dex_file->SizeOf();
977 }
978 return offset;
979}
980
Brian Carlstrom89521892011-12-07 22:05:07 -0800981size_t OatWriter::InitDexFiles(size_t offset) {
982 // calculate the offsets within OatDexFiles to the DexFiles
983 for (size_t i = 0; i != dex_files_->size(); ++i) {
984 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700985 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800986 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700987 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800988
989 // set offset in OatDexFile to DexFile
990 oat_dex_files_[i]->dex_file_offset_ = offset;
991
992 const DexFile* dex_file = (*dex_files_)[i];
993 offset += dex_file->GetHeader().file_size_;
994 }
995 return offset;
996}
997
Brian Carlstrom389efb02012-01-11 12:06:26 -0800998size_t OatWriter::InitOatClasses(size_t offset) {
Brian Carlstrom389efb02012-01-11 12:06:26 -0800999 // calculate the offsets within OatDexFiles to OatClasses
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001000 InitOatClassesMethodVisitor visitor(this, offset);
1001 bool success = VisitDexMethods(&visitor);
1002 CHECK(success);
1003 offset = visitor.GetOffset();
Brian Carlstromba150c32013-08-27 17:31:03 -07001004
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001005 // Update oat_dex_files_.
1006 auto oat_class_it = oat_classes_.begin();
1007 for (OatDexFile* oat_dex_file : oat_dex_files_) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001008 for (uint32_t& method_offset : oat_dex_file->methods_offsets_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001009 DCHECK(oat_class_it != oat_classes_.end());
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001010 method_offset = (*oat_class_it)->offset_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001011 ++oat_class_it;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001012 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001013 oat_dex_file->UpdateChecksum(oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001014 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001015 CHECK(oat_class_it == oat_classes_.end());
1016
1017 return offset;
1018}
1019
1020size_t OatWriter::InitOatMaps(size_t offset) {
1021 #define VISIT(VisitorType) \
1022 do { \
1023 VisitorType visitor(this, offset); \
1024 bool success = VisitDexMethods(&visitor); \
1025 DCHECK(success); \
1026 offset = visitor.GetOffset(); \
1027 } while (false)
1028
1029 VISIT(InitMapMethodVisitor<GcMapDataAccess>);
1030 VISIT(InitMapMethodVisitor<MappingTableDataAccess>);
1031 VISIT(InitMapMethodVisitor<VmapTableDataAccess>);
1032
1033 #undef VISIT
1034
Brian Carlstrome24fa612011-09-29 00:53:55 -07001035 return offset;
1036}
1037
1038size_t OatWriter::InitOatCode(size_t offset) {
1039 // calculate the offsets within OatHeader to executable code
1040 size_t old_offset = offset;
Dave Allison50abf0a2014-06-23 13:19:59 -07001041 size_t adjusted_offset = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001042 // required to be on a new page boundary
1043 offset = RoundUp(offset, kPageSize);
1044 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001045 size_executable_offset_alignment_ = offset - old_offset;
1046 if (compiler_driver_->IsImage()) {
Alex Lighta59dd802014-07-02 16:28:08 -07001047 CHECK_EQ(image_patch_delta_, 0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001048 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001049
Ian Rogers848871b2013-08-05 10:56:33 -07001050 #define DO_TRAMPOLINE(field, fn_name) \
1051 offset = CompiledCode::AlignCode(offset, instruction_set); \
Dave Allison50abf0a2014-06-23 13:19:59 -07001052 adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \
1053 oat_header_->Set ## fn_name ## Offset(adjusted_offset); \
Ian Rogers848871b2013-08-05 10:56:33 -07001054 field.reset(compiler_driver_->Create ## fn_name()); \
1055 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001056
Ian Rogers848871b2013-08-05 10:56:33 -07001057 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge);
1058 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge);
1059 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Andreas Gampe2da88232014-02-27 12:26:20 -08001060 DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline);
Jeff Hao88474b42013-10-23 16:24:40 -07001061 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -07001062 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
1063 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001064
Ian Rogers848871b2013-08-05 10:56:33 -07001065 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001066 } else {
Ian Rogers848871b2013-08-05 10:56:33 -07001067 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
1068 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
1069 oat_header_->SetJniDlsymLookupOffset(0);
Andreas Gampe2da88232014-02-27 12:26:20 -08001070 oat_header_->SetQuickGenericJniTrampolineOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -07001071 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001072 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -07001073 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Alex Lighta59dd802014-07-02 16:28:08 -07001074 oat_header_->SetImagePatchDelta(image_patch_delta_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001075 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001076 return offset;
1077}
1078
1079size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001080 #define VISIT(VisitorType) \
1081 do { \
1082 VisitorType visitor(this, offset); \
1083 bool success = VisitDexMethods(&visitor); \
1084 DCHECK(success); \
1085 offset = visitor.GetOffset(); \
1086 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001087
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001088 VISIT(InitCodeMethodVisitor);
Ian Rogers1212a022013-03-04 10:48:41 -08001089 if (compiler_driver_->IsImage()) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001090 VISIT(InitImageMethodVisitor);
Ian Rogers0571d352011-11-03 19:51:38 -07001091 }
Logan Chien8b977d32012-02-21 19:14:55 +08001092
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001093 #undef VISIT
1094
Brian Carlstrome24fa612011-09-29 00:53:55 -07001095 return offset;
1096}
1097
Ian Rogers3d504072014-03-01 09:16:49 -08001098bool OatWriter::Write(OutputStream* out) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001099 const off_t raw_file_offset = out->Seek(0, kSeekCurrent);
1100 if (raw_file_offset == (off_t) -1) {
1101 LOG(ERROR) << "Failed to get file offset in " << out->GetLocation();
1102 return false;
1103 }
1104 const size_t file_offset = static_cast<size_t>(raw_file_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001105
Vladimir Markof4da6752014-08-01 19:04:18 +01001106 // Reserve space for header. It will be written last - after updating the checksum.
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001107 size_t header_size = oat_header_->GetHeaderSize();
Vladimir Markof4da6752014-08-01 19:04:18 +01001108 if (out->Seek(header_size, kSeekCurrent) == (off_t) -1) {
1109 PLOG(ERROR) << "Failed to reserve space for oat header in " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001110 return false;
1111 }
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001112 size_oat_header_ += sizeof(OatHeader);
1113 size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001114
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001115 if (!WriteTables(out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001116 LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001117 return false;
1118 }
1119
Vladimir Markof4da6752014-08-01 19:04:18 +01001120 off_t tables_end_offset = out->Seek(0, kSeekCurrent);
1121 if (tables_end_offset == (off_t) -1) {
1122 LOG(ERROR) << "Failed to seek to oat code position in " << out->GetLocation();
1123 return false;
1124 }
1125 size_t relative_offset = static_cast<size_t>(tables_end_offset) - file_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001126 relative_offset = WriteMaps(out, file_offset, relative_offset);
1127 if (relative_offset == 0) {
1128 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
1129 return false;
1130 }
1131
1132 relative_offset = WriteCode(out, file_offset, relative_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001133 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001134 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001135 return false;
1136 }
1137
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001138 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
1139 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001140 LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001141 return false;
1142 }
1143
Vladimir Markof4da6752014-08-01 19:04:18 +01001144 const off_t oat_end_file_offset = out->Seek(0, kSeekCurrent);
1145 if (oat_end_file_offset == (off_t) -1) {
1146 LOG(ERROR) << "Failed to get oat end file offset in " << out->GetLocation();
1147 return false;
1148 }
1149
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001150 if (kIsDebugBuild) {
1151 uint32_t size_total = 0;
1152 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001153 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001154 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001155
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001156 DO_STAT(size_dex_file_alignment_);
1157 DO_STAT(size_executable_offset_alignment_);
1158 DO_STAT(size_oat_header_);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001159 DO_STAT(size_oat_header_key_value_store_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001160 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -07001161 DO_STAT(size_interpreter_to_interpreter_bridge_);
1162 DO_STAT(size_interpreter_to_compiled_code_bridge_);
1163 DO_STAT(size_jni_dlsym_lookup_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001164 DO_STAT(size_quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001165 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001166 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001167 DO_STAT(size_quick_to_interpreter_bridge_);
1168 DO_STAT(size_trampoline_alignment_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001169 DO_STAT(size_method_header_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001170 DO_STAT(size_code_);
1171 DO_STAT(size_code_alignment_);
Vladimir Markof4da6752014-08-01 19:04:18 +01001172 DO_STAT(size_relative_call_thunks_);
Vladimir Markoc74658b2015-03-31 10:26:41 +01001173 DO_STAT(size_misc_thunks_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001174 DO_STAT(size_mapping_table_);
1175 DO_STAT(size_vmap_table_);
1176 DO_STAT(size_gc_map_);
1177 DO_STAT(size_oat_dex_file_location_size_);
1178 DO_STAT(size_oat_dex_file_location_data_);
1179 DO_STAT(size_oat_dex_file_location_checksum_);
1180 DO_STAT(size_oat_dex_file_offset_);
1181 DO_STAT(size_oat_dex_file_methods_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001182 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001183 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001184 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001185 DO_STAT(size_oat_class_method_offsets_);
1186 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001187
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001188 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Vladimir Markof4da6752014-08-01 19:04:18 +01001189 CHECK_EQ(file_offset + size_total, static_cast<size_t>(oat_end_file_offset));
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001190 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001191 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001192
Vladimir Markof4da6752014-08-01 19:04:18 +01001193 CHECK_EQ(file_offset + size_, static_cast<size_t>(oat_end_file_offset));
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001194 CHECK_EQ(size_, relative_offset);
1195
Vladimir Markof4da6752014-08-01 19:04:18 +01001196 // Write the header now that the checksum is final.
1197 if (out->Seek(file_offset, kSeekSet) == (off_t) -1) {
1198 PLOG(ERROR) << "Failed to seek to oat header position in " << out->GetLocation();
1199 return false;
1200 }
1201 DCHECK_EQ(raw_file_offset, out->Seek(0, kSeekCurrent));
1202 if (!out->WriteFully(oat_header_, header_size)) {
1203 PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation();
1204 return false;
1205 }
1206 if (out->Seek(oat_end_file_offset, kSeekSet) == (off_t) -1) {
1207 PLOG(ERROR) << "Failed to seek to end after writing oat header to " << out->GetLocation();
1208 return false;
1209 }
1210 DCHECK_EQ(oat_end_file_offset, out->Seek(0, kSeekCurrent));
1211
Brian Carlstrome24fa612011-09-29 00:53:55 -07001212 return true;
1213}
1214
Ian Rogers3d504072014-03-01 09:16:49 -08001215bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001216 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001217 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001218 PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001219 return false;
1220 }
1221 }
Brian Carlstrom89521892011-12-07 22:05:07 -08001222 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001223 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Ian Rogers3d504072014-03-01 09:16:49 -08001224 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -08001225 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
1226 const DexFile* dex_file = (*dex_files_)[i];
1227 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
1228 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
1229 return false;
1230 }
1231 const DexFile* dex_file = (*dex_files_)[i];
Ian Rogers3d504072014-03-01 09:16:49 -08001232 if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001233 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
Ian Rogers3d504072014-03-01 09:16:49 -08001234 << " to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001235 return false;
1236 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001237 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -08001238 }
Brian Carlstrom389efb02012-01-11 12:06:26 -08001239 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001240 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001241 PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001242 return false;
1243 }
1244 }
1245 return true;
1246}
1247
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001248size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) {
1249 #define VISIT(VisitorType) \
1250 do { \
1251 VisitorType visitor(this, out, file_offset, relative_offset); \
1252 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1253 return 0; \
1254 } \
1255 relative_offset = visitor.GetOffset(); \
1256 } while (false)
1257
1258 size_t gc_maps_offset = relative_offset;
1259 VISIT(WriteMapMethodVisitor<GcMapDataAccess>);
1260 size_gc_map_ = relative_offset - gc_maps_offset;
1261
1262 size_t mapping_tables_offset = relative_offset;
1263 VISIT(WriteMapMethodVisitor<MappingTableDataAccess>);
1264 size_mapping_table_ = relative_offset - mapping_tables_offset;
1265
1266 size_t vmap_tables_offset = relative_offset;
1267 VISIT(WriteMapMethodVisitor<VmapTableDataAccess>);
1268 size_vmap_table_ = relative_offset - vmap_tables_offset;
1269
1270 #undef VISIT
1271
1272 return relative_offset;
1273}
1274
1275size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) {
Ian Rogers3d504072014-03-01 09:16:49 -08001276 off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001277 relative_offset += size_executable_offset_alignment_;
1278 DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset());
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001279 size_t expected_file_offset = file_offset + relative_offset;
1280 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001281 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Ian Rogers3d504072014-03-01 09:16:49 -08001282 << " Expected: " << expected_file_offset << " File: " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001283 return 0;
1284 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001285 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001286 if (compiler_driver_->IsImage()) {
1287 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001288
Ian Rogers848871b2013-08-05 10:56:33 -07001289 #define DO_TRAMPOLINE(field) \
1290 do { \
1291 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
1292 uint32_t alignment_padding = aligned_offset - relative_offset; \
Ian Rogers3d504072014-03-01 09:16:49 -08001293 out->Seek(alignment_padding, kSeekCurrent); \
Ian Rogers848871b2013-08-05 10:56:33 -07001294 size_trampoline_alignment_ += alignment_padding; \
Ian Rogers3d504072014-03-01 09:16:49 -08001295 if (!out->WriteFully(&(*field)[0], field->size())) { \
1296 PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \
Ian Rogers848871b2013-08-05 10:56:33 -07001297 return false; \
1298 } \
1299 size_ ## field += field->size(); \
1300 relative_offset += alignment_padding + field->size(); \
1301 DCHECK_OFFSET(); \
1302 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001303
Ian Rogers848871b2013-08-05 10:56:33 -07001304 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_);
1305 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_);
1306 DO_TRAMPOLINE(jni_dlsym_lookup_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001307 DO_TRAMPOLINE(quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001308 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001309 DO_TRAMPOLINE(quick_resolution_trampoline_);
1310 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
1311 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001312 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001313 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001314}
1315
Ian Rogers3d504072014-03-01 09:16:49 -08001316size_t OatWriter::WriteCodeDexFiles(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001317 const size_t file_offset,
1318 size_t relative_offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001319 #define VISIT(VisitorType) \
1320 do { \
1321 VisitorType visitor(this, out, file_offset, relative_offset); \
1322 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1323 return 0; \
1324 } \
1325 relative_offset = visitor.GetOffset(); \
1326 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001327
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001328 VISIT(WriteCodeMethodVisitor);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001329
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001330 #undef VISIT
Brian Carlstrom265091e2013-01-30 14:08:26 -08001331
Vladimir Markob163bb72015-03-31 21:49:49 +01001332 size_code_alignment_ += relative_patcher_->CodeAlignmentSize();
1333 size_relative_call_thunks_ += relative_patcher_->RelativeCallThunksSize();
1334 size_misc_thunks_ += relative_patcher_->MiscThunksSize();
1335
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001336 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001337}
1338
Vladimir Markof4da6752014-08-01 19:04:18 +01001339bool OatWriter::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) {
1340 static const uint8_t kPadding[] = {
1341 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
1342 };
1343 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
1344 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
1345 return false;
1346 }
1347 size_code_alignment_ += aligned_code_delta;
1348 return true;
1349}
1350
Vladimir Markob163bb72015-03-31 21:49:49 +01001351std::pair<bool, uint32_t> OatWriter::MethodOffsetMap::FindMethodOffset(MethodReference ref) {
1352 auto it = map.find(ref);
1353 if (it == map.end()) {
1354 return std::pair<bool, uint32_t>(false, 0u);
1355 } else {
1356 return std::pair<bool, uint32_t>(true, it->second);
1357 }
1358}
1359
Brian Carlstrom265091e2013-01-30 14:08:26 -08001360OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
1361 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -08001362 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001363 dex_file_location_size_ = location.size();
1364 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001365 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -08001366 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001367 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001368}
1369
1370size_t OatWriter::OatDexFile::SizeOf() const {
1371 return sizeof(dex_file_location_size_)
1372 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001373 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -08001374 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001375 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001376}
1377
Ian Rogers3d504072014-03-01 09:16:49 -08001378void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const {
1379 oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
1380 oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
1381 oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
1382 oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
1383 oat_header->UpdateChecksum(&methods_offsets_[0],
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001384 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001385}
1386
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001387bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001388 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001389 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001390 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001391 if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
1392 PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001393 return false;
1394 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001395 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001396 if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
1397 PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001398 return false;
1399 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001400 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Ian Rogers3d504072014-03-01 09:16:49 -08001401 if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
1402 PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001403 return false;
1404 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001405 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Ian Rogers3d504072014-03-01 09:16:49 -08001406 if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
1407 PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001408 return false;
1409 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001410 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Ian Rogers3d504072014-03-01 09:16:49 -08001411 if (!out->WriteFully(&methods_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001412 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001413 PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001414 return false;
1415 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001416 oat_writer->size_oat_dex_file_methods_offsets_ +=
1417 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001418 return true;
1419}
1420
Brian Carlstromba150c32013-08-27 17:31:03 -07001421OatWriter::OatClass::OatClass(size_t offset,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001422 const std::vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -07001423 uint32_t num_non_null_compiled_methods,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001424 mirror::Class::Status status)
1425 : compiled_methods_(compiled_methods) {
1426 uint32_t num_methods = compiled_methods.size();
Brian Carlstromba150c32013-08-27 17:31:03 -07001427 CHECK_LE(num_non_null_compiled_methods, num_methods);
1428
Brian Carlstrom265091e2013-01-30 14:08:26 -08001429 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -07001430 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
1431
1432 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
1433 // apply when there are 0 methods, we just arbitrarily say that 0
1434 // methods means kOatClassNoneCompiled and that we won't use
1435 // kOatClassAllCompiled unless there is at least one compiled
1436 // method. This means in an interpretter only system, we can assert
1437 // that all classes are kOatClassNoneCompiled.
1438 if (num_non_null_compiled_methods == 0) {
1439 type_ = kOatClassNoneCompiled;
1440 } else if (num_non_null_compiled_methods == num_methods) {
1441 type_ = kOatClassAllCompiled;
1442 } else {
1443 type_ = kOatClassSomeCompiled;
1444 }
1445
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001446 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -07001447 method_offsets_.resize(num_non_null_compiled_methods);
Vladimir Marko8a630572014-04-09 18:45:35 +01001448 method_headers_.resize(num_non_null_compiled_methods);
Brian Carlstromba150c32013-08-27 17:31:03 -07001449
1450 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
1451 if (type_ == kOatClassSomeCompiled) {
1452 method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator());
1453 method_bitmap_size_ = method_bitmap_->GetSizeOf();
1454 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
1455 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
1456 } else {
1457 method_bitmap_ = NULL;
1458 method_bitmap_size_ = 0;
1459 }
1460
1461 for (size_t i = 0; i < num_methods; i++) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001462 CompiledMethod* compiled_method = compiled_methods_[i];
Brian Carlstromba150c32013-08-27 17:31:03 -07001463 if (compiled_method == NULL) {
1464 oat_method_offsets_offsets_from_oat_class_[i] = 0;
1465 } else {
1466 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
1467 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
1468 if (type_ == kOatClassSomeCompiled) {
1469 method_bitmap_->SetBit(i);
1470 }
1471 }
1472 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001473}
1474
Brian Carlstromba150c32013-08-27 17:31:03 -07001475OatWriter::OatClass::~OatClass() {
Mathieu Chartier661974a2014-01-09 11:23:53 -08001476 delete method_bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -07001477}
1478
Brian Carlstrom265091e2013-01-30 14:08:26 -08001479size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
1480 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001481 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
1482 if (method_offset == 0) {
1483 return 0;
1484 }
1485 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001486}
1487
1488size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
1489 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001490 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08001491}
1492
1493size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001494 return sizeof(status_)
1495 + sizeof(type_)
1496 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
1497 + method_bitmap_size_
1498 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001499}
1500
Ian Rogers3d504072014-03-01 09:16:49 -08001501void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const {
1502 oat_header->UpdateChecksum(&status_, sizeof(status_));
1503 oat_header->UpdateChecksum(&type_, sizeof(type_));
Brian Carlstromba150c32013-08-27 17:31:03 -07001504 if (method_bitmap_size_ != 0) {
1505 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001506 oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_));
1507 oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001508 }
Ian Rogers3d504072014-03-01 09:16:49 -08001509 oat_header->UpdateChecksum(&method_offsets_[0],
1510 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001511}
1512
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001513bool OatWriter::OatClass::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001514 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001515 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001516 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001517 if (!out->WriteFully(&status_, sizeof(status_))) {
1518 PLOG(ERROR) << "Failed to write class status to " << out->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001519 return false;
1520 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001521 oat_writer->size_oat_class_status_ += sizeof(status_);
Ian Rogers3d504072014-03-01 09:16:49 -08001522 if (!out->WriteFully(&type_, sizeof(type_))) {
1523 PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001524 return false;
1525 }
1526 oat_writer->size_oat_class_type_ += sizeof(type_);
1527 if (method_bitmap_size_ != 0) {
1528 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001529 if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
1530 PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001531 return false;
1532 }
1533 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001534 if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
1535 PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001536 return false;
1537 }
1538 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1539 }
Ian Rogers3d504072014-03-01 09:16:49 -08001540 if (!out->WriteFully(&method_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001541 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001542 PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001543 return false;
1544 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001545 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001546 return true;
1547}
1548
Brian Carlstrome24fa612011-09-29 00:53:55 -07001549} // namespace art