Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 16 | |
| 17 | #include "oat_writer.h" |
| 18 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 19 | #include <zlib.h> |
| 20 | |
Vladimir Marko | c74658b | 2015-03-31 10:26:41 +0100 | [diff] [blame] | 21 | #include "arch/arm64/instruction_set_features_arm64.h" |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 22 | #include "base/allocator.h" |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 23 | #include "base/bit_vector.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 24 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 25 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 26 | #include "class_linker.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 27 | #include "compiled_class.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 28 | #include "compiled_method.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 29 | #include "dex_file-inl.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 30 | #include "dex/verification_results.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 31 | #include "driver/compiler_driver.h" |
| 32 | #include "driver/compiler_options.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 33 | #include "gc/space/space.h" |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 34 | #include "image_writer.h" |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 35 | #include "linker/relative_patcher.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 36 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 37 | #include "mirror/array.h" |
| 38 | #include "mirror/class_loader.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 39 | #include "mirror/object-inl.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 40 | #include "os.h" |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 41 | #include "output_stream.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 42 | #include "safe_map.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 43 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 44 | #include "handle_scope-inl.h" |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 45 | #include "verifier/method_verifier.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 46 | |
| 47 | namespace art { |
| 48 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 49 | #define DCHECK_OFFSET() \ |
| 50 | DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out->Seek(0, kSeekCurrent)) \ |
| 51 | << "file_offset=" << file_offset << " relative_offset=" << relative_offset |
| 52 | |
| 53 | #define DCHECK_OFFSET_() \ |
| 54 | DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out->Seek(0, kSeekCurrent)) \ |
| 55 | << "file_offset=" << file_offset << " offset_=" << offset_ |
| 56 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 57 | OatWriter::OatWriter(const std::vector<const DexFile*>& dex_files, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 58 | uint32_t image_file_location_oat_checksum, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 59 | uintptr_t image_file_location_oat_begin, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 60 | int32_t image_patch_delta, |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 61 | const CompilerDriver* compiler, |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 62 | ImageWriter* image_writer, |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 63 | TimingLogger* timings, |
| 64 | SafeMap<std::string, std::string>* key_value_store) |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 65 | : compiler_driver_(compiler), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 66 | image_writer_(image_writer), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 67 | dex_files_(&dex_files), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 68 | size_(0u), |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 69 | bss_size_(0u), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 70 | oat_data_offset_(0u), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 71 | image_file_location_oat_checksum_(image_file_location_oat_checksum), |
| 72 | image_file_location_oat_begin_(image_file_location_oat_begin), |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 73 | image_patch_delta_(image_patch_delta), |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 74 | key_value_store_(key_value_store), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 75 | oat_header_(NULL), |
| 76 | size_dex_file_alignment_(0), |
| 77 | size_executable_offset_alignment_(0), |
| 78 | size_oat_header_(0), |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 79 | size_oat_header_key_value_store_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 80 | size_dex_file_(0), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 81 | size_interpreter_to_interpreter_bridge_(0), |
| 82 | size_interpreter_to_compiled_code_bridge_(0), |
| 83 | size_jni_dlsym_lookup_(0), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 84 | size_quick_generic_jni_trampoline_(0), |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 85 | size_quick_imt_conflict_trampoline_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 86 | size_quick_resolution_trampoline_(0), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 87 | size_quick_to_interpreter_bridge_(0), |
| 88 | size_trampoline_alignment_(0), |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 89 | size_method_header_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 90 | size_code_(0), |
| 91 | size_code_alignment_(0), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 92 | size_relative_call_thunks_(0), |
Vladimir Marko | c74658b | 2015-03-31 10:26:41 +0100 | [diff] [blame] | 93 | size_misc_thunks_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 94 | size_mapping_table_(0), |
| 95 | size_vmap_table_(0), |
| 96 | size_gc_map_(0), |
| 97 | size_oat_dex_file_location_size_(0), |
| 98 | size_oat_dex_file_location_data_(0), |
| 99 | size_oat_dex_file_location_checksum_(0), |
| 100 | size_oat_dex_file_offset_(0), |
| 101 | size_oat_dex_file_methods_offsets_(0), |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 102 | size_oat_class_type_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 103 | size_oat_class_status_(0), |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 104 | size_oat_class_method_bitmaps_(0), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 105 | size_oat_class_method_offsets_(0), |
| 106 | method_offset_map_() { |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 107 | CHECK(key_value_store != nullptr); |
| 108 | |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 109 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
| 110 | const InstructionSetFeatures* features = compiler_driver_->GetInstructionSetFeatures(); |
| 111 | relative_patcher_ = linker::RelativePatcher::Create(instruction_set, features, |
| 112 | &method_offset_map_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 113 | |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 114 | size_t offset; |
| 115 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 116 | TimingLogger::ScopedTiming split("InitOatHeader", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 117 | offset = InitOatHeader(); |
| 118 | } |
| 119 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 120 | TimingLogger::ScopedTiming split("InitOatDexFiles", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 121 | offset = InitOatDexFiles(offset); |
| 122 | } |
| 123 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 124 | TimingLogger::ScopedTiming split("InitDexFiles", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 125 | offset = InitDexFiles(offset); |
| 126 | } |
| 127 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 128 | TimingLogger::ScopedTiming split("InitOatClasses", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 129 | offset = InitOatClasses(offset); |
| 130 | } |
| 131 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 132 | TimingLogger::ScopedTiming split("InitOatMaps", timings); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 133 | offset = InitOatMaps(offset); |
| 134 | } |
| 135 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 136 | TimingLogger::ScopedTiming split("InitOatCode", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 137 | offset = InitOatCode(offset); |
| 138 | } |
| 139 | { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 140 | TimingLogger::ScopedTiming split("InitOatCodeDexFiles", timings); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 141 | offset = InitOatCodeDexFiles(offset); |
| 142 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 143 | size_ = offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 144 | |
| 145 | CHECK_EQ(dex_files_->size(), oat_dex_files_.size()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 146 | CHECK_EQ(compiler->IsImage(), image_writer_ != nullptr); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 147 | CHECK_EQ(compiler->IsImage(), |
| 148 | key_value_store_->find(OatHeader::kImageLocationKey) == key_value_store_->end()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 149 | CHECK_ALIGNED(image_patch_delta_, kPageSize); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 152 | OatWriter::~OatWriter() { |
| 153 | delete oat_header_; |
| 154 | STLDeleteElements(&oat_dex_files_); |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 155 | STLDeleteElements(&oat_classes_); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 158 | struct OatWriter::GcMapDataAccess { |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 159 | static const SwapVector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 160 | return compiled_method->GetGcMap(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE { |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 164 | uint32_t offset = oat_class->method_headers_[method_offsets_index].gc_map_offset_; |
| 165 | return offset == 0u ? 0u : |
| 166 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset) |
| 170 | ALWAYS_INLINE { |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 171 | oat_class->method_headers_[method_offsets_index].gc_map_offset_ = |
| 172 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 175 | static const char* Name() { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 176 | return "GC map"; |
| 177 | } |
| 178 | }; |
| 179 | |
| 180 | struct OatWriter::MappingTableDataAccess { |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 181 | static const SwapVector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE { |
Nicolas Geoffray | 376b2bb | 2014-12-09 14:26:32 +0000 | [diff] [blame] | 182 | return compiled_method->GetMappingTable(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE { |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 186 | uint32_t offset = oat_class->method_headers_[method_offsets_index].mapping_table_offset_; |
| 187 | return offset == 0u ? 0u : |
| 188 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset) |
| 192 | ALWAYS_INLINE { |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 193 | oat_class->method_headers_[method_offsets_index].mapping_table_offset_ = |
| 194 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 197 | static const char* Name() { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 198 | return "mapping table"; |
| 199 | } |
| 200 | }; |
| 201 | |
| 202 | struct OatWriter::VmapTableDataAccess { |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 203 | static const SwapVector<uint8_t>* GetData(const CompiledMethod* compiled_method) ALWAYS_INLINE { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 204 | return compiled_method->GetVmapTable(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static uint32_t GetOffset(OatClass* oat_class, size_t method_offsets_index) ALWAYS_INLINE { |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 208 | uint32_t offset = oat_class->method_headers_[method_offsets_index].vmap_table_offset_; |
| 209 | return offset == 0u ? 0u : |
| 210 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static void SetOffset(OatClass* oat_class, size_t method_offsets_index, uint32_t offset) |
| 214 | ALWAYS_INLINE { |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 215 | oat_class->method_headers_[method_offsets_index].vmap_table_offset_ = |
| 216 | (oat_class->method_offsets_[method_offsets_index].code_offset_ & ~1) - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 219 | static const char* Name() { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 220 | return "vmap table"; |
| 221 | } |
| 222 | }; |
| 223 | |
| 224 | class OatWriter::DexMethodVisitor { |
| 225 | public: |
| 226 | DexMethodVisitor(OatWriter* writer, size_t offset) |
| 227 | : writer_(writer), |
| 228 | offset_(offset), |
| 229 | dex_file_(nullptr), |
| 230 | class_def_index_(DexFile::kDexNoIndex) { |
| 231 | } |
| 232 | |
| 233 | virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) { |
| 234 | DCHECK(dex_file_ == nullptr); |
| 235 | DCHECK_EQ(class_def_index_, DexFile::kDexNoIndex); |
| 236 | dex_file_ = dex_file; |
| 237 | class_def_index_ = class_def_index; |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0; |
| 242 | |
| 243 | virtual bool EndClass() { |
| 244 | if (kIsDebugBuild) { |
| 245 | dex_file_ = nullptr; |
| 246 | class_def_index_ = DexFile::kDexNoIndex; |
| 247 | } |
| 248 | return true; |
| 249 | } |
| 250 | |
| 251 | size_t GetOffset() const { |
| 252 | return offset_; |
| 253 | } |
| 254 | |
| 255 | protected: |
| 256 | virtual ~DexMethodVisitor() { } |
| 257 | |
| 258 | OatWriter* const writer_; |
| 259 | |
| 260 | // The offset is usually advanced for each visited method by the derived class. |
| 261 | size_t offset_; |
| 262 | |
| 263 | // The dex file and class def index are set in StartClass(). |
| 264 | const DexFile* dex_file_; |
| 265 | size_t class_def_index_; |
| 266 | }; |
| 267 | |
| 268 | class OatWriter::OatDexMethodVisitor : public DexMethodVisitor { |
| 269 | public: |
| 270 | OatDexMethodVisitor(OatWriter* writer, size_t offset) |
| 271 | : DexMethodVisitor(writer, offset), |
| 272 | oat_class_index_(0u), |
| 273 | method_offsets_index_(0u) { |
| 274 | } |
| 275 | |
| 276 | bool StartClass(const DexFile* dex_file, size_t class_def_index) { |
| 277 | DexMethodVisitor::StartClass(dex_file, class_def_index); |
| 278 | DCHECK_LT(oat_class_index_, writer_->oat_classes_.size()); |
| 279 | method_offsets_index_ = 0u; |
| 280 | return true; |
| 281 | } |
| 282 | |
| 283 | bool EndClass() { |
| 284 | ++oat_class_index_; |
| 285 | return DexMethodVisitor::EndClass(); |
| 286 | } |
| 287 | |
| 288 | protected: |
| 289 | size_t oat_class_index_; |
| 290 | size_t method_offsets_index_; |
| 291 | }; |
| 292 | |
| 293 | class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor { |
| 294 | public: |
| 295 | InitOatClassesMethodVisitor(OatWriter* writer, size_t offset) |
| 296 | : DexMethodVisitor(writer, offset), |
| 297 | compiled_methods_(), |
| 298 | num_non_null_compiled_methods_(0u) { |
| 299 | compiled_methods_.reserve(256u); |
| 300 | } |
| 301 | |
| 302 | bool StartClass(const DexFile* dex_file, size_t class_def_index) { |
| 303 | DexMethodVisitor::StartClass(dex_file, class_def_index); |
| 304 | compiled_methods_.clear(); |
| 305 | num_non_null_compiled_methods_ = 0u; |
| 306 | return true; |
| 307 | } |
| 308 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 309 | bool VisitMethod(size_t class_def_method_index ATTRIBUTE_UNUSED, const ClassDataItemIterator& it) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 310 | // Fill in the compiled_methods_ array for methods that have a |
| 311 | // CompiledMethod. We track the number of non-null entries in |
| 312 | // num_non_null_compiled_methods_ since we only want to allocate |
| 313 | // OatMethodOffsets for the compiled methods. |
| 314 | uint32_t method_idx = it.GetMemberIndex(); |
| 315 | CompiledMethod* compiled_method = |
| 316 | writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx)); |
| 317 | compiled_methods_.push_back(compiled_method); |
| 318 | if (compiled_method != nullptr) { |
| 319 | ++num_non_null_compiled_methods_; |
| 320 | } |
| 321 | return true; |
| 322 | } |
| 323 | |
| 324 | bool EndClass() { |
| 325 | ClassReference class_ref(dex_file_, class_def_index_); |
| 326 | CompiledClass* compiled_class = writer_->compiler_driver_->GetCompiledClass(class_ref); |
| 327 | mirror::Class::Status status; |
| 328 | if (compiled_class != NULL) { |
| 329 | status = compiled_class->GetStatus(); |
| 330 | } else if (writer_->compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) { |
| 331 | status = mirror::Class::kStatusError; |
| 332 | } else { |
| 333 | status = mirror::Class::kStatusNotReady; |
| 334 | } |
| 335 | |
| 336 | OatClass* oat_class = new OatClass(offset_, compiled_methods_, |
| 337 | num_non_null_compiled_methods_, status); |
| 338 | writer_->oat_classes_.push_back(oat_class); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 339 | oat_class->UpdateChecksum(writer_->oat_header_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 340 | offset_ += oat_class->SizeOf(); |
| 341 | return DexMethodVisitor::EndClass(); |
| 342 | } |
| 343 | |
| 344 | private: |
| 345 | std::vector<CompiledMethod*> compiled_methods_; |
| 346 | size_t num_non_null_compiled_methods_; |
| 347 | }; |
| 348 | |
| 349 | class OatWriter::InitCodeMethodVisitor : public OatDexMethodVisitor { |
| 350 | public: |
| 351 | InitCodeMethodVisitor(OatWriter* writer, size_t offset) |
| 352 | : OatDexMethodVisitor(writer, offset) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 353 | writer_->absolute_patch_locations_.reserve( |
| 354 | writer_->compiler_driver_->GetNonRelativeLinkerPatchCount()); |
| 355 | } |
| 356 | |
| 357 | bool EndClass() { |
| 358 | OatDexMethodVisitor::EndClass(); |
| 359 | if (oat_class_index_ == writer_->oat_classes_.size()) { |
Vladimir Marko | 4d23c9d | 2015-04-01 23:03:09 +0100 | [diff] [blame] | 360 | offset_ = writer_->relative_patcher_->ReserveSpace(offset_, nullptr, |
| 361 | MethodReference(nullptr, 0u)); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 362 | } |
| 363 | return true; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
| 367 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 368 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 369 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 370 | |
| 371 | if (compiled_method != nullptr) { |
| 372 | // Derived from CompiledMethod. |
| 373 | uint32_t quick_code_offset = 0; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 374 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 375 | const SwapVector<uint8_t>* quick_code = compiled_method->GetQuickCode(); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 376 | CHECK(quick_code != nullptr); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 377 | uint32_t code_size = quick_code->size() * sizeof(uint8_t); |
| 378 | CHECK_NE(code_size, 0U); |
| 379 | uint32_t thumb_offset = compiled_method->CodeDelta(); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 380 | |
| 381 | // Deduplicate code arrays. |
Vladimir Marko | c74658b | 2015-03-31 10:26:41 +0100 | [diff] [blame] | 382 | bool deduped = false; |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 383 | auto lb = dedupe_map_.lower_bound(compiled_method); |
| 384 | if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(compiled_method, lb->first)) { |
| 385 | quick_code_offset = lb->second; |
| 386 | deduped = true; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 387 | } else { |
Vladimir Marko | 4d23c9d | 2015-04-01 23:03:09 +0100 | [diff] [blame] | 388 | offset_ = writer_->relative_patcher_->ReserveSpace( |
| 389 | offset_, compiled_method, MethodReference(dex_file_, it.GetMemberIndex())); |
Vladimir Marko | c74658b | 2015-03-31 10:26:41 +0100 | [diff] [blame] | 390 | offset_ = compiled_method->AlignCode(offset_); |
| 391 | DCHECK_ALIGNED_PARAM(offset_, |
| 392 | GetInstructionSetAlignment(compiled_method->GetInstructionSet())); |
| 393 | quick_code_offset = offset_ + sizeof(OatQuickMethodHeader) + thumb_offset; |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 394 | dedupe_map_.PutBefore(lb, compiled_method, quick_code_offset); |
| 395 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 396 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 397 | MethodReference method_ref(dex_file_, it.GetMemberIndex()); |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 398 | 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 Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 401 | // 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 Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 406 | writer_->method_offset_map_.map.PutBefore(method_lb, method_ref, quick_code_offset); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 407 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 408 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 409 | // 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 Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 436 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 437 | 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 Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 445 | if (!patch.IsPcRelative()) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 446 | writer_->absolute_patch_locations_.push_back(base_loc + patch.LiteralOffset()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 447 | } |
| 448 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 449 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 450 | } |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 451 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 452 | if (writer_->compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols()) { |
| 453 | // Record debug information for this function if we are doing that. |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 454 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 455 | std::string name = PrettyMethod(it.GetMemberIndex(), *dex_file_, true); |
| 456 | if (deduped) { |
| 457 | // TODO We should place the DEDUPED tag on the first instance of a deduplicated symbol |
| 458 | // so that it will show up in a debuggerd crash report. |
| 459 | name += " [ DEDUPED ]"; |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 460 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 461 | |
| 462 | const uint32_t quick_code_start = quick_code_offset - |
| 463 | writer_->oat_header_->GetExecutableOffset(); |
| 464 | const DexFile::CodeItem *code_item = it.GetMethodCodeItem(); |
| 465 | writer_->method_info_.push_back(DebugInfo(name, |
| 466 | dex_file_->GetSourceFile(dex_file_->GetClassDef(class_def_index_)), |
| 467 | quick_code_start, quick_code_start + code_size, |
| 468 | code_item == nullptr ? nullptr : dex_file_->GetDebugInfoStream(code_item), |
| 469 | compiled_method)); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 470 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 471 | |
| 472 | if (kIsDebugBuild) { |
| 473 | // We expect GC maps except when the class hasn't been verified or the method is native. |
| 474 | const CompilerDriver* compiler_driver = writer_->compiler_driver_; |
| 475 | ClassReference class_ref(dex_file_, class_def_index_); |
| 476 | CompiledClass* compiled_class = compiler_driver->GetCompiledClass(class_ref); |
| 477 | mirror::Class::Status status; |
| 478 | if (compiled_class != NULL) { |
| 479 | status = compiled_class->GetStatus(); |
| 480 | } else if (compiler_driver->GetVerificationResults()->IsClassRejected(class_ref)) { |
| 481 | status = mirror::Class::kStatusError; |
| 482 | } else { |
| 483 | status = mirror::Class::kStatusNotReady; |
| 484 | } |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 485 | const SwapVector<uint8_t>* gc_map = compiled_method->GetGcMap(); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 486 | if (gc_map != nullptr) { |
| 487 | size_t gc_map_size = gc_map->size() * sizeof(gc_map[0]); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 488 | bool is_native = it.MemberIsNative(); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 489 | CHECK(gc_map_size != 0 || is_native || status < mirror::Class::kStatusVerified) |
| 490 | << gc_map << " " << gc_map_size << " " << (is_native ? "true" : "false") << " " |
| 491 | << (status < mirror::Class::kStatusVerified) << " " << status << " " |
| 492 | << PrettyMethod(it.GetMemberIndex(), *dex_file_); |
| 493 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 497 | OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_]; |
| 498 | offsets->code_offset_ = quick_code_offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 499 | ++method_offsets_index_; |
| 500 | } |
| 501 | |
| 502 | return true; |
| 503 | } |
| 504 | |
| 505 | private: |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 506 | struct CodeOffsetsKeyComparator { |
| 507 | bool operator()(const CompiledMethod* lhs, const CompiledMethod* rhs) const { |
| 508 | if (lhs->GetQuickCode() != rhs->GetQuickCode()) { |
| 509 | return lhs->GetQuickCode() < rhs->GetQuickCode(); |
| 510 | } |
| 511 | // If the code is the same, all other fields are likely to be the same as well. |
| 512 | if (UNLIKELY(lhs->GetMappingTable() != rhs->GetMappingTable())) { |
| 513 | return lhs->GetMappingTable() < rhs->GetMappingTable(); |
| 514 | } |
| 515 | if (UNLIKELY(lhs->GetVmapTable() != rhs->GetVmapTable())) { |
| 516 | return lhs->GetVmapTable() < rhs->GetVmapTable(); |
| 517 | } |
| 518 | if (UNLIKELY(lhs->GetGcMap() != rhs->GetGcMap())) { |
| 519 | return lhs->GetGcMap() < rhs->GetGcMap(); |
| 520 | } |
| 521 | const auto& lhs_patches = lhs->GetPatches(); |
| 522 | const auto& rhs_patches = rhs->GetPatches(); |
| 523 | if (UNLIKELY(lhs_patches.size() != rhs_patches.size())) { |
| 524 | return lhs_patches.size() < rhs_patches.size(); |
| 525 | } |
| 526 | auto rit = rhs_patches.begin(); |
| 527 | for (const LinkerPatch& lpatch : lhs_patches) { |
| 528 | if (UNLIKELY(!(lpatch == *rit))) { |
| 529 | return lpatch < *rit; |
| 530 | } |
| 531 | ++rit; |
| 532 | } |
| 533 | return false; |
| 534 | } |
| 535 | }; |
| 536 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 537 | // Deduplication is already done on a pointer basis by the compiler driver, |
| 538 | // so we can simply compare the pointers to find out if things are duplicated. |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 539 | SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 540 | }; |
| 541 | |
| 542 | template <typename DataAccess> |
| 543 | class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor { |
| 544 | public: |
| 545 | InitMapMethodVisitor(OatWriter* writer, size_t offset) |
| 546 | : OatDexMethodVisitor(writer, offset) { |
| 547 | } |
| 548 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 549 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it ATTRIBUTE_UNUSED) |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 550 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 551 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 552 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 553 | |
| 554 | if (compiled_method != nullptr) { |
| 555 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 556 | DCHECK_EQ(DataAccess::GetOffset(oat_class, method_offsets_index_), 0u); |
| 557 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 558 | const SwapVector<uint8_t>* map = DataAccess::GetData(compiled_method); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 559 | uint32_t map_size = map == nullptr ? 0 : map->size() * sizeof((*map)[0]); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 560 | if (map_size != 0u) { |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 561 | auto lb = dedupe_map_.lower_bound(map); |
| 562 | if (lb != dedupe_map_.end() && !dedupe_map_.key_comp()(map, lb->first)) { |
| 563 | DataAccess::SetOffset(oat_class, method_offsets_index_, lb->second); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 564 | } else { |
| 565 | DataAccess::SetOffset(oat_class, method_offsets_index_, offset_); |
Vladimir Marko | bd72fc1 | 2014-07-09 16:06:40 +0100 | [diff] [blame] | 566 | dedupe_map_.PutBefore(lb, map, offset_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 567 | offset_ += map_size; |
| 568 | writer_->oat_header_->UpdateChecksum(&(*map)[0], map_size); |
| 569 | } |
| 570 | } |
| 571 | ++method_offsets_index_; |
| 572 | } |
| 573 | |
| 574 | return true; |
| 575 | } |
| 576 | |
| 577 | private: |
| 578 | // Deduplication is already done on a pointer basis by the compiler driver, |
| 579 | // so we can simply compare the pointers to find out if things are duplicated. |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 580 | SafeMap<const SwapVector<uint8_t>*, uint32_t> dedupe_map_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 581 | }; |
| 582 | |
| 583 | class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor { |
| 584 | public: |
| 585 | InitImageMethodVisitor(OatWriter* writer, size_t offset) |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 586 | : OatDexMethodVisitor(writer, offset), |
| 587 | pointer_size_(GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet())) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
| 591 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 592 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 593 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 594 | |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 595 | OatMethodOffsets offsets(0u); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 596 | if (compiled_method != nullptr) { |
| 597 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 598 | offsets = oat_class->method_offsets_[method_offsets_index_]; |
| 599 | ++method_offsets_index_; |
| 600 | } |
| 601 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 602 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 603 | InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_)); |
| 604 | // Unchecked as we hold mutator_lock_ on entry. |
| 605 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 606 | StackHandleScope<1> hs(soa.Self()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 607 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache(*dex_file_))); |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 608 | mirror::ArtMethod* method = linker->ResolveMethod(*dex_file_, it.GetMemberIndex(), dex_cache, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 609 | NullHandle<mirror::ClassLoader>(), |
| 610 | NullHandle<mirror::ArtMethod>(), |
| 611 | invoke_type); |
Andreas Gampe | d9efea6 | 2014-07-21 22:56:08 -0700 | [diff] [blame] | 612 | if (method == nullptr) { |
| 613 | LOG(ERROR) << "Unexpected failure to resolve a method: " |
| 614 | << PrettyMethod(it.GetMemberIndex(), *dex_file_, true); |
| 615 | soa.Self()->AssertPendingException(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 616 | mirror::Throwable* exc = soa.Self()->GetException(); |
Andreas Gampe | d9efea6 | 2014-07-21 22:56:08 -0700 | [diff] [blame] | 617 | std::string dump = exc->Dump(); |
| 618 | LOG(FATAL) << dump; |
| 619 | } |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 620 | method->SetEntryPointFromQuickCompiledCodePtrSize(reinterpret_cast<void*>(offsets.code_offset_), |
| 621 | pointer_size_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 622 | |
| 623 | return true; |
| 624 | } |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 625 | |
| 626 | protected: |
| 627 | const size_t pointer_size_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 628 | }; |
| 629 | |
| 630 | class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor { |
| 631 | public: |
| 632 | WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset, |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 633 | size_t relative_offset) SHARED_LOCK_FUNCTION(Locks::mutator_lock_) |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 634 | : OatDexMethodVisitor(writer, relative_offset), |
| 635 | out_(out), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 636 | file_offset_(file_offset), |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 637 | soa_(Thread::Current()), |
| 638 | no_thread_suspension_(soa_.Self(), "OatWriter patching"), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 639 | class_linker_(Runtime::Current()->GetClassLinker()), |
| 640 | dex_cache_(nullptr) { |
| 641 | if (writer_->image_writer_ != nullptr) { |
| 642 | // If we're creating the image, the address space must be ready so that we can apply patches. |
| 643 | CHECK(writer_->image_writer_->IsImageAddressSpaceReady()); |
| 644 | patched_code_.reserve(16 * KB); |
| 645 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 646 | } |
| 647 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 648 | ~WriteCodeMethodVisitor() UNLOCK_FUNCTION(Locks::mutator_lock_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | bool StartClass(const DexFile* dex_file, size_t class_def_index) |
| 652 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 653 | OatDexMethodVisitor::StartClass(dex_file, class_def_index); |
| 654 | if (dex_cache_ == nullptr || dex_cache_->GetDexFile() != dex_file) { |
| 655 | dex_cache_ = class_linker_->FindDexCache(*dex_file); |
| 656 | } |
| 657 | return true; |
| 658 | } |
| 659 | |
| 660 | bool EndClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 661 | bool result = OatDexMethodVisitor::EndClass(); |
| 662 | if (oat_class_index_ == writer_->oat_classes_.size()) { |
| 663 | DCHECK(result); // OatDexMethodVisitor::EndClass() never fails. |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 664 | offset_ = writer_->relative_patcher_->WriteThunks(out_, offset_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 665 | if (UNLIKELY(offset_ == 0u)) { |
| 666 | PLOG(ERROR) << "Failed to write final relative call thunks"; |
| 667 | result = false; |
| 668 | } |
| 669 | } |
| 670 | return result; |
| 671 | } |
| 672 | |
| 673 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
| 674 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 675 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 676 | const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 677 | |
| 678 | if (compiled_method != NULL) { // ie. not an abstract method |
| 679 | size_t file_offset = file_offset_; |
| 680 | OutputStream* out = out_; |
| 681 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 682 | const SwapVector<uint8_t>* quick_code = compiled_method->GetQuickCode(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 683 | if (quick_code != nullptr) { |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 684 | // Need a wrapper if we create a copy for patching. |
| 685 | ArrayRef<const uint8_t> wrapped(*quick_code); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 686 | uint32_t code_size = quick_code->size() * sizeof(uint8_t); |
| 687 | CHECK_NE(code_size, 0U); |
| 688 | |
| 689 | // Deduplicate code arrays. |
| 690 | const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_]; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 691 | if (method_offsets.code_offset_ >= offset_) { |
Vladimir Marko | c74658b | 2015-03-31 10:26:41 +0100 | [diff] [blame] | 692 | offset_ = writer_->relative_patcher_->WriteThunks(out, offset_); |
| 693 | if (offset_ == 0u) { |
| 694 | ReportWriteFailure("relative call thunk", it); |
| 695 | return false; |
| 696 | } |
| 697 | uint32_t aligned_offset = compiled_method->AlignCode(offset_); |
| 698 | uint32_t aligned_code_delta = aligned_offset - offset_; |
| 699 | if (aligned_code_delta != 0) { |
| 700 | if (!writer_->WriteCodeAlignment(out, aligned_code_delta)) { |
| 701 | ReportWriteFailure("code alignment padding", it); |
| 702 | return false; |
| 703 | } |
| 704 | offset_ += aligned_code_delta; |
| 705 | DCHECK_OFFSET_(); |
| 706 | } |
| 707 | DCHECK_ALIGNED_PARAM(offset_, |
| 708 | GetInstructionSetAlignment(compiled_method->GetInstructionSet())); |
| 709 | DCHECK_EQ(method_offsets.code_offset_, |
| 710 | offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta()) |
| 711 | << PrettyMethod(it.GetMemberIndex(), *dex_file_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 712 | const OatQuickMethodHeader& method_header = |
| 713 | oat_class->method_headers_[method_offsets_index_]; |
| 714 | writer_->oat_header_->UpdateChecksum(&method_header, sizeof(method_header)); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 715 | if (!out->WriteFully(&method_header, sizeof(method_header))) { |
| 716 | ReportWriteFailure("method header", it); |
| 717 | return false; |
| 718 | } |
| 719 | writer_->size_method_header_ += sizeof(method_header); |
| 720 | offset_ += sizeof(method_header); |
| 721 | DCHECK_OFFSET_(); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 722 | |
| 723 | if (!compiled_method->GetPatches().empty()) { |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 724 | patched_code_.assign(quick_code->begin(), quick_code->end()); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 725 | wrapped = ArrayRef<const uint8_t>(patched_code_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 726 | for (const LinkerPatch& patch : compiled_method->GetPatches()) { |
| 727 | if (patch.Type() == kLinkerPatchCallRelative) { |
| 728 | // NOTE: Relative calls across oat files are not supported. |
| 729 | uint32_t target_offset = GetTargetOffset(patch); |
| 730 | uint32_t literal_offset = patch.LiteralOffset(); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 731 | writer_->relative_patcher_->PatchCall(&patched_code_, literal_offset, |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 732 | offset_ + literal_offset, target_offset); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 733 | } else if (patch.Type() == kLinkerPatchDexCacheArray) { |
| 734 | uint32_t target_offset = GetDexCacheOffset(patch); |
| 735 | uint32_t literal_offset = patch.LiteralOffset(); |
| 736 | writer_->relative_patcher_->PatchDexCacheReference(&patched_code_, patch, |
| 737 | offset_ + literal_offset, |
| 738 | target_offset); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 739 | } else if (patch.Type() == kLinkerPatchCall) { |
| 740 | uint32_t target_offset = GetTargetOffset(patch); |
| 741 | PatchCodeAddress(&patched_code_, patch.LiteralOffset(), target_offset); |
| 742 | } else if (patch.Type() == kLinkerPatchMethod) { |
| 743 | mirror::ArtMethod* method = GetTargetMethod(patch); |
| 744 | PatchObjectAddress(&patched_code_, patch.LiteralOffset(), method); |
| 745 | } else if (patch.Type() == kLinkerPatchType) { |
| 746 | mirror::Class* type = GetTargetType(patch); |
| 747 | PatchObjectAddress(&patched_code_, patch.LiteralOffset(), type); |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 752 | writer_->oat_header_->UpdateChecksum(wrapped.data(), code_size); |
| 753 | if (!out->WriteFully(wrapped.data(), code_size)) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 754 | ReportWriteFailure("method code", it); |
| 755 | return false; |
| 756 | } |
| 757 | writer_->size_code_ += code_size; |
| 758 | offset_ += code_size; |
| 759 | } |
| 760 | DCHECK_OFFSET_(); |
| 761 | } |
| 762 | ++method_offsets_index_; |
| 763 | } |
| 764 | |
| 765 | return true; |
| 766 | } |
| 767 | |
| 768 | private: |
| 769 | OutputStream* const out_; |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 770 | const size_t file_offset_; |
| 771 | const ScopedObjectAccess soa_; |
| 772 | const ScopedAssertNoThreadSuspension no_thread_suspension_; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 773 | ClassLinker* const class_linker_; |
| 774 | mirror::DexCache* dex_cache_; |
| 775 | std::vector<uint8_t> patched_code_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 776 | |
| 777 | void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) { |
| 778 | PLOG(ERROR) << "Failed to write " << what << " for " |
| 779 | << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation(); |
| 780 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 781 | |
| 782 | mirror::ArtMethod* GetTargetMethod(const LinkerPatch& patch) |
| 783 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 784 | MethodReference ref = patch.TargetMethod(); |
| 785 | mirror::DexCache* dex_cache = |
| 786 | (dex_file_ == ref.dex_file) ? dex_cache_ : class_linker_->FindDexCache(*ref.dex_file); |
| 787 | mirror::ArtMethod* method = dex_cache->GetResolvedMethod(ref.dex_method_index); |
| 788 | CHECK(method != nullptr); |
| 789 | return method; |
| 790 | } |
| 791 | |
| 792 | uint32_t GetTargetOffset(const LinkerPatch& patch) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 793 | auto target_it = writer_->method_offset_map_.map.find(patch.TargetMethod()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 794 | uint32_t target_offset = |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 795 | (target_it != writer_->method_offset_map_.map.end()) ? target_it->second : 0u; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 796 | // If there's no compiled code, point to the correct trampoline. |
| 797 | if (UNLIKELY(target_offset == 0)) { |
| 798 | mirror::ArtMethod* target = GetTargetMethod(patch); |
| 799 | DCHECK(target != nullptr); |
Jeff Hao | a0acc2d | 2015-01-27 11:22:04 -0800 | [diff] [blame] | 800 | size_t size = GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet()); |
| 801 | const void* oat_code_offset = target->GetEntryPointFromQuickCompiledCodePtrSize(size); |
| 802 | if (oat_code_offset != 0) { |
| 803 | DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(oat_code_offset)); |
| 804 | DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(oat_code_offset)); |
| 805 | DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(oat_code_offset)); |
| 806 | target_offset = PointerToLowMemUInt32(oat_code_offset); |
| 807 | } else { |
| 808 | target_offset = target->IsNative() |
| 809 | ? writer_->oat_header_->GetQuickGenericJniTrampolineOffset() |
| 810 | : writer_->oat_header_->GetQuickToInterpreterBridgeOffset(); |
| 811 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 812 | } |
| 813 | return target_offset; |
| 814 | } |
| 815 | |
| 816 | mirror::Class* GetTargetType(const LinkerPatch& patch) |
| 817 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 818 | mirror::DexCache* dex_cache = (dex_file_ == patch.TargetTypeDexFile()) |
| 819 | ? dex_cache_ : class_linker_->FindDexCache(*patch.TargetTypeDexFile()); |
| 820 | mirror::Class* type = dex_cache->GetResolvedType(patch.TargetTypeIndex()); |
| 821 | CHECK(type != nullptr); |
| 822 | return type; |
| 823 | } |
| 824 | |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 825 | uint32_t GetDexCacheOffset(const LinkerPatch& patch) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 826 | if (writer_->image_writer_ != nullptr) { |
| 827 | auto* element = writer_->image_writer_->GetDexCacheArrayElementImageAddress( |
| 828 | patch.TargetDexCacheDexFile(), patch.TargetDexCacheElementOffset()); |
| 829 | const uint8_t* oat_data = writer_->image_writer_->GetOatFileBegin() + file_offset_; |
| 830 | return reinterpret_cast<const uint8_t*>(element) - oat_data; |
| 831 | } else { |
| 832 | LOG(FATAL) << "Unimplemented."; |
| 833 | UNREACHABLE(); |
| 834 | } |
| 835 | } |
| 836 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 837 | void PatchObjectAddress(std::vector<uint8_t>* code, uint32_t offset, mirror::Object* object) |
| 838 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 839 | // NOTE: Direct method pointers across oat files don't use linker patches. However, direct |
| 840 | // type pointers across oat files do. (TODO: Investigate why.) |
| 841 | if (writer_->image_writer_ != nullptr) { |
| 842 | object = writer_->image_writer_->GetImageAddress(object); |
| 843 | } |
| 844 | uint32_t address = PointerToLowMemUInt32(object); |
| 845 | DCHECK_LE(offset + 4, code->size()); |
| 846 | uint8_t* data = &(*code)[offset]; |
| 847 | data[0] = address & 0xffu; |
| 848 | data[1] = (address >> 8) & 0xffu; |
| 849 | data[2] = (address >> 16) & 0xffu; |
| 850 | data[3] = (address >> 24) & 0xffu; |
| 851 | } |
| 852 | |
| 853 | void PatchCodeAddress(std::vector<uint8_t>* code, uint32_t offset, uint32_t target_offset) |
| 854 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Jeff Hao | a0acc2d | 2015-01-27 11:22:04 -0800 | [diff] [blame] | 855 | uint32_t address = writer_->image_writer_ == nullptr ? target_offset : |
| 856 | PointerToLowMemUInt32(writer_->image_writer_->GetOatFileBegin() + |
| 857 | writer_->oat_data_offset_ + target_offset); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 858 | DCHECK_LE(offset + 4, code->size()); |
| 859 | uint8_t* data = &(*code)[offset]; |
| 860 | data[0] = address & 0xffu; |
| 861 | data[1] = (address >> 8) & 0xffu; |
| 862 | data[2] = (address >> 16) & 0xffu; |
| 863 | data[3] = (address >> 24) & 0xffu; |
| 864 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 865 | }; |
| 866 | |
| 867 | template <typename DataAccess> |
| 868 | class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor { |
| 869 | public: |
| 870 | WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset, |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 871 | size_t relative_offset) |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 872 | : OatDexMethodVisitor(writer, relative_offset), |
| 873 | out_(out), |
| 874 | file_offset_(file_offset) { |
| 875 | } |
| 876 | |
| 877 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) { |
| 878 | OatClass* oat_class = writer_->oat_classes_[oat_class_index_]; |
| 879 | const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 880 | |
| 881 | if (compiled_method != NULL) { // ie. not an abstract method |
| 882 | size_t file_offset = file_offset_; |
| 883 | OutputStream* out = out_; |
| 884 | |
| 885 | uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_); |
| 886 | ++method_offsets_index_; |
| 887 | |
| 888 | // Write deduplicated map. |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 889 | const SwapVector<uint8_t>* map = DataAccess::GetData(compiled_method); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 890 | size_t map_size = map == nullptr ? 0 : map->size() * sizeof((*map)[0]); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 891 | DCHECK((map_size == 0u && map_offset == 0u) || |
| 892 | (map_size != 0u && map_offset != 0u && map_offset <= offset_)) |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 893 | << map_size << " " << map_offset << " " << offset_ << " " |
| 894 | << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " for " << DataAccess::Name(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 895 | if (map_size != 0u && map_offset == offset_) { |
| 896 | if (UNLIKELY(!out->WriteFully(&(*map)[0], map_size))) { |
| 897 | ReportWriteFailure(it); |
| 898 | return false; |
| 899 | } |
| 900 | offset_ += map_size; |
| 901 | } |
| 902 | DCHECK_OFFSET_(); |
| 903 | } |
| 904 | |
| 905 | return true; |
| 906 | } |
| 907 | |
| 908 | private: |
| 909 | OutputStream* const out_; |
| 910 | size_t const file_offset_; |
| 911 | |
| 912 | void ReportWriteFailure(const ClassDataItemIterator& it) { |
| 913 | PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for " |
| 914 | << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation(); |
| 915 | } |
| 916 | }; |
| 917 | |
| 918 | // Visit all methods from all classes in all dex files with the specified visitor. |
| 919 | bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) { |
| 920 | for (const DexFile* dex_file : *dex_files_) { |
| 921 | const size_t class_def_count = dex_file->NumClassDefs(); |
| 922 | for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) { |
| 923 | if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) { |
| 924 | return false; |
| 925 | } |
| 926 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 927 | const uint8_t* class_data = dex_file->GetClassData(class_def); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 928 | if (class_data != NULL) { // ie not an empty class, such as a marker interface |
| 929 | ClassDataItemIterator it(*dex_file, class_data); |
| 930 | while (it.HasNextStaticField()) { |
| 931 | it.Next(); |
| 932 | } |
| 933 | while (it.HasNextInstanceField()) { |
| 934 | it.Next(); |
| 935 | } |
| 936 | size_t class_def_method_index = 0u; |
| 937 | while (it.HasNextDirectMethod()) { |
| 938 | if (!visitor->VisitMethod(class_def_method_index, it)) { |
| 939 | return false; |
| 940 | } |
| 941 | ++class_def_method_index; |
| 942 | it.Next(); |
| 943 | } |
| 944 | while (it.HasNextVirtualMethod()) { |
| 945 | if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) { |
| 946 | return false; |
| 947 | } |
| 948 | ++class_def_method_index; |
| 949 | it.Next(); |
| 950 | } |
| 951 | } |
| 952 | if (UNLIKELY(!visitor->EndClass())) { |
| 953 | return false; |
| 954 | } |
| 955 | } |
| 956 | } |
| 957 | return true; |
| 958 | } |
| 959 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 960 | size_t OatWriter::InitOatHeader() { |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 961 | oat_header_ = OatHeader::Create(compiler_driver_->GetInstructionSet(), |
| 962 | compiler_driver_->GetInstructionSetFeatures(), |
| 963 | dex_files_, |
| 964 | image_file_location_oat_checksum_, |
| 965 | image_file_location_oat_begin_, |
| 966 | key_value_store_); |
| 967 | |
| 968 | return oat_header_->GetHeaderSize(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | size_t OatWriter::InitOatDexFiles(size_t offset) { |
| 972 | // create the OatDexFiles |
| 973 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 974 | const DexFile* dex_file = (*dex_files_)[i]; |
| 975 | CHECK(dex_file != NULL); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 976 | OatDexFile* oat_dex_file = new OatDexFile(offset, *dex_file); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 977 | oat_dex_files_.push_back(oat_dex_file); |
| 978 | offset += oat_dex_file->SizeOf(); |
| 979 | } |
| 980 | return offset; |
| 981 | } |
| 982 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 983 | size_t OatWriter::InitDexFiles(size_t offset) { |
| 984 | // calculate the offsets within OatDexFiles to the DexFiles |
| 985 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 986 | // dex files are required to be 4 byte aligned |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 987 | size_t original_offset = offset; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 988 | offset = RoundUp(offset, 4); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 989 | size_dex_file_alignment_ += offset - original_offset; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 990 | |
| 991 | // set offset in OatDexFile to DexFile |
| 992 | oat_dex_files_[i]->dex_file_offset_ = offset; |
| 993 | |
| 994 | const DexFile* dex_file = (*dex_files_)[i]; |
| 995 | offset += dex_file->GetHeader().file_size_; |
| 996 | } |
| 997 | return offset; |
| 998 | } |
| 999 | |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 1000 | size_t OatWriter::InitOatClasses(size_t offset) { |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 1001 | // calculate the offsets within OatDexFiles to OatClasses |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1002 | InitOatClassesMethodVisitor visitor(this, offset); |
| 1003 | bool success = VisitDexMethods(&visitor); |
| 1004 | CHECK(success); |
| 1005 | offset = visitor.GetOffset(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1006 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1007 | // Update oat_dex_files_. |
| 1008 | auto oat_class_it = oat_classes_.begin(); |
| 1009 | for (OatDexFile* oat_dex_file : oat_dex_files_) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1010 | for (uint32_t& method_offset : oat_dex_file->methods_offsets_) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1011 | DCHECK(oat_class_it != oat_classes_.end()); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1012 | method_offset = (*oat_class_it)->offset_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1013 | ++oat_class_it; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1014 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1015 | oat_dex_file->UpdateChecksum(oat_header_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1016 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1017 | CHECK(oat_class_it == oat_classes_.end()); |
| 1018 | |
| 1019 | return offset; |
| 1020 | } |
| 1021 | |
| 1022 | size_t OatWriter::InitOatMaps(size_t offset) { |
| 1023 | #define VISIT(VisitorType) \ |
| 1024 | do { \ |
| 1025 | VisitorType visitor(this, offset); \ |
| 1026 | bool success = VisitDexMethods(&visitor); \ |
| 1027 | DCHECK(success); \ |
| 1028 | offset = visitor.GetOffset(); \ |
| 1029 | } while (false) |
| 1030 | |
| 1031 | VISIT(InitMapMethodVisitor<GcMapDataAccess>); |
| 1032 | VISIT(InitMapMethodVisitor<MappingTableDataAccess>); |
| 1033 | VISIT(InitMapMethodVisitor<VmapTableDataAccess>); |
| 1034 | |
| 1035 | #undef VISIT |
| 1036 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1037 | return offset; |
| 1038 | } |
| 1039 | |
| 1040 | size_t OatWriter::InitOatCode(size_t offset) { |
| 1041 | // calculate the offsets within OatHeader to executable code |
| 1042 | size_t old_offset = offset; |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 1043 | size_t adjusted_offset = offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1044 | // required to be on a new page boundary |
| 1045 | offset = RoundUp(offset, kPageSize); |
| 1046 | oat_header_->SetExecutableOffset(offset); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1047 | size_executable_offset_alignment_ = offset - old_offset; |
| 1048 | if (compiler_driver_->IsImage()) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1049 | CHECK_EQ(image_patch_delta_, 0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1050 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1051 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1052 | #define DO_TRAMPOLINE(field, fn_name) \ |
| 1053 | offset = CompiledCode::AlignCode(offset, instruction_set); \ |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 1054 | adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \ |
| 1055 | oat_header_->Set ## fn_name ## Offset(adjusted_offset); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1056 | field.reset(compiler_driver_->Create ## fn_name()); \ |
| 1057 | offset += field->size(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1058 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1059 | DO_TRAMPOLINE(interpreter_to_interpreter_bridge_, InterpreterToInterpreterBridge); |
| 1060 | DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_, InterpreterToCompiledCodeBridge); |
| 1061 | DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1062 | DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1063 | DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1064 | DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline); |
| 1065 | DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1066 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1067 | #undef DO_TRAMPOLINE |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1068 | } else { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1069 | oat_header_->SetInterpreterToInterpreterBridgeOffset(0); |
| 1070 | oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0); |
| 1071 | oat_header_->SetJniDlsymLookupOffset(0); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1072 | oat_header_->SetQuickGenericJniTrampolineOffset(0); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1073 | oat_header_->SetQuickImtConflictTrampolineOffset(0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1074 | oat_header_->SetQuickResolutionTrampolineOffset(0); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1075 | oat_header_->SetQuickToInterpreterBridgeOffset(0); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1076 | oat_header_->SetImagePatchDelta(image_patch_delta_); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1077 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1078 | return offset; |
| 1079 | } |
| 1080 | |
| 1081 | size_t OatWriter::InitOatCodeDexFiles(size_t offset) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1082 | #define VISIT(VisitorType) \ |
| 1083 | do { \ |
| 1084 | VisitorType visitor(this, offset); \ |
| 1085 | bool success = VisitDexMethods(&visitor); \ |
| 1086 | DCHECK(success); \ |
| 1087 | offset = visitor.GetOffset(); \ |
| 1088 | } while (false) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1089 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1090 | VISIT(InitCodeMethodVisitor); |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 1091 | if (compiler_driver_->IsImage()) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1092 | VISIT(InitImageMethodVisitor); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1093 | } |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 1094 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1095 | #undef VISIT |
| 1096 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1097 | return offset; |
| 1098 | } |
| 1099 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1100 | bool OatWriter::Write(OutputStream* out) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1101 | const off_t raw_file_offset = out->Seek(0, kSeekCurrent); |
| 1102 | if (raw_file_offset == (off_t) -1) { |
| 1103 | LOG(ERROR) << "Failed to get file offset in " << out->GetLocation(); |
| 1104 | return false; |
| 1105 | } |
| 1106 | const size_t file_offset = static_cast<size_t>(raw_file_offset); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1107 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1108 | // Reserve space for header. It will be written last - after updating the checksum. |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1109 | size_t header_size = oat_header_->GetHeaderSize(); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1110 | if (out->Seek(header_size, kSeekCurrent) == (off_t) -1) { |
| 1111 | PLOG(ERROR) << "Failed to reserve space for oat header in " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1112 | return false; |
| 1113 | } |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1114 | size_oat_header_ += sizeof(OatHeader); |
| 1115 | size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 1116 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1117 | if (!WriteTables(out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1118 | LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1119 | return false; |
| 1120 | } |
| 1121 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1122 | off_t tables_end_offset = out->Seek(0, kSeekCurrent); |
| 1123 | if (tables_end_offset == (off_t) -1) { |
| 1124 | LOG(ERROR) << "Failed to seek to oat code position in " << out->GetLocation(); |
| 1125 | return false; |
| 1126 | } |
| 1127 | size_t relative_offset = static_cast<size_t>(tables_end_offset) - file_offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1128 | relative_offset = WriteMaps(out, file_offset, relative_offset); |
| 1129 | if (relative_offset == 0) { |
| 1130 | LOG(ERROR) << "Failed to write oat code to " << out->GetLocation(); |
| 1131 | return false; |
| 1132 | } |
| 1133 | |
| 1134 | relative_offset = WriteCode(out, file_offset, relative_offset); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1135 | if (relative_offset == 0) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1136 | LOG(ERROR) << "Failed to write oat code to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1137 | return false; |
| 1138 | } |
| 1139 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1140 | relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset); |
| 1141 | if (relative_offset == 0) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1142 | LOG(ERROR) << "Failed to write oat code for dex files to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1143 | return false; |
| 1144 | } |
| 1145 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1146 | const off_t oat_end_file_offset = out->Seek(0, kSeekCurrent); |
| 1147 | if (oat_end_file_offset == (off_t) -1) { |
| 1148 | LOG(ERROR) << "Failed to get oat end file offset in " << out->GetLocation(); |
| 1149 | return false; |
| 1150 | } |
| 1151 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1152 | if (kIsDebugBuild) { |
| 1153 | uint32_t size_total = 0; |
| 1154 | #define DO_STAT(x) \ |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 1155 | VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \ |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1156 | size_total += x; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1157 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1158 | DO_STAT(size_dex_file_alignment_); |
| 1159 | DO_STAT(size_executable_offset_alignment_); |
| 1160 | DO_STAT(size_oat_header_); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1161 | DO_STAT(size_oat_header_key_value_store_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1162 | DO_STAT(size_dex_file_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1163 | DO_STAT(size_interpreter_to_interpreter_bridge_); |
| 1164 | DO_STAT(size_interpreter_to_compiled_code_bridge_); |
| 1165 | DO_STAT(size_jni_dlsym_lookup_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1166 | DO_STAT(size_quick_generic_jni_trampoline_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1167 | DO_STAT(size_quick_imt_conflict_trampoline_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1168 | DO_STAT(size_quick_resolution_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1169 | DO_STAT(size_quick_to_interpreter_bridge_); |
| 1170 | DO_STAT(size_trampoline_alignment_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1171 | DO_STAT(size_method_header_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1172 | DO_STAT(size_code_); |
| 1173 | DO_STAT(size_code_alignment_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1174 | DO_STAT(size_relative_call_thunks_); |
Vladimir Marko | c74658b | 2015-03-31 10:26:41 +0100 | [diff] [blame] | 1175 | DO_STAT(size_misc_thunks_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1176 | DO_STAT(size_mapping_table_); |
| 1177 | DO_STAT(size_vmap_table_); |
| 1178 | DO_STAT(size_gc_map_); |
| 1179 | DO_STAT(size_oat_dex_file_location_size_); |
| 1180 | DO_STAT(size_oat_dex_file_location_data_); |
| 1181 | DO_STAT(size_oat_dex_file_location_checksum_); |
| 1182 | DO_STAT(size_oat_dex_file_offset_); |
| 1183 | DO_STAT(size_oat_dex_file_methods_offsets_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1184 | DO_STAT(size_oat_class_type_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1185 | DO_STAT(size_oat_class_status_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1186 | DO_STAT(size_oat_class_method_bitmaps_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1187 | DO_STAT(size_oat_class_method_offsets_); |
| 1188 | #undef DO_STAT |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1189 | |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 1190 | VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \ |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1191 | CHECK_EQ(file_offset + size_total, static_cast<size_t>(oat_end_file_offset)); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1192 | CHECK_EQ(size_, size_total); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1193 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1194 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1195 | CHECK_EQ(file_offset + size_, static_cast<size_t>(oat_end_file_offset)); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1196 | CHECK_EQ(size_, relative_offset); |
| 1197 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1198 | // Write the header now that the checksum is final. |
| 1199 | if (out->Seek(file_offset, kSeekSet) == (off_t) -1) { |
| 1200 | PLOG(ERROR) << "Failed to seek to oat header position in " << out->GetLocation(); |
| 1201 | return false; |
| 1202 | } |
| 1203 | DCHECK_EQ(raw_file_offset, out->Seek(0, kSeekCurrent)); |
| 1204 | if (!out->WriteFully(oat_header_, header_size)) { |
| 1205 | PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation(); |
| 1206 | return false; |
| 1207 | } |
| 1208 | if (out->Seek(oat_end_file_offset, kSeekSet) == (off_t) -1) { |
| 1209 | PLOG(ERROR) << "Failed to seek to end after writing oat header to " << out->GetLocation(); |
| 1210 | return false; |
| 1211 | } |
| 1212 | DCHECK_EQ(oat_end_file_offset, out->Seek(0, kSeekCurrent)); |
| 1213 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1214 | return true; |
| 1215 | } |
| 1216 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1217 | bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1218 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1219 | if (!oat_dex_files_[i]->Write(this, out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1220 | PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1221 | return false; |
| 1222 | } |
| 1223 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1224 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1225 | uint32_t expected_offset = file_offset + oat_dex_files_[i]->dex_file_offset_; |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1226 | off_t actual_offset = out->Seek(expected_offset, kSeekSet); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1227 | if (static_cast<uint32_t>(actual_offset) != expected_offset) { |
| 1228 | const DexFile* dex_file = (*dex_files_)[i]; |
| 1229 | PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset |
| 1230 | << " Expected: " << expected_offset << " File: " << dex_file->GetLocation(); |
| 1231 | return false; |
| 1232 | } |
| 1233 | const DexFile* dex_file = (*dex_files_)[i]; |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1234 | if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1235 | PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1236 | << " to " << out->GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1237 | return false; |
| 1238 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1239 | size_dex_file_ += dex_file->GetHeader().file_size_; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1240 | } |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 1241 | for (size_t i = 0; i != oat_classes_.size(); ++i) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1242 | if (!oat_classes_[i]->Write(this, out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1243 | PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1244 | return false; |
| 1245 | } |
| 1246 | } |
| 1247 | return true; |
| 1248 | } |
| 1249 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1250 | size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) { |
| 1251 | #define VISIT(VisitorType) \ |
| 1252 | do { \ |
| 1253 | VisitorType visitor(this, out, file_offset, relative_offset); \ |
| 1254 | if (UNLIKELY(!VisitDexMethods(&visitor))) { \ |
| 1255 | return 0; \ |
| 1256 | } \ |
| 1257 | relative_offset = visitor.GetOffset(); \ |
| 1258 | } while (false) |
| 1259 | |
| 1260 | size_t gc_maps_offset = relative_offset; |
| 1261 | VISIT(WriteMapMethodVisitor<GcMapDataAccess>); |
| 1262 | size_gc_map_ = relative_offset - gc_maps_offset; |
| 1263 | |
| 1264 | size_t mapping_tables_offset = relative_offset; |
| 1265 | VISIT(WriteMapMethodVisitor<MappingTableDataAccess>); |
| 1266 | size_mapping_table_ = relative_offset - mapping_tables_offset; |
| 1267 | |
| 1268 | size_t vmap_tables_offset = relative_offset; |
| 1269 | VISIT(WriteMapMethodVisitor<VmapTableDataAccess>); |
| 1270 | size_vmap_table_ = relative_offset - vmap_tables_offset; |
| 1271 | |
| 1272 | #undef VISIT |
| 1273 | |
| 1274 | return relative_offset; |
| 1275 | } |
| 1276 | |
| 1277 | size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1278 | off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1279 | relative_offset += size_executable_offset_alignment_; |
| 1280 | DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset()); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1281 | size_t expected_file_offset = file_offset + relative_offset; |
| 1282 | if (static_cast<uint32_t>(new_offset) != expected_file_offset) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 1283 | PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1284 | << " Expected: " << expected_file_offset << " File: " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1285 | return 0; |
| 1286 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1287 | DCHECK_OFFSET(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1288 | if (compiler_driver_->IsImage()) { |
| 1289 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1290 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1291 | #define DO_TRAMPOLINE(field) \ |
| 1292 | do { \ |
| 1293 | uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \ |
| 1294 | uint32_t alignment_padding = aligned_offset - relative_offset; \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1295 | out->Seek(alignment_padding, kSeekCurrent); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1296 | size_trampoline_alignment_ += alignment_padding; \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1297 | if (!out->WriteFully(&(*field)[0], field->size())) { \ |
| 1298 | PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1299 | return false; \ |
| 1300 | } \ |
| 1301 | size_ ## field += field->size(); \ |
| 1302 | relative_offset += alignment_padding + field->size(); \ |
| 1303 | DCHECK_OFFSET(); \ |
| 1304 | } while (false) |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1305 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1306 | DO_TRAMPOLINE(interpreter_to_interpreter_bridge_); |
| 1307 | DO_TRAMPOLINE(interpreter_to_compiled_code_bridge_); |
| 1308 | DO_TRAMPOLINE(jni_dlsym_lookup_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1309 | DO_TRAMPOLINE(quick_generic_jni_trampoline_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1310 | DO_TRAMPOLINE(quick_imt_conflict_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1311 | DO_TRAMPOLINE(quick_resolution_trampoline_); |
| 1312 | DO_TRAMPOLINE(quick_to_interpreter_bridge_); |
| 1313 | #undef DO_TRAMPOLINE |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1314 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1315 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1318 | size_t OatWriter::WriteCodeDexFiles(OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1319 | const size_t file_offset, |
| 1320 | size_t relative_offset) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1321 | #define VISIT(VisitorType) \ |
| 1322 | do { \ |
| 1323 | VisitorType visitor(this, out, file_offset, relative_offset); \ |
| 1324 | if (UNLIKELY(!VisitDexMethods(&visitor))) { \ |
| 1325 | return 0; \ |
| 1326 | } \ |
| 1327 | relative_offset = visitor.GetOffset(); \ |
| 1328 | } while (false) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1329 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1330 | VISIT(WriteCodeMethodVisitor); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1331 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1332 | #undef VISIT |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1333 | |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 1334 | size_code_alignment_ += relative_patcher_->CodeAlignmentSize(); |
| 1335 | size_relative_call_thunks_ += relative_patcher_->RelativeCallThunksSize(); |
| 1336 | size_misc_thunks_ += relative_patcher_->MiscThunksSize(); |
| 1337 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1338 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1339 | } |
| 1340 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1341 | bool OatWriter::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) { |
| 1342 | static const uint8_t kPadding[] = { |
| 1343 | 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u |
| 1344 | }; |
| 1345 | DCHECK_LE(aligned_code_delta, sizeof(kPadding)); |
| 1346 | if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) { |
| 1347 | return false; |
| 1348 | } |
| 1349 | size_code_alignment_ += aligned_code_delta; |
| 1350 | return true; |
| 1351 | } |
| 1352 | |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 1353 | std::pair<bool, uint32_t> OatWriter::MethodOffsetMap::FindMethodOffset(MethodReference ref) { |
| 1354 | auto it = map.find(ref); |
| 1355 | if (it == map.end()) { |
| 1356 | return std::pair<bool, uint32_t>(false, 0u); |
| 1357 | } else { |
| 1358 | return std::pair<bool, uint32_t>(true, it->second); |
| 1359 | } |
| 1360 | } |
| 1361 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1362 | OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) { |
| 1363 | offset_ = offset; |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 1364 | const std::string& location(dex_file.GetLocation()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1365 | dex_file_location_size_ = location.size(); |
| 1366 | dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data()); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1367 | dex_file_location_checksum_ = dex_file.GetLocationChecksum(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1368 | dex_file_offset_ = 0; |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1369 | methods_offsets_.resize(dex_file.NumClassDefs()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | size_t OatWriter::OatDexFile::SizeOf() const { |
| 1373 | return sizeof(dex_file_location_size_) |
| 1374 | + dex_file_location_size_ |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1375 | + sizeof(dex_file_location_checksum_) |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1376 | + sizeof(dex_file_offset_) |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1377 | + (sizeof(methods_offsets_[0]) * methods_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1378 | } |
| 1379 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1380 | void OatWriter::OatDexFile::UpdateChecksum(OatHeader* oat_header) const { |
| 1381 | oat_header->UpdateChecksum(&dex_file_location_size_, sizeof(dex_file_location_size_)); |
| 1382 | oat_header->UpdateChecksum(dex_file_location_data_, dex_file_location_size_); |
| 1383 | oat_header->UpdateChecksum(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_)); |
| 1384 | oat_header->UpdateChecksum(&dex_file_offset_, sizeof(dex_file_offset_)); |
| 1385 | oat_header->UpdateChecksum(&methods_offsets_[0], |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1386 | sizeof(methods_offsets_[0]) * methods_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1387 | } |
| 1388 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1389 | bool OatWriter::OatDexFile::Write(OatWriter* oat_writer, |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1390 | OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1391 | const size_t file_offset) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1392 | DCHECK_OFFSET_(); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1393 | if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) { |
| 1394 | PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1395 | return false; |
| 1396 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1397 | oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1398 | if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) { |
| 1399 | PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1400 | return false; |
| 1401 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1402 | oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_; |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1403 | if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) { |
| 1404 | PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1405 | return false; |
| 1406 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1407 | oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1408 | if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) { |
| 1409 | PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1410 | return false; |
| 1411 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1412 | oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1413 | if (!out->WriteFully(&methods_offsets_[0], |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 1414 | sizeof(methods_offsets_[0]) * methods_offsets_.size())) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1415 | PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1416 | return false; |
| 1417 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1418 | oat_writer->size_oat_dex_file_methods_offsets_ += |
| 1419 | sizeof(methods_offsets_[0]) * methods_offsets_.size(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1420 | return true; |
| 1421 | } |
| 1422 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1423 | OatWriter::OatClass::OatClass(size_t offset, |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1424 | const std::vector<CompiledMethod*>& compiled_methods, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1425 | uint32_t num_non_null_compiled_methods, |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1426 | mirror::Class::Status status) |
| 1427 | : compiled_methods_(compiled_methods) { |
| 1428 | uint32_t num_methods = compiled_methods.size(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1429 | CHECK_LE(num_non_null_compiled_methods, num_methods); |
| 1430 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1431 | offset_ = offset; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1432 | oat_method_offsets_offsets_from_oat_class_.resize(num_methods); |
| 1433 | |
| 1434 | // Since both kOatClassNoneCompiled and kOatClassAllCompiled could |
| 1435 | // apply when there are 0 methods, we just arbitrarily say that 0 |
| 1436 | // methods means kOatClassNoneCompiled and that we won't use |
| 1437 | // kOatClassAllCompiled unless there is at least one compiled |
| 1438 | // method. This means in an interpretter only system, we can assert |
| 1439 | // that all classes are kOatClassNoneCompiled. |
| 1440 | if (num_non_null_compiled_methods == 0) { |
| 1441 | type_ = kOatClassNoneCompiled; |
| 1442 | } else if (num_non_null_compiled_methods == num_methods) { |
| 1443 | type_ = kOatClassAllCompiled; |
| 1444 | } else { |
| 1445 | type_ = kOatClassSomeCompiled; |
| 1446 | } |
| 1447 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1448 | status_ = status; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1449 | method_offsets_.resize(num_non_null_compiled_methods); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 1450 | method_headers_.resize(num_non_null_compiled_methods); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1451 | |
| 1452 | uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_); |
| 1453 | if (type_ == kOatClassSomeCompiled) { |
| 1454 | method_bitmap_ = new BitVector(num_methods, false, Allocator::GetMallocAllocator()); |
| 1455 | method_bitmap_size_ = method_bitmap_->GetSizeOf(); |
| 1456 | oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_); |
| 1457 | oat_method_offsets_offset_from_oat_class += method_bitmap_size_; |
| 1458 | } else { |
| 1459 | method_bitmap_ = NULL; |
| 1460 | method_bitmap_size_ = 0; |
| 1461 | } |
| 1462 | |
| 1463 | for (size_t i = 0; i < num_methods; i++) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1464 | CompiledMethod* compiled_method = compiled_methods_[i]; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1465 | if (compiled_method == NULL) { |
| 1466 | oat_method_offsets_offsets_from_oat_class_[i] = 0; |
| 1467 | } else { |
| 1468 | oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class; |
| 1469 | oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets); |
| 1470 | if (type_ == kOatClassSomeCompiled) { |
| 1471 | method_bitmap_->SetBit(i); |
| 1472 | } |
| 1473 | } |
| 1474 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1475 | } |
| 1476 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1477 | OatWriter::OatClass::~OatClass() { |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 1478 | delete method_bitmap_; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1479 | } |
| 1480 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1481 | size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader( |
| 1482 | size_t class_def_method_index_) const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1483 | uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_); |
| 1484 | if (method_offset == 0) { |
| 1485 | return 0; |
| 1486 | } |
| 1487 | return offset_ + method_offset; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass( |
| 1491 | size_t class_def_method_index_) const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1492 | return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_]; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | size_t OatWriter::OatClass::SizeOf() const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1496 | return sizeof(status_) |
| 1497 | + sizeof(type_) |
| 1498 | + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_)) |
| 1499 | + method_bitmap_size_ |
| 1500 | + (sizeof(method_offsets_[0]) * method_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1501 | } |
| 1502 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1503 | void OatWriter::OatClass::UpdateChecksum(OatHeader* oat_header) const { |
| 1504 | oat_header->UpdateChecksum(&status_, sizeof(status_)); |
| 1505 | oat_header->UpdateChecksum(&type_, sizeof(type_)); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1506 | if (method_bitmap_size_ != 0) { |
| 1507 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1508 | oat_header->UpdateChecksum(&method_bitmap_size_, sizeof(method_bitmap_size_)); |
| 1509 | oat_header->UpdateChecksum(method_bitmap_->GetRawStorage(), method_bitmap_size_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1510 | } |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1511 | oat_header->UpdateChecksum(&method_offsets_[0], |
| 1512 | sizeof(method_offsets_[0]) * method_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1513 | } |
| 1514 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1515 | bool OatWriter::OatClass::Write(OatWriter* oat_writer, |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1516 | OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1517 | const size_t file_offset) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1518 | DCHECK_OFFSET_(); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1519 | if (!out->WriteFully(&status_, sizeof(status_))) { |
| 1520 | PLOG(ERROR) << "Failed to write class status to " << out->GetLocation(); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1521 | return false; |
| 1522 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1523 | oat_writer->size_oat_class_status_ += sizeof(status_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1524 | if (!out->WriteFully(&type_, sizeof(type_))) { |
| 1525 | PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1526 | return false; |
| 1527 | } |
| 1528 | oat_writer->size_oat_class_type_ += sizeof(type_); |
| 1529 | if (method_bitmap_size_ != 0) { |
| 1530 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1531 | if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) { |
| 1532 | PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1533 | return false; |
| 1534 | } |
| 1535 | oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1536 | if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) { |
| 1537 | PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1538 | return false; |
| 1539 | } |
| 1540 | oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_; |
| 1541 | } |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1542 | if (!out->WriteFully(&method_offsets_[0], |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 1543 | sizeof(method_offsets_[0]) * method_offsets_.size())) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1544 | PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1545 | return false; |
| 1546 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1547 | oat_writer->size_oat_class_method_offsets_ += sizeof(method_offsets_[0]) * method_offsets_.size(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1548 | return true; |
| 1549 | } |
| 1550 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1551 | } // namespace art |