blob: 399b1439d28c0819fae18fe1fcbfd908c2d187bd [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.
Brian Carlstrome24fa612011-09-29 00:53:55 -070028class PACKED 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,
Ian Rogers30fab402012-01-23 15:43:46 -080035 uint32_t oat_begin,
36 uint32_t oat_end)
37 : image_begin_(image_begin),
Brian Carlstrome24fa612011-09-29 00:53:55 -070038 oat_checksum_(oat_checksum),
Ian Rogers30fab402012-01-23 15:43:46 -080039 oat_begin_(oat_begin),
40 oat_end_(oat_end),
Brian Carlstrome24fa612011-09-29 00:53:55 -070041 image_roots_(image_roots) {
Ian Rogers30fab402012-01-23 15:43:46 -080042 CHECK_EQ(image_begin, RoundUp(image_begin, kPageSize));
43 CHECK_EQ(oat_begin, RoundUp(oat_begin, kPageSize));
44 CHECK_LT(image_begin, image_roots);
45 CHECK_LT(image_roots, oat_begin);
46 CHECK_LT(oat_begin, oat_end);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070047 memcpy(magic_, kImageMagic, sizeof(kImageMagic));
48 memcpy(version_, kImageVersion, sizeof(kImageVersion));
49 }
50
Brian Carlstrom78128a62011-09-15 17:21:19 -070051 bool IsValid() const {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070052 if (memcmp(magic_, kImageMagic, sizeof(kImageMagic) != 0)) {
53 return false;
54 }
55 if (memcmp(version_, kImageVersion, sizeof(kImageVersion) != 0)) {
56 return false;
57 }
58 return true;
59 }
60
Brian Carlstrom78128a62011-09-15 17:21:19 -070061 const char* GetMagic() const {
62 CHECK(IsValid());
63 return reinterpret_cast<const char*>(magic_);
64 }
65
Ian Rogers30fab402012-01-23 15:43:46 -080066 byte* GetImageBegin() const {
67 return reinterpret_cast<byte*>(image_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070068 }
69
70 uint32_t GetOatChecksum() const {
71 return oat_checksum_;
72 }
73
Ian Rogers30fab402012-01-23 15:43:46 -080074 byte* GetOatBegin() const {
75 return reinterpret_cast<byte*>(oat_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070076 }
77
Ian Rogers30fab402012-01-23 15:43:46 -080078 byte* GetOatEnd() const {
79 return reinterpret_cast<byte*>(oat_end_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070080 }
81
Brian Carlstrom16192862011-09-12 17:50:06 -070082 enum ImageRoot {
83 kJniStubArray,
Brian Carlstrome24fa612011-09-29 00:53:55 -070084 kAbstractMethodErrorStubArray,
Ian Rogersad25ac52011-10-04 19:13:33 -070085 kStaticResolutionStubArray,
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070086 kUnknownMethodResolutionStubArray,
Ian Rogers19846512012-02-24 11:42:47 -080087 kResolutionMethod,
Ian Rogersff1ed472011-09-20 13:46:24 -070088 kCalleeSaveMethod,
Ian Rogers4f0d07c2011-10-06 23:38:47 -070089 kRefsOnlySaveMethod,
90 kRefsAndArgsSaveMethod,
Brian Carlstrome24fa612011-09-29 00:53:55 -070091 kOatLocation,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070092 kDexCaches,
Brian Carlstrom34f426c2011-10-04 12:58:02 -070093 kClassRoots,
Brian Carlstrom16192862011-09-12 17:50:06 -070094 kImageRootsMax,
95 };
96
Ian Rogers00f7d0e2012-07-19 15:28:27 -070097 Object* GetImageRoot(ImageRoot image_root) const
Ian Rogersb726dcb2012-09-05 08:57:23 -070098 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromaded5f72011-10-07 17:15:04 -070099 return GetImageRoots()->Get(image_root);
Brian Carlstrom16192862011-09-12 17:50:06 -0700100 }
101
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700102 private:
Brian Carlstromaded5f72011-10-07 17:15:04 -0700103 ObjectArray<Object>* GetImageRoots() const {
104 return reinterpret_cast<ObjectArray<Object>*>(image_roots_);
105 }
106
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700107 static const byte kImageMagic[4];
108 static const byte kImageVersion[4];
109
110 byte magic_[4];
111 byte version_[4];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700112
113 // required base address for mapping the image.
Ian Rogers30fab402012-01-23 15:43:46 -0800114 uint32_t image_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700115
116 // checksum of the oat file we link to for load time sanity check
117 uint32_t oat_checksum_;
118
119 // required oat address expected by image Method::GetCode() pointers.
Ian Rogers30fab402012-01-23 15:43:46 -0800120 uint32_t oat_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700121
122 // end of oat address range for this image file, used for positioning a following image
Ian Rogers30fab402012-01-23 15:43:46 -0800123 uint32_t oat_end_;
Brian Carlstrom16192862011-09-12 17:50:06 -0700124
125 // absolute address of an Object[] of objects needed to reinitialize from an image
126 uint32_t image_roots_;
127
128 friend class ImageWriter;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800129 friend class ImageDumper; // For GetImageRoots()
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700130};
131
132} // namespace art
133
134#endif // ART_SRC_IMAGE_H_