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