blob: 9d98431183ea5d818e5eb3eb9d354e9f4d5c9fa7 [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
Mathieu Chartierc6068c72018-11-13 16:00:58 -0800119 uint32_t GetDataSize() const {
120 return data_size_;
121 }
122
123 uint32_t GetImageSize() const {
124 return image_size_;
125 }
126
Mathieu Chartier1a842962018-11-13 15:09:51 -0800127 private:
128 // Storage method for the image, the image may be compressed.
129 StorageMode storage_mode_ = kDefaultStorageMode;
130
131 // Compressed offset and size.
132 uint32_t data_offset_ = 0u;
133 uint32_t data_size_ = 0u;
134
135 // Image offset and size (decompressed or mapped location).
136 uint32_t image_offset_ = 0u;
137 uint32_t image_size_ = 0u;
138 };
139
140 ImageHeader() {}
Ian Rogers30fab402012-01-23 15:43:46 -0800141 ImageHeader(uint32_t image_begin,
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800142 uint32_t image_size,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700143 ImageSection* sections,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700144 uint32_t image_roots,
145 uint32_t oat_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800146 uint32_t oat_file_begin,
147 uint32_t oat_data_begin,
148 uint32_t oat_data_end,
Igor Murashkin46774762014-10-22 11:37:02 -0700149 uint32_t oat_file_end,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800150 uint32_t boot_image_begin,
151 uint32_t boot_image_size,
Mathieu Chartier1a842962018-11-13 15:09:51 -0800152 uint32_t pointer_size);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700153
Brian Carlstrom68708f52013-09-03 14:15:31 -0700154 bool IsValid() const;
155 const char* GetMagic() const;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700156
Ian Rogers13735952014-10-08 12:43:28 -0700157 uint8_t* GetImageBegin() const {
158 return reinterpret_cast<uint8_t*>(image_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700159 }
160
Mathieu Chartier31e89252013-08-28 11:29:12 -0700161 size_t GetImageSize() const {
Vladimir Markoc10a0c62018-11-16 11:39:22 +0000162 return image_size_;
163 }
164
165 uint32_t GetImageChecksum() const {
166 return image_checksum_;
167 }
168
169 void SetImageChecksum(uint32_t image_checksum) {
170 image_checksum_ = image_checksum;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700171 }
172
Brian Carlstrome24fa612011-09-29 00:53:55 -0700173 uint32_t GetOatChecksum() const {
174 return oat_checksum_;
175 }
176
Brian Carlstroma85b8372012-10-18 17:00:32 -0700177 void SetOatChecksum(uint32_t oat_checksum) {
178 oat_checksum_ = oat_checksum;
179 }
180
Mathieu Chartier5351da02016-02-17 16:19:53 -0800181 // The location that the oat file was expected to be when the image was created. The actual
182 // oat file may be at a different location for application images.
Ian Rogers13735952014-10-08 12:43:28 -0700183 uint8_t* GetOatFileBegin() const {
184 return reinterpret_cast<uint8_t*>(oat_file_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700185 }
186
Ian Rogers13735952014-10-08 12:43:28 -0700187 uint8_t* GetOatDataBegin() const {
188 return reinterpret_cast<uint8_t*>(oat_data_begin_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800189 }
190
Ian Rogers13735952014-10-08 12:43:28 -0700191 uint8_t* GetOatDataEnd() const {
192 return reinterpret_cast<uint8_t*>(oat_data_end_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800193 }
194
Ian Rogers13735952014-10-08 12:43:28 -0700195 uint8_t* GetOatFileEnd() const {
196 return reinterpret_cast<uint8_t*>(oat_file_end_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700197 }
198
Andreas Gampebda1d602016-08-29 17:43:45 -0700199 PointerSize GetPointerSize() const;
Andreas Gampe542451c2016-07-26 09:02:02 -0700200
201 uint32_t GetPointerSizeUnchecked() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700202 return pointer_size_;
203 }
204
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000205 static std::string GetOatLocationFromImageLocation(const std::string& image) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100206 return GetLocationFromImageLocation(image, "oat");
207 }
208
209 static std::string GetVdexLocationFromImageLocation(const std::string& image) {
210 return GetLocationFromImageLocation(image, "vdex");
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000211 }
212
Mathieu Chartiere401d142015-04-22 13:56:20 -0700213 enum ImageMethod {
Ian Rogers19846512012-02-24 11:42:47 -0800214 kResolutionMethod,
Jeff Hao88474b42013-10-23 16:24:40 -0700215 kImtConflictMethod,
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700216 kImtUnimplementedMethod,
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100217 kSaveAllCalleeSavesMethod,
218 kSaveRefsOnlyMethod,
219 kSaveRefsAndArgsMethod,
Vladimir Marko952dbb12016-07-28 12:01:51 +0100220 kSaveEverythingMethod,
Mingyao Yang0a87a652017-04-12 13:43:15 -0700221 kSaveEverythingMethodForClinit,
222 kSaveEverythingMethodForSuspendCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700223 kImageMethodsCount, // Number of elements in enum.
224 };
225
226 enum ImageRoot {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700227 kDexCaches,
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700228 kClassRoots,
Vladimir Markoc13fbd82018-06-04 16:16:28 +0100229 kOomeWhenThrowingException, // Pre-allocated OOME when throwing exception.
230 kOomeWhenThrowingOome, // Pre-allocated OOME when throwing OOME.
231 kOomeWhenHandlingStackOverflow, // Pre-allocated OOME when handling StackOverflowError.
232 kNoClassDefFoundError, // Pre-allocated NoClassDefFoundError.
Vladimir Markof75613c2018-06-05 12:51:04 +0100233 kSpecialRoots, // Different for boot image and app image, see aliases below.
Brian Carlstrom16192862011-09-12 17:50:06 -0700234 kImageRootsMax,
Vladimir Markof75613c2018-06-05 12:51:04 +0100235
236 // Aliases.
237 kAppImageClassLoader = kSpecialRoots, // The class loader used to build the app image.
238 kBootImageLiveObjects = kSpecialRoots, // Array of boot image objects that must be kept live.
Brian Carlstrom16192862011-09-12 17:50:06 -0700239 };
240
Chris Wailes0c61be42018-09-26 17:27:34 -0700241 /*
242 * This describes the number and ordering of sections inside of Boot
243 * and App Images. It is very important that changes to this struct
244 * are reflected in the compiler and loader.
245 *
246 * See:
247 * - ImageWriter::ImageInfo::CreateImageSections()
248 * - ImageWriter::Write()
249 * - ImageWriter::AllocMemory()
250 */
Mathieu Chartiere401d142015-04-22 13:56:20 -0700251 enum ImageSections {
252 kSectionObjects,
253 kSectionArtFields,
254 kSectionArtMethods,
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700255 kSectionRuntimeMethods,
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000256 kSectionImTables,
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700257 kSectionIMTConflictTables,
Vladimir Marko05792b92015-08-03 11:56:49 +0100258 kSectionDexCacheArrays,
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700259 kSectionInternedStrings,
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800260 kSectionClassTable,
Chris Wailes0c61be42018-09-26 17:27:34 -0700261 kSectionStringReferenceOffsets,
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700262 kSectionMetadata,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700263 kSectionImageBitmap,
264 kSectionCount, // Number of elements in enum.
265 };
266
Vladimir Markof75613c2018-06-05 12:51:04 +0100267 static size_t NumberOfImageRoots(bool app_image ATTRIBUTE_UNUSED) {
268 // At the moment, boot image and app image have the same number of roots,
269 // though the meaning of the kSpecialRoots is different.
270 return kImageRootsMax;
Vladimir Markoeca3eda2016-11-09 16:26:44 +0000271 }
272
Mathieu Chartiere401d142015-04-22 13:56:20 -0700273 ArtMethod* GetImageMethod(ImageMethod index) const;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700274
Mathieu Chartier1a842962018-11-13 15:09:51 -0800275 ImageSection& GetImageSection(ImageSections index) {
276 DCHECK_LT(static_cast<size_t>(index), kSectionCount);
277 return sections_[index];
278 }
279
Vladimir Markocd87c3e2017-09-05 13:11:57 +0100280 const ImageSection& GetImageSection(ImageSections index) const {
281 DCHECK_LT(static_cast<size_t>(index), kSectionCount);
282 return sections_[index];
283 }
284
285 const ImageSection& GetObjectsSection() const {
286 return GetImageSection(kSectionObjects);
287 }
288
289 const ImageSection& GetFieldsSection() const {
290 return GetImageSection(ImageHeader::kSectionArtFields);
291 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700292
Mathieu Chartiere401d142015-04-22 13:56:20 -0700293 const ImageSection& GetMethodsSection() const {
294 return GetImageSection(kSectionArtMethods);
295 }
296
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700297 const ImageSection& GetRuntimeMethodsSection() const {
298 return GetImageSection(kSectionRuntimeMethods);
299 }
300
Vladimir Markocd87c3e2017-09-05 13:11:57 +0100301 const ImageSection& GetImTablesSection() const {
302 return GetImageSection(kSectionImTables);
303 }
304
305 const ImageSection& GetIMTConflictTablesSection() const {
306 return GetImageSection(kSectionIMTConflictTables);
307 }
308
309 const ImageSection& GetDexCacheArraysSection() const {
310 return GetImageSection(kSectionDexCacheArrays);
311 }
312
313 const ImageSection& GetInternedStringsSection() const {
314 return GetImageSection(kSectionInternedStrings);
315 }
316
317 const ImageSection& GetClassTableSection() const {
318 return GetImageSection(kSectionClassTable);
319 }
320
Chris Wailes0c61be42018-09-26 17:27:34 -0700321 const ImageSection& GetImageStringReferenceOffsetsSection() const {
322 return GetImageSection(kSectionStringReferenceOffsets);
323 }
324
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700325 const ImageSection& GetMetadataSection() const {
326 return GetImageSection(kSectionMetadata);
327 }
328
Vladimir Marko6d3c1812018-10-24 14:00:00 +0100329 const ImageSection& GetImageBitmapSection() const {
330 return GetImageSection(kSectionImageBitmap);
331 }
332
Mathieu Chartier4a26f172016-01-26 14:26:18 -0800333 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Vladimir Markoc13fbd82018-06-04 16:16:28 +0100334 ObjPtr<mirror::Object> GetImageRoot(ImageRoot image_root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700335 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4a26f172016-01-26 14:26:18 -0800336
337 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Vladimir Markoc13fbd82018-06-04 16:16:28 +0100338 ObjPtr<mirror::ObjectArray<mirror::Object>> GetImageRoots() const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700339 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700340
Vladimir Marko4df2d802018-09-27 16:42:44 +0000341 void RelocateImage(int64_t delta);
342 void RelocateImageMethods(int64_t delta);
343 void RelocateImageObjects(int64_t delta);
Alex Light53cb16b2014-06-12 11:26:29 -0700344
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800345 uint32_t GetBootImageBegin() const {
346 return boot_image_begin_;
347 }
348
349 uint32_t GetBootImageSize() const {
350 return boot_image_size_;
351 }
352
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800353 uint64_t GetDataSize() const {
354 return data_size_;
355 }
356
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800357 bool IsAppImage() const {
358 // App images currently require a boot image, if the size is non zero then it is an app image
359 // header.
360 return boot_image_size_ != 0u;
361 }
362
David Sehra49e0532017-08-25 08:05:29 -0700363 // Visit mirror::Objects in the section starting at base.
364 // TODO: Delete base parameter if it is always equal to GetImageBegin.
365 void VisitObjects(ObjectVisitor* visitor,
366 uint8_t* base,
367 PointerSize pointer_size) const
368 REQUIRES_SHARED(Locks::mutator_lock_);
369
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700370 // Visit ArtMethods in the section starting at base. Includes runtime methods.
371 // TODO: Delete base parameter if it is always equal to GetImageBegin.
Andreas Gampe542451c2016-07-26 09:02:02 -0700372 void VisitPackedArtMethods(ArtMethodVisitor* visitor,
373 uint8_t* base,
374 PointerSize pointer_size) const;
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700375
376 // Visit ArtMethods in the section starting at base.
377 // TODO: Delete base parameter if it is always equal to GetImageBegin.
378 void VisitPackedArtFields(ArtFieldVisitor* visitor, uint8_t* base) const;
379
380 template <typename Visitor>
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000381 void VisitPackedImTables(const Visitor& visitor,
382 uint8_t* base,
Andreas Gampe542451c2016-07-26 09:02:02 -0700383 PointerSize pointer_size) const;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000384
385 template <typename Visitor>
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700386 void VisitPackedImtConflictTables(const Visitor& visitor,
387 uint8_t* base,
Andreas Gampe542451c2016-07-26 09:02:02 -0700388 PointerSize pointer_size) const;
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700389
Mathieu Chartier1a842962018-11-13 15:09:51 -0800390 IterationRange<const Block*> GetBlocks() const {
391 return GetBlocks(GetImageBegin());
392 }
393
394 IterationRange<const Block*> GetBlocks(const uint8_t* image_begin) const {
395 const Block* begin = reinterpret_cast<const Block*>(image_begin + blocks_offset_);
396 return {begin, begin + blocks_count_};
397 }
398
399 // Return true if the image has any compressed blocks.
400 bool HasCompressedBlock() const {
401 return blocks_count_ != 0u;
402 }
403
404 uint32_t GetBlockCount() const {
405 return blocks_count_;
406 }
407
Alex Light53cb16b2014-06-12 11:26:29 -0700408 private:
Ian Rogers13735952014-10-08 12:43:28 -0700409 static const uint8_t kImageMagic[4];
410 static const uint8_t kImageVersion[4];
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700411
David Brazdil7b49e6c2016-09-01 11:06:18 +0100412 static std::string GetLocationFromImageLocation(const std::string& image,
413 const std::string& extension) {
414 std::string filename = image;
415 if (filename.length() <= 3) {
416 filename += "." + extension;
417 } else {
418 filename.replace(filename.length() - 3, 3, extension);
419 }
420 return filename;
421 }
422
Ian Rogers13735952014-10-08 12:43:28 -0700423 uint8_t magic_[4];
424 uint8_t version_[4];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700425
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800426 // Required base address for mapping the image.
Vladimir Marko312f10e2018-11-21 12:35:24 +0000427 uint32_t image_begin_ = 0u;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700428
Mathieu Chartier31e89252013-08-28 11:29:12 -0700429 // Image size, not page aligned.
Vladimir Marko312f10e2018-11-21 12:35:24 +0000430 uint32_t image_size_ = 0u;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700431
Vladimir Markoc10a0c62018-11-16 11:39:22 +0000432 // Image file checksum (calculated with the checksum field set to 0).
Vladimir Marko312f10e2018-11-21 12:35:24 +0000433 uint32_t image_checksum_ = 0u;
Vladimir Markoc10a0c62018-11-16 11:39:22 +0000434
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800435 // Checksum of the oat file we link to for load time sanity check.
Vladimir Marko312f10e2018-11-21 12:35:24 +0000436 uint32_t oat_checksum_ = 0u;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700437
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800438 // Start address for oat file. Will be before oat_data_begin_ for .so files.
Vladimir Marko312f10e2018-11-21 12:35:24 +0000439 uint32_t oat_file_begin_ = 0u;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700440
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800441 // Required oat address expected by image Method::GetCode() pointers.
Vladimir Marko312f10e2018-11-21 12:35:24 +0000442 uint32_t oat_data_begin_ = 0u;
Brian Carlstrom16192862011-09-12 17:50:06 -0700443
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800444 // End of oat data address range for this image file.
Vladimir Marko312f10e2018-11-21 12:35:24 +0000445 uint32_t oat_data_end_ = 0u;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800446
447 // End of oat file address range. will be after oat_data_end_ for
448 // .so files. Used for positioning a following alloc spaces.
Vladimir Marko312f10e2018-11-21 12:35:24 +0000449 uint32_t oat_file_end_ = 0u;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800450
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800451 // Boot image begin and end (app image headers only).
Vladimir Marko312f10e2018-11-21 12:35:24 +0000452 uint32_t boot_image_begin_ = 0u;
453 uint32_t boot_image_size_ = 0u; // Includes heap (*.art) and code (.oat).
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800454
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800455 // Absolute address of an Object[] of objects needed to reinitialize from an image.
Vladimir Marko312f10e2018-11-21 12:35:24 +0000456 uint32_t image_roots_ = 0u;
Brian Carlstrom16192862011-09-12 17:50:06 -0700457
Mathieu Chartiere401d142015-04-22 13:56:20 -0700458 // Pointer size, this affects the size of the ArtMethods.
Vladimir Marko312f10e2018-11-21 12:35:24 +0000459 uint32_t pointer_size_ = 0u;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700460
Mathieu Chartier67ad20e2015-12-09 15:41:09 -0800461 // Image section sizes/offsets correspond to the uncompressed form.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700462 ImageSection sections_[kSectionCount];
463
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800464 // Image methods, may be inside of the boot image for app images.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700465 uint64_t image_methods_[kImageMethodsCount];
466
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800467 // Data size for the image data excluding the bitmap and the header. For compressed images, this
468 // is the compressed size in the file.
Vladimir Marko312f10e2018-11-21 12:35:24 +0000469 uint32_t data_size_ = 0u;
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800470
Mathieu Chartier1a842962018-11-13 15:09:51 -0800471 // Image blocks, only used for compressed images.
472 uint32_t blocks_offset_ = 0u;
473 uint32_t blocks_count_ = 0u;
474
Vladimir Marko74527972016-11-29 15:57:32 +0000475 friend class linker::ImageWriter;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700476};
477
Chris Wailes0c61be42018-09-26 17:27:34 -0700478/*
Chris Wailesfbeef462018-10-19 14:16:35 -0700479 * This type holds the information necessary to fix up AppImage string
480 * references.
481 *
482 * The first element of the pair is an offset into the image space. If the
483 * offset is tagged (testable using HasDexCacheNativeRefTag) it indicates the location
484 * of a DexCache object that has one or more native references to managed
485 * strings that need to be fixed up. In this case the second element has no
486 * meaningful value.
487 *
488 * If the first element isn't tagged then it indicates the location of a
489 * managed object with a field that needs fixing up. In this case the second
490 * element of the pair is an object-relative offset to the field in question.
491 */
492typedef std::pair<uint32_t, uint32_t> AppImageReferenceOffsetInfo;
493
494/*
495 * Tags the last bit. Used by AppImage logic to differentiate between pointers
496 * to managed objects and pointers to native reference arrays.
Chris Wailes0c61be42018-09-26 17:27:34 -0700497 */
498template<typename T>
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700499T SetDexCacheStringNativeRefTag(T val) {
Chris Wailes0c61be42018-09-26 17:27:34 -0700500 static_assert(std::is_integral<T>::value, "Expected integral type.");
501
502 return val | 1u;
503}
504
505/*
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700506 * Tags the second last bit. Used by AppImage logic to differentiate between pointers
507 * to managed objects and pointers to native reference arrays.
508 */
509template<typename T>
510T SetDexCachePreResolvedStringNativeRefTag(T val) {
511 static_assert(std::is_integral<T>::value, "Expected integral type.");
512
513 return val | 2u;
514}
515
516/*
Chris Wailes0c61be42018-09-26 17:27:34 -0700517 * Retrieves the value of the last bit. Used by AppImage logic to
Chris Wailesfbeef462018-10-19 14:16:35 -0700518 * differentiate between pointers to managed objects and pointers to native
519 * reference arrays.
Chris Wailes0c61be42018-09-26 17:27:34 -0700520 */
521template<typename T>
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700522bool HasDexCacheStringNativeRefTag(T val) {
Chris Wailes0c61be42018-09-26 17:27:34 -0700523 static_assert(std::is_integral<T>::value, "Expected integral type.");
524
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700525 return (val & 1u) != 0u;
526}
527
528/*
529 * Retrieves the value of the second last bit. Used by AppImage logic to
530 * differentiate between pointers to managed objects and pointers to native
531 * reference arrays.
532 */
533template<typename T>
534bool HasDexCachePreResolvedStringNativeRefTag(T val) {
535 static_assert(std::is_integral<T>::value, "Expected integral type.");
536
537 return (val & 2u) != 0u;
Chris Wailes0c61be42018-09-26 17:27:34 -0700538}
539
540/*
541 * Sets the last bit of the value to 0. Used by AppImage logic to
Chris Wailesfbeef462018-10-19 14:16:35 -0700542 * differentiate between pointers to managed objects and pointers to native
543 * reference arrays.
Chris Wailes0c61be42018-09-26 17:27:34 -0700544 */
545template<typename T>
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700546T ClearDexCacheNativeRefTags(T val) {
Chris Wailes0c61be42018-09-26 17:27:34 -0700547 static_assert(std::is_integral<T>::value, "Expected integral type.");
548
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700549 return val & ~3u;
Chris Wailes0c61be42018-09-26 17:27:34 -0700550}
551
Mathieu Chartiere401d142015-04-22 13:56:20 -0700552std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageMethod& policy);
553std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageRoot& policy);
554std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageSections& section);
555std::ostream& operator<<(std::ostream& os, const ImageSection& section);
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800556std::ostream& operator<<(std::ostream& os, const ImageHeader::StorageMode& mode);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700557
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700558} // namespace art
559
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700560#endif // ART_RUNTIME_IMAGE_H_