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