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