blob: 837fc656c5768e005ebc2cf1a7bf0df8593ecdab [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Carlstrome24fa612011-09-29 00:53:55 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_OAT_FILE_H_
18#define ART_RUNTIME_OAT_FILE_H_
Brian Carlstrome24fa612011-09-29 00:53:55 -070019
Vladimir Marko3f5838d2014-08-07 18:07:18 +010020#include <list>
Brian Carlstrom700c8d32012-11-05 10:42:02 -080021#include <string>
Brian Carlstrome24fa612011-09-29 00:53:55 -070022#include <vector>
23
Vladimir Markoaad75c62016-10-03 08:46:48 +000024#include "base/array_ref.h"
Vladimir Marko3f5838d2014-08-07 18:07:18 +010025#include "base/mutex.h"
David Sehr8f4b0562018-03-02 12:01:51 -080026#include "base/os.h"
David Sehr67bf42e2018-02-26 16:43:04 -080027#include "base/safe_map.h"
Vladimir Marko539690a2014-06-05 18:36:42 +010028#include "base/stringpiece.h"
David Sehr67bf42e2018-02-26 16:43:04 -080029#include "base/tracking_safe_map.h"
David Sehr8f4b0562018-03-02 12:01:51 -080030#include "base/utils.h"
Vladimir Marko2c64a832018-01-04 11:31:56 +000031#include "class_status.h"
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010032#include "compiler_filter.h"
David Sehr9e734c72018-01-04 17:56:19 -080033#include "dex/dex_file.h"
34#include "dex/dex_file_layout.h"
David Sehrc75f0af2018-04-05 11:02:03 -070035#include "dex/type_lookup_table.h"
David Sehr0225f8e2018-01-31 08:52:24 +000036#include "dex/utf.h"
Vladimir Markof3c52b42017-11-17 17:32:12 +000037#include "index_bss_mapping.h"
Vladimir Marko2c64a832018-01-04 11:31:56 +000038#include "mirror/object.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "oat.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070040
41namespace art {
42
Brian Carlstromba150c32013-08-27 17:31:03 -070043class BitVector;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080044class ElfFile;
Mathieu Chartier120aa282017-08-05 16:03:03 -070045class DexLayoutSections;
Vladimir Markoaad75c62016-10-03 08:46:48 +000046template <class MirrorType> class GcRoot;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080047class MemMap;
Richard Uhler07b3c232015-03-31 15:57:54 -070048class OatDexFile;
Andreas Gampe513061a2017-06-01 09:17:34 -070049class OatHeader;
50class OatMethodOffsets;
51class OatQuickMethodHeader;
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010052class VdexFile;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080053
Mathieu Chartier5351da02016-02-17 16:19:53 -080054namespace gc {
55namespace collector {
56class DummyOatFile;
57} // namespace collector
58} // namespace gc
59
David Brazdil7b49e6c2016-09-01 11:06:18 +010060// Runtime representation of the OAT file format which holds compiler output.
61// The class opens an OAT file from storage and maps it to memory, typically with
62// dlopen and provides access to its internal data structures (see OatWriter for
63// for more details about the OAT format).
64// In the process of loading OAT, the class also loads the associated VDEX file
65// with the input DEX files (see VdexFile for details about the VDEX format).
66// The raw DEX data are accessible transparently through the OatDexFile objects.
67
Andreas Gampe049cff02015-12-01 23:27:12 -080068class OatFile {
Brian Carlstrome24fa612011-09-29 00:53:55 -070069 public:
Jeff Haof0192c82016-03-28 20:39:50 -070070 // Special classpath that skips shared library check.
71 static constexpr const char* kSpecialSharedLibrary = "&";
72
Richard Uhler07b3c232015-03-31 15:57:54 -070073 typedef art::OatDexFile OatDexFile;
74
Alex Light84d76052014-08-22 17:49:35 -070075 // Opens an oat file contained within the given elf file. This is always opened as
76 // non-executable at the moment.
Nicolas Geoffray96dca1c2018-04-19 14:43:29 +010077 static OatFile* OpenWithElfFile(int zip_fd,
78 ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +010079 VdexFile* vdex_file,
80 const std::string& location,
Richard Uhlere5fed032015-03-18 08:21:11 -070081 const char* abs_dex_location,
Igor Murashkinb1d8c312015-08-04 11:18:43 -070082 std::string* error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070083 // Open an oat file. Returns null on failure. Requested base can
Brian Carlstrome24fa612011-09-29 00:53:55 -070084 // optionally be used to request where the file should be loaded.
Richard Uhlere5fed032015-03-18 08:21:11 -070085 // See the ResolveRelativeEncodedDexLocation for a description of how the
86 // abs_dex_location argument is used.
Nicolas Geoffray96dca1c2018-04-19 14:43:29 +010087 static OatFile* Open(int zip_fd,
88 const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -080089 const std::string& location,
Ian Rogers13735952014-10-08 12:43:28 -070090 uint8_t* requested_base,
Igor Murashkin46774762014-10-22 11:37:02 -070091 uint8_t* oat_file_begin,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070092 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -080093 bool low_4gb,
Richard Uhlere5fed032015-03-18 08:21:11 -070094 const char* abs_dex_location,
Igor Murashkinb1d8c312015-08-04 11:18:43 -070095 std::string* error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -070096
Shubham Ajmerab22dea02017-10-04 18:36:41 -070097 // Similar to OatFile::Open(const std::string...), but accepts input vdex and
Nicolas Geoffray96dca1c2018-04-19 14:43:29 +010098 // odex files as file descriptors. We also take zip_fd in case the vdex does not
99 // contain the dex code, and we need to read it from the zip file.
100 static OatFile* Open(int zip_fd,
101 int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700102 int oat_fd,
103 const std::string& oat_location,
104 uint8_t* requested_base,
105 uint8_t* oat_file_begin,
106 bool executable,
107 bool low_4gb,
108 const char* abs_dex_location,
109 std::string* error_msg);
110
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800111 // Open an oat file from an already opened File.
Brian Carlstrom265091e2013-01-30 14:08:26 -0800112 // Does not use dlopen underneath so cannot be used for runtime use
113 // where relocations may be required. Currently used from
114 // ImageWriter which wants to open a writable version from an existing
115 // file descriptor for patching.
Nicolas Geoffray96dca1c2018-04-19 14:43:29 +0100116 static OatFile* OpenWritable(int zip_fd,
117 File* file,
118 const std::string& location,
Richard Uhlere5fed032015-03-18 08:21:11 -0700119 const char* abs_dex_location,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700120 std::string* error_msg);
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +0000121 // Open an oat file from an already opened File. Maps it PROT_READ, MAP_PRIVATE.
Nicolas Geoffray96dca1c2018-04-19 14:43:29 +0100122 static OatFile* OpenReadable(int zip_fd,
123 File* file,
124 const std::string& location,
Richard Uhlere5fed032015-03-18 08:21:11 -0700125 const char* abs_dex_location,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700126 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800127
Andreas Gampe049cff02015-12-01 23:27:12 -0800128 virtual ~OatFile();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700129
Alex Light9dcc4572014-08-14 14:16:26 -0700130 bool IsExecutable() const {
131 return is_executable_;
132 }
133
Andreas Gampe7ba64962014-10-23 11:37:40 -0700134 bool IsPic() const;
135
Sebastien Hertz0de11332015-05-13 12:14:05 +0200136 // Indicates whether the oat file was compiled with full debugging capability.
137 bool IsDebuggable() const;
138
Andreas Gampe29d38e72016-03-23 15:31:51 +0000139 CompilerFilter::Filter GetCompilerFilter() const;
Calin Juravleb077e152016-02-18 18:47:37 +0000140
Calin Juravle44e5efa2017-09-12 00:54:26 -0700141 std::string GetClassLoaderContext() const;
142
Calin Juravle0e09dfc2018-02-12 19:01:09 -0800143 const char* GetCompilationReason() const;
144
Brian Carlstrome24fa612011-09-29 00:53:55 -0700145 const std::string& GetLocation() const {
146 return location_;
147 }
148
149 const OatHeader& GetOatHeader() const;
150
Richard Uhler07b3c232015-03-31 15:57:54 -0700151 class OatMethod FINAL {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700152 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700153 void LinkMethod(ArtMethod* method) const;
Brian Carlstromae826982011-11-09 01:33:42 -0800154
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100155 uint32_t GetCodeOffset() const;
Brian Carlstromae826982011-11-09 01:33:42 -0800156
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100157 const void* GetQuickCode() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700158
159 // Returns size of quick code.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800160 uint32_t GetQuickCodeSize() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700161 uint32_t GetQuickCodeSizeOffset() const;
162
163 // Returns OatQuickMethodHeader for debugging. Most callers should
164 // use more specific methods such as GetQuickCodeSize.
165 const OatQuickMethodHeader* GetOatQuickMethodHeader() const;
166 uint32_t GetOatQuickMethodHeaderOffset() const;
Logan Chien0c717dd2012-03-28 18:31:07 +0800167
Vladimir Marko7624d252014-05-02 14:40:15 +0100168 size_t GetFrameSizeInBytes() const;
169 uint32_t GetCoreSpillMask() const;
170 uint32_t GetFpSpillMask() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700171
Vladimir Marko8a630572014-04-09 18:45:35 +0100172 const uint8_t* GetVmapTable() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700173 uint32_t GetVmapTableOffset() const;
174 uint32_t GetVmapTableOffsetOffset() const;
Vladimir Marko8a630572014-04-09 18:45:35 +0100175
Brian Carlstromae826982011-11-09 01:33:42 -0800176 // Create an OatMethod with offsets relative to the given base address
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800177 OatMethod(const uint8_t* base, const uint32_t code_offset)
178 : begin_(base), code_offset_(code_offset) {
Ian Rogers97b52f82014-08-14 11:34:07 -0700179 }
Andreas Gampe758a8012015-04-03 21:28:42 -0700180 OatMethod(const OatMethod&) = default;
Ian Rogers97b52f82014-08-14 11:34:07 -0700181 ~OatMethod() {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700182
Andreas Gampe758a8012015-04-03 21:28:42 -0700183 OatMethod& operator=(const OatMethod&) = default;
184
Ian Rogers97b52f82014-08-14 11:34:07 -0700185 // A representation of an invalid OatMethod, used when an OatMethod or OatClass can't be found.
186 // See ClassLinker::FindOatMethodFor.
187 static const OatMethod Invalid() {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800188 return OatMethod(nullptr, -1);
Ian Rogers97b52f82014-08-14 11:34:07 -0700189 }
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100190
Brian Carlstromae826982011-11-09 01:33:42 -0800191 private:
192 template<class T>
193 T GetOatPointer(uint32_t offset) const {
194 if (offset == 0) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700195 return nullptr;
Brian Carlstromae826982011-11-09 01:33:42 -0800196 }
Ian Rogers30fab402012-01-23 15:43:46 -0800197 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800198 }
199
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800200 const uint8_t* begin_;
201 uint32_t code_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800202
203 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700204 };
205
Richard Uhler07b3c232015-03-31 15:57:54 -0700206 class OatClass FINAL {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700207 public:
Vladimir Marko2c64a832018-01-04 11:31:56 +0000208 ClassStatus GetStatus() const {
Brian Carlstromba150c32013-08-27 17:31:03 -0700209 return status_;
210 }
211
212 OatClassType GetType() const {
213 return type_;
214 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800215
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700216 // Get the OatMethod entry based on its index into the class
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700217 // defintion. Direct methods come first, followed by virtual
218 // methods. Note that runtime created methods such as miranda
Brian Carlstrome24fa612011-09-29 00:53:55 -0700219 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700220 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700221
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700222 // Return a pointer to the OatMethodOffsets for the requested
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700223 // method_index, or null if none is present. Note that most
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700224 // callers should use GetOatMethod.
225 const OatMethodOffsets* GetOatMethodOffsets(uint32_t method_index) const;
226
227 // Return the offset from the start of the OatFile to the
228 // OatMethodOffsets for the requested method_index, or 0 if none
229 // is present. Note that most callers should use GetOatMethod.
230 uint32_t GetOatMethodOffsetsOffset(uint32_t method_index) const;
231
Ian Rogers97b52f82014-08-14 11:34:07 -0700232 // A representation of an invalid OatClass, used when an OatClass can't be found.
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100233 // See FindOatClass().
Ian Rogers97b52f82014-08-14 11:34:07 -0700234 static OatClass Invalid() {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000235 return OatClass(/* oat_file */ nullptr,
Vladimir Marko2c64a832018-01-04 11:31:56 +0000236 ClassStatus::kErrorUnresolved,
Vladimir Marko72ab6842017-01-20 19:32:50 +0000237 kOatClassNoneCompiled,
238 /* bitmap_size */ 0,
239 /* bitmap_pointer */ nullptr,
240 /* methods_pointer */ nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700241 }
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100242
Brian Carlstrome24fa612011-09-29 00:53:55 -0700243 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800244 OatClass(const OatFile* oat_file,
Vladimir Marko2c64a832018-01-04 11:31:56 +0000245 ClassStatus status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700246 OatClassType type,
247 uint32_t bitmap_size,
248 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800249 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700250
Ian Rogers97b52f82014-08-14 11:34:07 -0700251 const OatFile* const oat_file_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700252
Vladimir Marko2c64a832018-01-04 11:31:56 +0000253 const ClassStatus status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700254
Ian Rogers97b52f82014-08-14 11:34:07 -0700255 const OatClassType type_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700256
Ian Rogers97b52f82014-08-14 11:34:07 -0700257 const uint32_t* const bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700258
Ian Rogers97b52f82014-08-14 11:34:07 -0700259 const OatMethodOffsets* const methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700260
Richard Uhler07b3c232015-03-31 15:57:54 -0700261 friend class art::OatDexFile;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700262 };
Richard Uhler9a37efc2016-08-05 16:32:55 -0700263
264 // Get the OatDexFile for the given dex_location within this oat file.
265 // If dex_location_checksum is non-null, the OatDexFile will only be
266 // returned if it has a matching checksum.
267 // If error_msg is non-null and no OatDexFile is returned, error_msg will
268 // be updated with a description of why no OatDexFile was returned.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700269 const OatDexFile* GetOatDexFile(const char* dex_location,
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700270 const uint32_t* const dex_location_checksum,
Richard Uhler9a37efc2016-08-05 16:32:55 -0700271 /*out*/std::string* error_msg = nullptr) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700272 REQUIRES(!secondary_lookup_lock_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700273
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100274 const std::vector<const OatDexFile*>& GetOatDexFiles() const {
275 return oat_dex_files_storage_;
276 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700277
Ian Rogers30fab402012-01-23 15:43:46 -0800278 size_t Size() const {
279 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700280 }
281
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800282 bool Contains(const void* p) const {
283 return p >= Begin() && p < End();
284 }
285
Vladimir Marko5c42c292015-02-25 12:02:49 +0000286 size_t BssSize() const {
287 return BssEnd() - BssBegin();
288 }
289
David Srbeckyec2cdf42017-12-08 16:21:25 +0000290 size_t VdexSize() const {
291 return VdexEnd() - VdexBegin();
292 }
293
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100294 size_t BssMethodsOffset() const {
295 // Note: This is used only for symbolizer and needs to return a valid .bss offset.
296 return (bss_methods_ != nullptr) ? bss_methods_ - BssBegin() : BssRootsOffset();
297 }
298
Vladimir Markoaad75c62016-10-03 08:46:48 +0000299 size_t BssRootsOffset() const {
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100300 // Note: This is used only for symbolizer and needs to return a valid .bss offset.
301 return (bss_roots_ != nullptr) ? bss_roots_ - BssBegin() : BssSize();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000302 }
303
David Brazdil7b49e6c2016-09-01 11:06:18 +0100304 size_t DexSize() const {
305 return DexEnd() - DexBegin();
306 }
307
Ian Rogers13735952014-10-08 12:43:28 -0700308 const uint8_t* Begin() const;
309 const uint8_t* End() const;
Alex Light53cb16b2014-06-12 11:26:29 -0700310
Vladimir Marko5c42c292015-02-25 12:02:49 +0000311 const uint8_t* BssBegin() const;
312 const uint8_t* BssEnd() const;
313
David Srbeckyec2cdf42017-12-08 16:21:25 +0000314 const uint8_t* VdexBegin() const;
315 const uint8_t* VdexEnd() const;
316
David Brazdil7b49e6c2016-09-01 11:06:18 +0100317 const uint8_t* DexBegin() const;
318 const uint8_t* DexEnd() const;
319
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100320 ArrayRef<ArtMethod*> GetBssMethods() const;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000321 ArrayRef<GcRoot<mirror::Object>> GetBssGcRoots() const;
322
Richard Uhlere5fed032015-03-18 08:21:11 -0700323 // Returns the absolute dex location for the encoded relative dex location.
324 //
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700325 // If not null, abs_dex_location is used to resolve the absolute dex
Richard Uhlere5fed032015-03-18 08:21:11 -0700326 // location of relative dex locations encoded in the oat file.
327 // For example, given absolute location "/data/app/foo/base.apk", encoded
Calin Juravlea308a322017-07-18 16:51:51 -0700328 // dex locations "base.apk", "base.apk!classes2.dex", etc. would be resolved
329 // to "/data/app/foo/base.apk", "/data/app/foo/base.apk!classes2.dex", etc.
Richard Uhlere5fed032015-03-18 08:21:11 -0700330 // Relative encoded dex locations that don't match the given abs_dex_location
331 // are left unchanged.
332 static std::string ResolveRelativeEncodedDexLocation(
333 const char* abs_dex_location, const std::string& rel_dex_location);
334
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100335 // Finds the associated oat class for a dex_file and descriptor. Returns an invalid OatClass on
336 // error and sets found to false.
337 static OatClass FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, bool* found);
338
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100339 VdexFile* GetVdexFile() const {
340 return vdex_.get();
341 }
342
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000343 // Whether the OatFile embeds the Dex code.
344 bool ContainsDexCode() const {
345 return uncompressed_dex_files_ == nullptr;
346 }
347
Andreas Gampe049cff02015-12-01 23:27:12 -0800348 protected:
349 OatFile(const std::string& filename, bool executable);
350
Brian Carlstrome24fa612011-09-29 00:53:55 -0700351 private:
Brian Carlstrome24fa612011-09-29 00:53:55 -0700352 // The oat file name.
353 //
354 // The image will embed this to link its associated oat file.
355 const std::string location_;
356
David Brazdil7b49e6c2016-09-01 11:06:18 +0100357 // Pointer to the Vdex file with the Dex files for this Oat file.
358 std::unique_ptr<VdexFile> vdex_;
359
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800360 // Pointer to OatHeader.
Ian Rogers13735952014-10-08 12:43:28 -0700361 const uint8_t* begin_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800362
363 // Pointer to end of oat region for bounds checking.
Ian Rogers13735952014-10-08 12:43:28 -0700364 const uint8_t* end_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800365
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700366 // Pointer to the .bss section, if present, otherwise null.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100367 uint8_t* bss_begin_;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000368
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700369 // Pointer to the end of the .bss section, if present, otherwise null.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100370 uint8_t* bss_end_;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000371
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100372 // Pointer to the beginning of the ArtMethod*s in .bss section, if present, otherwise null.
373 uint8_t* bss_methods_;
374
Vladimir Markoaad75c62016-10-03 08:46:48 +0000375 // Pointer to the beginning of the GC roots in .bss section, if present, otherwise null.
376 uint8_t* bss_roots_;
377
Alex Light9dcc4572014-08-14 14:16:26 -0700378 // Was this oat_file loaded executable?
379 const bool is_executable_;
380
David Srbeckyec2cdf42017-12-08 16:21:25 +0000381 // Pointer to the .vdex section, if present, otherwise null.
382 uint8_t* vdex_begin_;
383
384 // Pointer to the end of the .vdex section, if present, otherwise null.
385 uint8_t* vdex_end_;
386
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100387 // Owning storage for the OatDexFile objects.
388 std::vector<const OatDexFile*> oat_dex_files_storage_;
389
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100390 // NOTE: We use a StringPiece as the key type to avoid a memory allocation on every
391 // lookup with a const char* key. The StringPiece doesn't own its backing storage,
392 // therefore we're using the OatDexFile::dex_file_location_ as the backing storage
393 // for keys in oat_dex_files_ and the string_cache_ entries for the backing storage
394 // of keys in secondary_oat_dex_files_ and oat_dex_files_by_canonical_location_.
Mathieu Chartierbad02672014-08-25 13:08:22 -0700395 typedef AllocationTrackingSafeMap<StringPiece, const OatDexFile*, kAllocatorTagOatFile> Table;
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100396
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100397 // Map each location and canonical location (if different) retrieved from the
398 // oat file to its OatDexFile. This map doesn't change after it's constructed in Setup()
399 // and therefore doesn't need any locking and provides the cheapest dex file lookup
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700400 // for GetOatDexFile() for a very frequent use case. Never contains a null value.
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100401 Table oat_dex_files_;
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100402
403 // Lock guarding all members needed for secondary lookup in GetOatDexFile().
404 mutable Mutex secondary_lookup_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
405
406 // If the primary oat_dex_files_ lookup fails, use a secondary map. This map stores
407 // the results of all previous secondary lookups, whether successful (non-null) or
408 // failed (null). If it doesn't contain an entry we need to calculate the canonical
409 // location and use oat_dex_files_by_canonical_location_.
410 mutable Table secondary_oat_dex_files_ GUARDED_BY(secondary_lookup_lock_);
411
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100412 // Cache of strings. Contains the backing storage for keys in the secondary_oat_dex_files_
413 // and the lazily initialized oat_dex_files_by_canonical_location_.
414 // NOTE: We're keeping references to contained strings in form of StringPiece and adding
415 // new strings to the end. The adding of a new element must not touch any previously stored
416 // elements. std::list<> and std::deque<> satisfy this requirement, std::vector<> doesn't.
417 mutable std::list<std::string> string_cache_ GUARDED_BY(secondary_lookup_lock_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700418
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000419 // Cache of dex files mapped directly from a location, in case the OatFile does
420 // not embed the dex code.
421 std::unique_ptr<std::vector<std::unique_ptr<const DexFile>>> uncompressed_dex_files_;
422
Mathieu Chartier5351da02016-02-17 16:19:53 -0800423 friend class gc::collector::DummyOatFile; // For modifying begin_ and end_.
Brian Carlstrome24fa612011-09-29 00:53:55 -0700424 friend class OatClass;
Richard Uhler07b3c232015-03-31 15:57:54 -0700425 friend class art::OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800426 friend class OatDumper; // For GetBase and GetLimit
Andreas Gampe049cff02015-12-01 23:27:12 -0800427 friend class OatFileBase;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700428 DISALLOW_COPY_AND_ASSIGN(OatFile);
429};
430
Richard Uhler07b3c232015-03-31 15:57:54 -0700431// OatDexFile should be an inner class of OatFile. Unfortunately, C++ doesn't
432// support forward declarations of inner classes, and we want to
433// forward-declare OatDexFile so that we can store an opaque pointer to an
434// OatDexFile in DexFile.
435class OatDexFile FINAL {
436 public:
437 // Opens the DexFile referred to by this OatDexFile from within the containing OatFile.
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700438 std::unique_ptr<const DexFile> OpenDexFile(std::string* error_msg) const;
Richard Uhler07b3c232015-03-31 15:57:54 -0700439
Mathieu Chartier1b868492016-11-16 16:22:37 -0800440 // May return null if the OatDexFile only contains a type lookup table. This case only happens
441 // for the compiler to speed up compilation.
Richard Uhler07b3c232015-03-31 15:57:54 -0700442 const OatFile* GetOatFile() const {
Mathieu Chartier1b868492016-11-16 16:22:37 -0800443 // Avoid pulling in runtime.h in the header file.
444 if (kIsDebugBuild && oat_file_ == nullptr) {
445 AssertAotCompiler();
446 }
Richard Uhler07b3c232015-03-31 15:57:54 -0700447 return oat_file_;
448 }
449
450 // Returns the size of the DexFile refered to by this OatDexFile.
451 size_t FileSize() const;
452
453 // Returns original path of DexFile that was the source of this OatDexFile.
454 const std::string& GetDexFileLocation() const {
455 return dex_file_location_;
456 }
457
458 // Returns the canonical location of DexFile that was the source of this OatDexFile.
459 const std::string& GetCanonicalDexFileLocation() const {
460 return canonical_dex_file_location_;
461 }
462
463 // Returns checksum of original DexFile that was the source of this OatDexFile;
464 uint32_t GetDexFileLocationChecksum() const {
465 return dex_file_location_checksum_;
466 }
467
468 // Returns the OatClass for the class specified by the given DexFile class_def_index.
469 OatFile::OatClass GetOatClass(uint16_t class_def_index) const;
470
471 // Returns the offset to the OatClass information. Most callers should use GetOatClass.
472 uint32_t GetOatClassOffset(uint16_t class_def_index) const;
473
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300474 const uint8_t* GetLookupTableData() const {
475 return lookup_table_data_;
476 }
477
Vladimir Markof3c52b42017-11-17 17:32:12 +0000478 const IndexBssMapping* GetMethodBssMapping() const {
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100479 return method_bss_mapping_;
480 }
481
Vladimir Markof3c52b42017-11-17 17:32:12 +0000482 const IndexBssMapping* GetTypeBssMapping() const {
483 return type_bss_mapping_;
484 }
485
486 const IndexBssMapping* GetStringBssMapping() const {
487 return string_bss_mapping_;
488 }
489
Andreas Gampe2ba88952016-04-29 17:52:07 -0700490 const uint8_t* GetDexFilePointer() const {
491 return dex_file_pointer_;
492 }
493
David Sehr9aa352e2016-09-15 18:13:52 -0700494 // Looks up a class definition by its class descriptor. Hash must be
495 // ComputeModifiedUtf8Hash(descriptor).
496 static const DexFile::ClassDef* FindClassDef(const DexFile& dex_file,
497 const char* descriptor,
498 size_t hash);
499
Mathieu Chartier120aa282017-08-05 16:03:03 -0700500 // Madvise the dex file based on the state we are moving to.
501 static void MadviseDexFile(const DexFile& dex_file, MadviseState state);
502
David Sehr9aa352e2016-09-15 18:13:52 -0700503 TypeLookupTable* GetTypeLookupTable() const {
504 return lookup_table_.get();
505 }
506
Richard Uhler07b3c232015-03-31 15:57:54 -0700507 ~OatDexFile();
508
Mathieu Chartier1b868492016-11-16 16:22:37 -0800509 // Create only with a type lookup table, used by the compiler to speed up compilation.
510 explicit OatDexFile(std::unique_ptr<TypeLookupTable>&& lookup_table);
511
Mathieu Chartier120aa282017-08-05 16:03:03 -0700512 // Return the dex layout sections.
513 const DexLayoutSections* GetDexLayoutSections() const {
514 return dex_layout_sections_;
515 }
516
Richard Uhler07b3c232015-03-31 15:57:54 -0700517 private:
518 OatDexFile(const OatFile* oat_file,
519 const std::string& dex_file_location,
520 const std::string& canonical_dex_file_location,
521 uint32_t dex_file_checksum,
522 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300523 const uint8_t* lookup_table_data,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000524 const IndexBssMapping* method_bss_mapping,
525 const IndexBssMapping* type_bss_mapping,
526 const IndexBssMapping* string_bss_mapping,
Vladimir Marko09d09432015-09-08 13:47:48 +0100527 const uint32_t* oat_class_offsets_pointer,
Mathieu Chartier120aa282017-08-05 16:03:03 -0700528 const DexLayoutSections* dex_layout_sections);
Richard Uhler07b3c232015-03-31 15:57:54 -0700529
Mathieu Chartier1b868492016-11-16 16:22:37 -0800530 static void AssertAotCompiler();
531
532 const OatFile* const oat_file_ = nullptr;
Richard Uhler07b3c232015-03-31 15:57:54 -0700533 const std::string dex_file_location_;
534 const std::string canonical_dex_file_location_;
Mathieu Chartier1b868492016-11-16 16:22:37 -0800535 const uint32_t dex_file_location_checksum_ = 0u;
536 const uint8_t* const dex_file_pointer_ = nullptr;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100537 const uint8_t* const lookup_table_data_ = nullptr;
Vladimir Markof3c52b42017-11-17 17:32:12 +0000538 const IndexBssMapping* const method_bss_mapping_ = nullptr;
539 const IndexBssMapping* const type_bss_mapping_ = nullptr;
540 const IndexBssMapping* const string_bss_mapping_ = nullptr;
Mathieu Chartier1b868492016-11-16 16:22:37 -0800541 const uint32_t* const oat_class_offsets_pointer_ = 0u;
David Sehr9aa352e2016-09-15 18:13:52 -0700542 mutable std::unique_ptr<TypeLookupTable> lookup_table_;
Mathieu Chartier120aa282017-08-05 16:03:03 -0700543 const DexLayoutSections* const dex_layout_sections_ = nullptr;
Richard Uhler07b3c232015-03-31 15:57:54 -0700544
545 friend class OatFile;
Andreas Gampe049cff02015-12-01 23:27:12 -0800546 friend class OatFileBase;
Richard Uhler07b3c232015-03-31 15:57:54 -0700547 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
548};
549
Brian Carlstrome24fa612011-09-29 00:53:55 -0700550} // namespace art
551
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700552#endif // ART_RUNTIME_OAT_FILE_H_