Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_IMAGE_H_ |
| 18 | #define ART_RUNTIME_IMAGE_H_ |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 19 | |
| 20 | #include <string.h> |
| 21 | |
| 22 | #include "globals.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 23 | #include "mirror/object.h" |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | // header of image files written by ImageWriter, read and validated by Space. |
Ian Rogers | df1ce91 | 2012-11-27 17:07:11 -0800 | [diff] [blame] | 28 | class PACKED(4) ImageHeader { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 29 | public: |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 30 | ImageHeader() : compile_pic_(0) {} |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 31 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 32 | ImageHeader(uint32_t image_begin, |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 33 | uint32_t image_size_, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 34 | uint32_t art_fields_offset, |
| 35 | uint32_t art_fields_size, |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 36 | uint32_t image_bitmap_offset, |
| 37 | uint32_t image_bitmap_size, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 38 | uint32_t image_roots, |
| 39 | uint32_t oat_checksum, |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 40 | uint32_t oat_file_begin, |
| 41 | uint32_t oat_data_begin, |
| 42 | uint32_t oat_data_end, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 43 | uint32_t oat_file_end, |
| 44 | bool compile_pic_); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 45 | |
Brian Carlstrom | 68708f5 | 2013-09-03 14:15:31 -0700 | [diff] [blame] | 46 | bool IsValid() const; |
| 47 | const char* GetMagic() const; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 48 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 49 | uint8_t* GetImageBegin() const { |
| 50 | return reinterpret_cast<uint8_t*>(image_begin_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 53 | size_t GetImageSize() const { |
| 54 | return static_cast<uint32_t>(image_size_); |
| 55 | } |
| 56 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 57 | size_t GetArtFieldsOffset() const { |
| 58 | return art_fields_offset_; |
| 59 | } |
| 60 | |
| 61 | size_t GetArtFieldsSize() const { |
| 62 | return art_fields_size_; |
| 63 | } |
| 64 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 65 | size_t GetImageBitmapOffset() const { |
| 66 | return image_bitmap_offset_; |
| 67 | } |
| 68 | |
| 69 | size_t GetImageBitmapSize() const { |
| 70 | return image_bitmap_size_; |
| 71 | } |
| 72 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 73 | uint32_t GetOatChecksum() const { |
| 74 | return oat_checksum_; |
| 75 | } |
| 76 | |
Brian Carlstrom | a85b837 | 2012-10-18 17:00:32 -0700 | [diff] [blame] | 77 | void SetOatChecksum(uint32_t oat_checksum) { |
| 78 | oat_checksum_ = oat_checksum; |
| 79 | } |
| 80 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 81 | uint8_t* GetOatFileBegin() const { |
| 82 | return reinterpret_cast<uint8_t*>(oat_file_begin_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 85 | uint8_t* GetOatDataBegin() const { |
| 86 | return reinterpret_cast<uint8_t*>(oat_data_begin_); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 89 | uint8_t* GetOatDataEnd() const { |
| 90 | return reinterpret_cast<uint8_t*>(oat_data_end_); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 93 | uint8_t* GetOatFileEnd() const { |
| 94 | return reinterpret_cast<uint8_t*>(oat_file_end_); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 97 | off_t GetPatchDelta() const { |
| 98 | return patch_delta_; |
| 99 | } |
| 100 | |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 101 | static std::string GetOatLocationFromImageLocation(const std::string& image) { |
| 102 | std::string oat_filename = image; |
| 103 | if (oat_filename.length() <= 3) { |
Ian Rogers | b9beb2e | 2014-05-09 16:57:40 -0700 | [diff] [blame] | 104 | oat_filename += ".oat"; |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 105 | } else { |
| 106 | oat_filename.replace(oat_filename.length() - 3, 3, "oat"); |
| 107 | } |
| 108 | return oat_filename; |
| 109 | } |
| 110 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 111 | enum ImageRoot { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 112 | kResolutionMethod, |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 113 | kImtConflictMethod, |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 114 | kImtUnimplementedMethod, |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 115 | kDefaultImt, |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 116 | kCalleeSaveMethod, |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 117 | kRefsOnlySaveMethod, |
| 118 | kRefsAndArgsSaveMethod, |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 119 | kDexCaches, |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 120 | kClassRoots, |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 121 | kImageRootsMax, |
| 122 | }; |
| 123 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 124 | mirror::Object* GetImageRoot(ImageRoot image_root) const |
| 125 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 126 | mirror::ObjectArray<mirror::Object>* GetImageRoots() const |
| 127 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 128 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 129 | void RelocateImage(off_t delta); |
| 130 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 131 | bool CompilePic() const { |
| 132 | return compile_pic_ != 0; |
| 133 | } |
| 134 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 135 | private: |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 136 | static const uint8_t kImageMagic[4]; |
| 137 | static const uint8_t kImageVersion[4]; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 138 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 139 | uint8_t magic_[4]; |
| 140 | uint8_t version_[4]; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 141 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 142 | // Required base address for mapping the image. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 143 | uint32_t image_begin_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 144 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 145 | // Image size, not page aligned. |
| 146 | uint32_t image_size_; |
| 147 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 148 | // ArtField array offset. |
| 149 | uint32_t art_fields_offset_; |
| 150 | |
| 151 | // ArtField size in bytes. |
| 152 | uint32_t art_fields_size_; |
| 153 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 154 | // Image bitmap offset in the file. |
| 155 | uint32_t image_bitmap_offset_; |
| 156 | |
| 157 | // Size of the image bitmap. |
| 158 | uint32_t image_bitmap_size_; |
| 159 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 160 | // Checksum of the oat file we link to for load time sanity check. |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 161 | uint32_t oat_checksum_; |
| 162 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 163 | // Start address for oat file. Will be before oat_data_begin_ for .so files. |
| 164 | uint32_t oat_file_begin_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 165 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 166 | // Required oat address expected by image Method::GetCode() pointers. |
| 167 | uint32_t oat_data_begin_; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 168 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 169 | // End of oat data address range for this image file. |
| 170 | uint32_t oat_data_end_; |
| 171 | |
| 172 | // End of oat file address range. will be after oat_data_end_ for |
| 173 | // .so files. Used for positioning a following alloc spaces. |
| 174 | uint32_t oat_file_end_; |
| 175 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 176 | // The total delta that this image has been patched. |
| 177 | int32_t patch_delta_; |
| 178 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 179 | // Absolute address of an Object[] of objects needed to reinitialize from an image. |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 180 | uint32_t image_roots_; |
| 181 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 182 | // Boolean (0 or 1) to denote if the image was compiled with --compile-pic option |
| 183 | const uint32_t compile_pic_; |
| 184 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 185 | friend class ImageWriter; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | } // namespace art |
| 189 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 190 | #endif // ART_RUNTIME_IMAGE_H_ |