12faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes/*
22faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Copyright (C) 2011 The Android Open Source Project
32faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
42faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
52faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * you may not use this file except in compliance with the License.
62faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * You may obtain a copy of the License at
72faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
82faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
92faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
102faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Unless required by applicable law or agreed to in writing, software
112faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
122faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * See the License for the specific language governing permissions and
142faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * limitations under the License.
152faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes */
16e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
17fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#ifndef ART_RUNTIME_OAT_H_
18fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#define ART_RUNTIME_OAT_H_
19e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
20e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom#include <vector>
21e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
22761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/macros.h"
23e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom#include "dex_file.h"
240f3c55331439970e01af67f80ac117c473bc04cfElliott Hughes#include "instruction_set.h"
257624d25dad2d1ba25969ae704fccf68649103ae5Vladimir Marko#include "quick/quick_method_frame_info.h"
2622f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe#include "safe_map.h"
27e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
28e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstromnamespace art {
29e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
30df1ce91ba97bc79a0637e5504b39318fb1c9f577Ian Rogersclass PACKED(4) OatHeader {
31e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom public:
327c3d13aebdd8611cae58a1048bffb13cbdc465cbBrian Carlstrom  static const uint8_t kOatMagic[4];
337c3d13aebdd8611cae58a1048bffb13cbdc465cbBrian Carlstrom  static const uint8_t kOatVersion[4];
347c3d13aebdd8611cae58a1048bffb13cbdc465cbBrian Carlstrom
3522f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe  static constexpr const char* kImageLocationKey = "image-location";
3622f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe  static constexpr const char* kDex2OatCmdLineKey = "dex2oat-cmdline";
3722f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe  static constexpr const char* kDex2OatHostKey = "dex2oat-host";
38d7392faea80acb5d73a027bb384e3222bc2c2e43Andreas Gampe  static constexpr const char* kPicKey = "pic";
3922f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe
4022f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe  static OatHeader* Create(InstructionSet instruction_set,
4122f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe                           const InstructionSetFeatures& instruction_set_features,
4222f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe                           const std::vector<const DexFile*>* dex_files,
4322f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe                           uint32_t image_file_location_oat_checksum,
4422f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe                           uint32_t image_file_location_oat_data_begin,
4522f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe                           const SafeMap<std::string, std::string>* variable_data);
46e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
47e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  bool IsValid() const;
48e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  const char* GetMagic() const;
49e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  uint32_t GetChecksum() const;
50e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  void UpdateChecksum(const void* data, size_t length);
514f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers  uint32_t GetDexFileCount() const {
524f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers    DCHECK(IsValid());
534f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers    return dex_file_count_;
544f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers  }
55e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  uint32_t GetExecutableOffset() const;
56e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  void SetExecutableOffset(uint32_t executable_offset);
57468532ea115657709bc32ee498e701a4c71762d4Ian Rogers
58468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  const void* GetInterpreterToInterpreterBridge() const;
59468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  uint32_t GetInterpreterToInterpreterBridgeOffset() const;
60468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  void SetInterpreterToInterpreterBridgeOffset(uint32_t offset);
61468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  const void* GetInterpreterToCompiledCodeBridge() const;
62468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  uint32_t GetInterpreterToCompiledCodeBridgeOffset() const;
63468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  void SetInterpreterToCompiledCodeBridgeOffset(uint32_t offset);
64468532ea115657709bc32ee498e701a4c71762d4Ian Rogers
65468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  const void* GetJniDlsymLookup() const;
66468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  uint32_t GetJniDlsymLookupOffset() const;
67468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  void SetJniDlsymLookupOffset(uint32_t offset);
68468532ea115657709bc32ee498e701a4c71762d4Ian Rogers
690aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  const void* GetPortableResolutionTrampoline() const;
700aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  uint32_t GetPortableResolutionTrampolineOffset() const;
710aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  void SetPortableResolutionTrampolineOffset(uint32_t offset);
7288474b416eb257078e590bf9bc7957cee604a186Jeff Hao  const void* GetPortableImtConflictTrampoline() const;
7388474b416eb257078e590bf9bc7957cee604a186Jeff Hao  uint32_t GetPortableImtConflictTrampolineOffset() const;
7488474b416eb257078e590bf9bc7957cee604a186Jeff Hao  void SetPortableImtConflictTrampolineOffset(uint32_t offset);
75468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  const void* GetPortableToInterpreterBridge() const;
76468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  uint32_t GetPortableToInterpreterBridgeOffset() const;
77468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  void SetPortableToInterpreterBridgeOffset(uint32_t offset);
78468532ea115657709bc32ee498e701a4c71762d4Ian Rogers
792da882315a61072664f7ce3c212307342e907207Andreas Gampe  const void* GetQuickGenericJniTrampoline() const;
802da882315a61072664f7ce3c212307342e907207Andreas Gampe  uint32_t GetQuickGenericJniTrampolineOffset() const;
812da882315a61072664f7ce3c212307342e907207Andreas Gampe  void SetQuickGenericJniTrampolineOffset(uint32_t offset);
820aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  const void* GetQuickResolutionTrampoline() const;
830aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  uint32_t GetQuickResolutionTrampolineOffset() const;
840aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  void SetQuickResolutionTrampolineOffset(uint32_t offset);
8588474b416eb257078e590bf9bc7957cee604a186Jeff Hao  const void* GetQuickImtConflictTrampoline() const;
8688474b416eb257078e590bf9bc7957cee604a186Jeff Hao  uint32_t GetQuickImtConflictTrampolineOffset() const;
8788474b416eb257078e590bf9bc7957cee604a186Jeff Hao  void SetQuickImtConflictTrampolineOffset(uint32_t offset);
88468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  const void* GetQuickToInterpreterBridge() const;
89468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  uint32_t GetQuickToInterpreterBridgeOffset() const;
90468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  void SetQuickToInterpreterBridgeOffset(uint32_t offset);
91468532ea115657709bc32ee498e701a4c71762d4Ian Rogers
92a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  int32_t GetImagePatchDelta() const;
93a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  void RelocateOat(off_t delta);
94a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  void SetImagePatchDelta(int32_t off);
95a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
960aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  InstructionSet GetInstructionSet() const;
977020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison  const InstructionSetFeatures& GetInstructionSetFeatures() const;
9828db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom  uint32_t GetImageFileLocationOatChecksum() const;
99700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  uint32_t GetImageFileLocationOatDataBegin() const;
10022f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe
10122f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe  uint32_t GetKeyValueStoreSize() const;
10222f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe  const uint8_t* GetKeyValueStore() const;
10322f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe  const char* GetStoreValueByKey(const char* key) const;
10422f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe  bool GetStoreKeyValuePairByIndex(size_t index, const char** key, const char** value) const;
10522f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe
10622f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe  size_t GetHeaderSize() const;
10790ca5c0301651101de0e363842e5d08ae65233f7Igor Murashkin  bool IsPic() const;
108e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
109e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom private:
11022f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe  OatHeader(InstructionSet instruction_set,
11122f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe            const InstructionSetFeatures& instruction_set_features,
11222f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe            const std::vector<const DexFile*>* dex_files,
11322f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe            uint32_t image_file_location_oat_checksum,
11422f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe            uint32_t image_file_location_oat_data_begin,
11522f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe            const SafeMap<std::string, std::string>* variable_data);
11622f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe
11722f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe  void Flatten(const SafeMap<std::string, std::string>* variable_data);
11822f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe
119e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  uint8_t magic_[4];
120e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  uint8_t version_[4];
121e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  uint32_t adler32_checksum_;
122a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes
123a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes  InstructionSet instruction_set_;
1247020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison  InstructionSetFeatures instruction_set_features_;
125e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  uint32_t dex_file_count_;
126e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  uint32_t executable_offset_;
127468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  uint32_t interpreter_to_interpreter_bridge_offset_;
128468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  uint32_t interpreter_to_compiled_code_bridge_offset_;
129468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  uint32_t jni_dlsym_lookup_offset_;
13088474b416eb257078e590bf9bc7957cee604a186Jeff Hao  uint32_t portable_imt_conflict_trampoline_offset_;
1310aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  uint32_t portable_resolution_trampoline_offset_;
132468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  uint32_t portable_to_interpreter_bridge_offset_;
1332da882315a61072664f7ce3c212307342e907207Andreas Gampe  uint32_t quick_generic_jni_trampoline_offset_;
13488474b416eb257078e590bf9bc7957cee604a186Jeff Hao  uint32_t quick_imt_conflict_trampoline_offset_;
1350aba0ba155bef7346bde19e53581200b89ae8a7aJeff Hao  uint32_t quick_resolution_trampoline_offset_;
136468532ea115657709bc32ee498e701a4c71762d4Ian Rogers  uint32_t quick_to_interpreter_bridge_offset_;
137e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
138a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  // The amount that the image this oat is associated with has been patched.
139a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  int32_t image_patch_delta_;
140a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
14128db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom  uint32_t image_file_location_oat_checksum_;
142700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  uint32_t image_file_location_oat_data_begin_;
14322f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe
14422f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe  uint32_t key_value_store_size_;
14522f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe  uint8_t key_value_store_[0];  // note variable width data at end
14681f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom
147e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  DISALLOW_COPY_AND_ASSIGN(OatHeader);
148e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom};
149e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
1508a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko// OatMethodOffsets are currently 5x32-bits=160-bits long, so if we can
151ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom// save even one OatMethodOffsets struct, the more complicated encoding
1528a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko// using a bitmap pays for itself since few classes will have 160
153ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom// methods.
154ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstromenum OatClassType {
155ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom  kOatClassAllCompiled = 0,   // OatClass is followed by an OatMethodOffsets for each method.
156ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom  kOatClassSomeCompiled = 1,  // A bitmap of which OatMethodOffsets are present follows the OatClass.
157ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom  kOatClassNoneCompiled = 2,  // All methods are interpretted so no OatMethodOffsets are necessary.
158ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom  kOatClassMax = 3,
159ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom};
160ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom
161ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstromstd::ostream& operator<<(std::ostream& os, const OatClassType& rhs);
162ba150c37d582eeeb8c11ba5245edc281cf31793cBrian Carlstrom
163df1ce91ba97bc79a0637e5504b39318fb1c9f577Ian Rogersclass PACKED(4) OatMethodOffsets {
1643320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom public:
165c6fc909c6b42b4c365b1e7f86639ee4b360249faMathieu Chartier  OatMethodOffsets(uint32_t code_offset = 0);
166971bf3f9184010d68b9a3ad30b396fa401af91a3Logan Chien
1673320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom  ~OatMethodOffsets();
1683320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom
1693320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom  uint32_t code_offset_;
1703320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom};
1713320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom
1727624d25dad2d1ba25969ae704fccf68649103ae5Vladimir Marko// OatQuickMethodHeader precedes the raw code chunk generated by the Quick compiler.
1737624d25dad2d1ba25969ae704fccf68649103ae5Vladimir Markoclass PACKED(4) OatQuickMethodHeader {
17496c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko public:
175c6fc909c6b42b4c365b1e7f86639ee4b360249faMathieu Chartier  OatQuickMethodHeader(uint32_t mapping_table_offset = 0U, uint32_t vmap_table_offset = 0U,
176c6fc909c6b42b4c365b1e7f86639ee4b360249faMathieu Chartier                       uint32_t gc_map_offset = 0U, uint32_t frame_size_in_bytes = 0U,
177c6fc909c6b42b4c365b1e7f86639ee4b360249faMathieu Chartier                       uint32_t core_spill_mask = 0U, uint32_t fp_spill_mask = 0U,
178c6fc909c6b42b4c365b1e7f86639ee4b360249faMathieu Chartier                       uint32_t code_size = 0U);
17996c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko
1807624d25dad2d1ba25969ae704fccf68649103ae5Vladimir Marko  ~OatQuickMethodHeader();
18196c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko
1828a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko  // The offset in bytes from the start of the mapping table to the end of the header.
1838a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko  uint32_t mapping_table_offset_;
1848a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko  // The offset in bytes from the start of the vmap table to the end of the header.
1858a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko  uint32_t vmap_table_offset_;
186c6fc909c6b42b4c365b1e7f86639ee4b360249faMathieu Chartier  // The offset in bytes from the start of the gc map to the end of the header.
187c6fc909c6b42b4c365b1e7f86639ee4b360249faMathieu Chartier  uint32_t gc_map_offset_;
1887624d25dad2d1ba25969ae704fccf68649103ae5Vladimir Marko  // The stack frame information.
1897624d25dad2d1ba25969ae704fccf68649103ae5Vladimir Marko  QuickMethodFrameInfo frame_info_;
19096c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  // The code size in bytes.
19196c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko  uint32_t code_size_;
19296c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko};
19396c6ab93336b972a38bd2448bcccf19188b8389bVladimir Marko
194e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom}  // namespace art
195e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
196fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_OAT_H_
197