blob: 2b24087d4d0ccc9649e5ad69edc7533b9d990343 [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
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +010022#include "base/bit_utils.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070023#include "base/enums.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070024#include "globals.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;
31
32class ArtMethodVisitor {
33 public:
34 virtual ~ArtMethodVisitor() {}
35
36 virtual void Visit(ArtMethod* method) = 0;
37};
38
39class ArtFieldVisitor {
40 public:
41 virtual ~ArtFieldVisitor() {}
42
43 virtual void Visit(ArtField* method) = 0;
44};
45
Mathieu Chartiere401d142015-04-22 13:56:20 -070046class PACKED(4) ImageSection {
47 public:
48 ImageSection() : offset_(0), size_(0) { }
49 ImageSection(uint32_t offset, uint32_t size) : offset_(offset), size_(size) { }
50 ImageSection(const ImageSection& section) = default;
51 ImageSection& operator=(const ImageSection& section) = default;
52
53 uint32_t Offset() const {
54 return offset_;
55 }
56
57 uint32_t Size() const {
58 return size_;
59 }
60
61 uint32_t End() const {
62 return Offset() + Size();
63 }
64
65 bool Contains(uint64_t offset) const {
66 return offset - offset_ < size_;
67 }
68
69 private:
70 uint32_t offset_;
71 uint32_t size_;
72};
73
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070074// header of image files written by ImageWriter, read and validated by Space.
Ian Rogersdf1ce912012-11-27 17:07:11 -080075class PACKED(4) ImageHeader {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070076 public:
Mathieu Chartierceb07b32015-12-10 09:33:21 -080077 enum StorageMode : uint32_t {
78 kStorageModeUncompressed,
79 kStorageModeLZ4,
Mathieu Chartiera6e81ed2016-02-25 13:52:10 -080080 kStorageModeLZ4HC,
Mathieu Chartierceb07b32015-12-10 09:33:21 -080081 kStorageModeCount, // Number of elements in enum.
82 };
83 static constexpr StorageMode kDefaultStorageMode = kStorageModeUncompressed;
84
Sebastien Hertzaa50d3a2015-08-25 15:25:41 +020085 ImageHeader()
Mathieu Chartierceb07b32015-12-10 09:33:21 -080086 : image_begin_(0U),
87 image_size_(0U),
88 oat_checksum_(0U),
89 oat_file_begin_(0U),
90 oat_data_begin_(0U),
91 oat_data_end_(0U),
92 oat_file_end_(0U),
Mathieu Chartierfbc31082016-01-24 11:59:56 -080093 boot_image_begin_(0U),
94 boot_image_size_(0U),
95 boot_oat_begin_(0U),
96 boot_oat_size_(0U),
Mathieu Chartierceb07b32015-12-10 09:33:21 -080097 patch_delta_(0),
98 image_roots_(0U),
99 pointer_size_(0U),
100 compile_pic_(0),
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800101 is_pic_(0),
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800102 storage_mode_(kDefaultStorageMode),
103 data_size_(0) {}
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700104
Ian Rogers30fab402012-01-23 15:43:46 -0800105 ImageHeader(uint32_t image_begin,
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800106 uint32_t image_size,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700107 ImageSection* sections,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700108 uint32_t image_roots,
109 uint32_t oat_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800110 uint32_t oat_file_begin,
111 uint32_t oat_data_begin,
112 uint32_t oat_data_end,
Igor Murashkin46774762014-10-22 11:37:02 -0700113 uint32_t oat_file_end,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800114 uint32_t boot_image_begin,
115 uint32_t boot_image_size,
116 uint32_t boot_oat_begin,
117 uint32_t boot_oat_size,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700118 uint32_t pointer_size,
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800119 bool compile_pic,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800120 bool is_pic,
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800121 StorageMode storage_mode,
122 size_t data_size);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700123
Brian Carlstrom68708f52013-09-03 14:15:31 -0700124 bool IsValid() const;
125 const char* GetMagic() const;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700126
Ian Rogers13735952014-10-08 12:43:28 -0700127 uint8_t* GetImageBegin() const {
128 return reinterpret_cast<uint8_t*>(image_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700129 }
130
Mathieu Chartier31e89252013-08-28 11:29:12 -0700131 size_t GetImageSize() const {
132 return static_cast<uint32_t>(image_size_);
133 }
134
Brian Carlstrome24fa612011-09-29 00:53:55 -0700135 uint32_t GetOatChecksum() const {
136 return oat_checksum_;
137 }
138
Brian Carlstroma85b8372012-10-18 17:00:32 -0700139 void SetOatChecksum(uint32_t oat_checksum) {
140 oat_checksum_ = oat_checksum;
141 }
142
Mathieu Chartier5351da02016-02-17 16:19:53 -0800143 // The location that the oat file was expected to be when the image was created. The actual
144 // oat file may be at a different location for application images.
Ian Rogers13735952014-10-08 12:43:28 -0700145 uint8_t* GetOatFileBegin() const {
146 return reinterpret_cast<uint8_t*>(oat_file_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700147 }
148
Ian Rogers13735952014-10-08 12:43:28 -0700149 uint8_t* GetOatDataBegin() const {
150 return reinterpret_cast<uint8_t*>(oat_data_begin_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800151 }
152
Ian Rogers13735952014-10-08 12:43:28 -0700153 uint8_t* GetOatDataEnd() const {
154 return reinterpret_cast<uint8_t*>(oat_data_end_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800155 }
156
Ian Rogers13735952014-10-08 12:43:28 -0700157 uint8_t* GetOatFileEnd() const {
158 return reinterpret_cast<uint8_t*>(oat_file_end_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700159 }
160
Andreas Gampebda1d602016-08-29 17:43:45 -0700161 PointerSize GetPointerSize() const;
Andreas Gampe542451c2016-07-26 09:02:02 -0700162
163 uint32_t GetPointerSizeUnchecked() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700164 return pointer_size_;
165 }
166
Alex Light53cb16b2014-06-12 11:26:29 -0700167 off_t GetPatchDelta() const {
168 return patch_delta_;
169 }
170
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000171 static std::string GetOatLocationFromImageLocation(const std::string& image) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100172 return GetLocationFromImageLocation(image, "oat");
173 }
174
175 static std::string GetVdexLocationFromImageLocation(const std::string& image) {
176 return GetLocationFromImageLocation(image, "vdex");
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000177 }
178
Mathieu Chartiere401d142015-04-22 13:56:20 -0700179 enum ImageMethod {
Ian Rogers19846512012-02-24 11:42:47 -0800180 kResolutionMethod,
Jeff Hao88474b42013-10-23 16:24:40 -0700181 kImtConflictMethod,
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700182 kImtUnimplementedMethod,
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100183 kSaveAllCalleeSavesMethod,
184 kSaveRefsOnlyMethod,
185 kSaveRefsAndArgsMethod,
Vladimir Marko952dbb12016-07-28 12:01:51 +0100186 kSaveEverythingMethod,
Mingyao Yang0a87a652017-04-12 13:43:15 -0700187 kSaveEverythingMethodForClinit,
188 kSaveEverythingMethodForSuspendCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700189 kImageMethodsCount, // Number of elements in enum.
190 };
191
192 enum ImageRoot {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700193 kDexCaches,
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700194 kClassRoots,
Vladimir Markoeca3eda2016-11-09 16:26:44 +0000195 kClassLoader, // App image only.
Brian Carlstrom16192862011-09-12 17:50:06 -0700196 kImageRootsMax,
197 };
198
Mathieu Chartiere401d142015-04-22 13:56:20 -0700199 enum ImageSections {
200 kSectionObjects,
201 kSectionArtFields,
202 kSectionArtMethods,
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700203 kSectionRuntimeMethods,
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000204 kSectionImTables,
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700205 kSectionIMTConflictTables,
Vladimir Marko05792b92015-08-03 11:56:49 +0100206 kSectionDexCacheArrays,
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700207 kSectionInternedStrings,
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800208 kSectionClassTable,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700209 kSectionImageBitmap,
210 kSectionCount, // Number of elements in enum.
211 };
212
Vladimir Markoeca3eda2016-11-09 16:26:44 +0000213 static size_t NumberOfImageRoots(bool app_image) {
214 return app_image ? kImageRootsMax : kImageRootsMax - 1u;
215 }
216
Mathieu Chartiere401d142015-04-22 13:56:20 -0700217 ArtMethod* GetImageMethod(ImageMethod index) const;
218 void SetImageMethod(ImageMethod index, ArtMethod* method);
219
Vladimir Markocd87c3e2017-09-05 13:11:57 +0100220 const ImageSection& GetImageSection(ImageSections index) const {
221 DCHECK_LT(static_cast<size_t>(index), kSectionCount);
222 return sections_[index];
223 }
224
225 const ImageSection& GetObjectsSection() const {
226 return GetImageSection(kSectionObjects);
227 }
228
229 const ImageSection& GetFieldsSection() const {
230 return GetImageSection(ImageHeader::kSectionArtFields);
231 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700232
Mathieu Chartiere401d142015-04-22 13:56:20 -0700233 const ImageSection& GetMethodsSection() const {
234 return GetImageSection(kSectionArtMethods);
235 }
236
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700237 const ImageSection& GetRuntimeMethodsSection() const {
238 return GetImageSection(kSectionRuntimeMethods);
239 }
240
Vladimir Markocd87c3e2017-09-05 13:11:57 +0100241 const ImageSection& GetImTablesSection() const {
242 return GetImageSection(kSectionImTables);
243 }
244
245 const ImageSection& GetIMTConflictTablesSection() const {
246 return GetImageSection(kSectionIMTConflictTables);
247 }
248
249 const ImageSection& GetDexCacheArraysSection() const {
250 return GetImageSection(kSectionDexCacheArrays);
251 }
252
253 const ImageSection& GetInternedStringsSection() const {
254 return GetImageSection(kSectionInternedStrings);
255 }
256
257 const ImageSection& GetClassTableSection() const {
258 return GetImageSection(kSectionClassTable);
259 }
260
261 const ImageSection& GetImageBitmapSection() const {
262 return GetImageSection(kSectionImageBitmap);
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700263 }
264
Mathieu Chartier4a26f172016-01-26 14:26:18 -0800265 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266 mirror::Object* GetImageRoot(ImageRoot image_root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700267 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4a26f172016-01-26 14:26:18 -0800268
269 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800270 mirror::ObjectArray<mirror::Object>* GetImageRoots() const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700271 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700272
Alex Light53cb16b2014-06-12 11:26:29 -0700273 void RelocateImage(off_t delta);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800274 void RelocateImageMethods(off_t delta);
275 void RelocateImageObjects(off_t delta);
Alex Light53cb16b2014-06-12 11:26:29 -0700276
Igor Murashkin46774762014-10-22 11:37:02 -0700277 bool CompilePic() const {
278 return compile_pic_ != 0;
279 }
280
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800281 bool IsPic() const {
282 return is_pic_ != 0;
283 }
284
285 uint32_t GetBootImageBegin() const {
286 return boot_image_begin_;
287 }
288
289 uint32_t GetBootImageSize() const {
290 return boot_image_size_;
291 }
292
293 uint32_t GetBootOatBegin() const {
294 return boot_oat_begin_;
295 }
296
297 uint32_t GetBootOatSize() const {
298 return boot_oat_size_;
299 }
300
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800301 StorageMode GetStorageMode() const {
302 return storage_mode_;
303 }
304
305 uint64_t GetDataSize() const {
306 return data_size_;
307 }
308
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800309 bool IsAppImage() const {
310 // App images currently require a boot image, if the size is non zero then it is an app image
311 // header.
312 return boot_image_size_ != 0u;
313 }
314
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100315 uint32_t GetBootImageConstantTablesOffset() const {
316 // Interned strings table and class table for boot image are mmapped read only.
317 DCHECK(!IsAppImage());
318 const ImageSection& interned_strings = GetInternedStringsSection();
319 DCHECK_ALIGNED(interned_strings.Offset(), kPageSize);
320 return interned_strings.Offset();
321 }
322
323 uint32_t GetBootImageConstantTablesSize() const {
324 uint32_t start_offset = GetBootImageConstantTablesOffset();
325 const ImageSection& class_table = GetClassTableSection();
326 DCHECK_LE(start_offset, class_table.Offset());
327 size_t tables_size = class_table.Offset() + class_table.Size() - start_offset;
328 return RoundUp(tables_size, kPageSize);
329 }
330
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700331 // Visit ArtMethods in the section starting at base. Includes runtime methods.
332 // TODO: Delete base parameter if it is always equal to GetImageBegin.
Andreas Gampe542451c2016-07-26 09:02:02 -0700333 void VisitPackedArtMethods(ArtMethodVisitor* visitor,
334 uint8_t* base,
335 PointerSize pointer_size) const;
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700336
337 // Visit ArtMethods in the section starting at base.
338 // TODO: Delete base parameter if it is always equal to GetImageBegin.
339 void VisitPackedArtFields(ArtFieldVisitor* visitor, uint8_t* base) const;
340
341 template <typename Visitor>
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000342 void VisitPackedImTables(const Visitor& visitor,
343 uint8_t* base,
Andreas Gampe542451c2016-07-26 09:02:02 -0700344 PointerSize pointer_size) const;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000345
346 template <typename Visitor>
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700347 void VisitPackedImtConflictTables(const Visitor& visitor,
348 uint8_t* base,
Andreas Gampe542451c2016-07-26 09:02:02 -0700349 PointerSize pointer_size) const;
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700350
Alex Light53cb16b2014-06-12 11:26:29 -0700351 private:
Ian Rogers13735952014-10-08 12:43:28 -0700352 static const uint8_t kImageMagic[4];
353 static const uint8_t kImageVersion[4];
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700354
David Brazdil7b49e6c2016-09-01 11:06:18 +0100355 static std::string GetLocationFromImageLocation(const std::string& image,
356 const std::string& extension) {
357 std::string filename = image;
358 if (filename.length() <= 3) {
359 filename += "." + extension;
360 } else {
361 filename.replace(filename.length() - 3, 3, extension);
362 }
363 return filename;
364 }
365
Ian Rogers13735952014-10-08 12:43:28 -0700366 uint8_t magic_[4];
367 uint8_t version_[4];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700368
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800369 // Required base address for mapping the image.
Ian Rogers30fab402012-01-23 15:43:46 -0800370 uint32_t image_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700371
Mathieu Chartier31e89252013-08-28 11:29:12 -0700372 // Image size, not page aligned.
373 uint32_t image_size_;
374
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800375 // Checksum of the oat file we link to for load time sanity check.
Brian Carlstrome24fa612011-09-29 00:53:55 -0700376 uint32_t oat_checksum_;
377
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800378 // Start address for oat file. Will be before oat_data_begin_ for .so files.
379 uint32_t oat_file_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700380
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800381 // Required oat address expected by image Method::GetCode() pointers.
382 uint32_t oat_data_begin_;
Brian Carlstrom16192862011-09-12 17:50:06 -0700383
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800384 // End of oat data address range for this image file.
385 uint32_t oat_data_end_;
386
387 // End of oat file address range. will be after oat_data_end_ for
388 // .so files. Used for positioning a following alloc spaces.
389 uint32_t oat_file_end_;
390
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800391 // Boot image begin and end (app image headers only).
392 uint32_t boot_image_begin_;
393 uint32_t boot_image_size_;
394
395 // Boot oat begin and end (app image headers only).
396 uint32_t boot_oat_begin_;
397 uint32_t boot_oat_size_;
398
399 // TODO: We should probably insert a boot image checksum for app images.
400
Alex Light53cb16b2014-06-12 11:26:29 -0700401 // The total delta that this image has been patched.
402 int32_t patch_delta_;
403
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800404 // Absolute address of an Object[] of objects needed to reinitialize from an image.
Brian Carlstrom16192862011-09-12 17:50:06 -0700405 uint32_t image_roots_;
406
Mathieu Chartiere401d142015-04-22 13:56:20 -0700407 // Pointer size, this affects the size of the ArtMethods.
408 uint32_t pointer_size_;
409
Igor Murashkin46774762014-10-22 11:37:02 -0700410 // Boolean (0 or 1) to denote if the image was compiled with --compile-pic option
411 const uint32_t compile_pic_;
412
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800413 // Boolean (0 or 1) to denote if the image can be mapped at a random address, this only refers to
414 // the .art file. Currently, app oat files do not depend on their app image. There are no pointers
415 // from the app oat code to the app image.
416 const uint32_t is_pic_;
417
Mathieu Chartier67ad20e2015-12-09 15:41:09 -0800418 // Image section sizes/offsets correspond to the uncompressed form.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700419 ImageSection sections_[kSectionCount];
420
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800421 // Image methods, may be inside of the boot image for app images.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700422 uint64_t image_methods_[kImageMethodsCount];
423
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800424 // Storage method for the image, the image may be compressed.
425 StorageMode storage_mode_;
426
427 // Data size for the image data excluding the bitmap and the header. For compressed images, this
428 // is the compressed size in the file.
429 uint32_t data_size_;
430
Brian Carlstrom16192862011-09-12 17:50:06 -0700431 friend class ImageWriter;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700432};
433
Mathieu Chartiere401d142015-04-22 13:56:20 -0700434std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageMethod& policy);
435std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageRoot& policy);
436std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageSections& section);
437std::ostream& operator<<(std::ostream& os, const ImageSection& section);
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800438std::ostream& operator<<(std::ostream& os, const ImageHeader::StorageMode& mode);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700439
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700440} // namespace art
441
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700442#endif // ART_RUNTIME_IMAGE_H_