blob: fdf904d1f045a1c40cceeb84298dcf91908ba2fa [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"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Ian Rogerse77493c2014-08-20 15:08:45 -070023#include "base/allocator.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070024#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080025#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080026#include "base/unix_file/fd_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070027#include "class_linker.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070028#include "compiled_class.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000029#include "compiled_method.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "dex_file-inl.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000031#include "dex/verification_results.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000032#include "driver/compiler_driver.h"
33#include "driver/compiler_options.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070034#include "gc/space/space.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010035#include "image_writer.h"
Vladimir Markob163bb72015-03-31 21:49:49 +010036#include "linker/relative_patcher.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)
Mathieu Chartier90443472015-07-16 20:32:27 -0700368 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100369 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();
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000377 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800378 uint32_t thumb_offset = compiled_method->CodeDelta();
Elliott Hughes956af0f2014-12-11 14:34:28 -0800379
David Srbecky009e2a62015-04-15 02:46:30 +0100380 // Deduplicate code arrays if we are not producing debuggable code.
Vladimir Markoc74658b2015-03-31 10:26:41 +0100381 bool deduped = false;
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000382 if (debuggable_) {
383 quick_code_offset = NewQuickCodeOffset(compiled_method, it, thumb_offset);
384 } else {
385 auto lb = dedupe_map_.lower_bound(compiled_method);
386 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(compiled_method, lb->first)) {
387 quick_code_offset = lb->second;
388 deduped = true;
Nicolas Geoffrayf0758792015-07-13 11:56:00 +0000389 } else {
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000390 quick_code_offset = NewQuickCodeOffset(compiled_method, it, thumb_offset);
391 dedupe_map_.PutBefore(lb, compiled_method, quick_code_offset);
David Srbecky009e2a62015-04-15 02:46:30 +0100392 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800393 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100394
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100395 if (code_size != 0) {
396 MethodReference method_ref(dex_file_, it.GetMemberIndex());
397 auto method_lb = writer_->method_offset_map_.map.lower_bound(method_ref);
398 if (method_lb != writer_->method_offset_map_.map.end() &&
399 !writer_->method_offset_map_.map.key_comp()(method_ref, method_lb->first)) {
400 // TODO: Should this be a hard failure?
401 LOG(WARNING) << "Multiple definitions of "
402 << PrettyMethod(method_ref.dex_method_index, *method_ref.dex_file)
403 << ((method_lb->second != quick_code_offset) ? "; OFFSET MISMATCH" : "");
404 } else {
405 writer_->method_offset_map_.map.PutBefore(method_lb, method_ref, quick_code_offset);
406 }
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_;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100414 // If we don't have quick code, then we must have a vmap, as that is how the dex2dex
415 // compiler records its transformations.
416 DCHECK(quick_code != nullptr || vmap_table_offset != 0);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800417 uint32_t gc_map_offset = method_header->gc_map_offset_;
418 // The code offset was 0 when the mapping/vmap table offset was set, so it's set
419 // to 0-offset and we need to adjust it by code_offset.
420 uint32_t code_offset = quick_code_offset - thumb_offset;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100421 if (mapping_table_offset != 0u && code_offset != 0u) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800422 mapping_table_offset += code_offset;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100423 DCHECK_LT(mapping_table_offset, code_offset) << "Overflow in oat offsets";
Elliott Hughes956af0f2014-12-11 14:34:28 -0800424 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100425 if (vmap_table_offset != 0u && code_offset != 0u) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800426 vmap_table_offset += code_offset;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100427 DCHECK_LT(vmap_table_offset, code_offset) << "Overflow in oat offsets";
Elliott Hughes956af0f2014-12-11 14:34:28 -0800428 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100429 if (gc_map_offset != 0u && code_offset != 0u) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800430 gc_map_offset += code_offset;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100431 DCHECK_LT(gc_map_offset, code_offset) << "Overflow in oat offsets";
Elliott Hughes956af0f2014-12-11 14:34:28 -0800432 }
433 uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
434 uint32_t core_spill_mask = compiled_method->GetCoreSpillMask();
435 uint32_t fp_spill_mask = compiled_method->GetFpSpillMask();
436 *method_header = OatQuickMethodHeader(mapping_table_offset, vmap_table_offset,
437 gc_map_offset, frame_size_in_bytes, core_spill_mask,
438 fp_spill_mask, code_size);
Vladimir Marko7624d252014-05-02 14:40:15 +0100439
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000440 if (!deduped) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800441 // Update offsets. (Checksum is updated when writing.)
442 offset_ += sizeof(*method_header); // Method header is prepended before code.
443 offset_ += code_size;
444 // Record absolute patch locations.
445 if (!compiled_method->GetPatches().empty()) {
446 uintptr_t base_loc = offset_ - code_size - writer_->oat_header_->GetExecutableOffset();
447 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
Vladimir Marko20f85592015-03-19 10:07:02 +0000448 if (!patch.IsPcRelative()) {
David Srbeckyf8980872015-05-22 17:04:47 +0100449 writer_->absolute_patch_locations_.push_back(base_loc + patch.LiteralOffset());
Vladimir Markof4da6752014-08-01 19:04:18 +0100450 }
451 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100452 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800453 }
Alex Light78382fa2014-06-06 15:45:32 -0700454
David Srbecky8363c772015-05-28 16:12:43 +0100455 if (writer_->compiler_driver_->GetCompilerOptions().GetGenerateDebugInfo()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800456 // Record debug information for this function if we are doing that.
Elliott Hughes956af0f2014-12-11 14:34:28 -0800457 const uint32_t quick_code_start = quick_code_offset -
David Srbecky6f715892015-03-30 14:21:42 +0100458 writer_->oat_header_->GetExecutableOffset() - thumb_offset;
David Srbecky0df9e1f2015-04-07 19:02:58 +0100459 writer_->method_info_.push_back(DebugInfo {
460 dex_file_,
461 class_def_index_,
462 it.GetMemberIndex(),
463 it.GetMethodAccessFlags(),
464 it.GetMethodCodeItem(),
465 deduped,
466 quick_code_start,
467 quick_code_start + code_size,
468 compiled_method});
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100469 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100470
471 if (kIsDebugBuild) {
472 // We expect GC maps except when the class hasn't been verified or the method is native.
473 const CompilerDriver* compiler_driver = writer_->compiler_driver_;
474 ClassReference class_ref(dex_file_, class_def_index_);
475 CompiledClass* compiled_class = compiler_driver->GetCompiledClass(class_ref);
476 mirror::Class::Status status;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700477 if (compiled_class != nullptr) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100478 status = compiled_class->GetStatus();
479 } else if (compiler_driver->GetVerificationResults()->IsClassRejected(class_ref)) {
480 status = mirror::Class::kStatusError;
481 } else {
482 status = mirror::Class::kStatusNotReady;
483 }
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800484 const SwapVector<uint8_t>* gc_map = compiled_method->GetGcMap();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100485 if (gc_map != nullptr) {
486 size_t gc_map_size = gc_map->size() * sizeof(gc_map[0]);
Andreas Gampe51829322014-08-25 15:05:04 -0700487 bool is_native = it.MemberIsNative();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100488 CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified)
489 << gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " "
490 << (status < mirror::Class::kStatusVerified) << " " << status << " "
491 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
492 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100493 }
494
495 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
496 OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_];
497 offsets->code_offset_ = quick_code_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100498 ++method_offsets_index_;
499 }
500
501 return true;
502 }
503
504 private:
Vladimir Marko20f85592015-03-19 10:07:02 +0000505 struct CodeOffsetsKeyComparator {
506 bool operator()(const CompiledMethod* lhs, const CompiledMethod* rhs) const {
507 if (lhs->GetQuickCode() != rhs->GetQuickCode()) {
508 return lhs->GetQuickCode() < rhs->GetQuickCode();
509 }
510 // If the code is the same, all other fields are likely to be the same as well.
511 if (UNLIKELY(lhs->GetMappingTable() != rhs->GetMappingTable())) {
512 return lhs->GetMappingTable() < rhs->GetMappingTable();
513 }
514 if (UNLIKELY(lhs->GetVmapTable() != rhs->GetVmapTable())) {
515 return lhs->GetVmapTable() < rhs->GetVmapTable();
516 }
517 if (UNLIKELY(lhs->GetGcMap() != rhs->GetGcMap())) {
518 return lhs->GetGcMap() < rhs->GetGcMap();
519 }
520 const auto& lhs_patches = lhs->GetPatches();
521 const auto& rhs_patches = rhs->GetPatches();
522 if (UNLIKELY(lhs_patches.size() != rhs_patches.size())) {
523 return lhs_patches.size() < rhs_patches.size();
524 }
525 auto rit = rhs_patches.begin();
526 for (const LinkerPatch& lpatch : lhs_patches) {
527 if (UNLIKELY(!(lpatch == *rit))) {
528 return lpatch < *rit;
529 }
530 ++rit;
531 }
532 return false;
533 }
534 };
535
David Srbecky009e2a62015-04-15 02:46:30 +0100536 uint32_t NewQuickCodeOffset(CompiledMethod* compiled_method,
537 const ClassDataItemIterator& it,
538 uint32_t thumb_offset) {
539 offset_ = writer_->relative_patcher_->ReserveSpace(
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100540 offset_, compiled_method, MethodReference(dex_file_, it.GetMemberIndex()));
David Srbecky009e2a62015-04-15 02:46:30 +0100541 offset_ = compiled_method->AlignCode(offset_);
542 DCHECK_ALIGNED_PARAM(offset_,
543 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
544 return offset_ + sizeof(OatQuickMethodHeader) + thumb_offset;
545 }
546
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100547 // Deduplication is already done on a pointer basis by the compiler driver,
548 // so we can simply compare the pointers to find out if things are duplicated.
Vladimir Marko8a630572014-04-09 18:45:35 +0100549 SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_;
David Srbecky2f6cdb02015-04-11 00:17:53 +0100550
David Srbecky009e2a62015-04-15 02:46:30 +0100551 // Cache of compiler's --debuggable option.
552 const bool debuggable_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100553};
554
555template <typename DataAccess>
556class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor {
557 public:
558 InitMapMethodVisitor(OatWriter* writer, size_t offset)
559 : OatDexMethodVisitor(writer, offset) {
560 }
561
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700562 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it ATTRIBUTE_UNUSED)
Mathieu Chartier90443472015-07-16 20:32:27 -0700563 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100564 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
565 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
566
567 if (compiled_method != nullptr) {
568 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
569 DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u);
570
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800571 const SwapVector<uint8_t>* map = DataAccess::GetData(compiled_method);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100572 uint32_t map_size = map == nullptr ? 0 : map->size() * sizeof((*map)[0]);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100573 if (map_size != 0u) {
Vladimir Markobd72fc12014-07-09 16:06:40 +0100574 auto lb = dedupe_map_.lower_bound(map);
575 if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(map, lb->first)) {
576 DataAccess::SetOffset(oat_class, method_offsets_index_, lb->second);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100577 } else {
578 DataAccess::SetOffset(oat_class, method_offsets_index_, offset_);
Vladimir Markobd72fc12014-07-09 16:06:40 +0100579 dedupe_map_.PutBefore(lb, map, offset_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100580 offset_ += map_size;
581 writer_->oat_header_->UpdateChecksum(&(*map)[0], map_size);
582 }
583 }
584 ++method_offsets_index_;
585 }
586
587 return true;
588 }
589
590 private:
591 // Deduplication is already done on a pointer basis by the compiler driver,
592 // so we can simply compare the pointers to find out if things are duplicated.
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800593 SafeMap<const SwapVector<uint8_t>*, uint32_t> dedupe_map_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100594};
595
596class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor {
597 public:
598 InitImageMethodVisitor(OatWriter* writer, size_t offset)
Jeff Haoc7d11882015-02-03 15:08:39 -0800599 : OatDexMethodVisitor(writer, offset),
600 pointer_size_(GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet())) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100601 }
602
603 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
Mathieu Chartier90443472015-07-16 20:32:27 -0700604 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100605 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
606 CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
607
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800608 OatMethodOffsets offsets(0u);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100609 if (compiled_method != nullptr) {
610 DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size());
611 offsets = oat_class->method_offsets_[method_offsets_index_];
612 ++method_offsets_index_;
613 }
614
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100615 ClassLinker* linker = Runtime::Current()->GetClassLinker();
616 InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_));
617 // Unchecked as we hold mutator_lock_ on entry.
618 ScopedObjectAccessUnchecked soa(Thread::Current());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800619 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700620 Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache(
621 Thread::Current(), *dex_file_)));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700622 ArtMethod* method = linker->ResolveMethod(
623 *dex_file_, it.GetMemberIndex(), dex_cache, NullHandle<mirror::ClassLoader>(), nullptr,
624 invoke_type);
Andreas Gamped9efea62014-07-21 22:56:08 -0700625 if (method == nullptr) {
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100626 LOG(INTERNAL_FATAL) << "Unexpected failure to resolve a method: "
627 << PrettyMethod(it.GetMemberIndex(), *dex_file_, true);
Andreas Gamped9efea62014-07-21 22:56:08 -0700628 soa.Self()->AssertPendingException();
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000629 mirror::Throwable* exc = soa.Self()->GetException();
Andreas Gamped9efea62014-07-21 22:56:08 -0700630 std::string dump = exc->Dump();
631 LOG(FATAL) << dump;
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100632 UNREACHABLE();
Andreas Gamped9efea62014-07-21 22:56:08 -0700633 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100634
635 if (compiled_method != nullptr && compiled_method->GetQuickCode()->size() != 0) {
636 method->SetEntryPointFromQuickCompiledCodePtrSize(
637 reinterpret_cast<void*>(offsets.code_offset_), pointer_size_);
638 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100639
640 return true;
641 }
Jeff Haoc7d11882015-02-03 15:08:39 -0800642
643 protected:
644 const size_t pointer_size_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100645};
646
647class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor {
648 public:
649 WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
Vladimir Markof4da6752014-08-01 19:04:18 +0100650 size_t relative_offset) SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100651 : OatDexMethodVisitor(writer, relative_offset),
652 out_(out),
Vladimir Markof4da6752014-08-01 19:04:18 +0100653 file_offset_(file_offset),
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100654 soa_(Thread::Current()),
655 no_thread_suspension_(soa_.Self(), "OatWriter patching"),
Vladimir Markof4da6752014-08-01 19:04:18 +0100656 class_linker_(Runtime::Current()->GetClassLinker()),
657 dex_cache_(nullptr) {
658 if (writer_->image_writer_ != nullptr) {
659 // If we're creating the image, the address space must be ready so that we can apply patches.
660 CHECK(writer_->image_writer_->IsImageAddressSpaceReady());
661 patched_code_.reserve(16 * KB);
662 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100663 }
664
Vladimir Markof4da6752014-08-01 19:04:18 +0100665 ~WriteCodeMethodVisitor() UNLOCK_FUNCTION(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100666 }
667
668 bool StartClass(const DexFile* dex_file, size_t class_def_index)
Mathieu Chartier90443472015-07-16 20:32:27 -0700669 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100670 OatDexMethodVisitor::StartClass(dex_file, class_def_index);
671 if (dex_cache_ == nullptr || dex_cache_->GetDexFile() != dex_file) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700672 dex_cache_ = class_linker_->FindDexCache(Thread::Current(), *dex_file);
Vladimir Markof4da6752014-08-01 19:04:18 +0100673 }
674 return true;
675 }
676
Mathieu Chartier90443472015-07-16 20:32:27 -0700677 bool EndClass() SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100678 bool result = OatDexMethodVisitor::EndClass();
679 if (oat_class_index_ == writer_->oat_classes_.size()) {
680 DCHECK(result); // OatDexMethodVisitor::EndClass() never fails.
Vladimir Marko20f85592015-03-19 10:07:02 +0000681 offset_ = writer_->relative_patcher_->WriteThunks(out_, offset_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100682 if (UNLIKELY(offset_ == 0u)) {
683 PLOG(ERROR) << "Failed to write final relative call thunks";
684 result = false;
685 }
686 }
687 return result;
688 }
689
690 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it)
Mathieu Chartier90443472015-07-16 20:32:27 -0700691 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100692 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
693 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
694
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700695 // No thread suspension since dex_cache_ that may get invalidated if that occurs.
696 ScopedAssertNoThreadSuspension tsc(Thread::Current(), __FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700697 if (compiled_method != nullptr) { // ie. not an abstract method
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100698 size_t file_offset = file_offset_;
699 OutputStream* out = out_;
700
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800701 const SwapVector<uint8_t>* quick_code = compiled_method->GetQuickCode();
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100702 // Need a wrapper if we create a copy for patching.
703 ArrayRef<const uint8_t> wrapped(*quick_code);
704 uint32_t code_size = quick_code->size() * sizeof(uint8_t);
Nicolas Geoffrayf0758792015-07-13 11:56:00 +0000705
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100706 // Deduplicate code arrays.
707 const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_];
708 if (method_offsets.code_offset_ > offset_) {
709 offset_ = writer_->relative_patcher_->WriteThunks(out, offset_);
710 if (offset_ == 0u) {
711 ReportWriteFailure("relative call thunk", it);
712 return false;
Nicolas Geoffrayf0758792015-07-13 11:56:00 +0000713 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100714 uint32_t aligned_offset = compiled_method->AlignCode(offset_);
715 uint32_t aligned_code_delta = aligned_offset - offset_;
716 if (aligned_code_delta != 0) {
717 if (!writer_->WriteCodeAlignment(out, aligned_code_delta)) {
718 ReportWriteFailure("code alignment padding", it);
719 return false;
720 }
721 offset_ += aligned_code_delta;
722 DCHECK_OFFSET_();
723 }
724 DCHECK_ALIGNED_PARAM(offset_,
725 GetInstructionSetAlignment(compiled_method->GetInstructionSet()));
726 DCHECK_EQ(method_offsets.code_offset_,
727 offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta())
728 << PrettyMethod(it.GetMemberIndex(), *dex_file_);
729 const OatQuickMethodHeader& method_header =
730 oat_class->method_headers_[method_offsets_index_];
731 writer_->oat_header_->UpdateChecksum(&method_header, sizeof(method_header));
732 if (!out->WriteFully(&method_header, sizeof(method_header))) {
733 ReportWriteFailure("method header", it);
734 return false;
735 }
736 writer_->size_method_header_ += sizeof(method_header);
737 offset_ += sizeof(method_header);
Nicolas Geoffrayed6195a2015-07-13 17:02:30 +0000738 DCHECK_OFFSET_();
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100739
740 if (!compiled_method->GetPatches().empty()) {
741 patched_code_.assign(quick_code->begin(), quick_code->end());
742 wrapped = ArrayRef<const uint8_t>(patched_code_);
743 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
744 if (patch.Type() == kLinkerPatchCallRelative) {
745 // NOTE: Relative calls across oat files are not supported.
746 uint32_t target_offset = GetTargetOffset(patch);
747 uint32_t literal_offset = patch.LiteralOffset();
748 writer_->relative_patcher_->PatchCall(&patched_code_, literal_offset,
749 offset_ + literal_offset, target_offset);
750 } else if (patch.Type() == kLinkerPatchDexCacheArray) {
751 uint32_t target_offset = GetDexCacheOffset(patch);
752 uint32_t literal_offset = patch.LiteralOffset();
753 writer_->relative_patcher_->PatchDexCacheReference(&patched_code_, patch,
754 offset_ + literal_offset,
755 target_offset);
756 } else if (patch.Type() == kLinkerPatchCall) {
757 uint32_t target_offset = GetTargetOffset(patch);
758 PatchCodeAddress(&patched_code_, patch.LiteralOffset(), target_offset);
759 } else if (patch.Type() == kLinkerPatchMethod) {
760 ArtMethod* method = GetTargetMethod(patch);
761 PatchMethodAddress(&patched_code_, patch.LiteralOffset(), method);
762 } else if (patch.Type() == kLinkerPatchType) {
763 mirror::Class* type = GetTargetType(patch);
764 PatchObjectAddress(&patched_code_, patch.LiteralOffset(), type);
765 }
766 }
767 }
768
769 writer_->oat_header_->UpdateChecksum(wrapped.data(), code_size);
770 if (!out->WriteFully(wrapped.data(), code_size)) {
771 ReportWriteFailure("method code", it);
772 return false;
773 }
774 writer_->size_code_ += code_size;
775 offset_ += code_size;
Nicolas Geoffrayf0758792015-07-13 11:56:00 +0000776 }
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100777 DCHECK_OFFSET_();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100778 ++method_offsets_index_;
779 }
780
781 return true;
782 }
783
784 private:
785 OutputStream* const out_;
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100786 const size_t file_offset_;
787 const ScopedObjectAccess soa_;
788 const ScopedAssertNoThreadSuspension no_thread_suspension_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100789 ClassLinker* const class_linker_;
790 mirror::DexCache* dex_cache_;
791 std::vector<uint8_t> patched_code_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100792
793 void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) {
794 PLOG(ERROR) << "Failed to write " << what << " for "
795 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
796 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100797
Mathieu Chartiere401d142015-04-22 13:56:20 -0700798 ArtMethod* GetTargetMethod(const LinkerPatch& patch)
Mathieu Chartier90443472015-07-16 20:32:27 -0700799 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100800 MethodReference ref = patch.TargetMethod();
801 mirror::DexCache* dex_cache =
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700802 (dex_file_ == ref.dex_file) ? dex_cache_ : class_linker_->FindDexCache(
803 Thread::Current(), *ref.dex_file);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700804 ArtMethod* method = dex_cache->GetResolvedMethod(
805 ref.dex_method_index, class_linker_->GetImagePointerSize());
Vladimir Markof4da6752014-08-01 19:04:18 +0100806 CHECK(method != nullptr);
807 return method;
808 }
809
Mathieu Chartier90443472015-07-16 20:32:27 -0700810 uint32_t GetTargetOffset(const LinkerPatch& patch) SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markob163bb72015-03-31 21:49:49 +0100811 auto target_it = writer_->method_offset_map_.map.find(patch.TargetMethod());
Vladimir Markof4da6752014-08-01 19:04:18 +0100812 uint32_t target_offset =
Vladimir Markob163bb72015-03-31 21:49:49 +0100813 (target_it != writer_->method_offset_map_.map.end()) ? target_it->second : 0u;
Vladimir Markof4da6752014-08-01 19:04:18 +0100814 // If there's no compiled code, point to the correct trampoline.
815 if (UNLIKELY(target_offset == 0)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700816 ArtMethod* target = GetTargetMethod(patch);
Vladimir Markof4da6752014-08-01 19:04:18 +0100817 DCHECK(target != nullptr);
Jeff Haoa0acc2d2015-01-27 11:22:04 -0800818 size_t size = GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet());
819 const void* oat_code_offset = target->GetEntryPointFromQuickCompiledCodePtrSize(size);
820 if (oat_code_offset != 0) {
821 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(oat_code_offset));
822 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(oat_code_offset));
823 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(oat_code_offset));
824 target_offset = PointerToLowMemUInt32(oat_code_offset);
825 } else {
826 target_offset = target->IsNative()
827 ? writer_->oat_header_->GetQuickGenericJniTrampolineOffset()
828 : writer_->oat_header_->GetQuickToInterpreterBridgeOffset();
829 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100830 }
831 return target_offset;
832 }
833
834 mirror::Class* GetTargetType(const LinkerPatch& patch)
Mathieu Chartier90443472015-07-16 20:32:27 -0700835 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100836 mirror::DexCache* dex_cache = (dex_file_ == patch.TargetTypeDexFile())
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700837 ? dex_cache_ : class_linker_->FindDexCache(Thread::Current(), *patch.TargetTypeDexFile());
Vladimir Markof4da6752014-08-01 19:04:18 +0100838 mirror::Class* type = dex_cache->GetResolvedType(patch.TargetTypeIndex());
839 CHECK(type != nullptr);
840 return type;
841 }
842
Mathieu Chartier90443472015-07-16 20:32:27 -0700843 uint32_t GetDexCacheOffset(const LinkerPatch& patch) SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Marko20f85592015-03-19 10:07:02 +0000844 if (writer_->image_writer_ != nullptr) {
845 auto* element = writer_->image_writer_->GetDexCacheArrayElementImageAddress(
846 patch.TargetDexCacheDexFile(), patch.TargetDexCacheElementOffset());
847 const uint8_t* oat_data = writer_->image_writer_->GetOatFileBegin() + file_offset_;
848 return reinterpret_cast<const uint8_t*>(element) - oat_data;
849 } else {
850 LOG(FATAL) << "Unimplemented.";
851 UNREACHABLE();
852 }
853 }
854
Vladimir Markof4da6752014-08-01 19:04:18 +0100855 void PatchObjectAddress(std::vector<uint8_t>* code, uint32_t offset, mirror::Object* object)
Mathieu Chartier90443472015-07-16 20:32:27 -0700856 SHARED_REQUIRES(Locks::mutator_lock_) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100857 // NOTE: Direct method pointers across oat files don't use linker patches. However, direct
858 // type pointers across oat files do. (TODO: Investigate why.)
859 if (writer_->image_writer_ != nullptr) {
860 object = writer_->image_writer_->GetImageAddress(object);
861 }
862 uint32_t address = PointerToLowMemUInt32(object);
863 DCHECK_LE(offset + 4, code->size());
864 uint8_t* data = &(*code)[offset];
865 data[0] = address & 0xffu;
866 data[1] = (address >> 8) & 0xffu;
867 data[2] = (address >> 16) & 0xffu;
868 data[3] = (address >> 24) & 0xffu;
869 }
870
Mathieu Chartiere401d142015-04-22 13:56:20 -0700871 void PatchMethodAddress(std::vector<uint8_t>* code, uint32_t offset, ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700872 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700873 // NOTE: Direct method pointers across oat files don't use linker patches. However, direct
874 // type pointers across oat files do. (TODO: Investigate why.)
875 if (writer_->image_writer_ != nullptr) {
876 method = writer_->image_writer_->GetImageMethodAddress(method);
877 }
878 // Note: We only patch ArtMethods to low 4gb since thats where the image is.
879 uint32_t address = PointerToLowMemUInt32(method);
880 DCHECK_LE(offset + 4, code->size());
881 uint8_t* data = &(*code)[offset];
882 data[0] = address & 0xffu;
883 data[1] = (address >> 8) & 0xffu;
884 data[2] = (address >> 16) & 0xffu;
885 data[3] = (address >> 24) & 0xffu;
886 }
887
Vladimir Markof4da6752014-08-01 19:04:18 +0100888 void PatchCodeAddress(std::vector<uint8_t>* code, uint32_t offset, uint32_t target_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700889 SHARED_REQUIRES(Locks::mutator_lock_) {
Jeff Haoa0acc2d2015-01-27 11:22:04 -0800890 uint32_t address = writer_->image_writer_ == nullptr ? target_offset :
891 PointerToLowMemUInt32(writer_->image_writer_->GetOatFileBegin() +
892 writer_->oat_data_offset_ + target_offset);
Vladimir Markof4da6752014-08-01 19:04:18 +0100893 DCHECK_LE(offset + 4, code->size());
894 uint8_t* data = &(*code)[offset];
895 data[0] = address & 0xffu;
896 data[1] = (address >> 8) & 0xffu;
897 data[2] = (address >> 16) & 0xffu;
898 data[3] = (address >> 24) & 0xffu;
899 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100900};
901
902template <typename DataAccess>
903class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor {
904 public:
905 WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset,
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800906 size_t relative_offset)
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100907 : OatDexMethodVisitor(writer, relative_offset),
908 out_(out),
909 file_offset_(file_offset) {
910 }
911
912 bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) {
913 OatClass* oat_class = writer_->oat_classes_[oat_class_index_];
914 const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index);
915
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700916 if (compiled_method != nullptr) { // ie. not an abstract method
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100917 size_t file_offset = file_offset_;
918 OutputStream* out = out_;
919
920 uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_);
921 ++method_offsets_index_;
922
923 // Write deduplicated map.
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800924 const SwapVector<uint8_t>* map = DataAccess::GetData(compiled_method);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100925 size_t map_size = map == nullptr ? 0 : map->size() * sizeof((*map)[0]);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100926 DCHECK((map_size == 0u && map_offset == 0u) ||
927 (map_size != 0u && map_offset != 0u && map_offset <= offset_))
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800928 << map_size << " " << map_offset << " " << offset_ << " "
929 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " for " << DataAccess::Name();
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100930 if (map_size != 0u && map_offset == offset_) {
931 if (UNLIKELY(!out->WriteFully(&(*map)[0], map_size))) {
932 ReportWriteFailure(it);
933 return false;
934 }
935 offset_ += map_size;
936 }
937 DCHECK_OFFSET_();
938 }
939
940 return true;
941 }
942
943 private:
944 OutputStream* const out_;
945 size_t const file_offset_;
946
947 void ReportWriteFailure(const ClassDataItemIterator& it) {
948 PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for "
949 << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation();
950 }
951};
952
953// Visit all methods from all classes in all dex files with the specified visitor.
954bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) {
955 for (const DexFile* dex_file : *dex_files_) {
956 const size_t class_def_count = dex_file->NumClassDefs();
957 for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) {
958 if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) {
959 return false;
960 }
961 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700962 const uint8_t* class_data = dex_file->GetClassData(class_def);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700963 if (class_data != nullptr) { // ie not an empty class, such as a marker interface
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100964 ClassDataItemIterator it(*dex_file, class_data);
965 while (it.HasNextStaticField()) {
966 it.Next();
967 }
968 while (it.HasNextInstanceField()) {
969 it.Next();
970 }
971 size_t class_def_method_index = 0u;
972 while (it.HasNextDirectMethod()) {
973 if (!visitor->VisitMethod(class_def_method_index, it)) {
974 return false;
975 }
976 ++class_def_method_index;
977 it.Next();
978 }
979 while (it.HasNextVirtualMethod()) {
980 if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) {
981 return false;
982 }
983 ++class_def_method_index;
984 it.Next();
985 }
986 }
987 if (UNLIKELY(!visitor->EndClass())) {
988 return false;
989 }
990 }
991 }
992 return true;
993}
994
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700995size_t OatWriter::InitOatHeader() {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700996 oat_header_ = OatHeader::Create(compiler_driver_->GetInstructionSet(),
997 compiler_driver_->GetInstructionSetFeatures(),
998 dex_files_,
999 image_file_location_oat_checksum_,
1000 image_file_location_oat_begin_,
1001 key_value_store_);
1002
1003 return oat_header_->GetHeaderSize();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001004}
1005
1006size_t OatWriter::InitOatDexFiles(size_t offset) {
1007 // create the OatDexFiles
1008 for (size_t i = 0; i != dex_files_->size(); ++i) {
1009 const DexFile* dex_file = (*dex_files_)[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001010 CHECK(dex_file != nullptr);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001011 OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001012 oat_dex_files_.push_back(oat_dex_file);
1013 offset += oat_dex_file->SizeOf();
1014 }
1015 return offset;
1016}
1017
Brian Carlstrom89521892011-12-07 22:05:07 -08001018size_t OatWriter::InitDexFiles(size_t offset) {
1019 // calculate the offsets within OatDexFiles to the DexFiles
1020 for (size_t i = 0; i != dex_files_->size(); ++i) {
1021 // dex files are required to be 4 byte aligned
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001022 size_t original_offset = offset;
Brian Carlstrom89521892011-12-07 22:05:07 -08001023 offset = RoundUp(offset, 4);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001024 size_dex_file_alignment_ += offset - original_offset;
Brian Carlstrom89521892011-12-07 22:05:07 -08001025
1026 // set offset in OatDexFile to DexFile
1027 oat_dex_files_[i]->dex_file_offset_ = offset;
1028
1029 const DexFile* dex_file = (*dex_files_)[i];
1030 offset += dex_file->GetHeader().file_size_;
1031 }
1032 return offset;
1033}
1034
Brian Carlstrom389efb02012-01-11 12:06:26 -08001035size_t OatWriter::InitOatClasses(size_t offset) {
Brian Carlstrom389efb02012-01-11 12:06:26 -08001036 // calculate the offsets within OatDexFiles to OatClasses
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001037 InitOatClassesMethodVisitor visitor(this, offset);
1038 bool success = VisitDexMethods(&visitor);
1039 CHECK(success);
1040 offset = visitor.GetOffset();
Brian Carlstromba150c32013-08-27 17:31:03 -07001041
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001042 // Update oat_dex_files_.
1043 auto oat_class_it = oat_classes_.begin();
1044 for (OatDexFile* oat_dex_file : oat_dex_files_) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001045 for (uint32_t& method_offset : oat_dex_file->methods_offsets_) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001046 DCHECK(oat_class_it != oat_classes_.end());
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001047 method_offset = (*oat_class_it)->offset_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001048 ++oat_class_it;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001049 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001050 oat_dex_file->UpdateChecksum(oat_header_);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001051 }
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001052 CHECK(oat_class_it == oat_classes_.end());
1053
1054 return offset;
1055}
1056
1057size_t OatWriter::InitOatMaps(size_t offset) {
1058 #define VISIT(VisitorType) \
1059 do { \
1060 VisitorType visitor(this, offset); \
1061 bool success = VisitDexMethods(&visitor); \
1062 DCHECK(success); \
1063 offset = visitor.GetOffset(); \
1064 } while (false)
1065
1066 VISIT(InitMapMethodVisitor<GcMapDataAccess>);
1067 VISIT(InitMapMethodVisitor<MappingTableDataAccess>);
1068 VISIT(InitMapMethodVisitor<VmapTableDataAccess>);
1069
1070 #undef VISIT
1071
Brian Carlstrome24fa612011-09-29 00:53:55 -07001072 return offset;
1073}
1074
1075size_t OatWriter::InitOatCode(size_t offset) {
1076 // calculate the offsets within OatHeader to executable code
1077 size_t old_offset = offset;
Dave Allison50abf0a2014-06-23 13:19:59 -07001078 size_t adjusted_offset = offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001079 // required to be on a new page boundary
1080 offset = RoundUp(offset, kPageSize);
1081 oat_header_->SetExecutableOffset(offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001082 size_executable_offset_alignment_ = offset - old_offset;
1083 if (compiler_driver_->IsImage()) {
Alex Lighta59dd802014-07-02 16:28:08 -07001084 CHECK_EQ(image_patch_delta_, 0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001085 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001086
Ian Rogers848871b2013-08-05 10:56:33 -07001087 #define DO_TRAMPOLINE(field, fn_name) \
1088 offset = CompiledCode::AlignCode(offset, instruction_set); \
Dave Allison50abf0a2014-06-23 13:19:59 -07001089 adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \
1090 oat_header_->Set ## fn_name ## Offset(adjusted_offset); \
Ian Rogers848871b2013-08-05 10:56:33 -07001091 field.reset(compiler_driver_->Create ## fn_name()); \
1092 offset += field->size();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001093
Ian Rogers848871b2013-08-05 10:56:33 -07001094 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge);
1095 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge);
1096 DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup);
Andreas Gampe2da88232014-02-27 12:26:20 -08001097 DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline);
Jeff Hao88474b42013-10-23 16:24:40 -07001098 DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline);
Ian Rogers848871b2013-08-05 10:56:33 -07001099 DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline);
1100 DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001101
Ian Rogers848871b2013-08-05 10:56:33 -07001102 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001103 } else {
Ian Rogers848871b2013-08-05 10:56:33 -07001104 oat_header_->SetInterpreterToInterpreterBridgeOffset(0);
1105 oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0);
1106 oat_header_->SetJniDlsymLookupOffset(0);
Andreas Gampe2da88232014-02-27 12:26:20 -08001107 oat_header_->SetQuickGenericJniTrampolineOffset(0);
Jeff Hao88474b42013-10-23 16:24:40 -07001108 oat_header_->SetQuickImtConflictTrampolineOffset(0);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001109 oat_header_->SetQuickResolutionTrampolineOffset(0);
Ian Rogers848871b2013-08-05 10:56:33 -07001110 oat_header_->SetQuickToInterpreterBridgeOffset(0);
Alex Lighta59dd802014-07-02 16:28:08 -07001111 oat_header_->SetImagePatchDelta(image_patch_delta_);
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001112 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001113 return offset;
1114}
1115
1116size_t OatWriter::InitOatCodeDexFiles(size_t offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001117 #define VISIT(VisitorType) \
1118 do { \
1119 VisitorType visitor(this, offset); \
1120 bool success = VisitDexMethods(&visitor); \
1121 DCHECK(success); \
1122 offset = visitor.GetOffset(); \
1123 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001124
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001125 VISIT(InitCodeMethodVisitor);
Ian Rogers1212a022013-03-04 10:48:41 -08001126 if (compiler_driver_->IsImage()) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001127 VISIT(InitImageMethodVisitor);
Ian Rogers0571d352011-11-03 19:51:38 -07001128 }
Logan Chien8b977d32012-02-21 19:14:55 +08001129
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001130 #undef VISIT
1131
Brian Carlstrome24fa612011-09-29 00:53:55 -07001132 return offset;
1133}
1134
David Srbeckybc90fd02015-04-22 19:40:27 +01001135bool OatWriter::WriteRodata(OutputStream* out) {
Vladimir Markof4da6752014-08-01 19:04:18 +01001136 const off_t raw_file_offset = out->Seek(0, kSeekCurrent);
1137 if (raw_file_offset == (off_t) -1) {
1138 LOG(ERROR) << "Failed to get file offset in " << out->GetLocation();
1139 return false;
1140 }
1141 const size_t file_offset = static_cast<size_t>(raw_file_offset);
David Srbeckybc90fd02015-04-22 19:40:27 +01001142 oat_data_offset_ = file_offset;
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001143
Vladimir Markof4da6752014-08-01 19:04:18 +01001144 // Reserve space for header. It will be written last - after updating the checksum.
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001145 size_t header_size = oat_header_->GetHeaderSize();
Vladimir Markof4da6752014-08-01 19:04:18 +01001146 if (out->Seek(header_size, kSeekCurrent) == (off_t) -1) {
1147 PLOG(ERROR) << "Failed to reserve space for oat header in " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001148 return false;
1149 }
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001150 size_oat_header_ += sizeof(OatHeader);
1151 size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader);
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001152
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001153 if (!WriteTables(out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001154 LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001155 return false;
1156 }
1157
Vladimir Markof4da6752014-08-01 19:04:18 +01001158 off_t tables_end_offset = out->Seek(0, kSeekCurrent);
1159 if (tables_end_offset == (off_t) -1) {
1160 LOG(ERROR) << "Failed to seek to oat code position in " << out->GetLocation();
1161 return false;
1162 }
1163 size_t relative_offset = static_cast<size_t>(tables_end_offset) - file_offset;
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001164 relative_offset = WriteMaps(out, file_offset, relative_offset);
1165 if (relative_offset == 0) {
1166 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
1167 return false;
1168 }
1169
David Srbeckybc90fd02015-04-22 19:40:27 +01001170 // Write padding.
1171 off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent);
1172 relative_offset += size_executable_offset_alignment_;
1173 DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset());
1174 size_t expected_file_offset = file_offset + relative_offset;
1175 if (static_cast<uint32_t>(new_offset) != expected_file_offset) {
1176 PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset
1177 << " Expected: " << expected_file_offset << " File: " << out->GetLocation();
1178 return 0;
1179 }
1180 DCHECK_OFFSET();
1181
1182 return true;
1183}
1184
1185bool OatWriter::WriteCode(OutputStream* out) {
1186 size_t header_size = oat_header_->GetHeaderSize();
1187 const size_t file_offset = oat_data_offset_;
1188 size_t relative_offset = oat_header_->GetExecutableOffset();
1189 DCHECK_OFFSET();
1190
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001191 relative_offset = WriteCode(out, file_offset, relative_offset);
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001192 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001193 LOG(ERROR) << "Failed to write oat code to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001194 return false;
1195 }
1196
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001197 relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset);
1198 if (relative_offset == 0) {
Ian Rogers3d504072014-03-01 09:16:49 -08001199 LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001200 return false;
1201 }
1202
Vladimir Markof4da6752014-08-01 19:04:18 +01001203 const off_t oat_end_file_offset = out->Seek(0, kSeekCurrent);
1204 if (oat_end_file_offset == (off_t) -1) {
1205 LOG(ERROR) << "Failed to get oat end file offset in " << out->GetLocation();
1206 return false;
1207 }
1208
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001209 if (kIsDebugBuild) {
1210 uint32_t size_total = 0;
1211 #define DO_STAT(x) \
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001212 VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001213 size_total += x;
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001214
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001215 DO_STAT(size_dex_file_alignment_);
1216 DO_STAT(size_executable_offset_alignment_);
1217 DO_STAT(size_oat_header_);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001218 DO_STAT(size_oat_header_key_value_store_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001219 DO_STAT(size_dex_file_);
Ian Rogers848871b2013-08-05 10:56:33 -07001220 DO_STAT(size_interpreter_to_interpreter_bridge_);
1221 DO_STAT(size_interpreter_to_compiled_code_bridge_);
1222 DO_STAT(size_jni_dlsym_lookup_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001223 DO_STAT(size_quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001224 DO_STAT(size_quick_imt_conflict_trampoline_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001225 DO_STAT(size_quick_resolution_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001226 DO_STAT(size_quick_to_interpreter_bridge_);
1227 DO_STAT(size_trampoline_alignment_);
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001228 DO_STAT(size_method_header_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001229 DO_STAT(size_code_);
1230 DO_STAT(size_code_alignment_);
Vladimir Markof4da6752014-08-01 19:04:18 +01001231 DO_STAT(size_relative_call_thunks_);
Vladimir Markoc74658b2015-03-31 10:26:41 +01001232 DO_STAT(size_misc_thunks_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001233 DO_STAT(size_mapping_table_);
1234 DO_STAT(size_vmap_table_);
1235 DO_STAT(size_gc_map_);
1236 DO_STAT(size_oat_dex_file_location_size_);
1237 DO_STAT(size_oat_dex_file_location_data_);
1238 DO_STAT(size_oat_dex_file_location_checksum_);
1239 DO_STAT(size_oat_dex_file_offset_);
1240 DO_STAT(size_oat_dex_file_methods_offsets_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001241 DO_STAT(size_oat_class_type_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001242 DO_STAT(size_oat_class_status_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001243 DO_STAT(size_oat_class_method_bitmaps_);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001244 DO_STAT(size_oat_class_method_offsets_);
1245 #undef DO_STAT
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001246
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001247 VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \
Vladimir Markof4da6752014-08-01 19:04:18 +01001248 CHECK_EQ(file_offset + size_total, static_cast<size_t>(oat_end_file_offset));
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001249 CHECK_EQ(size_, size_total);
Ian Rogers4bdbbc82013-06-10 16:02:31 -07001250 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001251
Vladimir Markof4da6752014-08-01 19:04:18 +01001252 CHECK_EQ(file_offset + size_, static_cast<size_t>(oat_end_file_offset));
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001253 CHECK_EQ(size_, relative_offset);
1254
Vladimir Markof4da6752014-08-01 19:04:18 +01001255 // Write the header now that the checksum is final.
1256 if (out->Seek(file_offset, kSeekSet) == (off_t) -1) {
1257 PLOG(ERROR) << "Failed to seek to oat header position in " << out->GetLocation();
1258 return false;
1259 }
David Srbeckybc90fd02015-04-22 19:40:27 +01001260 DCHECK_EQ(file_offset, static_cast<size_t>(out->Seek(0, kSeekCurrent)));
Vladimir Markof4da6752014-08-01 19:04:18 +01001261 if (!out->WriteFully(oat_header_, header_size)) {
1262 PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation();
1263 return false;
1264 }
1265 if (out->Seek(oat_end_file_offset, kSeekSet) == (off_t) -1) {
1266 PLOG(ERROR) << "Failed to seek to end after writing oat header to " << out->GetLocation();
1267 return false;
1268 }
1269 DCHECK_EQ(oat_end_file_offset, out->Seek(0, kSeekCurrent));
1270
Brian Carlstrome24fa612011-09-29 00:53:55 -07001271 return true;
1272}
1273
Ian Rogers3d504072014-03-01 09:16:49 -08001274bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001275 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001276 if (!oat_dex_files_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001277 PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001278 return false;
1279 }
1280 }
Brian Carlstrom89521892011-12-07 22:05:07 -08001281 for (size_t i = 0; i != oat_dex_files_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001282 uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_;
Ian Rogers3d504072014-03-01 09:16:49 -08001283 off_t actual_offset = out->Seek(expected_offset, kSeekSet);
Brian Carlstrom89521892011-12-07 22:05:07 -08001284 if (static_cast<uint32_t>(actual_offset) != expected_offset) {
1285 const DexFile* dex_file = (*dex_files_)[i];
1286 PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset
1287 << " Expected: " << expected_offset << " File: " << dex_file->GetLocation();
1288 return false;
1289 }
1290 const DexFile* dex_file = (*dex_files_)[i];
Ian Rogers3d504072014-03-01 09:16:49 -08001291 if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001292 PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation()
Ian Rogers3d504072014-03-01 09:16:49 -08001293 << " to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001294 return false;
1295 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001296 size_dex_file_ += dex_file->GetHeader().file_size_;
Brian Carlstrom89521892011-12-07 22:05:07 -08001297 }
Brian Carlstrom389efb02012-01-11 12:06:26 -08001298 for (size_t i = 0; i != oat_classes_.size(); ++i) {
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001299 if (!oat_classes_[i]->Write(this, out, file_offset)) {
Ian Rogers3d504072014-03-01 09:16:49 -08001300 PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001301 return false;
1302 }
1303 }
1304 return true;
1305}
1306
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001307size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) {
1308 #define VISIT(VisitorType) \
1309 do { \
1310 VisitorType visitor(this, out, file_offset, relative_offset); \
1311 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1312 return 0; \
1313 } \
1314 relative_offset = visitor.GetOffset(); \
1315 } while (false)
1316
1317 size_t gc_maps_offset = relative_offset;
1318 VISIT(WriteMapMethodVisitor<GcMapDataAccess>);
1319 size_gc_map_ = relative_offset - gc_maps_offset;
1320
1321 size_t mapping_tables_offset = relative_offset;
1322 VISIT(WriteMapMethodVisitor<MappingTableDataAccess>);
1323 size_mapping_table_ = relative_offset - mapping_tables_offset;
1324
1325 size_t vmap_tables_offset = relative_offset;
1326 VISIT(WriteMapMethodVisitor<VmapTableDataAccess>);
1327 size_vmap_table_ = relative_offset - vmap_tables_offset;
1328
1329 #undef VISIT
1330
1331 return relative_offset;
1332}
1333
1334size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) {
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001335 if (compiler_driver_->IsImage()) {
1336 InstructionSet instruction_set = compiler_driver_->GetInstructionSet();
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001337
Ian Rogers848871b2013-08-05 10:56:33 -07001338 #define DO_TRAMPOLINE(field) \
1339 do { \
1340 uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \
1341 uint32_t alignment_padding = aligned_offset - relative_offset; \
Ian Rogers3d504072014-03-01 09:16:49 -08001342 out->Seek(alignment_padding, kSeekCurrent); \
Ian Rogers848871b2013-08-05 10:56:33 -07001343 size_trampoline_alignment_ += alignment_padding; \
Ian Rogers3d504072014-03-01 09:16:49 -08001344 if (!out->WriteFully(&(*field)[0], field->size())) { \
1345 PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \
Ian Rogers848871b2013-08-05 10:56:33 -07001346 return false; \
1347 } \
1348 size_ ## field += field->size(); \
1349 relative_offset += alignment_padding + field->size(); \
1350 DCHECK_OFFSET(); \
1351 } while (false)
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001352
Ian Rogers848871b2013-08-05 10:56:33 -07001353 DO_TRAMPOLINE(interpreter_to_interpreter_bridge_);
1354 DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_);
1355 DO_TRAMPOLINE(jni_dlsym_lookup_);
Andreas Gampe2da88232014-02-27 12:26:20 -08001356 DO_TRAMPOLINE(quick_generic_jni_trampoline_);
Jeff Hao88474b42013-10-23 16:24:40 -07001357 DO_TRAMPOLINE(quick_imt_conflict_trampoline_);
Ian Rogers848871b2013-08-05 10:56:33 -07001358 DO_TRAMPOLINE(quick_resolution_trampoline_);
1359 DO_TRAMPOLINE(quick_to_interpreter_bridge_);
1360 #undef DO_TRAMPOLINE
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001361 }
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001362 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001363}
1364
Ian Rogers3d504072014-03-01 09:16:49 -08001365size_t OatWriter::WriteCodeDexFiles(OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001366 const size_t file_offset,
1367 size_t relative_offset) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001368 #define VISIT(VisitorType) \
1369 do { \
1370 VisitorType visitor(this, out, file_offset, relative_offset); \
1371 if (UNLIKELY(!VisitDexMethods(&visitor))) { \
1372 return 0; \
1373 } \
1374 relative_offset = visitor.GetOffset(); \
1375 } while (false)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001376
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001377 VISIT(WriteCodeMethodVisitor);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001378
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001379 #undef VISIT
Brian Carlstrom265091e2013-01-30 14:08:26 -08001380
Vladimir Markob163bb72015-03-31 21:49:49 +01001381 size_code_alignment_ += relative_patcher_->CodeAlignmentSize();
1382 size_relative_call_thunks_ += relative_patcher_->RelativeCallThunksSize();
1383 size_misc_thunks_ += relative_patcher_->MiscThunksSize();
1384
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001385 return relative_offset;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001386}
1387
Vladimir Markof4da6752014-08-01 19:04:18 +01001388bool OatWriter::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) {
1389 static const uint8_t kPadding[] = {
1390 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
1391 };
1392 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
1393 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
1394 return false;
1395 }
1396 size_code_alignment_ += aligned_code_delta;
1397 return true;
1398}
1399
Vladimir Markob163bb72015-03-31 21:49:49 +01001400std::pair<bool, uint32_t> OatWriter::MethodOffsetMap::FindMethodOffset(MethodReference ref) {
1401 auto it = map.find(ref);
1402 if (it == map.end()) {
1403 return std::pair<bool, uint32_t>(false, 0u);
1404 } else {
1405 return std::pair<bool, uint32_t>(true, it->second);
1406 }
1407}
1408
Brian Carlstrom265091e2013-01-30 14:08:26 -08001409OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) {
1410 offset_ = offset;
Elliott Hughes95572412011-12-13 18:14:20 -08001411 const std::string& location(dex_file.GetLocation());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001412 dex_file_location_size_ = location.size();
1413 dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data());
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001414 dex_file_location_checksum_ = dex_file.GetLocationChecksum();
Brian Carlstrom89521892011-12-07 22:05:07 -08001415 dex_file_offset_ = 0;
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001416 methods_offsets_.resize(dex_file.NumClassDefs());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001417}
1418
1419size_t OatWriter::OatDexFile::SizeOf() const {
1420 return sizeof(dex_file_location_size_)
1421 + dex_file_location_size_
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001422 + sizeof(dex_file_location_checksum_)
Brian Carlstrom89521892011-12-07 22:05:07 -08001423 + sizeof(dex_file_offset_)
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001424 + (sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001425}
1426
Ian Rogers3d504072014-03-01 09:16:49 -08001427void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const {
1428 oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_));
1429 oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_);
1430 oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_));
1431 oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_));
1432 oat_header->UpdateChecksum(&methods_offsets_[0],
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001433 sizeof(methods_offsets_[0]) * methods_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001434}
1435
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001436bool OatWriter::OatDexFile::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001437 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001438 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001439 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001440 if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) {
1441 PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001442 return false;
1443 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001444 oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001445 if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) {
1446 PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001447 return false;
1448 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001449 oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_;
Ian Rogers3d504072014-03-01 09:16:49 -08001450 if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) {
1451 PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001452 return false;
1453 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001454 oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_);
Ian Rogers3d504072014-03-01 09:16:49 -08001455 if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) {
1456 PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation();
Brian Carlstrom89521892011-12-07 22:05:07 -08001457 return false;
1458 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001459 oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_);
Ian Rogers3d504072014-03-01 09:16:49 -08001460 if (!out->WriteFully(&methods_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001461 sizeof(methods_offsets_[0]) * methods_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001462 PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001463 return false;
1464 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001465 oat_writer->size_oat_dex_file_methods_offsets_ +=
1466 sizeof(methods_offsets_[0]) * methods_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001467 return true;
1468}
1469
Brian Carlstromba150c32013-08-27 17:31:03 -07001470OatWriter::OatClass::OatClass(size_t offset,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001471 const std::vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -07001472 uint32_t num_non_null_compiled_methods,
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001473 mirror::Class::Status status)
1474 : compiled_methods_(compiled_methods) {
1475 uint32_t num_methods = compiled_methods.size();
Brian Carlstromba150c32013-08-27 17:31:03 -07001476 CHECK_LE(num_non_null_compiled_methods, num_methods);
1477
Brian Carlstrom265091e2013-01-30 14:08:26 -08001478 offset_ = offset;
Brian Carlstromba150c32013-08-27 17:31:03 -07001479 oat_method_offsets_offsets_from_oat_class_.resize(num_methods);
1480
1481 // Since both kOatClassNoneCompiled and kOatClassAllCompiled could
1482 // apply when there are 0 methods, we just arbitrarily say that 0
1483 // methods means kOatClassNoneCompiled and that we won't use
1484 // kOatClassAllCompiled unless there is at least one compiled
1485 // method. This means in an interpretter only system, we can assert
1486 // that all classes are kOatClassNoneCompiled.
1487 if (num_non_null_compiled_methods == 0) {
1488 type_ = kOatClassNoneCompiled;
1489 } else if (num_non_null_compiled_methods == num_methods) {
1490 type_ = kOatClassAllCompiled;
1491 } else {
1492 type_ = kOatClassSomeCompiled;
1493 }
1494
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001495 status_ = status;
Brian Carlstromba150c32013-08-27 17:31:03 -07001496 method_offsets_.resize(num_non_null_compiled_methods);
Vladimir Marko8a630572014-04-09 18:45:35 +01001497 method_headers_.resize(num_non_null_compiled_methods);
Brian Carlstromba150c32013-08-27 17:31:03 -07001498
1499 uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_);
1500 if (type_ == kOatClassSomeCompiled) {
1501 method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator());
1502 method_bitmap_size_ = method_bitmap_->GetSizeOf();
1503 oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_);
1504 oat_method_offsets_offset_from_oat_class += method_bitmap_size_;
1505 } else {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001506 method_bitmap_ = nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001507 method_bitmap_size_ = 0;
1508 }
1509
1510 for (size_t i = 0; i < num_methods; i++) {
Vladimir Marko96c6ab92014-04-08 14:00:50 +01001511 CompiledMethod* compiled_method = compiled_methods_[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001512 if (compiled_method == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001513 oat_method_offsets_offsets_from_oat_class_[i] = 0;
1514 } else {
1515 oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class;
1516 oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets);
1517 if (type_ == kOatClassSomeCompiled) {
1518 method_bitmap_->SetBit(i);
1519 }
1520 }
1521 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001522}
1523
Brian Carlstromba150c32013-08-27 17:31:03 -07001524OatWriter::OatClass::~OatClass() {
Mathieu Chartier661974a2014-01-09 11:23:53 -08001525 delete method_bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -07001526}
1527
Brian Carlstrom265091e2013-01-30 14:08:26 -08001528size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader(
1529 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001530 uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_);
1531 if (method_offset == 0) {
1532 return 0;
1533 }
1534 return offset_ + method_offset;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001535}
1536
1537size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass(
1538 size_t class_def_method_index_) const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001539 return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_];
Brian Carlstrom265091e2013-01-30 14:08:26 -08001540}
1541
1542size_t OatWriter::OatClass::SizeOf() const {
Brian Carlstromba150c32013-08-27 17:31:03 -07001543 return sizeof(status_)
1544 + sizeof(type_)
1545 + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_))
1546 + method_bitmap_size_
1547 + (sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001548}
1549
Ian Rogers3d504072014-03-01 09:16:49 -08001550void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const {
1551 oat_header->UpdateChecksum(&status_, sizeof(status_));
1552 oat_header->UpdateChecksum(&type_, sizeof(type_));
Brian Carlstromba150c32013-08-27 17:31:03 -07001553 if (method_bitmap_size_ != 0) {
1554 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001555 oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_));
1556 oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_);
Brian Carlstromba150c32013-08-27 17:31:03 -07001557 }
Ian Rogers3d504072014-03-01 09:16:49 -08001558 oat_header->UpdateChecksum(&method_offsets_[0],
1559 sizeof(method_offsets_[0]) * method_offsets_.size());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001560}
1561
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001562bool OatWriter::OatClass::Write(OatWriter* oat_writer,
Ian Rogers3d504072014-03-01 09:16:49 -08001563 OutputStream* out,
Brian Carlstromc50d8e12013-07-23 22:35:16 -07001564 const size_t file_offset) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001565 DCHECK_OFFSET_();
Ian Rogers3d504072014-03-01 09:16:49 -08001566 if (!out->WriteFully(&status_, sizeof(status_))) {
1567 PLOG(ERROR) << "Failed to write class status to " << out->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001568 return false;
1569 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001570 oat_writer->size_oat_class_status_ += sizeof(status_);
Ian Rogers3d504072014-03-01 09:16:49 -08001571 if (!out->WriteFully(&type_, sizeof(type_))) {
1572 PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001573 return false;
1574 }
1575 oat_writer->size_oat_class_type_ += sizeof(type_);
1576 if (method_bitmap_size_ != 0) {
1577 CHECK_EQ(kOatClassSomeCompiled, type_);
Ian Rogers3d504072014-03-01 09:16:49 -08001578 if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) {
1579 PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001580 return false;
1581 }
1582 oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_);
Ian Rogers3d504072014-03-01 09:16:49 -08001583 if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) {
1584 PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001585 return false;
1586 }
1587 oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_;
1588 }
Ian Rogers3d504072014-03-01 09:16:49 -08001589 if (!out->WriteFully(&method_offsets_[0],
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001590 sizeof(method_offsets_[0]) * method_offsets_.size())) {
Ian Rogers3d504072014-03-01 09:16:49 -08001591 PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001592 return false;
1593 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -07001594 oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001595 return true;
1596}
1597
Brian Carlstrome24fa612011-09-29 00:53:55 -07001598} // namespace art