blob: f38f04be8bf1912d597f0d4206ecc599e8bbfdfb [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
17#ifndef ART_SRC_IMAGE_H_
18#define ART_SRC_IMAGE_H_
19
20#include <string.h>
21
22#include "globals.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070023#include "object.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070024
25namespace art {
26
27// header of image files written by ImageWriter, read and validated by Space.
Ian Rogersdf1ce912012-11-27 17:07:11 -080028class PACKED(4) ImageHeader {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070029 public:
30 ImageHeader() {}
31
Ian Rogers30fab402012-01-23 15:43:46 -080032 ImageHeader(uint32_t image_begin,
Brian Carlstrome24fa612011-09-29 00:53:55 -070033 uint32_t image_roots,
34 uint32_t oat_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -080035 uint32_t oat_file_begin,
36 uint32_t oat_data_begin,
37 uint32_t oat_data_end,
38 uint32_t oat_file_end)
Ian Rogers30fab402012-01-23 15:43:46 -080039 : image_begin_(image_begin),
Brian Carlstrome24fa612011-09-29 00:53:55 -070040 oat_checksum_(oat_checksum),
Brian Carlstrom700c8d32012-11-05 10:42:02 -080041 oat_file_begin_(oat_file_begin),
42 oat_data_begin_(oat_data_begin),
43 oat_data_end_(oat_data_end),
44 oat_file_end_(oat_file_end),
Brian Carlstrome24fa612011-09-29 00:53:55 -070045 image_roots_(image_roots) {
Ian Rogers30fab402012-01-23 15:43:46 -080046 CHECK_EQ(image_begin, RoundUp(image_begin, kPageSize));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080047 CHECK_EQ(oat_file_begin, RoundUp(oat_file_begin, kPageSize));
48 CHECK_EQ(oat_data_begin, RoundUp(oat_data_begin, kPageSize));
Ian Rogers30fab402012-01-23 15:43:46 -080049 CHECK_LT(image_begin, image_roots);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080050 CHECK_LT(image_roots, oat_file_begin);
51 CHECK_LE(oat_file_begin, oat_data_begin);
52 CHECK_LT(oat_data_begin, oat_data_end);
53 CHECK_LE(oat_data_end, oat_file_end);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070054 memcpy(magic_, kImageMagic, sizeof(kImageMagic));
55 memcpy(version_, kImageVersion, sizeof(kImageVersion));
56 }
57
Brian Carlstrom78128a62011-09-15 17:21:19 -070058 bool IsValid() const {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070059 if (memcmp(magic_, kImageMagic, sizeof(kImageMagic) != 0)) {
60 return false;
61 }
62 if (memcmp(version_, kImageVersion, sizeof(kImageVersion) != 0)) {
63 return false;
64 }
65 return true;
66 }
67
Brian Carlstrom78128a62011-09-15 17:21:19 -070068 const char* GetMagic() const {
69 CHECK(IsValid());
70 return reinterpret_cast<const char*>(magic_);
71 }
72
Ian Rogers30fab402012-01-23 15:43:46 -080073 byte* GetImageBegin() const {
74 return reinterpret_cast<byte*>(image_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070075 }
76
77 uint32_t GetOatChecksum() const {
78 return oat_checksum_;
79 }
80
Brian Carlstroma85b8372012-10-18 17:00:32 -070081 void SetOatChecksum(uint32_t oat_checksum) {
82 oat_checksum_ = oat_checksum;
83 }
84
Brian Carlstrom700c8d32012-11-05 10:42:02 -080085 byte* GetOatFileBegin() const {
86 return reinterpret_cast<byte*>(oat_file_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070087 }
88
Brian Carlstrom700c8d32012-11-05 10:42:02 -080089 byte* GetOatDataBegin() const {
90 return reinterpret_cast<byte*>(oat_data_begin_);
91 }
92
93 byte* GetOatDataEnd() const {
94 return reinterpret_cast<byte*>(oat_data_end_);
95 }
96
97 byte* GetOatFileEnd() const {
98 return reinterpret_cast<byte*>(oat_file_end_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070099 }
100
Brian Carlstrom16192862011-09-12 17:50:06 -0700101 enum ImageRoot {
102 kJniStubArray,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700103 kAbstractMethodErrorStubArray,
Ian Rogersad25ac52011-10-04 19:13:33 -0700104 kStaticResolutionStubArray,
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700105 kUnknownMethodResolutionStubArray,
Ian Rogers19846512012-02-24 11:42:47 -0800106 kResolutionMethod,
Ian Rogersff1ed472011-09-20 13:46:24 -0700107 kCalleeSaveMethod,
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700108 kRefsOnlySaveMethod,
109 kRefsAndArgsSaveMethod,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700110 kOatLocation,
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700111 kDexCaches,
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700112 kClassRoots,
Brian Carlstrom16192862011-09-12 17:50:06 -0700113 kImageRootsMax,
114 };
115
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700116 Object* GetImageRoot(ImageRoot image_root) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700117 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700118 return GetImageRoots()->Get(image_root);
Brian Carlstrom16192862011-09-12 17:50:06 -0700119 }
120
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700121 private:
Brian Carlstromaded5f72011-10-07 17:15:04 -0700122 ObjectArray<Object>* GetImageRoots() const {
123 return reinterpret_cast<ObjectArray<Object>*>(image_roots_);
124 }
125
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700126 static const byte kImageMagic[4];
127 static const byte kImageVersion[4];
128
129 byte magic_[4];
130 byte version_[4];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700131
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800132 // Required base address for mapping the image.
Ian Rogers30fab402012-01-23 15:43:46 -0800133 uint32_t image_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700134
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800135 // Checksum of the oat file we link to for load time sanity check.
Brian Carlstrome24fa612011-09-29 00:53:55 -0700136 uint32_t oat_checksum_;
137
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800138 // Start address for oat file. Will be before oat_data_begin_ for .so files.
139 uint32_t oat_file_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700140
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800141 // Required oat address expected by image Method::GetCode() pointers.
142 uint32_t oat_data_begin_;
Brian Carlstrom16192862011-09-12 17:50:06 -0700143
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800144 // End of oat data address range for this image file.
145 uint32_t oat_data_end_;
146
147 // End of oat file address range. will be after oat_data_end_ for
148 // .so files. Used for positioning a following alloc spaces.
149 uint32_t oat_file_end_;
150
151 // Absolute address of an Object[] of objects needed to reinitialize from an image.
Brian Carlstrom16192862011-09-12 17:50:06 -0700152 uint32_t image_roots_;
153
154 friend class ImageWriter;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800155 friend class ImageDumper; // For GetImageRoots()
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700156};
157
158} // namespace art
159
160#endif // ART_SRC_IMAGE_H_