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 <array>
21 #include <vector>
22 
23 #include "base/macros.h"
24 #include "base/safe_map.h"
25 #include "compiler_filter.h"
26 
27 namespace art {
28 
29 enum class InstructionSet;
30 class InstructionSetFeatures;
31 
32 class PACKED(4) OatHeader {
33  public:
34   static constexpr std::array<uint8_t, 4> kOatMagic { { 'o', 'a', 't', '\n' } };
35   // Last oat version changed reason: Remove unused trampoline entrypoints.
36   static constexpr std::array<uint8_t, 4> kOatVersion { { '1', '7', '0', '\0' } };
37 
38   static constexpr const char* kDex2OatCmdLineKey = "dex2oat-cmdline";
39   static constexpr const char* kDebuggableKey = "debuggable";
40   static constexpr const char* kNativeDebuggableKey = "native-debuggable";
41   static constexpr const char* kCompilerFilter = "compiler-filter";
42   static constexpr const char* kClassPathKey = "classpath";
43   static constexpr const char* kBootClassPathKey = "bootclasspath";
44   static constexpr const char* kBootClassPathChecksumsKey = "bootclasspath-checksums";
45   static constexpr const char* kConcurrentCopying = "concurrent-copying";
46   static constexpr const char* kCompilationReasonKey = "compilation-reason";
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   static void CheckOatVersion(std::array<uint8_t, 4> version);
60   const char* GetMagic() const;
61   uint32_t GetChecksum() const;
62   void SetChecksum(uint32_t checksum);
GetDexFileCount()63   uint32_t GetDexFileCount() const {
64     DCHECK(IsValid());
65     return dex_file_count_;
66   }
67   uint32_t GetOatDexFilesOffset() const;
68   void SetOatDexFilesOffset(uint32_t oat_dex_files_offset);
69   uint32_t GetExecutableOffset() const;
70   void SetExecutableOffset(uint32_t executable_offset);
71 
72   const void* GetJniDlsymLookup() const;
73   uint32_t GetJniDlsymLookupOffset() const;
74   void SetJniDlsymLookupOffset(uint32_t offset);
75 
76   const void* GetQuickGenericJniTrampoline() const;
77   uint32_t GetQuickGenericJniTrampolineOffset() const;
78   void SetQuickGenericJniTrampolineOffset(uint32_t offset);
79   const void* GetQuickResolutionTrampoline() const;
80   uint32_t GetQuickResolutionTrampolineOffset() const;
81   void SetQuickResolutionTrampolineOffset(uint32_t offset);
82   const void* GetQuickImtConflictTrampoline() const;
83   uint32_t GetQuickImtConflictTrampolineOffset() const;
84   void SetQuickImtConflictTrampolineOffset(uint32_t offset);
85   const void* GetQuickToInterpreterBridge() const;
86   uint32_t GetQuickToInterpreterBridgeOffset() const;
87   void SetQuickToInterpreterBridgeOffset(uint32_t offset);
88 
89   InstructionSet GetInstructionSet() const;
90   uint32_t GetInstructionSetFeaturesBitmap() const;
91 
92   uint32_t GetKeyValueStoreSize() const;
93   const uint8_t* GetKeyValueStore() const;
94   const char* GetStoreValueByKey(const char* key) const;
95   bool GetStoreKeyValuePairByIndex(size_t index, const char** key, const char** value) const;
96 
97   size_t GetHeaderSize() const;
98   bool IsDebuggable() const;
99   bool IsNativeDebuggable() const;
100   CompilerFilter::Filter GetCompilerFilter() const;
101   bool IsConcurrentCopying() const;
102 
103  private:
104   bool KeyHasValue(const char* key, const char* value, size_t value_size) const;
105 
106   OatHeader(InstructionSet instruction_set,
107             const InstructionSetFeatures* instruction_set_features,
108             uint32_t dex_file_count,
109             const SafeMap<std::string, std::string>* variable_data);
110 
111   // Returns true if the value of the given key is "true", false otherwise.
112   bool IsKeyEnabled(const char* key) const;
113 
114   void Flatten(const SafeMap<std::string, std::string>* variable_data);
115 
116   std::array<uint8_t, 4> magic_;
117   std::array<uint8_t, 4> version_;
118   uint32_t oat_checksum_;
119 
120   InstructionSet instruction_set_;
121   uint32_t instruction_set_features_bitmap_;
122   uint32_t dex_file_count_;
123   uint32_t oat_dex_files_offset_;
124   uint32_t executable_offset_;
125   uint32_t jni_dlsym_lookup_offset_;
126   uint32_t quick_generic_jni_trampoline_offset_;
127   uint32_t quick_imt_conflict_trampoline_offset_;
128   uint32_t quick_resolution_trampoline_offset_;
129   uint32_t quick_to_interpreter_bridge_offset_;
130 
131   uint32_t key_value_store_size_;
132   uint8_t key_value_store_[0];  // note variable width data at end
133 
134   DISALLOW_COPY_AND_ASSIGN(OatHeader);
135 };
136 
137 // OatMethodOffsets are currently 5x32-bits=160-bits long, so if we can
138 // save even one OatMethodOffsets struct, the more complicated encoding
139 // using a bitmap pays for itself since few classes will have 160
140 // methods.
141 enum OatClassType {
142   kOatClassAllCompiled = 0,   // OatClass is followed by an OatMethodOffsets for each method.
143   kOatClassSomeCompiled = 1,  // A bitmap of which OatMethodOffsets are present follows the OatClass.
144   kOatClassNoneCompiled = 2,  // All methods are interpreted so no OatMethodOffsets are necessary.
145   kOatClassMax = 3,
146 };
147 
148 std::ostream& operator<<(std::ostream& os, const OatClassType& rhs);
149 
150 class PACKED(4) OatMethodOffsets {
151  public:
152   explicit OatMethodOffsets(uint32_t code_offset = 0);
153 
154   ~OatMethodOffsets();
155 
156   OatMethodOffsets(const OatMethodOffsets&) = default;
157   OatMethodOffsets& operator=(const OatMethodOffsets&) = default;
158 
159   uint32_t code_offset_;
160 };
161 
162 }  // namespace art
163 
164 #endif  // ART_RUNTIME_OAT_H_
165