blob: 712092077353025800ce76052956e51d3437c388 [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 Marko71b0ddf2015-04-02 19:45:06 +0100360 offset_ = writer_->relative_patcher_->ReserveSpaceEnd(offset_);
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 Marko4d23c9d2015-04-01 23:03:09 +0100387 offset_ = writer_->relative_patcher_->ReserveSpace(
388 offset_, compiled_method, MethodReference(dex_file_, it.GetMemberIndex()));
Vladimir Markoc74658b2015-03-31 10:26:41 +0100389 offset_ = compiled_method->AlignCode(offset_);
390 DCHECK_ALIGNED_PARAM(offset_,
391 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
392 quick_code_offset = offset_ + sizeof(OatQuickMethodHeader) + thumb_offset;
Elliott Hughes956af0f2014-12-11 14:34:28 -0800393 dedupe_map_.PutBefore(lb, compiled_method, quick_code_offset);
394 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100395
Elliott Hughes956af0f2014-12-11 14:34:28 -0800396 MethodReference method_ref(dex_file_, it.GetMemberIndex());
Vladimir Markob163bb72015-03-31 21:49:49 +0100397 auto method_lb = writer_->method_offset_map_.map.lower_bound(method_ref);
398 if (method_lb != writer_->method_offset_map_.map.end() &&
399 !writer_->method_offset_map_.map.key_comp()(method_ref, method_lb->first)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800400 // TODO: Should this be a hard failure?
401 LOG(WARNING) << "Multiple definitions of "
402 << PrettyMethod(method_ref.dex_method_index, *method_ref.dex_file)
403 << ((method_lb->second != quick_code_offset) ? "; OFFSET MISMATCH" : "");
404 } else {
Vladimir Markob163bb72015-03-31 21:49:49 +0100405 writer_->method_offset_map_.map.PutBefore(method_lb, method_ref, quick_code_offset);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800406 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100407
Elliott Hughes956af0f2014-12-11 14:34:28 -0800408 // Update quick method header.
409 DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size());
410 OatQuickMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_];
411 uint32_t mapping_table_offset = method_header->mapping_table_offset_;
412 uint32_t vmap_table_offset = method_header->vmap_table_offset_;
413 uint32_t gc_map_offset = method_header->gc_map_offset_;
414 // The code offset was 0 when the mapping/vmap table offset was set, so it's set
415 // to 0-offset and we need to adjust it by code_offset.
416 uint32_t code_offset = quick_code_offset - thumb_offset;
417 if (mapping_table_offset != 0u) {
418 mapping_table_offset += code_offset;
419 DCHECK_LT(mapping_table_offset, code_offset);
420 }
421 if (vmap_table_offset != 0u) {
422 vmap_table_offset += code_offset;
423 DCHECK_LT(vmap_table_offset, code_offset);
424 }
425 if (gc_map_offset != 0u) {
426 gc_map_offset += code_offset;
427 DCHECK_LT(gc_map_offset, code_offset);
428 }
429 uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
430 uint32_t core_spill_mask = compiled_method->GetCoreSpillMask();
431 uint32_t fp_spill_mask = compiled_method->GetFpSpillMask();
432 *method_header = OatQuickMethodHeader(mapping_table_offset, vmap_table_offset,
433 gc_map_offset, frame_size_in_bytes, core_spill_mask,
434 fp_spill_mask, code_size);
Vladimir Marko7624d252014-05-02 14:40:15 +0100435
Elliott Hughes956af0f2014-12-11 14:34:28 -0800436 if (!deduped) {
437 // Update offsets. (Checksum is updated when writing.)
438 offset_ += sizeof(*method_header); // Method header is prepended before code.
439 offset_ += code_size;
440 // Record absolute patch locations.
441 if (!compiled_method->GetPatches().empty()) {
442 uintptr_t base_loc = offset_ - code_size - writer_->oat_header_->GetExecutableOffset();
443 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
Vladimir Marko20f85592015-03-19 10:07:02 +0000444 if (!patch.IsPcRelative()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800445 writer_->absolute_patch_locations_.push_back(base_loc + patch.LiteralOffset());
Vladimir Markof4da6752014-08-01 19:04:18 +0100446 }
447 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100448 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800449 }
Alex Light78382fa2014-06-06 15:45:32 -0700450
Elliott Hughes956af0f2014-12-11 14:34:28 -0800451 if (writer_->compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols()) {
452 // Record debug information for this function if we are doing that.
Alex Light78382fa2014-06-06 15:45:32 -0700453
Elliott Hughes956af0f2014-12-11 14:34:28 -0800454 std::string name = PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
455 if (deduped) {
456 // TODO We should place the DEDUPED tag on the first instance of a deduplicated symbol
457 // so that it will show up in a debuggerd crash report.
458 name += " [ DEDUPED ]";
Alex Light78382fa2014-06-06 15:45:32 -0700459 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800460
461 const uint32_t quick_code_start = quick_code_offset -
David Srbecky6f715892015-03-30 14:21:42 +0100462 writer_->oat_header_->GetExecutableOffset() - thumb_offset;
Elliott Hughes956af0f2014-12-11 14:34:28 -0800463 const DexFile::CodeItem *code_item = it.GetMethodCodeItem();
David Srbecky6f715892015-03-30 14:21:42 +0100464 const DexFile::ClassDef& class_def = dex_file_->GetClassDef(class_def_index_);
465 writer_->method_info_.push_back(DebugInfo(name, deduped,
466 dex_file_->GetClassDescriptor(class_def),
467 dex_file_->GetSourceFile(class_def),
Elliott Hughes956af0f2014-12-11 14:34:28 -0800468 quick_code_start, quick_code_start + code_size,
469 code_item == nullptr ? nullptr : dex_file_->GetDebugInfoStream(code_item),
470 compiled_method));
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100471 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100472
473 if (kIsDebugBuild) {
474 // We expect GC maps except when the class hasn't been verified or the method is native.
475 const CompilerDriver* compiler_driver = writer_->compiler_driver_;
476 ClassReference class_ref(dex_file_, class_def_index_);
477 CompiledClass* compiled_class = compiler_driver->GetCompiledClass(class_ref);
478 mirror::Class::Status status;
479 if (compiled_class != NULL) {
480 status = compiled_class->GetStatus();
481 } else if (compiler_driver->GetVerificationResults()->IsClassRejected(class_ref)) {
482 status = mirror::Class::kStatusError;
483 } else {
484 status = mirror::Class::kStatusNotReady;
485 }
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800486 const SwapVector<uint8_t>* gc_map = compiled_method->GetGcMap();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100487 if (gc_map != nullptr) {
488 size_t gc_map_size = gc_map->size() * sizeof(gc_map[0]);
Andreas Gampe51829322014-08-25 15:05:04 -0700489 bool is_native = it.MemberIsNative();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100490 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
491 << gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
492 << (status < mirror::Class::kStatusVerified) << " " << status << " "
493 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
494 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100495 }
496
497 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
498 OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_];
499 offsets->code_offset_ = quick_code_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100500 ++method_offsets_index_;
501 }
502
503 return true;
504 }
505
506 private:
Vladimir Marko20f85592015-03-19 10:07:02 +0000507 struct CodeOffsetsKeyComparator {
508 bool operator()(const CompiledMethod* lhs, const CompiledMethod* rhs) const {
509 if (lhs->GetQuickCode() != rhs->GetQuickCode()) {
510 return lhs->GetQuickCode() < rhs->GetQuickCode();
511 }
512 // If the code is the same, all other fields are likely to be the same as well.
513 if (UNLIKELY(lhs->GetMappingTable() != rhs->GetMappingTable())) {
514 return lhs->GetMappingTable() < rhs->GetMappingTable();
515 }
516 if (UNLIKELY(lhs->GetVmapTable() != rhs->GetVmapTable())) {
517 return lhs->GetVmapTable() < rhs->GetVmapTable();
518 }
519 if (UNLIKELY(lhs->GetGcMap() != rhs->GetGcMap())) {
520 return lhs->GetGcMap() < rhs->GetGcMap();
521 }
522 const auto& lhs_patches = lhs->GetPatches();
523 const auto& rhs_patches = rhs->GetPatches();
524 if (UNLIKELY(lhs_patches.size() != rhs_patches.size())) {
525 return lhs_patches.size() < rhs_patches.size();
526 }
527 auto rit = rhs_patches.begin();
528 for (const LinkerPatch& lpatch : lhs_patches) {
529 if (UNLIKELY(!(lpatch == *rit))) {
530 return lpatch < *rit;
531 }
532 ++rit;
533 }
534 return false;
535 }
536 };
537
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100538 // Deduplication is already done on a pointer basis by the compiler driver,
539 // so we can simply compare the pointers to find out if things are duplicated.
Vladimir Marko8a630572014-04-09 18:45:35 +0100540 SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100541};
542
543template <typename DataAccess>
544class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor {
545 public:
546 InitMapMethodVisitor(OatWriter* writer, size_t offset)
547 : OatDexMethodVisitor(writer, offset) {
548 }
549
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700550 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it ATTRIBUTE_UNUSED)
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100551 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
552 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
553 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
554
555 if (compiled_method != nullptr) {
556 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
557 DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u);
558
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800559 const SwapVector<uint8_t>* map = DataAccess::GetData(compiled_method);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100560 uint32_t map_size = map == nullptr ? 0 : map->size() * sizeof((*map)[0]);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100561 if (map_size != 0u) {
Vladimir Markobd72fc12014-07-09 16:06:40 +0100562 auto lb = dedupe_map_.lower_bound(map);
563 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(map, lb->first)) {
564 DataAccess::SetOffset(oat_class, method_offsets_index_, lb->second);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100565 } else {
566 DataAccess::SetOffset(oat_class, method_offsets_index_, offset_);
Vladimir Markobd72fc12014-07-09 16:06:40 +0100567 dedupe_map_.PutBefore(lb, map, offset_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100568 offset_ += map_size;
569 writer_->oat_header_->UpdateChecksum(&(*map)[0], map_size);
570 }
571 }
572 ++method_offsets_index_;
573 }
574
575 return true;
576 }
577
578 private:
579 // Deduplication is already done on a pointer basis by the compiler driver,
580 // so we can simply compare the pointers to find out if things are duplicated.
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800581 SafeMap<const SwapVector<uint8_t>*, uint32_t> dedupe_map_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100582};
583
584class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor {
585 public:
586 InitImageMethodVisitor(OatWriter* writer, size_t offset)
Jeff Haoc7d11882015-02-03 15:08:39 -0800587 : OatDexMethodVisitor(writer, offset),
588 pointer_size_(GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet())) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100589 }
590
591 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
592 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
593 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
594 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
595
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800596 OatMethodOffsets offsets(0u);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100597 if (compiled_method != nullptr) {
598 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
599 offsets = oat_class->method_offsets_[method_offsets_index_];
600 ++method_offsets_index_;
601 }
602
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100603 ClassLinker* linker = Runtime::Current()->GetClassLinker();
604 InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_));
605 // Unchecked as we hold mutator_lock_ on entry.
606 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800607 StackHandleScope<1> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700608 Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache(*dex_file_)));
Vladimir Marko7624d252014-05-02 14:40:15 +0100609 mirror::ArtMethod* method = linker->ResolveMethod(*dex_file_, it.GetMemberIndex(), dex_cache,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700610 NullHandle<mirror::ClassLoader>(),
611 NullHandle<mirror::ArtMethod>(),
612 invoke_type);
Andreas Gamped9efea62014-07-21 22:56:08 -0700613 if (method == nullptr) {
614 LOG(ERROR) << "Unexpected failure to resolve a method: "
615 << PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
616 soa.Self()->AssertPendingException();
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000617 mirror::Throwable* exc = soa.Self()->GetException();
Andreas Gamped9efea62014-07-21 22:56:08 -0700618 std::string dump = exc->Dump();
619 LOG(FATAL) << dump;
620 }
Jeff Haoc7d11882015-02-03 15:08:39 -0800621 method->SetEntryPointFromQuickCompiledCodePtrSize(reinterpret_cast<void*>(offsets.code_offset_),
622 pointer_size_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100623
624 return true;
625 }
Jeff Haoc7d11882015-02-03 15:08:39 -0800626
627 protected:
628 const size_t pointer_size_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100629};
630
631class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor {
632 public:
633 WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
Vladimir Markof4da6752014-08-01 19:04:18 +0100634 size_t relative_offset) SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100635 : OatDexMethodVisitor(writer, relative_offset),
636 out_(out),
Vladimir Markof4da6752014-08-01 19:04:18 +0100637 file_offset_(file_offset),
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100638 soa_(Thread::Current()),
639 no_thread_suspension_(soa_.Self(), "OatWriter patching"),
Vladimir Markof4da6752014-08-01 19:04:18 +0100640 class_linker_(Runtime::Current()->GetClassLinker()),
641 dex_cache_(nullptr) {
642 if (writer_->image_writer_ != nullptr) {
643 // If we're creating the image, the address space must be ready so that we can apply patches.
644 CHECK(writer_->image_writer_->IsImageAddressSpaceReady());
645 patched_code_.reserve(16 * KB);
646 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100647 }
648
Vladimir Markof4da6752014-08-01 19:04:18 +0100649 ~WriteCodeMethodVisitor() UNLOCK_FUNCTION(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100650 }
651
652 bool StartClass(const DexFile* dex_file, size_t class_def_index)
653 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
654 OatDexMethodVisitor::StartClass(dex_file, class_def_index);
655 if (dex_cache_ == nullptr || dex_cache_->GetDexFile() != dex_file) {
656 dex_cache_ = class_linker_->FindDexCache(*dex_file);
657 }
658 return true;
659 }
660
661 bool EndClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
662 bool result = OatDexMethodVisitor::EndClass();
663 if (oat_class_index_ == writer_->oat_classes_.size()) {
664 DCHECK(result); // OatDexMethodVisitor::EndClass() never fails.
Vladimir Marko20f85592015-03-19 10:07:02 +0000665 offset_ = writer_->relative_patcher_->WriteThunks(out_, offset_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100666 if (UNLIKELY(offset_ == 0u)) {
667 PLOG(ERROR) << "Failed to write final relative call thunks";
668 result = false;
669 }
670 }
671 return result;
672 }
673
674 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
675 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100676 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
677 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
678
679 if (compiled_method != NULL) { // ie. not an abstract method
680 size_t file_offset = file_offset_;
681 OutputStream* out = out_;
682
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800683 const SwapVector<uint8_t>* quick_code = compiled_method->GetQuickCode();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100684 if (quick_code != nullptr) {
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800685 // Need a wrapper if we create a copy for patching.
686 ArrayRef<const uint8_t> wrapped(*quick_code);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100687 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
688 CHECK_NE(code_size, 0U);
689
690 // Deduplicate code arrays.
691 const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100692 if (method_offsets.code_offset_ >= offset_) {
Vladimir Markoc74658b2015-03-31 10:26:41 +0100693 offset_ = writer_->relative_patcher_->WriteThunks(out, offset_);
694 if (offset_ == 0u) {
695 ReportWriteFailure("relative call thunk", it);
696 return false;
697 }
698 uint32_t aligned_offset = compiled_method->AlignCode(offset_);
699 uint32_t aligned_code_delta = aligned_offset - offset_;
700 if (aligned_code_delta != 0) {
701 if (!writer_->WriteCodeAlignment(out, aligned_code_delta)) {
702 ReportWriteFailure("code alignment padding", it);
703 return false;
704 }
705 offset_ += aligned_code_delta;
706 DCHECK_OFFSET_();
707 }
708 DCHECK_ALIGNED_PARAM(offset_,
709 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
710 DCHECK_EQ(method_offsets.code_offset_,
711 offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta())
712 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100713 const OatQuickMethodHeader& method_header =
714 oat_class->method_headers_[method_offsets_index_];
715 writer_->oat_header_->UpdateChecksum(&method_header, sizeof(method_header));
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100716 if (!out->WriteFully(&method_header, sizeof(method_header))) {
717 ReportWriteFailure("method header", it);
718 return false;
719 }
720 writer_->size_method_header_ += sizeof(method_header);
721 offset_ += sizeof(method_header);
722 DCHECK_OFFSET_();
Vladimir Markof4da6752014-08-01 19:04:18 +0100723
724 if (!compiled_method->GetPatches().empty()) {
Vladimir Marko20f85592015-03-19 10:07:02 +0000725 patched_code_.assign(quick_code->begin(), quick_code->end());
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800726 wrapped = ArrayRef<const uint8_t>(patched_code_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100727 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
728 if (patch.Type() == kLinkerPatchCallRelative) {
729 // NOTE: Relative calls across oat files are not supported.
730 uint32_t target_offset = GetTargetOffset(patch);
731 uint32_t literal_offset = patch.LiteralOffset();
Vladimir Marko20f85592015-03-19 10:07:02 +0000732 writer_->relative_patcher_->PatchCall(&patched_code_, literal_offset,
Vladimir Markof4da6752014-08-01 19:04:18 +0100733 offset_ + literal_offset, target_offset);
Vladimir Marko20f85592015-03-19 10:07:02 +0000734 } else if (patch.Type() == kLinkerPatchDexCacheArray) {
735 uint32_t target_offset = GetDexCacheOffset(patch);
736 uint32_t literal_offset = patch.LiteralOffset();
737 writer_->relative_patcher_->PatchDexCacheReference(&patched_code_, patch,
738 offset_ + literal_offset,
739 target_offset);
Vladimir Markof4da6752014-08-01 19:04:18 +0100740 } else if (patch.Type() == kLinkerPatchCall) {
741 uint32_t target_offset = GetTargetOffset(patch);
742 PatchCodeAddress(&patched_code_, patch.LiteralOffset(), target_offset);
743 } else if (patch.Type() == kLinkerPatchMethod) {
744 mirror::ArtMethod* method = GetTargetMethod(patch);
745 PatchObjectAddress(&patched_code_, patch.LiteralOffset(), method);
746 } else if (patch.Type() == kLinkerPatchType) {
747 mirror::Class* type = GetTargetType(patch);
748 PatchObjectAddress(&patched_code_, patch.LiteralOffset(), type);
749 }
750 }
751 }
752
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800753 writer_->oat_header_->UpdateChecksum(wrapped.data(), code_size);
754 if (!out->WriteFully(wrapped.data(), code_size)) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100755 ReportWriteFailure("method code", it);
756 return false;
757 }
758 writer_->size_code_ += code_size;
759 offset_ += code_size;
760 }
761 DCHECK_OFFSET_();
762 }
763 ++method_offsets_index_;
764 }
765
766 return true;
767 }
768
769 private:
770 OutputStream* const out_;
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100771 const size_t file_offset_;
772 const ScopedObjectAccess soa_;
773 const ScopedAssertNoThreadSuspension no_thread_suspension_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100774 ClassLinker* const class_linker_;
775 mirror::DexCache* dex_cache_;
776 std::vector<uint8_t> patched_code_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100777
778 void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) {
779 PLOG(ERROR) << "Failed to write " << what << " for "
780 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
781 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100782
783 mirror::ArtMethod* GetTargetMethod(const LinkerPatch& patch)
784 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
785 MethodReference ref = patch.TargetMethod();
786 mirror::DexCache* dex_cache =
787 (dex_file_ == ref.dex_file) ? dex_cache_ : class_linker_->FindDexCache(*ref.dex_file);
788 mirror::ArtMethod* method = dex_cache->GetResolvedMethod(ref.dex_method_index);
789 CHECK(method != nullptr);
790 return method;
791 }
792
793 uint32_t GetTargetOffset(const LinkerPatch& patch) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Vladimir Markob163bb72015-03-31 21:49:49 +0100794 auto target_it = writer_->method_offset_map_.map.find(patch.TargetMethod());
Vladimir Markof4da6752014-08-01 19:04:18 +0100795 uint32_t target_offset =
Vladimir Markob163bb72015-03-31 21:49:49 +0100796 (target_it != writer_->method_offset_map_.map.end()) ? target_it->second : 0u;
Vladimir Markof4da6752014-08-01 19:04:18 +0100797 // If there's no compiled code, point to the correct trampoline.
798 if (UNLIKELY(target_offset == 0)) {
799 mirror::ArtMethod* target = GetTargetMethod(patch);
800 DCHECK(target != nullptr);
Jeff Haoa0acc2d2015-01-27 11:22:04 -0800801 size_t size = GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet());
802 const void* oat_code_offset = target->GetEntryPointFromQuickCompiledCodePtrSize(size);
803 if (oat_code_offset != 0) {
804 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(oat_code_offset));
805 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(oat_code_offset));
806 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(oat_code_offset));
807 target_offset = PointerToLowMemUInt32(oat_code_offset);
808 } else {
809 target_offset = target->IsNative()
810 ? writer_->oat_header_->GetQuickGenericJniTrampolineOffset()
811 : writer_->oat_header_->GetQuickToInterpreterBridgeOffset();
812 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100813 }
814 return target_offset;
815 }
816
817 mirror::Class* GetTargetType(const LinkerPatch& patch)
818 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
819 mirror::DexCache* dex_cache = (dex_file_ == patch.TargetTypeDexFile())
820 ? dex_cache_ : class_linker_->FindDexCache(*patch.TargetTypeDexFile());
821 mirror::Class* type = dex_cache->GetResolvedType(patch.TargetTypeIndex());
822 CHECK(type != nullptr);
823 return type;
824 }
825
Vladimir Marko20f85592015-03-19 10:07:02 +0000826 uint32_t GetDexCacheOffset(const LinkerPatch& patch) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
827 if (writer_->image_writer_ != nullptr) {
828 auto* element = writer_->image_writer_->GetDexCacheArrayElementImageAddress(
829 patch.TargetDexCacheDexFile(), patch.TargetDexCacheElementOffset());
830 const uint8_t* oat_data = writer_->image_writer_->GetOatFileBegin() + file_offset_;
831 return reinterpret_cast<const uint8_t*>(element) - oat_data;
832 } else {
833 LOG(FATAL) << "Unimplemented.";
834 UNREACHABLE();
835 }
836 }
837
Vladimir Markof4da6752014-08-01 19:04:18 +0100838 void PatchObjectAddress(std::vector<uint8_t>* code, uint32_t offset, mirror::Object* object)
839 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
840 // NOTE: Direct method pointers across oat files don't use linker patches. However, direct
841 // type pointers across oat files do. (TODO: Investigate why.)
842 if (writer_->image_writer_ != nullptr) {
843 object = writer_->image_writer_->GetImageAddress(object);
844 }
845 uint32_t address = PointerToLowMemUInt32(object);
846 DCHECK_LE(offset + 4, code->size());
847 uint8_t* data = &(*code)[offset];
848 data[0] = address & 0xffu;
849 data[1] = (address >> 8) & 0xffu;
850 data[2] = (address >> 16) & 0xffu;
851 data[3] = (address >> 24) & 0xffu;
852 }
853
854 void PatchCodeAddress(std::vector<uint8_t>* code, uint32_t offset, uint32_t target_offset)
855 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Jeff Haoa0acc2d2015-01-27 11:22:04 -0800856 uint32_t address = writer_->image_writer_ == nullptr ? target_offset :
857 PointerToLowMemUInt32(writer_->image_writer_->GetOatFileBegin() +
858 writer_->oat_data_offset_ + target_offset);
Vladimir Markof4da6752014-08-01 19:04:18 +0100859 DCHECK_LE(offset + 4, code->size());
860 uint8_t* data = &(*code)[offset];
861 data[0] = address & 0xffu;
862 data[1] = (address >> 8) & 0xffu;
863 data[2] = (address >> 16) & 0xffu;
864 data[3] = (address >> 24) & 0xffu;
865 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100866};
867
868template <typename DataAccess>
869class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor {
870 public:
871 WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800872 size_t relative_offset)
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100873 : OatDexMethodVisitor(writer, relative_offset),
874 out_(out),
875 file_offset_(file_offset) {
876 }
877
878 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
879 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
880 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
881
882 if (compiled_method != NULL) { // ie. not an abstract method
883 size_t file_offset = file_offset_;
884 OutputStream* out = out_;
885
886 uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_);
887 ++method_offsets_index_;
888
889 // Write deduplicated map.
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800890 const SwapVector<uint8_t>* map = DataAccess::GetData(compiled_method);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100891 size_t map_size = map == nullptr ? 0 : map->size() * sizeof((*map)[0]);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100892 DCHECK((map_size == 0u && map_offset == 0u) ||
893 (map_size != 0u && map_offset != 0u && map_offset <= offset_))
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800894 << map_size << " " << map_offset << " " << offset_ << " "
895 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " for " << DataAccess::Name();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100896 if (map_size != 0u && map_offset == offset_) {
897 if (UNLIKELY(!out->WriteFully(&(*map)[0], map_size))) {
898 ReportWriteFailure(it);
899 return false;
900 }
901 offset_ += map_size;
902 }
903 DCHECK_OFFSET_();
904 }
905
906 return true;
907 }
908
909 private:
910 OutputStream* const out_;
911 size_t const file_offset_;
912
913 void ReportWriteFailure(const ClassDataItemIterator& it) {
914 PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for "
915 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
916 }
917};
918
919// Visit all methods from all classes in all dex files with the specified visitor.
920bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) {
921 for (const DexFile* dex_file : *dex_files_) {
922 const size_t class_def_count = dex_file->NumClassDefs();
923 for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) {
924 if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) {
925 return false;
926 }
927 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700928 const uint8_t* class_data = dex_file->GetClassData(class_def);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100929 if (class_data != NULL) { // ie not an empty class, such as a marker interface
930 ClassDataItemIterator it(*dex_file, class_data);
931 while (it.HasNextStaticField()) {
932 it.Next();
933 }
934 while (it.HasNextInstanceField()) {
935 it.Next();
936 }
937 size_t class_def_method_index = 0u;
938 while (it.HasNextDirectMethod()) {
939 if (!visitor->VisitMethod(class_def_method_index, it)) {
940 return false;
941 }
942 ++class_def_method_index;
943 it.Next();
944 }
945 while (it.HasNextVirtualMethod()) {
946 if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) {
947 return false;
948 }
949 ++class_def_method_index;
950 it.Next();
951 }
952 }
953 if (UNLIKELY(!visitor->EndClass())) {
954 return false;
955 }
956 }
957 }
958 return true;
959}
960
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700961size_t OatWriter::InitOatHeader() {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700962 oat_header_ = OatHeader::Create(compiler_driver_->GetInstructionSet(),
963 compiler_driver_->GetInstructionSetFeatures(),
964 dex_files_,
965 image_file_location_oat_checksum_,
966 image_file_location_oat_begin_,
967 key_value_store_);
968
969 return oat_header_->GetHeaderSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700970}
971
972size_t OatWriter::InitOatDexFiles(size_t offset) {
973 // create the OatDexFiles
974 for (size_t i = 0; i != dex_files_->size(); ++i) {
975 const DexFile* dex_file = (*dex_files_)[i];
976 CHECK(dex_file != NULL);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800977 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700978 oat_dex_files_.push_back(oat_dex_file);
979 offset += oat_dex_file->SizeOf();
980 }
981 return offset;
982}
983
Brian Carlstrom89521892011-12-07 22:05:07 -0800984size_t OatWriter::InitDexFiles(size_t offset) {
985 // calculate the offsets within OatDexFiles to the DexFiles
986 for (size_t i = 0; i != dex_files_->size(); ++i) {
987 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700988 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800989 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700990 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800991
992 // set offset in OatDexFile to DexFile
993 oat_dex_files_[i]->dex_file_offset_ = offset;
994
995 const DexFile* dex_file = (*dex_files_)[i];
996 offset += dex_file->GetHeader().file_size_;
997 }
998 return offset;
999}
1000
Brian Carlstrom389efb02012-01-11 12:06:26 -08001001size_t OatWriter::InitOatClasses(size_t offset) {
Brian Carlstrom389efb02012-01-11 12:06:26 -08001002 // calculate the offsets within OatDexFiles to OatClasses
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001003 InitOatClassesMethodVisitor visitor(this, offset);
1004 bool success = VisitDexMethods(&visitor);
1005 CHECK(success);
1006 offset = visitor.GetOffset();
Brian Carlstromba150c32013-08-27 17:31:03 -07001007
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001008 // Update oat_dex_files_.
1009 auto oat_class_it = oat_classes_.begin();
1010 for (OatDexFile* oat_dex_file : oat_dex_files_) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001011 for (uint32_t& method_offset : oat_dex_file->methods_offsets_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001012 DCHECK(oat_class_it != oat_classes_.end());
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001013 method_offset = (*oat_class_it)->offset_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001014 ++oat_class_it;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001015 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001016 oat_dex_file->UpdateChecksum(oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001017 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001018 CHECK(oat_class_it == oat_classes_.end());
1019
1020 return offset;
1021}
1022
1023size_t OatWriter::InitOatMaps(size_t offset) {
1024 #define VISIT(VisitorType) \
1025 do { \
1026 VisitorType visitor(this, offset); \
1027 bool success = VisitDexMethods(&visitor); \
1028 DCHECK(success); \
1029 offset = visitor.GetOffset(); \
1030 } while (false)
1031
1032 VISIT(InitMapMethodVisitor<GcMapDataAccess>);
1033 VISIT(InitMapMethodVisitor<MappingTableDataAccess>);
1034 VISIT(InitMapMethodVisitor<VmapTableDataAccess>);
1035
1036 #undef VISIT
1037
Brian Carlstrome24fa612011-09-29 00:53:55 -07001038 return offset;
1039}
1040
1041size_t OatWriter::InitOatCode(size_t offset) {
1042 // calculate the offsets within OatHeader to executable code
1043 size_t old_offset = offset;
Dave Allison50abf0a2014-06-23 13:19:59 -07001044 size_t adjusted_offset = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001045 // required to be on a new page boundary
1046 offset = RoundUp(offset, kPageSize);
1047 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001048 size_executable_offset_alignment_ = offset - old_offset;
1049 if (compiler_driver_->IsImage()) {
Alex Lighta59dd802014-07-02 16:28:08 -07001050 CHECK_EQ(image_patch_delta_, 0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001051 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001052
Ian Rogers848871b2013-08-05 10:56:33 -07001053 #define DO_TRAMPOLINE(field, fn_name) \
1054 offset = CompiledCode::AlignCode(offset, instruction_set); \
Dave Allison50abf0a2014-06-23 13:19:59 -07001055 adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \
1056 oat_header_->Set ## fn_name ## Offset(adjusted_offset); \
Ian Rogers848871b2013-08-05 10:56:33 -07001057 field.reset(compiler_driver_->Create ## fn_name()); \
1058 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001059
Ian Rogers848871b2013-08-05 10:56:33 -07001060 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge);
1061 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge);
1062 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Andreas Gampe2da88232014-02-27 12:26:20 -08001063 DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline);
Jeff Hao88474b42013-10-23 16:24:40 -07001064 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -07001065 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
1066 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001067
Ian Rogers848871b2013-08-05 10:56:33 -07001068 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001069 } else {
Ian Rogers848871b2013-08-05 10:56:33 -07001070 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
1071 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
1072 oat_header_->SetJniDlsymLookupOffset(0);
Andreas Gampe2da88232014-02-27 12:26:20 -08001073 oat_header_->SetQuickGenericJniTrampolineOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -07001074 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001075 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -07001076 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Alex Lighta59dd802014-07-02 16:28:08 -07001077 oat_header_->SetImagePatchDelta(image_patch_delta_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001078 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001079 return offset;
1080}
1081
1082size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001083 #define VISIT(VisitorType) \
1084 do { \
1085 VisitorType visitor(this, offset); \
1086 bool success = VisitDexMethods(&visitor); \
1087 DCHECK(success); \
1088 offset = visitor.GetOffset(); \
1089 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001090
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001091 VISIT(InitCodeMethodVisitor);
Ian Rogers1212a022013-03-04 10:48:41 -08001092 if (compiler_driver_->IsImage()) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001093 VISIT(InitImageMethodVisitor);
Ian Rogers0571d352011-11-03 19:51:38 -07001094 }
Logan Chien8b977d32012-02-21 19:14:55 +08001095
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001096 #undef VISIT
1097
Brian Carlstrome24fa612011-09-29 00:53:55 -07001098 return offset;
1099}
1100
Ian Rogers3d504072014-03-01 09:16:49 -08001101bool OatWriter::Write(OutputStream* out) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001102 const off_t raw_file_offset = out->Seek(0, kSeekCurrent);
1103 if (raw_file_offset == (off_t) -1) {
1104 LOG(ERROR) << "Failed to get file offset in " << out->GetLocation();
1105 return false;
1106 }
1107 const size_t file_offset = static_cast<size_t>(raw_file_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001108
Vladimir Markof4da6752014-08-01 19:04:18 +01001109 // Reserve space for header. It will be written last - after updating the checksum.
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001110 size_t header_size = oat_header_->GetHeaderSize();
Vladimir Markof4da6752014-08-01 19:04:18 +01001111 if (out->Seek(header_size, kSeekCurrent) == (off_t) -1) {
1112 PLOG(ERROR) << "Failed to reserve space for oat header in " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001113 return false;
1114 }
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001115 size_oat_header_ += sizeof(OatHeader);
1116 size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001117
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001118 if (!WriteTables(out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001119 LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001120 return false;
1121 }
1122
Vladimir Markof4da6752014-08-01 19:04:18 +01001123 off_t tables_end_offset = out->Seek(0, kSeekCurrent);
1124 if (tables_end_offset == (off_t) -1) {
1125 LOG(ERROR) << "Failed to seek to oat code position in " << out->GetLocation();
1126 return false;
1127 }
1128 size_t relative_offset = static_cast<size_t>(tables_end_offset) - file_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001129 relative_offset = WriteMaps(out, file_offset, relative_offset);
1130 if (relative_offset == 0) {
1131 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
1132 return false;
1133 }
1134
1135 relative_offset = WriteCode(out, file_offset, relative_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001136 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001137 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001138 return false;
1139 }
1140
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001141 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
1142 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001143 LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001144 return false;
1145 }
1146
Vladimir Markof4da6752014-08-01 19:04:18 +01001147 const off_t oat_end_file_offset = out->Seek(0, kSeekCurrent);
1148 if (oat_end_file_offset == (off_t) -1) {
1149 LOG(ERROR) << "Failed to get oat end file offset in " << out->GetLocation();
1150 return false;
1151 }
1152
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001153 if (kIsDebugBuild) {
1154 uint32_t size_total = 0;
1155 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001156 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001157 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001158
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001159 DO_STAT(size_dex_file_alignment_);
1160 DO_STAT(size_executable_offset_alignment_);
1161 DO_STAT(size_oat_header_);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001162 DO_STAT(size_oat_header_key_value_store_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001163 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -07001164 DO_STAT(size_interpreter_to_interpreter_bridge_);
1165 DO_STAT(size_interpreter_to_compiled_code_bridge_);
1166 DO_STAT(size_jni_dlsym_lookup_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001167 DO_STAT(size_quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001168 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001169 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001170 DO_STAT(size_quick_to_interpreter_bridge_);
1171 DO_STAT(size_trampoline_alignment_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001172 DO_STAT(size_method_header_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001173 DO_STAT(size_code_);
1174 DO_STAT(size_code_alignment_);
Vladimir Markof4da6752014-08-01 19:04:18 +01001175 DO_STAT(size_relative_call_thunks_);
Vladimir Markoc74658b2015-03-31 10:26:41 +01001176 DO_STAT(size_misc_thunks_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001177 DO_STAT(size_mapping_table_);
1178 DO_STAT(size_vmap_table_);
1179 DO_STAT(size_gc_map_);
1180 DO_STAT(size_oat_dex_file_location_size_);
1181 DO_STAT(size_oat_dex_file_location_data_);
1182 DO_STAT(size_oat_dex_file_location_checksum_);
1183 DO_STAT(size_oat_dex_file_offset_);
1184 DO_STAT(size_oat_dex_file_methods_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001185 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001186 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001187 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001188 DO_STAT(size_oat_class_method_offsets_);
1189 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001190
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001191 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Vladimir Markof4da6752014-08-01 19:04:18 +01001192 CHECK_EQ(file_offset + size_total, static_cast<size_t>(oat_end_file_offset));
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001193 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001194 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001195
Vladimir Markof4da6752014-08-01 19:04:18 +01001196 CHECK_EQ(file_offset + size_, static_cast<size_t>(oat_end_file_offset));
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001197 CHECK_EQ(size_, relative_offset);
1198
Vladimir Markof4da6752014-08-01 19:04:18 +01001199 // Write the header now that the checksum is final.
1200 if (out->Seek(file_offset, kSeekSet) == (off_t) -1) {
1201 PLOG(ERROR) << "Failed to seek to oat header position in " << out->GetLocation();
1202 return false;
1203 }
1204 DCHECK_EQ(raw_file_offset, out->Seek(0, kSeekCurrent));
1205 if (!out->WriteFully(oat_header_, header_size)) {
1206 PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation();
1207 return false;
1208 }
1209 if (out->Seek(oat_end_file_offset, kSeekSet) == (off_t) -1) {
1210 PLOG(ERROR) << "Failed to seek to end after writing oat header to " << out->GetLocation();
1211 return false;
1212 }
1213 DCHECK_EQ(oat_end_file_offset, out->Seek(0, kSeekCurrent));
1214
Brian Carlstrome24fa612011-09-29 00:53:55 -07001215 return true;
1216}
1217
Ian Rogers3d504072014-03-01 09:16:49 -08001218bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001219 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001220 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001221 PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001222 return false;
1223 }
1224 }
Brian Carlstrom89521892011-12-07 22:05:07 -08001225 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001226 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Ian Rogers3d504072014-03-01 09:16:49 -08001227 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -08001228 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
1229 const DexFile* dex_file = (*dex_files_)[i];
1230 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
1231 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
1232 return false;
1233 }
1234 const DexFile* dex_file = (*dex_files_)[i];
Ian Rogers3d504072014-03-01 09:16:49 -08001235 if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001236 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
Ian Rogers3d504072014-03-01 09:16:49 -08001237 << " to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001238 return false;
1239 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001240 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -08001241 }
Brian Carlstrom389efb02012-01-11 12:06:26 -08001242 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001243 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001244 PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001245 return false;
1246 }
1247 }
1248 return true;
1249}
1250
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001251size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) {
1252 #define VISIT(VisitorType) \
1253 do { \
1254 VisitorType visitor(this, out, file_offset, relative_offset); \
1255 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1256 return 0; \
1257 } \
1258 relative_offset = visitor.GetOffset(); \
1259 } while (false)
1260
1261 size_t gc_maps_offset = relative_offset;
1262 VISIT(WriteMapMethodVisitor<GcMapDataAccess>);
1263 size_gc_map_ = relative_offset - gc_maps_offset;
1264
1265 size_t mapping_tables_offset = relative_offset;
1266 VISIT(WriteMapMethodVisitor<MappingTableDataAccess>);
1267 size_mapping_table_ = relative_offset - mapping_tables_offset;
1268
1269 size_t vmap_tables_offset = relative_offset;
1270 VISIT(WriteMapMethodVisitor<VmapTableDataAccess>);
1271 size_vmap_table_ = relative_offset - vmap_tables_offset;
1272
1273 #undef VISIT
1274
1275 return relative_offset;
1276}
1277
1278size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) {
Ian Rogers3d504072014-03-01 09:16:49 -08001279 off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001280 relative_offset += size_executable_offset_alignment_;
1281 DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset());
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001282 size_t expected_file_offset = file_offset + relative_offset;
1283 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001284 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Ian Rogers3d504072014-03-01 09:16:49 -08001285 << " Expected: " << expected_file_offset << " File: " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001286 return 0;
1287 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001288 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001289 if (compiler_driver_->IsImage()) {
1290 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001291
Ian Rogers848871b2013-08-05 10:56:33 -07001292 #define DO_TRAMPOLINE(field) \
1293 do { \
1294 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
1295 uint32_t alignment_padding = aligned_offset - relative_offset; \
Ian Rogers3d504072014-03-01 09:16:49 -08001296 out->Seek(alignment_padding, kSeekCurrent); \
Ian Rogers848871b2013-08-05 10:56:33 -07001297 size_trampoline_alignment_ += alignment_padding; \
Ian Rogers3d504072014-03-01 09:16:49 -08001298 if (!out->WriteFully(&(*field)[0], field->size())) { \
1299 PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \
Ian Rogers848871b2013-08-05 10:56:33 -07001300 return false; \
1301 } \
1302 size_ ## field += field->size(); \
1303 relative_offset += alignment_padding + field->size(); \
1304 DCHECK_OFFSET(); \
1305 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001306
Ian Rogers848871b2013-08-05 10:56:33 -07001307 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_);
1308 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_);
1309 DO_TRAMPOLINE(jni_dlsym_lookup_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001310 DO_TRAMPOLINE(quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001311 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001312 DO_TRAMPOLINE(quick_resolution_trampoline_);
1313 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
1314 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001315 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001316 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001317}
1318
Ian Rogers3d504072014-03-01 09:16:49 -08001319size_t OatWriter::WriteCodeDexFiles(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001320 const size_t file_offset,
1321 size_t relative_offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001322 #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)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001330
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001331 VISIT(WriteCodeMethodVisitor);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001332
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001333 #undef VISIT
Brian Carlstrom265091e2013-01-30 14:08:26 -08001334
Vladimir Markob163bb72015-03-31 21:49:49 +01001335 size_code_alignment_ += relative_patcher_->CodeAlignmentSize();
1336 size_relative_call_thunks_ += relative_patcher_->RelativeCallThunksSize();
1337 size_misc_thunks_ += relative_patcher_->MiscThunksSize();
1338
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001339 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001340}
1341
Vladimir Markof4da6752014-08-01 19:04:18 +01001342bool OatWriter::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) {
1343 static const uint8_t kPadding[] = {
1344 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
1345 };
1346 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
1347 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
1348 return false;
1349 }
1350 size_code_alignment_ += aligned_code_delta;
1351 return true;
1352}
1353
Vladimir Markob163bb72015-03-31 21:49:49 +01001354std::pair<bool, uint32_t> OatWriter::MethodOffsetMap::FindMethodOffset(MethodReference ref) {
1355 auto it = map.find(ref);
1356 if (it == map.end()) {
1357 return std::pair<bool, uint32_t>(false, 0u);
1358 } else {
1359 return std::pair<bool, uint32_t>(true, it->second);
1360 }
1361}
1362
Brian Carlstrom265091e2013-01-30 14:08:26 -08001363OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
1364 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -08001365 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001366 dex_file_location_size_ = location.size();
1367 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001368 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -08001369 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001370 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001371}
1372
1373size_t OatWriter::OatDexFile::SizeOf() const {
1374 return sizeof(dex_file_location_size_)
1375 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001376 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -08001377 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001378 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001379}
1380
Ian Rogers3d504072014-03-01 09:16:49 -08001381void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const {
1382 oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
1383 oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
1384 oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
1385 oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
1386 oat_header->UpdateChecksum(&methods_offsets_[0],
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001387 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001388}
1389
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001390bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001391 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001392 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001393 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001394 if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
1395 PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001396 return false;
1397 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001398 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001399 if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
1400 PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001401 return false;
1402 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001403 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Ian Rogers3d504072014-03-01 09:16:49 -08001404 if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
1405 PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001406 return false;
1407 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001408 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Ian Rogers3d504072014-03-01 09:16:49 -08001409 if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
1410 PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001411 return false;
1412 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001413 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Ian Rogers3d504072014-03-01 09:16:49 -08001414 if (!out->WriteFully(&methods_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001415 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001416 PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001417 return false;
1418 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001419 oat_writer->size_oat_dex_file_methods_offsets_ +=
1420 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001421 return true;
1422}
1423
Brian Carlstromba150c32013-08-27 17:31:03 -07001424OatWriter::OatClass::OatClass(size_t offset,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001425 const std::vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -07001426 uint32_t num_non_null_compiled_methods,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001427 mirror::Class::Status status)
1428 : compiled_methods_(compiled_methods) {
1429 uint32_t num_methods = compiled_methods.size();
Brian Carlstromba150c32013-08-27 17:31:03 -07001430 CHECK_LE(num_non_null_compiled_methods, num_methods);
1431
Brian Carlstrom265091e2013-01-30 14:08:26 -08001432 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -07001433 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
1434
1435 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
1436 // apply when there are 0 methods, we just arbitrarily say that 0
1437 // methods means kOatClassNoneCompiled and that we won't use
1438 // kOatClassAllCompiled unless there is at least one compiled
1439 // method. This means in an interpretter only system, we can assert
1440 // that all classes are kOatClassNoneCompiled.
1441 if (num_non_null_compiled_methods == 0) {
1442 type_ = kOatClassNoneCompiled;
1443 } else if (num_non_null_compiled_methods == num_methods) {
1444 type_ = kOatClassAllCompiled;
1445 } else {
1446 type_ = kOatClassSomeCompiled;
1447 }
1448
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001449 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -07001450 method_offsets_.resize(num_non_null_compiled_methods);
Vladimir Marko8a630572014-04-09 18:45:35 +01001451 method_headers_.resize(num_non_null_compiled_methods);
Brian Carlstromba150c32013-08-27 17:31:03 -07001452
1453 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
1454 if (type_ == kOatClassSomeCompiled) {
1455 method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator());
1456 method_bitmap_size_ = method_bitmap_->GetSizeOf();
1457 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
1458 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
1459 } else {
1460 method_bitmap_ = NULL;
1461 method_bitmap_size_ = 0;
1462 }
1463
1464 for (size_t i = 0; i < num_methods; i++) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001465 CompiledMethod* compiled_method = compiled_methods_[i];
Brian Carlstromba150c32013-08-27 17:31:03 -07001466 if (compiled_method == NULL) {
1467 oat_method_offsets_offsets_from_oat_class_[i] = 0;
1468 } else {
1469 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
1470 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
1471 if (type_ == kOatClassSomeCompiled) {
1472 method_bitmap_->SetBit(i);
1473 }
1474 }
1475 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001476}
1477
Brian Carlstromba150c32013-08-27 17:31:03 -07001478OatWriter::OatClass::~OatClass() {
Mathieu Chartier661974a2014-01-09 11:23:53 -08001479 delete method_bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -07001480}
1481
Brian Carlstrom265091e2013-01-30 14:08:26 -08001482size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
1483 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001484 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
1485 if (method_offset == 0) {
1486 return 0;
1487 }
1488 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001489}
1490
1491size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
1492 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001493 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08001494}
1495
1496size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001497 return sizeof(status_)
1498 + sizeof(type_)
1499 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
1500 + method_bitmap_size_
1501 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001502}
1503
Ian Rogers3d504072014-03-01 09:16:49 -08001504void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const {
1505 oat_header->UpdateChecksum(&status_, sizeof(status_));
1506 oat_header->UpdateChecksum(&type_, sizeof(type_));
Brian Carlstromba150c32013-08-27 17:31:03 -07001507 if (method_bitmap_size_ != 0) {
1508 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001509 oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_));
1510 oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001511 }
Ian Rogers3d504072014-03-01 09:16:49 -08001512 oat_header->UpdateChecksum(&method_offsets_[0],
1513 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001514}
1515
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001516bool OatWriter::OatClass::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001517 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001518 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001519 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001520 if (!out->WriteFully(&status_, sizeof(status_))) {
1521 PLOG(ERROR) << "Failed to write class status to " << out->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001522 return false;
1523 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001524 oat_writer->size_oat_class_status_ += sizeof(status_);
Ian Rogers3d504072014-03-01 09:16:49 -08001525 if (!out->WriteFully(&type_, sizeof(type_))) {
1526 PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001527 return false;
1528 }
1529 oat_writer->size_oat_class_type_ += sizeof(type_);
1530 if (method_bitmap_size_ != 0) {
1531 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001532 if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
1533 PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001534 return false;
1535 }
1536 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001537 if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
1538 PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001539 return false;
1540 }
1541 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1542 }
Ian Rogers3d504072014-03-01 09:16:49 -08001543 if (!out->WriteFully(&method_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001544 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001545 PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001546 return false;
1547 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001548 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001549 return true;
1550}
1551
Brian Carlstrome24fa612011-09-29 00:53:55 -07001552} // namespace art