Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [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 | */ |
| 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_COMPILER_IMAGE_WRITER_H_ |
| 18 | #define ART_COMPILER_IMAGE_WRITER_H_ |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 21 | #include "base/memory_tool.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 22 | |
| 23 | #include <cstddef> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 24 | #include <memory> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 25 | #include <set> |
| 26 | #include <string> |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 27 | #include <ostream> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 28 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 29 | #include "base/bit_utils.h" |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 30 | #include "base/macros.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 31 | #include "driver/compiler_driver.h" |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 32 | #include "gc/space/space.h" |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 33 | #include "length_prefixed_array.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 34 | #include "lock_word.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 35 | #include "mem_map.h" |
| 36 | #include "oat_file.h" |
| 37 | #include "mirror/dex_cache.h" |
| 38 | #include "os.h" |
| 39 | #include "safe_map.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 40 | #include "utils.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 41 | |
| 42 | namespace art { |
| 43 | |
| 44 | // Write a Space built during compilation for use during execution. |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 45 | class ImageWriter FINAL { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 46 | public: |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 47 | ImageWriter(const CompilerDriver& compiler_driver, uintptr_t image_begin, |
| 48 | bool compile_pic) |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 49 | : compiler_driver_(compiler_driver), image_begin_(reinterpret_cast<uint8_t*>(image_begin)), |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 50 | image_end_(0), image_objects_offset_begin_(0), image_roots_address_(0), oat_file_(nullptr), |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 51 | oat_data_begin_(nullptr), interpreter_to_interpreter_bridge_offset_(0), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 52 | interpreter_to_compiled_code_bridge_offset_(0), jni_dlsym_lookup_offset_(0), |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 53 | quick_generic_jni_trampoline_offset_(0), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 54 | quick_imt_conflict_trampoline_offset_(0), quick_resolution_trampoline_offset_(0), |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 55 | quick_to_interpreter_bridge_offset_(0), compile_pic_(compile_pic), |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 56 | target_ptr_size_(InstructionSetPointerSize(compiler_driver_.GetInstructionSet())), |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 57 | bin_slot_sizes_(), bin_slot_offsets_(), bin_slot_count_(), |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 58 | intern_table_bytes_(0u), image_method_array_(ImageHeader::kImageMethodsCount), |
| 59 | dirty_methods_(0u), clean_methods_(0u) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 60 | CHECK_NE(image_begin, 0U); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 61 | std::fill(image_methods_, image_methods_ + arraysize(image_methods_), nullptr); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 62 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 63 | |
Andreas Gampe | 245ee00 | 2014-12-04 21:25:04 -0800 | [diff] [blame] | 64 | ~ImageWriter() { |
Andreas Gampe | 245ee00 | 2014-12-04 21:25:04 -0800 | [diff] [blame] | 65 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 66 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 67 | bool PrepareImageAddressSpace(); |
| 68 | |
| 69 | bool IsImageAddressSpaceReady() const { |
| 70 | return image_roots_address_ != 0u; |
| 71 | } |
| 72 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 73 | template <typename T> |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 74 | T* GetImageAddress(T* object) const SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 75 | return object == nullptr ? nullptr : |
| 76 | reinterpret_cast<T*>(image_begin_ + GetImageOffset(object)); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 79 | ArtMethod* GetImageMethodAddress(ArtMethod* method) SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 80 | |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 81 | mirror::HeapReference<mirror::Object>* GetDexCacheArrayElementImageAddress( |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 82 | const DexFile* dex_file, uint32_t offset) const SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 83 | auto it = dex_cache_array_starts_.find(dex_file); |
| 84 | DCHECK(it != dex_cache_array_starts_.end()); |
| 85 | return reinterpret_cast<mirror::HeapReference<mirror::Object>*>( |
| 86 | image_begin_ + RoundUp(sizeof(ImageHeader), kObjectAlignment) + it->second + offset); |
| 87 | } |
| 88 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 89 | uint8_t* GetOatFileBegin() const; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 90 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 91 | bool Write(const std::string& image_filename, const std::string& oat_filename, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 92 | const std::string& oat_location) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 93 | REQUIRES(!Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 94 | |
| 95 | uintptr_t GetOatDataBegin() { |
| 96 | return reinterpret_cast<uintptr_t>(oat_data_begin_); |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | bool AllocMemory(); |
| 101 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 102 | // Mark the objects defined in this space in the given live bitmap. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 103 | void RecordImageAllocations() SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 104 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 105 | // Classify different kinds of bins that objects end up getting packed into during image writing. |
| 106 | enum Bin { |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 107 | // Dex cache arrays have a special slot for PC-relative addressing. Since they are |
| 108 | // huge, and as such their dirtiness is not important for the clean/dirty separation, |
| 109 | // we arbitrarily keep them at the beginning. |
| 110 | kBinDexCacheArray, // Object arrays belonging to dex cache. |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 111 | // Likely-clean: |
| 112 | kBinString, // [String] Almost always immutable (except for obj header). |
| 113 | kBinArtMethodsManagedInitialized, // [ArtMethod] Not-native, and initialized. Unlikely to dirty |
| 114 | // Unknown mix of clean/dirty: |
| 115 | kBinRegular, |
| 116 | // Likely-dirty: |
| 117 | // All classes get their own bins since their fields often dirty |
| 118 | kBinClassInitializedFinalStatics, // Class initializers have been run, no non-final statics |
| 119 | kBinClassInitialized, // Class initializers have been run |
| 120 | kBinClassVerified, // Class verified, but initializers haven't been run |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 121 | // Add more bins here if we add more segregation code. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 122 | // Non mirror fields must be below. |
| 123 | // ArtFields should be always clean. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 124 | kBinArtField, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 125 | // If the class is initialized, then the ArtMethods are probably clean. |
| 126 | kBinArtMethodClean, |
| 127 | // ArtMethods may be dirty if the class has native methods or a declaring class that isn't |
| 128 | // initialized. |
| 129 | kBinArtMethodDirty, |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 130 | kBinSize, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 131 | // Number of bins which are for mirror objects. |
| 132 | kBinMirrorCount = kBinArtField, |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 133 | }; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 134 | friend std::ostream& operator<<(std::ostream& stream, const Bin& bin); |
| 135 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 136 | enum NativeObjectRelocationType { |
| 137 | kNativeObjectRelocationTypeArtField, |
| 138 | kNativeObjectRelocationTypeArtFieldArray, |
| 139 | kNativeObjectRelocationTypeArtMethodClean, |
| 140 | kNativeObjectRelocationTypeArtMethodArrayClean, |
| 141 | kNativeObjectRelocationTypeArtMethodDirty, |
| 142 | kNativeObjectRelocationTypeArtMethodArrayDirty, |
| 143 | }; |
| 144 | friend std::ostream& operator<<(std::ostream& stream, const NativeObjectRelocationType& type); |
| 145 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 146 | static constexpr size_t kBinBits = MinimumBitsToStore<uint32_t>(kBinMirrorCount - 1); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 147 | // uint32 = typeof(lockword_) |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 148 | // Subtract read barrier bits since we want these to remain 0, or else it may result in DCHECK |
| 149 | // failures due to invalid read barrier bits during object field reads. |
| 150 | static const size_t kBinShift = BitSizeOf<uint32_t>() - kBinBits - |
| 151 | LockWord::kReadBarrierStateSize; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 152 | // 111000.....0 |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 153 | static const size_t kBinMask = ((static_cast<size_t>(1) << kBinBits) - 1) << kBinShift; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 154 | |
| 155 | // We use the lock word to store the bin # and bin index of the object in the image. |
| 156 | // |
| 157 | // The struct size must be exactly sizeof(LockWord), currently 32-bits, since this will end up |
| 158 | // stored in the lock word bit-for-bit when object forwarding addresses are being calculated. |
| 159 | struct BinSlot { |
| 160 | explicit BinSlot(uint32_t lockword); |
| 161 | BinSlot(Bin bin, uint32_t index); |
| 162 | |
| 163 | // The bin an object belongs to, i.e. regular, class/verified, class/initialized, etc. |
| 164 | Bin GetBin() const; |
| 165 | // The offset in bytes from the beginning of the bin. Aligned to object size. |
| 166 | uint32_t GetIndex() const; |
| 167 | // Pack into a single uint32_t, for storing into a lock word. |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 168 | uint32_t Uint32Value() const { return lockword_; } |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 169 | // Comparison operator for map support |
| 170 | bool operator<(const BinSlot& other) const { return lockword_ < other.lockword_; } |
| 171 | |
| 172 | private: |
| 173 | // Must be the same size as LockWord, any larger and we would truncate the data. |
| 174 | const uint32_t lockword_; |
| 175 | }; |
| 176 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 177 | // We use the lock word to store the offset of the object in the image. |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 178 | void AssignImageOffset(mirror::Object* object, BinSlot bin_slot) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 179 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 180 | void SetImageOffset(mirror::Object* object, size_t offset) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 181 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 182 | bool IsImageOffsetAssigned(mirror::Object* object) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 183 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 184 | size_t GetImageOffset(mirror::Object* object) const SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 185 | void UpdateImageOffset(mirror::Object* obj, uintptr_t offset) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 186 | SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 187 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 188 | void PrepareDexCacheArraySlots() SHARED_REQUIRES(Locks::mutator_lock_); |
| 189 | void AssignImageBinSlot(mirror::Object* object) SHARED_REQUIRES(Locks::mutator_lock_); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 190 | void SetImageBinSlot(mirror::Object* object, BinSlot bin_slot) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 191 | SHARED_REQUIRES(Locks::mutator_lock_); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 192 | bool IsImageBinSlotAssigned(mirror::Object* object) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 193 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 194 | BinSlot GetImageBinSlot(mirror::Object* object) const SHARED_REQUIRES(Locks::mutator_lock_); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 195 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 196 | void AddMethodPointerArray(mirror::PointerArray* arr) SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 197 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 198 | static void* GetImageAddressCallback(void* writer, mirror::Object* obj) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 199 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 200 | return reinterpret_cast<ImageWriter*>(writer)->GetImageAddress(obj); |
| 201 | } |
| 202 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 203 | mirror::Object* GetLocalAddress(mirror::Object* object) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 204 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 205 | size_t offset = GetImageOffset(object); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 206 | uint8_t* dst = image_->Begin() + offset; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 207 | return reinterpret_cast<mirror::Object*>(dst); |
| 208 | } |
| 209 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 210 | const uint8_t* GetOatAddress(uint32_t offset) const { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 211 | // With Quick, code is within the OatFile, as there are all in one |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 212 | // .o ELF object. |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 213 | DCHECK_LE(offset, oat_file_->Size()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 214 | DCHECK(oat_data_begin_ != nullptr); |
| 215 | return offset == 0u ? nullptr : oat_data_begin_ + offset; |
| 216 | } |
| 217 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 218 | // Returns true if the class was in the original requested image classes list. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 219 | bool IsImageClass(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 220 | |
| 221 | // Debug aid that list of requested image classes. |
| 222 | void DumpImageClasses(); |
| 223 | |
| 224 | // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying. |
| 225 | void ComputeLazyFieldsForImageClasses() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 226 | SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 227 | |
| 228 | // Wire dex cache resolved strings to strings in the image to avoid runtime resolution. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 229 | void ComputeEagerResolvedStrings() SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 230 | static void ComputeEagerResolvedStringsCallback(mirror::Object* obj, void* arg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 231 | SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 232 | |
| 233 | // Remove unwanted classes from various roots. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 234 | void PruneNonImageClasses() SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 235 | |
| 236 | // Verify unwanted classes removed. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 237 | void CheckNonImageClassesRemoved() SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 238 | static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 239 | SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 240 | |
| 241 | // Lays out where the image objects will be at runtime. |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 242 | void CalculateNewObjectOffsets() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 243 | SHARED_REQUIRES(Locks::mutator_lock_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 244 | void CreateHeader(size_t oat_loaded_size, size_t oat_data_offset) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 245 | SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 246 | mirror::ObjectArray<mirror::Object>* CreateImageRoots() const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 247 | SHARED_REQUIRES(Locks::mutator_lock_); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 248 | void CalculateObjectBinSlots(mirror::Object* obj) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 249 | SHARED_REQUIRES(Locks::mutator_lock_); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 250 | void UnbinObjectsIntoOffset(mirror::Object* obj) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 251 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 252 | |
| 253 | void WalkInstanceFields(mirror::Object* obj, mirror::Class* klass) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 254 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 255 | void WalkFieldsInOrder(mirror::Object* obj) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 256 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 257 | static void WalkFieldsCallback(mirror::Object* obj, void* arg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 258 | SHARED_REQUIRES(Locks::mutator_lock_); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 259 | static void UnbinObjectsIntoOffsetCallback(mirror::Object* obj, void* arg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 260 | SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 261 | |
| 262 | // Creates the contiguous image in memory and adjusts pointers. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 263 | void CopyAndFixupNativeData() SHARED_REQUIRES(Locks::mutator_lock_); |
| 264 | void CopyAndFixupObjects() SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 265 | static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 266 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 267 | void CopyAndFixupObject(mirror::Object* obj) SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 268 | void CopyAndFixupMethod(ArtMethod* orig, ArtMethod* copy) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 269 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 270 | void FixupClass(mirror::Class* orig, mirror::Class* copy) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 271 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 272 | void FixupObject(mirror::Object* orig, mirror::Object* copy) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 273 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 274 | void FixupPointerArray(mirror::Object* dst, mirror::PointerArray* arr, mirror::Class* klass, |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 275 | Bin array_type) SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 276 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 277 | // Get quick code for non-resolution/imt_conflict/abstract method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 278 | const uint8_t* GetQuickCode(ArtMethod* method, bool* quick_is_interpreted) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 279 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 280 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 281 | const uint8_t* GetQuickEntryPoint(ArtMethod* method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 282 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 283 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 284 | // Patches references in OatFile to expect runtime addresses. |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 285 | void SetOatChecksumFromElfFile(File* elf_file); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 286 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 287 | // Calculate the sum total of the bin slot sizes in [0, up_to). Defaults to all bins. |
| 288 | size_t GetBinSizeSum(Bin up_to = kBinSize) const; |
| 289 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 290 | // Return true if a method is likely to be dirtied at runtime. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 291 | bool WillMethodBeDirty(ArtMethod* m) const SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 292 | |
| 293 | // Assign the offset for an ArtMethod. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 294 | void AssignMethodOffset(ArtMethod* method, NativeObjectRelocationType type) |
| 295 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 296 | |
| 297 | static Bin BinTypeForNativeRelocationType(NativeObjectRelocationType type); |
| 298 | |
| 299 | void* NativeLocationInImage(void* obj); |
Andreas Gampe | 245ee00 | 2014-12-04 21:25:04 -0800 | [diff] [blame] | 300 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 301 | const CompilerDriver& compiler_driver_; |
| 302 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 303 | // Beginning target image address for the output image. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 304 | uint8_t* image_begin_; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 305 | |
| 306 | // Offset to the free space in image_. |
| 307 | size_t image_end_; |
| 308 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 309 | // Offset from image_begin_ to where the first object is in image_. |
| 310 | size_t image_objects_offset_begin_; |
| 311 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 312 | // The image roots address in the image. |
| 313 | uint32_t image_roots_address_; |
| 314 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 315 | // oat file with code for this image |
| 316 | OatFile* oat_file_; |
| 317 | |
| 318 | // Memory mapped for generating the image. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 319 | std::unique_ptr<MemMap> image_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 320 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 321 | // Indexes, lengths for dex cache arrays (objects are inside of the image so that they don't |
| 322 | // move). |
| 323 | struct DexCacheArrayLocation { |
| 324 | size_t offset_; |
| 325 | size_t length_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 326 | Bin bin_type_; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 327 | }; |
| 328 | SafeMap<mirror::Object*, DexCacheArrayLocation> dex_cache_array_indexes_; |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 329 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 330 | // Pointer arrays that need to be updated. Since these are only some int and long arrays, we need |
| 331 | // to keep track. These include vtable arrays, iftable arrays, and dex caches. |
| 332 | std::unordered_map<mirror::PointerArray*, Bin> pointer_arrays_; |
| 333 | |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 334 | // The start offsets of the dex cache arrays. |
| 335 | SafeMap<const DexFile*, size_t> dex_cache_array_starts_; |
| 336 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 337 | // Saved hash codes. We use these to restore lockwords which were temporarily used to have |
| 338 | // forwarding addresses as well as copying over hash codes. |
| 339 | std::unordered_map<mirror::Object*, uint32_t> saved_hashcode_map_; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 340 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 341 | // Beginning target oat address for the pointers from the output image to its oat file. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 342 | const uint8_t* oat_data_begin_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 343 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 344 | // Image bitmap which lets us know where the objects inside of the image reside. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 345 | std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_; |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 346 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 347 | // Offset from oat_data_begin_ to the stubs. |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 348 | uint32_t interpreter_to_interpreter_bridge_offset_; |
| 349 | uint32_t interpreter_to_compiled_code_bridge_offset_; |
| 350 | uint32_t jni_dlsym_lookup_offset_; |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 351 | uint32_t quick_generic_jni_trampoline_offset_; |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 352 | uint32_t quick_imt_conflict_trampoline_offset_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 353 | uint32_t quick_resolution_trampoline_offset_; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 354 | uint32_t quick_to_interpreter_bridge_offset_; |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 355 | const bool compile_pic_; |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 356 | |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 357 | // Size of pointers on the target architecture. |
| 358 | size_t target_ptr_size_; |
| 359 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 360 | // Bin slot tracking for dirty object packing |
| 361 | size_t bin_slot_sizes_[kBinSize]; // Number of bytes in a bin |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 362 | size_t bin_slot_offsets_[kBinSize]; // Number of bytes in previous bins. |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 363 | size_t bin_slot_count_[kBinSize]; // Number of objects in a bin |
| 364 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 365 | // Cached size of the intern table for when we allocate memory. |
| 366 | size_t intern_table_bytes_; |
| 367 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 368 | // ArtField, ArtMethod relocating map. These are allocated as array of structs but we want to |
| 369 | // have one entry per art field for convenience. ArtFields are placed right after the end of the |
| 370 | // image objects (aka sum of bin_slot_sizes_). ArtMethods are placed right after the ArtFields. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 371 | struct NativeObjectRelocation { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 372 | uintptr_t offset; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 373 | NativeObjectRelocationType type; |
| 374 | |
| 375 | bool IsArtMethodRelocation() const { |
| 376 | return type == kNativeObjectRelocationTypeArtMethodClean || |
| 377 | type == kNativeObjectRelocationTypeArtMethodDirty; |
| 378 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 379 | }; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 380 | std::unordered_map<void*, NativeObjectRelocation> native_object_relocations_; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 381 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 382 | // Runtime ArtMethods which aren't reachable from any Class but need to be copied into the image. |
| 383 | ArtMethod* image_methods_[ImageHeader::kImageMethodsCount]; |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 384 | // Fake length prefixed array for image methods. This array does not contain the actual |
| 385 | // ArtMethods. We only use it for the header and relocation addresses. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 386 | LengthPrefixedArray<ArtMethod> image_method_array_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 387 | |
| 388 | // Counters for measurements, used for logging only. |
| 389 | uint64_t dirty_methods_; |
| 390 | uint64_t clean_methods_; |
Andreas Gampe | 245ee00 | 2014-12-04 21:25:04 -0800 | [diff] [blame] | 391 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 392 | friend class FixupClassVisitor; |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 393 | friend class FixupRootVisitor; |
| 394 | friend class FixupVisitor; |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 395 | friend class NonImageClassesVisitor; |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 396 | DISALLOW_COPY_AND_ASSIGN(ImageWriter); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 397 | }; |
| 398 | |
| 399 | } // namespace art |
| 400 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 401 | #endif // ART_COMPILER_IMAGE_WRITER_H_ |