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