blob: a16f3c98fb5e457fa5478a88c8bf100877b7df06 [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:
Sebastien Hertzaa50d3a2015-08-25 15:25:41 +020081 ImageHeader()
Nicolas Geoffray83d4d722015-12-10 08:26:32 +000082 : image_begin_(0U), image_size_(0U), oat_checksum_(0U), oat_file_begin_(0U),
83 oat_data_begin_(0U), oat_data_end_(0U), oat_file_end_(0U), patch_delta_(0),
84 image_roots_(0U), pointer_size_(0U), compile_pic_(0) {}
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070085
Ian Rogers30fab402012-01-23 15:43:46 -080086 ImageHeader(uint32_t image_begin,
Mathieu Chartier763a31e2015-11-16 16:05:55 -080087 uint32_t image_size,
Mathieu Chartiere401d142015-04-22 13:56:20 -070088 ImageSection* sections,
Brian Carlstrome24fa612011-09-29 00:53:55 -070089 uint32_t image_roots,
90 uint32_t oat_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -080091 uint32_t oat_file_begin,
92 uint32_t oat_data_begin,
93 uint32_t oat_data_end,
Igor Murashkin46774762014-10-22 11:37:02 -070094 uint32_t oat_file_end,
Mathieu Chartiere401d142015-04-22 13:56:20 -070095 uint32_t pointer_size,
Nicolas Geoffray83d4d722015-12-10 08:26:32 +000096 bool compile_pic);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070097
Brian Carlstrom68708f52013-09-03 14:15:31 -070098 bool IsValid() const;
99 const char* GetMagic() const;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700100
Ian Rogers13735952014-10-08 12:43:28 -0700101 uint8_t* GetImageBegin() const {
102 return reinterpret_cast<uint8_t*>(image_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700103 }
104
Mathieu Chartier31e89252013-08-28 11:29:12 -0700105 size_t GetImageSize() const {
106 return static_cast<uint32_t>(image_size_);
107 }
108
Brian Carlstrome24fa612011-09-29 00:53:55 -0700109 uint32_t GetOatChecksum() const {
110 return oat_checksum_;
111 }
112
Brian Carlstroma85b8372012-10-18 17:00:32 -0700113 void SetOatChecksum(uint32_t oat_checksum) {
114 oat_checksum_ = oat_checksum;
115 }
116
Ian Rogers13735952014-10-08 12:43:28 -0700117 uint8_t* GetOatFileBegin() const {
118 return reinterpret_cast<uint8_t*>(oat_file_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700119 }
120
Ian Rogers13735952014-10-08 12:43:28 -0700121 uint8_t* GetOatDataBegin() const {
122 return reinterpret_cast<uint8_t*>(oat_data_begin_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800123 }
124
Ian Rogers13735952014-10-08 12:43:28 -0700125 uint8_t* GetOatDataEnd() const {
126 return reinterpret_cast<uint8_t*>(oat_data_end_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800127 }
128
Ian Rogers13735952014-10-08 12:43:28 -0700129 uint8_t* GetOatFileEnd() const {
130 return reinterpret_cast<uint8_t*>(oat_file_end_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700131 }
132
Mathieu Chartiere401d142015-04-22 13:56:20 -0700133 uint32_t GetPointerSize() const {
134 return pointer_size_;
135 }
136
Alex Light53cb16b2014-06-12 11:26:29 -0700137 off_t GetPatchDelta() const {
138 return patch_delta_;
139 }
140
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000141 static std::string GetOatLocationFromImageLocation(const std::string& image) {
142 std::string oat_filename = image;
143 if (oat_filename.length() <= 3) {
Ian Rogersb9beb2e2014-05-09 16:57:40 -0700144 oat_filename += ".oat";
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000145 } else {
146 oat_filename.replace(oat_filename.length() - 3, 3, "oat");
147 }
148 return oat_filename;
149 }
150
Mathieu Chartiere401d142015-04-22 13:56:20 -0700151 enum ImageMethod {
Ian Rogers19846512012-02-24 11:42:47 -0800152 kResolutionMethod,
Jeff Hao88474b42013-10-23 16:24:40 -0700153 kImtConflictMethod,
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700154 kImtUnimplementedMethod,
Ian Rogersff1ed472011-09-20 13:46:24 -0700155 kCalleeSaveMethod,
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700156 kRefsOnlySaveMethod,
157 kRefsAndArgsSaveMethod,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700158 kImageMethodsCount, // Number of elements in enum.
159 };
160
161 enum ImageRoot {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700162 kDexCaches,
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700163 kClassRoots,
Brian Carlstrom16192862011-09-12 17:50:06 -0700164 kImageRootsMax,
165 };
166
Mathieu Chartiere401d142015-04-22 13:56:20 -0700167 enum ImageSections {
168 kSectionObjects,
169 kSectionArtFields,
170 kSectionArtMethods,
Vladimir Marko05792b92015-08-03 11:56:49 +0100171 kSectionDexCacheArrays,
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700172 kSectionInternedStrings,
Mathieu Chartier208a5cb2015-12-02 15:44:07 -0800173 kSectionClassTable,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700174 kSectionImageBitmap,
175 kSectionCount, // Number of elements in enum.
176 };
177
178 ArtMethod* GetImageMethod(ImageMethod index) const;
179 void SetImageMethod(ImageMethod index, ArtMethod* method);
180
181 const ImageSection& GetImageSection(ImageSections index) const;
182 const ImageSection& GetMethodsSection() const {
183 return GetImageSection(kSectionArtMethods);
184 }
185
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186 mirror::Object* GetImageRoot(ImageRoot image_root) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700187 SHARED_REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800188 mirror::ObjectArray<mirror::Object>* GetImageRoots() const
Mathieu Chartier90443472015-07-16 20:32:27 -0700189 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700190
Alex Light53cb16b2014-06-12 11:26:29 -0700191 void RelocateImage(off_t delta);
192
Igor Murashkin46774762014-10-22 11:37:02 -0700193 bool CompilePic() const {
194 return compile_pic_ != 0;
195 }
196
Alex Light53cb16b2014-06-12 11:26:29 -0700197 private:
Ian Rogers13735952014-10-08 12:43:28 -0700198 static const uint8_t kImageMagic[4];
199 static const uint8_t kImageVersion[4];
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700200
Ian Rogers13735952014-10-08 12:43:28 -0700201 uint8_t magic_[4];
202 uint8_t version_[4];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700203
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800204 // Required base address for mapping the image.
Ian Rogers30fab402012-01-23 15:43:46 -0800205 uint32_t image_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700206
Mathieu Chartier31e89252013-08-28 11:29:12 -0700207 // Image size, not page aligned.
208 uint32_t image_size_;
209
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800210 // Checksum of the oat file we link to for load time sanity check.
Brian Carlstrome24fa612011-09-29 00:53:55 -0700211 uint32_t oat_checksum_;
212
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800213 // Start address for oat file. Will be before oat_data_begin_ for .so files.
214 uint32_t oat_file_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700215
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800216 // Required oat address expected by image Method::GetCode() pointers.
217 uint32_t oat_data_begin_;
Brian Carlstrom16192862011-09-12 17:50:06 -0700218
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800219 // End of oat data address range for this image file.
220 uint32_t oat_data_end_;
221
222 // End of oat file address range. will be after oat_data_end_ for
223 // .so files. Used for positioning a following alloc spaces.
224 uint32_t oat_file_end_;
225
Alex Light53cb16b2014-06-12 11:26:29 -0700226 // The total delta that this image has been patched.
227 int32_t patch_delta_;
228
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800229 // Absolute address of an Object[] of objects needed to reinitialize from an image.
Brian Carlstrom16192862011-09-12 17:50:06 -0700230 uint32_t image_roots_;
231
Mathieu Chartiere401d142015-04-22 13:56:20 -0700232 // Pointer size, this affects the size of the ArtMethods.
233 uint32_t pointer_size_;
234
Igor Murashkin46774762014-10-22 11:37:02 -0700235 // Boolean (0 or 1) to denote if the image was compiled with --compile-pic option
236 const uint32_t compile_pic_;
237
Mathieu Chartiere401d142015-04-22 13:56:20 -0700238 // Image sections
239 ImageSection sections_[kSectionCount];
240
241 // Image methods.
242 uint64_t image_methods_[kImageMethodsCount];
243
Brian Carlstrom16192862011-09-12 17:50:06 -0700244 friend class ImageWriter;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700245};
246
Mathieu Chartiere401d142015-04-22 13:56:20 -0700247std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageMethod& policy);
248std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageRoot& policy);
249std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageSections& section);
250std::ostream& operator<<(std::ostream& os, const ImageSection& section);
251
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700252} // namespace art
253
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700254#endif // ART_RUNTIME_IMAGE_H_