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 
25 namespace art {
26 
27 class PACKED(4) ImageSection {
28  public:
ImageSection()29   ImageSection() : offset_(0), size_(0) { }
ImageSection(uint32_t offset,uint32_t size)30   ImageSection(uint32_t offset, uint32_t size) : offset_(offset), size_(size) { }
31   ImageSection(const ImageSection& section) = default;
32   ImageSection& operator=(const ImageSection& section) = default;
33 
Offset()34   uint32_t Offset() const {
35     return offset_;
36   }
37 
Size()38   uint32_t Size() const {
39     return size_;
40   }
41 
End()42   uint32_t End() const {
43     return Offset() + Size();
44   }
45 
Contains(uint64_t offset)46   bool Contains(uint64_t offset) const {
47     return offset - offset_ < size_;
48   }
49 
50  private:
51   uint32_t offset_;
52   uint32_t size_;
53 };
54 
55 // header of image files written by ImageWriter, read and validated by Space.
56 class PACKED(4) ImageHeader {
57  public:
ImageHeader()58   ImageHeader() : compile_pic_(0) {}
59 
60   ImageHeader(uint32_t image_begin,
61               uint32_t image_size_,
62               ImageSection* sections,
63               uint32_t image_roots,
64               uint32_t oat_checksum,
65               uint32_t oat_file_begin,
66               uint32_t oat_data_begin,
67               uint32_t oat_data_end,
68               uint32_t oat_file_end,
69               uint32_t pointer_size,
70               bool compile_pic_);
71 
72   bool IsValid() const;
73   const char* GetMagic() const;
74 
GetImageBegin()75   uint8_t* GetImageBegin() const {
76     return reinterpret_cast<uint8_t*>(image_begin_);
77   }
78 
GetImageSize()79   size_t GetImageSize() const {
80     return static_cast<uint32_t>(image_size_);
81   }
82 
GetOatChecksum()83   uint32_t GetOatChecksum() const {
84     return oat_checksum_;
85   }
86 
SetOatChecksum(uint32_t oat_checksum)87   void SetOatChecksum(uint32_t oat_checksum) {
88     oat_checksum_ = oat_checksum;
89   }
90 
GetOatFileBegin()91   uint8_t* GetOatFileBegin() const {
92     return reinterpret_cast<uint8_t*>(oat_file_begin_);
93   }
94 
GetOatDataBegin()95   uint8_t* GetOatDataBegin() const {
96     return reinterpret_cast<uint8_t*>(oat_data_begin_);
97   }
98 
GetOatDataEnd()99   uint8_t* GetOatDataEnd() const {
100     return reinterpret_cast<uint8_t*>(oat_data_end_);
101   }
102 
GetOatFileEnd()103   uint8_t* GetOatFileEnd() const {
104     return reinterpret_cast<uint8_t*>(oat_file_end_);
105   }
106 
GetPointerSize()107   uint32_t GetPointerSize() const {
108     return pointer_size_;
109   }
110 
GetPatchDelta()111   off_t GetPatchDelta() const {
112     return patch_delta_;
113   }
114 
GetOatLocationFromImageLocation(const std::string & image)115   static std::string GetOatLocationFromImageLocation(const std::string& image) {
116     std::string oat_filename = image;
117     if (oat_filename.length() <= 3) {
118       oat_filename += ".oat";
119     } else {
120       oat_filename.replace(oat_filename.length() - 3, 3, "oat");
121     }
122     return oat_filename;
123   }
124 
125   enum ImageMethod {
126     kResolutionMethod,
127     kImtConflictMethod,
128     kImtUnimplementedMethod,
129     kCalleeSaveMethod,
130     kRefsOnlySaveMethod,
131     kRefsAndArgsSaveMethod,
132     kImageMethodsCount,  // Number of elements in enum.
133   };
134 
135   enum ImageRoot {
136     kDexCaches,
137     kClassRoots,
138     kImageRootsMax,
139   };
140 
141   enum ImageSections {
142     kSectionObjects,
143     kSectionArtFields,
144     kSectionArtMethods,
145     kSectionInternedStrings,
146     kSectionImageBitmap,
147     kSectionCount,  // Number of elements in enum.
148   };
149 
150   ArtMethod* GetImageMethod(ImageMethod index) const;
151   void SetImageMethod(ImageMethod index, ArtMethod* method);
152 
153   const ImageSection& GetImageSection(ImageSections index) const;
GetMethodsSection()154   const ImageSection& GetMethodsSection() const {
155     return GetImageSection(kSectionArtMethods);
156   }
157 
158   mirror::Object* GetImageRoot(ImageRoot image_root) const
159       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
160   mirror::ObjectArray<mirror::Object>* GetImageRoots() const
161       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
162 
163   void RelocateImage(off_t delta);
164 
CompilePic()165   bool CompilePic() const {
166     return compile_pic_ != 0;
167   }
168 
169  private:
170   static const uint8_t kImageMagic[4];
171   static const uint8_t kImageVersion[4];
172 
173   uint8_t magic_[4];
174   uint8_t version_[4];
175 
176   // Required base address for mapping the image.
177   uint32_t image_begin_;
178 
179   // Image size, not page aligned.
180   uint32_t image_size_;
181 
182   // Checksum of the oat file we link to for load time sanity check.
183   uint32_t oat_checksum_;
184 
185   // Start address for oat file. Will be before oat_data_begin_ for .so files.
186   uint32_t oat_file_begin_;
187 
188   // Required oat address expected by image Method::GetCode() pointers.
189   uint32_t oat_data_begin_;
190 
191   // End of oat data address range for this image file.
192   uint32_t oat_data_end_;
193 
194   // End of oat file address range. will be after oat_data_end_ for
195   // .so files. Used for positioning a following alloc spaces.
196   uint32_t oat_file_end_;
197 
198   // The total delta that this image has been patched.
199   int32_t patch_delta_;
200 
201   // Absolute address of an Object[] of objects needed to reinitialize from an image.
202   uint32_t image_roots_;
203 
204   // Pointer size, this affects the size of the ArtMethods.
205   uint32_t pointer_size_;
206 
207   // Boolean (0 or 1) to denote if the image was compiled with --compile-pic option
208   const uint32_t compile_pic_;
209 
210   // Image sections
211   ImageSection sections_[kSectionCount];
212 
213   // Image methods.
214   uint64_t image_methods_[kImageMethodsCount];
215 
216   friend class ImageWriter;
217 };
218 
219 std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageMethod& policy);
220 std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageRoot& policy);
221 std::ostream& operator<<(std::ostream& os, const ImageHeader::ImageSections& section);
222 std::ostream& operator<<(std::ostream& os, const ImageSection& section);
223 
224 }  // namespace art
225 
226 #endif  // ART_RUNTIME_IMAGE_H_
227