blob: 680ce0a801bc9b8999dbf478a052b1bfa04951fd [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
Ian Rogerse77493c2014-08-20 15:08:45 -070021#include "base/allocator.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070022#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080023#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080024#include "base/unix_file/fd_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070025#include "class_linker.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070026#include "compiled_class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070027#include "dex_file-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000028#include "dex/verification_results.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "gc/space/space.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070030#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "mirror/array.h"
32#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070033#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070034#include "os.h"
Brian Carlstromcd60ac72013-01-20 17:09:51 -080035#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070036#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070037#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070038#include "handle_scope-inl.h"
jeffhaoec014232012-09-05 10:42:25 -070039#include "verifier/method_verifier.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070040
41namespace art {
42
Vladimir Marko96c6ab92014-04-08 14:00:50 +010043#define DCHECK_OFFSET() \
44 DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out->Seek(0, kSeekCurrent)) \
45 << "file_offset=" << file_offset << " relative_offset=" << relative_offset
46
47#define DCHECK_OFFSET_() \
48 DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out->Seek(0, kSeekCurrent)) \
49 << "file_offset=" << file_offset << " offset_=" << offset_
50
Brian Carlstrom3320cf42011-10-04 14:58:28 -070051OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070052 uint32_t image_file_location_oat_checksum,
Ian Rogersef7d42f2014-01-06 12:55:46 -080053 uintptr_t image_file_location_oat_begin,
Alex Lighta59dd802014-07-02 16:28:08 -070054 int32_t image_patch_delta,
Ian Rogersca368cb2013-11-15 15:52:08 -080055 const CompilerDriver* compiler,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070056 TimingLogger* timings,
57 SafeMap<std::string, std::string>* key_value_store)
Jeff Hao0aba0ba2013-06-03 14:49:28 -070058 : compiler_driver_(compiler),
59 dex_files_(&dex_files),
60 image_file_location_oat_checksum_(image_file_location_oat_checksum),
61 image_file_location_oat_begin_(image_file_location_oat_begin),
Alex Lighta59dd802014-07-02 16:28:08 -070062 image_patch_delta_(image_patch_delta),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070063 key_value_store_(key_value_store),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070064 oat_header_(NULL),
65 size_dex_file_alignment_(0),
66 size_executable_offset_alignment_(0),
67 size_oat_header_(0),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070068 size_oat_header_key_value_store_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070069 size_dex_file_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070070 size_interpreter_to_interpreter_bridge_(0),
71 size_interpreter_to_compiled_code_bridge_(0),
72 size_jni_dlsym_lookup_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070073 size_portable_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070074 size_portable_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070075 size_portable_to_interpreter_bridge_(0),
Andreas Gampe2da88232014-02-27 12:26:20 -080076 size_quick_generic_jni_trampoline_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070077 size_quick_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070078 size_quick_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070079 size_quick_to_interpreter_bridge_(0),
80 size_trampoline_alignment_(0),
Vladimir Marko96c6ab92014-04-08 14:00:50 +010081 size_method_header_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070082 size_code_(0),
83 size_code_alignment_(0),
84 size_mapping_table_(0),
85 size_vmap_table_(0),
86 size_gc_map_(0),
87 size_oat_dex_file_location_size_(0),
88 size_oat_dex_file_location_data_(0),
89 size_oat_dex_file_location_checksum_(0),
90 size_oat_dex_file_offset_(0),
91 size_oat_dex_file_methods_offsets_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -070092 size_oat_class_type_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070093 size_oat_class_status_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -070094 size_oat_class_method_bitmaps_(0),
Dave Allisond6ed6422014-04-09 23:36:15 +000095 size_oat_class_method_offsets_(0) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070096 CHECK(key_value_store != nullptr);
97
Ian Rogersca368cb2013-11-15 15:52:08 -080098 size_t offset;
99 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700100 TimingLogger::ScopedTiming split("InitOatHeader", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800101 offset = InitOatHeader();
102 }
103 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700104 TimingLogger::ScopedTiming split("InitOatDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800105 offset = InitOatDexFiles(offset);
106 }
107 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700108 TimingLogger::ScopedTiming split("InitDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800109 offset = InitDexFiles(offset);
110 }
111 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700112 TimingLogger::ScopedTiming split("InitOatClasses", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800113 offset = InitOatClasses(offset);
114 }
115 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700116 TimingLogger::ScopedTiming split("InitOatMaps", timings);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100117 offset = InitOatMaps(offset);
118 }
119 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700120 TimingLogger::ScopedTiming split("InitOatCode", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800121 offset = InitOatCode(offset);
122 }
123 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700124 TimingLogger::ScopedTiming split("InitOatCodeDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800125 offset = InitOatCodeDexFiles(offset);
126 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700127 size_ = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700128
129 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700130 CHECK_EQ(compiler->IsImage(),
131 key_value_store_->find(OatHeader::kImageLocationKey) == key_value_store_->end());
Alex Lighta59dd802014-07-02 16:28:08 -0700132 CHECK_ALIGNED(image_patch_delta_, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700133}
134
Ian Rogers0571d352011-11-03 19:51:38 -0700135OatWriter::~OatWriter() {
136 delete oat_header_;
137 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800138 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -0700139}
140
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100141struct OatWriter::GcMapDataAccess {
142 static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
143 return &compiled_method->GetGcMap();
144 }
145
146 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
147 return oat_class->method_offsets_[method_offsets_index].gc_map_offset_;
148 }
149
150 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
151 ALWAYS_INLINE {
152 oat_class->method_offsets_[method_offsets_index].gc_map_offset_ = offset;
153 }
154
155 static const char* Name() ALWAYS_INLINE {
156 return "GC map";
157 }
158};
159
160struct OatWriter::MappingTableDataAccess {
161 static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
162 return &compiled_method->GetMappingTable();
163 }
164
165 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100166 uint32_t offset = oat_class->method_headers_[method_offsets_index].mapping_table_offset_;
167 return offset == 0u ? 0u :
168 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100169 }
170
171 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
172 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100173 oat_class->method_headers_[method_offsets_index].mapping_table_offset_ =
174 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100175 }
176
177 static const char* Name() ALWAYS_INLINE {
178 return "mapping table";
179 }
180};
181
182struct OatWriter::VmapTableDataAccess {
183 static const std::vector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
184 return &compiled_method->GetVmapTable();
185 }
186
187 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100188 uint32_t offset = oat_class->method_headers_[method_offsets_index].vmap_table_offset_;
189 return offset == 0u ? 0u :
190 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100191 }
192
193 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
194 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100195 oat_class->method_headers_[method_offsets_index].vmap_table_offset_ =
196 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100197 }
198
199 static const char* Name() ALWAYS_INLINE {
200 return "vmap table";
201 }
202};
203
204class OatWriter::DexMethodVisitor {
205 public:
206 DexMethodVisitor(OatWriter* writer, size_t offset)
207 : writer_(writer),
208 offset_(offset),
209 dex_file_(nullptr),
210 class_def_index_(DexFile::kDexNoIndex) {
211 }
212
213 virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) {
214 DCHECK(dex_file_ == nullptr);
215 DCHECK_EQ(class_def_index_, DexFile::kDexNoIndex);
216 dex_file_ = dex_file;
217 class_def_index_ = class_def_index;
218 return true;
219 }
220
221 virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0;
222
223 virtual bool EndClass() {
224 if (kIsDebugBuild) {
225 dex_file_ = nullptr;
226 class_def_index_ = DexFile::kDexNoIndex;
227 }
228 return true;
229 }
230
231 size_t GetOffset() const {
232 return offset_;
233 }
234
235 protected:
236 virtual ~DexMethodVisitor() { }
237
238 OatWriter* const writer_;
239
240 // The offset is usually advanced for each visited method by the derived class.
241 size_t offset_;
242
243 // The dex file and class def index are set in StartClass().
244 const DexFile* dex_file_;
245 size_t class_def_index_;
246};
247
248class OatWriter::OatDexMethodVisitor : public DexMethodVisitor {
249 public:
250 OatDexMethodVisitor(OatWriter* writer, size_t offset)
251 : DexMethodVisitor(writer, offset),
252 oat_class_index_(0u),
253 method_offsets_index_(0u) {
254 }
255
256 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
257 DexMethodVisitor::StartClass(dex_file, class_def_index);
258 DCHECK_LT(oat_class_index_, writer_->oat_classes_.size());
259 method_offsets_index_ = 0u;
260 return true;
261 }
262
263 bool EndClass() {
264 ++oat_class_index_;
265 return DexMethodVisitor::EndClass();
266 }
267
268 protected:
269 size_t oat_class_index_;
270 size_t method_offsets_index_;
271};
272
273class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor {
274 public:
275 InitOatClassesMethodVisitor(OatWriter* writer, size_t offset)
276 : DexMethodVisitor(writer, offset),
277 compiled_methods_(),
278 num_non_null_compiled_methods_(0u) {
279 compiled_methods_.reserve(256u);
280 }
281
282 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
283 DexMethodVisitor::StartClass(dex_file, class_def_index);
284 compiled_methods_.clear();
285 num_non_null_compiled_methods_ = 0u;
286 return true;
287 }
288
289 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
290 // Fill in the compiled_methods_ array for methods that have a
291 // CompiledMethod. We track the number of non-null entries in
292 // num_non_null_compiled_methods_ since we only want to allocate
293 // OatMethodOffsets for the compiled methods.
294 uint32_t method_idx = it.GetMemberIndex();
295 CompiledMethod* compiled_method =
296 writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx));
297 compiled_methods_.push_back(compiled_method);
298 if (compiled_method != nullptr) {
299 ++num_non_null_compiled_methods_;
300 }
301 return true;
302 }
303
304 bool EndClass() {
305 ClassReference class_ref(dex_file_, class_def_index_);
306 CompiledClass* compiled_class = writer_->compiler_driver_->GetCompiledClass(class_ref);
307 mirror::Class::Status status;
308 if (compiled_class != NULL) {
309 status = compiled_class->GetStatus();
310 } else if (writer_->compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) {
311 status = mirror::Class::kStatusError;
312 } else {
313 status = mirror::Class::kStatusNotReady;
314 }
315
316 OatClass* oat_class = new OatClass(offset_, compiled_methods_,
317 num_non_null_compiled_methods_, status);
318 writer_->oat_classes_.push_back(oat_class);
319 offset_ += oat_class->SizeOf();
320 return DexMethodVisitor::EndClass();
321 }
322
323 private:
324 std::vector<CompiledMethod*> compiled_methods_;
325 size_t num_non_null_compiled_methods_;
326};
327
328class OatWriter::InitCodeMethodVisitor : public OatDexMethodVisitor {
329 public:
330 InitCodeMethodVisitor(OatWriter* writer, size_t offset)
331 : OatDexMethodVisitor(writer, offset) {
332 }
333
334 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
335 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
336 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
337 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
338
339 if (compiled_method != nullptr) {
340 // Derived from CompiledMethod.
341 uint32_t quick_code_offset = 0;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100342
343 const std::vector<uint8_t>* portable_code = compiled_method->GetPortableCode();
344 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode();
345 if (portable_code != nullptr) {
346 CHECK(quick_code == nullptr);
347 size_t oat_method_offsets_offset =
348 oat_class->GetOatMethodOffsetsOffsetFromOatHeader(class_def_method_index);
349 compiled_method->AddOatdataOffsetToCompliledCodeOffset(
350 oat_method_offsets_offset + OFFSETOF_MEMBER(OatMethodOffsets, code_offset_));
351 } else {
352 CHECK(quick_code != nullptr);
353 offset_ = compiled_method->AlignCode(offset_);
354 DCHECK_ALIGNED_PARAM(offset_,
355 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
356 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
357 CHECK_NE(code_size, 0U);
358 uint32_t thumb_offset = compiled_method->CodeDelta();
Vladimir Marko7624d252014-05-02 14:40:15 +0100359 quick_code_offset = offset_ + sizeof(OatQuickMethodHeader) + thumb_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100360
Alex Light78382fa2014-06-06 15:45:32 -0700361 bool deduped = false;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100362
363 // Deduplicate code arrays.
Vladimir Markobd72fc12014-07-09 16:06:40 +0100364 auto lb = dedupe_map_.lower_bound(compiled_method);
365 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(compiled_method, lb->first)) {
366 quick_code_offset = lb->second;
Alex Light78382fa2014-06-06 15:45:32 -0700367 deduped = true;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100368 } else {
Vladimir Markobd72fc12014-07-09 16:06:40 +0100369 dedupe_map_.PutBefore(lb, compiled_method, quick_code_offset);
Vladimir Marko7624d252014-05-02 14:40:15 +0100370 }
371
372 // Update quick method header.
373 DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size());
374 OatQuickMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_];
375 uint32_t mapping_table_offset = method_header->mapping_table_offset_;
376 uint32_t vmap_table_offset = method_header->vmap_table_offset_;
377 // The code offset was 0 when the mapping/vmap table offset was set, so it's set
378 // to 0-offset and we need to adjust it by code_offset.
379 uint32_t code_offset = quick_code_offset - thumb_offset;
380 if (mapping_table_offset != 0u) {
381 mapping_table_offset += code_offset;
382 DCHECK_LT(mapping_table_offset, code_offset);
383 }
384 if (vmap_table_offset != 0u) {
385 vmap_table_offset += code_offset;
386 DCHECK_LT(vmap_table_offset, code_offset);
387 }
388 uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
389 uint32_t core_spill_mask = compiled_method->GetCoreSpillMask();
390 uint32_t fp_spill_mask = compiled_method->GetFpSpillMask();
391 *method_header = OatQuickMethodHeader(mapping_table_offset, vmap_table_offset,
392 frame_size_in_bytes, core_spill_mask, fp_spill_mask,
393 code_size);
394
395 // Update checksum if this wasn't a duplicate.
Vladimir Markobd72fc12014-07-09 16:06:40 +0100396 if (!deduped) {
Vladimir Marko8a630572014-04-09 18:45:35 +0100397 writer_->oat_header_->UpdateChecksum(method_header, sizeof(*method_header));
398 offset_ += sizeof(*method_header); // Method header is prepended before code.
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100399 writer_->oat_header_->UpdateChecksum(&(*quick_code)[0], code_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100400 offset_ += code_size;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100401 }
Alex Light78382fa2014-06-06 15:45:32 -0700402
Andreas Gampe79273802014-08-05 20:21:05 -0700403 if (writer_->compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols()) {
404 // Record debug information for this function if we are doing that.
Alex Light78382fa2014-06-06 15:45:32 -0700405
Alex Light78382fa2014-06-06 15:45:32 -0700406 std::string name = PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
407 if (deduped) {
Andreas Gampe79273802014-08-05 20:21:05 -0700408 // TODO We should place the DEDUPED tag on the first instance of a deduplicated symbol
409 // so that it will show up in a debuggerd crash report.
Alex Light78382fa2014-06-06 15:45:32 -0700410 name += " [ DEDUPED ]";
411 }
Andreas Gampe79273802014-08-05 20:21:05 -0700412
413 const uint32_t quick_code_start = quick_code_offset -
414 writer_->oat_header_->GetExecutableOffset();
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700415 const DexFile::CodeItem *code_item = it.GetMethodCodeItem();
Andreas Gampe79273802014-08-05 20:21:05 -0700416 writer_->method_info_.push_back(DebugInfo(name,
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700417 dex_file_->GetSourceFile(dex_file_->GetClassDef(class_def_index_)),
418 quick_code_start, quick_code_start + code_size,
419 code_item == nullptr ? nullptr : dex_file_->GetDebugInfoStream(code_item),
420 compiled_method));
Alex Light78382fa2014-06-06 15:45:32 -0700421 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100422 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100423
424 if (kIsDebugBuild) {
425 // We expect GC maps except when the class hasn't been verified or the method is native.
426 const CompilerDriver* compiler_driver = writer_->compiler_driver_;
427 ClassReference class_ref(dex_file_, class_def_index_);
428 CompiledClass* compiled_class = compiler_driver->GetCompiledClass(class_ref);
429 mirror::Class::Status status;
430 if (compiled_class != NULL) {
431 status = compiled_class->GetStatus();
432 } else if (compiler_driver->GetVerificationResults()->IsClassRejected(class_ref)) {
433 status = mirror::Class::kStatusError;
434 } else {
435 status = mirror::Class::kStatusNotReady;
436 }
437 const std::vector<uint8_t>& gc_map = compiled_method->GetGcMap();
438 size_t gc_map_size = gc_map.size() * sizeof(gc_map[0]);
439 bool is_native = (it.GetMemberAccessFlags() & kAccNative) != 0;
440 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
441 << &gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
442 << (status < mirror::Class::kStatusVerified) << " " << status << " "
443 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
444 }
445
446 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
447 OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_];
448 offsets->code_offset_ = quick_code_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100449 ++method_offsets_index_;
450 }
451
452 return true;
453 }
454
455 private:
456 // Deduplication is already done on a pointer basis by the compiler driver,
457 // so we can simply compare the pointers to find out if things are duplicated.
Vladimir Marko8a630572014-04-09 18:45:35 +0100458 SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100459};
460
461template <typename DataAccess>
462class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor {
463 public:
464 InitMapMethodVisitor(OatWriter* writer, size_t offset)
465 : OatDexMethodVisitor(writer, offset) {
466 }
467
468 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
469 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
470 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
471 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
472
473 if (compiled_method != nullptr) {
474 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
475 DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u);
476
477 const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method);
478 uint32_t map_size = map->size() * sizeof((*map)[0]);
479 if (map_size != 0u) {
Vladimir Markobd72fc12014-07-09 16:06:40 +0100480 auto lb = dedupe_map_.lower_bound(map);
481 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(map, lb->first)) {
482 DataAccess::SetOffset(oat_class, method_offsets_index_, lb->second);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100483 } else {
484 DataAccess::SetOffset(oat_class, method_offsets_index_, offset_);
Vladimir Markobd72fc12014-07-09 16:06:40 +0100485 dedupe_map_.PutBefore(lb, map, offset_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100486 offset_ += map_size;
487 writer_->oat_header_->UpdateChecksum(&(*map)[0], map_size);
488 }
489 }
490 ++method_offsets_index_;
491 }
492
493 return true;
494 }
495
496 private:
497 // Deduplication is already done on a pointer basis by the compiler driver,
498 // so we can simply compare the pointers to find out if things are duplicated.
499 SafeMap<const std::vector<uint8_t>*, uint32_t> dedupe_map_;
500};
501
502class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor {
503 public:
504 InitImageMethodVisitor(OatWriter* writer, size_t offset)
505 : OatDexMethodVisitor(writer, offset) {
506 }
507
508 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
509 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
510 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
511 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
512
Vladimir Marko7624d252014-05-02 14:40:15 +0100513 OatMethodOffsets offsets(0u, 0u);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100514 if (compiled_method != nullptr) {
515 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
516 offsets = oat_class->method_offsets_[method_offsets_index_];
517 ++method_offsets_index_;
518 }
519
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100520 ClassLinker* linker = Runtime::Current()->GetClassLinker();
521 InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_));
522 // Unchecked as we hold mutator_lock_ on entry.
523 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700524 StackHandleScope<2> hs(soa.Self());
525 Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache(*dex_file_)));
Vladimir Marko7624d252014-05-02 14:40:15 +0100526 mirror::ArtMethod* method = linker->ResolveMethod(*dex_file_, it.GetMemberIndex(), dex_cache,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700527 NullHandle<mirror::ClassLoader>(),
528 NullHandle<mirror::ArtMethod>(),
529 invoke_type);
Andreas Gamped9efea62014-07-21 22:56:08 -0700530 if (method == nullptr) {
531 LOG(ERROR) << "Unexpected failure to resolve a method: "
532 << PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
533 soa.Self()->AssertPendingException();
534 mirror::Throwable* exc = soa.Self()->GetException(nullptr);
535 std::string dump = exc->Dump();
536 LOG(FATAL) << dump;
537 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100538 // Portable code offsets are set by ElfWriterMclinker::FixupCompiledCodeOffset after linking.
539 method->SetQuickOatCodeOffset(offsets.code_offset_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100540 method->SetOatNativeGcMapOffset(offsets.gc_map_offset_);
541
542 return true;
543 }
544};
545
546class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor {
547 public:
548 WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
549 size_t relative_offset)
550 : OatDexMethodVisitor(writer, relative_offset),
551 out_(out),
552 file_offset_(file_offset) {
553 }
554
555 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
556 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
557 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
558
559 if (compiled_method != NULL) { // ie. not an abstract method
560 size_t file_offset = file_offset_;
561 OutputStream* out = out_;
562
563 const std::vector<uint8_t>* quick_code = compiled_method->GetQuickCode();
564 if (quick_code != nullptr) {
565 CHECK(compiled_method->GetPortableCode() == nullptr);
566 uint32_t aligned_offset = compiled_method->AlignCode(offset_);
567 uint32_t aligned_code_delta = aligned_offset - offset_;
568 if (aligned_code_delta != 0) {
569 static const uint8_t kPadding[] = {
570 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
571 };
572 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
573 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
574 ReportWriteFailure("code alignment padding", it);
575 return false;
576 }
577 writer_->size_code_alignment_ += aligned_code_delta;
578 offset_ += aligned_code_delta;
579 DCHECK_OFFSET_();
580 }
581 DCHECK_ALIGNED_PARAM(offset_,
582 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
583 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
584 CHECK_NE(code_size, 0U);
585
586 // Deduplicate code arrays.
587 const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_];
588 DCHECK(method_offsets.code_offset_ < offset_ || method_offsets.code_offset_ ==
Vladimir Marko7624d252014-05-02 14:40:15 +0100589 offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta())
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100590 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
591 if (method_offsets.code_offset_ >= offset_) {
Vladimir Marko7624d252014-05-02 14:40:15 +0100592 const OatQuickMethodHeader& method_header = oat_class->method_headers_[method_offsets_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100593 if (!out->WriteFully(&method_header, sizeof(method_header))) {
594 ReportWriteFailure("method header", it);
595 return false;
596 }
597 writer_->size_method_header_ += sizeof(method_header);
598 offset_ += sizeof(method_header);
599 DCHECK_OFFSET_();
600 if (!out->WriteFully(&(*quick_code)[0], code_size)) {
601 ReportWriteFailure("method code", it);
602 return false;
603 }
604 writer_->size_code_ += code_size;
605 offset_ += code_size;
606 }
607 DCHECK_OFFSET_();
608 }
609 ++method_offsets_index_;
610 }
611
612 return true;
613 }
614
615 private:
616 OutputStream* const out_;
617 size_t const file_offset_;
618
619 void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) {
620 PLOG(ERROR) << "Failed to write " << what << " for "
621 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
622 }
623};
624
625template <typename DataAccess>
626class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor {
627 public:
628 WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
629 size_t relative_offset)
630 : OatDexMethodVisitor(writer, relative_offset),
631 out_(out),
632 file_offset_(file_offset) {
633 }
634
635 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
636 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
637 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
638
639 if (compiled_method != NULL) { // ie. not an abstract method
640 size_t file_offset = file_offset_;
641 OutputStream* out = out_;
642
643 uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_);
644 ++method_offsets_index_;
645
646 // Write deduplicated map.
647 const std::vector<uint8_t>* map = DataAccess::GetData(compiled_method);
648 size_t map_size = map->size() * sizeof((*map)[0]);
649 DCHECK((map_size == 0u && map_offset == 0u) ||
650 (map_size != 0u && map_offset != 0u && map_offset <= offset_))
651 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
652 if (map_size != 0u && map_offset == offset_) {
653 if (UNLIKELY(!out->WriteFully(&(*map)[0], map_size))) {
654 ReportWriteFailure(it);
655 return false;
656 }
657 offset_ += map_size;
658 }
659 DCHECK_OFFSET_();
660 }
661
662 return true;
663 }
664
665 private:
666 OutputStream* const out_;
667 size_t const file_offset_;
668
669 void ReportWriteFailure(const ClassDataItemIterator& it) {
670 PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for "
671 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
672 }
673};
674
675// Visit all methods from all classes in all dex files with the specified visitor.
676bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) {
677 for (const DexFile* dex_file : *dex_files_) {
678 const size_t class_def_count = dex_file->NumClassDefs();
679 for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) {
680 if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) {
681 return false;
682 }
683 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
684 const byte* class_data = dex_file->GetClassData(class_def);
685 if (class_data != NULL) { // ie not an empty class, such as a marker interface
686 ClassDataItemIterator it(*dex_file, class_data);
687 while (it.HasNextStaticField()) {
688 it.Next();
689 }
690 while (it.HasNextInstanceField()) {
691 it.Next();
692 }
693 size_t class_def_method_index = 0u;
694 while (it.HasNextDirectMethod()) {
695 if (!visitor->VisitMethod(class_def_method_index, it)) {
696 return false;
697 }
698 ++class_def_method_index;
699 it.Next();
700 }
701 while (it.HasNextVirtualMethod()) {
702 if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) {
703 return false;
704 }
705 ++class_def_method_index;
706 it.Next();
707 }
708 }
709 if (UNLIKELY(!visitor->EndClass())) {
710 return false;
711 }
712 }
713 }
714 return true;
715}
716
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700717size_t OatWriter::InitOatHeader() {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700718 oat_header_ = OatHeader::Create(compiler_driver_->GetInstructionSet(),
719 compiler_driver_->GetInstructionSetFeatures(),
720 dex_files_,
721 image_file_location_oat_checksum_,
722 image_file_location_oat_begin_,
723 key_value_store_);
724
725 return oat_header_->GetHeaderSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700726}
727
728size_t OatWriter::InitOatDexFiles(size_t offset) {
729 // create the OatDexFiles
730 for (size_t i = 0; i != dex_files_->size(); ++i) {
731 const DexFile* dex_file = (*dex_files_)[i];
732 CHECK(dex_file != NULL);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800733 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700734 oat_dex_files_.push_back(oat_dex_file);
735 offset += oat_dex_file->SizeOf();
736 }
737 return offset;
738}
739
Brian Carlstrom89521892011-12-07 22:05:07 -0800740size_t OatWriter::InitDexFiles(size_t offset) {
741 // calculate the offsets within OatDexFiles to the DexFiles
742 for (size_t i = 0; i != dex_files_->size(); ++i) {
743 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700744 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800745 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700746 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800747
748 // set offset in OatDexFile to DexFile
749 oat_dex_files_[i]->dex_file_offset_ = offset;
750
751 const DexFile* dex_file = (*dex_files_)[i];
752 offset += dex_file->GetHeader().file_size_;
753 }
754 return offset;
755}
756
Brian Carlstrom389efb02012-01-11 12:06:26 -0800757size_t OatWriter::InitOatClasses(size_t offset) {
Brian Carlstrom389efb02012-01-11 12:06:26 -0800758 // calculate the offsets within OatDexFiles to OatClasses
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100759 InitOatClassesMethodVisitor visitor(this, offset);
760 bool success = VisitDexMethods(&visitor);
761 CHECK(success);
762 offset = visitor.GetOffset();
Brian Carlstromba150c32013-08-27 17:31:03 -0700763
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100764 // Update oat_dex_files_.
765 auto oat_class_it = oat_classes_.begin();
766 for (OatDexFile* oat_dex_file : oat_dex_files_) {
767 for (uint32_t& offset : oat_dex_file->methods_offsets_) {
768 DCHECK(oat_class_it != oat_classes_.end());
769 offset = (*oat_class_it)->offset_;
770 ++oat_class_it;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700771 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100772 oat_dex_file->UpdateChecksum(oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700773 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100774 CHECK(oat_class_it == oat_classes_.end());
775
776 return offset;
777}
778
779size_t OatWriter::InitOatMaps(size_t offset) {
780 #define VISIT(VisitorType) \
781 do { \
782 VisitorType visitor(this, offset); \
783 bool success = VisitDexMethods(&visitor); \
784 DCHECK(success); \
785 offset = visitor.GetOffset(); \
786 } while (false)
787
788 VISIT(InitMapMethodVisitor<GcMapDataAccess>);
789 VISIT(InitMapMethodVisitor<MappingTableDataAccess>);
790 VISIT(InitMapMethodVisitor<VmapTableDataAccess>);
791
792 #undef VISIT
793
Brian Carlstrome24fa612011-09-29 00:53:55 -0700794 return offset;
795}
796
797size_t OatWriter::InitOatCode(size_t offset) {
798 // calculate the offsets within OatHeader to executable code
799 size_t old_offset = offset;
Dave Allison50abf0a2014-06-23 13:19:59 -0700800 size_t adjusted_offset = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700801 // required to be on a new page boundary
802 offset = RoundUp(offset, kPageSize);
803 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700804 size_executable_offset_alignment_ = offset - old_offset;
805 if (compiler_driver_->IsImage()) {
Alex Lighta59dd802014-07-02 16:28:08 -0700806 CHECK_EQ(image_patch_delta_, 0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700807 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700808
Ian Rogers848871b2013-08-05 10:56:33 -0700809 #define DO_TRAMPOLINE(field, fn_name) \
810 offset = CompiledCode::AlignCode(offset, instruction_set); \
Dave Allison50abf0a2014-06-23 13:19:59 -0700811 adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \
812 oat_header_->Set ## fn_name ## Offset(adjusted_offset); \
Ian Rogers848871b2013-08-05 10:56:33 -0700813 field.reset(compiler_driver_->Create ## fn_name()); \
814 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700815
Ian Rogers848871b2013-08-05 10:56:33 -0700816 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge);
817 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge);
818 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Jeff Hao88474b42013-10-23 16:24:40 -0700819 DO_TRAMPOLINE(portable_imt_conflict_trampoline_, PortableImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700820 DO_TRAMPOLINE(portable_resolution_trampoline_, PortableResolutionTrampoline);
821 DO_TRAMPOLINE(portable_to_interpreter_bridge_, PortableToInterpreterBridge);
Andreas Gampe2da88232014-02-27 12:26:20 -0800822 DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline);
Jeff Hao88474b42013-10-23 16:24:40 -0700823 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -0700824 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
825 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700826
Ian Rogers848871b2013-08-05 10:56:33 -0700827 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700828 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700829 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
830 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
831 oat_header_->SetJniDlsymLookupOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700832 oat_header_->SetPortableImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700833 oat_header_->SetPortableResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700834 oat_header_->SetPortableToInterpreterBridgeOffset(0);
Andreas Gampe2da88232014-02-27 12:26:20 -0800835 oat_header_->SetQuickGenericJniTrampolineOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -0700836 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700837 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -0700838 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Alex Lighta59dd802014-07-02 16:28:08 -0700839 oat_header_->SetImagePatchDelta(image_patch_delta_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700840 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700841 return offset;
842}
843
844size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100845 #define VISIT(VisitorType) \
846 do { \
847 VisitorType visitor(this, offset); \
848 bool success = VisitDexMethods(&visitor); \
849 DCHECK(success); \
850 offset = visitor.GetOffset(); \
851 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700852
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100853 VISIT(InitCodeMethodVisitor);
Ian Rogers1212a022013-03-04 10:48:41 -0800854 if (compiler_driver_->IsImage()) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100855 VISIT(InitImageMethodVisitor);
Ian Rogers0571d352011-11-03 19:51:38 -0700856 }
Logan Chien8b977d32012-02-21 19:14:55 +0800857
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100858 #undef VISIT
859
Brian Carlstrome24fa612011-09-29 00:53:55 -0700860 return offset;
861}
862
Ian Rogers3d504072014-03-01 09:16:49 -0800863bool OatWriter::Write(OutputStream* out) {
864 const size_t file_offset = out->Seek(0, kSeekCurrent);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700865
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700866 size_t header_size = oat_header_->GetHeaderSize();
867 if (!out->WriteFully(oat_header_, header_size)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800868 PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700869 return false;
870 }
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700871 size_oat_header_ += sizeof(OatHeader);
872 size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700873
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700874 if (!WriteTables(out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800875 LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700876 return false;
877 }
878
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100879 size_t relative_offset = out->Seek(0, kSeekCurrent) - file_offset;
880 relative_offset = WriteMaps(out, file_offset, relative_offset);
881 if (relative_offset == 0) {
882 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
883 return false;
884 }
885
886 relative_offset = WriteCode(out, file_offset, relative_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700887 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800888 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700889 return false;
890 }
891
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700892 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
893 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -0800894 LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700895 return false;
896 }
897
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700898 if (kIsDebugBuild) {
899 uint32_t size_total = 0;
900 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700901 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700902 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700903
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700904 DO_STAT(size_dex_file_alignment_);
905 DO_STAT(size_executable_offset_alignment_);
906 DO_STAT(size_oat_header_);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700907 DO_STAT(size_oat_header_key_value_store_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700908 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -0700909 DO_STAT(size_interpreter_to_interpreter_bridge_);
910 DO_STAT(size_interpreter_to_compiled_code_bridge_);
911 DO_STAT(size_jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -0700912 DO_STAT(size_portable_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700913 DO_STAT(size_portable_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700914 DO_STAT(size_portable_to_interpreter_bridge_);
Andreas Gampe2da88232014-02-27 12:26:20 -0800915 DO_STAT(size_quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -0700916 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700917 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -0700918 DO_STAT(size_quick_to_interpreter_bridge_);
919 DO_STAT(size_trampoline_alignment_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100920 DO_STAT(size_method_header_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700921 DO_STAT(size_code_);
922 DO_STAT(size_code_alignment_);
923 DO_STAT(size_mapping_table_);
924 DO_STAT(size_vmap_table_);
925 DO_STAT(size_gc_map_);
926 DO_STAT(size_oat_dex_file_location_size_);
927 DO_STAT(size_oat_dex_file_location_data_);
928 DO_STAT(size_oat_dex_file_location_checksum_);
929 DO_STAT(size_oat_dex_file_offset_);
930 DO_STAT(size_oat_dex_file_methods_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700931 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700932 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700933 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700934 DO_STAT(size_oat_class_method_offsets_);
935 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700936
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700937 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Ian Rogers3d504072014-03-01 09:16:49 -0800938 CHECK_EQ(file_offset + size_total, static_cast<uint32_t>(out->Seek(0, kSeekCurrent)));
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700939 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -0700940 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700941
Ian Rogers3d504072014-03-01 09:16:49 -0800942 CHECK_EQ(file_offset + size_, static_cast<uint32_t>(out->Seek(0, kSeekCurrent)));
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700943 CHECK_EQ(size_, relative_offset);
944
Brian Carlstrome24fa612011-09-29 00:53:55 -0700945 return true;
946}
947
Ian Rogers3d504072014-03-01 09:16:49 -0800948bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700949 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700950 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800951 PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700952 return false;
953 }
954 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800955 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700956 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Ian Rogers3d504072014-03-01 09:16:49 -0800957 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -0800958 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
959 const DexFile* dex_file = (*dex_files_)[i];
960 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
961 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
962 return false;
963 }
964 const DexFile* dex_file = (*dex_files_)[i];
Ian Rogers3d504072014-03-01 09:16:49 -0800965 if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700966 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
Ian Rogers3d504072014-03-01 09:16:49 -0800967 << " to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -0800968 return false;
969 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700970 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800971 }
Brian Carlstrom389efb02012-01-11 12:06:26 -0800972 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700973 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -0800974 PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700975 return false;
976 }
977 }
978 return true;
979}
980
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100981size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) {
982 #define VISIT(VisitorType) \
983 do { \
984 VisitorType visitor(this, out, file_offset, relative_offset); \
985 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
986 return 0; \
987 } \
988 relative_offset = visitor.GetOffset(); \
989 } while (false)
990
991 size_t gc_maps_offset = relative_offset;
992 VISIT(WriteMapMethodVisitor<GcMapDataAccess>);
993 size_gc_map_ = relative_offset - gc_maps_offset;
994
995 size_t mapping_tables_offset = relative_offset;
996 VISIT(WriteMapMethodVisitor<MappingTableDataAccess>);
997 size_mapping_table_ = relative_offset - mapping_tables_offset;
998
999 size_t vmap_tables_offset = relative_offset;
1000 VISIT(WriteMapMethodVisitor<VmapTableDataAccess>);
1001 size_vmap_table_ = relative_offset - vmap_tables_offset;
1002
1003 #undef VISIT
1004
1005 return relative_offset;
1006}
1007
1008size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) {
Ian Rogers3d504072014-03-01 09:16:49 -08001009 off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001010 relative_offset += size_executable_offset_alignment_;
1011 DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset());
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001012 size_t expected_file_offset = file_offset + relative_offset;
1013 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001014 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
Ian Rogers3d504072014-03-01 09:16:49 -08001015 << " Expected: " << expected_file_offset << " File: " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001016 return 0;
1017 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001018 DCHECK_OFFSET();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001019 if (compiler_driver_->IsImage()) {
1020 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001021
Ian Rogers848871b2013-08-05 10:56:33 -07001022 #define DO_TRAMPOLINE(field) \
1023 do { \
1024 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
1025 uint32_t alignment_padding = aligned_offset - relative_offset; \
Ian Rogers3d504072014-03-01 09:16:49 -08001026 out->Seek(alignment_padding, kSeekCurrent); \
Ian Rogers848871b2013-08-05 10:56:33 -07001027 size_trampoline_alignment_ += alignment_padding; \
Ian Rogers3d504072014-03-01 09:16:49 -08001028 if (!out->WriteFully(&(*field)[0], field->size())) { \
1029 PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \
Ian Rogers848871b2013-08-05 10:56:33 -07001030 return false; \
1031 } \
1032 size_ ## field += field->size(); \
1033 relative_offset += alignment_padding + field->size(); \
1034 DCHECK_OFFSET(); \
1035 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001036
Ian Rogers848871b2013-08-05 10:56:33 -07001037 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_);
1038 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_);
1039 DO_TRAMPOLINE(jni_dlsym_lookup_);
Jeff Hao88474b42013-10-23 16:24:40 -07001040 DO_TRAMPOLINE(portable_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001041 DO_TRAMPOLINE(portable_resolution_trampoline_);
1042 DO_TRAMPOLINE(portable_to_interpreter_bridge_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001043 DO_TRAMPOLINE(quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001044 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001045 DO_TRAMPOLINE(quick_resolution_trampoline_);
1046 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
1047 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001048 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001049 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001050}
1051
Ian Rogers3d504072014-03-01 09:16:49 -08001052size_t OatWriter::WriteCodeDexFiles(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001053 const size_t file_offset,
1054 size_t relative_offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001055 #define VISIT(VisitorType) \
1056 do { \
1057 VisitorType visitor(this, out, file_offset, relative_offset); \
1058 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1059 return 0; \
1060 } \
1061 relative_offset = visitor.GetOffset(); \
1062 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001063
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001064 VISIT(WriteCodeMethodVisitor);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001065
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001066 #undef VISIT
Brian Carlstrom265091e2013-01-30 14:08:26 -08001067
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001068 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001069}
1070
Brian Carlstrom265091e2013-01-30 14:08:26 -08001071OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
1072 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -08001073 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001074 dex_file_location_size_ = location.size();
1075 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001076 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -08001077 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001078 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001079}
1080
1081size_t OatWriter::OatDexFile::SizeOf() const {
1082 return sizeof(dex_file_location_size_)
1083 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001084 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -08001085 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001086 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001087}
1088
Ian Rogers3d504072014-03-01 09:16:49 -08001089void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const {
1090 oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
1091 oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
1092 oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
1093 oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
1094 oat_header->UpdateChecksum(&methods_offsets_[0],
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001095 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001096}
1097
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001098bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001099 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001100 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001101 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001102 if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
1103 PLOG(ERROR) << "Failed to write dex file location length 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_size_ += sizeof(dex_file_location_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001107 if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
1108 PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001109 return false;
1110 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001111 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Ian Rogers3d504072014-03-01 09:16:49 -08001112 if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
1113 PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001114 return false;
1115 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001116 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Ian Rogers3d504072014-03-01 09:16:49 -08001117 if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
1118 PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001119 return false;
1120 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001121 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Ian Rogers3d504072014-03-01 09:16:49 -08001122 if (!out->WriteFully(&methods_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001123 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001124 PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001125 return false;
1126 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001127 oat_writer->size_oat_dex_file_methods_offsets_ +=
1128 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001129 return true;
1130}
1131
Brian Carlstromba150c32013-08-27 17:31:03 -07001132OatWriter::OatClass::OatClass(size_t offset,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001133 const std::vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -07001134 uint32_t num_non_null_compiled_methods,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001135 mirror::Class::Status status)
1136 : compiled_methods_(compiled_methods) {
1137 uint32_t num_methods = compiled_methods.size();
Brian Carlstromba150c32013-08-27 17:31:03 -07001138 CHECK_LE(num_non_null_compiled_methods, num_methods);
1139
Brian Carlstrom265091e2013-01-30 14:08:26 -08001140 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -07001141 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
1142
1143 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
1144 // apply when there are 0 methods, we just arbitrarily say that 0
1145 // methods means kOatClassNoneCompiled and that we won't use
1146 // kOatClassAllCompiled unless there is at least one compiled
1147 // method. This means in an interpretter only system, we can assert
1148 // that all classes are kOatClassNoneCompiled.
1149 if (num_non_null_compiled_methods == 0) {
1150 type_ = kOatClassNoneCompiled;
1151 } else if (num_non_null_compiled_methods == num_methods) {
1152 type_ = kOatClassAllCompiled;
1153 } else {
1154 type_ = kOatClassSomeCompiled;
1155 }
1156
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001157 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -07001158 method_offsets_.resize(num_non_null_compiled_methods);
Vladimir Marko8a630572014-04-09 18:45:35 +01001159 method_headers_.resize(num_non_null_compiled_methods);
Brian Carlstromba150c32013-08-27 17:31:03 -07001160
1161 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
1162 if (type_ == kOatClassSomeCompiled) {
1163 method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator());
1164 method_bitmap_size_ = method_bitmap_->GetSizeOf();
1165 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
1166 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
1167 } else {
1168 method_bitmap_ = NULL;
1169 method_bitmap_size_ = 0;
1170 }
1171
1172 for (size_t i = 0; i < num_methods; i++) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001173 CompiledMethod* compiled_method = compiled_methods_[i];
Brian Carlstromba150c32013-08-27 17:31:03 -07001174 if (compiled_method == NULL) {
1175 oat_method_offsets_offsets_from_oat_class_[i] = 0;
1176 } else {
1177 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
1178 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
1179 if (type_ == kOatClassSomeCompiled) {
1180 method_bitmap_->SetBit(i);
1181 }
1182 }
1183 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001184}
1185
Brian Carlstromba150c32013-08-27 17:31:03 -07001186OatWriter::OatClass::~OatClass() {
Mathieu Chartier661974a2014-01-09 11:23:53 -08001187 delete method_bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -07001188}
1189
Brian Carlstrom265091e2013-01-30 14:08:26 -08001190size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
1191 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001192 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
1193 if (method_offset == 0) {
1194 return 0;
1195 }
1196 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001197}
1198
1199size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
1200 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001201 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08001202}
1203
1204size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001205 return sizeof(status_)
1206 + sizeof(type_)
1207 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
1208 + method_bitmap_size_
1209 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001210}
1211
Ian Rogers3d504072014-03-01 09:16:49 -08001212void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const {
1213 oat_header->UpdateChecksum(&status_, sizeof(status_));
1214 oat_header->UpdateChecksum(&type_, sizeof(type_));
Brian Carlstromba150c32013-08-27 17:31:03 -07001215 if (method_bitmap_size_ != 0) {
1216 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001217 oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_));
1218 oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001219 }
Ian Rogers3d504072014-03-01 09:16:49 -08001220 oat_header->UpdateChecksum(&method_offsets_[0],
1221 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001222}
1223
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001224bool OatWriter::OatClass::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001225 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001226 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001227 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001228 if (!out->WriteFully(&status_, sizeof(status_))) {
1229 PLOG(ERROR) << "Failed to write class status to " << out->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001230 return false;
1231 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001232 oat_writer->size_oat_class_status_ += sizeof(status_);
Ian Rogers3d504072014-03-01 09:16:49 -08001233 if (!out->WriteFully(&type_, sizeof(type_))) {
1234 PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001235 return false;
1236 }
1237 oat_writer->size_oat_class_type_ += sizeof(type_);
1238 if (method_bitmap_size_ != 0) {
1239 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001240 if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
1241 PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001242 return false;
1243 }
1244 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001245 if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
1246 PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001247 return false;
1248 }
1249 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1250 }
Ian Rogers3d504072014-03-01 09:16:49 -08001251 if (!out->WriteFully(&method_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001252 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001253 PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001254 return false;
1255 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001256 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001257 return true;
1258}
1259
Brian Carlstrome24fa612011-09-29 00:53:55 -07001260} // namespace art