blob: 63a3c8ccdc51d513682179015ad1f6337d29200f [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
Brian Carlstromba150c32013-08-27 17:31:03 -070021#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080022#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080023#include "base/unix_file/fd_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070024#include "class_linker.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070025#include "compiled_class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070026#include "dex_file-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000027#include "dex/verification_results.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/space/space.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070029#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "mirror/array.h"
31#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070032#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070033#include "os.h"
Brian Carlstromcd60ac72013-01-20 17:09:51 -080034#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070035#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070036#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070037#include "handle_scope-inl.h"
jeffhaoec014232012-09-05 10:42:25 -070038#include "verifier/method_verifier.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070039
40namespace art {
41
Vladimir Marko96c6ab92014-04-08 14:00:50 +010042#define DCHECK_OFFSET() \
43 DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out->Seek(0, kSeekCurrent)) \
44 << "file_offset=" << file_offset << " relative_offset=" << relative_offset
45
46#define DCHECK_OFFSET_() \
47 DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out->Seek(0, kSeekCurrent)) \
48 << "file_offset=" << file_offset << " offset_=" << offset_
49
Brian Carlstrom3320cf42011-10-04 14:58:28 -070050OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070051 uint32_t image_file_location_oat_checksum,
Ian Rogersef7d42f2014-01-06 12:55:46 -080052 uintptr_t image_file_location_oat_begin,
Ian Rogersca368cb2013-11-15 15:52:08 -080053 const CompilerDriver* compiler,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070054 TimingLogger* timings,
55 SafeMap<std::string, std::string>* key_value_store)
Jeff Hao0aba0ba2013-06-03 14:49:28 -070056 : compiler_driver_(compiler),
57 dex_files_(&dex_files),
58 image_file_location_oat_checksum_(image_file_location_oat_checksum),
59 image_file_location_oat_begin_(image_file_location_oat_begin),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070060 key_value_store_(key_value_store),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070061 oat_header_(NULL),
62 size_dex_file_alignment_(0),
63 size_executable_offset_alignment_(0),
64 size_oat_header_(0),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070065 size_oat_header_key_value_store_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070066 size_dex_file_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070067 size_interpreter_to_interpreter_bridge_(0),
68 size_interpreter_to_compiled_code_bridge_(0),
69 size_jni_dlsym_lookup_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070070 size_portable_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070071 size_portable_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070072 size_portable_to_interpreter_bridge_(0),
Andreas Gampe2da88232014-02-27 12:26:20 -080073 size_quick_generic_jni_trampoline_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070074 size_quick_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070075 size_quick_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070076 size_quick_to_interpreter_bridge_(0),
77 size_trampoline_alignment_(0),
Vladimir Marko96c6ab92014-04-08 14:00:50 +010078 size_method_header_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070079 size_code_(0),
80 size_code_alignment_(0),
81 size_mapping_table_(0),
82 size_vmap_table_(0),
83 size_gc_map_(0),
84 size_oat_dex_file_location_size_(0),
85 size_oat_dex_file_location_data_(0),
86 size_oat_dex_file_location_checksum_(0),
87 size_oat_dex_file_offset_(0),
88 size_oat_dex_file_methods_offsets_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -070089 size_oat_class_type_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070090 size_oat_class_status_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -070091 size_oat_class_method_bitmaps_(0),
Dave Allisond6ed6422014-04-09 23:36:15 +000092 size_oat_class_method_offsets_(0) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070093 CHECK(key_value_store != nullptr);
94
Ian Rogersca368cb2013-11-15 15:52:08 -080095 size_t offset;
96 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -070097 TimingLogger::ScopedTiming split("InitOatHeader", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -080098 offset = InitOatHeader();
99 }
100 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700101 TimingLogger::ScopedTiming split("InitOatDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800102 offset = InitOatDexFiles(offset);
103 }
104 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700105 TimingLogger::ScopedTiming split("InitDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800106 offset = InitDexFiles(offset);
107 }
108 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700109 TimingLogger::ScopedTiming split("InitOatClasses", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800110 offset = InitOatClasses(offset);
111 }
112 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700113 TimingLogger::ScopedTiming split("InitOatMaps", timings);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100114 offset = InitOatMaps(offset);
115 }
116 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700117 TimingLogger::ScopedTiming split("InitOatCode", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800118 offset = InitOatCode(offset);
119 }
120 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700121 TimingLogger::ScopedTiming split("InitOatCodeDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800122 offset = InitOatCodeDexFiles(offset);
123 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700124 size_ = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700125
126 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700127 CHECK_EQ(compiler->IsImage(),
128 key_value_store_->find(OatHeader::kImageLocationKey) == key_value_store_->end());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700129}
130
Ian Rogers0571d352011-11-03 19:51:38 -0700131OatWriter::~OatWriter() {
132 delete oat_header_;
133 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800134 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -0700135}
136
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100137struct OatWriter::GcMapDataAccess {
138 static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
139 return &compiled_method->GetGcMap();
140 }
141
142 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
143 return oat_class->method_offsets_[method_offsets_index].gc_map_offset_;
144 }
145
146 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
147 ALWAYS_INLINE {
148 oat_class->method_offsets_[method_offsets_index].gc_map_offset_ = offset;
149 }
150
151 static const char* Name() ALWAYS_INLINE {
152 return "GC map";
153 }
154};
155
156struct OatWriter::MappingTableDataAccess {
157 static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
158 return &compiled_method->GetMappingTable();
159 }
160
161 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100162 uint32_t offset = oat_class->method_headers_[method_offsets_index].mapping_table_offset_;
163 return offset == 0u ? 0u :
164 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100165 }
166
167 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
168 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100169 oat_class->method_headers_[method_offsets_index].mapping_table_offset_ =
170 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100171 }
172
173 static const char* Name() ALWAYS_INLINE {
174 return "mapping table";
175 }
176};
177
178struct OatWriter::VmapTableDataAccess {
179 static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
180 return &compiled_method->GetVmapTable();
181 }
182
183 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100184 uint32_t offset = oat_class->method_headers_[method_offsets_index].vmap_table_offset_;
185 return offset == 0u ? 0u :
186 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100187 }
188
189 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
190 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100191 oat_class->method_headers_[method_offsets_index].vmap_table_offset_ =
192 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100193 }
194
195 static const char* Name() ALWAYS_INLINE {
196 return "vmap table";
197 }
198};
199
200class OatWriter::DexMethodVisitor {
201 public:
202 DexMethodVisitor(OatWriter* writer, size_t offset)
203 : writer_(writer),
204 offset_(offset),
205 dex_file_(nullptr),
206 class_def_index_(DexFile::kDexNoIndex) {
207 }
208
209 virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) {
210 DCHECK(dex_file_ == nullptr);
211 DCHECK_EQ(class_def_index_, DexFile::kDexNoIndex);
212 dex_file_ = dex_file;
213 class_def_index_ = class_def_index;
214 return true;
215 }
216
217 virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0;
218
219 virtual bool EndClass() {
220 if (kIsDebugBuild) {
221 dex_file_ = nullptr;
222 class_def_index_ = DexFile::kDexNoIndex;
223 }
224 return true;
225 }
226
227 size_t GetOffset() const {
228 return offset_;
229 }
230
231 protected:
232 virtual ~DexMethodVisitor() { }
233
234 OatWriter* const writer_;
235
236 // The offset is usually advanced for each visited method by the derived class.
237 size_t offset_;
238
239 // The dex file and class def index are set in StartClass().
240 const DexFile* dex_file_;
241 size_t class_def_index_;
242};
243
244class OatWriter::OatDexMethodVisitor : public DexMethodVisitor {
245 public:
246 OatDexMethodVisitor(OatWriter* writer, size_t offset)
247 : DexMethodVisitor(writer, offset),
248 oat_class_index_(0u),
249 method_offsets_index_(0u) {
250 }
251
252 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
253 DexMethodVisitor::StartClass(dex_file, class_def_index);
254 DCHECK_LT(oat_class_index_, writer_->oat_classes_.size());
255 method_offsets_index_ = 0u;
256 return true;
257 }
258
259 bool EndClass() {
260 ++oat_class_index_;
261 return DexMethodVisitor::EndClass();
262 }
263
264 protected:
265 size_t oat_class_index_;
266 size_t method_offsets_index_;
267};
268
269class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor {
270 public:
271 InitOatClassesMethodVisitor(OatWriter* writer, size_t offset)
272 : DexMethodVisitor(writer, offset),
273 compiled_methods_(),
274 num_non_null_compiled_methods_(0u) {
275 compiled_methods_.reserve(256u);
276 }
277
278 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
279 DexMethodVisitor::StartClass(dex_file, class_def_index);
280 compiled_methods_.clear();
281 num_non_null_compiled_methods_ = 0u;
282 return true;
283 }
284
285 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
286 // Fill in the compiled_methods_ array for methods that have a
287 // CompiledMethod. We track the number of non-null entries in
288 // num_non_null_compiled_methods_ since we only want to allocate
289 // OatMethodOffsets for the compiled methods.
290 uint32_t method_idx = it.GetMemberIndex();
291 CompiledMethod* compiled_method =
292 writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx));
293 compiled_methods_.push_back(compiled_method);
294 if (compiled_method != nullptr) {
295 ++num_non_null_compiled_methods_;
296 }
297 return true;
298 }
299
300 bool EndClass() {
301 ClassReference class_ref(dex_file_, class_def_index_);
302 CompiledClass* compiled_class = writer_->compiler_driver_->GetCompiledClass(class_ref);
303 mirror::Class::Status status;
304 if (compiled_class != NULL) {
305 status = compiled_class->GetStatus();
306 } else if (writer_->compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) {
307 status = mirror::Class::kStatusError;
308 } else {
309 status = mirror::Class::kStatusNotReady;
310 }
311
312 OatClass* oat_class = new OatClass(offset_, compiled_methods_,
313 num_non_null_compiled_methods_, status);
314 writer_->oat_classes_.push_back(oat_class);
315 offset_ += oat_class->SizeOf();
316 return DexMethodVisitor::EndClass();
317 }
318
319 private:
320 std::vector<CompiledMethod*> compiled_methods_;
321 size_t num_non_null_compiled_methods_;
322};
323
324class OatWriter::InitCodeMethodVisitor : public OatDexMethodVisitor {
325 public:
326 InitCodeMethodVisitor(OatWriter* writer, size_t offset)
327 : OatDexMethodVisitor(writer, offset) {
328 }
329
330 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
331 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
332 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
333 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
334
335 if (compiled_method != nullptr) {
336 // Derived from CompiledMethod.
337 uint32_t quick_code_offset = 0;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100338
339 const std::vector<uint8_t>* portable_code = compiled_method->GetPortableCode();
340 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode();
341 if (portable_code != nullptr) {
342 CHECK(quick_code == nullptr);
343 size_t oat_method_offsets_offset =
344 oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index);
345 compiled_method->AddOatdataOffsetToCompliledCodeOffset(
346 oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_));
347 } else {
348 CHECK(quick_code != nullptr);
349 offset_ = compiled_method->AlignCode(offset_);
350 DCHECK_ALIGNED_PARAM(offset_,
351 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
352 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
353 CHECK_NE(code_size, 0U);
354 uint32_t thumb_offset = compiled_method->CodeDelta();
Vladimir Marko7624d252014-05-02 14:40:15 +0100355 quick_code_offset = offset_ + sizeof(OatQuickMethodHeader) + thumb_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100356
Alex Light78382fa2014-06-06 15:45:32 -0700357 bool force_debug_capture = false;
358 bool deduped = false;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100359
360 // Deduplicate code arrays.
Vladimir Markobd72fc12014-07-09 16:06:40 +0100361 auto lb = dedupe_map_.lower_bound(compiled_method);
362 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(compiled_method, lb->first)) {
363 quick_code_offset = lb->second;
Alex Light78382fa2014-06-06 15:45:32 -0700364 deduped = true;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100365 } else {
Vladimir Markobd72fc12014-07-09 16:06:40 +0100366 dedupe_map_.PutBefore(lb, compiled_method, quick_code_offset);
Vladimir Marko7624d252014-05-02 14:40:15 +0100367 }
368
369 // Update quick method header.
370 DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size());
371 OatQuickMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_];
372 uint32_t mapping_table_offset = method_header->mapping_table_offset_;
373 uint32_t vmap_table_offset = method_header->vmap_table_offset_;
374 // The code offset was 0 when the mapping/vmap table offset was set, so it's set
375 // to 0-offset and we need to adjust it by code_offset.
376 uint32_t code_offset = quick_code_offset - thumb_offset;
377 if (mapping_table_offset != 0u) {
378 mapping_table_offset += code_offset;
379 DCHECK_LT(mapping_table_offset, code_offset);
380 }
381 if (vmap_table_offset != 0u) {
382 vmap_table_offset += code_offset;
383 DCHECK_LT(vmap_table_offset, code_offset);
384 }
385 uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
386 uint32_t core_spill_mask = compiled_method->GetCoreSpillMask();
387 uint32_t fp_spill_mask = compiled_method->GetFpSpillMask();
388 *method_header = OatQuickMethodHeader(mapping_table_offset, vmap_table_offset,
389 frame_size_in_bytes, core_spill_mask, fp_spill_mask,
390 code_size);
391
392 // Update checksum if this wasn't a duplicate.
Vladimir Markobd72fc12014-07-09 16:06:40 +0100393 if (!deduped) {
Vladimir Marko8a630572014-04-09 18:45:35 +0100394 writer_->oat_header_->UpdateChecksum(method_header, sizeof(*method_header));
395 offset_ += sizeof(*method_header); // Method header is prepended before code.
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100396 writer_->oat_header_->UpdateChecksum(&(*quick_code)[0], code_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100397 offset_ += code_size;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100398 }
Alex Light78382fa2014-06-06 15:45:32 -0700399
400 uint32_t quick_code_start = quick_code_offset - writer_->oat_header_->GetExecutableOffset();
401 std::vector<uint8_t>* cfi_info = writer_->compiler_driver_->GetCallFrameInformation();
402 if (cfi_info != nullptr) {
403 // Copy in the FDE, if present
404 const std::vector<uint8_t>* fde = compiled_method->GetCFIInfo();
405 if (fde != nullptr) {
406 // Copy the information into cfi_info and then fix the address in the new copy.
407 int cur_offset = cfi_info->size();
408 cfi_info->insert(cfi_info->end(), fde->begin(), fde->end());
409
410 // Set the 'initial_location' field to address the start of the method.
411 uint32_t offset_to_update = cur_offset + 2*sizeof(uint32_t);
412 (*cfi_info)[offset_to_update+0] = quick_code_start;
413 (*cfi_info)[offset_to_update+1] = quick_code_start >> 8;
414 (*cfi_info)[offset_to_update+2] = quick_code_start >> 16;
415 (*cfi_info)[offset_to_update+3] = quick_code_start >> 24;
416 force_debug_capture = true;
417 }
418 }
419
420
421 if (writer_->compiler_driver_->DidIncludeDebugSymbols() || force_debug_capture) {
422 // Record debug information for this function if we are doing that or
423 // we have CFI and so need it.
424 std::string name = PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
425 if (deduped) {
426 // TODO We should place the DEDUPED tag on the first instance of a
427 // deduplicated symbol so that it will show up in a debuggerd crash
428 // report.
429 name += " [ DEDUPED ]";
430 }
431 writer_->method_info_.push_back(DebugInfo(name, quick_code_start,
432 quick_code_start + code_size));
433 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100434 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100435
436 if (kIsDebugBuild) {
437 // We expect GC maps except when the class hasn't been verified or the method is native.
438 const CompilerDriver* compiler_driver = writer_->compiler_driver_;
439 ClassReference class_ref(dex_file_, class_def_index_);
440 CompiledClass* compiled_class = compiler_driver->GetCompiledClass(class_ref);
441 mirror::Class::Status status;
442 if (compiled_class != NULL) {
443 status = compiled_class->GetStatus();
444 } else if (compiler_driver->GetVerificationResults()->IsClassRejected(class_ref)) {
445 status = mirror::Class::kStatusError;
446 } else {
447 status = mirror::Class::kStatusNotReady;
448 }
449 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
450 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
451 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
452 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
453 << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
454 << (status < mirror::Class::kStatusVerified) << " " << status << " "
455 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
456 }
457
458 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
459 OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_];
460 offsets->code_offset_ = quick_code_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100461 ++method_offsets_index_;
462 }
463
464 return true;
465 }
466
467 private:
468 // Deduplication is already done on a pointer basis by the compiler driver,
469 // so we can simply compare the pointers to find out if things are duplicated.
Vladimir Marko8a630572014-04-09 18:45:35 +0100470 SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100471};
472
473template <typename DataAccess>
474class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor {
475 public:
476 InitMapMethodVisitor(OatWriter* writer, size_t offset)
477 : OatDexMethodVisitor(writer, offset) {
478 }
479
480 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
481 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
482 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
483 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
484
485 if (compiled_method != nullptr) {
486 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
487 DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u);
488
489 const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method);
490 uint32_t map_size = map->size() * sizeof((*map)[0]);
491 if (map_size != 0u) {
Vladimir Markobd72fc12014-07-09 16:06:40 +0100492 auto lb = dedupe_map_.lower_bound(map);
493 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(map, lb->first)) {
494 DataAccess::SetOffset(oat_class, method_offsets_index_, lb->second);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100495 } else {
496 DataAccess::SetOffset(oat_class, method_offsets_index_, offset_);
Vladimir Markobd72fc12014-07-09 16:06:40 +0100497 dedupe_map_.PutBefore(lb, map, offset_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100498 offset_ += map_size;
499 writer_->oat_header_->UpdateChecksum(&(*map)[0], map_size);
500 }
501 }
502 ++method_offsets_index_;
503 }
504
505 return true;
506 }
507
508 private:
509 // Deduplication is already done on a pointer basis by the compiler driver,
510 // so we can simply compare the pointers to find out if things are duplicated.
511 SafeMap<const std::vector<uint8_t>*, uint32_t> dedupe_map_;
512};
513
514class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor {
515 public:
516 InitImageMethodVisitor(OatWriter* writer, size_t offset)
517 : OatDexMethodVisitor(writer, offset) {
518 }
519
520 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
521 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
522 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
523 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
524
Vladimir Marko7624d252014-05-02 14:40:15 +0100525 OatMethodOffsets offsets(0u, 0u);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100526 if (compiled_method != nullptr) {
527 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
528 offsets = oat_class->method_offsets_[method_offsets_index_];
529 ++method_offsets_index_;
530 }
531
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100532 ClassLinker* linker = Runtime::Current()->GetClassLinker();
533 InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_));
534 // Unchecked as we hold mutator_lock_ on entry.
535 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700536 StackHandleScope<2> hs(soa.Self());
537 Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache(*dex_file_)));
Vladimir Marko7624d252014-05-02 14:40:15 +0100538 mirror::ArtMethod* method = linker->ResolveMethod(*dex_file_, it.GetMemberIndex(), dex_cache,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700539 NullHandle<mirror::ClassLoader>(),
540 NullHandle<mirror::ArtMethod>(),
541 invoke_type);
Brian Carlstrom3c911d62014-06-13 18:23:20 -0700542 CHECK(method != NULL) << PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100543 // Portable code offsets are set by ElfWriterMclinker::FixupCompiledCodeOffset after linking.
544 method->SetQuickOatCodeOffset(offsets.code_offset_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100545 method->SetOatNativeGcMapOffset(offsets.gc_map_offset_);
546
547 return true;
548 }
549};
550
551class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor {
552 public:
553 WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
554 size_t relative_offset)
555 : OatDexMethodVisitor(writer, relative_offset),
556 out_(out),
557 file_offset_(file_offset) {
558 }
559
560 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
561 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
562 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
563
564 if (compiled_method != NULL) { // ie. not an abstract method
565 size_t file_offset = file_offset_;
566 OutputStream* out = out_;
567
568 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode();
569 if (quick_code != nullptr) {
570 CHECK(compiled_method->GetPortableCode() == nullptr);
571 uint32_t aligned_offset = compiled_method->AlignCode(offset_);
572 uint32_t aligned_code_delta = aligned_offset - offset_;
573 if (aligned_code_delta != 0) {
574 static const uint8_t kPadding[] = {
575 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
576 };
577 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
578 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
579 ReportWriteFailure("code alignment padding", it);
580 return false;
581 }
582 writer_->size_code_alignment_ += aligned_code_delta;
583 offset_ += aligned_code_delta;
584 DCHECK_OFFSET_();
585 }
586 DCHECK_ALIGNED_PARAM(offset_,
587 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
588 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
589 CHECK_NE(code_size, 0U);
590
591 // Deduplicate code arrays.
592 const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_];
593 DCHECK(method_offsets.code_offset_ < offset_ || method_offsets.code_offset_ ==
Vladimir Marko7624d252014-05-02 14:40:15 +0100594 offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta())
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100595 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
596 if (method_offsets.code_offset_ >= offset_) {
Vladimir Marko7624d252014-05-02 14:40:15 +0100597 const OatQuickMethodHeader& method_header = oat_class->method_headers_[method_offsets_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100598 if (!out->WriteFully(&method_header, sizeof(method_header))) {
599 ReportWriteFailure("method header", it);
600 return false;
601 }
602 writer_->size_method_header_ += sizeof(method_header);
603 offset_ += sizeof(method_header);
604 DCHECK_OFFSET_();
605 if (!out->WriteFully(&(*quick_code)[0], code_size)) {
606 ReportWriteFailure("method code", it);
607 return false;
608 }
609 writer_->size_code_ += code_size;
610 offset_ += code_size;
611 }
612 DCHECK_OFFSET_();
613 }
614 ++method_offsets_index_;
615 }
616
617 return true;
618 }
619
620 private:
621 OutputStream* const out_;
622 size_t const file_offset_;
623
624 void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) {
625 PLOG(ERROR) << "Failed to write " << what << " for "
626 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
627 }
628};
629
630template <typename DataAccess>
631class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor {
632 public:
633 WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
634 size_t relative_offset)
635 : OatDexMethodVisitor(writer, relative_offset),
636 out_(out),
637 file_offset_(file_offset) {
638 }
639
640 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
641 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
642 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
643
644 if (compiled_method != NULL) { // ie. not an abstract method
645 size_t file_offset = file_offset_;
646 OutputStream* out = out_;
647
648 uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_);
649 ++method_offsets_index_;
650
651 // Write deduplicated map.
652 const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method);
653 size_t map_size = map->size() * sizeof((*map)[0]);
654 DCHECK((map_size == 0u && map_offset == 0u) ||
655 (map_size != 0u && map_offset != 0u && map_offset <= offset_))
656 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
657 if (map_size != 0u && map_offset == offset_) {
658 if (UNLIKELY(!out->WriteFully(&(*map)[0], map_size))) {
659 ReportWriteFailure(it);
660 return false;
661 }
662 offset_ += map_size;
663 }
664 DCHECK_OFFSET_();
665 }
666
667 return true;
668 }
669
670 private:
671 OutputStream* const out_;
672 size_t const file_offset_;
673
674 void ReportWriteFailure(const ClassDataItemIterator& it) {
675 PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for "
676 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
677 }
678};
679
680// Visit all methods from all classes in all dex files with the specified visitor.
681bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) {
682 for (const DexFile* dex_file : *dex_files_) {
683 const size_t class_def_count = dex_file->NumClassDefs();
684 for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) {
685 if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) {
686 return false;
687 }
688 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
689 const byte* class_data = dex_file->GetClassData(class_def);
690 if (class_data != NULL) { // ie not an empty class, such as a marker interface
691 ClassDataItemIterator it(*dex_file, class_data);
692 while (it.HasNextStaticField()) {
693 it.Next();
694 }
695 while (it.HasNextInstanceField()) {
696 it.Next();
697 }
698 size_t class_def_method_index = 0u;
699 while (it.HasNextDirectMethod()) {
700 if (!visitor->VisitMethod(class_def_method_index, it)) {
701 return false;
702 }
703 ++class_def_method_index;
704 it.Next();
705 }
706 while (it.HasNextVirtualMethod()) {
707 if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) {
708 return false;
709 }
710 ++class_def_method_index;
711 it.Next();
712 }
713 }
714 if (UNLIKELY(!visitor->EndClass())) {
715 return false;
716 }
717 }
718 }
719 return true;
720}
721
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700722size_t OatWriter::InitOatHeader() {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700723 oat_header_ = OatHeader::Create(compiler_driver_->GetInstructionSet(),
724 compiler_driver_->GetInstructionSetFeatures(),
725 dex_files_,
726 image_file_location_oat_checksum_,
727 image_file_location_oat_begin_,
728 key_value_store_);
729
730 return oat_header_->GetHeaderSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700731}
732
733size_t OatWriter::InitOatDexFiles(size_t offset) {
734 // create the OatDexFiles
735 for (size_t i = 0; i != dex_files_->size(); ++i) {
736 const DexFile* dex_file = (*dex_files_)[i];
737 CHECK(dex_file != NULL);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800738 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700739 oat_dex_files_.push_back(oat_dex_file);
740 offset += oat_dex_file->SizeOf();
741 }
742 return offset;
743}
744
Brian Carlstrom89521892011-12-07 22:05:07 -0800745size_t OatWriter::InitDexFiles(size_t offset) {
746 // calculate the offsets within OatDexFiles to the DexFiles
747 for (size_t i = 0; i != dex_files_->size(); ++i) {
748 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700749 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800750 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700751 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800752
753 // set offset in OatDexFile to DexFile
754 oat_dex_files_[i]->dex_file_offset_ = offset;
755
756 const DexFile* dex_file = (*dex_files_)[i];
757 offset += dex_file->GetHeader().file_size_;
758 }
759 return offset;
760}
761
Brian Carlstrom389efb02012-01-11 12:06:26 -0800762size_t OatWriter::InitOatClasses(size_t offset) {
Brian Carlstrom389efb02012-01-11 12:06:26 -0800763 // calculate the offsets within OatDexFiles to OatClasses
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100764 InitOatClassesMethodVisitor visitor(this, offset);
765 bool success = VisitDexMethods(&visitor);
766 CHECK(success);
767 offset = visitor.GetOffset();
Brian Carlstromba150c32013-08-27 17:31:03 -0700768
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100769 // Update oat_dex_files_.
770 auto oat_class_it = oat_classes_.begin();
771 for (OatDexFile* oat_dex_file : oat_dex_files_) {
772 for (uint32_t& offset : oat_dex_file->methods_offsets_) {
773 DCHECK(oat_class_it != oat_classes_.end());
774 offset = (*oat_class_it)->offset_;
775 ++oat_class_it;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700776 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100777 oat_dex_file->UpdateChecksum(oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700778 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100779 CHECK(oat_class_it == oat_classes_.end());
780
781 return offset;
782}
783
784size_t OatWriter::InitOatMaps(size_t offset) {
785 #define VISIT(VisitorType) \
786 do { \
787 VisitorType visitor(this, offset); \
788 bool success = VisitDexMethods(&visitor); \
789 DCHECK(success); \
790 offset = visitor.GetOffset(); \
791 } while (false)
792
793 VISIT(InitMapMethodVisitor<GcMapDataAccess>);
794 VISIT(InitMapMethodVisitor<MappingTableDataAccess>);
795 VISIT(InitMapMethodVisitor<VmapTableDataAccess>);
796
797 #undef VISIT
798
Brian Carlstrome24fa612011-09-29 00:53:55 -0700799 return offset;
800}
801
802size_t OatWriter::InitOatCode(size_t offset) {
803 // calculate the offsets within OatHeader to executable code
804 size_t old_offset = offset;
Dave Allison50abf0a2014-06-23 13:19:59 -0700805 size_t adjusted_offset = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700806 // required to be on a new page boundary
807 offset = RoundUp(offset, kPageSize);
808 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700809 size_executable_offset_alignment_ = offset - old_offset;
810 if (compiler_driver_->IsImage()) {
811 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700812
Ian Rogers848871b2013-08-05 10:56:33 -0700813 #define DO_TRAMPOLINE(field, fn_name) \
814 offset = CompiledCode::AlignCode(offset, instruction_set); \
Dave Allison50abf0a2014-06-23 13:19:59 -0700815 adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \
816 oat_header_->Set ## fn_name ## Offset(adjusted_offset); \
Ian Rogers848871b2013-08-05 10:56:33 -0700817 field.reset(compiler_driver_->Create ## fn_name()); \
818 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700819
Ian Rogers848871b2013-08-05 10:56:33 -0700820 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge);
821 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge);
822 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Jeff Hao88474b42013-10-23 16:24:40 -0700823 DO_TRAMPOLINE(portable_imt_conflict_trampoline_, PortableImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700824 DO_TRAMPOLINE(portable_resolution_trampoline_, PortableResolutionTrampoline);
825 DO_TRAMPOLINE(portable_to_interpreter_bridge_, PortableToInterpreterBridge);
Andreas Gampe2da88232014-02-27 12:26:20 -0800826 DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline);
Jeff Hao88474b42013-10-23 16:24:40 -0700827 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700828 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
829 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700830
Ian Rogers848871b2013-08-05 10:56:33 -0700831 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700832 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700833 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
834 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
835 oat_header_->SetJniDlsymLookupOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700836 oat_header_->SetPortableImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700837 oat_header_->SetPortableResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700838 oat_header_->SetPortableToInterpreterBridgeOffset(0);
Andreas Gampe2da88232014-02-27 12:26:20 -0800839 oat_header_->SetQuickGenericJniTrampolineOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700840 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700841 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700842 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700843 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700844 return offset;
845}
846
847size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100848 #define VISIT(VisitorType) \
849 do { \
850 VisitorType visitor(this, offset); \
851 bool success = VisitDexMethods(&visitor); \
852 DCHECK(success); \
853 offset = visitor.GetOffset(); \
854 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700855
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100856 VISIT(InitCodeMethodVisitor);
Ian Rogers1212a022013-03-04 10:48:41 -0800857 if (compiler_driver_->IsImage()) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100858 VISIT(InitImageMethodVisitor);
Ian Rogers0571d352011-11-03 19:51:38 -0700859 }
Logan Chien8b977d32012-02-21 19:14:55 +0800860
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100861 #undef VISIT
862
Brian Carlstrome24fa612011-09-29 00:53:55 -0700863 return offset;
864}
865
Ian Rogers3d504072014-03-01 09:16:49 -0800866bool OatWriter::Write(OutputStream* out) {
867 const size_t file_offset = out->Seek(0, kSeekCurrent);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700868
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700869 size_t header_size = oat_header_->GetHeaderSize();
870 if (!out->WriteFully(oat_header_, header_size)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800871 PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700872 return false;
873 }
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700874 size_oat_header_ += sizeof(OatHeader);
875 size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700876
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700877 if (!WriteTables(out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800878 LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700879 return false;
880 }
881
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100882 size_t relative_offset = out->Seek(0, kSeekCurrent) - file_offset;
883 relative_offset = WriteMaps(out, file_offset, relative_offset);
884 if (relative_offset == 0) {
885 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
886 return false;
887 }
888
889 relative_offset = WriteCode(out, file_offset, relative_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700890 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800891 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700892 return false;
893 }
894
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700895 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
896 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800897 LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700898 return false;
899 }
900
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700901 if (kIsDebugBuild) {
902 uint32_t size_total = 0;
903 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700904 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700905 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700906
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700907 DO_STAT(size_dex_file_alignment_);
908 DO_STAT(size_executable_offset_alignment_);
909 DO_STAT(size_oat_header_);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700910 DO_STAT(size_oat_header_key_value_store_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700911 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -0700912 DO_STAT(size_interpreter_to_interpreter_bridge_);
913 DO_STAT(size_interpreter_to_compiled_code_bridge_);
914 DO_STAT(size_jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -0700915 DO_STAT(size_portable_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700916 DO_STAT(size_portable_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700917 DO_STAT(size_portable_to_interpreter_bridge_);
Andreas Gampe2da88232014-02-27 12:26:20 -0800918 DO_STAT(size_quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -0700919 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700920 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700921 DO_STAT(size_quick_to_interpreter_bridge_);
922 DO_STAT(size_trampoline_alignment_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100923 DO_STAT(size_method_header_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700924 DO_STAT(size_code_);
925 DO_STAT(size_code_alignment_);
926 DO_STAT(size_mapping_table_);
927 DO_STAT(size_vmap_table_);
928 DO_STAT(size_gc_map_);
929 DO_STAT(size_oat_dex_file_location_size_);
930 DO_STAT(size_oat_dex_file_location_data_);
931 DO_STAT(size_oat_dex_file_location_checksum_);
932 DO_STAT(size_oat_dex_file_offset_);
933 DO_STAT(size_oat_dex_file_methods_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700934 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700935 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700936 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700937 DO_STAT(size_oat_class_method_offsets_);
938 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700939
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700940 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Ian Rogers3d504072014-03-01 09:16:49 -0800941 CHECK_EQ(file_offset + size_total, static_cast<uint32_t>(out->Seek(0, kSeekCurrent)));
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700942 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700943 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700944
Ian Rogers3d504072014-03-01 09:16:49 -0800945 CHECK_EQ(file_offset + size_, static_cast<uint32_t>(out->Seek(0, kSeekCurrent)));
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700946 CHECK_EQ(size_, relative_offset);
947
Brian Carlstrome24fa612011-09-29 00:53:55 -0700948 return true;
949}
950
Ian Rogers3d504072014-03-01 09:16:49 -0800951bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700952 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700953 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800954 PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700955 return false;
956 }
957 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800958 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700959 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Ian Rogers3d504072014-03-01 09:16:49 -0800960 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -0800961 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
962 const DexFile* dex_file = (*dex_files_)[i];
963 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
964 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
965 return false;
966 }
967 const DexFile* dex_file = (*dex_files_)[i];
Ian Rogers3d504072014-03-01 09:16:49 -0800968 if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700969 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
Ian Rogers3d504072014-03-01 09:16:49 -0800970 << " to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800971 return false;
972 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700973 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800974 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800975 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700976 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800977 PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700978 return false;
979 }
980 }
981 return true;
982}
983
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100984size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) {
985 #define VISIT(VisitorType) \
986 do { \
987 VisitorType visitor(this, out, file_offset, relative_offset); \
988 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
989 return 0; \
990 } \
991 relative_offset = visitor.GetOffset(); \
992 } while (false)
993
994 size_t gc_maps_offset = relative_offset;
995 VISIT(WriteMapMethodVisitor<GcMapDataAccess>);
996 size_gc_map_ = relative_offset - gc_maps_offset;
997
998 size_t mapping_tables_offset = relative_offset;
999 VISIT(WriteMapMethodVisitor<MappingTableDataAccess>);
1000 size_mapping_table_ = relative_offset - mapping_tables_offset;
1001
1002 size_t vmap_tables_offset = relative_offset;
1003 VISIT(WriteMapMethodVisitor<VmapTableDataAccess>);
1004 size_vmap_table_ = relative_offset - vmap_tables_offset;
1005
1006 #undef VISIT
1007
1008 return relative_offset;
1009}
1010
1011size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) {
Ian Rogers3d504072014-03-01 09:16:49 -08001012 off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001013 relative_offset += size_executable_offset_alignment_;
1014 DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset());
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001015 size_t expected_file_offset = file_offset + relative_offset;
1016 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001017 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Ian Rogers3d504072014-03-01 09:16:49 -08001018 << " Expected: " << expected_file_offset << " File: " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001019 return 0;
1020 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001021 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001022 if (compiler_driver_->IsImage()) {
1023 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001024
Ian Rogers848871b2013-08-05 10:56:33 -07001025 #define DO_TRAMPOLINE(field) \
1026 do { \
1027 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
1028 uint32_t alignment_padding = aligned_offset - relative_offset; \
Ian Rogers3d504072014-03-01 09:16:49 -08001029 out->Seek(alignment_padding, kSeekCurrent); \
Ian Rogers848871b2013-08-05 10:56:33 -07001030 size_trampoline_alignment_ += alignment_padding; \
Ian Rogers3d504072014-03-01 09:16:49 -08001031 if (!out->WriteFully(&(*field)[0], field->size())) { \
1032 PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \
Ian Rogers848871b2013-08-05 10:56:33 -07001033 return false; \
1034 } \
1035 size_ ## field += field->size(); \
1036 relative_offset += alignment_padding + field->size(); \
1037 DCHECK_OFFSET(); \
1038 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001039
Ian Rogers848871b2013-08-05 10:56:33 -07001040 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_);
1041 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_);
1042 DO_TRAMPOLINE(jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -07001043 DO_TRAMPOLINE(portable_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001044 DO_TRAMPOLINE(portable_resolution_trampoline_);
1045 DO_TRAMPOLINE(portable_to_interpreter_bridge_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001046 DO_TRAMPOLINE(quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001047 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001048 DO_TRAMPOLINE(quick_resolution_trampoline_);
1049 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
1050 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001051 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001052 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001053}
1054
Ian Rogers3d504072014-03-01 09:16:49 -08001055size_t OatWriter::WriteCodeDexFiles(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001056 const size_t file_offset,
1057 size_t relative_offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001058 #define VISIT(VisitorType) \
1059 do { \
1060 VisitorType visitor(this, out, file_offset, relative_offset); \
1061 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1062 return 0; \
1063 } \
1064 relative_offset = visitor.GetOffset(); \
1065 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001066
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001067 VISIT(WriteCodeMethodVisitor);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001068
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001069 #undef VISIT
Brian Carlstrom265091e2013-01-30 14:08:26 -08001070
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001071 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001072}
1073
Brian Carlstrom265091e2013-01-30 14:08:26 -08001074OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
1075 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -08001076 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001077 dex_file_location_size_ = location.size();
1078 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001079 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -08001080 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001081 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001082}
1083
1084size_t OatWriter::OatDexFile::SizeOf() const {
1085 return sizeof(dex_file_location_size_)
1086 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001087 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -08001088 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001089 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001090}
1091
Ian Rogers3d504072014-03-01 09:16:49 -08001092void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const {
1093 oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
1094 oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
1095 oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
1096 oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
1097 oat_header->UpdateChecksum(&methods_offsets_[0],
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001098 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001099}
1100
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001101bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001102 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001103 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001104 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001105 if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
1106 PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001107 return false;
1108 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001109 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001110 if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
1111 PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001112 return false;
1113 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001114 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Ian Rogers3d504072014-03-01 09:16:49 -08001115 if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
1116 PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001117 return false;
1118 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001119 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Ian Rogers3d504072014-03-01 09:16:49 -08001120 if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
1121 PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001122 return false;
1123 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001124 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Ian Rogers3d504072014-03-01 09:16:49 -08001125 if (!out->WriteFully(&methods_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001126 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001127 PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001128 return false;
1129 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001130 oat_writer->size_oat_dex_file_methods_offsets_ +=
1131 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001132 return true;
1133}
1134
Brian Carlstromba150c32013-08-27 17:31:03 -07001135OatWriter::OatClass::OatClass(size_t offset,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001136 const std::vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -07001137 uint32_t num_non_null_compiled_methods,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001138 mirror::Class::Status status)
1139 : compiled_methods_(compiled_methods) {
1140 uint32_t num_methods = compiled_methods.size();
Brian Carlstromba150c32013-08-27 17:31:03 -07001141 CHECK_LE(num_non_null_compiled_methods, num_methods);
1142
Brian Carlstrom265091e2013-01-30 14:08:26 -08001143 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -07001144 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
1145
1146 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
1147 // apply when there are 0 methods, we just arbitrarily say that 0
1148 // methods means kOatClassNoneCompiled and that we won't use
1149 // kOatClassAllCompiled unless there is at least one compiled
1150 // method. This means in an interpretter only system, we can assert
1151 // that all classes are kOatClassNoneCompiled.
1152 if (num_non_null_compiled_methods == 0) {
1153 type_ = kOatClassNoneCompiled;
1154 } else if (num_non_null_compiled_methods == num_methods) {
1155 type_ = kOatClassAllCompiled;
1156 } else {
1157 type_ = kOatClassSomeCompiled;
1158 }
1159
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001160 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -07001161 method_offsets_.resize(num_non_null_compiled_methods);
Vladimir Marko8a630572014-04-09 18:45:35 +01001162 method_headers_.resize(num_non_null_compiled_methods);
Brian Carlstromba150c32013-08-27 17:31:03 -07001163
1164 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
1165 if (type_ == kOatClassSomeCompiled) {
1166 method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator());
1167 method_bitmap_size_ = method_bitmap_->GetSizeOf();
1168 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
1169 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
1170 } else {
1171 method_bitmap_ = NULL;
1172 method_bitmap_size_ = 0;
1173 }
1174
1175 for (size_t i = 0; i < num_methods; i++) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001176 CompiledMethod* compiled_method = compiled_methods_[i];
Brian Carlstromba150c32013-08-27 17:31:03 -07001177 if (compiled_method == NULL) {
1178 oat_method_offsets_offsets_from_oat_class_[i] = 0;
1179 } else {
1180 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
1181 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
1182 if (type_ == kOatClassSomeCompiled) {
1183 method_bitmap_->SetBit(i);
1184 }
1185 }
1186 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001187}
1188
Brian Carlstromba150c32013-08-27 17:31:03 -07001189OatWriter::OatClass::~OatClass() {
Mathieu Chartier661974a2014-01-09 11:23:53 -08001190 delete method_bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -07001191}
1192
Brian Carlstrom265091e2013-01-30 14:08:26 -08001193size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
1194 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001195 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
1196 if (method_offset == 0) {
1197 return 0;
1198 }
1199 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001200}
1201
1202size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
1203 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001204 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08001205}
1206
1207size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001208 return sizeof(status_)
1209 + sizeof(type_)
1210 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
1211 + method_bitmap_size_
1212 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001213}
1214
Ian Rogers3d504072014-03-01 09:16:49 -08001215void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const {
1216 oat_header->UpdateChecksum(&status_, sizeof(status_));
1217 oat_header->UpdateChecksum(&type_, sizeof(type_));
Brian Carlstromba150c32013-08-27 17:31:03 -07001218 if (method_bitmap_size_ != 0) {
1219 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001220 oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_));
1221 oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001222 }
Ian Rogers3d504072014-03-01 09:16:49 -08001223 oat_header->UpdateChecksum(&method_offsets_[0],
1224 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001225}
1226
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001227bool OatWriter::OatClass::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001228 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001229 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001230 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001231 if (!out->WriteFully(&status_, sizeof(status_))) {
1232 PLOG(ERROR) << "Failed to write class status to " << out->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001233 return false;
1234 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001235 oat_writer->size_oat_class_status_ += sizeof(status_);
Ian Rogers3d504072014-03-01 09:16:49 -08001236 if (!out->WriteFully(&type_, sizeof(type_))) {
1237 PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001238 return false;
1239 }
1240 oat_writer->size_oat_class_type_ += sizeof(type_);
1241 if (method_bitmap_size_ != 0) {
1242 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001243 if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
1244 PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001245 return false;
1246 }
1247 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001248 if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
1249 PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001250 return false;
1251 }
1252 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1253 }
Ian Rogers3d504072014-03-01 09:16:49 -08001254 if (!out->WriteFully(&method_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001255 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001256 PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001257 return false;
1258 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001259 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001260 return true;
1261}
1262
Brian Carlstrome24fa612011-09-29 00:53:55 -07001263} // namespace art