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