blob: 1ba5d3218e490096399ff4349f5281c15e483347 [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,
Alex Lighta59dd802014-07-02 16:28:08 -070053 int32_t image_patch_delta,
Ian Rogersca368cb2013-11-15 15:52:08 -080054 const CompilerDriver* compiler,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070055 TimingLogger* timings,
56 SafeMap<std::string, std::string>* key_value_store)
Jeff Hao0aba0ba2013-06-03 14:49:28 -070057 : compiler_driver_(compiler),
58 dex_files_(&dex_files),
59 image_file_location_oat_checksum_(image_file_location_oat_checksum),
60 image_file_location_oat_begin_(image_file_location_oat_begin),
Alex Lighta59dd802014-07-02 16:28:08 -070061 image_patch_delta_(image_patch_delta),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070062 key_value_store_(key_value_store),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070063 oat_header_(NULL),
64 size_dex_file_alignment_(0),
65 size_executable_offset_alignment_(0),
66 size_oat_header_(0),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070067 size_oat_header_key_value_store_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070068 size_dex_file_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070069 size_interpreter_to_interpreter_bridge_(0),
70 size_interpreter_to_compiled_code_bridge_(0),
71 size_jni_dlsym_lookup_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070072 size_portable_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070073 size_portable_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070074 size_portable_to_interpreter_bridge_(0),
Andreas Gampe2da88232014-02-27 12:26:20 -080075 size_quick_generic_jni_trampoline_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070076 size_quick_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070077 size_quick_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070078 size_quick_to_interpreter_bridge_(0),
79 size_trampoline_alignment_(0),
Vladimir Marko96c6ab92014-04-08 14:00:50 +010080 size_method_header_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070081 size_code_(0),
82 size_code_alignment_(0),
83 size_mapping_table_(0),
84 size_vmap_table_(0),
85 size_gc_map_(0),
86 size_oat_dex_file_location_size_(0),
87 size_oat_dex_file_location_data_(0),
88 size_oat_dex_file_location_checksum_(0),
89 size_oat_dex_file_offset_(0),
90 size_oat_dex_file_methods_offsets_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -070091 size_oat_class_type_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070092 size_oat_class_status_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -070093 size_oat_class_method_bitmaps_(0),
Dave Allisond6ed6422014-04-09 23:36:15 +000094 size_oat_class_method_offsets_(0) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070095 CHECK(key_value_store != nullptr);
96
Ian Rogersca368cb2013-11-15 15:52:08 -080097 size_t offset;
98 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -070099 TimingLogger::ScopedTiming split("InitOatHeader", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800100 offset = InitOatHeader();
101 }
102 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700103 TimingLogger::ScopedTiming split("InitOatDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800104 offset = InitOatDexFiles(offset);
105 }
106 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700107 TimingLogger::ScopedTiming split("InitDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800108 offset = InitDexFiles(offset);
109 }
110 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700111 TimingLogger::ScopedTiming split("InitOatClasses", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800112 offset = InitOatClasses(offset);
113 }
114 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700115 TimingLogger::ScopedTiming split("InitOatMaps", timings);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100116 offset = InitOatMaps(offset);
117 }
118 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700119 TimingLogger::ScopedTiming split("InitOatCode", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800120 offset = InitOatCode(offset);
121 }
122 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700123 TimingLogger::ScopedTiming split("InitOatCodeDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800124 offset = InitOatCodeDexFiles(offset);
125 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700126 size_ = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700127
128 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700129 CHECK_EQ(compiler->IsImage(),
130 key_value_store_->find(OatHeader::kImageLocationKey) == key_value_store_->end());
Alex Lighta59dd802014-07-02 16:28:08 -0700131 CHECK_ALIGNED(image_patch_delta_, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700132}
133
Ian Rogers0571d352011-11-03 19:51:38 -0700134OatWriter::~OatWriter() {
135 delete oat_header_;
136 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800137 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -0700138}
139
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100140struct OatWriter::GcMapDataAccess {
141 static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
142 return &compiled_method->GetGcMap();
143 }
144
145 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
146 return oat_class->method_offsets_[method_offsets_index].gc_map_offset_;
147 }
148
149 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
150 ALWAYS_INLINE {
151 oat_class->method_offsets_[method_offsets_index].gc_map_offset_ = offset;
152 }
153
154 static const char* Name() ALWAYS_INLINE {
155 return "GC map";
156 }
157};
158
159struct OatWriter::MappingTableDataAccess {
160 static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
161 return &compiled_method->GetMappingTable();
162 }
163
164 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100165 uint32_t offset = oat_class->method_headers_[method_offsets_index].mapping_table_offset_;
166 return offset == 0u ? 0u :
167 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100168 }
169
170 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
171 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100172 oat_class->method_headers_[method_offsets_index].mapping_table_offset_ =
173 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100174 }
175
176 static const char* Name() ALWAYS_INLINE {
177 return "mapping table";
178 }
179};
180
181struct OatWriter::VmapTableDataAccess {
182 static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
183 return &compiled_method->GetVmapTable();
184 }
185
186 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100187 uint32_t offset = oat_class->method_headers_[method_offsets_index].vmap_table_offset_;
188 return offset == 0u ? 0u :
189 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100190 }
191
192 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
193 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100194 oat_class->method_headers_[method_offsets_index].vmap_table_offset_ =
195 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100196 }
197
198 static const char* Name() ALWAYS_INLINE {
199 return "vmap table";
200 }
201};
202
203class OatWriter::DexMethodVisitor {
204 public:
205 DexMethodVisitor(OatWriter* writer, size_t offset)
206 : writer_(writer),
207 offset_(offset),
208 dex_file_(nullptr),
209 class_def_index_(DexFile::kDexNoIndex) {
210 }
211
212 virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) {
213 DCHECK(dex_file_ == nullptr);
214 DCHECK_EQ(class_def_index_, DexFile::kDexNoIndex);
215 dex_file_ = dex_file;
216 class_def_index_ = class_def_index;
217 return true;
218 }
219
220 virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0;
221
222 virtual bool EndClass() {
223 if (kIsDebugBuild) {
224 dex_file_ = nullptr;
225 class_def_index_ = DexFile::kDexNoIndex;
226 }
227 return true;
228 }
229
230 size_t GetOffset() const {
231 return offset_;
232 }
233
234 protected:
235 virtual ~DexMethodVisitor() { }
236
237 OatWriter* const writer_;
238
239 // The offset is usually advanced for each visited method by the derived class.
240 size_t offset_;
241
242 // The dex file and class def index are set in StartClass().
243 const DexFile* dex_file_;
244 size_t class_def_index_;
245};
246
247class OatWriter::OatDexMethodVisitor : public DexMethodVisitor {
248 public:
249 OatDexMethodVisitor(OatWriter* writer, size_t offset)
250 : DexMethodVisitor(writer, offset),
251 oat_class_index_(0u),
252 method_offsets_index_(0u) {
253 }
254
255 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
256 DexMethodVisitor::StartClass(dex_file, class_def_index);
257 DCHECK_LT(oat_class_index_, writer_->oat_classes_.size());
258 method_offsets_index_ = 0u;
259 return true;
260 }
261
262 bool EndClass() {
263 ++oat_class_index_;
264 return DexMethodVisitor::EndClass();
265 }
266
267 protected:
268 size_t oat_class_index_;
269 size_t method_offsets_index_;
270};
271
272class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor {
273 public:
274 InitOatClassesMethodVisitor(OatWriter* writer, size_t offset)
275 : DexMethodVisitor(writer, offset),
276 compiled_methods_(),
277 num_non_null_compiled_methods_(0u) {
278 compiled_methods_.reserve(256u);
279 }
280
281 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
282 DexMethodVisitor::StartClass(dex_file, class_def_index);
283 compiled_methods_.clear();
284 num_non_null_compiled_methods_ = 0u;
285 return true;
286 }
287
288 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
289 // Fill in the compiled_methods_ array for methods that have a
290 // CompiledMethod. We track the number of non-null entries in
291 // num_non_null_compiled_methods_ since we only want to allocate
292 // OatMethodOffsets for the compiled methods.
293 uint32_t method_idx = it.GetMemberIndex();
294 CompiledMethod* compiled_method =
295 writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx));
296 compiled_methods_.push_back(compiled_method);
297 if (compiled_method != nullptr) {
298 ++num_non_null_compiled_methods_;
299 }
300 return true;
301 }
302
303 bool EndClass() {
304 ClassReference class_ref(dex_file_, class_def_index_);
305 CompiledClass* compiled_class = writer_->compiler_driver_->GetCompiledClass(class_ref);
306 mirror::Class::Status status;
307 if (compiled_class != NULL) {
308 status = compiled_class->GetStatus();
309 } else if (writer_->compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) {
310 status = mirror::Class::kStatusError;
311 } else {
312 status = mirror::Class::kStatusNotReady;
313 }
314
315 OatClass* oat_class = new OatClass(offset_, compiled_methods_,
316 num_non_null_compiled_methods_, status);
317 writer_->oat_classes_.push_back(oat_class);
318 offset_ += oat_class->SizeOf();
319 return DexMethodVisitor::EndClass();
320 }
321
322 private:
323 std::vector<CompiledMethod*> compiled_methods_;
324 size_t num_non_null_compiled_methods_;
325};
326
327class OatWriter::InitCodeMethodVisitor : public OatDexMethodVisitor {
328 public:
329 InitCodeMethodVisitor(OatWriter* writer, size_t offset)
330 : OatDexMethodVisitor(writer, offset) {
331 }
332
333 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
334 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
335 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
336 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
337
338 if (compiled_method != nullptr) {
339 // Derived from CompiledMethod.
340 uint32_t quick_code_offset = 0;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100341
342 const std::vector<uint8_t>* portable_code = compiled_method->GetPortableCode();
343 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode();
344 if (portable_code != nullptr) {
345 CHECK(quick_code == nullptr);
346 size_t oat_method_offsets_offset =
347 oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index);
348 compiled_method->AddOatdataOffsetToCompliledCodeOffset(
349 oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_));
350 } else {
351 CHECK(quick_code != nullptr);
352 offset_ = compiled_method->AlignCode(offset_);
353 DCHECK_ALIGNED_PARAM(offset_,
354 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
355 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
356 CHECK_NE(code_size, 0U);
357 uint32_t thumb_offset = compiled_method->CodeDelta();
Vladimir Marko7624d252014-05-02 14:40:15 +0100358 quick_code_offset = offset_ + sizeof(OatQuickMethodHeader) + thumb_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100359
Alex Light78382fa2014-06-06 15:45:32 -0700360 bool deduped = false;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100361
362 // Deduplicate code arrays.
Vladimir Markobd72fc12014-07-09 16:06:40 +0100363 auto lb = dedupe_map_.lower_bound(compiled_method);
364 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(compiled_method, lb->first)) {
365 quick_code_offset = lb->second;
Alex Light78382fa2014-06-06 15:45:32 -0700366 deduped = true;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100367 } else {
Vladimir Markobd72fc12014-07-09 16:06:40 +0100368 dedupe_map_.PutBefore(lb, compiled_method, quick_code_offset);
Vladimir Marko7624d252014-05-02 14:40:15 +0100369 }
370
371 // Update quick method header.
372 DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size());
373 OatQuickMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_];
374 uint32_t mapping_table_offset = method_header->mapping_table_offset_;
375 uint32_t vmap_table_offset = method_header->vmap_table_offset_;
376 // The code offset was 0 when the mapping/vmap table offset was set, so it's set
377 // to 0-offset and we need to adjust it by code_offset.
378 uint32_t code_offset = quick_code_offset - thumb_offset;
379 if (mapping_table_offset != 0u) {
380 mapping_table_offset += code_offset;
381 DCHECK_LT(mapping_table_offset, code_offset);
382 }
383 if (vmap_table_offset != 0u) {
384 vmap_table_offset += code_offset;
385 DCHECK_LT(vmap_table_offset, code_offset);
386 }
387 uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
388 uint32_t core_spill_mask = compiled_method->GetCoreSpillMask();
389 uint32_t fp_spill_mask = compiled_method->GetFpSpillMask();
390 *method_header = OatQuickMethodHeader(mapping_table_offset, vmap_table_offset,
391 frame_size_in_bytes, core_spill_mask, fp_spill_mask,
392 code_size);
393
394 // Update checksum if this wasn't a duplicate.
Vladimir Markobd72fc12014-07-09 16:06:40 +0100395 if (!deduped) {
Vladimir Marko8a630572014-04-09 18:45:35 +0100396 writer_->oat_header_->UpdateChecksum(method_header, sizeof(*method_header));
397 offset_ += sizeof(*method_header); // Method header is prepended before code.
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100398 writer_->oat_header_->UpdateChecksum(&(*quick_code)[0], code_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100399 offset_ += code_size;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100400 }
Alex Light78382fa2014-06-06 15:45:32 -0700401
Andreas Gampe79273802014-08-05 20:21:05 -0700402 if (writer_->compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols()) {
403 // Record debug information for this function if we are doing that.
Alex Light78382fa2014-06-06 15:45:32 -0700404
Alex Light78382fa2014-06-06 15:45:32 -0700405 std::string name = PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
406 if (deduped) {
Andreas Gampe79273802014-08-05 20:21:05 -0700407 // TODO We should place the DEDUPED tag on the first instance of a deduplicated symbol
408 // so that it will show up in a debuggerd crash report.
Alex Light78382fa2014-06-06 15:45:32 -0700409 name += " [ DEDUPED ]";
410 }
Andreas Gampe79273802014-08-05 20:21:05 -0700411
412 const uint32_t quick_code_start = quick_code_offset -
413 writer_->oat_header_->GetExecutableOffset();
414 writer_->method_info_.push_back(DebugInfo(name,
415 quick_code_start,
416 quick_code_start + code_size,
417 compiled_method));
Alex Light78382fa2014-06-06 15:45:32 -0700418 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100419 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100420
421 if (kIsDebugBuild) {
422 // We expect GC maps except when the class hasn't been verified or the method is native.
423 const CompilerDriver* compiler_driver = writer_->compiler_driver_;
424 ClassReference class_ref(dex_file_, class_def_index_);
425 CompiledClass* compiled_class = compiler_driver->GetCompiledClass(class_ref);
426 mirror::Class::Status status;
427 if (compiled_class != NULL) {
428 status = compiled_class->GetStatus();
429 } else if (compiler_driver->GetVerificationResults()->IsClassRejected(class_ref)) {
430 status = mirror::Class::kStatusError;
431 } else {
432 status = mirror::Class::kStatusNotReady;
433 }
434 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
435 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
436 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
437 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
438 << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
439 << (status < mirror::Class::kStatusVerified) << " " << status << " "
440 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
441 }
442
443 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
444 OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_];
445 offsets->code_offset_ = quick_code_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100446 ++method_offsets_index_;
447 }
448
449 return true;
450 }
451
452 private:
453 // Deduplication is already done on a pointer basis by the compiler driver,
454 // so we can simply compare the pointers to find out if things are duplicated.
Vladimir Marko8a630572014-04-09 18:45:35 +0100455 SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100456};
457
458template <typename DataAccess>
459class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor {
460 public:
461 InitMapMethodVisitor(OatWriter* writer, size_t offset)
462 : OatDexMethodVisitor(writer, offset) {
463 }
464
465 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
466 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
467 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
468 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
469
470 if (compiled_method != nullptr) {
471 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
472 DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u);
473
474 const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method);
475 uint32_t map_size = map->size() * sizeof((*map)[0]);
476 if (map_size != 0u) {
Vladimir Markobd72fc12014-07-09 16:06:40 +0100477 auto lb = dedupe_map_.lower_bound(map);
478 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(map, lb->first)) {
479 DataAccess::SetOffset(oat_class, method_offsets_index_, lb->second);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100480 } else {
481 DataAccess::SetOffset(oat_class, method_offsets_index_, offset_);
Vladimir Markobd72fc12014-07-09 16:06:40 +0100482 dedupe_map_.PutBefore(lb, map, offset_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100483 offset_ += map_size;
484 writer_->oat_header_->UpdateChecksum(&(*map)[0], map_size);
485 }
486 }
487 ++method_offsets_index_;
488 }
489
490 return true;
491 }
492
493 private:
494 // Deduplication is already done on a pointer basis by the compiler driver,
495 // so we can simply compare the pointers to find out if things are duplicated.
496 SafeMap<const std::vector<uint8_t>*, uint32_t> dedupe_map_;
497};
498
499class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor {
500 public:
501 InitImageMethodVisitor(OatWriter* writer, size_t offset)
502 : OatDexMethodVisitor(writer, offset) {
503 }
504
505 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
506 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
507 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
508 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
509
Vladimir Marko7624d252014-05-02 14:40:15 +0100510 OatMethodOffsets offsets(0u, 0u);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100511 if (compiled_method != nullptr) {
512 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
513 offsets = oat_class->method_offsets_[method_offsets_index_];
514 ++method_offsets_index_;
515 }
516
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100517 ClassLinker* linker = Runtime::Current()->GetClassLinker();
518 InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_));
519 // Unchecked as we hold mutator_lock_ on entry.
520 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700521 StackHandleScope<2> hs(soa.Self());
522 Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache(*dex_file_)));
Vladimir Marko7624d252014-05-02 14:40:15 +0100523 mirror::ArtMethod* method = linker->ResolveMethod(*dex_file_, it.GetMemberIndex(), dex_cache,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700524 NullHandle<mirror::ClassLoader>(),
525 NullHandle<mirror::ArtMethod>(),
526 invoke_type);
Brian Carlstrom3c911d62014-06-13 18:23:20 -0700527 CHECK(method != NULL) << PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100528 // Portable code offsets are set by ElfWriterMclinker::FixupCompiledCodeOffset after linking.
529 method->SetQuickOatCodeOffset(offsets.code_offset_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100530 method->SetOatNativeGcMapOffset(offsets.gc_map_offset_);
531
532 return true;
533 }
534};
535
536class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor {
537 public:
538 WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
539 size_t relative_offset)
540 : OatDexMethodVisitor(writer, relative_offset),
541 out_(out),
542 file_offset_(file_offset) {
543 }
544
545 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
546 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
547 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
548
549 if (compiled_method != NULL) { // ie. not an abstract method
550 size_t file_offset = file_offset_;
551 OutputStream* out = out_;
552
553 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode();
554 if (quick_code != nullptr) {
555 CHECK(compiled_method->GetPortableCode() == nullptr);
556 uint32_t aligned_offset = compiled_method->AlignCode(offset_);
557 uint32_t aligned_code_delta = aligned_offset - offset_;
558 if (aligned_code_delta != 0) {
559 static const uint8_t kPadding[] = {
560 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
561 };
562 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
563 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
564 ReportWriteFailure("code alignment padding", it);
565 return false;
566 }
567 writer_->size_code_alignment_ += aligned_code_delta;
568 offset_ += aligned_code_delta;
569 DCHECK_OFFSET_();
570 }
571 DCHECK_ALIGNED_PARAM(offset_,
572 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
573 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
574 CHECK_NE(code_size, 0U);
575
576 // Deduplicate code arrays.
577 const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_];
578 DCHECK(method_offsets.code_offset_ < offset_ || method_offsets.code_offset_ ==
Vladimir Marko7624d252014-05-02 14:40:15 +0100579 offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta())
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100580 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
581 if (method_offsets.code_offset_ >= offset_) {
Vladimir Marko7624d252014-05-02 14:40:15 +0100582 const OatQuickMethodHeader& method_header = oat_class->method_headers_[method_offsets_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100583 if (!out->WriteFully(&method_header, sizeof(method_header))) {
584 ReportWriteFailure("method header", it);
585 return false;
586 }
587 writer_->size_method_header_ += sizeof(method_header);
588 offset_ += sizeof(method_header);
589 DCHECK_OFFSET_();
590 if (!out->WriteFully(&(*quick_code)[0], code_size)) {
591 ReportWriteFailure("method code", it);
592 return false;
593 }
594 writer_->size_code_ += code_size;
595 offset_ += code_size;
596 }
597 DCHECK_OFFSET_();
598 }
599 ++method_offsets_index_;
600 }
601
602 return true;
603 }
604
605 private:
606 OutputStream* const out_;
607 size_t const file_offset_;
608
609 void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) {
610 PLOG(ERROR) << "Failed to write " << what << " for "
611 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
612 }
613};
614
615template <typename DataAccess>
616class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor {
617 public:
618 WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
619 size_t relative_offset)
620 : OatDexMethodVisitor(writer, relative_offset),
621 out_(out),
622 file_offset_(file_offset) {
623 }
624
625 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
626 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
627 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
628
629 if (compiled_method != NULL) { // ie. not an abstract method
630 size_t file_offset = file_offset_;
631 OutputStream* out = out_;
632
633 uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_);
634 ++method_offsets_index_;
635
636 // Write deduplicated map.
637 const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method);
638 size_t map_size = map->size() * sizeof((*map)[0]);
639 DCHECK((map_size == 0u && map_offset == 0u) ||
640 (map_size != 0u && map_offset != 0u && map_offset <= offset_))
641 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
642 if (map_size != 0u && map_offset == offset_) {
643 if (UNLIKELY(!out->WriteFully(&(*map)[0], map_size))) {
644 ReportWriteFailure(it);
645 return false;
646 }
647 offset_ += map_size;
648 }
649 DCHECK_OFFSET_();
650 }
651
652 return true;
653 }
654
655 private:
656 OutputStream* const out_;
657 size_t const file_offset_;
658
659 void ReportWriteFailure(const ClassDataItemIterator& it) {
660 PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for "
661 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
662 }
663};
664
665// Visit all methods from all classes in all dex files with the specified visitor.
666bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) {
667 for (const DexFile* dex_file : *dex_files_) {
668 const size_t class_def_count = dex_file->NumClassDefs();
669 for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) {
670 if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) {
671 return false;
672 }
673 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
674 const byte* class_data = dex_file->GetClassData(class_def);
675 if (class_data != NULL) { // ie not an empty class, such as a marker interface
676 ClassDataItemIterator it(*dex_file, class_data);
677 while (it.HasNextStaticField()) {
678 it.Next();
679 }
680 while (it.HasNextInstanceField()) {
681 it.Next();
682 }
683 size_t class_def_method_index = 0u;
684 while (it.HasNextDirectMethod()) {
685 if (!visitor->VisitMethod(class_def_method_index, it)) {
686 return false;
687 }
688 ++class_def_method_index;
689 it.Next();
690 }
691 while (it.HasNextVirtualMethod()) {
692 if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) {
693 return false;
694 }
695 ++class_def_method_index;
696 it.Next();
697 }
698 }
699 if (UNLIKELY(!visitor->EndClass())) {
700 return false;
701 }
702 }
703 }
704 return true;
705}
706
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700707size_t OatWriter::InitOatHeader() {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700708 oat_header_ = OatHeader::Create(compiler_driver_->GetInstructionSet(),
709 compiler_driver_->GetInstructionSetFeatures(),
710 dex_files_,
711 image_file_location_oat_checksum_,
712 image_file_location_oat_begin_,
713 key_value_store_);
714
715 return oat_header_->GetHeaderSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700716}
717
718size_t OatWriter::InitOatDexFiles(size_t offset) {
719 // create the OatDexFiles
720 for (size_t i = 0; i != dex_files_->size(); ++i) {
721 const DexFile* dex_file = (*dex_files_)[i];
722 CHECK(dex_file != NULL);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800723 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700724 oat_dex_files_.push_back(oat_dex_file);
725 offset += oat_dex_file->SizeOf();
726 }
727 return offset;
728}
729
Brian Carlstrom89521892011-12-07 22:05:07 -0800730size_t OatWriter::InitDexFiles(size_t offset) {
731 // calculate the offsets within OatDexFiles to the DexFiles
732 for (size_t i = 0; i != dex_files_->size(); ++i) {
733 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700734 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800735 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700736 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800737
738 // set offset in OatDexFile to DexFile
739 oat_dex_files_[i]->dex_file_offset_ = offset;
740
741 const DexFile* dex_file = (*dex_files_)[i];
742 offset += dex_file->GetHeader().file_size_;
743 }
744 return offset;
745}
746
Brian Carlstrom389efb02012-01-11 12:06:26 -0800747size_t OatWriter::InitOatClasses(size_t offset) {
Brian Carlstrom389efb02012-01-11 12:06:26 -0800748 // calculate the offsets within OatDexFiles to OatClasses
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100749 InitOatClassesMethodVisitor visitor(this, offset);
750 bool success = VisitDexMethods(&visitor);
751 CHECK(success);
752 offset = visitor.GetOffset();
Brian Carlstromba150c32013-08-27 17:31:03 -0700753
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100754 // Update oat_dex_files_.
755 auto oat_class_it = oat_classes_.begin();
756 for (OatDexFile* oat_dex_file : oat_dex_files_) {
757 for (uint32_t& offset : oat_dex_file->methods_offsets_) {
758 DCHECK(oat_class_it != oat_classes_.end());
759 offset = (*oat_class_it)->offset_;
760 ++oat_class_it;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700761 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100762 oat_dex_file->UpdateChecksum(oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700763 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100764 CHECK(oat_class_it == oat_classes_.end());
765
766 return offset;
767}
768
769size_t OatWriter::InitOatMaps(size_t offset) {
770 #define VISIT(VisitorType) \
771 do { \
772 VisitorType visitor(this, offset); \
773 bool success = VisitDexMethods(&visitor); \
774 DCHECK(success); \
775 offset = visitor.GetOffset(); \
776 } while (false)
777
778 VISIT(InitMapMethodVisitor<GcMapDataAccess>);
779 VISIT(InitMapMethodVisitor<MappingTableDataAccess>);
780 VISIT(InitMapMethodVisitor<VmapTableDataAccess>);
781
782 #undef VISIT
783
Brian Carlstrome24fa612011-09-29 00:53:55 -0700784 return offset;
785}
786
787size_t OatWriter::InitOatCode(size_t offset) {
788 // calculate the offsets within OatHeader to executable code
789 size_t old_offset = offset;
Dave Allison50abf0a2014-06-23 13:19:59 -0700790 size_t adjusted_offset = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700791 // required to be on a new page boundary
792 offset = RoundUp(offset, kPageSize);
793 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700794 size_executable_offset_alignment_ = offset - old_offset;
795 if (compiler_driver_->IsImage()) {
Alex Lighta59dd802014-07-02 16:28:08 -0700796 CHECK_EQ(image_patch_delta_, 0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700797 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700798
Ian Rogers848871b2013-08-05 10:56:33 -0700799 #define DO_TRAMPOLINE(field, fn_name) \
800 offset = CompiledCode::AlignCode(offset, instruction_set); \
Dave Allison50abf0a2014-06-23 13:19:59 -0700801 adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \
802 oat_header_->Set ## fn_name ## Offset(adjusted_offset); \
Ian Rogers848871b2013-08-05 10:56:33 -0700803 field.reset(compiler_driver_->Create ## fn_name()); \
804 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700805
Ian Rogers848871b2013-08-05 10:56:33 -0700806 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge);
807 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge);
808 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Jeff Hao88474b42013-10-23 16:24:40 -0700809 DO_TRAMPOLINE(portable_imt_conflict_trampoline_, PortableImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700810 DO_TRAMPOLINE(portable_resolution_trampoline_, PortableResolutionTrampoline);
811 DO_TRAMPOLINE(portable_to_interpreter_bridge_, PortableToInterpreterBridge);
Andreas Gampe2da88232014-02-27 12:26:20 -0800812 DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline);
Jeff Hao88474b42013-10-23 16:24:40 -0700813 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700814 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
815 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700816
Ian Rogers848871b2013-08-05 10:56:33 -0700817 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700818 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700819 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
820 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
821 oat_header_->SetJniDlsymLookupOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700822 oat_header_->SetPortableImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700823 oat_header_->SetPortableResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700824 oat_header_->SetPortableToInterpreterBridgeOffset(0);
Andreas Gampe2da88232014-02-27 12:26:20 -0800825 oat_header_->SetQuickGenericJniTrampolineOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700826 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700827 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700828 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Alex Lighta59dd802014-07-02 16:28:08 -0700829 oat_header_->SetImagePatchDelta(image_patch_delta_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700830 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700831 return offset;
832}
833
834size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100835 #define VISIT(VisitorType) \
836 do { \
837 VisitorType visitor(this, offset); \
838 bool success = VisitDexMethods(&visitor); \
839 DCHECK(success); \
840 offset = visitor.GetOffset(); \
841 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700842
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100843 VISIT(InitCodeMethodVisitor);
Ian Rogers1212a022013-03-04 10:48:41 -0800844 if (compiler_driver_->IsImage()) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100845 VISIT(InitImageMethodVisitor);
Ian Rogers0571d352011-11-03 19:51:38 -0700846 }
Logan Chien8b977d32012-02-21 19:14:55 +0800847
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100848 #undef VISIT
849
Brian Carlstrome24fa612011-09-29 00:53:55 -0700850 return offset;
851}
852
Ian Rogers3d504072014-03-01 09:16:49 -0800853bool OatWriter::Write(OutputStream* out) {
854 const size_t file_offset = out->Seek(0, kSeekCurrent);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700855
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700856 size_t header_size = oat_header_->GetHeaderSize();
857 if (!out->WriteFully(oat_header_, header_size)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800858 PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700859 return false;
860 }
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700861 size_oat_header_ += sizeof(OatHeader);
862 size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700863
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700864 if (!WriteTables(out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800865 LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700866 return false;
867 }
868
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100869 size_t relative_offset = out->Seek(0, kSeekCurrent) - file_offset;
870 relative_offset = WriteMaps(out, file_offset, relative_offset);
871 if (relative_offset == 0) {
872 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
873 return false;
874 }
875
876 relative_offset = WriteCode(out, file_offset, relative_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700877 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800878 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700879 return false;
880 }
881
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700882 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
883 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800884 LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700885 return false;
886 }
887
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700888 if (kIsDebugBuild) {
889 uint32_t size_total = 0;
890 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700891 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700892 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700893
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700894 DO_STAT(size_dex_file_alignment_);
895 DO_STAT(size_executable_offset_alignment_);
896 DO_STAT(size_oat_header_);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700897 DO_STAT(size_oat_header_key_value_store_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700898 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -0700899 DO_STAT(size_interpreter_to_interpreter_bridge_);
900 DO_STAT(size_interpreter_to_compiled_code_bridge_);
901 DO_STAT(size_jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -0700902 DO_STAT(size_portable_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700903 DO_STAT(size_portable_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700904 DO_STAT(size_portable_to_interpreter_bridge_);
Andreas Gampe2da88232014-02-27 12:26:20 -0800905 DO_STAT(size_quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -0700906 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700907 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700908 DO_STAT(size_quick_to_interpreter_bridge_);
909 DO_STAT(size_trampoline_alignment_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100910 DO_STAT(size_method_header_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700911 DO_STAT(size_code_);
912 DO_STAT(size_code_alignment_);
913 DO_STAT(size_mapping_table_);
914 DO_STAT(size_vmap_table_);
915 DO_STAT(size_gc_map_);
916 DO_STAT(size_oat_dex_file_location_size_);
917 DO_STAT(size_oat_dex_file_location_data_);
918 DO_STAT(size_oat_dex_file_location_checksum_);
919 DO_STAT(size_oat_dex_file_offset_);
920 DO_STAT(size_oat_dex_file_methods_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700921 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700922 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700923 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700924 DO_STAT(size_oat_class_method_offsets_);
925 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700926
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700927 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Ian Rogers3d504072014-03-01 09:16:49 -0800928 CHECK_EQ(file_offset + size_total, static_cast<uint32_t>(out->Seek(0, kSeekCurrent)));
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700929 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700930 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700931
Ian Rogers3d504072014-03-01 09:16:49 -0800932 CHECK_EQ(file_offset + size_, static_cast<uint32_t>(out->Seek(0, kSeekCurrent)));
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700933 CHECK_EQ(size_, relative_offset);
934
Brian Carlstrome24fa612011-09-29 00:53:55 -0700935 return true;
936}
937
Ian Rogers3d504072014-03-01 09:16:49 -0800938bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700939 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700940 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800941 PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700942 return false;
943 }
944 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800945 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700946 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Ian Rogers3d504072014-03-01 09:16:49 -0800947 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -0800948 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
949 const DexFile* dex_file = (*dex_files_)[i];
950 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
951 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
952 return false;
953 }
954 const DexFile* dex_file = (*dex_files_)[i];
Ian Rogers3d504072014-03-01 09:16:49 -0800955 if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700956 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
Ian Rogers3d504072014-03-01 09:16:49 -0800957 << " to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800958 return false;
959 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700960 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800961 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800962 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700963 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800964 PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700965 return false;
966 }
967 }
968 return true;
969}
970
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100971size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) {
972 #define VISIT(VisitorType) \
973 do { \
974 VisitorType visitor(this, out, file_offset, relative_offset); \
975 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
976 return 0; \
977 } \
978 relative_offset = visitor.GetOffset(); \
979 } while (false)
980
981 size_t gc_maps_offset = relative_offset;
982 VISIT(WriteMapMethodVisitor<GcMapDataAccess>);
983 size_gc_map_ = relative_offset - gc_maps_offset;
984
985 size_t mapping_tables_offset = relative_offset;
986 VISIT(WriteMapMethodVisitor<MappingTableDataAccess>);
987 size_mapping_table_ = relative_offset - mapping_tables_offset;
988
989 size_t vmap_tables_offset = relative_offset;
990 VISIT(WriteMapMethodVisitor<VmapTableDataAccess>);
991 size_vmap_table_ = relative_offset - vmap_tables_offset;
992
993 #undef VISIT
994
995 return relative_offset;
996}
997
998size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) {
Ian Rogers3d504072014-03-01 09:16:49 -0800999 off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001000 relative_offset += size_executable_offset_alignment_;
1001 DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset());
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001002 size_t expected_file_offset = file_offset + relative_offset;
1003 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001004 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Ian Rogers3d504072014-03-01 09:16:49 -08001005 << " Expected: " << expected_file_offset << " File: " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001006 return 0;
1007 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001008 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001009 if (compiler_driver_->IsImage()) {
1010 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001011
Ian Rogers848871b2013-08-05 10:56:33 -07001012 #define DO_TRAMPOLINE(field) \
1013 do { \
1014 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
1015 uint32_t alignment_padding = aligned_offset - relative_offset; \
Ian Rogers3d504072014-03-01 09:16:49 -08001016 out->Seek(alignment_padding, kSeekCurrent); \
Ian Rogers848871b2013-08-05 10:56:33 -07001017 size_trampoline_alignment_ += alignment_padding; \
Ian Rogers3d504072014-03-01 09:16:49 -08001018 if (!out->WriteFully(&(*field)[0], field->size())) { \
1019 PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \
Ian Rogers848871b2013-08-05 10:56:33 -07001020 return false; \
1021 } \
1022 size_ ## field += field->size(); \
1023 relative_offset += alignment_padding + field->size(); \
1024 DCHECK_OFFSET(); \
1025 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001026
Ian Rogers848871b2013-08-05 10:56:33 -07001027 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_);
1028 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_);
1029 DO_TRAMPOLINE(jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -07001030 DO_TRAMPOLINE(portable_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001031 DO_TRAMPOLINE(portable_resolution_trampoline_);
1032 DO_TRAMPOLINE(portable_to_interpreter_bridge_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001033 DO_TRAMPOLINE(quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001034 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001035 DO_TRAMPOLINE(quick_resolution_trampoline_);
1036 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
1037 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001038 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001039 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001040}
1041
Ian Rogers3d504072014-03-01 09:16:49 -08001042size_t OatWriter::WriteCodeDexFiles(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001043 const size_t file_offset,
1044 size_t relative_offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001045 #define VISIT(VisitorType) \
1046 do { \
1047 VisitorType visitor(this, out, file_offset, relative_offset); \
1048 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1049 return 0; \
1050 } \
1051 relative_offset = visitor.GetOffset(); \
1052 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001053
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001054 VISIT(WriteCodeMethodVisitor);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001055
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001056 #undef VISIT
Brian Carlstrom265091e2013-01-30 14:08:26 -08001057
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001058 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001059}
1060
Brian Carlstrom265091e2013-01-30 14:08:26 -08001061OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
1062 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -08001063 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001064 dex_file_location_size_ = location.size();
1065 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001066 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -08001067 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001068 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001069}
1070
1071size_t OatWriter::OatDexFile::SizeOf() const {
1072 return sizeof(dex_file_location_size_)
1073 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001074 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -08001075 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001076 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001077}
1078
Ian Rogers3d504072014-03-01 09:16:49 -08001079void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const {
1080 oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
1081 oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
1082 oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
1083 oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
1084 oat_header->UpdateChecksum(&methods_offsets_[0],
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001085 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001086}
1087
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001088bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001089 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001090 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001091 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001092 if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
1093 PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001094 return false;
1095 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001096 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001097 if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
1098 PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001099 return false;
1100 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001101 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Ian Rogers3d504072014-03-01 09:16:49 -08001102 if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
1103 PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001104 return false;
1105 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001106 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Ian Rogers3d504072014-03-01 09:16:49 -08001107 if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
1108 PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001109 return false;
1110 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001111 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Ian Rogers3d504072014-03-01 09:16:49 -08001112 if (!out->WriteFully(&methods_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001113 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001114 PLOG(ERROR) << "Failed to write methods offsets 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_methods_offsets_ +=
1118 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001119 return true;
1120}
1121
Brian Carlstromba150c32013-08-27 17:31:03 -07001122OatWriter::OatClass::OatClass(size_t offset,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001123 const std::vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -07001124 uint32_t num_non_null_compiled_methods,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001125 mirror::Class::Status status)
1126 : compiled_methods_(compiled_methods) {
1127 uint32_t num_methods = compiled_methods.size();
Brian Carlstromba150c32013-08-27 17:31:03 -07001128 CHECK_LE(num_non_null_compiled_methods, num_methods);
1129
Brian Carlstrom265091e2013-01-30 14:08:26 -08001130 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -07001131 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
1132
1133 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
1134 // apply when there are 0 methods, we just arbitrarily say that 0
1135 // methods means kOatClassNoneCompiled and that we won't use
1136 // kOatClassAllCompiled unless there is at least one compiled
1137 // method. This means in an interpretter only system, we can assert
1138 // that all classes are kOatClassNoneCompiled.
1139 if (num_non_null_compiled_methods == 0) {
1140 type_ = kOatClassNoneCompiled;
1141 } else if (num_non_null_compiled_methods == num_methods) {
1142 type_ = kOatClassAllCompiled;
1143 } else {
1144 type_ = kOatClassSomeCompiled;
1145 }
1146
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001147 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -07001148 method_offsets_.resize(num_non_null_compiled_methods);
Vladimir Marko8a630572014-04-09 18:45:35 +01001149 method_headers_.resize(num_non_null_compiled_methods);
Brian Carlstromba150c32013-08-27 17:31:03 -07001150
1151 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
1152 if (type_ == kOatClassSomeCompiled) {
1153 method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator());
1154 method_bitmap_size_ = method_bitmap_->GetSizeOf();
1155 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
1156 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
1157 } else {
1158 method_bitmap_ = NULL;
1159 method_bitmap_size_ = 0;
1160 }
1161
1162 for (size_t i = 0; i < num_methods; i++) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001163 CompiledMethod* compiled_method = compiled_methods_[i];
Brian Carlstromba150c32013-08-27 17:31:03 -07001164 if (compiled_method == NULL) {
1165 oat_method_offsets_offsets_from_oat_class_[i] = 0;
1166 } else {
1167 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
1168 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
1169 if (type_ == kOatClassSomeCompiled) {
1170 method_bitmap_->SetBit(i);
1171 }
1172 }
1173 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001174}
1175
Brian Carlstromba150c32013-08-27 17:31:03 -07001176OatWriter::OatClass::~OatClass() {
Mathieu Chartier661974a2014-01-09 11:23:53 -08001177 delete method_bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -07001178}
1179
Brian Carlstrom265091e2013-01-30 14:08:26 -08001180size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
1181 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001182 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
1183 if (method_offset == 0) {
1184 return 0;
1185 }
1186 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001187}
1188
1189size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
1190 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001191 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08001192}
1193
1194size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001195 return sizeof(status_)
1196 + sizeof(type_)
1197 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
1198 + method_bitmap_size_
1199 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001200}
1201
Ian Rogers3d504072014-03-01 09:16:49 -08001202void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const {
1203 oat_header->UpdateChecksum(&status_, sizeof(status_));
1204 oat_header->UpdateChecksum(&type_, sizeof(type_));
Brian Carlstromba150c32013-08-27 17:31:03 -07001205 if (method_bitmap_size_ != 0) {
1206 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001207 oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_));
1208 oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001209 }
Ian Rogers3d504072014-03-01 09:16:49 -08001210 oat_header->UpdateChecksum(&method_offsets_[0],
1211 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001212}
1213
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001214bool OatWriter::OatClass::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001215 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001216 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001217 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001218 if (!out->WriteFully(&status_, sizeof(status_))) {
1219 PLOG(ERROR) << "Failed to write class status to " << out->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001220 return false;
1221 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001222 oat_writer->size_oat_class_status_ += sizeof(status_);
Ian Rogers3d504072014-03-01 09:16:49 -08001223 if (!out->WriteFully(&type_, sizeof(type_))) {
1224 PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001225 return false;
1226 }
1227 oat_writer->size_oat_class_type_ += sizeof(type_);
1228 if (method_bitmap_size_ != 0) {
1229 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001230 if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
1231 PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001232 return false;
1233 }
1234 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001235 if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
1236 PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001237 return false;
1238 }
1239 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1240 }
Ian Rogers3d504072014-03-01 09:16:49 -08001241 if (!out->WriteFully(&method_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001242 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001243 PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001244 return false;
1245 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001246 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001247 return true;
1248}
1249
Brian Carlstrome24fa612011-09-29 00:53:55 -07001250} // namespace art