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