blob: 8f153b19054a2063fbbf4c007cbc1b7f939ef14f [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
Vladimir Markoc74658b2015-03-31 10:26:41 +010021#include "arch/arm64/instruction_set_features_arm64.h"
Ian Rogerse77493c2014-08-20 15:08:45 -070022#include "base/allocator.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070023#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080024#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080025#include "base/unix_file/fd_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070026#include "class_linker.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070027#include "compiled_class.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000028#include "compiled_method.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "dex_file-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000030#include "dex/verification_results.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000031#include "driver/compiler_driver.h"
32#include "driver/compiler_options.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070033#include "gc/space/space.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010034#include "image_writer.h"
Vladimir Markob163bb72015-03-31 21:49:49 +010035#include "linker/relative_patcher.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070036#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/array.h"
38#include "mirror/class_loader.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010039#include "mirror/dex_cache-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070040#include "mirror/object-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070041#include "os.h"
Brian Carlstromcd60ac72013-01-20 17:09:51 -080042#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070043#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070044#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070045#include "handle_scope-inl.h"
jeffhaoec014232012-09-05 10:42:25 -070046#include "verifier/method_verifier.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070047
48namespace art {
49
Vladimir Marko96c6ab92014-04-08 14:00:50 +010050#define DCHECK_OFFSET() \
51 DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out->Seek(0, kSeekCurrent)) \
52 << "file_offset=" << file_offset << " relative_offset=" << relative_offset
53
54#define DCHECK_OFFSET_() \
55 DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out->Seek(0, kSeekCurrent)) \
56 << "file_offset=" << file_offset << " offset_=" << offset_
57
Brian Carlstrom3320cf42011-10-04 14:58:28 -070058OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070059 uint32_t image_file_location_oat_checksum,
Ian Rogersef7d42f2014-01-06 12:55:46 -080060 uintptr_t image_file_location_oat_begin,
Alex Lighta59dd802014-07-02 16:28:08 -070061 int32_t image_patch_delta,
Ian Rogersca368cb2013-11-15 15:52:08 -080062 const CompilerDriver* compiler,
Vladimir Markof4da6752014-08-01 19:04:18 +010063 ImageWriter* image_writer,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070064 TimingLogger* timings,
65 SafeMap<std::string, std::string>* key_value_store)
Jeff Hao0aba0ba2013-06-03 14:49:28 -070066 : compiler_driver_(compiler),
Vladimir Markof4da6752014-08-01 19:04:18 +010067 image_writer_(image_writer),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070068 dex_files_(&dex_files),
Vladimir Markof4da6752014-08-01 19:04:18 +010069 size_(0u),
Vladimir Marko5c42c292015-02-25 12:02:49 +000070 bss_size_(0u),
Vladimir Markof4da6752014-08-01 19:04:18 +010071 oat_data_offset_(0u),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070072 image_file_location_oat_checksum_(image_file_location_oat_checksum),
73 image_file_location_oat_begin_(image_file_location_oat_begin),
Alex Lighta59dd802014-07-02 16:28:08 -070074 image_patch_delta_(image_patch_delta),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070075 key_value_store_(key_value_store),
Mathieu Chartier2cebb242015-04-21 16:50:40 -070076 oat_header_(nullptr),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070077 size_dex_file_alignment_(0),
78 size_executable_offset_alignment_(0),
79 size_oat_header_(0),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070080 size_oat_header_key_value_store_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070081 size_dex_file_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070082 size_interpreter_to_interpreter_bridge_(0),
83 size_interpreter_to_compiled_code_bridge_(0),
84 size_jni_dlsym_lookup_(0),
Andreas Gampe2da88232014-02-27 12:26:20 -080085 size_quick_generic_jni_trampoline_(0),
Jeff Hao88474b42013-10-23 16:24:40 -070086 size_quick_imt_conflict_trampoline_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070087 size_quick_resolution_trampoline_(0),
Ian Rogers848871b2013-08-05 10:56:33 -070088 size_quick_to_interpreter_bridge_(0),
89 size_trampoline_alignment_(0),
Vladimir Marko96c6ab92014-04-08 14:00:50 +010090 size_method_header_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070091 size_code_(0),
92 size_code_alignment_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +010093 size_relative_call_thunks_(0),
Vladimir Markoc74658b2015-03-31 10:26:41 +010094 size_misc_thunks_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -070095 size_mapping_table_(0),
96 size_vmap_table_(0),
97 size_gc_map_(0),
98 size_oat_dex_file_location_size_(0),
99 size_oat_dex_file_location_data_(0),
100 size_oat_dex_file_location_checksum_(0),
101 size_oat_dex_file_offset_(0),
102 size_oat_dex_file_methods_offsets_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -0700103 size_oat_class_type_(0),
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700104 size_oat_class_status_(0),
Brian Carlstromba150c32013-08-27 17:31:03 -0700105 size_oat_class_method_bitmaps_(0),
Vladimir Markof4da6752014-08-01 19:04:18 +0100106 size_oat_class_method_offsets_(0),
107 method_offset_map_() {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700108 CHECK(key_value_store != nullptr);
109
Vladimir Markob163bb72015-03-31 21:49:49 +0100110 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
111 const InstructionSetFeatures* features = compiler_driver_->GetInstructionSetFeatures();
112 relative_patcher_ = linker::RelativePatcher::Create(instruction_set, features,
113 &method_offset_map_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100114
Ian Rogersca368cb2013-11-15 15:52:08 -0800115 size_t offset;
116 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700117 TimingLogger::ScopedTiming split("InitOatHeader", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800118 offset = InitOatHeader();
119 }
120 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700121 TimingLogger::ScopedTiming split("InitOatDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800122 offset = InitOatDexFiles(offset);
123 }
124 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700125 TimingLogger::ScopedTiming split("InitDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800126 offset = InitDexFiles(offset);
127 }
128 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700129 TimingLogger::ScopedTiming split("InitOatClasses", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800130 offset = InitOatClasses(offset);
131 }
132 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700133 TimingLogger::ScopedTiming split("InitOatMaps", timings);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100134 offset = InitOatMaps(offset);
135 }
136 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700137 TimingLogger::ScopedTiming split("InitOatCode", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800138 offset = InitOatCode(offset);
139 }
140 {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700141 TimingLogger::ScopedTiming split("InitOatCodeDexFiles", timings);
Ian Rogersca368cb2013-11-15 15:52:08 -0800142 offset = InitOatCodeDexFiles(offset);
143 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700144 size_ = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700145
146 CHECK_EQ(dex_files_->size(), oat_dex_files_.size());
Vladimir Markof4da6752014-08-01 19:04:18 +0100147 CHECK_EQ(compiler->IsImage(), image_writer_ != nullptr);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700148 CHECK_EQ(compiler->IsImage(),
149 key_value_store_->find(OatHeader::kImageLocationKey) == key_value_store_->end());
Alex Lighta59dd802014-07-02 16:28:08 -0700150 CHECK_ALIGNED(image_patch_delta_, kPageSize);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700151}
152
Ian Rogers0571d352011-11-03 19:51:38 -0700153OatWriter::~OatWriter() {
154 delete oat_header_;
155 STLDeleteElements(&oat_dex_files_);
Brian Carlstrom389efb02012-01-11 12:06:26 -0800156 STLDeleteElements(&oat_classes_);
Ian Rogers0571d352011-11-03 19:51:38 -0700157}
158
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100159struct OatWriter::GcMapDataAccess {
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800160 static const SwapVector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100161 return compiled_method->GetGcMap();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100162 }
163
164 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800165 uint32_t offset = oat_class->method_headers_[method_offsets_index].gc_map_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 {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800172 oat_class->method_headers_[method_offsets_index].gc_map_offset_ =
173 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100174 }
175
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800176 static const char* Name() {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100177 return "GC map";
178 }
179};
180
181struct OatWriter::MappingTableDataAccess {
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800182 static const SwapVector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000183 return compiled_method->GetMappingTable();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100184 }
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].mapping_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].mapping_table_offset_ =
195 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100196 }
197
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800198 static const char* Name() {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100199 return "mapping table";
200 }
201};
202
203struct OatWriter::VmapTableDataAccess {
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800204 static const SwapVector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800205 return compiled_method->GetVmapTable();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100206 }
207
208 static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100209 uint32_t offset = oat_class->method_headers_[method_offsets_index].vmap_table_offset_;
210 return offset == 0u ? 0u :
211 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100212 }
213
214 static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset)
215 ALWAYS_INLINE {
Vladimir Marko8a630572014-04-09 18:45:35 +0100216 oat_class->method_headers_[method_offsets_index].vmap_table_offset_ =
217 (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100218 }
219
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800220 static const char* Name() {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100221 return "vmap table";
222 }
223};
224
225class OatWriter::DexMethodVisitor {
226 public:
227 DexMethodVisitor(OatWriter* writer, size_t offset)
228 : writer_(writer),
229 offset_(offset),
230 dex_file_(nullptr),
231 class_def_index_(DexFile::kDexNoIndex) {
232 }
233
234 virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) {
235 DCHECK(dex_file_ == nullptr);
236 DCHECK_EQ(class_def_index_, DexFile::kDexNoIndex);
237 dex_file_ = dex_file;
238 class_def_index_ = class_def_index;
239 return true;
240 }
241
242 virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0;
243
244 virtual bool EndClass() {
245 if (kIsDebugBuild) {
246 dex_file_ = nullptr;
247 class_def_index_ = DexFile::kDexNoIndex;
248 }
249 return true;
250 }
251
252 size_t GetOffset() const {
253 return offset_;
254 }
255
256 protected:
257 virtual ~DexMethodVisitor() { }
258
259 OatWriter* const writer_;
260
261 // The offset is usually advanced for each visited method by the derived class.
262 size_t offset_;
263
264 // The dex file and class def index are set in StartClass().
265 const DexFile* dex_file_;
266 size_t class_def_index_;
267};
268
269class OatWriter::OatDexMethodVisitor : public DexMethodVisitor {
270 public:
271 OatDexMethodVisitor(OatWriter* writer, size_t offset)
272 : DexMethodVisitor(writer, offset),
273 oat_class_index_(0u),
274 method_offsets_index_(0u) {
275 }
276
277 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
278 DexMethodVisitor::StartClass(dex_file, class_def_index);
279 DCHECK_LT(oat_class_index_, writer_->oat_classes_.size());
280 method_offsets_index_ = 0u;
281 return true;
282 }
283
284 bool EndClass() {
285 ++oat_class_index_;
286 return DexMethodVisitor::EndClass();
287 }
288
289 protected:
290 size_t oat_class_index_;
291 size_t method_offsets_index_;
292};
293
294class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor {
295 public:
296 InitOatClassesMethodVisitor(OatWriter* writer, size_t offset)
297 : DexMethodVisitor(writer, offset),
298 compiled_methods_(),
299 num_non_null_compiled_methods_(0u) {
300 compiled_methods_.reserve(256u);
301 }
302
303 bool StartClass(const DexFile* dex_file, size_t class_def_index) {
304 DexMethodVisitor::StartClass(dex_file, class_def_index);
305 compiled_methods_.clear();
306 num_non_null_compiled_methods_ = 0u;
307 return true;
308 }
309
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700310 bool VisitMethod(size_t class_def_method_index ATTRIBUTE_UNUSED, const ClassDataItemIterator& it) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100311 // Fill in the compiled_methods_ array for methods that have a
312 // CompiledMethod. We track the number of non-null entries in
313 // num_non_null_compiled_methods_ since we only want to allocate
314 // OatMethodOffsets for the compiled methods.
315 uint32_t method_idx = it.GetMemberIndex();
316 CompiledMethod* compiled_method =
317 writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx));
318 compiled_methods_.push_back(compiled_method);
319 if (compiled_method != nullptr) {
320 ++num_non_null_compiled_methods_;
321 }
322 return true;
323 }
324
325 bool EndClass() {
326 ClassReference class_ref(dex_file_, class_def_index_);
327 CompiledClass* compiled_class = writer_->compiler_driver_->GetCompiledClass(class_ref);
328 mirror::Class::Status status;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700329 if (compiled_class != nullptr) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100330 status = compiled_class->GetStatus();
331 } else if (writer_->compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) {
332 status = mirror::Class::kStatusError;
333 } else {
334 status = mirror::Class::kStatusNotReady;
335 }
336
337 OatClass* oat_class = new OatClass(offset_, compiled_methods_,
338 num_non_null_compiled_methods_, status);
339 writer_->oat_classes_.push_back(oat_class);
Vladimir Markof4da6752014-08-01 19:04:18 +0100340 oat_class->UpdateChecksum(writer_->oat_header_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100341 offset_ += oat_class->SizeOf();
342 return DexMethodVisitor::EndClass();
343 }
344
345 private:
346 std::vector<CompiledMethod*> compiled_methods_;
347 size_t num_non_null_compiled_methods_;
348};
349
350class OatWriter::InitCodeMethodVisitor : public OatDexMethodVisitor {
351 public:
352 InitCodeMethodVisitor(OatWriter* writer, size_t offset)
David Srbecky009e2a62015-04-15 02:46:30 +0100353 : OatDexMethodVisitor(writer, offset),
David Srbecky009e2a62015-04-15 02:46:30 +0100354 debuggable_(writer->GetCompilerDriver()->GetCompilerOptions().GetDebuggable()) {
David Srbeckyf8980872015-05-22 17:04:47 +0100355 writer_->absolute_patch_locations_.reserve(
Vladimir Markof4da6752014-08-01 19:04:18 +0100356 writer_->compiler_driver_->GetNonRelativeLinkerPatchCount());
357 }
358
359 bool EndClass() {
360 OatDexMethodVisitor::EndClass();
361 if (oat_class_index_ == writer_->oat_classes_.size()) {
Vladimir Marko71b0ddf2015-04-02 19:45:06 +0100362 offset_ = writer_->relative_patcher_->ReserveSpaceEnd(offset_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100363 }
364 return true;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100365 }
366
367 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
368 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
369 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
370 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
371
372 if (compiled_method != nullptr) {
373 // Derived from CompiledMethod.
374 uint32_t quick_code_offset = 0;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100375
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800376 const SwapVector<uint8_t>* quick_code = compiled_method->GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -0800377 CHECK(quick_code != nullptr);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800378 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
379 CHECK_NE(code_size, 0U);
380 uint32_t thumb_offset = compiled_method->CodeDelta();
Elliott Hughes956af0f2014-12-11 14:34:28 -0800381
David Srbecky009e2a62015-04-15 02:46:30 +0100382 // Deduplicate code arrays if we are not producing debuggable code.
Vladimir Markoc74658b2015-03-31 10:26:41 +0100383 bool deduped = false;
David Srbecky009e2a62015-04-15 02:46:30 +0100384 if (debuggable_) {
385 quick_code_offset = NewQuickCodeOffset(compiled_method, it, thumb_offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100386 } else {
David Srbecky009e2a62015-04-15 02:46:30 +0100387 auto lb = dedupe_map_.lower_bound(compiled_method);
388 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(compiled_method, lb->first)) {
389 quick_code_offset = lb->second;
390 deduped = true;
391 } else {
392 quick_code_offset = NewQuickCodeOffset(compiled_method, it, thumb_offset);
393 dedupe_map_.PutBefore(lb, compiled_method, quick_code_offset);
394 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800395 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100396
Elliott Hughes956af0f2014-12-11 14:34:28 -0800397 MethodReference method_ref(dex_file_, it.GetMemberIndex());
Vladimir Markob163bb72015-03-31 21:49:49 +0100398 auto method_lb = writer_->method_offset_map_.map.lower_bound(method_ref);
399 if (method_lb != writer_->method_offset_map_.map.end() &&
400 !writer_->method_offset_map_.map.key_comp()(method_ref, method_lb->first)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800401 // TODO: Should this be a hard failure?
402 LOG(WARNING) << "Multiple definitions of "
403 << PrettyMethod(method_ref.dex_method_index, *method_ref.dex_file)
404 << ((method_lb->second != quick_code_offset) ? "; OFFSET MISMATCH" : "");
405 } else {
Vladimir Markob163bb72015-03-31 21:49:49 +0100406 writer_->method_offset_map_.map.PutBefore(method_lb, method_ref, quick_code_offset);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800407 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100408
Elliott Hughes956af0f2014-12-11 14:34:28 -0800409 // Update quick method header.
410 DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size());
411 OatQuickMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_];
412 uint32_t mapping_table_offset = method_header->mapping_table_offset_;
413 uint32_t vmap_table_offset = method_header->vmap_table_offset_;
414 uint32_t gc_map_offset = method_header->gc_map_offset_;
415 // The code offset was 0 when the mapping/vmap table offset was set, so it's set
416 // to 0-offset and we need to adjust it by code_offset.
417 uint32_t code_offset = quick_code_offset - thumb_offset;
418 if (mapping_table_offset != 0u) {
419 mapping_table_offset += code_offset;
420 DCHECK_LT(mapping_table_offset, code_offset);
421 }
422 if (vmap_table_offset != 0u) {
423 vmap_table_offset += code_offset;
424 DCHECK_LT(vmap_table_offset, code_offset);
425 }
426 if (gc_map_offset != 0u) {
427 gc_map_offset += code_offset;
428 DCHECK_LT(gc_map_offset, code_offset);
429 }
430 uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
431 uint32_t core_spill_mask = compiled_method->GetCoreSpillMask();
432 uint32_t fp_spill_mask = compiled_method->GetFpSpillMask();
433 *method_header = OatQuickMethodHeader(mapping_table_offset, vmap_table_offset,
434 gc_map_offset, frame_size_in_bytes, core_spill_mask,
435 fp_spill_mask, code_size);
Vladimir Marko7624d252014-05-02 14:40:15 +0100436
Elliott Hughes956af0f2014-12-11 14:34:28 -0800437 if (!deduped) {
438 // Update offsets. (Checksum is updated when writing.)
439 offset_ += sizeof(*method_header); // Method header is prepended before code.
440 offset_ += code_size;
441 // Record absolute patch locations.
442 if (!compiled_method->GetPatches().empty()) {
443 uintptr_t base_loc = offset_ - code_size - writer_->oat_header_->GetExecutableOffset();
444 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
Vladimir Marko20f85592015-03-19 10:07:02 +0000445 if (!patch.IsPcRelative()) {
David Srbeckyf8980872015-05-22 17:04:47 +0100446 writer_->absolute_patch_locations_.push_back(base_loc + patch.LiteralOffset());
Vladimir Markof4da6752014-08-01 19:04:18 +0100447 }
448 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100449 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800450 }
Alex Light78382fa2014-06-06 15:45:32 -0700451
David Srbecky8363c772015-05-28 16:12:43 +0100452 if (writer_->compiler_driver_->GetCompilerOptions().GetGenerateDebugInfo()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800453 // Record debug information for this function if we are doing that.
Elliott Hughes956af0f2014-12-11 14:34:28 -0800454 const uint32_t quick_code_start = quick_code_offset -
David Srbecky6f715892015-03-30 14:21:42 +0100455 writer_->oat_header_->GetExecutableOffset() - thumb_offset;
David Srbecky0df9e1f2015-04-07 19:02:58 +0100456 writer_->method_info_.push_back(DebugInfo {
457 dex_file_,
458 class_def_index_,
459 it.GetMemberIndex(),
460 it.GetMethodAccessFlags(),
461 it.GetMethodCodeItem(),
462 deduped,
463 quick_code_start,
464 quick_code_start + code_size,
465 compiled_method});
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100466 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100467
468 if (kIsDebugBuild) {
469 // We expect GC maps except when the class hasn't been verified or the method is native.
470 const CompilerDriver* compiler_driver = writer_->compiler_driver_;
471 ClassReference class_ref(dex_file_, class_def_index_);
472 CompiledClass* compiled_class = compiler_driver->GetCompiledClass(class_ref);
473 mirror::Class::Status status;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700474 if (compiled_class != nullptr) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100475 status = compiled_class->GetStatus();
476 } else if (compiler_driver->GetVerificationResults()->IsClassRejected(class_ref)) {
477 status = mirror::Class::kStatusError;
478 } else {
479 status = mirror::Class::kStatusNotReady;
480 }
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800481 const SwapVector<uint8_t>* gc_map = compiled_method->GetGcMap();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100482 if (gc_map != nullptr) {
483 size_t gc_map_size = gc_map->size() * sizeof(gc_map[0]);
Andreas Gampe51829322014-08-25 15:05:04 -0700484 bool is_native = it.MemberIsNative();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100485 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
486 << gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
487 << (status < mirror::Class::kStatusVerified) << " " << status << " "
488 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
489 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100490 }
491
492 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
493 OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_];
494 offsets->code_offset_ = quick_code_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100495 ++method_offsets_index_;
496 }
497
498 return true;
499 }
500
501 private:
Vladimir Marko20f85592015-03-19 10:07:02 +0000502 struct CodeOffsetsKeyComparator {
503 bool operator()(const CompiledMethod* lhs, const CompiledMethod* rhs) const {
504 if (lhs->GetQuickCode() != rhs->GetQuickCode()) {
505 return lhs->GetQuickCode() < rhs->GetQuickCode();
506 }
507 // If the code is the same, all other fields are likely to be the same as well.
508 if (UNLIKELY(lhs->GetMappingTable() != rhs->GetMappingTable())) {
509 return lhs->GetMappingTable() < rhs->GetMappingTable();
510 }
511 if (UNLIKELY(lhs->GetVmapTable() != rhs->GetVmapTable())) {
512 return lhs->GetVmapTable() < rhs->GetVmapTable();
513 }
514 if (UNLIKELY(lhs->GetGcMap() != rhs->GetGcMap())) {
515 return lhs->GetGcMap() < rhs->GetGcMap();
516 }
517 const auto& lhs_patches = lhs->GetPatches();
518 const auto& rhs_patches = rhs->GetPatches();
519 if (UNLIKELY(lhs_patches.size() != rhs_patches.size())) {
520 return lhs_patches.size() < rhs_patches.size();
521 }
522 auto rit = rhs_patches.begin();
523 for (const LinkerPatch& lpatch : lhs_patches) {
524 if (UNLIKELY(!(lpatch == *rit))) {
525 return lpatch < *rit;
526 }
527 ++rit;
528 }
529 return false;
530 }
531 };
532
David Srbecky009e2a62015-04-15 02:46:30 +0100533 uint32_t NewQuickCodeOffset(CompiledMethod* compiled_method,
534 const ClassDataItemIterator& it,
535 uint32_t thumb_offset) {
536 offset_ = writer_->relative_patcher_->ReserveSpace(
537 offset_, compiled_method, MethodReference(dex_file_, it.GetMemberIndex()));
538 offset_ = compiled_method->AlignCode(offset_);
539 DCHECK_ALIGNED_PARAM(offset_,
540 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
541 return offset_ + sizeof(OatQuickMethodHeader) + thumb_offset;
542 }
543
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100544 // Deduplication is already done on a pointer basis by the compiler driver,
545 // so we can simply compare the pointers to find out if things are duplicated.
Vladimir Marko8a630572014-04-09 18:45:35 +0100546 SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_;
David Srbecky2f6cdb02015-04-11 00:17:53 +0100547
David Srbecky009e2a62015-04-15 02:46:30 +0100548 // Cache of compiler's --debuggable option.
549 const bool debuggable_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100550};
551
552template <typename DataAccess>
553class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor {
554 public:
555 InitMapMethodVisitor(OatWriter* writer, size_t offset)
556 : OatDexMethodVisitor(writer, offset) {
557 }
558
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700559 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it ATTRIBUTE_UNUSED)
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100560 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
561 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
562 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
563
564 if (compiled_method != nullptr) {
565 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
566 DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u);
567
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800568 const SwapVector<uint8_t>* map = DataAccess::GetData(compiled_method);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100569 uint32_t map_size = map == nullptr ? 0 : map->size() * sizeof((*map)[0]);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100570 if (map_size != 0u) {
Vladimir Markobd72fc12014-07-09 16:06:40 +0100571 auto lb = dedupe_map_.lower_bound(map);
572 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(map, lb->first)) {
573 DataAccess::SetOffset(oat_class, method_offsets_index_, lb->second);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100574 } else {
575 DataAccess::SetOffset(oat_class, method_offsets_index_, offset_);
Vladimir Markobd72fc12014-07-09 16:06:40 +0100576 dedupe_map_.PutBefore(lb, map, offset_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100577 offset_ += map_size;
578 writer_->oat_header_->UpdateChecksum(&(*map)[0], map_size);
579 }
580 }
581 ++method_offsets_index_;
582 }
583
584 return true;
585 }
586
587 private:
588 // Deduplication is already done on a pointer basis by the compiler driver,
589 // so we can simply compare the pointers to find out if things are duplicated.
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800590 SafeMap<const SwapVector<uint8_t>*, uint32_t> dedupe_map_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100591};
592
593class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor {
594 public:
595 InitImageMethodVisitor(OatWriter* writer, size_t offset)
Jeff Haoc7d11882015-02-03 15:08:39 -0800596 : OatDexMethodVisitor(writer, offset),
597 pointer_size_(GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet())) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100598 }
599
600 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
601 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
602 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
603 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
604
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800605 OatMethodOffsets offsets(0u);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100606 if (compiled_method != nullptr) {
607 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
608 offsets = oat_class->method_offsets_[method_offsets_index_];
609 ++method_offsets_index_;
610 }
611
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100612 ClassLinker* linker = Runtime::Current()->GetClassLinker();
613 InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_));
614 // Unchecked as we hold mutator_lock_ on entry.
615 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800616 StackHandleScope<1> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700617 Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache(*dex_file_)));
Vladimir Marko7624d252014-05-02 14:40:15 +0100618 mirror::ArtMethod* method = linker->ResolveMethod(*dex_file_, it.GetMemberIndex(), dex_cache,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700619 NullHandle<mirror::ClassLoader>(),
620 NullHandle<mirror::ArtMethod>(),
621 invoke_type);
Andreas Gamped9efea62014-07-21 22:56:08 -0700622 if (method == nullptr) {
623 LOG(ERROR) << "Unexpected failure to resolve a method: "
624 << PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
625 soa.Self()->AssertPendingException();
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000626 mirror::Throwable* exc = soa.Self()->GetException();
Andreas Gamped9efea62014-07-21 22:56:08 -0700627 std::string dump = exc->Dump();
628 LOG(FATAL) << dump;
629 }
Jeff Haoc7d11882015-02-03 15:08:39 -0800630 method->SetEntryPointFromQuickCompiledCodePtrSize(reinterpret_cast<void*>(offsets.code_offset_),
631 pointer_size_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100632
633 return true;
634 }
Jeff Haoc7d11882015-02-03 15:08:39 -0800635
636 protected:
637 const size_t pointer_size_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100638};
639
640class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor {
641 public:
642 WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
Vladimir Markof4da6752014-08-01 19:04:18 +0100643 size_t relative_offset) SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100644 : OatDexMethodVisitor(writer, relative_offset),
645 out_(out),
Vladimir Markof4da6752014-08-01 19:04:18 +0100646 file_offset_(file_offset),
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100647 soa_(Thread::Current()),
648 no_thread_suspension_(soa_.Self(), "OatWriter patching"),
Vladimir Markof4da6752014-08-01 19:04:18 +0100649 class_linker_(Runtime::Current()->GetClassLinker()),
650 dex_cache_(nullptr) {
651 if (writer_->image_writer_ != nullptr) {
652 // If we're creating the image, the address space must be ready so that we can apply patches.
653 CHECK(writer_->image_writer_->IsImageAddressSpaceReady());
654 patched_code_.reserve(16 * KB);
655 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100656 }
657
Vladimir Markof4da6752014-08-01 19:04:18 +0100658 ~WriteCodeMethodVisitor() UNLOCK_FUNCTION(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100659 }
660
661 bool StartClass(const DexFile* dex_file, size_t class_def_index)
662 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
663 OatDexMethodVisitor::StartClass(dex_file, class_def_index);
664 if (dex_cache_ == nullptr || dex_cache_->GetDexFile() != dex_file) {
665 dex_cache_ = class_linker_->FindDexCache(*dex_file);
666 }
667 return true;
668 }
669
670 bool EndClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
671 bool result = OatDexMethodVisitor::EndClass();
672 if (oat_class_index_ == writer_->oat_classes_.size()) {
673 DCHECK(result); // OatDexMethodVisitor::EndClass() never fails.
Vladimir Marko20f85592015-03-19 10:07:02 +0000674 offset_ = writer_->relative_patcher_->WriteThunks(out_, offset_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100675 if (UNLIKELY(offset_ == 0u)) {
676 PLOG(ERROR) << "Failed to write final relative call thunks";
677 result = false;
678 }
679 }
680 return result;
681 }
682
683 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
684 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100685 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
686 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
687
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700688 if (compiled_method != nullptr) { // ie. not an abstract method
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100689 size_t file_offset = file_offset_;
690 OutputStream* out = out_;
691
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800692 const SwapVector<uint8_t>* quick_code = compiled_method->GetQuickCode();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100693 if (quick_code != nullptr) {
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800694 // Need a wrapper if we create a copy for patching.
695 ArrayRef<const uint8_t> wrapped(*quick_code);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100696 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
697 CHECK_NE(code_size, 0U);
698
699 // Deduplicate code arrays.
700 const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_];
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100701 if (method_offsets.code_offset_ >= offset_) {
Vladimir Markoc74658b2015-03-31 10:26:41 +0100702 offset_ = writer_->relative_patcher_->WriteThunks(out, offset_);
703 if (offset_ == 0u) {
704 ReportWriteFailure("relative call thunk", it);
705 return false;
706 }
707 uint32_t aligned_offset = compiled_method->AlignCode(offset_);
708 uint32_t aligned_code_delta = aligned_offset - offset_;
709 if (aligned_code_delta != 0) {
710 if (!writer_->WriteCodeAlignment(out, aligned_code_delta)) {
711 ReportWriteFailure("code alignment padding", it);
712 return false;
713 }
714 offset_ += aligned_code_delta;
715 DCHECK_OFFSET_();
716 }
717 DCHECK_ALIGNED_PARAM(offset_,
718 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
719 DCHECK_EQ(method_offsets.code_offset_,
720 offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta())
721 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100722 const OatQuickMethodHeader& method_header =
723 oat_class->method_headers_[method_offsets_index_];
724 writer_->oat_header_->UpdateChecksum(&method_header, sizeof(method_header));
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100725 if (!out->WriteFully(&method_header, sizeof(method_header))) {
726 ReportWriteFailure("method header", it);
727 return false;
728 }
729 writer_->size_method_header_ += sizeof(method_header);
730 offset_ += sizeof(method_header);
731 DCHECK_OFFSET_();
Vladimir Markof4da6752014-08-01 19:04:18 +0100732
733 if (!compiled_method->GetPatches().empty()) {
Vladimir Marko20f85592015-03-19 10:07:02 +0000734 patched_code_.assign(quick_code->begin(), quick_code->end());
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800735 wrapped = ArrayRef<const uint8_t>(patched_code_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100736 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
737 if (patch.Type() == kLinkerPatchCallRelative) {
738 // NOTE: Relative calls across oat files are not supported.
739 uint32_t target_offset = GetTargetOffset(patch);
740 uint32_t literal_offset = patch.LiteralOffset();
Vladimir Marko20f85592015-03-19 10:07:02 +0000741 writer_->relative_patcher_->PatchCall(&patched_code_, literal_offset,
Vladimir Markof4da6752014-08-01 19:04:18 +0100742 offset_ + literal_offset, target_offset);
Vladimir Marko20f85592015-03-19 10:07:02 +0000743 } else if (patch.Type() == kLinkerPatchDexCacheArray) {
744 uint32_t target_offset = GetDexCacheOffset(patch);
745 uint32_t literal_offset = patch.LiteralOffset();
746 writer_->relative_patcher_->PatchDexCacheReference(&patched_code_, patch,
747 offset_ + literal_offset,
748 target_offset);
Vladimir Markof4da6752014-08-01 19:04:18 +0100749 } else if (patch.Type() == kLinkerPatchCall) {
750 uint32_t target_offset = GetTargetOffset(patch);
751 PatchCodeAddress(&patched_code_, patch.LiteralOffset(), target_offset);
752 } else if (patch.Type() == kLinkerPatchMethod) {
753 mirror::ArtMethod* method = GetTargetMethod(patch);
754 PatchObjectAddress(&patched_code_, patch.LiteralOffset(), method);
755 } else if (patch.Type() == kLinkerPatchType) {
756 mirror::Class* type = GetTargetType(patch);
757 PatchObjectAddress(&patched_code_, patch.LiteralOffset(), type);
758 }
759 }
760 }
761
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800762 writer_->oat_header_->UpdateChecksum(wrapped.data(), code_size);
763 if (!out->WriteFully(wrapped.data(), code_size)) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100764 ReportWriteFailure("method code", it);
765 return false;
766 }
767 writer_->size_code_ += code_size;
768 offset_ += code_size;
769 }
770 DCHECK_OFFSET_();
771 }
772 ++method_offsets_index_;
773 }
774
775 return true;
776 }
777
778 private:
779 OutputStream* const out_;
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100780 const size_t file_offset_;
781 const ScopedObjectAccess soa_;
782 const ScopedAssertNoThreadSuspension no_thread_suspension_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100783 ClassLinker* const class_linker_;
784 mirror::DexCache* dex_cache_;
785 std::vector<uint8_t> patched_code_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100786
787 void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) {
788 PLOG(ERROR) << "Failed to write " << what << " for "
789 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
790 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100791
792 mirror::ArtMethod* GetTargetMethod(const LinkerPatch& patch)
793 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
794 MethodReference ref = patch.TargetMethod();
795 mirror::DexCache* dex_cache =
796 (dex_file_ == ref.dex_file) ? dex_cache_ : class_linker_->FindDexCache(*ref.dex_file);
797 mirror::ArtMethod* method = dex_cache->GetResolvedMethod(ref.dex_method_index);
798 CHECK(method != nullptr);
799 return method;
800 }
801
802 uint32_t GetTargetOffset(const LinkerPatch& patch) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Vladimir Markob163bb72015-03-31 21:49:49 +0100803 auto target_it = writer_->method_offset_map_.map.find(patch.TargetMethod());
Vladimir Markof4da6752014-08-01 19:04:18 +0100804 uint32_t target_offset =
Vladimir Markob163bb72015-03-31 21:49:49 +0100805 (target_it != writer_->method_offset_map_.map.end()) ? target_it->second : 0u;
Vladimir Markof4da6752014-08-01 19:04:18 +0100806 // If there's no compiled code, point to the correct trampoline.
807 if (UNLIKELY(target_offset == 0)) {
808 mirror::ArtMethod* target = GetTargetMethod(patch);
809 DCHECK(target != nullptr);
Jeff Haoa0acc2d2015-01-27 11:22:04 -0800810 size_t size = GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet());
811 const void* oat_code_offset = target->GetEntryPointFromQuickCompiledCodePtrSize(size);
812 if (oat_code_offset != 0) {
813 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(oat_code_offset));
814 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(oat_code_offset));
815 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(oat_code_offset));
816 target_offset = PointerToLowMemUInt32(oat_code_offset);
817 } else {
818 target_offset = target->IsNative()
819 ? writer_->oat_header_->GetQuickGenericJniTrampolineOffset()
820 : writer_->oat_header_->GetQuickToInterpreterBridgeOffset();
821 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100822 }
823 return target_offset;
824 }
825
826 mirror::Class* GetTargetType(const LinkerPatch& patch)
827 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
828 mirror::DexCache* dex_cache = (dex_file_ == patch.TargetTypeDexFile())
829 ? dex_cache_ : class_linker_->FindDexCache(*patch.TargetTypeDexFile());
830 mirror::Class* type = dex_cache->GetResolvedType(patch.TargetTypeIndex());
831 CHECK(type != nullptr);
832 return type;
833 }
834
Vladimir Marko20f85592015-03-19 10:07:02 +0000835 uint32_t GetDexCacheOffset(const LinkerPatch& patch) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
836 if (writer_->image_writer_ != nullptr) {
837 auto* element = writer_->image_writer_->GetDexCacheArrayElementImageAddress(
838 patch.TargetDexCacheDexFile(), patch.TargetDexCacheElementOffset());
839 const uint8_t* oat_data = writer_->image_writer_->GetOatFileBegin() + file_offset_;
840 return reinterpret_cast<const uint8_t*>(element) - oat_data;
841 } else {
842 LOG(FATAL) << "Unimplemented.";
843 UNREACHABLE();
844 }
845 }
846
Vladimir Markof4da6752014-08-01 19:04:18 +0100847 void PatchObjectAddress(std::vector<uint8_t>* code, uint32_t offset, mirror::Object* object)
848 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
849 // NOTE: Direct method pointers across oat files don't use linker patches. However, direct
850 // type pointers across oat files do. (TODO: Investigate why.)
851 if (writer_->image_writer_ != nullptr) {
852 object = writer_->image_writer_->GetImageAddress(object);
853 }
854 uint32_t address = PointerToLowMemUInt32(object);
855 DCHECK_LE(offset + 4, code->size());
856 uint8_t* data = &(*code)[offset];
857 data[0] = address & 0xffu;
858 data[1] = (address >> 8) & 0xffu;
859 data[2] = (address >> 16) & 0xffu;
860 data[3] = (address >> 24) & 0xffu;
861 }
862
863 void PatchCodeAddress(std::vector<uint8_t>* code, uint32_t offset, uint32_t target_offset)
864 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Jeff Haoa0acc2d2015-01-27 11:22:04 -0800865 uint32_t address = writer_->image_writer_ == nullptr ? target_offset :
866 PointerToLowMemUInt32(writer_->image_writer_->GetOatFileBegin() +
867 writer_->oat_data_offset_ + target_offset);
Vladimir Markof4da6752014-08-01 19:04:18 +0100868 DCHECK_LE(offset + 4, code->size());
869 uint8_t* data = &(*code)[offset];
870 data[0] = address & 0xffu;
871 data[1] = (address >> 8) & 0xffu;
872 data[2] = (address >> 16) & 0xffu;
873 data[3] = (address >> 24) & 0xffu;
874 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100875};
876
877template <typename DataAccess>
878class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor {
879 public:
880 WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800881 size_t relative_offset)
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100882 : OatDexMethodVisitor(writer, relative_offset),
883 out_(out),
884 file_offset_(file_offset) {
885 }
886
887 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
888 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
889 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
890
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700891 if (compiled_method != nullptr) { // ie. not an abstract method
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100892 size_t file_offset = file_offset_;
893 OutputStream* out = out_;
894
895 uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_);
896 ++method_offsets_index_;
897
898 // Write deduplicated map.
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800899 const SwapVector<uint8_t>* map = DataAccess::GetData(compiled_method);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100900 size_t map_size = map == nullptr ? 0 : map->size() * sizeof((*map)[0]);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100901 DCHECK((map_size == 0u && map_offset == 0u) ||
902 (map_size != 0u && map_offset != 0u && map_offset <= offset_))
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800903 << map_size << " " << map_offset << " " << offset_ << " "
904 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " for " << DataAccess::Name();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100905 if (map_size != 0u && map_offset == offset_) {
906 if (UNLIKELY(!out->WriteFully(&(*map)[0], map_size))) {
907 ReportWriteFailure(it);
908 return false;
909 }
910 offset_ += map_size;
911 }
912 DCHECK_OFFSET_();
913 }
914
915 return true;
916 }
917
918 private:
919 OutputStream* const out_;
920 size_t const file_offset_;
921
922 void ReportWriteFailure(const ClassDataItemIterator& it) {
923 PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for "
924 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
925 }
926};
927
928// Visit all methods from all classes in all dex files with the specified visitor.
929bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) {
930 for (const DexFile* dex_file : *dex_files_) {
931 const size_t class_def_count = dex_file->NumClassDefs();
932 for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) {
933 if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) {
934 return false;
935 }
936 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700937 const uint8_t* class_data = dex_file->GetClassData(class_def);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700938 if (class_data != nullptr) { // ie not an empty class, such as a marker interface
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100939 ClassDataItemIterator it(*dex_file, class_data);
940 while (it.HasNextStaticField()) {
941 it.Next();
942 }
943 while (it.HasNextInstanceField()) {
944 it.Next();
945 }
946 size_t class_def_method_index = 0u;
947 while (it.HasNextDirectMethod()) {
948 if (!visitor->VisitMethod(class_def_method_index, it)) {
949 return false;
950 }
951 ++class_def_method_index;
952 it.Next();
953 }
954 while (it.HasNextVirtualMethod()) {
955 if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) {
956 return false;
957 }
958 ++class_def_method_index;
959 it.Next();
960 }
961 }
962 if (UNLIKELY(!visitor->EndClass())) {
963 return false;
964 }
965 }
966 }
967 return true;
968}
969
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700970size_t OatWriter::InitOatHeader() {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700971 oat_header_ = OatHeader::Create(compiler_driver_->GetInstructionSet(),
972 compiler_driver_->GetInstructionSetFeatures(),
973 dex_files_,
974 image_file_location_oat_checksum_,
975 image_file_location_oat_begin_,
976 key_value_store_);
977
978 return oat_header_->GetHeaderSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700979}
980
981size_t OatWriter::InitOatDexFiles(size_t offset) {
982 // create the OatDexFiles
983 for (size_t i = 0; i != dex_files_->size(); ++i) {
984 const DexFile* dex_file = (*dex_files_)[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700985 CHECK(dex_file != nullptr);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800986 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700987 oat_dex_files_.push_back(oat_dex_file);
988 offset += oat_dex_file->SizeOf();
989 }
990 return offset;
991}
992
Brian Carlstrom89521892011-12-07 22:05:07 -0800993size_t OatWriter::InitDexFiles(size_t offset) {
994 // calculate the offsets within OatDexFiles to the DexFiles
995 for (size_t i = 0; i != dex_files_->size(); ++i) {
996 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700997 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -0800998 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700999 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -08001000
1001 // set offset in OatDexFile to DexFile
1002 oat_dex_files_[i]->dex_file_offset_ = offset;
1003
1004 const DexFile* dex_file = (*dex_files_)[i];
1005 offset += dex_file->GetHeader().file_size_;
1006 }
1007 return offset;
1008}
1009
Brian Carlstrom389efb02012-01-11 12:06:26 -08001010size_t OatWriter::InitOatClasses(size_t offset) {
Brian Carlstrom389efb02012-01-11 12:06:26 -08001011 // calculate the offsets within OatDexFiles to OatClasses
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001012 InitOatClassesMethodVisitor visitor(this, offset);
1013 bool success = VisitDexMethods(&visitor);
1014 CHECK(success);
1015 offset = visitor.GetOffset();
Brian Carlstromba150c32013-08-27 17:31:03 -07001016
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001017 // Update oat_dex_files_.
1018 auto oat_class_it = oat_classes_.begin();
1019 for (OatDexFile* oat_dex_file : oat_dex_files_) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001020 for (uint32_t& method_offset : oat_dex_file->methods_offsets_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001021 DCHECK(oat_class_it != oat_classes_.end());
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001022 method_offset = (*oat_class_it)->offset_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001023 ++oat_class_it;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001024 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001025 oat_dex_file->UpdateChecksum(oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001026 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001027 CHECK(oat_class_it == oat_classes_.end());
1028
1029 return offset;
1030}
1031
1032size_t OatWriter::InitOatMaps(size_t offset) {
1033 #define VISIT(VisitorType) \
1034 do { \
1035 VisitorType visitor(this, offset); \
1036 bool success = VisitDexMethods(&visitor); \
1037 DCHECK(success); \
1038 offset = visitor.GetOffset(); \
1039 } while (false)
1040
1041 VISIT(InitMapMethodVisitor<GcMapDataAccess>);
1042 VISIT(InitMapMethodVisitor<MappingTableDataAccess>);
1043 VISIT(InitMapMethodVisitor<VmapTableDataAccess>);
1044
1045 #undef VISIT
1046
Brian Carlstrome24fa612011-09-29 00:53:55 -07001047 return offset;
1048}
1049
1050size_t OatWriter::InitOatCode(size_t offset) {
1051 // calculate the offsets within OatHeader to executable code
1052 size_t old_offset = offset;
Dave Allison50abf0a2014-06-23 13:19:59 -07001053 size_t adjusted_offset = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001054 // required to be on a new page boundary
1055 offset = RoundUp(offset, kPageSize);
1056 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001057 size_executable_offset_alignment_ = offset - old_offset;
1058 if (compiler_driver_->IsImage()) {
Alex Lighta59dd802014-07-02 16:28:08 -07001059 CHECK_EQ(image_patch_delta_, 0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001060 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001061
Ian Rogers848871b2013-08-05 10:56:33 -07001062 #define DO_TRAMPOLINE(field, fn_name) \
1063 offset = CompiledCode::AlignCode(offset, instruction_set); \
Dave Allison50abf0a2014-06-23 13:19:59 -07001064 adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \
1065 oat_header_->Set ## fn_name ## Offset(adjusted_offset); \
Ian Rogers848871b2013-08-05 10:56:33 -07001066 field.reset(compiler_driver_->Create ## fn_name()); \
1067 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001068
Ian Rogers848871b2013-08-05 10:56:33 -07001069 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge);
1070 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge);
1071 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Andreas Gampe2da88232014-02-27 12:26:20 -08001072 DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline);
Jeff Hao88474b42013-10-23 16:24:40 -07001073 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -07001074 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
1075 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001076
Ian Rogers848871b2013-08-05 10:56:33 -07001077 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001078 } else {
Ian Rogers848871b2013-08-05 10:56:33 -07001079 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
1080 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
1081 oat_header_->SetJniDlsymLookupOffset(0);
Andreas Gampe2da88232014-02-27 12:26:20 -08001082 oat_header_->SetQuickGenericJniTrampolineOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -07001083 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001084 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -07001085 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Alex Lighta59dd802014-07-02 16:28:08 -07001086 oat_header_->SetImagePatchDelta(image_patch_delta_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001087 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001088 return offset;
1089}
1090
1091size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001092 #define VISIT(VisitorType) \
1093 do { \
1094 VisitorType visitor(this, offset); \
1095 bool success = VisitDexMethods(&visitor); \
1096 DCHECK(success); \
1097 offset = visitor.GetOffset(); \
1098 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001099
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001100 VISIT(InitCodeMethodVisitor);
Ian Rogers1212a022013-03-04 10:48:41 -08001101 if (compiler_driver_->IsImage()) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001102 VISIT(InitImageMethodVisitor);
Ian Rogers0571d352011-11-03 19:51:38 -07001103 }
Logan Chien8b977d32012-02-21 19:14:55 +08001104
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001105 #undef VISIT
1106
Brian Carlstrome24fa612011-09-29 00:53:55 -07001107 return offset;
1108}
1109
David Srbeckybc90fd02015-04-22 19:40:27 +01001110bool OatWriter::WriteRodata(OutputStream* out) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001111 const off_t raw_file_offset = out->Seek(0, kSeekCurrent);
1112 if (raw_file_offset == (off_t) -1) {
1113 LOG(ERROR) << "Failed to get file offset in " << out->GetLocation();
1114 return false;
1115 }
1116 const size_t file_offset = static_cast<size_t>(raw_file_offset);
David Srbeckybc90fd02015-04-22 19:40:27 +01001117 oat_data_offset_ = file_offset;
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001118
Vladimir Markof4da6752014-08-01 19:04:18 +01001119 // Reserve space for header. It will be written last - after updating the checksum.
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001120 size_t header_size = oat_header_->GetHeaderSize();
Vladimir Markof4da6752014-08-01 19:04:18 +01001121 if (out->Seek(header_size, kSeekCurrent) == (off_t) -1) {
1122 PLOG(ERROR) << "Failed to reserve space for oat header in " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001123 return false;
1124 }
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001125 size_oat_header_ += sizeof(OatHeader);
1126 size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001127
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001128 if (!WriteTables(out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001129 LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001130 return false;
1131 }
1132
Vladimir Markof4da6752014-08-01 19:04:18 +01001133 off_t tables_end_offset = out->Seek(0, kSeekCurrent);
1134 if (tables_end_offset == (off_t) -1) {
1135 LOG(ERROR) << "Failed to seek to oat code position in " << out->GetLocation();
1136 return false;
1137 }
1138 size_t relative_offset = static_cast<size_t>(tables_end_offset) - file_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001139 relative_offset = WriteMaps(out, file_offset, relative_offset);
1140 if (relative_offset == 0) {
1141 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
1142 return false;
1143 }
1144
David Srbeckybc90fd02015-04-22 19:40:27 +01001145 // Write padding.
1146 off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent);
1147 relative_offset += size_executable_offset_alignment_;
1148 DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset());
1149 size_t expected_file_offset = file_offset + relative_offset;
1150 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
1151 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
1152 << " Expected: " << expected_file_offset << " File: " << out->GetLocation();
1153 return 0;
1154 }
1155 DCHECK_OFFSET();
1156
1157 return true;
1158}
1159
1160bool OatWriter::WriteCode(OutputStream* out) {
1161 size_t header_size = oat_header_->GetHeaderSize();
1162 const size_t file_offset = oat_data_offset_;
1163 size_t relative_offset = oat_header_->GetExecutableOffset();
1164 DCHECK_OFFSET();
1165
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001166 relative_offset = WriteCode(out, file_offset, relative_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001167 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001168 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001169 return false;
1170 }
1171
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001172 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
1173 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001174 LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001175 return false;
1176 }
1177
Vladimir Markof4da6752014-08-01 19:04:18 +01001178 const off_t oat_end_file_offset = out->Seek(0, kSeekCurrent);
1179 if (oat_end_file_offset == (off_t) -1) {
1180 LOG(ERROR) << "Failed to get oat end file offset in " << out->GetLocation();
1181 return false;
1182 }
1183
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001184 if (kIsDebugBuild) {
1185 uint32_t size_total = 0;
1186 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001187 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001188 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001189
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001190 DO_STAT(size_dex_file_alignment_);
1191 DO_STAT(size_executable_offset_alignment_);
1192 DO_STAT(size_oat_header_);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001193 DO_STAT(size_oat_header_key_value_store_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001194 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -07001195 DO_STAT(size_interpreter_to_interpreter_bridge_);
1196 DO_STAT(size_interpreter_to_compiled_code_bridge_);
1197 DO_STAT(size_jni_dlsym_lookup_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001198 DO_STAT(size_quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001199 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001200 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001201 DO_STAT(size_quick_to_interpreter_bridge_);
1202 DO_STAT(size_trampoline_alignment_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001203 DO_STAT(size_method_header_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001204 DO_STAT(size_code_);
1205 DO_STAT(size_code_alignment_);
Vladimir Markof4da6752014-08-01 19:04:18 +01001206 DO_STAT(size_relative_call_thunks_);
Vladimir Markoc74658b2015-03-31 10:26:41 +01001207 DO_STAT(size_misc_thunks_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001208 DO_STAT(size_mapping_table_);
1209 DO_STAT(size_vmap_table_);
1210 DO_STAT(size_gc_map_);
1211 DO_STAT(size_oat_dex_file_location_size_);
1212 DO_STAT(size_oat_dex_file_location_data_);
1213 DO_STAT(size_oat_dex_file_location_checksum_);
1214 DO_STAT(size_oat_dex_file_offset_);
1215 DO_STAT(size_oat_dex_file_methods_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001216 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001217 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001218 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001219 DO_STAT(size_oat_class_method_offsets_);
1220 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001221
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001222 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Vladimir Markof4da6752014-08-01 19:04:18 +01001223 CHECK_EQ(file_offset + size_total, static_cast<size_t>(oat_end_file_offset));
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001224 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001225 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001226
Vladimir Markof4da6752014-08-01 19:04:18 +01001227 CHECK_EQ(file_offset + size_, static_cast<size_t>(oat_end_file_offset));
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001228 CHECK_EQ(size_, relative_offset);
1229
Vladimir Markof4da6752014-08-01 19:04:18 +01001230 // Write the header now that the checksum is final.
1231 if (out->Seek(file_offset, kSeekSet) == (off_t) -1) {
1232 PLOG(ERROR) << "Failed to seek to oat header position in " << out->GetLocation();
1233 return false;
1234 }
David Srbeckybc90fd02015-04-22 19:40:27 +01001235 DCHECK_EQ(file_offset, static_cast<size_t>(out->Seek(0, kSeekCurrent)));
Vladimir Markof4da6752014-08-01 19:04:18 +01001236 if (!out->WriteFully(oat_header_, header_size)) {
1237 PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation();
1238 return false;
1239 }
1240 if (out->Seek(oat_end_file_offset, kSeekSet) == (off_t) -1) {
1241 PLOG(ERROR) << "Failed to seek to end after writing oat header to " << out->GetLocation();
1242 return false;
1243 }
1244 DCHECK_EQ(oat_end_file_offset, out->Seek(0, kSeekCurrent));
1245
Brian Carlstrome24fa612011-09-29 00:53:55 -07001246 return true;
1247}
1248
Ian Rogers3d504072014-03-01 09:16:49 -08001249bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001250 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001251 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001252 PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001253 return false;
1254 }
1255 }
Brian Carlstrom89521892011-12-07 22:05:07 -08001256 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001257 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Ian Rogers3d504072014-03-01 09:16:49 -08001258 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -08001259 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
1260 const DexFile* dex_file = (*dex_files_)[i];
1261 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
1262 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
1263 return false;
1264 }
1265 const DexFile* dex_file = (*dex_files_)[i];
Ian Rogers3d504072014-03-01 09:16:49 -08001266 if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001267 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
Ian Rogers3d504072014-03-01 09:16:49 -08001268 << " to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001269 return false;
1270 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001271 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -08001272 }
Brian Carlstrom389efb02012-01-11 12:06:26 -08001273 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001274 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001275 PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001276 return false;
1277 }
1278 }
1279 return true;
1280}
1281
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001282size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) {
1283 #define VISIT(VisitorType) \
1284 do { \
1285 VisitorType visitor(this, out, file_offset, relative_offset); \
1286 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1287 return 0; \
1288 } \
1289 relative_offset = visitor.GetOffset(); \
1290 } while (false)
1291
1292 size_t gc_maps_offset = relative_offset;
1293 VISIT(WriteMapMethodVisitor<GcMapDataAccess>);
1294 size_gc_map_ = relative_offset - gc_maps_offset;
1295
1296 size_t mapping_tables_offset = relative_offset;
1297 VISIT(WriteMapMethodVisitor<MappingTableDataAccess>);
1298 size_mapping_table_ = relative_offset - mapping_tables_offset;
1299
1300 size_t vmap_tables_offset = relative_offset;
1301 VISIT(WriteMapMethodVisitor<VmapTableDataAccess>);
1302 size_vmap_table_ = relative_offset - vmap_tables_offset;
1303
1304 #undef VISIT
1305
1306 return relative_offset;
1307}
1308
1309size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001310 if (compiler_driver_->IsImage()) {
1311 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001312
Ian Rogers848871b2013-08-05 10:56:33 -07001313 #define DO_TRAMPOLINE(field) \
1314 do { \
1315 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
1316 uint32_t alignment_padding = aligned_offset - relative_offset; \
Ian Rogers3d504072014-03-01 09:16:49 -08001317 out->Seek(alignment_padding, kSeekCurrent); \
Ian Rogers848871b2013-08-05 10:56:33 -07001318 size_trampoline_alignment_ += alignment_padding; \
Ian Rogers3d504072014-03-01 09:16:49 -08001319 if (!out->WriteFully(&(*field)[0], field->size())) { \
1320 PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \
Ian Rogers848871b2013-08-05 10:56:33 -07001321 return false; \
1322 } \
1323 size_ ## field += field->size(); \
1324 relative_offset += alignment_padding + field->size(); \
1325 DCHECK_OFFSET(); \
1326 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001327
Ian Rogers848871b2013-08-05 10:56:33 -07001328 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_);
1329 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_);
1330 DO_TRAMPOLINE(jni_dlsym_lookup_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001331 DO_TRAMPOLINE(quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001332 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001333 DO_TRAMPOLINE(quick_resolution_trampoline_);
1334 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
1335 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001336 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001337 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001338}
1339
Ian Rogers3d504072014-03-01 09:16:49 -08001340size_t OatWriter::WriteCodeDexFiles(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001341 const size_t file_offset,
1342 size_t relative_offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001343 #define VISIT(VisitorType) \
1344 do { \
1345 VisitorType visitor(this, out, file_offset, relative_offset); \
1346 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1347 return 0; \
1348 } \
1349 relative_offset = visitor.GetOffset(); \
1350 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001351
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001352 VISIT(WriteCodeMethodVisitor);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001353
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001354 #undef VISIT
Brian Carlstrom265091e2013-01-30 14:08:26 -08001355
Vladimir Markob163bb72015-03-31 21:49:49 +01001356 size_code_alignment_ += relative_patcher_->CodeAlignmentSize();
1357 size_relative_call_thunks_ += relative_patcher_->RelativeCallThunksSize();
1358 size_misc_thunks_ += relative_patcher_->MiscThunksSize();
1359
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001360 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001361}
1362
Vladimir Markof4da6752014-08-01 19:04:18 +01001363bool OatWriter::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) {
1364 static const uint8_t kPadding[] = {
1365 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
1366 };
1367 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
1368 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
1369 return false;
1370 }
1371 size_code_alignment_ += aligned_code_delta;
1372 return true;
1373}
1374
Vladimir Markob163bb72015-03-31 21:49:49 +01001375std::pair<bool, uint32_t> OatWriter::MethodOffsetMap::FindMethodOffset(MethodReference ref) {
1376 auto it = map.find(ref);
1377 if (it == map.end()) {
1378 return std::pair<bool, uint32_t>(false, 0u);
1379 } else {
1380 return std::pair<bool, uint32_t>(true, it->second);
1381 }
1382}
1383
Brian Carlstrom265091e2013-01-30 14:08:26 -08001384OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
1385 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -08001386 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001387 dex_file_location_size_ = location.size();
1388 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001389 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -08001390 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001391 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001392}
1393
1394size_t OatWriter::OatDexFile::SizeOf() const {
1395 return sizeof(dex_file_location_size_)
1396 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001397 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -08001398 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001399 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001400}
1401
Ian Rogers3d504072014-03-01 09:16:49 -08001402void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const {
1403 oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
1404 oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
1405 oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
1406 oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
1407 oat_header->UpdateChecksum(&methods_offsets_[0],
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001408 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001409}
1410
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001411bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001412 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001413 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001414 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001415 if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
1416 PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001417 return false;
1418 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001419 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001420 if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
1421 PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001422 return false;
1423 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001424 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Ian Rogers3d504072014-03-01 09:16:49 -08001425 if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
1426 PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001427 return false;
1428 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001429 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Ian Rogers3d504072014-03-01 09:16:49 -08001430 if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
1431 PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001432 return false;
1433 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001434 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Ian Rogers3d504072014-03-01 09:16:49 -08001435 if (!out->WriteFully(&methods_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001436 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001437 PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001438 return false;
1439 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001440 oat_writer->size_oat_dex_file_methods_offsets_ +=
1441 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001442 return true;
1443}
1444
Brian Carlstromba150c32013-08-27 17:31:03 -07001445OatWriter::OatClass::OatClass(size_t offset,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001446 const std::vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -07001447 uint32_t num_non_null_compiled_methods,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001448 mirror::Class::Status status)
1449 : compiled_methods_(compiled_methods) {
1450 uint32_t num_methods = compiled_methods.size();
Brian Carlstromba150c32013-08-27 17:31:03 -07001451 CHECK_LE(num_non_null_compiled_methods, num_methods);
1452
Brian Carlstrom265091e2013-01-30 14:08:26 -08001453 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -07001454 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
1455
1456 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
1457 // apply when there are 0 methods, we just arbitrarily say that 0
1458 // methods means kOatClassNoneCompiled and that we won't use
1459 // kOatClassAllCompiled unless there is at least one compiled
1460 // method. This means in an interpretter only system, we can assert
1461 // that all classes are kOatClassNoneCompiled.
1462 if (num_non_null_compiled_methods == 0) {
1463 type_ = kOatClassNoneCompiled;
1464 } else if (num_non_null_compiled_methods == num_methods) {
1465 type_ = kOatClassAllCompiled;
1466 } else {
1467 type_ = kOatClassSomeCompiled;
1468 }
1469
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001470 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -07001471 method_offsets_.resize(num_non_null_compiled_methods);
Vladimir Marko8a630572014-04-09 18:45:35 +01001472 method_headers_.resize(num_non_null_compiled_methods);
Brian Carlstromba150c32013-08-27 17:31:03 -07001473
1474 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
1475 if (type_ == kOatClassSomeCompiled) {
1476 method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator());
1477 method_bitmap_size_ = method_bitmap_->GetSizeOf();
1478 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
1479 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
1480 } else {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001481 method_bitmap_ = nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001482 method_bitmap_size_ = 0;
1483 }
1484
1485 for (size_t i = 0; i < num_methods; i++) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001486 CompiledMethod* compiled_method = compiled_methods_[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001487 if (compiled_method == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001488 oat_method_offsets_offsets_from_oat_class_[i] = 0;
1489 } else {
1490 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
1491 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
1492 if (type_ == kOatClassSomeCompiled) {
1493 method_bitmap_->SetBit(i);
1494 }
1495 }
1496 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001497}
1498
Brian Carlstromba150c32013-08-27 17:31:03 -07001499OatWriter::OatClass::~OatClass() {
Mathieu Chartier661974a2014-01-09 11:23:53 -08001500 delete method_bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -07001501}
1502
Brian Carlstrom265091e2013-01-30 14:08:26 -08001503size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
1504 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001505 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
1506 if (method_offset == 0) {
1507 return 0;
1508 }
1509 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001510}
1511
1512size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
1513 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001514 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08001515}
1516
1517size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001518 return sizeof(status_)
1519 + sizeof(type_)
1520 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
1521 + method_bitmap_size_
1522 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001523}
1524
Ian Rogers3d504072014-03-01 09:16:49 -08001525void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const {
1526 oat_header->UpdateChecksum(&status_, sizeof(status_));
1527 oat_header->UpdateChecksum(&type_, sizeof(type_));
Brian Carlstromba150c32013-08-27 17:31:03 -07001528 if (method_bitmap_size_ != 0) {
1529 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001530 oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_));
1531 oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001532 }
Ian Rogers3d504072014-03-01 09:16:49 -08001533 oat_header->UpdateChecksum(&method_offsets_[0],
1534 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001535}
1536
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001537bool OatWriter::OatClass::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001538 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001539 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001540 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001541 if (!out->WriteFully(&status_, sizeof(status_))) {
1542 PLOG(ERROR) << "Failed to write class status to " << out->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001543 return false;
1544 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001545 oat_writer->size_oat_class_status_ += sizeof(status_);
Ian Rogers3d504072014-03-01 09:16:49 -08001546 if (!out->WriteFully(&type_, sizeof(type_))) {
1547 PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001548 return false;
1549 }
1550 oat_writer->size_oat_class_type_ += sizeof(type_);
1551 if (method_bitmap_size_ != 0) {
1552 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001553 if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
1554 PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001555 return false;
1556 }
1557 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001558 if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
1559 PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001560 return false;
1561 }
1562 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1563 }
Ian Rogers3d504072014-03-01 09:16:49 -08001564 if (!out->WriteFully(&method_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001565 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001566 PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001567 return false;
1568 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001569 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001570 return true;
1571}
1572
Brian Carlstrome24fa612011-09-29 00:53:55 -07001573} // namespace art