blob: 47032d4a8f77f85ea216146e84a5064d0ebcf813 [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>
Vladimir Marko59ae4f92019-02-04 14:06:02 +000022#include <string_view>
Brian Carlstrome24fa612011-09-29 00:53:55 -070023#include <vector>
24
Vladimir Markoaad75c62016-10-03 08:46:48 +000025#include "base/array_ref.h"
Vladimir Marko3f5838d2014-08-07 18:07:18 +010026#include "base/mutex.h"
David Sehrc431b9d2018-03-02 12:01:51 -080027#include "base/os.h"
David Sehr67bf42e2018-02-26 16:43:04 -080028#include "base/safe_map.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"
David Srbecky605a5fe2019-04-24 14:05:21 +010038#include "runtime.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070039
40namespace art {
41
Brian Carlstromba150c32013-08-27 17:31:03 -070042class BitVector;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080043class DexFile;
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
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080054namespace dex {
55struct ClassDef;
56} // namespace dex
57
Mathieu Chartier5351da02016-02-17 16:19:53 -080058namespace gc {
59namespace collector {
60class DummyOatFile;
61} // namespace collector
62} // namespace gc
63
David Brazdil7b49e6c2016-09-01 11:06:18 +010064// Runtime representation of the OAT file format which holds compiler output.
65// The class opens an OAT file from storage and maps it to memory, typically with
66// dlopen and provides access to its internal data structures (see OatWriter for
67// for more details about the OAT format).
68// In the process of loading OAT, the class also loads the associated VDEX file
69// with the input DEX files (see VdexFile for details about the VDEX format).
70// The raw DEX data are accessible transparently through the OatDexFile objects.
71
Andreas Gampe049cff02015-12-01 23:27:12 -080072class OatFile {
Brian Carlstrome24fa612011-09-29 00:53:55 -070073 public:
Jeff Haof0192c82016-03-28 20:39:50 -070074 // Special classpath that skips shared library check.
75 static constexpr const char* kSpecialSharedLibrary = "&";
76
Alex Light84d76052014-08-22 17:49:35 -070077 // Opens an oat file contained within the given elf file. This is always opened as
78 // non-executable at the moment.
Nicolas Geoffray30025092018-04-19 14:43:29 +010079 static OatFile* OpenWithElfFile(int zip_fd,
80 ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +010081 VdexFile* vdex_file,
82 const std::string& location,
Richard Uhlere5fed032015-03-18 08:21:11 -070083 const char* abs_dex_location,
Igor Murashkinb1d8c312015-08-04 11:18:43 -070084 std::string* error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070085 // Open an oat file. Returns null on failure. Requested base can
Brian Carlstrome24fa612011-09-29 00:53:55 -070086 // optionally be used to request where the file should be loaded.
Richard Uhlere5fed032015-03-18 08:21:11 -070087 // See the ResolveRelativeEncodedDexLocation for a description of how the
88 // abs_dex_location argument is used.
Nicolas Geoffray30025092018-04-19 14:43:29 +010089 static OatFile* Open(int zip_fd,
90 const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -080091 const std::string& location,
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,
Vladimir Markoc09cd052018-08-23 16:36:36 +010095 /*inout*/MemMap* reservation, // Where to load if not null.
96 /*out*/std::string* error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -070097
Shubham Ajmerab22dea02017-10-04 18:36:41 -070098 // Similar to OatFile::Open(const std::string...), but accepts input vdex and
Nicolas Geoffray30025092018-04-19 14:43:29 +010099 // odex files as file descriptors. We also take zip_fd in case the vdex does not
100 // contain the dex code, and we need to read it from the zip file.
101 static OatFile* Open(int zip_fd,
102 int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700103 int oat_fd,
104 const std::string& oat_location,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700105 bool executable,
106 bool low_4gb,
107 const char* abs_dex_location,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100108 /*inout*/MemMap* reservation, // Where to load if not null.
109 /*out*/std::string* error_msg);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700110
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 Geoffray30025092018-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 Geoffray30025092018-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
David Brazdil7126c5b2019-03-05 00:02:51 +0000128 // Initialize OatFile instance from an already loaded VdexFile. This assumes
129 // the vdex does not have a dex section and accepts a vector of DexFiles separately.
130 static OatFile* OpenFromVdex(const std::vector<const DexFile*>& dex_files,
131 std::unique_ptr<VdexFile>&& vdex_file,
132 const std::string& location);
133
Andreas Gampe049cff02015-12-01 23:27:12 -0800134 virtual ~OatFile();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700135
Alex Light9dcc4572014-08-14 14:16:26 -0700136 bool IsExecutable() const {
137 return is_executable_;
138 }
139
Sebastien Hertz0de11332015-05-13 12:14:05 +0200140 // Indicates whether the oat file was compiled with full debugging capability.
141 bool IsDebuggable() const;
142
Andreas Gampe29d38e72016-03-23 15:31:51 +0000143 CompilerFilter::Filter GetCompilerFilter() const;
Calin Juravleb077e152016-02-18 18:47:37 +0000144
Calin Juravle44e5efa2017-09-12 00:54:26 -0700145 std::string GetClassLoaderContext() const;
146
Calin Juravle0e09dfc2018-02-12 19:01:09 -0800147 const char* GetCompilationReason() const;
148
Brian Carlstrome24fa612011-09-29 00:53:55 -0700149 const std::string& GetLocation() const {
150 return location_;
151 }
152
153 const OatHeader& GetOatHeader() const;
154
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100155 class OatMethod final {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700156 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700157 void LinkMethod(ArtMethod* method) const;
Brian Carlstromae826982011-11-09 01:33:42 -0800158
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100159 uint32_t GetCodeOffset() const;
Brian Carlstromae826982011-11-09 01:33:42 -0800160
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100161 const void* GetQuickCode() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700162
163 // Returns size of quick code.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800164 uint32_t GetQuickCodeSize() const;
Nicolas Geoffray57083762019-03-05 09:24:45 +0000165 uint32_t GetQuickCodeSizeOffset() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700166
167 // Returns OatQuickMethodHeader for debugging. Most callers should
168 // use more specific methods such as GetQuickCodeSize.
169 const OatQuickMethodHeader* GetOatQuickMethodHeader() const;
170 uint32_t GetOatQuickMethodHeaderOffset() const;
Logan Chien0c717dd2012-03-28 18:31:07 +0800171
Vladimir Marko7624d252014-05-02 14:40:15 +0100172 size_t GetFrameSizeInBytes() const;
173 uint32_t GetCoreSpillMask() const;
174 uint32_t GetFpSpillMask() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700175
Vladimir Marko8a630572014-04-09 18:45:35 +0100176 const uint8_t* GetVmapTable() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700177 uint32_t GetVmapTableOffset() const;
178 uint32_t GetVmapTableOffsetOffset() const;
Vladimir Marko8a630572014-04-09 18:45:35 +0100179
Brian Carlstromae826982011-11-09 01:33:42 -0800180 // Create an OatMethod with offsets relative to the given base address
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800181 OatMethod(const uint8_t* base, const uint32_t code_offset)
182 : begin_(base), code_offset_(code_offset) {
Ian Rogers97b52f82014-08-14 11:34:07 -0700183 }
Andreas Gampe758a8012015-04-03 21:28:42 -0700184 OatMethod(const OatMethod&) = default;
Ian Rogers97b52f82014-08-14 11:34:07 -0700185 ~OatMethod() {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700186
Andreas Gampe758a8012015-04-03 21:28:42 -0700187 OatMethod& operator=(const OatMethod&) = default;
188
Ian Rogers97b52f82014-08-14 11:34:07 -0700189 // A representation of an invalid OatMethod, used when an OatMethod or OatClass can't be found.
190 // See ClassLinker::FindOatMethodFor.
191 static const OatMethod Invalid() {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800192 return OatMethod(nullptr, -1);
Ian Rogers97b52f82014-08-14 11:34:07 -0700193 }
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100194
Brian Carlstromae826982011-11-09 01:33:42 -0800195 private:
196 template<class T>
197 T GetOatPointer(uint32_t offset) const {
198 if (offset == 0) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700199 return nullptr;
Brian Carlstromae826982011-11-09 01:33:42 -0800200 }
Ian Rogers30fab402012-01-23 15:43:46 -0800201 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800202 }
203
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800204 const uint8_t* begin_;
205 uint32_t code_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800206
207 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700208 };
209
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100210 class OatClass final {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700211 public:
Vladimir Marko2c64a832018-01-04 11:31:56 +0000212 ClassStatus GetStatus() const {
Brian Carlstromba150c32013-08-27 17:31:03 -0700213 return status_;
214 }
215
216 OatClassType GetType() const {
217 return type_;
218 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800219
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700220 // Get the OatMethod entry based on its index into the class
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700221 // defintion. Direct methods come first, followed by virtual
222 // methods. Note that runtime created methods such as miranda
Brian Carlstrome24fa612011-09-29 00:53:55 -0700223 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700224 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700225
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700226 // Return a pointer to the OatMethodOffsets for the requested
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700227 // method_index, or null if none is present. Note that most
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700228 // callers should use GetOatMethod.
229 const OatMethodOffsets* GetOatMethodOffsets(uint32_t method_index) const;
230
231 // Return the offset from the start of the OatFile to the
232 // OatMethodOffsets for the requested method_index, or 0 if none
233 // is present. Note that most callers should use GetOatMethod.
234 uint32_t GetOatMethodOffsetsOffset(uint32_t method_index) const;
235
Ian Rogers97b52f82014-08-14 11:34:07 -0700236 // A representation of an invalid OatClass, used when an OatClass can't be found.
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100237 // See FindOatClass().
Ian Rogers97b52f82014-08-14 11:34:07 -0700238 static OatClass Invalid() {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700239 return OatClass(/* oat_file= */ nullptr,
Vladimir Marko2c64a832018-01-04 11:31:56 +0000240 ClassStatus::kErrorUnresolved,
Vladimir Marko72ab6842017-01-20 19:32:50 +0000241 kOatClassNoneCompiled,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700242 /* bitmap_size= */ 0,
243 /* bitmap_pointer= */ nullptr,
244 /* methods_pointer= */ nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700245 }
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100246
Brian Carlstrome24fa612011-09-29 00:53:55 -0700247 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800248 OatClass(const OatFile* oat_file,
Vladimir Marko2c64a832018-01-04 11:31:56 +0000249 ClassStatus status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700250 OatClassType type,
251 uint32_t bitmap_size,
252 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800253 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700254
Ian Rogers97b52f82014-08-14 11:34:07 -0700255 const OatFile* const oat_file_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700256
Vladimir Marko2c64a832018-01-04 11:31:56 +0000257 const ClassStatus status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700258
Ian Rogers97b52f82014-08-14 11:34:07 -0700259 const OatClassType type_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700260
Ian Rogers97b52f82014-08-14 11:34:07 -0700261 const uint32_t* const bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700262
Ian Rogers97b52f82014-08-14 11:34:07 -0700263 const OatMethodOffsets* const methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700264
Richard Uhler07b3c232015-03-31 15:57:54 -0700265 friend class art::OatDexFile;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700266 };
Richard Uhler9a37efc2016-08-05 16:32:55 -0700267
268 // Get the OatDexFile for the given dex_location within this oat file.
269 // If dex_location_checksum is non-null, the OatDexFile will only be
270 // returned if it has a matching checksum.
271 // If error_msg is non-null and no OatDexFile is returned, error_msg will
272 // be updated with a description of why no OatDexFile was returned.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700273 const OatDexFile* GetOatDexFile(const char* dex_location,
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700274 const uint32_t* const dex_location_checksum,
Richard Uhler9a37efc2016-08-05 16:32:55 -0700275 /*out*/std::string* error_msg = nullptr) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700276 REQUIRES(!secondary_lookup_lock_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700277
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100278 const std::vector<const OatDexFile*>& GetOatDexFiles() const {
279 return oat_dex_files_storage_;
280 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700281
Ian Rogers30fab402012-01-23 15:43:46 -0800282 size_t Size() const {
283 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700284 }
285
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800286 bool Contains(const void* p) const {
287 return p >= Begin() && p < End();
288 }
289
Vladimir Markob066d432018-01-03 13:14:37 +0000290 size_t DataBimgRelRoSize() const {
291 return DataBimgRelRoEnd() - DataBimgRelRoBegin();
292 }
293
Vladimir Marko5c42c292015-02-25 12:02:49 +0000294 size_t BssSize() const {
295 return BssEnd() - BssBegin();
296 }
297
David Srbeckyec2cdf42017-12-08 16:21:25 +0000298 size_t VdexSize() const {
299 return VdexEnd() - VdexBegin();
300 }
301
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100302 size_t BssMethodsOffset() const {
303 // Note: This is used only for symbolizer and needs to return a valid .bss offset.
304 return (bss_methods_ != nullptr) ? bss_methods_ - BssBegin() : BssRootsOffset();
305 }
306
Vladimir Markoaad75c62016-10-03 08:46:48 +0000307 size_t BssRootsOffset() const {
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100308 // Note: This is used only for symbolizer and needs to return a valid .bss offset.
309 return (bss_roots_ != nullptr) ? bss_roots_ - BssBegin() : BssSize();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000310 }
311
David Brazdil7b49e6c2016-09-01 11:06:18 +0100312 size_t DexSize() const {
313 return DexEnd() - DexBegin();
314 }
315
Ian Rogers13735952014-10-08 12:43:28 -0700316 const uint8_t* Begin() const;
317 const uint8_t* End() const;
Alex Light53cb16b2014-06-12 11:26:29 -0700318
Vladimir Markob066d432018-01-03 13:14:37 +0000319 const uint8_t* DataBimgRelRoBegin() const { return data_bimg_rel_ro_begin_; }
320 const uint8_t* DataBimgRelRoEnd() const { return data_bimg_rel_ro_end_; }
Vladimir Marko5c42c292015-02-25 12:02:49 +0000321
Vladimir Markob066d432018-01-03 13:14:37 +0000322 const uint8_t* BssBegin() const { return bss_begin_; }
323 const uint8_t* BssEnd() const { return bss_end_; }
324
325 const uint8_t* VdexBegin() const { return vdex_begin_; }
326 const uint8_t* VdexEnd() const { return vdex_end_; }
David Srbeckyec2cdf42017-12-08 16:21:25 +0000327
David Brazdil7b49e6c2016-09-01 11:06:18 +0100328 const uint8_t* DexBegin() const;
329 const uint8_t* DexEnd() const;
330
Vladimir Markob066d432018-01-03 13:14:37 +0000331 ArrayRef<const uint32_t> GetBootImageRelocations() const;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100332 ArrayRef<ArtMethod*> GetBssMethods() const;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000333 ArrayRef<GcRoot<mirror::Object>> GetBssGcRoots() const;
334
Vladimir Marko1cedb4a2019-02-06 14:13:28 +0000335 // Initialize relocation sections (.data.bimg.rel.ro and .bss).
336 void InitializeRelocations() const;
337
David Brazdil3e8aae02019-03-26 18:48:02 +0000338 // Constructs the absolute dex location and/or dex file name for the relative dex
339 // location (`rel_dex_location`) in the oat file, using the `abs_dex_location` of
340 // the dex file this oat belongs to.
341 //
342 // The dex file name and dex location differ when cross compiling where the dex file
343 // name is the host path (for opening files) and dex location is the future path on target.
Richard Uhlere5fed032015-03-18 08:21:11 -0700344 //
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700345 // If not null, abs_dex_location is used to resolve the absolute dex
Richard Uhlere5fed032015-03-18 08:21:11 -0700346 // location of relative dex locations encoded in the oat file.
347 // For example, given absolute location "/data/app/foo/base.apk", encoded
Calin Juravlea308a322017-07-18 16:51:51 -0700348 // dex locations "base.apk", "base.apk!classes2.dex", etc. would be resolved
349 // to "/data/app/foo/base.apk", "/data/app/foo/base.apk!classes2.dex", etc.
Richard Uhlere5fed032015-03-18 08:21:11 -0700350 // Relative encoded dex locations that don't match the given abs_dex_location
351 // are left unchanged.
David Brazdil3e8aae02019-03-26 18:48:02 +0000352 //
353 // Computation of both `dex_file_location` and `dex_file_name` can be skipped
354 // by setting the corresponding out parameter to `nullptr`.
355 static void ResolveRelativeEncodedDexLocation(const char* abs_dex_location,
356 const std::string& rel_dex_location,
357 /* out */ std::string* dex_file_location,
358 /* out */ std::string* dex_file_name = nullptr);
Richard Uhlere5fed032015-03-18 08:21:11 -0700359
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100360 // Finds the associated oat class for a dex_file and descriptor. Returns an invalid OatClass on
361 // error and sets found to false.
362 static OatClass FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, bool* found);
363
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100364 VdexFile* GetVdexFile() const {
365 return vdex_.get();
366 }
367
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000368 // Whether the OatFile embeds the Dex code.
369 bool ContainsDexCode() const {
370 return uncompressed_dex_files_ == nullptr;
371 }
372
Andreas Gampe049cff02015-12-01 23:27:12 -0800373 protected:
374 OatFile(const std::string& filename, bool executable);
375
Brian Carlstrome24fa612011-09-29 00:53:55 -0700376 private:
Brian Carlstrome24fa612011-09-29 00:53:55 -0700377 // The oat file name.
378 //
379 // The image will embed this to link its associated oat file.
380 const std::string location_;
381
David Brazdil7b49e6c2016-09-01 11:06:18 +0100382 // Pointer to the Vdex file with the Dex files for this Oat file.
383 std::unique_ptr<VdexFile> vdex_;
384
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800385 // Pointer to OatHeader.
Ian Rogers13735952014-10-08 12:43:28 -0700386 const uint8_t* begin_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800387
388 // Pointer to end of oat region for bounds checking.
Ian Rogers13735952014-10-08 12:43:28 -0700389 const uint8_t* end_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800390
Vladimir Markob066d432018-01-03 13:14:37 +0000391 // Pointer to the .data.bimg.rel.ro section, if present, otherwise null.
392 const uint8_t* data_bimg_rel_ro_begin_;
393
394 // Pointer to the end of the .data.bimg.rel.ro section, if present, otherwise null.
395 const uint8_t* data_bimg_rel_ro_end_;
396
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700397 // Pointer to the .bss section, if present, otherwise null.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100398 uint8_t* bss_begin_;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000399
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700400 // Pointer to the end of the .bss section, if present, otherwise null.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100401 uint8_t* bss_end_;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000402
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100403 // Pointer to the beginning of the ArtMethod*s in .bss section, if present, otherwise null.
404 uint8_t* bss_methods_;
405
Vladimir Markoaad75c62016-10-03 08:46:48 +0000406 // Pointer to the beginning of the GC roots in .bss section, if present, otherwise null.
407 uint8_t* bss_roots_;
408
Alex Light9dcc4572014-08-14 14:16:26 -0700409 // Was this oat_file loaded executable?
410 const bool is_executable_;
411
David Srbeckyec2cdf42017-12-08 16:21:25 +0000412 // Pointer to the .vdex section, if present, otherwise null.
413 uint8_t* vdex_begin_;
414
415 // Pointer to the end of the .vdex section, if present, otherwise null.
416 uint8_t* vdex_end_;
417
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100418 // Owning storage for the OatDexFile objects.
419 std::vector<const OatDexFile*> oat_dex_files_storage_;
420
Vladimir Marko59ae4f92019-02-04 14:06:02 +0000421 // NOTE: We use a std::string_view as the key type to avoid a memory allocation on every
422 // lookup with a const char* key. The std::string_view doesn't own its backing storage,
Nicolas Geoffraye3e0f702019-03-12 07:02:02 +0000423 // therefore we're using the OatFile's stored dex location as the backing storage
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100424 // for keys in oat_dex_files_ and the string_cache_ entries for the backing storage
425 // of keys in secondary_oat_dex_files_ and oat_dex_files_by_canonical_location_.
Vladimir Marko59ae4f92019-02-04 14:06:02 +0000426 using Table =
427 AllocationTrackingSafeMap<std::string_view, const OatDexFile*, kAllocatorTagOatFile>;
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100428
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100429 // Map each location and canonical location (if different) retrieved from the
430 // oat file to its OatDexFile. This map doesn't change after it's constructed in Setup()
431 // and therefore doesn't need any locking and provides the cheapest dex file lookup
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700432 // for GetOatDexFile() for a very frequent use case. Never contains a null value.
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100433 Table oat_dex_files_;
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100434
435 // Lock guarding all members needed for secondary lookup in GetOatDexFile().
436 mutable Mutex secondary_lookup_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
437
438 // If the primary oat_dex_files_ lookup fails, use a secondary map. This map stores
439 // the results of all previous secondary lookups, whether successful (non-null) or
440 // failed (null). If it doesn't contain an entry we need to calculate the canonical
441 // location and use oat_dex_files_by_canonical_location_.
442 mutable Table secondary_oat_dex_files_ GUARDED_BY(secondary_lookup_lock_);
443
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100444 // Cache of strings. Contains the backing storage for keys in the secondary_oat_dex_files_
445 // and the lazily initialized oat_dex_files_by_canonical_location_.
Vladimir Marko59ae4f92019-02-04 14:06:02 +0000446 // NOTE: We're keeping references to contained strings in form of std::string_view and adding
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100447 // new strings to the end. The adding of a new element must not touch any previously stored
448 // elements. std::list<> and std::deque<> satisfy this requirement, std::vector<> doesn't.
449 mutable std::list<std::string> string_cache_ GUARDED_BY(secondary_lookup_lock_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700450
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000451 // Cache of dex files mapped directly from a location, in case the OatFile does
452 // not embed the dex code.
453 std::unique_ptr<std::vector<std::unique_ptr<const DexFile>>> uncompressed_dex_files_;
454
Mathieu Chartier5351da02016-02-17 16:19:53 -0800455 friend class gc::collector::DummyOatFile; // For modifying begin_ and end_.
Brian Carlstrome24fa612011-09-29 00:53:55 -0700456 friend class OatClass;
Richard Uhler07b3c232015-03-31 15:57:54 -0700457 friend class art::OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800458 friend class OatDumper; // For GetBase and GetLimit
Andreas Gampe049cff02015-12-01 23:27:12 -0800459 friend class OatFileBase;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700460 DISALLOW_COPY_AND_ASSIGN(OatFile);
461};
462
Richard Uhler07b3c232015-03-31 15:57:54 -0700463// OatDexFile should be an inner class of OatFile. Unfortunately, C++ doesn't
464// support forward declarations of inner classes, and we want to
465// forward-declare OatDexFile so that we can store an opaque pointer to an
466// OatDexFile in DexFile.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100467class OatDexFile final {
Richard Uhler07b3c232015-03-31 15:57:54 -0700468 public:
469 // Opens the DexFile referred to by this OatDexFile from within the containing OatFile.
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700470 std::unique_ptr<const DexFile> OpenDexFile(std::string* error_msg) const;
Richard Uhler07b3c232015-03-31 15:57:54 -0700471
Mathieu Chartier1b868492016-11-16 16:22:37 -0800472 // May return null if the OatDexFile only contains a type lookup table. This case only happens
David Srbecky605a5fe2019-04-24 14:05:21 +0100473 // for the compiler to speed up compilation, or in jitzygote.
Richard Uhler07b3c232015-03-31 15:57:54 -0700474 const OatFile* GetOatFile() const {
Mathieu Chartier1b868492016-11-16 16:22:37 -0800475 // Avoid pulling in runtime.h in the header file.
476 if (kIsDebugBuild && oat_file_ == nullptr) {
David Srbecky605a5fe2019-04-24 14:05:21 +0100477 if (!Runtime::Current()->IsUsingApexBootImageLocation()) {
478 AssertAotCompiler();
479 }
Mathieu Chartier1b868492016-11-16 16:22:37 -0800480 }
Richard Uhler07b3c232015-03-31 15:57:54 -0700481 return oat_file_;
482 }
483
484 // Returns the size of the DexFile refered to by this OatDexFile.
485 size_t FileSize() const;
486
487 // Returns original path of DexFile that was the source of this OatDexFile.
488 const std::string& GetDexFileLocation() const {
489 return dex_file_location_;
490 }
491
492 // Returns the canonical location of DexFile that was the source of this OatDexFile.
493 const std::string& GetCanonicalDexFileLocation() const {
494 return canonical_dex_file_location_;
495 }
496
497 // Returns checksum of original DexFile that was the source of this OatDexFile;
498 uint32_t GetDexFileLocationChecksum() const {
499 return dex_file_location_checksum_;
500 }
501
502 // Returns the OatClass for the class specified by the given DexFile class_def_index.
503 OatFile::OatClass GetOatClass(uint16_t class_def_index) const;
504
505 // Returns the offset to the OatClass information. Most callers should use GetOatClass.
506 uint32_t GetOatClassOffset(uint16_t class_def_index) const;
507
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300508 const uint8_t* GetLookupTableData() const {
509 return lookup_table_data_;
510 }
511
Vladimir Markof3c52b42017-11-17 17:32:12 +0000512 const IndexBssMapping* GetMethodBssMapping() const {
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100513 return method_bss_mapping_;
514 }
515
Vladimir Markof3c52b42017-11-17 17:32:12 +0000516 const IndexBssMapping* GetTypeBssMapping() const {
517 return type_bss_mapping_;
518 }
519
520 const IndexBssMapping* GetStringBssMapping() const {
521 return string_bss_mapping_;
522 }
523
Andreas Gampe2ba88952016-04-29 17:52:07 -0700524 const uint8_t* GetDexFilePointer() const {
525 return dex_file_pointer_;
526 }
527
Hans Boehm206348c2018-12-05 11:11:33 -0800528 ArrayRef<const uint8_t> GetQuickenedInfoOf(const DexFile& dex_file,
529 uint32_t dex_method_idx) const;
530
David Sehr9aa352e2016-09-15 18:13:52 -0700531 // Looks up a class definition by its class descriptor. Hash must be
532 // ComputeModifiedUtf8Hash(descriptor).
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800533 static const dex::ClassDef* FindClassDef(const DexFile& dex_file,
534 const char* descriptor,
535 size_t hash);
David Sehr9aa352e2016-09-15 18:13:52 -0700536
Mathieu Chartier120aa282017-08-05 16:03:03 -0700537 // Madvise the dex file based on the state we are moving to.
538 static void MadviseDexFile(const DexFile& dex_file, MadviseState state);
539
Vladimir Markoea341d22018-05-11 10:33:37 +0100540 const TypeLookupTable& GetTypeLookupTable() const {
541 return lookup_table_;
David Sehr9aa352e2016-09-15 18:13:52 -0700542 }
543
Richard Uhler07b3c232015-03-31 15:57:54 -0700544 ~OatDexFile();
545
Mathieu Chartier1b868492016-11-16 16:22:37 -0800546 // Create only with a type lookup table, used by the compiler to speed up compilation.
Vladimir Markoea341d22018-05-11 10:33:37 +0100547 explicit OatDexFile(TypeLookupTable&& lookup_table);
Mathieu Chartier1b868492016-11-16 16:22:37 -0800548
Mathieu Chartier120aa282017-08-05 16:03:03 -0700549 // Return the dex layout sections.
550 const DexLayoutSections* GetDexLayoutSections() const {
551 return dex_layout_sections_;
552 }
553
Richard Uhler07b3c232015-03-31 15:57:54 -0700554 private:
555 OatDexFile(const OatFile* oat_file,
556 const std::string& dex_file_location,
557 const std::string& canonical_dex_file_location,
558 uint32_t dex_file_checksum,
559 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300560 const uint8_t* lookup_table_data,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000561 const IndexBssMapping* method_bss_mapping,
562 const IndexBssMapping* type_bss_mapping,
563 const IndexBssMapping* string_bss_mapping,
Vladimir Marko09d09432015-09-08 13:47:48 +0100564 const uint32_t* oat_class_offsets_pointer,
Mathieu Chartier120aa282017-08-05 16:03:03 -0700565 const DexLayoutSections* dex_layout_sections);
Richard Uhler07b3c232015-03-31 15:57:54 -0700566
David Brazdil7126c5b2019-03-05 00:02:51 +0000567 // Create an OatDexFile wrapping an existing DexFile. Will set the OatDexFile
568 // pointer in the DexFile.
569 OatDexFile(const OatFile* oat_file,
570 const DexFile* dex_file,
571 const std::string& dex_file_location,
572 const std::string& canonical_dex_file_location);
573
574 bool IsBackedByVdexOnly() const;
575
Mathieu Chartier1b868492016-11-16 16:22:37 -0800576 static void AssertAotCompiler();
577
578 const OatFile* const oat_file_ = nullptr;
Richard Uhler07b3c232015-03-31 15:57:54 -0700579 const std::string dex_file_location_;
580 const std::string canonical_dex_file_location_;
Mathieu Chartier1b868492016-11-16 16:22:37 -0800581 const uint32_t dex_file_location_checksum_ = 0u;
582 const uint8_t* const dex_file_pointer_ = nullptr;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100583 const uint8_t* const lookup_table_data_ = nullptr;
Vladimir Markof3c52b42017-11-17 17:32:12 +0000584 const IndexBssMapping* const method_bss_mapping_ = nullptr;
585 const IndexBssMapping* const type_bss_mapping_ = nullptr;
586 const IndexBssMapping* const string_bss_mapping_ = nullptr;
Yi Kong4b22b342018-08-02 14:43:21 -0700587 const uint32_t* const oat_class_offsets_pointer_ = nullptr;
Vladimir Markoea341d22018-05-11 10:33:37 +0100588 TypeLookupTable lookup_table_;
Mathieu Chartier120aa282017-08-05 16:03:03 -0700589 const DexLayoutSections* const dex_layout_sections_ = nullptr;
Richard Uhler07b3c232015-03-31 15:57:54 -0700590
591 friend class OatFile;
Andreas Gampe049cff02015-12-01 23:27:12 -0800592 friend class OatFileBase;
Richard Uhler07b3c232015-03-31 15:57:54 -0700593 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
594};
595
Brian Carlstrome24fa612011-09-29 00:53:55 -0700596} // namespace art
597
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700598#endif // ART_RUNTIME_OAT_FILE_H_