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 | |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 20 | #include <unistd.h> |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 21 | #include <zlib.h> |
| 22 | |
Vladimir Marko | c74658b | 2015-03-31 10:26:41 +0100 | [diff] [blame] | 23 | #include "arch/arm64/instruction_set_features_arm64.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 24 | #include "art_method-inl.h" |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 25 | #include "base/allocator.h" |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 26 | #include "base/bit_vector-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 27 | #include "base/enums.h" |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 28 | #include "base/file_magic.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 29 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 30 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 31 | #include "class_linker.h" |
Vladimir Marko | 94ec2db | 2017-09-06 17:21:03 +0100 | [diff] [blame] | 32 | #include "class_table-inl.h" |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 33 | #include "compiled_method-inl.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 34 | #include "debug/method_debug_info.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 35 | #include "dex/verification_results.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 36 | #include "dex_file-inl.h" |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 37 | #include "dex_file_loader.h" |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 38 | #include "dex_file_types.h" |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 39 | #include "dexlayout.h" |
Andreas Gampe | d482e73 | 2017-04-24 17:59:09 -0700 | [diff] [blame] | 40 | #include "driver/compiler_driver-inl.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 41 | #include "driver/compiler_options.h" |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 42 | #include "gc/space/image_space.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 43 | #include "gc/space/space.h" |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 44 | #include "handle_scope-inl.h" |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 45 | #include "image_writer.h" |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 46 | #include "linker/buffered_output_stream.h" |
| 47 | #include "linker/file_output_stream.h" |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 48 | #include "linker/index_bss_mapping_encoder.h" |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 49 | #include "linker/linker_patch.h" |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 50 | #include "linker/multi_oat_relative_patcher.h" |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 51 | #include "linker/output_stream.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 52 | #include "mirror/array.h" |
| 53 | #include "mirror/class_loader.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 54 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 55 | #include "mirror/object-inl.h" |
Mathieu Chartier | 292567e | 2017-10-12 13:24:38 -0700 | [diff] [blame] | 56 | #include "standard_dex_file.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 57 | #include "oat_quick_method_header.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 58 | #include "os.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 59 | #include "safe_map.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 60 | #include "scoped_thread_state_change-inl.h" |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 61 | #include "type_lookup_table.h" |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 62 | #include "utils/dex_cache_arrays_layout-inl.h" |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 63 | #include "vdex_file.h" |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 64 | #include "verifier/verifier_deps.h" |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 65 | #include "zip_archive.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 66 | |
| 67 | namespace art { |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 68 | namespace linker { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 69 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 70 | namespace { // anonymous namespace |
| 71 | |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 72 | // If we write dex layout info in the oat file. |
| 73 | static constexpr bool kWriteDexLayoutInfo = true; |
| 74 | |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 75 | // Force the OAT method layout to be sorted-by-name instead of |
| 76 | // the default (class_def_idx, method_idx). |
| 77 | // |
| 78 | // Otherwise if profiles are used, that will act as |
| 79 | // the primary sort order. |
| 80 | // |
| 81 | // A bit easier to use for development since oatdump can easily |
| 82 | // show that things are being re-ordered when two methods aren't adjacent. |
| 83 | static constexpr bool kOatWriterForceOatCodeLayout = false; |
| 84 | |
| 85 | static constexpr bool kOatWriterDebugOatCodeLayout = false; |
| 86 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 87 | typedef DexFile::Header __attribute__((aligned(1))) UnalignedDexFileHeader; |
| 88 | |
| 89 | const UnalignedDexFileHeader* AsUnalignedDexFileHeader(const uint8_t* raw_data) { |
| 90 | return reinterpret_cast<const UnalignedDexFileHeader*>(raw_data); |
| 91 | } |
| 92 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 93 | class ChecksumUpdatingOutputStream : public OutputStream { |
| 94 | public: |
| 95 | ChecksumUpdatingOutputStream(OutputStream* out, OatHeader* oat_header) |
| 96 | : OutputStream(out->GetLocation()), out_(out), oat_header_(oat_header) { } |
| 97 | |
| 98 | bool WriteFully(const void* buffer, size_t byte_count) OVERRIDE { |
| 99 | oat_header_->UpdateChecksum(buffer, byte_count); |
| 100 | return out_->WriteFully(buffer, byte_count); |
| 101 | } |
| 102 | |
| 103 | off_t Seek(off_t offset, Whence whence) OVERRIDE { |
| 104 | return out_->Seek(offset, whence); |
| 105 | } |
| 106 | |
| 107 | bool Flush() OVERRIDE { |
| 108 | return out_->Flush(); |
| 109 | } |
| 110 | |
| 111 | private: |
| 112 | OutputStream* const out_; |
| 113 | OatHeader* const oat_header_; |
| 114 | }; |
| 115 | |
Vladimir Marko | 0c737df | 2016-08-01 16:33:16 +0100 | [diff] [blame] | 116 | inline uint32_t CodeAlignmentSize(uint32_t header_offset, const CompiledMethod& compiled_method) { |
| 117 | // We want to align the code rather than the preheader. |
| 118 | uint32_t unaligned_code_offset = header_offset + sizeof(OatQuickMethodHeader); |
| 119 | uint32_t aligned_code_offset = compiled_method.AlignCode(unaligned_code_offset); |
| 120 | return aligned_code_offset - unaligned_code_offset; |
| 121 | } |
| 122 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 123 | } // anonymous namespace |
| 124 | |
| 125 | // Defines the location of the raw dex file to write. |
| 126 | class OatWriter::DexFileSource { |
| 127 | public: |
Mathieu Chartier | 497d526 | 2017-02-28 20:17:30 -0800 | [diff] [blame] | 128 | enum Type { |
| 129 | kNone, |
| 130 | kZipEntry, |
| 131 | kRawFile, |
| 132 | kRawData, |
| 133 | }; |
| 134 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 135 | explicit DexFileSource(ZipEntry* zip_entry) |
| 136 | : type_(kZipEntry), source_(zip_entry) { |
| 137 | DCHECK(source_ != nullptr); |
| 138 | } |
| 139 | |
| 140 | explicit DexFileSource(File* raw_file) |
| 141 | : type_(kRawFile), source_(raw_file) { |
| 142 | DCHECK(source_ != nullptr); |
| 143 | } |
| 144 | |
| 145 | explicit DexFileSource(const uint8_t* dex_file) |
| 146 | : type_(kRawData), source_(dex_file) { |
| 147 | DCHECK(source_ != nullptr); |
| 148 | } |
| 149 | |
Mathieu Chartier | 497d526 | 2017-02-28 20:17:30 -0800 | [diff] [blame] | 150 | Type GetType() const { return type_; } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 151 | bool IsZipEntry() const { return type_ == kZipEntry; } |
| 152 | bool IsRawFile() const { return type_ == kRawFile; } |
| 153 | bool IsRawData() const { return type_ == kRawData; } |
| 154 | |
| 155 | ZipEntry* GetZipEntry() const { |
| 156 | DCHECK(IsZipEntry()); |
| 157 | DCHECK(source_ != nullptr); |
| 158 | return static_cast<ZipEntry*>(const_cast<void*>(source_)); |
| 159 | } |
| 160 | |
| 161 | File* GetRawFile() const { |
| 162 | DCHECK(IsRawFile()); |
| 163 | DCHECK(source_ != nullptr); |
| 164 | return static_cast<File*>(const_cast<void*>(source_)); |
| 165 | } |
| 166 | |
| 167 | const uint8_t* GetRawData() const { |
| 168 | DCHECK(IsRawData()); |
| 169 | DCHECK(source_ != nullptr); |
| 170 | return static_cast<const uint8_t*>(source_); |
| 171 | } |
| 172 | |
| 173 | void Clear() { |
| 174 | type_ = kNone; |
| 175 | source_ = nullptr; |
| 176 | } |
| 177 | |
| 178 | private: |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 179 | Type type_; |
| 180 | const void* source_; |
| 181 | }; |
| 182 | |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 183 | // OatClassHeader is the header only part of the oat class that is required even when compilation |
| 184 | // is not enabled. |
| 185 | class OatWriter::OatClassHeader { |
| 186 | public: |
| 187 | OatClassHeader(uint32_t offset, |
| 188 | uint32_t num_non_null_compiled_methods, |
| 189 | uint32_t num_methods, |
| 190 | mirror::Class::Status status) |
| 191 | : status_(status), |
| 192 | offset_(offset) { |
| 193 | // We just arbitrarily say that 0 methods means kOatClassNoneCompiled and that we won't use |
| 194 | // kOatClassAllCompiled unless there is at least one compiled method. This means in an |
| 195 | // interpreter only system, we can assert that all classes are kOatClassNoneCompiled. |
| 196 | if (num_non_null_compiled_methods == 0) { |
| 197 | type_ = kOatClassNoneCompiled; |
| 198 | } else if (num_non_null_compiled_methods == num_methods) { |
| 199 | type_ = kOatClassAllCompiled; |
| 200 | } else { |
| 201 | type_ = kOatClassSomeCompiled; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const; |
| 206 | |
| 207 | static size_t SizeOf() { |
| 208 | return sizeof(status_) + sizeof(type_); |
| 209 | } |
| 210 | |
| 211 | // Data to write. |
| 212 | static_assert(mirror::Class::Status::kStatusMax < (1 << 16), "class status won't fit in 16bits"); |
| 213 | int16_t status_; |
| 214 | |
| 215 | static_assert(OatClassType::kOatClassMax < (1 << 16), "oat_class type won't fit in 16bits"); |
| 216 | uint16_t type_; |
| 217 | |
| 218 | // Offset of start of OatClass from beginning of OatHeader. It is |
| 219 | // used to validate file position when writing. |
| 220 | uint32_t offset_; |
| 221 | }; |
| 222 | |
| 223 | // The actual oat class body contains the information about compiled methods. It is only required |
| 224 | // for compiler filters that have any compilation. |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 225 | class OatWriter::OatClass { |
| 226 | public: |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 227 | OatClass(const dchecked_vector<CompiledMethod*>& compiled_methods, |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 228 | uint32_t compiled_methods_with_code, |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 229 | uint16_t oat_class_type); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 230 | OatClass(OatClass&& src) = default; |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 231 | size_t SizeOf() const; |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 232 | bool Write(OatWriter* oat_writer, OutputStream* out) const; |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 233 | |
| 234 | CompiledMethod* GetCompiledMethod(size_t class_def_method_index) const { |
| 235 | return compiled_methods_[class_def_method_index]; |
| 236 | } |
| 237 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 238 | // CompiledMethods for each class_def_method_index, or null if no method is available. |
| 239 | dchecked_vector<CompiledMethod*> compiled_methods_; |
| 240 | |
| 241 | // Offset from OatClass::offset_ to the OatMethodOffsets for the |
| 242 | // class_def_method_index. If 0, it means the corresponding |
| 243 | // CompiledMethod entry in OatClass::compiled_methods_ should be |
| 244 | // null and that the OatClass::type_ should be kOatClassBitmap. |
| 245 | dchecked_vector<uint32_t> oat_method_offsets_offsets_from_oat_class_; |
| 246 | |
| 247 | // Data to write. |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 248 | uint32_t method_bitmap_size_; |
| 249 | |
| 250 | // bit vector indexed by ClassDef method index. When |
| 251 | // OatClassType::type_ is kOatClassBitmap, a set bit indicates the |
| 252 | // method has an OatMethodOffsets in methods_offsets_, otherwise |
| 253 | // the entry was ommited to save space. If OatClassType::type_ is |
| 254 | // not is kOatClassBitmap, the bitmap will be null. |
| 255 | std::unique_ptr<BitVector> method_bitmap_; |
| 256 | |
| 257 | // OatMethodOffsets and OatMethodHeaders for each CompiledMethod |
| 258 | // present in the OatClass. Note that some may be missing if |
| 259 | // OatClass::compiled_methods_ contains null values (and |
| 260 | // oat_method_offsets_offsets_from_oat_class_ should contain 0 |
| 261 | // values in this case). |
| 262 | dchecked_vector<OatMethodOffsets> method_offsets_; |
| 263 | dchecked_vector<OatQuickMethodHeader> method_headers_; |
| 264 | |
| 265 | private: |
| 266 | size_t GetMethodOffsetsRawSize() const { |
| 267 | return method_offsets_.size() * sizeof(method_offsets_[0]); |
| 268 | } |
| 269 | |
| 270 | DISALLOW_COPY_AND_ASSIGN(OatClass); |
| 271 | }; |
| 272 | |
| 273 | class OatWriter::OatDexFile { |
| 274 | public: |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 275 | OatDexFile(const char* dex_file_location, |
| 276 | DexFileSource source, |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 277 | CreateTypeLookupTable create_type_lookup_table, |
| 278 | uint32_t dex_file_location_checksun, |
| 279 | size_t dex_file_size); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 280 | OatDexFile(OatDexFile&& src) = default; |
| 281 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 282 | const char* GetLocation() const { |
| 283 | return dex_file_location_data_; |
| 284 | } |
| 285 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 286 | size_t SizeOf() const; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 287 | bool Write(OatWriter* oat_writer, OutputStream* out) const; |
| 288 | bool WriteClassOffsets(OatWriter* oat_writer, OutputStream* out); |
| 289 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 290 | size_t GetClassOffsetsRawSize() const { |
| 291 | return class_offsets_.size() * sizeof(class_offsets_[0]); |
| 292 | } |
| 293 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 294 | // The source of the dex file. |
| 295 | DexFileSource source_; |
| 296 | |
| 297 | // Whether to create the type lookup table. |
| 298 | CreateTypeLookupTable create_type_lookup_table_; |
| 299 | |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 300 | // Dex file size. Passed in the constructor, but could be |
| 301 | // overwritten by LayoutAndWriteDexFile. |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 302 | size_t dex_file_size_; |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 303 | |
| 304 | // Offset of start of OatDexFile from beginning of OatHeader. It is |
| 305 | // used to validate file position when writing. |
| 306 | size_t offset_; |
| 307 | |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 308 | ///// Start of data to write to vdex/oat file. |
| 309 | |
| 310 | const uint32_t dex_file_location_size_; |
| 311 | const char* const dex_file_location_data_; |
| 312 | |
| 313 | // The checksum of the dex file. |
| 314 | const uint32_t dex_file_location_checksum_; |
| 315 | |
| 316 | // Offset of the dex file in the vdex file. Set when writing dex files in |
| 317 | // SeekToDexFile. |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 318 | uint32_t dex_file_offset_; |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 319 | |
| 320 | // The lookup table offset in the oat file. Set in WriteTypeLookupTables. |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 321 | uint32_t lookup_table_offset_; |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 322 | |
| 323 | // Class and BSS offsets set in PrepareLayout. |
| 324 | uint32_t class_offsets_offset_; |
Nicolas Geoffray | 715d672 | 2017-11-20 22:28:46 +0000 | [diff] [blame] | 325 | uint32_t method_bss_mapping_offset_; |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 326 | uint32_t type_bss_mapping_offset_; |
| 327 | uint32_t string_bss_mapping_offset_; |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 328 | |
| 329 | // Offset of dex sections that will have different runtime madvise states. |
| 330 | // Set in WriteDexLayoutSections. |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 331 | uint32_t dex_sections_layout_offset_; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 332 | |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 333 | // Data to write to a separate section. We set the length |
| 334 | // of the vector in OpenDexFiles. |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 335 | dchecked_vector<uint32_t> class_offsets_; |
| 336 | |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 337 | // Dex section layout info to serialize. |
| 338 | DexLayoutSections dex_sections_layout_; |
| 339 | |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 340 | ///// End of data to write to vdex/oat file. |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 341 | private: |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 342 | DISALLOW_COPY_AND_ASSIGN(OatDexFile); |
| 343 | }; |
| 344 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 345 | #define DCHECK_OFFSET() \ |
| 346 | DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out->Seek(0, kSeekCurrent)) \ |
| 347 | << "file_offset=" << file_offset << " relative_offset=" << relative_offset |
| 348 | |
| 349 | #define DCHECK_OFFSET_() \ |
| 350 | DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out->Seek(0, kSeekCurrent)) \ |
| 351 | << "file_offset=" << file_offset << " offset_=" << offset_ |
| 352 | |
Mathieu Chartier | 603ccab | 2017-10-20 14:34:28 -0700 | [diff] [blame] | 353 | OatWriter::OatWriter(bool compiling_boot_image, |
| 354 | TimingLogger* timings, |
| 355 | ProfileCompilationInfo* info, |
| 356 | CompactDexLevel compact_dex_level) |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 357 | : write_state_(WriteState::kAddingDexFileSources), |
| 358 | timings_(timings), |
| 359 | raw_dex_files_(), |
| 360 | zip_archives_(), |
| 361 | zipped_dex_files_(), |
| 362 | zipped_dex_file_locations_(), |
| 363 | compiler_driver_(nullptr), |
| 364 | image_writer_(nullptr), |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 365 | compiling_boot_image_(compiling_boot_image), |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 366 | dex_files_(nullptr), |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 367 | vdex_size_(0u), |
| 368 | vdex_dex_files_offset_(0u), |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 369 | vdex_verifier_deps_offset_(0u), |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 370 | vdex_quickening_info_offset_(0u), |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 371 | oat_size_(0u), |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 372 | bss_start_(0u), |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 373 | bss_size_(0u), |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 374 | bss_methods_offset_(0u), |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 375 | bss_roots_offset_(0u), |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 376 | bss_method_entry_references_(), |
| 377 | bss_method_entries_(), |
Vladimir Marko | 6bec91c | 2017-01-09 15:03:12 +0000 | [diff] [blame] | 378 | bss_type_entries_(), |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 379 | bss_string_entries_(), |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 380 | map_boot_image_tables_to_bss_(false), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 381 | oat_data_offset_(0u), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 382 | oat_header_(nullptr), |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 383 | size_vdex_header_(0), |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 384 | size_vdex_checksums_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 385 | size_dex_file_alignment_(0), |
| 386 | size_executable_offset_alignment_(0), |
| 387 | size_oat_header_(0), |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 388 | size_oat_header_key_value_store_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 389 | size_dex_file_(0), |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 390 | size_verifier_deps_(0), |
| 391 | size_verifier_deps_alignment_(0), |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 392 | size_quickening_info_(0), |
| 393 | size_quickening_info_alignment_(0), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 394 | size_interpreter_to_interpreter_bridge_(0), |
| 395 | size_interpreter_to_compiled_code_bridge_(0), |
| 396 | size_jni_dlsym_lookup_(0), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 397 | size_quick_generic_jni_trampoline_(0), |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 398 | size_quick_imt_conflict_trampoline_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 399 | size_quick_resolution_trampoline_(0), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 400 | size_quick_to_interpreter_bridge_(0), |
| 401 | size_trampoline_alignment_(0), |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 402 | size_method_header_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 403 | size_code_(0), |
| 404 | size_code_alignment_(0), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 405 | size_relative_call_thunks_(0), |
Vladimir Marko | c74658b | 2015-03-31 10:26:41 +0100 | [diff] [blame] | 406 | size_misc_thunks_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 407 | size_vmap_table_(0), |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 408 | size_method_info_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 409 | size_oat_dex_file_location_size_(0), |
| 410 | size_oat_dex_file_location_data_(0), |
| 411 | size_oat_dex_file_location_checksum_(0), |
| 412 | size_oat_dex_file_offset_(0), |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 413 | size_oat_dex_file_class_offsets_offset_(0), |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 414 | size_oat_dex_file_lookup_table_offset_(0), |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 415 | size_oat_dex_file_dex_layout_sections_offset_(0), |
| 416 | size_oat_dex_file_dex_layout_sections_(0), |
| 417 | size_oat_dex_file_dex_layout_sections_alignment_(0), |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 418 | size_oat_dex_file_method_bss_mapping_offset_(0), |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 419 | size_oat_dex_file_type_bss_mapping_offset_(0), |
| 420 | size_oat_dex_file_string_bss_mapping_offset_(0), |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 421 | size_oat_lookup_table_alignment_(0), |
| 422 | size_oat_lookup_table_(0), |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 423 | size_oat_class_offsets_alignment_(0), |
| 424 | size_oat_class_offsets_(0), |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 425 | size_oat_class_type_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 426 | size_oat_class_status_(0), |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 427 | size_oat_class_method_bitmaps_(0), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 428 | size_oat_class_method_offsets_(0), |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 429 | size_method_bss_mappings_(0u), |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 430 | size_type_bss_mappings_(0u), |
| 431 | size_string_bss_mappings_(0u), |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 432 | relative_patcher_(nullptr), |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 433 | absolute_patch_locations_(), |
Mathieu Chartier | 603ccab | 2017-10-20 14:34:28 -0700 | [diff] [blame] | 434 | profile_compilation_info_(info), |
| 435 | compact_dex_level_(compact_dex_level) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 438 | static bool ValidateDexFileHeader(const uint8_t* raw_header, const char* location) { |
| 439 | const bool valid_standard_dex_magic = DexFileLoader::IsMagicValid(raw_header); |
| 440 | if (!valid_standard_dex_magic) { |
| 441 | LOG(ERROR) << "Invalid magic number in dex file header. " << " File: " << location; |
| 442 | return false; |
| 443 | } |
| 444 | if (!DexFileLoader::IsVersionAndMagicValid(raw_header)) { |
| 445 | LOG(ERROR) << "Invalid version number in dex file header. " << " File: " << location; |
| 446 | return false; |
| 447 | } |
| 448 | const UnalignedDexFileHeader* header = AsUnalignedDexFileHeader(raw_header); |
| 449 | if (header->file_size_ < sizeof(DexFile::Header)) { |
| 450 | LOG(ERROR) << "Dex file header specifies file size insufficient to contain the header." |
| 451 | << " File: " << location; |
| 452 | return false; |
| 453 | } |
| 454 | return true; |
| 455 | } |
| 456 | |
| 457 | static const UnalignedDexFileHeader* GetDexFileHeader(File* file, |
| 458 | uint8_t* raw_header, |
| 459 | const char* location) { |
| 460 | // Read the dex file header and perform minimal verification. |
| 461 | if (!file->ReadFully(raw_header, sizeof(DexFile::Header))) { |
| 462 | PLOG(ERROR) << "Failed to read dex file header. Actual: " |
| 463 | << " File: " << location << " Output: " << file->GetPath(); |
| 464 | return nullptr; |
| 465 | } |
| 466 | if (!ValidateDexFileHeader(raw_header, location)) { |
| 467 | return nullptr; |
| 468 | } |
| 469 | |
| 470 | return AsUnalignedDexFileHeader(raw_header); |
| 471 | } |
| 472 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 473 | bool OatWriter::AddDexFileSource(const char* filename, |
| 474 | const char* location, |
| 475 | CreateTypeLookupTable create_type_lookup_table) { |
| 476 | DCHECK(write_state_ == WriteState::kAddingDexFileSources); |
| 477 | uint32_t magic; |
| 478 | std::string error_msg; |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 479 | File fd = OpenAndReadMagic(filename, &magic, &error_msg); |
| 480 | if (fd.Fd() == -1) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 481 | PLOG(ERROR) << "Failed to read magic number from dex file: '" << filename << "'"; |
| 482 | return false; |
Mathieu Chartier | cf76bf8 | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 483 | } else if (DexFileLoader::IsMagicValid(magic)) { |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 484 | uint8_t raw_header[sizeof(DexFile::Header)]; |
| 485 | const UnalignedDexFileHeader* header = GetDexFileHeader(&fd, raw_header, location); |
| 486 | if (header == nullptr) { |
| 487 | return false; |
| 488 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 489 | // The file is open for reading, not writing, so it's OK to let the File destructor |
| 490 | // close it without checking for explicit Close(), so pass checkUsage = false. |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 491 | raw_dex_files_.emplace_back(new File(fd.Release(), location, /* checkUsage */ false)); |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 492 | oat_dex_files_.emplace_back(/* OatDexFile */ |
| 493 | location, |
| 494 | DexFileSource(raw_dex_files_.back().get()), |
| 495 | create_type_lookup_table, |
| 496 | header->checksum_, |
| 497 | header->file_size_); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 498 | } else if (IsZipMagic(magic)) { |
| 499 | if (!AddZippedDexFilesSource(std::move(fd), location, create_type_lookup_table)) { |
| 500 | return false; |
| 501 | } |
| 502 | } else { |
| 503 | LOG(ERROR) << "Expected valid zip or dex file: '" << filename << "'"; |
| 504 | return false; |
| 505 | } |
| 506 | return true; |
| 507 | } |
| 508 | |
| 509 | // Add dex file source(s) from a zip file specified by a file handle. |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 510 | bool OatWriter::AddZippedDexFilesSource(File&& zip_fd, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 511 | const char* location, |
| 512 | CreateTypeLookupTable create_type_lookup_table) { |
| 513 | DCHECK(write_state_ == WriteState::kAddingDexFileSources); |
| 514 | std::string error_msg; |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 515 | zip_archives_.emplace_back(ZipArchive::OpenFromFd(zip_fd.Release(), location, &error_msg)); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 516 | ZipArchive* zip_archive = zip_archives_.back().get(); |
| 517 | if (zip_archive == nullptr) { |
| 518 | LOG(ERROR) << "Failed to open zip from file descriptor for '" << location << "': " |
| 519 | << error_msg; |
| 520 | return false; |
| 521 | } |
| 522 | for (size_t i = 0; ; ++i) { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 523 | std::string entry_name = DexFileLoader::GetMultiDexClassesDexName(i); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 524 | std::unique_ptr<ZipEntry> entry(zip_archive->Find(entry_name.c_str(), &error_msg)); |
| 525 | if (entry == nullptr) { |
| 526 | break; |
| 527 | } |
| 528 | zipped_dex_files_.push_back(std::move(entry)); |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 529 | zipped_dex_file_locations_.push_back(DexFileLoader::GetMultiDexLocation(i, location)); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 530 | const char* full_location = zipped_dex_file_locations_.back().c_str(); |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 531 | // We override the checksum from header with the CRC from ZIP entry. |
| 532 | oat_dex_files_.emplace_back(/* OatDexFile */ |
| 533 | full_location, |
| 534 | DexFileSource(zipped_dex_files_.back().get()), |
| 535 | create_type_lookup_table, |
| 536 | zipped_dex_files_.back()->GetCrc32(), |
| 537 | zipped_dex_files_.back()->GetUncompressedLength()); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 538 | } |
| 539 | if (zipped_dex_file_locations_.empty()) { |
| 540 | LOG(ERROR) << "No dex files in zip file '" << location << "': " << error_msg; |
| 541 | return false; |
| 542 | } |
| 543 | return true; |
| 544 | } |
| 545 | |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 546 | // Add dex file source(s) from a vdex file specified by a file handle. |
| 547 | bool OatWriter::AddVdexDexFilesSource(const VdexFile& vdex_file, |
| 548 | const char* location, |
| 549 | CreateTypeLookupTable create_type_lookup_table) { |
| 550 | DCHECK(write_state_ == WriteState::kAddingDexFileSources); |
| 551 | const uint8_t* current_dex_data = nullptr; |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 552 | for (size_t i = 0; i < vdex_file.GetHeader().GetNumberOfDexFiles(); ++i) { |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 553 | current_dex_data = vdex_file.GetNextDexFileData(current_dex_data); |
| 554 | if (current_dex_data == nullptr) { |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 555 | LOG(ERROR) << "Unexpected number of dex files in vdex " << location; |
| 556 | return false; |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 557 | } |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 558 | |
Mathieu Chartier | cf76bf8 | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 559 | if (!DexFileLoader::IsMagicValid(current_dex_data)) { |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 560 | LOG(ERROR) << "Invalid magic in vdex file created from " << location; |
| 561 | return false; |
| 562 | } |
| 563 | // We used `zipped_dex_file_locations_` to keep the strings in memory. |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 564 | zipped_dex_file_locations_.push_back(DexFileLoader::GetMultiDexLocation(i, location)); |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 565 | const char* full_location = zipped_dex_file_locations_.back().c_str(); |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 566 | const UnalignedDexFileHeader* header = AsUnalignedDexFileHeader(current_dex_data); |
| 567 | oat_dex_files_.emplace_back(/* OatDexFile */ |
| 568 | full_location, |
| 569 | DexFileSource(current_dex_data), |
| 570 | create_type_lookup_table, |
| 571 | vdex_file.GetLocationChecksum(i), |
| 572 | header->file_size_); |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 573 | } |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 574 | |
| 575 | if (vdex_file.GetNextDexFileData(current_dex_data) != nullptr) { |
| 576 | LOG(ERROR) << "Unexpected number of dex files in vdex " << location; |
| 577 | return false; |
| 578 | } |
| 579 | |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 580 | if (oat_dex_files_.empty()) { |
| 581 | LOG(ERROR) << "No dex files in vdex file created from " << location; |
| 582 | return false; |
| 583 | } |
| 584 | return true; |
| 585 | } |
| 586 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 587 | // Add dex file source from raw memory. |
| 588 | bool OatWriter::AddRawDexFileSource(const ArrayRef<const uint8_t>& data, |
| 589 | const char* location, |
| 590 | uint32_t location_checksum, |
| 591 | CreateTypeLookupTable create_type_lookup_table) { |
| 592 | DCHECK(write_state_ == WriteState::kAddingDexFileSources); |
| 593 | if (data.size() < sizeof(DexFile::Header)) { |
| 594 | LOG(ERROR) << "Provided data is shorter than dex file header. size: " |
| 595 | << data.size() << " File: " << location; |
| 596 | return false; |
| 597 | } |
| 598 | if (!ValidateDexFileHeader(data.data(), location)) { |
| 599 | return false; |
| 600 | } |
| 601 | const UnalignedDexFileHeader* header = AsUnalignedDexFileHeader(data.data()); |
| 602 | if (data.size() < header->file_size_) { |
| 603 | LOG(ERROR) << "Truncated dex file data. Data size: " << data.size() |
| 604 | << " file size from header: " << header->file_size_ << " File: " << location; |
| 605 | return false; |
| 606 | } |
| 607 | |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 608 | oat_dex_files_.emplace_back(/* OatDexFile */ |
| 609 | location, |
| 610 | DexFileSource(data.data()), |
| 611 | create_type_lookup_table, |
| 612 | location_checksum, |
| 613 | header->file_size_); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 614 | return true; |
| 615 | } |
| 616 | |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 617 | dchecked_vector<std::string> OatWriter::GetSourceLocations() const { |
| 618 | dchecked_vector<std::string> locations; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 619 | locations.reserve(oat_dex_files_.size()); |
| 620 | for (const OatDexFile& oat_dex_file : oat_dex_files_) { |
| 621 | locations.push_back(oat_dex_file.GetLocation()); |
| 622 | } |
| 623 | return locations; |
| 624 | } |
| 625 | |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 626 | bool OatWriter::MayHaveCompiledMethods() const { |
| 627 | return CompilerFilter::IsAnyCompilationEnabled( |
| 628 | GetCompilerDriver()->GetCompilerOptions().GetCompilerFilter()); |
| 629 | } |
| 630 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 631 | bool OatWriter::WriteAndOpenDexFiles( |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 632 | File* vdex_file, |
| 633 | OutputStream* oat_rodata, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 634 | InstructionSet instruction_set, |
| 635 | const InstructionSetFeatures* instruction_set_features, |
| 636 | SafeMap<std::string, std::string>* key_value_store, |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 637 | bool verify, |
Nicolas Geoffray | 81f57d1 | 2016-12-20 13:17:09 +0000 | [diff] [blame] | 638 | bool update_input_vdex, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 639 | /*out*/ std::unique_ptr<MemMap>* opened_dex_files_map, |
| 640 | /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files) { |
| 641 | CHECK(write_state_ == WriteState::kAddingDexFileSources); |
| 642 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 643 | // Record the ELF rodata section offset, i.e. the beginning of the OAT data. |
| 644 | if (!RecordOatDataOffset(oat_rodata)) { |
| 645 | return false; |
| 646 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 647 | |
| 648 | std::unique_ptr<MemMap> dex_files_map; |
| 649 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 650 | |
| 651 | // Initialize VDEX and OAT headers. |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 652 | |
| 653 | // Reserve space for Vdex header and checksums. |
| 654 | vdex_size_ = sizeof(VdexFile::Header) + oat_dex_files_.size() * sizeof(VdexFile::VdexChecksum); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 655 | oat_size_ = InitOatHeader(instruction_set, |
| 656 | instruction_set_features, |
| 657 | dchecked_integral_cast<uint32_t>(oat_dex_files_.size()), |
| 658 | key_value_store); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 659 | |
| 660 | ChecksumUpdatingOutputStream checksum_updating_rodata(oat_rodata, oat_header_.get()); |
| 661 | |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 662 | std::unique_ptr<BufferedOutputStream> vdex_out = |
| 663 | std::make_unique<BufferedOutputStream>(std::make_unique<FileOutputStream>(vdex_file)); |
| 664 | // Write DEX files into VDEX, mmap and open them. |
| 665 | if (!WriteDexFiles(vdex_out.get(), vdex_file, update_input_vdex) || |
| 666 | !OpenDexFiles(vdex_file, verify, &dex_files_map, &dex_files)) { |
| 667 | return false; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 668 | } |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 669 | |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 670 | // Write type lookup tables into the oat file. |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 671 | if (!WriteTypeLookupTables(&checksum_updating_rodata, dex_files)) { |
| 672 | return false; |
| 673 | } |
| 674 | |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 675 | // Write dex layout sections into the oat file. |
| 676 | if (!WriteDexLayoutSections(&checksum_updating_rodata, dex_files)) { |
| 677 | return false; |
| 678 | } |
| 679 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 680 | *opened_dex_files_map = std::move(dex_files_map); |
| 681 | *opened_dex_files = std::move(dex_files); |
| 682 | write_state_ = WriteState::kPrepareLayout; |
| 683 | return true; |
| 684 | } |
| 685 | |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 686 | void OatWriter::PrepareLayout(MultiOatRelativePatcher* relative_patcher) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 687 | CHECK(write_state_ == WriteState::kPrepareLayout); |
| 688 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 689 | relative_patcher_ = relative_patcher; |
| 690 | SetMultiOatRelativePatcherAdjustment(); |
| 691 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 692 | if (compiling_boot_image_) { |
| 693 | CHECK(image_writer_ != nullptr); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 694 | } |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 695 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 696 | CHECK_EQ(instruction_set, oat_header_->GetInstructionSet()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 697 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 698 | { |
| 699 | TimingLogger::ScopedTiming split("InitBssLayout", timings_); |
| 700 | InitBssLayout(instruction_set); |
| 701 | } |
| 702 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 703 | uint32_t offset = oat_size_; |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 704 | { |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 705 | TimingLogger::ScopedTiming split("InitClassOffsets", timings_); |
| 706 | offset = InitClassOffsets(offset); |
| 707 | } |
| 708 | { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 709 | TimingLogger::ScopedTiming split("InitOatClasses", timings_); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 710 | offset = InitOatClasses(offset); |
| 711 | } |
| 712 | { |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 713 | TimingLogger::ScopedTiming split("InitIndexBssMappings", timings_); |
| 714 | offset = InitIndexBssMappings(offset); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 715 | } |
| 716 | { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 717 | TimingLogger::ScopedTiming split("InitOatMaps", timings_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 718 | offset = InitOatMaps(offset); |
| 719 | } |
| 720 | { |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 721 | TimingLogger::ScopedTiming split("InitOatDexFiles", timings_); |
| 722 | oat_header_->SetOatDexFilesOffset(offset); |
| 723 | offset = InitOatDexFiles(offset); |
| 724 | } |
| 725 | { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 726 | TimingLogger::ScopedTiming split("InitOatCode", timings_); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 727 | offset = InitOatCode(offset); |
| 728 | } |
| 729 | { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 730 | TimingLogger::ScopedTiming split("InitOatCodeDexFiles", timings_); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 731 | offset = InitOatCodeDexFiles(offset); |
| 732 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 733 | oat_size_ = offset; |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 734 | bss_start_ = (bss_size_ != 0u) ? RoundUp(oat_size_, kPageSize) : 0u; |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 735 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 736 | CHECK_EQ(dex_files_->size(), oat_dex_files_.size()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 737 | if (compiling_boot_image_) { |
| 738 | CHECK_EQ(image_writer_ != nullptr, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 739 | oat_header_->GetStoreValueByKey(OatHeader::kImageLocationKey) == nullptr); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 740 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 741 | |
| 742 | write_state_ = WriteState::kWriteRoData; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 745 | OatWriter::~OatWriter() { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 748 | class OatWriter::DexMethodVisitor { |
| 749 | public: |
| 750 | DexMethodVisitor(OatWriter* writer, size_t offset) |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 751 | : writer_(writer), |
| 752 | offset_(offset), |
| 753 | dex_file_(nullptr), |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 754 | class_def_index_(dex::kDexNoIndex) {} |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 755 | |
| 756 | virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) { |
| 757 | DCHECK(dex_file_ == nullptr); |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 758 | DCHECK_EQ(class_def_index_, dex::kDexNoIndex); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 759 | dex_file_ = dex_file; |
| 760 | class_def_index_ = class_def_index; |
| 761 | return true; |
| 762 | } |
| 763 | |
| 764 | virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0; |
| 765 | |
| 766 | virtual bool EndClass() { |
| 767 | if (kIsDebugBuild) { |
| 768 | dex_file_ = nullptr; |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 769 | class_def_index_ = dex::kDexNoIndex; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 770 | } |
| 771 | return true; |
| 772 | } |
| 773 | |
| 774 | size_t GetOffset() const { |
| 775 | return offset_; |
| 776 | } |
| 777 | |
| 778 | protected: |
| 779 | virtual ~DexMethodVisitor() { } |
| 780 | |
| 781 | OatWriter* const writer_; |
| 782 | |
| 783 | // The offset is usually advanced for each visited method by the derived class. |
| 784 | size_t offset_; |
| 785 | |
| 786 | // The dex file and class def index are set in StartClass(). |
| 787 | const DexFile* dex_file_; |
| 788 | size_t class_def_index_; |
| 789 | }; |
| 790 | |
| 791 | class OatWriter::OatDexMethodVisitor : public DexMethodVisitor { |
| 792 | public: |
| 793 | OatDexMethodVisitor(OatWriter* writer, size_t offset) |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 794 | : DexMethodVisitor(writer, offset), |
| 795 | oat_class_index_(0u), |
| 796 | method_offsets_index_(0u) {} |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 797 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 798 | bool StartClass(const DexFile* dex_file, size_t class_def_index) OVERRIDE { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 799 | DexMethodVisitor::StartClass(dex_file, class_def_index); |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 800 | if (kIsDebugBuild && writer_->MayHaveCompiledMethods()) { |
| 801 | // There are no oat classes if there aren't any compiled methods. |
| 802 | CHECK_LT(oat_class_index_, writer_->oat_classes_.size()); |
| 803 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 804 | method_offsets_index_ = 0u; |
| 805 | return true; |
| 806 | } |
| 807 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 808 | bool EndClass() OVERRIDE { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 809 | ++oat_class_index_; |
| 810 | return DexMethodVisitor::EndClass(); |
| 811 | } |
| 812 | |
| 813 | protected: |
| 814 | size_t oat_class_index_; |
| 815 | size_t method_offsets_index_; |
| 816 | }; |
| 817 | |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 818 | static bool HasCompiledCode(const CompiledMethod* method) { |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 819 | return method != nullptr && !method->GetQuickCode().empty(); |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | static bool HasQuickeningInfo(const CompiledMethod* method) { |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 823 | // The dextodexcompiler puts the quickening info table into the CompiledMethod |
| 824 | // for simplicity. |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 825 | return method != nullptr && method->GetQuickCode().empty() && !method->GetVmapTable().empty(); |
| 826 | } |
| 827 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 828 | class OatWriter::InitBssLayoutMethodVisitor : public DexMethodVisitor { |
| 829 | public: |
| 830 | explicit InitBssLayoutMethodVisitor(OatWriter* writer) |
| 831 | : DexMethodVisitor(writer, /* offset */ 0u) {} |
| 832 | |
| 833 | bool VisitMethod(size_t class_def_method_index ATTRIBUTE_UNUSED, |
| 834 | const ClassDataItemIterator& it) OVERRIDE { |
| 835 | // Look for patches with .bss references and prepare maps with placeholders for their offsets. |
| 836 | CompiledMethod* compiled_method = writer_->compiler_driver_->GetCompiledMethod( |
| 837 | MethodReference(dex_file_, it.GetMemberIndex())); |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 838 | if (HasCompiledCode(compiled_method)) { |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 839 | for (const LinkerPatch& patch : compiled_method->GetPatches()) { |
| 840 | if (patch.GetType() == LinkerPatch::Type::kMethodBssEntry) { |
| 841 | MethodReference target_method = patch.TargetMethod(); |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 842 | AddBssReference(target_method, |
| 843 | target_method.dex_file->NumMethodIds(), |
| 844 | &writer_->bss_method_entry_references_); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 845 | writer_->bss_method_entries_.Overwrite(target_method, /* placeholder */ 0u); |
| 846 | } else if (patch.GetType() == LinkerPatch::Type::kTypeBssEntry) { |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 847 | TypeReference target_type(patch.TargetTypeDexFile(), patch.TargetTypeIndex()); |
| 848 | AddBssReference(target_type, |
| 849 | target_type.dex_file->NumTypeIds(), |
| 850 | &writer_->bss_type_entry_references_); |
| 851 | writer_->bss_type_entries_.Overwrite(target_type, /* placeholder */ 0u); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 852 | } else if (patch.GetType() == LinkerPatch::Type::kStringBssEntry) { |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 853 | StringReference target_string(patch.TargetStringDexFile(), patch.TargetStringIndex()); |
| 854 | AddBssReference(target_string, |
| 855 | target_string.dex_file->NumStringIds(), |
| 856 | &writer_->bss_string_entry_references_); |
| 857 | writer_->bss_string_entries_.Overwrite(target_string, /* placeholder */ 0u); |
Vladimir Marko | 94ec2db | 2017-09-06 17:21:03 +0100 | [diff] [blame] | 858 | } else if (patch.GetType() == LinkerPatch::Type::kStringInternTable || |
| 859 | patch.GetType() == LinkerPatch::Type::kTypeClassTable) { |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 860 | writer_->map_boot_image_tables_to_bss_ = true; |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 861 | } |
| 862 | } |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 863 | } else { |
| 864 | DCHECK(compiled_method == nullptr || compiled_method->GetPatches().empty()); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 865 | } |
| 866 | return true; |
| 867 | } |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 868 | |
| 869 | private: |
| 870 | void AddBssReference(const DexFileReference& ref, |
| 871 | size_t number_of_indexes, |
| 872 | /*inout*/ SafeMap<const DexFile*, BitVector>* references) { |
| 873 | // We currently support inlining of throwing instructions only when they originate in the |
| 874 | // same dex file as the outer method. All .bss references are used by throwing instructions. |
| 875 | DCHECK_EQ(dex_file_, ref.dex_file); |
| 876 | |
| 877 | auto refs_it = references->find(ref.dex_file); |
| 878 | if (refs_it == references->end()) { |
| 879 | refs_it = references->Put( |
| 880 | ref.dex_file, |
| 881 | BitVector(number_of_indexes, /* expandable */ false, Allocator::GetMallocAllocator())); |
| 882 | refs_it->second.ClearAllBits(); |
| 883 | } |
| 884 | refs_it->second.SetBit(ref.index); |
| 885 | } |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 886 | }; |
| 887 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 888 | class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor { |
| 889 | public: |
| 890 | InitOatClassesMethodVisitor(OatWriter* writer, size_t offset) |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 891 | : DexMethodVisitor(writer, offset), |
| 892 | compiled_methods_(), |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 893 | compiled_methods_with_code_(0u) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 894 | size_t num_classes = 0u; |
| 895 | for (const OatDexFile& oat_dex_file : writer_->oat_dex_files_) { |
| 896 | num_classes += oat_dex_file.class_offsets_.size(); |
| 897 | } |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 898 | // If we aren't compiling only reserve headers. |
| 899 | writer_->oat_class_headers_.reserve(num_classes); |
| 900 | if (writer->MayHaveCompiledMethods()) { |
| 901 | writer->oat_classes_.reserve(num_classes); |
| 902 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 903 | compiled_methods_.reserve(256u); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 904 | // If there are any classes, the class offsets allocation aligns the offset. |
| 905 | DCHECK(num_classes == 0u || IsAligned<4u>(offset)); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 906 | } |
| 907 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 908 | bool StartClass(const DexFile* dex_file, size_t class_def_index) OVERRIDE { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 909 | DexMethodVisitor::StartClass(dex_file, class_def_index); |
| 910 | compiled_methods_.clear(); |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 911 | compiled_methods_with_code_ = 0u; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 912 | return true; |
| 913 | } |
| 914 | |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 915 | bool VisitMethod(size_t class_def_method_index ATTRIBUTE_UNUSED, |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 916 | const ClassDataItemIterator& it) OVERRIDE { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 917 | // Fill in the compiled_methods_ array for methods that have a |
| 918 | // CompiledMethod. We track the number of non-null entries in |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 919 | // compiled_methods_with_code_ since we only want to allocate |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 920 | // OatMethodOffsets for the compiled methods. |
| 921 | uint32_t method_idx = it.GetMemberIndex(); |
| 922 | CompiledMethod* compiled_method = |
| 923 | writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx)); |
| 924 | compiled_methods_.push_back(compiled_method); |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 925 | if (HasCompiledCode(compiled_method)) { |
| 926 | ++compiled_methods_with_code_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 927 | } |
| 928 | return true; |
| 929 | } |
| 930 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 931 | bool EndClass() OVERRIDE { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 932 | ClassReference class_ref(dex_file_, class_def_index_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 933 | mirror::Class::Status status; |
Andreas Gampe | bb84610 | 2017-05-11 21:03:35 -0700 | [diff] [blame] | 934 | bool found = writer_->compiler_driver_->GetCompiledClass(class_ref, &status); |
| 935 | if (!found) { |
Mathieu Chartier | 0733dc8 | 2017-07-17 14:05:28 -0700 | [diff] [blame] | 936 | VerificationResults* results = writer_->compiler_driver_->GetVerificationResults(); |
| 937 | if (results != nullptr && results->IsClassRejected(class_ref)) { |
Andreas Gampe | bb84610 | 2017-05-11 21:03:35 -0700 | [diff] [blame] | 938 | // The oat class status is used only for verification of resolved classes, |
| 939 | // so use kStatusErrorResolved whether the class was resolved or unresolved |
| 940 | // during compile-time verification. |
| 941 | status = mirror::Class::kStatusErrorResolved; |
| 942 | } else { |
| 943 | status = mirror::Class::kStatusNotReady; |
| 944 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 945 | } |
| 946 | |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 947 | writer_->oat_class_headers_.emplace_back(offset_, |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 948 | compiled_methods_with_code_, |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 949 | compiled_methods_.size(), |
| 950 | status); |
| 951 | OatClassHeader& header = writer_->oat_class_headers_.back(); |
| 952 | offset_ += header.SizeOf(); |
| 953 | if (writer_->MayHaveCompiledMethods()) { |
| 954 | writer_->oat_classes_.emplace_back(compiled_methods_, |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 955 | compiled_methods_with_code_, |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 956 | header.type_); |
| 957 | offset_ += writer_->oat_classes_.back().SizeOf(); |
| 958 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 959 | return DexMethodVisitor::EndClass(); |
| 960 | } |
| 961 | |
| 962 | private: |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 963 | dchecked_vector<CompiledMethod*> compiled_methods_; |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 964 | size_t compiled_methods_with_code_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 965 | }; |
| 966 | |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 967 | // CompiledMethod + metadata required to do ordered method layout. |
| 968 | // |
| 969 | // See also OrderedMethodVisitor. |
| 970 | struct OatWriter::OrderedMethodData { |
| 971 | ProfileCompilationInfo::MethodHotness method_hotness; |
| 972 | OatClass* oat_class; |
| 973 | CompiledMethod* compiled_method; |
| 974 | MethodReference method_reference; |
| 975 | size_t method_offsets_index; |
| 976 | |
| 977 | size_t class_def_index; |
| 978 | uint32_t access_flags; |
| 979 | const DexFile::CodeItem* code_item; |
| 980 | |
| 981 | // A value of -1 denotes missing debug info |
| 982 | static constexpr size_t kDebugInfoIdxInvalid = static_cast<size_t>(-1); |
| 983 | // Index into writer_->method_info_ |
| 984 | size_t debug_info_idx; |
| 985 | |
| 986 | bool HasDebugInfo() const { |
| 987 | return debug_info_idx != kDebugInfoIdxInvalid; |
| 988 | } |
| 989 | |
| 990 | // Bin each method according to the profile flags. |
| 991 | // |
| 992 | // Groups by e.g. |
| 993 | // -- not hot at all |
| 994 | // -- hot |
| 995 | // -- hot and startup |
| 996 | // -- hot and post-startup |
| 997 | // -- hot and startup and poststartup |
| 998 | // -- startup |
| 999 | // -- startup and post-startup |
| 1000 | // -- post-startup |
| 1001 | // |
| 1002 | // (See MethodHotness enum definition for up-to-date binning order.) |
| 1003 | bool operator<(const OrderedMethodData& other) const { |
| 1004 | if (kOatWriterForceOatCodeLayout) { |
| 1005 | // Development flag: Override default behavior by sorting by name. |
| 1006 | |
| 1007 | std::string name = method_reference.PrettyMethod(); |
| 1008 | std::string other_name = other.method_reference.PrettyMethod(); |
| 1009 | return name < other_name; |
| 1010 | } |
| 1011 | |
| 1012 | // Use the profile's method hotness to determine sort order. |
| 1013 | if (GetMethodHotnessOrder() < other.GetMethodHotnessOrder()) { |
| 1014 | return true; |
| 1015 | } |
| 1016 | |
| 1017 | // Default: retain the original order. |
| 1018 | return false; |
| 1019 | } |
| 1020 | |
| 1021 | private: |
| 1022 | // Used to determine relative order for OAT code layout when determining |
| 1023 | // binning. |
| 1024 | size_t GetMethodHotnessOrder() const { |
| 1025 | bool hotness[] = { |
| 1026 | method_hotness.IsHot(), |
| 1027 | method_hotness.IsStartup(), |
| 1028 | method_hotness.IsPostStartup() |
| 1029 | }; |
| 1030 | |
| 1031 | |
| 1032 | // Note: Bin-to-bin order does not matter. If the kernel does or does not read-ahead |
| 1033 | // any memory, it only goes into the buffer cache and does not grow the PSS until the first |
| 1034 | // time that memory is referenced in the process. |
| 1035 | |
| 1036 | size_t hotness_bits = 0; |
| 1037 | for (size_t i = 0; i < arraysize(hotness); ++i) { |
| 1038 | if (hotness[i]) { |
| 1039 | hotness_bits |= (1 << i); |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | if (kIsDebugBuild) { |
| 1044 | // Check for bins that are always-empty given a real profile. |
| 1045 | if (method_hotness.IsHot() && |
| 1046 | !method_hotness.IsStartup() && !method_hotness.IsPostStartup()) { |
| 1047 | std::string name = method_reference.PrettyMethod(); |
Mathieu Chartier | c46cf80 | 2017-09-28 11:52:19 -0700 | [diff] [blame] | 1048 | LOG(FATAL) << "Method " << name << " had a Hot method that wasn't marked " |
| 1049 | << "either start-up or post-startup. Possible corrupted profile?"; |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1050 | // This is not fatal, so only warn. |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | return hotness_bits; |
| 1055 | } |
| 1056 | }; |
| 1057 | |
| 1058 | // Given a queue of CompiledMethod in some total order, |
| 1059 | // visit each one in that order. |
| 1060 | class OatWriter::OrderedMethodVisitor { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1061 | public: |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1062 | explicit OrderedMethodVisitor(OrderedMethodList ordered_methods) |
| 1063 | : ordered_methods_(std::move(ordered_methods)) { |
| 1064 | } |
| 1065 | |
| 1066 | virtual ~OrderedMethodVisitor() {} |
| 1067 | |
| 1068 | // Invoke VisitMethod in the order of `ordered_methods`, then invoke VisitComplete. |
| 1069 | bool Visit() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1070 | if (!VisitStart()) { |
| 1071 | return false; |
| 1072 | } |
| 1073 | |
| 1074 | for (const OrderedMethodData& method_data : ordered_methods_) { |
| 1075 | if (!VisitMethod(method_data)) { |
| 1076 | return false; |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | return VisitComplete(); |
| 1081 | } |
| 1082 | |
| 1083 | // Invoked once at the beginning, prior to visiting anything else. |
| 1084 | // |
| 1085 | // Return false to abort further visiting. |
| 1086 | virtual bool VisitStart() { return true; } |
| 1087 | |
| 1088 | // Invoked repeatedly in the order specified by `ordered_methods`. |
| 1089 | // |
| 1090 | // Return false to short-circuit and to stop visiting further methods. |
| 1091 | virtual bool VisitMethod(const OrderedMethodData& method_data) |
| 1092 | REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
| 1093 | |
| 1094 | // Invoked once at the end, after every other method has been successfully visited. |
| 1095 | // |
| 1096 | // Return false to indicate the overall `Visit` has failed. |
| 1097 | virtual bool VisitComplete() = 0; |
| 1098 | |
| 1099 | OrderedMethodList ReleaseOrderedMethods() { |
| 1100 | return std::move(ordered_methods_); |
| 1101 | } |
| 1102 | |
| 1103 | private: |
| 1104 | // List of compiled methods, sorted by the order defined in OrderedMethodData. |
| 1105 | // Methods can be inserted more than once in case of duplicated methods. |
| 1106 | OrderedMethodList ordered_methods_; |
| 1107 | }; |
| 1108 | |
| 1109 | // Visit every compiled method in order to determine its order within the OAT file. |
| 1110 | // Methods from the same class do not need to be adjacent in the OAT code. |
| 1111 | class OatWriter::LayoutCodeMethodVisitor : public OatDexMethodVisitor { |
| 1112 | public: |
| 1113 | LayoutCodeMethodVisitor(OatWriter* writer, size_t offset) |
| 1114 | : OatDexMethodVisitor(writer, offset) { |
| 1115 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1116 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 1117 | bool EndClass() OVERRIDE { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1118 | OatDexMethodVisitor::EndClass(); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1119 | return true; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1120 | } |
| 1121 | |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1122 | bool VisitMethod(size_t class_def_method_index, |
| 1123 | const ClassDataItemIterator& it) |
| 1124 | OVERRIDE |
| 1125 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1126 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 1127 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1128 | OatClass* oat_class = &writer_->oat_classes_[oat_class_index_]; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1129 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 1130 | |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 1131 | if (HasCompiledCode(compiled_method)) { |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1132 | size_t debug_info_idx = OrderedMethodData::kDebugInfoIdxInvalid; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1133 | |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1134 | { |
| 1135 | const CompilerOptions& compiler_options = writer_->compiler_driver_->GetCompilerOptions(); |
| 1136 | ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode(); |
| 1137 | uint32_t code_size = quick_code.size() * sizeof(uint8_t); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 1138 | |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1139 | // Debug method info must be pushed in the original order |
| 1140 | // (i.e. all methods from the same class must be adjacent in the debug info sections) |
| 1141 | // ElfCompilationUnitWriter::Write requires this. |
| 1142 | if (compiler_options.GenerateAnyDebugInfo() && code_size != 0) { |
| 1143 | debug::MethodDebugInfo info = debug::MethodDebugInfo(); |
| 1144 | writer_->method_info_.push_back(info); |
| 1145 | |
| 1146 | // The debug info is filled in LayoutReserveOffsetCodeMethodVisitor |
| 1147 | // once we know the offsets. |
| 1148 | // |
| 1149 | // Store the index into writer_->method_info_ since future push-backs |
| 1150 | // could reallocate and change the underlying data address. |
| 1151 | debug_info_idx = writer_->method_info_.size() - 1; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1152 | } |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | MethodReference method_ref(dex_file_, it.GetMemberIndex()); |
| 1156 | |
| 1157 | // Lookup method hotness from profile, if available. |
| 1158 | // Otherwise assume a default of none-hotness. |
| 1159 | ProfileCompilationInfo::MethodHotness method_hotness = |
| 1160 | writer_->profile_compilation_info_ != nullptr |
| 1161 | ? writer_->profile_compilation_info_->GetMethodHotness(method_ref) |
| 1162 | : ProfileCompilationInfo::MethodHotness(); |
| 1163 | |
| 1164 | // Handle duplicate methods by pushing them repeatedly. |
| 1165 | OrderedMethodData method_data = { |
| 1166 | method_hotness, |
| 1167 | oat_class, |
| 1168 | compiled_method, |
| 1169 | method_ref, |
| 1170 | method_offsets_index_, |
| 1171 | class_def_index_, |
| 1172 | it.GetMethodAccessFlags(), |
| 1173 | it.GetMethodCodeItem(), |
| 1174 | debug_info_idx |
| 1175 | }; |
| 1176 | ordered_methods_.push_back(method_data); |
| 1177 | |
| 1178 | method_offsets_index_++; |
| 1179 | } |
| 1180 | |
| 1181 | return true; |
| 1182 | } |
| 1183 | |
| 1184 | OrderedMethodList ReleaseOrderedMethods() { |
| 1185 | if (kOatWriterForceOatCodeLayout || writer_->profile_compilation_info_ != nullptr) { |
| 1186 | // Sort by the method ordering criteria (in OrderedMethodData). |
| 1187 | // Since most methods will have the same ordering criteria, |
| 1188 | // we preserve the original insertion order within the same sort order. |
| 1189 | std::stable_sort(ordered_methods_.begin(), ordered_methods_.end()); |
| 1190 | } else { |
| 1191 | // The profile-less behavior is as if every method had 0 hotness |
| 1192 | // associated with it. |
| 1193 | // |
| 1194 | // Since sorting all methods with hotness=0 should give back the same |
| 1195 | // order as before, don't do anything. |
| 1196 | DCHECK(std::is_sorted(ordered_methods_.begin(), ordered_methods_.end())); |
| 1197 | } |
| 1198 | |
| 1199 | return std::move(ordered_methods_); |
| 1200 | } |
| 1201 | |
| 1202 | private: |
| 1203 | // List of compiled methods, later to be sorted by order defined in OrderedMethodData. |
| 1204 | // Methods can be inserted more than once in case of duplicated methods. |
| 1205 | OrderedMethodList ordered_methods_; |
| 1206 | }; |
| 1207 | |
| 1208 | // Given a method order, reserve the offsets for each CompiledMethod in the OAT file. |
| 1209 | class OatWriter::LayoutReserveOffsetCodeMethodVisitor : public OrderedMethodVisitor { |
| 1210 | public: |
| 1211 | LayoutReserveOffsetCodeMethodVisitor(OatWriter* writer, |
| 1212 | size_t offset, |
| 1213 | OrderedMethodList ordered_methods) |
| 1214 | : LayoutReserveOffsetCodeMethodVisitor(writer, |
| 1215 | offset, |
| 1216 | writer->GetCompilerDriver()->GetCompilerOptions(), |
| 1217 | std::move(ordered_methods)) { |
| 1218 | } |
| 1219 | |
| 1220 | virtual bool VisitComplete() OVERRIDE { |
| 1221 | offset_ = writer_->relative_patcher_->ReserveSpaceEnd(offset_); |
| 1222 | if (generate_debug_info_) { |
| 1223 | std::vector<debug::MethodDebugInfo> thunk_infos = |
| 1224 | relative_patcher_->GenerateThunkDebugInfo(executable_offset_); |
| 1225 | writer_->method_info_.insert(writer_->method_info_.end(), |
| 1226 | std::make_move_iterator(thunk_infos.begin()), |
| 1227 | std::make_move_iterator(thunk_infos.end())); |
| 1228 | } |
| 1229 | return true; |
| 1230 | } |
| 1231 | |
| 1232 | virtual bool VisitMethod(const OrderedMethodData& method_data) |
| 1233 | OVERRIDE |
| 1234 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1235 | OatClass* oat_class = method_data.oat_class; |
| 1236 | CompiledMethod* compiled_method = method_data.compiled_method; |
| 1237 | const MethodReference& method_ref = method_data.method_reference; |
| 1238 | uint16_t method_offsets_index_ = method_data.method_offsets_index; |
| 1239 | size_t class_def_index = method_data.class_def_index; |
| 1240 | uint32_t access_flags = method_data.access_flags; |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1241 | bool has_debug_info = method_data.HasDebugInfo(); |
| 1242 | size_t debug_info_idx = method_data.debug_info_idx; |
| 1243 | |
| 1244 | DCHECK(HasCompiledCode(compiled_method)) << method_ref.PrettyMethod(); |
| 1245 | |
| 1246 | // Derived from CompiledMethod. |
| 1247 | uint32_t quick_code_offset = 0; |
| 1248 | |
| 1249 | ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode(); |
| 1250 | uint32_t code_size = quick_code.size() * sizeof(uint8_t); |
| 1251 | uint32_t thumb_offset = compiled_method->CodeDelta(); |
| 1252 | |
| 1253 | // Deduplicate code arrays if we are not producing debuggable code. |
| 1254 | bool deduped = true; |
| 1255 | if (debuggable_) { |
| 1256 | quick_code_offset = relative_patcher_->GetOffset(method_ref); |
| 1257 | if (quick_code_offset != 0u) { |
| 1258 | // Duplicate methods, we want the same code for both of them so that the oat writer puts |
| 1259 | // the same code in both ArtMethods so that we do not get different oat code at runtime. |
Nicolas Geoffray | ed6195a | 2015-07-13 17:02:30 +0000 | [diff] [blame] | 1260 | } else { |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1261 | quick_code_offset = NewQuickCodeOffset(compiled_method, method_ref, thumb_offset); |
| 1262 | deduped = false; |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 1263 | } |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1264 | } else { |
| 1265 | quick_code_offset = dedupe_map_.GetOrCreate( |
| 1266 | compiled_method, |
| 1267 | [this, &deduped, compiled_method, &method_ref, thumb_offset]() { |
| 1268 | deduped = false; |
| 1269 | return NewQuickCodeOffset(compiled_method, method_ref, thumb_offset); |
| 1270 | }); |
| 1271 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1272 | |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1273 | if (code_size != 0) { |
| 1274 | if (relative_patcher_->GetOffset(method_ref) != 0u) { |
| 1275 | // TODO: Should this be a hard failure? |
| 1276 | LOG(WARNING) << "Multiple definitions of " |
| 1277 | << method_ref.dex_file->PrettyMethod(method_ref.index) |
| 1278 | << " offsets " << relative_patcher_->GetOffset(method_ref) |
| 1279 | << " " << quick_code_offset; |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 1280 | } else { |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1281 | relative_patcher_->SetOffset(method_ref, quick_code_offset); |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | // Update quick method header. |
| 1286 | DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size()); |
| 1287 | OatQuickMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_]; |
| 1288 | uint32_t vmap_table_offset = method_header->GetVmapTableOffset(); |
| 1289 | uint32_t method_info_offset = method_header->GetMethodInfoOffset(); |
| 1290 | // The code offset was 0 when the mapping/vmap table offset was set, so it's set |
| 1291 | // to 0-offset and we need to adjust it by code_offset. |
| 1292 | uint32_t code_offset = quick_code_offset - thumb_offset; |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 1293 | CHECK(!compiled_method->GetQuickCode().empty()); |
| 1294 | // If the code is compiled, we write the offset of the stack map relative |
| 1295 | // to the code. |
| 1296 | if (vmap_table_offset != 0u) { |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1297 | vmap_table_offset += code_offset; |
| 1298 | DCHECK_LT(vmap_table_offset, code_offset); |
| 1299 | } |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 1300 | if (method_info_offset != 0u) { |
| 1301 | method_info_offset += code_offset; |
| 1302 | DCHECK_LT(method_info_offset, code_offset); |
| 1303 | } |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1304 | uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes(); |
| 1305 | uint32_t core_spill_mask = compiled_method->GetCoreSpillMask(); |
| 1306 | uint32_t fp_spill_mask = compiled_method->GetFpSpillMask(); |
| 1307 | *method_header = OatQuickMethodHeader(vmap_table_offset, |
| 1308 | method_info_offset, |
| 1309 | frame_size_in_bytes, |
| 1310 | core_spill_mask, |
| 1311 | fp_spill_mask, |
| 1312 | code_size); |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 1313 | |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1314 | if (!deduped) { |
| 1315 | // Update offsets. (Checksum is updated when writing.) |
| 1316 | offset_ += sizeof(*method_header); // Method header is prepended before code. |
| 1317 | offset_ += code_size; |
| 1318 | // Record absolute patch locations. |
| 1319 | if (!compiled_method->GetPatches().empty()) { |
| 1320 | uintptr_t base_loc = offset_ - code_size - writer_->oat_header_->GetExecutableOffset(); |
| 1321 | for (const LinkerPatch& patch : compiled_method->GetPatches()) { |
| 1322 | if (!patch.IsPcRelative()) { |
| 1323 | writer_->absolute_patch_locations_.push_back(base_loc + patch.LiteralOffset()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1324 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1325 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 1326 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1327 | } |
| 1328 | |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1329 | // Exclude quickened dex methods (code_size == 0) since they have no native code. |
| 1330 | if (generate_debug_info_ && code_size != 0) { |
| 1331 | DCHECK(has_debug_info); |
| 1332 | |
| 1333 | bool has_code_info = method_header->IsOptimized(); |
| 1334 | // Record debug information for this function if we are doing that. |
| 1335 | debug::MethodDebugInfo& info = writer_->method_info_[debug_info_idx]; |
| 1336 | DCHECK(info.trampoline_name.empty()); |
| 1337 | info.dex_file = method_ref.dex_file; |
| 1338 | info.class_def_index = class_def_index; |
| 1339 | info.dex_method_index = method_ref.index; |
| 1340 | info.access_flags = access_flags; |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 1341 | // For intrinsics emitted by codegen, the code has no relation to the original code item. |
| 1342 | info.code_item = compiled_method->IsIntrinsic() ? nullptr : method_data.code_item; |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1343 | info.isa = compiled_method->GetInstructionSet(); |
| 1344 | info.deduped = deduped; |
| 1345 | info.is_native_debuggable = native_debuggable_; |
| 1346 | info.is_optimized = method_header->IsOptimized(); |
| 1347 | info.is_code_address_text_relative = true; |
| 1348 | info.code_address = code_offset - executable_offset_; |
| 1349 | info.code_size = code_size; |
| 1350 | info.frame_size_in_bytes = compiled_method->GetFrameSizeInBytes(); |
| 1351 | info.code_info = has_code_info ? compiled_method->GetVmapTable().data() : nullptr; |
| 1352 | info.cfi = compiled_method->GetCFIInfo(); |
| 1353 | } else { |
| 1354 | DCHECK(!has_debug_info); |
| 1355 | } |
| 1356 | |
| 1357 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 1358 | OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_]; |
| 1359 | offsets->code_offset_ = quick_code_offset; |
| 1360 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1361 | return true; |
| 1362 | } |
| 1363 | |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1364 | size_t GetOffset() const { |
| 1365 | return offset_; |
| 1366 | } |
| 1367 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1368 | private: |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1369 | LayoutReserveOffsetCodeMethodVisitor(OatWriter* writer, |
| 1370 | size_t offset, |
| 1371 | const CompilerOptions& compiler_options, |
| 1372 | OrderedMethodList ordered_methods) |
| 1373 | : OrderedMethodVisitor(std::move(ordered_methods)), |
| 1374 | writer_(writer), |
| 1375 | offset_(offset), |
Vladimir Marko | 1b404a8 | 2017-09-01 13:35:26 +0100 | [diff] [blame] | 1376 | relative_patcher_(writer->relative_patcher_), |
| 1377 | executable_offset_(writer->oat_header_->GetExecutableOffset()), |
| 1378 | debuggable_(compiler_options.GetDebuggable()), |
| 1379 | native_debuggable_(compiler_options.GetNativeDebuggable()), |
| 1380 | generate_debug_info_(compiler_options.GenerateAnyDebugInfo()) { |
| 1381 | writer->absolute_patch_locations_.reserve( |
| 1382 | writer->GetCompilerDriver()->GetNonRelativeLinkerPatchCount()); |
| 1383 | } |
| 1384 | |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 1385 | struct CodeOffsetsKeyComparator { |
| 1386 | bool operator()(const CompiledMethod* lhs, const CompiledMethod* rhs) const { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 1387 | // Code is deduplicated by CompilerDriver, compare only data pointers. |
| 1388 | if (lhs->GetQuickCode().data() != rhs->GetQuickCode().data()) { |
| 1389 | return lhs->GetQuickCode().data() < rhs->GetQuickCode().data(); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 1390 | } |
| 1391 | // 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] | 1392 | if (UNLIKELY(lhs->GetVmapTable().data() != rhs->GetVmapTable().data())) { |
| 1393 | return lhs->GetVmapTable().data() < rhs->GetVmapTable().data(); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 1394 | } |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 1395 | if (UNLIKELY(lhs->GetMethodInfo().data() != rhs->GetMethodInfo().data())) { |
| 1396 | return lhs->GetMethodInfo().data() < rhs->GetMethodInfo().data(); |
| 1397 | } |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 1398 | if (UNLIKELY(lhs->GetPatches().data() != rhs->GetPatches().data())) { |
| 1399 | return lhs->GetPatches().data() < rhs->GetPatches().data(); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 1400 | } |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 1401 | if (UNLIKELY(lhs->IsIntrinsic() != rhs->IsIntrinsic())) { |
| 1402 | return rhs->IsIntrinsic(); |
| 1403 | } |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 1404 | return false; |
| 1405 | } |
| 1406 | }; |
| 1407 | |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 1408 | uint32_t NewQuickCodeOffset(CompiledMethod* compiled_method, |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1409 | const MethodReference& method_ref, |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 1410 | uint32_t thumb_offset) { |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1411 | offset_ = relative_patcher_->ReserveSpace(offset_, compiled_method, method_ref); |
Vladimir Marko | 0c737df | 2016-08-01 16:33:16 +0100 | [diff] [blame] | 1412 | offset_ += CodeAlignmentSize(offset_, *compiled_method); |
| 1413 | DCHECK_ALIGNED_PARAM(offset_ + sizeof(OatQuickMethodHeader), |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 1414 | GetInstructionSetAlignment(compiled_method->GetInstructionSet())); |
| 1415 | return offset_ + sizeof(OatQuickMethodHeader) + thumb_offset; |
| 1416 | } |
| 1417 | |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1418 | OatWriter* writer_; |
| 1419 | |
| 1420 | // Offset of the code of the compiled methods. |
| 1421 | size_t offset_; |
| 1422 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1423 | // Deduplication is already done on a pointer basis by the compiler driver, |
| 1424 | // 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] | 1425 | SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_; |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 1426 | |
Vladimir Marko | 1b404a8 | 2017-09-01 13:35:26 +0100 | [diff] [blame] | 1427 | // Cache writer_'s members and compiler options. |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 1428 | MultiOatRelativePatcher* relative_patcher_; |
Vladimir Marko | 1b404a8 | 2017-09-01 13:35:26 +0100 | [diff] [blame] | 1429 | uint32_t executable_offset_; |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 1430 | const bool debuggable_; |
Vladimir Marko | 1b404a8 | 2017-09-01 13:35:26 +0100 | [diff] [blame] | 1431 | const bool native_debuggable_; |
| 1432 | const bool generate_debug_info_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1433 | }; |
| 1434 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1435 | class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor { |
| 1436 | public: |
| 1437 | InitMapMethodVisitor(OatWriter* writer, size_t offset) |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 1438 | : OatDexMethodVisitor(writer, offset) {} |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1439 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1440 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it ATTRIBUTE_UNUSED) |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 1441 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1442 | OatClass* oat_class = &writer_->oat_classes_[oat_class_index_]; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1443 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 1444 | |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 1445 | if (HasCompiledCode(compiled_method)) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1446 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 1447 | DCHECK_EQ(oat_class->method_headers_[method_offsets_index_].GetVmapTableOffset(), 0u); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1448 | |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 1449 | ArrayRef<const uint8_t> map = compiled_method->GetVmapTable(); |
| 1450 | uint32_t map_size = map.size() * sizeof(map[0]); |
| 1451 | if (map_size != 0u) { |
| 1452 | size_t offset = dedupe_map_.GetOrCreate( |
| 1453 | map.data(), |
| 1454 | [this, map_size]() { |
| 1455 | uint32_t new_offset = offset_; |
| 1456 | offset_ += map_size; |
| 1457 | return new_offset; |
| 1458 | }); |
| 1459 | // Code offset is not initialized yet, so set the map offset to 0u-offset. |
| 1460 | DCHECK_EQ(oat_class->method_offsets_[method_offsets_index_].code_offset_, 0u); |
| 1461 | oat_class->method_headers_[method_offsets_index_].SetVmapTableOffset(0u - offset); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1462 | } |
| 1463 | ++method_offsets_index_; |
| 1464 | } |
| 1465 | |
| 1466 | return true; |
| 1467 | } |
| 1468 | |
| 1469 | private: |
| 1470 | // Deduplication is already done on a pointer basis by the compiler driver, |
| 1471 | // 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] | 1472 | SafeMap<const uint8_t*, uint32_t> dedupe_map_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1473 | }; |
| 1474 | |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 1475 | class OatWriter::InitMethodInfoVisitor : public OatDexMethodVisitor { |
| 1476 | public: |
| 1477 | InitMethodInfoVisitor(OatWriter* writer, size_t offset) : OatDexMethodVisitor(writer, offset) {} |
| 1478 | |
| 1479 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it ATTRIBUTE_UNUSED) |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 1480 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 1481 | OatClass* oat_class = &writer_->oat_classes_[oat_class_index_]; |
| 1482 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 1483 | |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 1484 | if (HasCompiledCode(compiled_method)) { |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 1485 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 1486 | DCHECK_EQ(oat_class->method_headers_[method_offsets_index_].GetMethodInfoOffset(), 0u); |
| 1487 | ArrayRef<const uint8_t> map = compiled_method->GetMethodInfo(); |
| 1488 | const uint32_t map_size = map.size() * sizeof(map[0]); |
| 1489 | if (map_size != 0u) { |
| 1490 | size_t offset = dedupe_map_.GetOrCreate( |
| 1491 | map.data(), |
| 1492 | [this, map_size]() { |
| 1493 | uint32_t new_offset = offset_; |
| 1494 | offset_ += map_size; |
| 1495 | return new_offset; |
| 1496 | }); |
| 1497 | // Code offset is not initialized yet, so set the map offset to 0u-offset. |
| 1498 | DCHECK_EQ(oat_class->method_offsets_[method_offsets_index_].code_offset_, 0u); |
| 1499 | oat_class->method_headers_[method_offsets_index_].SetMethodInfoOffset(0u - offset); |
| 1500 | } |
| 1501 | ++method_offsets_index_; |
| 1502 | } |
| 1503 | |
| 1504 | return true; |
| 1505 | } |
| 1506 | |
| 1507 | private: |
| 1508 | // Deduplication is already done on a pointer basis by the compiler driver, |
| 1509 | // so we can simply compare the pointers to find out if things are duplicated. |
| 1510 | SafeMap<const uint8_t*, uint32_t> dedupe_map_; |
| 1511 | }; |
| 1512 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1513 | class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor { |
| 1514 | public: |
Artem Udovichenko | b3f2b5c | 2017-01-31 11:49:33 +0300 | [diff] [blame] | 1515 | InitImageMethodVisitor(OatWriter* writer, |
| 1516 | size_t offset, |
| 1517 | const std::vector<const DexFile*>* dex_files) |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 1518 | : OatDexMethodVisitor(writer, offset), |
| 1519 | pointer_size_(GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet())), |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 1520 | class_loader_(writer->HasImage() ? writer->image_writer_->GetClassLoader() : nullptr), |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 1521 | dex_files_(dex_files), |
| 1522 | class_linker_(Runtime::Current()->GetClassLinker()) {} |
Artem Udovichenko | b3f2b5c | 2017-01-31 11:49:33 +0300 | [diff] [blame] | 1523 | |
| 1524 | // Handle copied methods here. Copy pointer to quick code from |
| 1525 | // an origin method to a copied method only if they are |
| 1526 | // in the same oat file. If the origin and the copied methods are |
| 1527 | // in different oat files don't touch the copied method. |
| 1528 | // References to other oat files are not supported yet. |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 1529 | bool StartClass(const DexFile* dex_file, size_t class_def_index) OVERRIDE |
Artem Udovichenko | b3f2b5c | 2017-01-31 11:49:33 +0300 | [diff] [blame] | 1530 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1531 | OatDexMethodVisitor::StartClass(dex_file, class_def_index); |
| 1532 | // Skip classes that are not in the image. |
| 1533 | if (!IsImageClass()) { |
| 1534 | return true; |
| 1535 | } |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 1536 | ObjPtr<mirror::DexCache> dex_cache = class_linker_->FindDexCache(Thread::Current(), *dex_file); |
Artem Udovichenko | b3f2b5c | 2017-01-31 11:49:33 +0300 | [diff] [blame] | 1537 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
| 1538 | mirror::Class* klass = dex_cache->GetResolvedType(class_def.class_idx_); |
| 1539 | if (klass != nullptr) { |
| 1540 | for (ArtMethod& method : klass->GetCopiedMethods(pointer_size_)) { |
| 1541 | // Find origin method. Declaring class and dex_method_idx |
| 1542 | // in the copied method should be the same as in the origin |
| 1543 | // method. |
| 1544 | mirror::Class* declaring_class = method.GetDeclaringClass(); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 1545 | ArtMethod* origin = declaring_class->FindClassMethod( |
Artem Udovichenko | b3f2b5c | 2017-01-31 11:49:33 +0300 | [diff] [blame] | 1546 | declaring_class->GetDexCache(), |
| 1547 | method.GetDexMethodIndex(), |
| 1548 | pointer_size_); |
| 1549 | CHECK(origin != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 1550 | CHECK(!origin->IsDirect()); |
| 1551 | CHECK(origin->GetDeclaringClass() == declaring_class); |
Artem Udovichenko | b3f2b5c | 2017-01-31 11:49:33 +0300 | [diff] [blame] | 1552 | if (IsInOatFile(&declaring_class->GetDexFile())) { |
| 1553 | const void* code_ptr = |
| 1554 | origin->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size_); |
| 1555 | if (code_ptr == nullptr) { |
| 1556 | methods_to_process_.push_back(std::make_pair(&method, origin)); |
| 1557 | } else { |
| 1558 | method.SetEntryPointFromQuickCompiledCodePtrSize( |
| 1559 | code_ptr, pointer_size_); |
| 1560 | } |
| 1561 | } |
| 1562 | } |
| 1563 | } |
| 1564 | return true; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1565 | } |
| 1566 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 1567 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) OVERRIDE |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1568 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1569 | // Skip methods that are not in the image. |
Artem Udovichenko | b3f2b5c | 2017-01-31 11:49:33 +0300 | [diff] [blame] | 1570 | if (!IsImageClass()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1571 | return true; |
| 1572 | } |
| 1573 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1574 | OatClass* oat_class = &writer_->oat_classes_[oat_class_index_]; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1575 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 1576 | |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 1577 | OatMethodOffsets offsets(0u); |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 1578 | if (HasCompiledCode(compiled_method)) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1579 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 1580 | offsets = oat_class->method_offsets_[method_offsets_index_]; |
| 1581 | ++method_offsets_index_; |
| 1582 | } |
| 1583 | |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 1584 | Thread* self = Thread::Current(); |
| 1585 | ObjPtr<mirror::DexCache> dex_cache = class_linker_->FindDexCache(self, *dex_file_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1586 | ArtMethod* method; |
| 1587 | if (writer_->HasBootImage()) { |
| 1588 | const InvokeType invoke_type = it.GetMethodInvokeType( |
| 1589 | dex_file_->GetClassDef(class_def_index_)); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 1590 | // Unchecked as we hold mutator_lock_ on entry. |
| 1591 | ScopedObjectAccessUnchecked soa(self); |
| 1592 | StackHandleScope<1> hs(self); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 1593 | method = class_linker_->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>( |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1594 | *dex_file_, |
| 1595 | it.GetMemberIndex(), |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 1596 | hs.NewHandle(dex_cache), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1597 | ScopedNullHandle<mirror::ClassLoader>(), |
| 1598 | nullptr, |
| 1599 | invoke_type); |
| 1600 | if (method == nullptr) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 1601 | LOG(FATAL_WITHOUT_ABORT) << "Unexpected failure to resolve a method: " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1602 | << dex_file_->PrettyMethod(it.GetMemberIndex(), true); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 1603 | self->AssertPendingException(); |
| 1604 | mirror::Throwable* exc = self->GetException(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1605 | std::string dump = exc->Dump(); |
| 1606 | LOG(FATAL) << dump; |
| 1607 | UNREACHABLE(); |
| 1608 | } |
| 1609 | } else { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 1610 | // Should already have been resolved by the compiler. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1611 | // It may not be resolved if the class failed to verify, in this case, don't set the |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 1612 | // entrypoint. This is not fatal since we shall use a resolution method. |
| 1613 | method = class_linker_->LookupResolvedMethod(it.GetMemberIndex(), dex_cache, class_loader_); |
Andreas Gampe | d9efea6 | 2014-07-21 22:56:08 -0700 | [diff] [blame] | 1614 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1615 | if (method != nullptr && |
| 1616 | compiled_method != nullptr && |
| 1617 | compiled_method->GetQuickCode().size() != 0) { |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 1618 | method->SetEntryPointFromQuickCompiledCodePtrSize( |
| 1619 | reinterpret_cast<void*>(offsets.code_offset_), pointer_size_); |
| 1620 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1621 | |
| 1622 | return true; |
| 1623 | } |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 1624 | |
Artem Udovichenko | b3f2b5c | 2017-01-31 11:49:33 +0300 | [diff] [blame] | 1625 | // Check whether current class is image class |
| 1626 | bool IsImageClass() { |
| 1627 | const DexFile::TypeId& type_id = |
| 1628 | dex_file_->GetTypeId(dex_file_->GetClassDef(class_def_index_).class_idx_); |
| 1629 | const char* class_descriptor = dex_file_->GetTypeDescriptor(type_id); |
| 1630 | return writer_->GetCompilerDriver()->IsImageClass(class_descriptor); |
| 1631 | } |
| 1632 | |
| 1633 | // Check whether specified dex file is in the compiled oat file. |
| 1634 | bool IsInOatFile(const DexFile* dex_file) { |
| 1635 | return ContainsElement(*dex_files_, dex_file); |
| 1636 | } |
| 1637 | |
| 1638 | // Assign a pointer to quick code for copied methods |
| 1639 | // not handled in the method StartClass |
| 1640 | void Postprocess() { |
| 1641 | for (std::pair<ArtMethod*, ArtMethod*>& p : methods_to_process_) { |
| 1642 | ArtMethod* method = p.first; |
| 1643 | ArtMethod* origin = p.second; |
| 1644 | const void* code_ptr = |
| 1645 | origin->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size_); |
| 1646 | if (code_ptr != nullptr) { |
| 1647 | method->SetEntryPointFromQuickCompiledCodePtrSize(code_ptr, pointer_size_); |
| 1648 | } |
| 1649 | } |
| 1650 | } |
| 1651 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 1652 | private: |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1653 | const PointerSize pointer_size_; |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 1654 | ObjPtr<mirror::ClassLoader> class_loader_; |
Artem Udovichenko | b3f2b5c | 2017-01-31 11:49:33 +0300 | [diff] [blame] | 1655 | const std::vector<const DexFile*>* dex_files_; |
| 1656 | ClassLinker* const class_linker_; |
| 1657 | std::vector<std::pair<ArtMethod*, ArtMethod*>> methods_to_process_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1658 | }; |
| 1659 | |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1660 | class OatWriter::WriteCodeMethodVisitor : public OrderedMethodVisitor { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1661 | public: |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1662 | WriteCodeMethodVisitor(OatWriter* writer, |
| 1663 | OutputStream* out, |
| 1664 | const size_t file_offset, |
| 1665 | size_t relative_offset, |
| 1666 | OrderedMethodList ordered_methods) |
| 1667 | : OrderedMethodVisitor(std::move(ordered_methods)), |
| 1668 | writer_(writer), |
| 1669 | offset_(relative_offset), |
| 1670 | dex_file_(nullptr), |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 1671 | pointer_size_(GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet())), |
| 1672 | class_loader_(writer->HasImage() ? writer->image_writer_->GetClassLoader() : nullptr), |
| 1673 | out_(out), |
| 1674 | file_offset_(file_offset), |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 1675 | class_linker_(Runtime::Current()->GetClassLinker()), |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1676 | dex_cache_(nullptr), |
| 1677 | no_thread_suspension_("OatWriter patching") { |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 1678 | patched_code_.reserve(16 * KB); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1679 | if (writer_->HasBootImage()) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1680 | // If we're creating the image, the address space must be ready so that we can apply patches. |
| 1681 | CHECK(writer_->image_writer_->IsImageAddressSpaceReady()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1682 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1683 | } |
| 1684 | |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1685 | virtual bool VisitStart() OVERRIDE { |
| 1686 | return true; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1687 | } |
| 1688 | |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1689 | void UpdateDexFileAndDexCache(const DexFile* dex_file) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1690 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1691 | dex_file_ = dex_file; |
| 1692 | |
| 1693 | // Ordered method visiting is only for compiled methods. |
| 1694 | DCHECK(writer_->MayHaveCompiledMethods()); |
| 1695 | |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 1696 | if (writer_->GetCompilerDriver()->GetCompilerOptions().IsAotCompilationEnabled()) { |
| 1697 | // Only need to set the dex cache if we have compilation. Other modes might have unloaded it. |
| 1698 | if (dex_cache_ == nullptr || dex_cache_->GetDexFile() != dex_file) { |
| 1699 | dex_cache_ = class_linker_->FindDexCache(Thread::Current(), *dex_file); |
| 1700 | DCHECK(dex_cache_ != nullptr); |
| 1701 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1702 | } |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | virtual bool VisitComplete() { |
| 1706 | offset_ = writer_->relative_patcher_->WriteThunks(out_, offset_); |
| 1707 | if (UNLIKELY(offset_ == 0u)) { |
| 1708 | PLOG(ERROR) << "Failed to write final relative call thunks"; |
| 1709 | return false; |
| 1710 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1711 | return true; |
| 1712 | } |
| 1713 | |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1714 | virtual bool VisitMethod(const OrderedMethodData& method_data) OVERRIDE |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1715 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1716 | const MethodReference& method_ref = method_data.method_reference; |
| 1717 | UpdateDexFileAndDexCache(method_ref.dex_file); |
| 1718 | |
| 1719 | OatClass* oat_class = method_data.oat_class; |
| 1720 | CompiledMethod* compiled_method = method_data.compiled_method; |
| 1721 | uint16_t method_offsets_index = method_data.method_offsets_index; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1722 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1723 | // No thread suspension since dex_cache_ that may get invalidated if that occurs. |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 1724 | ScopedAssertNoThreadSuspension tsc(__FUNCTION__); |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1725 | DCHECK(HasCompiledCode(compiled_method)) << method_ref.PrettyMethod(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1726 | |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1727 | // TODO: cleanup DCHECK_OFFSET_ to accept file_offset as parameter. |
| 1728 | size_t file_offset = file_offset_; // Used by DCHECK_OFFSET_ macro. |
| 1729 | OutputStream* out = out_; |
Nicolas Geoffray | f075879 | 2015-07-13 11:56:00 +0000 | [diff] [blame] | 1730 | |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1731 | ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode(); |
| 1732 | uint32_t code_size = quick_code.size() * sizeof(uint8_t); |
| 1733 | |
| 1734 | // Deduplicate code arrays. |
| 1735 | const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index]; |
| 1736 | if (method_offsets.code_offset_ > offset_) { |
| 1737 | offset_ = writer_->relative_patcher_->WriteThunks(out, offset_); |
| 1738 | if (offset_ == 0u) { |
| 1739 | ReportWriteFailure("relative call thunk", method_ref); |
| 1740 | return false; |
| 1741 | } |
| 1742 | uint32_t alignment_size = CodeAlignmentSize(offset_, *compiled_method); |
| 1743 | if (alignment_size != 0) { |
| 1744 | if (!writer_->WriteCodeAlignment(out, alignment_size)) { |
| 1745 | ReportWriteFailure("code alignment padding", method_ref); |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 1746 | return false; |
Nicolas Geoffray | f075879 | 2015-07-13 11:56:00 +0000 | [diff] [blame] | 1747 | } |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1748 | offset_ += alignment_size; |
Nicolas Geoffray | ed6195a | 2015-07-13 17:02:30 +0000 | [diff] [blame] | 1749 | DCHECK_OFFSET_(); |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1750 | } |
| 1751 | DCHECK_ALIGNED_PARAM(offset_ + sizeof(OatQuickMethodHeader), |
| 1752 | GetInstructionSetAlignment(compiled_method->GetInstructionSet())); |
| 1753 | DCHECK_EQ(method_offsets.code_offset_, |
| 1754 | offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta()) |
| 1755 | << dex_file_->PrettyMethod(method_ref.index); |
| 1756 | const OatQuickMethodHeader& method_header = |
| 1757 | oat_class->method_headers_[method_offsets_index]; |
| 1758 | if (!out->WriteFully(&method_header, sizeof(method_header))) { |
| 1759 | ReportWriteFailure("method header", method_ref); |
| 1760 | return false; |
| 1761 | } |
| 1762 | writer_->size_method_header_ += sizeof(method_header); |
| 1763 | offset_ += sizeof(method_header); |
| 1764 | DCHECK_OFFSET_(); |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 1765 | |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1766 | if (!compiled_method->GetPatches().empty()) { |
| 1767 | patched_code_.assign(quick_code.begin(), quick_code.end()); |
| 1768 | quick_code = ArrayRef<const uint8_t>(patched_code_); |
| 1769 | for (const LinkerPatch& patch : compiled_method->GetPatches()) { |
| 1770 | uint32_t literal_offset = patch.LiteralOffset(); |
| 1771 | switch (patch.GetType()) { |
| 1772 | case LinkerPatch::Type::kMethodBssEntry: { |
| 1773 | uint32_t target_offset = |
| 1774 | writer_->bss_start_ + writer_->bss_method_entries_.Get(patch.TargetMethod()); |
| 1775 | writer_->relative_patcher_->PatchPcRelativeReference(&patched_code_, |
| 1776 | patch, |
| 1777 | offset_ + literal_offset, |
| 1778 | target_offset); |
| 1779 | break; |
| 1780 | } |
| 1781 | case LinkerPatch::Type::kCallRelative: { |
| 1782 | // NOTE: Relative calls across oat files are not supported. |
| 1783 | uint32_t target_offset = GetTargetOffset(patch); |
| 1784 | writer_->relative_patcher_->PatchCall(&patched_code_, |
| 1785 | literal_offset, |
| 1786 | offset_ + literal_offset, |
| 1787 | target_offset); |
| 1788 | break; |
| 1789 | } |
| 1790 | case LinkerPatch::Type::kStringRelative: { |
| 1791 | uint32_t target_offset = GetTargetObjectOffset(GetTargetString(patch)); |
| 1792 | writer_->relative_patcher_->PatchPcRelativeReference(&patched_code_, |
| 1793 | patch, |
| 1794 | offset_ + literal_offset, |
| 1795 | target_offset); |
| 1796 | break; |
| 1797 | } |
| 1798 | case LinkerPatch::Type::kStringInternTable: { |
| 1799 | uint32_t target_offset = GetInternTableEntryOffset(patch); |
| 1800 | writer_->relative_patcher_->PatchPcRelativeReference(&patched_code_, |
| 1801 | patch, |
| 1802 | offset_ + literal_offset, |
| 1803 | target_offset); |
| 1804 | break; |
| 1805 | } |
| 1806 | case LinkerPatch::Type::kStringBssEntry: { |
| 1807 | StringReference ref(patch.TargetStringDexFile(), patch.TargetStringIndex()); |
| 1808 | uint32_t target_offset = |
| 1809 | writer_->bss_start_ + writer_->bss_string_entries_.Get(ref); |
| 1810 | writer_->relative_patcher_->PatchPcRelativeReference(&patched_code_, |
| 1811 | patch, |
| 1812 | offset_ + literal_offset, |
| 1813 | target_offset); |
| 1814 | break; |
| 1815 | } |
| 1816 | case LinkerPatch::Type::kTypeRelative: { |
| 1817 | uint32_t target_offset = GetTargetObjectOffset(GetTargetType(patch)); |
| 1818 | writer_->relative_patcher_->PatchPcRelativeReference(&patched_code_, |
| 1819 | patch, |
| 1820 | offset_ + literal_offset, |
| 1821 | target_offset); |
| 1822 | break; |
| 1823 | } |
| 1824 | case LinkerPatch::Type::kTypeClassTable: { |
| 1825 | uint32_t target_offset = GetClassTableEntryOffset(patch); |
| 1826 | writer_->relative_patcher_->PatchPcRelativeReference(&patched_code_, |
| 1827 | patch, |
| 1828 | offset_ + literal_offset, |
| 1829 | target_offset); |
| 1830 | break; |
| 1831 | } |
| 1832 | case LinkerPatch::Type::kTypeBssEntry: { |
| 1833 | TypeReference ref(patch.TargetTypeDexFile(), patch.TargetTypeIndex()); |
| 1834 | uint32_t target_offset = writer_->bss_start_ + writer_->bss_type_entries_.Get(ref); |
| 1835 | writer_->relative_patcher_->PatchPcRelativeReference(&patched_code_, |
| 1836 | patch, |
| 1837 | offset_ + literal_offset, |
| 1838 | target_offset); |
| 1839 | break; |
| 1840 | } |
| 1841 | case LinkerPatch::Type::kCall: { |
| 1842 | uint32_t target_offset = GetTargetOffset(patch); |
| 1843 | PatchCodeAddress(&patched_code_, literal_offset, target_offset); |
| 1844 | break; |
| 1845 | } |
| 1846 | case LinkerPatch::Type::kMethodRelative: { |
| 1847 | uint32_t target_offset = GetTargetMethodOffset(GetTargetMethod(patch)); |
| 1848 | writer_->relative_patcher_->PatchPcRelativeReference(&patched_code_, |
| 1849 | patch, |
| 1850 | offset_ + literal_offset, |
| 1851 | target_offset); |
| 1852 | break; |
| 1853 | } |
| 1854 | case LinkerPatch::Type::kBakerReadBarrierBranch: { |
| 1855 | writer_->relative_patcher_->PatchBakerReadBarrierBranch(&patched_code_, |
| 1856 | patch, |
| 1857 | offset_ + literal_offset); |
| 1858 | break; |
| 1859 | } |
| 1860 | default: { |
| 1861 | DCHECK(false) << "Unexpected linker patch type: " << patch.GetType(); |
| 1862 | break; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 1863 | } |
| 1864 | } |
| 1865 | } |
Nicolas Geoffray | f075879 | 2015-07-13 11:56:00 +0000 | [diff] [blame] | 1866 | } |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1867 | |
| 1868 | if (!out->WriteFully(quick_code.data(), code_size)) { |
| 1869 | ReportWriteFailure("method code", method_ref); |
| 1870 | return false; |
| 1871 | } |
| 1872 | writer_->size_code_ += code_size; |
| 1873 | offset_ += code_size; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1874 | } |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1875 | DCHECK_OFFSET_(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1876 | |
| 1877 | return true; |
| 1878 | } |
| 1879 | |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1880 | size_t GetOffset() const { |
| 1881 | return offset_; |
| 1882 | } |
| 1883 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1884 | private: |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1885 | OatWriter* const writer_; |
| 1886 | |
| 1887 | // Updated in VisitMethod as methods are written out. |
| 1888 | size_t offset_; |
| 1889 | |
| 1890 | // Potentially varies with every different VisitMethod. |
| 1891 | // Used to determine which DexCache to use when finding ArtMethods. |
| 1892 | const DexFile* dex_file_; |
| 1893 | |
| 1894 | // Pointer size we are compiling to. |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 1895 | const PointerSize pointer_size_; |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1896 | // The image writer's classloader, if there is one, else null. |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1897 | ObjPtr<mirror::ClassLoader> class_loader_; |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1898 | // Stream to output file, where the OAT code will be written to. |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1899 | OutputStream* const out_; |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 1900 | const size_t file_offset_; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1901 | ClassLinker* const class_linker_; |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1902 | ObjPtr<mirror::DexCache> dex_cache_; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1903 | std::vector<uint8_t> patched_code_; |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1904 | const ScopedAssertNoThreadSuspension no_thread_suspension_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1905 | |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1906 | void ReportWriteFailure(const char* what, const MethodReference& method_ref) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1907 | PLOG(ERROR) << "Failed to write " << what << " for " |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1908 | << method_ref.PrettyMethod() << " to " << out_->GetLocation(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1909 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1910 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1911 | ArtMethod* GetTargetMethod(const LinkerPatch& patch) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1912 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1913 | MethodReference ref = patch.TargetMethod(); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1914 | ObjPtr<mirror::DexCache> dex_cache = |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1915 | (dex_file_ == ref.dex_file) ? dex_cache_ : class_linker_->FindDexCache( |
| 1916 | Thread::Current(), *ref.dex_file); |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 1917 | ArtMethod* method = |
| 1918 | class_linker_->LookupResolvedMethod(ref.index, dex_cache, class_loader_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1919 | CHECK(method != nullptr); |
| 1920 | return method; |
| 1921 | } |
| 1922 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1923 | uint32_t GetTargetOffset(const LinkerPatch& patch) REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1924 | uint32_t target_offset = writer_->relative_patcher_->GetOffset(patch.TargetMethod()); |
| 1925 | // If there's no new compiled code, either we're compiling an app and the target method |
| 1926 | // is in the boot image, or we need to point to the correct trampoline. |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1927 | if (UNLIKELY(target_offset == 0)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1928 | ArtMethod* target = GetTargetMethod(patch); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1929 | DCHECK(target != nullptr); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 1930 | const void* oat_code_offset = |
| 1931 | target->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size_); |
Jeff Hao | a0acc2d | 2015-01-27 11:22:04 -0800 | [diff] [blame] | 1932 | if (oat_code_offset != 0) { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1933 | DCHECK(!writer_->HasBootImage()); |
Jeff Hao | a0acc2d | 2015-01-27 11:22:04 -0800 | [diff] [blame] | 1934 | DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(oat_code_offset)); |
| 1935 | DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(oat_code_offset)); |
| 1936 | DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(oat_code_offset)); |
| 1937 | target_offset = PointerToLowMemUInt32(oat_code_offset); |
| 1938 | } else { |
| 1939 | target_offset = target->IsNative() |
| 1940 | ? writer_->oat_header_->GetQuickGenericJniTrampolineOffset() |
| 1941 | : writer_->oat_header_->GetQuickToInterpreterBridgeOffset(); |
| 1942 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1943 | } |
| 1944 | return target_offset; |
| 1945 | } |
| 1946 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1947 | ObjPtr<mirror::DexCache> GetDexCache(const DexFile* target_dex_file) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1948 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 052164a | 2016-04-27 13:54:18 +0100 | [diff] [blame] | 1949 | return (target_dex_file == dex_file_) |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1950 | ? dex_cache_ |
Vladimir Marko | 052164a | 2016-04-27 13:54:18 +0100 | [diff] [blame] | 1951 | : class_linker_->FindDexCache(Thread::Current(), *target_dex_file); |
| 1952 | } |
| 1953 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1954 | mirror::Class* GetTargetType(const LinkerPatch& patch) REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1955 | DCHECK(writer_->HasImage()); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1956 | ObjPtr<mirror::DexCache> dex_cache = GetDexCache(patch.TargetTypeDexFile()); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1957 | ObjPtr<mirror::Class> type = |
| 1958 | ClassLinker::LookupResolvedType(patch.TargetTypeIndex(), dex_cache, class_loader_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1959 | CHECK(type != nullptr); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1960 | return type.Ptr(); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1961 | } |
| 1962 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1963 | mirror::String* GetTargetString(const LinkerPatch& patch) REQUIRES_SHARED(Locks::mutator_lock_) { |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 1964 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 1965 | mirror::String* string = linker->LookupString(*patch.TargetStringDexFile(), |
| 1966 | patch.TargetStringIndex(), |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 1967 | GetDexCache(patch.TargetStringDexFile())); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1968 | DCHECK(string != nullptr); |
| 1969 | DCHECK(writer_->HasBootImage() || |
| 1970 | Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(string)); |
| 1971 | return string; |
| 1972 | } |
| 1973 | |
Vladimir Marko | 6597946 | 2017-05-19 17:25:12 +0100 | [diff] [blame] | 1974 | uint32_t GetTargetMethodOffset(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1975 | DCHECK(writer_->HasBootImage()); |
| 1976 | method = writer_->image_writer_->GetImageMethodAddress(method); |
| 1977 | size_t oat_index = writer_->image_writer_->GetOatIndexForDexFile(dex_file_); |
| 1978 | uintptr_t oat_data_begin = writer_->image_writer_->GetOatDataBegin(oat_index); |
| 1979 | // TODO: Clean up offset types. The target offset must be treated as signed. |
| 1980 | return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method) - oat_data_begin); |
| 1981 | } |
| 1982 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1983 | uint32_t GetTargetObjectOffset(mirror::Object* object) REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1984 | DCHECK(writer_->HasBootImage()); |
| 1985 | object = writer_->image_writer_->GetImageAddress(object); |
| 1986 | size_t oat_index = writer_->image_writer_->GetOatIndexForDexFile(dex_file_); |
| 1987 | uintptr_t oat_data_begin = writer_->image_writer_->GetOatDataBegin(oat_index); |
| 1988 | // TODO: Clean up offset types. The target offset must be treated as signed. |
| 1989 | return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(object) - oat_data_begin); |
| 1990 | } |
| 1991 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1992 | void PatchObjectAddress(std::vector<uint8_t>* code, uint32_t offset, mirror::Object* object) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1993 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1994 | if (writer_->HasBootImage()) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1995 | object = writer_->image_writer_->GetImageAddress(object); |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 1996 | } else { |
| 1997 | // NOTE: We're using linker patches for app->boot references when the image can |
| 1998 | // be relocated and therefore we need to emit .oat_patches. We're not using this |
| 1999 | // for app->app references, so check that the object is in the image space. |
| 2000 | DCHECK(Runtime::Current()->GetHeap()->FindSpaceFromObject(object, false)->IsImageSpace()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2001 | } |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 2002 | // 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] | 2003 | uint32_t address = PointerToLowMemUInt32(object); |
| 2004 | DCHECK_LE(offset + 4, code->size()); |
| 2005 | uint8_t* data = &(*code)[offset]; |
| 2006 | data[0] = address & 0xffu; |
| 2007 | data[1] = (address >> 8) & 0xffu; |
| 2008 | data[2] = (address >> 16) & 0xffu; |
| 2009 | data[3] = (address >> 24) & 0xffu; |
| 2010 | } |
| 2011 | |
| 2012 | void PatchCodeAddress(std::vector<uint8_t>* code, uint32_t offset, uint32_t target_offset) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2013 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 2014 | uint32_t address = target_offset; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2015 | if (writer_->HasBootImage()) { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2016 | size_t oat_index = writer_->image_writer_->GetOatIndexForDexCache(dex_cache_); |
| 2017 | // TODO: Clean up offset types. |
| 2018 | // The target_offset must be treated as signed for cross-oat patching. |
| 2019 | const void* target = reinterpret_cast<const void*>( |
| 2020 | writer_->image_writer_->GetOatDataBegin(oat_index) + |
| 2021 | static_cast<int32_t>(target_offset)); |
| 2022 | address = PointerToLowMemUInt32(target); |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 2023 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2024 | DCHECK_LE(offset + 4, code->size()); |
| 2025 | uint8_t* data = &(*code)[offset]; |
| 2026 | data[0] = address & 0xffu; |
| 2027 | data[1] = (address >> 8) & 0xffu; |
| 2028 | data[2] = (address >> 16) & 0xffu; |
| 2029 | data[3] = (address >> 24) & 0xffu; |
| 2030 | } |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 2031 | |
| 2032 | // Calculate the offset of the InternTable slot (GcRoot<String>) when mmapped to the .bss. |
| 2033 | uint32_t GetInternTableEntryOffset(const LinkerPatch& patch) |
| 2034 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2035 | DCHECK(!writer_->HasBootImage()); |
| 2036 | const uint8_t* string_root = writer_->LookupBootImageInternTableSlot( |
| 2037 | *patch.TargetStringDexFile(), patch.TargetStringIndex()); |
| 2038 | DCHECK(string_root != nullptr); |
Vladimir Marko | 94ec2db | 2017-09-06 17:21:03 +0100 | [diff] [blame] | 2039 | return GetBootImageTableEntryOffset(string_root); |
| 2040 | } |
| 2041 | |
| 2042 | // Calculate the offset of the ClassTable::TableSlot when mmapped to the .bss. |
| 2043 | uint32_t GetClassTableEntryOffset(const LinkerPatch& patch) |
| 2044 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2045 | DCHECK(!writer_->HasBootImage()); |
| 2046 | const uint8_t* table_slot = |
| 2047 | writer_->LookupBootImageClassTableSlot(*patch.TargetTypeDexFile(), patch.TargetTypeIndex()); |
| 2048 | DCHECK(table_slot != nullptr); |
| 2049 | return GetBootImageTableEntryOffset(table_slot); |
| 2050 | } |
| 2051 | |
| 2052 | uint32_t GetBootImageTableEntryOffset(const uint8_t* raw_root) { |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 2053 | uint32_t base_offset = writer_->bss_start_; |
| 2054 | for (gc::space::ImageSpace* space : Runtime::Current()->GetHeap()->GetBootImageSpaces()) { |
| 2055 | const uint8_t* const_tables_begin = |
| 2056 | space->Begin() + space->GetImageHeader().GetBootImageConstantTablesOffset(); |
Vladimir Marko | 94ec2db | 2017-09-06 17:21:03 +0100 | [diff] [blame] | 2057 | size_t offset = static_cast<size_t>(raw_root - const_tables_begin); |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 2058 | if (offset < space->GetImageHeader().GetBootImageConstantTablesSize()) { |
| 2059 | DCHECK_LE(base_offset + offset, writer_->bss_start_ + writer_->bss_methods_offset_); |
| 2060 | return base_offset + offset; |
| 2061 | } |
| 2062 | base_offset += space->GetImageHeader().GetBootImageConstantTablesSize(); |
| 2063 | } |
| 2064 | LOG(FATAL) << "Didn't find boot image string in boot image intern tables!"; |
| 2065 | UNREACHABLE(); |
| 2066 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 2067 | }; |
| 2068 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 2069 | class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor { |
| 2070 | public: |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 2071 | WriteMapMethodVisitor(OatWriter* writer, |
| 2072 | OutputStream* out, |
| 2073 | const size_t file_offset, |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 2074 | size_t relative_offset) |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2075 | : OatDexMethodVisitor(writer, relative_offset), |
| 2076 | out_(out), |
| 2077 | file_offset_(file_offset) {} |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 2078 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2079 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) OVERRIDE { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2080 | OatClass* oat_class = &writer_->oat_classes_[oat_class_index_]; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 2081 | const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 2082 | |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 2083 | if (HasCompiledCode(compiled_method)) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 2084 | size_t file_offset = file_offset_; |
| 2085 | OutputStream* out = out_; |
| 2086 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 2087 | uint32_t map_offset = oat_class->method_headers_[method_offsets_index_].GetVmapTableOffset(); |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 2088 | uint32_t code_offset = oat_class->method_offsets_[method_offsets_index_].code_offset_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 2089 | ++method_offsets_index_; |
| 2090 | |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 2091 | DCHECK((compiled_method->GetVmapTable().size() == 0u && map_offset == 0u) || |
| 2092 | (compiled_method->GetVmapTable().size() != 0u && map_offset != 0u)) |
| 2093 | << compiled_method->GetVmapTable().size() << " " << map_offset << " " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2094 | << dex_file_->PrettyMethod(it.GetMemberIndex()); |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 2095 | |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 2096 | // If vdex is enabled, only emit the map for compiled code. The quickening info |
| 2097 | // is emitted in the vdex already. |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 2098 | if (map_offset != 0u) { |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 2099 | // Transform map_offset to actual oat data offset. |
| 2100 | map_offset = (code_offset - compiled_method->CodeDelta()) - map_offset; |
| 2101 | DCHECK_NE(map_offset, 0u); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2102 | DCHECK_LE(map_offset, offset_) << dex_file_->PrettyMethod(it.GetMemberIndex()); |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 2103 | |
| 2104 | ArrayRef<const uint8_t> map = compiled_method->GetVmapTable(); |
| 2105 | size_t map_size = map.size() * sizeof(map[0]); |
| 2106 | if (map_offset == offset_) { |
| 2107 | // Write deduplicated map (code info for Optimizing or transformation info for dex2dex). |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 2108 | if (UNLIKELY(!out->WriteFully(map.data(), map_size))) { |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 2109 | ReportWriteFailure(it); |
| 2110 | return false; |
| 2111 | } |
| 2112 | offset_ += map_size; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 2113 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 2114 | } |
| 2115 | DCHECK_OFFSET_(); |
| 2116 | } |
| 2117 | |
| 2118 | return true; |
| 2119 | } |
| 2120 | |
| 2121 | private: |
| 2122 | OutputStream* const out_; |
| 2123 | size_t const file_offset_; |
| 2124 | |
| 2125 | void ReportWriteFailure(const ClassDataItemIterator& it) { |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 2126 | PLOG(ERROR) << "Failed to write map for " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2127 | << dex_file_->PrettyMethod(it.GetMemberIndex()) << " to " << out_->GetLocation(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 2128 | } |
| 2129 | }; |
| 2130 | |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 2131 | class OatWriter::WriteMethodInfoVisitor : public OatDexMethodVisitor { |
| 2132 | public: |
| 2133 | WriteMethodInfoVisitor(OatWriter* writer, |
| 2134 | OutputStream* out, |
| 2135 | const size_t file_offset, |
| 2136 | size_t relative_offset) |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2137 | : OatDexMethodVisitor(writer, relative_offset), |
| 2138 | out_(out), |
| 2139 | file_offset_(file_offset) {} |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 2140 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2141 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) OVERRIDE { |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 2142 | OatClass* oat_class = &writer_->oat_classes_[oat_class_index_]; |
| 2143 | const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 2144 | |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 2145 | if (HasCompiledCode(compiled_method)) { |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 2146 | size_t file_offset = file_offset_; |
| 2147 | OutputStream* out = out_; |
| 2148 | uint32_t map_offset = oat_class->method_headers_[method_offsets_index_].GetMethodInfoOffset(); |
| 2149 | uint32_t code_offset = oat_class->method_offsets_[method_offsets_index_].code_offset_; |
| 2150 | ++method_offsets_index_; |
| 2151 | DCHECK((compiled_method->GetMethodInfo().size() == 0u && map_offset == 0u) || |
| 2152 | (compiled_method->GetMethodInfo().size() != 0u && map_offset != 0u)) |
| 2153 | << compiled_method->GetMethodInfo().size() << " " << map_offset << " " |
| 2154 | << dex_file_->PrettyMethod(it.GetMemberIndex()); |
| 2155 | if (map_offset != 0u) { |
| 2156 | // Transform map_offset to actual oat data offset. |
| 2157 | map_offset = (code_offset - compiled_method->CodeDelta()) - map_offset; |
| 2158 | DCHECK_NE(map_offset, 0u); |
| 2159 | DCHECK_LE(map_offset, offset_) << dex_file_->PrettyMethod(it.GetMemberIndex()); |
| 2160 | |
| 2161 | ArrayRef<const uint8_t> map = compiled_method->GetMethodInfo(); |
| 2162 | size_t map_size = map.size() * sizeof(map[0]); |
| 2163 | if (map_offset == offset_) { |
| 2164 | // Write deduplicated map (code info for Optimizing or transformation info for dex2dex). |
| 2165 | if (UNLIKELY(!out->WriteFully(map.data(), map_size))) { |
| 2166 | ReportWriteFailure(it); |
| 2167 | return false; |
| 2168 | } |
| 2169 | offset_ += map_size; |
| 2170 | } |
| 2171 | } |
| 2172 | DCHECK_OFFSET_(); |
| 2173 | } |
| 2174 | |
| 2175 | return true; |
| 2176 | } |
| 2177 | |
| 2178 | private: |
| 2179 | OutputStream* const out_; |
| 2180 | size_t const file_offset_; |
| 2181 | |
| 2182 | void ReportWriteFailure(const ClassDataItemIterator& it) { |
| 2183 | PLOG(ERROR) << "Failed to write map for " |
| 2184 | << dex_file_->PrettyMethod(it.GetMemberIndex()) << " to " << out_->GetLocation(); |
| 2185 | } |
| 2186 | }; |
| 2187 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 2188 | // Visit all methods from all classes in all dex files with the specified visitor. |
| 2189 | bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) { |
| 2190 | for (const DexFile* dex_file : *dex_files_) { |
| 2191 | const size_t class_def_count = dex_file->NumClassDefs(); |
| 2192 | for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) { |
| 2193 | if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) { |
| 2194 | return false; |
| 2195 | } |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 2196 | if (MayHaveCompiledMethods()) { |
Nicolas Geoffray | 60ca949 | 2016-12-20 21:15:00 +0000 | [diff] [blame] | 2197 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
| 2198 | const uint8_t* class_data = dex_file->GetClassData(class_def); |
| 2199 | if (class_data != nullptr) { // ie not an empty class, such as a marker interface |
| 2200 | ClassDataItemIterator it(*dex_file, class_data); |
Mathieu Chartier | e17cf24 | 2017-06-19 11:05:51 -0700 | [diff] [blame] | 2201 | it.SkipAllFields(); |
Nicolas Geoffray | 60ca949 | 2016-12-20 21:15:00 +0000 | [diff] [blame] | 2202 | size_t class_def_method_index = 0u; |
Mathieu Chartier | b7c273c | 2017-11-10 18:07:56 -0800 | [diff] [blame] | 2203 | while (it.HasNextMethod()) { |
Nicolas Geoffray | 60ca949 | 2016-12-20 21:15:00 +0000 | [diff] [blame] | 2204 | if (!visitor->VisitMethod(class_def_method_index, it)) { |
| 2205 | return false; |
| 2206 | } |
| 2207 | ++class_def_method_index; |
| 2208 | it.Next(); |
| 2209 | } |
Mathieu Chartier | b7c273c | 2017-11-10 18:07:56 -0800 | [diff] [blame] | 2210 | DCHECK(!it.HasNext()); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 2211 | } |
| 2212 | } |
| 2213 | if (UNLIKELY(!visitor->EndClass())) { |
| 2214 | return false; |
| 2215 | } |
| 2216 | } |
| 2217 | } |
| 2218 | return true; |
| 2219 | } |
| 2220 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2221 | size_t OatWriter::InitOatHeader(InstructionSet instruction_set, |
| 2222 | const InstructionSetFeatures* instruction_set_features, |
| 2223 | uint32_t num_dex_files, |
| 2224 | SafeMap<std::string, std::string>* key_value_store) { |
| 2225 | TimingLogger::ScopedTiming split("InitOatHeader", timings_); |
| 2226 | oat_header_.reset(OatHeader::Create(instruction_set, |
| 2227 | instruction_set_features, |
| 2228 | num_dex_files, |
| 2229 | key_value_store)); |
| 2230 | size_oat_header_ += sizeof(OatHeader); |
| 2231 | size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 2232 | return oat_header_->GetHeaderSize(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2233 | } |
| 2234 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2235 | size_t OatWriter::InitClassOffsets(size_t offset) { |
| 2236 | // Reserve space for class offsets in OAT and update class_offsets_offset_. |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2237 | for (OatDexFile& oat_dex_file : oat_dex_files_) { |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2238 | DCHECK_EQ(oat_dex_file.class_offsets_offset_, 0u); |
| 2239 | if (!oat_dex_file.class_offsets_.empty()) { |
| 2240 | // Class offsets are required to be 4 byte aligned. |
| 2241 | offset = RoundUp(offset, 4u); |
| 2242 | oat_dex_file.class_offsets_offset_ = offset; |
| 2243 | offset += oat_dex_file.GetClassOffsetsRawSize(); |
| 2244 | DCHECK_ALIGNED(offset, 4u); |
| 2245 | } |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 2246 | } |
| 2247 | return offset; |
| 2248 | } |
| 2249 | |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 2250 | size_t OatWriter::InitOatClasses(size_t offset) { |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 2251 | // calculate the offsets within OatDexFiles to OatClasses |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 2252 | InitOatClassesMethodVisitor visitor(this, offset); |
| 2253 | bool success = VisitDexMethods(&visitor); |
| 2254 | CHECK(success); |
| 2255 | offset = visitor.GetOffset(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 2256 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 2257 | // Update oat_dex_files_. |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 2258 | auto oat_class_it = oat_class_headers_.begin(); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2259 | for (OatDexFile& oat_dex_file : oat_dex_files_) { |
| 2260 | for (uint32_t& class_offset : oat_dex_file.class_offsets_) { |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 2261 | DCHECK(oat_class_it != oat_class_headers_.end()); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2262 | class_offset = oat_class_it->offset_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 2263 | ++oat_class_it; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2264 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2265 | } |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 2266 | CHECK(oat_class_it == oat_class_headers_.end()); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 2267 | |
| 2268 | return offset; |
| 2269 | } |
| 2270 | |
| 2271 | size_t OatWriter::InitOatMaps(size_t offset) { |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 2272 | if (!MayHaveCompiledMethods()) { |
Nicolas Geoffray | 60ca949 | 2016-12-20 21:15:00 +0000 | [diff] [blame] | 2273 | return offset; |
| 2274 | } |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 2275 | { |
| 2276 | InitMapMethodVisitor visitor(this, offset); |
| 2277 | bool success = VisitDexMethods(&visitor); |
| 2278 | DCHECK(success); |
| 2279 | offset = visitor.GetOffset(); |
| 2280 | } |
| 2281 | { |
| 2282 | InitMethodInfoVisitor visitor(this, offset); |
| 2283 | bool success = VisitDexMethods(&visitor); |
| 2284 | DCHECK(success); |
| 2285 | offset = visitor.GetOffset(); |
| 2286 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2287 | return offset; |
| 2288 | } |
| 2289 | |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 2290 | template <typename GetBssOffset> |
| 2291 | static size_t CalculateNumberOfIndexBssMappingEntries(size_t number_of_indexes, |
| 2292 | size_t slot_size, |
| 2293 | const BitVector& indexes, |
| 2294 | GetBssOffset get_bss_offset) { |
| 2295 | IndexBssMappingEncoder encoder(number_of_indexes, slot_size); |
| 2296 | size_t number_of_entries = 0u; |
| 2297 | bool first_index = true; |
| 2298 | for (uint32_t index : indexes.Indexes()) { |
| 2299 | uint32_t bss_offset = get_bss_offset(index); |
| 2300 | if (first_index || !encoder.TryMerge(index, bss_offset)) { |
| 2301 | encoder.Reset(index, bss_offset); |
| 2302 | ++number_of_entries; |
| 2303 | first_index = false; |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2304 | } |
| 2305 | } |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 2306 | DCHECK_NE(number_of_entries, 0u); |
| 2307 | return number_of_entries; |
| 2308 | } |
| 2309 | |
| 2310 | template <typename GetBssOffset> |
| 2311 | static size_t CalculateIndexBssMappingSize(size_t number_of_indexes, |
| 2312 | size_t slot_size, |
| 2313 | const BitVector& indexes, |
| 2314 | GetBssOffset get_bss_offset) { |
| 2315 | size_t number_of_entries = CalculateNumberOfIndexBssMappingEntries(number_of_indexes, |
| 2316 | slot_size, |
| 2317 | indexes, |
| 2318 | get_bss_offset); |
| 2319 | return IndexBssMapping::ComputeSize(number_of_entries); |
| 2320 | } |
| 2321 | |
| 2322 | size_t OatWriter::InitIndexBssMappings(size_t offset) { |
| 2323 | if (bss_method_entry_references_.empty() && |
| 2324 | bss_type_entry_references_.empty() && |
| 2325 | bss_string_entry_references_.empty()) { |
| 2326 | return offset; |
| 2327 | } |
| 2328 | // If there are any classes, the class offsets allocation aligns the offset |
| 2329 | // and we cannot have any index bss mappings without class offsets. |
| 2330 | static_assert(alignof(IndexBssMapping) == 4u, "IndexBssMapping alignment check."); |
| 2331 | DCHECK_ALIGNED(offset, 4u); |
| 2332 | |
| 2333 | size_t number_of_method_dex_files = 0u; |
| 2334 | size_t number_of_type_dex_files = 0u; |
| 2335 | size_t number_of_string_dex_files = 0u; |
| 2336 | PointerSize pointer_size = GetInstructionSetPointerSize(oat_header_->GetInstructionSet()); |
| 2337 | for (size_t i = 0, size = dex_files_->size(); i != size; ++i) { |
| 2338 | const DexFile* dex_file = (*dex_files_)[i]; |
| 2339 | auto method_it = bss_method_entry_references_.find(dex_file); |
| 2340 | if (method_it != bss_method_entry_references_.end()) { |
| 2341 | const BitVector& method_indexes = method_it->second; |
| 2342 | ++number_of_method_dex_files; |
| 2343 | oat_dex_files_[i].method_bss_mapping_offset_ = offset; |
| 2344 | offset += CalculateIndexBssMappingSize( |
| 2345 | dex_file->NumMethodIds(), |
| 2346 | static_cast<size_t>(pointer_size), |
| 2347 | method_indexes, |
| 2348 | [=](uint32_t index) { |
| 2349 | return bss_method_entries_.Get({dex_file, index}); |
| 2350 | }); |
| 2351 | } |
| 2352 | |
| 2353 | auto type_it = bss_type_entry_references_.find(dex_file); |
| 2354 | if (type_it != bss_type_entry_references_.end()) { |
| 2355 | const BitVector& type_indexes = type_it->second; |
| 2356 | ++number_of_type_dex_files; |
| 2357 | oat_dex_files_[i].type_bss_mapping_offset_ = offset; |
| 2358 | offset += CalculateIndexBssMappingSize( |
| 2359 | dex_file->NumTypeIds(), |
| 2360 | sizeof(GcRoot<mirror::Class>), |
| 2361 | type_indexes, |
| 2362 | [=](uint32_t index) { |
| 2363 | return bss_type_entries_.Get({dex_file, dex::TypeIndex(index)}); |
| 2364 | }); |
| 2365 | } |
| 2366 | |
| 2367 | auto string_it = bss_string_entry_references_.find(dex_file); |
| 2368 | if (string_it != bss_string_entry_references_.end()) { |
| 2369 | const BitVector& string_indexes = string_it->second; |
| 2370 | ++number_of_string_dex_files; |
| 2371 | oat_dex_files_[i].string_bss_mapping_offset_ = offset; |
| 2372 | offset += CalculateIndexBssMappingSize( |
| 2373 | dex_file->NumStringIds(), |
| 2374 | sizeof(GcRoot<mirror::String>), |
| 2375 | string_indexes, |
| 2376 | [=](uint32_t index) { |
| 2377 | return bss_string_entries_.Get({dex_file, dex::StringIndex(index)}); |
| 2378 | }); |
| 2379 | } |
| 2380 | } |
| 2381 | // Check that all dex files targeted by bss entries are in `*dex_files_`. |
| 2382 | CHECK_EQ(number_of_method_dex_files, bss_method_entry_references_.size()); |
| 2383 | CHECK_EQ(number_of_type_dex_files, bss_type_entry_references_.size()); |
| 2384 | CHECK_EQ(number_of_string_dex_files, bss_string_entry_references_.size()); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2385 | return offset; |
| 2386 | } |
| 2387 | |
| 2388 | size_t OatWriter::InitOatDexFiles(size_t offset) { |
| 2389 | // Initialize offsets of oat dex files. |
| 2390 | for (OatDexFile& oat_dex_file : oat_dex_files_) { |
| 2391 | oat_dex_file.offset_ = offset; |
| 2392 | offset += oat_dex_file.SizeOf(); |
| 2393 | } |
| 2394 | return offset; |
| 2395 | } |
| 2396 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2397 | size_t OatWriter::InitOatCode(size_t offset) { |
| 2398 | // calculate the offsets within OatHeader to executable code |
| 2399 | size_t old_offset = offset; |
| 2400 | // required to be on a new page boundary |
| 2401 | offset = RoundUp(offset, kPageSize); |
| 2402 | oat_header_->SetExecutableOffset(offset); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 2403 | size_executable_offset_alignment_ = offset - old_offset; |
Vladimir Marko | 1b404a8 | 2017-09-01 13:35:26 +0100 | [diff] [blame] | 2404 | // TODO: Remove unused trampoline offsets from the OatHeader (requires oat version change). |
| 2405 | oat_header_->SetInterpreterToInterpreterBridgeOffset(0); |
| 2406 | oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0); |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 2407 | if (compiler_driver_->GetCompilerOptions().IsBootImage()) { |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 2408 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Vladimir Marko | 1b404a8 | 2017-09-01 13:35:26 +0100 | [diff] [blame] | 2409 | const bool generate_debug_info = compiler_driver_->GetCompilerOptions().GenerateAnyDebugInfo(); |
| 2410 | size_t adjusted_offset = offset; |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 2411 | |
Vladimir Marko | 1b404a8 | 2017-09-01 13:35:26 +0100 | [diff] [blame] | 2412 | #define DO_TRAMPOLINE(field, fn_name) \ |
| 2413 | offset = CompiledCode::AlignCode(offset, instruction_set); \ |
| 2414 | adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \ |
| 2415 | oat_header_->Set ## fn_name ## Offset(adjusted_offset); \ |
| 2416 | (field) = compiler_driver_->Create ## fn_name(); \ |
| 2417 | if (generate_debug_info) { \ |
| 2418 | debug::MethodDebugInfo info = {}; \ |
| 2419 | info.trampoline_name = #fn_name; \ |
| 2420 | info.isa = instruction_set; \ |
| 2421 | info.is_code_address_text_relative = true; \ |
| 2422 | /* Use the code offset rather than the `adjusted_offset`. */ \ |
| 2423 | info.code_address = offset - oat_header_->GetExecutableOffset(); \ |
| 2424 | info.code_size = (field)->size(); \ |
| 2425 | method_info_.push_back(std::move(info)); \ |
| 2426 | } \ |
Chih-Hung Hsieh | fba3997 | 2016-05-11 11:26:48 -0700 | [diff] [blame] | 2427 | offset += (field)->size(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 2428 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2429 | DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2430 | DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 2431 | DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2432 | DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline); |
| 2433 | DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 2434 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2435 | #undef DO_TRAMPOLINE |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 2436 | } else { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2437 | oat_header_->SetJniDlsymLookupOffset(0); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2438 | oat_header_->SetQuickGenericJniTrampolineOffset(0); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 2439 | oat_header_->SetQuickImtConflictTrampolineOffset(0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 2440 | oat_header_->SetQuickResolutionTrampolineOffset(0); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2441 | oat_header_->SetQuickToInterpreterBridgeOffset(0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 2442 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2443 | return offset; |
| 2444 | } |
| 2445 | |
| 2446 | size_t OatWriter::InitOatCodeDexFiles(size_t offset) { |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 2447 | if (!compiler_driver_->GetCompilerOptions().IsAnyCompilationEnabled()) { |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 2448 | if (kOatWriterDebugOatCodeLayout) { |
| 2449 | LOG(INFO) << "InitOatCodeDexFiles: OatWriter(" |
| 2450 | << this << "), " |
| 2451 | << "compilation is disabled"; |
| 2452 | } |
| 2453 | |
Nicolas Geoffray | 60ca949 | 2016-12-20 21:15:00 +0000 | [diff] [blame] | 2454 | return offset; |
| 2455 | } |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 2456 | bool success = false; |
| 2457 | |
| 2458 | { |
| 2459 | ScopedObjectAccess soa(Thread::Current()); |
| 2460 | |
| 2461 | LayoutCodeMethodVisitor layout_code_visitor(this, offset); |
| 2462 | success = VisitDexMethods(&layout_code_visitor); |
| 2463 | DCHECK(success); |
| 2464 | |
| 2465 | LayoutReserveOffsetCodeMethodVisitor layout_reserve_code_visitor( |
| 2466 | this, |
| 2467 | offset, |
| 2468 | layout_code_visitor.ReleaseOrderedMethods()); |
| 2469 | success = layout_reserve_code_visitor.Visit(); |
| 2470 | DCHECK(success); |
| 2471 | offset = layout_reserve_code_visitor.GetOffset(); |
| 2472 | |
| 2473 | // Save the method order because the WriteCodeMethodVisitor will need this |
| 2474 | // order again. |
| 2475 | DCHECK(ordered_methods_ == nullptr); |
| 2476 | ordered_methods_.reset( |
| 2477 | new OrderedMethodList( |
| 2478 | layout_reserve_code_visitor.ReleaseOrderedMethods())); |
| 2479 | |
| 2480 | if (kOatWriterDebugOatCodeLayout) { |
| 2481 | LOG(INFO) << "IniatOatCodeDexFiles: method order: "; |
| 2482 | for (const OrderedMethodData& ordered_method : *ordered_methods_) { |
| 2483 | std::string pretty_name = ordered_method.method_reference.PrettyMethod(); |
| 2484 | LOG(INFO) << pretty_name |
| 2485 | << "@ offset " |
| 2486 | << relative_patcher_->GetOffset(ordered_method.method_reference) |
| 2487 | << " X hotness " |
| 2488 | << reinterpret_cast<void*>(ordered_method.method_hotness.GetFlags()); |
| 2489 | } |
| 2490 | } |
| 2491 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2492 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2493 | if (HasImage()) { |
Artem Udovichenko | b3f2b5c | 2017-01-31 11:49:33 +0300 | [diff] [blame] | 2494 | InitImageMethodVisitor image_visitor(this, offset, dex_files_); |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 2495 | success = VisitDexMethods(&image_visitor); |
Artem Udovichenko | b3f2b5c | 2017-01-31 11:49:33 +0300 | [diff] [blame] | 2496 | image_visitor.Postprocess(); |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 2497 | DCHECK(success); |
| 2498 | offset = image_visitor.GetOffset(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2499 | } |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 2500 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2501 | return offset; |
| 2502 | } |
| 2503 | |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 2504 | void OatWriter::InitBssLayout(InstructionSet instruction_set) { |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2505 | { |
| 2506 | InitBssLayoutMethodVisitor visitor(this); |
| 2507 | bool success = VisitDexMethods(&visitor); |
| 2508 | DCHECK(success); |
| 2509 | } |
| 2510 | |
| 2511 | DCHECK_EQ(bss_size_, 0u); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2512 | if (HasBootImage()) { |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 2513 | DCHECK(!map_boot_image_tables_to_bss_); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2514 | DCHECK(bss_string_entries_.empty()); |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 2515 | } |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 2516 | if (!map_boot_image_tables_to_bss_ && |
| 2517 | bss_method_entries_.empty() && |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 2518 | bss_type_entries_.empty() && |
| 2519 | bss_string_entries_.empty()) { |
| 2520 | // Nothing to put to the .bss section. |
| 2521 | return; |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2522 | } |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 2523 | |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 2524 | // Allocate space for boot image tables in the .bss section. |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2525 | PointerSize pointer_size = GetInstructionSetPointerSize(instruction_set); |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 2526 | if (map_boot_image_tables_to_bss_) { |
| 2527 | for (gc::space::ImageSpace* space : Runtime::Current()->GetHeap()->GetBootImageSpaces()) { |
| 2528 | bss_size_ += space->GetImageHeader().GetBootImageConstantTablesSize(); |
| 2529 | } |
| 2530 | } |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 2531 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2532 | bss_methods_offset_ = bss_size_; |
| 2533 | |
| 2534 | // Prepare offsets for .bss ArtMethod entries. |
| 2535 | for (auto& entry : bss_method_entries_) { |
| 2536 | DCHECK_EQ(entry.second, 0u); |
| 2537 | entry.second = bss_size_; |
| 2538 | bss_size_ += static_cast<size_t>(pointer_size); |
| 2539 | } |
| 2540 | |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 2541 | bss_roots_offset_ = bss_size_; |
| 2542 | |
Vladimir Marko | 6bec91c | 2017-01-09 15:03:12 +0000 | [diff] [blame] | 2543 | // Prepare offsets for .bss Class entries. |
| 2544 | for (auto& entry : bss_type_entries_) { |
| 2545 | DCHECK_EQ(entry.second, 0u); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2546 | entry.second = bss_size_; |
Vladimir Marko | 6bec91c | 2017-01-09 15:03:12 +0000 | [diff] [blame] | 2547 | bss_size_ += sizeof(GcRoot<mirror::Class>); |
| 2548 | } |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 2549 | // Prepare offsets for .bss String entries. |
| 2550 | for (auto& entry : bss_string_entries_) { |
| 2551 | DCHECK_EQ(entry.second, 0u); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2552 | entry.second = bss_size_; |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 2553 | bss_size_ += sizeof(GcRoot<mirror::String>); |
| 2554 | } |
| 2555 | } |
| 2556 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 2557 | bool OatWriter::WriteRodata(OutputStream* out) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2558 | CHECK(write_state_ == WriteState::kWriteRoData); |
| 2559 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2560 | size_t file_offset = oat_data_offset_; |
| 2561 | off_t current_offset = out->Seek(0, kSeekCurrent); |
| 2562 | if (current_offset == static_cast<off_t>(-1)) { |
| 2563 | PLOG(ERROR) << "Failed to retrieve current position in " << out->GetLocation(); |
| 2564 | } |
| 2565 | DCHECK_GE(static_cast<size_t>(current_offset), file_offset + oat_header_->GetHeaderSize()); |
| 2566 | size_t relative_offset = current_offset - file_offset; |
| 2567 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 2568 | // Wrap out to update checksum with each write. |
| 2569 | ChecksumUpdatingOutputStream checksum_updating_out(out, oat_header_.get()); |
| 2570 | out = &checksum_updating_out; |
| 2571 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2572 | relative_offset = WriteClassOffsets(out, file_offset, relative_offset); |
| 2573 | if (relative_offset == 0) { |
| 2574 | PLOG(ERROR) << "Failed to write class offsets to " << out->GetLocation(); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2575 | return false; |
| 2576 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 2577 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2578 | relative_offset = WriteClasses(out, file_offset, relative_offset); |
| 2579 | if (relative_offset == 0) { |
| 2580 | PLOG(ERROR) << "Failed to write classes to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2581 | return false; |
| 2582 | } |
| 2583 | |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 2584 | relative_offset = WriteIndexBssMappings(out, file_offset, relative_offset); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2585 | if (relative_offset == 0) { |
| 2586 | PLOG(ERROR) << "Failed to write method bss mappings to " << out->GetLocation(); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2587 | return false; |
| 2588 | } |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2589 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 2590 | relative_offset = WriteMaps(out, file_offset, relative_offset); |
| 2591 | if (relative_offset == 0) { |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2592 | PLOG(ERROR) << "Failed to write oat code to " << out->GetLocation(); |
| 2593 | return false; |
| 2594 | } |
| 2595 | |
| 2596 | relative_offset = WriteOatDexFiles(out, file_offset, relative_offset); |
| 2597 | if (relative_offset == 0) { |
| 2598 | PLOG(ERROR) << "Failed to write oat dex information to " << out->GetLocation(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 2599 | return false; |
| 2600 | } |
| 2601 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 2602 | // Write padding. |
| 2603 | off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent); |
| 2604 | relative_offset += size_executable_offset_alignment_; |
| 2605 | DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset()); |
| 2606 | size_t expected_file_offset = file_offset + relative_offset; |
| 2607 | if (static_cast<uint32_t>(new_offset) != expected_file_offset) { |
| 2608 | PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset |
| 2609 | << " Expected: " << expected_file_offset << " File: " << out->GetLocation(); |
| 2610 | return 0; |
| 2611 | } |
| 2612 | DCHECK_OFFSET(); |
| 2613 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2614 | write_state_ = WriteState::kWriteText; |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 2615 | return true; |
| 2616 | } |
| 2617 | |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 2618 | class OatWriter::WriteQuickeningInfoMethodVisitor : public DexMethodVisitor { |
| 2619 | public: |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 2620 | WriteQuickeningInfoMethodVisitor(OatWriter* writer, |
| 2621 | OutputStream* out, |
| 2622 | uint32_t offset, |
| 2623 | SafeMap<const uint8_t*, uint32_t>* offset_map) |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2624 | : DexMethodVisitor(writer, offset), |
| 2625 | out_(out), |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 2626 | written_bytes_(0u), |
| 2627 | offset_map_(offset_map) {} |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 2628 | |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 2629 | bool VisitMethod(size_t class_def_method_index ATTRIBUTE_UNUSED, const ClassDataItemIterator& it) |
| 2630 | OVERRIDE { |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 2631 | uint32_t method_idx = it.GetMemberIndex(); |
| 2632 | CompiledMethod* compiled_method = |
| 2633 | writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx)); |
| 2634 | |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 2635 | if (HasQuickeningInfo(compiled_method)) { |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 2636 | ArrayRef<const uint8_t> map = compiled_method->GetVmapTable(); |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 2637 | // Deduplication is already done on a pointer basis by the compiler driver, |
| 2638 | // so we can simply compare the pointers to find out if things are duplicated. |
| 2639 | if (offset_map_->find(map.data()) == offset_map_->end()) { |
| 2640 | uint32_t length = map.size() * sizeof(map.front()); |
| 2641 | offset_map_->Put(map.data(), written_bytes_); |
| 2642 | if (!out_->WriteFully(&length, sizeof(length)) || |
| 2643 | !out_->WriteFully(map.data(), length)) { |
| 2644 | PLOG(ERROR) << "Failed to write quickening info for " |
| 2645 | << dex_file_->PrettyMethod(it.GetMemberIndex()) << " to " |
| 2646 | << out_->GetLocation(); |
| 2647 | return false; |
| 2648 | } |
| 2649 | written_bytes_ += sizeof(length) + length; |
| 2650 | offset_ += sizeof(length) + length; |
| 2651 | } |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 2652 | } |
| 2653 | |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 2654 | return true; |
| 2655 | } |
| 2656 | |
| 2657 | size_t GetNumberOfWrittenBytes() const { |
| 2658 | return written_bytes_; |
| 2659 | } |
| 2660 | |
| 2661 | private: |
| 2662 | OutputStream* const out_; |
| 2663 | size_t written_bytes_; |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 2664 | // Maps quickening map to its offset in the file. |
| 2665 | SafeMap<const uint8_t*, uint32_t>* offset_map_; |
| 2666 | }; |
| 2667 | |
| 2668 | class OatWriter::WriteQuickeningIndicesMethodVisitor { |
| 2669 | public: |
| 2670 | WriteQuickeningIndicesMethodVisitor(OutputStream* out, |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 2671 | uint32_t quickening_info_bytes, |
| 2672 | const SafeMap<const uint8_t*, uint32_t>& offset_map) |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 2673 | : out_(out), |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 2674 | quickening_info_bytes_(quickening_info_bytes), |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 2675 | written_bytes_(0u), |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 2676 | offset_map_(offset_map) {} |
| 2677 | |
| 2678 | bool VisitDexMethods(const std::vector<const DexFile*>& dex_files, const CompilerDriver& driver) { |
| 2679 | for (const DexFile* dex_file : dex_files) { |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 2680 | const size_t class_def_count = dex_file->NumClassDefs(); |
| 2681 | for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) { |
| 2682 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
| 2683 | const uint8_t* class_data = dex_file->GetClassData(class_def); |
| 2684 | if (class_data == nullptr) { |
| 2685 | continue; |
| 2686 | } |
| 2687 | for (ClassDataItemIterator class_it(*dex_file, class_data); |
| 2688 | class_it.HasNext(); |
| 2689 | class_it.Next()) { |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 2690 | if (!class_it.IsAtMethod() || class_it.GetMethodCodeItem() == nullptr) { |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 2691 | continue; |
| 2692 | } |
| 2693 | uint32_t method_idx = class_it.GetMemberIndex(); |
| 2694 | CompiledMethod* compiled_method = |
| 2695 | driver.GetCompiledMethod(MethodReference(dex_file, method_idx)); |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 2696 | const DexFile::CodeItem* code_item = class_it.GetMethodCodeItem(); |
| 2697 | uint32_t existing_debug_info_offset = OatFile::GetDebugInfoOffset(*dex_file, code_item); |
| 2698 | // If the existing offset is already out of bounds (and not magic marker 0xFFFFFFFF) |
| 2699 | // we will pretend the method has been quickened. |
| 2700 | bool existing_offset_out_of_bounds = |
| 2701 | (existing_debug_info_offset >= dex_file->Size() && |
| 2702 | existing_debug_info_offset != 0xFFFFFFFF); |
| 2703 | bool has_quickening_info = HasQuickeningInfo(compiled_method); |
| 2704 | if (has_quickening_info || existing_offset_out_of_bounds) { |
| 2705 | uint32_t new_debug_info_offset = |
| 2706 | dex_file->Size() + quickening_info_bytes_ + written_bytes_; |
| 2707 | // Abort if overflow. |
| 2708 | CHECK_GE(new_debug_info_offset, dex_file->Size()); |
| 2709 | const_cast<DexFile::CodeItem*>(code_item)->SetDebugInfoOffset(new_debug_info_offset); |
| 2710 | uint32_t quickening_offset = has_quickening_info |
| 2711 | ? offset_map_.Get(compiled_method->GetVmapTable().data()) |
| 2712 | : VdexFile::kNoQuickeningInfoOffset; |
| 2713 | if (!out_->WriteFully(&existing_debug_info_offset, |
| 2714 | sizeof(existing_debug_info_offset)) || |
| 2715 | !out_->WriteFully(&quickening_offset, sizeof(quickening_offset))) { |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 2716 | PLOG(ERROR) << "Failed to write quickening info for " |
| 2717 | << dex_file->PrettyMethod(method_idx) << " to " |
| 2718 | << out_->GetLocation(); |
| 2719 | return false; |
| 2720 | } |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 2721 | written_bytes_ += sizeof(existing_debug_info_offset) + sizeof(quickening_offset); |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 2722 | } |
| 2723 | } |
| 2724 | } |
| 2725 | } |
| 2726 | return true; |
| 2727 | } |
| 2728 | |
| 2729 | size_t GetNumberOfWrittenBytes() const { |
| 2730 | return written_bytes_; |
| 2731 | } |
| 2732 | |
| 2733 | private: |
| 2734 | OutputStream* const out_; |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 2735 | const uint32_t quickening_info_bytes_; |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 2736 | size_t written_bytes_; |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 2737 | // Maps quickening map to its offset in the file. |
| 2738 | const SafeMap<const uint8_t*, uint32_t>& offset_map_; |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 2739 | }; |
| 2740 | |
| 2741 | bool OatWriter::WriteQuickeningInfo(OutputStream* vdex_out) { |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 2742 | size_t initial_offset = vdex_size_; |
| 2743 | size_t start_offset = RoundUp(initial_offset, 4u); |
| 2744 | |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 2745 | off_t actual_offset = vdex_out->Seek(start_offset, kSeekSet); |
| 2746 | if (actual_offset != static_cast<off_t>(start_offset)) { |
| 2747 | PLOG(ERROR) << "Failed to seek to quickening info section. Actual: " << actual_offset |
| 2748 | << " Expected: " << start_offset |
| 2749 | << " Output: " << vdex_out->GetLocation(); |
| 2750 | return false; |
| 2751 | } |
| 2752 | |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 2753 | if (compiler_driver_->GetCompilerOptions().IsAnyCompilationEnabled()) { |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 2754 | std::vector<uint32_t> dex_files_indices; |
| 2755 | SafeMap<const uint8_t*, uint32_t> offset_map; |
| 2756 | WriteQuickeningInfoMethodVisitor visitor1(this, vdex_out, start_offset, &offset_map); |
| 2757 | if (!VisitDexMethods(&visitor1)) { |
| 2758 | PLOG(ERROR) << "Failed to write the vdex quickening info. File: " << vdex_out->GetLocation(); |
| 2759 | return false; |
| 2760 | } |
| 2761 | |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 2762 | if (visitor1.GetNumberOfWrittenBytes() > 0) { |
| 2763 | WriteQuickeningIndicesMethodVisitor visitor2(vdex_out, |
| 2764 | visitor1.GetNumberOfWrittenBytes(), |
| 2765 | offset_map); |
| 2766 | if (!visitor2.VisitDexMethods(*dex_files_, *compiler_driver_)) { |
| 2767 | PLOG(ERROR) << "Failed to write the vdex quickening info. File: " |
| 2768 | << vdex_out->GetLocation(); |
| 2769 | return false; |
| 2770 | } |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 2771 | |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 2772 | if (!vdex_out->Flush()) { |
| 2773 | PLOG(ERROR) << "Failed to flush stream after writing quickening info." |
| 2774 | << " File: " << vdex_out->GetLocation(); |
| 2775 | return false; |
| 2776 | } |
| 2777 | size_quickening_info_ = visitor1.GetNumberOfWrittenBytes() + |
| 2778 | visitor2.GetNumberOfWrittenBytes(); |
| 2779 | } else { |
| 2780 | // We know we did not quicken. |
| 2781 | size_quickening_info_ = 0; |
Nicolas Geoffray | 60ca949 | 2016-12-20 21:15:00 +0000 | [diff] [blame] | 2782 | } |
Nicolas Geoffray | 60ca949 | 2016-12-20 21:15:00 +0000 | [diff] [blame] | 2783 | } else { |
| 2784 | // We know we did not quicken. |
| 2785 | size_quickening_info_ = 0; |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 2786 | } |
| 2787 | |
David Brazdil | 93592f5 | 2017-12-08 10:53:27 +0000 | [diff] [blame^] | 2788 | if (size_quickening_info_ == 0) { |
| 2789 | // Nothing was written. Leave `vdex_size_` untouched and unaligned. |
| 2790 | vdex_quickening_info_offset_ = initial_offset; |
| 2791 | size_quickening_info_alignment_ = 0; |
| 2792 | } else { |
| 2793 | vdex_size_ = start_offset + size_quickening_info_; |
| 2794 | vdex_quickening_info_offset_ = start_offset; |
| 2795 | size_quickening_info_alignment_ = start_offset - initial_offset; |
| 2796 | } |
| 2797 | |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 2798 | return true; |
| 2799 | } |
| 2800 | |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 2801 | bool OatWriter::WriteVerifierDeps(OutputStream* vdex_out, verifier::VerifierDeps* verifier_deps) { |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 2802 | if (verifier_deps == nullptr) { |
| 2803 | // Nothing to write. Record the offset, but no need |
| 2804 | // for alignment. |
| 2805 | vdex_verifier_deps_offset_ = vdex_size_; |
| 2806 | return true; |
| 2807 | } |
| 2808 | |
| 2809 | size_t initial_offset = vdex_size_; |
| 2810 | size_t start_offset = RoundUp(initial_offset, 4u); |
| 2811 | |
| 2812 | vdex_size_ = start_offset; |
| 2813 | vdex_verifier_deps_offset_ = vdex_size_; |
| 2814 | size_verifier_deps_alignment_ = start_offset - initial_offset; |
| 2815 | |
| 2816 | off_t actual_offset = vdex_out->Seek(start_offset, kSeekSet); |
| 2817 | if (actual_offset != static_cast<off_t>(start_offset)) { |
| 2818 | PLOG(ERROR) << "Failed to seek to verifier deps section. Actual: " << actual_offset |
| 2819 | << " Expected: " << start_offset |
| 2820 | << " Output: " << vdex_out->GetLocation(); |
| 2821 | return false; |
| 2822 | } |
| 2823 | |
| 2824 | std::vector<uint8_t> buffer; |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 2825 | verifier_deps->Encode(*dex_files_, &buffer); |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 2826 | |
| 2827 | if (!vdex_out->WriteFully(buffer.data(), buffer.size())) { |
| 2828 | PLOG(ERROR) << "Failed to write verifier deps." |
| 2829 | << " File: " << vdex_out->GetLocation(); |
| 2830 | return false; |
| 2831 | } |
| 2832 | if (!vdex_out->Flush()) { |
| 2833 | PLOG(ERROR) << "Failed to flush stream after writing verifier deps." |
| 2834 | << " File: " << vdex_out->GetLocation(); |
| 2835 | return false; |
| 2836 | } |
| 2837 | |
| 2838 | size_verifier_deps_ = buffer.size(); |
| 2839 | vdex_size_ += size_verifier_deps_; |
| 2840 | return true; |
| 2841 | } |
| 2842 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 2843 | bool OatWriter::WriteCode(OutputStream* out) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2844 | CHECK(write_state_ == WriteState::kWriteText); |
| 2845 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 2846 | // Wrap out to update checksum with each write. |
| 2847 | ChecksumUpdatingOutputStream checksum_updating_out(out, oat_header_.get()); |
| 2848 | out = &checksum_updating_out; |
| 2849 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2850 | SetMultiOatRelativePatcherAdjustment(); |
| 2851 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 2852 | const size_t file_offset = oat_data_offset_; |
| 2853 | size_t relative_offset = oat_header_->GetExecutableOffset(); |
| 2854 | DCHECK_OFFSET(); |
| 2855 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 2856 | relative_offset = WriteCode(out, file_offset, relative_offset); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 2857 | if (relative_offset == 0) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 2858 | LOG(ERROR) << "Failed to write oat code to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2859 | return false; |
| 2860 | } |
| 2861 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 2862 | relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset); |
| 2863 | if (relative_offset == 0) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 2864 | 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] | 2865 | return false; |
| 2866 | } |
| 2867 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2868 | const off_t oat_end_file_offset = out->Seek(0, kSeekCurrent); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2869 | if (oat_end_file_offset == static_cast<off_t>(-1)) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2870 | LOG(ERROR) << "Failed to get oat end file offset in " << out->GetLocation(); |
| 2871 | return false; |
| 2872 | } |
| 2873 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 2874 | if (kIsDebugBuild) { |
| 2875 | uint32_t size_total = 0; |
| 2876 | #define DO_STAT(x) \ |
Chih-Hung Hsieh | fba3997 | 2016-05-11 11:26:48 -0700 | [diff] [blame] | 2877 | VLOG(compiler) << #x "=" << PrettySize(x) << " (" << (x) << "B)"; \ |
| 2878 | size_total += (x); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 2879 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2880 | DO_STAT(size_vdex_header_); |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 2881 | DO_STAT(size_vdex_checksums_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 2882 | DO_STAT(size_dex_file_alignment_); |
| 2883 | DO_STAT(size_executable_offset_alignment_); |
| 2884 | DO_STAT(size_oat_header_); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 2885 | DO_STAT(size_oat_header_key_value_store_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 2886 | DO_STAT(size_dex_file_); |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 2887 | DO_STAT(size_verifier_deps_); |
| 2888 | DO_STAT(size_verifier_deps_alignment_); |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 2889 | DO_STAT(size_quickening_info_); |
| 2890 | DO_STAT(size_quickening_info_alignment_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2891 | DO_STAT(size_interpreter_to_interpreter_bridge_); |
| 2892 | DO_STAT(size_interpreter_to_compiled_code_bridge_); |
| 2893 | DO_STAT(size_jni_dlsym_lookup_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2894 | DO_STAT(size_quick_generic_jni_trampoline_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 2895 | DO_STAT(size_quick_imt_conflict_trampoline_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 2896 | DO_STAT(size_quick_resolution_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2897 | DO_STAT(size_quick_to_interpreter_bridge_); |
| 2898 | DO_STAT(size_trampoline_alignment_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 2899 | DO_STAT(size_method_header_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 2900 | DO_STAT(size_code_); |
| 2901 | DO_STAT(size_code_alignment_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2902 | DO_STAT(size_relative_call_thunks_); |
Vladimir Marko | c74658b | 2015-03-31 10:26:41 +0100 | [diff] [blame] | 2903 | DO_STAT(size_misc_thunks_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 2904 | DO_STAT(size_vmap_table_); |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 2905 | DO_STAT(size_method_info_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 2906 | DO_STAT(size_oat_dex_file_location_size_); |
| 2907 | DO_STAT(size_oat_dex_file_location_data_); |
| 2908 | DO_STAT(size_oat_dex_file_location_checksum_); |
| 2909 | DO_STAT(size_oat_dex_file_offset_); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2910 | DO_STAT(size_oat_dex_file_class_offsets_offset_); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2911 | DO_STAT(size_oat_dex_file_lookup_table_offset_); |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 2912 | DO_STAT(size_oat_dex_file_dex_layout_sections_offset_); |
| 2913 | DO_STAT(size_oat_dex_file_dex_layout_sections_); |
| 2914 | DO_STAT(size_oat_dex_file_dex_layout_sections_alignment_); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2915 | DO_STAT(size_oat_dex_file_method_bss_mapping_offset_); |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 2916 | DO_STAT(size_oat_dex_file_type_bss_mapping_offset_); |
| 2917 | DO_STAT(size_oat_dex_file_string_bss_mapping_offset_); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2918 | DO_STAT(size_oat_lookup_table_alignment_); |
| 2919 | DO_STAT(size_oat_lookup_table_); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2920 | DO_STAT(size_oat_class_offsets_alignment_); |
| 2921 | DO_STAT(size_oat_class_offsets_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 2922 | DO_STAT(size_oat_class_type_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 2923 | DO_STAT(size_oat_class_status_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 2924 | DO_STAT(size_oat_class_method_bitmaps_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 2925 | DO_STAT(size_oat_class_method_offsets_); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2926 | DO_STAT(size_method_bss_mappings_); |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 2927 | DO_STAT(size_type_bss_mappings_); |
| 2928 | DO_STAT(size_string_bss_mappings_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 2929 | #undef DO_STAT |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 2930 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2931 | VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; |
| 2932 | |
| 2933 | CHECK_EQ(vdex_size_ + oat_size_, size_total); |
| 2934 | CHECK_EQ(file_offset + size_total - vdex_size_, static_cast<size_t>(oat_end_file_offset)); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 2935 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 2936 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2937 | CHECK_EQ(file_offset + oat_size_, static_cast<size_t>(oat_end_file_offset)); |
| 2938 | CHECK_EQ(oat_size_, relative_offset); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 2939 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2940 | write_state_ = WriteState::kWriteHeader; |
| 2941 | return true; |
| 2942 | } |
| 2943 | |
| 2944 | bool OatWriter::WriteHeader(OutputStream* out, |
| 2945 | uint32_t image_file_location_oat_checksum, |
| 2946 | uintptr_t image_file_location_oat_begin, |
| 2947 | int32_t image_patch_delta) { |
| 2948 | CHECK(write_state_ == WriteState::kWriteHeader); |
| 2949 | |
| 2950 | oat_header_->SetImageFileLocationOatChecksum(image_file_location_oat_checksum); |
| 2951 | oat_header_->SetImageFileLocationOatDataBegin(image_file_location_oat_begin); |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 2952 | if (compiler_driver_->GetCompilerOptions().IsBootImage()) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2953 | CHECK_EQ(image_patch_delta, 0); |
| 2954 | CHECK_EQ(oat_header_->GetImagePatchDelta(), 0); |
| 2955 | } else { |
| 2956 | CHECK_ALIGNED(image_patch_delta, kPageSize); |
| 2957 | oat_header_->SetImagePatchDelta(image_patch_delta); |
| 2958 | } |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2959 | oat_header_->UpdateChecksumWithHeaderData(); |
| 2960 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2961 | const size_t file_offset = oat_data_offset_; |
| 2962 | |
| 2963 | off_t current_offset = out->Seek(0, kSeekCurrent); |
| 2964 | if (current_offset == static_cast<off_t>(-1)) { |
| 2965 | PLOG(ERROR) << "Failed to get current offset from " << out->GetLocation(); |
| 2966 | return false; |
| 2967 | } |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2968 | if (out->Seek(file_offset, kSeekSet) == static_cast<off_t>(-1)) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2969 | PLOG(ERROR) << "Failed to seek to oat header position in " << out->GetLocation(); |
| 2970 | return false; |
| 2971 | } |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 2972 | DCHECK_EQ(file_offset, static_cast<size_t>(out->Seek(0, kSeekCurrent))); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2973 | |
| 2974 | // Flush all other data before writing the header. |
| 2975 | if (!out->Flush()) { |
| 2976 | PLOG(ERROR) << "Failed to flush before writing oat header to " << out->GetLocation(); |
| 2977 | return false; |
| 2978 | } |
| 2979 | // Write the header. |
| 2980 | size_t header_size = oat_header_->GetHeaderSize(); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2981 | if (!out->WriteFully(oat_header_.get(), header_size)) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2982 | PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation(); |
| 2983 | return false; |
| 2984 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2985 | // Flush the header data. |
| 2986 | if (!out->Flush()) { |
| 2987 | PLOG(ERROR) << "Failed to flush after writing oat header to " << out->GetLocation(); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2988 | return false; |
| 2989 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2990 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2991 | if (out->Seek(current_offset, kSeekSet) == static_cast<off_t>(-1)) { |
| 2992 | PLOG(ERROR) << "Failed to seek back after writing oat header to " << out->GetLocation(); |
| 2993 | return false; |
| 2994 | } |
| 2995 | DCHECK_EQ(current_offset, out->Seek(0, kSeekCurrent)); |
| 2996 | |
| 2997 | write_state_ = WriteState::kDone; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2998 | return true; |
| 2999 | } |
| 3000 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3001 | size_t OatWriter::WriteClassOffsets(OutputStream* out, size_t file_offset, size_t relative_offset) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3002 | for (OatDexFile& oat_dex_file : oat_dex_files_) { |
| 3003 | if (oat_dex_file.class_offsets_offset_ != 0u) { |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3004 | // Class offsets are required to be 4 byte aligned. |
| 3005 | if (UNLIKELY(!IsAligned<4u>(relative_offset))) { |
| 3006 | size_t padding_size = RoundUp(relative_offset, 4u) - relative_offset; |
| 3007 | if (!WriteUpTo16BytesAlignment(out, padding_size, &size_oat_class_offsets_alignment_)) { |
| 3008 | return 0u; |
| 3009 | } |
| 3010 | relative_offset += padding_size; |
Vladimir Marko | 919f553 | 2016-01-20 19:13:01 +0000 | [diff] [blame] | 3011 | } |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3012 | DCHECK_OFFSET(); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3013 | if (!oat_dex_file.WriteClassOffsets(this, out)) { |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3014 | return 0u; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3015 | } |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3016 | relative_offset += oat_dex_file.GetClassOffsetsRawSize(); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3017 | } |
| 3018 | } |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3019 | return relative_offset; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3022 | size_t OatWriter::WriteClasses(OutputStream* out, size_t file_offset, size_t relative_offset) { |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 3023 | const bool may_have_compiled = MayHaveCompiledMethods(); |
| 3024 | if (may_have_compiled) { |
| 3025 | CHECK_EQ(oat_class_headers_.size(), oat_classes_.size()); |
| 3026 | } |
| 3027 | for (size_t i = 0; i < oat_class_headers_.size(); ++i) { |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3028 | // If there are any classes, the class offsets allocation aligns the offset. |
| 3029 | DCHECK_ALIGNED(relative_offset, 4u); |
| 3030 | DCHECK_OFFSET(); |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 3031 | if (!oat_class_headers_[i].Write(this, out, oat_data_offset_)) { |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3032 | return 0u; |
Vladimir Marko | 919f553 | 2016-01-20 19:13:01 +0000 | [diff] [blame] | 3033 | } |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 3034 | relative_offset += oat_class_headers_[i].SizeOf(); |
| 3035 | if (may_have_compiled) { |
| 3036 | if (!oat_classes_[i].Write(this, out)) { |
| 3037 | return 0u; |
| 3038 | } |
| 3039 | relative_offset += oat_classes_[i].SizeOf(); |
| 3040 | } |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 3041 | } |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3042 | return relative_offset; |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 3043 | } |
| 3044 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3045 | size_t OatWriter::WriteMaps(OutputStream* out, size_t file_offset, size_t relative_offset) { |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 3046 | { |
| 3047 | size_t vmap_tables_offset = relative_offset; |
| 3048 | WriteMapMethodVisitor visitor(this, out, file_offset, relative_offset); |
| 3049 | if (UNLIKELY(!VisitDexMethods(&visitor))) { |
| 3050 | return 0; |
| 3051 | } |
| 3052 | relative_offset = visitor.GetOffset(); |
| 3053 | size_vmap_table_ = relative_offset - vmap_tables_offset; |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 3054 | } |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 3055 | { |
| 3056 | size_t method_infos_offset = relative_offset; |
| 3057 | WriteMethodInfoVisitor visitor(this, out, file_offset, relative_offset); |
| 3058 | if (UNLIKELY(!VisitDexMethods(&visitor))) { |
| 3059 | return 0; |
| 3060 | } |
| 3061 | relative_offset = visitor.GetOffset(); |
| 3062 | size_method_info_ = relative_offset - method_infos_offset; |
| 3063 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 3064 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 3065 | return relative_offset; |
| 3066 | } |
| 3067 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3068 | |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 3069 | template <typename GetBssOffset> |
| 3070 | size_t WriteIndexBssMapping(OutputStream* out, |
| 3071 | size_t number_of_indexes, |
| 3072 | size_t slot_size, |
| 3073 | const BitVector& indexes, |
| 3074 | GetBssOffset get_bss_offset) { |
| 3075 | // Allocate the IndexBssMapping. |
| 3076 | size_t number_of_entries = CalculateNumberOfIndexBssMappingEntries( |
| 3077 | number_of_indexes, slot_size, indexes, get_bss_offset); |
| 3078 | size_t mappings_size = IndexBssMapping::ComputeSize(number_of_entries); |
| 3079 | DCHECK_ALIGNED(mappings_size, sizeof(uint32_t)); |
| 3080 | std::unique_ptr<uint32_t[]> storage(new uint32_t[mappings_size / sizeof(uint32_t)]); |
| 3081 | IndexBssMapping* mappings = new(storage.get()) IndexBssMapping(number_of_entries); |
| 3082 | mappings->ClearPadding(); |
| 3083 | // Encode the IndexBssMapping. |
| 3084 | IndexBssMappingEncoder encoder(number_of_indexes, slot_size); |
| 3085 | auto init_it = mappings->begin(); |
| 3086 | bool first_index = true; |
| 3087 | for (uint32_t index : indexes.Indexes()) { |
| 3088 | size_t bss_offset = get_bss_offset(index); |
| 3089 | if (first_index) { |
| 3090 | first_index = false; |
| 3091 | encoder.Reset(index, bss_offset); |
| 3092 | } else if (!encoder.TryMerge(index, bss_offset)) { |
| 3093 | *init_it = encoder.GetEntry(); |
| 3094 | ++init_it; |
| 3095 | encoder.Reset(index, bss_offset); |
| 3096 | } |
| 3097 | } |
| 3098 | // Store the last entry. |
| 3099 | *init_it = encoder.GetEntry(); |
| 3100 | ++init_it; |
| 3101 | DCHECK(init_it == mappings->end()); |
| 3102 | |
| 3103 | if (!out->WriteFully(storage.get(), mappings_size)) { |
| 3104 | return 0u; |
| 3105 | } |
| 3106 | return mappings_size; |
| 3107 | } |
| 3108 | |
| 3109 | size_t OatWriter::WriteIndexBssMappings(OutputStream* out, |
| 3110 | size_t file_offset, |
| 3111 | size_t relative_offset) { |
| 3112 | TimingLogger::ScopedTiming split("WriteMethodBssMappings", timings_); |
| 3113 | if (bss_method_entry_references_.empty() && |
| 3114 | bss_type_entry_references_.empty() && |
| 3115 | bss_string_entry_references_.empty()) { |
| 3116 | return relative_offset; |
| 3117 | } |
| 3118 | // If there are any classes, the class offsets allocation aligns the offset |
| 3119 | // and we cannot have method bss mappings without class offsets. |
| 3120 | static_assert(alignof(IndexBssMapping) == sizeof(uint32_t), |
| 3121 | "IndexBssMapping alignment check."); |
| 3122 | DCHECK_ALIGNED(relative_offset, sizeof(uint32_t)); |
| 3123 | |
| 3124 | PointerSize pointer_size = GetInstructionSetPointerSize(oat_header_->GetInstructionSet()); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3125 | for (size_t i = 0, size = dex_files_->size(); i != size; ++i) { |
| 3126 | const DexFile* dex_file = (*dex_files_)[i]; |
| 3127 | OatDexFile* oat_dex_file = &oat_dex_files_[i]; |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 3128 | auto method_it = bss_method_entry_references_.find(dex_file); |
| 3129 | if (method_it != bss_method_entry_references_.end()) { |
| 3130 | const BitVector& method_indexes = method_it->second; |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3131 | DCHECK_EQ(relative_offset, oat_dex_file->method_bss_mapping_offset_); |
| 3132 | DCHECK_OFFSET(); |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 3133 | size_t method_mappings_size = WriteIndexBssMapping( |
| 3134 | out, |
| 3135 | dex_file->NumMethodIds(), |
| 3136 | static_cast<size_t>(pointer_size), |
| 3137 | method_indexes, |
| 3138 | [=](uint32_t index) { |
| 3139 | return bss_method_entries_.Get({dex_file, index}); |
| 3140 | }); |
| 3141 | if (method_mappings_size == 0u) { |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3142 | return 0u; |
| 3143 | } |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 3144 | size_method_bss_mappings_ += method_mappings_size; |
| 3145 | relative_offset += method_mappings_size; |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3146 | } else { |
| 3147 | DCHECK_EQ(0u, oat_dex_file->method_bss_mapping_offset_); |
| 3148 | } |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 3149 | |
| 3150 | auto type_it = bss_type_entry_references_.find(dex_file); |
| 3151 | if (type_it != bss_type_entry_references_.end()) { |
| 3152 | const BitVector& type_indexes = type_it->second; |
| 3153 | DCHECK_EQ(relative_offset, oat_dex_file->type_bss_mapping_offset_); |
| 3154 | DCHECK_OFFSET(); |
| 3155 | size_t type_mappings_size = WriteIndexBssMapping( |
| 3156 | out, |
| 3157 | dex_file->NumTypeIds(), |
| 3158 | sizeof(GcRoot<mirror::Class>), |
| 3159 | type_indexes, |
| 3160 | [=](uint32_t index) { |
| 3161 | return bss_type_entries_.Get({dex_file, dex::TypeIndex(index)}); |
| 3162 | }); |
| 3163 | if (type_mappings_size == 0u) { |
| 3164 | return 0u; |
| 3165 | } |
| 3166 | size_type_bss_mappings_ += type_mappings_size; |
| 3167 | relative_offset += type_mappings_size; |
| 3168 | } else { |
| 3169 | DCHECK_EQ(0u, oat_dex_file->type_bss_mapping_offset_); |
| 3170 | } |
| 3171 | |
| 3172 | auto string_it = bss_string_entry_references_.find(dex_file); |
| 3173 | if (string_it != bss_string_entry_references_.end()) { |
| 3174 | const BitVector& string_indexes = string_it->second; |
| 3175 | DCHECK_EQ(relative_offset, oat_dex_file->string_bss_mapping_offset_); |
| 3176 | DCHECK_OFFSET(); |
| 3177 | size_t string_mappings_size = WriteIndexBssMapping( |
| 3178 | out, |
| 3179 | dex_file->NumStringIds(), |
| 3180 | sizeof(GcRoot<mirror::String>), |
| 3181 | string_indexes, |
| 3182 | [=](uint32_t index) { |
| 3183 | return bss_string_entries_.Get({dex_file, dex::StringIndex(index)}); |
| 3184 | }); |
| 3185 | if (string_mappings_size == 0u) { |
| 3186 | return 0u; |
| 3187 | } |
| 3188 | size_string_bss_mappings_ += string_mappings_size; |
| 3189 | relative_offset += string_mappings_size; |
| 3190 | } else { |
| 3191 | DCHECK_EQ(0u, oat_dex_file->string_bss_mapping_offset_); |
| 3192 | } |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3193 | } |
| 3194 | return relative_offset; |
| 3195 | } |
| 3196 | |
| 3197 | size_t OatWriter::WriteOatDexFiles(OutputStream* out, size_t file_offset, size_t relative_offset) { |
| 3198 | TimingLogger::ScopedTiming split("WriteOatDexFiles", timings_); |
| 3199 | |
| 3200 | for (size_t i = 0, size = oat_dex_files_.size(); i != size; ++i) { |
| 3201 | OatDexFile* oat_dex_file = &oat_dex_files_[i]; |
| 3202 | DCHECK_EQ(relative_offset, oat_dex_file->offset_); |
| 3203 | DCHECK_OFFSET(); |
| 3204 | |
| 3205 | // Write OatDexFile. |
| 3206 | if (!oat_dex_file->Write(this, out)) { |
| 3207 | return 0u; |
| 3208 | } |
| 3209 | relative_offset += oat_dex_file->SizeOf(); |
| 3210 | } |
| 3211 | |
| 3212 | return relative_offset; |
| 3213 | } |
| 3214 | |
| 3215 | size_t OatWriter::WriteCode(OutputStream* out, size_t file_offset, size_t relative_offset) { |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 3216 | if (compiler_driver_->GetCompilerOptions().IsBootImage()) { |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 3217 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 3218 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 3219 | #define DO_TRAMPOLINE(field) \ |
| 3220 | do { \ |
| 3221 | uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \ |
| 3222 | uint32_t alignment_padding = aligned_offset - relative_offset; \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 3223 | out->Seek(alignment_padding, kSeekCurrent); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 3224 | size_trampoline_alignment_ += alignment_padding; \ |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 3225 | if (!out->WriteFully((field)->data(), (field)->size())) { \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 3226 | PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 3227 | return false; \ |
| 3228 | } \ |
Chih-Hung Hsieh | fba3997 | 2016-05-11 11:26:48 -0700 | [diff] [blame] | 3229 | size_ ## field += (field)->size(); \ |
| 3230 | relative_offset += alignment_padding + (field)->size(); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 3231 | DCHECK_OFFSET(); \ |
| 3232 | } while (false) |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 3233 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 3234 | DO_TRAMPOLINE(jni_dlsym_lookup_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 3235 | DO_TRAMPOLINE(quick_generic_jni_trampoline_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 3236 | DO_TRAMPOLINE(quick_imt_conflict_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 3237 | DO_TRAMPOLINE(quick_resolution_trampoline_); |
| 3238 | DO_TRAMPOLINE(quick_to_interpreter_bridge_); |
| 3239 | #undef DO_TRAMPOLINE |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 3240 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 3241 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 3242 | } |
| 3243 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 3244 | size_t OatWriter::WriteCodeDexFiles(OutputStream* out, |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3245 | size_t file_offset, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 3246 | size_t relative_offset) { |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 3247 | if (!compiler_driver_->GetCompilerOptions().IsAnyCompilationEnabled()) { |
| 3248 | // As with InitOatCodeDexFiles, also skip the writer if |
| 3249 | // compilation was disabled. |
| 3250 | if (kOatWriterDebugOatCodeLayout) { |
| 3251 | LOG(INFO) << "WriteCodeDexFiles: OatWriter(" |
| 3252 | << this << "), " |
| 3253 | << "compilation is disabled"; |
| 3254 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 3255 | |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 3256 | return relative_offset; |
| 3257 | } |
| 3258 | ScopedObjectAccess soa(Thread::Current()); |
| 3259 | DCHECK(ordered_methods_ != nullptr); |
| 3260 | std::unique_ptr<OrderedMethodList> ordered_methods_ptr = |
| 3261 | std::move(ordered_methods_); |
| 3262 | WriteCodeMethodVisitor visitor(this, |
| 3263 | out, |
| 3264 | file_offset, |
| 3265 | relative_offset, |
| 3266 | std::move(*ordered_methods_ptr)); |
| 3267 | if (UNLIKELY(!visitor.Visit())) { |
| 3268 | return 0; |
| 3269 | } |
| 3270 | relative_offset = visitor.GetOffset(); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 3271 | |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 3272 | size_code_alignment_ += relative_patcher_->CodeAlignmentSize(); |
| 3273 | size_relative_call_thunks_ += relative_patcher_->RelativeCallThunksSize(); |
| 3274 | size_misc_thunks_ += relative_patcher_->MiscThunksSize(); |
| 3275 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 3276 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 3277 | } |
| 3278 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 3279 | bool OatWriter::RecordOatDataOffset(OutputStream* out) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 3280 | // Get the elf file offset of the oat file. |
| 3281 | const off_t raw_file_offset = out->Seek(0, kSeekCurrent); |
| 3282 | if (raw_file_offset == static_cast<off_t>(-1)) { |
| 3283 | LOG(ERROR) << "Failed to get file offset in " << out->GetLocation(); |
| 3284 | return false; |
| 3285 | } |
| 3286 | oat_data_offset_ = static_cast<size_t>(raw_file_offset); |
| 3287 | return true; |
| 3288 | } |
| 3289 | |
Nicolas Geoffray | 81f57d1 | 2016-12-20 13:17:09 +0000 | [diff] [blame] | 3290 | bool OatWriter::WriteDexFiles(OutputStream* out, File* file, bool update_input_vdex) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3291 | TimingLogger::ScopedTiming split("Write Dex files", timings_); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3292 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3293 | vdex_dex_files_offset_ = vdex_size_; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3294 | |
| 3295 | // Write dex files. |
| 3296 | for (OatDexFile& oat_dex_file : oat_dex_files_) { |
Nicolas Geoffray | 81f57d1 | 2016-12-20 13:17:09 +0000 | [diff] [blame] | 3297 | if (!WriteDexFile(out, file, &oat_dex_file, update_input_vdex)) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3298 | return false; |
| 3299 | } |
| 3300 | } |
| 3301 | |
Nicolas Geoffray | 81f57d1 | 2016-12-20 13:17:09 +0000 | [diff] [blame] | 3302 | CloseSources(); |
| 3303 | return true; |
| 3304 | } |
| 3305 | |
| 3306 | void OatWriter::CloseSources() { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3307 | for (OatDexFile& oat_dex_file : oat_dex_files_) { |
| 3308 | oat_dex_file.source_.Clear(); // Get rid of the reference, it's about to be invalidated. |
| 3309 | } |
| 3310 | zipped_dex_files_.clear(); |
| 3311 | zip_archives_.clear(); |
| 3312 | raw_dex_files_.clear(); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3313 | } |
| 3314 | |
Nicolas Geoffray | 81f57d1 | 2016-12-20 13:17:09 +0000 | [diff] [blame] | 3315 | bool OatWriter::WriteDexFile(OutputStream* out, |
| 3316 | File* file, |
| 3317 | OatDexFile* oat_dex_file, |
| 3318 | bool update_input_vdex) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3319 | if (!SeekToDexFile(out, file, oat_dex_file)) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3320 | return false; |
| 3321 | } |
Mathieu Chartier | 603ccab | 2017-10-20 14:34:28 -0700 | [diff] [blame] | 3322 | if (profile_compilation_info_ != nullptr || |
| 3323 | compact_dex_level_ != CompactDexLevel::kCompactDexLevelNone) { |
Nicolas Geoffray | 1cfea7a | 2017-05-24 14:44:38 +0100 | [diff] [blame] | 3324 | CHECK(!update_input_vdex) << "We should never update the input vdex when doing dexlayout"; |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 3325 | if (!LayoutAndWriteDexFile(out, oat_dex_file)) { |
| 3326 | return false; |
| 3327 | } |
| 3328 | } else if (oat_dex_file->source_.IsZipEntry()) { |
Nicolas Geoffray | 81f57d1 | 2016-12-20 13:17:09 +0000 | [diff] [blame] | 3329 | DCHECK(!update_input_vdex); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3330 | if (!WriteDexFile(out, file, oat_dex_file, oat_dex_file->source_.GetZipEntry())) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3331 | return false; |
| 3332 | } |
| 3333 | } else if (oat_dex_file->source_.IsRawFile()) { |
Nicolas Geoffray | 81f57d1 | 2016-12-20 13:17:09 +0000 | [diff] [blame] | 3334 | DCHECK(!update_input_vdex); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3335 | if (!WriteDexFile(out, file, oat_dex_file, oat_dex_file->source_.GetRawFile())) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3336 | return false; |
| 3337 | } |
| 3338 | } else { |
| 3339 | DCHECK(oat_dex_file->source_.IsRawData()); |
Nicolas Geoffray | 81f57d1 | 2016-12-20 13:17:09 +0000 | [diff] [blame] | 3340 | if (!WriteDexFile(out, oat_dex_file, oat_dex_file->source_.GetRawData(), update_input_vdex)) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3341 | return false; |
| 3342 | } |
| 3343 | } |
| 3344 | |
| 3345 | // Update current size and account for the written data. |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 3346 | DCHECK_EQ(vdex_size_, oat_dex_file->dex_file_offset_); |
| 3347 | vdex_size_ += oat_dex_file->dex_file_size_; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3348 | size_dex_file_ += oat_dex_file->dex_file_size_; |
| 3349 | return true; |
| 3350 | } |
| 3351 | |
| 3352 | bool OatWriter::SeekToDexFile(OutputStream* out, File* file, OatDexFile* oat_dex_file) { |
| 3353 | // Dex files are required to be 4 byte aligned. |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 3354 | size_t initial_offset = vdex_size_; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3355 | size_t start_offset = RoundUp(initial_offset, 4); |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 3356 | size_t file_offset = start_offset; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3357 | size_dex_file_alignment_ += start_offset - initial_offset; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3358 | |
| 3359 | // Seek to the start of the dex file and flush any pending operations in the stream. |
| 3360 | // Verify that, after flushing the stream, the file is at the same offset as the stream. |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3361 | off_t actual_offset = out->Seek(file_offset, kSeekSet); |
| 3362 | if (actual_offset != static_cast<off_t>(file_offset)) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3363 | PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3364 | << " Expected: " << file_offset |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3365 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 3366 | return false; |
| 3367 | } |
| 3368 | if (!out->Flush()) { |
| 3369 | PLOG(ERROR) << "Failed to flush before writing dex file." |
| 3370 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 3371 | return false; |
| 3372 | } |
| 3373 | actual_offset = lseek(file->Fd(), 0, SEEK_CUR); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3374 | if (actual_offset != static_cast<off_t>(file_offset)) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3375 | PLOG(ERROR) << "Stream/file position mismatch! Actual: " << actual_offset |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3376 | << " Expected: " << file_offset |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3377 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 3378 | return false; |
| 3379 | } |
| 3380 | |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 3381 | vdex_size_ = start_offset; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3382 | oat_dex_file->dex_file_offset_ = start_offset; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3383 | return true; |
| 3384 | } |
| 3385 | |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 3386 | bool OatWriter::LayoutAndWriteDexFile(OutputStream* out, OatDexFile* oat_dex_file) { |
| 3387 | TimingLogger::ScopedTiming split("Dex Layout", timings_); |
| 3388 | std::string error_msg; |
| 3389 | std::string location(oat_dex_file->GetLocation()); |
| 3390 | std::unique_ptr<const DexFile> dex_file; |
| 3391 | if (oat_dex_file->source_.IsZipEntry()) { |
| 3392 | ZipEntry* zip_entry = oat_dex_file->source_.GetZipEntry(); |
| 3393 | std::unique_ptr<MemMap> mem_map( |
| 3394 | zip_entry->ExtractToMemMap(location.c_str(), "classes.dex", &error_msg)); |
Jeff Hao | 41b2f53 | 2017-03-02 16:36:31 -0800 | [diff] [blame] | 3395 | if (mem_map == nullptr) { |
| 3396 | LOG(ERROR) << "Failed to extract dex file to mem map for layout: " << error_msg; |
| 3397 | return false; |
| 3398 | } |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 3399 | dex_file = DexFileLoader::Open(location, |
| 3400 | zip_entry->GetCrc32(), |
| 3401 | std::move(mem_map), |
| 3402 | /* verify */ true, |
| 3403 | /* verify_checksum */ true, |
| 3404 | &error_msg); |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 3405 | } else if (oat_dex_file->source_.IsRawFile()) { |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 3406 | File* raw_file = oat_dex_file->source_.GetRawFile(); |
Jeff Hao | 68c48f0 | 2017-08-24 11:36:24 -0700 | [diff] [blame] | 3407 | int dup_fd = dup(raw_file->Fd()); |
| 3408 | if (dup_fd < 0) { |
| 3409 | PLOG(ERROR) << "Failed to dup dex file descriptor (" << raw_file->Fd() << ") at " << location; |
| 3410 | return false; |
| 3411 | } |
Nicolas Geoffray | 095c6c9 | 2017-10-19 13:59:55 +0100 | [diff] [blame] | 3412 | dex_file = DexFileLoader::OpenDex( |
| 3413 | dup_fd, location, /* verify */ true, /* verify_checksum */ true, &error_msg); |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 3414 | } else { |
| 3415 | // The source data is a vdex file. |
| 3416 | CHECK(oat_dex_file->source_.IsRawData()) |
| 3417 | << static_cast<size_t>(oat_dex_file->source_.GetType()); |
| 3418 | const uint8_t* raw_dex_file = oat_dex_file->source_.GetRawData(); |
| 3419 | // Note: The raw data has already been checked to contain the header |
| 3420 | // and all the data that the header specifies as the file size. |
| 3421 | DCHECK(raw_dex_file != nullptr); |
| 3422 | DCHECK(ValidateDexFileHeader(raw_dex_file, oat_dex_file->GetLocation())); |
| 3423 | const UnalignedDexFileHeader* header = AsUnalignedDexFileHeader(raw_dex_file); |
| 3424 | // Since the source may have had its layout changed, or may be quickened, don't verify it. |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 3425 | dex_file = DexFileLoader::Open(raw_dex_file, |
| 3426 | header->file_size_, |
| 3427 | location, |
| 3428 | oat_dex_file->dex_file_location_checksum_, |
| 3429 | nullptr, |
| 3430 | /* verify */ false, |
| 3431 | /* verify_checksum */ false, |
| 3432 | &error_msg); |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 3433 | } |
Jeff Hao | de19754 | 2017-02-03 10:48:13 -0800 | [diff] [blame] | 3434 | if (dex_file == nullptr) { |
Jeff Hao | d9df780 | 2017-02-06 16:41:16 -0800 | [diff] [blame] | 3435 | LOG(ERROR) << "Failed to open dex file for layout: " << error_msg; |
Jeff Hao | de19754 | 2017-02-03 10:48:13 -0800 | [diff] [blame] | 3436 | return false; |
| 3437 | } |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 3438 | Options options; |
| 3439 | options.output_to_memmap_ = true; |
Mathieu Chartier | 603ccab | 2017-10-20 14:34:28 -0700 | [diff] [blame] | 3440 | options.compact_dex_level_ = compact_dex_level_; |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 3441 | DexLayout dex_layout(options, profile_compilation_info_, nullptr); |
| 3442 | dex_layout.ProcessDexFile(location.c_str(), dex_file.get(), 0); |
| 3443 | std::unique_ptr<MemMap> mem_map(dex_layout.GetAndReleaseMemMap()); |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 3444 | oat_dex_file->dex_sections_layout_ = dex_layout.GetSections(); |
| 3445 | // Dex layout can affect the size of the dex file, so we update here what we have set |
| 3446 | // when adding the dex file as a source. |
| 3447 | const UnalignedDexFileHeader* header = AsUnalignedDexFileHeader(mem_map->Begin()); |
| 3448 | oat_dex_file->dex_file_size_ = header->file_size_; |
Nicolas Geoffray | 81f57d1 | 2016-12-20 13:17:09 +0000 | [diff] [blame] | 3449 | if (!WriteDexFile(out, oat_dex_file, mem_map->Begin(), /* update_input_vdex */ false)) { |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 3450 | return false; |
| 3451 | } |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 3452 | CHECK_EQ(oat_dex_file->dex_file_location_checksum_, dex_file->GetLocationChecksum()); |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 3453 | return true; |
| 3454 | } |
| 3455 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3456 | bool OatWriter::WriteDexFile(OutputStream* out, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3457 | File* file, |
| 3458 | OatDexFile* oat_dex_file, |
| 3459 | ZipEntry* dex_file) { |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 3460 | size_t start_offset = vdex_size_; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3461 | DCHECK_EQ(static_cast<off_t>(start_offset), out->Seek(0, kSeekCurrent)); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3462 | |
| 3463 | // Extract the dex file and get the extracted size. |
| 3464 | std::string error_msg; |
| 3465 | if (!dex_file->ExtractToFile(*file, &error_msg)) { |
| 3466 | LOG(ERROR) << "Failed to extract dex file from ZIP entry: " << error_msg |
| 3467 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 3468 | return false; |
| 3469 | } |
| 3470 | if (file->Flush() != 0) { |
| 3471 | PLOG(ERROR) << "Failed to flush dex file from ZIP entry." |
| 3472 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 3473 | return false; |
| 3474 | } |
| 3475 | off_t extracted_end = lseek(file->Fd(), 0, SEEK_CUR); |
| 3476 | if (extracted_end == static_cast<off_t>(-1)) { |
| 3477 | PLOG(ERROR) << "Failed get end offset after writing dex file from ZIP entry." |
| 3478 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 3479 | return false; |
| 3480 | } |
| 3481 | if (extracted_end < static_cast<off_t>(start_offset)) { |
| 3482 | LOG(ERROR) << "Dex file end position is before start position! End: " << extracted_end |
| 3483 | << " Start: " << start_offset |
| 3484 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 3485 | return false; |
| 3486 | } |
| 3487 | uint64_t extracted_size = static_cast<uint64_t>(extracted_end - start_offset); |
| 3488 | if (extracted_size < sizeof(DexFile::Header)) { |
| 3489 | LOG(ERROR) << "Extracted dex file is shorter than dex file header. size: " |
| 3490 | << extracted_size << " File: " << oat_dex_file->GetLocation(); |
| 3491 | return false; |
| 3492 | } |
| 3493 | |
| 3494 | // Read the dex file header and extract required data to OatDexFile. |
| 3495 | off_t actual_offset = lseek(file->Fd(), start_offset, SEEK_SET); |
| 3496 | if (actual_offset != static_cast<off_t>(start_offset)) { |
| 3497 | PLOG(ERROR) << "Failed to seek back to dex file header. Actual: " << actual_offset |
| 3498 | << " Expected: " << start_offset |
| 3499 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 3500 | return false; |
| 3501 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3502 | if (extracted_size < oat_dex_file->dex_file_size_) { |
| 3503 | LOG(ERROR) << "Extracted truncated dex file. Extracted size: " << extracted_size |
| 3504 | << " file size from header: " << oat_dex_file->dex_file_size_ |
| 3505 | << " File: " << oat_dex_file->GetLocation(); |
| 3506 | return false; |
| 3507 | } |
| 3508 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3509 | // Seek both file and stream to the end offset. |
| 3510 | size_t end_offset = start_offset + oat_dex_file->dex_file_size_; |
| 3511 | actual_offset = lseek(file->Fd(), end_offset, SEEK_SET); |
| 3512 | if (actual_offset != static_cast<off_t>(end_offset)) { |
| 3513 | PLOG(ERROR) << "Failed to seek to end of dex file. Actual: " << actual_offset |
| 3514 | << " Expected: " << end_offset |
| 3515 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 3516 | return false; |
| 3517 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3518 | actual_offset = out->Seek(end_offset, kSeekSet); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3519 | if (actual_offset != static_cast<off_t>(end_offset)) { |
| 3520 | PLOG(ERROR) << "Failed to seek stream to end of dex file. Actual: " << actual_offset |
| 3521 | << " Expected: " << end_offset << " File: " << oat_dex_file->GetLocation(); |
| 3522 | return false; |
| 3523 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3524 | if (!out->Flush()) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3525 | PLOG(ERROR) << "Failed to flush stream after seeking over dex file." |
| 3526 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 3527 | return false; |
| 3528 | } |
| 3529 | |
| 3530 | // If we extracted more than the size specified in the header, truncate the file. |
| 3531 | if (extracted_size > oat_dex_file->dex_file_size_) { |
| 3532 | if (file->SetLength(end_offset) != 0) { |
| 3533 | PLOG(ERROR) << "Failed to truncate excessive dex file length." |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3534 | << " File: " << oat_dex_file->GetLocation() |
| 3535 | << " Output: " << file->GetPath(); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3536 | return false; |
| 3537 | } |
| 3538 | } |
| 3539 | |
| 3540 | return true; |
| 3541 | } |
| 3542 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3543 | bool OatWriter::WriteDexFile(OutputStream* out, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3544 | File* file, |
| 3545 | OatDexFile* oat_dex_file, |
| 3546 | File* dex_file) { |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 3547 | size_t start_offset = vdex_size_; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3548 | DCHECK_EQ(static_cast<off_t>(start_offset), out->Seek(0, kSeekCurrent)); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3549 | |
| 3550 | off_t input_offset = lseek(dex_file->Fd(), 0, SEEK_SET); |
| 3551 | if (input_offset != static_cast<off_t>(0)) { |
| 3552 | PLOG(ERROR) << "Failed to seek to dex file header. Actual: " << input_offset |
| 3553 | << " Expected: 0" |
| 3554 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 3555 | return false; |
| 3556 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3557 | |
| 3558 | // Copy the input dex file using sendfile(). |
| 3559 | if (!file->Copy(dex_file, 0, oat_dex_file->dex_file_size_)) { |
| 3560 | PLOG(ERROR) << "Failed to copy dex file to oat file." |
| 3561 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 3562 | return false; |
| 3563 | } |
| 3564 | if (file->Flush() != 0) { |
| 3565 | PLOG(ERROR) << "Failed to flush dex file." |
| 3566 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 3567 | return false; |
| 3568 | } |
| 3569 | |
| 3570 | // Check file position and seek the stream to the end offset. |
| 3571 | size_t end_offset = start_offset + oat_dex_file->dex_file_size_; |
| 3572 | off_t actual_offset = lseek(file->Fd(), 0, SEEK_CUR); |
| 3573 | if (actual_offset != static_cast<off_t>(end_offset)) { |
| 3574 | PLOG(ERROR) << "Unexpected file position after copying dex file. Actual: " << actual_offset |
| 3575 | << " Expected: " << end_offset |
| 3576 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 3577 | return false; |
| 3578 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3579 | actual_offset = out->Seek(end_offset, kSeekSet); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3580 | if (actual_offset != static_cast<off_t>(end_offset)) { |
| 3581 | PLOG(ERROR) << "Failed to seek stream to end of dex file. Actual: " << actual_offset |
| 3582 | << " Expected: " << end_offset << " File: " << oat_dex_file->GetLocation(); |
| 3583 | return false; |
| 3584 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3585 | if (!out->Flush()) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3586 | PLOG(ERROR) << "Failed to flush stream after seeking over dex file." |
| 3587 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 3588 | return false; |
| 3589 | } |
| 3590 | |
| 3591 | return true; |
| 3592 | } |
| 3593 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3594 | bool OatWriter::WriteDexFile(OutputStream* out, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3595 | OatDexFile* oat_dex_file, |
Nicolas Geoffray | 81f57d1 | 2016-12-20 13:17:09 +0000 | [diff] [blame] | 3596 | const uint8_t* dex_file, |
| 3597 | bool update_input_vdex) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3598 | // Note: The raw data has already been checked to contain the header |
| 3599 | // and all the data that the header specifies as the file size. |
| 3600 | DCHECK(dex_file != nullptr); |
| 3601 | DCHECK(ValidateDexFileHeader(dex_file, oat_dex_file->GetLocation())); |
| 3602 | const UnalignedDexFileHeader* header = AsUnalignedDexFileHeader(dex_file); |
| 3603 | |
Nicolas Geoffray | 81f57d1 | 2016-12-20 13:17:09 +0000 | [diff] [blame] | 3604 | if (update_input_vdex) { |
| 3605 | // The vdex already contains the dex code, no need to write it again. |
| 3606 | } else { |
| 3607 | if (!out->WriteFully(dex_file, header->file_size_)) { |
| 3608 | PLOG(ERROR) << "Failed to write dex file " << oat_dex_file->GetLocation() |
| 3609 | << " to " << out->GetLocation(); |
| 3610 | return false; |
| 3611 | } |
| 3612 | if (!out->Flush()) { |
| 3613 | PLOG(ERROR) << "Failed to flush stream after writing dex file." |
| 3614 | << " File: " << oat_dex_file->GetLocation(); |
| 3615 | return false; |
| 3616 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3617 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3618 | return true; |
| 3619 | } |
| 3620 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3621 | bool OatWriter::OpenDexFiles( |
| 3622 | File* file, |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 3623 | bool verify, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3624 | /*out*/ std::unique_ptr<MemMap>* opened_dex_files_map, |
| 3625 | /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files) { |
| 3626 | TimingLogger::ScopedTiming split("OpenDexFiles", timings_); |
| 3627 | |
| 3628 | if (oat_dex_files_.empty()) { |
| 3629 | // Nothing to do. |
| 3630 | return true; |
| 3631 | } |
| 3632 | |
| 3633 | size_t map_offset = oat_dex_files_[0].dex_file_offset_; |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 3634 | size_t length = vdex_size_ - map_offset; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3635 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3636 | std::string error_msg; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3637 | std::unique_ptr<MemMap> dex_files_map(MemMap::MapFile( |
| 3638 | length, |
| 3639 | PROT_READ | PROT_WRITE, |
| 3640 | MAP_SHARED, |
| 3641 | file->Fd(), |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 3642 | map_offset, |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3643 | /* low_4gb */ false, |
| 3644 | file->GetPath().c_str(), |
| 3645 | &error_msg)); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3646 | if (dex_files_map == nullptr) { |
| 3647 | LOG(ERROR) << "Failed to mmap() dex files from oat file. File: " << file->GetPath() |
| 3648 | << " error: " << error_msg; |
| 3649 | return false; |
| 3650 | } |
| 3651 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 3652 | for (OatDexFile& oat_dex_file : oat_dex_files_) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3653 | const uint8_t* raw_dex_file = |
| 3654 | dex_files_map->Begin() + oat_dex_file.dex_file_offset_ - map_offset; |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 3655 | |
| 3656 | if (kIsDebugBuild) { |
| 3657 | // Sanity check our input files. |
| 3658 | // Note that ValidateDexFileHeader() logs error messages. |
| 3659 | CHECK(ValidateDexFileHeader(raw_dex_file, oat_dex_file.GetLocation())) |
| 3660 | << "Failed to verify written dex file header!" |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3661 | << " Output: " << file->GetPath() << " ~ " << std::hex << map_offset |
| 3662 | << " ~ " << static_cast<const void*>(raw_dex_file); |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 3663 | |
| 3664 | const UnalignedDexFileHeader* header = AsUnalignedDexFileHeader(raw_dex_file); |
| 3665 | CHECK_EQ(header->file_size_, oat_dex_file.dex_file_size_) |
| 3666 | << "File size mismatch in written dex file header! Expected: " |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3667 | << oat_dex_file.dex_file_size_ << " Actual: " << header->file_size_ |
| 3668 | << " Output: " << file->GetPath(); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3669 | } |
| 3670 | |
| 3671 | // Now, open the dex file. |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 3672 | dex_files.emplace_back(DexFileLoader::Open(raw_dex_file, |
| 3673 | oat_dex_file.dex_file_size_, |
| 3674 | oat_dex_file.GetLocation(), |
| 3675 | oat_dex_file.dex_file_location_checksum_, |
| 3676 | /* oat_dex_file */ nullptr, |
| 3677 | verify, |
| 3678 | verify, |
| 3679 | &error_msg)); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3680 | if (dex_files.back() == nullptr) { |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 3681 | LOG(ERROR) << "Failed to open dex file from oat file. File: " << oat_dex_file.GetLocation() |
| 3682 | << " Error: " << error_msg; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3683 | return false; |
| 3684 | } |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 3685 | |
| 3686 | // Set the class_offsets size now that we have easy access to the DexFile and |
| 3687 | // it has been verified in DexFileLoader::Open. |
| 3688 | oat_dex_file.class_offsets_.resize(dex_files.back()->GetHeader().class_defs_size_); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3689 | } |
| 3690 | |
| 3691 | *opened_dex_files_map = std::move(dex_files_map); |
| 3692 | *opened_dex_files = std::move(dex_files); |
| 3693 | return true; |
| 3694 | } |
| 3695 | |
| 3696 | bool OatWriter::WriteTypeLookupTables( |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3697 | OutputStream* oat_rodata, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3698 | const std::vector<std::unique_ptr<const DexFile>>& opened_dex_files) { |
| 3699 | TimingLogger::ScopedTiming split("WriteTypeLookupTables", timings_); |
| 3700 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3701 | uint32_t expected_offset = oat_data_offset_ + oat_size_; |
| 3702 | off_t actual_offset = oat_rodata->Seek(expected_offset, kSeekSet); |
| 3703 | if (static_cast<uint32_t>(actual_offset) != expected_offset) { |
| 3704 | PLOG(ERROR) << "Failed to seek to TypeLookupTable section. Actual: " << actual_offset |
| 3705 | << " Expected: " << expected_offset << " File: " << oat_rodata->GetLocation(); |
| 3706 | return false; |
| 3707 | } |
| 3708 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3709 | DCHECK_EQ(opened_dex_files.size(), oat_dex_files_.size()); |
| 3710 | for (size_t i = 0, size = opened_dex_files.size(); i != size; ++i) { |
| 3711 | OatDexFile* oat_dex_file = &oat_dex_files_[i]; |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 3712 | DCHECK_EQ(oat_dex_file->lookup_table_offset_, 0u); |
| 3713 | |
| 3714 | if (oat_dex_file->create_type_lookup_table_ != CreateTypeLookupTable::kCreate || |
| 3715 | oat_dex_file->class_offsets_.empty()) { |
| 3716 | continue; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3717 | } |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 3718 | |
| 3719 | size_t table_size = TypeLookupTable::RawDataLength(oat_dex_file->class_offsets_.size()); |
| 3720 | if (table_size == 0u) { |
| 3721 | continue; |
| 3722 | } |
| 3723 | |
| 3724 | // Create the lookup table. When `nullptr` is given as the storage buffer, |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 3725 | // TypeLookupTable allocates its own and OatDexFile takes ownership. |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 3726 | const DexFile& dex_file = *opened_dex_files[i]; |
| 3727 | { |
| 3728 | std::unique_ptr<TypeLookupTable> type_lookup_table = |
| 3729 | TypeLookupTable::Create(dex_file, /* storage */ nullptr); |
| 3730 | type_lookup_table_oat_dex_files_.push_back( |
| 3731 | std::make_unique<art::OatDexFile>(std::move(type_lookup_table))); |
| 3732 | dex_file.SetOatDexFile(type_lookup_table_oat_dex_files_.back().get()); |
| 3733 | } |
| 3734 | TypeLookupTable* const table = type_lookup_table_oat_dex_files_.back()->GetTypeLookupTable(); |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 3735 | |
| 3736 | // Type tables are required to be 4 byte aligned. |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3737 | size_t initial_offset = oat_size_; |
| 3738 | size_t rodata_offset = RoundUp(initial_offset, 4); |
| 3739 | size_t padding_size = rodata_offset - initial_offset; |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 3740 | |
| 3741 | if (padding_size != 0u) { |
| 3742 | std::vector<uint8_t> buffer(padding_size, 0u); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3743 | if (!oat_rodata->WriteFully(buffer.data(), padding_size)) { |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 3744 | PLOG(ERROR) << "Failed to write lookup table alignment padding." |
| 3745 | << " File: " << oat_dex_file->GetLocation() |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3746 | << " Output: " << oat_rodata->GetLocation(); |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 3747 | return false; |
| 3748 | } |
| 3749 | } |
| 3750 | |
| 3751 | DCHECK_EQ(oat_data_offset_ + rodata_offset, |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3752 | static_cast<size_t>(oat_rodata->Seek(0u, kSeekCurrent))); |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 3753 | DCHECK_EQ(table_size, table->RawDataLength()); |
| 3754 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3755 | if (!oat_rodata->WriteFully(table->RawData(), table_size)) { |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 3756 | PLOG(ERROR) << "Failed to write lookup table." |
| 3757 | << " File: " << oat_dex_file->GetLocation() |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3758 | << " Output: " << oat_rodata->GetLocation(); |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 3759 | return false; |
| 3760 | } |
| 3761 | |
| 3762 | oat_dex_file->lookup_table_offset_ = rodata_offset; |
| 3763 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3764 | oat_size_ += padding_size + table_size; |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 3765 | size_oat_lookup_table_ += table_size; |
| 3766 | size_oat_lookup_table_alignment_ += padding_size; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3767 | } |
| 3768 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3769 | if (!oat_rodata->Flush()) { |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 3770 | PLOG(ERROR) << "Failed to flush stream after writing type lookup tables." |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3771 | << " File: " << oat_rodata->GetLocation(); |
| 3772 | return false; |
| 3773 | } |
| 3774 | |
| 3775 | return true; |
| 3776 | } |
| 3777 | |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 3778 | bool OatWriter::WriteDexLayoutSections( |
| 3779 | OutputStream* oat_rodata, |
| 3780 | const std::vector<std::unique_ptr<const DexFile>>& opened_dex_files) { |
| 3781 | TimingLogger::ScopedTiming split(__FUNCTION__, timings_); |
| 3782 | |
| 3783 | if (!kWriteDexLayoutInfo) { |
| 3784 | return true;; |
| 3785 | } |
| 3786 | |
| 3787 | uint32_t expected_offset = oat_data_offset_ + oat_size_; |
| 3788 | off_t actual_offset = oat_rodata->Seek(expected_offset, kSeekSet); |
| 3789 | if (static_cast<uint32_t>(actual_offset) != expected_offset) { |
| 3790 | PLOG(ERROR) << "Failed to seek to dex layout section offset section. Actual: " << actual_offset |
| 3791 | << " Expected: " << expected_offset << " File: " << oat_rodata->GetLocation(); |
| 3792 | return false; |
| 3793 | } |
| 3794 | |
| 3795 | DCHECK_EQ(opened_dex_files.size(), oat_dex_files_.size()); |
| 3796 | size_t rodata_offset = oat_size_; |
| 3797 | for (size_t i = 0, size = opened_dex_files.size(); i != size; ++i) { |
| 3798 | OatDexFile* oat_dex_file = &oat_dex_files_[i]; |
| 3799 | DCHECK_EQ(oat_dex_file->dex_sections_layout_offset_, 0u); |
| 3800 | |
| 3801 | // Write dex layout section alignment bytes. |
| 3802 | const size_t padding_size = |
| 3803 | RoundUp(rodata_offset, alignof(DexLayoutSections)) - rodata_offset; |
| 3804 | if (padding_size != 0u) { |
| 3805 | std::vector<uint8_t> buffer(padding_size, 0u); |
| 3806 | if (!oat_rodata->WriteFully(buffer.data(), padding_size)) { |
| 3807 | PLOG(ERROR) << "Failed to write lookup table alignment padding." |
| 3808 | << " File: " << oat_dex_file->GetLocation() |
| 3809 | << " Output: " << oat_rodata->GetLocation(); |
| 3810 | return false; |
| 3811 | } |
| 3812 | size_oat_dex_file_dex_layout_sections_alignment_ += padding_size; |
| 3813 | rodata_offset += padding_size; |
| 3814 | } |
| 3815 | |
| 3816 | DCHECK_ALIGNED(rodata_offset, alignof(DexLayoutSections)); |
| 3817 | DCHECK_EQ(oat_data_offset_ + rodata_offset, |
| 3818 | static_cast<size_t>(oat_rodata->Seek(0u, kSeekCurrent))); |
| 3819 | DCHECK(oat_dex_file != nullptr); |
| 3820 | if (!oat_rodata->WriteFully(&oat_dex_file->dex_sections_layout_, |
| 3821 | sizeof(oat_dex_file->dex_sections_layout_))) { |
| 3822 | PLOG(ERROR) << "Failed to write dex layout sections." |
| 3823 | << " File: " << oat_dex_file->GetLocation() |
| 3824 | << " Output: " << oat_rodata->GetLocation(); |
| 3825 | return false; |
| 3826 | } |
| 3827 | oat_dex_file->dex_sections_layout_offset_ = rodata_offset; |
| 3828 | size_oat_dex_file_dex_layout_sections_ += sizeof(oat_dex_file->dex_sections_layout_); |
| 3829 | rodata_offset += sizeof(oat_dex_file->dex_sections_layout_); |
| 3830 | } |
| 3831 | oat_size_ = rodata_offset; |
| 3832 | |
| 3833 | if (!oat_rodata->Flush()) { |
| 3834 | PLOG(ERROR) << "Failed to flush stream after writing type dex layout sections." |
| 3835 | << " File: " << oat_rodata->GetLocation(); |
| 3836 | return false; |
| 3837 | } |
| 3838 | |
| 3839 | return true; |
| 3840 | } |
| 3841 | |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 3842 | bool OatWriter::WriteChecksumsAndVdexHeader(OutputStream* vdex_out) { |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 3843 | // Write checksums |
| 3844 | off_t actual_offset = vdex_out->Seek(sizeof(VdexFile::Header), kSeekSet); |
| 3845 | if (actual_offset != sizeof(VdexFile::Header)) { |
| 3846 | PLOG(ERROR) << "Failed to seek to the checksum location of vdex file. Actual: " << actual_offset |
| 3847 | << " File: " << vdex_out->GetLocation(); |
| 3848 | return false; |
| 3849 | } |
| 3850 | |
| 3851 | for (size_t i = 0, size = oat_dex_files_.size(); i != size; ++i) { |
| 3852 | OatDexFile* oat_dex_file = &oat_dex_files_[i]; |
| 3853 | if (!vdex_out->WriteFully( |
| 3854 | &oat_dex_file->dex_file_location_checksum_, sizeof(VdexFile::VdexChecksum))) { |
| 3855 | PLOG(ERROR) << "Failed to write dex file location checksum. File: " |
| 3856 | << vdex_out->GetLocation(); |
| 3857 | return false; |
| 3858 | } |
| 3859 | size_vdex_checksums_ += sizeof(VdexFile::VdexChecksum); |
| 3860 | } |
| 3861 | |
| 3862 | // Write header. |
| 3863 | actual_offset = vdex_out->Seek(0, kSeekSet); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3864 | if (actual_offset != 0) { |
| 3865 | PLOG(ERROR) << "Failed to seek to the beginning of vdex file. Actual: " << actual_offset |
| 3866 | << " File: " << vdex_out->GetLocation(); |
| 3867 | return false; |
| 3868 | } |
| 3869 | |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 3870 | DCHECK_NE(vdex_dex_files_offset_, 0u); |
| 3871 | DCHECK_NE(vdex_verifier_deps_offset_, 0u); |
David Brazdil | 93592f5 | 2017-12-08 10:53:27 +0000 | [diff] [blame^] | 3872 | DCHECK_NE(vdex_quickening_info_offset_, 0u); |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 3873 | |
| 3874 | size_t dex_section_size = vdex_verifier_deps_offset_ - vdex_dex_files_offset_; |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 3875 | size_t verifier_deps_section_size = vdex_quickening_info_offset_ - vdex_verifier_deps_offset_; |
| 3876 | size_t quickening_info_section_size = vdex_size_ - vdex_quickening_info_offset_; |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 3877 | |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 3878 | VdexFile::Header vdex_header(oat_dex_files_.size(), |
| 3879 | dex_section_size, |
| 3880 | verifier_deps_section_size, |
| 3881 | quickening_info_section_size); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3882 | if (!vdex_out->WriteFully(&vdex_header, sizeof(VdexFile::Header))) { |
| 3883 | PLOG(ERROR) << "Failed to write vdex header. File: " << vdex_out->GetLocation(); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3884 | return false; |
| 3885 | } |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 3886 | size_vdex_header_ = sizeof(VdexFile::Header); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3887 | |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 3888 | if (!vdex_out->Flush()) { |
| 3889 | PLOG(ERROR) << "Failed to flush stream after writing to vdex file." |
| 3890 | << " File: " << vdex_out->GetLocation(); |
| 3891 | return false; |
| 3892 | } |
| 3893 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3894 | return true; |
| 3895 | } |
| 3896 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 3897 | bool OatWriter::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) { |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3898 | return WriteUpTo16BytesAlignment(out, aligned_code_delta, &size_code_alignment_); |
| 3899 | } |
| 3900 | |
| 3901 | bool OatWriter::WriteUpTo16BytesAlignment(OutputStream* out, uint32_t size, uint32_t* stat) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 3902 | static const uint8_t kPadding[] = { |
| 3903 | 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u |
| 3904 | }; |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3905 | DCHECK_LE(size, sizeof(kPadding)); |
| 3906 | if (UNLIKELY(!out->WriteFully(kPadding, size))) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 3907 | return false; |
| 3908 | } |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3909 | *stat += size; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 3910 | return true; |
| 3911 | } |
| 3912 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 3913 | void OatWriter::SetMultiOatRelativePatcherAdjustment() { |
| 3914 | DCHECK(dex_files_ != nullptr); |
| 3915 | DCHECK(relative_patcher_ != nullptr); |
| 3916 | DCHECK_NE(oat_data_offset_, 0u); |
| 3917 | if (image_writer_ != nullptr && !dex_files_->empty()) { |
| 3918 | // The oat data begin may not be initialized yet but the oat file offset is ready. |
| 3919 | size_t oat_index = image_writer_->GetOatIndexForDexFile(dex_files_->front()); |
| 3920 | size_t elf_file_offset = image_writer_->GetOatFileOffset(oat_index); |
| 3921 | relative_patcher_->StartOatFile(elf_file_offset + oat_data_offset_); |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 3922 | } |
| 3923 | } |
| 3924 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3925 | OatWriter::OatDexFile::OatDexFile(const char* dex_file_location, |
| 3926 | DexFileSource source, |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 3927 | CreateTypeLookupTable create_type_lookup_table, |
| 3928 | uint32_t dex_file_location_checksum, |
| 3929 | size_t dex_file_size) |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3930 | : source_(source), |
| 3931 | create_type_lookup_table_(create_type_lookup_table), |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 3932 | dex_file_size_(dex_file_size), |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3933 | offset_(0), |
| 3934 | dex_file_location_size_(strlen(dex_file_location)), |
| 3935 | dex_file_location_data_(dex_file_location), |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 3936 | dex_file_location_checksum_(dex_file_location_checksum), |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3937 | dex_file_offset_(0u), |
Nicolas Geoffray | 715d672 | 2017-11-20 22:28:46 +0000 | [diff] [blame] | 3938 | lookup_table_offset_(0u), |
Nicolas Geoffray | 6f73f4a | 2017-11-23 12:51:47 +0000 | [diff] [blame] | 3939 | class_offsets_offset_(0u), |
Nicolas Geoffray | 97a042e | 2017-11-16 10:49:59 +0000 | [diff] [blame] | 3940 | method_bss_mapping_offset_(0u), |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 3941 | type_bss_mapping_offset_(0u), |
| 3942 | string_bss_mapping_offset_(0u), |
Nicolas Geoffray | 715d672 | 2017-11-20 22:28:46 +0000 | [diff] [blame] | 3943 | dex_sections_layout_offset_(0u), |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3944 | class_offsets_() { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 3945 | } |
| 3946 | |
| 3947 | size_t OatWriter::OatDexFile::SizeOf() const { |
| 3948 | return sizeof(dex_file_location_size_) |
| 3949 | + dex_file_location_size_ |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 3950 | + sizeof(dex_file_location_checksum_) |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 3951 | + sizeof(dex_file_offset_) |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3952 | + sizeof(class_offsets_offset_) |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3953 | + sizeof(lookup_table_offset_) |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 3954 | + sizeof(method_bss_mapping_offset_) |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 3955 | + sizeof(type_bss_mapping_offset_) |
| 3956 | + sizeof(string_bss_mapping_offset_) |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 3957 | + sizeof(dex_sections_layout_offset_); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
| 3960 | bool OatWriter::OatDexFile::Write(OatWriter* oat_writer, OutputStream* out) const { |
| 3961 | const size_t file_offset = oat_writer->oat_data_offset_; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 3962 | DCHECK_OFFSET_(); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3963 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 3964 | if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 3965 | PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 3966 | return false; |
| 3967 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 3968 | oat_writer->size_oat_dex_file_location_size_ += sizeof(dex_file_location_size_); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3969 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 3970 | if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 3971 | PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 3972 | return false; |
| 3973 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 3974 | oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3975 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 3976 | if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 3977 | PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 3978 | return false; |
| 3979 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 3980 | oat_writer->size_oat_dex_file_location_checksum_ += sizeof(dex_file_location_checksum_); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3981 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 3982 | if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 3983 | PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 3984 | return false; |
| 3985 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 3986 | oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3987 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 3988 | if (!out->WriteFully(&class_offsets_offset_, sizeof(class_offsets_offset_))) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3989 | PLOG(ERROR) << "Failed to write class offsets offset to " << out->GetLocation(); |
| 3990 | return false; |
| 3991 | } |
| 3992 | oat_writer->size_oat_dex_file_class_offsets_offset_ += sizeof(class_offsets_offset_); |
| 3993 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 3994 | if (!out->WriteFully(&lookup_table_offset_, sizeof(lookup_table_offset_))) { |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 3995 | PLOG(ERROR) << "Failed to write lookup table offset to " << out->GetLocation(); |
| 3996 | return false; |
| 3997 | } |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 3998 | oat_writer->size_oat_dex_file_lookup_table_offset_ += sizeof(lookup_table_offset_); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3999 | |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 4000 | if (!out->WriteFully(&dex_sections_layout_offset_, sizeof(dex_sections_layout_offset_))) { |
| 4001 | PLOG(ERROR) << "Failed to write dex section layout info to " << out->GetLocation(); |
| 4002 | return false; |
| 4003 | } |
| 4004 | oat_writer->size_oat_dex_file_dex_layout_sections_offset_ += sizeof(dex_sections_layout_offset_); |
| 4005 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 4006 | if (!out->WriteFully(&method_bss_mapping_offset_, sizeof(method_bss_mapping_offset_))) { |
| 4007 | PLOG(ERROR) << "Failed to write method bss mapping offset to " << out->GetLocation(); |
| 4008 | return false; |
| 4009 | } |
| 4010 | oat_writer->size_oat_dex_file_method_bss_mapping_offset_ += sizeof(method_bss_mapping_offset_); |
| 4011 | |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 4012 | if (!out->WriteFully(&type_bss_mapping_offset_, sizeof(type_bss_mapping_offset_))) { |
| 4013 | PLOG(ERROR) << "Failed to write type bss mapping offset to " << out->GetLocation(); |
| 4014 | return false; |
| 4015 | } |
| 4016 | oat_writer->size_oat_dex_file_type_bss_mapping_offset_ += sizeof(type_bss_mapping_offset_); |
| 4017 | |
| 4018 | if (!out->WriteFully(&string_bss_mapping_offset_, sizeof(string_bss_mapping_offset_))) { |
| 4019 | PLOG(ERROR) << "Failed to write string bss mapping offset to " << out->GetLocation(); |
| 4020 | return false; |
| 4021 | } |
| 4022 | oat_writer->size_oat_dex_file_string_bss_mapping_offset_ += sizeof(string_bss_mapping_offset_); |
| 4023 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 4024 | return true; |
| 4025 | } |
| 4026 | |
| 4027 | bool OatWriter::OatDexFile::WriteClassOffsets(OatWriter* oat_writer, OutputStream* out) { |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 4028 | if (!out->WriteFully(class_offsets_.data(), GetClassOffsetsRawSize())) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 4029 | PLOG(ERROR) << "Failed to write oat class offsets for " << GetLocation() |
| 4030 | << " to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 4031 | return false; |
| 4032 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 4033 | oat_writer->size_oat_class_offsets_ += GetClassOffsetsRawSize(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 4034 | return true; |
| 4035 | } |
| 4036 | |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 4037 | OatWriter::OatClass::OatClass(const dchecked_vector<CompiledMethod*>& compiled_methods, |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 4038 | uint32_t compiled_methods_with_code, |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 4039 | uint16_t oat_class_type) |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 4040 | : compiled_methods_(compiled_methods) { |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 4041 | const uint32_t num_methods = compiled_methods.size(); |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 4042 | CHECK_LE(compiled_methods_with_code, num_methods); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 4043 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 4044 | oat_method_offsets_offsets_from_oat_class_.resize(num_methods); |
| 4045 | |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 4046 | method_offsets_.resize(compiled_methods_with_code); |
| 4047 | method_headers_.resize(compiled_methods_with_code); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 4048 | |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 4049 | uint32_t oat_method_offsets_offset_from_oat_class = OatClassHeader::SizeOf(); |
| 4050 | // We only create this instance if there are at least some compiled. |
| 4051 | if (oat_class_type == kOatClassSomeCompiled) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 4052 | method_bitmap_.reset(new BitVector(num_methods, false, Allocator::GetMallocAllocator())); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 4053 | method_bitmap_size_ = method_bitmap_->GetSizeOf(); |
| 4054 | oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_); |
| 4055 | oat_method_offsets_offset_from_oat_class += method_bitmap_size_; |
| 4056 | } else { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 4057 | method_bitmap_ = nullptr; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 4058 | method_bitmap_size_ = 0; |
| 4059 | } |
| 4060 | |
| 4061 | for (size_t i = 0; i < num_methods; i++) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 4062 | CompiledMethod* compiled_method = compiled_methods_[i]; |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 4063 | if (HasCompiledCode(compiled_method)) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 4064 | oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class; |
| 4065 | oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets); |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 4066 | if (oat_class_type == kOatClassSomeCompiled) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 4067 | method_bitmap_->SetBit(i); |
| 4068 | } |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 4069 | } else { |
| 4070 | oat_method_offsets_offsets_from_oat_class_[i] = 0; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 4071 | } |
| 4072 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 4073 | } |
| 4074 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 4075 | size_t OatWriter::OatClass::SizeOf() const { |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 4076 | return ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_)) |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 4077 | + method_bitmap_size_ |
| 4078 | + (sizeof(method_offsets_[0]) * method_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 4079 | } |
| 4080 | |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 4081 | bool OatWriter::OatClassHeader::Write(OatWriter* oat_writer, |
| 4082 | OutputStream* out, |
| 4083 | const size_t file_offset) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 4084 | DCHECK_OFFSET_(); |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 4085 | if (!out->WriteFully(&status_, sizeof(status_))) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 4086 | PLOG(ERROR) << "Failed to write class status to " << out->GetLocation(); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 4087 | return false; |
| 4088 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 4089 | oat_writer->size_oat_class_status_ += sizeof(status_); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 4090 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 4091 | if (!out->WriteFully(&type_, sizeof(type_))) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 4092 | PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 4093 | return false; |
| 4094 | } |
| 4095 | oat_writer->size_oat_class_type_ += sizeof(type_); |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 4096 | return true; |
| 4097 | } |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 4098 | |
Mathieu Chartier | 3957bff | 2017-07-16 13:55:27 -0700 | [diff] [blame] | 4099 | bool OatWriter::OatClass::Write(OatWriter* oat_writer, OutputStream* out) const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 4100 | if (method_bitmap_size_ != 0) { |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 4101 | if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 4102 | PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 4103 | return false; |
| 4104 | } |
| 4105 | oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 4106 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 4107 | if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 4108 | PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 4109 | return false; |
| 4110 | } |
| 4111 | oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_; |
| 4112 | } |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 4113 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 4114 | if (!out->WriteFully(method_offsets_.data(), GetMethodOffsetsRawSize())) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 4115 | PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 4116 | return false; |
| 4117 | } |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 4118 | oat_writer->size_oat_class_method_offsets_ += GetMethodOffsetsRawSize(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 4119 | return true; |
| 4120 | } |
| 4121 | |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 4122 | const uint8_t* OatWriter::LookupBootImageInternTableSlot(const DexFile& dex_file, |
| 4123 | dex::StringIndex string_idx) |
Vladimir Marko | 94ec2db | 2017-09-06 17:21:03 +0100 | [diff] [blame] | 4124 | NO_THREAD_SAFETY_ANALYSIS { // Single-threaded OatWriter can avoid locking. |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 4125 | uint32_t utf16_length; |
| 4126 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
| 4127 | DCHECK_EQ(utf16_length, CountModifiedUtf8Chars(utf8_data)); |
| 4128 | InternTable::Utf8String string(utf16_length, |
| 4129 | utf8_data, |
| 4130 | ComputeUtf16HashFromModifiedUtf8(utf8_data, utf16_length)); |
| 4131 | const InternTable* intern_table = Runtime::Current()->GetClassLinker()->intern_table_; |
| 4132 | for (const InternTable::Table::UnorderedSet& table : intern_table->strong_interns_.tables_) { |
| 4133 | auto it = table.Find(string); |
| 4134 | if (it != table.end()) { |
| 4135 | return reinterpret_cast<const uint8_t*>(std::addressof(*it)); |
| 4136 | } |
| 4137 | } |
| 4138 | LOG(FATAL) << "Did not find boot image string " << utf8_data; |
| 4139 | UNREACHABLE(); |
| 4140 | } |
| 4141 | |
Vladimir Marko | 94ec2db | 2017-09-06 17:21:03 +0100 | [diff] [blame] | 4142 | const uint8_t* OatWriter::LookupBootImageClassTableSlot(const DexFile& dex_file, |
| 4143 | dex::TypeIndex type_idx) |
| 4144 | NO_THREAD_SAFETY_ANALYSIS { // Single-threaded OatWriter can avoid locking. |
| 4145 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
| 4146 | ClassTable::DescriptorHashPair pair(descriptor, ComputeModifiedUtf8Hash(descriptor)); |
| 4147 | ClassTable* table = Runtime::Current()->GetClassLinker()->boot_class_table_.get(); |
| 4148 | for (const ClassTable::ClassSet& class_set : table->classes_) { |
| 4149 | auto it = class_set.Find(pair); |
| 4150 | if (it != class_set.end()) { |
| 4151 | return reinterpret_cast<const uint8_t*>(std::addressof(*it)); |
| 4152 | } |
| 4153 | } |
| 4154 | LOG(FATAL) << "Did not find boot image class " << descriptor; |
| 4155 | UNREACHABLE(); |
| 4156 | } |
| 4157 | |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 4158 | } // namespace linker |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 4159 | } // namespace art |