blob: 3e9c01f9c2d0b73dc53e6cc52f7ef9f4b010e1af [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 Sehrc431b9d2018-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"
Vladimir Marko2c64a832018-01-04 11:31:56 +000030#include "class_status.h"
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010031#include "compiler_filter.h"
David Sehr9e734c72018-01-04 17:56:19 -080032#include "dex/dex_file_layout.h"
David Sehr9c4a0152018-04-05 12:23:54 -070033#include "dex/type_lookup_table.h"
David Sehr0225f8e2018-01-31 08:52:24 +000034#include "dex/utf.h"
Vladimir Markof3c52b42017-11-17 17:32:12 +000035#include "index_bss_mapping.h"
Vladimir Marko2c64a832018-01-04 11:31:56 +000036#include "mirror/object.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "oat.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070038
39namespace art {
40
Brian Carlstromba150c32013-08-27 17:31:03 -070041class BitVector;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080042class DexFile;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080043class ElfFile;
Mathieu Chartier120aa282017-08-05 16:03:03 -070044class DexLayoutSections;
Vladimir Markoaad75c62016-10-03 08:46:48 +000045template <class MirrorType> class GcRoot;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080046class MemMap;
Richard Uhler07b3c232015-03-31 15:57:54 -070047class OatDexFile;
Andreas Gampe513061a2017-06-01 09:17:34 -070048class OatHeader;
49class OatMethodOffsets;
50class OatQuickMethodHeader;
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010051class VdexFile;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080052
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080053namespace dex {
54struct ClassDef;
55} // namespace dex
56
Mathieu Chartier5351da02016-02-17 16:19:53 -080057namespace gc {
58namespace collector {
59class DummyOatFile;
60} // namespace collector
61} // namespace gc
62
David Brazdil7b49e6c2016-09-01 11:06:18 +010063// Runtime representation of the OAT file format which holds compiler output.
64// The class opens an OAT file from storage and maps it to memory, typically with
65// dlopen and provides access to its internal data structures (see OatWriter for
66// for more details about the OAT format).
67// In the process of loading OAT, the class also loads the associated VDEX file
68// with the input DEX files (see VdexFile for details about the VDEX format).
69// The raw DEX data are accessible transparently through the OatDexFile objects.
70
Andreas Gampe049cff02015-12-01 23:27:12 -080071class OatFile {
Brian Carlstrome24fa612011-09-29 00:53:55 -070072 public:
Jeff Haof0192c82016-03-28 20:39:50 -070073 // Special classpath that skips shared library check.
74 static constexpr const char* kSpecialSharedLibrary = "&";
75
Alex Light84d76052014-08-22 17:49:35 -070076 // Opens an oat file contained within the given elf file. This is always opened as
77 // non-executable at the moment.
Nicolas Geoffray30025092018-04-19 14:43:29 +010078 static OatFile* OpenWithElfFile(int zip_fd,
79 ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +010080 VdexFile* vdex_file,
81 const std::string& location,
Richard Uhlere5fed032015-03-18 08:21:11 -070082 const char* abs_dex_location,
Igor Murashkinb1d8c312015-08-04 11:18:43 -070083 std::string* error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070084 // Open an oat file. Returns null on failure. Requested base can
Brian Carlstrome24fa612011-09-29 00:53:55 -070085 // optionally be used to request where the file should be loaded.
Richard Uhlere5fed032015-03-18 08:21:11 -070086 // See the ResolveRelativeEncodedDexLocation for a description of how the
87 // abs_dex_location argument is used.
Nicolas Geoffray30025092018-04-19 14:43:29 +010088 static OatFile* Open(int zip_fd,
89 const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -080090 const std::string& location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070091 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -080092 bool low_4gb,
Richard Uhlere5fed032015-03-18 08:21:11 -070093 const char* abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +010094 /*inout*/MemMap* reservation, // Where to load if not null.
95 /*out*/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 Geoffray30025092018-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,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700104 bool executable,
105 bool low_4gb,
106 const char* abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100107 /*inout*/MemMap* reservation, // Where to load if not null.
108 /*out*/std::string* error_msg);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700109
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800110 // Open an oat file from an already opened File.
Brian Carlstrom265091e2013-01-30 14:08:26 -0800111 // Does not use dlopen underneath so cannot be used for runtime use
112 // where relocations may be required. Currently used from
113 // ImageWriter which wants to open a writable version from an existing
114 // file descriptor for patching.
Nicolas Geoffray30025092018-04-19 14:43:29 +0100115 static OatFile* OpenWritable(int zip_fd,
116 File* file,
117 const std::string& location,
Richard Uhlere5fed032015-03-18 08:21:11 -0700118 const char* abs_dex_location,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700119 std::string* error_msg);
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +0000120 // Open an oat file from an already opened File. Maps it PROT_READ, MAP_PRIVATE.
Nicolas Geoffray30025092018-04-19 14:43:29 +0100121 static OatFile* OpenReadable(int zip_fd,
122 File* file,
123 const std::string& location,
Richard Uhlere5fed032015-03-18 08:21:11 -0700124 const char* abs_dex_location,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700125 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800126
Andreas Gampe049cff02015-12-01 23:27:12 -0800127 virtual ~OatFile();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700128
Alex Light9dcc4572014-08-14 14:16:26 -0700129 bool IsExecutable() const {
130 return is_executable_;
131 }
132
Sebastien Hertz0de11332015-05-13 12:14:05 +0200133 // Indicates whether the oat file was compiled with full debugging capability.
134 bool IsDebuggable() const;
135
Andreas Gampe29d38e72016-03-23 15:31:51 +0000136 CompilerFilter::Filter GetCompilerFilter() const;
Calin Juravleb077e152016-02-18 18:47:37 +0000137
Calin Juravle44e5efa2017-09-12 00:54:26 -0700138 std::string GetClassLoaderContext() const;
139
Calin Juravle0e09dfc2018-02-12 19:01:09 -0800140 const char* GetCompilationReason() const;
141
Brian Carlstrome24fa612011-09-29 00:53:55 -0700142 const std::string& GetLocation() const {
143 return location_;
144 }
145
146 const OatHeader& GetOatHeader() const;
147
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100148 class OatMethod final {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700149 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700150 void LinkMethod(ArtMethod* method) const;
Brian Carlstromae826982011-11-09 01:33:42 -0800151
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100152 uint32_t GetCodeOffset() const;
Brian Carlstromae826982011-11-09 01:33:42 -0800153
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100154 const void* GetQuickCode() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700155
156 // Returns size of quick code.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800157 uint32_t GetQuickCodeSize() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700158 uint32_t GetQuickCodeSizeOffset() const;
159
160 // Returns OatQuickMethodHeader for debugging. Most callers should
161 // use more specific methods such as GetQuickCodeSize.
162 const OatQuickMethodHeader* GetOatQuickMethodHeader() const;
163 uint32_t GetOatQuickMethodHeaderOffset() const;
Logan Chien0c717dd2012-03-28 18:31:07 +0800164
Vladimir Marko7624d252014-05-02 14:40:15 +0100165 size_t GetFrameSizeInBytes() const;
166 uint32_t GetCoreSpillMask() const;
167 uint32_t GetFpSpillMask() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700168
Vladimir Marko8a630572014-04-09 18:45:35 +0100169 const uint8_t* GetVmapTable() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700170 uint32_t GetVmapTableOffset() const;
171 uint32_t GetVmapTableOffsetOffset() const;
Vladimir Marko8a630572014-04-09 18:45:35 +0100172
Brian Carlstromae826982011-11-09 01:33:42 -0800173 // Create an OatMethod with offsets relative to the given base address
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800174 OatMethod(const uint8_t* base, const uint32_t code_offset)
175 : begin_(base), code_offset_(code_offset) {
Ian Rogers97b52f82014-08-14 11:34:07 -0700176 }
Andreas Gampe758a8012015-04-03 21:28:42 -0700177 OatMethod(const OatMethod&) = default;
Ian Rogers97b52f82014-08-14 11:34:07 -0700178 ~OatMethod() {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700179
Andreas Gampe758a8012015-04-03 21:28:42 -0700180 OatMethod& operator=(const OatMethod&) = default;
181
Ian Rogers97b52f82014-08-14 11:34:07 -0700182 // A representation of an invalid OatMethod, used when an OatMethod or OatClass can't be found.
183 // See ClassLinker::FindOatMethodFor.
184 static const OatMethod Invalid() {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800185 return OatMethod(nullptr, -1);
Ian Rogers97b52f82014-08-14 11:34:07 -0700186 }
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100187
Brian Carlstromae826982011-11-09 01:33:42 -0800188 private:
189 template<class T>
190 T GetOatPointer(uint32_t offset) const {
191 if (offset == 0) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700192 return nullptr;
Brian Carlstromae826982011-11-09 01:33:42 -0800193 }
Ian Rogers30fab402012-01-23 15:43:46 -0800194 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800195 }
196
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800197 const uint8_t* begin_;
198 uint32_t code_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800199
200 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700201 };
202
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100203 class OatClass final {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700204 public:
Vladimir Marko2c64a832018-01-04 11:31:56 +0000205 ClassStatus GetStatus() const {
Brian Carlstromba150c32013-08-27 17:31:03 -0700206 return status_;
207 }
208
209 OatClassType GetType() const {
210 return type_;
211 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800212
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700213 // Get the OatMethod entry based on its index into the class
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700214 // defintion. Direct methods come first, followed by virtual
215 // methods. Note that runtime created methods such as miranda
Brian Carlstrome24fa612011-09-29 00:53:55 -0700216 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700217 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700218
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700219 // Return a pointer to the OatMethodOffsets for the requested
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700220 // method_index, or null if none is present. Note that most
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700221 // callers should use GetOatMethod.
222 const OatMethodOffsets* GetOatMethodOffsets(uint32_t method_index) const;
223
224 // Return the offset from the start of the OatFile to the
225 // OatMethodOffsets for the requested method_index, or 0 if none
226 // is present. Note that most callers should use GetOatMethod.
227 uint32_t GetOatMethodOffsetsOffset(uint32_t method_index) const;
228
Ian Rogers97b52f82014-08-14 11:34:07 -0700229 // A representation of an invalid OatClass, used when an OatClass can't be found.
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100230 // See FindOatClass().
Ian Rogers97b52f82014-08-14 11:34:07 -0700231 static OatClass Invalid() {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700232 return OatClass(/* oat_file= */ nullptr,
Vladimir Marko2c64a832018-01-04 11:31:56 +0000233 ClassStatus::kErrorUnresolved,
Vladimir Marko72ab6842017-01-20 19:32:50 +0000234 kOatClassNoneCompiled,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700235 /* bitmap_size= */ 0,
236 /* bitmap_pointer= */ nullptr,
237 /* methods_pointer= */ nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700238 }
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100239
Brian Carlstrome24fa612011-09-29 00:53:55 -0700240 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800241 OatClass(const OatFile* oat_file,
Vladimir Marko2c64a832018-01-04 11:31:56 +0000242 ClassStatus status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700243 OatClassType type,
244 uint32_t bitmap_size,
245 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800246 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700247
Ian Rogers97b52f82014-08-14 11:34:07 -0700248 const OatFile* const oat_file_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700249
Vladimir Marko2c64a832018-01-04 11:31:56 +0000250 const ClassStatus status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700251
Ian Rogers97b52f82014-08-14 11:34:07 -0700252 const OatClassType type_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700253
Ian Rogers97b52f82014-08-14 11:34:07 -0700254 const uint32_t* const bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700255
Ian Rogers97b52f82014-08-14 11:34:07 -0700256 const OatMethodOffsets* const methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700257
Richard Uhler07b3c232015-03-31 15:57:54 -0700258 friend class art::OatDexFile;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700259 };
Richard Uhler9a37efc2016-08-05 16:32:55 -0700260
261 // Get the OatDexFile for the given dex_location within this oat file.
262 // If dex_location_checksum is non-null, the OatDexFile will only be
263 // returned if it has a matching checksum.
264 // If error_msg is non-null and no OatDexFile is returned, error_msg will
265 // be updated with a description of why no OatDexFile was returned.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700266 const OatDexFile* GetOatDexFile(const char* dex_location,
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700267 const uint32_t* const dex_location_checksum,
Richard Uhler9a37efc2016-08-05 16:32:55 -0700268 /*out*/std::string* error_msg = nullptr) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700269 REQUIRES(!secondary_lookup_lock_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700270
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100271 const std::vector<const OatDexFile*>& GetOatDexFiles() const {
272 return oat_dex_files_storage_;
273 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700274
Ian Rogers30fab402012-01-23 15:43:46 -0800275 size_t Size() const {
276 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700277 }
278
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800279 bool Contains(const void* p) const {
280 return p >= Begin() && p < End();
281 }
282
Vladimir Markob066d432018-01-03 13:14:37 +0000283 size_t DataBimgRelRoSize() const {
284 return DataBimgRelRoEnd() - DataBimgRelRoBegin();
285 }
286
Vladimir Marko5c42c292015-02-25 12:02:49 +0000287 size_t BssSize() const {
288 return BssEnd() - BssBegin();
289 }
290
David Srbeckyec2cdf42017-12-08 16:21:25 +0000291 size_t VdexSize() const {
292 return VdexEnd() - VdexBegin();
293 }
294
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100295 size_t BssMethodsOffset() const {
296 // Note: This is used only for symbolizer and needs to return a valid .bss offset.
297 return (bss_methods_ != nullptr) ? bss_methods_ - BssBegin() : BssRootsOffset();
298 }
299
Vladimir Markoaad75c62016-10-03 08:46:48 +0000300 size_t BssRootsOffset() const {
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100301 // Note: This is used only for symbolizer and needs to return a valid .bss offset.
302 return (bss_roots_ != nullptr) ? bss_roots_ - BssBegin() : BssSize();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000303 }
304
David Brazdil7b49e6c2016-09-01 11:06:18 +0100305 size_t DexSize() const {
306 return DexEnd() - DexBegin();
307 }
308
Ian Rogers13735952014-10-08 12:43:28 -0700309 const uint8_t* Begin() const;
310 const uint8_t* End() const;
Alex Light53cb16b2014-06-12 11:26:29 -0700311
Vladimir Markob066d432018-01-03 13:14:37 +0000312 const uint8_t* DataBimgRelRoBegin() const { return data_bimg_rel_ro_begin_; }
313 const uint8_t* DataBimgRelRoEnd() const { return data_bimg_rel_ro_end_; }
Vladimir Marko5c42c292015-02-25 12:02:49 +0000314
Vladimir Markob066d432018-01-03 13:14:37 +0000315 const uint8_t* BssBegin() const { return bss_begin_; }
316 const uint8_t* BssEnd() const { return bss_end_; }
317
318 const uint8_t* VdexBegin() const { return vdex_begin_; }
319 const uint8_t* VdexEnd() const { return vdex_end_; }
David Srbeckyec2cdf42017-12-08 16:21:25 +0000320
David Brazdil7b49e6c2016-09-01 11:06:18 +0100321 const uint8_t* DexBegin() const;
322 const uint8_t* DexEnd() const;
323
Vladimir Markob066d432018-01-03 13:14:37 +0000324 ArrayRef<const uint32_t> GetBootImageRelocations() const;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100325 ArrayRef<ArtMethod*> GetBssMethods() const;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000326 ArrayRef<GcRoot<mirror::Object>> GetBssGcRoots() const;
327
Richard Uhlere5fed032015-03-18 08:21:11 -0700328 // Returns the absolute dex location for the encoded relative dex location.
329 //
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700330 // If not null, abs_dex_location is used to resolve the absolute dex
Richard Uhlere5fed032015-03-18 08:21:11 -0700331 // location of relative dex locations encoded in the oat file.
332 // For example, given absolute location "/data/app/foo/base.apk", encoded
Calin Juravlea308a322017-07-18 16:51:51 -0700333 // dex locations "base.apk", "base.apk!classes2.dex", etc. would be resolved
334 // to "/data/app/foo/base.apk", "/data/app/foo/base.apk!classes2.dex", etc.
Richard Uhlere5fed032015-03-18 08:21:11 -0700335 // Relative encoded dex locations that don't match the given abs_dex_location
336 // are left unchanged.
337 static std::string ResolveRelativeEncodedDexLocation(
338 const char* abs_dex_location, const std::string& rel_dex_location);
339
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100340 // Finds the associated oat class for a dex_file and descriptor. Returns an invalid OatClass on
341 // error and sets found to false.
342 static OatClass FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, bool* found);
343
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100344 VdexFile* GetVdexFile() const {
345 return vdex_.get();
346 }
347
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000348 // Whether the OatFile embeds the Dex code.
349 bool ContainsDexCode() const {
350 return uncompressed_dex_files_ == nullptr;
351 }
352
Andreas Gampe049cff02015-12-01 23:27:12 -0800353 protected:
354 OatFile(const std::string& filename, bool executable);
355
Brian Carlstrome24fa612011-09-29 00:53:55 -0700356 private:
Brian Carlstrome24fa612011-09-29 00:53:55 -0700357 // The oat file name.
358 //
359 // The image will embed this to link its associated oat file.
360 const std::string location_;
361
David Brazdil7b49e6c2016-09-01 11:06:18 +0100362 // Pointer to the Vdex file with the Dex files for this Oat file.
363 std::unique_ptr<VdexFile> vdex_;
364
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800365 // Pointer to OatHeader.
Ian Rogers13735952014-10-08 12:43:28 -0700366 const uint8_t* begin_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800367
368 // Pointer to end of oat region for bounds checking.
Ian Rogers13735952014-10-08 12:43:28 -0700369 const uint8_t* end_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800370
Vladimir Markob066d432018-01-03 13:14:37 +0000371 // Pointer to the .data.bimg.rel.ro section, if present, otherwise null.
372 const uint8_t* data_bimg_rel_ro_begin_;
373
374 // Pointer to the end of the .data.bimg.rel.ro section, if present, otherwise null.
375 const uint8_t* data_bimg_rel_ro_end_;
376
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700377 // Pointer to the .bss section, if present, otherwise null.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100378 uint8_t* bss_begin_;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000379
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700380 // Pointer to the end of the .bss section, if present, otherwise null.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100381 uint8_t* bss_end_;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000382
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100383 // Pointer to the beginning of the ArtMethod*s in .bss section, if present, otherwise null.
384 uint8_t* bss_methods_;
385
Vladimir Markoaad75c62016-10-03 08:46:48 +0000386 // Pointer to the beginning of the GC roots in .bss section, if present, otherwise null.
387 uint8_t* bss_roots_;
388
Alex Light9dcc4572014-08-14 14:16:26 -0700389 // Was this oat_file loaded executable?
390 const bool is_executable_;
391
David Srbeckyec2cdf42017-12-08 16:21:25 +0000392 // Pointer to the .vdex section, if present, otherwise null.
393 uint8_t* vdex_begin_;
394
395 // Pointer to the end of the .vdex section, if present, otherwise null.
396 uint8_t* vdex_end_;
397
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100398 // Owning storage for the OatDexFile objects.
399 std::vector<const OatDexFile*> oat_dex_files_storage_;
400
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100401 // NOTE: We use a StringPiece as the key type to avoid a memory allocation on every
402 // lookup with a const char* key. The StringPiece doesn't own its backing storage,
403 // therefore we're using the OatDexFile::dex_file_location_ as the backing storage
404 // for keys in oat_dex_files_ and the string_cache_ entries for the backing storage
405 // of keys in secondary_oat_dex_files_ and oat_dex_files_by_canonical_location_.
Mathieu Chartierbad02672014-08-25 13:08:22 -0700406 typedef AllocationTrackingSafeMap<StringPiece, const OatDexFile*, kAllocatorTagOatFile> Table;
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100407
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100408 // Map each location and canonical location (if different) retrieved from the
409 // oat file to its OatDexFile. This map doesn't change after it's constructed in Setup()
410 // and therefore doesn't need any locking and provides the cheapest dex file lookup
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700411 // for GetOatDexFile() for a very frequent use case. Never contains a null value.
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100412 Table oat_dex_files_;
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100413
414 // Lock guarding all members needed for secondary lookup in GetOatDexFile().
415 mutable Mutex secondary_lookup_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
416
417 // If the primary oat_dex_files_ lookup fails, use a secondary map. This map stores
418 // the results of all previous secondary lookups, whether successful (non-null) or
419 // failed (null). If it doesn't contain an entry we need to calculate the canonical
420 // location and use oat_dex_files_by_canonical_location_.
421 mutable Table secondary_oat_dex_files_ GUARDED_BY(secondary_lookup_lock_);
422
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100423 // Cache of strings. Contains the backing storage for keys in the secondary_oat_dex_files_
424 // and the lazily initialized oat_dex_files_by_canonical_location_.
425 // NOTE: We're keeping references to contained strings in form of StringPiece and adding
426 // new strings to the end. The adding of a new element must not touch any previously stored
427 // elements. std::list<> and std::deque<> satisfy this requirement, std::vector<> doesn't.
428 mutable std::list<std::string> string_cache_ GUARDED_BY(secondary_lookup_lock_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700429
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000430 // Cache of dex files mapped directly from a location, in case the OatFile does
431 // not embed the dex code.
432 std::unique_ptr<std::vector<std::unique_ptr<const DexFile>>> uncompressed_dex_files_;
433
Mathieu Chartier5351da02016-02-17 16:19:53 -0800434 friend class gc::collector::DummyOatFile; // For modifying begin_ and end_.
Brian Carlstrome24fa612011-09-29 00:53:55 -0700435 friend class OatClass;
Richard Uhler07b3c232015-03-31 15:57:54 -0700436 friend class art::OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800437 friend class OatDumper; // For GetBase and GetLimit
Andreas Gampe049cff02015-12-01 23:27:12 -0800438 friend class OatFileBase;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700439 DISALLOW_COPY_AND_ASSIGN(OatFile);
440};
441
Richard Uhler07b3c232015-03-31 15:57:54 -0700442// OatDexFile should be an inner class of OatFile. Unfortunately, C++ doesn't
443// support forward declarations of inner classes, and we want to
444// forward-declare OatDexFile so that we can store an opaque pointer to an
445// OatDexFile in DexFile.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100446class OatDexFile final {
Richard Uhler07b3c232015-03-31 15:57:54 -0700447 public:
448 // Opens the DexFile referred to by this OatDexFile from within the containing OatFile.
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700449 std::unique_ptr<const DexFile> OpenDexFile(std::string* error_msg) const;
Richard Uhler07b3c232015-03-31 15:57:54 -0700450
Mathieu Chartier1b868492016-11-16 16:22:37 -0800451 // May return null if the OatDexFile only contains a type lookup table. This case only happens
452 // for the compiler to speed up compilation.
Richard Uhler07b3c232015-03-31 15:57:54 -0700453 const OatFile* GetOatFile() const {
Mathieu Chartier1b868492016-11-16 16:22:37 -0800454 // Avoid pulling in runtime.h in the header file.
455 if (kIsDebugBuild && oat_file_ == nullptr) {
456 AssertAotCompiler();
457 }
Richard Uhler07b3c232015-03-31 15:57:54 -0700458 return oat_file_;
459 }
460
461 // Returns the size of the DexFile refered to by this OatDexFile.
462 size_t FileSize() const;
463
464 // Returns original path of DexFile that was the source of this OatDexFile.
465 const std::string& GetDexFileLocation() const {
466 return dex_file_location_;
467 }
468
469 // Returns the canonical location of DexFile that was the source of this OatDexFile.
470 const std::string& GetCanonicalDexFileLocation() const {
471 return canonical_dex_file_location_;
472 }
473
474 // Returns checksum of original DexFile that was the source of this OatDexFile;
475 uint32_t GetDexFileLocationChecksum() const {
476 return dex_file_location_checksum_;
477 }
478
479 // Returns the OatClass for the class specified by the given DexFile class_def_index.
480 OatFile::OatClass GetOatClass(uint16_t class_def_index) const;
481
482 // Returns the offset to the OatClass information. Most callers should use GetOatClass.
483 uint32_t GetOatClassOffset(uint16_t class_def_index) const;
484
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300485 const uint8_t* GetLookupTableData() const {
486 return lookup_table_data_;
487 }
488
Vladimir Markof3c52b42017-11-17 17:32:12 +0000489 const IndexBssMapping* GetMethodBssMapping() const {
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100490 return method_bss_mapping_;
491 }
492
Vladimir Markof3c52b42017-11-17 17:32:12 +0000493 const IndexBssMapping* GetTypeBssMapping() const {
494 return type_bss_mapping_;
495 }
496
497 const IndexBssMapping* GetStringBssMapping() const {
498 return string_bss_mapping_;
499 }
500
Andreas Gampe2ba88952016-04-29 17:52:07 -0700501 const uint8_t* GetDexFilePointer() const {
502 return dex_file_pointer_;
503 }
504
David Sehr9aa352e2016-09-15 18:13:52 -0700505 // Looks up a class definition by its class descriptor. Hash must be
506 // ComputeModifiedUtf8Hash(descriptor).
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800507 static const dex::ClassDef* FindClassDef(const DexFile& dex_file,
508 const char* descriptor,
509 size_t hash);
David Sehr9aa352e2016-09-15 18:13:52 -0700510
Mathieu Chartier120aa282017-08-05 16:03:03 -0700511 // Madvise the dex file based on the state we are moving to.
512 static void MadviseDexFile(const DexFile& dex_file, MadviseState state);
513
Vladimir Markoea341d22018-05-11 10:33:37 +0100514 const TypeLookupTable& GetTypeLookupTable() const {
515 return lookup_table_;
David Sehr9aa352e2016-09-15 18:13:52 -0700516 }
517
Richard Uhler07b3c232015-03-31 15:57:54 -0700518 ~OatDexFile();
519
Mathieu Chartier1b868492016-11-16 16:22:37 -0800520 // Create only with a type lookup table, used by the compiler to speed up compilation.
Vladimir Markoea341d22018-05-11 10:33:37 +0100521 explicit OatDexFile(TypeLookupTable&& lookup_table);
Mathieu Chartier1b868492016-11-16 16:22:37 -0800522
Mathieu Chartier120aa282017-08-05 16:03:03 -0700523 // Return the dex layout sections.
524 const DexLayoutSections* GetDexLayoutSections() const {
525 return dex_layout_sections_;
526 }
527
Richard Uhler07b3c232015-03-31 15:57:54 -0700528 private:
529 OatDexFile(const OatFile* oat_file,
530 const std::string& dex_file_location,
531 const std::string& canonical_dex_file_location,
532 uint32_t dex_file_checksum,
533 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300534 const uint8_t* lookup_table_data,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000535 const IndexBssMapping* method_bss_mapping,
536 const IndexBssMapping* type_bss_mapping,
537 const IndexBssMapping* string_bss_mapping,
Vladimir Marko09d09432015-09-08 13:47:48 +0100538 const uint32_t* oat_class_offsets_pointer,
Mathieu Chartier120aa282017-08-05 16:03:03 -0700539 const DexLayoutSections* dex_layout_sections);
Richard Uhler07b3c232015-03-31 15:57:54 -0700540
Mathieu Chartier1b868492016-11-16 16:22:37 -0800541 static void AssertAotCompiler();
542
543 const OatFile* const oat_file_ = nullptr;
Richard Uhler07b3c232015-03-31 15:57:54 -0700544 const std::string dex_file_location_;
545 const std::string canonical_dex_file_location_;
Mathieu Chartier1b868492016-11-16 16:22:37 -0800546 const uint32_t dex_file_location_checksum_ = 0u;
547 const uint8_t* const dex_file_pointer_ = nullptr;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100548 const uint8_t* const lookup_table_data_ = nullptr;
Vladimir Markof3c52b42017-11-17 17:32:12 +0000549 const IndexBssMapping* const method_bss_mapping_ = nullptr;
550 const IndexBssMapping* const type_bss_mapping_ = nullptr;
551 const IndexBssMapping* const string_bss_mapping_ = nullptr;
Yi Kong4b22b342018-08-02 14:43:21 -0700552 const uint32_t* const oat_class_offsets_pointer_ = nullptr;
Vladimir Markoea341d22018-05-11 10:33:37 +0100553 TypeLookupTable lookup_table_;
Mathieu Chartier120aa282017-08-05 16:03:03 -0700554 const DexLayoutSections* const dex_layout_sections_ = nullptr;
Richard Uhler07b3c232015-03-31 15:57:54 -0700555
556 friend class OatFile;
Andreas Gampe049cff02015-12-01 23:27:12 -0800557 friend class OatFileBase;
Richard Uhler07b3c232015-03-31 15:57:54 -0700558 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
559};
560
Brian Carlstrome24fa612011-09-29 00:53:55 -0700561} // namespace art
562
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700563#endif // ART_RUNTIME_OAT_FILE_H_