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 | |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 20 | #include <list> |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 21 | #include <string> |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 24 | #include "base/mutex.h" |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 25 | #include "base/stringpiece.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 26 | #include "dex_file.h" |
| 27 | #include "invoke_type.h" |
| 28 | #include "mem_map.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 29 | #include "mirror/class.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | #include "oat.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 31 | #include "os.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 35 | class BitVector; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 36 | class ElfFile; |
| 37 | class MemMap; |
| 38 | class OatMethodOffsets; |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 39 | class OatHeader; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 40 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 41 | class OatFile { |
| 42 | public: |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 43 | // Opens an oat file contained within the given elf file. This is always opened as |
| 44 | // non-executable at the moment. |
| 45 | static OatFile* OpenWithElfFile(ElfFile* elf_file, const std::string& location, |
| 46 | std::string* error_msg); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 47 | // Open an oat file. Returns NULL on failure. Requested base can |
| 48 | // optionally be used to request where the file should be loaded. |
| 49 | static OatFile* Open(const std::string& filename, |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 50 | const std::string& location, |
Brian Carlstrom | f1d3455 | 2013-07-12 20:22:23 -0700 | [diff] [blame] | 51 | byte* requested_base, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 52 | bool executable, |
| 53 | std::string* error_msg); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 54 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 55 | // Open an oat file from an already opened File. |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 56 | // Does not use dlopen underneath so cannot be used for runtime use |
| 57 | // where relocations may be required. Currently used from |
| 58 | // ImageWriter which wants to open a writable version from an existing |
| 59 | // file descriptor for patching. |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 60 | static OatFile* OpenWritable(File* file, const std::string& location, std::string* error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 61 | // Opens an oat file from an already opened File. Maps it PROT_READ, MAP_PRIVATE. |
| 62 | static OatFile* OpenReadable(File* file, const std::string& location, std::string* error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 63 | |
| 64 | // 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] | 65 | static OatFile* OpenMemory(std::vector<uint8_t>& oat_contents, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 66 | const std::string& location, |
| 67 | std::string* error_msg); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 68 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 69 | ~OatFile(); |
| 70 | |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 71 | bool IsExecutable() const { |
| 72 | return is_executable_; |
| 73 | } |
| 74 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 75 | ElfFile* GetElfFile() const { |
| 76 | CHECK_NE(reinterpret_cast<uintptr_t>(elf_file_.get()), reinterpret_cast<uintptr_t>(nullptr)) |
| 77 | << "Cannot get an elf file from " << GetLocation(); |
| 78 | return elf_file_.get(); |
| 79 | } |
| 80 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 81 | const std::string& GetLocation() const { |
| 82 | return location_; |
| 83 | } |
| 84 | |
| 85 | const OatHeader& GetOatHeader() const; |
| 86 | |
| 87 | class OatDexFile; |
| 88 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 89 | class OatMethod { |
| 90 | public: |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 91 | void LinkMethod(mirror::ArtMethod* method) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 92 | |
| 93 | uint32_t GetCodeOffset() const { |
| 94 | return code_offset_; |
| 95 | } |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 96 | uint32_t GetNativeGcMapOffset() const { |
| 97 | return native_gc_map_offset_; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 98 | } |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 99 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 100 | const void* GetPortableCode() const { |
| 101 | // TODO: encode whether code is portable/quick in flags within OatMethod. |
| 102 | if (kUsePortableCompiler) { |
| 103 | return GetOatPointer<const void*>(code_offset_); |
| 104 | } else { |
| 105 | return nullptr; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | const void* GetQuickCode() const { |
| 110 | if (kUsePortableCompiler) { |
| 111 | return nullptr; |
| 112 | } else { |
| 113 | return GetOatPointer<const void*>(code_offset_); |
| 114 | } |
| 115 | } |
| 116 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame^] | 117 | // Returns 0. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 118 | uint32_t GetPortableCodeSize() const { |
| 119 | // TODO: With Quick, we store the size before the code. With Portable, the code is in a .o |
| 120 | // file we don't manage ourselves. ELF symbols do have a concept of size, so we could capture |
| 121 | // that and store it somewhere, such as the OatMethod. |
| 122 | return 0; |
| 123 | } |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame^] | 124 | |
| 125 | // Returns size of quick code. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 126 | uint32_t GetQuickCodeSize() const; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame^] | 127 | uint32_t GetQuickCodeSizeOffset() const; |
| 128 | |
| 129 | // Returns OatQuickMethodHeader for debugging. Most callers should |
| 130 | // use more specific methods such as GetQuickCodeSize. |
| 131 | const OatQuickMethodHeader* GetOatQuickMethodHeader() const; |
| 132 | uint32_t GetOatQuickMethodHeaderOffset() const; |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 133 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 134 | const uint8_t* GetNativeGcMap() const { |
| 135 | return GetOatPointer<const uint8_t*>(native_gc_map_offset_); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 136 | } |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 137 | |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 138 | size_t GetFrameSizeInBytes() const; |
| 139 | uint32_t GetCoreSpillMask() const; |
| 140 | uint32_t GetFpSpillMask() const; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame^] | 141 | |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 142 | const uint8_t* GetMappingTable() const; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame^] | 143 | uint32_t GetMappingTableOffset() const; |
| 144 | uint32_t GetMappingTableOffsetOffset() const; |
| 145 | |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 146 | const uint8_t* GetVmapTable() const; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame^] | 147 | uint32_t GetVmapTableOffset() const; |
| 148 | uint32_t GetVmapTableOffsetOffset() const; |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 149 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 150 | // Create an OatMethod with offsets relative to the given base address |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 151 | OatMethod(const byte* base, const uint32_t code_offset, const uint32_t gc_map_offset) |
| 152 | : begin_(base), |
| 153 | code_offset_(code_offset), |
| 154 | native_gc_map_offset_(gc_map_offset) { |
| 155 | } |
| 156 | ~OatMethod() {} |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 157 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 158 | // A representation of an invalid OatMethod, used when an OatMethod or OatClass can't be found. |
| 159 | // See ClassLinker::FindOatMethodFor. |
| 160 | static const OatMethod Invalid() { |
| 161 | return OatMethod(nullptr, -1, -1); |
| 162 | } |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 163 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 164 | private: |
| 165 | template<class T> |
| 166 | T GetOatPointer(uint32_t offset) const { |
| 167 | if (offset == 0) { |
| 168 | return NULL; |
| 169 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 170 | return reinterpret_cast<T>(begin_ + offset); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 173 | const byte* const begin_; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 174 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 175 | const uint32_t code_offset_; |
| 176 | const uint32_t native_gc_map_offset_; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 177 | |
| 178 | friend class OatClass; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 181 | class OatClass { |
| 182 | public: |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 183 | mirror::Class::Status GetStatus() const { |
| 184 | return status_; |
| 185 | } |
| 186 | |
| 187 | OatClassType GetType() const { |
| 188 | return type_; |
| 189 | } |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 190 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 191 | // Get the OatMethod entry based on its index into the class |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame^] | 192 | // defintion. Direct methods come first, followed by virtual |
| 193 | // methods. Note that runtime created methods such as miranda |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 194 | // methods are not included. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 195 | const OatMethod GetOatMethod(uint32_t method_index) const; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 196 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame^] | 197 | // Return a pointer to the OatMethodOffsets for the requested |
| 198 | // method_index, or nullptr if none is present. Note that most |
| 199 | // callers should use GetOatMethod. |
| 200 | const OatMethodOffsets* GetOatMethodOffsets(uint32_t method_index) const; |
| 201 | |
| 202 | // Return the offset from the start of the OatFile to the |
| 203 | // OatMethodOffsets for the requested method_index, or 0 if none |
| 204 | // is present. Note that most callers should use GetOatMethod. |
| 205 | uint32_t GetOatMethodOffsetsOffset(uint32_t method_index) const; |
| 206 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 207 | // A representation of an invalid OatClass, used when an OatClass can't be found. |
| 208 | // See ClassLinker::FindOatClass. |
| 209 | static OatClass Invalid() { |
| 210 | return OatClass(nullptr, mirror::Class::kStatusError, kOatClassNoneCompiled, 0, nullptr, |
| 211 | nullptr); |
| 212 | } |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 213 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 214 | private: |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 215 | OatClass(const OatFile* oat_file, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 216 | mirror::Class::Status status, |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 217 | OatClassType type, |
| 218 | uint32_t bitmap_size, |
| 219 | const uint32_t* bitmap_pointer, |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 220 | const OatMethodOffsets* methods_pointer); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 221 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 222 | const OatFile* const oat_file_; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 223 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 224 | const mirror::Class::Status status_; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 225 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 226 | const OatClassType type_; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 227 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 228 | const uint32_t* const bitmap_; |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 229 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 230 | const OatMethodOffsets* const methods_pointer_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 231 | |
| 232 | friend class OatDexFile; |
| 233 | }; |
| 234 | |
| 235 | class OatDexFile { |
| 236 | public: |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 237 | // Opens the DexFile referred to by this OatDexFile from within the containing OatFile. |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 238 | const DexFile* OpenDexFile(std::string* error_msg) const; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 239 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 240 | const OatFile* GetOatFile() const { |
| 241 | return oat_file_; |
| 242 | } |
| 243 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 244 | // Returns the size of the DexFile refered to by this OatDexFile. |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 245 | size_t FileSize() const; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 246 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 247 | // Returns original path of DexFile that was the source of this OatDexFile. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 248 | const std::string& GetDexFileLocation() const { |
| 249 | return dex_file_location_; |
| 250 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 251 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 252 | // Returns the canonical location of DexFile that was the source of this OatDexFile. |
| 253 | const std::string& GetCanonicalDexFileLocation() const { |
| 254 | return canonical_dex_file_location_; |
| 255 | } |
| 256 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 257 | // Returns checksum of original DexFile that was the source of this OatDexFile; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 258 | uint32_t GetDexFileLocationChecksum() const { |
| 259 | return dex_file_location_checksum_; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 262 | // Returns the OatClass for the class specified by the given DexFile class_def_index. |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 263 | OatClass GetOatClass(uint16_t class_def_index) const; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 264 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame^] | 265 | // Returns the offset to the OatClass information. Most callers should use GetOatClass. |
| 266 | uint32_t GetOatClassOffset(uint16_t class_def_index) const; |
| 267 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 268 | ~OatDexFile(); |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 269 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 270 | private: |
| 271 | OatDexFile(const OatFile* oat_file, |
Elliott Hughes | aa6a588 | 2012-01-13 19:39:16 -0800 | [diff] [blame] | 272 | const std::string& dex_file_location, |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 273 | const std::string& canonical_dex_file_location, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 274 | uint32_t dex_file_checksum, |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 275 | const byte* dex_file_pointer, |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 276 | const uint32_t* oat_class_offsets_pointer); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 277 | |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 278 | const OatFile* const oat_file_; |
| 279 | const std::string dex_file_location_; |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 280 | const std::string canonical_dex_file_location_; |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 281 | const uint32_t dex_file_location_checksum_; |
| 282 | const byte* const dex_file_pointer_; |
| 283 | const uint32_t* const oat_class_offsets_pointer_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 284 | |
| 285 | friend class OatFile; |
| 286 | DISALLOW_COPY_AND_ASSIGN(OatDexFile); |
| 287 | }; |
| 288 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 289 | const OatDexFile* GetOatDexFile(const char* dex_location, |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 290 | const uint32_t* const dex_location_checksum, |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 291 | bool exception_if_not_found = true) const |
| 292 | LOCKS_EXCLUDED(secondary_lookup_lock_); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 293 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 294 | const std::vector<const OatDexFile*>& GetOatDexFiles() const { |
| 295 | return oat_dex_files_storage_; |
| 296 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 297 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 298 | size_t Size() const { |
| 299 | return End() - Begin(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 302 | const byte* Begin() const; |
| 303 | const byte* End() const; |
| 304 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 305 | private: |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 306 | static void CheckLocation(const std::string& location); |
| 307 | |
| 308 | static OatFile* OpenDlopen(const std::string& elf_filename, |
| 309 | const std::string& location, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 310 | byte* requested_base, |
| 311 | std::string* error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 312 | |
| 313 | static OatFile* OpenElfFile(File* file, |
| 314 | const std::string& location, |
| 315 | byte* requested_base, |
Brian Carlstrom | f1d3455 | 2013-07-12 20:22:23 -0700 | [diff] [blame] | 316 | bool writable, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 317 | bool executable, |
| 318 | std::string* error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 319 | |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 320 | explicit OatFile(const std::string& filename, bool executable); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 321 | bool Dlopen(const std::string& elf_filename, byte* requested_base, std::string* error_msg); |
| 322 | bool ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable, |
| 323 | std::string* error_msg); |
| 324 | bool Setup(std::string* error_msg); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 325 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 326 | // The oat file name. |
| 327 | // |
| 328 | // The image will embed this to link its associated oat file. |
| 329 | const std::string location_; |
| 330 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 331 | // Pointer to OatHeader. |
| 332 | const byte* begin_; |
| 333 | |
| 334 | // Pointer to end of oat region for bounds checking. |
| 335 | const byte* end_; |
| 336 | |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 337 | // Was this oat_file loaded executable? |
| 338 | const bool is_executable_; |
| 339 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 340 | // Backing memory map for oat file during when opened by ElfWriter during initial compilation. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 341 | std::unique_ptr<MemMap> mem_map_; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 342 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 343 | // Backing memory map for oat file during cross compilation. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 344 | std::unique_ptr<ElfFile> elf_file_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 345 | |
| 346 | // dlopen handle during runtime. |
| 347 | void* dlopen_handle_; |
| 348 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 349 | // Owning storage for the OatDexFile objects. |
| 350 | std::vector<const OatDexFile*> oat_dex_files_storage_; |
| 351 | |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 352 | // NOTE: We use a StringPiece as the key type to avoid a memory allocation on every |
| 353 | // lookup with a const char* key. The StringPiece doesn't own its backing storage, |
| 354 | // therefore we're using the OatDexFile::dex_file_location_ as the backing storage |
| 355 | // for keys in oat_dex_files_ and the string_cache_ entries for the backing storage |
| 356 | // of keys in secondary_oat_dex_files_ and oat_dex_files_by_canonical_location_. |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 357 | typedef AllocationTrackingSafeMap<StringPiece, const OatDexFile*, kAllocatorTagOatFile> Table; |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 358 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 359 | // Map each location and canonical location (if different) retrieved from the |
| 360 | // oat file to its OatDexFile. This map doesn't change after it's constructed in Setup() |
| 361 | // and therefore doesn't need any locking and provides the cheapest dex file lookup |
| 362 | // for GetOatDexFile() for a very frequent use case. Never contains a nullptr value. |
| 363 | Table oat_dex_files_; |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 364 | |
| 365 | // Lock guarding all members needed for secondary lookup in GetOatDexFile(). |
| 366 | mutable Mutex secondary_lookup_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 367 | |
| 368 | // If the primary oat_dex_files_ lookup fails, use a secondary map. This map stores |
| 369 | // the results of all previous secondary lookups, whether successful (non-null) or |
| 370 | // failed (null). If it doesn't contain an entry we need to calculate the canonical |
| 371 | // location and use oat_dex_files_by_canonical_location_. |
| 372 | mutable Table secondary_oat_dex_files_ GUARDED_BY(secondary_lookup_lock_); |
| 373 | |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 374 | // Cache of strings. Contains the backing storage for keys in the secondary_oat_dex_files_ |
| 375 | // and the lazily initialized oat_dex_files_by_canonical_location_. |
| 376 | // NOTE: We're keeping references to contained strings in form of StringPiece and adding |
| 377 | // new strings to the end. The adding of a new element must not touch any previously stored |
| 378 | // elements. std::list<> and std::deque<> satisfy this requirement, std::vector<> doesn't. |
| 379 | mutable std::list<std::string> string_cache_ GUARDED_BY(secondary_lookup_lock_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 380 | |
| 381 | friend class OatClass; |
| 382 | friend class OatDexFile; |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 383 | friend class OatDumper; // For GetBase and GetLimit |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 384 | DISALLOW_COPY_AND_ASSIGN(OatFile); |
| 385 | }; |
| 386 | |
| 387 | } // namespace art |
| 388 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 389 | #endif // ART_RUNTIME_OAT_FILE_H_ |