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 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 20 | #include <string> |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 23 | #include "dex_file.h" |
| 24 | #include "invoke_type.h" |
| 25 | #include "mem_map.h" |
| 26 | #include "mirror/abstract_method.h" |
| 27 | #include "oat.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 28 | #include "os.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 32 | class ElfFile; |
| 33 | class MemMap; |
| 34 | class OatMethodOffsets; |
| 35 | struct OatHeader; |
| 36 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 37 | class OatFile { |
| 38 | public: |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 39 | // Returns an OatFile name based on a DexFile location |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 40 | static std::string DexFilenameToOatFilename(const std::string& location); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 41 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 42 | // Open an oat file. Returns NULL on failure. Requested base can |
| 43 | // optionally be used to request where the file should be loaded. |
| 44 | static OatFile* Open(const std::string& filename, |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 45 | const std::string& location, |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 46 | byte* requested_base); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 47 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 48 | // Open an oat file from an already opened File. |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 49 | // Does not use dlopen underneath so cannot be used for runtime use |
| 50 | // where relocations may be required. Currently used from |
| 51 | // ImageWriter which wants to open a writable version from an existing |
| 52 | // file descriptor for patching. |
| 53 | static OatFile* OpenWritable(File* file, const std::string& location); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 54 | |
| 55 | // Open an oat file backed by a std::vector with the given location. |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 56 | static OatFile* OpenMemory(std::vector<uint8_t>& oat_contents, |
| 57 | const std::string& location); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 58 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 59 | ~OatFile(); |
| 60 | |
| 61 | const std::string& GetLocation() const { |
| 62 | return location_; |
| 63 | } |
| 64 | |
| 65 | const OatHeader& GetOatHeader() const; |
| 66 | |
| 67 | class OatDexFile; |
| 68 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 69 | class OatMethod { |
| 70 | public: |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 71 | void LinkMethod(mirror::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 | |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 95 | const void* GetCode() const; |
| 96 | uint32_t GetCodeSize() const; |
| 97 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 98 | const uint32_t* GetMappingTable() const { |
| 99 | return GetOatPointer<const uint32_t*>(mapping_table_offset_); |
| 100 | } |
| 101 | const uint16_t* GetVmapTable() const { |
| 102 | return GetOatPointer<const uint16_t*>(vmap_table_offset_); |
| 103 | } |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 104 | const uint8_t* GetNativeGcMap() const { |
| 105 | return GetOatPointer<const uint8_t*>(native_gc_map_offset_); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 106 | } |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 107 | |
Ian Rogers | c928de9 | 2013-02-27 14:30:44 -0800 | [diff] [blame] | 108 | #if defined(ART_USE_PORTABLE_COMPILER) |
TDYa127 | eead4ac | 2012-06-03 07:15:25 -0700 | [diff] [blame] | 109 | const void* GetProxyStub() const; |
| 110 | #endif |
| 111 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 112 | ~OatMethod(); |
| 113 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 114 | // Create an OatMethod with offsets relative to the given base address |
| 115 | OatMethod(const byte* base, |
| 116 | const uint32_t code_offset, |
| 117 | const size_t frame_size_in_bytes, |
| 118 | const uint32_t core_spill_mask, |
| 119 | const uint32_t fp_spill_mask, |
| 120 | const uint32_t mapping_table_offset, |
| 121 | const uint32_t vmap_table_offset, |
Jeff Hao | 74180ca | 2013-03-27 15:29:11 -0700 | [diff] [blame^] | 122 | const uint32_t gc_map_offset |
Ian Rogers | c928de9 | 2013-02-27 14:30:44 -0800 | [diff] [blame] | 123 | #if defined(ART_USE_PORTABLE_COMPILER) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 124 | , const uint32_t proxy_stub_offset |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 125 | #endif |
| 126 | ); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 127 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 128 | private: |
| 129 | template<class T> |
| 130 | T GetOatPointer(uint32_t offset) const { |
| 131 | if (offset == 0) { |
| 132 | return NULL; |
| 133 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 134 | return reinterpret_cast<T>(begin_ + offset); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 137 | const byte* begin_; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 138 | |
| 139 | uint32_t code_offset_; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 140 | size_t frame_size_in_bytes_; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 141 | uint32_t core_spill_mask_; |
| 142 | uint32_t fp_spill_mask_; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 143 | uint32_t mapping_table_offset_; |
| 144 | uint32_t vmap_table_offset_; |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 145 | uint32_t native_gc_map_offset_; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 146 | |
Ian Rogers | c928de9 | 2013-02-27 14:30:44 -0800 | [diff] [blame] | 147 | #if defined(ART_USE_PORTABLE_COMPILER) |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 148 | uint32_t proxy_stub_offset_; |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 149 | #endif |
| 150 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 151 | friend class OatClass; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 152 | }; |
| 153 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 154 | class OatClass { |
| 155 | public: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 156 | mirror::Class::Status GetStatus() const; |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 157 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 158 | // get the OatMethod entry based on its index into the class |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 159 | // defintion. direct methods come first, followed by virtual |
| 160 | // methods. note that runtime created methods such as miranda |
| 161 | // methods are not included. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 162 | const OatMethod GetOatMethod(uint32_t method_index) const; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 163 | ~OatClass(); |
| 164 | |
| 165 | private: |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 166 | OatClass(const OatFile* oat_file, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 167 | mirror::Class::Status status, |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 168 | const OatMethodOffsets* methods_pointer); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 169 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 170 | const OatFile* oat_file_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 171 | const mirror::Class::Status status_; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 172 | const OatMethodOffsets* methods_pointer_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 173 | |
| 174 | friend class OatDexFile; |
| 175 | }; |
| 176 | |
| 177 | class OatDexFile { |
| 178 | public: |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 179 | const DexFile* OpenDexFile() const; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 180 | const OatClass* GetOatClass(uint32_t class_def_index) const; |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 181 | size_t FileSize() const; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 182 | |
| 183 | const std::string& GetDexFileLocation() const { |
| 184 | return dex_file_location_; |
| 185 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 186 | |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 187 | uint32_t GetDexFileLocationChecksum() const { |
| 188 | return dex_file_location_checksum_; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 191 | ~OatDexFile(); |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 192 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 193 | private: |
| 194 | OatDexFile(const OatFile* oat_file, |
Elliott Hughes | aa6a588 | 2012-01-13 19:39:16 -0800 | [diff] [blame] | 195 | const std::string& dex_file_location, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 196 | uint32_t dex_file_checksum, |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 197 | const byte* dex_file_pointer, |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 198 | const uint32_t* oat_class_offsets_pointer); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 199 | |
| 200 | const OatFile* oat_file_; |
| 201 | std::string dex_file_location_; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 202 | uint32_t dex_file_location_checksum_; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 203 | const byte* dex_file_pointer_; |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 204 | const uint32_t* oat_class_offsets_pointer_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 205 | |
| 206 | friend class OatFile; |
| 207 | DISALLOW_COPY_AND_ASSIGN(OatDexFile); |
| 208 | }; |
| 209 | |
Ian Rogers | 7fe2c69 | 2011-12-06 16:35:59 -0800 | [diff] [blame] | 210 | const OatDexFile* GetOatDexFile(const std::string& dex_file_location, |
| 211 | bool warn_if_not_found = true) const; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 212 | std::vector<const OatDexFile*> GetOatDexFiles() const; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 213 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 214 | size_t Size() const { |
| 215 | return End() - Begin(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | private: |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 219 | static void CheckLocation(const std::string& location); |
| 220 | |
| 221 | static OatFile* OpenDlopen(const std::string& elf_filename, |
| 222 | const std::string& location, |
| 223 | byte* requested_base); |
| 224 | |
| 225 | static OatFile* OpenElfFile(File* file, |
| 226 | const std::string& location, |
| 227 | byte* requested_base, |
| 228 | bool writable); |
| 229 | |
Elliott Hughes | a51a3dd | 2011-10-17 15:19:26 -0700 | [diff] [blame] | 230 | explicit OatFile(const std::string& filename); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 231 | bool Dlopen(const std::string& elf_filename, byte* requested_base); |
| 232 | bool ElfFileOpen(File* file, byte* requested_base, bool writable); |
| 233 | void Setup(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 234 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 235 | const byte* Begin() const; |
| 236 | const byte* End() const; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 237 | |
| 238 | // The oat file name. |
| 239 | // |
| 240 | // The image will embed this to link its associated oat file. |
| 241 | const std::string location_; |
| 242 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 243 | // Pointer to OatHeader. |
| 244 | const byte* begin_; |
| 245 | |
| 246 | // Pointer to end of oat region for bounds checking. |
| 247 | const byte* end_; |
| 248 | |
| 249 | // Backing memory map for oat file during when opened by ElfWriter during initial compilation. |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 250 | UniquePtr<MemMap> mem_map_; |
| 251 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 252 | // Backing memory map for oat file during cross compilation. |
| 253 | UniquePtr<ElfFile> elf_file_; |
| 254 | |
| 255 | // dlopen handle during runtime. |
| 256 | void* dlopen_handle_; |
| 257 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 258 | typedef SafeMap<std::string, const OatDexFile*> Table; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 259 | Table oat_dex_files_; |
| 260 | |
| 261 | friend class OatClass; |
| 262 | friend class OatDexFile; |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 263 | friend class OatDumper; // For GetBase and GetLimit |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 264 | DISALLOW_COPY_AND_ASSIGN(OatFile); |
| 265 | }; |
| 266 | |
| 267 | } // namespace art |
| 268 | |
| 269 | #endif // ART_SRC_OAT_WRITER_H_ |