image.h revision 208a5cb383dd9dcd3461f89b74af5df67dc8d794
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 kSectionClassTable, 174 kSectionImageBitmap, 175 kSectionCount, // Number of elements in enum. 176 }; 177 178 ArtMethod* GetImageMethod(ImageMethod index) const; 179 void SetImageMethod(ImageMethod index, ArtMethod* method); 180 181 const ImageSection& GetImageSection(ImageSections index) const; 182 const ImageSection& GetMethodsSection() const { 183 return GetImageSection(kSectionArtMethods); 184 } 185 186 mirror::Object* GetImageRoot(ImageRoot image_root) const 187 SHARED_REQUIRES(Locks::mutator_lock_); 188 mirror::ObjectArray<mirror::Object>* GetImageRoots() const 189 SHARED_REQUIRES(Locks::mutator_lock_); 190 191 void RelocateImage(off_t delta); 192 193 bool CompilePic() const { 194 return compile_pic_ != 0; 195 } 196 197 private: 198 static const uint8_t kImageMagic[4]; 199 static const uint8_t kImageVersion[4]; 200 201 uint8_t magic_[4]; 202 uint8_t version_[4]; 203 204 // Required base address for mapping the image. 205 uint32_t image_begin_; 206 207 // Image size, not page aligned. 208 uint32_t image_size_; 209 210 // Checksum of the oat file we link to for load time sanity check. 211 uint32_t oat_checksum_; 212 213 // Start address for oat file. Will be before oat_data_begin_ for .so files. 214 uint32_t oat_file_begin_; 215 216 // Required oat address expected by image Method::GetCode() pointers. 217 uint32_t oat_data_begin_; 218 219 // End of oat data address range for this image file. 220 uint32_t oat_data_end_; 221 222 // End of oat file address range. will be after oat_data_end_ for 223 // .so files. Used for positioning a following alloc spaces. 224 uint32_t oat_file_end_; 225 226 // The total delta that this image has been patched. 227 int32_t patch_delta_; 228 229 // Absolute address of an Object[] of objects needed to reinitialize from an image. 230 uint32_t image_roots_; 231 232 // Pointer size, this affects the size of the ArtMethods. 233 uint32_t pointer_size_; 234 235 // Boolean (0 or 1) to denote if the image was compiled with --compile-pic option 236 const uint32_t compile_pic_; 237 238 // Image sections 239 ImageSection sections_[kSectionCount]; 240 241 // Image methods. 242 uint64_t image_methods_[kImageMethodsCount]; 243 244 friend class ImageWriter; 245}; 246 247std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageMethod& policy); 248std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageRoot& policy); 249std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageSections& section); 250std::ostream& operator<<(std::ostream& os, const ImageSection& section); 251 252} // namespace art 253 254#endif // ART_RUNTIME_IMAGE_H_ 255