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" |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame] | 23 | #include "base/globals.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | #include "mirror/object.h" |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 28 | class ArtField; |
| 29 | class ArtMethod; |
Vladimir Marko | c13fbd8 | 2018-06-04 16:16:28 +0100 | [diff] [blame] | 30 | template <class MirrorType> class ObjPtr; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 31 | |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 32 | namespace linker { |
| 33 | class ImageWriter; |
| 34 | } // namespace linker |
| 35 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 36 | class ObjectVisitor { |
| 37 | public: |
| 38 | virtual ~ObjectVisitor() {} |
| 39 | |
| 40 | virtual void Visit(mirror::Object* object) = 0; |
| 41 | }; |
| 42 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 43 | class ArtMethodVisitor { |
| 44 | public: |
| 45 | virtual ~ArtMethodVisitor() {} |
| 46 | |
| 47 | virtual void Visit(ArtMethod* method) = 0; |
| 48 | }; |
| 49 | |
| 50 | class ArtFieldVisitor { |
| 51 | public: |
| 52 | virtual ~ArtFieldVisitor() {} |
| 53 | |
| 54 | virtual void Visit(ArtField* method) = 0; |
| 55 | }; |
| 56 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 57 | class PACKED(4) ImageSection { |
| 58 | public: |
| 59 | ImageSection() : offset_(0), size_(0) { } |
| 60 | ImageSection(uint32_t offset, uint32_t size) : offset_(offset), size_(size) { } |
| 61 | ImageSection(const ImageSection& section) = default; |
| 62 | ImageSection& operator=(const ImageSection& section) = default; |
| 63 | |
| 64 | uint32_t Offset() const { |
| 65 | return offset_; |
| 66 | } |
| 67 | |
| 68 | uint32_t Size() const { |
| 69 | return size_; |
| 70 | } |
| 71 | |
| 72 | uint32_t End() const { |
| 73 | return Offset() + Size(); |
| 74 | } |
| 75 | |
| 76 | bool Contains(uint64_t offset) const { |
| 77 | return offset - offset_ < size_; |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | uint32_t offset_; |
| 82 | uint32_t size_; |
| 83 | }; |
| 84 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 85 | // header of image files written by ImageWriter, read and validated by Space. |
Ian Rogers | df1ce91 | 2012-11-27 17:07:11 -0800 | [diff] [blame] | 86 | class PACKED(4) ImageHeader { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 87 | public: |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 88 | enum StorageMode : uint32_t { |
| 89 | kStorageModeUncompressed, |
| 90 | kStorageModeLZ4, |
Mathieu Chartier | a6e81ed | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 91 | kStorageModeLZ4HC, |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 92 | kStorageModeCount, // Number of elements in enum. |
| 93 | }; |
| 94 | static constexpr StorageMode kDefaultStorageMode = kStorageModeUncompressed; |
| 95 | |
Sebastien Hertz | aa50d3a | 2015-08-25 15:25:41 +0200 | [diff] [blame] | 96 | ImageHeader() |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 97 | : image_begin_(0U), |
| 98 | image_size_(0U), |
| 99 | oat_checksum_(0U), |
| 100 | oat_file_begin_(0U), |
| 101 | oat_data_begin_(0U), |
| 102 | oat_data_end_(0U), |
| 103 | oat_file_end_(0U), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 104 | boot_image_begin_(0U), |
| 105 | boot_image_size_(0U), |
| 106 | boot_oat_begin_(0U), |
| 107 | boot_oat_size_(0U), |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 108 | patch_delta_(0), |
| 109 | image_roots_(0U), |
| 110 | pointer_size_(0U), |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 111 | storage_mode_(kDefaultStorageMode), |
| 112 | data_size_(0) {} |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 113 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 114 | ImageHeader(uint32_t image_begin, |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 115 | uint32_t image_size, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 116 | ImageSection* sections, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 117 | uint32_t image_roots, |
| 118 | uint32_t oat_checksum, |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 119 | uint32_t oat_file_begin, |
| 120 | uint32_t oat_data_begin, |
| 121 | uint32_t oat_data_end, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 122 | uint32_t oat_file_end, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 123 | uint32_t boot_image_begin, |
| 124 | uint32_t boot_image_size, |
| 125 | uint32_t boot_oat_begin, |
| 126 | uint32_t boot_oat_size, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 127 | uint32_t pointer_size, |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 128 | StorageMode storage_mode, |
| 129 | size_t data_size); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 130 | |
Brian Carlstrom | 68708f5 | 2013-09-03 14:15:31 -0700 | [diff] [blame] | 131 | bool IsValid() const; |
| 132 | const char* GetMagic() const; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 133 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 134 | uint8_t* GetImageBegin() const { |
| 135 | return reinterpret_cast<uint8_t*>(image_begin_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 138 | size_t GetImageSize() const { |
| 139 | return static_cast<uint32_t>(image_size_); |
| 140 | } |
| 141 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 142 | uint32_t GetOatChecksum() const { |
| 143 | return oat_checksum_; |
| 144 | } |
| 145 | |
Brian Carlstrom | a85b837 | 2012-10-18 17:00:32 -0700 | [diff] [blame] | 146 | void SetOatChecksum(uint32_t oat_checksum) { |
| 147 | oat_checksum_ = oat_checksum; |
| 148 | } |
| 149 | |
Mathieu Chartier | 5351da0 | 2016-02-17 16:19:53 -0800 | [diff] [blame] | 150 | // The location that the oat file was expected to be when the image was created. The actual |
| 151 | // oat file may be at a different location for application images. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 152 | uint8_t* GetOatFileBegin() const { |
| 153 | return reinterpret_cast<uint8_t*>(oat_file_begin_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 156 | uint8_t* GetOatDataBegin() const { |
| 157 | return reinterpret_cast<uint8_t*>(oat_data_begin_); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 160 | uint8_t* GetOatDataEnd() const { |
| 161 | return reinterpret_cast<uint8_t*>(oat_data_end_); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 164 | uint8_t* GetOatFileEnd() const { |
| 165 | return reinterpret_cast<uint8_t*>(oat_file_end_); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Andreas Gampe | bda1d60 | 2016-08-29 17:43:45 -0700 | [diff] [blame] | 168 | PointerSize GetPointerSize() const; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 169 | |
| 170 | uint32_t GetPointerSizeUnchecked() const { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 171 | return pointer_size_; |
| 172 | } |
| 173 | |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 174 | int32_t GetPatchDelta() const { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 175 | return patch_delta_; |
| 176 | } |
| 177 | |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 178 | void SetPatchDelta(int32_t patch_delta) { |
Alex Klyubin | d1d5c95 | 2017-12-15 12:57:33 -0800 | [diff] [blame] | 179 | patch_delta_ = patch_delta; |
| 180 | } |
| 181 | |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 182 | static std::string GetOatLocationFromImageLocation(const std::string& image) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 183 | return GetLocationFromImageLocation(image, "oat"); |
| 184 | } |
| 185 | |
| 186 | static std::string GetVdexLocationFromImageLocation(const std::string& image) { |
| 187 | return GetLocationFromImageLocation(image, "vdex"); |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 190 | enum ImageMethod { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 191 | kResolutionMethod, |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 192 | kImtConflictMethod, |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 193 | kImtUnimplementedMethod, |
Vladimir Marko | fd36f1f | 2016-08-03 18:49:58 +0100 | [diff] [blame] | 194 | kSaveAllCalleeSavesMethod, |
| 195 | kSaveRefsOnlyMethod, |
| 196 | kSaveRefsAndArgsMethod, |
Vladimir Marko | 952dbb1 | 2016-07-28 12:01:51 +0100 | [diff] [blame] | 197 | kSaveEverythingMethod, |
Mingyao Yang | 0a87a65 | 2017-04-12 13:43:15 -0700 | [diff] [blame] | 198 | kSaveEverythingMethodForClinit, |
| 199 | kSaveEverythingMethodForSuspendCheck, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 200 | kImageMethodsCount, // Number of elements in enum. |
| 201 | }; |
| 202 | |
| 203 | enum ImageRoot { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 204 | kDexCaches, |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 205 | kClassRoots, |
Vladimir Marko | c13fbd8 | 2018-06-04 16:16:28 +0100 | [diff] [blame] | 206 | kOomeWhenThrowingException, // Pre-allocated OOME when throwing exception. |
| 207 | kOomeWhenThrowingOome, // Pre-allocated OOME when throwing OOME. |
| 208 | kOomeWhenHandlingStackOverflow, // Pre-allocated OOME when handling StackOverflowError. |
| 209 | kNoClassDefFoundError, // Pre-allocated NoClassDefFoundError. |
Vladimir Marko | f75613c | 2018-06-05 12:51:04 +0100 | [diff] [blame] | 210 | kSpecialRoots, // Different for boot image and app image, see aliases below. |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 211 | kImageRootsMax, |
Vladimir Marko | f75613c | 2018-06-05 12:51:04 +0100 | [diff] [blame] | 212 | |
| 213 | // Aliases. |
| 214 | kAppImageClassLoader = kSpecialRoots, // The class loader used to build the app image. |
| 215 | kBootImageLiveObjects = kSpecialRoots, // Array of boot image objects that must be kept live. |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 216 | }; |
| 217 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 218 | /* |
| 219 | * This describes the number and ordering of sections inside of Boot |
| 220 | * and App Images. It is very important that changes to this struct |
| 221 | * are reflected in the compiler and loader. |
| 222 | * |
| 223 | * See: |
| 224 | * - ImageWriter::ImageInfo::CreateImageSections() |
| 225 | * - ImageWriter::Write() |
| 226 | * - ImageWriter::AllocMemory() |
| 227 | */ |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 228 | enum ImageSections { |
| 229 | kSectionObjects, |
| 230 | kSectionArtFields, |
| 231 | kSectionArtMethods, |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 232 | kSectionRuntimeMethods, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 233 | kSectionImTables, |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 234 | kSectionIMTConflictTables, |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 235 | kSectionDexCacheArrays, |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 236 | kSectionInternedStrings, |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 237 | kSectionClassTable, |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 238 | kSectionStringReferenceOffsets, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 239 | kSectionImageBitmap, |
Vladimir Marko | 6121aa6 | 2018-07-06 10:04:35 +0100 | [diff] [blame] | 240 | kSectionImageRelocations, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 241 | kSectionCount, // Number of elements in enum. |
| 242 | }; |
| 243 | |
Vladimir Marko | f75613c | 2018-06-05 12:51:04 +0100 | [diff] [blame] | 244 | static size_t NumberOfImageRoots(bool app_image ATTRIBUTE_UNUSED) { |
| 245 | // At the moment, boot image and app image have the same number of roots, |
| 246 | // though the meaning of the kSpecialRoots is different. |
| 247 | return kImageRootsMax; |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 250 | ArtMethod* GetImageMethod(ImageMethod index) const; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 251 | |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 252 | const ImageSection& GetImageSection(ImageSections index) const { |
| 253 | DCHECK_LT(static_cast<size_t>(index), kSectionCount); |
| 254 | return sections_[index]; |
| 255 | } |
| 256 | |
| 257 | const ImageSection& GetObjectsSection() const { |
| 258 | return GetImageSection(kSectionObjects); |
| 259 | } |
| 260 | |
| 261 | const ImageSection& GetFieldsSection() const { |
| 262 | return GetImageSection(ImageHeader::kSectionArtFields); |
| 263 | } |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 264 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 265 | const ImageSection& GetMethodsSection() const { |
| 266 | return GetImageSection(kSectionArtMethods); |
| 267 | } |
| 268 | |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 269 | const ImageSection& GetRuntimeMethodsSection() const { |
| 270 | return GetImageSection(kSectionRuntimeMethods); |
| 271 | } |
| 272 | |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 273 | const ImageSection& GetImTablesSection() const { |
| 274 | return GetImageSection(kSectionImTables); |
| 275 | } |
| 276 | |
| 277 | const ImageSection& GetIMTConflictTablesSection() const { |
| 278 | return GetImageSection(kSectionIMTConflictTables); |
| 279 | } |
| 280 | |
| 281 | const ImageSection& GetDexCacheArraysSection() const { |
| 282 | return GetImageSection(kSectionDexCacheArrays); |
| 283 | } |
| 284 | |
| 285 | const ImageSection& GetInternedStringsSection() const { |
| 286 | return GetImageSection(kSectionInternedStrings); |
| 287 | } |
| 288 | |
| 289 | const ImageSection& GetClassTableSection() const { |
| 290 | return GetImageSection(kSectionClassTable); |
| 291 | } |
| 292 | |
| 293 | const ImageSection& GetImageBitmapSection() const { |
| 294 | return GetImageSection(kSectionImageBitmap); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Vladimir Marko | 6121aa6 | 2018-07-06 10:04:35 +0100 | [diff] [blame] | 297 | const ImageSection& GetImageRelocationsSection() const { |
| 298 | return GetImageSection(kSectionImageRelocations); |
| 299 | } |
| 300 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 301 | const ImageSection& GetImageStringReferenceOffsetsSection() const { |
| 302 | return GetImageSection(kSectionStringReferenceOffsets); |
| 303 | } |
| 304 | |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 305 | template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier> |
Vladimir Marko | c13fbd8 | 2018-06-04 16:16:28 +0100 | [diff] [blame] | 306 | ObjPtr<mirror::Object> GetImageRoot(ImageRoot image_root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 307 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 308 | |
| 309 | template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier> |
Vladimir Marko | c13fbd8 | 2018-06-04 16:16:28 +0100 | [diff] [blame] | 310 | ObjPtr<mirror::ObjectArray<mirror::Object>> GetImageRoots() const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 311 | REQUIRES_SHARED(Locks::mutator_lock_); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 312 | |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 313 | void RelocateImage(int64_t delta); |
| 314 | void RelocateImageMethods(int64_t delta); |
| 315 | void RelocateImageObjects(int64_t delta); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 316 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 317 | uint32_t GetBootImageBegin() const { |
| 318 | return boot_image_begin_; |
| 319 | } |
| 320 | |
| 321 | uint32_t GetBootImageSize() const { |
| 322 | return boot_image_size_; |
| 323 | } |
| 324 | |
| 325 | uint32_t GetBootOatBegin() const { |
| 326 | return boot_oat_begin_; |
| 327 | } |
| 328 | |
| 329 | uint32_t GetBootOatSize() const { |
| 330 | return boot_oat_size_; |
| 331 | } |
| 332 | |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 333 | StorageMode GetStorageMode() const { |
| 334 | return storage_mode_; |
| 335 | } |
| 336 | |
| 337 | uint64_t GetDataSize() const { |
| 338 | return data_size_; |
| 339 | } |
| 340 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 341 | bool IsAppImage() const { |
| 342 | // App images currently require a boot image, if the size is non zero then it is an app image |
| 343 | // header. |
| 344 | return boot_image_size_ != 0u; |
| 345 | } |
| 346 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 347 | // Visit mirror::Objects in the section starting at base. |
| 348 | // TODO: Delete base parameter if it is always equal to GetImageBegin. |
| 349 | void VisitObjects(ObjectVisitor* visitor, |
| 350 | uint8_t* base, |
| 351 | PointerSize pointer_size) const |
| 352 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 353 | |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 354 | // Visit ArtMethods in the section starting at base. Includes runtime methods. |
| 355 | // TODO: Delete base parameter if it is always equal to GetImageBegin. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 356 | void VisitPackedArtMethods(ArtMethodVisitor* visitor, |
| 357 | uint8_t* base, |
| 358 | PointerSize pointer_size) const; |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 359 | |
| 360 | // Visit ArtMethods in the section starting at base. |
| 361 | // TODO: Delete base parameter if it is always equal to GetImageBegin. |
| 362 | void VisitPackedArtFields(ArtFieldVisitor* visitor, uint8_t* base) const; |
| 363 | |
| 364 | template <typename Visitor> |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 365 | void VisitPackedImTables(const Visitor& visitor, |
| 366 | uint8_t* base, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 367 | PointerSize pointer_size) const; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 368 | |
| 369 | template <typename Visitor> |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 370 | void VisitPackedImtConflictTables(const Visitor& visitor, |
| 371 | uint8_t* base, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 372 | PointerSize pointer_size) const; |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 373 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 374 | private: |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 375 | static const uint8_t kImageMagic[4]; |
| 376 | static const uint8_t kImageVersion[4]; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 377 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 378 | static std::string GetLocationFromImageLocation(const std::string& image, |
| 379 | const std::string& extension) { |
| 380 | std::string filename = image; |
| 381 | if (filename.length() <= 3) { |
| 382 | filename += "." + extension; |
| 383 | } else { |
| 384 | filename.replace(filename.length() - 3, 3, extension); |
| 385 | } |
| 386 | return filename; |
| 387 | } |
| 388 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 389 | uint8_t magic_[4]; |
| 390 | uint8_t version_[4]; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 391 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 392 | // Required base address for mapping the image. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 393 | uint32_t image_begin_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 394 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 395 | // Image size, not page aligned. |
| 396 | uint32_t image_size_; |
| 397 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 398 | // Checksum of the oat file we link to for load time sanity check. |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 399 | uint32_t oat_checksum_; |
| 400 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 401 | // Start address for oat file. Will be before oat_data_begin_ for .so files. |
| 402 | uint32_t oat_file_begin_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 403 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 404 | // Required oat address expected by image Method::GetCode() pointers. |
| 405 | uint32_t oat_data_begin_; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 406 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 407 | // End of oat data address range for this image file. |
| 408 | uint32_t oat_data_end_; |
| 409 | |
| 410 | // End of oat file address range. will be after oat_data_end_ for |
| 411 | // .so files. Used for positioning a following alloc spaces. |
| 412 | uint32_t oat_file_end_; |
| 413 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 414 | // Boot image begin and end (app image headers only). |
| 415 | uint32_t boot_image_begin_; |
| 416 | uint32_t boot_image_size_; |
| 417 | |
| 418 | // Boot oat begin and end (app image headers only). |
| 419 | uint32_t boot_oat_begin_; |
| 420 | uint32_t boot_oat_size_; |
| 421 | |
| 422 | // TODO: We should probably insert a boot image checksum for app images. |
| 423 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 424 | // The total delta that this image has been patched. |
| 425 | int32_t patch_delta_; |
| 426 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 427 | // Absolute address of an Object[] of objects needed to reinitialize from an image. |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 428 | uint32_t image_roots_; |
| 429 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 430 | // Pointer size, this affects the size of the ArtMethods. |
| 431 | uint32_t pointer_size_; |
| 432 | |
Mathieu Chartier | 67ad20e | 2015-12-09 15:41:09 -0800 | [diff] [blame] | 433 | // Image section sizes/offsets correspond to the uncompressed form. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 434 | ImageSection sections_[kSectionCount]; |
| 435 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 436 | // Image methods, may be inside of the boot image for app images. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 437 | uint64_t image_methods_[kImageMethodsCount]; |
| 438 | |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 439 | // Storage method for the image, the image may be compressed. |
| 440 | StorageMode storage_mode_; |
| 441 | |
| 442 | // Data size for the image data excluding the bitmap and the header. For compressed images, this |
| 443 | // is the compressed size in the file. |
| 444 | uint32_t data_size_; |
| 445 | |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 446 | friend class linker::ImageWriter; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 447 | }; |
| 448 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 449 | /* |
| 450 | * Tags the last bit. Used by AppImage logic to differentiate between managed |
| 451 | * and native references. |
| 452 | */ |
| 453 | template<typename T> |
| 454 | T SetNativeRefTag(T val) { |
| 455 | static_assert(std::is_integral<T>::value, "Expected integral type."); |
| 456 | |
| 457 | return val | 1u; |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * Retrieves the value of the last bit. Used by AppImage logic to |
| 462 | * differentiate between managed and native references. |
| 463 | */ |
| 464 | template<typename T> |
| 465 | bool HasNativeRefTag(T val) { |
| 466 | static_assert(std::is_integral<T>::value, "Expected integral type."); |
| 467 | |
| 468 | return (val & 1u) == 1u; |
| 469 | } |
| 470 | |
| 471 | /* |
| 472 | * Sets the last bit of the value to 0. Used by AppImage logic to |
| 473 | * differentiate between managed and native references. |
| 474 | */ |
| 475 | template<typename T> |
| 476 | T ClearNativeRefTag(T val) { |
| 477 | static_assert(std::is_integral<T>::value, "Expected integral type."); |
| 478 | |
| 479 | return val & ~1u; |
| 480 | } |
| 481 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 482 | std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageMethod& policy); |
| 483 | std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageRoot& policy); |
| 484 | std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageSections& section); |
| 485 | std::ostream& operator<<(std::ostream& os, const ImageSection& section); |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 486 | std::ostream& operator<<(std::ostream& os, const ImageHeader::StorageMode& mode); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 487 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 488 | } // namespace art |
| 489 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 490 | #endif // ART_RUNTIME_IMAGE_H_ |