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_OAT_H_
18 #define ART_RUNTIME_OAT_H_
19 
20 #include <vector>
21 
22 #include "base/macros.h"
23 #include "dex_file.h"
24 #include "instruction_set.h"
25 #include "quick/quick_method_frame_info.h"
26 #include "safe_map.h"
27 
28 namespace art {
29 
30 class PACKED(4) OatHeader {
31  public:
32   static const uint8_t kOatMagic[4];
33   static const uint8_t kOatVersion[4];
34 
35   static constexpr const char* kImageLocationKey = "image-location";
36   static constexpr const char* kDex2OatCmdLineKey = "dex2oat-cmdline";
37   static constexpr const char* kDex2OatHostKey = "dex2oat-host";
38   static constexpr const char* kPicKey = "pic";
39 
40   static OatHeader* Create(InstructionSet instruction_set,
41                            const InstructionSetFeatures& instruction_set_features,
42                            const std::vector<const DexFile*>* dex_files,
43                            uint32_t image_file_location_oat_checksum,
44                            uint32_t image_file_location_oat_data_begin,
45                            const SafeMap<std::string, std::string>* variable_data);
46 
47   bool IsValid() const;
48   const char* GetMagic() const;
49   uint32_t GetChecksum() const;
50   void UpdateChecksum(const void* data, size_t length);
GetDexFileCount()51   uint32_t GetDexFileCount() const {
52     DCHECK(IsValid());
53     return dex_file_count_;
54   }
55   uint32_t GetExecutableOffset() const;
56   void SetExecutableOffset(uint32_t executable_offset);
57 
58   const void* GetInterpreterToInterpreterBridge() const;
59   uint32_t GetInterpreterToInterpreterBridgeOffset() const;
60   void SetInterpreterToInterpreterBridgeOffset(uint32_t offset);
61   const void* GetInterpreterToCompiledCodeBridge() const;
62   uint32_t GetInterpreterToCompiledCodeBridgeOffset() const;
63   void SetInterpreterToCompiledCodeBridgeOffset(uint32_t offset);
64 
65   const void* GetJniDlsymLookup() const;
66   uint32_t GetJniDlsymLookupOffset() const;
67   void SetJniDlsymLookupOffset(uint32_t offset);
68 
69   const void* GetPortableResolutionTrampoline() const;
70   uint32_t GetPortableResolutionTrampolineOffset() const;
71   void SetPortableResolutionTrampolineOffset(uint32_t offset);
72   const void* GetPortableImtConflictTrampoline() const;
73   uint32_t GetPortableImtConflictTrampolineOffset() const;
74   void SetPortableImtConflictTrampolineOffset(uint32_t offset);
75   const void* GetPortableToInterpreterBridge() const;
76   uint32_t GetPortableToInterpreterBridgeOffset() const;
77   void SetPortableToInterpreterBridgeOffset(uint32_t offset);
78 
79   const void* GetQuickGenericJniTrampoline() const;
80   uint32_t GetQuickGenericJniTrampolineOffset() const;
81   void SetQuickGenericJniTrampolineOffset(uint32_t offset);
82   const void* GetQuickResolutionTrampoline() const;
83   uint32_t GetQuickResolutionTrampolineOffset() const;
84   void SetQuickResolutionTrampolineOffset(uint32_t offset);
85   const void* GetQuickImtConflictTrampoline() const;
86   uint32_t GetQuickImtConflictTrampolineOffset() const;
87   void SetQuickImtConflictTrampolineOffset(uint32_t offset);
88   const void* GetQuickToInterpreterBridge() const;
89   uint32_t GetQuickToInterpreterBridgeOffset() const;
90   void SetQuickToInterpreterBridgeOffset(uint32_t offset);
91 
92   int32_t GetImagePatchDelta() const;
93   void RelocateOat(off_t delta);
94   void SetImagePatchDelta(int32_t off);
95 
96   InstructionSet GetInstructionSet() const;
97   const InstructionSetFeatures& GetInstructionSetFeatures() const;
98   uint32_t GetImageFileLocationOatChecksum() const;
99   uint32_t GetImageFileLocationOatDataBegin() const;
100 
101   uint32_t GetKeyValueStoreSize() const;
102   const uint8_t* GetKeyValueStore() const;
103   const char* GetStoreValueByKey(const char* key) const;
104   bool GetStoreKeyValuePairByIndex(size_t index, const char** key, const char** value) const;
105 
106   size_t GetHeaderSize() const;
107   bool IsPic() const;
108 
109  private:
110   OatHeader(InstructionSet instruction_set,
111             const InstructionSetFeatures& instruction_set_features,
112             const std::vector<const DexFile*>* dex_files,
113             uint32_t image_file_location_oat_checksum,
114             uint32_t image_file_location_oat_data_begin,
115             const SafeMap<std::string, std::string>* variable_data);
116 
117   void Flatten(const SafeMap<std::string, std::string>* variable_data);
118 
119   uint8_t magic_[4];
120   uint8_t version_[4];
121   uint32_t adler32_checksum_;
122 
123   InstructionSet instruction_set_;
124   InstructionSetFeatures instruction_set_features_;
125   uint32_t dex_file_count_;
126   uint32_t executable_offset_;
127   uint32_t interpreter_to_interpreter_bridge_offset_;
128   uint32_t interpreter_to_compiled_code_bridge_offset_;
129   uint32_t jni_dlsym_lookup_offset_;
130   uint32_t portable_imt_conflict_trampoline_offset_;
131   uint32_t portable_resolution_trampoline_offset_;
132   uint32_t portable_to_interpreter_bridge_offset_;
133   uint32_t quick_generic_jni_trampoline_offset_;
134   uint32_t quick_imt_conflict_trampoline_offset_;
135   uint32_t quick_resolution_trampoline_offset_;
136   uint32_t quick_to_interpreter_bridge_offset_;
137 
138   // The amount that the image this oat is associated with has been patched.
139   int32_t image_patch_delta_;
140 
141   uint32_t image_file_location_oat_checksum_;
142   uint32_t image_file_location_oat_data_begin_;
143 
144   uint32_t key_value_store_size_;
145   uint8_t key_value_store_[0];  // note variable width data at end
146 
147   DISALLOW_COPY_AND_ASSIGN(OatHeader);
148 };
149 
150 // OatMethodOffsets are currently 5x32-bits=160-bits long, so if we can
151 // save even one OatMethodOffsets struct, the more complicated encoding
152 // using a bitmap pays for itself since few classes will have 160
153 // methods.
154 enum OatClassType {
155   kOatClassAllCompiled = 0,   // OatClass is followed by an OatMethodOffsets for each method.
156   kOatClassSomeCompiled = 1,  // A bitmap of which OatMethodOffsets are present follows the OatClass.
157   kOatClassNoneCompiled = 2,  // All methods are interpretted so no OatMethodOffsets are necessary.
158   kOatClassMax = 3,
159 };
160 
161 std::ostream& operator<<(std::ostream& os, const OatClassType& rhs);
162 
163 class PACKED(4) OatMethodOffsets {
164  public:
165   OatMethodOffsets(uint32_t code_offset = 0);
166 
167   ~OatMethodOffsets();
168 
169   uint32_t code_offset_;
170 };
171 
172 // OatQuickMethodHeader precedes the raw code chunk generated by the Quick compiler.
173 class PACKED(4) OatQuickMethodHeader {
174  public:
175   OatQuickMethodHeader(uint32_t mapping_table_offset = 0U, uint32_t vmap_table_offset = 0U,
176                        uint32_t gc_map_offset = 0U, uint32_t frame_size_in_bytes = 0U,
177                        uint32_t core_spill_mask = 0U, uint32_t fp_spill_mask = 0U,
178                        uint32_t code_size = 0U);
179 
180   ~OatQuickMethodHeader();
181 
182   // The offset in bytes from the start of the mapping table to the end of the header.
183   uint32_t mapping_table_offset_;
184   // The offset in bytes from the start of the vmap table to the end of the header.
185   uint32_t vmap_table_offset_;
186   // The offset in bytes from the start of the gc map to the end of the header.
187   uint32_t gc_map_offset_;
188   // The stack frame information.
189   QuickMethodFrameInfo frame_info_;
190   // The code size in bytes.
191   uint32_t code_size_;
192 };
193 
194 }  // namespace art
195 
196 #endif  // ART_RUNTIME_OAT_H_
197