blob: a21351bae432b71114d778a04c452e81d70e4883 [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,
Nicolas Geoffray452bee52014-07-09 07:58:10 +000053 const std::string& image_file_location,
Ian Rogersca368cb2013-11-15 15:52:08 -080054 const CompilerDriver* compiler,
Nicolas Geoffray452bee52014-07-09 07:58:10 +000055 TimingLogger* timings)
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),
Nicolas Geoffray452bee52014-07-09 07:58:10 +000060 image_file_location_(image_file_location),
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),
Nicolas Geoffray452bee52014-07-09 07:58:10 +000065 size_oat_header_image_file_location_(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) {
Ian Rogersca368cb2013-11-15 15:52:08 -080093 size_t offset;
94 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -070095 TimingLogger::ScopedTiming split("InitOatHeader", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -080096 offset = InitOatHeader();
97 }
98 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -070099 TimingLogger::ScopedTiming split("InitOatDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800100 offset = InitOatDexFiles(offset);
101 }
102 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700103 TimingLogger::ScopedTiming split("InitDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800104 offset = InitDexFiles(offset);
105 }
106 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700107 TimingLogger::ScopedTiming split("InitOatClasses", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800108 offset = InitOatClasses(offset);
109 }
110 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700111 TimingLogger::ScopedTiming split("InitOatMaps", timings);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100112 offset = InitOatMaps(offset);
113 }
114 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700115 TimingLogger::ScopedTiming split("InitOatCode", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800116 offset = InitOatCode(offset);
117 }
118 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700119 TimingLogger::ScopedTiming split("InitOatCodeDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800120 offset = InitOatCodeDexFiles(offset);
121 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700122 size_ = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700123
124 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Nicolas Geoffray452bee52014-07-09 07:58:10 +0000125 CHECK(image_file_location.empty() == compiler->IsImage());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700126}
127
Ian Rogers0571d352011-11-03 19:51:38 -0700128OatWriter::~OatWriter() {
129 delete oat_header_;
130 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800131 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -0700132}
133
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100134struct OatWriter::GcMapDataAccess {
135 static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
136 return &compiled_method->GetGcMap();
137 }
138
139 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
140 return oat_class->method_offsets_[method_offsets_index].gc_map_offset_;
141 }
142
143 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
144 ALWAYS_INLINE {
145 oat_class->method_offsets_[method_offsets_index].gc_map_offset_ = offset;
146 }
147
148 static const char* Name() ALWAYS_INLINE {
149 return "GC map";
150 }
151};
152
153struct OatWriter::MappingTableDataAccess {
154 static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
155 return &compiled_method->GetMappingTable();
156 }
157
158 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100159 uint32_t offset = oat_class->method_headers_[method_offsets_index].mapping_table_offset_;
160 return offset == 0u ? 0u :
161 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100162 }
163
164 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
165 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100166 oat_class->method_headers_[method_offsets_index].mapping_table_offset_ =
167 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100168 }
169
170 static const char* Name() ALWAYS_INLINE {
171 return "mapping table";
172 }
173};
174
175struct OatWriter::VmapTableDataAccess {
176 static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
177 return &compiled_method->GetVmapTable();
178 }
179
180 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100181 uint32_t offset = oat_class->method_headers_[method_offsets_index].vmap_table_offset_;
182 return offset == 0u ? 0u :
183 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100184 }
185
186 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
187 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100188 oat_class->method_headers_[method_offsets_index].vmap_table_offset_ =
189 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100190 }
191
192 static const char* Name() ALWAYS_INLINE {
193 return "vmap table";
194 }
195};
196
197class OatWriter::DexMethodVisitor {
198 public:
199 DexMethodVisitor(OatWriter* writer, size_t offset)
200 : writer_(writer),
201 offset_(offset),
202 dex_file_(nullptr),
203 class_def_index_(DexFile::kDexNoIndex) {
204 }
205
206 virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) {
207 DCHECK(dex_file_ == nullptr);
208 DCHECK_EQ(class_def_index_, DexFile::kDexNoIndex);
209 dex_file_ = dex_file;
210 class_def_index_ = class_def_index;
211 return true;
212 }
213
214 virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0;
215
216 virtual bool EndClass() {
217 if (kIsDebugBuild) {
218 dex_file_ = nullptr;
219 class_def_index_ = DexFile::kDexNoIndex;
220 }
221 return true;
222 }
223
224 size_t GetOffset() const {
225 return offset_;
226 }
227
228 protected:
229 virtual ~DexMethodVisitor() { }
230
231 OatWriter* const writer_;
232
233 // The offset is usually advanced for each visited method by the derived class.
234 size_t offset_;
235
236 // The dex file and class def index are set in StartClass().
237 const DexFile* dex_file_;
238 size_t class_def_index_;
239};
240
241class OatWriter::OatDexMethodVisitor : public DexMethodVisitor {
242 public:
243 OatDexMethodVisitor(OatWriter* writer, size_t offset)
244 : DexMethodVisitor(writer, offset),
245 oat_class_index_(0u),
246 method_offsets_index_(0u) {
247 }
248
249 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
250 DexMethodVisitor::StartClass(dex_file, class_def_index);
251 DCHECK_LT(oat_class_index_, writer_->oat_classes_.size());
252 method_offsets_index_ = 0u;
253 return true;
254 }
255
256 bool EndClass() {
257 ++oat_class_index_;
258 return DexMethodVisitor::EndClass();
259 }
260
261 protected:
262 size_t oat_class_index_;
263 size_t method_offsets_index_;
264};
265
266class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor {
267 public:
268 InitOatClassesMethodVisitor(OatWriter* writer, size_t offset)
269 : DexMethodVisitor(writer, offset),
270 compiled_methods_(),
271 num_non_null_compiled_methods_(0u) {
272 compiled_methods_.reserve(256u);
273 }
274
275 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
276 DexMethodVisitor::StartClass(dex_file, class_def_index);
277 compiled_methods_.clear();
278 num_non_null_compiled_methods_ = 0u;
279 return true;
280 }
281
282 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
283 // Fill in the compiled_methods_ array for methods that have a
284 // CompiledMethod. We track the number of non-null entries in
285 // num_non_null_compiled_methods_ since we only want to allocate
286 // OatMethodOffsets for the compiled methods.
287 uint32_t method_idx = it.GetMemberIndex();
288 CompiledMethod* compiled_method =
289 writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx));
290 compiled_methods_.push_back(compiled_method);
291 if (compiled_method != nullptr) {
292 ++num_non_null_compiled_methods_;
293 }
294 return true;
295 }
296
297 bool EndClass() {
298 ClassReference class_ref(dex_file_, class_def_index_);
299 CompiledClass* compiled_class = writer_->compiler_driver_->GetCompiledClass(class_ref);
300 mirror::Class::Status status;
301 if (compiled_class != NULL) {
302 status = compiled_class->GetStatus();
303 } else if (writer_->compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) {
304 status = mirror::Class::kStatusError;
305 } else {
306 status = mirror::Class::kStatusNotReady;
307 }
308
309 OatClass* oat_class = new OatClass(offset_, compiled_methods_,
310 num_non_null_compiled_methods_, status);
311 writer_->oat_classes_.push_back(oat_class);
312 offset_ += oat_class->SizeOf();
313 return DexMethodVisitor::EndClass();
314 }
315
316 private:
317 std::vector<CompiledMethod*> compiled_methods_;
318 size_t num_non_null_compiled_methods_;
319};
320
321class OatWriter::InitCodeMethodVisitor : public OatDexMethodVisitor {
322 public:
323 InitCodeMethodVisitor(OatWriter* writer, size_t offset)
324 : OatDexMethodVisitor(writer, offset) {
325 }
326
327 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
328 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
329 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
330 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
331
332 if (compiled_method != nullptr) {
333 // Derived from CompiledMethod.
334 uint32_t quick_code_offset = 0;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100335
336 const std::vector<uint8_t>* portable_code = compiled_method->GetPortableCode();
337 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode();
338 if (portable_code != nullptr) {
339 CHECK(quick_code == nullptr);
340 size_t oat_method_offsets_offset =
341 oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index);
342 compiled_method->AddOatdataOffsetToCompliledCodeOffset(
343 oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_));
344 } else {
345 CHECK(quick_code != nullptr);
346 offset_ = compiled_method->AlignCode(offset_);
347 DCHECK_ALIGNED_PARAM(offset_,
348 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
349 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
350 CHECK_NE(code_size, 0U);
351 uint32_t thumb_offset = compiled_method->CodeDelta();
Vladimir Marko7624d252014-05-02 14:40:15 +0100352 quick_code_offset = offset_ + sizeof(OatQuickMethodHeader) + thumb_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100353
Alex Light78382fa2014-06-06 15:45:32 -0700354 bool force_debug_capture = false;
355 bool deduped = false;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100356
357 // Deduplicate code arrays.
Vladimir Markobd72fc12014-07-09 16:06:40 +0100358 auto lb = dedupe_map_.lower_bound(compiled_method);
359 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(compiled_method, lb->first)) {
360 quick_code_offset = lb->second;
Alex Light78382fa2014-06-06 15:45:32 -0700361 deduped = true;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100362 } else {
Vladimir Markobd72fc12014-07-09 16:06:40 +0100363 dedupe_map_.PutBefore(lb, compiled_method, quick_code_offset);
Vladimir Marko7624d252014-05-02 14:40:15 +0100364 }
365
366 // Update quick method header.
367 DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size());
368 OatQuickMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_];
369 uint32_t mapping_table_offset = method_header->mapping_table_offset_;
370 uint32_t vmap_table_offset = method_header->vmap_table_offset_;
371 // The code offset was 0 when the mapping/vmap table offset was set, so it's set
372 // to 0-offset and we need to adjust it by code_offset.
373 uint32_t code_offset = quick_code_offset - thumb_offset;
374 if (mapping_table_offset != 0u) {
375 mapping_table_offset += code_offset;
376 DCHECK_LT(mapping_table_offset, code_offset);
377 }
378 if (vmap_table_offset != 0u) {
379 vmap_table_offset += code_offset;
380 DCHECK_LT(vmap_table_offset, code_offset);
381 }
382 uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
383 uint32_t core_spill_mask = compiled_method->GetCoreSpillMask();
384 uint32_t fp_spill_mask = compiled_method->GetFpSpillMask();
385 *method_header = OatQuickMethodHeader(mapping_table_offset, vmap_table_offset,
386 frame_size_in_bytes, core_spill_mask, fp_spill_mask,
387 code_size);
388
389 // Update checksum if this wasn't a duplicate.
Vladimir Markobd72fc12014-07-09 16:06:40 +0100390 if (!deduped) {
Vladimir Marko8a630572014-04-09 18:45:35 +0100391 writer_->oat_header_->UpdateChecksum(method_header, sizeof(*method_header));
392 offset_ += sizeof(*method_header); // Method header is prepended before code.
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100393 writer_->oat_header_->UpdateChecksum(&(*quick_code)[0], code_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100394 offset_ += code_size;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100395 }
Alex Light78382fa2014-06-06 15:45:32 -0700396
397 uint32_t quick_code_start = quick_code_offset - writer_->oat_header_->GetExecutableOffset();
398 std::vector<uint8_t>* cfi_info = writer_->compiler_driver_->GetCallFrameInformation();
399 if (cfi_info != nullptr) {
400 // Copy in the FDE, if present
401 const std::vector<uint8_t>* fde = compiled_method->GetCFIInfo();
402 if (fde != nullptr) {
403 // Copy the information into cfi_info and then fix the address in the new copy.
404 int cur_offset = cfi_info->size();
405 cfi_info->insert(cfi_info->end(), fde->begin(), fde->end());
406
407 // Set the 'initial_location' field to address the start of the method.
408 uint32_t offset_to_update = cur_offset + 2*sizeof(uint32_t);
409 (*cfi_info)[offset_to_update+0] = quick_code_start;
410 (*cfi_info)[offset_to_update+1] = quick_code_start >> 8;
411 (*cfi_info)[offset_to_update+2] = quick_code_start >> 16;
412 (*cfi_info)[offset_to_update+3] = quick_code_start >> 24;
413 force_debug_capture = true;
414 }
415 }
416
417
418 if (writer_->compiler_driver_->DidIncludeDebugSymbols() || force_debug_capture) {
419 // Record debug information for this function if we are doing that or
420 // we have CFI and so need it.
421 std::string name = PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
422 if (deduped) {
423 // TODO We should place the DEDUPED tag on the first instance of a
424 // deduplicated symbol so that it will show up in a debuggerd crash
425 // report.
426 name += " [ DEDUPED ]";
427 }
428 writer_->method_info_.push_back(DebugInfo(name, quick_code_start,
429 quick_code_start + code_size));
430 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100431 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100432
433 if (kIsDebugBuild) {
434 // We expect GC maps except when the class hasn't been verified or the method is native.
435 const CompilerDriver* compiler_driver = writer_->compiler_driver_;
436 ClassReference class_ref(dex_file_, class_def_index_);
437 CompiledClass* compiled_class = compiler_driver->GetCompiledClass(class_ref);
438 mirror::Class::Status status;
439 if (compiled_class != NULL) {
440 status = compiled_class->GetStatus();
441 } else if (compiler_driver->GetVerificationResults()->IsClassRejected(class_ref)) {
442 status = mirror::Class::kStatusError;
443 } else {
444 status = mirror::Class::kStatusNotReady;
445 }
446 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
447 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
448 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
449 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
450 << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
451 << (status < mirror::Class::kStatusVerified) << " " << status << " "
452 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
453 }
454
455 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
456 OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_];
457 offsets->code_offset_ = quick_code_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100458 ++method_offsets_index_;
459 }
460
461 return true;
462 }
463
464 private:
465 // Deduplication is already done on a pointer basis by the compiler driver,
466 // so we can simply compare the pointers to find out if things are duplicated.
Vladimir Marko8a630572014-04-09 18:45:35 +0100467 SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100468};
469
470template <typename DataAccess>
471class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor {
472 public:
473 InitMapMethodVisitor(OatWriter* writer, size_t offset)
474 : OatDexMethodVisitor(writer, offset) {
475 }
476
477 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
478 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
479 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
480 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
481
482 if (compiled_method != nullptr) {
483 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
484 DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u);
485
486 const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method);
487 uint32_t map_size = map->size() * sizeof((*map)[0]);
488 if (map_size != 0u) {
Vladimir Markobd72fc12014-07-09 16:06:40 +0100489 auto lb = dedupe_map_.lower_bound(map);
490 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(map, lb->first)) {
491 DataAccess::SetOffset(oat_class, method_offsets_index_, lb->second);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100492 } else {
493 DataAccess::SetOffset(oat_class, method_offsets_index_, offset_);
Vladimir Markobd72fc12014-07-09 16:06:40 +0100494 dedupe_map_.PutBefore(lb, map, offset_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100495 offset_ += map_size;
496 writer_->oat_header_->UpdateChecksum(&(*map)[0], map_size);
497 }
498 }
499 ++method_offsets_index_;
500 }
501
502 return true;
503 }
504
505 private:
506 // Deduplication is already done on a pointer basis by the compiler driver,
507 // so we can simply compare the pointers to find out if things are duplicated.
508 SafeMap<const std::vector<uint8_t>*, uint32_t> dedupe_map_;
509};
510
511class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor {
512 public:
513 InitImageMethodVisitor(OatWriter* writer, size_t offset)
514 : OatDexMethodVisitor(writer, offset) {
515 }
516
517 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
518 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
519 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
520 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
521
Vladimir Marko7624d252014-05-02 14:40:15 +0100522 OatMethodOffsets offsets(0u, 0u);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100523 if (compiled_method != nullptr) {
524 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
525 offsets = oat_class->method_offsets_[method_offsets_index_];
526 ++method_offsets_index_;
527 }
528
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100529 ClassLinker* linker = Runtime::Current()->GetClassLinker();
530 InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_));
531 // Unchecked as we hold mutator_lock_ on entry.
532 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700533 StackHandleScope<2> hs(soa.Self());
534 Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache(*dex_file_)));
Vladimir Marko7624d252014-05-02 14:40:15 +0100535 mirror::ArtMethod* method = linker->ResolveMethod(*dex_file_, it.GetMemberIndex(), dex_cache,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700536 NullHandle<mirror::ClassLoader>(),
537 NullHandle<mirror::ArtMethod>(),
538 invoke_type);
Brian Carlstrom3c911d62014-06-13 18:23:20 -0700539 CHECK(method != NULL) << PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100540 // Portable code offsets are set by ElfWriterMclinker::FixupCompiledCodeOffset after linking.
541 method->SetQuickOatCodeOffset(offsets.code_offset_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100542 method->SetOatNativeGcMapOffset(offsets.gc_map_offset_);
543
544 return true;
545 }
546};
547
548class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor {
549 public:
550 WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
551 size_t relative_offset)
552 : OatDexMethodVisitor(writer, relative_offset),
553 out_(out),
554 file_offset_(file_offset) {
555 }
556
557 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
558 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
559 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
560
561 if (compiled_method != NULL) { // ie. not an abstract method
562 size_t file_offset = file_offset_;
563 OutputStream* out = out_;
564
565 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode();
566 if (quick_code != nullptr) {
567 CHECK(compiled_method->GetPortableCode() == nullptr);
568 uint32_t aligned_offset = compiled_method->AlignCode(offset_);
569 uint32_t aligned_code_delta = aligned_offset - offset_;
570 if (aligned_code_delta != 0) {
571 static const uint8_t kPadding[] = {
572 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
573 };
574 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
575 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
576 ReportWriteFailure("code alignment padding", it);
577 return false;
578 }
579 writer_->size_code_alignment_ += aligned_code_delta;
580 offset_ += aligned_code_delta;
581 DCHECK_OFFSET_();
582 }
583 DCHECK_ALIGNED_PARAM(offset_,
584 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
585 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
586 CHECK_NE(code_size, 0U);
587
588 // Deduplicate code arrays.
589 const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_];
590 DCHECK(method_offsets.code_offset_ < offset_ || method_offsets.code_offset_ ==
Vladimir Marko7624d252014-05-02 14:40:15 +0100591 offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta())
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100592 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
593 if (method_offsets.code_offset_ >= offset_) {
Vladimir Marko7624d252014-05-02 14:40:15 +0100594 const OatQuickMethodHeader& method_header = oat_class->method_headers_[method_offsets_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100595 if (!out->WriteFully(&method_header, sizeof(method_header))) {
596 ReportWriteFailure("method header", it);
597 return false;
598 }
599 writer_->size_method_header_ += sizeof(method_header);
600 offset_ += sizeof(method_header);
601 DCHECK_OFFSET_();
602 if (!out->WriteFully(&(*quick_code)[0], code_size)) {
603 ReportWriteFailure("method code", it);
604 return false;
605 }
606 writer_->size_code_ += code_size;
607 offset_ += code_size;
608 }
609 DCHECK_OFFSET_();
610 }
611 ++method_offsets_index_;
612 }
613
614 return true;
615 }
616
617 private:
618 OutputStream* const out_;
619 size_t const file_offset_;
620
621 void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) {
622 PLOG(ERROR) << "Failed to write " << what << " for "
623 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
624 }
625};
626
627template <typename DataAccess>
628class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor {
629 public:
630 WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
631 size_t relative_offset)
632 : OatDexMethodVisitor(writer, relative_offset),
633 out_(out),
634 file_offset_(file_offset) {
635 }
636
637 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
638 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
639 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
640
641 if (compiled_method != NULL) { // ie. not an abstract method
642 size_t file_offset = file_offset_;
643 OutputStream* out = out_;
644
645 uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_);
646 ++method_offsets_index_;
647
648 // Write deduplicated map.
649 const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method);
650 size_t map_size = map->size() * sizeof((*map)[0]);
651 DCHECK((map_size == 0u && map_offset == 0u) ||
652 (map_size != 0u && map_offset != 0u && map_offset <= offset_))
653 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
654 if (map_size != 0u && map_offset == offset_) {
655 if (UNLIKELY(!out->WriteFully(&(*map)[0], map_size))) {
656 ReportWriteFailure(it);
657 return false;
658 }
659 offset_ += map_size;
660 }
661 DCHECK_OFFSET_();
662 }
663
664 return true;
665 }
666
667 private:
668 OutputStream* const out_;
669 size_t const file_offset_;
670
671 void ReportWriteFailure(const ClassDataItemIterator& it) {
672 PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for "
673 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
674 }
675};
676
677// Visit all methods from all classes in all dex files with the specified visitor.
678bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) {
679 for (const DexFile* dex_file : *dex_files_) {
680 const size_t class_def_count = dex_file->NumClassDefs();
681 for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) {
682 if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) {
683 return false;
684 }
685 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
686 const byte* class_data = dex_file->GetClassData(class_def);
687 if (class_data != NULL) { // ie not an empty class, such as a marker interface
688 ClassDataItemIterator it(*dex_file, class_data);
689 while (it.HasNextStaticField()) {
690 it.Next();
691 }
692 while (it.HasNextInstanceField()) {
693 it.Next();
694 }
695 size_t class_def_method_index = 0u;
696 while (it.HasNextDirectMethod()) {
697 if (!visitor->VisitMethod(class_def_method_index, it)) {
698 return false;
699 }
700 ++class_def_method_index;
701 it.Next();
702 }
703 while (it.HasNextVirtualMethod()) {
704 if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) {
705 return false;
706 }
707 ++class_def_method_index;
708 it.Next();
709 }
710 }
711 if (UNLIKELY(!visitor->EndClass())) {
712 return false;
713 }
714 }
715 }
716 return true;
717}
718
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700719size_t OatWriter::InitOatHeader() {
Nicolas Geoffray452bee52014-07-09 07:58:10 +0000720 // create the OatHeader
721 oat_header_ = new OatHeader(compiler_driver_->GetInstructionSet(),
722 compiler_driver_->GetInstructionSetFeatures(),
723 dex_files_,
724 image_file_location_oat_checksum_,
725 image_file_location_oat_begin_,
726 image_file_location_);
727 size_t offset = sizeof(*oat_header_);
728 offset += image_file_location_.size();
729 return offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700730}
731
732size_t OatWriter::InitOatDexFiles(size_t offset) {
733 // create the OatDexFiles
734 for (size_t i = 0; i != dex_files_->size(); ++i) {
735 const DexFile* dex_file = (*dex_files_)[i];
736 CHECK(dex_file != NULL);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800737 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700738 oat_dex_files_.push_back(oat_dex_file);
739 offset += oat_dex_file->SizeOf();
740 }
741 return offset;
742}
743
Brian Carlstrom89521892011-12-07 22:05:07 -0800744size_t OatWriter::InitDexFiles(size_t offset) {
745 // calculate the offsets within OatDexFiles to the DexFiles
746 for (size_t i = 0; i != dex_files_->size(); ++i) {
747 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700748 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800749 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700750 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800751
752 // set offset in OatDexFile to DexFile
753 oat_dex_files_[i]->dex_file_offset_ = offset;
754
755 const DexFile* dex_file = (*dex_files_)[i];
756 offset += dex_file->GetHeader().file_size_;
757 }
758 return offset;
759}
760
Brian Carlstrom389efb02012-01-11 12:06:26 -0800761size_t OatWriter::InitOatClasses(size_t offset) {
Brian Carlstrom389efb02012-01-11 12:06:26 -0800762 // calculate the offsets within OatDexFiles to OatClasses
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100763 InitOatClassesMethodVisitor visitor(this, offset);
764 bool success = VisitDexMethods(&visitor);
765 CHECK(success);
766 offset = visitor.GetOffset();
Brian Carlstromba150c32013-08-27 17:31:03 -0700767
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100768 // Update oat_dex_files_.
769 auto oat_class_it = oat_classes_.begin();
770 for (OatDexFile* oat_dex_file : oat_dex_files_) {
771 for (uint32_t& offset : oat_dex_file->methods_offsets_) {
772 DCHECK(oat_class_it != oat_classes_.end());
773 offset = (*oat_class_it)->offset_;
774 ++oat_class_it;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700775 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100776 oat_dex_file->UpdateChecksum(oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700777 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100778 CHECK(oat_class_it == oat_classes_.end());
779
780 return offset;
781}
782
783size_t OatWriter::InitOatMaps(size_t offset) {
784 #define VISIT(VisitorType) \
785 do { \
786 VisitorType visitor(this, offset); \
787 bool success = VisitDexMethods(&visitor); \
788 DCHECK(success); \
789 offset = visitor.GetOffset(); \
790 } while (false)
791
792 VISIT(InitMapMethodVisitor<GcMapDataAccess>);
793 VISIT(InitMapMethodVisitor<MappingTableDataAccess>);
794 VISIT(InitMapMethodVisitor<VmapTableDataAccess>);
795
796 #undef VISIT
797
Brian Carlstrome24fa612011-09-29 00:53:55 -0700798 return offset;
799}
800
801size_t OatWriter::InitOatCode(size_t offset) {
802 // calculate the offsets within OatHeader to executable code
803 size_t old_offset = offset;
Dave Allison50abf0a2014-06-23 13:19:59 -0700804 size_t adjusted_offset = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700805 // required to be on a new page boundary
806 offset = RoundUp(offset, kPageSize);
807 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700808 size_executable_offset_alignment_ = offset - old_offset;
809 if (compiler_driver_->IsImage()) {
810 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700811
Ian Rogers848871b2013-08-05 10:56:33 -0700812 #define DO_TRAMPOLINE(field, fn_name) \
813 offset = CompiledCode::AlignCode(offset, instruction_set); \
Dave Allison50abf0a2014-06-23 13:19:59 -0700814 adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \
815 oat_header_->Set ## fn_name ## Offset(adjusted_offset); \
Ian Rogers848871b2013-08-05 10:56:33 -0700816 field.reset(compiler_driver_->Create ## fn_name()); \
817 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700818
Ian Rogers848871b2013-08-05 10:56:33 -0700819 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge);
820 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge);
821 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Jeff Hao88474b42013-10-23 16:24:40 -0700822 DO_TRAMPOLINE(portable_imt_conflict_trampoline_, PortableImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700823 DO_TRAMPOLINE(portable_resolution_trampoline_, PortableResolutionTrampoline);
824 DO_TRAMPOLINE(portable_to_interpreter_bridge_, PortableToInterpreterBridge);
Andreas Gampe2da88232014-02-27 12:26:20 -0800825 DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline);
Jeff Hao88474b42013-10-23 16:24:40 -0700826 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700827 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
828 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700829
Ian Rogers848871b2013-08-05 10:56:33 -0700830 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700831 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700832 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
833 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
834 oat_header_->SetJniDlsymLookupOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700835 oat_header_->SetPortableImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700836 oat_header_->SetPortableResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700837 oat_header_->SetPortableToInterpreterBridgeOffset(0);
Andreas Gampe2da88232014-02-27 12:26:20 -0800838 oat_header_->SetQuickGenericJniTrampolineOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700839 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700840 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700841 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700842 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700843 return offset;
844}
845
846size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100847 #define VISIT(VisitorType) \
848 do { \
849 VisitorType visitor(this, offset); \
850 bool success = VisitDexMethods(&visitor); \
851 DCHECK(success); \
852 offset = visitor.GetOffset(); \
853 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700854
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100855 VISIT(InitCodeMethodVisitor);
Ian Rogers1212a022013-03-04 10:48:41 -0800856 if (compiler_driver_->IsImage()) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100857 VISIT(InitImageMethodVisitor);
Ian Rogers0571d352011-11-03 19:51:38 -0700858 }
Logan Chien8b977d32012-02-21 19:14:55 +0800859
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100860 #undef VISIT
861
Brian Carlstrome24fa612011-09-29 00:53:55 -0700862 return offset;
863}
864
Ian Rogers3d504072014-03-01 09:16:49 -0800865bool OatWriter::Write(OutputStream* out) {
866 const size_t file_offset = out->Seek(0, kSeekCurrent);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700867
Nicolas Geoffray452bee52014-07-09 07:58:10 +0000868 if (!out->WriteFully(oat_header_, sizeof(*oat_header_))) {
Ian Rogers3d504072014-03-01 09:16:49 -0800869 PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700870 return false;
871 }
Nicolas Geoffray452bee52014-07-09 07:58:10 +0000872 size_oat_header_ += sizeof(*oat_header_);
873
874 if (!out->WriteFully(image_file_location_.data(), image_file_location_.size())) {
875 PLOG(ERROR) << "Failed to write oat header image file location to " << out->GetLocation();
876 return false;
877 }
878 size_oat_header_image_file_location_ += image_file_location_.size();
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700879
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700880 if (!WriteTables(out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800881 LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700882 return false;
883 }
884
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100885 size_t relative_offset = out->Seek(0, kSeekCurrent) - file_offset;
886 relative_offset = WriteMaps(out, file_offset, relative_offset);
887 if (relative_offset == 0) {
888 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
889 return false;
890 }
891
892 relative_offset = WriteCode(out, file_offset, relative_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700893 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800894 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700895 return false;
896 }
897
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700898 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
899 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800900 LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700901 return false;
902 }
903
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700904 if (kIsDebugBuild) {
905 uint32_t size_total = 0;
906 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700907 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700908 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700909
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700910 DO_STAT(size_dex_file_alignment_);
911 DO_STAT(size_executable_offset_alignment_);
912 DO_STAT(size_oat_header_);
Nicolas Geoffray452bee52014-07-09 07:58:10 +0000913 DO_STAT(size_oat_header_image_file_location_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700914 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -0700915 DO_STAT(size_interpreter_to_interpreter_bridge_);
916 DO_STAT(size_interpreter_to_compiled_code_bridge_);
917 DO_STAT(size_jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -0700918 DO_STAT(size_portable_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700919 DO_STAT(size_portable_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700920 DO_STAT(size_portable_to_interpreter_bridge_);
Andreas Gampe2da88232014-02-27 12:26:20 -0800921 DO_STAT(size_quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -0700922 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700923 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700924 DO_STAT(size_quick_to_interpreter_bridge_);
925 DO_STAT(size_trampoline_alignment_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100926 DO_STAT(size_method_header_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700927 DO_STAT(size_code_);
928 DO_STAT(size_code_alignment_);
929 DO_STAT(size_mapping_table_);
930 DO_STAT(size_vmap_table_);
931 DO_STAT(size_gc_map_);
932 DO_STAT(size_oat_dex_file_location_size_);
933 DO_STAT(size_oat_dex_file_location_data_);
934 DO_STAT(size_oat_dex_file_location_checksum_);
935 DO_STAT(size_oat_dex_file_offset_);
936 DO_STAT(size_oat_dex_file_methods_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700937 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700938 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700939 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700940 DO_STAT(size_oat_class_method_offsets_);
941 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700942
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700943 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Ian Rogers3d504072014-03-01 09:16:49 -0800944 CHECK_EQ(file_offset + size_total, static_cast<uint32_t>(out->Seek(0, kSeekCurrent)));
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700945 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700946 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700947
Ian Rogers3d504072014-03-01 09:16:49 -0800948 CHECK_EQ(file_offset + size_, static_cast<uint32_t>(out->Seek(0, kSeekCurrent)));
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700949 CHECK_EQ(size_, relative_offset);
950
Brian Carlstrome24fa612011-09-29 00:53:55 -0700951 return true;
952}
953
Ian Rogers3d504072014-03-01 09:16:49 -0800954bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700955 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700956 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800957 PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700958 return false;
959 }
960 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800961 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700962 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Ian Rogers3d504072014-03-01 09:16:49 -0800963 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -0800964 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
965 const DexFile* dex_file = (*dex_files_)[i];
966 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
967 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
968 return false;
969 }
970 const DexFile* dex_file = (*dex_files_)[i];
Ian Rogers3d504072014-03-01 09:16:49 -0800971 if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700972 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
Ian Rogers3d504072014-03-01 09:16:49 -0800973 << " to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800974 return false;
975 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700976 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800977 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800978 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700979 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800980 PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700981 return false;
982 }
983 }
984 return true;
985}
986
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100987size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) {
988 #define VISIT(VisitorType) \
989 do { \
990 VisitorType visitor(this, out, file_offset, relative_offset); \
991 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
992 return 0; \
993 } \
994 relative_offset = visitor.GetOffset(); \
995 } while (false)
996
997 size_t gc_maps_offset = relative_offset;
998 VISIT(WriteMapMethodVisitor<GcMapDataAccess>);
999 size_gc_map_ = relative_offset - gc_maps_offset;
1000
1001 size_t mapping_tables_offset = relative_offset;
1002 VISIT(WriteMapMethodVisitor<MappingTableDataAccess>);
1003 size_mapping_table_ = relative_offset - mapping_tables_offset;
1004
1005 size_t vmap_tables_offset = relative_offset;
1006 VISIT(WriteMapMethodVisitor<VmapTableDataAccess>);
1007 size_vmap_table_ = relative_offset - vmap_tables_offset;
1008
1009 #undef VISIT
1010
1011 return relative_offset;
1012}
1013
1014size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) {
Ian Rogers3d504072014-03-01 09:16:49 -08001015 off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001016 relative_offset += size_executable_offset_alignment_;
1017 DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset());
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001018 size_t expected_file_offset = file_offset + relative_offset;
1019 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001020 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Ian Rogers3d504072014-03-01 09:16:49 -08001021 << " Expected: " << expected_file_offset << " File: " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001022 return 0;
1023 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001024 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001025 if (compiler_driver_->IsImage()) {
1026 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001027
Ian Rogers848871b2013-08-05 10:56:33 -07001028 #define DO_TRAMPOLINE(field) \
1029 do { \
1030 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
1031 uint32_t alignment_padding = aligned_offset - relative_offset; \
Ian Rogers3d504072014-03-01 09:16:49 -08001032 out->Seek(alignment_padding, kSeekCurrent); \
Ian Rogers848871b2013-08-05 10:56:33 -07001033 size_trampoline_alignment_ += alignment_padding; \
Ian Rogers3d504072014-03-01 09:16:49 -08001034 if (!out->WriteFully(&(*field)[0], field->size())) { \
1035 PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \
Ian Rogers848871b2013-08-05 10:56:33 -07001036 return false; \
1037 } \
1038 size_ ## field += field->size(); \
1039 relative_offset += alignment_padding + field->size(); \
1040 DCHECK_OFFSET(); \
1041 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001042
Ian Rogers848871b2013-08-05 10:56:33 -07001043 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_);
1044 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_);
1045 DO_TRAMPOLINE(jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -07001046 DO_TRAMPOLINE(portable_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001047 DO_TRAMPOLINE(portable_resolution_trampoline_);
1048 DO_TRAMPOLINE(portable_to_interpreter_bridge_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001049 DO_TRAMPOLINE(quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001050 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001051 DO_TRAMPOLINE(quick_resolution_trampoline_);
1052 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
1053 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001054 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001055 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001056}
1057
Ian Rogers3d504072014-03-01 09:16:49 -08001058size_t OatWriter::WriteCodeDexFiles(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001059 const size_t file_offset,
1060 size_t relative_offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001061 #define VISIT(VisitorType) \
1062 do { \
1063 VisitorType visitor(this, out, file_offset, relative_offset); \
1064 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1065 return 0; \
1066 } \
1067 relative_offset = visitor.GetOffset(); \
1068 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001069
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001070 VISIT(WriteCodeMethodVisitor);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001071
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001072 #undef VISIT
Brian Carlstrom265091e2013-01-30 14:08:26 -08001073
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001074 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001075}
1076
Brian Carlstrom265091e2013-01-30 14:08:26 -08001077OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
1078 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -08001079 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001080 dex_file_location_size_ = location.size();
1081 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001082 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -08001083 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001084 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001085}
1086
1087size_t OatWriter::OatDexFile::SizeOf() const {
1088 return sizeof(dex_file_location_size_)
1089 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001090 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -08001091 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001092 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001093}
1094
Ian Rogers3d504072014-03-01 09:16:49 -08001095void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const {
1096 oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
1097 oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
1098 oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
1099 oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
1100 oat_header->UpdateChecksum(&methods_offsets_[0],
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001101 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001102}
1103
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001104bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001105 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001106 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001107 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001108 if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
1109 PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001110 return false;
1111 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001112 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001113 if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
1114 PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001115 return false;
1116 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001117 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Ian Rogers3d504072014-03-01 09:16:49 -08001118 if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
1119 PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001120 return false;
1121 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001122 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Ian Rogers3d504072014-03-01 09:16:49 -08001123 if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
1124 PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001125 return false;
1126 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001127 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Ian Rogers3d504072014-03-01 09:16:49 -08001128 if (!out->WriteFully(&methods_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001129 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001130 PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001131 return false;
1132 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001133 oat_writer->size_oat_dex_file_methods_offsets_ +=
1134 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001135 return true;
1136}
1137
Brian Carlstromba150c32013-08-27 17:31:03 -07001138OatWriter::OatClass::OatClass(size_t offset,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001139 const std::vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -07001140 uint32_t num_non_null_compiled_methods,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001141 mirror::Class::Status status)
1142 : compiled_methods_(compiled_methods) {
1143 uint32_t num_methods = compiled_methods.size();
Brian Carlstromba150c32013-08-27 17:31:03 -07001144 CHECK_LE(num_non_null_compiled_methods, num_methods);
1145
Brian Carlstrom265091e2013-01-30 14:08:26 -08001146 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -07001147 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
1148
1149 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
1150 // apply when there are 0 methods, we just arbitrarily say that 0
1151 // methods means kOatClassNoneCompiled and that we won't use
1152 // kOatClassAllCompiled unless there is at least one compiled
1153 // method. This means in an interpretter only system, we can assert
1154 // that all classes are kOatClassNoneCompiled.
1155 if (num_non_null_compiled_methods == 0) {
1156 type_ = kOatClassNoneCompiled;
1157 } else if (num_non_null_compiled_methods == num_methods) {
1158 type_ = kOatClassAllCompiled;
1159 } else {
1160 type_ = kOatClassSomeCompiled;
1161 }
1162
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001163 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -07001164 method_offsets_.resize(num_non_null_compiled_methods);
Vladimir Marko8a630572014-04-09 18:45:35 +01001165 method_headers_.resize(num_non_null_compiled_methods);
Brian Carlstromba150c32013-08-27 17:31:03 -07001166
1167 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
1168 if (type_ == kOatClassSomeCompiled) {
1169 method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator());
1170 method_bitmap_size_ = method_bitmap_->GetSizeOf();
1171 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
1172 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
1173 } else {
1174 method_bitmap_ = NULL;
1175 method_bitmap_size_ = 0;
1176 }
1177
1178 for (size_t i = 0; i < num_methods; i++) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001179 CompiledMethod* compiled_method = compiled_methods_[i];
Brian Carlstromba150c32013-08-27 17:31:03 -07001180 if (compiled_method == NULL) {
1181 oat_method_offsets_offsets_from_oat_class_[i] = 0;
1182 } else {
1183 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
1184 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
1185 if (type_ == kOatClassSomeCompiled) {
1186 method_bitmap_->SetBit(i);
1187 }
1188 }
1189 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001190}
1191
Brian Carlstromba150c32013-08-27 17:31:03 -07001192OatWriter::OatClass::~OatClass() {
Mathieu Chartier661974a2014-01-09 11:23:53 -08001193 delete method_bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -07001194}
1195
Brian Carlstrom265091e2013-01-30 14:08:26 -08001196size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
1197 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001198 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
1199 if (method_offset == 0) {
1200 return 0;
1201 }
1202 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001203}
1204
1205size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
1206 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001207 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08001208}
1209
1210size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001211 return sizeof(status_)
1212 + sizeof(type_)
1213 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
1214 + method_bitmap_size_
1215 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001216}
1217
Ian Rogers3d504072014-03-01 09:16:49 -08001218void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const {
1219 oat_header->UpdateChecksum(&status_, sizeof(status_));
1220 oat_header->UpdateChecksum(&type_, sizeof(type_));
Brian Carlstromba150c32013-08-27 17:31:03 -07001221 if (method_bitmap_size_ != 0) {
1222 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001223 oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_));
1224 oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001225 }
Ian Rogers3d504072014-03-01 09:16:49 -08001226 oat_header->UpdateChecksum(&method_offsets_[0],
1227 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001228}
1229
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001230bool OatWriter::OatClass::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001231 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001232 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001233 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001234 if (!out->WriteFully(&status_, sizeof(status_))) {
1235 PLOG(ERROR) << "Failed to write class status to " << out->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001236 return false;
1237 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001238 oat_writer->size_oat_class_status_ += sizeof(status_);
Ian Rogers3d504072014-03-01 09:16:49 -08001239 if (!out->WriteFully(&type_, sizeof(type_))) {
1240 PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001241 return false;
1242 }
1243 oat_writer->size_oat_class_type_ += sizeof(type_);
1244 if (method_bitmap_size_ != 0) {
1245 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001246 if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
1247 PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001248 return false;
1249 }
1250 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001251 if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
1252 PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001253 return false;
1254 }
1255 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1256 }
Ian Rogers3d504072014-03-01 09:16:49 -08001257 if (!out->WriteFully(&method_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001258 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001259 PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001260 return false;
1261 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001262 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001263 return true;
1264}
1265
Brian Carlstrome24fa612011-09-29 00:53:55 -07001266} // namespace art