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