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