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