blob: c6be7ef3f78358db7c0ed731258a632da36a54ef [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 Chartiere401d142015-04-22 13:56:20 -070027class PACKED(4) ImageSection {
28 public:
29 ImageSection() : offset_(0), size_(0) { }
30 ImageSection(uint32_t offset, uint32_t size) : offset_(offset), size_(size) { }
31 ImageSection(const ImageSection& section) = default;
32 ImageSection& operator=(const ImageSection& section) = default;
33
34 uint32_t Offset() const {
35 return offset_;
36 }
37
38 uint32_t Size() const {
39 return size_;
40 }
41
42 uint32_t End() const {
43 return Offset() + Size();
44 }
45
46 bool Contains(uint64_t offset) const {
47 return offset - offset_ < size_;
48 }
49
50 private:
51 uint32_t offset_;
52 uint32_t size_;
53};
54
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070055// header of image files written by ImageWriter, read and validated by Space.
Ian Rogersdf1ce912012-11-27 17:07:11 -080056class PACKED(4) ImageHeader {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070057 public:
Igor Murashkin46774762014-10-22 11:37:02 -070058 ImageHeader() : compile_pic_(0) {}
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070059
Ian Rogers30fab402012-01-23 15:43:46 -080060 ImageHeader(uint32_t image_begin,
Mathieu Chartier31e89252013-08-28 11:29:12 -070061 uint32_t image_size_,
Mathieu Chartiere401d142015-04-22 13:56:20 -070062 ImageSection* sections,
Brian Carlstrome24fa612011-09-29 00:53:55 -070063 uint32_t image_roots,
64 uint32_t oat_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -080065 uint32_t oat_file_begin,
66 uint32_t oat_data_begin,
67 uint32_t oat_data_end,
Igor Murashkin46774762014-10-22 11:37:02 -070068 uint32_t oat_file_end,
Mathieu Chartiere401d142015-04-22 13:56:20 -070069 uint32_t pointer_size,
Igor Murashkin46774762014-10-22 11:37:02 -070070 bool compile_pic_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070071
Brian Carlstrom68708f52013-09-03 14:15:31 -070072 bool IsValid() const;
73 const char* GetMagic() const;
Brian Carlstrom78128a62011-09-15 17:21:19 -070074
Ian Rogers13735952014-10-08 12:43:28 -070075 uint8_t* GetImageBegin() const {
76 return reinterpret_cast<uint8_t*>(image_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070077 }
78
Mathieu Chartier31e89252013-08-28 11:29:12 -070079 size_t GetImageSize() const {
80 return static_cast<uint32_t>(image_size_);
81 }
82
Brian Carlstrome24fa612011-09-29 00:53:55 -070083 uint32_t GetOatChecksum() const {
84 return oat_checksum_;
85 }
86
Brian Carlstroma85b8372012-10-18 17:00:32 -070087 void SetOatChecksum(uint32_t oat_checksum) {
88 oat_checksum_ = oat_checksum;
89 }
90
Ian Rogers13735952014-10-08 12:43:28 -070091 uint8_t* GetOatFileBegin() const {
92 return reinterpret_cast<uint8_t*>(oat_file_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070093 }
94
Ian Rogers13735952014-10-08 12:43:28 -070095 uint8_t* GetOatDataBegin() const {
96 return reinterpret_cast<uint8_t*>(oat_data_begin_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080097 }
98
Ian Rogers13735952014-10-08 12:43:28 -070099 uint8_t* GetOatDataEnd() const {
100 return reinterpret_cast<uint8_t*>(oat_data_end_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800101 }
102
Ian Rogers13735952014-10-08 12:43:28 -0700103 uint8_t* GetOatFileEnd() const {
104 return reinterpret_cast<uint8_t*>(oat_file_end_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700105 }
106
Mathieu Chartiere401d142015-04-22 13:56:20 -0700107 uint32_t GetPointerSize() const {
108 return pointer_size_;
109 }
110
Alex Light53cb16b2014-06-12 11:26:29 -0700111 off_t GetPatchDelta() const {
112 return patch_delta_;
113 }
114
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000115 static std::string GetOatLocationFromImageLocation(const std::string& image) {
116 std::string oat_filename = image;
117 if (oat_filename.length() <= 3) {
Ian Rogersb9beb2e2014-05-09 16:57:40 -0700118 oat_filename += ".oat";
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000119 } else {
120 oat_filename.replace(oat_filename.length() - 3, 3, "oat");
121 }
122 return oat_filename;
123 }
124
Mathieu Chartiere401d142015-04-22 13:56:20 -0700125 enum ImageMethod {
Ian Rogers19846512012-02-24 11:42:47 -0800126 kResolutionMethod,
Jeff Hao88474b42013-10-23 16:24:40 -0700127 kImtConflictMethod,
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700128 kImtUnimplementedMethod,
Ian Rogersff1ed472011-09-20 13:46:24 -0700129 kCalleeSaveMethod,
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700130 kRefsOnlySaveMethod,
131 kRefsAndArgsSaveMethod,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700132 kImageMethodsCount, // Number of elements in enum.
133 };
134
135 enum ImageRoot {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700136 kDexCaches,
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700137 kClassRoots,
Brian Carlstrom16192862011-09-12 17:50:06 -0700138 kImageRootsMax,
139 };
140
Mathieu Chartiere401d142015-04-22 13:56:20 -0700141 enum ImageSections {
142 kSectionObjects,
143 kSectionArtFields,
144 kSectionArtMethods,
145 kSectionImageBitmap,
146 kSectionCount, // Number of elements in enum.
147 };
148
149 ArtMethod* GetImageMethod(ImageMethod index) const;
150 void SetImageMethod(ImageMethod index, ArtMethod* method);
151
152 const ImageSection& GetImageSection(ImageSections index) const;
153 const ImageSection& GetMethodsSection() const {
154 return GetImageSection(kSectionArtMethods);
155 }
156
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800157 mirror::Object* GetImageRoot(ImageRoot image_root) const
158 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800159 mirror::ObjectArray<mirror::Object>* GetImageRoots() const
160 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700161
Alex Light53cb16b2014-06-12 11:26:29 -0700162 void RelocateImage(off_t delta);
163
Igor Murashkin46774762014-10-22 11:37:02 -0700164 bool CompilePic() const {
165 return compile_pic_ != 0;
166 }
167
Alex Light53cb16b2014-06-12 11:26:29 -0700168 private:
Ian Rogers13735952014-10-08 12:43:28 -0700169 static const uint8_t kImageMagic[4];
170 static const uint8_t kImageVersion[4];
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700171
Ian Rogers13735952014-10-08 12:43:28 -0700172 uint8_t magic_[4];
173 uint8_t version_[4];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700174
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800175 // Required base address for mapping the image.
Ian Rogers30fab402012-01-23 15:43:46 -0800176 uint32_t image_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700177
Mathieu Chartier31e89252013-08-28 11:29:12 -0700178 // Image size, not page aligned.
179 uint32_t image_size_;
180
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800181 // Checksum of the oat file we link to for load time sanity check.
Brian Carlstrome24fa612011-09-29 00:53:55 -0700182 uint32_t oat_checksum_;
183
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800184 // Start address for oat file. Will be before oat_data_begin_ for .so files.
185 uint32_t oat_file_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700186
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800187 // Required oat address expected by image Method::GetCode() pointers.
188 uint32_t oat_data_begin_;
Brian Carlstrom16192862011-09-12 17:50:06 -0700189
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800190 // End of oat data address range for this image file.
191 uint32_t oat_data_end_;
192
193 // End of oat file address range. will be after oat_data_end_ for
194 // .so files. Used for positioning a following alloc spaces.
195 uint32_t oat_file_end_;
196
Alex Light53cb16b2014-06-12 11:26:29 -0700197 // The total delta that this image has been patched.
198 int32_t patch_delta_;
199
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800200 // Absolute address of an Object[] of objects needed to reinitialize from an image.
Brian Carlstrom16192862011-09-12 17:50:06 -0700201 uint32_t image_roots_;
202
Mathieu Chartiere401d142015-04-22 13:56:20 -0700203 // Pointer size, this affects the size of the ArtMethods.
204 uint32_t pointer_size_;
205
Igor Murashkin46774762014-10-22 11:37:02 -0700206 // Boolean (0 or 1) to denote if the image was compiled with --compile-pic option
207 const uint32_t compile_pic_;
208
Mathieu Chartiere401d142015-04-22 13:56:20 -0700209 // Image sections
210 ImageSection sections_[kSectionCount];
211
212 // Image methods.
213 uint64_t image_methods_[kImageMethodsCount];
214
Brian Carlstrom16192862011-09-12 17:50:06 -0700215 friend class ImageWriter;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700216};
217
Mathieu Chartiere401d142015-04-22 13:56:20 -0700218std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageMethod& policy);
219std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageRoot& policy);
220std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageSections& section);
221std::ostream& operator<<(std::ostream& os, const ImageSection& section);
222
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700223} // namespace art
224
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700225#endif // ART_RUNTIME_IMAGE_H_