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