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