blob: ce2bc587e39046dbce84f65e08012985a75b433a [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:
31 ImageHeader() {}
32
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_,
35 uint32_t image_bitmap_offset,
36 uint32_t image_bitmap_size,
Brian Carlstrome24fa612011-09-29 00:53:55 -070037 uint32_t image_roots,
38 uint32_t oat_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -080039 uint32_t oat_file_begin,
40 uint32_t oat_data_begin,
41 uint32_t oat_data_end,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042 uint32_t oat_file_end);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070043
Brian Carlstrom68708f52013-09-03 14:15:31 -070044 bool IsValid() const;
45 const char* GetMagic() const;
Brian Carlstrom78128a62011-09-15 17:21:19 -070046
Ian Rogers30fab402012-01-23 15:43:46 -080047 byte* GetImageBegin() const {
48 return reinterpret_cast<byte*>(image_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070049 }
50
Mathieu Chartier31e89252013-08-28 11:29:12 -070051 size_t GetImageSize() const {
52 return static_cast<uint32_t>(image_size_);
53 }
54
55 size_t GetImageBitmapOffset() const {
56 return image_bitmap_offset_;
57 }
58
59 size_t GetImageBitmapSize() const {
60 return image_bitmap_size_;
61 }
62
Brian Carlstrome24fa612011-09-29 00:53:55 -070063 uint32_t GetOatChecksum() const {
64 return oat_checksum_;
65 }
66
Brian Carlstroma85b8372012-10-18 17:00:32 -070067 void SetOatChecksum(uint32_t oat_checksum) {
68 oat_checksum_ = oat_checksum;
69 }
70
Brian Carlstrom700c8d32012-11-05 10:42:02 -080071 byte* GetOatFileBegin() const {
72 return reinterpret_cast<byte*>(oat_file_begin_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070073 }
74
Brian Carlstrom700c8d32012-11-05 10:42:02 -080075 byte* GetOatDataBegin() const {
76 return reinterpret_cast<byte*>(oat_data_begin_);
77 }
78
79 byte* GetOatDataEnd() const {
80 return reinterpret_cast<byte*>(oat_data_end_);
81 }
82
83 byte* GetOatFileEnd() const {
84 return reinterpret_cast<byte*>(oat_file_end_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070085 }
86
Mathieu Chartier31e89252013-08-28 11:29:12 -070087 size_t GetBitmapOffset() const {
88 return RoundUp(image_size_, kPageSize);
89 }
90
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +000091 static std::string GetOatLocationFromImageLocation(const std::string& image) {
92 std::string oat_filename = image;
93 if (oat_filename.length() <= 3) {
94 return oat_filename + ".oat";
95 } else {
96 oat_filename.replace(oat_filename.length() - 3, 3, "oat");
97 }
98 return oat_filename;
99 }
100
Brian Carlstrom16192862011-09-12 17:50:06 -0700101 enum ImageRoot {
Ian Rogers19846512012-02-24 11:42:47 -0800102 kResolutionMethod,
Jeff Hao88474b42013-10-23 16:24:40 -0700103 kImtConflictMethod,
104 kDefaultImt,
Ian Rogersff1ed472011-09-20 13:46:24 -0700105 kCalleeSaveMethod,
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700106 kRefsOnlySaveMethod,
107 kRefsAndArgsSaveMethod,
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700108 kDexCaches,
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700109 kClassRoots,
Brian Carlstrom16192862011-09-12 17:50:06 -0700110 kImageRootsMax,
111 };
112
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800113 mirror::Object* GetImageRoot(ImageRoot image_root) const
114 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom16192862011-09-12 17:50:06 -0700115
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700116 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800117 mirror::ObjectArray<mirror::Object>* GetImageRoots() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700118
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700119 static const byte kImageMagic[4];
120 static const byte kImageVersion[4];
121
122 byte magic_[4];
123 byte version_[4];
Brian Carlstroma663ea52011-08-19 23:33:41 -0700124
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800125 // Required base address for mapping the image.
Ian Rogers30fab402012-01-23 15:43:46 -0800126 uint32_t image_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700127
Mathieu Chartier31e89252013-08-28 11:29:12 -0700128 // Image size, not page aligned.
129 uint32_t image_size_;
130
131 // Image bitmap offset in the file.
132 uint32_t image_bitmap_offset_;
133
134 // Size of the image bitmap.
135 uint32_t image_bitmap_size_;
136
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800137 // Checksum of the oat file we link to for load time sanity check.
Brian Carlstrome24fa612011-09-29 00:53:55 -0700138 uint32_t oat_checksum_;
139
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800140 // Start address for oat file. Will be before oat_data_begin_ for .so files.
141 uint32_t oat_file_begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700142
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800143 // Required oat address expected by image Method::GetCode() pointers.
144 uint32_t oat_data_begin_;
Brian Carlstrom16192862011-09-12 17:50:06 -0700145
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800146 // End of oat data address range for this image file.
147 uint32_t oat_data_end_;
148
149 // End of oat file address range. will be after oat_data_end_ for
150 // .so files. Used for positioning a following alloc spaces.
151 uint32_t oat_file_end_;
152
153 // Absolute address of an Object[] of objects needed to reinitialize from an image.
Brian Carlstrom16192862011-09-12 17:50:06 -0700154 uint32_t image_roots_;
155
156 friend class ImageWriter;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800157 friend class ImageDumper; // For GetImageRoots()
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700158};
159
160} // namespace art
161
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700162#endif // ART_RUNTIME_IMAGE_H_