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