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 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 19 | #include <unistd.h> |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 20 | #include <zlib.h> |
| 21 | |
Vladimir Marko | c74658b | 2015-03-31 10:26:41 +0100 | [diff] [blame] | 22 | #include "arch/arm64/instruction_set_features_arm64.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 23 | #include "art_method-inl.h" |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 24 | #include "base/allocator.h" |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 25 | #include "base/bit_vector.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 26 | #include "base/enums.h" |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 27 | #include "base/file_magic.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 28 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 29 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 30 | #include "class_linker.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 31 | #include "compiled_class.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 32 | #include "compiled_method.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 33 | #include "debug/method_debug_info.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 34 | #include "dex/verification_results.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 35 | #include "dex_file-inl.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 36 | #include "driver/compiler_driver.h" |
| 37 | #include "driver/compiler_options.h" |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 38 | #include "gc/space/image_space.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 39 | #include "gc/space/space.h" |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 40 | #include "handle_scope-inl.h" |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 41 | #include "image_writer.h" |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 42 | #include "linker/buffered_output_stream.h" |
| 43 | #include "linker/file_output_stream.h" |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 44 | #include "linker/multi_oat_relative_patcher.h" |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 45 | #include "linker/output_stream.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 46 | #include "mirror/array.h" |
| 47 | #include "mirror/class_loader.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 48 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 49 | #include "mirror/object-inl.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 50 | #include "oat_quick_method_header.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 51 | #include "os.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 52 | #include "safe_map.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 53 | #include "scoped_thread_state_change.h" |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 54 | #include "type_lookup_table.h" |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 55 | #include "utils/dex_cache_arrays_layout-inl.h" |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 56 | #include "vdex_file.h" |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 57 | #include "verifier/method_verifier.h" |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 58 | #include "zip_archive.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 59 | |
| 60 | namespace art { |
| 61 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 62 | namespace { // anonymous namespace |
| 63 | |
| 64 | typedef DexFile::Header __attribute__((aligned(1))) UnalignedDexFileHeader; |
| 65 | |
| 66 | const UnalignedDexFileHeader* AsUnalignedDexFileHeader(const uint8_t* raw_data) { |
| 67 | return reinterpret_cast<const UnalignedDexFileHeader*>(raw_data); |
| 68 | } |
| 69 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 70 | class ChecksumUpdatingOutputStream : public OutputStream { |
| 71 | public: |
| 72 | ChecksumUpdatingOutputStream(OutputStream* out, OatHeader* oat_header) |
| 73 | : OutputStream(out->GetLocation()), out_(out), oat_header_(oat_header) { } |
| 74 | |
| 75 | bool WriteFully(const void* buffer, size_t byte_count) OVERRIDE { |
| 76 | oat_header_->UpdateChecksum(buffer, byte_count); |
| 77 | return out_->WriteFully(buffer, byte_count); |
| 78 | } |
| 79 | |
| 80 | off_t Seek(off_t offset, Whence whence) OVERRIDE { |
| 81 | return out_->Seek(offset, whence); |
| 82 | } |
| 83 | |
| 84 | bool Flush() OVERRIDE { |
| 85 | return out_->Flush(); |
| 86 | } |
| 87 | |
| 88 | private: |
| 89 | OutputStream* const out_; |
| 90 | OatHeader* const oat_header_; |
| 91 | }; |
| 92 | |
Vladimir Marko | 0c737df | 2016-08-01 16:33:16 +0100 | [diff] [blame] | 93 | inline uint32_t CodeAlignmentSize(uint32_t header_offset, const CompiledMethod& compiled_method) { |
| 94 | // We want to align the code rather than the preheader. |
| 95 | uint32_t unaligned_code_offset = header_offset + sizeof(OatQuickMethodHeader); |
| 96 | uint32_t aligned_code_offset = compiled_method.AlignCode(unaligned_code_offset); |
| 97 | return aligned_code_offset - unaligned_code_offset; |
| 98 | } |
| 99 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 100 | } // anonymous namespace |
| 101 | |
| 102 | // Defines the location of the raw dex file to write. |
| 103 | class OatWriter::DexFileSource { |
| 104 | public: |
| 105 | explicit DexFileSource(ZipEntry* zip_entry) |
| 106 | : type_(kZipEntry), source_(zip_entry) { |
| 107 | DCHECK(source_ != nullptr); |
| 108 | } |
| 109 | |
| 110 | explicit DexFileSource(File* raw_file) |
| 111 | : type_(kRawFile), source_(raw_file) { |
| 112 | DCHECK(source_ != nullptr); |
| 113 | } |
| 114 | |
| 115 | explicit DexFileSource(const uint8_t* dex_file) |
| 116 | : type_(kRawData), source_(dex_file) { |
| 117 | DCHECK(source_ != nullptr); |
| 118 | } |
| 119 | |
| 120 | bool IsZipEntry() const { return type_ == kZipEntry; } |
| 121 | bool IsRawFile() const { return type_ == kRawFile; } |
| 122 | bool IsRawData() const { return type_ == kRawData; } |
| 123 | |
| 124 | ZipEntry* GetZipEntry() const { |
| 125 | DCHECK(IsZipEntry()); |
| 126 | DCHECK(source_ != nullptr); |
| 127 | return static_cast<ZipEntry*>(const_cast<void*>(source_)); |
| 128 | } |
| 129 | |
| 130 | File* GetRawFile() const { |
| 131 | DCHECK(IsRawFile()); |
| 132 | DCHECK(source_ != nullptr); |
| 133 | return static_cast<File*>(const_cast<void*>(source_)); |
| 134 | } |
| 135 | |
| 136 | const uint8_t* GetRawData() const { |
| 137 | DCHECK(IsRawData()); |
| 138 | DCHECK(source_ != nullptr); |
| 139 | return static_cast<const uint8_t*>(source_); |
| 140 | } |
| 141 | |
| 142 | void Clear() { |
| 143 | type_ = kNone; |
| 144 | source_ = nullptr; |
| 145 | } |
| 146 | |
| 147 | private: |
| 148 | enum Type { |
| 149 | kNone, |
| 150 | kZipEntry, |
| 151 | kRawFile, |
| 152 | kRawData, |
| 153 | }; |
| 154 | |
| 155 | Type type_; |
| 156 | const void* source_; |
| 157 | }; |
| 158 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 159 | class OatWriter::OatClass { |
| 160 | public: |
| 161 | OatClass(size_t offset, |
| 162 | const dchecked_vector<CompiledMethod*>& compiled_methods, |
| 163 | uint32_t num_non_null_compiled_methods, |
| 164 | mirror::Class::Status status); |
| 165 | OatClass(OatClass&& src) = default; |
| 166 | size_t GetOatMethodOffsetsOffsetFromOatHeader(size_t class_def_method_index_) const; |
| 167 | size_t GetOatMethodOffsetsOffsetFromOatClass(size_t class_def_method_index_) const; |
| 168 | size_t SizeOf() const; |
| 169 | bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const; |
| 170 | |
| 171 | CompiledMethod* GetCompiledMethod(size_t class_def_method_index) const { |
| 172 | return compiled_methods_[class_def_method_index]; |
| 173 | } |
| 174 | |
| 175 | // Offset of start of OatClass from beginning of OatHeader. It is |
| 176 | // used to validate file position when writing. |
| 177 | size_t offset_; |
| 178 | |
| 179 | // CompiledMethods for each class_def_method_index, or null if no method is available. |
| 180 | dchecked_vector<CompiledMethod*> compiled_methods_; |
| 181 | |
| 182 | // Offset from OatClass::offset_ to the OatMethodOffsets for the |
| 183 | // class_def_method_index. If 0, it means the corresponding |
| 184 | // CompiledMethod entry in OatClass::compiled_methods_ should be |
| 185 | // null and that the OatClass::type_ should be kOatClassBitmap. |
| 186 | dchecked_vector<uint32_t> oat_method_offsets_offsets_from_oat_class_; |
| 187 | |
| 188 | // Data to write. |
| 189 | |
| 190 | static_assert(mirror::Class::Status::kStatusMax < (1 << 16), "class status won't fit in 16bits"); |
| 191 | int16_t status_; |
| 192 | |
| 193 | static_assert(OatClassType::kOatClassMax < (1 << 16), "oat_class type won't fit in 16bits"); |
| 194 | uint16_t type_; |
| 195 | |
| 196 | uint32_t method_bitmap_size_; |
| 197 | |
| 198 | // bit vector indexed by ClassDef method index. When |
| 199 | // OatClassType::type_ is kOatClassBitmap, a set bit indicates the |
| 200 | // method has an OatMethodOffsets in methods_offsets_, otherwise |
| 201 | // the entry was ommited to save space. If OatClassType::type_ is |
| 202 | // not is kOatClassBitmap, the bitmap will be null. |
| 203 | std::unique_ptr<BitVector> method_bitmap_; |
| 204 | |
| 205 | // OatMethodOffsets and OatMethodHeaders for each CompiledMethod |
| 206 | // present in the OatClass. Note that some may be missing if |
| 207 | // OatClass::compiled_methods_ contains null values (and |
| 208 | // oat_method_offsets_offsets_from_oat_class_ should contain 0 |
| 209 | // values in this case). |
| 210 | dchecked_vector<OatMethodOffsets> method_offsets_; |
| 211 | dchecked_vector<OatQuickMethodHeader> method_headers_; |
| 212 | |
| 213 | private: |
| 214 | size_t GetMethodOffsetsRawSize() const { |
| 215 | return method_offsets_.size() * sizeof(method_offsets_[0]); |
| 216 | } |
| 217 | |
| 218 | DISALLOW_COPY_AND_ASSIGN(OatClass); |
| 219 | }; |
| 220 | |
| 221 | class OatWriter::OatDexFile { |
| 222 | public: |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 223 | OatDexFile(const char* dex_file_location, |
| 224 | DexFileSource source, |
| 225 | CreateTypeLookupTable create_type_lookup_table); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 226 | OatDexFile(OatDexFile&& src) = default; |
| 227 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 228 | const char* GetLocation() const { |
| 229 | return dex_file_location_data_; |
| 230 | } |
| 231 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 232 | void ReserveClassOffsets(OatWriter* oat_writer); |
| 233 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 234 | size_t SizeOf() const; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 235 | bool Write(OatWriter* oat_writer, OutputStream* out) const; |
| 236 | bool WriteClassOffsets(OatWriter* oat_writer, OutputStream* out); |
| 237 | |
| 238 | // The source of the dex file. |
| 239 | DexFileSource source_; |
| 240 | |
| 241 | // Whether to create the type lookup table. |
| 242 | CreateTypeLookupTable create_type_lookup_table_; |
| 243 | |
| 244 | // Dex file size. Initialized when writing the dex file. |
| 245 | size_t dex_file_size_; |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 246 | |
| 247 | // Offset of start of OatDexFile from beginning of OatHeader. It is |
| 248 | // used to validate file position when writing. |
| 249 | size_t offset_; |
| 250 | |
| 251 | // Data to write. |
| 252 | uint32_t dex_file_location_size_; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 253 | const char* dex_file_location_data_; |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 254 | uint32_t dex_file_location_checksum_; |
| 255 | uint32_t dex_file_offset_; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 256 | uint32_t class_offsets_offset_; |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 257 | uint32_t lookup_table_offset_; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 258 | |
| 259 | // Data to write to a separate section. |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 260 | dchecked_vector<uint32_t> class_offsets_; |
| 261 | |
| 262 | private: |
| 263 | size_t GetClassOffsetsRawSize() const { |
| 264 | return class_offsets_.size() * sizeof(class_offsets_[0]); |
| 265 | } |
| 266 | |
| 267 | DISALLOW_COPY_AND_ASSIGN(OatDexFile); |
| 268 | }; |
| 269 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 270 | #define DCHECK_OFFSET() \ |
| 271 | DCHECK_EQ(static_cast<off_t>(file_offset + relative_offset), out->Seek(0, kSeekCurrent)) \ |
| 272 | << "file_offset=" << file_offset << " relative_offset=" << relative_offset |
| 273 | |
| 274 | #define DCHECK_OFFSET_() \ |
| 275 | DCHECK_EQ(static_cast<off_t>(file_offset + offset_), out->Seek(0, kSeekCurrent)) \ |
| 276 | << "file_offset=" << file_offset << " offset_=" << offset_ |
| 277 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 278 | OatWriter::OatWriter(bool compiling_boot_image, TimingLogger* timings) |
| 279 | : write_state_(WriteState::kAddingDexFileSources), |
| 280 | timings_(timings), |
| 281 | raw_dex_files_(), |
| 282 | zip_archives_(), |
| 283 | zipped_dex_files_(), |
| 284 | zipped_dex_file_locations_(), |
| 285 | compiler_driver_(nullptr), |
| 286 | image_writer_(nullptr), |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 287 | compiling_boot_image_(compiling_boot_image), |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 288 | dex_files_(nullptr), |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 289 | vdex_size_(0u), |
| 290 | vdex_dex_files_offset_(0u), |
| 291 | oat_size_(0u), |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 292 | bss_size_(0u), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 293 | oat_data_offset_(0u), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 294 | oat_header_(nullptr), |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 295 | size_vdex_header_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 296 | size_dex_file_alignment_(0), |
| 297 | size_executable_offset_alignment_(0), |
| 298 | size_oat_header_(0), |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 299 | size_oat_header_key_value_store_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 300 | size_dex_file_(0), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 301 | size_interpreter_to_interpreter_bridge_(0), |
| 302 | size_interpreter_to_compiled_code_bridge_(0), |
| 303 | size_jni_dlsym_lookup_(0), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 304 | size_quick_generic_jni_trampoline_(0), |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 305 | size_quick_imt_conflict_trampoline_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 306 | size_quick_resolution_trampoline_(0), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 307 | size_quick_to_interpreter_bridge_(0), |
| 308 | size_trampoline_alignment_(0), |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 309 | size_method_header_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 310 | size_code_(0), |
| 311 | size_code_alignment_(0), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 312 | size_relative_call_thunks_(0), |
Vladimir Marko | c74658b | 2015-03-31 10:26:41 +0100 | [diff] [blame] | 313 | size_misc_thunks_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 314 | size_vmap_table_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 315 | size_oat_dex_file_location_size_(0), |
| 316 | size_oat_dex_file_location_data_(0), |
| 317 | size_oat_dex_file_location_checksum_(0), |
| 318 | size_oat_dex_file_offset_(0), |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 319 | size_oat_dex_file_class_offsets_offset_(0), |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 320 | size_oat_dex_file_lookup_table_offset_(0), |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 321 | size_oat_lookup_table_alignment_(0), |
| 322 | size_oat_lookup_table_(0), |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 323 | size_oat_class_offsets_alignment_(0), |
| 324 | size_oat_class_offsets_(0), |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 325 | size_oat_class_type_(0), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 326 | size_oat_class_status_(0), |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 327 | size_oat_class_method_bitmaps_(0), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 328 | size_oat_class_method_offsets_(0), |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 329 | relative_patcher_(nullptr), |
| 330 | absolute_patch_locations_() { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | bool OatWriter::AddDexFileSource(const char* filename, |
| 334 | const char* location, |
| 335 | CreateTypeLookupTable create_type_lookup_table) { |
| 336 | DCHECK(write_state_ == WriteState::kAddingDexFileSources); |
| 337 | uint32_t magic; |
| 338 | std::string error_msg; |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 339 | File fd = OpenAndReadMagic(filename, &magic, &error_msg); |
| 340 | if (fd.Fd() == -1) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 341 | PLOG(ERROR) << "Failed to read magic number from dex file: '" << filename << "'"; |
| 342 | return false; |
| 343 | } else if (IsDexMagic(magic)) { |
| 344 | // The file is open for reading, not writing, so it's OK to let the File destructor |
| 345 | // close it without checking for explicit Close(), so pass checkUsage = false. |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 346 | raw_dex_files_.emplace_back(new File(fd.Release(), location, /* checkUsage */ false)); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 347 | oat_dex_files_.emplace_back(location, |
| 348 | DexFileSource(raw_dex_files_.back().get()), |
| 349 | create_type_lookup_table); |
| 350 | } else if (IsZipMagic(magic)) { |
| 351 | if (!AddZippedDexFilesSource(std::move(fd), location, create_type_lookup_table)) { |
| 352 | return false; |
| 353 | } |
| 354 | } else { |
| 355 | LOG(ERROR) << "Expected valid zip or dex file: '" << filename << "'"; |
| 356 | return false; |
| 357 | } |
| 358 | return true; |
| 359 | } |
| 360 | |
| 361 | // 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] | 362 | bool OatWriter::AddZippedDexFilesSource(File&& zip_fd, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 363 | const char* location, |
| 364 | CreateTypeLookupTable create_type_lookup_table) { |
| 365 | DCHECK(write_state_ == WriteState::kAddingDexFileSources); |
| 366 | std::string error_msg; |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 367 | zip_archives_.emplace_back(ZipArchive::OpenFromFd(zip_fd.Release(), location, &error_msg)); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 368 | ZipArchive* zip_archive = zip_archives_.back().get(); |
| 369 | if (zip_archive == nullptr) { |
| 370 | LOG(ERROR) << "Failed to open zip from file descriptor for '" << location << "': " |
| 371 | << error_msg; |
| 372 | return false; |
| 373 | } |
| 374 | for (size_t i = 0; ; ++i) { |
| 375 | std::string entry_name = DexFile::GetMultiDexClassesDexName(i); |
| 376 | std::unique_ptr<ZipEntry> entry(zip_archive->Find(entry_name.c_str(), &error_msg)); |
| 377 | if (entry == nullptr) { |
| 378 | break; |
| 379 | } |
| 380 | zipped_dex_files_.push_back(std::move(entry)); |
| 381 | zipped_dex_file_locations_.push_back(DexFile::GetMultiDexLocation(i, location)); |
| 382 | const char* full_location = zipped_dex_file_locations_.back().c_str(); |
| 383 | oat_dex_files_.emplace_back(full_location, |
| 384 | DexFileSource(zipped_dex_files_.back().get()), |
| 385 | create_type_lookup_table); |
| 386 | } |
| 387 | if (zipped_dex_file_locations_.empty()) { |
| 388 | LOG(ERROR) << "No dex files in zip file '" << location << "': " << error_msg; |
| 389 | return false; |
| 390 | } |
| 391 | return true; |
| 392 | } |
| 393 | |
| 394 | // Add dex file source from raw memory. |
| 395 | bool OatWriter::AddRawDexFileSource(const ArrayRef<const uint8_t>& data, |
| 396 | const char* location, |
| 397 | uint32_t location_checksum, |
| 398 | CreateTypeLookupTable create_type_lookup_table) { |
| 399 | DCHECK(write_state_ == WriteState::kAddingDexFileSources); |
| 400 | if (data.size() < sizeof(DexFile::Header)) { |
| 401 | LOG(ERROR) << "Provided data is shorter than dex file header. size: " |
| 402 | << data.size() << " File: " << location; |
| 403 | return false; |
| 404 | } |
| 405 | if (!ValidateDexFileHeader(data.data(), location)) { |
| 406 | return false; |
| 407 | } |
| 408 | const UnalignedDexFileHeader* header = AsUnalignedDexFileHeader(data.data()); |
| 409 | if (data.size() < header->file_size_) { |
| 410 | LOG(ERROR) << "Truncated dex file data. Data size: " << data.size() |
| 411 | << " file size from header: " << header->file_size_ << " File: " << location; |
| 412 | return false; |
| 413 | } |
| 414 | |
| 415 | oat_dex_files_.emplace_back(location, DexFileSource(data.data()), create_type_lookup_table); |
| 416 | oat_dex_files_.back().dex_file_location_checksum_ = location_checksum; |
| 417 | return true; |
| 418 | } |
| 419 | |
| 420 | dchecked_vector<const char*> OatWriter::GetSourceLocations() const { |
| 421 | dchecked_vector<const char*> locations; |
| 422 | locations.reserve(oat_dex_files_.size()); |
| 423 | for (const OatDexFile& oat_dex_file : oat_dex_files_) { |
| 424 | locations.push_back(oat_dex_file.GetLocation()); |
| 425 | } |
| 426 | return locations; |
| 427 | } |
| 428 | |
| 429 | bool OatWriter::WriteAndOpenDexFiles( |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 430 | File* vdex_file, |
| 431 | OutputStream* oat_rodata, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 432 | InstructionSet instruction_set, |
| 433 | const InstructionSetFeatures* instruction_set_features, |
| 434 | SafeMap<std::string, std::string>* key_value_store, |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 435 | bool verify, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 436 | /*out*/ std::unique_ptr<MemMap>* opened_dex_files_map, |
| 437 | /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files) { |
| 438 | CHECK(write_state_ == WriteState::kAddingDexFileSources); |
| 439 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 440 | // Record the ELF rodata section offset, i.e. the beginning of the OAT data. |
| 441 | if (!RecordOatDataOffset(oat_rodata)) { |
| 442 | return false; |
| 443 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 444 | |
| 445 | std::unique_ptr<MemMap> dex_files_map; |
| 446 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 447 | |
| 448 | // Initialize VDEX and OAT headers. |
| 449 | if (kIsVdexEnabled) { |
| 450 | size_vdex_header_ = sizeof(VdexFile::Header); |
| 451 | vdex_size_ = size_vdex_header_; |
| 452 | } |
| 453 | size_t oat_data_offset = InitOatHeader(instruction_set, |
| 454 | instruction_set_features, |
| 455 | dchecked_integral_cast<uint32_t>(oat_dex_files_.size()), |
| 456 | key_value_store); |
| 457 | oat_size_ = InitOatDexFiles(oat_data_offset); |
| 458 | |
| 459 | ChecksumUpdatingOutputStream checksum_updating_rodata(oat_rodata, oat_header_.get()); |
| 460 | |
| 461 | if (kIsVdexEnabled) { |
| 462 | std::unique_ptr<BufferedOutputStream> vdex_out( |
| 463 | MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(vdex_file))); |
| 464 | |
| 465 | // Write DEX files into VDEX, mmap and open them. |
| 466 | if (!WriteDexFiles(vdex_out.get(), vdex_file) || |
| 467 | !OpenDexFiles(vdex_file, verify, &dex_files_map, &dex_files)) { |
| 468 | return false; |
| 469 | } |
| 470 | |
| 471 | // VDEX is finalized. Seek to the beginning of the file and write the header. |
| 472 | if (!WriteVdexHeader(vdex_out.get())) { |
| 473 | return false; |
| 474 | } |
| 475 | } else { |
| 476 | // Write DEX files into OAT, mmap and open them. |
| 477 | if (!WriteDexFiles(oat_rodata, vdex_file) || |
| 478 | !OpenDexFiles(vdex_file, verify, &dex_files_map, &dex_files)) { |
| 479 | return false; |
| 480 | } |
| 481 | |
| 482 | // Do a bulk checksum update for Dex[]. Doing it piece by piece would be |
| 483 | // difficult because we're not using the OutputStream directly. |
| 484 | if (!oat_dex_files_.empty()) { |
| 485 | size_t size = oat_size_ - oat_dex_files_[0].dex_file_offset_; |
| 486 | oat_header_->UpdateChecksum(dex_files_map->Begin(), size); |
| 487 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 488 | } |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 489 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 490 | // Write TypeLookupTables into OAT. |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 491 | if (!WriteTypeLookupTables(&checksum_updating_rodata, dex_files)) { |
| 492 | return false; |
| 493 | } |
| 494 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 495 | // Reserve space for class offsets in OAT and update class_offsets_offset_. |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 496 | for (OatDexFile& oat_dex_file : oat_dex_files_) { |
| 497 | oat_dex_file.ReserveClassOffsets(this); |
| 498 | } |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 499 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 500 | // Write OatDexFiles into OAT. Needs to be done last, once offsets are collected. |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 501 | if (!WriteOatDexFiles(&checksum_updating_rodata)) { |
| 502 | return false; |
David Brazdil | b92ba62 | 2016-09-01 16:00:30 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 505 | *opened_dex_files_map = std::move(dex_files_map); |
| 506 | *opened_dex_files = std::move(dex_files); |
| 507 | write_state_ = WriteState::kPrepareLayout; |
| 508 | return true; |
| 509 | } |
| 510 | |
| 511 | void OatWriter::PrepareLayout(const CompilerDriver* compiler, |
| 512 | ImageWriter* image_writer, |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 513 | const std::vector<const DexFile*>& dex_files, |
| 514 | linker::MultiOatRelativePatcher* relative_patcher) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 515 | CHECK(write_state_ == WriteState::kPrepareLayout); |
| 516 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 517 | compiler_driver_ = compiler; |
| 518 | image_writer_ = image_writer; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 519 | dex_files_ = &dex_files; |
| 520 | relative_patcher_ = relative_patcher; |
| 521 | SetMultiOatRelativePatcherAdjustment(); |
| 522 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 523 | if (compiling_boot_image_) { |
| 524 | CHECK(image_writer_ != nullptr); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 525 | } |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 526 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 527 | CHECK_EQ(instruction_set, oat_header_->GetInstructionSet()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 528 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 529 | uint32_t offset = oat_size_; |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 530 | { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 531 | TimingLogger::ScopedTiming split("InitOatClasses", timings_); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 532 | offset = InitOatClasses(offset); |
| 533 | } |
| 534 | { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 535 | TimingLogger::ScopedTiming split("InitOatMaps", timings_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 536 | offset = InitOatMaps(offset); |
| 537 | } |
| 538 | { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 539 | TimingLogger::ScopedTiming split("InitOatCode", timings_); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 540 | offset = InitOatCode(offset); |
| 541 | } |
| 542 | { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 543 | TimingLogger::ScopedTiming split("InitOatCodeDexFiles", timings_); |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 544 | offset = InitOatCodeDexFiles(offset); |
| 545 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 546 | oat_size_ = offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 547 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 548 | if (!HasBootImage()) { |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 549 | // Allocate space for app dex cache arrays in the .bss section. |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 550 | size_t bss_start = RoundUp(oat_size_, kPageSize); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 551 | PointerSize pointer_size = GetInstructionSetPointerSize(instruction_set); |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 552 | bss_size_ = 0u; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 553 | for (const DexFile* dex_file : *dex_files_) { |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 554 | dex_cache_arrays_offsets_.Put(dex_file, bss_start + bss_size_); |
| 555 | DexCacheArraysLayout layout(pointer_size, dex_file); |
| 556 | bss_size_ += layout.Size(); |
| 557 | } |
| 558 | } |
| 559 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 560 | CHECK_EQ(dex_files_->size(), oat_dex_files_.size()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 561 | if (compiling_boot_image_) { |
| 562 | CHECK_EQ(image_writer_ != nullptr, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 563 | oat_header_->GetStoreValueByKey(OatHeader::kImageLocationKey) == nullptr); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 564 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 565 | |
| 566 | write_state_ = WriteState::kWriteRoData; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 569 | OatWriter::~OatWriter() { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 572 | class OatWriter::DexMethodVisitor { |
| 573 | public: |
| 574 | DexMethodVisitor(OatWriter* writer, size_t offset) |
| 575 | : writer_(writer), |
| 576 | offset_(offset), |
| 577 | dex_file_(nullptr), |
| 578 | class_def_index_(DexFile::kDexNoIndex) { |
| 579 | } |
| 580 | |
| 581 | virtual bool StartClass(const DexFile* dex_file, size_t class_def_index) { |
| 582 | DCHECK(dex_file_ == nullptr); |
| 583 | DCHECK_EQ(class_def_index_, DexFile::kDexNoIndex); |
| 584 | dex_file_ = dex_file; |
| 585 | class_def_index_ = class_def_index; |
| 586 | return true; |
| 587 | } |
| 588 | |
| 589 | virtual bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) = 0; |
| 590 | |
| 591 | virtual bool EndClass() { |
| 592 | if (kIsDebugBuild) { |
| 593 | dex_file_ = nullptr; |
| 594 | class_def_index_ = DexFile::kDexNoIndex; |
| 595 | } |
| 596 | return true; |
| 597 | } |
| 598 | |
| 599 | size_t GetOffset() const { |
| 600 | return offset_; |
| 601 | } |
| 602 | |
| 603 | protected: |
| 604 | virtual ~DexMethodVisitor() { } |
| 605 | |
| 606 | OatWriter* const writer_; |
| 607 | |
| 608 | // The offset is usually advanced for each visited method by the derived class. |
| 609 | size_t offset_; |
| 610 | |
| 611 | // The dex file and class def index are set in StartClass(). |
| 612 | const DexFile* dex_file_; |
| 613 | size_t class_def_index_; |
| 614 | }; |
| 615 | |
| 616 | class OatWriter::OatDexMethodVisitor : public DexMethodVisitor { |
| 617 | public: |
| 618 | OatDexMethodVisitor(OatWriter* writer, size_t offset) |
| 619 | : DexMethodVisitor(writer, offset), |
| 620 | oat_class_index_(0u), |
| 621 | method_offsets_index_(0u) { |
| 622 | } |
| 623 | |
| 624 | bool StartClass(const DexFile* dex_file, size_t class_def_index) { |
| 625 | DexMethodVisitor::StartClass(dex_file, class_def_index); |
| 626 | DCHECK_LT(oat_class_index_, writer_->oat_classes_.size()); |
| 627 | method_offsets_index_ = 0u; |
| 628 | return true; |
| 629 | } |
| 630 | |
| 631 | bool EndClass() { |
| 632 | ++oat_class_index_; |
| 633 | return DexMethodVisitor::EndClass(); |
| 634 | } |
| 635 | |
| 636 | protected: |
| 637 | size_t oat_class_index_; |
| 638 | size_t method_offsets_index_; |
| 639 | }; |
| 640 | |
| 641 | class OatWriter::InitOatClassesMethodVisitor : public DexMethodVisitor { |
| 642 | public: |
| 643 | InitOatClassesMethodVisitor(OatWriter* writer, size_t offset) |
| 644 | : DexMethodVisitor(writer, offset), |
| 645 | compiled_methods_(), |
| 646 | num_non_null_compiled_methods_(0u) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 647 | size_t num_classes = 0u; |
| 648 | for (const OatDexFile& oat_dex_file : writer_->oat_dex_files_) { |
| 649 | num_classes += oat_dex_file.class_offsets_.size(); |
| 650 | } |
| 651 | writer_->oat_classes_.reserve(num_classes); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 652 | compiled_methods_.reserve(256u); |
| 653 | } |
| 654 | |
| 655 | bool StartClass(const DexFile* dex_file, size_t class_def_index) { |
| 656 | DexMethodVisitor::StartClass(dex_file, class_def_index); |
| 657 | compiled_methods_.clear(); |
| 658 | num_non_null_compiled_methods_ = 0u; |
| 659 | return true; |
| 660 | } |
| 661 | |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 662 | bool VisitMethod(size_t class_def_method_index ATTRIBUTE_UNUSED, |
| 663 | const ClassDataItemIterator& it) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 664 | // Fill in the compiled_methods_ array for methods that have a |
| 665 | // CompiledMethod. We track the number of non-null entries in |
| 666 | // num_non_null_compiled_methods_ since we only want to allocate |
| 667 | // OatMethodOffsets for the compiled methods. |
| 668 | uint32_t method_idx = it.GetMemberIndex(); |
| 669 | CompiledMethod* compiled_method = |
| 670 | writer_->compiler_driver_->GetCompiledMethod(MethodReference(dex_file_, method_idx)); |
| 671 | compiled_methods_.push_back(compiled_method); |
| 672 | if (compiled_method != nullptr) { |
| 673 | ++num_non_null_compiled_methods_; |
| 674 | } |
| 675 | return true; |
| 676 | } |
| 677 | |
| 678 | bool EndClass() { |
| 679 | ClassReference class_ref(dex_file_, class_def_index_); |
| 680 | CompiledClass* compiled_class = writer_->compiler_driver_->GetCompiledClass(class_ref); |
| 681 | mirror::Class::Status status; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 682 | if (compiled_class != nullptr) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 683 | status = compiled_class->GetStatus(); |
| 684 | } else if (writer_->compiler_driver_->GetVerificationResults()->IsClassRejected(class_ref)) { |
| 685 | status = mirror::Class::kStatusError; |
| 686 | } else { |
| 687 | status = mirror::Class::kStatusNotReady; |
| 688 | } |
| 689 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 690 | writer_->oat_classes_.emplace_back(offset_, |
| 691 | compiled_methods_, |
| 692 | num_non_null_compiled_methods_, |
| 693 | status); |
| 694 | offset_ += writer_->oat_classes_.back().SizeOf(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 695 | return DexMethodVisitor::EndClass(); |
| 696 | } |
| 697 | |
| 698 | private: |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 699 | dchecked_vector<CompiledMethod*> compiled_methods_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 700 | size_t num_non_null_compiled_methods_; |
| 701 | }; |
| 702 | |
| 703 | class OatWriter::InitCodeMethodVisitor : public OatDexMethodVisitor { |
| 704 | public: |
| 705 | InitCodeMethodVisitor(OatWriter* writer, size_t offset) |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 706 | : OatDexMethodVisitor(writer, offset), |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 707 | debuggable_(writer->GetCompilerDriver()->GetCompilerOptions().GetDebuggable()) { |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 708 | writer_->absolute_patch_locations_.reserve( |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 709 | writer_->compiler_driver_->GetNonRelativeLinkerPatchCount()); |
| 710 | } |
| 711 | |
| 712 | bool EndClass() { |
| 713 | OatDexMethodVisitor::EndClass(); |
| 714 | if (oat_class_index_ == writer_->oat_classes_.size()) { |
Vladimir Marko | 71b0ddf | 2015-04-02 19:45:06 +0100 | [diff] [blame] | 715 | offset_ = writer_->relative_patcher_->ReserveSpaceEnd(offset_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 716 | } |
| 717 | return true; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 721 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 722 | OatClass* oat_class = &writer_->oat_classes_[oat_class_index_]; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 723 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 724 | |
| 725 | if (compiled_method != nullptr) { |
| 726 | // Derived from CompiledMethod. |
| 727 | uint32_t quick_code_offset = 0; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 728 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 729 | ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode(); |
| 730 | uint32_t code_size = quick_code.size() * sizeof(uint8_t); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 731 | uint32_t thumb_offset = compiled_method->CodeDelta(); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 732 | |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 733 | // Deduplicate code arrays if we are not producing debuggable code. |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 734 | bool deduped = true; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 735 | MethodReference method_ref(dex_file_, it.GetMemberIndex()); |
Nicolas Geoffray | ed6195a | 2015-07-13 17:02:30 +0000 | [diff] [blame] | 736 | if (debuggable_) { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 737 | quick_code_offset = writer_->relative_patcher_->GetOffset(method_ref); |
| 738 | if (quick_code_offset != 0u) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 739 | // Duplicate methods, we want the same code for both of them so that the oat writer puts |
| 740 | // the same code in both ArtMethods so that we do not get different oat code at runtime. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 741 | } else { |
| 742 | quick_code_offset = NewQuickCodeOffset(compiled_method, it, thumb_offset); |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 743 | deduped = false; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 744 | } |
Nicolas Geoffray | ed6195a | 2015-07-13 17:02:30 +0000 | [diff] [blame] | 745 | } else { |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 746 | quick_code_offset = dedupe_map_.GetOrCreate( |
| 747 | compiled_method, |
| 748 | [this, &deduped, compiled_method, &it, thumb_offset]() { |
| 749 | deduped = false; |
| 750 | return NewQuickCodeOffset(compiled_method, it, thumb_offset); |
| 751 | }); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 752 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 753 | |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 754 | if (code_size != 0) { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 755 | if (writer_->relative_patcher_->GetOffset(method_ref) != 0u) { |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 756 | // TODO: Should this be a hard failure? |
| 757 | LOG(WARNING) << "Multiple definitions of " |
| 758 | << PrettyMethod(method_ref.dex_method_index, *method_ref.dex_file) |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 759 | << " offsets " << writer_->relative_patcher_->GetOffset(method_ref) |
| 760 | << " " << quick_code_offset; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 761 | } else { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 762 | writer_->relative_patcher_->SetOffset(method_ref, quick_code_offset); |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 763 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 764 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 765 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 766 | // Update quick method header. |
| 767 | DCHECK_LT(method_offsets_index_, oat_class->method_headers_.size()); |
| 768 | OatQuickMethodHeader* method_header = &oat_class->method_headers_[method_offsets_index_]; |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 769 | uint32_t vmap_table_offset = method_header->vmap_table_offset_; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 770 | // If we don't have quick code, then we must have a vmap, as that is how the dex2dex |
| 771 | // compiler records its transformations. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 772 | DCHECK(!quick_code.empty() || vmap_table_offset != 0); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 773 | // The code offset was 0 when the mapping/vmap table offset was set, so it's set |
| 774 | // to 0-offset and we need to adjust it by code_offset. |
| 775 | uint32_t code_offset = quick_code_offset - thumb_offset; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 776 | if (vmap_table_offset != 0u && code_offset != 0u) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 777 | vmap_table_offset += code_offset; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 778 | DCHECK_LT(vmap_table_offset, code_offset) << "Overflow in oat offsets"; |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 779 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 780 | uint32_t frame_size_in_bytes = compiled_method->GetFrameSizeInBytes(); |
| 781 | uint32_t core_spill_mask = compiled_method->GetCoreSpillMask(); |
| 782 | uint32_t fp_spill_mask = compiled_method->GetFpSpillMask(); |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 783 | *method_header = OatQuickMethodHeader(vmap_table_offset, |
| 784 | frame_size_in_bytes, |
| 785 | core_spill_mask, |
| 786 | fp_spill_mask, |
| 787 | code_size); |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 788 | |
Nicolas Geoffray | ed6195a | 2015-07-13 17:02:30 +0000 | [diff] [blame] | 789 | if (!deduped) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 790 | // Update offsets. (Checksum is updated when writing.) |
| 791 | offset_ += sizeof(*method_header); // Method header is prepended before code. |
| 792 | offset_ += code_size; |
| 793 | // Record absolute patch locations. |
| 794 | if (!compiled_method->GetPatches().empty()) { |
| 795 | uintptr_t base_loc = offset_ - code_size - writer_->oat_header_->GetExecutableOffset(); |
| 796 | for (const LinkerPatch& patch : compiled_method->GetPatches()) { |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 797 | if (!patch.IsPcRelative()) { |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 798 | writer_->absolute_patch_locations_.push_back(base_loc + patch.LiteralOffset()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 799 | } |
| 800 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 801 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 802 | } |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 803 | |
David Srbecky | 91cc06c | 2016-03-07 16:13:58 +0000 | [diff] [blame] | 804 | const CompilerOptions& compiler_options = writer_->compiler_driver_->GetCompilerOptions(); |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 805 | // Exclude quickened dex methods (code_size == 0) since they have no native code. |
| 806 | if (compiler_options.GenerateAnyDebugInfo() && code_size != 0) { |
| 807 | bool has_code_info = method_header->IsOptimized(); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 808 | // Record debug information for this function if we are doing that. |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 809 | debug::MethodDebugInfo info = debug::MethodDebugInfo(); |
| 810 | info.trampoline_name = nullptr; |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 811 | info.dex_file = dex_file_; |
| 812 | info.class_def_index = class_def_index_; |
| 813 | info.dex_method_index = it.GetMemberIndex(); |
| 814 | info.access_flags = it.GetMethodAccessFlags(); |
| 815 | info.code_item = it.GetMethodCodeItem(); |
| 816 | info.isa = compiled_method->GetInstructionSet(); |
| 817 | info.deduped = deduped; |
| 818 | info.is_native_debuggable = compiler_options.GetNativeDebuggable(); |
| 819 | info.is_optimized = method_header->IsOptimized(); |
| 820 | info.is_code_address_text_relative = true; |
| 821 | info.code_address = code_offset - writer_->oat_header_->GetExecutableOffset(); |
| 822 | info.code_size = code_size; |
| 823 | info.frame_size_in_bytes = compiled_method->GetFrameSizeInBytes(); |
| 824 | info.code_info = has_code_info ? compiled_method->GetVmapTable().data() : nullptr; |
| 825 | info.cfi = compiled_method->GetCFIInfo(); |
| 826 | writer_->method_info_.push_back(info); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 827 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 828 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 829 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 830 | OatMethodOffsets* offsets = &oat_class->method_offsets_[method_offsets_index_]; |
| 831 | offsets->code_offset_ = quick_code_offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 832 | ++method_offsets_index_; |
| 833 | } |
| 834 | |
| 835 | return true; |
| 836 | } |
| 837 | |
| 838 | private: |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 839 | struct CodeOffsetsKeyComparator { |
| 840 | bool operator()(const CompiledMethod* lhs, const CompiledMethod* rhs) const { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 841 | // Code is deduplicated by CompilerDriver, compare only data pointers. |
| 842 | if (lhs->GetQuickCode().data() != rhs->GetQuickCode().data()) { |
| 843 | return lhs->GetQuickCode().data() < rhs->GetQuickCode().data(); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 844 | } |
| 845 | // 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] | 846 | if (UNLIKELY(lhs->GetVmapTable().data() != rhs->GetVmapTable().data())) { |
| 847 | return lhs->GetVmapTable().data() < rhs->GetVmapTable().data(); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 848 | } |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 849 | if (UNLIKELY(lhs->GetPatches().data() != rhs->GetPatches().data())) { |
| 850 | return lhs->GetPatches().data() < rhs->GetPatches().data(); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 851 | } |
| 852 | return false; |
| 853 | } |
| 854 | }; |
| 855 | |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 856 | uint32_t NewQuickCodeOffset(CompiledMethod* compiled_method, |
| 857 | const ClassDataItemIterator& it, |
| 858 | uint32_t thumb_offset) { |
| 859 | offset_ = writer_->relative_patcher_->ReserveSpace( |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 860 | offset_, compiled_method, MethodReference(dex_file_, it.GetMemberIndex())); |
Vladimir Marko | 0c737df | 2016-08-01 16:33:16 +0100 | [diff] [blame] | 861 | offset_ += CodeAlignmentSize(offset_, *compiled_method); |
| 862 | DCHECK_ALIGNED_PARAM(offset_ + sizeof(OatQuickMethodHeader), |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 863 | GetInstructionSetAlignment(compiled_method->GetInstructionSet())); |
| 864 | return offset_ + sizeof(OatQuickMethodHeader) + thumb_offset; |
| 865 | } |
| 866 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 867 | // Deduplication is already done on a pointer basis by the compiler driver, |
| 868 | // 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] | 869 | SafeMap<const CompiledMethod*, uint32_t, CodeOffsetsKeyComparator> dedupe_map_; |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 870 | |
David Srbecky | 009e2a6 | 2015-04-15 02:46:30 +0100 | [diff] [blame] | 871 | // Cache of compiler's --debuggable option. |
| 872 | const bool debuggable_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 873 | }; |
| 874 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 875 | class OatWriter::InitMapMethodVisitor : public OatDexMethodVisitor { |
| 876 | public: |
| 877 | InitMapMethodVisitor(OatWriter* writer, size_t offset) |
| 878 | : OatDexMethodVisitor(writer, offset) { |
| 879 | } |
| 880 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 881 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it ATTRIBUTE_UNUSED) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 882 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 883 | OatClass* oat_class = &writer_->oat_classes_[oat_class_index_]; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 884 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 885 | |
| 886 | if (compiled_method != nullptr) { |
| 887 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 888 | DCHECK_EQ(oat_class->method_headers_[method_offsets_index_].vmap_table_offset_, 0u); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 889 | |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 890 | ArrayRef<const uint8_t> map = compiled_method->GetVmapTable(); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 891 | uint32_t map_size = map.size() * sizeof(map[0]); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 892 | if (map_size != 0u) { |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 893 | size_t offset = dedupe_map_.GetOrCreate( |
| 894 | map.data(), |
| 895 | [this, map_size]() { |
| 896 | uint32_t new_offset = offset_; |
| 897 | offset_ += map_size; |
| 898 | return new_offset; |
| 899 | }); |
| 900 | // Code offset is not initialized yet, so set the map offset to 0u-offset. |
| 901 | DCHECK_EQ(oat_class->method_offsets_[method_offsets_index_].code_offset_, 0u); |
| 902 | oat_class->method_headers_[method_offsets_index_].vmap_table_offset_ = 0u - offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 903 | } |
| 904 | ++method_offsets_index_; |
| 905 | } |
| 906 | |
| 907 | return true; |
| 908 | } |
| 909 | |
| 910 | private: |
| 911 | // Deduplication is already done on a pointer basis by the compiler driver, |
| 912 | // so we can simply compare the pointers to find out if things are duplicated. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 913 | SafeMap<const uint8_t*, uint32_t> dedupe_map_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 914 | }; |
| 915 | |
| 916 | class OatWriter::InitImageMethodVisitor : public OatDexMethodVisitor { |
| 917 | public: |
| 918 | InitImageMethodVisitor(OatWriter* writer, size_t offset) |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 919 | : OatDexMethodVisitor(writer, offset), |
| 920 | pointer_size_(GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet())) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 924 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 925 | const DexFile::TypeId& type_id = |
| 926 | dex_file_->GetTypeId(dex_file_->GetClassDef(class_def_index_).class_idx_); |
| 927 | const char* class_descriptor = dex_file_->GetTypeDescriptor(type_id); |
| 928 | // Skip methods that are not in the image. |
| 929 | if (!writer_->GetCompilerDriver()->IsImageClass(class_descriptor)) { |
| 930 | return true; |
| 931 | } |
| 932 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 933 | OatClass* oat_class = &writer_->oat_classes_[oat_class_index_]; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 934 | CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 935 | |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 936 | OatMethodOffsets offsets(0u); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 937 | if (compiled_method != nullptr) { |
| 938 | DCHECK_LT(method_offsets_index_, oat_class->method_offsets_.size()); |
| 939 | offsets = oat_class->method_offsets_[method_offsets_index_]; |
| 940 | ++method_offsets_index_; |
| 941 | } |
| 942 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 943 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 944 | // Unchecked as we hold mutator_lock_ on entry. |
| 945 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 946 | StackHandleScope<1> hs(soa.Self()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 947 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(linker->FindDexCache( |
| 948 | Thread::Current(), *dex_file_))); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 949 | ArtMethod* method; |
| 950 | if (writer_->HasBootImage()) { |
| 951 | const InvokeType invoke_type = it.GetMethodInvokeType( |
| 952 | dex_file_->GetClassDef(class_def_index_)); |
| 953 | method = linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>( |
| 954 | *dex_file_, |
| 955 | it.GetMemberIndex(), |
| 956 | dex_cache, |
| 957 | ScopedNullHandle<mirror::ClassLoader>(), |
| 958 | nullptr, |
| 959 | invoke_type); |
| 960 | if (method == nullptr) { |
| 961 | LOG(INTERNAL_FATAL) << "Unexpected failure to resolve a method: " |
| 962 | << PrettyMethod(it.GetMemberIndex(), *dex_file_, true); |
| 963 | soa.Self()->AssertPendingException(); |
| 964 | mirror::Throwable* exc = soa.Self()->GetException(); |
| 965 | std::string dump = exc->Dump(); |
| 966 | LOG(FATAL) << dump; |
| 967 | UNREACHABLE(); |
| 968 | } |
| 969 | } else { |
| 970 | // Should already have been resolved by the compiler, just peek into the dex cache. |
| 971 | // It may not be resolved if the class failed to verify, in this case, don't set the |
| 972 | // entrypoint. This is not fatal since the dex cache will contain a resolution method. |
| 973 | method = dex_cache->GetResolvedMethod(it.GetMemberIndex(), linker->GetImagePointerSize()); |
Andreas Gampe | d9efea6 | 2014-07-21 22:56:08 -0700 | [diff] [blame] | 974 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 975 | if (method != nullptr && |
| 976 | compiled_method != nullptr && |
| 977 | compiled_method->GetQuickCode().size() != 0) { |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 978 | method->SetEntryPointFromQuickCompiledCodePtrSize( |
| 979 | reinterpret_cast<void*>(offsets.code_offset_), pointer_size_); |
| 980 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 981 | |
| 982 | return true; |
| 983 | } |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 984 | |
| 985 | protected: |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 986 | const PointerSize pointer_size_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 987 | }; |
| 988 | |
| 989 | class OatWriter::WriteCodeMethodVisitor : public OatDexMethodVisitor { |
| 990 | public: |
| 991 | WriteCodeMethodVisitor(OatWriter* writer, OutputStream* out, const size_t file_offset, |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 992 | size_t relative_offset) SHARED_LOCK_FUNCTION(Locks::mutator_lock_) |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 993 | : OatDexMethodVisitor(writer, relative_offset), |
| 994 | out_(out), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 995 | file_offset_(file_offset), |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 996 | soa_(Thread::Current()), |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame^] | 997 | no_thread_suspension_("OatWriter patching"), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 998 | class_linker_(Runtime::Current()->GetClassLinker()), |
| 999 | dex_cache_(nullptr) { |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 1000 | patched_code_.reserve(16 * KB); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1001 | if (writer_->HasBootImage()) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1002 | // If we're creating the image, the address space must be ready so that we can apply patches. |
| 1003 | CHECK(writer_->image_writer_->IsImageAddressSpaceReady()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1004 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1005 | } |
| 1006 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1007 | ~WriteCodeMethodVisitor() UNLOCK_FUNCTION(Locks::mutator_lock_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | bool StartClass(const DexFile* dex_file, size_t class_def_index) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1011 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1012 | OatDexMethodVisitor::StartClass(dex_file, class_def_index); |
| 1013 | if (dex_cache_ == nullptr || dex_cache_->GetDexFile() != dex_file) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1014 | dex_cache_ = class_linker_->FindDexCache(Thread::Current(), *dex_file); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1015 | DCHECK(dex_cache_ != nullptr); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1016 | } |
| 1017 | return true; |
| 1018 | } |
| 1019 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1020 | bool EndClass() REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1021 | bool result = OatDexMethodVisitor::EndClass(); |
| 1022 | if (oat_class_index_ == writer_->oat_classes_.size()) { |
| 1023 | DCHECK(result); // OatDexMethodVisitor::EndClass() never fails. |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 1024 | offset_ = writer_->relative_patcher_->WriteThunks(out_, offset_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1025 | if (UNLIKELY(offset_ == 0u)) { |
| 1026 | PLOG(ERROR) << "Failed to write final relative call thunks"; |
| 1027 | result = false; |
| 1028 | } |
| 1029 | } |
| 1030 | return result; |
| 1031 | } |
| 1032 | |
| 1033 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1034 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1035 | OatClass* oat_class = &writer_->oat_classes_[oat_class_index_]; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1036 | const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 1037 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1038 | // 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^] | 1039 | ScopedAssertNoThreadSuspension tsc(__FUNCTION__); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1040 | if (compiled_method != nullptr) { // ie. not an abstract method |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1041 | size_t file_offset = file_offset_; |
| 1042 | OutputStream* out = out_; |
| 1043 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 1044 | ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode(); |
| 1045 | uint32_t code_size = quick_code.size() * sizeof(uint8_t); |
Nicolas Geoffray | f075879 | 2015-07-13 11:56:00 +0000 | [diff] [blame] | 1046 | |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 1047 | // Deduplicate code arrays. |
| 1048 | const OatMethodOffsets& method_offsets = oat_class->method_offsets_[method_offsets_index_]; |
| 1049 | if (method_offsets.code_offset_ > offset_) { |
| 1050 | offset_ = writer_->relative_patcher_->WriteThunks(out, offset_); |
| 1051 | if (offset_ == 0u) { |
| 1052 | ReportWriteFailure("relative call thunk", it); |
| 1053 | return false; |
Nicolas Geoffray | f075879 | 2015-07-13 11:56:00 +0000 | [diff] [blame] | 1054 | } |
Vladimir Marko | 0c737df | 2016-08-01 16:33:16 +0100 | [diff] [blame] | 1055 | uint32_t alignment_size = CodeAlignmentSize(offset_, *compiled_method); |
| 1056 | if (alignment_size != 0) { |
| 1057 | if (!writer_->WriteCodeAlignment(out, alignment_size)) { |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 1058 | ReportWriteFailure("code alignment padding", it); |
| 1059 | return false; |
| 1060 | } |
Vladimir Marko | 0c737df | 2016-08-01 16:33:16 +0100 | [diff] [blame] | 1061 | offset_ += alignment_size; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 1062 | DCHECK_OFFSET_(); |
| 1063 | } |
Vladimir Marko | 0c737df | 2016-08-01 16:33:16 +0100 | [diff] [blame] | 1064 | DCHECK_ALIGNED_PARAM(offset_ + sizeof(OatQuickMethodHeader), |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 1065 | GetInstructionSetAlignment(compiled_method->GetInstructionSet())); |
| 1066 | DCHECK_EQ(method_offsets.code_offset_, |
| 1067 | offset_ + sizeof(OatQuickMethodHeader) + compiled_method->CodeDelta()) |
| 1068 | << PrettyMethod(it.GetMemberIndex(), *dex_file_); |
| 1069 | const OatQuickMethodHeader& method_header = |
| 1070 | oat_class->method_headers_[method_offsets_index_]; |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 1071 | if (!out->WriteFully(&method_header, sizeof(method_header))) { |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 1072 | ReportWriteFailure("method header", it); |
| 1073 | return false; |
| 1074 | } |
| 1075 | writer_->size_method_header_ += sizeof(method_header); |
| 1076 | offset_ += sizeof(method_header); |
Nicolas Geoffray | ed6195a | 2015-07-13 17:02:30 +0000 | [diff] [blame] | 1077 | DCHECK_OFFSET_(); |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 1078 | |
| 1079 | if (!compiled_method->GetPatches().empty()) { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 1080 | patched_code_.assign(quick_code.begin(), quick_code.end()); |
| 1081 | quick_code = ArrayRef<const uint8_t>(patched_code_); |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 1082 | for (const LinkerPatch& patch : compiled_method->GetPatches()) { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1083 | uint32_t literal_offset = patch.LiteralOffset(); |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 1084 | switch (patch.GetType()) { |
| 1085 | case LinkerPatch::Type::kCallRelative: { |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1086 | // NOTE: Relative calls across oat files are not supported. |
| 1087 | uint32_t target_offset = GetTargetOffset(patch); |
| 1088 | writer_->relative_patcher_->PatchCall(&patched_code_, |
| 1089 | literal_offset, |
| 1090 | offset_ + literal_offset, |
| 1091 | target_offset); |
| 1092 | break; |
| 1093 | } |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 1094 | case LinkerPatch::Type::kDexCacheArray: { |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1095 | uint32_t target_offset = GetDexCacheOffset(patch); |
| 1096 | writer_->relative_patcher_->PatchPcRelativeReference(&patched_code_, |
| 1097 | patch, |
| 1098 | offset_ + literal_offset, |
| 1099 | target_offset); |
| 1100 | break; |
| 1101 | } |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 1102 | case LinkerPatch::Type::kStringRelative: { |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1103 | uint32_t target_offset = GetTargetObjectOffset(GetTargetString(patch)); |
| 1104 | writer_->relative_patcher_->PatchPcRelativeReference(&patched_code_, |
| 1105 | patch, |
| 1106 | offset_ + literal_offset, |
| 1107 | target_offset); |
| 1108 | break; |
| 1109 | } |
Vladimir Marko | dbb7f5b | 2016-03-30 13:23:58 +0100 | [diff] [blame] | 1110 | case LinkerPatch::Type::kTypeRelative: { |
| 1111 | uint32_t target_offset = GetTargetObjectOffset(GetTargetType(patch)); |
| 1112 | writer_->relative_patcher_->PatchPcRelativeReference(&patched_code_, |
| 1113 | patch, |
| 1114 | offset_ + literal_offset, |
| 1115 | target_offset); |
| 1116 | break; |
| 1117 | } |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 1118 | case LinkerPatch::Type::kCall: { |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1119 | uint32_t target_offset = GetTargetOffset(patch); |
| 1120 | PatchCodeAddress(&patched_code_, literal_offset, target_offset); |
| 1121 | break; |
| 1122 | } |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 1123 | case LinkerPatch::Type::kMethod: { |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1124 | ArtMethod* method = GetTargetMethod(patch); |
| 1125 | PatchMethodAddress(&patched_code_, literal_offset, method); |
| 1126 | break; |
| 1127 | } |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 1128 | case LinkerPatch::Type::kString: { |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1129 | mirror::String* string = GetTargetString(patch); |
| 1130 | PatchObjectAddress(&patched_code_, literal_offset, string); |
| 1131 | break; |
| 1132 | } |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 1133 | case LinkerPatch::Type::kType: { |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1134 | mirror::Class* type = GetTargetType(patch); |
| 1135 | PatchObjectAddress(&patched_code_, literal_offset, type); |
| 1136 | break; |
| 1137 | } |
| 1138 | default: { |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 1139 | DCHECK_EQ(patch.GetType(), LinkerPatch::Type::kRecordPosition); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1140 | break; |
| 1141 | } |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 1146 | if (!out->WriteFully(quick_code.data(), code_size)) { |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 1147 | ReportWriteFailure("method code", it); |
| 1148 | return false; |
| 1149 | } |
| 1150 | writer_->size_code_ += code_size; |
| 1151 | offset_ += code_size; |
Nicolas Geoffray | f075879 | 2015-07-13 11:56:00 +0000 | [diff] [blame] | 1152 | } |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 1153 | DCHECK_OFFSET_(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1154 | ++method_offsets_index_; |
| 1155 | } |
| 1156 | |
| 1157 | return true; |
| 1158 | } |
| 1159 | |
| 1160 | private: |
| 1161 | OutputStream* const out_; |
Vladimir Marko | 7c2ad5a | 2014-09-24 12:42:55 +0100 | [diff] [blame] | 1162 | const size_t file_offset_; |
| 1163 | const ScopedObjectAccess soa_; |
| 1164 | const ScopedAssertNoThreadSuspension no_thread_suspension_; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1165 | ClassLinker* const class_linker_; |
| 1166 | mirror::DexCache* dex_cache_; |
| 1167 | std::vector<uint8_t> patched_code_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1168 | |
| 1169 | void ReportWriteFailure(const char* what, const ClassDataItemIterator& it) { |
| 1170 | PLOG(ERROR) << "Failed to write " << what << " for " |
| 1171 | << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation(); |
| 1172 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1173 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1174 | ArtMethod* GetTargetMethod(const LinkerPatch& patch) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1175 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1176 | MethodReference ref = patch.TargetMethod(); |
| 1177 | mirror::DexCache* dex_cache = |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1178 | (dex_file_ == ref.dex_file) ? dex_cache_ : class_linker_->FindDexCache( |
| 1179 | Thread::Current(), *ref.dex_file); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1180 | ArtMethod* method = dex_cache->GetResolvedMethod( |
| 1181 | ref.dex_method_index, class_linker_->GetImagePointerSize()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1182 | CHECK(method != nullptr); |
| 1183 | return method; |
| 1184 | } |
| 1185 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1186 | uint32_t GetTargetOffset(const LinkerPatch& patch) REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1187 | uint32_t target_offset = writer_->relative_patcher_->GetOffset(patch.TargetMethod()); |
| 1188 | // If there's no new compiled code, either we're compiling an app and the target method |
| 1189 | // 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] | 1190 | if (UNLIKELY(target_offset == 0)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1191 | ArtMethod* target = GetTargetMethod(patch); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1192 | DCHECK(target != nullptr); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1193 | PointerSize size = |
| 1194 | GetInstructionSetPointerSize(writer_->compiler_driver_->GetInstructionSet()); |
Jeff Hao | a0acc2d | 2015-01-27 11:22:04 -0800 | [diff] [blame] | 1195 | const void* oat_code_offset = target->GetEntryPointFromQuickCompiledCodePtrSize(size); |
| 1196 | if (oat_code_offset != 0) { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1197 | DCHECK(!writer_->HasBootImage()); |
Jeff Hao | a0acc2d | 2015-01-27 11:22:04 -0800 | [diff] [blame] | 1198 | DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(oat_code_offset)); |
| 1199 | DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(oat_code_offset)); |
| 1200 | DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(oat_code_offset)); |
| 1201 | target_offset = PointerToLowMemUInt32(oat_code_offset); |
| 1202 | } else { |
| 1203 | target_offset = target->IsNative() |
| 1204 | ? writer_->oat_header_->GetQuickGenericJniTrampolineOffset() |
| 1205 | : writer_->oat_header_->GetQuickToInterpreterBridgeOffset(); |
| 1206 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1207 | } |
| 1208 | return target_offset; |
| 1209 | } |
| 1210 | |
Vladimir Marko | 052164a | 2016-04-27 13:54:18 +0100 | [diff] [blame] | 1211 | mirror::DexCache* GetDexCache(const DexFile* target_dex_file) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1212 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 052164a | 2016-04-27 13:54:18 +0100 | [diff] [blame] | 1213 | return (target_dex_file == dex_file_) |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1214 | ? dex_cache_ |
Vladimir Marko | 052164a | 2016-04-27 13:54:18 +0100 | [diff] [blame] | 1215 | : class_linker_->FindDexCache(Thread::Current(), *target_dex_file); |
| 1216 | } |
| 1217 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1218 | mirror::Class* GetTargetType(const LinkerPatch& patch) REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 052164a | 2016-04-27 13:54:18 +0100 | [diff] [blame] | 1219 | mirror::DexCache* dex_cache = GetDexCache(patch.TargetTypeDexFile()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1220 | mirror::Class* type = dex_cache->GetResolvedType(patch.TargetTypeIndex()); |
| 1221 | CHECK(type != nullptr); |
| 1222 | return type; |
| 1223 | } |
| 1224 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1225 | mirror::String* GetTargetString(const LinkerPatch& patch) REQUIRES_SHARED(Locks::mutator_lock_) { |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 1226 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1227 | StackHandleScope<1> hs(soa.Self()); |
| 1228 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 1229 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(GetDexCache(patch.TargetStringDexFile()))); |
| 1230 | mirror::String* string = linker->LookupString(*patch.TargetStringDexFile(), |
| 1231 | patch.TargetStringIndex(), |
| 1232 | dex_cache); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1233 | DCHECK(string != nullptr); |
| 1234 | DCHECK(writer_->HasBootImage() || |
| 1235 | Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(string)); |
| 1236 | return string; |
| 1237 | } |
| 1238 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1239 | uint32_t GetDexCacheOffset(const LinkerPatch& patch) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1240 | if (writer_->HasBootImage()) { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1241 | uintptr_t element = writer_->image_writer_->GetDexCacheArrayElementImageAddress<uintptr_t>( |
| 1242 | patch.TargetDexCacheDexFile(), patch.TargetDexCacheElementOffset()); |
| 1243 | size_t oat_index = writer_->image_writer_->GetOatIndexForDexCache(dex_cache_); |
| 1244 | uintptr_t oat_data = writer_->image_writer_->GetOatDataBegin(oat_index); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1245 | return element - oat_data; |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 1246 | } else { |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 1247 | size_t start = writer_->dex_cache_arrays_offsets_.Get(patch.TargetDexCacheDexFile()); |
| 1248 | return start + patch.TargetDexCacheElementOffset(); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 1249 | } |
| 1250 | } |
| 1251 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1252 | uint32_t GetTargetObjectOffset(mirror::Object* object) REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 1253 | DCHECK(writer_->HasBootImage()); |
| 1254 | object = writer_->image_writer_->GetImageAddress(object); |
| 1255 | size_t oat_index = writer_->image_writer_->GetOatIndexForDexFile(dex_file_); |
| 1256 | uintptr_t oat_data_begin = writer_->image_writer_->GetOatDataBegin(oat_index); |
| 1257 | // TODO: Clean up offset types. The target offset must be treated as signed. |
| 1258 | return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(object) - oat_data_begin); |
| 1259 | } |
| 1260 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1261 | 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] | 1262 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1263 | if (writer_->HasBootImage()) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1264 | object = writer_->image_writer_->GetImageAddress(object); |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 1265 | } else { |
| 1266 | // NOTE: We're using linker patches for app->boot references when the image can |
| 1267 | // be relocated and therefore we need to emit .oat_patches. We're not using this |
| 1268 | // for app->app references, so check that the object is in the image space. |
| 1269 | DCHECK(Runtime::Current()->GetHeap()->FindSpaceFromObject(object, false)->IsImageSpace()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1270 | } |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 1271 | // 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] | 1272 | uint32_t address = PointerToLowMemUInt32(object); |
| 1273 | DCHECK_LE(offset + 4, code->size()); |
| 1274 | uint8_t* data = &(*code)[offset]; |
| 1275 | data[0] = address & 0xffu; |
| 1276 | data[1] = (address >> 8) & 0xffu; |
| 1277 | data[2] = (address >> 16) & 0xffu; |
| 1278 | data[3] = (address >> 24) & 0xffu; |
| 1279 | } |
| 1280 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1281 | void PatchMethodAddress(std::vector<uint8_t>* code, uint32_t offset, ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1282 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1283 | if (writer_->HasBootImage()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1284 | method = writer_->image_writer_->GetImageMethodAddress(method); |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 1285 | } else if (kIsDebugBuild) { |
| 1286 | // NOTE: We're using linker patches for app->boot references when the image can |
| 1287 | // be relocated and therefore we need to emit .oat_patches. We're not using this |
| 1288 | // for app->app references, so check that the method is an image method. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1289 | std::vector<gc::space::ImageSpace*> image_spaces = |
| 1290 | Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
| 1291 | bool contains_method = false; |
| 1292 | for (gc::space::ImageSpace* image_space : image_spaces) { |
| 1293 | size_t method_offset = reinterpret_cast<const uint8_t*>(method) - image_space->Begin(); |
| 1294 | contains_method |= |
| 1295 | image_space->GetImageHeader().GetMethodsSection().Contains(method_offset); |
| 1296 | } |
| 1297 | CHECK(contains_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1298 | } |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 1299 | // Note: We only patch targeting ArtMethods in image which is in the low 4gb. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1300 | uint32_t address = PointerToLowMemUInt32(method); |
| 1301 | DCHECK_LE(offset + 4, code->size()); |
| 1302 | uint8_t* data = &(*code)[offset]; |
| 1303 | data[0] = address & 0xffu; |
| 1304 | data[1] = (address >> 8) & 0xffu; |
| 1305 | data[2] = (address >> 16) & 0xffu; |
| 1306 | data[3] = (address >> 24) & 0xffu; |
| 1307 | } |
| 1308 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1309 | void PatchCodeAddress(std::vector<uint8_t>* code, uint32_t offset, uint32_t target_offset) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1310 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 1311 | uint32_t address = target_offset; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1312 | if (writer_->HasBootImage()) { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1313 | size_t oat_index = writer_->image_writer_->GetOatIndexForDexCache(dex_cache_); |
| 1314 | // TODO: Clean up offset types. |
| 1315 | // The target_offset must be treated as signed for cross-oat patching. |
| 1316 | const void* target = reinterpret_cast<const void*>( |
| 1317 | writer_->image_writer_->GetOatDataBegin(oat_index) + |
| 1318 | static_cast<int32_t>(target_offset)); |
| 1319 | address = PointerToLowMemUInt32(target); |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 1320 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1321 | DCHECK_LE(offset + 4, code->size()); |
| 1322 | uint8_t* data = &(*code)[offset]; |
| 1323 | data[0] = address & 0xffu; |
| 1324 | data[1] = (address >> 8) & 0xffu; |
| 1325 | data[2] = (address >> 16) & 0xffu; |
| 1326 | data[3] = (address >> 24) & 0xffu; |
| 1327 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1328 | }; |
| 1329 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1330 | class OatWriter::WriteMapMethodVisitor : public OatDexMethodVisitor { |
| 1331 | public: |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 1332 | WriteMapMethodVisitor(OatWriter* writer, |
| 1333 | OutputStream* out, |
| 1334 | const size_t file_offset, |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 1335 | size_t relative_offset) |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1336 | : OatDexMethodVisitor(writer, relative_offset), |
| 1337 | out_(out), |
| 1338 | file_offset_(file_offset) { |
| 1339 | } |
| 1340 | |
| 1341 | bool VisitMethod(size_t class_def_method_index, const ClassDataItemIterator& it) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1342 | OatClass* oat_class = &writer_->oat_classes_[oat_class_index_]; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1343 | const CompiledMethod* compiled_method = oat_class->GetCompiledMethod(class_def_method_index); |
| 1344 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1345 | if (compiled_method != nullptr) { // ie. not an abstract method |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1346 | size_t file_offset = file_offset_; |
| 1347 | OutputStream* out = out_; |
| 1348 | |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 1349 | uint32_t map_offset = oat_class->method_headers_[method_offsets_index_].vmap_table_offset_; |
| 1350 | uint32_t code_offset = oat_class->method_offsets_[method_offsets_index_].code_offset_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1351 | ++method_offsets_index_; |
| 1352 | |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 1353 | DCHECK((compiled_method->GetVmapTable().size() == 0u && map_offset == 0u) || |
| 1354 | (compiled_method->GetVmapTable().size() != 0u && map_offset != 0u)) |
| 1355 | << compiled_method->GetVmapTable().size() << " " << map_offset << " " |
| 1356 | << PrettyMethod(it.GetMemberIndex(), *dex_file_); |
| 1357 | |
| 1358 | if (map_offset != 0u) { |
| 1359 | // Transform map_offset to actual oat data offset. |
| 1360 | map_offset = (code_offset - compiled_method->CodeDelta()) - map_offset; |
| 1361 | DCHECK_NE(map_offset, 0u); |
| 1362 | DCHECK_LE(map_offset, offset_) << PrettyMethod(it.GetMemberIndex(), *dex_file_); |
| 1363 | |
| 1364 | ArrayRef<const uint8_t> map = compiled_method->GetVmapTable(); |
| 1365 | size_t map_size = map.size() * sizeof(map[0]); |
| 1366 | if (map_offset == offset_) { |
| 1367 | // Write deduplicated map (code info for Optimizing or transformation info for dex2dex). |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 1368 | if (UNLIKELY(!out->WriteFully(map.data(), map_size))) { |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 1369 | ReportWriteFailure(it); |
| 1370 | return false; |
| 1371 | } |
| 1372 | offset_ += map_size; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1373 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1374 | } |
| 1375 | DCHECK_OFFSET_(); |
| 1376 | } |
| 1377 | |
| 1378 | return true; |
| 1379 | } |
| 1380 | |
| 1381 | private: |
| 1382 | OutputStream* const out_; |
| 1383 | size_t const file_offset_; |
| 1384 | |
| 1385 | void ReportWriteFailure(const ClassDataItemIterator& it) { |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 1386 | PLOG(ERROR) << "Failed to write map for " |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1387 | << PrettyMethod(it.GetMemberIndex(), *dex_file_) << " to " << out_->GetLocation(); |
| 1388 | } |
| 1389 | }; |
| 1390 | |
| 1391 | // Visit all methods from all classes in all dex files with the specified visitor. |
| 1392 | bool OatWriter::VisitDexMethods(DexMethodVisitor* visitor) { |
| 1393 | for (const DexFile* dex_file : *dex_files_) { |
| 1394 | const size_t class_def_count = dex_file->NumClassDefs(); |
| 1395 | for (size_t class_def_index = 0; class_def_index != class_def_count; ++class_def_index) { |
| 1396 | if (UNLIKELY(!visitor->StartClass(dex_file, class_def_index))) { |
| 1397 | return false; |
| 1398 | } |
| 1399 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1400 | const uint8_t* class_data = dex_file->GetClassData(class_def); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1401 | if (class_data != nullptr) { // ie not an empty class, such as a marker interface |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1402 | ClassDataItemIterator it(*dex_file, class_data); |
| 1403 | while (it.HasNextStaticField()) { |
| 1404 | it.Next(); |
| 1405 | } |
| 1406 | while (it.HasNextInstanceField()) { |
| 1407 | it.Next(); |
| 1408 | } |
| 1409 | size_t class_def_method_index = 0u; |
| 1410 | while (it.HasNextDirectMethod()) { |
| 1411 | if (!visitor->VisitMethod(class_def_method_index, it)) { |
| 1412 | return false; |
| 1413 | } |
| 1414 | ++class_def_method_index; |
| 1415 | it.Next(); |
| 1416 | } |
| 1417 | while (it.HasNextVirtualMethod()) { |
| 1418 | if (UNLIKELY(!visitor->VisitMethod(class_def_method_index, it))) { |
| 1419 | return false; |
| 1420 | } |
| 1421 | ++class_def_method_index; |
| 1422 | it.Next(); |
| 1423 | } |
| 1424 | } |
| 1425 | if (UNLIKELY(!visitor->EndClass())) { |
| 1426 | return false; |
| 1427 | } |
| 1428 | } |
| 1429 | } |
| 1430 | return true; |
| 1431 | } |
| 1432 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1433 | size_t OatWriter::InitOatHeader(InstructionSet instruction_set, |
| 1434 | const InstructionSetFeatures* instruction_set_features, |
| 1435 | uint32_t num_dex_files, |
| 1436 | SafeMap<std::string, std::string>* key_value_store) { |
| 1437 | TimingLogger::ScopedTiming split("InitOatHeader", timings_); |
| 1438 | oat_header_.reset(OatHeader::Create(instruction_set, |
| 1439 | instruction_set_features, |
| 1440 | num_dex_files, |
| 1441 | key_value_store)); |
| 1442 | size_oat_header_ += sizeof(OatHeader); |
| 1443 | size_oat_header_key_value_store_ += oat_header_->GetHeaderSize() - sizeof(OatHeader); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1444 | return oat_header_->GetHeaderSize(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | size_t OatWriter::InitOatDexFiles(size_t offset) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1448 | TimingLogger::ScopedTiming split("InitOatDexFiles", timings_); |
| 1449 | // Initialize offsets of dex files. |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1450 | for (OatDexFile& oat_dex_file : oat_dex_files_) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1451 | oat_dex_file.offset_ = offset; |
| 1452 | offset += oat_dex_file.SizeOf(); |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 1453 | } |
| 1454 | return offset; |
| 1455 | } |
| 1456 | |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 1457 | size_t OatWriter::InitOatClasses(size_t offset) { |
Brian Carlstrom | 389efb0 | 2012-01-11 12:06:26 -0800 | [diff] [blame] | 1458 | // calculate the offsets within OatDexFiles to OatClasses |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1459 | InitOatClassesMethodVisitor visitor(this, offset); |
| 1460 | bool success = VisitDexMethods(&visitor); |
| 1461 | CHECK(success); |
| 1462 | offset = visitor.GetOffset(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1463 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1464 | // Update oat_dex_files_. |
| 1465 | auto oat_class_it = oat_classes_.begin(); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1466 | for (OatDexFile& oat_dex_file : oat_dex_files_) { |
| 1467 | for (uint32_t& class_offset : oat_dex_file.class_offsets_) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1468 | DCHECK(oat_class_it != oat_classes_.end()); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1469 | class_offset = oat_class_it->offset_; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1470 | ++oat_class_it; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1471 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1472 | } |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1473 | CHECK(oat_class_it == oat_classes_.end()); |
| 1474 | |
| 1475 | return offset; |
| 1476 | } |
| 1477 | |
| 1478 | size_t OatWriter::InitOatMaps(size_t offset) { |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 1479 | InitMapMethodVisitor visitor(this, offset); |
| 1480 | bool success = VisitDexMethods(&visitor); |
| 1481 | DCHECK(success); |
| 1482 | offset = visitor.GetOffset(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1483 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1484 | return offset; |
| 1485 | } |
| 1486 | |
| 1487 | size_t OatWriter::InitOatCode(size_t offset) { |
| 1488 | // calculate the offsets within OatHeader to executable code |
| 1489 | size_t old_offset = offset; |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 1490 | size_t adjusted_offset = offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1491 | // required to be on a new page boundary |
| 1492 | offset = RoundUp(offset, kPageSize); |
| 1493 | oat_header_->SetExecutableOffset(offset); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1494 | size_executable_offset_alignment_ = offset - old_offset; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1495 | if (compiler_driver_->IsBootImage()) { |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1496 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1497 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1498 | #define DO_TRAMPOLINE(field, fn_name) \ |
| 1499 | offset = CompiledCode::AlignCode(offset, instruction_set); \ |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 1500 | adjusted_offset = offset + CompiledCode::CodeDelta(instruction_set); \ |
| 1501 | oat_header_->Set ## fn_name ## Offset(adjusted_offset); \ |
Chih-Hung Hsieh | fba3997 | 2016-05-11 11:26:48 -0700 | [diff] [blame] | 1502 | (field) = compiler_driver_->Create ## fn_name(); \ |
| 1503 | offset += (field)->size(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1504 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1505 | DO_TRAMPOLINE(jni_dlsym_lookup_, JniDlsymLookup); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1506 | DO_TRAMPOLINE(quick_generic_jni_trampoline_, QuickGenericJniTrampoline); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1507 | DO_TRAMPOLINE(quick_imt_conflict_trampoline_, QuickImtConflictTrampoline); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1508 | DO_TRAMPOLINE(quick_resolution_trampoline_, QuickResolutionTrampoline); |
| 1509 | DO_TRAMPOLINE(quick_to_interpreter_bridge_, QuickToInterpreterBridge); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1510 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1511 | #undef DO_TRAMPOLINE |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1512 | } else { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1513 | oat_header_->SetInterpreterToInterpreterBridgeOffset(0); |
| 1514 | oat_header_->SetInterpreterToCompiledCodeBridgeOffset(0); |
| 1515 | oat_header_->SetJniDlsymLookupOffset(0); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1516 | oat_header_->SetQuickGenericJniTrampolineOffset(0); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1517 | oat_header_->SetQuickImtConflictTrampolineOffset(0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1518 | oat_header_->SetQuickResolutionTrampolineOffset(0); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1519 | oat_header_->SetQuickToInterpreterBridgeOffset(0); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1520 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1521 | return offset; |
| 1522 | } |
| 1523 | |
| 1524 | size_t OatWriter::InitOatCodeDexFiles(size_t offset) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1525 | #define VISIT(VisitorType) \ |
| 1526 | do { \ |
| 1527 | VisitorType visitor(this, offset); \ |
| 1528 | bool success = VisitDexMethods(&visitor); \ |
| 1529 | DCHECK(success); \ |
| 1530 | offset = visitor.GetOffset(); \ |
| 1531 | } while (false) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1532 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1533 | VISIT(InitCodeMethodVisitor); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1534 | if (HasImage()) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1535 | VISIT(InitImageMethodVisitor); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1536 | } |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 1537 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1538 | #undef VISIT |
| 1539 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1540 | return offset; |
| 1541 | } |
| 1542 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 1543 | bool OatWriter::WriteRodata(OutputStream* out) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1544 | CHECK(write_state_ == WriteState::kWriteRoData); |
| 1545 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 1546 | // Wrap out to update checksum with each write. |
| 1547 | ChecksumUpdatingOutputStream checksum_updating_out(out, oat_header_.get()); |
| 1548 | out = &checksum_updating_out; |
| 1549 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1550 | if (!WriteClassOffsets(out)) { |
| 1551 | LOG(ERROR) << "Failed to write class offsets to " << out->GetLocation(); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1552 | return false; |
| 1553 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1554 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1555 | if (!WriteClasses(out)) { |
| 1556 | LOG(ERROR) << "Failed to write classes to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1557 | return false; |
| 1558 | } |
| 1559 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1560 | off_t tables_end_offset = out->Seek(0, kSeekCurrent); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1561 | if (tables_end_offset == static_cast<off_t>(-1)) { |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 1562 | LOG(ERROR) << "Failed to get oat code position in " << out->GetLocation(); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1563 | return false; |
| 1564 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1565 | size_t file_offset = oat_data_offset_; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1566 | size_t relative_offset = static_cast<size_t>(tables_end_offset) - file_offset; |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1567 | relative_offset = WriteMaps(out, file_offset, relative_offset); |
| 1568 | if (relative_offset == 0) { |
| 1569 | LOG(ERROR) << "Failed to write oat code to " << out->GetLocation(); |
| 1570 | return false; |
| 1571 | } |
| 1572 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 1573 | // Write padding. |
| 1574 | off_t new_offset = out->Seek(size_executable_offset_alignment_, kSeekCurrent); |
| 1575 | relative_offset += size_executable_offset_alignment_; |
| 1576 | DCHECK_EQ(relative_offset, oat_header_->GetExecutableOffset()); |
| 1577 | size_t expected_file_offset = file_offset + relative_offset; |
| 1578 | if (static_cast<uint32_t>(new_offset) != expected_file_offset) { |
| 1579 | PLOG(ERROR) << "Failed to seek to oat code section. Actual: " << new_offset |
| 1580 | << " Expected: " << expected_file_offset << " File: " << out->GetLocation(); |
| 1581 | return 0; |
| 1582 | } |
| 1583 | DCHECK_OFFSET(); |
| 1584 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1585 | write_state_ = WriteState::kWriteText; |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 1586 | return true; |
| 1587 | } |
| 1588 | |
| 1589 | bool OatWriter::WriteCode(OutputStream* out) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1590 | CHECK(write_state_ == WriteState::kWriteText); |
| 1591 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 1592 | // Wrap out to update checksum with each write. |
| 1593 | ChecksumUpdatingOutputStream checksum_updating_out(out, oat_header_.get()); |
| 1594 | out = &checksum_updating_out; |
| 1595 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1596 | SetMultiOatRelativePatcherAdjustment(); |
| 1597 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 1598 | const size_t file_offset = oat_data_offset_; |
| 1599 | size_t relative_offset = oat_header_->GetExecutableOffset(); |
| 1600 | DCHECK_OFFSET(); |
| 1601 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1602 | relative_offset = WriteCode(out, file_offset, relative_offset); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1603 | if (relative_offset == 0) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1604 | LOG(ERROR) << "Failed to write oat code to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1605 | return false; |
| 1606 | } |
| 1607 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1608 | relative_offset = WriteCodeDexFiles(out, file_offset, relative_offset); |
| 1609 | if (relative_offset == 0) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1610 | 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] | 1611 | return false; |
| 1612 | } |
| 1613 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1614 | const off_t oat_end_file_offset = out->Seek(0, kSeekCurrent); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1615 | if (oat_end_file_offset == static_cast<off_t>(-1)) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1616 | LOG(ERROR) << "Failed to get oat end file offset in " << out->GetLocation(); |
| 1617 | return false; |
| 1618 | } |
| 1619 | |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1620 | if (kIsDebugBuild) { |
| 1621 | uint32_t size_total = 0; |
| 1622 | #define DO_STAT(x) \ |
Chih-Hung Hsieh | fba3997 | 2016-05-11 11:26:48 -0700 | [diff] [blame] | 1623 | VLOG(compiler) << #x "=" << PrettySize(x) << " (" << (x) << "B)"; \ |
| 1624 | size_total += (x); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1625 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1626 | DO_STAT(size_vdex_header_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1627 | DO_STAT(size_dex_file_alignment_); |
| 1628 | DO_STAT(size_executable_offset_alignment_); |
| 1629 | DO_STAT(size_oat_header_); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1630 | DO_STAT(size_oat_header_key_value_store_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1631 | DO_STAT(size_dex_file_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1632 | DO_STAT(size_interpreter_to_interpreter_bridge_); |
| 1633 | DO_STAT(size_interpreter_to_compiled_code_bridge_); |
| 1634 | DO_STAT(size_jni_dlsym_lookup_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1635 | DO_STAT(size_quick_generic_jni_trampoline_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1636 | DO_STAT(size_quick_imt_conflict_trampoline_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1637 | DO_STAT(size_quick_resolution_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1638 | DO_STAT(size_quick_to_interpreter_bridge_); |
| 1639 | DO_STAT(size_trampoline_alignment_); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1640 | DO_STAT(size_method_header_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1641 | DO_STAT(size_code_); |
| 1642 | DO_STAT(size_code_alignment_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1643 | DO_STAT(size_relative_call_thunks_); |
Vladimir Marko | c74658b | 2015-03-31 10:26:41 +0100 | [diff] [blame] | 1644 | DO_STAT(size_misc_thunks_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1645 | DO_STAT(size_vmap_table_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1646 | DO_STAT(size_oat_dex_file_location_size_); |
| 1647 | DO_STAT(size_oat_dex_file_location_data_); |
| 1648 | DO_STAT(size_oat_dex_file_location_checksum_); |
| 1649 | DO_STAT(size_oat_dex_file_offset_); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1650 | DO_STAT(size_oat_dex_file_class_offsets_offset_); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1651 | DO_STAT(size_oat_dex_file_lookup_table_offset_); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1652 | DO_STAT(size_oat_lookup_table_alignment_); |
| 1653 | DO_STAT(size_oat_lookup_table_); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1654 | DO_STAT(size_oat_class_offsets_alignment_); |
| 1655 | DO_STAT(size_oat_class_offsets_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1656 | DO_STAT(size_oat_class_type_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1657 | DO_STAT(size_oat_class_status_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 1658 | DO_STAT(size_oat_class_method_bitmaps_); |
Ian Rogers | 4bdbbc8 | 2013-06-10 16:02:31 -0700 | [diff] [blame] | 1659 | DO_STAT(size_oat_class_method_offsets_); |
| 1660 | #undef DO_STAT |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1661 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1662 | VLOG(compiler) << "size_total=" << PrettySize(size_total) << " (" << size_total << "B)"; |
| 1663 | |
| 1664 | CHECK_EQ(vdex_size_ + oat_size_, size_total); |
| 1665 | 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] | 1666 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1667 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1668 | CHECK_EQ(file_offset + oat_size_, static_cast<size_t>(oat_end_file_offset)); |
| 1669 | CHECK_EQ(oat_size_, relative_offset); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1670 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1671 | write_state_ = WriteState::kWriteHeader; |
| 1672 | return true; |
| 1673 | } |
| 1674 | |
| 1675 | bool OatWriter::WriteHeader(OutputStream* out, |
| 1676 | uint32_t image_file_location_oat_checksum, |
| 1677 | uintptr_t image_file_location_oat_begin, |
| 1678 | int32_t image_patch_delta) { |
| 1679 | CHECK(write_state_ == WriteState::kWriteHeader); |
| 1680 | |
| 1681 | oat_header_->SetImageFileLocationOatChecksum(image_file_location_oat_checksum); |
| 1682 | oat_header_->SetImageFileLocationOatDataBegin(image_file_location_oat_begin); |
| 1683 | if (compiler_driver_->IsBootImage()) { |
| 1684 | CHECK_EQ(image_patch_delta, 0); |
| 1685 | CHECK_EQ(oat_header_->GetImagePatchDelta(), 0); |
| 1686 | } else { |
| 1687 | CHECK_ALIGNED(image_patch_delta, kPageSize); |
| 1688 | oat_header_->SetImagePatchDelta(image_patch_delta); |
| 1689 | } |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1690 | oat_header_->UpdateChecksumWithHeaderData(); |
| 1691 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1692 | const size_t file_offset = oat_data_offset_; |
| 1693 | |
| 1694 | off_t current_offset = out->Seek(0, kSeekCurrent); |
| 1695 | if (current_offset == static_cast<off_t>(-1)) { |
| 1696 | PLOG(ERROR) << "Failed to get current offset from " << out->GetLocation(); |
| 1697 | return false; |
| 1698 | } |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1699 | if (out->Seek(file_offset, kSeekSet) == static_cast<off_t>(-1)) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1700 | PLOG(ERROR) << "Failed to seek to oat header position in " << out->GetLocation(); |
| 1701 | return false; |
| 1702 | } |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 1703 | DCHECK_EQ(file_offset, static_cast<size_t>(out->Seek(0, kSeekCurrent))); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1704 | |
| 1705 | // Flush all other data before writing the header. |
| 1706 | if (!out->Flush()) { |
| 1707 | PLOG(ERROR) << "Failed to flush before writing oat header to " << out->GetLocation(); |
| 1708 | return false; |
| 1709 | } |
| 1710 | // Write the header. |
| 1711 | size_t header_size = oat_header_->GetHeaderSize(); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1712 | if (!out->WriteFully(oat_header_.get(), header_size)) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1713 | PLOG(ERROR) << "Failed to write oat header to " << out->GetLocation(); |
| 1714 | return false; |
| 1715 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1716 | // Flush the header data. |
| 1717 | if (!out->Flush()) { |
| 1718 | PLOG(ERROR) << "Failed to flush after writing oat header to " << out->GetLocation(); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1719 | return false; |
| 1720 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1721 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1722 | if (out->Seek(current_offset, kSeekSet) == static_cast<off_t>(-1)) { |
| 1723 | PLOG(ERROR) << "Failed to seek back after writing oat header to " << out->GetLocation(); |
| 1724 | return false; |
| 1725 | } |
| 1726 | DCHECK_EQ(current_offset, out->Seek(0, kSeekCurrent)); |
| 1727 | |
| 1728 | write_state_ = WriteState::kDone; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1729 | return true; |
| 1730 | } |
| 1731 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1732 | bool OatWriter::WriteClassOffsets(OutputStream* out) { |
| 1733 | for (OatDexFile& oat_dex_file : oat_dex_files_) { |
| 1734 | if (oat_dex_file.class_offsets_offset_ != 0u) { |
| 1735 | uint32_t expected_offset = oat_data_offset_ + oat_dex_file.class_offsets_offset_; |
| 1736 | off_t actual_offset = out->Seek(expected_offset, kSeekSet); |
| 1737 | if (static_cast<uint32_t>(actual_offset) != expected_offset) { |
| 1738 | PLOG(ERROR) << "Failed to seek to oat class offsets section. Actual: " << actual_offset |
| 1739 | << " Expected: " << expected_offset << " File: " << oat_dex_file.GetLocation(); |
Vladimir Marko | 919f553 | 2016-01-20 19:13:01 +0000 | [diff] [blame] | 1740 | return false; |
| 1741 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1742 | if (!oat_dex_file.WriteClassOffsets(this, out)) { |
| 1743 | return false; |
| 1744 | } |
| 1745 | } |
| 1746 | } |
| 1747 | return true; |
| 1748 | } |
| 1749 | |
| 1750 | bool OatWriter::WriteClasses(OutputStream* out) { |
| 1751 | for (OatClass& oat_class : oat_classes_) { |
| 1752 | if (!oat_class.Write(this, out, oat_data_offset_)) { |
| 1753 | PLOG(ERROR) << "Failed to write oat methods information to " << out->GetLocation(); |
| 1754 | return false; |
Vladimir Marko | 919f553 | 2016-01-20 19:13:01 +0000 | [diff] [blame] | 1755 | } |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 1756 | } |
| 1757 | return true; |
| 1758 | } |
| 1759 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1760 | size_t OatWriter::WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1761 | size_t vmap_tables_offset = relative_offset; |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 1762 | WriteMapMethodVisitor visitor(this, out, file_offset, relative_offset); |
| 1763 | if (UNLIKELY(!VisitDexMethods(&visitor))) { |
| 1764 | return 0; |
| 1765 | } |
| 1766 | relative_offset = visitor.GetOffset(); |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1767 | size_vmap_table_ = relative_offset - vmap_tables_offset; |
| 1768 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1769 | return relative_offset; |
| 1770 | } |
| 1771 | |
| 1772 | size_t OatWriter::WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1773 | if (compiler_driver_->IsBootImage()) { |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1774 | InstructionSet instruction_set = compiler_driver_->GetInstructionSet(); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1775 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1776 | #define DO_TRAMPOLINE(field) \ |
| 1777 | do { \ |
| 1778 | uint32_t aligned_offset = CompiledCode::AlignCode(relative_offset, instruction_set); \ |
| 1779 | uint32_t alignment_padding = aligned_offset - relative_offset; \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1780 | out->Seek(alignment_padding, kSeekCurrent); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1781 | size_trampoline_alignment_ += alignment_padding; \ |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 1782 | if (!out->WriteFully((field)->data(), (field)->size())) { \ |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1783 | PLOG(ERROR) << "Failed to write " # field " to " << out->GetLocation(); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1784 | return false; \ |
| 1785 | } \ |
Chih-Hung Hsieh | fba3997 | 2016-05-11 11:26:48 -0700 | [diff] [blame] | 1786 | size_ ## field += (field)->size(); \ |
| 1787 | relative_offset += alignment_padding + (field)->size(); \ |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1788 | DCHECK_OFFSET(); \ |
| 1789 | } while (false) |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1790 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1791 | DO_TRAMPOLINE(jni_dlsym_lookup_); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1792 | DO_TRAMPOLINE(quick_generic_jni_trampoline_); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1793 | DO_TRAMPOLINE(quick_imt_conflict_trampoline_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1794 | DO_TRAMPOLINE(quick_resolution_trampoline_); |
| 1795 | DO_TRAMPOLINE(quick_to_interpreter_bridge_); |
| 1796 | #undef DO_TRAMPOLINE |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1797 | } |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1798 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1799 | } |
| 1800 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 1801 | size_t OatWriter::WriteCodeDexFiles(OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1802 | const size_t file_offset, |
| 1803 | size_t relative_offset) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1804 | #define VISIT(VisitorType) \ |
| 1805 | do { \ |
| 1806 | VisitorType visitor(this, out, file_offset, relative_offset); \ |
| 1807 | if (UNLIKELY(!VisitDexMethods(&visitor))) { \ |
| 1808 | return 0; \ |
| 1809 | } \ |
| 1810 | relative_offset = visitor.GetOffset(); \ |
| 1811 | } while (false) |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1812 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1813 | VISIT(WriteCodeMethodVisitor); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1814 | |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 1815 | #undef VISIT |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1816 | |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 1817 | size_code_alignment_ += relative_patcher_->CodeAlignmentSize(); |
| 1818 | size_relative_call_thunks_ += relative_patcher_->RelativeCallThunksSize(); |
| 1819 | size_misc_thunks_ += relative_patcher_->MiscThunksSize(); |
| 1820 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 1821 | return relative_offset; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1822 | } |
| 1823 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1824 | bool OatWriter::RecordOatDataOffset(OutputStream* out) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1825 | // Get the elf file offset of the oat file. |
| 1826 | const off_t raw_file_offset = out->Seek(0, kSeekCurrent); |
| 1827 | if (raw_file_offset == static_cast<off_t>(-1)) { |
| 1828 | LOG(ERROR) << "Failed to get file offset in " << out->GetLocation(); |
| 1829 | return false; |
| 1830 | } |
| 1831 | oat_data_offset_ = static_cast<size_t>(raw_file_offset); |
| 1832 | return true; |
| 1833 | } |
| 1834 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1835 | bool OatWriter::ReadDexFileHeader(File* file, OatDexFile* oat_dex_file) { |
| 1836 | // Read the dex file header and perform minimal verification. |
| 1837 | uint8_t raw_header[sizeof(DexFile::Header)]; |
| 1838 | if (!file->ReadFully(&raw_header, sizeof(DexFile::Header))) { |
| 1839 | PLOG(ERROR) << "Failed to read dex file header. Actual: " |
| 1840 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 1841 | return false; |
| 1842 | } |
| 1843 | if (!ValidateDexFileHeader(raw_header, oat_dex_file->GetLocation())) { |
| 1844 | return false; |
| 1845 | } |
| 1846 | |
| 1847 | const UnalignedDexFileHeader* header = AsUnalignedDexFileHeader(raw_header); |
| 1848 | oat_dex_file->dex_file_size_ = header->file_size_; |
| 1849 | oat_dex_file->dex_file_location_checksum_ = header->checksum_; |
| 1850 | oat_dex_file->class_offsets_.resize(header->class_defs_size_); |
| 1851 | return true; |
| 1852 | } |
| 1853 | |
| 1854 | bool OatWriter::ValidateDexFileHeader(const uint8_t* raw_header, const char* location) { |
| 1855 | if (!DexFile::IsMagicValid(raw_header)) { |
| 1856 | LOG(ERROR) << "Invalid magic number in dex file header. " << " File: " << location; |
| 1857 | return false; |
| 1858 | } |
| 1859 | if (!DexFile::IsVersionValid(raw_header)) { |
| 1860 | LOG(ERROR) << "Invalid version number in dex file header. " << " File: " << location; |
| 1861 | return false; |
| 1862 | } |
| 1863 | const UnalignedDexFileHeader* header = AsUnalignedDexFileHeader(raw_header); |
| 1864 | if (header->file_size_ < sizeof(DexFile::Header)) { |
| 1865 | LOG(ERROR) << "Dex file header specifies file size insufficient to contain the header." |
| 1866 | << " File: " << location; |
| 1867 | return false; |
| 1868 | } |
| 1869 | return true; |
| 1870 | } |
| 1871 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1872 | bool OatWriter::WriteDexFiles(OutputStream* out, File* file) { |
| 1873 | TimingLogger::ScopedTiming split("Write Dex files", timings_); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1874 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1875 | vdex_dex_files_offset_ = vdex_size_; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1876 | |
| 1877 | // Write dex files. |
| 1878 | for (OatDexFile& oat_dex_file : oat_dex_files_) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1879 | if (!WriteDexFile(out, file, &oat_dex_file)) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1880 | return false; |
| 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | // Close sources. |
| 1885 | for (OatDexFile& oat_dex_file : oat_dex_files_) { |
| 1886 | oat_dex_file.source_.Clear(); // Get rid of the reference, it's about to be invalidated. |
| 1887 | } |
| 1888 | zipped_dex_files_.clear(); |
| 1889 | zip_archives_.clear(); |
| 1890 | raw_dex_files_.clear(); |
| 1891 | return true; |
| 1892 | } |
| 1893 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1894 | bool OatWriter::WriteDexFile(OutputStream* out, File* file, OatDexFile* oat_dex_file) { |
| 1895 | if (!SeekToDexFile(out, file, oat_dex_file)) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1896 | return false; |
| 1897 | } |
| 1898 | if (oat_dex_file->source_.IsZipEntry()) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1899 | if (!WriteDexFile(out, file, oat_dex_file, oat_dex_file->source_.GetZipEntry())) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1900 | return false; |
| 1901 | } |
| 1902 | } else if (oat_dex_file->source_.IsRawFile()) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1903 | if (!WriteDexFile(out, file, oat_dex_file, oat_dex_file->source_.GetRawFile())) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1904 | return false; |
| 1905 | } |
| 1906 | } else { |
| 1907 | DCHECK(oat_dex_file->source_.IsRawData()); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1908 | if (!WriteDexFile(out, oat_dex_file, oat_dex_file->source_.GetRawData())) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1909 | return false; |
| 1910 | } |
| 1911 | } |
| 1912 | |
| 1913 | // Update current size and account for the written data. |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1914 | if (kIsVdexEnabled) { |
| 1915 | DCHECK_EQ(vdex_size_, oat_dex_file->dex_file_offset_); |
| 1916 | vdex_size_ += oat_dex_file->dex_file_size_; |
| 1917 | } else { |
| 1918 | DCHECK_EQ(oat_size_, oat_dex_file->dex_file_offset_); |
| 1919 | oat_size_ += oat_dex_file->dex_file_size_; |
| 1920 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1921 | size_dex_file_ += oat_dex_file->dex_file_size_; |
| 1922 | return true; |
| 1923 | } |
| 1924 | |
| 1925 | bool OatWriter::SeekToDexFile(OutputStream* out, File* file, OatDexFile* oat_dex_file) { |
| 1926 | // Dex files are required to be 4 byte aligned. |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1927 | size_t initial_offset = kIsVdexEnabled ? vdex_size_ : oat_size_; |
| 1928 | size_t start_offset = RoundUp(initial_offset, 4); |
| 1929 | size_t file_offset = kIsVdexEnabled ? start_offset : (oat_data_offset_ + start_offset); |
| 1930 | size_dex_file_alignment_ += start_offset - initial_offset; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1931 | |
| 1932 | // Seek to the start of the dex file and flush any pending operations in the stream. |
| 1933 | // 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] | 1934 | off_t actual_offset = out->Seek(file_offset, kSeekSet); |
| 1935 | if (actual_offset != static_cast<off_t>(file_offset)) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1936 | PLOG(ERROR) << "Failed to seek to dex file section. Actual: " << actual_offset |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1937 | << " Expected: " << file_offset |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1938 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 1939 | return false; |
| 1940 | } |
| 1941 | if (!out->Flush()) { |
| 1942 | PLOG(ERROR) << "Failed to flush before writing dex file." |
| 1943 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 1944 | return false; |
| 1945 | } |
| 1946 | actual_offset = lseek(file->Fd(), 0, SEEK_CUR); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1947 | if (actual_offset != static_cast<off_t>(file_offset)) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1948 | PLOG(ERROR) << "Stream/file position mismatch! Actual: " << actual_offset |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1949 | << " Expected: " << file_offset |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1950 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 1951 | return false; |
| 1952 | } |
| 1953 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1954 | if (kIsVdexEnabled) { |
| 1955 | vdex_size_ = start_offset; |
| 1956 | } else { |
| 1957 | oat_size_ = start_offset; |
| 1958 | } |
| 1959 | oat_dex_file->dex_file_offset_ = start_offset; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1960 | return true; |
| 1961 | } |
| 1962 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1963 | bool OatWriter::WriteDexFile(OutputStream* out, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1964 | File* file, |
| 1965 | OatDexFile* oat_dex_file, |
| 1966 | ZipEntry* dex_file) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1967 | size_t start_offset = kIsVdexEnabled ? vdex_size_ : oat_data_offset_ + oat_size_; |
| 1968 | DCHECK_EQ(static_cast<off_t>(start_offset), out->Seek(0, kSeekCurrent)); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1969 | |
| 1970 | // Extract the dex file and get the extracted size. |
| 1971 | std::string error_msg; |
| 1972 | if (!dex_file->ExtractToFile(*file, &error_msg)) { |
| 1973 | LOG(ERROR) << "Failed to extract dex file from ZIP entry: " << error_msg |
| 1974 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 1975 | return false; |
| 1976 | } |
| 1977 | if (file->Flush() != 0) { |
| 1978 | PLOG(ERROR) << "Failed to flush dex file from ZIP entry." |
| 1979 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 1980 | return false; |
| 1981 | } |
| 1982 | off_t extracted_end = lseek(file->Fd(), 0, SEEK_CUR); |
| 1983 | if (extracted_end == static_cast<off_t>(-1)) { |
| 1984 | PLOG(ERROR) << "Failed get end offset after writing dex file from ZIP entry." |
| 1985 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 1986 | return false; |
| 1987 | } |
| 1988 | if (extracted_end < static_cast<off_t>(start_offset)) { |
| 1989 | LOG(ERROR) << "Dex file end position is before start position! End: " << extracted_end |
| 1990 | << " Start: " << start_offset |
| 1991 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 1992 | return false; |
| 1993 | } |
| 1994 | uint64_t extracted_size = static_cast<uint64_t>(extracted_end - start_offset); |
| 1995 | if (extracted_size < sizeof(DexFile::Header)) { |
| 1996 | LOG(ERROR) << "Extracted dex file is shorter than dex file header. size: " |
| 1997 | << extracted_size << " File: " << oat_dex_file->GetLocation(); |
| 1998 | return false; |
| 1999 | } |
| 2000 | |
| 2001 | // Read the dex file header and extract required data to OatDexFile. |
| 2002 | off_t actual_offset = lseek(file->Fd(), start_offset, SEEK_SET); |
| 2003 | if (actual_offset != static_cast<off_t>(start_offset)) { |
| 2004 | PLOG(ERROR) << "Failed to seek back to dex file header. Actual: " << actual_offset |
| 2005 | << " Expected: " << start_offset |
| 2006 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 2007 | return false; |
| 2008 | } |
| 2009 | if (!ReadDexFileHeader(file, oat_dex_file)) { |
| 2010 | return false; |
| 2011 | } |
| 2012 | if (extracted_size < oat_dex_file->dex_file_size_) { |
| 2013 | LOG(ERROR) << "Extracted truncated dex file. Extracted size: " << extracted_size |
| 2014 | << " file size from header: " << oat_dex_file->dex_file_size_ |
| 2015 | << " File: " << oat_dex_file->GetLocation(); |
| 2016 | return false; |
| 2017 | } |
| 2018 | |
| 2019 | // Override the checksum from header with the CRC from ZIP entry. |
| 2020 | oat_dex_file->dex_file_location_checksum_ = dex_file->GetCrc32(); |
| 2021 | |
| 2022 | // Seek both file and stream to the end offset. |
| 2023 | size_t end_offset = start_offset + oat_dex_file->dex_file_size_; |
| 2024 | actual_offset = lseek(file->Fd(), end_offset, SEEK_SET); |
| 2025 | if (actual_offset != static_cast<off_t>(end_offset)) { |
| 2026 | PLOG(ERROR) << "Failed to seek to end of dex file. Actual: " << actual_offset |
| 2027 | << " Expected: " << end_offset |
| 2028 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 2029 | return false; |
| 2030 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2031 | actual_offset = out->Seek(end_offset, kSeekSet); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2032 | if (actual_offset != static_cast<off_t>(end_offset)) { |
| 2033 | PLOG(ERROR) << "Failed to seek stream to end of dex file. Actual: " << actual_offset |
| 2034 | << " Expected: " << end_offset << " File: " << oat_dex_file->GetLocation(); |
| 2035 | return false; |
| 2036 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2037 | if (!out->Flush()) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2038 | PLOG(ERROR) << "Failed to flush stream after seeking over dex file." |
| 2039 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 2040 | return false; |
| 2041 | } |
| 2042 | |
| 2043 | // If we extracted more than the size specified in the header, truncate the file. |
| 2044 | if (extracted_size > oat_dex_file->dex_file_size_) { |
| 2045 | if (file->SetLength(end_offset) != 0) { |
| 2046 | PLOG(ERROR) << "Failed to truncate excessive dex file length." |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2047 | << " File: " << oat_dex_file->GetLocation() |
| 2048 | << " Output: " << file->GetPath(); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2049 | return false; |
| 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | return true; |
| 2054 | } |
| 2055 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2056 | bool OatWriter::WriteDexFile(OutputStream* out, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2057 | File* file, |
| 2058 | OatDexFile* oat_dex_file, |
| 2059 | File* dex_file) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2060 | size_t start_offset = kIsVdexEnabled ? vdex_size_ : oat_data_offset_ + oat_size_; |
| 2061 | DCHECK_EQ(static_cast<off_t>(start_offset), out->Seek(0, kSeekCurrent)); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2062 | |
| 2063 | off_t input_offset = lseek(dex_file->Fd(), 0, SEEK_SET); |
| 2064 | if (input_offset != static_cast<off_t>(0)) { |
| 2065 | PLOG(ERROR) << "Failed to seek to dex file header. Actual: " << input_offset |
| 2066 | << " Expected: 0" |
| 2067 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 2068 | return false; |
| 2069 | } |
| 2070 | if (!ReadDexFileHeader(dex_file, oat_dex_file)) { |
| 2071 | return false; |
| 2072 | } |
| 2073 | |
| 2074 | // Copy the input dex file using sendfile(). |
| 2075 | if (!file->Copy(dex_file, 0, oat_dex_file->dex_file_size_)) { |
| 2076 | PLOG(ERROR) << "Failed to copy dex file to oat file." |
| 2077 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 2078 | return false; |
| 2079 | } |
| 2080 | if (file->Flush() != 0) { |
| 2081 | PLOG(ERROR) << "Failed to flush dex file." |
| 2082 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 2083 | return false; |
| 2084 | } |
| 2085 | |
| 2086 | // Check file position and seek the stream to the end offset. |
| 2087 | size_t end_offset = start_offset + oat_dex_file->dex_file_size_; |
| 2088 | off_t actual_offset = lseek(file->Fd(), 0, SEEK_CUR); |
| 2089 | if (actual_offset != static_cast<off_t>(end_offset)) { |
| 2090 | PLOG(ERROR) << "Unexpected file position after copying dex file. Actual: " << actual_offset |
| 2091 | << " Expected: " << end_offset |
| 2092 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 2093 | return false; |
| 2094 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2095 | actual_offset = out->Seek(end_offset, kSeekSet); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2096 | if (actual_offset != static_cast<off_t>(end_offset)) { |
| 2097 | PLOG(ERROR) << "Failed to seek stream to end of dex file. Actual: " << actual_offset |
| 2098 | << " Expected: " << end_offset << " File: " << oat_dex_file->GetLocation(); |
| 2099 | return false; |
| 2100 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2101 | if (!out->Flush()) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2102 | PLOG(ERROR) << "Failed to flush stream after seeking over dex file." |
| 2103 | << " File: " << oat_dex_file->GetLocation() << " Output: " << file->GetPath(); |
| 2104 | return false; |
| 2105 | } |
| 2106 | |
| 2107 | return true; |
| 2108 | } |
| 2109 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2110 | bool OatWriter::WriteDexFile(OutputStream* out, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2111 | OatDexFile* oat_dex_file, |
| 2112 | const uint8_t* dex_file) { |
| 2113 | // Note: The raw data has already been checked to contain the header |
| 2114 | // and all the data that the header specifies as the file size. |
| 2115 | DCHECK(dex_file != nullptr); |
| 2116 | DCHECK(ValidateDexFileHeader(dex_file, oat_dex_file->GetLocation())); |
| 2117 | const UnalignedDexFileHeader* header = AsUnalignedDexFileHeader(dex_file); |
| 2118 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2119 | if (!out->WriteFully(dex_file, header->file_size_)) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2120 | PLOG(ERROR) << "Failed to write dex file " << oat_dex_file->GetLocation() |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2121 | << " to " << out->GetLocation(); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2122 | return false; |
| 2123 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2124 | if (!out->Flush()) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2125 | PLOG(ERROR) << "Failed to flush stream after writing dex file." |
| 2126 | << " File: " << oat_dex_file->GetLocation(); |
| 2127 | return false; |
| 2128 | } |
| 2129 | |
| 2130 | // Update dex file size and resize class offsets in the OatDexFile. |
| 2131 | // Note: For raw data, the checksum is passed directly to AddRawDexFileSource(). |
| 2132 | oat_dex_file->dex_file_size_ = header->file_size_; |
| 2133 | oat_dex_file->class_offsets_.resize(header->class_defs_size_); |
| 2134 | return true; |
| 2135 | } |
| 2136 | |
| 2137 | bool OatWriter::WriteOatDexFiles(OutputStream* rodata) { |
| 2138 | TimingLogger::ScopedTiming split("WriteOatDexFiles", timings_); |
| 2139 | |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 2140 | off_t initial_offset = rodata->Seek(0, kSeekCurrent); |
| 2141 | if (initial_offset == static_cast<off_t>(-1)) { |
| 2142 | LOG(ERROR) << "Failed to get current position in " << rodata->GetLocation(); |
| 2143 | return false; |
| 2144 | } |
| 2145 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2146 | // Seek to the start of OatDexFiles, i.e. to the end of the OatHeader. If there are |
| 2147 | // no OatDexFiles, no data is actually written to .rodata before WriteHeader() and |
| 2148 | // this Seek() ensures that we reserve the space for OatHeader in .rodata. |
| 2149 | DCHECK(oat_dex_files_.empty() || oat_dex_files_[0u].offset_ == oat_header_->GetHeaderSize()); |
| 2150 | uint32_t expected_offset = oat_data_offset_ + oat_header_->GetHeaderSize(); |
| 2151 | off_t actual_offset = rodata->Seek(expected_offset, kSeekSet); |
| 2152 | if (static_cast<uint32_t>(actual_offset) != expected_offset) { |
| 2153 | PLOG(ERROR) << "Failed to seek to OatDexFile table section. Actual: " << actual_offset |
| 2154 | << " Expected: " << expected_offset << " File: " << rodata->GetLocation(); |
| 2155 | return false; |
| 2156 | } |
| 2157 | |
| 2158 | for (size_t i = 0, size = oat_dex_files_.size(); i != size; ++i) { |
| 2159 | OatDexFile* oat_dex_file = &oat_dex_files_[i]; |
| 2160 | |
| 2161 | DCHECK_EQ(oat_data_offset_ + oat_dex_file->offset_, |
| 2162 | static_cast<size_t>(rodata->Seek(0, kSeekCurrent))); |
| 2163 | |
| 2164 | // Write OatDexFile. |
| 2165 | if (!oat_dex_file->Write(this, rodata)) { |
| 2166 | PLOG(ERROR) << "Failed to write oat dex information to " << rodata->GetLocation(); |
| 2167 | return false; |
| 2168 | } |
| 2169 | } |
| 2170 | |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 2171 | // Seek back to the initial position. |
| 2172 | if (rodata->Seek(initial_offset, kSeekSet) != initial_offset) { |
| 2173 | PLOG(ERROR) << "Failed to seek to initial position. Actual: " << actual_offset |
| 2174 | << " Expected: " << initial_offset << " File: " << rodata->GetLocation(); |
| 2175 | return false; |
| 2176 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2177 | |
David Brazdil | b92ba62 | 2016-09-01 16:00:30 +0000 | [diff] [blame] | 2178 | return true; |
| 2179 | } |
| 2180 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2181 | bool OatWriter::OpenDexFiles( |
| 2182 | File* file, |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 2183 | bool verify, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2184 | /*out*/ std::unique_ptr<MemMap>* opened_dex_files_map, |
| 2185 | /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files) { |
| 2186 | TimingLogger::ScopedTiming split("OpenDexFiles", timings_); |
| 2187 | |
| 2188 | if (oat_dex_files_.empty()) { |
| 2189 | // Nothing to do. |
| 2190 | return true; |
| 2191 | } |
| 2192 | |
| 2193 | size_t map_offset = oat_dex_files_[0].dex_file_offset_; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2194 | size_t length = kIsVdexEnabled ? (vdex_size_ - map_offset) : (oat_size_ - map_offset); |
| 2195 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2196 | std::string error_msg; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2197 | std::unique_ptr<MemMap> dex_files_map(MemMap::MapFile( |
| 2198 | length, |
| 2199 | PROT_READ | PROT_WRITE, |
| 2200 | MAP_SHARED, |
| 2201 | file->Fd(), |
| 2202 | kIsVdexEnabled ? map_offset : (oat_data_offset_ + map_offset), |
| 2203 | /* low_4gb */ false, |
| 2204 | file->GetPath().c_str(), |
| 2205 | &error_msg)); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2206 | if (dex_files_map == nullptr) { |
| 2207 | LOG(ERROR) << "Failed to mmap() dex files from oat file. File: " << file->GetPath() |
| 2208 | << " error: " << error_msg; |
| 2209 | return false; |
| 2210 | } |
| 2211 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 2212 | for (OatDexFile& oat_dex_file : oat_dex_files_) { |
| 2213 | // Make sure no one messed with input files while we were copying data. |
| 2214 | // At the very least we need consistent file size and number of class definitions. |
| 2215 | const uint8_t* raw_dex_file = |
| 2216 | dex_files_map->Begin() + oat_dex_file.dex_file_offset_ - map_offset; |
| 2217 | if (!ValidateDexFileHeader(raw_dex_file, oat_dex_file.GetLocation())) { |
| 2218 | // Note: ValidateDexFileHeader() already logged an error message. |
| 2219 | LOG(ERROR) << "Failed to verify written dex file header!" |
| 2220 | << " Output: " << file->GetPath() << " ~ " << std::hex << map_offset |
| 2221 | << " ~ " << static_cast<const void*>(raw_dex_file); |
| 2222 | return false; |
| 2223 | } |
| 2224 | const UnalignedDexFileHeader* header = AsUnalignedDexFileHeader(raw_dex_file); |
| 2225 | if (header->file_size_ != oat_dex_file.dex_file_size_) { |
| 2226 | LOG(ERROR) << "File size mismatch in written dex file header! Expected: " |
| 2227 | << oat_dex_file.dex_file_size_ << " Actual: " << header->file_size_ |
| 2228 | << " Output: " << file->GetPath(); |
| 2229 | return false; |
| 2230 | } |
| 2231 | if (header->class_defs_size_ != oat_dex_file.class_offsets_.size()) { |
| 2232 | LOG(ERROR) << "Class defs size mismatch in written dex file header! Expected: " |
| 2233 | << oat_dex_file.class_offsets_.size() << " Actual: " << header->class_defs_size_ |
| 2234 | << " Output: " << file->GetPath(); |
| 2235 | return false; |
| 2236 | } |
| 2237 | |
| 2238 | // Now, open the dex file. |
| 2239 | dex_files.emplace_back(DexFile::Open(raw_dex_file, |
| 2240 | oat_dex_file.dex_file_size_, |
| 2241 | oat_dex_file.GetLocation(), |
| 2242 | oat_dex_file.dex_file_location_checksum_, |
| 2243 | /* oat_dex_file */ nullptr, |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 2244 | verify, |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 2245 | verify, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2246 | &error_msg)); |
| 2247 | if (dex_files.back() == nullptr) { |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 2248 | LOG(ERROR) << "Failed to open dex file from oat file. File: " << oat_dex_file.GetLocation() |
| 2249 | << " Error: " << error_msg; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2250 | return false; |
| 2251 | } |
| 2252 | } |
| 2253 | |
| 2254 | *opened_dex_files_map = std::move(dex_files_map); |
| 2255 | *opened_dex_files = std::move(dex_files); |
| 2256 | return true; |
| 2257 | } |
| 2258 | |
| 2259 | bool OatWriter::WriteTypeLookupTables( |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2260 | OutputStream* oat_rodata, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2261 | const std::vector<std::unique_ptr<const DexFile>>& opened_dex_files) { |
| 2262 | TimingLogger::ScopedTiming split("WriteTypeLookupTables", timings_); |
| 2263 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2264 | uint32_t expected_offset = oat_data_offset_ + oat_size_; |
| 2265 | off_t actual_offset = oat_rodata->Seek(expected_offset, kSeekSet); |
| 2266 | if (static_cast<uint32_t>(actual_offset) != expected_offset) { |
| 2267 | PLOG(ERROR) << "Failed to seek to TypeLookupTable section. Actual: " << actual_offset |
| 2268 | << " Expected: " << expected_offset << " File: " << oat_rodata->GetLocation(); |
| 2269 | return false; |
| 2270 | } |
| 2271 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2272 | DCHECK_EQ(opened_dex_files.size(), oat_dex_files_.size()); |
| 2273 | for (size_t i = 0, size = opened_dex_files.size(); i != size; ++i) { |
| 2274 | OatDexFile* oat_dex_file = &oat_dex_files_[i]; |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 2275 | DCHECK_EQ(oat_dex_file->lookup_table_offset_, 0u); |
| 2276 | |
| 2277 | if (oat_dex_file->create_type_lookup_table_ != CreateTypeLookupTable::kCreate || |
| 2278 | oat_dex_file->class_offsets_.empty()) { |
| 2279 | continue; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2280 | } |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 2281 | |
| 2282 | size_t table_size = TypeLookupTable::RawDataLength(oat_dex_file->class_offsets_.size()); |
| 2283 | if (table_size == 0u) { |
| 2284 | continue; |
| 2285 | } |
| 2286 | |
| 2287 | // Create the lookup table. When `nullptr` is given as the storage buffer, |
| 2288 | // TypeLookupTable allocates its own and DexFile takes ownership. |
| 2289 | opened_dex_files[i]->CreateTypeLookupTable(/* storage */ nullptr); |
| 2290 | TypeLookupTable* table = opened_dex_files[i]->GetTypeLookupTable(); |
| 2291 | |
| 2292 | // Type tables are required to be 4 byte aligned. |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2293 | size_t initial_offset = oat_size_; |
| 2294 | size_t rodata_offset = RoundUp(initial_offset, 4); |
| 2295 | size_t padding_size = rodata_offset - initial_offset; |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 2296 | |
| 2297 | if (padding_size != 0u) { |
| 2298 | std::vector<uint8_t> buffer(padding_size, 0u); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2299 | if (!oat_rodata->WriteFully(buffer.data(), padding_size)) { |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 2300 | PLOG(ERROR) << "Failed to write lookup table alignment padding." |
| 2301 | << " File: " << oat_dex_file->GetLocation() |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2302 | << " Output: " << oat_rodata->GetLocation(); |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 2303 | return false; |
| 2304 | } |
| 2305 | } |
| 2306 | |
| 2307 | DCHECK_EQ(oat_data_offset_ + rodata_offset, |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2308 | static_cast<size_t>(oat_rodata->Seek(0u, kSeekCurrent))); |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 2309 | DCHECK_EQ(table_size, table->RawDataLength()); |
| 2310 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2311 | if (!oat_rodata->WriteFully(table->RawData(), table_size)) { |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 2312 | PLOG(ERROR) << "Failed to write lookup table." |
| 2313 | << " File: " << oat_dex_file->GetLocation() |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2314 | << " Output: " << oat_rodata->GetLocation(); |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 2315 | return false; |
| 2316 | } |
| 2317 | |
| 2318 | oat_dex_file->lookup_table_offset_ = rodata_offset; |
| 2319 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2320 | oat_size_ += padding_size + table_size; |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 2321 | size_oat_lookup_table_ += table_size; |
| 2322 | size_oat_lookup_table_alignment_ += padding_size; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2325 | if (!oat_rodata->Flush()) { |
David Brazdil | 181e1cc | 2016-09-01 16:38:47 +0000 | [diff] [blame] | 2326 | PLOG(ERROR) << "Failed to flush stream after writing type lookup tables." |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2327 | << " File: " << oat_rodata->GetLocation(); |
| 2328 | return false; |
| 2329 | } |
| 2330 | |
| 2331 | return true; |
| 2332 | } |
| 2333 | |
| 2334 | bool OatWriter::WriteVdexHeader(OutputStream* vdex_out) { |
| 2335 | off_t actual_offset = vdex_out->Seek(0, kSeekSet); |
| 2336 | if (actual_offset != 0) { |
| 2337 | PLOG(ERROR) << "Failed to seek to the beginning of vdex file. Actual: " << actual_offset |
| 2338 | << " File: " << vdex_out->GetLocation(); |
| 2339 | return false; |
| 2340 | } |
| 2341 | |
| 2342 | VdexFile::Header vdex_header; |
| 2343 | if (!vdex_out->WriteFully(&vdex_header, sizeof(VdexFile::Header))) { |
| 2344 | PLOG(ERROR) << "Failed to write vdex header. File: " << vdex_out->GetLocation(); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2345 | return false; |
| 2346 | } |
| 2347 | |
| 2348 | return true; |
| 2349 | } |
| 2350 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2351 | bool OatWriter::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) { |
| 2352 | static const uint8_t kPadding[] = { |
| 2353 | 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u |
| 2354 | }; |
| 2355 | DCHECK_LE(aligned_code_delta, sizeof(kPadding)); |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 2356 | if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2357 | return false; |
| 2358 | } |
| 2359 | size_code_alignment_ += aligned_code_delta; |
| 2360 | return true; |
| 2361 | } |
| 2362 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2363 | void OatWriter::SetMultiOatRelativePatcherAdjustment() { |
| 2364 | DCHECK(dex_files_ != nullptr); |
| 2365 | DCHECK(relative_patcher_ != nullptr); |
| 2366 | DCHECK_NE(oat_data_offset_, 0u); |
| 2367 | if (image_writer_ != nullptr && !dex_files_->empty()) { |
| 2368 | // The oat data begin may not be initialized yet but the oat file offset is ready. |
| 2369 | size_t oat_index = image_writer_->GetOatIndexForDexFile(dex_files_->front()); |
| 2370 | size_t elf_file_offset = image_writer_->GetOatFileOffset(oat_index); |
| 2371 | relative_patcher_->StartOatFile(elf_file_offset + oat_data_offset_); |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 2372 | } |
| 2373 | } |
| 2374 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2375 | OatWriter::OatDexFile::OatDexFile(const char* dex_file_location, |
| 2376 | DexFileSource source, |
| 2377 | CreateTypeLookupTable create_type_lookup_table) |
| 2378 | : source_(source), |
| 2379 | create_type_lookup_table_(create_type_lookup_table), |
| 2380 | dex_file_size_(0), |
| 2381 | offset_(0), |
| 2382 | dex_file_location_size_(strlen(dex_file_location)), |
| 2383 | dex_file_location_data_(dex_file_location), |
| 2384 | dex_file_location_checksum_(0u), |
| 2385 | dex_file_offset_(0u), |
| 2386 | class_offsets_offset_(0u), |
| 2387 | lookup_table_offset_(0u), |
| 2388 | class_offsets_() { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | size_t OatWriter::OatDexFile::SizeOf() const { |
| 2392 | return sizeof(dex_file_location_size_) |
| 2393 | + dex_file_location_size_ |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 2394 | + sizeof(dex_file_location_checksum_) |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 2395 | + sizeof(dex_file_offset_) |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2396 | + sizeof(class_offsets_offset_) |
| 2397 | + sizeof(lookup_table_offset_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2398 | } |
| 2399 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2400 | void OatWriter::OatDexFile::ReserveClassOffsets(OatWriter* oat_writer) { |
| 2401 | DCHECK_EQ(class_offsets_offset_, 0u); |
| 2402 | if (!class_offsets_.empty()) { |
| 2403 | // Class offsets are required to be 4 byte aligned. |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2404 | size_t initial_offset = oat_writer->oat_size_; |
| 2405 | size_t offset = RoundUp(initial_offset, 4); |
| 2406 | oat_writer->size_oat_class_offsets_alignment_ += offset - initial_offset; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2407 | class_offsets_offset_ = offset; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2408 | oat_writer->oat_size_ = offset + GetClassOffsetsRawSize(); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2409 | } |
| 2410 | } |
| 2411 | |
| 2412 | bool OatWriter::OatDexFile::Write(OatWriter* oat_writer, OutputStream* out) const { |
| 2413 | const size_t file_offset = oat_writer->oat_data_offset_; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 2414 | DCHECK_OFFSET_(); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2415 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 2416 | if (!out->WriteFully(&dex_file_location_size_, sizeof(dex_file_location_size_))) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 2417 | PLOG(ERROR) << "Failed to write dex file location length to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2418 | return false; |
| 2419 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 2420 | 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] | 2421 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 2422 | if (!out->WriteFully(dex_file_location_data_, dex_file_location_size_)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 2423 | PLOG(ERROR) << "Failed to write dex file location data to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2424 | return false; |
| 2425 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 2426 | oat_writer->size_oat_dex_file_location_data_ += dex_file_location_size_; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2427 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 2428 | if (!out->WriteFully(&dex_file_location_checksum_, sizeof(dex_file_location_checksum_))) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 2429 | PLOG(ERROR) << "Failed to write dex file location checksum to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2430 | return false; |
| 2431 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 2432 | 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] | 2433 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 2434 | if (!out->WriteFully(&dex_file_offset_, sizeof(dex_file_offset_))) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 2435 | PLOG(ERROR) << "Failed to write dex file offset to " << out->GetLocation(); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 2436 | return false; |
| 2437 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 2438 | oat_writer->size_oat_dex_file_offset_ += sizeof(dex_file_offset_); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2439 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 2440 | if (!out->WriteFully(&class_offsets_offset_, sizeof(class_offsets_offset_))) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2441 | PLOG(ERROR) << "Failed to write class offsets offset to " << out->GetLocation(); |
| 2442 | return false; |
| 2443 | } |
| 2444 | oat_writer->size_oat_dex_file_class_offsets_offset_ += sizeof(class_offsets_offset_); |
| 2445 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 2446 | if (!out->WriteFully(&lookup_table_offset_, sizeof(lookup_table_offset_))) { |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 2447 | PLOG(ERROR) << "Failed to write lookup table offset to " << out->GetLocation(); |
| 2448 | return false; |
| 2449 | } |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2450 | 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] | 2451 | |
| 2452 | return true; |
| 2453 | } |
| 2454 | |
| 2455 | bool OatWriter::OatDexFile::WriteClassOffsets(OatWriter* oat_writer, OutputStream* out) { |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 2456 | if (!out->WriteFully(class_offsets_.data(), GetClassOffsetsRawSize())) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2457 | PLOG(ERROR) << "Failed to write oat class offsets for " << GetLocation() |
| 2458 | << " to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2459 | return false; |
| 2460 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2461 | oat_writer->size_oat_class_offsets_ += GetClassOffsetsRawSize(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2462 | return true; |
| 2463 | } |
| 2464 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 2465 | OatWriter::OatClass::OatClass(size_t offset, |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2466 | const dchecked_vector<CompiledMethod*>& compiled_methods, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 2467 | uint32_t num_non_null_compiled_methods, |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 2468 | mirror::Class::Status status) |
| 2469 | : compiled_methods_(compiled_methods) { |
| 2470 | uint32_t num_methods = compiled_methods.size(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 2471 | CHECK_LE(num_non_null_compiled_methods, num_methods); |
| 2472 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 2473 | offset_ = offset; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 2474 | oat_method_offsets_offsets_from_oat_class_.resize(num_methods); |
| 2475 | |
| 2476 | // Since both kOatClassNoneCompiled and kOatClassAllCompiled could |
| 2477 | // apply when there are 0 methods, we just arbitrarily say that 0 |
| 2478 | // methods means kOatClassNoneCompiled and that we won't use |
| 2479 | // kOatClassAllCompiled unless there is at least one compiled |
| 2480 | // method. This means in an interpretter only system, we can assert |
| 2481 | // that all classes are kOatClassNoneCompiled. |
| 2482 | if (num_non_null_compiled_methods == 0) { |
| 2483 | type_ = kOatClassNoneCompiled; |
| 2484 | } else if (num_non_null_compiled_methods == num_methods) { |
| 2485 | type_ = kOatClassAllCompiled; |
| 2486 | } else { |
| 2487 | type_ = kOatClassSomeCompiled; |
| 2488 | } |
| 2489 | |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 2490 | status_ = status; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 2491 | method_offsets_.resize(num_non_null_compiled_methods); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 2492 | method_headers_.resize(num_non_null_compiled_methods); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 2493 | |
| 2494 | uint32_t oat_method_offsets_offset_from_oat_class = sizeof(type_) + sizeof(status_); |
| 2495 | if (type_ == kOatClassSomeCompiled) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2496 | method_bitmap_.reset(new BitVector(num_methods, false, Allocator::GetMallocAllocator())); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 2497 | method_bitmap_size_ = method_bitmap_->GetSizeOf(); |
| 2498 | oat_method_offsets_offset_from_oat_class += sizeof(method_bitmap_size_); |
| 2499 | oat_method_offsets_offset_from_oat_class += method_bitmap_size_; |
| 2500 | } else { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2501 | method_bitmap_ = nullptr; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 2502 | method_bitmap_size_ = 0; |
| 2503 | } |
| 2504 | |
| 2505 | for (size_t i = 0; i < num_methods; i++) { |
Vladimir Marko | 96c6ab9 | 2014-04-08 14:00:50 +0100 | [diff] [blame] | 2506 | CompiledMethod* compiled_method = compiled_methods_[i]; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2507 | if (compiled_method == nullptr) { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 2508 | oat_method_offsets_offsets_from_oat_class_[i] = 0; |
| 2509 | } else { |
| 2510 | oat_method_offsets_offsets_from_oat_class_[i] = oat_method_offsets_offset_from_oat_class; |
| 2511 | oat_method_offsets_offset_from_oat_class += sizeof(OatMethodOffsets); |
| 2512 | if (type_ == kOatClassSomeCompiled) { |
| 2513 | method_bitmap_->SetBit(i); |
| 2514 | } |
| 2515 | } |
| 2516 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2517 | } |
| 2518 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 2519 | size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatHeader( |
| 2520 | size_t class_def_method_index_) const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 2521 | uint32_t method_offset = GetOatMethodOffsetsOffsetFromOatClass(class_def_method_index_); |
| 2522 | if (method_offset == 0) { |
| 2523 | return 0; |
| 2524 | } |
| 2525 | return offset_ + method_offset; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 2526 | } |
| 2527 | |
| 2528 | size_t OatWriter::OatClass::GetOatMethodOffsetsOffsetFromOatClass( |
| 2529 | size_t class_def_method_index_) const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 2530 | return oat_method_offsets_offsets_from_oat_class_[class_def_method_index_]; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 2531 | } |
| 2532 | |
| 2533 | size_t OatWriter::OatClass::SizeOf() const { |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 2534 | return sizeof(status_) |
| 2535 | + sizeof(type_) |
| 2536 | + ((method_bitmap_size_ == 0) ? 0 : sizeof(method_bitmap_size_)) |
| 2537 | + method_bitmap_size_ |
| 2538 | + (sizeof(method_offsets_[0]) * method_offsets_.size()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2539 | } |
| 2540 | |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 2541 | bool OatWriter::OatClass::Write(OatWriter* oat_writer, |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 2542 | OutputStream* out, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 2543 | const size_t file_offset) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 2544 | DCHECK_OFFSET_(); |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 2545 | if (!out->WriteFully(&status_, sizeof(status_))) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 2546 | PLOG(ERROR) << "Failed to write class status to " << out->GetLocation(); |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 2547 | return false; |
| 2548 | } |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 2549 | oat_writer->size_oat_class_status_ += sizeof(status_); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2550 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 2551 | if (!out->WriteFully(&type_, sizeof(type_))) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 2552 | PLOG(ERROR) << "Failed to write oat class type to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 2553 | return false; |
| 2554 | } |
| 2555 | oat_writer->size_oat_class_type_ += sizeof(type_); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2556 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 2557 | if (method_bitmap_size_ != 0) { |
| 2558 | CHECK_EQ(kOatClassSomeCompiled, type_); |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 2559 | if (!out->WriteFully(&method_bitmap_size_, sizeof(method_bitmap_size_))) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 2560 | PLOG(ERROR) << "Failed to write method bitmap size to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 2561 | return false; |
| 2562 | } |
| 2563 | oat_writer->size_oat_class_method_bitmaps_ += sizeof(method_bitmap_size_); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2564 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 2565 | if (!out->WriteFully(method_bitmap_->GetRawStorage(), method_bitmap_size_)) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 2566 | PLOG(ERROR) << "Failed to write method bitmap to " << out->GetLocation(); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 2567 | return false; |
| 2568 | } |
| 2569 | oat_writer->size_oat_class_method_bitmaps_ += method_bitmap_size_; |
| 2570 | } |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2571 | |
Vladimir Marko | e079e21 | 2016-05-25 12:49:49 +0100 | [diff] [blame] | 2572 | if (!out->WriteFully(method_offsets_.data(), GetMethodOffsetsRawSize())) { |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 2573 | PLOG(ERROR) << "Failed to write method offsets to " << out->GetLocation(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2574 | return false; |
| 2575 | } |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2576 | oat_writer->size_oat_class_method_offsets_ += GetMethodOffsetsRawSize(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2577 | return true; |
| 2578 | } |
| 2579 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 2580 | } // namespace art |