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