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 |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 20 | static std::string DexFilenameToOatFilename(const std::string& location); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 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: |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 40 | // Link Method for execution using the contents of this OatMethod |
| 41 | void LinkMethodPointers(Method* method) const; |
| 42 | |
| 43 | // Link Method for image writing using the contents of this OatMethod |
| 44 | void LinkMethodOffsets(Method* method) const; |
| 45 | |
| 46 | uint32_t GetCodeOffset() const { |
| 47 | return code_offset_; |
| 48 | } |
| 49 | size_t GetFrameSizeInBytes() const { |
| 50 | return frame_size_in_bytes_; |
| 51 | } |
| 52 | uint32_t GetCoreSpillMask() const { |
| 53 | return core_spill_mask_; |
| 54 | } |
| 55 | uint32_t GetFpSpillMask() const { |
| 56 | return fp_spill_mask_; |
| 57 | } |
| 58 | uint32_t GetMappingTableOffset() const { |
| 59 | return mapping_table_offset_; |
| 60 | } |
| 61 | uint32_t GetVmapTableOffset() const { |
| 62 | return vmap_table_offset_; |
| 63 | } |
| 64 | uint32_t GetInvokeStubOffset() const { |
| 65 | return invoke_stub_offset_; |
| 66 | } |
| 67 | |
| 68 | const void* GetCode() const { |
| 69 | return GetOatPointer<const void*>(code_offset_); |
| 70 | } |
| 71 | const uint32_t* GetMappingTable() const { |
| 72 | return GetOatPointer<const uint32_t*>(mapping_table_offset_); |
| 73 | } |
| 74 | const uint16_t* GetVmapTable() const { |
| 75 | return GetOatPointer<const uint16_t*>(vmap_table_offset_); |
| 76 | } |
| 77 | const Method::InvokeStub* GetInvokeStub() const { |
| 78 | return GetOatPointer<const Method::InvokeStub*>(invoke_stub_offset_); |
| 79 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 80 | |
| 81 | ~OatMethod(); |
| 82 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 83 | // Create an OatMethod with offsets relative to the given base address |
| 84 | OatMethod(const byte* base, |
| 85 | const uint32_t code_offset, |
| 86 | const size_t frame_size_in_bytes, |
| 87 | const uint32_t core_spill_mask, |
| 88 | const uint32_t fp_spill_mask, |
| 89 | const uint32_t mapping_table_offset, |
| 90 | const uint32_t vmap_table_offset, |
| 91 | const uint32_t invoke_stub_offset); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 92 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 93 | private: |
| 94 | template<class T> |
| 95 | T GetOatPointer(uint32_t offset) const { |
| 96 | if (offset == 0) { |
| 97 | return NULL; |
| 98 | } |
| 99 | return reinterpret_cast<T>(base_ + offset); |
| 100 | } |
| 101 | |
| 102 | const byte* base_; |
| 103 | |
| 104 | uint32_t code_offset_; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 105 | size_t frame_size_in_bytes_; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 106 | uint32_t core_spill_mask_; |
| 107 | uint32_t fp_spill_mask_; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 108 | uint32_t mapping_table_offset_; |
| 109 | uint32_t vmap_table_offset_; |
| 110 | uint32_t invoke_stub_offset_; |
| 111 | |
| 112 | friend class OatClass; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 115 | class OatClass { |
| 116 | public: |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 117 | Class::Status GetStatus() const; |
| 118 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 119 | // get the OatMethod entry based on its index into the class |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 120 | // defintion. direct methods come first, followed by virtual |
| 121 | // methods. note that runtime created methods such as miranda |
| 122 | // methods are not included. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 123 | const OatMethod GetOatMethod(uint32_t method_index) const; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 124 | ~OatClass(); |
| 125 | |
| 126 | private: |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 127 | OatClass(const OatFile* oat_file, |
| 128 | Class::Status status, |
| 129 | const OatMethodOffsets* methods_pointer); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 130 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 131 | const OatFile* oat_file_; |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 132 | const Class::Status status_; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 133 | const OatMethodOffsets* methods_pointer_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 134 | |
| 135 | friend class OatDexFile; |
| 136 | }; |
| 137 | |
| 138 | class OatDexFile { |
| 139 | public: |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 140 | const DexFile* OpenDexFile() const; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 141 | const OatClass* GetOatClass(uint32_t class_def_index) const; |
| 142 | |
| 143 | const std::string& GetDexFileLocation() const { |
| 144 | return dex_file_location_; |
| 145 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 146 | |
| 147 | uint32_t GetDexFileChecksum() const { |
| 148 | return dex_file_checksum_; |
| 149 | } |
| 150 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 151 | ~OatDexFile(); |
| 152 | private: |
| 153 | OatDexFile(const OatFile* oat_file, |
Elliott Hughes | aa6a588 | 2012-01-13 19:39:16 -0800 | [diff] [blame^] | 154 | const std::string& dex_file_location, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 155 | uint32_t dex_file_checksum, |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 156 | byte* dex_file_pointer, |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 157 | const uint32_t* oat_class_offsets_pointer); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 158 | |
| 159 | const OatFile* oat_file_; |
| 160 | std::string dex_file_location_; |
| 161 | uint32_t dex_file_checksum_; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 162 | const byte* dex_file_pointer_; |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 163 | const uint32_t* oat_class_offsets_pointer_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 164 | |
| 165 | friend class OatFile; |
| 166 | DISALLOW_COPY_AND_ASSIGN(OatDexFile); |
| 167 | }; |
| 168 | |
Ian Rogers | 7fe2c69 | 2011-12-06 16:35:59 -0800 | [diff] [blame] | 169 | const OatDexFile* GetOatDexFile(const std::string& dex_file_location, |
| 170 | bool warn_if_not_found = true) const; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 171 | std::vector<const OatDexFile*> GetOatDexFiles() const; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 172 | |
| 173 | size_t GetSize() const { |
| 174 | return GetLimit() - GetBase(); |
| 175 | } |
| 176 | |
| 177 | private: |
Elliott Hughes | a51a3dd | 2011-10-17 15:19:26 -0700 | [diff] [blame] | 178 | explicit OatFile(const std::string& filename); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 179 | bool Read(const std::string& filename, byte* requested_base); |
| 180 | |
| 181 | const byte* GetBase() const; |
| 182 | const byte* GetLimit() const; |
| 183 | |
| 184 | // The oat file name. |
| 185 | // |
| 186 | // The image will embed this to link its associated oat file. |
| 187 | const std::string location_; |
| 188 | |
| 189 | // backing memory map for oat file |
| 190 | UniquePtr<MemMap> mem_map_; |
| 191 | |
| 192 | typedef std::map<std::string, const OatDexFile*> Table; |
| 193 | Table oat_dex_files_; |
| 194 | |
| 195 | friend class OatClass; |
| 196 | friend class OatDexFile; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 197 | friend class OatDump; // For GetBase and GetLimit |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 198 | DISALLOW_COPY_AND_ASSIGN(OatFile); |
| 199 | }; |
| 200 | |
| 201 | } // namespace art |
| 202 | |
| 203 | #endif // ART_SRC_OAT_WRITER_H_ |