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 | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 24 | #include "base/array_ref.h" |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 25 | #include "base/mutex.h" |
Vladimir Marko | 539690a | 2014-06-05 18:36:42 +0100 | [diff] [blame] | 26 | #include "base/stringpiece.h" |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 27 | #include "compiler_filter.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | #include "dex_file.h" |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 29 | #include "method_bss_mapping.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 30 | #include "mirror/class.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 31 | #include "oat.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 32 | #include "os.h" |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 33 | #include "type_lookup_table.h" |
| 34 | #include "utf.h" |
Nicolas Geoffray | 6bc4374 | 2015-10-12 18:11:10 +0100 | [diff] [blame] | 35 | #include "utils.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 36 | |
| 37 | namespace art { |
| 38 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 39 | class BitVector; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 40 | class ElfFile; |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 41 | template <class MirrorType> class GcRoot; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 42 | class MemMap; |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 43 | class OatDexFile; |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 44 | class OatHeader; |
| 45 | class OatMethodOffsets; |
| 46 | class OatQuickMethodHeader; |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 47 | class VdexFile; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 48 | |
Mathieu Chartier | 5351da0 | 2016-02-17 16:19:53 -0800 | [diff] [blame] | 49 | namespace gc { |
| 50 | namespace collector { |
| 51 | class DummyOatFile; |
| 52 | } // namespace collector |
| 53 | } // namespace gc |
| 54 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 55 | // Runtime representation of the OAT file format which holds compiler output. |
| 56 | // The class opens an OAT file from storage and maps it to memory, typically with |
| 57 | // dlopen and provides access to its internal data structures (see OatWriter for |
| 58 | // for more details about the OAT format). |
| 59 | // In the process of loading OAT, the class also loads the associated VDEX file |
| 60 | // with the input DEX files (see VdexFile for details about the VDEX format). |
| 61 | // The raw DEX data are accessible transparently through the OatDexFile objects. |
| 62 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 63 | class OatFile { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 64 | public: |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 65 | // Special classpath that skips shared library check. |
| 66 | static constexpr const char* kSpecialSharedLibrary = "&"; |
| 67 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 68 | typedef art::OatDexFile OatDexFile; |
| 69 | |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 70 | // Opens an oat file contained within the given elf file. This is always opened as |
| 71 | // non-executable at the moment. |
David Brazdil | c93b3be | 2016-09-12 18:49:58 +0100 | [diff] [blame] | 72 | static OatFile* OpenWithElfFile(ElfFile* elf_file, |
| 73 | VdexFile* vdex_file, |
| 74 | const std::string& location, |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 75 | const char* abs_dex_location, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 76 | std::string* error_msg); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 77 | // Open an oat file. Returns null on failure. Requested base can |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 78 | // optionally be used to request where the file should be loaded. |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 79 | // See the ResolveRelativeEncodedDexLocation for a description of how the |
| 80 | // abs_dex_location argument is used. |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 81 | static OatFile* Open(const std::string& filename, |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 82 | const std::string& location, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 83 | uint8_t* requested_base, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 84 | uint8_t* oat_file_begin, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 85 | bool executable, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 86 | bool low_4gb, |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 87 | const char* abs_dex_location, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 88 | std::string* error_msg); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 89 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 90 | // Open an oat file from an already opened File. |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 91 | // Does not use dlopen underneath so cannot be used for runtime use |
| 92 | // where relocations may be required. Currently used from |
| 93 | // ImageWriter which wants to open a writable version from an existing |
| 94 | // file descriptor for patching. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 95 | static OatFile* OpenWritable(File* file, const std::string& location, |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 96 | const char* abs_dex_location, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 97 | std::string* error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 98 | // 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] | 99 | static OatFile* OpenReadable(File* file, const std::string& location, |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 100 | const char* abs_dex_location, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 101 | std::string* error_msg); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 102 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 103 | virtual ~OatFile(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 104 | |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 105 | bool IsExecutable() const { |
| 106 | return is_executable_; |
| 107 | } |
| 108 | |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 109 | bool IsPic() const; |
| 110 | |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 111 | // Indicates whether the oat file was compiled with full debugging capability. |
| 112 | bool IsDebuggable() const; |
| 113 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 114 | CompilerFilter::Filter GetCompilerFilter() const; |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 115 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 116 | const std::string& GetLocation() const { |
| 117 | return location_; |
| 118 | } |
| 119 | |
| 120 | const OatHeader& GetOatHeader() const; |
| 121 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 122 | class OatMethod FINAL { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 123 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 124 | void LinkMethod(ArtMethod* method) const; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 125 | |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 126 | uint32_t GetCodeOffset() const; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 127 | |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 128 | const void* GetQuickCode() const; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 129 | |
| 130 | // Returns size of quick code. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 131 | uint32_t GetQuickCodeSize() const; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 132 | uint32_t GetQuickCodeSizeOffset() const; |
| 133 | |
| 134 | // Returns OatQuickMethodHeader for debugging. Most callers should |
| 135 | // use more specific methods such as GetQuickCodeSize. |
| 136 | const OatQuickMethodHeader* GetOatQuickMethodHeader() const; |
| 137 | uint32_t GetOatQuickMethodHeaderOffset() const; |
Logan Chien | 0c717dd | 2012-03-28 18:31:07 +0800 | [diff] [blame] | 138 | |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 139 | size_t GetFrameSizeInBytes() const; |
| 140 | uint32_t GetCoreSpillMask() const; |
| 141 | uint32_t GetFpSpillMask() const; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 142 | |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 143 | const uint8_t* GetVmapTable() const; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 144 | uint32_t GetVmapTableOffset() const; |
| 145 | uint32_t GetVmapTableOffsetOffset() const; |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 146 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 147 | // Create an OatMethod with offsets relative to the given base address |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 148 | OatMethod(const uint8_t* base, const uint32_t code_offset) |
| 149 | : begin_(base), code_offset_(code_offset) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 150 | } |
Andreas Gampe | 758a801 | 2015-04-03 21:28:42 -0700 | [diff] [blame] | 151 | OatMethod(const OatMethod&) = default; |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 152 | ~OatMethod() {} |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 153 | |
Andreas Gampe | 758a801 | 2015-04-03 21:28:42 -0700 | [diff] [blame] | 154 | OatMethod& operator=(const OatMethod&) = default; |
| 155 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 156 | // A representation of an invalid OatMethod, used when an OatMethod or OatClass can't be found. |
| 157 | // See ClassLinker::FindOatMethodFor. |
| 158 | static const OatMethod Invalid() { |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 159 | return OatMethod(nullptr, -1); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 160 | } |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 161 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 162 | private: |
| 163 | template<class T> |
| 164 | T GetOatPointer(uint32_t offset) const { |
| 165 | if (offset == 0) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 166 | return nullptr; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 167 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 168 | return reinterpret_cast<T>(begin_ + offset); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 171 | const uint8_t* begin_; |
| 172 | uint32_t code_offset_; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 173 | |
| 174 | friend class OatClass; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 175 | }; |
| 176 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 177 | class OatClass FINAL { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 178 | public: |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 179 | mirror::Class::Status GetStatus() const { |
| 180 | return status_; |
| 181 | } |
| 182 | |
| 183 | OatClassType GetType() const { |
| 184 | return type_; |
| 185 | } |
Brian Carlstrom | 0755ec5 | 2012-01-11 15:19:46 -0800 | [diff] [blame] | 186 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 187 | // Get the OatMethod entry based on its index into the class |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 188 | // defintion. Direct methods come first, followed by virtual |
| 189 | // methods. Note that runtime created methods such as miranda |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 190 | // methods are not included. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 191 | const OatMethod GetOatMethod(uint32_t method_index) const; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 192 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 193 | // Return a pointer to the OatMethodOffsets for the requested |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 194 | // method_index, or null if none is present. Note that most |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 195 | // callers should use GetOatMethod. |
| 196 | const OatMethodOffsets* GetOatMethodOffsets(uint32_t method_index) const; |
| 197 | |
| 198 | // Return the offset from the start of the OatFile to the |
| 199 | // OatMethodOffsets for the requested method_index, or 0 if none |
| 200 | // is present. Note that most callers should use GetOatMethod. |
| 201 | uint32_t GetOatMethodOffsetsOffset(uint32_t method_index) const; |
| 202 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 203 | // A representation of an invalid OatClass, used when an OatClass can't be found. |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 204 | // See FindOatClass(). |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 205 | static OatClass Invalid() { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 206 | return OatClass(/* oat_file */ nullptr, |
| 207 | mirror::Class::kStatusErrorUnresolved, |
| 208 | kOatClassNoneCompiled, |
| 209 | /* bitmap_size */ 0, |
| 210 | /* bitmap_pointer */ nullptr, |
| 211 | /* methods_pointer */ nullptr); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 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 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 232 | friend class art::OatDexFile; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 233 | }; |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 234 | |
| 235 | // Get the OatDexFile for the given dex_location within this oat file. |
| 236 | // If dex_location_checksum is non-null, the OatDexFile will only be |
| 237 | // returned if it has a matching checksum. |
| 238 | // If error_msg is non-null and no OatDexFile is returned, error_msg will |
| 239 | // be updated with a description of why no OatDexFile was returned. |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 240 | const OatDexFile* GetOatDexFile(const char* dex_location, |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 241 | const uint32_t* const dex_location_checksum, |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 242 | /*out*/std::string* error_msg = nullptr) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 243 | REQUIRES(!secondary_lookup_lock_); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 244 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 245 | const std::vector<const OatDexFile*>& GetOatDexFiles() const { |
| 246 | return oat_dex_files_storage_; |
| 247 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 248 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 249 | size_t Size() const { |
| 250 | return End() - Begin(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 253 | bool Contains(const void* p) const { |
| 254 | return p >= Begin() && p < End(); |
| 255 | } |
| 256 | |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 257 | size_t BssSize() const { |
| 258 | return BssEnd() - BssBegin(); |
| 259 | } |
| 260 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 261 | size_t BssMethodsOffset() const { |
| 262 | // Note: This is used only for symbolizer and needs to return a valid .bss offset. |
| 263 | return (bss_methods_ != nullptr) ? bss_methods_ - BssBegin() : BssRootsOffset(); |
| 264 | } |
| 265 | |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 266 | size_t BssRootsOffset() const { |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 267 | // Note: This is used only for symbolizer and needs to return a valid .bss offset. |
| 268 | return (bss_roots_ != nullptr) ? bss_roots_ - BssBegin() : BssSize(); |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 269 | } |
| 270 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 271 | size_t DexSize() const { |
| 272 | return DexEnd() - DexBegin(); |
| 273 | } |
| 274 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 275 | const uint8_t* Begin() const; |
| 276 | const uint8_t* End() const; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 277 | |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 278 | const uint8_t* BssBegin() const; |
| 279 | const uint8_t* BssEnd() const; |
| 280 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 281 | const uint8_t* DexBegin() const; |
| 282 | const uint8_t* DexEnd() const; |
| 283 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 284 | ArrayRef<ArtMethod*> GetBssMethods() const; |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 285 | ArrayRef<GcRoot<mirror::Object>> GetBssGcRoots() const; |
| 286 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 287 | // Returns the absolute dex location for the encoded relative dex location. |
| 288 | // |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 289 | // 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] | 290 | // location of relative dex locations encoded in the oat file. |
| 291 | // For example, given absolute location "/data/app/foo/base.apk", encoded |
Calin Juravle | a308a32 | 2017-07-18 16:51:51 -0700 | [diff] [blame] | 292 | // dex locations "base.apk", "base.apk!classes2.dex", etc. would be resolved |
| 293 | // to "/data/app/foo/base.apk", "/data/app/foo/base.apk!classes2.dex", etc. |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 294 | // Relative encoded dex locations that don't match the given abs_dex_location |
| 295 | // are left unchanged. |
| 296 | static std::string ResolveRelativeEncodedDexLocation( |
| 297 | const char* abs_dex_location, const std::string& rel_dex_location); |
| 298 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 299 | // Finds the associated oat class for a dex_file and descriptor. Returns an invalid OatClass on |
| 300 | // error and sets found to false. |
| 301 | static OatClass FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, bool* found); |
| 302 | |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 303 | VdexFile* GetVdexFile() const { |
| 304 | return vdex_.get(); |
| 305 | } |
| 306 | |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 307 | protected: |
| 308 | OatFile(const std::string& filename, bool executable); |
| 309 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 310 | private: |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 311 | // The oat file name. |
| 312 | // |
| 313 | // The image will embed this to link its associated oat file. |
| 314 | const std::string location_; |
| 315 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 316 | // Pointer to the Vdex file with the Dex files for this Oat file. |
| 317 | std::unique_ptr<VdexFile> vdex_; |
| 318 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 319 | // Pointer to OatHeader. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 320 | const uint8_t* begin_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 321 | |
| 322 | // Pointer to end of oat region for bounds checking. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 323 | const uint8_t* end_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 324 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 325 | // Pointer to the .bss section, if present, otherwise null. |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 326 | uint8_t* bss_begin_; |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 327 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 328 | // Pointer to the end of the .bss section, if present, otherwise null. |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 329 | uint8_t* bss_end_; |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 330 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 331 | // Pointer to the beginning of the ArtMethod*s in .bss section, if present, otherwise null. |
| 332 | uint8_t* bss_methods_; |
| 333 | |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 334 | // Pointer to the beginning of the GC roots in .bss section, if present, otherwise null. |
| 335 | uint8_t* bss_roots_; |
| 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 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 340 | // Owning storage for the OatDexFile objects. |
| 341 | std::vector<const OatDexFile*> oat_dex_files_storage_; |
| 342 | |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 343 | // NOTE: We use a StringPiece as the key type to avoid a memory allocation on every |
| 344 | // lookup with a const char* key. The StringPiece doesn't own its backing storage, |
| 345 | // therefore we're using the OatDexFile::dex_file_location_ as the backing storage |
| 346 | // for keys in oat_dex_files_ and the string_cache_ entries for the backing storage |
| 347 | // 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] | 348 | typedef AllocationTrackingSafeMap<StringPiece, const OatDexFile*, kAllocatorTagOatFile> Table; |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 349 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 350 | // Map each location and canonical location (if different) retrieved from the |
| 351 | // oat file to its OatDexFile. This map doesn't change after it's constructed in Setup() |
| 352 | // 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] | 353 | // 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] | 354 | Table oat_dex_files_; |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 355 | |
| 356 | // Lock guarding all members needed for secondary lookup in GetOatDexFile(). |
| 357 | mutable Mutex secondary_lookup_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 358 | |
| 359 | // If the primary oat_dex_files_ lookup fails, use a secondary map. This map stores |
| 360 | // the results of all previous secondary lookups, whether successful (non-null) or |
| 361 | // failed (null). If it doesn't contain an entry we need to calculate the canonical |
| 362 | // location and use oat_dex_files_by_canonical_location_. |
| 363 | mutable Table secondary_oat_dex_files_ GUARDED_BY(secondary_lookup_lock_); |
| 364 | |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 365 | // Cache of strings. Contains the backing storage for keys in the secondary_oat_dex_files_ |
| 366 | // and the lazily initialized oat_dex_files_by_canonical_location_. |
| 367 | // NOTE: We're keeping references to contained strings in form of StringPiece and adding |
| 368 | // new strings to the end. The adding of a new element must not touch any previously stored |
| 369 | // elements. std::list<> and std::deque<> satisfy this requirement, std::vector<> doesn't. |
| 370 | mutable std::list<std::string> string_cache_ GUARDED_BY(secondary_lookup_lock_); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 371 | |
Mathieu Chartier | 5351da0 | 2016-02-17 16:19:53 -0800 | [diff] [blame] | 372 | friend class gc::collector::DummyOatFile; // For modifying begin_ and end_. |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 373 | friend class OatClass; |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 374 | friend class art::OatDexFile; |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 375 | friend class OatDumper; // For GetBase and GetLimit |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 376 | friend class OatFileBase; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 377 | DISALLOW_COPY_AND_ASSIGN(OatFile); |
| 378 | }; |
| 379 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 380 | // OatDexFile should be an inner class of OatFile. Unfortunately, C++ doesn't |
| 381 | // support forward declarations of inner classes, and we want to |
| 382 | // forward-declare OatDexFile so that we can store an opaque pointer to an |
| 383 | // OatDexFile in DexFile. |
| 384 | class OatDexFile FINAL { |
| 385 | public: |
| 386 | // 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] | 387 | std::unique_ptr<const DexFile> OpenDexFile(std::string* error_msg) const; |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 388 | |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 389 | // May return null if the OatDexFile only contains a type lookup table. This case only happens |
| 390 | // for the compiler to speed up compilation. |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 391 | const OatFile* GetOatFile() const { |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 392 | // Avoid pulling in runtime.h in the header file. |
| 393 | if (kIsDebugBuild && oat_file_ == nullptr) { |
| 394 | AssertAotCompiler(); |
| 395 | } |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 396 | return oat_file_; |
| 397 | } |
| 398 | |
| 399 | // Returns the size of the DexFile refered to by this OatDexFile. |
| 400 | size_t FileSize() const; |
| 401 | |
| 402 | // Returns original path of DexFile that was the source of this OatDexFile. |
| 403 | const std::string& GetDexFileLocation() const { |
| 404 | return dex_file_location_; |
| 405 | } |
| 406 | |
| 407 | // Returns the canonical location of DexFile that was the source of this OatDexFile. |
| 408 | const std::string& GetCanonicalDexFileLocation() const { |
| 409 | return canonical_dex_file_location_; |
| 410 | } |
| 411 | |
| 412 | // Returns checksum of original DexFile that was the source of this OatDexFile; |
| 413 | uint32_t GetDexFileLocationChecksum() const { |
| 414 | return dex_file_location_checksum_; |
| 415 | } |
| 416 | |
| 417 | // Returns the OatClass for the class specified by the given DexFile class_def_index. |
| 418 | OatFile::OatClass GetOatClass(uint16_t class_def_index) const; |
| 419 | |
| 420 | // Returns the offset to the OatClass information. Most callers should use GetOatClass. |
| 421 | uint32_t GetOatClassOffset(uint16_t class_def_index) const; |
| 422 | |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 423 | uint8_t* GetDexCacheArrays() const { |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 424 | return dex_cache_arrays_; |
| 425 | } |
| 426 | |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 427 | const uint8_t* GetLookupTableData() const { |
| 428 | return lookup_table_data_; |
| 429 | } |
| 430 | |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 431 | const MethodBssMapping* GetMethodBssMapping() const { |
| 432 | return method_bss_mapping_; |
| 433 | } |
| 434 | |
Andreas Gampe | 2ba8895 | 2016-04-29 17:52:07 -0700 | [diff] [blame] | 435 | const uint8_t* GetDexFilePointer() const { |
| 436 | return dex_file_pointer_; |
| 437 | } |
| 438 | |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 439 | // Looks up a class definition by its class descriptor. Hash must be |
| 440 | // ComputeModifiedUtf8Hash(descriptor). |
| 441 | static const DexFile::ClassDef* FindClassDef(const DexFile& dex_file, |
| 442 | const char* descriptor, |
| 443 | size_t hash); |
| 444 | |
| 445 | TypeLookupTable* GetTypeLookupTable() const { |
| 446 | return lookup_table_.get(); |
| 447 | } |
| 448 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 449 | ~OatDexFile(); |
| 450 | |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 451 | // Create only with a type lookup table, used by the compiler to speed up compilation. |
| 452 | explicit OatDexFile(std::unique_ptr<TypeLookupTable>&& lookup_table); |
| 453 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 454 | private: |
| 455 | OatDexFile(const OatFile* oat_file, |
| 456 | const std::string& dex_file_location, |
| 457 | const std::string& canonical_dex_file_location, |
| 458 | uint32_t dex_file_checksum, |
| 459 | const uint8_t* dex_file_pointer, |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 460 | const uint8_t* lookup_table_data, |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 461 | const MethodBssMapping* method_bss_mapping, |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 462 | const uint32_t* oat_class_offsets_pointer, |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 463 | uint8_t* dex_cache_arrays); |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 464 | |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 465 | static void AssertAotCompiler(); |
| 466 | |
| 467 | const OatFile* const oat_file_ = nullptr; |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 468 | const std::string dex_file_location_; |
| 469 | const std::string canonical_dex_file_location_; |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 470 | const uint32_t dex_file_location_checksum_ = 0u; |
| 471 | const uint8_t* const dex_file_pointer_ = nullptr; |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 472 | const uint8_t* const lookup_table_data_ = nullptr; |
| 473 | const MethodBssMapping* const method_bss_mapping_ = nullptr; |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 474 | const uint32_t* const oat_class_offsets_pointer_ = 0u; |
| 475 | uint8_t* const dex_cache_arrays_ = nullptr; |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 476 | mutable std::unique_ptr<TypeLookupTable> lookup_table_; |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 477 | |
| 478 | friend class OatFile; |
Andreas Gampe | 049cff0 | 2015-12-01 23:27:12 -0800 | [diff] [blame] | 479 | friend class OatFileBase; |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 480 | DISALLOW_COPY_AND_ASSIGN(OatDexFile); |
| 481 | }; |
| 482 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 483 | } // namespace art |
| 484 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 485 | #endif // ART_RUNTIME_OAT_FILE_H_ |