blob: 96889f6a2d7a20b5e6847fb7d0a58e48ac2f55dd [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
22#include "globals.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "mirror/object.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070024
25namespace art {
26
Mathieu Chartier54d220e2015-07-30 16:20:06 -070027class ArtField;
28class ArtMethod;
29
30class ArtMethodVisitor {
31 public:
32 virtual ~ArtMethodVisitor() {}
33
34 virtual void Visit(ArtMethod* method) = 0;
35};
36
37class ArtFieldVisitor {
38 public:
39 virtual ~ArtFieldVisitor() {}
40
41 virtual void Visit(ArtField* method) = 0;
42};
43
Mathieu Chartiere401d142015-04-22 13:56:20 -070044class PACKED(4) ImageSection {
45 public:
46 ImageSection() : offset_(0), size_(0) { }
47 ImageSection(uint32_t offset, uint32_t size) : offset_(offset), size_(size) { }
48 ImageSection(const ImageSection& section) = default;
49 ImageSection& operator=(const ImageSection& section) = default;
50
51 uint32_t Offset() const {
52 return offset_;
53 }
54
55 uint32_t Size() const {
56 return size_;
57 }
58
59 uint32_t End() const {
60 return Offset() + Size();
61 }
62
63 bool Contains(uint64_t offset) const {
64 return offset - offset_ < size_;
65 }
66
67 private:
68 uint32_t offset_;
69 uint32_t size_;
70};
71
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070072// header of image files written by ImageWriter, read and validated by Space.
Ian Rogersdf1ce912012-11-27 17:07:11 -080073class PACKED(4) ImageHeader {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070074 public:
Mathieu Chartierceb07b32015-12-10 09:33:21 -080075 enum StorageMode : uint32_t {
76 kStorageModeUncompressed,
77 kStorageModeLZ4,
Mathieu Chartiera6e81ed2016-02-25 13:52:10 -080078 kStorageModeLZ4HC,
Mathieu Chartierceb07b32015-12-10 09:33:21 -080079 kStorageModeCount, // Number of elements in enum.
80 };
81 static constexpr StorageMode kDefaultStorageMode = kStorageModeUncompressed;
82
Sebastien Hertzaa50d3a2015-08-25 15:25:41 +020083 ImageHeader()
Mathieu Chartierceb07b32015-12-10 09:33:21 -080084 : image_begin_(0U),
85 image_size_(0U),
86 oat_checksum_(0U),
87 oat_file_begin_(0U),
88 oat_data_begin_(0U),
89 oat_data_end_(0U),
90 oat_file_end_(0U),
Mathieu Chartierfbc31082016-01-24 11:59:56 -080091 boot_image_begin_(0U),
92 boot_image_size_(0U),
93 boot_oat_begin_(0U),
94 boot_oat_size_(0U),
Mathieu Chartierceb07b32015-12-10 09:33:21 -080095 patch_delta_(0),
96 image_roots_(0U),
97 pointer_size_(0U),
98 compile_pic_(0),
Mathieu Chartierfbc31082016-01-24 11:59:56 -080099 is_pic_(0),
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800100 storage_mode_(kDefaultStorageMode),
101 data_size_(0) {}
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700102
Ian Rogers30fab402012-01-23 15:43:46 -0800103 ImageHeader(uint32_t image_begin,
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800104 uint32_t image_size,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700105 ImageSection* sections,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700106 uint32_t image_roots,
107 uint32_t oat_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800108 uint32_t oat_file_begin,
109 uint32_t oat_data_begin,
110 uint32_t oat_data_end,
Igor Murashkin46774762014-10-22 11:37:02 -0700111 uint32_t oat_file_end,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800112 uint32_t boot_image_begin,
113 uint32_t boot_image_size,
114 uint32_t boot_oat_begin,
115 uint32_t boot_oat_size,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700116 uint32_t pointer_size,
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800117 bool compile_pic,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800118 bool is_pic,
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800119 StorageMode storage_mode,
120 size_t data_size);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700121
Brian Carlstrom68708f52013-09-03 14:15:31 -0700122 bool IsValid() const;
123 const char* GetMagic() const;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700124
Ian Rogers13735952014-10-08 12:43:28 -0700125 uint8_t* GetImageBegin() const {
126 return reinterpret_cast<uint8_t*>(image_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700127 }
128
Mathieu Chartier31e89252013-08-28 11:29:12 -0700129 size_t GetImageSize() const {
130 return static_cast<uint32_t>(image_size_);
131 }
132
Brian Carlstrome24fa612011-09-29 00:53:55 -0700133 uint32_t GetOatChecksum() const {
134 return oat_checksum_;
135 }
136
Brian Carlstroma85b8372012-10-18 17:00:32 -0700137 void SetOatChecksum(uint32_t oat_checksum) {
138 oat_checksum_ = oat_checksum;
139 }
140
Mathieu Chartier5351da02016-02-17 16:19:53 -0800141 // The location that the oat file was expected to be when the image was created. The actual
142 // oat file may be at a different location for application images.
Ian Rogers13735952014-10-08 12:43:28 -0700143 uint8_t* GetOatFileBegin() const {
144 return reinterpret_cast<uint8_t*>(oat_file_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700145 }
146
Ian Rogers13735952014-10-08 12:43:28 -0700147 uint8_t* GetOatDataBegin() const {
148 return reinterpret_cast<uint8_t*>(oat_data_begin_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800149 }
150
Ian Rogers13735952014-10-08 12:43:28 -0700151 uint8_t* GetOatDataEnd() const {
152 return reinterpret_cast<uint8_t*>(oat_data_end_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800153 }
154
Ian Rogers13735952014-10-08 12:43:28 -0700155 uint8_t* GetOatFileEnd() const {
156 return reinterpret_cast<uint8_t*>(oat_file_end_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700157 }
158
Mathieu Chartiere401d142015-04-22 13:56:20 -0700159 uint32_t GetPointerSize() const {
160 return pointer_size_;
161 }
162
Alex Light53cb16b2014-06-12 11:26:29 -0700163 off_t GetPatchDelta() const {
164 return patch_delta_;
165 }
166
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000167 static std::string GetOatLocationFromImageLocation(const std::string& image) {
168 std::string oat_filename = image;
169 if (oat_filename.length() <= 3) {
Ian Rogersb9beb2e2014-05-09 16:57:40 -0700170 oat_filename += ".oat";
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000171 } else {
172 oat_filename.replace(oat_filename.length() - 3, 3, "oat");
173 }
174 return oat_filename;
175 }
176
Mathieu Chartiere401d142015-04-22 13:56:20 -0700177 enum ImageMethod {
Ian Rogers19846512012-02-24 11:42:47 -0800178 kResolutionMethod,
Jeff Hao88474b42013-10-23 16:24:40 -0700179 kImtConflictMethod,
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700180 kImtUnimplementedMethod,
Ian Rogersff1ed472011-09-20 13:46:24 -0700181 kCalleeSaveMethod,
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700182 kRefsOnlySaveMethod,
183 kRefsAndArgsSaveMethod,
Vladimir Marko952dbb12016-07-28 12:01:51 +0100184 kSaveEverythingMethod,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700185 kImageMethodsCount, // Number of elements in enum.
186 };
187
188 enum ImageRoot {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700189 kDexCaches,
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700190 kClassRoots,
Brian Carlstrom16192862011-09-12 17:50:06 -0700191 kImageRootsMax,
192 };
193
Mathieu Chartiere401d142015-04-22 13:56:20 -0700194 enum ImageSections {
195 kSectionObjects,
196 kSectionArtFields,
197 kSectionArtMethods,
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700198 kSectionRuntimeMethods,
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000199 kSectionImTables,
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700200 kSectionIMTConflictTables,
Vladimir Marko05792b92015-08-03 11:56:49 +0100201 kSectionDexCacheArrays,
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700202 kSectionInternedStrings,
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800203 kSectionClassTable,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700204 kSectionImageBitmap,
205 kSectionCount, // Number of elements in enum.
206 };
207
208 ArtMethod* GetImageMethod(ImageMethod index) const;
209 void SetImageMethod(ImageMethod index, ArtMethod* method);
210
211 const ImageSection& GetImageSection(ImageSections index) const;
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700212
Mathieu Chartiere401d142015-04-22 13:56:20 -0700213 const ImageSection& GetMethodsSection() const {
214 return GetImageSection(kSectionArtMethods);
215 }
216
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700217 const ImageSection& GetRuntimeMethodsSection() const {
218 return GetImageSection(kSectionRuntimeMethods);
219 }
220
221 const ImageSection& GetFieldsSection() const {
222 return GetImageSection(ImageHeader::kSectionArtFields);
223 }
224
Mathieu Chartier4a26f172016-01-26 14:26:18 -0800225 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800226 mirror::Object* GetImageRoot(ImageRoot image_root) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700227 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier4a26f172016-01-26 14:26:18 -0800228
229 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800230 mirror::ObjectArray<mirror::Object>* GetImageRoots() const
Mathieu Chartier90443472015-07-16 20:32:27 -0700231 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700232
Alex Light53cb16b2014-06-12 11:26:29 -0700233 void RelocateImage(off_t delta);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800234 void RelocateImageMethods(off_t delta);
235 void RelocateImageObjects(off_t delta);
Alex Light53cb16b2014-06-12 11:26:29 -0700236
Igor Murashkin46774762014-10-22 11:37:02 -0700237 bool CompilePic() const {
238 return compile_pic_ != 0;
239 }
240
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800241 bool IsPic() const {
242 return is_pic_ != 0;
243 }
244
245 uint32_t GetBootImageBegin() const {
246 return boot_image_begin_;
247 }
248
249 uint32_t GetBootImageSize() const {
250 return boot_image_size_;
251 }
252
253 uint32_t GetBootOatBegin() const {
254 return boot_oat_begin_;
255 }
256
257 uint32_t GetBootOatSize() const {
258 return boot_oat_size_;
259 }
260
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800261 StorageMode GetStorageMode() const {
262 return storage_mode_;
263 }
264
265 uint64_t GetDataSize() const {
266 return data_size_;
267 }
268
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800269 bool IsAppImage() const {
270 // App images currently require a boot image, if the size is non zero then it is an app image
271 // header.
272 return boot_image_size_ != 0u;
273 }
274
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700275 // Visit ArtMethods in the section starting at base. Includes runtime methods.
276 // TODO: Delete base parameter if it is always equal to GetImageBegin.
277 void VisitPackedArtMethods(ArtMethodVisitor* visitor, uint8_t* base, size_t pointer_size) const;
278
279 // Visit ArtMethods in the section starting at base.
280 // TODO: Delete base parameter if it is always equal to GetImageBegin.
281 void VisitPackedArtFields(ArtFieldVisitor* visitor, uint8_t* base) const;
282
283 template <typename Visitor>
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000284 void VisitPackedImTables(const Visitor& visitor,
285 uint8_t* base,
286 size_t pointer_size) const;
287
288 template <typename Visitor>
Mathieu Chartiere42888f2016-04-14 10:49:19 -0700289 void VisitPackedImtConflictTables(const Visitor& visitor,
290 uint8_t* base,
291 size_t pointer_size) const;
292
Alex Light53cb16b2014-06-12 11:26:29 -0700293 private:
Ian Rogers13735952014-10-08 12:43:28 -0700294 static const uint8_t kImageMagic[4];
295 static const uint8_t kImageVersion[4];
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700296
Ian Rogers13735952014-10-08 12:43:28 -0700297 uint8_t magic_[4];
298 uint8_t version_[4];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700299
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800300 // Required base address for mapping the image.
Ian Rogers30fab402012-01-23 15:43:46 -0800301 uint32_t image_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700302
Mathieu Chartier31e89252013-08-28 11:29:12 -0700303 // Image size, not page aligned.
304 uint32_t image_size_;
305
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800306 // Checksum of the oat file we link to for load time sanity check.
Brian Carlstrome24fa612011-09-29 00:53:55 -0700307 uint32_t oat_checksum_;
308
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800309 // Start address for oat file. Will be before oat_data_begin_ for .so files.
310 uint32_t oat_file_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700311
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800312 // Required oat address expected by image Method::GetCode() pointers.
313 uint32_t oat_data_begin_;
Brian Carlstrom16192862011-09-12 17:50:06 -0700314
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800315 // End of oat data address range for this image file.
316 uint32_t oat_data_end_;
317
318 // End of oat file address range. will be after oat_data_end_ for
319 // .so files. Used for positioning a following alloc spaces.
320 uint32_t oat_file_end_;
321
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800322 // Boot image begin and end (app image headers only).
323 uint32_t boot_image_begin_;
324 uint32_t boot_image_size_;
325
326 // Boot oat begin and end (app image headers only).
327 uint32_t boot_oat_begin_;
328 uint32_t boot_oat_size_;
329
330 // TODO: We should probably insert a boot image checksum for app images.
331
Alex Light53cb16b2014-06-12 11:26:29 -0700332 // The total delta that this image has been patched.
333 int32_t patch_delta_;
334
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800335 // Absolute address of an Object[] of objects needed to reinitialize from an image.
Brian Carlstrom16192862011-09-12 17:50:06 -0700336 uint32_t image_roots_;
337
Mathieu Chartiere401d142015-04-22 13:56:20 -0700338 // Pointer size, this affects the size of the ArtMethods.
339 uint32_t pointer_size_;
340
Igor Murashkin46774762014-10-22 11:37:02 -0700341 // Boolean (0 or 1) to denote if the image was compiled with --compile-pic option
342 const uint32_t compile_pic_;
343
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800344 // Boolean (0 or 1) to denote if the image can be mapped at a random address, this only refers to
345 // the .art file. Currently, app oat files do not depend on their app image. There are no pointers
346 // from the app oat code to the app image.
347 const uint32_t is_pic_;
348
Mathieu Chartier67ad20e2015-12-09 15:41:09 -0800349 // Image section sizes/offsets correspond to the uncompressed form.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700350 ImageSection sections_[kSectionCount];
351
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800352 // Image methods, may be inside of the boot image for app images.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700353 uint64_t image_methods_[kImageMethodsCount];
354
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800355 // Storage method for the image, the image may be compressed.
356 StorageMode storage_mode_;
357
358 // Data size for the image data excluding the bitmap and the header. For compressed images, this
359 // is the compressed size in the file.
360 uint32_t data_size_;
361
Brian Carlstrom16192862011-09-12 17:50:06 -0700362 friend class ImageWriter;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700363};
364
Mathieu Chartiere401d142015-04-22 13:56:20 -0700365std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageMethod& policy);
366std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageRoot& policy);
367std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageSections& section);
368std::ostream& operator<<(std::ostream& os, const ImageSection& section);
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800369std::ostream& operator<<(std::ostream& os, const ImageHeader::StorageMode& mode);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700370
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700371} // namespace art
372
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700373#endif // ART_RUNTIME_IMAGE_H_