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> |
Mathieu Chartier | 4e9c4e7 | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 26 | #include <stack> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 27 | #include <string> |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 28 | #include <ostream> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 29 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 30 | #include "base/bit_utils.h" |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 31 | #include "base/dchecked_vector.h" |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 32 | #include "base/length_prefixed_array.h" |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 33 | #include "base/macros.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 34 | #include "driver/compiler_driver.h" |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 35 | #include "gc/space/space.h" |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 36 | #include "image.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 37 | #include "lock_word.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 38 | #include "mem_map.h" |
| 39 | #include "oat_file.h" |
| 40 | #include "mirror/dex_cache.h" |
| 41 | #include "os.h" |
| 42 | #include "safe_map.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 43 | #include "utils.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 44 | |
| 45 | namespace art { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 46 | namespace gc { |
| 47 | namespace space { |
| 48 | class ImageSpace; |
| 49 | } // namespace space |
| 50 | } // namespace gc |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 51 | |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame] | 52 | class ClassTable; |
| 53 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 54 | static constexpr int kInvalidFd = -1; |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 55 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 56 | // Write a Space built during compilation for use during execution. |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 57 | class ImageWriter FINAL { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 58 | public: |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 59 | ImageWriter(const CompilerDriver& compiler_driver, |
| 60 | uintptr_t image_begin, |
| 61 | bool compile_pic, |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 62 | bool compile_app_image, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 63 | ImageHeader::StorageMode image_storage_mode, |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 64 | const std::vector<const char*>& oat_filenames, |
| 65 | const std::unordered_map<const DexFile*, size_t>& dex_file_oat_index_map); |
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 { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 70 | DCHECK(!image_infos_.empty()); |
| 71 | for (const ImageInfo& image_info : image_infos_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 72 | if (image_info.image_roots_address_ == 0u) { |
| 73 | return false; |
| 74 | } |
| 75 | } |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 76 | return true; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 79 | template <typename T> |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 80 | T* GetImageAddress(T* object) const SHARED_REQUIRES(Locks::mutator_lock_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 81 | if (object == nullptr || IsInBootImage(object)) { |
| 82 | return object; |
| 83 | } else { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 84 | size_t oat_index = GetOatIndex(object); |
| 85 | const ImageInfo& image_info = GetImageInfo(oat_index); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 86 | return reinterpret_cast<T*>(image_info.image_begin_ + GetImageOffset(object)); |
| 87 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 90 | ArtMethod* GetImageMethodAddress(ArtMethod* method) SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 91 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 92 | template <typename PtrType> |
| 93 | PtrType GetDexCacheArrayElementImageAddress(const DexFile* dex_file, uint32_t offset) |
| 94 | const SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 95 | auto oat_it = dex_file_oat_index_map_.find(dex_file); |
| 96 | DCHECK(oat_it != dex_file_oat_index_map_.end()); |
| 97 | const ImageInfo& image_info = GetImageInfo(oat_it->second); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 98 | auto it = image_info.dex_cache_array_starts_.find(dex_file); |
| 99 | DCHECK(it != image_info.dex_cache_array_starts_.end()); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 100 | return reinterpret_cast<PtrType>( |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 101 | image_info.image_begin_ + image_info.bin_slot_offsets_[kBinDexCacheArray] + |
| 102 | it->second + offset); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 105 | size_t GetOatFileOffset(size_t oat_index) const { |
| 106 | return GetImageInfo(oat_index).oat_offset_; |
| 107 | } |
| 108 | |
| 109 | const uint8_t* GetOatFileBegin(size_t oat_index) const { |
| 110 | return GetImageInfo(oat_index).oat_file_begin_; |
| 111 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 112 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 113 | // If image_fd is not kInvalidFd, then we use that for the image file. Otherwise we open |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 114 | // the names in image_filenames. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 115 | // If oat_fd is not kInvalidFd, then we use that for the oat file. Otherwise we open |
| 116 | // the names in oat_filenames. |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 117 | bool Write(int image_fd, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 118 | const std::vector<const char*>& image_filenames, |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 119 | const std::vector<const char*>& oat_filenames) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 120 | REQUIRES(!Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 121 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 122 | uintptr_t GetOatDataBegin(size_t oat_index) { |
| 123 | return reinterpret_cast<uintptr_t>(GetImageInfo(oat_index).oat_data_begin_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 126 | // Get the index of the oat file containing the dex file. |
| 127 | // |
| 128 | // This "oat_index" is used to retrieve information about the the memory layout |
| 129 | // of the oat file and its associated image file, needed for link-time patching |
| 130 | // of references to the image or across oat files. |
| 131 | size_t GetOatIndexForDexFile(const DexFile* dex_file) const; |
| 132 | |
| 133 | // Get the index of the oat file containing the dex file served by the dex cache. |
| 134 | size_t GetOatIndexForDexCache(mirror::DexCache* dex_cache) const |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 135 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 136 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 137 | // Update the oat layout for the given oat file. |
| 138 | // This will make the oat_offset for the next oat file valid. |
| 139 | void UpdateOatFileLayout(size_t oat_index, |
| 140 | size_t oat_loaded_size, |
| 141 | size_t oat_data_offset, |
| 142 | size_t oat_data_size); |
| 143 | // Update information about the oat header, i.e. checksum and trampoline offsets. |
| 144 | void UpdateOatFileHeader(size_t oat_index, const OatHeader& oat_header); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 145 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 146 | private: |
Mathieu Chartier | 4e9c4e7 | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 147 | using WorkStack = std::stack<std::pair<mirror::Object*, size_t>>; |
| 148 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 149 | bool AllocMemory(); |
| 150 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 151 | // Mark the objects defined in this space in the given live bitmap. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 152 | void RecordImageAllocations() SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 153 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 154 | // Classify different kinds of bins that objects end up getting packed into during image writing. |
Mathieu Chartier | 80c563b | 2016-04-08 19:01:05 -0700 | [diff] [blame] | 155 | // Ordered from dirtiest to cleanest (until ArtMethods). |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 156 | enum Bin { |
Mathieu Chartier | 80c563b | 2016-04-08 19:01:05 -0700 | [diff] [blame] | 157 | kBinMiscDirty, // Dex caches, object locks, etc... |
| 158 | kBinClassVerified, // Class verified, but initializers haven't been run |
Mathieu Chartier | 8ace610 | 2016-04-08 18:54:36 -0700 | [diff] [blame] | 159 | // Unknown mix of clean/dirty: |
| 160 | kBinRegular, |
Mathieu Chartier | 80c563b | 2016-04-08 19:01:05 -0700 | [diff] [blame] | 161 | kBinClassInitialized, // Class initializers have been run |
Mathieu Chartier | 8ace610 | 2016-04-08 18:54:36 -0700 | [diff] [blame] | 162 | // All classes get their own bins since their fields often dirty |
| 163 | kBinClassInitializedFinalStatics, // Class initializers have been run, no non-final statics |
Mathieu Chartier | 80c563b | 2016-04-08 19:01:05 -0700 | [diff] [blame] | 164 | // Likely-clean: |
| 165 | kBinString, // [String] Almost always immutable (except for obj header). |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 166 | // Add more bins here if we add more segregation code. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 167 | // Non mirror fields must be below. |
| 168 | // ArtFields should be always clean. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 169 | kBinArtField, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 170 | // If the class is initialized, then the ArtMethods are probably clean. |
| 171 | kBinArtMethodClean, |
| 172 | // ArtMethods may be dirty if the class has native methods or a declaring class that isn't |
| 173 | // initialized. |
| 174 | kBinArtMethodDirty, |
Artem Udovichenko | df2d4f2 | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 175 | // IMT (clean) |
| 176 | kBinImTable, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 177 | // Conflict tables (clean). |
| 178 | kBinIMTConflictTable, |
| 179 | // Runtime methods (always clean, do not have a length prefix array). |
| 180 | kBinRuntimeMethod, |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 181 | // Dex cache arrays have a special slot for PC-relative addressing. Since they are |
| 182 | // huge, and as such their dirtiness is not important for the clean/dirty separation, |
| 183 | // we arbitrarily keep them at the end of the native data. |
| 184 | kBinDexCacheArray, // Arrays belonging to dex cache. |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 185 | kBinSize, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 186 | // Number of bins which are for mirror objects. |
| 187 | kBinMirrorCount = kBinArtField, |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 188 | }; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 189 | friend std::ostream& operator<<(std::ostream& stream, const Bin& bin); |
| 190 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 191 | enum NativeObjectRelocationType { |
| 192 | kNativeObjectRelocationTypeArtField, |
| 193 | kNativeObjectRelocationTypeArtFieldArray, |
| 194 | kNativeObjectRelocationTypeArtMethodClean, |
| 195 | kNativeObjectRelocationTypeArtMethodArrayClean, |
| 196 | kNativeObjectRelocationTypeArtMethodDirty, |
| 197 | kNativeObjectRelocationTypeArtMethodArrayDirty, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 198 | kNativeObjectRelocationTypeRuntimeMethod, |
Artem Udovichenko | df2d4f2 | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 199 | kNativeObjectRelocationTypeIMTable, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 200 | kNativeObjectRelocationTypeIMTConflictTable, |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 201 | kNativeObjectRelocationTypeDexCacheArray, |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 202 | }; |
| 203 | friend std::ostream& operator<<(std::ostream& stream, const NativeObjectRelocationType& type); |
| 204 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 205 | enum OatAddress { |
| 206 | kOatAddressInterpreterToInterpreterBridge, |
| 207 | kOatAddressInterpreterToCompiledCodeBridge, |
| 208 | kOatAddressJNIDlsymLookup, |
| 209 | kOatAddressQuickGenericJNITrampoline, |
| 210 | kOatAddressQuickIMTConflictTrampoline, |
| 211 | kOatAddressQuickResolutionTrampoline, |
| 212 | kOatAddressQuickToInterpreterBridge, |
| 213 | // Number of elements in the enum. |
| 214 | kOatAddressCount, |
| 215 | }; |
| 216 | friend std::ostream& operator<<(std::ostream& stream, const OatAddress& oat_address); |
| 217 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 218 | static constexpr size_t kBinBits = MinimumBitsToStore<uint32_t>(kBinMirrorCount - 1); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 219 | // uint32 = typeof(lockword_) |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 220 | // Subtract read barrier bits since we want these to remain 0, or else it may result in DCHECK |
| 221 | // failures due to invalid read barrier bits during object field reads. |
| 222 | static const size_t kBinShift = BitSizeOf<uint32_t>() - kBinBits - |
| 223 | LockWord::kReadBarrierStateSize; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 224 | // 111000.....0 |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 225 | 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] | 226 | |
| 227 | // We use the lock word to store the bin # and bin index of the object in the image. |
| 228 | // |
| 229 | // The struct size must be exactly sizeof(LockWord), currently 32-bits, since this will end up |
| 230 | // stored in the lock word bit-for-bit when object forwarding addresses are being calculated. |
| 231 | struct BinSlot { |
| 232 | explicit BinSlot(uint32_t lockword); |
| 233 | BinSlot(Bin bin, uint32_t index); |
| 234 | |
| 235 | // The bin an object belongs to, i.e. regular, class/verified, class/initialized, etc. |
| 236 | Bin GetBin() const; |
| 237 | // The offset in bytes from the beginning of the bin. Aligned to object size. |
| 238 | uint32_t GetIndex() const; |
| 239 | // Pack into a single uint32_t, for storing into a lock word. |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 240 | uint32_t Uint32Value() const { return lockword_; } |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 241 | // Comparison operator for map support |
| 242 | bool operator<(const BinSlot& other) const { return lockword_ < other.lockword_; } |
| 243 | |
| 244 | private: |
| 245 | // Must be the same size as LockWord, any larger and we would truncate the data. |
| 246 | const uint32_t lockword_; |
| 247 | }; |
| 248 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 249 | struct ImageInfo { |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 250 | ImageInfo(); |
| 251 | ImageInfo(ImageInfo&&) = default; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 252 | |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 253 | // Create the image sections into the out sections variable, returns the size of the image |
| 254 | // excluding the bitmap. |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 255 | size_t CreateImageSections(ImageSection* out_sections) const; |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 256 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 257 | std::unique_ptr<MemMap> image_; // Memory mapped for generating the image. |
| 258 | |
| 259 | // Target begin of this image. Notes: It is not valid to write here, this is the address |
| 260 | // of the target image, not necessarily where image_ is mapped. The address is only valid |
| 261 | // after layouting (otherwise null). |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 262 | uint8_t* image_begin_ = nullptr; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 263 | |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 264 | // Offset to the free space in image_, initially size of image header. |
| 265 | size_t image_end_ = RoundUp(sizeof(ImageHeader), kObjectAlignment); |
| 266 | uint32_t image_roots_address_ = 0; // The image roots address in the image. |
| 267 | size_t image_offset_ = 0; // Offset of this image from the start of the first image. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 268 | |
| 269 | // Image size is the *address space* covered by this image. As the live bitmap is aligned |
| 270 | // to the page size, the live bitmap will cover more address space than necessary. But live |
| 271 | // bitmaps may not overlap, so an image has a "shadow," which is accounted for in the size. |
| 272 | // The next image may only start at image_begin_ + image_size_ (which is guaranteed to be |
| 273 | // page-aligned). |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 274 | size_t image_size_ = 0; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 275 | |
| 276 | // Oat data. |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 277 | // Offset of the oat file for this image from start of oat files. This is |
| 278 | // valid when the previous oat file has been written. |
| 279 | size_t oat_offset_ = 0; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 280 | // Layout of the loaded ELF file containing the oat file, valid after UpdateOatFileLayout(). |
| 281 | const uint8_t* oat_file_begin_ = nullptr; |
| 282 | size_t oat_loaded_size_ = 0; |
| 283 | const uint8_t* oat_data_begin_ = nullptr; |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 284 | size_t oat_size_ = 0; // Size of the corresponding oat data. |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 285 | // The oat header checksum, valid after UpdateOatFileHeader(). |
| 286 | uint32_t oat_checksum_ = 0u; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 287 | |
| 288 | // Image bitmap which lets us know where the objects inside of the image reside. |
| 289 | std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> image_bitmap_; |
| 290 | |
| 291 | // The start offsets of the dex cache arrays. |
| 292 | SafeMap<const DexFile*, size_t> dex_cache_array_starts_; |
| 293 | |
| 294 | // Offset from oat_data_begin_ to the stubs. |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 295 | uint32_t oat_address_offsets_[kOatAddressCount] = {}; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 296 | |
| 297 | // Bin slot tracking for dirty object packing. |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 298 | size_t bin_slot_sizes_[kBinSize] = {}; // Number of bytes in a bin. |
| 299 | size_t bin_slot_offsets_[kBinSize] = {}; // Number of bytes in previous bins. |
| 300 | size_t bin_slot_count_[kBinSize] = {}; // Number of objects in a bin. |
| 301 | |
| 302 | // Cached size of the intern table for when we allocate memory. |
| 303 | size_t intern_table_bytes_ = 0; |
| 304 | |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame] | 305 | // Number of image class table bytes. |
| 306 | size_t class_table_bytes_ = 0; |
| 307 | |
| 308 | // Intern table associated with this image for serialization. |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 309 | std::unique_ptr<InternTable> intern_table_; |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame] | 310 | |
| 311 | // Class table associated with this image for serialization. |
| 312 | std::unique_ptr<ClassTable> class_table_; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 313 | }; |
| 314 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 315 | // 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] | 316 | void AssignImageOffset(mirror::Object* object, BinSlot bin_slot) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 317 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 318 | void SetImageOffset(mirror::Object* object, size_t offset) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 319 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 320 | bool IsImageOffsetAssigned(mirror::Object* object) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 321 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 322 | size_t GetImageOffset(mirror::Object* object) const SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 323 | void UpdateImageOffset(mirror::Object* obj, uintptr_t offset) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 324 | SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 325 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 326 | void PrepareDexCacheArraySlots() SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 4e9c4e7 | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 327 | void AssignImageBinSlot(mirror::Object* object, size_t oat_index) |
| 328 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 329 | mirror::Object* TryAssignBinSlot(WorkStack& work_stack, mirror::Object* obj, size_t oat_index) |
| 330 | SHARED_REQUIRES(Locks::mutator_lock_); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 331 | void SetImageBinSlot(mirror::Object* object, BinSlot bin_slot) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 332 | SHARED_REQUIRES(Locks::mutator_lock_); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 333 | bool IsImageBinSlotAssigned(mirror::Object* object) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 334 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 335 | BinSlot GetImageBinSlot(mirror::Object* object) const SHARED_REQUIRES(Locks::mutator_lock_); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 336 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 337 | void AddDexCacheArrayRelocation(void* array, size_t offset, mirror::DexCache* dex_cache) |
| 338 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 339 | void AddMethodPointerArray(mirror::PointerArray* arr) SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 340 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 341 | static void* GetImageAddressCallback(void* writer, mirror::Object* obj) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 342 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 343 | return reinterpret_cast<ImageWriter*>(writer)->GetImageAddress(obj); |
| 344 | } |
| 345 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 346 | mirror::Object* GetLocalAddress(mirror::Object* object) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 347 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 348 | size_t offset = GetImageOffset(object); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 349 | size_t oat_index = GetOatIndex(object); |
| 350 | const ImageInfo& image_info = GetImageInfo(oat_index); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 351 | uint8_t* dst = image_info.image_->Begin() + offset; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 352 | return reinterpret_cast<mirror::Object*>(dst); |
| 353 | } |
| 354 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 355 | // Returns the address in the boot image if we are compiling the app image. |
| 356 | const uint8_t* GetOatAddress(OatAddress type) const; |
| 357 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 358 | const uint8_t* GetOatAddressForOffset(uint32_t offset, const ImageInfo& image_info) const { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 359 | // With Quick, code is within the OatFile, as there are all in one |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 360 | // .o ELF object. But interpret it as signed. |
| 361 | DCHECK_LE(static_cast<int32_t>(offset), static_cast<int32_t>(image_info.oat_size_)); |
| 362 | DCHECK(image_info.oat_data_begin_ != nullptr); |
| 363 | return offset == 0u ? nullptr : image_info.oat_data_begin_ + static_cast<int32_t>(offset); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 366 | // Returns true if the class was in the original requested image classes list. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 367 | bool KeepClass(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 368 | |
| 369 | // Debug aid that list of requested image classes. |
| 370 | void DumpImageClasses(); |
| 371 | |
| 372 | // Preinitializes some otherwise lazy fields (such as Class name) to avoid runtime image dirtying. |
| 373 | void ComputeLazyFieldsForImageClasses() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 374 | SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 375 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 376 | // Remove unwanted classes from various roots. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 377 | void PruneNonImageClasses() SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 378 | |
| 379 | // Verify unwanted classes removed. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 380 | void CheckNonImageClassesRemoved() SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 381 | static void CheckNonImageClassesRemovedCallback(mirror::Object* obj, void* arg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 382 | SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 383 | |
| 384 | // Lays out where the image objects will be at runtime. |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 385 | void CalculateNewObjectOffsets() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 386 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 4e9c4e7 | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 387 | void ProcessWorkStack(WorkStack* work_stack) |
| 388 | SHARED_REQUIRES(Locks::mutator_lock_); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 389 | void CreateHeader(size_t oat_index) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 390 | SHARED_REQUIRES(Locks::mutator_lock_); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 391 | mirror::ObjectArray<mirror::Object>* CreateImageRoots(size_t oat_index) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 392 | SHARED_REQUIRES(Locks::mutator_lock_); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 393 | void UnbinObjectsIntoOffset(mirror::Object* obj) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 394 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 395 | |
Mathieu Chartier | 4e9c4e7 | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 396 | static void EnsureBinSlotAssignedCallback(mirror::Object* obj, void* arg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 397 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 4e9c4e7 | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 398 | static void DeflateMonitorCallback(mirror::Object* obj, void* arg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 399 | SHARED_REQUIRES(Locks::mutator_lock_); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 400 | static void UnbinObjectsIntoOffsetCallback(mirror::Object* obj, void* arg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 401 | SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 402 | |
| 403 | // Creates the contiguous image in memory and adjusts pointers. |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 404 | void CopyAndFixupNativeData(size_t oat_index) SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 405 | void CopyAndFixupObjects() SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 406 | static void CopyAndFixupObjectsCallback(mirror::Object* obj, void* arg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 407 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 408 | void CopyAndFixupObject(mirror::Object* obj) SHARED_REQUIRES(Locks::mutator_lock_); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 409 | void CopyAndFixupMethod(ArtMethod* orig, ArtMethod* copy, const ImageInfo& image_info) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 410 | SHARED_REQUIRES(Locks::mutator_lock_); |
Artem Udovichenko | df2d4f2 | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 411 | void CopyAndFixupImTable(ImTable* orig, ImTable* copy) SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 412 | void CopyAndFixupImtConflictTable(ImtConflictTable* orig, ImtConflictTable* copy) |
| 413 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 414 | void FixupClass(mirror::Class* orig, mirror::Class* copy) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 415 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 416 | void FixupObject(mirror::Object* orig, mirror::Object* copy) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 417 | SHARED_REQUIRES(Locks::mutator_lock_); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 418 | void FixupDexCache(mirror::DexCache* orig_dex_cache, mirror::DexCache* copy_dex_cache) |
| 419 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 420 | void FixupPointerArray(mirror::Object* dst, |
| 421 | mirror::PointerArray* arr, |
| 422 | mirror::Class* klass, |
| 423 | Bin array_type) |
| 424 | SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 425 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 426 | // Get quick code for non-resolution/imt_conflict/abstract method. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 427 | const uint8_t* GetQuickCode(ArtMethod* method, |
| 428 | const ImageInfo& image_info, |
| 429 | bool* quick_is_interpreted) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 430 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 431 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 432 | // Calculate the sum total of the bin slot sizes in [0, up_to). Defaults to all bins. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 433 | size_t GetBinSizeSum(ImageInfo& image_info, Bin up_to = kBinSize) const; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 434 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 435 | // Return true if a method is likely to be dirtied at runtime. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 436 | bool WillMethodBeDirty(ArtMethod* m) const SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 437 | |
| 438 | // Assign the offset for an ArtMethod. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 439 | void AssignMethodOffset(ArtMethod* method, |
| 440 | NativeObjectRelocationType type, |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 441 | size_t oat_index) |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 442 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 443 | |
Artem Udovichenko | df2d4f2 | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 444 | void TryAssignImTableOffset(ImTable* imt, size_t oat_index) SHARED_REQUIRES(Locks::mutator_lock_); |
| 445 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 446 | // Assign the offset for an IMT conflict table. Does nothing if the table already has a native |
| 447 | // relocation. |
| 448 | void TryAssignConflictTableOffset(ImtConflictTable* table, size_t oat_index) |
| 449 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 450 | |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 451 | // Return true if klass is loaded by the boot class loader but not in the boot image. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 452 | bool IsBootClassLoaderNonImageClass(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_); |
| 453 | |
Mathieu Chartier | 901e070 | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 454 | // Return true if klass depends on a boot class loader non image class. We want to prune these |
| 455 | // classes since we do not want any boot class loader classes in the image. This means that |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 456 | // we also cannot have any classes which refer to these boot class loader non image classes. |
Mathieu Chartier | 901e070 | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 457 | // PruneAppImageClass also prunes if klass depends on a non-image class according to the compiler |
| 458 | // driver. |
| 459 | bool PruneAppImageClass(mirror::Class* klass) |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 460 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 461 | |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 462 | // early_exit is true if we had a cyclic dependency anywhere down the chain. |
Mathieu Chartier | 901e070 | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 463 | bool PruneAppImageClassInternal(mirror::Class* klass, |
| 464 | bool* early_exit, |
| 465 | std::unordered_set<mirror::Class*>* visited) |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 466 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 467 | |
Mathieu Chartier | 4e9c4e7 | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 468 | bool IsMultiImage() const { |
| 469 | return image_infos_.size() > 1; |
| 470 | } |
| 471 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 472 | static Bin BinTypeForNativeRelocationType(NativeObjectRelocationType type); |
| 473 | |
Mathieu Chartier | 03d21bc | 2016-03-07 10:25:04 -0800 | [diff] [blame] | 474 | uintptr_t NativeOffsetInImage(void* obj) SHARED_REQUIRES(Locks::mutator_lock_); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 475 | |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 476 | // Location of where the object will be when the image is loaded at runtime. |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 477 | template <typename T> |
Mathieu Chartier | e8bf134 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 478 | T* NativeLocationInImage(T* obj) SHARED_REQUIRES(Locks::mutator_lock_); |
Andreas Gampe | 245ee00 | 2014-12-04 21:25:04 -0800 | [diff] [blame] | 479 | |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 480 | // Location of where the temporary copy of the object currently is. |
| 481 | template <typename T> |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 482 | T* NativeCopyLocation(T* obj, mirror::DexCache* dex_cache) SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 483 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 484 | // Return true of obj is inside of the boot image space. This may only return true if we are |
| 485 | // compiling an app image. |
| 486 | bool IsInBootImage(const void* obj) const; |
| 487 | |
| 488 | // Return true if ptr is within the boot oat file. |
| 489 | bool IsInBootOatFile(const void* ptr) const; |
| 490 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 491 | // Get the index of the oat file associated with the object. |
| 492 | size_t GetOatIndex(mirror::Object* object) const SHARED_REQUIRES(Locks::mutator_lock_); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 493 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 494 | // The oat index for shared data in multi-image and all data in single-image compilation. |
| 495 | size_t GetDefaultOatIndex() const { |
| 496 | return 0u; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 499 | ImageInfo& GetImageInfo(size_t oat_index) { |
| 500 | return image_infos_[oat_index]; |
| 501 | } |
| 502 | |
| 503 | const ImageInfo& GetImageInfo(size_t oat_index) const { |
| 504 | return image_infos_[oat_index]; |
| 505 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 506 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 507 | // Find an already strong interned string in the other images or in the boot image. Used to |
| 508 | // remove duplicates in the multi image and app image case. |
| 509 | mirror::String* FindInternedString(mirror::String* string) SHARED_REQUIRES(Locks::mutator_lock_); |
| 510 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 511 | // Return true if there already exists a native allocation for an object. |
| 512 | bool NativeRelocationAssigned(void* ptr) const; |
| 513 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 514 | const CompilerDriver& compiler_driver_; |
| 515 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 516 | // Beginning target image address for the first image. |
| 517 | uint8_t* global_image_begin_; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 518 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 519 | // Offset from image_begin_ to where the first object is in image_. |
| 520 | size_t image_objects_offset_begin_; |
| 521 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 522 | // Pointer arrays that need to be updated. Since these are only some int and long arrays, we need |
| 523 | // to keep track. These include vtable arrays, iftable arrays, and dex caches. |
| 524 | std::unordered_map<mirror::PointerArray*, Bin> pointer_arrays_; |
| 525 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 526 | // Saved hash codes. We use these to restore lockwords which were temporarily used to have |
| 527 | // forwarding addresses as well as copying over hash codes. |
| 528 | std::unordered_map<mirror::Object*, uint32_t> saved_hashcode_map_; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 529 | |
Mathieu Chartier | 4e9c4e7 | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 530 | // Oat index map for objects. |
| 531 | std::unordered_map<mirror::Object*, uint32_t> oat_index_map_; |
| 532 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 533 | // Boolean flags. |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 534 | const bool compile_pic_; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 535 | const bool compile_app_image_; |
| 536 | |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 537 | // Size of pointers on the target architecture. |
| 538 | size_t target_ptr_size_; |
| 539 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 540 | // Image data indexed by the oat file index. |
| 541 | dchecked_vector<ImageInfo> image_infos_; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 542 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 543 | // ArtField, ArtMethod relocating map. These are allocated as array of structs but we want to |
| 544 | // have one entry per art field for convenience. ArtFields are placed right after the end of the |
| 545 | // 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] | 546 | struct NativeObjectRelocation { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 547 | size_t oat_index; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 548 | uintptr_t offset; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 549 | NativeObjectRelocationType type; |
| 550 | |
| 551 | bool IsArtMethodRelocation() const { |
| 552 | return type == kNativeObjectRelocationTypeArtMethodClean || |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 553 | type == kNativeObjectRelocationTypeArtMethodDirty || |
| 554 | type == kNativeObjectRelocationTypeRuntimeMethod; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 555 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 556 | }; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 557 | std::unordered_map<void*, NativeObjectRelocation> native_object_relocations_; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 558 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 559 | // Runtime ArtMethods which aren't reachable from any Class but need to be copied into the image. |
| 560 | ArtMethod* image_methods_[ImageHeader::kImageMethodsCount]; |
| 561 | |
| 562 | // Counters for measurements, used for logging only. |
| 563 | uint64_t dirty_methods_; |
| 564 | uint64_t clean_methods_; |
Andreas Gampe | 245ee00 | 2014-12-04 21:25:04 -0800 | [diff] [blame] | 565 | |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 566 | // Prune class memoization table to speed up ContainsBootClassLoaderNonImageClass. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 567 | std::unordered_map<mirror::Class*, bool> prune_class_memo_; |
| 568 | |
Mathieu Chartier | 67ad20e | 2015-12-09 15:41:09 -0800 | [diff] [blame] | 569 | // Class loaders with a class table to write out. There should only be one class loader because |
| 570 | // dex2oat loads the dex files to be compiled into a single class loader. For the boot image, |
| 571 | // null is a valid entry. |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 572 | std::unordered_set<mirror::ClassLoader*> class_loaders_; |
| 573 | |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 574 | // Which mode the image is stored as, see image.h |
| 575 | const ImageHeader::StorageMode image_storage_mode_; |
| 576 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 577 | // The file names of oat files. |
| 578 | const std::vector<const char*>& oat_filenames_; |
| 579 | |
| 580 | // Map of dex files to the indexes of oat files that they were compiled into. |
| 581 | const std::unordered_map<const DexFile*, size_t>& dex_file_oat_index_map_; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 582 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 583 | friend class ContainsBootClassLoaderNonImageClassVisitor; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 584 | friend class FixupClassVisitor; |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 585 | friend class FixupRootVisitor; |
| 586 | friend class FixupVisitor; |
Mathieu Chartier | 4e9c4e7 | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 587 | class GetRootsVisitor; |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 588 | friend class NativeLocationVisitor; |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 589 | friend class NonImageClassesVisitor; |
Mathieu Chartier | 4e9c4e7 | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 590 | class VisitReferencesVisitor; |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 591 | DISALLOW_COPY_AND_ASSIGN(ImageWriter); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 592 | }; |
| 593 | |
| 594 | } // namespace art |
| 595 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 596 | #endif // ART_COMPILER_IMAGE_WRITER_H_ |