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