Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_IMAGE_H_ |
| 18 | #define ART_RUNTIME_IMAGE_H_ |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 19 | |
| 20 | #include <string.h> |
| 21 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 22 | #include "base/enums.h" |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 23 | #include "base/iteration_range.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | #include "mirror/object.h" |
Andreas Gampe | 5a0430d | 2019-01-04 14:33:57 -0800 | [diff] [blame] | 25 | #include "runtime_globals.h" |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 29 | class ArtField; |
| 30 | class ArtMethod; |
Vladimir Marko | c13fbd8 | 2018-06-04 16:16:28 +0100 | [diff] [blame] | 31 | template <class MirrorType> class ObjPtr; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 32 | |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 33 | namespace linker { |
| 34 | class ImageWriter; |
| 35 | } // namespace linker |
| 36 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 37 | class ObjectVisitor { |
| 38 | public: |
| 39 | virtual ~ObjectVisitor() {} |
| 40 | |
| 41 | virtual void Visit(mirror::Object* object) = 0; |
| 42 | }; |
| 43 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 44 | class PACKED(4) ImageSection { |
| 45 | public: |
| 46 | ImageSection() : offset_(0), size_(0) { } |
| 47 | ImageSection(uint32_t offset, uint32_t size) : offset_(offset), size_(size) { } |
| 48 | ImageSection(const ImageSection& section) = default; |
| 49 | ImageSection& operator=(const ImageSection& section) = default; |
| 50 | |
| 51 | uint32_t Offset() const { |
| 52 | return offset_; |
| 53 | } |
| 54 | |
| 55 | uint32_t Size() const { |
| 56 | return size_; |
| 57 | } |
| 58 | |
| 59 | uint32_t End() const { |
| 60 | return Offset() + Size(); |
| 61 | } |
| 62 | |
| 63 | bool Contains(uint64_t offset) const { |
| 64 | return offset - offset_ < size_; |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | uint32_t offset_; |
| 69 | uint32_t size_; |
| 70 | }; |
| 71 | |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 72 | // Header of image files written by ImageWriter, read and validated by Space. |
| 73 | // Packed to object alignment since the first object follows directly after the header. |
| 74 | static_assert(kObjectAlignment == 8, "Alignment check"); |
| 75 | class PACKED(8) ImageHeader { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 76 | public: |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 77 | enum StorageMode : uint32_t { |
| 78 | kStorageModeUncompressed, |
| 79 | kStorageModeLZ4, |
Mathieu Chartier | a6e81ed | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 80 | kStorageModeLZ4HC, |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 81 | kStorageModeCount, // Number of elements in enum. |
| 82 | }; |
| 83 | static constexpr StorageMode kDefaultStorageMode = kStorageModeUncompressed; |
| 84 | |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 85 | // Solid block of the image. May be compressed or uncompressed. |
| 86 | class PACKED(4) Block final { |
| 87 | public: |
| 88 | Block(StorageMode storage_mode, |
| 89 | uint32_t data_offset, |
| 90 | uint32_t data_size, |
| 91 | uint32_t image_offset, |
| 92 | uint32_t image_size) |
| 93 | : storage_mode_(storage_mode), |
| 94 | data_offset_(data_offset), |
| 95 | data_size_(data_size), |
| 96 | image_offset_(image_offset), |
| 97 | image_size_(image_size) {} |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 98 | |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 99 | bool Decompress(uint8_t* out_ptr, const uint8_t* in_ptr, std::string* error_msg) const; |
| 100 | |
| 101 | StorageMode GetStorageMode() const { |
| 102 | return storage_mode_; |
| 103 | } |
| 104 | |
Mathieu Chartier | c6068c7 | 2018-11-13 16:00:58 -0800 | [diff] [blame] | 105 | uint32_t GetDataSize() const { |
| 106 | return data_size_; |
| 107 | } |
| 108 | |
| 109 | uint32_t GetImageSize() const { |
| 110 | return image_size_; |
| 111 | } |
| 112 | |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 113 | private: |
| 114 | // Storage method for the image, the image may be compressed. |
| 115 | StorageMode storage_mode_ = kDefaultStorageMode; |
| 116 | |
| 117 | // Compressed offset and size. |
| 118 | uint32_t data_offset_ = 0u; |
| 119 | uint32_t data_size_ = 0u; |
| 120 | |
| 121 | // Image offset and size (decompressed or mapped location). |
| 122 | uint32_t image_offset_ = 0u; |
| 123 | uint32_t image_size_ = 0u; |
| 124 | }; |
| 125 | |
| 126 | ImageHeader() {} |
Vladimir Marko | 7391c8c | 2018-11-21 17:58:44 +0000 | [diff] [blame] | 127 | ImageHeader(uint32_t image_reservation_size, |
| 128 | uint32_t component_count, |
| 129 | uint32_t image_begin, |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 130 | uint32_t image_size, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 131 | ImageSection* sections, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 132 | uint32_t image_roots, |
| 133 | uint32_t oat_checksum, |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 134 | uint32_t oat_file_begin, |
| 135 | uint32_t oat_data_begin, |
| 136 | uint32_t oat_data_end, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 137 | uint32_t oat_file_end, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 138 | uint32_t boot_image_begin, |
| 139 | uint32_t boot_image_size, |
Vladimir Marko | 92eec3a | 2019-11-05 10:59:36 +0000 | [diff] [blame] | 140 | uint32_t boot_image_component_count, |
| 141 | uint32_t boot_image_checksum, |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 142 | uint32_t pointer_size); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 143 | |
Brian Carlstrom | 68708f5 | 2013-09-03 14:15:31 -0700 | [diff] [blame] | 144 | bool IsValid() const; |
| 145 | const char* GetMagic() const; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 146 | |
Vladimir Marko | 7391c8c | 2018-11-21 17:58:44 +0000 | [diff] [blame] | 147 | uint32_t GetImageReservationSize() const { |
| 148 | return image_reservation_size_; |
| 149 | } |
| 150 | |
| 151 | uint32_t GetComponentCount() const { |
| 152 | return component_count_; |
| 153 | } |
| 154 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 155 | uint8_t* GetImageBegin() const { |
| 156 | return reinterpret_cast<uint8_t*>(image_begin_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 159 | size_t GetImageSize() const { |
Vladimir Marko | c10a0c6 | 2018-11-16 11:39:22 +0000 | [diff] [blame] | 160 | return image_size_; |
| 161 | } |
| 162 | |
| 163 | uint32_t GetImageChecksum() const { |
| 164 | return image_checksum_; |
| 165 | } |
| 166 | |
| 167 | void SetImageChecksum(uint32_t image_checksum) { |
| 168 | image_checksum_ = image_checksum; |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 171 | uint32_t GetOatChecksum() const { |
| 172 | return oat_checksum_; |
| 173 | } |
| 174 | |
Brian Carlstrom | a85b837 | 2012-10-18 17:00:32 -0700 | [diff] [blame] | 175 | void SetOatChecksum(uint32_t oat_checksum) { |
| 176 | oat_checksum_ = oat_checksum; |
| 177 | } |
| 178 | |
Mathieu Chartier | 5351da0 | 2016-02-17 16:19:53 -0800 | [diff] [blame] | 179 | // The location that the oat file was expected to be when the image was created. The actual |
| 180 | // oat file may be at a different location for application images. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 181 | uint8_t* GetOatFileBegin() const { |
| 182 | return reinterpret_cast<uint8_t*>(oat_file_begin_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 185 | uint8_t* GetOatDataBegin() const { |
| 186 | return reinterpret_cast<uint8_t*>(oat_data_begin_); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 189 | uint8_t* GetOatDataEnd() const { |
| 190 | return reinterpret_cast<uint8_t*>(oat_data_end_); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 193 | uint8_t* GetOatFileEnd() const { |
| 194 | return reinterpret_cast<uint8_t*>(oat_file_end_); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Andreas Gampe | bda1d60 | 2016-08-29 17:43:45 -0700 | [diff] [blame] | 197 | PointerSize GetPointerSize() const; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 198 | |
| 199 | uint32_t GetPointerSizeUnchecked() const { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 200 | return pointer_size_; |
| 201 | } |
| 202 | |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 203 | static std::string GetOatLocationFromImageLocation(const std::string& image) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 204 | return GetLocationFromImageLocation(image, "oat"); |
| 205 | } |
| 206 | |
| 207 | static std::string GetVdexLocationFromImageLocation(const std::string& image) { |
| 208 | return GetLocationFromImageLocation(image, "vdex"); |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 211 | enum ImageMethod { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 212 | kResolutionMethod, |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 213 | kImtConflictMethod, |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 214 | kImtUnimplementedMethod, |
Vladimir Marko | fd36f1f | 2016-08-03 18:49:58 +0100 | [diff] [blame] | 215 | kSaveAllCalleeSavesMethod, |
| 216 | kSaveRefsOnlyMethod, |
| 217 | kSaveRefsAndArgsMethod, |
Vladimir Marko | 952dbb1 | 2016-07-28 12:01:51 +0100 | [diff] [blame] | 218 | kSaveEverythingMethod, |
Mingyao Yang | 0a87a65 | 2017-04-12 13:43:15 -0700 | [diff] [blame] | 219 | kSaveEverythingMethodForClinit, |
| 220 | kSaveEverythingMethodForSuspendCheck, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 221 | kImageMethodsCount, // Number of elements in enum. |
| 222 | }; |
| 223 | |
| 224 | enum ImageRoot { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 225 | kDexCaches, |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 226 | kClassRoots, |
Vladimir Marko | f75613c | 2018-06-05 12:51:04 +0100 | [diff] [blame] | 227 | kSpecialRoots, // Different for boot image and app image, see aliases below. |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 228 | kImageRootsMax, |
Vladimir Marko | f75613c | 2018-06-05 12:51:04 +0100 | [diff] [blame] | 229 | |
| 230 | // Aliases. |
| 231 | kAppImageClassLoader = kSpecialRoots, // The class loader used to build the app image. |
| 232 | kBootImageLiveObjects = kSpecialRoots, // Array of boot image objects that must be kept live. |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 233 | }; |
| 234 | |
Vladimir Marko | 024d69f | 2019-06-13 10:52:32 +0100 | [diff] [blame] | 235 | enum BootImageLiveObjects { |
| 236 | kOomeWhenThrowingException, // Pre-allocated OOME when throwing exception. |
| 237 | kOomeWhenThrowingOome, // Pre-allocated OOME when throwing OOME. |
| 238 | kOomeWhenHandlingStackOverflow, // Pre-allocated OOME when handling StackOverflowError. |
| 239 | kNoClassDefFoundError, // Pre-allocated NoClassDefFoundError. |
| 240 | kClearedJniWeakSentinel, // Pre-allocated sentinel for cleared weak JNI references. |
| 241 | kIntrinsicObjectsStart |
| 242 | }; |
| 243 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 244 | /* |
| 245 | * This describes the number and ordering of sections inside of Boot |
| 246 | * and App Images. It is very important that changes to this struct |
| 247 | * are reflected in the compiler and loader. |
| 248 | * |
| 249 | * See: |
| 250 | * - ImageWriter::ImageInfo::CreateImageSections() |
| 251 | * - ImageWriter::Write() |
| 252 | * - ImageWriter::AllocMemory() |
| 253 | */ |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 254 | enum ImageSections { |
| 255 | kSectionObjects, |
| 256 | kSectionArtFields, |
| 257 | kSectionArtMethods, |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 258 | kSectionRuntimeMethods, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 259 | kSectionImTables, |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 260 | kSectionIMTConflictTables, |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 261 | kSectionDexCacheArrays, |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 262 | kSectionInternedStrings, |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 263 | kSectionClassTable, |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 264 | kSectionStringReferenceOffsets, |
Mathieu Chartier | 1ca718e | 2018-10-23 12:55:34 -0700 | [diff] [blame] | 265 | kSectionMetadata, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 266 | kSectionImageBitmap, |
| 267 | kSectionCount, // Number of elements in enum. |
| 268 | }; |
| 269 | |
Vladimir Marko | f75613c | 2018-06-05 12:51:04 +0100 | [diff] [blame] | 270 | static size_t NumberOfImageRoots(bool app_image ATTRIBUTE_UNUSED) { |
| 271 | // At the moment, boot image and app image have the same number of roots, |
| 272 | // though the meaning of the kSpecialRoots is different. |
| 273 | return kImageRootsMax; |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 276 | ArtMethod* GetImageMethod(ImageMethod index) const; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 277 | |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 278 | ImageSection& GetImageSection(ImageSections index) { |
| 279 | DCHECK_LT(static_cast<size_t>(index), kSectionCount); |
| 280 | return sections_[index]; |
| 281 | } |
| 282 | |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 283 | const ImageSection& GetImageSection(ImageSections index) const { |
| 284 | DCHECK_LT(static_cast<size_t>(index), kSectionCount); |
| 285 | return sections_[index]; |
| 286 | } |
| 287 | |
| 288 | const ImageSection& GetObjectsSection() const { |
| 289 | return GetImageSection(kSectionObjects); |
| 290 | } |
| 291 | |
| 292 | const ImageSection& GetFieldsSection() const { |
| 293 | return GetImageSection(ImageHeader::kSectionArtFields); |
| 294 | } |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 295 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 296 | const ImageSection& GetMethodsSection() const { |
| 297 | return GetImageSection(kSectionArtMethods); |
| 298 | } |
| 299 | |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 300 | const ImageSection& GetRuntimeMethodsSection() const { |
| 301 | return GetImageSection(kSectionRuntimeMethods); |
| 302 | } |
| 303 | |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 304 | const ImageSection& GetImTablesSection() const { |
| 305 | return GetImageSection(kSectionImTables); |
| 306 | } |
| 307 | |
| 308 | const ImageSection& GetIMTConflictTablesSection() const { |
| 309 | return GetImageSection(kSectionIMTConflictTables); |
| 310 | } |
| 311 | |
| 312 | const ImageSection& GetDexCacheArraysSection() const { |
| 313 | return GetImageSection(kSectionDexCacheArrays); |
| 314 | } |
| 315 | |
| 316 | const ImageSection& GetInternedStringsSection() const { |
| 317 | return GetImageSection(kSectionInternedStrings); |
| 318 | } |
| 319 | |
| 320 | const ImageSection& GetClassTableSection() const { |
| 321 | return GetImageSection(kSectionClassTable); |
| 322 | } |
| 323 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 324 | const ImageSection& GetImageStringReferenceOffsetsSection() const { |
| 325 | return GetImageSection(kSectionStringReferenceOffsets); |
| 326 | } |
| 327 | |
Mathieu Chartier | 1ca718e | 2018-10-23 12:55:34 -0700 | [diff] [blame] | 328 | const ImageSection& GetMetadataSection() const { |
| 329 | return GetImageSection(kSectionMetadata); |
| 330 | } |
| 331 | |
Vladimir Marko | 6d3c181 | 2018-10-24 14:00:00 +0100 | [diff] [blame] | 332 | const ImageSection& GetImageBitmapSection() const { |
| 333 | return GetImageSection(kSectionImageBitmap); |
| 334 | } |
| 335 | |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 336 | template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier> |
Vladimir Marko | c13fbd8 | 2018-06-04 16:16:28 +0100 | [diff] [blame] | 337 | ObjPtr<mirror::Object> GetImageRoot(ImageRoot image_root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 338 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 339 | |
| 340 | template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier> |
Vladimir Marko | c13fbd8 | 2018-06-04 16:16:28 +0100 | [diff] [blame] | 341 | ObjPtr<mirror::ObjectArray<mirror::Object>> GetImageRoots() const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 342 | REQUIRES_SHARED(Locks::mutator_lock_); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 343 | |
Vladimir Marko | c0b30c9 | 2019-07-23 14:58:25 +0100 | [diff] [blame] | 344 | void RelocateImageReferences(int64_t delta); |
| 345 | void RelocateBootImageReferences(int64_t delta); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 346 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 347 | uint32_t GetBootImageBegin() const { |
| 348 | return boot_image_begin_; |
| 349 | } |
| 350 | |
| 351 | uint32_t GetBootImageSize() const { |
| 352 | return boot_image_size_; |
| 353 | } |
| 354 | |
Vladimir Marko | 92eec3a | 2019-11-05 10:59:36 +0000 | [diff] [blame] | 355 | uint32_t GetBootImageComponentCount() const { |
| 356 | return boot_image_component_count_; |
| 357 | } |
| 358 | |
| 359 | uint32_t GetBootImageChecksum() const { |
| 360 | return boot_image_checksum_; |
| 361 | } |
| 362 | |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 363 | uint64_t GetDataSize() const { |
| 364 | return data_size_; |
| 365 | } |
| 366 | |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 367 | bool IsAppImage() const; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 368 | |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 369 | uint32_t GetImageSpaceCount() const; |
| 370 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 371 | // Visit mirror::Objects in the section starting at base. |
| 372 | // TODO: Delete base parameter if it is always equal to GetImageBegin. |
| 373 | void VisitObjects(ObjectVisitor* visitor, |
| 374 | uint8_t* base, |
| 375 | PointerSize pointer_size) const |
| 376 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 377 | |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 378 | // Visit ArtMethods in the section starting at base. Includes runtime methods. |
| 379 | // TODO: Delete base parameter if it is always equal to GetImageBegin. |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 380 | // NO_THREAD_SAFETY_ANALYSIS for template visitor pattern. |
| 381 | template <typename Visitor> |
| 382 | void VisitPackedArtMethods(const Visitor& visitor, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 383 | uint8_t* base, |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 384 | PointerSize pointer_size) const NO_THREAD_SAFETY_ANALYSIS; |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 385 | |
| 386 | // Visit ArtMethods in the section starting at base. |
| 387 | // TODO: Delete base parameter if it is always equal to GetImageBegin. |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 388 | // NO_THREAD_SAFETY_ANALYSIS for template visitor pattern. |
| 389 | template <typename Visitor> |
| 390 | void VisitPackedArtFields(const Visitor& visitor, uint8_t* base) const NO_THREAD_SAFETY_ANALYSIS; |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 391 | |
| 392 | template <typename Visitor> |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 393 | void VisitPackedImTables(const Visitor& visitor, |
| 394 | uint8_t* base, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 395 | PointerSize pointer_size) const; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 396 | |
| 397 | template <typename Visitor> |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 398 | void VisitPackedImtConflictTables(const Visitor& visitor, |
| 399 | uint8_t* base, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 400 | PointerSize pointer_size) const; |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 401 | |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 402 | IterationRange<const Block*> GetBlocks() const { |
| 403 | return GetBlocks(GetImageBegin()); |
| 404 | } |
| 405 | |
| 406 | IterationRange<const Block*> GetBlocks(const uint8_t* image_begin) const { |
| 407 | const Block* begin = reinterpret_cast<const Block*>(image_begin + blocks_offset_); |
| 408 | return {begin, begin + blocks_count_}; |
| 409 | } |
| 410 | |
| 411 | // Return true if the image has any compressed blocks. |
| 412 | bool HasCompressedBlock() const { |
| 413 | return blocks_count_ != 0u; |
| 414 | } |
| 415 | |
| 416 | uint32_t GetBlockCount() const { |
| 417 | return blocks_count_; |
| 418 | } |
| 419 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 420 | private: |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 421 | static const uint8_t kImageMagic[4]; |
| 422 | static const uint8_t kImageVersion[4]; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 423 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 424 | static std::string GetLocationFromImageLocation(const std::string& image, |
| 425 | const std::string& extension) { |
| 426 | std::string filename = image; |
| 427 | if (filename.length() <= 3) { |
| 428 | filename += "." + extension; |
| 429 | } else { |
| 430 | filename.replace(filename.length() - 3, 3, extension); |
| 431 | } |
| 432 | return filename; |
| 433 | } |
| 434 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 435 | uint8_t magic_[4]; |
| 436 | uint8_t version_[4]; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 437 | |
Vladimir Marko | 7391c8c | 2018-11-21 17:58:44 +0000 | [diff] [blame] | 438 | // The total memory reservation size for the image. |
| 439 | // For boot image or boot image extension, the primary image includes the reservation |
| 440 | // for all image files and oat files, secondary images have the reservation set to 0. |
| 441 | // App images have reservation equal to `image_size_` rounded up to page size because |
| 442 | // their oat files are mmapped independently. |
| 443 | uint32_t image_reservation_size_ = 0u; |
| 444 | |
| 445 | // The number of components. |
| 446 | // For boot image or boot image extension, the primary image stores the total number |
| 447 | // of images, secondary images have this set to 0. |
| 448 | // App images have 1 component. |
| 449 | uint32_t component_count_ = 0u; |
| 450 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 451 | // Required base address for mapping the image. |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 452 | uint32_t image_begin_ = 0u; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 453 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 454 | // Image size, not page aligned. |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 455 | uint32_t image_size_ = 0u; |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 456 | |
Vladimir Marko | c10a0c6 | 2018-11-16 11:39:22 +0000 | [diff] [blame] | 457 | // Image file checksum (calculated with the checksum field set to 0). |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 458 | uint32_t image_checksum_ = 0u; |
Vladimir Marko | c10a0c6 | 2018-11-16 11:39:22 +0000 | [diff] [blame] | 459 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 460 | // Checksum of the oat file we link to for load time sanity check. |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 461 | uint32_t oat_checksum_ = 0u; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 462 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 463 | // Start address for oat file. Will be before oat_data_begin_ for .so files. |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 464 | uint32_t oat_file_begin_ = 0u; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 465 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 466 | // Required oat address expected by image Method::GetCode() pointers. |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 467 | uint32_t oat_data_begin_ = 0u; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 468 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 469 | // End of oat data address range for this image file. |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 470 | uint32_t oat_data_end_ = 0u; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 471 | |
| 472 | // End of oat file address range. will be after oat_data_end_ for |
| 473 | // .so files. Used for positioning a following alloc spaces. |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 474 | uint32_t oat_file_end_ = 0u; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 475 | |
Vladimir Marko | 92eec3a | 2019-11-05 10:59:36 +0000 | [diff] [blame] | 476 | // Boot image begin and end (only applies to boot image extension and app image headers). |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 477 | uint32_t boot_image_begin_ = 0u; |
| 478 | uint32_t boot_image_size_ = 0u; // Includes heap (*.art) and code (.oat). |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 479 | |
Vladimir Marko | 92eec3a | 2019-11-05 10:59:36 +0000 | [diff] [blame] | 480 | // Number of boot image components that this image depends on and their composite checksum |
| 481 | // (only applies to boot image extension and app image headers). |
| 482 | uint32_t boot_image_component_count_ = 0u; |
| 483 | uint32_t boot_image_checksum_ = 0u; |
| 484 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 485 | // Absolute address of an Object[] of objects needed to reinitialize from an image. |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 486 | uint32_t image_roots_ = 0u; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 487 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 488 | // Pointer size, this affects the size of the ArtMethods. |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 489 | uint32_t pointer_size_ = 0u; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 490 | |
Mathieu Chartier | 67ad20e | 2015-12-09 15:41:09 -0800 | [diff] [blame] | 491 | // Image section sizes/offsets correspond to the uncompressed form. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 492 | ImageSection sections_[kSectionCount]; |
| 493 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 494 | // Image methods, may be inside of the boot image for app images. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 495 | uint64_t image_methods_[kImageMethodsCount]; |
| 496 | |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 497 | // Data size for the image data excluding the bitmap and the header. For compressed images, this |
| 498 | // is the compressed size in the file. |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 499 | uint32_t data_size_ = 0u; |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 500 | |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 501 | // Image blocks, only used for compressed images. |
| 502 | uint32_t blocks_offset_ = 0u; |
| 503 | uint32_t blocks_count_ = 0u; |
| 504 | |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 505 | friend class linker::ImageWriter; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 506 | }; |
| 507 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 508 | /* |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 509 | * This type holds the information necessary to fix up AppImage string |
| 510 | * references. |
| 511 | * |
| 512 | * The first element of the pair is an offset into the image space. If the |
| 513 | * offset is tagged (testable using HasDexCacheNativeRefTag) it indicates the location |
| 514 | * of a DexCache object that has one or more native references to managed |
| 515 | * strings that need to be fixed up. In this case the second element has no |
| 516 | * meaningful value. |
| 517 | * |
| 518 | * If the first element isn't tagged then it indicates the location of a |
| 519 | * managed object with a field that needs fixing up. In this case the second |
| 520 | * element of the pair is an object-relative offset to the field in question. |
| 521 | */ |
| 522 | typedef std::pair<uint32_t, uint32_t> AppImageReferenceOffsetInfo; |
| 523 | |
| 524 | /* |
| 525 | * Tags the last bit. Used by AppImage logic to differentiate between pointers |
| 526 | * to managed objects and pointers to native reference arrays. |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 527 | */ |
| 528 | template<typename T> |
Mathieu Chartier | 1ca718e | 2018-10-23 12:55:34 -0700 | [diff] [blame] | 529 | T SetDexCacheStringNativeRefTag(T val) { |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 530 | static_assert(std::is_integral<T>::value, "Expected integral type."); |
| 531 | |
| 532 | return val | 1u; |
| 533 | } |
| 534 | |
| 535 | /* |
Mathieu Chartier | 1ca718e | 2018-10-23 12:55:34 -0700 | [diff] [blame] | 536 | * Tags the second last bit. Used by AppImage logic to differentiate between pointers |
| 537 | * to managed objects and pointers to native reference arrays. |
| 538 | */ |
| 539 | template<typename T> |
| 540 | T SetDexCachePreResolvedStringNativeRefTag(T val) { |
| 541 | static_assert(std::is_integral<T>::value, "Expected integral type."); |
| 542 | |
| 543 | return val | 2u; |
| 544 | } |
| 545 | |
| 546 | /* |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 547 | * Retrieves the value of the last bit. Used by AppImage logic to |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 548 | * differentiate between pointers to managed objects and pointers to native |
| 549 | * reference arrays. |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 550 | */ |
| 551 | template<typename T> |
Mathieu Chartier | 1ca718e | 2018-10-23 12:55:34 -0700 | [diff] [blame] | 552 | bool HasDexCacheStringNativeRefTag(T val) { |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 553 | static_assert(std::is_integral<T>::value, "Expected integral type."); |
| 554 | |
Mathieu Chartier | 1ca718e | 2018-10-23 12:55:34 -0700 | [diff] [blame] | 555 | return (val & 1u) != 0u; |
| 556 | } |
| 557 | |
| 558 | /* |
| 559 | * Retrieves the value of the second last bit. Used by AppImage logic to |
| 560 | * differentiate between pointers to managed objects and pointers to native |
| 561 | * reference arrays. |
| 562 | */ |
| 563 | template<typename T> |
| 564 | bool HasDexCachePreResolvedStringNativeRefTag(T val) { |
| 565 | static_assert(std::is_integral<T>::value, "Expected integral type."); |
| 566 | |
| 567 | return (val & 2u) != 0u; |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /* |
| 571 | * Sets the last bit of the value to 0. Used by AppImage logic to |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 572 | * differentiate between pointers to managed objects and pointers to native |
| 573 | * reference arrays. |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 574 | */ |
| 575 | template<typename T> |
Mathieu Chartier | 1ca718e | 2018-10-23 12:55:34 -0700 | [diff] [blame] | 576 | T ClearDexCacheNativeRefTags(T val) { |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 577 | static_assert(std::is_integral<T>::value, "Expected integral type."); |
| 578 | |
Mathieu Chartier | 1ca718e | 2018-10-23 12:55:34 -0700 | [diff] [blame] | 579 | return val & ~3u; |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Vladimir Marko | 9974e3c | 2020-06-10 16:27:06 +0100 | [diff] [blame] | 582 | std::ostream& operator<<(std::ostream& os, ImageHeader::ImageMethod method); |
| 583 | std::ostream& operator<<(std::ostream& os, ImageHeader::ImageRoot root); |
| 584 | std::ostream& operator<<(std::ostream& os, ImageHeader::ImageSections section); |
| 585 | std::ostream& operator<<(std::ostream& os, ImageHeader::StorageMode mode); |
| 586 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 587 | std::ostream& operator<<(std::ostream& os, const ImageSection& section); |
| 588 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 589 | } // namespace art |
| 590 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 591 | #endif // ART_RUNTIME_IMAGE_H_ |