blob: 76fb3b70c94d1df427a39bc031f52983b78d85bf [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Carlstrom4a289ed2011-08-16 17:17:49 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_IMAGE_H_
18#define ART_RUNTIME_IMAGE_H_
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070019
20#include <string.h>
21
Andreas Gampe542451c2016-07-26 09:02:02 -070022#include "base/enums.h"
David Sehr1979c642018-04-26 14:41:18 -070023#include "base/globals.h"
Mathieu Chartier1a842962018-11-13 15:09:51 -080024#include "base/iteration_range.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/object.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070026
27namespace art {
28
Mathieu Chartier54d220e2015-07-30 16:20:06 -070029class ArtField;
30class ArtMethod;
Vladimir Markoc13fbd82018-06-04 16:16:28 +010031template <class MirrorType> class ObjPtr;
Mathieu Chartier54d220e2015-07-30 16:20:06 -070032
Vladimir Marko74527972016-11-29 15:57:32 +000033namespace linker {
34class ImageWriter;
35} // namespace linker
36
David Sehra49e0532017-08-25 08:05:29 -070037class ObjectVisitor {
38 public:
39 virtual ~ObjectVisitor() {}
40
41 virtual void Visit(mirror::Object* object) = 0;
42};
43
Mathieu Chartier54d220e2015-07-30 16:20:06 -070044class ArtMethodVisitor {
45 public:
46 virtual ~ArtMethodVisitor() {}
47
48 virtual void Visit(ArtMethod* method) = 0;
49};
50
51class ArtFieldVisitor {
52 public:
53 virtual ~ArtFieldVisitor() {}
54
55 virtual void Visit(ArtField* method) = 0;
56};
57
Mathieu Chartiere401d142015-04-22 13:56:20 -070058class PACKED(4) ImageSection {
59 public:
60 ImageSection() : offset_(0), size_(0) { }
61 ImageSection(uint32_t offset, uint32_t size) : offset_(offset), size_(size) { }
62 ImageSection(const ImageSection& section) = default;
63 ImageSection& operator=(const ImageSection& section) = default;
64
65 uint32_t Offset() const {
66 return offset_;
67 }
68
69 uint32_t Size() const {
70 return size_;
71 }
72
73 uint32_t End() const {
74 return Offset() + Size();
75 }
76
77 bool Contains(uint64_t offset) const {
78 return offset - offset_ < size_;
79 }
80
81 private:
82 uint32_t offset_;
83 uint32_t size_;
84};
85
Mathieu Chartier1a842962018-11-13 15:09:51 -080086// Header of image files written by ImageWriter, read and validated by Space.
87// Packed to object alignment since the first object follows directly after the header.
88static_assert(kObjectAlignment == 8, "Alignment check");
89class PACKED(8) ImageHeader {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070090 public:
Mathieu Chartierceb07b32015-12-10 09:33:21 -080091 enum StorageMode : uint32_t {
92 kStorageModeUncompressed,
93 kStorageModeLZ4,
Mathieu Chartiera6e81ed2016-02-25 13:52:10 -080094 kStorageModeLZ4HC,
Mathieu Chartierceb07b32015-12-10 09:33:21 -080095 kStorageModeCount, // Number of elements in enum.
96 };
97 static constexpr StorageMode kDefaultStorageMode = kStorageModeUncompressed;
98
Mathieu Chartier1a842962018-11-13 15:09:51 -080099 // Solid block of the image. May be compressed or uncompressed.
100 class PACKED(4) Block final {
101 public:
102 Block(StorageMode storage_mode,
103 uint32_t data_offset,
104 uint32_t data_size,
105 uint32_t image_offset,
106 uint32_t image_size)
107 : storage_mode_(storage_mode),
108 data_offset_(data_offset),
109 data_size_(data_size),
110 image_offset_(image_offset),
111 image_size_(image_size) {}
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700112
Mathieu Chartier1a842962018-11-13 15:09:51 -0800113 bool Decompress(uint8_t* out_ptr, const uint8_t* in_ptr, std::string* error_msg) const;
114
115 StorageMode GetStorageMode() const {
116 return storage_mode_;
117 }
118
119 private:
120 // Storage method for the image, the image may be compressed.
121 StorageMode storage_mode_ = kDefaultStorageMode;
122
123 // Compressed offset and size.
124 uint32_t data_offset_ = 0u;
125 uint32_t data_size_ = 0u;
126
127 // Image offset and size (decompressed or mapped location).
128 uint32_t image_offset_ = 0u;
129 uint32_t image_size_ = 0u;
130 };
131
132 ImageHeader() {}
Ian Rogers30fab402012-01-23 15:43:46 -0800133 ImageHeader(uint32_t image_begin,
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800134 uint32_t image_size,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700135 ImageSection* sections,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700136 uint32_t image_roots,
137 uint32_t oat_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800138 uint32_t oat_file_begin,
139 uint32_t oat_data_begin,
140 uint32_t oat_data_end,
Igor Murashkin46774762014-10-22 11:37:02 -0700141 uint32_t oat_file_end,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800142 uint32_t boot_image_begin,
143 uint32_t boot_image_size,
Mathieu Chartier1a842962018-11-13 15:09:51 -0800144 uint32_t pointer_size);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700145
Brian Carlstrom68708f52013-09-03 14:15:31 -0700146 bool IsValid() const;
147 const char* GetMagic() const;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700148
Ian Rogers13735952014-10-08 12:43:28 -0700149 uint8_t* GetImageBegin() const {
150 return reinterpret_cast<uint8_t*>(image_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700151 }
152
Mathieu Chartier31e89252013-08-28 11:29:12 -0700153 size_t GetImageSize() const {
Vladimir Markoc10a0c62018-11-16 11:39:22 +0000154 return image_size_;
155 }
156
157 uint32_t GetImageChecksum() const {
158 return image_checksum_;
159 }
160
161 void SetImageChecksum(uint32_t image_checksum) {
162 image_checksum_ = image_checksum;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700163 }
164
Brian Carlstrome24fa612011-09-29 00:53:55 -0700165 uint32_t GetOatChecksum() const {
166 return oat_checksum_;
167 }
168
Brian Carlstroma85b8372012-10-18 17:00:32 -0700169 void SetOatChecksum(uint32_t oat_checksum) {
170 oat_checksum_ = oat_checksum;
171 }
172
Mathieu Chartier5351da02016-02-17 16:19:53 -0800173 // The location that the oat file was expected to be when the image was created. The actual
174 // oat file may be at a different location for application images.
Ian Rogers13735952014-10-08 12:43:28 -0700175 uint8_t* GetOatFileBegin() const {
176 return reinterpret_cast<uint8_t*>(oat_file_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700177 }
178
Ian Rogers13735952014-10-08 12:43:28 -0700179 uint8_t* GetOatDataBegin() const {
180 return reinterpret_cast<uint8_t*>(oat_data_begin_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800181 }
182
Ian Rogers13735952014-10-08 12:43:28 -0700183 uint8_t* GetOatDataEnd() const {
184 return reinterpret_cast<uint8_t*>(oat_data_end_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800185 }
186
Ian Rogers13735952014-10-08 12:43:28 -0700187 uint8_t* GetOatFileEnd() const {
188 return reinterpret_cast<uint8_t*>(oat_file_end_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700189 }
190
Andreas Gampebda1d602016-08-29 17:43:45 -0700191 PointerSize GetPointerSize() const;
Andreas Gampe542451c2016-07-26 09:02:02 -0700192
193 uint32_t GetPointerSizeUnchecked() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700194 return pointer_size_;
195 }
196
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000197 static std::string GetOatLocationFromImageLocation(const std::string& image) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100198 return GetLocationFromImageLocation(image, "oat");
199 }
200
201 static std::string GetVdexLocationFromImageLocation(const std::string& image) {
202 return GetLocationFromImageLocation(image, "vdex");
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000203 }
204
Mathieu Chartiere401d142015-04-22 13:56:20 -0700205 enum ImageMethod {
Ian Rogers19846512012-02-24 11:42:47 -0800206 kResolutionMethod,
Jeff Hao88474b42013-10-23 16:24:40 -0700207 kImtConflictMethod,
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700208 kImtUnimplementedMethod,
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100209 kSaveAllCalleeSavesMethod,
210 kSaveRefsOnlyMethod,
211 kSaveRefsAndArgsMethod,
Vladimir Marko952dbb12016-07-28 12:01:51 +0100212 kSaveEverythingMethod,
Mingyao Yang0a87a652017-04-12 13:43:15 -0700213 kSaveEverythingMethodForClinit,
214 kSaveEverythingMethodForSuspendCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700215 kImageMethodsCount, // Number of elements in enum.
216 };
217
218 enum ImageRoot {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700219 kDexCaches,
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700220 kClassRoots,
Vladimir Markoc13fbd82018-06-04 16:16:28 +0100221 kOomeWhenThrowingException, // Pre-allocated OOME when throwing exception.
222 kOomeWhenThrowingOome, // Pre-allocated OOME when throwing OOME.
223 kOomeWhenHandlingStackOverflow, // Pre-allocated OOME when handling StackOverflowError.
224 kNoClassDefFoundError, // Pre-allocated NoClassDefFoundError.
Vladimir Markof75613c2018-06-05 12:51:04 +0100225 kSpecialRoots, // Different for boot image and app image, see aliases below.
Brian Carlstrom16192862011-09-12 17:50:06 -0700226 kImageRootsMax,
Vladimir Markof75613c2018-06-05 12:51:04 +0100227
228 // Aliases.
229 kAppImageClassLoader = kSpecialRoots, // The class loader used to build the app image.
230 kBootImageLiveObjects = kSpecialRoots, // Array of boot image objects that must be kept live.
Brian Carlstrom16192862011-09-12 17:50:06 -0700231 };
232
Chris Wailes0c61be42018-09-26 17:27:34 -0700233 /*
234 * This describes the number and ordering of sections inside of Boot
235 * and App Images. It is very important that changes to this struct
236 * are reflected in the compiler and loader.
237 *
238 * See:
239 * - ImageWriter::ImageInfo::CreateImageSections()
240 * - ImageWriter::Write()
241 * - ImageWriter::AllocMemory()
242 */
Mathieu Chartiere401d142015-04-22 13:56:20 -0700243 enum ImageSections {
244 kSectionObjects,
245 kSectionArtFields,
246 kSectionArtMethods,
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700247 kSectionRuntimeMethods,
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000248 kSectionImTables,
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700249 kSectionIMTConflictTables,
Vladimir Marko05792b92015-08-03 11:56:49 +0100250 kSectionDexCacheArrays,
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700251 kSectionInternedStrings,
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800252 kSectionClassTable,
Chris Wailes0c61be42018-09-26 17:27:34 -0700253 kSectionStringReferenceOffsets,
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700254 kSectionMetadata,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700255 kSectionImageBitmap,
256 kSectionCount, // Number of elements in enum.
257 };
258
Vladimir Markof75613c2018-06-05 12:51:04 +0100259 static size_t NumberOfImageRoots(bool app_image ATTRIBUTE_UNUSED) {
260 // At the moment, boot image and app image have the same number of roots,
261 // though the meaning of the kSpecialRoots is different.
262 return kImageRootsMax;
Vladimir Markoeca3eda2016-11-09 16:26:44 +0000263 }
264
Mathieu Chartiere401d142015-04-22 13:56:20 -0700265 ArtMethod* GetImageMethod(ImageMethod index) const;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700266
Mathieu Chartier1a842962018-11-13 15:09:51 -0800267 ImageSection& GetImageSection(ImageSections index) {
268 DCHECK_LT(static_cast<size_t>(index), kSectionCount);
269 return sections_[index];
270 }
271
Vladimir Markocd87c3e2017-09-05 13:11:57 +0100272 const ImageSection& GetImageSection(ImageSections index) const {
273 DCHECK_LT(static_cast<size_t>(index), kSectionCount);
274 return sections_[index];
275 }
276
277 const ImageSection& GetObjectsSection() const {
278 return GetImageSection(kSectionObjects);
279 }
280
281 const ImageSection& GetFieldsSection() const {
282 return GetImageSection(ImageHeader::kSectionArtFields);
283 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700284
Mathieu Chartiere401d142015-04-22 13:56:20 -0700285 const ImageSection& GetMethodsSection() const {
286 return GetImageSection(kSectionArtMethods);
287 }
288
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700289 const ImageSection& GetRuntimeMethodsSection() const {
290 return GetImageSection(kSectionRuntimeMethods);
291 }
292
Vladimir Markocd87c3e2017-09-05 13:11:57 +0100293 const ImageSection& GetImTablesSection() const {
294 return GetImageSection(kSectionImTables);
295 }
296
297 const ImageSection& GetIMTConflictTablesSection() const {
298 return GetImageSection(kSectionIMTConflictTables);
299 }
300
301 const ImageSection& GetDexCacheArraysSection() const {
302 return GetImageSection(kSectionDexCacheArrays);
303 }
304
305 const ImageSection& GetInternedStringsSection() const {
306 return GetImageSection(kSectionInternedStrings);
307 }
308
309 const ImageSection& GetClassTableSection() const {
310 return GetImageSection(kSectionClassTable);
311 }
312
Chris Wailes0c61be42018-09-26 17:27:34 -0700313 const ImageSection& GetImageStringReferenceOffsetsSection() const {
314 return GetImageSection(kSectionStringReferenceOffsets);
315 }
316
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700317 const ImageSection& GetMetadataSection() const {
318 return GetImageSection(kSectionMetadata);
319 }
320
Vladimir Marko6d3c1812018-10-24 14:00:00 +0100321 const ImageSection& GetImageBitmapSection() const {
322 return GetImageSection(kSectionImageBitmap);
323 }
324
Mathieu Chartier4a26f172016-01-26 14:26:18 -0800325 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Vladimir Markoc13fbd82018-06-04 16:16:28 +0100326 ObjPtr<mirror::Object> GetImageRoot(ImageRoot image_root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700327 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4a26f172016-01-26 14:26:18 -0800328
329 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Vladimir Markoc13fbd82018-06-04 16:16:28 +0100330 ObjPtr<mirror::ObjectArray<mirror::Object>> GetImageRoots() const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700331 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700332
Vladimir Marko4df2d802018-09-27 16:42:44 +0000333 void RelocateImage(int64_t delta);
334 void RelocateImageMethods(int64_t delta);
335 void RelocateImageObjects(int64_t delta);
Alex Light53cb16b2014-06-12 11:26:29 -0700336
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800337 uint32_t GetBootImageBegin() const {
338 return boot_image_begin_;
339 }
340
341 uint32_t GetBootImageSize() const {
342 return boot_image_size_;
343 }
344
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800345 uint64_t GetDataSize() const {
346 return data_size_;
347 }
348
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800349 bool IsAppImage() const {
350 // App images currently require a boot image, if the size is non zero then it is an app image
351 // header.
352 return boot_image_size_ != 0u;
353 }
354
David Sehra49e0532017-08-25 08:05:29 -0700355 // Visit mirror::Objects in the section starting at base.
356 // TODO: Delete base parameter if it is always equal to GetImageBegin.
357 void VisitObjects(ObjectVisitor* visitor,
358 uint8_t* base,
359 PointerSize pointer_size) const
360 REQUIRES_SHARED(Locks::mutator_lock_);
361
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700362 // Visit ArtMethods in the section starting at base. Includes runtime methods.
363 // TODO: Delete base parameter if it is always equal to GetImageBegin.
Andreas Gampe542451c2016-07-26 09:02:02 -0700364 void VisitPackedArtMethods(ArtMethodVisitor* visitor,
365 uint8_t* base,
366 PointerSize pointer_size) const;
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700367
368 // Visit ArtMethods in the section starting at base.
369 // TODO: Delete base parameter if it is always equal to GetImageBegin.
370 void VisitPackedArtFields(ArtFieldVisitor* visitor, uint8_t* base) const;
371
372 template <typename Visitor>
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000373 void VisitPackedImTables(const Visitor& visitor,
374 uint8_t* base,
Andreas Gampe542451c2016-07-26 09:02:02 -0700375 PointerSize pointer_size) const;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000376
377 template <typename Visitor>
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700378 void VisitPackedImtConflictTables(const Visitor& visitor,
379 uint8_t* base,
Andreas Gampe542451c2016-07-26 09:02:02 -0700380 PointerSize pointer_size) const;
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700381
Mathieu Chartier1a842962018-11-13 15:09:51 -0800382 IterationRange<const Block*> GetBlocks() const {
383 return GetBlocks(GetImageBegin());
384 }
385
386 IterationRange<const Block*> GetBlocks(const uint8_t* image_begin) const {
387 const Block* begin = reinterpret_cast<const Block*>(image_begin + blocks_offset_);
388 return {begin, begin + blocks_count_};
389 }
390
391 // Return true if the image has any compressed blocks.
392 bool HasCompressedBlock() const {
393 return blocks_count_ != 0u;
394 }
395
396 uint32_t GetBlockCount() const {
397 return blocks_count_;
398 }
399
Alex Light53cb16b2014-06-12 11:26:29 -0700400 private:
Ian Rogers13735952014-10-08 12:43:28 -0700401 static const uint8_t kImageMagic[4];
402 static const uint8_t kImageVersion[4];
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700403
David Brazdil7b49e6c2016-09-01 11:06:18 +0100404 static std::string GetLocationFromImageLocation(const std::string& image,
405 const std::string& extension) {
406 std::string filename = image;
407 if (filename.length() <= 3) {
408 filename += "." + extension;
409 } else {
410 filename.replace(filename.length() - 3, 3, extension);
411 }
412 return filename;
413 }
414
Ian Rogers13735952014-10-08 12:43:28 -0700415 uint8_t magic_[4];
416 uint8_t version_[4];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700417
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800418 // Required base address for mapping the image.
Vladimir Marko312f10e2018-11-21 12:35:24 +0000419 uint32_t image_begin_ = 0u;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700420
Mathieu Chartier31e89252013-08-28 11:29:12 -0700421 // Image size, not page aligned.
Vladimir Marko312f10e2018-11-21 12:35:24 +0000422 uint32_t image_size_ = 0u;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700423
Vladimir Markoc10a0c62018-11-16 11:39:22 +0000424 // Image file checksum (calculated with the checksum field set to 0).
Vladimir Marko312f10e2018-11-21 12:35:24 +0000425 uint32_t image_checksum_ = 0u;
Vladimir Markoc10a0c62018-11-16 11:39:22 +0000426
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800427 // Checksum of the oat file we link to for load time sanity check.
Vladimir Marko312f10e2018-11-21 12:35:24 +0000428 uint32_t oat_checksum_ = 0u;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700429
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800430 // Start address for oat file. Will be before oat_data_begin_ for .so files.
Vladimir Marko312f10e2018-11-21 12:35:24 +0000431 uint32_t oat_file_begin_ = 0u;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700432
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800433 // Required oat address expected by image Method::GetCode() pointers.
Vladimir Marko312f10e2018-11-21 12:35:24 +0000434 uint32_t oat_data_begin_ = 0u;
Brian Carlstrom16192862011-09-12 17:50:06 -0700435
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800436 // End of oat data address range for this image file.
Vladimir Marko312f10e2018-11-21 12:35:24 +0000437 uint32_t oat_data_end_ = 0u;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800438
439 // End of oat file address range. will be after oat_data_end_ for
440 // .so files. Used for positioning a following alloc spaces.
Vladimir Marko312f10e2018-11-21 12:35:24 +0000441 uint32_t oat_file_end_ = 0u;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800442
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800443 // Boot image begin and end (app image headers only).
Vladimir Marko312f10e2018-11-21 12:35:24 +0000444 uint32_t boot_image_begin_ = 0u;
445 uint32_t boot_image_size_ = 0u; // Includes heap (*.art) and code (.oat).
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800446
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800447 // Absolute address of an Object[] of objects needed to reinitialize from an image.
Vladimir Marko312f10e2018-11-21 12:35:24 +0000448 uint32_t image_roots_ = 0u;
Brian Carlstrom16192862011-09-12 17:50:06 -0700449
Mathieu Chartiere401d142015-04-22 13:56:20 -0700450 // Pointer size, this affects the size of the ArtMethods.
Vladimir Marko312f10e2018-11-21 12:35:24 +0000451 uint32_t pointer_size_ = 0u;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700452
Mathieu Chartier67ad20e2015-12-09 15:41:09 -0800453 // Image section sizes/offsets correspond to the uncompressed form.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700454 ImageSection sections_[kSectionCount];
455
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800456 // Image methods, may be inside of the boot image for app images.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700457 uint64_t image_methods_[kImageMethodsCount];
458
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800459 // Data size for the image data excluding the bitmap and the header. For compressed images, this
460 // is the compressed size in the file.
Vladimir Marko312f10e2018-11-21 12:35:24 +0000461 uint32_t data_size_ = 0u;
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800462
Mathieu Chartier1a842962018-11-13 15:09:51 -0800463 // Image blocks, only used for compressed images.
464 uint32_t blocks_offset_ = 0u;
465 uint32_t blocks_count_ = 0u;
466
Vladimir Marko74527972016-11-29 15:57:32 +0000467 friend class linker::ImageWriter;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700468};
469
Chris Wailes0c61be42018-09-26 17:27:34 -0700470/*
Chris Wailesfbeef462018-10-19 14:16:35 -0700471 * This type holds the information necessary to fix up AppImage string
472 * references.
473 *
474 * The first element of the pair is an offset into the image space. If the
475 * offset is tagged (testable using HasDexCacheNativeRefTag) it indicates the location
476 * of a DexCache object that has one or more native references to managed
477 * strings that need to be fixed up. In this case the second element has no
478 * meaningful value.
479 *
480 * If the first element isn't tagged then it indicates the location of a
481 * managed object with a field that needs fixing up. In this case the second
482 * element of the pair is an object-relative offset to the field in question.
483 */
484typedef std::pair<uint32_t, uint32_t> AppImageReferenceOffsetInfo;
485
486/*
487 * Tags the last bit. Used by AppImage logic to differentiate between pointers
488 * to managed objects and pointers to native reference arrays.
Chris Wailes0c61be42018-09-26 17:27:34 -0700489 */
490template<typename T>
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700491T SetDexCacheStringNativeRefTag(T val) {
Chris Wailes0c61be42018-09-26 17:27:34 -0700492 static_assert(std::is_integral<T>::value, "Expected integral type.");
493
494 return val | 1u;
495}
496
497/*
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700498 * Tags the second last bit. Used by AppImage logic to differentiate between pointers
499 * to managed objects and pointers to native reference arrays.
500 */
501template<typename T>
502T SetDexCachePreResolvedStringNativeRefTag(T val) {
503 static_assert(std::is_integral<T>::value, "Expected integral type.");
504
505 return val | 2u;
506}
507
508/*
Chris Wailes0c61be42018-09-26 17:27:34 -0700509 * Retrieves the value of the last bit. Used by AppImage logic to
Chris Wailesfbeef462018-10-19 14:16:35 -0700510 * differentiate between pointers to managed objects and pointers to native
511 * reference arrays.
Chris Wailes0c61be42018-09-26 17:27:34 -0700512 */
513template<typename T>
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700514bool HasDexCacheStringNativeRefTag(T val) {
Chris Wailes0c61be42018-09-26 17:27:34 -0700515 static_assert(std::is_integral<T>::value, "Expected integral type.");
516
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700517 return (val & 1u) != 0u;
518}
519
520/*
521 * Retrieves the value of the second last bit. Used by AppImage logic to
522 * differentiate between pointers to managed objects and pointers to native
523 * reference arrays.
524 */
525template<typename T>
526bool HasDexCachePreResolvedStringNativeRefTag(T val) {
527 static_assert(std::is_integral<T>::value, "Expected integral type.");
528
529 return (val & 2u) != 0u;
Chris Wailes0c61be42018-09-26 17:27:34 -0700530}
531
532/*
533 * Sets the last bit of the value to 0. Used by AppImage logic to
Chris Wailesfbeef462018-10-19 14:16:35 -0700534 * differentiate between pointers to managed objects and pointers to native
535 * reference arrays.
Chris Wailes0c61be42018-09-26 17:27:34 -0700536 */
537template<typename T>
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700538T ClearDexCacheNativeRefTags(T val) {
Chris Wailes0c61be42018-09-26 17:27:34 -0700539 static_assert(std::is_integral<T>::value, "Expected integral type.");
540
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700541 return val & ~3u;
Chris Wailes0c61be42018-09-26 17:27:34 -0700542}
543
Mathieu Chartiere401d142015-04-22 13:56:20 -0700544std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageMethod& policy);
545std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageRoot& policy);
546std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageSections& section);
547std::ostream& operator<<(std::ostream& os, const ImageSection& section);
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800548std::ostream& operator<<(std::ostream& os, const ImageHeader::StorageMode& mode);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700549
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700550} // namespace art
551
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700552#endif // ART_RUNTIME_IMAGE_H_