blob: 8acd5bc4c40b7bf44ff5a2fb37a9e378b290b246 [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "mirror/object.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070025
26namespace art {
27
Mathieu Chartier54d220e2015-07-30 16:20:06 -070028class ArtField;
29class ArtMethod;
30
Vladimir Marko74527972016-11-29 15:57:32 +000031namespace linker {
32class ImageWriter;
33} // namespace linker
34
David Sehra49e0532017-08-25 08:05:29 -070035class ObjectVisitor {
36 public:
37 virtual ~ObjectVisitor() {}
38
39 virtual void Visit(mirror::Object* object) = 0;
40};
41
Mathieu Chartier54d220e2015-07-30 16:20:06 -070042class ArtMethodVisitor {
43 public:
44 virtual ~ArtMethodVisitor() {}
45
46 virtual void Visit(ArtMethod* method) = 0;
47};
48
49class ArtFieldVisitor {
50 public:
51 virtual ~ArtFieldVisitor() {}
52
53 virtual void Visit(ArtField* method) = 0;
54};
55
Mathieu Chartiere401d142015-04-22 13:56:20 -070056class PACKED(4) ImageSection {
57 public:
58 ImageSection() : offset_(0), size_(0) { }
59 ImageSection(uint32_t offset, uint32_t size) : offset_(offset), size_(size) { }
60 ImageSection(const ImageSection& section) = default;
61 ImageSection& operator=(const ImageSection& section) = default;
62
63 uint32_t Offset() const {
64 return offset_;
65 }
66
67 uint32_t Size() const {
68 return size_;
69 }
70
71 uint32_t End() const {
72 return Offset() + Size();
73 }
74
75 bool Contains(uint64_t offset) const {
76 return offset - offset_ < size_;
77 }
78
79 private:
80 uint32_t offset_;
81 uint32_t size_;
82};
83
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070084// header of image files written by ImageWriter, read and validated by Space.
Ian Rogersdf1ce912012-11-27 17:07:11 -080085class PACKED(4) ImageHeader {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070086 public:
Mathieu Chartierceb07b32015-12-10 09:33:21 -080087 enum StorageMode : uint32_t {
88 kStorageModeUncompressed,
89 kStorageModeLZ4,
Mathieu Chartiera6e81ed2016-02-25 13:52:10 -080090 kStorageModeLZ4HC,
Mathieu Chartierceb07b32015-12-10 09:33:21 -080091 kStorageModeCount, // Number of elements in enum.
92 };
93 static constexpr StorageMode kDefaultStorageMode = kStorageModeUncompressed;
94
Sebastien Hertzaa50d3a2015-08-25 15:25:41 +020095 ImageHeader()
Mathieu Chartierceb07b32015-12-10 09:33:21 -080096 : image_begin_(0U),
97 image_size_(0U),
98 oat_checksum_(0U),
99 oat_file_begin_(0U),
100 oat_data_begin_(0U),
101 oat_data_end_(0U),
102 oat_file_end_(0U),
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800103 boot_image_begin_(0U),
104 boot_image_size_(0U),
105 boot_oat_begin_(0U),
106 boot_oat_size_(0U),
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800107 patch_delta_(0),
108 image_roots_(0U),
109 pointer_size_(0U),
110 compile_pic_(0),
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800111 is_pic_(0),
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800112 storage_mode_(kDefaultStorageMode),
113 data_size_(0) {}
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700114
Ian Rogers30fab402012-01-23 15:43:46 -0800115 ImageHeader(uint32_t image_begin,
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800116 uint32_t image_size,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700117 ImageSection* sections,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700118 uint32_t image_roots,
119 uint32_t oat_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800120 uint32_t oat_file_begin,
121 uint32_t oat_data_begin,
122 uint32_t oat_data_end,
Igor Murashkin46774762014-10-22 11:37:02 -0700123 uint32_t oat_file_end,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800124 uint32_t boot_image_begin,
125 uint32_t boot_image_size,
126 uint32_t boot_oat_begin,
127 uint32_t boot_oat_size,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700128 uint32_t pointer_size,
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800129 bool compile_pic,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800130 bool is_pic,
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800131 StorageMode storage_mode,
132 size_t data_size);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700133
Brian Carlstrom68708f52013-09-03 14:15:31 -0700134 bool IsValid() const;
135 const char* GetMagic() const;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700136
Ian Rogers13735952014-10-08 12:43:28 -0700137 uint8_t* GetImageBegin() const {
138 return reinterpret_cast<uint8_t*>(image_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700139 }
140
Mathieu Chartier31e89252013-08-28 11:29:12 -0700141 size_t GetImageSize() const {
142 return static_cast<uint32_t>(image_size_);
143 }
144
Brian Carlstrome24fa612011-09-29 00:53:55 -0700145 uint32_t GetOatChecksum() const {
146 return oat_checksum_;
147 }
148
Brian Carlstroma85b8372012-10-18 17:00:32 -0700149 void SetOatChecksum(uint32_t oat_checksum) {
150 oat_checksum_ = oat_checksum;
151 }
152
Mathieu Chartier5351da02016-02-17 16:19:53 -0800153 // The location that the oat file was expected to be when the image was created. The actual
154 // oat file may be at a different location for application images.
Ian Rogers13735952014-10-08 12:43:28 -0700155 uint8_t* GetOatFileBegin() const {
156 return reinterpret_cast<uint8_t*>(oat_file_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700157 }
158
Ian Rogers13735952014-10-08 12:43:28 -0700159 uint8_t* GetOatDataBegin() const {
160 return reinterpret_cast<uint8_t*>(oat_data_begin_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800161 }
162
Ian Rogers13735952014-10-08 12:43:28 -0700163 uint8_t* GetOatDataEnd() const {
164 return reinterpret_cast<uint8_t*>(oat_data_end_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800165 }
166
Ian Rogers13735952014-10-08 12:43:28 -0700167 uint8_t* GetOatFileEnd() const {
168 return reinterpret_cast<uint8_t*>(oat_file_end_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700169 }
170
Andreas Gampebda1d602016-08-29 17:43:45 -0700171 PointerSize GetPointerSize() const;
Andreas Gampe542451c2016-07-26 09:02:02 -0700172
173 uint32_t GetPointerSizeUnchecked() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700174 return pointer_size_;
175 }
176
Alex Light53cb16b2014-06-12 11:26:29 -0700177 off_t GetPatchDelta() const {
178 return patch_delta_;
179 }
180
Alex Klyubind1d5c952017-12-15 12:57:33 -0800181 void SetPatchDelta(off_t patch_delta) {
182 patch_delta_ = patch_delta;
183 }
184
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000185 static std::string GetOatLocationFromImageLocation(const std::string& image) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100186 return GetLocationFromImageLocation(image, "oat");
187 }
188
189 static std::string GetVdexLocationFromImageLocation(const std::string& image) {
190 return GetLocationFromImageLocation(image, "vdex");
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000191 }
192
Mathieu Chartiere401d142015-04-22 13:56:20 -0700193 enum ImageMethod {
Ian Rogers19846512012-02-24 11:42:47 -0800194 kResolutionMethod,
Jeff Hao88474b42013-10-23 16:24:40 -0700195 kImtConflictMethod,
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700196 kImtUnimplementedMethod,
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100197 kSaveAllCalleeSavesMethod,
198 kSaveRefsOnlyMethod,
199 kSaveRefsAndArgsMethod,
Vladimir Marko952dbb12016-07-28 12:01:51 +0100200 kSaveEverythingMethod,
Mingyao Yang0a87a652017-04-12 13:43:15 -0700201 kSaveEverythingMethodForClinit,
202 kSaveEverythingMethodForSuspendCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700203 kImageMethodsCount, // Number of elements in enum.
204 };
205
206 enum ImageRoot {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700207 kDexCaches,
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700208 kClassRoots,
Vladimir Markoeca3eda2016-11-09 16:26:44 +0000209 kClassLoader, // App image only.
Brian Carlstrom16192862011-09-12 17:50:06 -0700210 kImageRootsMax,
211 };
212
Mathieu Chartiere401d142015-04-22 13:56:20 -0700213 enum ImageSections {
214 kSectionObjects,
215 kSectionArtFields,
216 kSectionArtMethods,
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700217 kSectionRuntimeMethods,
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000218 kSectionImTables,
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700219 kSectionIMTConflictTables,
Vladimir Marko05792b92015-08-03 11:56:49 +0100220 kSectionDexCacheArrays,
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700221 kSectionInternedStrings,
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800222 kSectionClassTable,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700223 kSectionImageBitmap,
224 kSectionCount, // Number of elements in enum.
225 };
226
Vladimir Markoeca3eda2016-11-09 16:26:44 +0000227 static size_t NumberOfImageRoots(bool app_image) {
228 return app_image ? kImageRootsMax : kImageRootsMax - 1u;
229 }
230
Mathieu Chartiere401d142015-04-22 13:56:20 -0700231 ArtMethod* GetImageMethod(ImageMethod index) const;
232 void SetImageMethod(ImageMethod index, ArtMethod* method);
233
Vladimir Markocd87c3e2017-09-05 13:11:57 +0100234 const ImageSection& GetImageSection(ImageSections index) const {
235 DCHECK_LT(static_cast<size_t>(index), kSectionCount);
236 return sections_[index];
237 }
238
239 const ImageSection& GetObjectsSection() const {
240 return GetImageSection(kSectionObjects);
241 }
242
243 const ImageSection& GetFieldsSection() const {
244 return GetImageSection(ImageHeader::kSectionArtFields);
245 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700246
Mathieu Chartiere401d142015-04-22 13:56:20 -0700247 const ImageSection& GetMethodsSection() const {
248 return GetImageSection(kSectionArtMethods);
249 }
250
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700251 const ImageSection& GetRuntimeMethodsSection() const {
252 return GetImageSection(kSectionRuntimeMethods);
253 }
254
Vladimir Markocd87c3e2017-09-05 13:11:57 +0100255 const ImageSection& GetImTablesSection() const {
256 return GetImageSection(kSectionImTables);
257 }
258
259 const ImageSection& GetIMTConflictTablesSection() const {
260 return GetImageSection(kSectionIMTConflictTables);
261 }
262
263 const ImageSection& GetDexCacheArraysSection() const {
264 return GetImageSection(kSectionDexCacheArrays);
265 }
266
267 const ImageSection& GetInternedStringsSection() const {
268 return GetImageSection(kSectionInternedStrings);
269 }
270
271 const ImageSection& GetClassTableSection() const {
272 return GetImageSection(kSectionClassTable);
273 }
274
275 const ImageSection& GetImageBitmapSection() const {
276 return GetImageSection(kSectionImageBitmap);
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700277 }
278
Mathieu Chartier4a26f172016-01-26 14:26:18 -0800279 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800280 mirror::Object* GetImageRoot(ImageRoot image_root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700281 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4a26f172016-01-26 14:26:18 -0800282
283 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800284 mirror::ObjectArray<mirror::Object>* GetImageRoots() const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700285 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700286
Alex Light53cb16b2014-06-12 11:26:29 -0700287 void RelocateImage(off_t delta);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800288 void RelocateImageMethods(off_t delta);
289 void RelocateImageObjects(off_t delta);
Alex Light53cb16b2014-06-12 11:26:29 -0700290
Igor Murashkin46774762014-10-22 11:37:02 -0700291 bool CompilePic() const {
292 return compile_pic_ != 0;
293 }
294
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800295 bool IsPic() const {
296 return is_pic_ != 0;
297 }
298
299 uint32_t GetBootImageBegin() const {
300 return boot_image_begin_;
301 }
302
303 uint32_t GetBootImageSize() const {
304 return boot_image_size_;
305 }
306
307 uint32_t GetBootOatBegin() const {
308 return boot_oat_begin_;
309 }
310
311 uint32_t GetBootOatSize() const {
312 return boot_oat_size_;
313 }
314
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800315 StorageMode GetStorageMode() const {
316 return storage_mode_;
317 }
318
319 uint64_t GetDataSize() const {
320 return data_size_;
321 }
322
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800323 bool IsAppImage() const {
324 // App images currently require a boot image, if the size is non zero then it is an app image
325 // header.
326 return boot_image_size_ != 0u;
327 }
328
David Sehra49e0532017-08-25 08:05:29 -0700329 // Visit mirror::Objects in the section starting at base.
330 // TODO: Delete base parameter if it is always equal to GetImageBegin.
331 void VisitObjects(ObjectVisitor* visitor,
332 uint8_t* base,
333 PointerSize pointer_size) const
334 REQUIRES_SHARED(Locks::mutator_lock_);
335
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700336 // Visit ArtMethods in the section starting at base. Includes runtime methods.
337 // TODO: Delete base parameter if it is always equal to GetImageBegin.
Andreas Gampe542451c2016-07-26 09:02:02 -0700338 void VisitPackedArtMethods(ArtMethodVisitor* visitor,
339 uint8_t* base,
340 PointerSize pointer_size) const;
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700341
342 // Visit ArtMethods in the section starting at base.
343 // TODO: Delete base parameter if it is always equal to GetImageBegin.
344 void VisitPackedArtFields(ArtFieldVisitor* visitor, uint8_t* base) const;
345
346 template <typename Visitor>
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000347 void VisitPackedImTables(const Visitor& visitor,
348 uint8_t* base,
Andreas Gampe542451c2016-07-26 09:02:02 -0700349 PointerSize pointer_size) const;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000350
351 template <typename Visitor>
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700352 void VisitPackedImtConflictTables(const Visitor& visitor,
353 uint8_t* base,
Andreas Gampe542451c2016-07-26 09:02:02 -0700354 PointerSize pointer_size) const;
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700355
Alex Light53cb16b2014-06-12 11:26:29 -0700356 private:
Ian Rogers13735952014-10-08 12:43:28 -0700357 static const uint8_t kImageMagic[4];
358 static const uint8_t kImageVersion[4];
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700359
David Brazdil7b49e6c2016-09-01 11:06:18 +0100360 static std::string GetLocationFromImageLocation(const std::string& image,
361 const std::string& extension) {
362 std::string filename = image;
363 if (filename.length() <= 3) {
364 filename += "." + extension;
365 } else {
366 filename.replace(filename.length() - 3, 3, extension);
367 }
368 return filename;
369 }
370
Ian Rogers13735952014-10-08 12:43:28 -0700371 uint8_t magic_[4];
372 uint8_t version_[4];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700373
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800374 // Required base address for mapping the image.
Ian Rogers30fab402012-01-23 15:43:46 -0800375 uint32_t image_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700376
Mathieu Chartier31e89252013-08-28 11:29:12 -0700377 // Image size, not page aligned.
378 uint32_t image_size_;
379
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800380 // Checksum of the oat file we link to for load time sanity check.
Brian Carlstrome24fa612011-09-29 00:53:55 -0700381 uint32_t oat_checksum_;
382
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800383 // Start address for oat file. Will be before oat_data_begin_ for .so files.
384 uint32_t oat_file_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700385
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800386 // Required oat address expected by image Method::GetCode() pointers.
387 uint32_t oat_data_begin_;
Brian Carlstrom16192862011-09-12 17:50:06 -0700388
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800389 // End of oat data address range for this image file.
390 uint32_t oat_data_end_;
391
392 // End of oat file address range. will be after oat_data_end_ for
393 // .so files. Used for positioning a following alloc spaces.
394 uint32_t oat_file_end_;
395
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800396 // Boot image begin and end (app image headers only).
397 uint32_t boot_image_begin_;
398 uint32_t boot_image_size_;
399
400 // Boot oat begin and end (app image headers only).
401 uint32_t boot_oat_begin_;
402 uint32_t boot_oat_size_;
403
404 // TODO: We should probably insert a boot image checksum for app images.
405
Alex Light53cb16b2014-06-12 11:26:29 -0700406 // The total delta that this image has been patched.
407 int32_t patch_delta_;
408
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800409 // Absolute address of an Object[] of objects needed to reinitialize from an image.
Brian Carlstrom16192862011-09-12 17:50:06 -0700410 uint32_t image_roots_;
411
Mathieu Chartiere401d142015-04-22 13:56:20 -0700412 // Pointer size, this affects the size of the ArtMethods.
413 uint32_t pointer_size_;
414
Igor Murashkin46774762014-10-22 11:37:02 -0700415 // Boolean (0 or 1) to denote if the image was compiled with --compile-pic option
416 const uint32_t compile_pic_;
417
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800418 // Boolean (0 or 1) to denote if the image can be mapped at a random address, this only refers to
419 // the .art file. Currently, app oat files do not depend on their app image. There are no pointers
420 // from the app oat code to the app image.
421 const uint32_t is_pic_;
422
Mathieu Chartier67ad20e2015-12-09 15:41:09 -0800423 // Image section sizes/offsets correspond to the uncompressed form.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700424 ImageSection sections_[kSectionCount];
425
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800426 // Image methods, may be inside of the boot image for app images.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700427 uint64_t image_methods_[kImageMethodsCount];
428
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800429 // Storage method for the image, the image may be compressed.
430 StorageMode storage_mode_;
431
432 // Data size for the image data excluding the bitmap and the header. For compressed images, this
433 // is the compressed size in the file.
434 uint32_t data_size_;
435
Vladimir Marko74527972016-11-29 15:57:32 +0000436 friend class linker::ImageWriter;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700437};
438
Mathieu Chartiere401d142015-04-22 13:56:20 -0700439std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageMethod& policy);
440std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageRoot& policy);
441std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageSections& section);
442std::ostream& operator<<(std::ostream& os, const ImageSection& section);
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800443std::ostream& operator<<(std::ostream& os, const ImageHeader::StorageMode& mode);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700444
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700445} // namespace art
446
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700447#endif // ART_RUNTIME_IMAGE_H_