blob: 613414aa256ea8840c0aa50b323ad9e567164b95 [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"
Mathieu Chartier31e89252013-08-28 11:29:12 -070024#include "utils.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070025
26namespace art {
27
28// header of image files written by ImageWriter, read and validated by Space.
Ian Rogersdf1ce912012-11-27 17:07:11 -080029class PACKED(4) ImageHeader {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070030 public:
Igor Murashkin46774762014-10-22 11:37:02 -070031 ImageHeader() : compile_pic_(0) {}
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070032
Ian Rogers30fab402012-01-23 15:43:46 -080033 ImageHeader(uint32_t image_begin,
Mathieu Chartier31e89252013-08-28 11:29:12 -070034 uint32_t image_size_,
Mathieu Chartierc7853442015-03-27 14:35:38 -070035 uint32_t art_fields_offset,
36 uint32_t art_fields_size,
Mathieu Chartier31e89252013-08-28 11:29:12 -070037 uint32_t image_bitmap_offset,
38 uint32_t image_bitmap_size,
Brian Carlstrome24fa612011-09-29 00:53:55 -070039 uint32_t image_roots,
40 uint32_t oat_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -080041 uint32_t oat_file_begin,
42 uint32_t oat_data_begin,
43 uint32_t oat_data_end,
Igor Murashkin46774762014-10-22 11:37:02 -070044 uint32_t oat_file_end,
45 bool compile_pic_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070046
Brian Carlstrom68708f52013-09-03 14:15:31 -070047 bool IsValid() const;
48 const char* GetMagic() const;
Brian Carlstrom78128a62011-09-15 17:21:19 -070049
Ian Rogers13735952014-10-08 12:43:28 -070050 uint8_t* GetImageBegin() const {
51 return reinterpret_cast<uint8_t*>(image_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070052 }
53
Mathieu Chartier31e89252013-08-28 11:29:12 -070054 size_t GetImageSize() const {
55 return static_cast<uint32_t>(image_size_);
56 }
57
Mathieu Chartierc7853442015-03-27 14:35:38 -070058 size_t GetArtFieldsOffset() const {
59 return art_fields_offset_;
60 }
61
62 size_t GetArtFieldsSize() const {
63 return art_fields_size_;
64 }
65
Mathieu Chartier31e89252013-08-28 11:29:12 -070066 size_t GetImageBitmapOffset() const {
67 return image_bitmap_offset_;
68 }
69
70 size_t GetImageBitmapSize() const {
71 return image_bitmap_size_;
72 }
73
Brian Carlstrome24fa612011-09-29 00:53:55 -070074 uint32_t GetOatChecksum() const {
75 return oat_checksum_;
76 }
77
Brian Carlstroma85b8372012-10-18 17:00:32 -070078 void SetOatChecksum(uint32_t oat_checksum) {
79 oat_checksum_ = oat_checksum;
80 }
81
Ian Rogers13735952014-10-08 12:43:28 -070082 uint8_t* GetOatFileBegin() const {
83 return reinterpret_cast<uint8_t*>(oat_file_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070084 }
85
Ian Rogers13735952014-10-08 12:43:28 -070086 uint8_t* GetOatDataBegin() const {
87 return reinterpret_cast<uint8_t*>(oat_data_begin_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080088 }
89
Ian Rogers13735952014-10-08 12:43:28 -070090 uint8_t* GetOatDataEnd() const {
91 return reinterpret_cast<uint8_t*>(oat_data_end_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080092 }
93
Ian Rogers13735952014-10-08 12:43:28 -070094 uint8_t* GetOatFileEnd() const {
95 return reinterpret_cast<uint8_t*>(oat_file_end_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070096 }
97
Alex Light53cb16b2014-06-12 11:26:29 -070098 off_t GetPatchDelta() const {
99 return patch_delta_;
100 }
101
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000102 static std::string GetOatLocationFromImageLocation(const std::string& image) {
103 std::string oat_filename = image;
104 if (oat_filename.length() <= 3) {
Ian Rogersb9beb2e2014-05-09 16:57:40 -0700105 oat_filename += ".oat";
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000106 } else {
107 oat_filename.replace(oat_filename.length() - 3, 3, "oat");
108 }
109 return oat_filename;
110 }
111
Brian Carlstrom16192862011-09-12 17:50:06 -0700112 enum ImageRoot {
Ian Rogers19846512012-02-24 11:42:47 -0800113 kResolutionMethod,
Jeff Hao88474b42013-10-23 16:24:40 -0700114 kImtConflictMethod,
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700115 kImtUnimplementedMethod,
Jeff Hao88474b42013-10-23 16:24:40 -0700116 kDefaultImt,
Ian Rogersff1ed472011-09-20 13:46:24 -0700117 kCalleeSaveMethod,
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700118 kRefsOnlySaveMethod,
119 kRefsAndArgsSaveMethod,
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700120 kDexCaches,
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700121 kClassRoots,
Brian Carlstrom16192862011-09-12 17:50:06 -0700122 kImageRootsMax,
123 };
124
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800125 mirror::Object* GetImageRoot(ImageRoot image_root) const
126 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800127 mirror::ObjectArray<mirror::Object>* GetImageRoots() const
128 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700129
Alex Light53cb16b2014-06-12 11:26:29 -0700130 void RelocateImage(off_t delta);
131
Igor Murashkin46774762014-10-22 11:37:02 -0700132 bool CompilePic() const {
133 return compile_pic_ != 0;
134 }
135
Alex Light53cb16b2014-06-12 11:26:29 -0700136 private:
Ian Rogers13735952014-10-08 12:43:28 -0700137 static const uint8_t kImageMagic[4];
138 static const uint8_t kImageVersion[4];
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700139
Ian Rogers13735952014-10-08 12:43:28 -0700140 uint8_t magic_[4];
141 uint8_t version_[4];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700142
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800143 // Required base address for mapping the image.
Ian Rogers30fab402012-01-23 15:43:46 -0800144 uint32_t image_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700145
Mathieu Chartier31e89252013-08-28 11:29:12 -0700146 // Image size, not page aligned.
147 uint32_t image_size_;
148
Mathieu Chartierc7853442015-03-27 14:35:38 -0700149 // ArtField array offset.
150 uint32_t art_fields_offset_;
151
152 // ArtField size in bytes.
153 uint32_t art_fields_size_;
154
Mathieu Chartier31e89252013-08-28 11:29:12 -0700155 // Image bitmap offset in the file.
156 uint32_t image_bitmap_offset_;
157
158 // Size of the image bitmap.
159 uint32_t image_bitmap_size_;
160
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800161 // Checksum of the oat file we link to for load time sanity check.
Brian Carlstrome24fa612011-09-29 00:53:55 -0700162 uint32_t oat_checksum_;
163
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800164 // Start address for oat file. Will be before oat_data_begin_ for .so files.
165 uint32_t oat_file_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700166
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800167 // Required oat address expected by image Method::GetCode() pointers.
168 uint32_t oat_data_begin_;
Brian Carlstrom16192862011-09-12 17:50:06 -0700169
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800170 // End of oat data address range for this image file.
171 uint32_t oat_data_end_;
172
173 // End of oat file address range. will be after oat_data_end_ for
174 // .so files. Used for positioning a following alloc spaces.
175 uint32_t oat_file_end_;
176
Alex Light53cb16b2014-06-12 11:26:29 -0700177 // The total delta that this image has been patched.
178 int32_t patch_delta_;
179
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800180 // Absolute address of an Object[] of objects needed to reinitialize from an image.
Brian Carlstrom16192862011-09-12 17:50:06 -0700181 uint32_t image_roots_;
182
Igor Murashkin46774762014-10-22 11:37:02 -0700183 // Boolean (0 or 1) to denote if the image was compiled with --compile-pic option
184 const uint32_t compile_pic_;
185
Brian Carlstrom16192862011-09-12 17:50:06 -0700186 friend class ImageWriter;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700187};
188
189} // namespace art
190
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700191#endif // ART_RUNTIME_IMAGE_H_