Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_OAT_FILE_H_ |
| 4 | #define ART_SRC_OAT_FILE_H_ |
| 5 | |
| 6 | #include <vector> |
| 7 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8 | #include "constants.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 9 | #include "dex_file.h" |
| 10 | #include "mem_map.h" |
| 11 | #include "oat.h" |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 12 | #include "object.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 13 | |
| 14 | namespace art { |
| 15 | |
| 16 | class OatFile { |
| 17 | public: |
| 18 | |
| 19 | // Open an oat file. Returns NULL on failure. Requested base can |
| 20 | // optionally be used to request where the file should be loaded. |
| 21 | static OatFile* Open(const std::string& filename, |
| 22 | const std::string& strip_location_prefix, |
| 23 | byte* requested_base); |
| 24 | |
| 25 | ~OatFile(); |
| 26 | |
| 27 | const std::string& GetLocation() const { |
| 28 | return location_; |
| 29 | } |
| 30 | |
| 31 | const OatHeader& GetOatHeader() const; |
| 32 | |
| 33 | class OatDexFile; |
| 34 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 35 | class OatMethod { |
| 36 | public: |
| 37 | // Create an OatMethod backed by an OatFile |
| 38 | OatMethod(const void* code, |
| 39 | const size_t frame_size_in_bytes, |
| 40 | const size_t return_pc_offset_in_bytes, |
| 41 | const uint32_t core_spill_mask, |
| 42 | const uint32_t fp_spill_mask, |
| 43 | const uint32_t* mapping_table, |
| 44 | const uint16_t* vmap_table, |
| 45 | const Method::InvokeStub* invoke_stub); |
| 46 | |
| 47 | ~OatMethod(); |
| 48 | |
| 49 | // Link Method using the contents of this OatMethod |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame^] | 50 | void LinkMethod(Method* method) const; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 51 | |
| 52 | const void* code_; |
| 53 | size_t frame_size_in_bytes_; |
| 54 | size_t return_pc_offset_in_bytes_; |
| 55 | uint32_t core_spill_mask_; |
| 56 | uint32_t fp_spill_mask_; |
| 57 | const uint32_t* mapping_table_; |
| 58 | const uint16_t* vmap_table_; |
| 59 | const Method::InvokeStub* invoke_stub_; |
| 60 | }; |
| 61 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 62 | class OatClass { |
| 63 | public: |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 64 | // get the OatMethod entry based on its index into the class |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 65 | // defintion. direct methods come first, followed by virtual |
| 66 | // methods. note that runtime created methods such as miranda |
| 67 | // methods are not included. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame^] | 68 | const OatMethod GetOatMethod(uint32_t method_index) const; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 69 | ~OatClass(); |
| 70 | |
| 71 | private: |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 72 | OatClass(const OatFile* oat_file, const OatMethodOffsets* methods_pointer); |
| 73 | |
| 74 | template<class T> |
| 75 | T GetOatPointer(uint32_t offset) const { |
| 76 | if (offset == 0) { |
| 77 | return NULL; |
| 78 | } |
| 79 | T pointer = reinterpret_cast<T>(oat_file_->GetBase() + offset); |
| 80 | CHECK_LT(pointer, reinterpret_cast<T>(oat_file_->GetLimit())); |
| 81 | return pointer; |
| 82 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 83 | |
| 84 | const OatFile* oat_file_; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 85 | const OatMethodOffsets* methods_pointer_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 86 | |
| 87 | friend class OatDexFile; |
| 88 | }; |
| 89 | |
| 90 | class OatDexFile { |
| 91 | public: |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame^] | 92 | const OatClass* GetOatClass(uint32_t class_def_index) const; |
| 93 | |
| 94 | const std::string& GetDexFileLocation() const { |
| 95 | return dex_file_location_; |
| 96 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 97 | |
| 98 | uint32_t GetDexFileChecksum() const { |
| 99 | return dex_file_checksum_; |
| 100 | } |
| 101 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 102 | ~OatDexFile(); |
| 103 | private: |
| 104 | OatDexFile(const OatFile* oat_file, |
| 105 | std::string dex_file_location, |
| 106 | uint32_t dex_file_checksum, |
| 107 | uint32_t* classes_pointer); |
| 108 | |
| 109 | const OatFile* oat_file_; |
| 110 | std::string dex_file_location_; |
| 111 | uint32_t dex_file_checksum_; |
| 112 | const uint32_t* classes_pointer_; |
| 113 | |
| 114 | friend class OatFile; |
| 115 | DISALLOW_COPY_AND_ASSIGN(OatDexFile); |
| 116 | }; |
| 117 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame^] | 118 | const OatDexFile* GetOatDexFile(const std::string& dex_file_location) const; |
| 119 | std::vector<const OatDexFile*> GetOatDexFiles() const; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 120 | |
| 121 | size_t GetSize() const { |
| 122 | return GetLimit() - GetBase(); |
| 123 | } |
| 124 | |
| 125 | private: |
| 126 | OatFile(const std::string& filename); |
| 127 | bool Read(const std::string& filename, byte* requested_base); |
| 128 | |
| 129 | const byte* GetBase() const; |
| 130 | const byte* GetLimit() const; |
| 131 | |
| 132 | // The oat file name. |
| 133 | // |
| 134 | // The image will embed this to link its associated oat file. |
| 135 | const std::string location_; |
| 136 | |
| 137 | // backing memory map for oat file |
| 138 | UniquePtr<MemMap> mem_map_; |
| 139 | |
| 140 | typedef std::map<std::string, const OatDexFile*> Table; |
| 141 | Table oat_dex_files_; |
| 142 | |
| 143 | friend class OatClass; |
| 144 | friend class OatDexFile; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame^] | 145 | friend class OatDump; // For GetBase and GetLimit |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 146 | DISALLOW_COPY_AND_ASSIGN(OatFile); |
| 147 | }; |
| 148 | |
| 149 | } // namespace art |
| 150 | |
| 151 | #endif // ART_SRC_OAT_WRITER_H_ |