blob: 42abffc738d9db5474742c0c8cac2553380e75da [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"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070023#include "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
31class ArtMethodVisitor {
32 public:
33 virtual ~ArtMethodVisitor() {}
34
35 virtual void Visit(ArtMethod* method) = 0;
36};
37
38class ArtFieldVisitor {
39 public:
40 virtual ~ArtFieldVisitor() {}
41
42 virtual void Visit(ArtField* method) = 0;
43};
44
Mathieu Chartiere401d142015-04-22 13:56:20 -070045class PACKED(4) ImageSection {
46 public:
47 ImageSection() : offset_(0), size_(0) { }
48 ImageSection(uint32_t offset, uint32_t size) : offset_(offset), size_(size) { }
49 ImageSection(const ImageSection& section) = default;
50 ImageSection& operator=(const ImageSection& section) = default;
51
52 uint32_t Offset() const {
53 return offset_;
54 }
55
56 uint32_t Size() const {
57 return size_;
58 }
59
60 uint32_t End() const {
61 return Offset() + Size();
62 }
63
64 bool Contains(uint64_t offset) const {
65 return offset - offset_ < size_;
66 }
67
68 private:
69 uint32_t offset_;
70 uint32_t size_;
71};
72
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070073// header of image files written by ImageWriter, read and validated by Space.
Ian Rogersdf1ce912012-11-27 17:07:11 -080074class PACKED(4) ImageHeader {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070075 public:
Mathieu Chartierceb07b32015-12-10 09:33:21 -080076 enum StorageMode : uint32_t {
77 kStorageModeUncompressed,
78 kStorageModeLZ4,
Mathieu Chartiera6e81ed2016-02-25 13:52:10 -080079 kStorageModeLZ4HC,
Mathieu Chartierceb07b32015-12-10 09:33:21 -080080 kStorageModeCount, // Number of elements in enum.
81 };
82 static constexpr StorageMode kDefaultStorageMode = kStorageModeUncompressed;
83
Sebastien Hertzaa50d3a2015-08-25 15:25:41 +020084 ImageHeader()
Mathieu Chartierceb07b32015-12-10 09:33:21 -080085 : image_begin_(0U),
86 image_size_(0U),
87 oat_checksum_(0U),
88 oat_file_begin_(0U),
89 oat_data_begin_(0U),
90 oat_data_end_(0U),
91 oat_file_end_(0U),
Mathieu Chartierfbc31082016-01-24 11:59:56 -080092 boot_image_begin_(0U),
93 boot_image_size_(0U),
94 boot_oat_begin_(0U),
95 boot_oat_size_(0U),
Mathieu Chartierceb07b32015-12-10 09:33:21 -080096 patch_delta_(0),
97 image_roots_(0U),
98 pointer_size_(0U),
99 compile_pic_(0),
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800100 is_pic_(0),
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800101 storage_mode_(kDefaultStorageMode),
102 data_size_(0) {}
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700103
Ian Rogers30fab402012-01-23 15:43:46 -0800104 ImageHeader(uint32_t image_begin,
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800105 uint32_t image_size,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700106 ImageSection* sections,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700107 uint32_t image_roots,
108 uint32_t oat_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800109 uint32_t oat_file_begin,
110 uint32_t oat_data_begin,
111 uint32_t oat_data_end,
Igor Murashkin46774762014-10-22 11:37:02 -0700112 uint32_t oat_file_end,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800113 uint32_t boot_image_begin,
114 uint32_t boot_image_size,
115 uint32_t boot_oat_begin,
116 uint32_t boot_oat_size,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700117 uint32_t pointer_size,
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800118 bool compile_pic,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800119 bool is_pic,
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800120 StorageMode storage_mode,
121 size_t data_size);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700122
Brian Carlstrom68708f52013-09-03 14:15:31 -0700123 bool IsValid() const;
124 const char* GetMagic() const;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700125
Ian Rogers13735952014-10-08 12:43:28 -0700126 uint8_t* GetImageBegin() const {
127 return reinterpret_cast<uint8_t*>(image_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700128 }
129
Mathieu Chartier31e89252013-08-28 11:29:12 -0700130 size_t GetImageSize() const {
131 return static_cast<uint32_t>(image_size_);
132 }
133
Brian Carlstrome24fa612011-09-29 00:53:55 -0700134 uint32_t GetOatChecksum() const {
135 return oat_checksum_;
136 }
137
Brian Carlstroma85b8372012-10-18 17:00:32 -0700138 void SetOatChecksum(uint32_t oat_checksum) {
139 oat_checksum_ = oat_checksum;
140 }
141
Mathieu Chartier5351da02016-02-17 16:19:53 -0800142 // The location that the oat file was expected to be when the image was created. The actual
143 // oat file may be at a different location for application images.
Ian Rogers13735952014-10-08 12:43:28 -0700144 uint8_t* GetOatFileBegin() const {
145 return reinterpret_cast<uint8_t*>(oat_file_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700146 }
147
Ian Rogers13735952014-10-08 12:43:28 -0700148 uint8_t* GetOatDataBegin() const {
149 return reinterpret_cast<uint8_t*>(oat_data_begin_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800150 }
151
Ian Rogers13735952014-10-08 12:43:28 -0700152 uint8_t* GetOatDataEnd() const {
153 return reinterpret_cast<uint8_t*>(oat_data_end_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800154 }
155
Ian Rogers13735952014-10-08 12:43:28 -0700156 uint8_t* GetOatFileEnd() const {
157 return reinterpret_cast<uint8_t*>(oat_file_end_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700158 }
159
Andreas Gampebda1d602016-08-29 17:43:45 -0700160 PointerSize GetPointerSize() const;
Andreas Gampe542451c2016-07-26 09:02:02 -0700161
162 uint32_t GetPointerSizeUnchecked() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700163 return pointer_size_;
164 }
165
Alex Light53cb16b2014-06-12 11:26:29 -0700166 off_t GetPatchDelta() const {
167 return patch_delta_;
168 }
169
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000170 static std::string GetOatLocationFromImageLocation(const std::string& image) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100171 return GetLocationFromImageLocation(image, "oat");
172 }
173
174 static std::string GetVdexLocationFromImageLocation(const std::string& image) {
175 return GetLocationFromImageLocation(image, "vdex");
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000176 }
177
Mathieu Chartiere401d142015-04-22 13:56:20 -0700178 enum ImageMethod {
Ian Rogers19846512012-02-24 11:42:47 -0800179 kResolutionMethod,
Jeff Hao88474b42013-10-23 16:24:40 -0700180 kImtConflictMethod,
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700181 kImtUnimplementedMethod,
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100182 kSaveAllCalleeSavesMethod,
183 kSaveRefsOnlyMethod,
184 kSaveRefsAndArgsMethod,
Vladimir Marko952dbb12016-07-28 12:01:51 +0100185 kSaveEverythingMethod,
Mingyao Yang0a87a652017-04-12 13:43:15 -0700186 kSaveEverythingMethodForClinit,
187 kSaveEverythingMethodForSuspendCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700188 kImageMethodsCount, // Number of elements in enum.
189 };
190
191 enum ImageRoot {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700192 kDexCaches,
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700193 kClassRoots,
Vladimir Markoeca3eda2016-11-09 16:26:44 +0000194 kClassLoader, // App image only.
Brian Carlstrom16192862011-09-12 17:50:06 -0700195 kImageRootsMax,
196 };
197
Mathieu Chartiere401d142015-04-22 13:56:20 -0700198 enum ImageSections {
199 kSectionObjects,
200 kSectionArtFields,
201 kSectionArtMethods,
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700202 kSectionRuntimeMethods,
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000203 kSectionImTables,
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700204 kSectionIMTConflictTables,
Vladimir Marko05792b92015-08-03 11:56:49 +0100205 kSectionDexCacheArrays,
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700206 kSectionInternedStrings,
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800207 kSectionClassTable,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700208 kSectionImageBitmap,
209 kSectionCount, // Number of elements in enum.
210 };
211
Vladimir Markoeca3eda2016-11-09 16:26:44 +0000212 static size_t NumberOfImageRoots(bool app_image) {
213 return app_image ? kImageRootsMax : kImageRootsMax - 1u;
214 }
215
Mathieu Chartiere401d142015-04-22 13:56:20 -0700216 ArtMethod* GetImageMethod(ImageMethod index) const;
217 void SetImageMethod(ImageMethod index, ArtMethod* method);
218
Vladimir Markocd87c3e2017-09-05 13:11:57 +0100219 const ImageSection& GetImageSection(ImageSections index) const {
220 DCHECK_LT(static_cast<size_t>(index), kSectionCount);
221 return sections_[index];
222 }
223
224 const ImageSection& GetObjectsSection() const {
225 return GetImageSection(kSectionObjects);
226 }
227
228 const ImageSection& GetFieldsSection() const {
229 return GetImageSection(ImageHeader::kSectionArtFields);
230 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700231
Mathieu Chartiere401d142015-04-22 13:56:20 -0700232 const ImageSection& GetMethodsSection() const {
233 return GetImageSection(kSectionArtMethods);
234 }
235
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700236 const ImageSection& GetRuntimeMethodsSection() const {
237 return GetImageSection(kSectionRuntimeMethods);
238 }
239
Vladimir Markocd87c3e2017-09-05 13:11:57 +0100240 const ImageSection& GetImTablesSection() const {
241 return GetImageSection(kSectionImTables);
242 }
243
244 const ImageSection& GetIMTConflictTablesSection() const {
245 return GetImageSection(kSectionIMTConflictTables);
246 }
247
248 const ImageSection& GetDexCacheArraysSection() const {
249 return GetImageSection(kSectionDexCacheArrays);
250 }
251
252 const ImageSection& GetInternedStringsSection() const {
253 return GetImageSection(kSectionInternedStrings);
254 }
255
256 const ImageSection& GetClassTableSection() const {
257 return GetImageSection(kSectionClassTable);
258 }
259
260 const ImageSection& GetImageBitmapSection() const {
261 return GetImageSection(kSectionImageBitmap);
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700262 }
263
Mathieu Chartier4a26f172016-01-26 14:26:18 -0800264 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800265 mirror::Object* GetImageRoot(ImageRoot image_root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700266 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4a26f172016-01-26 14:26:18 -0800267
268 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800269 mirror::ObjectArray<mirror::Object>* GetImageRoots() const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700270 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700271
Alex Light53cb16b2014-06-12 11:26:29 -0700272 void RelocateImage(off_t delta);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800273 void RelocateImageMethods(off_t delta);
274 void RelocateImageObjects(off_t delta);
Alex Light53cb16b2014-06-12 11:26:29 -0700275
Igor Murashkin46774762014-10-22 11:37:02 -0700276 bool CompilePic() const {
277 return compile_pic_ != 0;
278 }
279
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800280 bool IsPic() const {
281 return is_pic_ != 0;
282 }
283
284 uint32_t GetBootImageBegin() const {
285 return boot_image_begin_;
286 }
287
288 uint32_t GetBootImageSize() const {
289 return boot_image_size_;
290 }
291
292 uint32_t GetBootOatBegin() const {
293 return boot_oat_begin_;
294 }
295
296 uint32_t GetBootOatSize() const {
297 return boot_oat_size_;
298 }
299
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800300 StorageMode GetStorageMode() const {
301 return storage_mode_;
302 }
303
304 uint64_t GetDataSize() const {
305 return data_size_;
306 }
307
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800308 bool IsAppImage() const {
309 // App images currently require a boot image, if the size is non zero then it is an app image
310 // header.
311 return boot_image_size_ != 0u;
312 }
313
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700314 // Visit ArtMethods in the section starting at base. Includes runtime methods.
315 // TODO: Delete base parameter if it is always equal to GetImageBegin.
Andreas Gampe542451c2016-07-26 09:02:02 -0700316 void VisitPackedArtMethods(ArtMethodVisitor* visitor,
317 uint8_t* base,
318 PointerSize pointer_size) const;
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700319
320 // Visit ArtMethods in the section starting at base.
321 // TODO: Delete base parameter if it is always equal to GetImageBegin.
322 void VisitPackedArtFields(ArtFieldVisitor* visitor, uint8_t* base) const;
323
324 template <typename Visitor>
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000325 void VisitPackedImTables(const Visitor& visitor,
326 uint8_t* base,
Andreas Gampe542451c2016-07-26 09:02:02 -0700327 PointerSize pointer_size) const;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000328
329 template <typename Visitor>
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700330 void VisitPackedImtConflictTables(const Visitor& visitor,
331 uint8_t* base,
Andreas Gampe542451c2016-07-26 09:02:02 -0700332 PointerSize pointer_size) const;
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700333
Alex Light53cb16b2014-06-12 11:26:29 -0700334 private:
Ian Rogers13735952014-10-08 12:43:28 -0700335 static const uint8_t kImageMagic[4];
336 static const uint8_t kImageVersion[4];
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700337
David Brazdil7b49e6c2016-09-01 11:06:18 +0100338 static std::string GetLocationFromImageLocation(const std::string& image,
339 const std::string& extension) {
340 std::string filename = image;
341 if (filename.length() <= 3) {
342 filename += "." + extension;
343 } else {
344 filename.replace(filename.length() - 3, 3, extension);
345 }
346 return filename;
347 }
348
Ian Rogers13735952014-10-08 12:43:28 -0700349 uint8_t magic_[4];
350 uint8_t version_[4];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700351
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800352 // Required base address for mapping the image.
Ian Rogers30fab402012-01-23 15:43:46 -0800353 uint32_t image_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700354
Mathieu Chartier31e89252013-08-28 11:29:12 -0700355 // Image size, not page aligned.
356 uint32_t image_size_;
357
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800358 // Checksum of the oat file we link to for load time sanity check.
Brian Carlstrome24fa612011-09-29 00:53:55 -0700359 uint32_t oat_checksum_;
360
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800361 // Start address for oat file. Will be before oat_data_begin_ for .so files.
362 uint32_t oat_file_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700363
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800364 // Required oat address expected by image Method::GetCode() pointers.
365 uint32_t oat_data_begin_;
Brian Carlstrom16192862011-09-12 17:50:06 -0700366
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800367 // End of oat data address range for this image file.
368 uint32_t oat_data_end_;
369
370 // End of oat file address range. will be after oat_data_end_ for
371 // .so files. Used for positioning a following alloc spaces.
372 uint32_t oat_file_end_;
373
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800374 // Boot image begin and end (app image headers only).
375 uint32_t boot_image_begin_;
376 uint32_t boot_image_size_;
377
378 // Boot oat begin and end (app image headers only).
379 uint32_t boot_oat_begin_;
380 uint32_t boot_oat_size_;
381
382 // TODO: We should probably insert a boot image checksum for app images.
383
Alex Light53cb16b2014-06-12 11:26:29 -0700384 // The total delta that this image has been patched.
385 int32_t patch_delta_;
386
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800387 // Absolute address of an Object[] of objects needed to reinitialize from an image.
Brian Carlstrom16192862011-09-12 17:50:06 -0700388 uint32_t image_roots_;
389
Mathieu Chartiere401d142015-04-22 13:56:20 -0700390 // Pointer size, this affects the size of the ArtMethods.
391 uint32_t pointer_size_;
392
Igor Murashkin46774762014-10-22 11:37:02 -0700393 // Boolean (0 or 1) to denote if the image was compiled with --compile-pic option
394 const uint32_t compile_pic_;
395
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800396 // Boolean (0 or 1) to denote if the image can be mapped at a random address, this only refers to
397 // the .art file. Currently, app oat files do not depend on their app image. There are no pointers
398 // from the app oat code to the app image.
399 const uint32_t is_pic_;
400
Mathieu Chartier67ad20e2015-12-09 15:41:09 -0800401 // Image section sizes/offsets correspond to the uncompressed form.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700402 ImageSection sections_[kSectionCount];
403
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800404 // Image methods, may be inside of the boot image for app images.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700405 uint64_t image_methods_[kImageMethodsCount];
406
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800407 // Storage method for the image, the image may be compressed.
408 StorageMode storage_mode_;
409
410 // Data size for the image data excluding the bitmap and the header. For compressed images, this
411 // is the compressed size in the file.
412 uint32_t data_size_;
413
Brian Carlstrom16192862011-09-12 17:50:06 -0700414 friend class ImageWriter;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700415};
416
Mathieu Chartiere401d142015-04-22 13:56:20 -0700417std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageMethod& policy);
418std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageRoot& policy);
419std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageSections& section);
420std::ostream& operator<<(std::ostream& os, const ImageSection& section);
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800421std::ostream& operator<<(std::ostream& os, const ImageHeader::StorageMode& mode);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700422
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700423} // namespace art
424
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700425#endif // ART_RUNTIME_IMAGE_H_