image.h revision 179486aee731f734207873244542993ed4bcff21
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 */ 16 17#ifndef ART_RUNTIME_IMAGE_H_ 18#define ART_RUNTIME_IMAGE_H_ 19 20#include <string.h> 21 22#include "globals.h" 23#include "mirror/object.h" 24#include "utils.h" 25 26namespace art { 27 28// header of image files written by ImageWriter, read and validated by Space. 29class PACKED(4) ImageHeader { 30 public: 31 ImageHeader() {} 32 33 ImageHeader(uint32_t image_begin, 34 uint32_t image_size_, 35 uint32_t image_bitmap_offset, 36 uint32_t image_bitmap_size, 37 uint32_t image_roots, 38 uint32_t oat_checksum, 39 uint32_t oat_file_begin, 40 uint32_t oat_data_begin, 41 uint32_t oat_data_end, 42 uint32_t oat_file_end); 43 44 bool IsValid() const { 45 if (memcmp(magic_, kImageMagic, sizeof(kImageMagic)) != 0) { 46 return false; 47 } 48 if (memcmp(version_, kImageVersion, sizeof(kImageVersion)) != 0) { 49 return false; 50 } 51 return true; 52 } 53 54 const char* GetMagic() const { 55 CHECK(IsValid()); 56 return reinterpret_cast<const char*>(magic_); 57 } 58 59 byte* GetImageBegin() const { 60 return reinterpret_cast<byte*>(image_begin_); 61 } 62 63 size_t GetImageSize() const { 64 return static_cast<uint32_t>(image_size_); 65 } 66 67 size_t GetImageBitmapOffset() const { 68 return image_bitmap_offset_; 69 } 70 71 size_t GetImageBitmapSize() const { 72 return image_bitmap_size_; 73 } 74 75 uint32_t GetOatChecksum() const { 76 return oat_checksum_; 77 } 78 79 void SetOatChecksum(uint32_t oat_checksum) { 80 oat_checksum_ = oat_checksum; 81 } 82 83 byte* GetOatFileBegin() const { 84 return reinterpret_cast<byte*>(oat_file_begin_); 85 } 86 87 byte* GetOatDataBegin() const { 88 return reinterpret_cast<byte*>(oat_data_begin_); 89 } 90 91 byte* GetOatDataEnd() const { 92 return reinterpret_cast<byte*>(oat_data_end_); 93 } 94 95 byte* GetOatFileEnd() const { 96 return reinterpret_cast<byte*>(oat_file_end_); 97 } 98 99 size_t GetBitmapOffset() const { 100 return RoundUp(image_size_, kPageSize); 101 } 102 103 enum ImageRoot { 104 kResolutionMethod, 105 kCalleeSaveMethod, 106 kRefsOnlySaveMethod, 107 kRefsAndArgsSaveMethod, 108 kOatLocation, 109 kDexCaches, 110 kClassRoots, 111 kImageRootsMax, 112 }; 113 114 mirror::Object* GetImageRoot(ImageRoot image_root) const 115 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 116 117 private: 118 mirror::ObjectArray<mirror::Object>* GetImageRoots() const; 119 120 static const byte kImageMagic[4]; 121 static const byte kImageVersion[4]; 122 123 byte magic_[4]; 124 byte version_[4]; 125 126 // Required base address for mapping the image. 127 uint32_t image_begin_; 128 129 // Image size, not page aligned. 130 uint32_t image_size_; 131 132 // Image bitmap offset in the file. 133 uint32_t image_bitmap_offset_; 134 135 // Size of the image bitmap. 136 uint32_t image_bitmap_size_; 137 138 // Checksum of the oat file we link to for load time sanity check. 139 uint32_t oat_checksum_; 140 141 // Start address for oat file. Will be before oat_data_begin_ for .so files. 142 uint32_t oat_file_begin_; 143 144 // Required oat address expected by image Method::GetCode() pointers. 145 uint32_t oat_data_begin_; 146 147 // End of oat data address range for this image file. 148 uint32_t oat_data_end_; 149 150 // End of oat file address range. will be after oat_data_end_ for 151 // .so files. Used for positioning a following alloc spaces. 152 uint32_t oat_file_end_; 153 154 // Absolute address of an Object[] of objects needed to reinitialize from an image. 155 uint32_t image_roots_; 156 157 friend class ImageWriter; 158 friend class ImageDumper; // For GetImageRoots() 159}; 160 161} // namespace art 162 163#endif // ART_RUNTIME_IMAGE_H_ 164