image.h revision 05792b98980741111b4d0a24d68cff2a8e070a3a
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 25namespace art { 26 27class ArtField; 28class ArtMethod; 29 30class ArtMethodVisitor { 31 public: 32 virtual ~ArtMethodVisitor() {} 33 34 virtual void Visit(ArtMethod* method) = 0; 35}; 36 37class ArtFieldVisitor { 38 public: 39 virtual ~ArtFieldVisitor() {} 40 41 virtual void Visit(ArtField* method) = 0; 42}; 43 44class 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 67 // Visit ArtMethods in the section starting at base. 68 void VisitPackedArtMethods(ArtMethodVisitor* visitor, uint8_t* base, size_t pointer_size) const; 69 70 // Visit ArtMethods in the section starting at base. 71 void VisitPackedArtFields(ArtFieldVisitor* visitor, uint8_t* base) const; 72 73 private: 74 uint32_t offset_; 75 uint32_t size_; 76}; 77 78// header of image files written by ImageWriter, read and validated by Space. 79class PACKED(4) ImageHeader { 80 public: 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) {} 85 86 ImageHeader(uint32_t image_begin, 87 uint32_t image_size_, 88 ImageSection* sections, 89 uint32_t image_roots, 90 uint32_t oat_checksum, 91 uint32_t oat_file_begin, 92 uint32_t oat_data_begin, 93 uint32_t oat_data_end, 94 uint32_t oat_file_end, 95 uint32_t pointer_size, 96 bool compile_pic_); 97 98 bool IsValid() const; 99 const char* GetMagic() const; 100 101 uint8_t* GetImageBegin() const { 102 return reinterpret_cast<uint8_t*>(image_begin_); 103 } 104 105 size_t GetImageSize() const { 106 return static_cast<uint32_t>(image_size_); 107 } 108 109 uint32_t GetOatChecksum() const { 110 return oat_checksum_; 111 } 112 113 void SetOatChecksum(uint32_t oat_checksum) { 114 oat_checksum_ = oat_checksum; 115 } 116 117 uint8_t* GetOatFileBegin() const { 118 return reinterpret_cast<uint8_t*>(oat_file_begin_); 119 } 120 121 uint8_t* GetOatDataBegin() const { 122 return reinterpret_cast<uint8_t*>(oat_data_begin_); 123 } 124 125 uint8_t* GetOatDataEnd() const { 126 return reinterpret_cast<uint8_t*>(oat_data_end_); 127 } 128 129 uint8_t* GetOatFileEnd() const { 130 return reinterpret_cast<uint8_t*>(oat_file_end_); 131 } 132 133 uint32_t GetPointerSize() const { 134 return pointer_size_; 135 } 136 137 off_t GetPatchDelta() const { 138 return patch_delta_; 139 } 140 141 static std::string GetOatLocationFromImageLocation(const std::string& image) { 142 std::string oat_filename = image; 143 if (oat_filename.length() <= 3) { 144 oat_filename += ".oat"; 145 } else { 146 oat_filename.replace(oat_filename.length() - 3, 3, "oat"); 147 } 148 return oat_filename; 149 } 150 151 enum ImageMethod { 152 kResolutionMethod, 153 kImtConflictMethod, 154 kImtUnimplementedMethod, 155 kCalleeSaveMethod, 156 kRefsOnlySaveMethod, 157 kRefsAndArgsSaveMethod, 158 kImageMethodsCount, // Number of elements in enum. 159 }; 160 161 enum ImageRoot { 162 kDexCaches, 163 kClassRoots, 164 kImageRootsMax, 165 }; 166 167 enum ImageSections { 168 kSectionObjects, 169 kSectionArtFields, 170 kSectionArtMethods, 171 kSectionDexCacheArrays, 172 kSectionInternedStrings, 173 kSectionImageBitmap, 174 kSectionCount, // Number of elements in enum. 175 }; 176 177 ArtMethod* GetImageMethod(ImageMethod index) const; 178 void SetImageMethod(ImageMethod index, ArtMethod* method); 179 180 const ImageSection& GetImageSection(ImageSections index) const; 181 const ImageSection& GetMethodsSection() const { 182 return GetImageSection(kSectionArtMethods); 183 } 184 185 mirror::Object* GetImageRoot(ImageRoot image_root) const 186 SHARED_REQUIRES(Locks::mutator_lock_); 187 mirror::ObjectArray<mirror::Object>* GetImageRoots() const 188 SHARED_REQUIRES(Locks::mutator_lock_); 189 190 void RelocateImage(off_t delta); 191 192 bool CompilePic() const { 193 return compile_pic_ != 0; 194 } 195 196 private: 197 static const uint8_t kImageMagic[4]; 198 static const uint8_t kImageVersion[4]; 199 200 uint8_t magic_[4]; 201 uint8_t version_[4]; 202 203 // Required base address for mapping the image. 204 uint32_t image_begin_; 205 206 // Image size, not page aligned. 207 uint32_t image_size_; 208 209 // Checksum of the oat file we link to for load time sanity check. 210 uint32_t oat_checksum_; 211 212 // Start address for oat file. Will be before oat_data_begin_ for .so files. 213 uint32_t oat_file_begin_; 214 215 // Required oat address expected by image Method::GetCode() pointers. 216 uint32_t oat_data_begin_; 217 218 // End of oat data address range for this image file. 219 uint32_t oat_data_end_; 220 221 // End of oat file address range. will be after oat_data_end_ for 222 // .so files. Used for positioning a following alloc spaces. 223 uint32_t oat_file_end_; 224 225 // The total delta that this image has been patched. 226 int32_t patch_delta_; 227 228 // Absolute address of an Object[] of objects needed to reinitialize from an image. 229 uint32_t image_roots_; 230 231 // Pointer size, this affects the size of the ArtMethods. 232 uint32_t pointer_size_; 233 234 // Boolean (0 or 1) to denote if the image was compiled with --compile-pic option 235 const uint32_t compile_pic_; 236 237 // Image sections 238 ImageSection sections_[kSectionCount]; 239 240 // Image methods. 241 uint64_t image_methods_[kImageMethodsCount]; 242 243 friend class ImageWriter; 244}; 245 246std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageMethod& policy); 247std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageRoot& policy); 248std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageSections& section); 249std::ostream& operator<<(std::ostream& os, const ImageSection& section); 250 251} // namespace art 252 253#endif // ART_RUNTIME_IMAGE_H_ 254