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 "arch/instruction_set.h"
23 #include "base/macros.h"
24 #include "compiler_filter.h"
25 #include "dex_file.h"
26 #include "safe_map.h"
27 
28 namespace art {
29 
30 class InstructionSetFeatures;
31 
32 class PACKED(4) OatHeader {
33  public:
34   static constexpr uint8_t kOatMagic[] = { 'o', 'a', 't', '\n' };
35   static constexpr uint8_t kOatVersion[] = { '1', '2', '4', '\0' };  // New compiler filter names.
36 
37   static constexpr const char* kImageLocationKey = "image-location";
38   static constexpr const char* kDex2OatCmdLineKey = "dex2oat-cmdline";
39   static constexpr const char* kDex2OatHostKey = "dex2oat-host";
40   static constexpr const char* kPicKey = "pic";
41   static constexpr const char* kDebuggableKey = "debuggable";
42   static constexpr const char* kNativeDebuggableKey = "native-debuggable";
43   static constexpr const char* kCompilerFilter = "compiler-filter";
44   static constexpr const char* kClassPathKey = "classpath";
45   static constexpr const char* kBootClassPathKey = "bootclasspath";
46   static constexpr const char* kConcurrentCopying = "concurrent-copying";
47 
48   static constexpr const char kTrueValue[] = "true";
49   static constexpr const char kFalseValue[] = "false";
50 
51 
52   static OatHeader* Create(InstructionSet instruction_set,
53                            const InstructionSetFeatures* instruction_set_features,
54                            uint32_t dex_file_count,
55                            const SafeMap<std::string, std::string>* variable_data);
56 
57   bool IsValid() const;
58   std::string GetValidationErrorMessage() const;
59   const char* GetMagic() const;
60   uint32_t GetChecksum() const;
61   void UpdateChecksumWithHeaderData();
62   void UpdateChecksum(const void* data, size_t length);
GetDexFileCount()63   uint32_t GetDexFileCount() const {
64     DCHECK(IsValid());
65     return dex_file_count_;
66   }
67   uint32_t GetExecutableOffset() const;
68   void SetExecutableOffset(uint32_t executable_offset);
69 
70   const void* GetInterpreterToInterpreterBridge() const;
71   uint32_t GetInterpreterToInterpreterBridgeOffset() const;
72   void SetInterpreterToInterpreterBridgeOffset(uint32_t offset);
73   const void* GetInterpreterToCompiledCodeBridge() const;
74   uint32_t GetInterpreterToCompiledCodeBridgeOffset() const;
75   void SetInterpreterToCompiledCodeBridgeOffset(uint32_t offset);
76 
77   const void* GetJniDlsymLookup() const;
78   uint32_t GetJniDlsymLookupOffset() const;
79   void SetJniDlsymLookupOffset(uint32_t offset);
80 
81   const void* GetQuickGenericJniTrampoline() const;
82   uint32_t GetQuickGenericJniTrampolineOffset() const;
83   void SetQuickGenericJniTrampolineOffset(uint32_t offset);
84   const void* GetQuickResolutionTrampoline() const;
85   uint32_t GetQuickResolutionTrampolineOffset() const;
86   void SetQuickResolutionTrampolineOffset(uint32_t offset);
87   const void* GetQuickImtConflictTrampoline() const;
88   uint32_t GetQuickImtConflictTrampolineOffset() const;
89   void SetQuickImtConflictTrampolineOffset(uint32_t offset);
90   const void* GetQuickToInterpreterBridge() const;
91   uint32_t GetQuickToInterpreterBridgeOffset() const;
92   void SetQuickToInterpreterBridgeOffset(uint32_t offset);
93 
94   int32_t GetImagePatchDelta() const;
95   void RelocateOat(off_t delta);
96   void SetImagePatchDelta(int32_t off);
97 
98   InstructionSet GetInstructionSet() const;
99   uint32_t GetInstructionSetFeaturesBitmap() const;
100 
101   uint32_t GetImageFileLocationOatChecksum() const;
102   void SetImageFileLocationOatChecksum(uint32_t image_file_location_oat_checksum);
103   uint32_t GetImageFileLocationOatDataBegin() const;
104   void SetImageFileLocationOatDataBegin(uint32_t image_file_location_oat_data_begin);
105 
106   uint32_t GetKeyValueStoreSize() const;
107   const uint8_t* GetKeyValueStore() const;
108   const char* GetStoreValueByKey(const char* key) const;
109   bool GetStoreKeyValuePairByIndex(size_t index, const char** key, const char** value) const;
110 
111   size_t GetHeaderSize() const;
112   bool IsPic() const;
113   bool IsDebuggable() const;
114   bool IsNativeDebuggable() const;
115   CompilerFilter::Filter GetCompilerFilter() const;
116   bool IsConcurrentCopying() const;
117 
118  private:
119   bool KeyHasValue(const char* key, const char* value, size_t value_size) const;
120 
121   OatHeader(InstructionSet instruction_set,
122             const InstructionSetFeatures* instruction_set_features,
123             uint32_t dex_file_count,
124             const SafeMap<std::string, std::string>* variable_data);
125 
126   // Returns true if the value of the given key is "true", false otherwise.
127   bool IsKeyEnabled(const char* key) const;
128 
129   void Flatten(const SafeMap<std::string, std::string>* variable_data);
130 
131   uint8_t magic_[4];
132   uint8_t version_[4];
133   uint32_t adler32_checksum_;
134 
135   InstructionSet instruction_set_;
136   uint32_t instruction_set_features_bitmap_;
137   uint32_t dex_file_count_;
138   uint32_t executable_offset_;
139   uint32_t interpreter_to_interpreter_bridge_offset_;
140   uint32_t interpreter_to_compiled_code_bridge_offset_;
141   uint32_t jni_dlsym_lookup_offset_;
142   uint32_t quick_generic_jni_trampoline_offset_;
143   uint32_t quick_imt_conflict_trampoline_offset_;
144   uint32_t quick_resolution_trampoline_offset_;
145   uint32_t quick_to_interpreter_bridge_offset_;
146 
147   // The amount that the image this oat is associated with has been patched.
148   int32_t image_patch_delta_;
149 
150   uint32_t image_file_location_oat_checksum_;
151   uint32_t image_file_location_oat_data_begin_;
152 
153   uint32_t key_value_store_size_;
154   uint8_t key_value_store_[0];  // note variable width data at end
155 
156   DISALLOW_COPY_AND_ASSIGN(OatHeader);
157 };
158 
159 // OatMethodOffsets are currently 5x32-bits=160-bits long, so if we can
160 // save even one OatMethodOffsets struct, the more complicated encoding
161 // using a bitmap pays for itself since few classes will have 160
162 // methods.
163 enum OatClassType {
164   kOatClassAllCompiled = 0,   // OatClass is followed by an OatMethodOffsets for each method.
165   kOatClassSomeCompiled = 1,  // A bitmap of which OatMethodOffsets are present follows the OatClass.
166   kOatClassNoneCompiled = 2,  // All methods are interpreted so no OatMethodOffsets are necessary.
167   kOatClassMax = 3,
168 };
169 
170 std::ostream& operator<<(std::ostream& os, const OatClassType& rhs);
171 
172 class PACKED(4) OatMethodOffsets {
173  public:
174   explicit OatMethodOffsets(uint32_t code_offset = 0);
175 
176   ~OatMethodOffsets();
177 
178   OatMethodOffsets& operator=(const OatMethodOffsets&) = default;
179 
180   uint32_t code_offset_;
181 };
182 
183 }  // namespace art
184 
185 #endif  // ART_RUNTIME_OAT_H_
186