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 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 27 | class ArtField; |
| 28 | class ArtMethod; |
| 29 | |
| 30 | class ArtMethodVisitor { |
| 31 | public: |
| 32 | virtual ~ArtMethodVisitor() {} |
| 33 | |
| 34 | virtual void Visit(ArtMethod* method) = 0; |
| 35 | }; |
| 36 | |
| 37 | class ArtFieldVisitor { |
| 38 | public: |
| 39 | virtual ~ArtFieldVisitor() {} |
| 40 | |
| 41 | virtual void Visit(ArtField* method) = 0; |
| 42 | }; |
| 43 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 44 | class PACKED(4) ImageSection { |
| 45 | public: |
| 46 | ImageSection() : offset_(0), size_(0) { } |
| 47 | ImageSection(uint32_t offset, uint32_t size) : offset_(offset), size_(size) { } |
| 48 | ImageSection(const ImageSection& section) = default; |
| 49 | ImageSection& operator=(const ImageSection& section) = default; |
| 50 | |
| 51 | uint32_t Offset() const { |
| 52 | return offset_; |
| 53 | } |
| 54 | |
| 55 | uint32_t Size() const { |
| 56 | return size_; |
| 57 | } |
| 58 | |
| 59 | uint32_t End() const { |
| 60 | return Offset() + Size(); |
| 61 | } |
| 62 | |
| 63 | bool Contains(uint64_t offset) const { |
| 64 | return offset - offset_ < size_; |
| 65 | } |
| 66 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 67 | // Visit ArtMethods in the section starting at base. |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 68 | void VisitPackedArtMethods(ArtMethodVisitor* visitor, uint8_t* base, size_t pointer_size) const; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 69 | |
| 70 | // Visit ArtMethods in the section starting at base. |
| 71 | void VisitPackedArtFields(ArtFieldVisitor* visitor, uint8_t* base) const; |
| 72 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 73 | private: |
| 74 | uint32_t offset_; |
| 75 | uint32_t size_; |
| 76 | }; |
| 77 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 78 | // header of image files written by ImageWriter, read and validated by Space. |
Ian Rogers | df1ce91 | 2012-11-27 17:07:11 -0800 | [diff] [blame] | 79 | class PACKED(4) ImageHeader { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 80 | public: |
Sebastien Hertz | aa50d3a | 2015-08-25 15:25:41 +0200 | [diff] [blame^] | 81 | ImageHeader() |
| 82 | : image_begin_(0U), image_size_(0U), oat_checksum_(0U), oat_file_begin_(0U), |
| 83 | oat_data_begin_(0U), oat_data_end_(0U), oat_file_end_(0U), patch_delta_(0), |
| 84 | image_roots_(0U), pointer_size_(0U), compile_pic_(0) {} |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 85 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 86 | ImageHeader(uint32_t image_begin, |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 87 | uint32_t image_size_, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 88 | ImageSection* sections, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 89 | uint32_t image_roots, |
| 90 | uint32_t oat_checksum, |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 91 | uint32_t oat_file_begin, |
| 92 | uint32_t oat_data_begin, |
| 93 | uint32_t oat_data_end, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 94 | uint32_t oat_file_end, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 95 | uint32_t pointer_size, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 96 | bool compile_pic_); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 97 | |
Brian Carlstrom | 68708f5 | 2013-09-03 14:15:31 -0700 | [diff] [blame] | 98 | bool IsValid() const; |
| 99 | const char* GetMagic() const; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 100 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 101 | uint8_t* GetImageBegin() const { |
| 102 | return reinterpret_cast<uint8_t*>(image_begin_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 105 | size_t GetImageSize() const { |
| 106 | return static_cast<uint32_t>(image_size_); |
| 107 | } |
| 108 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 109 | uint32_t GetOatChecksum() const { |
| 110 | return oat_checksum_; |
| 111 | } |
| 112 | |
Brian Carlstrom | a85b837 | 2012-10-18 17:00:32 -0700 | [diff] [blame] | 113 | void SetOatChecksum(uint32_t oat_checksum) { |
| 114 | oat_checksum_ = oat_checksum; |
| 115 | } |
| 116 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 117 | uint8_t* GetOatFileBegin() const { |
| 118 | return reinterpret_cast<uint8_t*>(oat_file_begin_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 121 | uint8_t* GetOatDataBegin() const { |
| 122 | return reinterpret_cast<uint8_t*>(oat_data_begin_); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 125 | uint8_t* GetOatDataEnd() const { |
| 126 | return reinterpret_cast<uint8_t*>(oat_data_end_); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 129 | uint8_t* GetOatFileEnd() const { |
| 130 | return reinterpret_cast<uint8_t*>(oat_file_end_); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 133 | uint32_t GetPointerSize() const { |
| 134 | return pointer_size_; |
| 135 | } |
| 136 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 137 | off_t GetPatchDelta() const { |
| 138 | return patch_delta_; |
| 139 | } |
| 140 | |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 141 | static std::string GetOatLocationFromImageLocation(const std::string& image) { |
| 142 | std::string oat_filename = image; |
| 143 | if (oat_filename.length() <= 3) { |
Ian Rogers | b9beb2e | 2014-05-09 16:57:40 -0700 | [diff] [blame] | 144 | oat_filename += ".oat"; |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 145 | } else { |
| 146 | oat_filename.replace(oat_filename.length() - 3, 3, "oat"); |
| 147 | } |
| 148 | return oat_filename; |
| 149 | } |
| 150 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 151 | enum ImageMethod { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 152 | kResolutionMethod, |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 153 | kImtConflictMethod, |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 154 | kImtUnimplementedMethod, |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 155 | kCalleeSaveMethod, |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 156 | kRefsOnlySaveMethod, |
| 157 | kRefsAndArgsSaveMethod, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 158 | kImageMethodsCount, // Number of elements in enum. |
| 159 | }; |
| 160 | |
| 161 | enum ImageRoot { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 162 | kDexCaches, |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 163 | kClassRoots, |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 164 | kImageRootsMax, |
| 165 | }; |
| 166 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 167 | enum ImageSections { |
| 168 | kSectionObjects, |
| 169 | kSectionArtFields, |
| 170 | kSectionArtMethods, |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 171 | kSectionInternedStrings, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 172 | kSectionImageBitmap, |
| 173 | kSectionCount, // Number of elements in enum. |
| 174 | }; |
| 175 | |
| 176 | ArtMethod* GetImageMethod(ImageMethod index) const; |
| 177 | void SetImageMethod(ImageMethod index, ArtMethod* method); |
| 178 | |
| 179 | const ImageSection& GetImageSection(ImageSections index) const; |
| 180 | const ImageSection& GetMethodsSection() const { |
| 181 | return GetImageSection(kSectionArtMethods); |
| 182 | } |
| 183 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 184 | mirror::Object* GetImageRoot(ImageRoot image_root) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 185 | SHARED_REQUIRES(Locks::mutator_lock_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 186 | mirror::ObjectArray<mirror::Object>* GetImageRoots() const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 187 | SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 188 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 189 | void RelocateImage(off_t delta); |
| 190 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 191 | bool CompilePic() const { |
| 192 | return compile_pic_ != 0; |
| 193 | } |
| 194 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 195 | private: |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 196 | static const uint8_t kImageMagic[4]; |
| 197 | static const uint8_t kImageVersion[4]; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 198 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 199 | uint8_t magic_[4]; |
| 200 | uint8_t version_[4]; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 201 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 202 | // Required base address for mapping the image. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 203 | uint32_t image_begin_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 204 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 205 | // Image size, not page aligned. |
| 206 | uint32_t image_size_; |
| 207 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 208 | // 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] | 209 | uint32_t oat_checksum_; |
| 210 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 211 | // Start address for oat file. Will be before oat_data_begin_ for .so files. |
| 212 | uint32_t oat_file_begin_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 213 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 214 | // Required oat address expected by image Method::GetCode() pointers. |
| 215 | uint32_t oat_data_begin_; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 216 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 217 | // End of oat data address range for this image file. |
| 218 | uint32_t oat_data_end_; |
| 219 | |
| 220 | // End of oat file address range. will be after oat_data_end_ for |
| 221 | // .so files. Used for positioning a following alloc spaces. |
| 222 | uint32_t oat_file_end_; |
| 223 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 224 | // The total delta that this image has been patched. |
| 225 | int32_t patch_delta_; |
| 226 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 227 | // 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] | 228 | uint32_t image_roots_; |
| 229 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 230 | // Pointer size, this affects the size of the ArtMethods. |
| 231 | uint32_t pointer_size_; |
| 232 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 233 | // Boolean (0 or 1) to denote if the image was compiled with --compile-pic option |
| 234 | const uint32_t compile_pic_; |
| 235 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 236 | // Image sections |
| 237 | ImageSection sections_[kSectionCount]; |
| 238 | |
| 239 | // Image methods. |
| 240 | uint64_t image_methods_[kImageMethodsCount]; |
| 241 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 242 | friend class ImageWriter; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 243 | }; |
| 244 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 245 | std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageMethod& policy); |
| 246 | std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageRoot& policy); |
| 247 | std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageSections& section); |
| 248 | std::ostream& operator<<(std::ostream& os, const ImageSection& section); |
| 249 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 250 | } // namespace art |
| 251 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 252 | #endif // ART_RUNTIME_IMAGE_H_ |