blob: c449e43243bfcd1ddf65860b5881c41ff3fb8335 [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
Mathieu Chartier54d220e2015-07-30 16:20:06 -070067 // Visit ArtMethods in the section starting at base.
Vladimir Markocf36d492015-08-12 19:27:26 +010068 void VisitPackedArtMethods(ArtMethodVisitor* visitor, uint8_t* base, size_t pointer_size) const;
Mathieu Chartier54d220e2015-07-30 16:20:06 -070069
70 // Visit ArtMethods in the section starting at base.
71 void VisitPackedArtFields(ArtFieldVisitor* visitor, uint8_t* base) const;
72
Mathieu Chartiere401d142015-04-22 13:56:20 -070073 private:
74 uint32_t offset_;
75 uint32_t size_;
76};
77
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070078// header of image files written by ImageWriter, read and validated by Space.
Ian Rogersdf1ce912012-11-27 17:07:11 -080079class PACKED(4) ImageHeader {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070080 public:
Mathieu Chartierceb07b32015-12-10 09:33:21 -080081 enum StorageMode : uint32_t {
82 kStorageModeUncompressed,
83 kStorageModeLZ4,
84 kStorageModeCount, // Number of elements in enum.
85 };
86 static constexpr StorageMode kDefaultStorageMode = kStorageModeUncompressed;
87
Sebastien Hertzaa50d3a2015-08-25 15:25:41 +020088 ImageHeader()
Mathieu Chartierceb07b32015-12-10 09:33:21 -080089 : image_begin_(0U),
90 image_size_(0U),
91 oat_checksum_(0U),
92 oat_file_begin_(0U),
93 oat_data_begin_(0U),
94 oat_data_end_(0U),
95 oat_file_end_(0U),
Mathieu Chartierfbc31082016-01-24 11:59:56 -080096 boot_image_begin_(0U),
97 boot_image_size_(0U),
98 boot_oat_begin_(0U),
99 boot_oat_size_(0U),
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800100 patch_delta_(0),
101 image_roots_(0U),
102 pointer_size_(0U),
103 compile_pic_(0),
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800104 is_pic_(0),
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800105 storage_mode_(kDefaultStorageMode),
106 data_size_(0) {}
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700107
Ian Rogers30fab402012-01-23 15:43:46 -0800108 ImageHeader(uint32_t image_begin,
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800109 uint32_t image_size,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700110 ImageSection* sections,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700111 uint32_t image_roots,
112 uint32_t oat_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800113 uint32_t oat_file_begin,
114 uint32_t oat_data_begin,
115 uint32_t oat_data_end,
Igor Murashkin46774762014-10-22 11:37:02 -0700116 uint32_t oat_file_end,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800117 uint32_t boot_image_begin,
118 uint32_t boot_image_size,
119 uint32_t boot_oat_begin,
120 uint32_t boot_oat_size,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700121 uint32_t pointer_size,
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800122 bool compile_pic,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800123 bool is_pic,
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800124 StorageMode storage_mode,
125 size_t data_size);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700126
Brian Carlstrom68708f52013-09-03 14:15:31 -0700127 bool IsValid() const;
128 const char* GetMagic() const;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700129
Ian Rogers13735952014-10-08 12:43:28 -0700130 uint8_t* GetImageBegin() const {
131 return reinterpret_cast<uint8_t*>(image_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700132 }
133
Mathieu Chartier31e89252013-08-28 11:29:12 -0700134 size_t GetImageSize() const {
135 return static_cast<uint32_t>(image_size_);
136 }
137
Brian Carlstrome24fa612011-09-29 00:53:55 -0700138 uint32_t GetOatChecksum() const {
139 return oat_checksum_;
140 }
141
Brian Carlstroma85b8372012-10-18 17:00:32 -0700142 void SetOatChecksum(uint32_t oat_checksum) {
143 oat_checksum_ = oat_checksum;
144 }
145
Ian Rogers13735952014-10-08 12:43:28 -0700146 uint8_t* GetOatFileBegin() const {
147 return reinterpret_cast<uint8_t*>(oat_file_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700148 }
149
Ian Rogers13735952014-10-08 12:43:28 -0700150 uint8_t* GetOatDataBegin() const {
151 return reinterpret_cast<uint8_t*>(oat_data_begin_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800152 }
153
Ian Rogers13735952014-10-08 12:43:28 -0700154 uint8_t* GetOatDataEnd() const {
155 return reinterpret_cast<uint8_t*>(oat_data_end_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800156 }
157
Ian Rogers13735952014-10-08 12:43:28 -0700158 uint8_t* GetOatFileEnd() const {
159 return reinterpret_cast<uint8_t*>(oat_file_end_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700160 }
161
Mathieu Chartiere401d142015-04-22 13:56:20 -0700162 uint32_t GetPointerSize() const {
163 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) {
171 std::string oat_filename = image;
172 if (oat_filename.length() <= 3) {
Ian Rogersb9beb2e2014-05-09 16:57:40 -0700173 oat_filename += ".oat";
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000174 } else {
175 oat_filename.replace(oat_filename.length() - 3, 3, "oat");
176 }
177 return oat_filename;
178 }
179
Mathieu Chartiere401d142015-04-22 13:56:20 -0700180 enum ImageMethod {
Ian Rogers19846512012-02-24 11:42:47 -0800181 kResolutionMethod,
Jeff Hao88474b42013-10-23 16:24:40 -0700182 kImtConflictMethod,
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700183 kImtUnimplementedMethod,
Ian Rogersff1ed472011-09-20 13:46:24 -0700184 kCalleeSaveMethod,
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700185 kRefsOnlySaveMethod,
186 kRefsAndArgsSaveMethod,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700187 kImageMethodsCount, // Number of elements in enum.
188 };
189
190 enum ImageRoot {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700191 kDexCaches,
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700192 kClassRoots,
Brian Carlstrom16192862011-09-12 17:50:06 -0700193 kImageRootsMax,
194 };
195
Mathieu Chartiere401d142015-04-22 13:56:20 -0700196 enum ImageSections {
197 kSectionObjects,
198 kSectionArtFields,
199 kSectionArtMethods,
Vladimir Marko05792b92015-08-03 11:56:49 +0100200 kSectionDexCacheArrays,
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700201 kSectionInternedStrings,
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800202 kSectionClassTable,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700203 kSectionImageBitmap,
204 kSectionCount, // Number of elements in enum.
205 };
206
207 ArtMethod* GetImageMethod(ImageMethod index) const;
208 void SetImageMethod(ImageMethod index, ArtMethod* method);
209
210 const ImageSection& GetImageSection(ImageSections index) const;
211 const ImageSection& GetMethodsSection() const {
212 return GetImageSection(kSectionArtMethods);
213 }
214
Mathieu Chartier4a26f172016-01-26 14:26:18 -0800215 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800216 mirror::Object* GetImageRoot(ImageRoot image_root) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700217 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier4a26f172016-01-26 14:26:18 -0800218
219 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800220 mirror::ObjectArray<mirror::Object>* GetImageRoots() const
Mathieu Chartier90443472015-07-16 20:32:27 -0700221 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700222
Alex Light53cb16b2014-06-12 11:26:29 -0700223 void RelocateImage(off_t delta);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800224 void RelocateImageMethods(off_t delta);
225 void RelocateImageObjects(off_t delta);
Alex Light53cb16b2014-06-12 11:26:29 -0700226
Igor Murashkin46774762014-10-22 11:37:02 -0700227 bool CompilePic() const {
228 return compile_pic_ != 0;
229 }
230
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800231 bool IsPic() const {
232 return is_pic_ != 0;
233 }
234
235 uint32_t GetBootImageBegin() const {
236 return boot_image_begin_;
237 }
238
239 uint32_t GetBootImageSize() const {
240 return boot_image_size_;
241 }
242
243 uint32_t GetBootOatBegin() const {
244 return boot_oat_begin_;
245 }
246
247 uint32_t GetBootOatSize() const {
248 return boot_oat_size_;
249 }
250
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800251 StorageMode GetStorageMode() const {
252 return storage_mode_;
253 }
254
255 uint64_t GetDataSize() const {
256 return data_size_;
257 }
258
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800259 bool IsAppImage() const {
260 // App images currently require a boot image, if the size is non zero then it is an app image
261 // header.
262 return boot_image_size_ != 0u;
263 }
264
Alex Light53cb16b2014-06-12 11:26:29 -0700265 private:
Ian Rogers13735952014-10-08 12:43:28 -0700266 static const uint8_t kImageMagic[4];
267 static const uint8_t kImageVersion[4];
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700268
Ian Rogers13735952014-10-08 12:43:28 -0700269 uint8_t magic_[4];
270 uint8_t version_[4];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700271
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800272 // Required base address for mapping the image.
Ian Rogers30fab402012-01-23 15:43:46 -0800273 uint32_t image_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700274
Mathieu Chartier31e89252013-08-28 11:29:12 -0700275 // Image size, not page aligned.
276 uint32_t image_size_;
277
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800278 // Checksum of the oat file we link to for load time sanity check.
Brian Carlstrome24fa612011-09-29 00:53:55 -0700279 uint32_t oat_checksum_;
280
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800281 // Start address for oat file. Will be before oat_data_begin_ for .so files.
282 uint32_t oat_file_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700283
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800284 // Required oat address expected by image Method::GetCode() pointers.
285 uint32_t oat_data_begin_;
Brian Carlstrom16192862011-09-12 17:50:06 -0700286
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800287 // End of oat data address range for this image file.
288 uint32_t oat_data_end_;
289
290 // End of oat file address range. will be after oat_data_end_ for
291 // .so files. Used for positioning a following alloc spaces.
292 uint32_t oat_file_end_;
293
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800294 // Boot image begin and end (app image headers only).
295 uint32_t boot_image_begin_;
296 uint32_t boot_image_size_;
297
298 // Boot oat begin and end (app image headers only).
299 uint32_t boot_oat_begin_;
300 uint32_t boot_oat_size_;
301
302 // TODO: We should probably insert a boot image checksum for app images.
303
Alex Light53cb16b2014-06-12 11:26:29 -0700304 // The total delta that this image has been patched.
305 int32_t patch_delta_;
306
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800307 // Absolute address of an Object[] of objects needed to reinitialize from an image.
Brian Carlstrom16192862011-09-12 17:50:06 -0700308 uint32_t image_roots_;
309
Mathieu Chartiere401d142015-04-22 13:56:20 -0700310 // Pointer size, this affects the size of the ArtMethods.
311 uint32_t pointer_size_;
312
Igor Murashkin46774762014-10-22 11:37:02 -0700313 // Boolean (0 or 1) to denote if the image was compiled with --compile-pic option
314 const uint32_t compile_pic_;
315
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800316 // Boolean (0 or 1) to denote if the image can be mapped at a random address, this only refers to
317 // the .art file. Currently, app oat files do not depend on their app image. There are no pointers
318 // from the app oat code to the app image.
319 const uint32_t is_pic_;
320
Mathieu Chartier67ad20e2015-12-09 15:41:09 -0800321 // Image section sizes/offsets correspond to the uncompressed form.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700322 ImageSection sections_[kSectionCount];
323
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800324 // Image methods, may be inside of the boot image for app images.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700325 uint64_t image_methods_[kImageMethodsCount];
326
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800327 // Storage method for the image, the image may be compressed.
328 StorageMode storage_mode_;
329
330 // Data size for the image data excluding the bitmap and the header. For compressed images, this
331 // is the compressed size in the file.
332 uint32_t data_size_;
333
Brian Carlstrom16192862011-09-12 17:50:06 -0700334 friend class ImageWriter;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700335};
336
Mathieu Chartiere401d142015-04-22 13:56:20 -0700337std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageMethod& policy);
338std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageRoot& policy);
339std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageSections& section);
340std::ostream& operator<<(std::ostream& os, const ImageSection& section);
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800341std::ostream& operator<<(std::ostream& os, const ImageHeader::StorageMode& mode);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700342
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700343} // namespace art
344
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700345#endif // ART_RUNTIME_IMAGE_H_