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