blob: c1b1acb368210d7ef8b3ac5cc9fafb50807732c2 [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"
Eric Holkc7ac91b2021-02-04 21:44:01 +000026#include "base/compiler_filter.h"
Vladimir Marko3f5838d2014-08-07 18:07:18 +010027#include "base/mutex.h"
David Sehrc431b9d2018-03-02 12:01:51 -080028#include "base/os.h"
David Sehr67bf42e2018-02-26 16:43:04 -080029#include "base/safe_map.h"
David Sehr67bf42e2018-02-26 16:43:04 -080030#include "base/tracking_safe_map.h"
Vladimir Marko2c64a832018-01-04 11:31:56 +000031#include "class_status.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"
Brian Carlstrome24fa612011-09-29 00:53:55 -070037
38namespace art {
39
Brian Carlstromba150c32013-08-27 17:31:03 -070040class BitVector;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080041class DexFile;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080042class ElfFile;
Mathieu Chartier120aa282017-08-05 16:03:03 -070043class DexLayoutSections;
Vladimir Markoaad75c62016-10-03 08:46:48 +000044template <class MirrorType> class GcRoot;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080045class MemMap;
Richard Uhler07b3c232015-03-31 15:57:54 -070046class OatDexFile;
Andreas Gampe513061a2017-06-01 09:17:34 -070047class OatHeader;
48class OatMethodOffsets;
49class OatQuickMethodHeader;
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010050class VdexFile;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080051
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080052namespace dex {
53struct ClassDef;
54} // namespace dex
55
Mathieu Chartier5351da02016-02-17 16:19:53 -080056namespace gc {
57namespace collector {
Roland Levillain3c98d692020-07-27 16:25:54 +010058class FakeOatFile;
Mathieu Chartier5351da02016-02-17 16:19:53 -080059} // namespace collector
60} // namespace gc
61
Vladimir Markod3d00c02019-11-07 15:09:07 +000062// OatMethodOffsets are currently 5x32-bits=160-bits long, so if we can
63// save even one OatMethodOffsets struct, the more complicated encoding
64// using a bitmap pays for itself since few classes will have 160
65// methods.
Vladimir Marko3d76ebe2021-04-19 15:07:50 +000066enum class OatClassType : uint8_t {
67 kAllCompiled = 0, // OatClass is followed by an OatMethodOffsets for each method.
68 kSomeCompiled = 1, // A bitmap of OatMethodOffsets that are present follows the OatClass.
69 kNoneCompiled = 2, // All methods are interpreted so no OatMethodOffsets are necessary.
Vladimir Markod3d00c02019-11-07 15:09:07 +000070 kOatClassMax = 3,
71};
72
Vladimir Marko9974e3c2020-06-10 16:27:06 +010073std::ostream& operator<<(std::ostream& os, OatClassType rhs);
Vladimir Markod3d00c02019-11-07 15:09:07 +000074
75class PACKED(4) OatMethodOffsets {
76 public:
77 explicit OatMethodOffsets(uint32_t code_offset = 0) : code_offset_(code_offset) {}
78
79 ~OatMethodOffsets() {}
80
81 OatMethodOffsets(const OatMethodOffsets&) = default;
82 OatMethodOffsets& operator=(const OatMethodOffsets&) = default;
83
84 uint32_t code_offset_;
85};
86
David Brazdil7b49e6c2016-09-01 11:06:18 +010087// Runtime representation of the OAT file format which holds compiler output.
88// The class opens an OAT file from storage and maps it to memory, typically with
89// dlopen and provides access to its internal data structures (see OatWriter for
90// for more details about the OAT format).
91// In the process of loading OAT, the class also loads the associated VDEX file
92// with the input DEX files (see VdexFile for details about the VDEX format).
93// The raw DEX data are accessible transparently through the OatDexFile objects.
94
Andreas Gampe049cff02015-12-01 23:27:12 -080095class OatFile {
Brian Carlstrome24fa612011-09-29 00:53:55 -070096 public:
Vladimir Markob7bf8432019-12-03 13:18:50 +000097 // Open an oat file. Returns null on failure.
Vladimir Marko69944682019-12-09 15:16:39 +000098 // The `dex_filenames` argument, if provided, overrides the dex locations
99 // from oat file when opening the dex files if they are not embedded in the
100 // vdex file. These may differ for cross-compilation (the dex file name is
101 // the host path and dex location is the future path on target) and testing.
Nicolas Geoffray30025092018-04-19 14:43:29 +0100102 static OatFile* Open(int zip_fd,
103 const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800104 const std::string& location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700105 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800106 bool low_4gb,
Vladimir Markob7bf8432019-12-03 13:18:50 +0000107 ArrayRef<const std::string> dex_filenames,
Victor Hsiehf667c332021-05-27 11:35:44 -0700108 ArrayRef<const int> dex_fds,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100109 /*inout*/MemMap* reservation, // Where to load if not null.
110 /*out*/std::string* error_msg);
Vladimir Markob7bf8432019-12-03 13:18:50 +0000111 // Helper overload that takes a single dex filename and no reservation.
112 static OatFile* Open(int zip_fd,
113 const std::string& filename,
114 const std::string& location,
115 bool executable,
116 bool low_4gb,
117 const std::string& dex_filename,
118 /*out*/std::string* error_msg) {
119 return Open(zip_fd,
120 filename,
121 location,
122 executable,
123 low_4gb,
124 ArrayRef<const std::string>(&dex_filename, /*size=*/ 1u),
Victor Hsiehf667c332021-05-27 11:35:44 -0700125 /*dex_fds=*/ ArrayRef<const int>(), // not currently supported
Vladimir Markob7bf8432019-12-03 13:18:50 +0000126 /*reservation=*/ nullptr,
127 error_msg);
128 }
129 // Helper overload that takes no dex filename and no reservation.
130 static OatFile* Open(int zip_fd,
131 const std::string& filename,
132 const std::string& location,
133 bool executable,
134 bool low_4gb,
135 /*out*/std::string* error_msg) {
136 return Open(zip_fd,
137 filename,
138 location,
139 executable,
140 low_4gb,
Victor Hsiehf667c332021-05-27 11:35:44 -0700141 /*dex_filenames=*/ ArrayRef<const std::string>(),
142 /*dex_fds=*/ ArrayRef<const int>(), // not currently supported
Vladimir Markob7bf8432019-12-03 13:18:50 +0000143 /*reservation=*/ nullptr,
144 error_msg);
145 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700146
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700147 // Similar to OatFile::Open(const std::string...), but accepts input vdex and
Nicolas Geoffray30025092018-04-19 14:43:29 +0100148 // odex files as file descriptors. We also take zip_fd in case the vdex does not
149 // contain the dex code, and we need to read it from the zip file.
150 static OatFile* Open(int zip_fd,
151 int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700152 int oat_fd,
153 const std::string& oat_location,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700154 bool executable,
155 bool low_4gb,
Vladimir Markob7bf8432019-12-03 13:18:50 +0000156 ArrayRef<const std::string> dex_filenames,
Victor Hsiehce9b9022021-07-21 10:44:06 -0700157 ArrayRef<const int> dex_fds,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100158 /*inout*/MemMap* reservation, // Where to load if not null.
159 /*out*/std::string* error_msg);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700160
David Brazdil7126c5b2019-03-05 00:02:51 +0000161 // Initialize OatFile instance from an already loaded VdexFile. This assumes
162 // the vdex does not have a dex section and accepts a vector of DexFiles separately.
163 static OatFile* OpenFromVdex(const std::vector<const DexFile*>& dex_files,
164 std::unique_ptr<VdexFile>&& vdex_file,
165 const std::string& location);
166
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000167 // Initialize OatFile instance from an already loaded VdexFile. The dex files
168 // will be opened through `zip_fd` or `dex_location` if `zip_fd` is -1.
169 static OatFile* OpenFromVdex(int zip_fd,
170 std::unique_ptr<VdexFile>&& vdex_file,
171 const std::string& location,
172 std::string* error_msg);
173
174 // Return whether the `OatFile` uses a vdex-only file.
175 bool IsBackedByVdexOnly() const;
176
Andreas Gampe049cff02015-12-01 23:27:12 -0800177 virtual ~OatFile();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700178
Alex Light9dcc4572014-08-14 14:16:26 -0700179 bool IsExecutable() const {
180 return is_executable_;
181 }
182
Sebastien Hertz0de11332015-05-13 12:14:05 +0200183 // Indicates whether the oat file was compiled with full debugging capability.
184 bool IsDebuggable() const;
185
Andreas Gampe29d38e72016-03-23 15:31:51 +0000186 CompilerFilter::Filter GetCompilerFilter() const;
Calin Juravleb077e152016-02-18 18:47:37 +0000187
Calin Juravle44e5efa2017-09-12 00:54:26 -0700188 std::string GetClassLoaderContext() const;
189
Calin Juravle0e09dfc2018-02-12 19:01:09 -0800190 const char* GetCompilationReason() const;
191
Brian Carlstrome24fa612011-09-29 00:53:55 -0700192 const std::string& GetLocation() const {
193 return location_;
194 }
195
196 const OatHeader& GetOatHeader() const;
197
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100198 class OatMethod final {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700199 public:
Peter Collingbourne584b1972022-02-08 17:48:42 -0800200 uint32_t GetCodeOffset() const { return code_offset_; }
Brian Carlstromae826982011-11-09 01:33:42 -0800201
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100202 const void* GetQuickCode() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700203
204 // Returns size of quick code.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800205 uint32_t GetQuickCodeSize() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700206
207 // Returns OatQuickMethodHeader for debugging. Most callers should
208 // use more specific methods such as GetQuickCodeSize.
209 const OatQuickMethodHeader* GetOatQuickMethodHeader() const;
210 uint32_t GetOatQuickMethodHeaderOffset() const;
Logan Chien0c717dd2012-03-28 18:31:07 +0800211
Vladimir Marko7624d252014-05-02 14:40:15 +0100212 size_t GetFrameSizeInBytes() const;
213 uint32_t GetCoreSpillMask() const;
214 uint32_t GetFpSpillMask() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700215
Vladimir Marko8a630572014-04-09 18:45:35 +0100216 const uint8_t* GetVmapTable() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700217 uint32_t GetVmapTableOffset() const;
Vladimir Marko8a630572014-04-09 18:45:35 +0100218
Brian Carlstromae826982011-11-09 01:33:42 -0800219 // Create an OatMethod with offsets relative to the given base address
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800220 OatMethod(const uint8_t* base, const uint32_t code_offset)
221 : begin_(base), code_offset_(code_offset) {
Ian Rogers97b52f82014-08-14 11:34:07 -0700222 }
Andreas Gampe758a8012015-04-03 21:28:42 -0700223 OatMethod(const OatMethod&) = default;
Ian Rogers97b52f82014-08-14 11:34:07 -0700224 ~OatMethod() {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700225
Andreas Gampe758a8012015-04-03 21:28:42 -0700226 OatMethod& operator=(const OatMethod&) = default;
227
Ian Rogers97b52f82014-08-14 11:34:07 -0700228 // A representation of an invalid OatMethod, used when an OatMethod or OatClass can't be found.
229 // See ClassLinker::FindOatMethodFor.
230 static const OatMethod Invalid() {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800231 return OatMethod(nullptr, -1);
Ian Rogers97b52f82014-08-14 11:34:07 -0700232 }
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100233
Brian Carlstromae826982011-11-09 01:33:42 -0800234 private:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800235 const uint8_t* begin_;
236 uint32_t code_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800237
238 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700239 };
240
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100241 class OatClass final {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700242 public:
Vladimir Marko2c64a832018-01-04 11:31:56 +0000243 ClassStatus GetStatus() const {
Brian Carlstromba150c32013-08-27 17:31:03 -0700244 return status_;
245 }
246
247 OatClassType GetType() const {
248 return type_;
249 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800250
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700251 // Get the OatMethod entry based on its index into the class
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700252 // defintion. Direct methods come first, followed by virtual
253 // methods. Note that runtime created methods such as miranda
Brian Carlstrome24fa612011-09-29 00:53:55 -0700254 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700255 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700256
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700257 // Return a pointer to the OatMethodOffsets for the requested
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700258 // method_index, or null if none is present. Note that most
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700259 // callers should use GetOatMethod.
260 const OatMethodOffsets* GetOatMethodOffsets(uint32_t method_index) const;
261
262 // Return the offset from the start of the OatFile to the
263 // OatMethodOffsets for the requested method_index, or 0 if none
264 // is present. Note that most callers should use GetOatMethod.
265 uint32_t GetOatMethodOffsetsOffset(uint32_t method_index) const;
266
Ian Rogers97b52f82014-08-14 11:34:07 -0700267 // A representation of an invalid OatClass, used when an OatClass can't be found.
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100268 // See FindOatClass().
Ian Rogers97b52f82014-08-14 11:34:07 -0700269 static OatClass Invalid() {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700270 return OatClass(/* oat_file= */ nullptr,
Vladimir Marko2c64a832018-01-04 11:31:56 +0000271 ClassStatus::kErrorUnresolved,
Vladimir Marko3d76ebe2021-04-19 15:07:50 +0000272 OatClassType::kNoneCompiled,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700273 /* bitmap_size= */ 0,
274 /* bitmap_pointer= */ nullptr,
275 /* methods_pointer= */ nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700276 }
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100277
Brian Carlstrome24fa612011-09-29 00:53:55 -0700278 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800279 OatClass(const OatFile* oat_file,
Vladimir Marko2c64a832018-01-04 11:31:56 +0000280 ClassStatus status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700281 OatClassType type,
Vladimir Marko2cca1e02021-04-21 09:21:16 +0000282 uint32_t num_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -0700283 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800284 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700285
Ian Rogers97b52f82014-08-14 11:34:07 -0700286 const OatFile* const oat_file_;
Vladimir Marko2c64a832018-01-04 11:31:56 +0000287 const ClassStatus status_;
Ian Rogers97b52f82014-08-14 11:34:07 -0700288 const OatClassType type_;
Vladimir Marko2cca1e02021-04-21 09:21:16 +0000289 const uint32_t num_methods_;
Ian Rogers97b52f82014-08-14 11:34:07 -0700290 const uint32_t* const bitmap_;
Ian Rogers97b52f82014-08-14 11:34:07 -0700291 const OatMethodOffsets* const methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700292
Richard Uhler07b3c232015-03-31 15:57:54 -0700293 friend class art::OatDexFile;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700294 };
Richard Uhler9a37efc2016-08-05 16:32:55 -0700295
296 // Get the OatDexFile for the given dex_location within this oat file.
297 // If dex_location_checksum is non-null, the OatDexFile will only be
298 // returned if it has a matching checksum.
299 // If error_msg is non-null and no OatDexFile is returned, error_msg will
300 // be updated with a description of why no OatDexFile was returned.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700301 const OatDexFile* GetOatDexFile(const char* dex_location,
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700302 const uint32_t* const dex_location_checksum,
Richard Uhler9a37efc2016-08-05 16:32:55 -0700303 /*out*/std::string* error_msg = nullptr) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700304 REQUIRES(!secondary_lookup_lock_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700305
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100306 const std::vector<const OatDexFile*>& GetOatDexFiles() const {
307 return oat_dex_files_storage_;
308 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700309
Ian Rogers30fab402012-01-23 15:43:46 -0800310 size_t Size() const {
311 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700312 }
313
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800314 bool Contains(const void* p) const {
315 return p >= Begin() && p < End();
316 }
317
Vladimir Markob066d432018-01-03 13:14:37 +0000318 size_t DataBimgRelRoSize() const {
319 return DataBimgRelRoEnd() - DataBimgRelRoBegin();
320 }
321
Vladimir Marko5c42c292015-02-25 12:02:49 +0000322 size_t BssSize() const {
323 return BssEnd() - BssBegin();
324 }
325
David Srbeckyec2cdf42017-12-08 16:21:25 +0000326 size_t VdexSize() const {
327 return VdexEnd() - VdexBegin();
328 }
329
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100330 size_t BssMethodsOffset() const {
331 // Note: This is used only for symbolizer and needs to return a valid .bss offset.
332 return (bss_methods_ != nullptr) ? bss_methods_ - BssBegin() : BssRootsOffset();
333 }
334
Vladimir Markoaad75c62016-10-03 08:46:48 +0000335 size_t BssRootsOffset() const {
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100336 // Note: This is used only for symbolizer and needs to return a valid .bss offset.
337 return (bss_roots_ != nullptr) ? bss_roots_ - BssBegin() : BssSize();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000338 }
339
David Brazdil7b49e6c2016-09-01 11:06:18 +0100340 size_t DexSize() const {
341 return DexEnd() - DexBegin();
342 }
343
Ian Rogers13735952014-10-08 12:43:28 -0700344 const uint8_t* Begin() const;
345 const uint8_t* End() const;
Alex Light53cb16b2014-06-12 11:26:29 -0700346
Vladimir Markob066d432018-01-03 13:14:37 +0000347 const uint8_t* DataBimgRelRoBegin() const { return data_bimg_rel_ro_begin_; }
348 const uint8_t* DataBimgRelRoEnd() const { return data_bimg_rel_ro_end_; }
Vladimir Marko5c42c292015-02-25 12:02:49 +0000349
Vladimir Markob066d432018-01-03 13:14:37 +0000350 const uint8_t* BssBegin() const { return bss_begin_; }
351 const uint8_t* BssEnd() const { return bss_end_; }
352
353 const uint8_t* VdexBegin() const { return vdex_begin_; }
354 const uint8_t* VdexEnd() const { return vdex_end_; }
David Srbeckyec2cdf42017-12-08 16:21:25 +0000355
David Brazdil7b49e6c2016-09-01 11:06:18 +0100356 const uint8_t* DexBegin() const;
357 const uint8_t* DexEnd() const;
358
Vladimir Markob066d432018-01-03 13:14:37 +0000359 ArrayRef<const uint32_t> GetBootImageRelocations() const;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100360 ArrayRef<ArtMethod*> GetBssMethods() const;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000361 ArrayRef<GcRoot<mirror::Object>> GetBssGcRoots() const;
362
Vladimir Marko1cedb4a2019-02-06 14:13:28 +0000363 // Initialize relocation sections (.data.bimg.rel.ro and .bss).
364 void InitializeRelocations() const;
365
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100366 // Finds the associated oat class for a dex_file and descriptor. Returns an invalid OatClass on
367 // error and sets found to false.
368 static OatClass FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, bool* found);
369
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100370 VdexFile* GetVdexFile() const {
371 return vdex_.get();
372 }
373
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000374 // Whether the OatFile embeds the Dex code.
375 bool ContainsDexCode() const {
Vladimir Markob7bf8432019-12-03 13:18:50 +0000376 return external_dex_files_.empty();
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000377 }
378
Eric Holkbc89ed42020-04-29 19:59:24 +0000379 // Returns whether an image (e.g. app image) is required to safely execute this OAT file.
380 bool RequiresImage() const;
381
Santiago Aboy Solanesb7404382022-02-02 16:00:52 +0000382 struct BssMappingInfo {
383 const IndexBssMapping* method_bss_mapping = nullptr;
384 const IndexBssMapping* type_bss_mapping = nullptr;
385 const IndexBssMapping* public_type_bss_mapping = nullptr;
386 const IndexBssMapping* package_type_bss_mapping = nullptr;
387 const IndexBssMapping* string_bss_mapping = nullptr;
388 };
389
390 ArrayRef<const BssMappingInfo> GetBcpBssInfo() const {
391 return ArrayRef<const BssMappingInfo>(bcp_bss_info_);
392 }
393
394 // Returns the mapping info of `dex_file` if found in the BcpBssInfo, or nullptr otherwise.
395 const BssMappingInfo* FindBcpMappingInfo(const DexFile* dex_file) const;
396
Andreas Gampe049cff02015-12-01 23:27:12 -0800397 protected:
398 OatFile(const std::string& filename, bool executable);
399
Brian Carlstrome24fa612011-09-29 00:53:55 -0700400 private:
Brian Carlstrome24fa612011-09-29 00:53:55 -0700401 // The oat file name.
402 //
403 // The image will embed this to link its associated oat file.
404 const std::string location_;
405
David Brazdil7b49e6c2016-09-01 11:06:18 +0100406 // Pointer to the Vdex file with the Dex files for this Oat file.
407 std::unique_ptr<VdexFile> vdex_;
408
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800409 // Pointer to OatHeader.
Ian Rogers13735952014-10-08 12:43:28 -0700410 const uint8_t* begin_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800411
412 // Pointer to end of oat region for bounds checking.
Ian Rogers13735952014-10-08 12:43:28 -0700413 const uint8_t* end_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800414
Vladimir Markob066d432018-01-03 13:14:37 +0000415 // Pointer to the .data.bimg.rel.ro section, if present, otherwise null.
416 const uint8_t* data_bimg_rel_ro_begin_;
417
418 // Pointer to the end of the .data.bimg.rel.ro section, if present, otherwise null.
419 const uint8_t* data_bimg_rel_ro_end_;
420
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700421 // Pointer to the .bss section, if present, otherwise null.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100422 uint8_t* bss_begin_;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000423
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700424 // Pointer to the end of the .bss section, if present, otherwise null.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100425 uint8_t* bss_end_;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000426
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100427 // Pointer to the beginning of the ArtMethod*s in .bss section, if present, otherwise null.
428 uint8_t* bss_methods_;
429
Vladimir Markoaad75c62016-10-03 08:46:48 +0000430 // Pointer to the beginning of the GC roots in .bss section, if present, otherwise null.
431 uint8_t* bss_roots_;
432
Alex Light9dcc4572014-08-14 14:16:26 -0700433 // Was this oat_file loaded executable?
434 const bool is_executable_;
435
David Srbeckyec2cdf42017-12-08 16:21:25 +0000436 // Pointer to the .vdex section, if present, otherwise null.
437 uint8_t* vdex_begin_;
438
439 // Pointer to the end of the .vdex section, if present, otherwise null.
440 uint8_t* vdex_end_;
441
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100442 // Owning storage for the OatDexFile objects.
443 std::vector<const OatDexFile*> oat_dex_files_storage_;
444
Santiago Aboy Solanesb7404382022-02-02 16:00:52 +0000445 // Mapping info for DexFiles in the BCP.
446 std::vector<BssMappingInfo> bcp_bss_info_;
447
Vladimir Marko59ae4f92019-02-04 14:06:02 +0000448 // NOTE: We use a std::string_view as the key type to avoid a memory allocation on every
449 // lookup with a const char* key. The std::string_view doesn't own its backing storage,
Nicolas Geoffraye3e0f702019-03-12 07:02:02 +0000450 // therefore we're using the OatFile's stored dex location as the backing storage
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100451 // for keys in oat_dex_files_ and the string_cache_ entries for the backing storage
452 // of keys in secondary_oat_dex_files_ and oat_dex_files_by_canonical_location_.
Vladimir Marko59ae4f92019-02-04 14:06:02 +0000453 using Table =
454 AllocationTrackingSafeMap<std::string_view, const OatDexFile*, kAllocatorTagOatFile>;
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100455
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100456 // Map each location and canonical location (if different) retrieved from the
457 // oat file to its OatDexFile. This map doesn't change after it's constructed in Setup()
458 // and therefore doesn't need any locking and provides the cheapest dex file lookup
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700459 // for GetOatDexFile() for a very frequent use case. Never contains a null value.
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100460 Table oat_dex_files_;
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100461
462 // Lock guarding all members needed for secondary lookup in GetOatDexFile().
463 mutable Mutex secondary_lookup_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
464
465 // If the primary oat_dex_files_ lookup fails, use a secondary map. This map stores
466 // the results of all previous secondary lookups, whether successful (non-null) or
467 // failed (null). If it doesn't contain an entry we need to calculate the canonical
468 // location and use oat_dex_files_by_canonical_location_.
469 mutable Table secondary_oat_dex_files_ GUARDED_BY(secondary_lookup_lock_);
470
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100471 // Cache of strings. Contains the backing storage for keys in the secondary_oat_dex_files_
472 // and the lazily initialized oat_dex_files_by_canonical_location_.
Vladimir Marko59ae4f92019-02-04 14:06:02 +0000473 // NOTE: We're keeping references to contained strings in form of std::string_view and adding
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100474 // new strings to the end. The adding of a new element must not touch any previously stored
475 // elements. std::list<> and std::deque<> satisfy this requirement, std::vector<> doesn't.
476 mutable std::list<std::string> string_cache_ GUARDED_BY(secondary_lookup_lock_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700477
Vladimir Markob7bf8432019-12-03 13:18:50 +0000478 // Dex files opened directly from a file referenced from the oat file or specifed
479 // by the `dex_filenames` parameter, in case the OatFile does not embed the dex code.
480 std::vector<std::unique_ptr<const DexFile>> external_dex_files_;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000481
Roland Levillain3c98d692020-07-27 16:25:54 +0100482 friend class gc::collector::FakeOatFile; // For modifying begin_ and end_.
Brian Carlstrome24fa612011-09-29 00:53:55 -0700483 friend class OatClass;
Richard Uhler07b3c232015-03-31 15:57:54 -0700484 friend class art::OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800485 friend class OatDumper; // For GetBase and GetLimit
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000486 friend class OatFileBackedByVdex;
Andreas Gampe049cff02015-12-01 23:27:12 -0800487 friend class OatFileBase;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700488 DISALLOW_COPY_AND_ASSIGN(OatFile);
489};
490
Richard Uhler07b3c232015-03-31 15:57:54 -0700491// OatDexFile should be an inner class of OatFile. Unfortunately, C++ doesn't
492// support forward declarations of inner classes, and we want to
493// forward-declare OatDexFile so that we can store an opaque pointer to an
494// OatDexFile in DexFile.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100495class OatDexFile final {
Richard Uhler07b3c232015-03-31 15:57:54 -0700496 public:
497 // Opens the DexFile referred to by this OatDexFile from within the containing OatFile.
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700498 std::unique_ptr<const DexFile> OpenDexFile(std::string* error_msg) const;
Richard Uhler07b3c232015-03-31 15:57:54 -0700499
Mathieu Chartier1b868492016-11-16 16:22:37 -0800500 // May return null if the OatDexFile only contains a type lookup table. This case only happens
David Srbecky605a5fe2019-04-24 14:05:21 +0100501 // for the compiler to speed up compilation, or in jitzygote.
Richard Uhler07b3c232015-03-31 15:57:54 -0700502 const OatFile* GetOatFile() const {
503 return oat_file_;
504 }
505
506 // Returns the size of the DexFile refered to by this OatDexFile.
507 size_t FileSize() const;
508
509 // Returns original path of DexFile that was the source of this OatDexFile.
510 const std::string& GetDexFileLocation() const {
511 return dex_file_location_;
512 }
513
514 // Returns the canonical location of DexFile that was the source of this OatDexFile.
515 const std::string& GetCanonicalDexFileLocation() const {
516 return canonical_dex_file_location_;
517 }
518
519 // Returns checksum of original DexFile that was the source of this OatDexFile;
520 uint32_t GetDexFileLocationChecksum() const {
521 return dex_file_location_checksum_;
522 }
523
524 // Returns the OatClass for the class specified by the given DexFile class_def_index.
525 OatFile::OatClass GetOatClass(uint16_t class_def_index) const;
526
527 // Returns the offset to the OatClass information. Most callers should use GetOatClass.
528 uint32_t GetOatClassOffset(uint16_t class_def_index) const;
529
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300530 const uint8_t* GetLookupTableData() const {
531 return lookup_table_data_;
532 }
533
Vladimir Markof3c52b42017-11-17 17:32:12 +0000534 const IndexBssMapping* GetMethodBssMapping() const {
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100535 return method_bss_mapping_;
536 }
537
Vladimir Markof3c52b42017-11-17 17:32:12 +0000538 const IndexBssMapping* GetTypeBssMapping() const {
539 return type_bss_mapping_;
540 }
541
Vladimir Marko8f63f102020-09-28 12:10:28 +0100542 const IndexBssMapping* GetPublicTypeBssMapping() const {
543 return public_type_bss_mapping_;
544 }
545
546 const IndexBssMapping* GetPackageTypeBssMapping() const {
547 return package_type_bss_mapping_;
548 }
549
Vladimir Markof3c52b42017-11-17 17:32:12 +0000550 const IndexBssMapping* GetStringBssMapping() const {
551 return string_bss_mapping_;
552 }
553
Andreas Gampe2ba88952016-04-29 17:52:07 -0700554 const uint8_t* GetDexFilePointer() const {
555 return dex_file_pointer_;
556 }
557
David Sehr9aa352e2016-09-15 18:13:52 -0700558 // Looks up a class definition by its class descriptor. Hash must be
559 // ComputeModifiedUtf8Hash(descriptor).
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800560 static const dex::ClassDef* FindClassDef(const DexFile& dex_file,
561 const char* descriptor,
562 size_t hash);
David Sehr9aa352e2016-09-15 18:13:52 -0700563
Jared Duke95bb9952021-08-11 15:07:25 -0700564 // Madvise the dex file for load-time usage.
565 static void MadviseDexFileAtLoad(const DexFile& dex_file);
Mathieu Chartier120aa282017-08-05 16:03:03 -0700566
Vladimir Markoea341d22018-05-11 10:33:37 +0100567 const TypeLookupTable& GetTypeLookupTable() const {
568 return lookup_table_;
David Sehr9aa352e2016-09-15 18:13:52 -0700569 }
570
Richard Uhler07b3c232015-03-31 15:57:54 -0700571 ~OatDexFile();
572
Mathieu Chartier1b868492016-11-16 16:22:37 -0800573 // Create only with a type lookup table, used by the compiler to speed up compilation.
Vladimir Markoea341d22018-05-11 10:33:37 +0100574 explicit OatDexFile(TypeLookupTable&& lookup_table);
Mathieu Chartier1b868492016-11-16 16:22:37 -0800575
Mathieu Chartier120aa282017-08-05 16:03:03 -0700576 // Return the dex layout sections.
577 const DexLayoutSections* GetDexLayoutSections() const {
578 return dex_layout_sections_;
579 }
580
Richard Uhler07b3c232015-03-31 15:57:54 -0700581 private:
582 OatDexFile(const OatFile* oat_file,
583 const std::string& dex_file_location,
584 const std::string& canonical_dex_file_location,
585 uint32_t dex_file_checksum,
586 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300587 const uint8_t* lookup_table_data,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000588 const IndexBssMapping* method_bss_mapping,
589 const IndexBssMapping* type_bss_mapping,
Vladimir Marko8f63f102020-09-28 12:10:28 +0100590 const IndexBssMapping* public_type_bss_mapping,
591 const IndexBssMapping* package_type_bss_mapping,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000592 const IndexBssMapping* string_bss_mapping,
Vladimir Marko09d09432015-09-08 13:47:48 +0100593 const uint32_t* oat_class_offsets_pointer,
Mathieu Chartier120aa282017-08-05 16:03:03 -0700594 const DexLayoutSections* dex_layout_sections);
Richard Uhler07b3c232015-03-31 15:57:54 -0700595
David Brazdil7126c5b2019-03-05 00:02:51 +0000596 // Create an OatDexFile wrapping an existing DexFile. Will set the OatDexFile
597 // pointer in the DexFile.
598 OatDexFile(const OatFile* oat_file,
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000599 const uint8_t* dex_file_pointer,
600 uint32_t dex_file_checksum,
David Brazdil7126c5b2019-03-05 00:02:51 +0000601 const std::string& dex_file_location,
Nicolas Geoffray0b943282021-04-16 09:16:00 +0000602 const std::string& canonical_dex_file_location,
603 const uint8_t* lookup_table_data);
David Brazdil7126c5b2019-03-05 00:02:51 +0000604
605 bool IsBackedByVdexOnly() const;
Nicolas Geoffray0b943282021-04-16 09:16:00 +0000606 void InitializeTypeLookupTable();
David Brazdil7126c5b2019-03-05 00:02:51 +0000607
Mathieu Chartier1b868492016-11-16 16:22:37 -0800608 static void AssertAotCompiler();
609
610 const OatFile* const oat_file_ = nullptr;
Richard Uhler07b3c232015-03-31 15:57:54 -0700611 const std::string dex_file_location_;
612 const std::string canonical_dex_file_location_;
Mathieu Chartier1b868492016-11-16 16:22:37 -0800613 const uint32_t dex_file_location_checksum_ = 0u;
614 const uint8_t* const dex_file_pointer_ = nullptr;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100615 const uint8_t* const lookup_table_data_ = nullptr;
Vladimir Markof3c52b42017-11-17 17:32:12 +0000616 const IndexBssMapping* const method_bss_mapping_ = nullptr;
617 const IndexBssMapping* const type_bss_mapping_ = nullptr;
Vladimir Marko8f63f102020-09-28 12:10:28 +0100618 const IndexBssMapping* const public_type_bss_mapping_ = nullptr;
619 const IndexBssMapping* const package_type_bss_mapping_ = nullptr;
Vladimir Markof3c52b42017-11-17 17:32:12 +0000620 const IndexBssMapping* const string_bss_mapping_ = nullptr;
Yi Kong4b22b342018-08-02 14:43:21 -0700621 const uint32_t* const oat_class_offsets_pointer_ = nullptr;
Vladimir Markoea341d22018-05-11 10:33:37 +0100622 TypeLookupTable lookup_table_;
Mathieu Chartier120aa282017-08-05 16:03:03 -0700623 const DexLayoutSections* const dex_layout_sections_ = nullptr;
Richard Uhler07b3c232015-03-31 15:57:54 -0700624
625 friend class OatFile;
Andreas Gampe049cff02015-12-01 23:27:12 -0800626 friend class OatFileBase;
Nicolas Geoffray16f60dc2021-02-16 15:35:16 +0000627 friend class OatFileBackedByVdex;
Richard Uhler07b3c232015-03-31 15:57:54 -0700628 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
629};
630
Brian Carlstrome24fa612011-09-29 00:53:55 -0700631} // namespace art
632
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700633#endif // ART_RUNTIME_OAT_FILE_H_