image.h revision a6e81ed4c185b7362cd5199ebe5507d00883a9b0
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 enum StorageMode : uint32_t { 82 kStorageModeUncompressed, 83 kStorageModeLZ4, 84 kStorageModeLZ4HC, 85 kStorageModeCount, // Number of elements in enum. 86 }; 87 static constexpr StorageMode kDefaultStorageMode = kStorageModeUncompressed; 88 89 ImageHeader() 90 : image_begin_(0U), 91 image_size_(0U), 92 oat_checksum_(0U), 93 oat_file_begin_(0U), 94 oat_data_begin_(0U), 95 oat_data_end_(0U), 96 oat_file_end_(0U), 97 boot_image_begin_(0U), 98 boot_image_size_(0U), 99 boot_oat_begin_(0U), 100 boot_oat_size_(0U), 101 patch_delta_(0), 102 image_roots_(0U), 103 pointer_size_(0U), 104 compile_pic_(0), 105 is_pic_(0), 106 storage_mode_(kDefaultStorageMode), 107 data_size_(0) {} 108 109 ImageHeader(uint32_t image_begin, 110 uint32_t image_size, 111 ImageSection* sections, 112 uint32_t image_roots, 113 uint32_t oat_checksum, 114 uint32_t oat_file_begin, 115 uint32_t oat_data_begin, 116 uint32_t oat_data_end, 117 uint32_t oat_file_end, 118 uint32_t boot_image_begin, 119 uint32_t boot_image_size, 120 uint32_t boot_oat_begin, 121 uint32_t boot_oat_size, 122 uint32_t pointer_size, 123 bool compile_pic, 124 bool is_pic, 125 StorageMode storage_mode, 126 size_t data_size); 127 128 bool IsValid() const; 129 const char* GetMagic() const; 130 131 uint8_t* GetImageBegin() const { 132 return reinterpret_cast<uint8_t*>(image_begin_); 133 } 134 135 size_t GetImageSize() const { 136 return static_cast<uint32_t>(image_size_); 137 } 138 139 uint32_t GetOatChecksum() const { 140 return oat_checksum_; 141 } 142 143 void SetOatChecksum(uint32_t oat_checksum) { 144 oat_checksum_ = oat_checksum; 145 } 146 147 // The location that the oat file was expected to be when the image was created. The actual 148 // oat file may be at a different location for application images. 149 uint8_t* GetOatFileBegin() const { 150 return reinterpret_cast<uint8_t*>(oat_file_begin_); 151 } 152 153 uint8_t* GetOatDataBegin() const { 154 return reinterpret_cast<uint8_t*>(oat_data_begin_); 155 } 156 157 uint8_t* GetOatDataEnd() const { 158 return reinterpret_cast<uint8_t*>(oat_data_end_); 159 } 160 161 uint8_t* GetOatFileEnd() const { 162 return reinterpret_cast<uint8_t*>(oat_file_end_); 163 } 164 165 uint32_t GetPointerSize() const { 166 return pointer_size_; 167 } 168 169 off_t GetPatchDelta() const { 170 return patch_delta_; 171 } 172 173 static std::string GetOatLocationFromImageLocation(const std::string& image) { 174 std::string oat_filename = image; 175 if (oat_filename.length() <= 3) { 176 oat_filename += ".oat"; 177 } else { 178 oat_filename.replace(oat_filename.length() - 3, 3, "oat"); 179 } 180 return oat_filename; 181 } 182 183 enum ImageMethod { 184 kResolutionMethod, 185 kImtConflictMethod, 186 kImtUnimplementedMethod, 187 kCalleeSaveMethod, 188 kRefsOnlySaveMethod, 189 kRefsAndArgsSaveMethod, 190 kImageMethodsCount, // Number of elements in enum. 191 }; 192 193 enum ImageRoot { 194 kDexCaches, 195 kClassRoots, 196 kImageRootsMax, 197 }; 198 199 enum ImageSections { 200 kSectionObjects, 201 kSectionArtFields, 202 kSectionArtMethods, 203 kSectionDexCacheArrays, 204 kSectionInternedStrings, 205 kSectionClassTable, 206 kSectionImageBitmap, 207 kSectionCount, // Number of elements in enum. 208 }; 209 210 ArtMethod* GetImageMethod(ImageMethod index) const; 211 void SetImageMethod(ImageMethod index, ArtMethod* method); 212 213 const ImageSection& GetImageSection(ImageSections index) const; 214 const ImageSection& GetMethodsSection() const { 215 return GetImageSection(kSectionArtMethods); 216 } 217 218 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 219 mirror::Object* GetImageRoot(ImageRoot image_root) const 220 SHARED_REQUIRES(Locks::mutator_lock_); 221 222 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 223 mirror::ObjectArray<mirror::Object>* GetImageRoots() const 224 SHARED_REQUIRES(Locks::mutator_lock_); 225 226 void RelocateImage(off_t delta); 227 void RelocateImageMethods(off_t delta); 228 void RelocateImageObjects(off_t delta); 229 230 bool CompilePic() const { 231 return compile_pic_ != 0; 232 } 233 234 bool IsPic() const { 235 return is_pic_ != 0; 236 } 237 238 uint32_t GetBootImageBegin() const { 239 return boot_image_begin_; 240 } 241 242 uint32_t GetBootImageSize() const { 243 return boot_image_size_; 244 } 245 246 uint32_t GetBootOatBegin() const { 247 return boot_oat_begin_; 248 } 249 250 uint32_t GetBootOatSize() const { 251 return boot_oat_size_; 252 } 253 254 StorageMode GetStorageMode() const { 255 return storage_mode_; 256 } 257 258 uint64_t GetDataSize() const { 259 return data_size_; 260 } 261 262 bool IsAppImage() const { 263 // App images currently require a boot image, if the size is non zero then it is an app image 264 // header. 265 return boot_image_size_ != 0u; 266 } 267 268 private: 269 static const uint8_t kImageMagic[4]; 270 static const uint8_t kImageVersion[4]; 271 272 uint8_t magic_[4]; 273 uint8_t version_[4]; 274 275 // Required base address for mapping the image. 276 uint32_t image_begin_; 277 278 // Image size, not page aligned. 279 uint32_t image_size_; 280 281 // Checksum of the oat file we link to for load time sanity check. 282 uint32_t oat_checksum_; 283 284 // Start address for oat file. Will be before oat_data_begin_ for .so files. 285 uint32_t oat_file_begin_; 286 287 // Required oat address expected by image Method::GetCode() pointers. 288 uint32_t oat_data_begin_; 289 290 // End of oat data address range for this image file. 291 uint32_t oat_data_end_; 292 293 // End of oat file address range. will be after oat_data_end_ for 294 // .so files. Used for positioning a following alloc spaces. 295 uint32_t oat_file_end_; 296 297 // Boot image begin and end (app image headers only). 298 uint32_t boot_image_begin_; 299 uint32_t boot_image_size_; 300 301 // Boot oat begin and end (app image headers only). 302 uint32_t boot_oat_begin_; 303 uint32_t boot_oat_size_; 304 305 // TODO: We should probably insert a boot image checksum for app images. 306 307 // The total delta that this image has been patched. 308 int32_t patch_delta_; 309 310 // Absolute address of an Object[] of objects needed to reinitialize from an image. 311 uint32_t image_roots_; 312 313 // Pointer size, this affects the size of the ArtMethods. 314 uint32_t pointer_size_; 315 316 // Boolean (0 or 1) to denote if the image was compiled with --compile-pic option 317 const uint32_t compile_pic_; 318 319 // Boolean (0 or 1) to denote if the image can be mapped at a random address, this only refers to 320 // the .art file. Currently, app oat files do not depend on their app image. There are no pointers 321 // from the app oat code to the app image. 322 const uint32_t is_pic_; 323 324 // Image section sizes/offsets correspond to the uncompressed form. 325 ImageSection sections_[kSectionCount]; 326 327 // Image methods, may be inside of the boot image for app images. 328 uint64_t image_methods_[kImageMethodsCount]; 329 330 // Storage method for the image, the image may be compressed. 331 StorageMode storage_mode_; 332 333 // Data size for the image data excluding the bitmap and the header. For compressed images, this 334 // is the compressed size in the file. 335 uint32_t data_size_; 336 337 friend class ImageWriter; 338}; 339 340std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageMethod& policy); 341std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageRoot& policy); 342std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageSections& section); 343std::ostream& operator<<(std::ostream& os, const ImageSection& section); 344std::ostream& operator<<(std::ostream& os, const ImageHeader::StorageMode& mode); 345 346} // namespace art 347 348#endif // ART_RUNTIME_IMAGE_H_ 349