blob: 7d4e6dfd6b53118db220e0132d0bfe74e78d60eb [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"
Vladimir Marko539690a2014-06-05 18:36:42 +010026#include "base/stringpiece.h"
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010027#include "compiler_filter.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "dex_file.h"
Mathieu Chartier120aa282017-08-05 16:03:03 -070029#include "dex_file_layout.h"
Vladimir Marko0eb882b2017-05-15 13:39:18 +010030#include "method_bss_mapping.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070031#include "mirror/class.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "oat.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080033#include "os.h"
David Sehr9aa352e2016-09-15 18:13:52 -070034#include "type_lookup_table.h"
35#include "utf.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010036#include "utils.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070037
38namespace art {
39
Brian Carlstromba150c32013-08-27 17:31:03 -070040class BitVector;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080041class ElfFile;
Mathieu Chartier120aa282017-08-05 16:03:03 -070042class DexLayoutSections;
Vladimir Markoaad75c62016-10-03 08:46:48 +000043template <class MirrorType> class GcRoot;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080044class MemMap;
Richard Uhler07b3c232015-03-31 15:57:54 -070045class OatDexFile;
Andreas Gampe513061a2017-06-01 09:17:34 -070046class OatHeader;
47class OatMethodOffsets;
48class OatQuickMethodHeader;
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010049class VdexFile;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080050
Mathieu Chartier5351da02016-02-17 16:19:53 -080051namespace gc {
52namespace collector {
53class DummyOatFile;
54} // namespace collector
55} // namespace gc
56
David Brazdil7b49e6c2016-09-01 11:06:18 +010057// Runtime representation of the OAT file format which holds compiler output.
58// The class opens an OAT file from storage and maps it to memory, typically with
59// dlopen and provides access to its internal data structures (see OatWriter for
60// for more details about the OAT format).
61// In the process of loading OAT, the class also loads the associated VDEX file
62// with the input DEX files (see VdexFile for details about the VDEX format).
63// The raw DEX data are accessible transparently through the OatDexFile objects.
64
Andreas Gampe049cff02015-12-01 23:27:12 -080065class OatFile {
Brian Carlstrome24fa612011-09-29 00:53:55 -070066 public:
Jeff Haof0192c82016-03-28 20:39:50 -070067 // Special classpath that skips shared library check.
68 static constexpr const char* kSpecialSharedLibrary = "&";
69
Richard Uhler07b3c232015-03-31 15:57:54 -070070 typedef art::OatDexFile OatDexFile;
71
Alex Light84d76052014-08-22 17:49:35 -070072 // Opens an oat file contained within the given elf file. This is always opened as
73 // non-executable at the moment.
David Brazdilc93b3be2016-09-12 18:49:58 +010074 static OatFile* OpenWithElfFile(ElfFile* elf_file,
75 VdexFile* vdex_file,
76 const std::string& location,
Richard Uhlere5fed032015-03-18 08:21:11 -070077 const char* abs_dex_location,
Igor Murashkinb1d8c312015-08-04 11:18:43 -070078 std::string* error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070079 // Open an oat file. Returns null on failure. Requested base can
Brian Carlstrome24fa612011-09-29 00:53:55 -070080 // optionally be used to request where the file should be loaded.
Richard Uhlere5fed032015-03-18 08:21:11 -070081 // See the ResolveRelativeEncodedDexLocation for a description of how the
82 // abs_dex_location argument is used.
Brian Carlstrome24fa612011-09-29 00:53:55 -070083 static OatFile* Open(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -080084 const std::string& location,
Ian Rogers13735952014-10-08 12:43:28 -070085 uint8_t* requested_base,
Igor Murashkin46774762014-10-22 11:37:02 -070086 uint8_t* oat_file_begin,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070087 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -080088 bool low_4gb,
Richard Uhlere5fed032015-03-18 08:21:11 -070089 const char* abs_dex_location,
Igor Murashkinb1d8c312015-08-04 11:18:43 -070090 std::string* error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -070091
Shubham Ajmerab22dea02017-10-04 18:36:41 -070092 // Similar to OatFile::Open(const std::string...), but accepts input vdex and
93 // odex files as file descriptors.
94 static OatFile* Open(int vdex_fd,
95 int oat_fd,
96 const std::string& oat_location,
97 uint8_t* requested_base,
98 uint8_t* oat_file_begin,
99 bool executable,
100 bool low_4gb,
101 const char* abs_dex_location,
102 std::string* error_msg);
103
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800104 // Open an oat file from an already opened File.
Brian Carlstrom265091e2013-01-30 14:08:26 -0800105 // Does not use dlopen underneath so cannot be used for runtime use
106 // where relocations may be required. Currently used from
107 // ImageWriter which wants to open a writable version from an existing
108 // file descriptor for patching.
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700109 static OatFile* OpenWritable(File* file, const std::string& location,
Richard Uhlere5fed032015-03-18 08:21:11 -0700110 const char* abs_dex_location,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700111 std::string* error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700112 // Opens an oat file from an already opened File. Maps it PROT_READ, MAP_PRIVATE.
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700113 static OatFile* OpenReadable(File* file, const std::string& location,
Richard Uhlere5fed032015-03-18 08:21:11 -0700114 const char* abs_dex_location,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700115 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800116
Andreas Gampe049cff02015-12-01 23:27:12 -0800117 virtual ~OatFile();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700118
Alex Light9dcc4572014-08-14 14:16:26 -0700119 bool IsExecutable() const {
120 return is_executable_;
121 }
122
Andreas Gampe7ba64962014-10-23 11:37:40 -0700123 bool IsPic() const;
124
Sebastien Hertz0de11332015-05-13 12:14:05 +0200125 // Indicates whether the oat file was compiled with full debugging capability.
126 bool IsDebuggable() const;
127
Andreas Gampe29d38e72016-03-23 15:31:51 +0000128 CompilerFilter::Filter GetCompilerFilter() const;
Calin Juravleb077e152016-02-18 18:47:37 +0000129
Calin Juravle44e5efa2017-09-12 00:54:26 -0700130 std::string GetClassLoaderContext() const;
131
Brian Carlstrome24fa612011-09-29 00:53:55 -0700132 const std::string& GetLocation() const {
133 return location_;
134 }
135
136 const OatHeader& GetOatHeader() const;
137
Richard Uhler07b3c232015-03-31 15:57:54 -0700138 class OatMethod FINAL {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700139 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700140 void LinkMethod(ArtMethod* method) const;
Brian Carlstromae826982011-11-09 01:33:42 -0800141
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100142 uint32_t GetCodeOffset() const;
Brian Carlstromae826982011-11-09 01:33:42 -0800143
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100144 const void* GetQuickCode() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700145
146 // Returns size of quick code.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800147 uint32_t GetQuickCodeSize() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700148 uint32_t GetQuickCodeSizeOffset() const;
149
150 // Returns OatQuickMethodHeader for debugging. Most callers should
151 // use more specific methods such as GetQuickCodeSize.
152 const OatQuickMethodHeader* GetOatQuickMethodHeader() const;
153 uint32_t GetOatQuickMethodHeaderOffset() const;
Logan Chien0c717dd2012-03-28 18:31:07 +0800154
Vladimir Marko7624d252014-05-02 14:40:15 +0100155 size_t GetFrameSizeInBytes() const;
156 uint32_t GetCoreSpillMask() const;
157 uint32_t GetFpSpillMask() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700158
Vladimir Marko8a630572014-04-09 18:45:35 +0100159 const uint8_t* GetVmapTable() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700160 uint32_t GetVmapTableOffset() const;
161 uint32_t GetVmapTableOffsetOffset() const;
Vladimir Marko8a630572014-04-09 18:45:35 +0100162
Brian Carlstromae826982011-11-09 01:33:42 -0800163 // Create an OatMethod with offsets relative to the given base address
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800164 OatMethod(const uint8_t* base, const uint32_t code_offset)
165 : begin_(base), code_offset_(code_offset) {
Ian Rogers97b52f82014-08-14 11:34:07 -0700166 }
Andreas Gampe758a8012015-04-03 21:28:42 -0700167 OatMethod(const OatMethod&) = default;
Ian Rogers97b52f82014-08-14 11:34:07 -0700168 ~OatMethod() {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700169
Andreas Gampe758a8012015-04-03 21:28:42 -0700170 OatMethod& operator=(const OatMethod&) = default;
171
Ian Rogers97b52f82014-08-14 11:34:07 -0700172 // A representation of an invalid OatMethod, used when an OatMethod or OatClass can't be found.
173 // See ClassLinker::FindOatMethodFor.
174 static const OatMethod Invalid() {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800175 return OatMethod(nullptr, -1);
Ian Rogers97b52f82014-08-14 11:34:07 -0700176 }
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100177
Brian Carlstromae826982011-11-09 01:33:42 -0800178 private:
179 template<class T>
180 T GetOatPointer(uint32_t offset) const {
181 if (offset == 0) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700182 return nullptr;
Brian Carlstromae826982011-11-09 01:33:42 -0800183 }
Ian Rogers30fab402012-01-23 15:43:46 -0800184 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800185 }
186
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800187 const uint8_t* begin_;
188 uint32_t code_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800189
190 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700191 };
192
Richard Uhler07b3c232015-03-31 15:57:54 -0700193 class OatClass FINAL {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700194 public:
Brian Carlstromba150c32013-08-27 17:31:03 -0700195 mirror::Class::Status GetStatus() const {
196 return status_;
197 }
198
199 OatClassType GetType() const {
200 return type_;
201 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800202
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700203 // Get the OatMethod entry based on its index into the class
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700204 // defintion. Direct methods come first, followed by virtual
205 // methods. Note that runtime created methods such as miranda
Brian Carlstrome24fa612011-09-29 00:53:55 -0700206 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700207 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700208
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700209 // Return a pointer to the OatMethodOffsets for the requested
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700210 // method_index, or null if none is present. Note that most
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700211 // callers should use GetOatMethod.
212 const OatMethodOffsets* GetOatMethodOffsets(uint32_t method_index) const;
213
214 // Return the offset from the start of the OatFile to the
215 // OatMethodOffsets for the requested method_index, or 0 if none
216 // is present. Note that most callers should use GetOatMethod.
217 uint32_t GetOatMethodOffsetsOffset(uint32_t method_index) const;
218
Ian Rogers97b52f82014-08-14 11:34:07 -0700219 // A representation of an invalid OatClass, used when an OatClass can't be found.
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100220 // See FindOatClass().
Ian Rogers97b52f82014-08-14 11:34:07 -0700221 static OatClass Invalid() {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000222 return OatClass(/* oat_file */ nullptr,
223 mirror::Class::kStatusErrorUnresolved,
224 kOatClassNoneCompiled,
225 /* bitmap_size */ 0,
226 /* bitmap_pointer */ nullptr,
227 /* methods_pointer */ nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700228 }
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100229
Brian Carlstrome24fa612011-09-29 00:53:55 -0700230 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800231 OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800232 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700233 OatClassType type,
234 uint32_t bitmap_size,
235 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800236 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700237
Ian Rogers97b52f82014-08-14 11:34:07 -0700238 const OatFile* const oat_file_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700239
Ian Rogers97b52f82014-08-14 11:34:07 -0700240 const mirror::Class::Status status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700241
Ian Rogers97b52f82014-08-14 11:34:07 -0700242 const OatClassType type_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700243
Ian Rogers97b52f82014-08-14 11:34:07 -0700244 const uint32_t* const bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700245
Ian Rogers97b52f82014-08-14 11:34:07 -0700246 const OatMethodOffsets* const methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700247
Richard Uhler07b3c232015-03-31 15:57:54 -0700248 friend class art::OatDexFile;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700249 };
Richard Uhler9a37efc2016-08-05 16:32:55 -0700250
251 // Get the OatDexFile for the given dex_location within this oat file.
252 // If dex_location_checksum is non-null, the OatDexFile will only be
253 // returned if it has a matching checksum.
254 // If error_msg is non-null and no OatDexFile is returned, error_msg will
255 // be updated with a description of why no OatDexFile was returned.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700256 const OatDexFile* GetOatDexFile(const char* dex_location,
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700257 const uint32_t* const dex_location_checksum,
Richard Uhler9a37efc2016-08-05 16:32:55 -0700258 /*out*/std::string* error_msg = nullptr) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700259 REQUIRES(!secondary_lookup_lock_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700260
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100261 const std::vector<const OatDexFile*>& GetOatDexFiles() const {
262 return oat_dex_files_storage_;
263 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700264
Ian Rogers30fab402012-01-23 15:43:46 -0800265 size_t Size() const {
266 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700267 }
268
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800269 bool Contains(const void* p) const {
270 return p >= Begin() && p < End();
271 }
272
Vladimir Marko5c42c292015-02-25 12:02:49 +0000273 size_t BssSize() const {
274 return BssEnd() - BssBegin();
275 }
276
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100277 size_t BssMethodsOffset() const {
278 // Note: This is used only for symbolizer and needs to return a valid .bss offset.
279 return (bss_methods_ != nullptr) ? bss_methods_ - BssBegin() : BssRootsOffset();
280 }
281
Vladimir Markoaad75c62016-10-03 08:46:48 +0000282 size_t BssRootsOffset() const {
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100283 // Note: This is used only for symbolizer and needs to return a valid .bss offset.
284 return (bss_roots_ != nullptr) ? bss_roots_ - BssBegin() : BssSize();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000285 }
286
David Brazdil7b49e6c2016-09-01 11:06:18 +0100287 size_t DexSize() const {
288 return DexEnd() - DexBegin();
289 }
290
Ian Rogers13735952014-10-08 12:43:28 -0700291 const uint8_t* Begin() const;
292 const uint8_t* End() const;
Alex Light53cb16b2014-06-12 11:26:29 -0700293
Vladimir Marko5c42c292015-02-25 12:02:49 +0000294 const uint8_t* BssBegin() const;
295 const uint8_t* BssEnd() const;
296
David Brazdil7b49e6c2016-09-01 11:06:18 +0100297 const uint8_t* DexBegin() const;
298 const uint8_t* DexEnd() const;
299
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100300 ArrayRef<ArtMethod*> GetBssMethods() const;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000301 ArrayRef<GcRoot<mirror::Object>> GetBssGcRoots() const;
302
Richard Uhlere5fed032015-03-18 08:21:11 -0700303 // Returns the absolute dex location for the encoded relative dex location.
304 //
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700305 // If not null, abs_dex_location is used to resolve the absolute dex
Richard Uhlere5fed032015-03-18 08:21:11 -0700306 // location of relative dex locations encoded in the oat file.
307 // For example, given absolute location "/data/app/foo/base.apk", encoded
Calin Juravlea308a322017-07-18 16:51:51 -0700308 // dex locations "base.apk", "base.apk!classes2.dex", etc. would be resolved
309 // to "/data/app/foo/base.apk", "/data/app/foo/base.apk!classes2.dex", etc.
Richard Uhlere5fed032015-03-18 08:21:11 -0700310 // Relative encoded dex locations that don't match the given abs_dex_location
311 // are left unchanged.
312 static std::string ResolveRelativeEncodedDexLocation(
313 const char* abs_dex_location, const std::string& rel_dex_location);
314
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100315 // Finds the associated oat class for a dex_file and descriptor. Returns an invalid OatClass on
316 // error and sets found to false.
317 static OatClass FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, bool* found);
318
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100319 VdexFile* GetVdexFile() const {
320 return vdex_.get();
321 }
322
Andreas Gampe049cff02015-12-01 23:27:12 -0800323 protected:
324 OatFile(const std::string& filename, bool executable);
325
Brian Carlstrome24fa612011-09-29 00:53:55 -0700326 private:
Brian Carlstrome24fa612011-09-29 00:53:55 -0700327 // The oat file name.
328 //
329 // The image will embed this to link its associated oat file.
330 const std::string location_;
331
David Brazdil7b49e6c2016-09-01 11:06:18 +0100332 // Pointer to the Vdex file with the Dex files for this Oat file.
333 std::unique_ptr<VdexFile> vdex_;
334
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800335 // Pointer to OatHeader.
Ian Rogers13735952014-10-08 12:43:28 -0700336 const uint8_t* begin_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800337
338 // Pointer to end of oat region for bounds checking.
Ian Rogers13735952014-10-08 12:43:28 -0700339 const uint8_t* end_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800340
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700341 // Pointer to the .bss section, if present, otherwise null.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100342 uint8_t* bss_begin_;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000343
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700344 // Pointer to the end of the .bss section, if present, otherwise null.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100345 uint8_t* bss_end_;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000346
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100347 // Pointer to the beginning of the ArtMethod*s in .bss section, if present, otherwise null.
348 uint8_t* bss_methods_;
349
Vladimir Markoaad75c62016-10-03 08:46:48 +0000350 // Pointer to the beginning of the GC roots in .bss section, if present, otherwise null.
351 uint8_t* bss_roots_;
352
Alex Light9dcc4572014-08-14 14:16:26 -0700353 // Was this oat_file loaded executable?
354 const bool is_executable_;
355
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100356 // Owning storage for the OatDexFile objects.
357 std::vector<const OatDexFile*> oat_dex_files_storage_;
358
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100359 // NOTE: We use a StringPiece as the key type to avoid a memory allocation on every
360 // lookup with a const char* key. The StringPiece doesn't own its backing storage,
361 // therefore we're using the OatDexFile::dex_file_location_ as the backing storage
362 // for keys in oat_dex_files_ and the string_cache_ entries for the backing storage
363 // of keys in secondary_oat_dex_files_ and oat_dex_files_by_canonical_location_.
Mathieu Chartierbad02672014-08-25 13:08:22 -0700364 typedef AllocationTrackingSafeMap<StringPiece, const OatDexFile*, kAllocatorTagOatFile> Table;
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100365
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100366 // Map each location and canonical location (if different) retrieved from the
367 // oat file to its OatDexFile. This map doesn't change after it's constructed in Setup()
368 // and therefore doesn't need any locking and provides the cheapest dex file lookup
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700369 // for GetOatDexFile() for a very frequent use case. Never contains a null value.
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100370 Table oat_dex_files_;
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100371
372 // Lock guarding all members needed for secondary lookup in GetOatDexFile().
373 mutable Mutex secondary_lookup_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
374
375 // If the primary oat_dex_files_ lookup fails, use a secondary map. This map stores
376 // the results of all previous secondary lookups, whether successful (non-null) or
377 // failed (null). If it doesn't contain an entry we need to calculate the canonical
378 // location and use oat_dex_files_by_canonical_location_.
379 mutable Table secondary_oat_dex_files_ GUARDED_BY(secondary_lookup_lock_);
380
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100381 // Cache of strings. Contains the backing storage for keys in the secondary_oat_dex_files_
382 // and the lazily initialized oat_dex_files_by_canonical_location_.
383 // NOTE: We're keeping references to contained strings in form of StringPiece and adding
384 // new strings to the end. The adding of a new element must not touch any previously stored
385 // elements. std::list<> and std::deque<> satisfy this requirement, std::vector<> doesn't.
386 mutable std::list<std::string> string_cache_ GUARDED_BY(secondary_lookup_lock_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700387
Mathieu Chartier5351da02016-02-17 16:19:53 -0800388 friend class gc::collector::DummyOatFile; // For modifying begin_ and end_.
Brian Carlstrome24fa612011-09-29 00:53:55 -0700389 friend class OatClass;
Richard Uhler07b3c232015-03-31 15:57:54 -0700390 friend class art::OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800391 friend class OatDumper; // For GetBase and GetLimit
Andreas Gampe049cff02015-12-01 23:27:12 -0800392 friend class OatFileBase;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700393 DISALLOW_COPY_AND_ASSIGN(OatFile);
394};
395
Richard Uhler07b3c232015-03-31 15:57:54 -0700396// OatDexFile should be an inner class of OatFile. Unfortunately, C++ doesn't
397// support forward declarations of inner classes, and we want to
398// forward-declare OatDexFile so that we can store an opaque pointer to an
399// OatDexFile in DexFile.
400class OatDexFile FINAL {
401 public:
402 // Opens the DexFile referred to by this OatDexFile from within the containing OatFile.
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700403 std::unique_ptr<const DexFile> OpenDexFile(std::string* error_msg) const;
Richard Uhler07b3c232015-03-31 15:57:54 -0700404
Mathieu Chartier1b868492016-11-16 16:22:37 -0800405 // May return null if the OatDexFile only contains a type lookup table. This case only happens
406 // for the compiler to speed up compilation.
Richard Uhler07b3c232015-03-31 15:57:54 -0700407 const OatFile* GetOatFile() const {
Mathieu Chartier1b868492016-11-16 16:22:37 -0800408 // Avoid pulling in runtime.h in the header file.
409 if (kIsDebugBuild && oat_file_ == nullptr) {
410 AssertAotCompiler();
411 }
Richard Uhler07b3c232015-03-31 15:57:54 -0700412 return oat_file_;
413 }
414
415 // Returns the size of the DexFile refered to by this OatDexFile.
416 size_t FileSize() const;
417
418 // Returns original path of DexFile that was the source of this OatDexFile.
419 const std::string& GetDexFileLocation() const {
420 return dex_file_location_;
421 }
422
423 // Returns the canonical location of DexFile that was the source of this OatDexFile.
424 const std::string& GetCanonicalDexFileLocation() const {
425 return canonical_dex_file_location_;
426 }
427
428 // Returns checksum of original DexFile that was the source of this OatDexFile;
429 uint32_t GetDexFileLocationChecksum() const {
430 return dex_file_location_checksum_;
431 }
432
433 // Returns the OatClass for the class specified by the given DexFile class_def_index.
434 OatFile::OatClass GetOatClass(uint16_t class_def_index) const;
435
436 // Returns the offset to the OatClass information. Most callers should use GetOatClass.
437 uint32_t GetOatClassOffset(uint16_t class_def_index) const;
438
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300439 const uint8_t* GetLookupTableData() const {
440 return lookup_table_data_;
441 }
442
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100443 const MethodBssMapping* GetMethodBssMapping() const {
444 return method_bss_mapping_;
445 }
446
Andreas Gampe2ba88952016-04-29 17:52:07 -0700447 const uint8_t* GetDexFilePointer() const {
448 return dex_file_pointer_;
449 }
450
David Sehr9aa352e2016-09-15 18:13:52 -0700451 // Looks up a class definition by its class descriptor. Hash must be
452 // ComputeModifiedUtf8Hash(descriptor).
453 static const DexFile::ClassDef* FindClassDef(const DexFile& dex_file,
454 const char* descriptor,
455 size_t hash);
456
Mathieu Chartier120aa282017-08-05 16:03:03 -0700457 // Madvise the dex file based on the state we are moving to.
458 static void MadviseDexFile(const DexFile& dex_file, MadviseState state);
459
David Sehr9aa352e2016-09-15 18:13:52 -0700460 TypeLookupTable* GetTypeLookupTable() const {
461 return lookup_table_.get();
462 }
463
Richard Uhler07b3c232015-03-31 15:57:54 -0700464 ~OatDexFile();
465
Mathieu Chartier1b868492016-11-16 16:22:37 -0800466 // Create only with a type lookup table, used by the compiler to speed up compilation.
467 explicit OatDexFile(std::unique_ptr<TypeLookupTable>&& lookup_table);
468
Mathieu Chartier120aa282017-08-05 16:03:03 -0700469 // Return the dex layout sections.
470 const DexLayoutSections* GetDexLayoutSections() const {
471 return dex_layout_sections_;
472 }
473
Richard Uhler07b3c232015-03-31 15:57:54 -0700474 private:
475 OatDexFile(const OatFile* oat_file,
476 const std::string& dex_file_location,
477 const std::string& canonical_dex_file_location,
478 uint32_t dex_file_checksum,
479 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300480 const uint8_t* lookup_table_data,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100481 const MethodBssMapping* method_bss_mapping,
Vladimir Marko09d09432015-09-08 13:47:48 +0100482 const uint32_t* oat_class_offsets_pointer,
Mathieu Chartier120aa282017-08-05 16:03:03 -0700483 const DexLayoutSections* dex_layout_sections);
Richard Uhler07b3c232015-03-31 15:57:54 -0700484
Mathieu Chartier1b868492016-11-16 16:22:37 -0800485 static void AssertAotCompiler();
486
487 const OatFile* const oat_file_ = nullptr;
Richard Uhler07b3c232015-03-31 15:57:54 -0700488 const std::string dex_file_location_;
489 const std::string canonical_dex_file_location_;
Mathieu Chartier1b868492016-11-16 16:22:37 -0800490 const uint32_t dex_file_location_checksum_ = 0u;
491 const uint8_t* const dex_file_pointer_ = nullptr;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100492 const uint8_t* const lookup_table_data_ = nullptr;
493 const MethodBssMapping* const method_bss_mapping_ = nullptr;
Mathieu Chartier1b868492016-11-16 16:22:37 -0800494 const uint32_t* const oat_class_offsets_pointer_ = 0u;
David Sehr9aa352e2016-09-15 18:13:52 -0700495 mutable std::unique_ptr<TypeLookupTable> lookup_table_;
Mathieu Chartier120aa282017-08-05 16:03:03 -0700496 const DexLayoutSections* const dex_layout_sections_ = nullptr;
Richard Uhler07b3c232015-03-31 15:57:54 -0700497
498 friend class OatFile;
Andreas Gampe049cff02015-12-01 23:27:12 -0800499 friend class OatFileBase;
Richard Uhler07b3c232015-03-31 15:57:54 -0700500 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
501};
502
Brian Carlstrome24fa612011-09-29 00:53:55 -0700503} // namespace art
504
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700505#endif // ART_RUNTIME_OAT_FILE_H_