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