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_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame^] | 719 | const DexFile::TypeId& type_id = |
| 720 | dex_file_->GetTypeId(dex_file_->GetClassDef(class_def_index_).class_idx_); |
| 721 | const char* class_descriptor = dex_file_->GetTypeDescriptor(type_id); |
| 722 | // Skip methods that are not in the image. |
| 723 | if (!writer_->GetCompilerDriver()->IsImageClass(class_descriptor)) { |
| 724 | return true; |
| 725 | } |
| 726 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 727 | OatClass* oat_class = &writer_->oat_classes_[oat_class_index_]; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 728 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 729 | |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 730 | OatMethodOffsets offsets(0u); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 731 | if (compiled_method != nullptr) { |
| 732 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 733 | offsets = oat_class->method_offsets_[method_offsets_index_]; |
| 734 | ++method_offsets_index_; |
| 735 | } |
| 736 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 737 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 738 | InvokeType invoke_type = it.GetMethodInvokeType(dex_file_->GetClassDef(class_def_index_)); |
| 739 | // Unchecked as we hold mutator_lock_ on entry. |
| 740 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 741 | StackHandleScope<1> hs(soa.Self()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 742 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache( |
| 743 | Thread::Current(), *dex_file_))); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 744 | ArtMethod* method = linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>( |
| 745 | *dex_file_, |
| 746 | it.GetMemberIndex(), |
| 747 | dex_cache, |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 748 | ScopedNullHandle<mirror::ClassLoader>(), |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 749 | nullptr, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 750 | invoke_type); |
Andreas Gampe | d9efea6 | 2014-07-21 22:56:08 -0700 | [diff] [blame] | 751 | if (method == nullptr) { |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 752 | LOG(INTERNAL_FATAL) << "Unexpected failure to resolve a method: " |
| 753 | << PrettyMethod(it.GetMemberIndex(), *dex_file_, true); |
Andreas Gampe | d9efea6 | 2014-07-21 22:56:08 -0700 | [diff] [blame] | 754 | soa.Self()->AssertPendingException(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 755 | mirror::Throwable* exc = soa.Self()->GetException(); |
Andreas Gampe | d9efea6 | 2014-07-21 22:56:08 -0700 | [diff] [blame] | 756 | std::string dump = exc->Dump(); |
| 757 | LOG(FATAL) << dump; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 758 | UNREACHABLE(); |
Andreas Gampe | d9efea6 | 2014-07-21 22:56:08 -0700 | [diff] [blame] | 759 | } |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 760 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 761 | if (compiled_method != nullptr && compiled_method->GetQuickCode().size() != 0) { |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 762 | method->SetEntryPointFromQuickCompiledCodePtrSize( |
| 763 | reinterpret_cast<void*>(offsets.code_offset_), pointer_size_); |
| 764 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 765 | |
| 766 | return true; |
| 767 | } |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 768 | |
| 769 | protected: |
| 770 | const size_t pointer_size_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 771 | }; |
| 772 | |
| 773 | class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor { |
| 774 | public: |
| 775 | WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset, |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 776 | size_t relative_offset) SHARED_LOCK_FUNCTION(Locks::mutator_lock_) |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 777 | : OatDexMethodVisitor(writer, relative_offset), |
| 778 | out_(out), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 779 | file_offset_(file_offset), |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 780 | soa_(Thread::Current()), |
| 781 | no_thread_suspension_(soa_.Self(), "OatWriter patching"), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 782 | class_linker_(Runtime::Current()->GetClassLinker()), |
| 783 | dex_cache_(nullptr) { |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 784 | patched_code_.reserve(16 * KB); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 785 | if (writer_->HasBootImage()) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 786 | // If we're creating the image, the address space must be ready so that we can apply patches. |
| 787 | CHECK(writer_->image_writer_->IsImageAddressSpaceReady()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 788 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 789 | } |
| 790 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 791 | ~WriteCodeMethodVisitor() UNLOCK_FUNCTION(Locks::mutator_lock_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | bool StartClass(const DexFile* dex_file, size_t class_def_index) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 795 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 796 | OatDexMethodVisitor::StartClass(dex_file, class_def_index); |
| 797 | if (dex_cache_ == nullptr || dex_cache_->GetDexFile() != dex_file) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 798 | dex_cache_ = class_linker_->FindDexCache(Thread::Current(), *dex_file); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 799 | } |
| 800 | return true; |
| 801 | } |
| 802 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 803 | bool EndClass() SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 804 | bool result = OatDexMethodVisitor::EndClass(); |
| 805 | if (oat_class_index_ == writer_->oat_classes_.size()) { |
| 806 | DCHECK(result); // OatDexMethodVisitor::EndClass() never fails. |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 807 | offset_ = writer_->relative_patcher_->WriteThunks(out_, offset_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 808 | if (UNLIKELY(offset_ == 0u)) { |
| 809 | PLOG(ERROR) << "Failed to write final relative call thunks"; |
| 810 | result = false; |
| 811 | } |
| 812 | } |
| 813 | return result; |
| 814 | } |
| 815 | |
| 816 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 817 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 818 | OatClass* oat_class = &writer_->oat_classes_[oat_class_index_]; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 819 | const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 820 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 821 | // No thread suspension since dex_cache_ that may get invalidated if that occurs. |
| 822 | ScopedAssertNoThreadSuspension tsc(Thread::Current(), __FUNCTION__); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 823 | if (compiled_method != nullptr) { // ie. not an abstract method |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 824 | size_t file_offset = file_offset_; |
| 825 | OutputStream* out = out_; |
| 826 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 827 | ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode(); |
| 828 | uint32_t code_size = quick_code.size() * sizeof(uint8_t); |
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 | // Deduplicate code arrays. |
| 831 | const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_]; |
| 832 | if (method_offsets.code_offset_ > offset_) { |
| 833 | offset_ = writer_->relative_patcher_->WriteThunks(out, offset_); |
| 834 | if (offset_ == 0u) { |
| 835 | ReportWriteFailure("relative call thunk", it); |
| 836 | return false; |
Nicolas Geoffray | f075879 | 2015-07-13 11:56:00 +0000 | [diff] [blame] | 837 | } |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 838 | uint32_t aligned_offset = compiled_method->AlignCode(offset_); |
| 839 | uint32_t aligned_code_delta = aligned_offset - offset_; |
| 840 | if (aligned_code_delta != 0) { |
| 841 | if (!writer_->WriteCodeAlignment(out, aligned_code_delta)) { |
| 842 | ReportWriteFailure("code alignment padding", it); |
| 843 | return false; |
| 844 | } |
| 845 | offset_ += aligned_code_delta; |
| 846 | DCHECK_OFFSET_(); |
| 847 | } |
| 848 | DCHECK_ALIGNED_PARAM(offset_, |
| 849 | GetInstructionSetAlignment(compiled_method->GetInstructionSet())); |
| 850 | DCHECK_EQ(method_offsets.code_offset_, |
| 851 | offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta()) |
| 852 | << PrettyMethod(it.GetMemberIndex(), *dex_file_); |
| 853 | const OatQuickMethodHeader& method_header = |
| 854 | oat_class->method_headers_[method_offsets_index_]; |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 855 | if (!writer_->WriteData(out, &method_header, sizeof(method_header))) { |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 856 | ReportWriteFailure("method header", it); |
| 857 | return false; |
| 858 | } |
| 859 | writer_->size_method_header_ += sizeof(method_header); |
| 860 | offset_ += sizeof(method_header); |
Nicolas Geoffray | ed6195a | 2015-07-13 17:02:30 +0000 | [diff] [blame] | 861 | DCHECK_OFFSET_(); |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 862 | |
| 863 | if (!compiled_method->GetPatches().empty()) { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 864 | patched_code_.assign(quick_code.begin(), quick_code.end()); |
| 865 | quick_code = ArrayRef<const uint8_t>(patched_code_); |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 866 | for (const LinkerPatch& patch : compiled_method->GetPatches()) { |
| 867 | if (patch.Type() == kLinkerPatchCallRelative) { |
| 868 | // NOTE: Relative calls across oat files are not supported. |
| 869 | uint32_t target_offset = GetTargetOffset(patch); |
| 870 | uint32_t literal_offset = patch.LiteralOffset(); |
| 871 | writer_->relative_patcher_->PatchCall(&patched_code_, literal_offset, |
| 872 | offset_ + literal_offset, target_offset); |
| 873 | } else if (patch.Type() == kLinkerPatchDexCacheArray) { |
| 874 | uint32_t target_offset = GetDexCacheOffset(patch); |
| 875 | uint32_t literal_offset = patch.LiteralOffset(); |
| 876 | writer_->relative_patcher_->PatchDexCacheReference(&patched_code_, patch, |
| 877 | offset_ + literal_offset, |
| 878 | target_offset); |
| 879 | } else if (patch.Type() == kLinkerPatchCall) { |
| 880 | uint32_t target_offset = GetTargetOffset(patch); |
| 881 | PatchCodeAddress(&patched_code_, patch.LiteralOffset(), target_offset); |
| 882 | } else if (patch.Type() == kLinkerPatchMethod) { |
| 883 | ArtMethod* method = GetTargetMethod(patch); |
| 884 | PatchMethodAddress(&patched_code_, patch.LiteralOffset(), method); |
| 885 | } else if (patch.Type() == kLinkerPatchType) { |
| 886 | mirror::Class* type = GetTargetType(patch); |
| 887 | PatchObjectAddress(&patched_code_, patch.LiteralOffset(), type); |
| 888 | } |
| 889 | } |
| 890 | } |
| 891 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 892 | if (!writer_->WriteData(out, quick_code.data(), code_size)) { |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 893 | ReportWriteFailure("method code", it); |
| 894 | return false; |
| 895 | } |
| 896 | writer_->size_code_ += code_size; |
| 897 | offset_ += code_size; |
Nicolas Geoffray | f075879 | 2015-07-13 11:56:00 +0000 | [diff] [blame] | 898 | } |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 899 | DCHECK_OFFSET_(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 900 | ++method_offsets_index_; |
| 901 | } |
| 902 | |
| 903 | return true; |
| 904 | } |
| 905 | |
| 906 | private: |
| 907 | OutputStream* const out_; |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 908 | const size_t file_offset_; |
| 909 | const ScopedObjectAccess soa_; |
| 910 | const ScopedAssertNoThreadSuspension no_thread_suspension_; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 911 | ClassLinker* const class_linker_; |
| 912 | mirror::DexCache* dex_cache_; |
| 913 | std::vector<uint8_t> patched_code_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 914 | |
| 915 | void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) { |
| 916 | PLOG(ERROR) << "Failed to write " << what << " for " |
| 917 | << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation(); |
| 918 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 919 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 920 | ArtMethod* GetTargetMethod(const LinkerPatch& patch) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 921 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 922 | MethodReference ref = patch.TargetMethod(); |
| 923 | mirror::DexCache* dex_cache = |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 924 | (dex_file_ == ref.dex_file) ? dex_cache_ : class_linker_->FindDexCache( |
| 925 | Thread::Current(), *ref.dex_file); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 926 | ArtMethod* method = dex_cache->GetResolvedMethod( |
| 927 | ref.dex_method_index, class_linker_->GetImagePointerSize()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 928 | CHECK(method != nullptr); |
| 929 | return method; |
| 930 | } |
| 931 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 932 | uint32_t GetTargetOffset(const LinkerPatch& patch) SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 933 | auto target_it = writer_->method_offset_map_.map.find(patch.TargetMethod()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 934 | uint32_t target_offset = |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 935 | (target_it != writer_->method_offset_map_.map.end()) ? target_it->second : 0u; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 936 | // If there's no compiled code, point to the correct trampoline. |
| 937 | if (UNLIKELY(target_offset == 0)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 938 | ArtMethod* target = GetTargetMethod(patch); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 939 | DCHECK(target != nullptr); |
Jeff Hao | a0acc2d | 2015-01-27 11:22:04 -0800 | [diff] [blame] | 940 | size_t size = GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet()); |
| 941 | const void* oat_code_offset = target->GetEntryPointFromQuickCompiledCodePtrSize(size); |
| 942 | if (oat_code_offset != 0) { |
| 943 | DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(oat_code_offset)); |
| 944 | DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(oat_code_offset)); |
| 945 | DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(oat_code_offset)); |
| 946 | target_offset = PointerToLowMemUInt32(oat_code_offset); |
| 947 | } else { |
| 948 | target_offset = target->IsNative() |
| 949 | ? writer_->oat_header_->GetQuickGenericJniTrampolineOffset() |
| 950 | : writer_->oat_header_->GetQuickToInterpreterBridgeOffset(); |
| 951 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 952 | } |
| 953 | return target_offset; |
| 954 | } |
| 955 | |
| 956 | mirror::Class* GetTargetType(const LinkerPatch& patch) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 957 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 958 | mirror::DexCache* dex_cache = (dex_file_ == patch.TargetTypeDexFile()) |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 959 | ? dex_cache_ : class_linker_->FindDexCache(Thread::Current(), *patch.TargetTypeDexFile()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 960 | mirror::Class* type = dex_cache->GetResolvedType(patch.TargetTypeIndex()); |
| 961 | CHECK(type != nullptr); |
| 962 | return type; |
| 963 | } |
| 964 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 965 | uint32_t GetDexCacheOffset(const LinkerPatch& patch) SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 966 | if (writer_->HasBootImage()) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 967 | auto* element = writer_->image_writer_->GetDexCacheArrayElementImageAddress<const uint8_t*>( |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 968 | patch.TargetDexCacheDexFile(), patch.TargetDexCacheElementOffset()); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame^] | 969 | const char* oat_filename = writer_->image_writer_->GetOatFilenameForDexCache(dex_cache_); |
| 970 | const uint8_t* oat_data = |
| 971 | writer_->image_writer_->GetOatFileBegin(oat_filename) + file_offset_; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 972 | return element - oat_data; |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 973 | } else { |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 974 | size_t start = writer_->dex_cache_arrays_offsets_.Get(patch.TargetDexCacheDexFile()); |
| 975 | return start + patch.TargetDexCacheElementOffset(); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 976 | } |
| 977 | } |
| 978 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 979 | 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] | 980 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 981 | if (writer_->HasBootImage()) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 982 | object = writer_->image_writer_->GetImageAddress(object); |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 983 | } else { |
| 984 | // NOTE: We're using linker patches for app->boot references when the image can |
| 985 | // be relocated and therefore we need to emit .oat_patches. We're not using this |
| 986 | // for app->app references, so check that the object is in the image space. |
| 987 | DCHECK(Runtime::Current()->GetHeap()->FindSpaceFromObject(object, false)->IsImageSpace()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 988 | } |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 989 | // 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] | 990 | uint32_t address = PointerToLowMemUInt32(object); |
| 991 | DCHECK_LE(offset + 4, code->size()); |
| 992 | uint8_t* data = &(*code)[offset]; |
| 993 | data[0] = address & 0xffu; |
| 994 | data[1] = (address >> 8) & 0xffu; |
| 995 | data[2] = (address >> 16) & 0xffu; |
| 996 | data[3] = (address >> 24) & 0xffu; |
| 997 | } |
| 998 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 999 | void PatchMethodAddress(std::vector<uint8_t>* code, uint32_t offset, ArtMethod* method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1000 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1001 | if (writer_->HasBootImage()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1002 | method = writer_->image_writer_->GetImageMethodAddress(method); |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 1003 | } else if (kIsDebugBuild) { |
| 1004 | // NOTE: We're using linker patches for app->boot references when the image can |
| 1005 | // be relocated and therefore we need to emit .oat_patches. We're not using this |
| 1006 | // for app->app references, so check that the method is an image method. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame^] | 1007 | std::vector<gc::space::ImageSpace*> image_spaces = |
| 1008 | Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
| 1009 | bool contains_method = false; |
| 1010 | for (gc::space::ImageSpace* image_space : image_spaces) { |
| 1011 | size_t method_offset = reinterpret_cast<const uint8_t*>(method) - image_space->Begin(); |
| 1012 | contains_method |= |
| 1013 | image_space->GetImageHeader().GetMethodsSection().Contains(method_offset); |
| 1014 | } |
| 1015 | CHECK(contains_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1016 | } |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 1017 | // 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] | 1018 | uint32_t address = PointerToLowMemUInt32(method); |
| 1019 | DCHECK_LE(offset + 4, code->size()); |
| 1020 | uint8_t* data = &(*code)[offset]; |
| 1021 | data[0] = address & 0xffu; |
| 1022 | data[1] = (address >> 8) & 0xffu; |
| 1023 | data[2] = (address >> 16) & 0xffu; |
| 1024 | data[3] = (address >> 24) & 0xffu; |
| 1025 | } |
| 1026 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1027 | 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] | 1028 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 1029 | uint32_t address = target_offset; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1030 | if (writer_->HasBootImage()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame^] | 1031 | const char* oat_filename = writer_->image_writer_->GetOatFilenameForDexCache(dex_cache_); |
| 1032 | address = PointerToLowMemUInt32(writer_->image_writer_->GetOatFileBegin(oat_filename) + |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 1033 | writer_->oat_data_offset_ + target_offset); |
| 1034 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1035 | DCHECK_LE(offset + 4, code->size()); |
| 1036 | uint8_t* data = &(*code)[offset]; |
| 1037 | data[0] = address & 0xffu; |
| 1038 | data[1] = (address >> 8) & 0xffu; |
| 1039 | data[2] = (address >> 16) & 0xffu; |
| 1040 | data[3] = (address >> 24) & 0xffu; |
| 1041 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1042 | }; |
| 1043 | |
| 1044 | template <typename DataAccess> |
| 1045 | class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor { |
| 1046 | public: |
| 1047 | WriteMapMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset, |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 1048 | size_t relative_offset) |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1049 | : OatDexMethodVisitor(writer, relative_offset), |
| 1050 | out_(out), |
| 1051 | file_offset_(file_offset) { |
| 1052 | } |
| 1053 | |
| 1054 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1055 | OatClass* oat_class = &writer_->oat_classes_[oat_class_index_]; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1056 | const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 1057 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1058 | if (compiled_method != nullptr) { // ie. not an abstract method |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1059 | size_t file_offset = file_offset_; |
| 1060 | OutputStream* out = out_; |
| 1061 | |
| 1062 | uint32_t map_offset = DataAccess::GetOffset(oat_class, method_offsets_index_); |
| 1063 | ++method_offsets_index_; |
| 1064 | |
| 1065 | // Write deduplicated map. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 1066 | ArrayRef<const uint8_t> map = DataAccess::GetData(compiled_method); |
| 1067 | size_t map_size = map.size() * sizeof(map[0]); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1068 | DCHECK((map_size == 0u && map_offset == 0u) || |
| 1069 | (map_size != 0u && map_offset != 0u && map_offset <= offset_)) |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 1070 | << map_size << " " << map_offset << " " << offset_ << " " |
| 1071 | << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " for " << DataAccess::Name(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1072 | if (map_size != 0u && map_offset == offset_) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1073 | if (UNLIKELY(!writer_->WriteData(out, map.data(), map_size))) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1074 | ReportWriteFailure(it); |
| 1075 | return false; |
| 1076 | } |
| 1077 | offset_ += map_size; |
| 1078 | } |
| 1079 | DCHECK_OFFSET_(); |
| 1080 | } |
| 1081 | |
| 1082 | return true; |
| 1083 | } |
| 1084 | |
| 1085 | private: |
| 1086 | OutputStream* const out_; |
| 1087 | size_t const file_offset_; |
| 1088 | |
| 1089 | void ReportWriteFailure(const ClassDataItemIterator& it) { |
| 1090 | PLOG(ERROR) << "Failed to write " << DataAccess::Name() << " for " |
| 1091 | << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation(); |
| 1092 | } |
| 1093 | }; |
| 1094 | |
| 1095 | // Visit all methods from all classes in all dex files with the specified visitor. |
| 1096 | bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) { |
| 1097 | for (const DexFile* dex_file : *dex_files_) { |
| 1098 | const size_t class_def_count = dex_file->NumClassDefs(); |
| 1099 | for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) { |
| 1100 | if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) { |
| 1101 | return false; |
| 1102 | } |
| 1103 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1104 | const uint8_t* class_data = dex_file->GetClassData(class_def); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1105 | 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] | 1106 | ClassDataItemIterator it(*dex_file, class_data); |
| 1107 | while (it.HasNextStaticField()) { |
| 1108 | it.Next(); |
| 1109 | } |
| 1110 | while (it.HasNextInstanceField()) { |
| 1111 | it.Next(); |
| 1112 | } |
| 1113 | size_t class_def_method_index = 0u; |
| 1114 | while (it.HasNextDirectMethod()) { |
| 1115 | if (!visitor->VisitMethod(class_def_method_index, it)) { |
| 1116 | return false; |
| 1117 | } |
| 1118 | ++class_def_method_index; |
| 1119 | it.Next(); |
| 1120 | } |
| 1121 | while (it.HasNextVirtualMethod()) { |
| 1122 | if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) { |
| 1123 | return false; |
| 1124 | } |
| 1125 | ++class_def_method_index; |
| 1126 | it.Next(); |
| 1127 | } |
| 1128 | } |
| 1129 | if (UNLIKELY(!visitor->EndClass())) { |
| 1130 | return false; |
| 1131 | } |
| 1132 | } |
| 1133 | } |
| 1134 | return true; |
| 1135 | } |
| 1136 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 1137 | size_t OatWriter::InitOatHeader() { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1138 | oat_header_.reset(OatHeader::Create(compiler_driver_->GetInstructionSet(), |
| 1139 | compiler_driver_->GetInstructionSetFeatures(), |
| 1140 | dchecked_integral_cast<uint32_t>(dex_files_->size()), |
| 1141 | key_value_store_)); |
| 1142 | oat_header_->SetImageFileLocationOatChecksum(image_file_location_oat_checksum_); |
| 1143 | oat_header_->SetImageFileLocationOatDataBegin(image_file_location_oat_begin_); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1144 | |
| 1145 | return oat_header_->GetHeaderSize(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | size_t OatWriter::InitOatDexFiles(size_t offset) { |
| 1149 | // create the OatDexFiles |
| 1150 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 1151 | const DexFile* dex_file = (*dex_files_)[i]; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1152 | CHECK(dex_file != nullptr); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1153 | oat_dex_files_.emplace_back(offset, *dex_file); |
| 1154 | offset += oat_dex_files_.back().SizeOf(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1155 | } |
| 1156 | return offset; |
| 1157 | } |
| 1158 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1159 | size_t OatWriter::InitDexFiles(size_t offset) { |
| 1160 | // calculate the offsets within OatDexFiles to the DexFiles |
| 1161 | for (size_t i = 0; i != dex_files_->size(); ++i) { |
| 1162 | // dex files are required to be 4 byte aligned |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1163 | size_t original_offset = offset; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1164 | offset = RoundUp(offset, 4); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1165 | size_dex_file_alignment_ += offset - original_offset; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1166 | |
| 1167 | // set offset in OatDexFile to DexFile |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1168 | oat_dex_files_[i].dex_file_offset_ = offset; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1169 | |
| 1170 | const DexFile* dex_file = (*dex_files_)[i]; |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 1171 | |
| 1172 | // Initialize type lookup table |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1173 | oat_dex_files_[i].lookup_table_ = dex_file->GetTypeLookupTable(); |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 1174 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1175 | offset += dex_file->GetHeader().file_size_; |
| 1176 | } |
| 1177 | return offset; |
| 1178 | } |
| 1179 | |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 1180 | size_t OatWriter::InitLookupTables(size_t offset) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1181 | for (OatDexFile& oat_dex_file : oat_dex_files_) { |
| 1182 | if (oat_dex_file.lookup_table_ != nullptr) { |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 1183 | uint32_t aligned_offset = RoundUp(offset, 4); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1184 | oat_dex_file.lookup_table_offset_ = aligned_offset; |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 1185 | size_oat_lookup_table_alignment_ += aligned_offset - offset; |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1186 | offset = aligned_offset + oat_dex_file.lookup_table_->RawDataLength(); |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 1187 | } else { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1188 | oat_dex_file.lookup_table_offset_ = 0; |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 1189 | } |
| 1190 | } |
| 1191 | return offset; |
| 1192 | } |
| 1193 | |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 1194 | size_t OatWriter::InitOatClasses(size_t offset) { |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 1195 | // calculate the offsets within OatDexFiles to OatClasses |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1196 | InitOatClassesMethodVisitor visitor(this, offset); |
| 1197 | bool success = VisitDexMethods(&visitor); |
| 1198 | CHECK(success); |
| 1199 | offset = visitor.GetOffset(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1200 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1201 | // Update oat_dex_files_. |
| 1202 | auto oat_class_it = oat_classes_.begin(); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1203 | for (OatDexFile& oat_dex_file : oat_dex_files_) { |
| 1204 | for (uint32_t& class_offset : oat_dex_file.class_offsets_) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1205 | DCHECK(oat_class_it != oat_classes_.end()); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1206 | class_offset = oat_class_it->offset_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1207 | ++oat_class_it; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1208 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1209 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1210 | CHECK(oat_class_it == oat_classes_.end()); |
| 1211 | |
| 1212 | return offset; |
| 1213 | } |
| 1214 | |
| 1215 | size_t OatWriter::InitOatMaps(size_t offset) { |
| 1216 | #define VISIT(VisitorType) \ |
| 1217 | do { \ |
| 1218 | VisitorType visitor(this, offset); \ |
| 1219 | bool success = VisitDexMethods(&visitor); \ |
| 1220 | DCHECK(success); \ |
| 1221 | offset = visitor.GetOffset(); \ |
| 1222 | } while (false) |
| 1223 | |
| 1224 | VISIT(InitMapMethodVisitor<GcMapDataAccess>); |
| 1225 | VISIT(InitMapMethodVisitor<MappingTableDataAccess>); |
| 1226 | VISIT(InitMapMethodVisitor<VmapTableDataAccess>); |
| 1227 | |
| 1228 | #undef VISIT |
| 1229 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1230 | return offset; |
| 1231 | } |
| 1232 | |
| 1233 | size_t OatWriter::InitOatCode(size_t offset) { |
| 1234 | // calculate the offsets within OatHeader to executable code |
| 1235 | size_t old_offset = offset; |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 1236 | size_t adjusted_offset = offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1237 | // required to be on a new page boundary |
| 1238 | offset = RoundUp(offset, kPageSize); |
| 1239 | oat_header_->SetExecutableOffset(offset); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1240 | size_executable_offset_alignment_ = offset - old_offset; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1241 | if (compiler_driver_->IsBootImage()) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1242 | CHECK_EQ(image_patch_delta_, 0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1243 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1244 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1245 | #define DO_TRAMPOLINE(field, fn_name) \ |
| 1246 | offset = CompiledCode::AlignCode(offset, instruction_set); \ |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 1247 | adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \ |
| 1248 | oat_header_->Set ## fn_name ## Offset(adjusted_offset); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1249 | field.reset(compiler_driver_->Create ## fn_name()); \ |
| 1250 | offset += field->size(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1251 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1252 | DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1253 | DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1254 | DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1255 | DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline); |
| 1256 | DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1257 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1258 | #undef DO_TRAMPOLINE |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1259 | } else { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1260 | oat_header_->SetInterpreterToInterpreterBridgeOffset(0); |
| 1261 | oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0); |
| 1262 | oat_header_->SetJniDlsymLookupOffset(0); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1263 | oat_header_->SetQuickGenericJniTrampolineOffset(0); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1264 | oat_header_->SetQuickImtConflictTrampolineOffset(0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1265 | oat_header_->SetQuickResolutionTrampolineOffset(0); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1266 | oat_header_->SetQuickToInterpreterBridgeOffset(0); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1267 | oat_header_->SetImagePatchDelta(image_patch_delta_); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1268 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1269 | return offset; |
| 1270 | } |
| 1271 | |
| 1272 | size_t OatWriter::InitOatCodeDexFiles(size_t offset) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1273 | #define VISIT(VisitorType) \ |
| 1274 | do { \ |
| 1275 | VisitorType visitor(this, offset); \ |
| 1276 | bool success = VisitDexMethods(&visitor); \ |
| 1277 | DCHECK(success); \ |
| 1278 | offset = visitor.GetOffset(); \ |
| 1279 | } while (false) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1280 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1281 | VISIT(InitCodeMethodVisitor); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1282 | if (compiler_driver_->IsBootImage()) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1283 | VISIT(InitImageMethodVisitor); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1284 | } |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 1285 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1286 | #undef VISIT |
| 1287 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1288 | return offset; |
| 1289 | } |
| 1290 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 1291 | bool OatWriter::WriteRodata(OutputStream* out) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1292 | if (!GetOatDataOffset(out)) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1293 | return false; |
| 1294 | } |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1295 | const size_t file_offset = oat_data_offset_; |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1296 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1297 | // 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] | 1298 | size_t header_size = oat_header_->GetHeaderSize(); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1299 | if (out->Seek(header_size, kSeekCurrent) == static_cast<off_t>(-1)) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1300 | PLOG(ERROR) << "Failed to reserve space for oat header in " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1301 | return false; |
| 1302 | } |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1303 | size_oat_header_ += sizeof(OatHeader); |
| 1304 | size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 1305 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1306 | if (!WriteTables(out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1307 | LOG(ERROR) << "Failed to write oat tables to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1308 | return false; |
| 1309 | } |
| 1310 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1311 | off_t tables_end_offset = out->Seek(0, kSeekCurrent); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1312 | if (tables_end_offset == static_cast<off_t>(-1)) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1313 | LOG(ERROR) << "Failed to seek to oat code position in " << out->GetLocation(); |
| 1314 | return false; |
| 1315 | } |
| 1316 | 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] | 1317 | relative_offset = WriteMaps(out, file_offset, relative_offset); |
| 1318 | if (relative_offset == 0) { |
| 1319 | LOG(ERROR) << "Failed to write oat code to " << out->GetLocation(); |
| 1320 | return false; |
| 1321 | } |
| 1322 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 1323 | // Write padding. |
| 1324 | off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent); |
| 1325 | relative_offset += size_executable_offset_alignment_; |
| 1326 | DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset()); |
| 1327 | size_t expected_file_offset = file_offset + relative_offset; |
| 1328 | if (static_cast<uint32_t>(new_offset) != expected_file_offset) { |
| 1329 | PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset |
| 1330 | << " Expected: " << expected_file_offset << " File: " << out->GetLocation(); |
| 1331 | return 0; |
| 1332 | } |
| 1333 | DCHECK_OFFSET(); |
| 1334 | |
| 1335 | return true; |
| 1336 | } |
| 1337 | |
| 1338 | bool OatWriter::WriteCode(OutputStream* out) { |
| 1339 | size_t header_size = oat_header_->GetHeaderSize(); |
| 1340 | const size_t file_offset = oat_data_offset_; |
| 1341 | size_t relative_offset = oat_header_->GetExecutableOffset(); |
| 1342 | DCHECK_OFFSET(); |
| 1343 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1344 | relative_offset = WriteCode(out, file_offset, relative_offset); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1345 | if (relative_offset == 0) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1346 | LOG(ERROR) << "Failed to write oat code to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1347 | return false; |
| 1348 | } |
| 1349 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1350 | relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset); |
| 1351 | if (relative_offset == 0) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1352 | 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] | 1353 | return false; |
| 1354 | } |
| 1355 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1356 | const off_t oat_end_file_offset = out->Seek(0, kSeekCurrent); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1357 | if (oat_end_file_offset == static_cast<off_t>(-1)) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1358 | LOG(ERROR) << "Failed to get oat end file offset in " << out->GetLocation(); |
| 1359 | return false; |
| 1360 | } |
| 1361 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1362 | if (kIsDebugBuild) { |
| 1363 | uint32_t size_total = 0; |
| 1364 | #define DO_STAT(x) \ |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 1365 | VLOG(compiler) << #x "=" << PrettySize(x) << " (" << x << "B)"; \ |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1366 | size_total += x; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1367 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1368 | DO_STAT(size_dex_file_alignment_); |
| 1369 | DO_STAT(size_executable_offset_alignment_); |
| 1370 | DO_STAT(size_oat_header_); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1371 | DO_STAT(size_oat_header_key_value_store_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1372 | DO_STAT(size_dex_file_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1373 | DO_STAT(size_interpreter_to_interpreter_bridge_); |
| 1374 | DO_STAT(size_interpreter_to_compiled_code_bridge_); |
| 1375 | DO_STAT(size_jni_dlsym_lookup_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1376 | DO_STAT(size_quick_generic_jni_trampoline_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1377 | DO_STAT(size_quick_imt_conflict_trampoline_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1378 | DO_STAT(size_quick_resolution_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1379 | DO_STAT(size_quick_to_interpreter_bridge_); |
| 1380 | DO_STAT(size_trampoline_alignment_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1381 | DO_STAT(size_method_header_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1382 | DO_STAT(size_code_); |
| 1383 | DO_STAT(size_code_alignment_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1384 | DO_STAT(size_relative_call_thunks_); |
Vladimir Marko | c74658b | 2015-03-31 10:26:41 +0100 | [diff] [blame] | 1385 | DO_STAT(size_misc_thunks_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1386 | DO_STAT(size_mapping_table_); |
| 1387 | DO_STAT(size_vmap_table_); |
| 1388 | DO_STAT(size_gc_map_); |
| 1389 | DO_STAT(size_oat_dex_file_location_size_); |
| 1390 | DO_STAT(size_oat_dex_file_location_data_); |
| 1391 | DO_STAT(size_oat_dex_file_location_checksum_); |
| 1392 | DO_STAT(size_oat_dex_file_offset_); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1393 | DO_STAT(size_oat_dex_file_lookup_table_offset_); |
| 1394 | DO_STAT(size_oat_dex_file_class_offsets_); |
| 1395 | DO_STAT(size_oat_lookup_table_alignment_); |
| 1396 | DO_STAT(size_oat_lookup_table_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1397 | DO_STAT(size_oat_class_type_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1398 | DO_STAT(size_oat_class_status_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1399 | DO_STAT(size_oat_class_method_bitmaps_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1400 | DO_STAT(size_oat_class_method_offsets_); |
| 1401 | #undef DO_STAT |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1402 | |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 1403 | VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; \ |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1404 | 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] | 1405 | CHECK_EQ(size_, size_total); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1406 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1407 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1408 | 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] | 1409 | CHECK_EQ(size_, relative_offset); |
| 1410 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1411 | // Finalize the header checksum. |
| 1412 | oat_header_->UpdateChecksumWithHeaderData(); |
| 1413 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1414 | // Write the header now that the checksum is final. |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1415 | if (out->Seek(file_offset, kSeekSet) == static_cast<off_t>(-1)) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1416 | PLOG(ERROR) << "Failed to seek to oat header position in " << out->GetLocation(); |
| 1417 | return false; |
| 1418 | } |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 1419 | DCHECK_EQ(file_offset, static_cast<size_t>(out->Seek(0, kSeekCurrent))); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1420 | if (!out->WriteFully(oat_header_.get(), header_size)) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1421 | PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation(); |
| 1422 | return false; |
| 1423 | } |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1424 | 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] | 1425 | PLOG(ERROR) << "Failed to seek to end after writing oat header to " << out->GetLocation(); |
| 1426 | return false; |
| 1427 | } |
| 1428 | DCHECK_EQ(oat_end_file_offset, out->Seek(0, kSeekCurrent)); |
| 1429 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1430 | return true; |
| 1431 | } |
| 1432 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1433 | bool OatWriter::WriteTables(OutputStream* out, const size_t file_offset) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1434 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1435 | if (!oat_dex_files_[i].Write(this, out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1436 | PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1437 | return false; |
| 1438 | } |
| 1439 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1440 | for (size_t i = 0; i != oat_dex_files_.size(); ++i) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1441 | 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] | 1442 | off_t actual_offset = out->Seek(expected_offset, kSeekSet); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1443 | if (static_cast<uint32_t>(actual_offset) != expected_offset) { |
| 1444 | const DexFile* dex_file = (*dex_files_)[i]; |
| 1445 | PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset |
| 1446 | << " Expected: " << expected_offset << " File: " << dex_file->GetLocation(); |
| 1447 | return false; |
| 1448 | } |
| 1449 | const DexFile* dex_file = (*dex_files_)[i]; |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1450 | if (!out->WriteFully(&dex_file->GetHeader(), dex_file->GetHeader().file_size_)) { |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1451 | PLOG(ERROR) << "Failed to write dex file " << dex_file->GetLocation() |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1452 | << " to " << out->GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1453 | return false; |
| 1454 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1455 | size_dex_file_ += dex_file->GetHeader().file_size_; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1456 | } |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 1457 | if (!WriteLookupTables(out, file_offset)) { |
| 1458 | return false; |
| 1459 | } |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 1460 | for (size_t i = 0; i != oat_classes_.size(); ++i) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1461 | if (!oat_classes_[i].Write(this, out, file_offset)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1462 | PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1463 | return false; |
| 1464 | } |
| 1465 | } |
| 1466 | return true; |
| 1467 | } |
| 1468 | |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 1469 | bool OatWriter::WriteLookupTables(OutputStream* out, const size_t file_offset) { |
| 1470 | for (size_t i = 0; i < oat_dex_files_.size(); ++i) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1471 | const uint32_t lookup_table_offset = oat_dex_files_[i].lookup_table_offset_; |
| 1472 | const TypeLookupTable* table = oat_dex_files_[i].lookup_table_; |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 1473 | DCHECK_EQ(lookup_table_offset == 0, table == nullptr); |
| 1474 | if (lookup_table_offset == 0) { |
| 1475 | continue; |
| 1476 | } |
| 1477 | const uint32_t expected_offset = file_offset + lookup_table_offset; |
| 1478 | off_t actual_offset = out->Seek(expected_offset, kSeekSet); |
| 1479 | if (static_cast<uint32_t>(actual_offset) != expected_offset) { |
| 1480 | const DexFile* dex_file = (*dex_files_)[i]; |
| 1481 | PLOG(ERROR) << "Failed to seek to lookup table section. Actual: " << actual_offset |
| 1482 | << " Expected: " << expected_offset << " File: " << dex_file->GetLocation(); |
| 1483 | return false; |
| 1484 | } |
| 1485 | if (table != nullptr) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1486 | if (!WriteData(out, table->RawData(), table->RawDataLength())) { |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 1487 | const DexFile* dex_file = (*dex_files_)[i]; |
| 1488 | PLOG(ERROR) << "Failed to write lookup table for " << dex_file->GetLocation() |
| 1489 | << " to " << out->GetLocation(); |
| 1490 | return false; |
| 1491 | } |
| 1492 | size_oat_lookup_table_ += table->RawDataLength(); |
| 1493 | } |
| 1494 | } |
| 1495 | return true; |
| 1496 | } |
| 1497 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1498 | size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) { |
| 1499 | #define VISIT(VisitorType) \ |
| 1500 | do { \ |
| 1501 | VisitorType visitor(this, out, file_offset, relative_offset); \ |
| 1502 | if (UNLIKELY(!VisitDexMethods(&visitor))) { \ |
| 1503 | return 0; \ |
| 1504 | } \ |
| 1505 | relative_offset = visitor.GetOffset(); \ |
| 1506 | } while (false) |
| 1507 | |
| 1508 | size_t gc_maps_offset = relative_offset; |
| 1509 | VISIT(WriteMapMethodVisitor<GcMapDataAccess>); |
| 1510 | size_gc_map_ = relative_offset - gc_maps_offset; |
| 1511 | |
| 1512 | size_t mapping_tables_offset = relative_offset; |
| 1513 | VISIT(WriteMapMethodVisitor<MappingTableDataAccess>); |
| 1514 | size_mapping_table_ = relative_offset - mapping_tables_offset; |
| 1515 | |
| 1516 | size_t vmap_tables_offset = relative_offset; |
| 1517 | VISIT(WriteMapMethodVisitor<VmapTableDataAccess>); |
| 1518 | size_vmap_table_ = relative_offset - vmap_tables_offset; |
| 1519 | |
| 1520 | #undef VISIT |
| 1521 | |
| 1522 | return relative_offset; |
| 1523 | } |
| 1524 | |
| 1525 | 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] | 1526 | if (compiler_driver_->IsBootImage()) { |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1527 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1528 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1529 | #define DO_TRAMPOLINE(field) \ |
| 1530 | do { \ |
| 1531 | uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \ |
| 1532 | uint32_t alignment_padding = aligned_offset - relative_offset; \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1533 | out->Seek(alignment_padding, kSeekCurrent); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1534 | size_trampoline_alignment_ += alignment_padding; \ |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1535 | if (!WriteData(out, field->data(), field->size())) { \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1536 | PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1537 | return false; \ |
| 1538 | } \ |
| 1539 | size_ ## field += field->size(); \ |
| 1540 | relative_offset += alignment_padding + field->size(); \ |
| 1541 | DCHECK_OFFSET(); \ |
| 1542 | } while (false) |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1543 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1544 | DO_TRAMPOLINE(jni_dlsym_lookup_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1545 | DO_TRAMPOLINE(quick_generic_jni_trampoline_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1546 | DO_TRAMPOLINE(quick_imt_conflict_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1547 | DO_TRAMPOLINE(quick_resolution_trampoline_); |
| 1548 | DO_TRAMPOLINE(quick_to_interpreter_bridge_); |
| 1549 | #undef DO_TRAMPOLINE |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1550 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1551 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1552 | } |
| 1553 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1554 | size_t OatWriter::WriteCodeDexFiles(OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1555 | const size_t file_offset, |
| 1556 | size_t relative_offset) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1557 | #define VISIT(VisitorType) \ |
| 1558 | do { \ |
| 1559 | VisitorType visitor(this, out, file_offset, relative_offset); \ |
| 1560 | if (UNLIKELY(!VisitDexMethods(&visitor))) { \ |
| 1561 | return 0; \ |
| 1562 | } \ |
| 1563 | relative_offset = visitor.GetOffset(); \ |
| 1564 | } while (false) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1565 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1566 | VISIT(WriteCodeMethodVisitor); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1567 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1568 | #undef VISIT |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1569 | |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 1570 | size_code_alignment_ += relative_patcher_->CodeAlignmentSize(); |
| 1571 | size_relative_call_thunks_ += relative_patcher_->RelativeCallThunksSize(); |
| 1572 | size_misc_thunks_ += relative_patcher_->MiscThunksSize(); |
| 1573 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1574 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1575 | } |
| 1576 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1577 | bool OatWriter::GetOatDataOffset(OutputStream* out) { |
| 1578 | // Get the elf file offset of the oat file. |
| 1579 | const off_t raw_file_offset = out->Seek(0, kSeekCurrent); |
| 1580 | if (raw_file_offset == static_cast<off_t>(-1)) { |
| 1581 | LOG(ERROR) << "Failed to get file offset in " << out->GetLocation(); |
| 1582 | return false; |
| 1583 | } |
| 1584 | oat_data_offset_ = static_cast<size_t>(raw_file_offset); |
| 1585 | return true; |
| 1586 | } |
| 1587 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1588 | bool OatWriter::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) { |
| 1589 | static const uint8_t kPadding[] = { |
| 1590 | 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u |
| 1591 | }; |
| 1592 | DCHECK_LE(aligned_code_delta, sizeof(kPadding)); |
| 1593 | if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) { |
| 1594 | return false; |
| 1595 | } |
| 1596 | size_code_alignment_ += aligned_code_delta; |
| 1597 | return true; |
| 1598 | } |
| 1599 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1600 | bool OatWriter::WriteData(OutputStream* out, const void* data, size_t size) { |
| 1601 | oat_header_->UpdateChecksum(data, size); |
| 1602 | return out->WriteFully(data, size); |
| 1603 | } |
| 1604 | |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 1605 | std::pair<bool, uint32_t> OatWriter::MethodOffsetMap::FindMethodOffset(MethodReference ref) { |
| 1606 | auto it = map.find(ref); |
| 1607 | if (it == map.end()) { |
| 1608 | return std::pair<bool, uint32_t>(false, 0u); |
| 1609 | } else { |
| 1610 | return std::pair<bool, uint32_t>(true, it->second); |
| 1611 | } |
| 1612 | } |
| 1613 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1614 | OatWriter::OatDexFile::OatDexFile(size_t offset, const DexFile& dex_file) { |
| 1615 | offset_ = offset; |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 1616 | const std::string& location(dex_file.GetLocation()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1617 | dex_file_location_size_ = location.size(); |
| 1618 | dex_file_location_data_ = reinterpret_cast<const uint8_t*>(location.data()); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1619 | dex_file_location_checksum_ = dex_file.GetLocationChecksum(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1620 | dex_file_offset_ = 0; |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 1621 | lookup_table_offset_ = 0; |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1622 | class_offsets_.resize(dex_file.NumClassDefs()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | size_t OatWriter::OatDexFile::SizeOf() const { |
| 1626 | return sizeof(dex_file_location_size_) |
| 1627 | + dex_file_location_size_ |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1628 | + sizeof(dex_file_location_checksum_) |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1629 | + sizeof(dex_file_offset_) |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 1630 | + sizeof(lookup_table_offset_) |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1631 | + (sizeof(class_offsets_[0]) * class_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1632 | } |
| 1633 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1634 | bool OatWriter::OatDexFile::Write(OatWriter* oat_writer, |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1635 | OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1636 | const size_t file_offset) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1637 | DCHECK_OFFSET_(); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1638 | 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] | 1639 | PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [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_location_size_ += sizeof(dex_file_location_size_); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1643 | 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] | 1644 | PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1645 | return false; |
| 1646 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1647 | oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_; |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1648 | if (!oat_writer->WriteData(out, |
| 1649 | &dex_file_location_checksum_, |
| 1650 | sizeof(dex_file_location_checksum_))) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1651 | PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1652 | return false; |
| 1653 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1654 | 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] | 1655 | if (!oat_writer->WriteData(out, &dex_file_offset_, sizeof(dex_file_offset_))) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1656 | PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 1657 | return false; |
| 1658 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1659 | oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1660 | if (!oat_writer->WriteData(out, &lookup_table_offset_, sizeof(lookup_table_offset_))) { |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 1661 | PLOG(ERROR) << "Failed to write lookup table offset to " << out->GetLocation(); |
| 1662 | return false; |
| 1663 | } |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1664 | oat_writer->size_oat_dex_file_lookup_table_offset_ += sizeof(lookup_table_offset_); |
| 1665 | if (!oat_writer->WriteData(out, class_offsets_.data(), GetClassOffsetsRawSize())) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1666 | PLOG(ERROR) << "Failed to write methods offsets to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1667 | return false; |
| 1668 | } |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1669 | oat_writer->size_oat_dex_file_class_offsets_ += GetClassOffsetsRawSize(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1670 | return true; |
| 1671 | } |
| 1672 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1673 | OatWriter::OatClass::OatClass(size_t offset, |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1674 | const dchecked_vector<CompiledMethod*>& compiled_methods, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1675 | uint32_t num_non_null_compiled_methods, |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1676 | mirror::Class::Status status) |
| 1677 | : compiled_methods_(compiled_methods) { |
| 1678 | uint32_t num_methods = compiled_methods.size(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1679 | CHECK_LE(num_non_null_compiled_methods, num_methods); |
| 1680 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1681 | offset_ = offset; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1682 | oat_method_offsets_offsets_from_oat_class_.resize(num_methods); |
| 1683 | |
| 1684 | // Since both kOatClassNoneCompiled and kOatClassAllCompiled could |
| 1685 | // apply when there are 0 methods, we just arbitrarily say that 0 |
| 1686 | // methods means kOatClassNoneCompiled and that we won't use |
| 1687 | // kOatClassAllCompiled unless there is at least one compiled |
| 1688 | // method. This means in an interpretter only system, we can assert |
| 1689 | // that all classes are kOatClassNoneCompiled. |
| 1690 | if (num_non_null_compiled_methods == 0) { |
| 1691 | type_ = kOatClassNoneCompiled; |
| 1692 | } else if (num_non_null_compiled_methods == num_methods) { |
| 1693 | type_ = kOatClassAllCompiled; |
| 1694 | } else { |
| 1695 | type_ = kOatClassSomeCompiled; |
| 1696 | } |
| 1697 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1698 | status_ = status; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1699 | method_offsets_.resize(num_non_null_compiled_methods); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 1700 | method_headers_.resize(num_non_null_compiled_methods); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1701 | |
| 1702 | uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_); |
| 1703 | if (type_ == kOatClassSomeCompiled) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1704 | method_bitmap_.reset(new BitVector(num_methods, false, Allocator::GetMallocAllocator())); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1705 | method_bitmap_size_ = method_bitmap_->GetSizeOf(); |
| 1706 | oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_); |
| 1707 | oat_method_offsets_offset_from_oat_class += method_bitmap_size_; |
| 1708 | } else { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1709 | method_bitmap_ = nullptr; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1710 | method_bitmap_size_ = 0; |
| 1711 | } |
| 1712 | |
| 1713 | for (size_t i = 0; i < num_methods; i++) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1714 | CompiledMethod* compiled_method = compiled_methods_[i]; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1715 | if (compiled_method == nullptr) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1716 | oat_method_offsets_offsets_from_oat_class_[i] = 0; |
| 1717 | } else { |
| 1718 | oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class; |
| 1719 | oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets); |
| 1720 | if (type_ == kOatClassSomeCompiled) { |
| 1721 | method_bitmap_->SetBit(i); |
| 1722 | } |
| 1723 | } |
| 1724 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1725 | } |
| 1726 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1727 | size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader( |
| 1728 | size_t class_def_method_index_) const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1729 | uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_); |
| 1730 | if (method_offset == 0) { |
| 1731 | return 0; |
| 1732 | } |
| 1733 | return offset_ + method_offset; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass( |
| 1737 | size_t class_def_method_index_) const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1738 | return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_]; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | size_t OatWriter::OatClass::SizeOf() const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1742 | return sizeof(status_) |
| 1743 | + sizeof(type_) |
| 1744 | + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_)) |
| 1745 | + method_bitmap_size_ |
| 1746 | + (sizeof(method_offsets_[0]) * method_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1747 | } |
| 1748 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1749 | bool OatWriter::OatClass::Write(OatWriter* oat_writer, |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1750 | OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1751 | const size_t file_offset) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1752 | DCHECK_OFFSET_(); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1753 | if (!oat_writer->WriteData(out, &status_, sizeof(status_))) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1754 | PLOG(ERROR) << "Failed to write class status to " << out->GetLocation(); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 1755 | return false; |
| 1756 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1757 | oat_writer->size_oat_class_status_ += sizeof(status_); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1758 | |
| 1759 | if (!oat_writer->WriteData(out, &type_, sizeof(type_))) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1760 | PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1761 | return false; |
| 1762 | } |
| 1763 | oat_writer->size_oat_class_type_ += sizeof(type_); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1764 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1765 | if (method_bitmap_size_ != 0) { |
| 1766 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1767 | if (!oat_writer->WriteData(out, &method_bitmap_size_, sizeof(method_bitmap_size_))) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1768 | PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1769 | return false; |
| 1770 | } |
| 1771 | oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1772 | |
| 1773 | if (!oat_writer->WriteData(out, method_bitmap_->GetRawStorage(), method_bitmap_size_)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1774 | PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1775 | return false; |
| 1776 | } |
| 1777 | oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_; |
| 1778 | } |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1779 | |
| 1780 | if (!oat_writer->WriteData(out, method_offsets_.data(), GetMethodOffsetsRawSize())) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1781 | PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1782 | return false; |
| 1783 | } |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1784 | oat_writer->size_oat_class_method_offsets_ += GetMethodOffsetsRawSize(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1785 | return true; |
| 1786 | } |
| 1787 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1788 | } // namespace art |