image.h revision 9583fbcf597eff6d0b3c5359b8e8d5f70ed82c40
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#include "utils.h"
25
26namespace art {
27
28// header of image files written by ImageWriter, read and validated by Space.
29class PACKED(4) ImageHeader {
30 public:
31  ImageHeader() {}
32
33  ImageHeader(uint32_t image_begin,
34              uint32_t image_size_,
35              uint32_t image_bitmap_offset,
36              uint32_t image_bitmap_size,
37              uint32_t image_roots,
38              uint32_t oat_checksum,
39              uint32_t oat_file_begin,
40              uint32_t oat_data_begin,
41              uint32_t oat_data_end,
42              uint32_t oat_file_end);
43
44  bool IsValid() const;
45  const char* GetMagic() const;
46
47  byte* GetImageBegin() const {
48    return reinterpret_cast<byte*>(image_begin_);
49  }
50
51  size_t GetImageSize() const {
52    return static_cast<uint32_t>(image_size_);
53  }
54
55  size_t GetImageBitmapOffset() const {
56    return image_bitmap_offset_;
57  }
58
59  size_t GetImageBitmapSize() const {
60    return image_bitmap_size_;
61  }
62
63  uint32_t GetOatChecksum() const {
64    return oat_checksum_;
65  }
66
67  void SetOatChecksum(uint32_t oat_checksum) {
68    oat_checksum_ = oat_checksum;
69  }
70
71  byte* GetOatFileBegin() const {
72    return reinterpret_cast<byte*>(oat_file_begin_);
73  }
74
75  byte* GetOatDataBegin() const {
76    return reinterpret_cast<byte*>(oat_data_begin_);
77  }
78
79  byte* GetOatDataEnd() const {
80    return reinterpret_cast<byte*>(oat_data_end_);
81  }
82
83  byte* GetOatFileEnd() const {
84    return reinterpret_cast<byte*>(oat_file_end_);
85  }
86
87  size_t GetBitmapOffset() const {
88    return RoundUp(image_size_, kPageSize);
89  }
90
91  static std::string GetOatLocationFromImageLocation(const std::string& image) {
92    std::string oat_filename = image;
93    if (oat_filename.length() <= 3) {
94      return oat_filename + ".oat";
95    } else {
96      oat_filename.replace(oat_filename.length() - 3, 3, "oat");
97    }
98    return oat_filename;
99  }
100
101  enum ImageRoot {
102    kResolutionMethod,
103    kImtConflictMethod,
104    kDefaultImt,
105    kCalleeSaveMethod,
106    kRefsOnlySaveMethod,
107    kRefsAndArgsSaveMethod,
108    kDexCaches,
109    kClassRoots,
110    kImageRootsMax,
111  };
112
113  mirror::Object* GetImageRoot(ImageRoot image_root) const
114      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
115
116 private:
117  mirror::ObjectArray<mirror::Object>* GetImageRoots() const;
118
119  static const byte kImageMagic[4];
120  static const byte kImageVersion[4];
121
122  byte magic_[4];
123  byte version_[4];
124
125  // Required base address for mapping the image.
126  uint32_t image_begin_;
127
128  // Image size, not page aligned.
129  uint32_t image_size_;
130
131  // Image bitmap offset in the file.
132  uint32_t image_bitmap_offset_;
133
134  // Size of the image bitmap.
135  uint32_t image_bitmap_size_;
136
137  // Checksum of the oat file we link to for load time sanity check.
138  uint32_t oat_checksum_;
139
140  // Start address for oat file. Will be before oat_data_begin_ for .so files.
141  uint32_t oat_file_begin_;
142
143  // Required oat address expected by image Method::GetCode() pointers.
144  uint32_t oat_data_begin_;
145
146  // End of oat data address range for this image file.
147  uint32_t oat_data_end_;
148
149  // End of oat file address range. will be after oat_data_end_ for
150  // .so files. Used for positioning a following alloc spaces.
151  uint32_t oat_file_end_;
152
153  // Absolute address of an Object[] of objects needed to reinitialize from an image.
154  uint32_t image_roots_;
155
156  friend class ImageWriter;
157  friend class ImageDumper;  // For GetImageRoots()
158};
159
160}  // namespace art
161
162#endif  // ART_RUNTIME_IMAGE_H_
163