blob: 73f4ba1fd5d1e08120fa381eb33feaaf8874f0a6 [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"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000026#include "dex/verification_results.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070027#include "gc/space/space.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070028#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/array.h"
30#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070031#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070032#include "os.h"
Brian Carlstromcd60ac72013-01-20 17:09:51 -080033#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070034#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070035#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070036#include "handle_scope-inl.h"
jeffhaoec014232012-09-05 10:42:25 -070037#include "verifier/method_verifier.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070038
39namespace art {
40
Vladimir Marko96c6ab92014-04-08 14:00:50 +010041#define DCHECK_OFFSET() \
42 DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out->Seek(0, kSeekCurrent)) \
43 << "file_offset=" << file_offset << " relative_offset=" << relative_offset
44
45#define DCHECK_OFFSET_() \
46 DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out->Seek(0, kSeekCurrent)) \
47 << "file_offset=" << file_offset << " offset_=" << offset_
48
Brian Carlstrom3320cf42011-10-04 14:58:28 -070049OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070050 uint32_t image_file_location_oat_checksum,
Ian Rogersef7d42f2014-01-06 12:55:46 -080051 uintptr_t image_file_location_oat_begin,
Ian Rogersca368cb2013-11-15 15:52:08 -080052 const CompilerDriver* compiler,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070053 TimingLogger* timings,
54 SafeMap<std::string, std::string>* key_value_store)
Jeff Hao0aba0ba2013-06-03 14:49:28 -070055 : compiler_driver_(compiler),
56 dex_files_(&dex_files),
57 image_file_location_oat_checksum_(image_file_location_oat_checksum),
58 image_file_location_oat_begin_(image_file_location_oat_begin),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070059 key_value_store_(key_value_store),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070060 oat_header_(NULL),
61 size_dex_file_alignment_(0),
62 size_executable_offset_alignment_(0),
63 size_oat_header_(0),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070064 size_oat_header_key_value_store_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070065 size_dex_file_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070066 size_interpreter_to_interpreter_bridge_(0),
67 size_interpreter_to_compiled_code_bridge_(0),
68 size_jni_dlsym_lookup_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070069 size_portable_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070070 size_portable_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070071 size_portable_to_interpreter_bridge_(0),
Andreas Gampe2da88232014-02-27 12:26:20 -080072 size_quick_generic_jni_trampoline_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070073 size_quick_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070074 size_quick_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070075 size_quick_to_interpreter_bridge_(0),
76 size_trampoline_alignment_(0),
Vladimir Marko96c6ab92014-04-08 14:00:50 +010077 size_method_header_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070078 size_code_(0),
79 size_code_alignment_(0),
80 size_mapping_table_(0),
81 size_vmap_table_(0),
82 size_gc_map_(0),
83 size_oat_dex_file_location_size_(0),
84 size_oat_dex_file_location_data_(0),
85 size_oat_dex_file_location_checksum_(0),
86 size_oat_dex_file_offset_(0),
87 size_oat_dex_file_methods_offsets_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -070088 size_oat_class_type_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070089 size_oat_class_status_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -070090 size_oat_class_method_bitmaps_(0),
Dave Allisond6ed6422014-04-09 23:36:15 +000091 size_oat_class_method_offsets_(0) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070092 CHECK(key_value_store != nullptr);
93
Ian Rogersca368cb2013-11-15 15:52:08 -080094 size_t offset;
95 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -070096 TimingLogger::ScopedTiming split("InitOatHeader", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -080097 offset = InitOatHeader();
98 }
99 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700100 TimingLogger::ScopedTiming split("InitOatDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800101 offset = InitOatDexFiles(offset);
102 }
103 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700104 TimingLogger::ScopedTiming split("InitDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800105 offset = InitDexFiles(offset);
106 }
107 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700108 TimingLogger::ScopedTiming split("InitOatClasses", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800109 offset = InitOatClasses(offset);
110 }
111 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700112 TimingLogger::ScopedTiming split("InitOatMaps", timings);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100113 offset = InitOatMaps(offset);
114 }
115 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700116 TimingLogger::ScopedTiming split("InitOatCode", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800117 offset = InitOatCode(offset);
118 }
119 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700120 TimingLogger::ScopedTiming split("InitOatCodeDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800121 offset = InitOatCodeDexFiles(offset);
122 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700123 size_ = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700124
125 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700126 CHECK_EQ(compiler->IsImage(),
127 key_value_store_->find(OatHeader::kImageLocationKey) == key_value_store_->end());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700128}
129
Ian Rogers0571d352011-11-03 19:51:38 -0700130OatWriter::~OatWriter() {
131 delete oat_header_;
132 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800133 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -0700134}
135
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100136struct OatWriter::GcMapDataAccess {
137 static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
138 return &compiled_method->GetGcMap();
139 }
140
141 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
142 return oat_class->method_offsets_[method_offsets_index].gc_map_offset_;
143 }
144
145 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
146 ALWAYS_INLINE {
147 oat_class->method_offsets_[method_offsets_index].gc_map_offset_ = offset;
148 }
149
150 static const char* Name() ALWAYS_INLINE {
151 return "GC map";
152 }
153};
154
155struct OatWriter::MappingTableDataAccess {
156 static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
157 return &compiled_method->GetMappingTable();
158 }
159
160 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100161 uint32_t offset = oat_class->method_headers_[method_offsets_index].mapping_table_offset_;
162 return offset == 0u ? 0u :
163 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100164 }
165
166 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
167 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100168 oat_class->method_headers_[method_offsets_index].mapping_table_offset_ =
169 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100170 }
171
172 static const char* Name() ALWAYS_INLINE {
173 return "mapping table";
174 }
175};
176
177struct OatWriter::VmapTableDataAccess {
178 static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
179 return &compiled_method->GetVmapTable();
180 }
181
182 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100183 uint32_t offset = oat_class->method_headers_[method_offsets_index].vmap_table_offset_;
184 return offset == 0u ? 0u :
185 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100186 }
187
188 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
189 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100190 oat_class->method_headers_[method_offsets_index].vmap_table_offset_ =
191 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100192 }
193
194 static const char* Name() ALWAYS_INLINE {
195 return "vmap table";
196 }
197};
198
199class OatWriter::DexMethodVisitor {
200 public:
201 DexMethodVisitor(OatWriter* writer, size_t offset)
202 : writer_(writer),
203 offset_(offset),
204 dex_file_(nullptr),
205 class_def_index_(DexFile::kDexNoIndex) {
206 }
207
208 virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) {
209 DCHECK(dex_file_ == nullptr);
210 DCHECK_EQ(class_def_index_, DexFile::kDexNoIndex);
211 dex_file_ = dex_file;
212 class_def_index_ = class_def_index;
213 return true;
214 }
215
216 virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0;
217
218 virtual bool EndClass() {
219 if (kIsDebugBuild) {
220 dex_file_ = nullptr;
221 class_def_index_ = DexFile::kDexNoIndex;
222 }
223 return true;
224 }
225
226 size_t GetOffset() const {
227 return offset_;
228 }
229
230 protected:
231 virtual ~DexMethodVisitor() { }
232
233 OatWriter* const writer_;
234
235 // The offset is usually advanced for each visited method by the derived class.
236 size_t offset_;
237
238 // The dex file and class def index are set in StartClass().
239 const DexFile* dex_file_;
240 size_t class_def_index_;
241};
242
243class OatWriter::OatDexMethodVisitor : public DexMethodVisitor {
244 public:
245 OatDexMethodVisitor(OatWriter* writer, size_t offset)
246 : DexMethodVisitor(writer, offset),
247 oat_class_index_(0u),
248 method_offsets_index_(0u) {
249 }
250
251 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
252 DexMethodVisitor::StartClass(dex_file, class_def_index);
253 DCHECK_LT(oat_class_index_, writer_->oat_classes_.size());
254 method_offsets_index_ = 0u;
255 return true;
256 }
257
258 bool EndClass() {
259 ++oat_class_index_;
260 return DexMethodVisitor::EndClass();
261 }
262
263 protected:
264 size_t oat_class_index_;
265 size_t method_offsets_index_;
266};
267
268class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor {
269 public:
270 InitOatClassesMethodVisitor(OatWriter* writer, size_t offset)
271 : DexMethodVisitor(writer, offset),
272 compiled_methods_(),
273 num_non_null_compiled_methods_(0u) {
274 compiled_methods_.reserve(256u);
275 }
276
277 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
278 DexMethodVisitor::StartClass(dex_file, class_def_index);
279 compiled_methods_.clear();
280 num_non_null_compiled_methods_ = 0u;
281 return true;
282 }
283
284 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
285 // Fill in the compiled_methods_ array for methods that have a
286 // CompiledMethod. We track the number of non-null entries in
287 // num_non_null_compiled_methods_ since we only want to allocate
288 // OatMethodOffsets for the compiled methods.
289 uint32_t method_idx = it.GetMemberIndex();
290 CompiledMethod* compiled_method =
291 writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx));
292 compiled_methods_.push_back(compiled_method);
293 if (compiled_method != nullptr) {
294 ++num_non_null_compiled_methods_;
295 }
296 return true;
297 }
298
299 bool EndClass() {
300 ClassReference class_ref(dex_file_, class_def_index_);
301 CompiledClass* compiled_class = writer_->compiler_driver_->GetCompiledClass(class_ref);
302 mirror::Class::Status status;
303 if (compiled_class != NULL) {
304 status = compiled_class->GetStatus();
305 } else if (writer_->compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) {
306 status = mirror::Class::kStatusError;
307 } else {
308 status = mirror::Class::kStatusNotReady;
309 }
310
311 OatClass* oat_class = new OatClass(offset_, compiled_methods_,
312 num_non_null_compiled_methods_, status);
313 writer_->oat_classes_.push_back(oat_class);
314 offset_ += oat_class->SizeOf();
315 return DexMethodVisitor::EndClass();
316 }
317
318 private:
319 std::vector<CompiledMethod*> compiled_methods_;
320 size_t num_non_null_compiled_methods_;
321};
322
323class OatWriter::InitCodeMethodVisitor : public OatDexMethodVisitor {
324 public:
325 InitCodeMethodVisitor(OatWriter* writer, size_t offset)
326 : OatDexMethodVisitor(writer, offset) {
327 }
328
329 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
330 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
331 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
332 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
333
334 if (compiled_method != nullptr) {
335 // Derived from CompiledMethod.
336 uint32_t quick_code_offset = 0;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100337
338 const std::vector<uint8_t>* portable_code = compiled_method->GetPortableCode();
339 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode();
340 if (portable_code != nullptr) {
341 CHECK(quick_code == nullptr);
342 size_t oat_method_offsets_offset =
343 oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index);
344 compiled_method->AddOatdataOffsetToCompliledCodeOffset(
345 oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_));
346 } else {
347 CHECK(quick_code != nullptr);
348 offset_ = compiled_method->AlignCode(offset_);
349 DCHECK_ALIGNED_PARAM(offset_,
350 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
351 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
352 CHECK_NE(code_size, 0U);
353 uint32_t thumb_offset = compiled_method->CodeDelta();
Vladimir Marko7624d252014-05-02 14:40:15 +0100354 quick_code_offset = offset_ + sizeof(OatQuickMethodHeader) + thumb_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100355
Alex Light78382fa2014-06-06 15:45:32 -0700356 bool force_debug_capture = false;
357 bool deduped = false;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100358
359 // Deduplicate code arrays.
Vladimir Markobd72fc12014-07-09 16:06:40 +0100360 auto lb = dedupe_map_.lower_bound(compiled_method);
361 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(compiled_method, lb->first)) {
362 quick_code_offset = lb->second;
Alex Light78382fa2014-06-06 15:45:32 -0700363 deduped = true;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100364 } else {
Vladimir Markobd72fc12014-07-09 16:06:40 +0100365 dedupe_map_.PutBefore(lb, compiled_method, quick_code_offset);
Vladimir Marko7624d252014-05-02 14:40:15 +0100366 }
367
368 // Update quick method header.
369 DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size());
370 OatQuickMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_];
371 uint32_t mapping_table_offset = method_header->mapping_table_offset_;
372 uint32_t vmap_table_offset = method_header->vmap_table_offset_;
373 // The code offset was 0 when the mapping/vmap table offset was set, so it's set
374 // to 0-offset and we need to adjust it by code_offset.
375 uint32_t code_offset = quick_code_offset - thumb_offset;
376 if (mapping_table_offset != 0u) {
377 mapping_table_offset += code_offset;
378 DCHECK_LT(mapping_table_offset, code_offset);
379 }
380 if (vmap_table_offset != 0u) {
381 vmap_table_offset += code_offset;
382 DCHECK_LT(vmap_table_offset, code_offset);
383 }
384 uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
385 uint32_t core_spill_mask = compiled_method->GetCoreSpillMask();
386 uint32_t fp_spill_mask = compiled_method->GetFpSpillMask();
387 *method_header = OatQuickMethodHeader(mapping_table_offset, vmap_table_offset,
388 frame_size_in_bytes, core_spill_mask, fp_spill_mask,
389 code_size);
390
391 // Update checksum if this wasn't a duplicate.
Vladimir Markobd72fc12014-07-09 16:06:40 +0100392 if (!deduped) {
Vladimir Marko8a630572014-04-09 18:45:35 +0100393 writer_->oat_header_->UpdateChecksum(method_header, sizeof(*method_header));
394 offset_ += sizeof(*method_header); // Method header is prepended before code.
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100395 writer_->oat_header_->UpdateChecksum(&(*quick_code)[0], code_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100396 offset_ += code_size;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100397 }
Alex Light78382fa2014-06-06 15:45:32 -0700398
399 uint32_t quick_code_start = quick_code_offset - writer_->oat_header_->GetExecutableOffset();
400 std::vector<uint8_t>* cfi_info = writer_->compiler_driver_->GetCallFrameInformation();
401 if (cfi_info != nullptr) {
402 // Copy in the FDE, if present
403 const std::vector<uint8_t>* fde = compiled_method->GetCFIInfo();
404 if (fde != nullptr) {
405 // Copy the information into cfi_info and then fix the address in the new copy.
406 int cur_offset = cfi_info->size();
407 cfi_info->insert(cfi_info->end(), fde->begin(), fde->end());
408
409 // Set the 'initial_location' field to address the start of the method.
410 uint32_t offset_to_update = cur_offset + 2*sizeof(uint32_t);
411 (*cfi_info)[offset_to_update+0] = quick_code_start;
412 (*cfi_info)[offset_to_update+1] = quick_code_start >> 8;
413 (*cfi_info)[offset_to_update+2] = quick_code_start >> 16;
414 (*cfi_info)[offset_to_update+3] = quick_code_start >> 24;
415 force_debug_capture = true;
416 }
417 }
418
419
420 if (writer_->compiler_driver_->DidIncludeDebugSymbols() || force_debug_capture) {
421 // Record debug information for this function if we are doing that or
422 // we have CFI and so need it.
423 std::string name = PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
424 if (deduped) {
425 // TODO We should place the DEDUPED tag on the first instance of a
426 // deduplicated symbol so that it will show up in a debuggerd crash
427 // report.
428 name += " [ DEDUPED ]";
429 }
430 writer_->method_info_.push_back(DebugInfo(name, quick_code_start,
431 quick_code_start + code_size));
432 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100433 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100434
435 if (kIsDebugBuild) {
436 // We expect GC maps except when the class hasn't been verified or the method is native.
437 const CompilerDriver* compiler_driver = writer_->compiler_driver_;
438 ClassReference class_ref(dex_file_, class_def_index_);
439 CompiledClass* compiled_class = compiler_driver->GetCompiledClass(class_ref);
440 mirror::Class::Status status;
441 if (compiled_class != NULL) {
442 status = compiled_class->GetStatus();
443 } else if (compiler_driver->GetVerificationResults()->IsClassRejected(class_ref)) {
444 status = mirror::Class::kStatusError;
445 } else {
446 status = mirror::Class::kStatusNotReady;
447 }
448 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
449 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
450 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
451 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
452 << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
453 << (status < mirror::Class::kStatusVerified) << " " << status << " "
454 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
455 }
456
457 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
458 OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_];
459 offsets->code_offset_ = quick_code_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100460 ++method_offsets_index_;
461 }
462
463 return true;
464 }
465
466 private:
467 // Deduplication is already done on a pointer basis by the compiler driver,
468 // so we can simply compare the pointers to find out if things are duplicated.
Vladimir Marko8a630572014-04-09 18:45:35 +0100469 SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100470};
471
472template <typename DataAccess>
473class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor {
474 public:
475 InitMapMethodVisitor(OatWriter* writer, size_t offset)
476 : OatDexMethodVisitor(writer, offset) {
477 }
478
479 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
480 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
481 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
482 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
483
484 if (compiled_method != nullptr) {
485 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
486 DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u);
487
488 const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method);
489 uint32_t map_size = map->size() * sizeof((*map)[0]);
490 if (map_size != 0u) {
Vladimir Markobd72fc12014-07-09 16:06:40 +0100491 auto lb = dedupe_map_.lower_bound(map);
492 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(map, lb->first)) {
493 DataAccess::SetOffset(oat_class, method_offsets_index_, lb->second);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100494 } else {
495 DataAccess::SetOffset(oat_class, method_offsets_index_, offset_);
Vladimir Markobd72fc12014-07-09 16:06:40 +0100496 dedupe_map_.PutBefore(lb, map, offset_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100497 offset_ += map_size;
498 writer_->oat_header_->UpdateChecksum(&(*map)[0], map_size);
499 }
500 }
501 ++method_offsets_index_;
502 }
503
504 return true;
505 }
506
507 private:
508 // Deduplication is already done on a pointer basis by the compiler driver,
509 // so we can simply compare the pointers to find out if things are duplicated.
510 SafeMap<const std::vector<uint8_t>*, uint32_t> dedupe_map_;
511};
512
513class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor {
514 public:
515 InitImageMethodVisitor(OatWriter* writer, size_t offset)
516 : OatDexMethodVisitor(writer, offset) {
517 }
518
519 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
520 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
521 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
522 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
523
Vladimir Marko7624d252014-05-02 14:40:15 +0100524 OatMethodOffsets offsets(0u, 0u);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100525 if (compiled_method != nullptr) {
526 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
527 offsets = oat_class->method_offsets_[method_offsets_index_];
528 ++method_offsets_index_;
529 }
530
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100531 ClassLinker* linker = Runtime::Current()->GetClassLinker();
532 InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_));
533 // Unchecked as we hold mutator_lock_ on entry.
534 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700535 StackHandleScope<2> hs(soa.Self());
536 Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache(*dex_file_)));
Vladimir Marko7624d252014-05-02 14:40:15 +0100537 mirror::ArtMethod* method = linker->ResolveMethod(*dex_file_, it.GetMemberIndex(), dex_cache,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700538 NullHandle<mirror::ClassLoader>(),
539 NullHandle<mirror::ArtMethod>(),
540 invoke_type);
Brian Carlstrom3c911d62014-06-13 18:23:20 -0700541 CHECK(method != NULL) << PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100542 // Portable code offsets are set by ElfWriterMclinker::FixupCompiledCodeOffset after linking.
543 method->SetQuickOatCodeOffset(offsets.code_offset_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100544 method->SetOatNativeGcMapOffset(offsets.gc_map_offset_);
545
546 return true;
547 }
548};
549
550class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor {
551 public:
552 WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
553 size_t relative_offset)
554 : OatDexMethodVisitor(writer, relative_offset),
555 out_(out),
556 file_offset_(file_offset) {
557 }
558
559 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
560 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
561 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
562
563 if (compiled_method != NULL) { // ie. not an abstract method
564 size_t file_offset = file_offset_;
565 OutputStream* out = out_;
566
567 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode();
568 if (quick_code != nullptr) {
569 CHECK(compiled_method->GetPortableCode() == nullptr);
570 uint32_t aligned_offset = compiled_method->AlignCode(offset_);
571 uint32_t aligned_code_delta = aligned_offset - offset_;
572 if (aligned_code_delta != 0) {
573 static const uint8_t kPadding[] = {
574 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
575 };
576 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
577 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
578 ReportWriteFailure("code alignment padding", it);
579 return false;
580 }
581 writer_->size_code_alignment_ += aligned_code_delta;
582 offset_ += aligned_code_delta;
583 DCHECK_OFFSET_();
584 }
585 DCHECK_ALIGNED_PARAM(offset_,
586 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
587 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
588 CHECK_NE(code_size, 0U);
589
590 // Deduplicate code arrays.
591 const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_];
592 DCHECK(method_offsets.code_offset_ < offset_ || method_offsets.code_offset_ ==
Vladimir Marko7624d252014-05-02 14:40:15 +0100593 offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta())
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100594 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
595 if (method_offsets.code_offset_ >= offset_) {
Vladimir Marko7624d252014-05-02 14:40:15 +0100596 const OatQuickMethodHeader& method_header = oat_class->method_headers_[method_offsets_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100597 if (!out->WriteFully(&method_header, sizeof(method_header))) {
598 ReportWriteFailure("method header", it);
599 return false;
600 }
601 writer_->size_method_header_ += sizeof(method_header);
602 offset_ += sizeof(method_header);
603 DCHECK_OFFSET_();
604 if (!out->WriteFully(&(*quick_code)[0], code_size)) {
605 ReportWriteFailure("method code", it);
606 return false;
607 }
608 writer_->size_code_ += code_size;
609 offset_ += code_size;
610 }
611 DCHECK_OFFSET_();
612 }
613 ++method_offsets_index_;
614 }
615
616 return true;
617 }
618
619 private:
620 OutputStream* const out_;
621 size_t const file_offset_;
622
623 void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) {
624 PLOG(ERROR) << "Failed to write " << what << " for "
625 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
626 }
627};
628
629template <typename DataAccess>
630class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor {
631 public:
632 WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
633 size_t relative_offset)
634 : OatDexMethodVisitor(writer, relative_offset),
635 out_(out),
636 file_offset_(file_offset) {
637 }
638
639 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
640 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
641 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
642
643 if (compiled_method != NULL) { // ie. not an abstract method
644 size_t file_offset = file_offset_;
645 OutputStream* out = out_;
646
647 uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_);
648 ++method_offsets_index_;
649
650 // Write deduplicated map.
651 const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method);
652 size_t map_size = map->size() * sizeof((*map)[0]);
653 DCHECK((map_size == 0u && map_offset == 0u) ||
654 (map_size != 0u && map_offset != 0u && map_offset <= offset_))
655 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
656 if (map_size != 0u && map_offset == offset_) {
657 if (UNLIKELY(!out->WriteFully(&(*map)[0], map_size))) {
658 ReportWriteFailure(it);
659 return false;
660 }
661 offset_ += map_size;
662 }
663 DCHECK_OFFSET_();
664 }
665
666 return true;
667 }
668
669 private:
670 OutputStream* const out_;
671 size_t const file_offset_;
672
673 void ReportWriteFailure(const ClassDataItemIterator& it) {
674 PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for "
675 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
676 }
677};
678
679// Visit all methods from all classes in all dex files with the specified visitor.
680bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) {
681 for (const DexFile* dex_file : *dex_files_) {
682 const size_t class_def_count = dex_file->NumClassDefs();
683 for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) {
684 if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) {
685 return false;
686 }
687 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
688 const byte* class_data = dex_file->GetClassData(class_def);
689 if (class_data != NULL) { // ie not an empty class, such as a marker interface
690 ClassDataItemIterator it(*dex_file, class_data);
691 while (it.HasNextStaticField()) {
692 it.Next();
693 }
694 while (it.HasNextInstanceField()) {
695 it.Next();
696 }
697 size_t class_def_method_index = 0u;
698 while (it.HasNextDirectMethod()) {
699 if (!visitor->VisitMethod(class_def_method_index, it)) {
700 return false;
701 }
702 ++class_def_method_index;
703 it.Next();
704 }
705 while (it.HasNextVirtualMethod()) {
706 if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) {
707 return false;
708 }
709 ++class_def_method_index;
710 it.Next();
711 }
712 }
713 if (UNLIKELY(!visitor->EndClass())) {
714 return false;
715 }
716 }
717 }
718 return true;
719}
720
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700721size_t OatWriter::InitOatHeader() {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700722 oat_header_ = OatHeader::Create(compiler_driver_->GetInstructionSet(),
723 compiler_driver_->GetInstructionSetFeatures(),
724 dex_files_,
725 image_file_location_oat_checksum_,
726 image_file_location_oat_begin_,
727 key_value_store_);
728
729 return oat_header_->GetHeaderSize();
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
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700868 size_t header_size = oat_header_->GetHeaderSize();
869 if (!out->WriteFully(oat_header_, header_size)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800870 PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700871 return false;
872 }
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700873 size_oat_header_ += sizeof(OatHeader);
874 size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700875
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700876 if (!WriteTables(out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800877 LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700878 return false;
879 }
880
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100881 size_t relative_offset = out->Seek(0, kSeekCurrent) - file_offset;
882 relative_offset = WriteMaps(out, file_offset, relative_offset);
883 if (relative_offset == 0) {
884 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
885 return false;
886 }
887
888 relative_offset = WriteCode(out, file_offset, relative_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700889 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800890 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700891 return false;
892 }
893
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700894 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
895 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800896 LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700897 return false;
898 }
899
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700900 if (kIsDebugBuild) {
901 uint32_t size_total = 0;
902 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700903 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700904 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700905
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700906 DO_STAT(size_dex_file_alignment_);
907 DO_STAT(size_executable_offset_alignment_);
908 DO_STAT(size_oat_header_);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700909 DO_STAT(size_oat_header_key_value_store_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700910 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -0700911 DO_STAT(size_interpreter_to_interpreter_bridge_);
912 DO_STAT(size_interpreter_to_compiled_code_bridge_);
913 DO_STAT(size_jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -0700914 DO_STAT(size_portable_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700915 DO_STAT(size_portable_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700916 DO_STAT(size_portable_to_interpreter_bridge_);
Andreas Gampe2da88232014-02-27 12:26:20 -0800917 DO_STAT(size_quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -0700918 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700919 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700920 DO_STAT(size_quick_to_interpreter_bridge_);
921 DO_STAT(size_trampoline_alignment_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100922 DO_STAT(size_method_header_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700923 DO_STAT(size_code_);
924 DO_STAT(size_code_alignment_);
925 DO_STAT(size_mapping_table_);
926 DO_STAT(size_vmap_table_);
927 DO_STAT(size_gc_map_);
928 DO_STAT(size_oat_dex_file_location_size_);
929 DO_STAT(size_oat_dex_file_location_data_);
930 DO_STAT(size_oat_dex_file_location_checksum_);
931 DO_STAT(size_oat_dex_file_offset_);
932 DO_STAT(size_oat_dex_file_methods_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700933 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700934 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700935 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700936 DO_STAT(size_oat_class_method_offsets_);
937 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700938
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700939 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Ian Rogers3d504072014-03-01 09:16:49 -0800940 CHECK_EQ(file_offset + size_total, static_cast<uint32_t>(out->Seek(0, kSeekCurrent)));
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700941 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700942 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700943
Ian Rogers3d504072014-03-01 09:16:49 -0800944 CHECK_EQ(file_offset + size_, static_cast<uint32_t>(out->Seek(0, kSeekCurrent)));
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700945 CHECK_EQ(size_, relative_offset);
946
Brian Carlstrome24fa612011-09-29 00:53:55 -0700947 return true;
948}
949
Ian Rogers3d504072014-03-01 09:16:49 -0800950bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700951 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700952 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800953 PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700954 return false;
955 }
956 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800957 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700958 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Ian Rogers3d504072014-03-01 09:16:49 -0800959 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -0800960 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
961 const DexFile* dex_file = (*dex_files_)[i];
962 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
963 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
964 return false;
965 }
966 const DexFile* dex_file = (*dex_files_)[i];
Ian Rogers3d504072014-03-01 09:16:49 -0800967 if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700968 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
Ian Rogers3d504072014-03-01 09:16:49 -0800969 << " to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800970 return false;
971 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700972 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800973 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800974 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700975 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800976 PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700977 return false;
978 }
979 }
980 return true;
981}
982
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100983size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) {
984 #define VISIT(VisitorType) \
985 do { \
986 VisitorType visitor(this, out, file_offset, relative_offset); \
987 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
988 return 0; \
989 } \
990 relative_offset = visitor.GetOffset(); \
991 } while (false)
992
993 size_t gc_maps_offset = relative_offset;
994 VISIT(WriteMapMethodVisitor<GcMapDataAccess>);
995 size_gc_map_ = relative_offset - gc_maps_offset;
996
997 size_t mapping_tables_offset = relative_offset;
998 VISIT(WriteMapMethodVisitor<MappingTableDataAccess>);
999 size_mapping_table_ = relative_offset - mapping_tables_offset;
1000
1001 size_t vmap_tables_offset = relative_offset;
1002 VISIT(WriteMapMethodVisitor<VmapTableDataAccess>);
1003 size_vmap_table_ = relative_offset - vmap_tables_offset;
1004
1005 #undef VISIT
1006
1007 return relative_offset;
1008}
1009
1010size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) {
Ian Rogers3d504072014-03-01 09:16:49 -08001011 off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001012 relative_offset += size_executable_offset_alignment_;
1013 DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset());
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001014 size_t expected_file_offset = file_offset + relative_offset;
1015 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001016 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Ian Rogers3d504072014-03-01 09:16:49 -08001017 << " Expected: " << expected_file_offset << " File: " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001018 return 0;
1019 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001020 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001021 if (compiler_driver_->IsImage()) {
1022 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001023
Ian Rogers848871b2013-08-05 10:56:33 -07001024 #define DO_TRAMPOLINE(field) \
1025 do { \
1026 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
1027 uint32_t alignment_padding = aligned_offset - relative_offset; \
Ian Rogers3d504072014-03-01 09:16:49 -08001028 out->Seek(alignment_padding, kSeekCurrent); \
Ian Rogers848871b2013-08-05 10:56:33 -07001029 size_trampoline_alignment_ += alignment_padding; \
Ian Rogers3d504072014-03-01 09:16:49 -08001030 if (!out->WriteFully(&(*field)[0], field->size())) { \
1031 PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \
Ian Rogers848871b2013-08-05 10:56:33 -07001032 return false; \
1033 } \
1034 size_ ## field += field->size(); \
1035 relative_offset += alignment_padding + field->size(); \
1036 DCHECK_OFFSET(); \
1037 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001038
Ian Rogers848871b2013-08-05 10:56:33 -07001039 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_);
1040 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_);
1041 DO_TRAMPOLINE(jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -07001042 DO_TRAMPOLINE(portable_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001043 DO_TRAMPOLINE(portable_resolution_trampoline_);
1044 DO_TRAMPOLINE(portable_to_interpreter_bridge_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001045 DO_TRAMPOLINE(quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001046 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001047 DO_TRAMPOLINE(quick_resolution_trampoline_);
1048 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
1049 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001050 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001051 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001052}
1053
Ian Rogers3d504072014-03-01 09:16:49 -08001054size_t OatWriter::WriteCodeDexFiles(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001055 const size_t file_offset,
1056 size_t relative_offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001057 #define VISIT(VisitorType) \
1058 do { \
1059 VisitorType visitor(this, out, file_offset, relative_offset); \
1060 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1061 return 0; \
1062 } \
1063 relative_offset = visitor.GetOffset(); \
1064 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001065
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001066 VISIT(WriteCodeMethodVisitor);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001067
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001068 #undef VISIT
Brian Carlstrom265091e2013-01-30 14:08:26 -08001069
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001070 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001071}
1072
Brian Carlstrom265091e2013-01-30 14:08:26 -08001073OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
1074 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -08001075 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001076 dex_file_location_size_ = location.size();
1077 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001078 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -08001079 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001080 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001081}
1082
1083size_t OatWriter::OatDexFile::SizeOf() const {
1084 return sizeof(dex_file_location_size_)
1085 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001086 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -08001087 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001088 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001089}
1090
Ian Rogers3d504072014-03-01 09:16:49 -08001091void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const {
1092 oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
1093 oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
1094 oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
1095 oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
1096 oat_header->UpdateChecksum(&methods_offsets_[0],
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001097 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001098}
1099
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001100bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001101 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001102 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001103 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001104 if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
1105 PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001106 return false;
1107 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001108 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001109 if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
1110 PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001111 return false;
1112 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001113 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Ian Rogers3d504072014-03-01 09:16:49 -08001114 if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
1115 PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001116 return false;
1117 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001118 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Ian Rogers3d504072014-03-01 09:16:49 -08001119 if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
1120 PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001121 return false;
1122 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001123 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Ian Rogers3d504072014-03-01 09:16:49 -08001124 if (!out->WriteFully(&methods_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001125 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001126 PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001127 return false;
1128 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001129 oat_writer->size_oat_dex_file_methods_offsets_ +=
1130 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001131 return true;
1132}
1133
Brian Carlstromba150c32013-08-27 17:31:03 -07001134OatWriter::OatClass::OatClass(size_t offset,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001135 const std::vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -07001136 uint32_t num_non_null_compiled_methods,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001137 mirror::Class::Status status)
1138 : compiled_methods_(compiled_methods) {
1139 uint32_t num_methods = compiled_methods.size();
Brian Carlstromba150c32013-08-27 17:31:03 -07001140 CHECK_LE(num_non_null_compiled_methods, num_methods);
1141
Brian Carlstrom265091e2013-01-30 14:08:26 -08001142 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -07001143 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
1144
1145 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
1146 // apply when there are 0 methods, we just arbitrarily say that 0
1147 // methods means kOatClassNoneCompiled and that we won't use
1148 // kOatClassAllCompiled unless there is at least one compiled
1149 // method. This means in an interpretter only system, we can assert
1150 // that all classes are kOatClassNoneCompiled.
1151 if (num_non_null_compiled_methods == 0) {
1152 type_ = kOatClassNoneCompiled;
1153 } else if (num_non_null_compiled_methods == num_methods) {
1154 type_ = kOatClassAllCompiled;
1155 } else {
1156 type_ = kOatClassSomeCompiled;
1157 }
1158
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001159 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -07001160 method_offsets_.resize(num_non_null_compiled_methods);
Vladimir Marko8a630572014-04-09 18:45:35 +01001161 method_headers_.resize(num_non_null_compiled_methods);
Brian Carlstromba150c32013-08-27 17:31:03 -07001162
1163 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
1164 if (type_ == kOatClassSomeCompiled) {
1165 method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator());
1166 method_bitmap_size_ = method_bitmap_->GetSizeOf();
1167 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
1168 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
1169 } else {
1170 method_bitmap_ = NULL;
1171 method_bitmap_size_ = 0;
1172 }
1173
1174 for (size_t i = 0; i < num_methods; i++) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001175 CompiledMethod* compiled_method = compiled_methods_[i];
Brian Carlstromba150c32013-08-27 17:31:03 -07001176 if (compiled_method == NULL) {
1177 oat_method_offsets_offsets_from_oat_class_[i] = 0;
1178 } else {
1179 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
1180 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
1181 if (type_ == kOatClassSomeCompiled) {
1182 method_bitmap_->SetBit(i);
1183 }
1184 }
1185 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001186}
1187
Brian Carlstromba150c32013-08-27 17:31:03 -07001188OatWriter::OatClass::~OatClass() {
Mathieu Chartier661974a2014-01-09 11:23:53 -08001189 delete method_bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -07001190}
1191
Brian Carlstrom265091e2013-01-30 14:08:26 -08001192size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
1193 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001194 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
1195 if (method_offset == 0) {
1196 return 0;
1197 }
1198 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001199}
1200
1201size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
1202 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001203 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08001204}
1205
1206size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001207 return sizeof(status_)
1208 + sizeof(type_)
1209 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
1210 + method_bitmap_size_
1211 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001212}
1213
Ian Rogers3d504072014-03-01 09:16:49 -08001214void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const {
1215 oat_header->UpdateChecksum(&status_, sizeof(status_));
1216 oat_header->UpdateChecksum(&type_, sizeof(type_));
Brian Carlstromba150c32013-08-27 17:31:03 -07001217 if (method_bitmap_size_ != 0) {
1218 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001219 oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_));
1220 oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001221 }
Ian Rogers3d504072014-03-01 09:16:49 -08001222 oat_header->UpdateChecksum(&method_offsets_[0],
1223 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001224}
1225
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001226bool OatWriter::OatClass::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001227 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001228 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001229 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001230 if (!out->WriteFully(&status_, sizeof(status_))) {
1231 PLOG(ERROR) << "Failed to write class status to " << out->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001232 return false;
1233 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001234 oat_writer->size_oat_class_status_ += sizeof(status_);
Ian Rogers3d504072014-03-01 09:16:49 -08001235 if (!out->WriteFully(&type_, sizeof(type_))) {
1236 PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001237 return false;
1238 }
1239 oat_writer->size_oat_class_type_ += sizeof(type_);
1240 if (method_bitmap_size_ != 0) {
1241 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001242 if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
1243 PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001244 return false;
1245 }
1246 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001247 if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
1248 PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001249 return false;
1250 }
1251 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1252 }
Ian Rogers3d504072014-03-01 09:16:49 -08001253 if (!out->WriteFully(&method_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001254 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001255 PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001256 return false;
1257 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001258 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001259 return true;
1260}
1261
Brian Carlstrome24fa612011-09-29 00:53:55 -07001262} // namespace art