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