Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_IMAGE_H_ |
| 4 | #define ART_SRC_IMAGE_H_ |
| 5 | |
| 6 | #include <string.h> |
| 7 | |
| 8 | #include "globals.h" |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 9 | #include "object.h" |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 10 | |
| 11 | namespace art { |
| 12 | |
| 13 | // header of image files written by ImageWriter, read and validated by Space. |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 14 | class PACKED ImageHeader { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 15 | public: |
| 16 | ImageHeader() {} |
| 17 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 18 | ImageHeader(uint32_t image_base_addr, |
| 19 | uint32_t image_roots, |
| 20 | uint32_t oat_checksum, |
| 21 | uint32_t oat_base_addr, |
| 22 | uint32_t oat_limit_addr) |
| 23 | : image_base_addr_(image_base_addr), |
| 24 | oat_checksum_(oat_checksum), |
| 25 | oat_base_addr_(oat_base_addr), |
| 26 | oat_limit_addr_(oat_limit_addr), |
| 27 | image_roots_(image_roots) { |
| 28 | CHECK_EQ(image_base_addr, RoundUp(image_base_addr, kPageSize)); |
| 29 | CHECK_EQ(oat_base_addr, RoundUp(oat_base_addr, kPageSize)); |
| 30 | CHECK_LT(image_base_addr, image_roots); |
| 31 | CHECK_LT(image_roots, oat_base_addr); |
| 32 | CHECK_LT(oat_base_addr, oat_limit_addr); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 33 | memcpy(magic_, kImageMagic, sizeof(kImageMagic)); |
| 34 | memcpy(version_, kImageVersion, sizeof(kImageVersion)); |
| 35 | } |
| 36 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 37 | bool IsValid() const { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 38 | if (memcmp(magic_, kImageMagic, sizeof(kImageMagic) != 0)) { |
| 39 | return false; |
| 40 | } |
| 41 | if (memcmp(version_, kImageVersion, sizeof(kImageVersion) != 0)) { |
| 42 | return false; |
| 43 | } |
| 44 | return true; |
| 45 | } |
| 46 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 47 | const char* GetMagic() const { |
| 48 | CHECK(IsValid()); |
| 49 | return reinterpret_cast<const char*>(magic_); |
| 50 | } |
| 51 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 52 | byte* GetImageBaseAddr() const { |
| 53 | return reinterpret_cast<byte*>(image_base_addr_); |
| 54 | } |
| 55 | |
| 56 | uint32_t GetOatChecksum() const { |
| 57 | return oat_checksum_; |
| 58 | } |
| 59 | |
| 60 | byte* GetOatBaseAddr() const { |
| 61 | return reinterpret_cast<byte*>(oat_base_addr_); |
| 62 | } |
| 63 | |
| 64 | byte* GetOatLimitAddr() const { |
| 65 | return reinterpret_cast<byte*>(oat_limit_addr_); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 68 | enum ImageRoot { |
| 69 | kJniStubArray, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 70 | kAbstractMethodErrorStubArray, |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 71 | kInstanceResolutionStubArray, |
| 72 | kStaticResolutionStubArray, |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 73 | kUnknownMethodResolutionStubArray, |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 74 | kCalleeSaveMethod, |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 75 | kRefsOnlySaveMethod, |
| 76 | kRefsAndArgsSaveMethod, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 77 | kOatLocation, |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 78 | kDexCaches, |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 79 | kClassRoots, |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 80 | kImageRootsMax, |
| 81 | }; |
| 82 | |
| 83 | Object* GetImageRoot(ImageRoot image_root) const { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame^] | 84 | return GetImageRoots()->Get(image_root); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 87 | private: |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame^] | 88 | ObjectArray<Object>* GetImageRoots() const { |
| 89 | return reinterpret_cast<ObjectArray<Object>*>(image_roots_); |
| 90 | } |
| 91 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 92 | static const byte kImageMagic[4]; |
| 93 | static const byte kImageVersion[4]; |
| 94 | |
| 95 | byte magic_[4]; |
| 96 | byte version_[4]; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 97 | |
| 98 | // required base address for mapping the image. |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 99 | uint32_t image_base_addr_; |
| 100 | |
| 101 | // checksum of the oat file we link to for load time sanity check |
| 102 | uint32_t oat_checksum_; |
| 103 | |
| 104 | // required oat address expected by image Method::GetCode() pointers. |
| 105 | uint32_t oat_base_addr_; |
| 106 | |
| 107 | // end of oat address range for this image file, used for positioning a following image |
| 108 | uint32_t oat_limit_addr_; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 109 | |
| 110 | // absolute address of an Object[] of objects needed to reinitialize from an image |
| 111 | uint32_t image_roots_; |
| 112 | |
| 113 | friend class ImageWriter; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame^] | 114 | friend class ImageDump; // For GetImageRoots() |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | } // namespace art |
| 118 | |
| 119 | #endif // ART_SRC_IMAGE_H_ |