image.h revision 2d2621a1463d2f3f03fa73503fa42e43657cdcfc
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  uint8_t* GetImageBegin() const {
48    return reinterpret_cast<uint8_t*>(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  uint8_t* GetOatFileBegin() const {
72    return reinterpret_cast<uint8_t*>(oat_file_begin_);
73  }
74
75  uint8_t* GetOatDataBegin() const {
76    return reinterpret_cast<uint8_t*>(oat_data_begin_);
77  }
78
79  uint8_t* GetOatDataEnd() const {
80    return reinterpret_cast<uint8_t*>(oat_data_end_);
81  }
82
83  uint8_t* GetOatFileEnd() const {
84    return reinterpret_cast<uint8_t*>(oat_file_end_);
85  }
86
87  off_t GetPatchDelta() const {
88    return patch_delta_;
89  }
90
91  size_t GetBitmapOffset() const {
92    return RoundUp(image_size_, kPageSize);
93  }
94
95  static std::string GetOatLocationFromImageLocation(const std::string& image) {
96    std::string oat_filename = image;
97    if (oat_filename.length() <= 3) {
98      oat_filename += ".oat";
99    } else {
100      oat_filename.replace(oat_filename.length() - 3, 3, "oat");
101    }
102    return oat_filename;
103  }
104
105  enum ImageRoot {
106    kResolutionMethod,
107    kImtConflictMethod,
108    kImtUnimplementedMethod,
109    kDefaultImt,
110    kCalleeSaveMethod,
111    kRefsOnlySaveMethod,
112    kRefsAndArgsSaveMethod,
113    kDexCaches,
114    kClassRoots,
115    kImageRootsMax,
116  };
117
118  mirror::Object* GetImageRoot(ImageRoot image_root) const
119      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
120  mirror::ObjectArray<mirror::Object>* GetImageRoots() const;
121
122  void RelocateImage(off_t delta);
123
124 private:
125  static const uint8_t kImageMagic[4];
126  static const uint8_t kImageVersion[4];
127
128  uint8_t magic_[4];
129  uint8_t version_[4];
130
131  // Required base address for mapping the image.
132  uint32_t image_begin_;
133
134  // Image size, not page aligned.
135  uint32_t image_size_;
136
137  // Image bitmap offset in the file.
138  uint32_t image_bitmap_offset_;
139
140  // Size of the image bitmap.
141  uint32_t image_bitmap_size_;
142
143  // Checksum of the oat file we link to for load time sanity check.
144  uint32_t oat_checksum_;
145
146  // Start address for oat file. Will be before oat_data_begin_ for .so files.
147  uint32_t oat_file_begin_;
148
149  // Required oat address expected by image Method::GetCode() pointers.
150  uint32_t oat_data_begin_;
151
152  // End of oat data address range for this image file.
153  uint32_t oat_data_end_;
154
155  // End of oat file address range. will be after oat_data_end_ for
156  // .so files. Used for positioning a following alloc spaces.
157  uint32_t oat_file_end_;
158
159  // The total delta that this image has been patched.
160  int32_t patch_delta_;
161
162  // Absolute address of an Object[] of objects needed to reinitialize from an image.
163  uint32_t image_roots_;
164
165  friend class ImageWriter;
166};
167
168}  // namespace art
169
170#endif  // ART_RUNTIME_IMAGE_H_
171