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