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