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