blob: 6c40c6890277e57adc9e389274277ebe15d5420b [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 Marko3f5838d2014-08-07 18:07:18 +010024#include "base/mutex.h"
Igor Murashkina315f5c2015-07-31 17:35:52 -070025#include "base/out_fwd.h"
Vladimir Marko539690a2014-06-05 18:36:42 +010026#include "base/stringpiece.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "dex_file.h"
28#include "invoke_type.h"
29#include "mem_map.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070030#include "mirror/class.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "oat.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080032#include "os.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070033
34namespace art {
35
Brian Carlstromba150c32013-08-27 17:31:03 -070036class BitVector;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080037class ElfFile;
38class MemMap;
39class OatMethodOffsets;
Ian Rogers33e95662013-05-20 20:29:14 -070040class OatHeader;
Richard Uhler07b3c232015-03-31 15:57:54 -070041class OatDexFile;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080042
Richard Uhler07b3c232015-03-31 15:57:54 -070043class OatFile FINAL {
Brian Carlstrome24fa612011-09-29 00:53:55 -070044 public:
Richard Uhler07b3c232015-03-31 15:57:54 -070045 typedef art::OatDexFile OatDexFile;
46
Alex Light84d76052014-08-22 17:49:35 -070047 // Opens an oat file contained within the given elf file. This is always opened as
48 // non-executable at the moment.
Igor Murashkina315f5c2015-07-31 17:35:52 -070049 static OatFile* OpenWithElfFile(ElfFile* elf_file,
50 const std::string& location,
Richard Uhlere5fed032015-03-18 08:21:11 -070051 const char* abs_dex_location,
Igor Murashkina315f5c2015-07-31 17:35:52 -070052 out<std::string> error_msg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070053 // Open an oat file. Returns null on failure. Requested base can
Brian Carlstrome24fa612011-09-29 00:53:55 -070054 // optionally be used to request where the file should be loaded.
Richard Uhlere5fed032015-03-18 08:21:11 -070055 // See the ResolveRelativeEncodedDexLocation for a description of how the
56 // abs_dex_location argument is used.
Brian Carlstrome24fa612011-09-29 00:53:55 -070057 static OatFile* Open(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -080058 const std::string& location,
Ian Rogers13735952014-10-08 12:43:28 -070059 uint8_t* requested_base,
Igor Murashkin46774762014-10-22 11:37:02 -070060 uint8_t* oat_file_begin,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070061 bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -070062 const char* abs_dex_location,
Igor Murashkina315f5c2015-07-31 17:35:52 -070063 out<std::string> error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -070064
Brian Carlstrom700c8d32012-11-05 10:42:02 -080065 // Open an oat file from an already opened File.
Brian Carlstrom265091e2013-01-30 14:08:26 -080066 // Does not use dlopen underneath so cannot be used for runtime use
67 // where relocations may be required. Currently used from
68 // ImageWriter which wants to open a writable version from an existing
69 // file descriptor for patching.
Igor Murashkina315f5c2015-07-31 17:35:52 -070070 static OatFile* OpenWritable(File* file,
71 const std::string& location,
Richard Uhlere5fed032015-03-18 08:21:11 -070072 const char* abs_dex_location,
Igor Murashkina315f5c2015-07-31 17:35:52 -070073 out<std::string> error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -070074 // Opens an oat file from an already opened File. Maps it PROT_READ, MAP_PRIVATE.
Igor Murashkina315f5c2015-07-31 17:35:52 -070075 static OatFile* OpenReadable(File* file,
76 const std::string& location,
Richard Uhlere5fed032015-03-18 08:21:11 -070077 const char* abs_dex_location,
Igor Murashkina315f5c2015-07-31 17:35:52 -070078 out<std::string> error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080079
Brian Carlstrome24fa612011-09-29 00:53:55 -070080 ~OatFile();
81
Alex Light9dcc4572014-08-14 14:16:26 -070082 bool IsExecutable() const {
83 return is_executable_;
84 }
85
Andreas Gampe7ba64962014-10-23 11:37:40 -070086 bool IsPic() const;
87
Sebastien Hertz0de11332015-05-13 12:14:05 +020088 // Indicates whether the oat file was compiled with full debugging capability.
89 bool IsDebuggable() const;
90
Alex Light53cb16b2014-06-12 11:26:29 -070091 ElfFile* GetElfFile() const {
92 CHECK_NE(reinterpret_cast<uintptr_t>(elf_file_.get()), reinterpret_cast<uintptr_t>(nullptr))
93 << "Cannot get an elf file from " << GetLocation();
94 return elf_file_.get();
95 }
96
Brian Carlstrome24fa612011-09-29 00:53:55 -070097 const std::string& GetLocation() const {
98 return location_;
99 }
100
101 const OatHeader& GetOatHeader() const;
102
Richard Uhler07b3c232015-03-31 15:57:54 -0700103 class OatMethod FINAL {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700104 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700105 void LinkMethod(ArtMethod* method) const;
Brian Carlstromae826982011-11-09 01:33:42 -0800106
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100107 uint32_t GetCodeOffset() const;
Brian Carlstromae826982011-11-09 01:33:42 -0800108
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +0100109 const void* GetQuickCode() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700110
111 // Returns size of quick code.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800112 uint32_t GetQuickCodeSize() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700113 uint32_t GetQuickCodeSizeOffset() const;
114
115 // Returns OatQuickMethodHeader for debugging. Most callers should
116 // use more specific methods such as GetQuickCodeSize.
117 const OatQuickMethodHeader* GetOatQuickMethodHeader() const;
118 uint32_t GetOatQuickMethodHeaderOffset() const;
Logan Chien0c717dd2012-03-28 18:31:07 +0800119
Vladimir Marko7624d252014-05-02 14:40:15 +0100120 size_t GetFrameSizeInBytes() const;
121 uint32_t GetCoreSpillMask() const;
122 uint32_t GetFpSpillMask() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700123
Vladimir Marko8a630572014-04-09 18:45:35 +0100124 const uint8_t* GetMappingTable() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700125 uint32_t GetMappingTableOffset() const;
126 uint32_t GetMappingTableOffsetOffset() const;
127
Vladimir Marko8a630572014-04-09 18:45:35 +0100128 const uint8_t* GetVmapTable() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700129 uint32_t GetVmapTableOffset() const;
130 uint32_t GetVmapTableOffsetOffset() const;
Vladimir Marko8a630572014-04-09 18:45:35 +0100131
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800132 const uint8_t* GetGcMap() const;
133 uint32_t GetGcMapOffset() const;
134 uint32_t GetGcMapOffsetOffset() const;
135
Brian Carlstromae826982011-11-09 01:33:42 -0800136 // Create an OatMethod with offsets relative to the given base address
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800137 OatMethod(const uint8_t* base, const uint32_t code_offset)
138 : begin_(base), code_offset_(code_offset) {
Ian Rogers97b52f82014-08-14 11:34:07 -0700139 }
Andreas Gampe758a8012015-04-03 21:28:42 -0700140 OatMethod(const OatMethod&) = default;
Ian Rogers97b52f82014-08-14 11:34:07 -0700141 ~OatMethod() {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700142
Andreas Gampe758a8012015-04-03 21:28:42 -0700143 OatMethod& operator=(const OatMethod&) = default;
144
Ian Rogers97b52f82014-08-14 11:34:07 -0700145 // A representation of an invalid OatMethod, used when an OatMethod or OatClass can't be found.
146 // See ClassLinker::FindOatMethodFor.
147 static const OatMethod Invalid() {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800148 return OatMethod(nullptr, -1);
Ian Rogers97b52f82014-08-14 11:34:07 -0700149 }
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100150
Brian Carlstromae826982011-11-09 01:33:42 -0800151 private:
152 template<class T>
153 T GetOatPointer(uint32_t offset) const {
154 if (offset == 0) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700155 return nullptr;
Brian Carlstromae826982011-11-09 01:33:42 -0800156 }
Ian Rogers30fab402012-01-23 15:43:46 -0800157 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800158 }
159
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800160 const uint8_t* begin_;
161 uint32_t code_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800162
163 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700164 };
165
Richard Uhler07b3c232015-03-31 15:57:54 -0700166 class OatClass FINAL {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700167 public:
Brian Carlstromba150c32013-08-27 17:31:03 -0700168 mirror::Class::Status GetStatus() const {
169 return status_;
170 }
171
172 OatClassType GetType() const {
173 return type_;
174 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800175
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700176 // Get the OatMethod entry based on its index into the class
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700177 // defintion. Direct methods come first, followed by virtual
178 // methods. Note that runtime created methods such as miranda
Brian Carlstrome24fa612011-09-29 00:53:55 -0700179 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700180 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700181
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700182 // Return a pointer to the OatMethodOffsets for the requested
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700183 // method_index, or null if none is present. Note that most
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700184 // callers should use GetOatMethod.
185 const OatMethodOffsets* GetOatMethodOffsets(uint32_t method_index) const;
186
187 // Return the offset from the start of the OatFile to the
188 // OatMethodOffsets for the requested method_index, or 0 if none
189 // is present. Note that most callers should use GetOatMethod.
190 uint32_t GetOatMethodOffsetsOffset(uint32_t method_index) const;
191
Ian Rogers97b52f82014-08-14 11:34:07 -0700192 // A representation of an invalid OatClass, used when an OatClass can't be found.
193 // See ClassLinker::FindOatClass.
194 static OatClass Invalid() {
195 return OatClass(nullptr, mirror::Class::kStatusError, kOatClassNoneCompiled, 0, nullptr,
196 nullptr);
197 }
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100198
Brian Carlstrome24fa612011-09-29 00:53:55 -0700199 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800200 OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800201 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700202 OatClassType type,
203 uint32_t bitmap_size,
204 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800205 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700206
Ian Rogers97b52f82014-08-14 11:34:07 -0700207 const OatFile* const oat_file_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700208
Ian Rogers97b52f82014-08-14 11:34:07 -0700209 const mirror::Class::Status status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700210
Ian Rogers97b52f82014-08-14 11:34:07 -0700211 const OatClassType type_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700212
Ian Rogers97b52f82014-08-14 11:34:07 -0700213 const uint32_t* const bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700214
Ian Rogers97b52f82014-08-14 11:34:07 -0700215 const OatMethodOffsets* const methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700216
Richard Uhler07b3c232015-03-31 15:57:54 -0700217 friend class art::OatDexFile;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700218 };
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700219 const OatDexFile* GetOatDexFile(const char* dex_location,
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700220 const uint32_t* const dex_location_checksum,
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100221 bool exception_if_not_found = true) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700222 REQUIRES(!secondary_lookup_lock_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700223
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100224 const std::vector<const OatDexFile*>& GetOatDexFiles() const {
225 return oat_dex_files_storage_;
226 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700227
Ian Rogers30fab402012-01-23 15:43:46 -0800228 size_t Size() const {
229 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700230 }
231
Vladimir Marko5c42c292015-02-25 12:02:49 +0000232 size_t BssSize() const {
233 return BssEnd() - BssBegin();
234 }
235
Ian Rogers13735952014-10-08 12:43:28 -0700236 const uint8_t* Begin() const;
237 const uint8_t* End() const;
Alex Light53cb16b2014-06-12 11:26:29 -0700238
Vladimir Marko5c42c292015-02-25 12:02:49 +0000239 const uint8_t* BssBegin() const;
240 const uint8_t* BssEnd() const;
241
Richard Uhlere5fed032015-03-18 08:21:11 -0700242 // Returns the absolute dex location for the encoded relative dex location.
243 //
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700244 // If not null, abs_dex_location is used to resolve the absolute dex
Richard Uhlere5fed032015-03-18 08:21:11 -0700245 // location of relative dex locations encoded in the oat file.
246 // For example, given absolute location "/data/app/foo/base.apk", encoded
247 // dex locations "base.apk", "base.apk:classes2.dex", etc. would be resolved
248 // to "/data/app/foo/base.apk", "/data/app/foo/base.apk:classes2.dex", etc.
249 // Relative encoded dex locations that don't match the given abs_dex_location
250 // are left unchanged.
251 static std::string ResolveRelativeEncodedDexLocation(
252 const char* abs_dex_location, const std::string& rel_dex_location);
253
Andreas Gampe7848da42015-04-09 11:15:04 -0700254 // Create a dependency list (dex locations and checksums) for the given dex files.
255 static std::string EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files);
256
257 // Check the given dependency list against their dex files - thus the name "Static," this does
258 // not check the class-loader environment, only whether there have been file updates.
Igor Murashkina315f5c2015-07-31 17:35:52 -0700259 static bool CheckStaticDexFileDependencies(const char* dex_dependencies,
260 out<std::string> error_msg);
Andreas Gampe7848da42015-04-09 11:15:04 -0700261
262 // Get the dex locations of a dependency list. Note: this is *not* cleaned for synthetic
263 // locations of multidex files.
264 static bool GetDexLocationsFromDependencies(const char* dex_dependencies,
Igor Murashkina315f5c2015-07-31 17:35:52 -0700265 out<std::vector<std::string>> locations);
Andreas Gampe7848da42015-04-09 11:15:04 -0700266
Brian Carlstrome24fa612011-09-29 00:53:55 -0700267 private:
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800268 static void CheckLocation(const std::string& location);
269
Andreas Gampefa8429b2015-04-07 18:34:42 -0700270 static OatFile* OpenDlopen(const std::string& elf_filename,
271 const std::string& location,
272 uint8_t* requested_base,
273 const char* abs_dex_location,
Igor Murashkina315f5c2015-07-31 17:35:52 -0700274 out<std::string> error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700275
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800276 static OatFile* OpenElfFile(File* file,
277 const std::string& location,
Ian Rogers13735952014-10-08 12:43:28 -0700278 uint8_t* requested_base,
Igor Murashkin46774762014-10-22 11:37:02 -0700279 uint8_t* oat_file_begin, // Override base if not null
Brian Carlstromf1d34552013-07-12 20:22:23 -0700280 bool writable,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700281 bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700282 const char* abs_dex_location,
Igor Murashkina315f5c2015-07-31 17:35:52 -0700283 out<std::string> error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800284
Alex Light9dcc4572014-08-14 14:16:26 -0700285 explicit OatFile(const std::string& filename, bool executable);
Igor Murashkina315f5c2015-07-31 17:35:52 -0700286 bool Dlopen(const std::string& elf_filename,
287 uint8_t* requested_base,
288 const char* abs_dex_location,
289 out<std::string> error_msg);
290 bool ElfFileOpen(File* file,
291 uint8_t* requested_base,
Igor Murashkin46774762014-10-22 11:37:02 -0700292 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
Igor Murashkina315f5c2015-07-31 17:35:52 -0700293 bool writable,
294 bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700295 const char* abs_dex_location,
Igor Murashkina315f5c2015-07-31 17:35:52 -0700296 out<std::string> error_msg);
Richard Uhlere5fed032015-03-18 08:21:11 -0700297
Igor Murashkina315f5c2015-07-31 17:35:52 -0700298 bool Setup(const char* abs_dex_location, out<std::string> error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700299
Brian Carlstrome24fa612011-09-29 00:53:55 -0700300 // The oat file name.
301 //
302 // The image will embed this to link its associated oat file.
303 const std::string location_;
304
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800305 // Pointer to OatHeader.
Ian Rogers13735952014-10-08 12:43:28 -0700306 const uint8_t* begin_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800307
308 // Pointer to end of oat region for bounds checking.
Ian Rogers13735952014-10-08 12:43:28 -0700309 const uint8_t* end_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800310
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700311 // Pointer to the .bss section, if present, otherwise null.
Vladimir Marko5c42c292015-02-25 12:02:49 +0000312 const uint8_t* bss_begin_;
313
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700314 // Pointer to the end of the .bss section, if present, otherwise null.
Vladimir Marko5c42c292015-02-25 12:02:49 +0000315 const uint8_t* bss_end_;
316
Alex Light9dcc4572014-08-14 14:16:26 -0700317 // Was this oat_file loaded executable?
318 const bool is_executable_;
319
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800320 // Backing memory map for oat file during when opened by ElfWriter during initial compilation.
Ian Rogers700a4022014-05-19 16:49:03 -0700321 std::unique_ptr<MemMap> mem_map_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700322
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800323 // Backing memory map for oat file during cross compilation.
Ian Rogers700a4022014-05-19 16:49:03 -0700324 std::unique_ptr<ElfFile> elf_file_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800325
326 // dlopen handle during runtime.
327 void* dlopen_handle_;
328
David Srbecky82e73dc2015-06-17 18:36:23 +0100329 // Dummy memory map objects corresponding to the regions mapped by dlopen.
330 std::vector<std::unique_ptr<MemMap>> dlopen_mmaps_;
331
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100332 // Owning storage for the OatDexFile objects.
333 std::vector<const OatDexFile*> oat_dex_files_storage_;
334
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100335 // NOTE: We use a StringPiece as the key type to avoid a memory allocation on every
336 // lookup with a const char* key. The StringPiece doesn't own its backing storage,
337 // therefore we're using the OatDexFile::dex_file_location_ as the backing storage
338 // for keys in oat_dex_files_ and the string_cache_ entries for the backing storage
339 // of keys in secondary_oat_dex_files_ and oat_dex_files_by_canonical_location_.
Mathieu Chartierbad02672014-08-25 13:08:22 -0700340 typedef AllocationTrackingSafeMap<StringPiece, const OatDexFile*, kAllocatorTagOatFile> Table;
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100341
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100342 // Map each location and canonical location (if different) retrieved from the
343 // oat file to its OatDexFile. This map doesn't change after it's constructed in Setup()
344 // and therefore doesn't need any locking and provides the cheapest dex file lookup
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700345 // for GetOatDexFile() for a very frequent use case. Never contains a null value.
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100346 Table oat_dex_files_;
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100347
348 // Lock guarding all members needed for secondary lookup in GetOatDexFile().
349 mutable Mutex secondary_lookup_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
350
351 // If the primary oat_dex_files_ lookup fails, use a secondary map. This map stores
352 // the results of all previous secondary lookups, whether successful (non-null) or
353 // failed (null). If it doesn't contain an entry we need to calculate the canonical
354 // location and use oat_dex_files_by_canonical_location_.
355 mutable Table secondary_oat_dex_files_ GUARDED_BY(secondary_lookup_lock_);
356
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100357 // Cache of strings. Contains the backing storage for keys in the secondary_oat_dex_files_
358 // and the lazily initialized oat_dex_files_by_canonical_location_.
359 // NOTE: We're keeping references to contained strings in form of StringPiece and adding
360 // new strings to the end. The adding of a new element must not touch any previously stored
361 // elements. std::list<> and std::deque<> satisfy this requirement, std::vector<> doesn't.
362 mutable std::list<std::string> string_cache_ GUARDED_BY(secondary_lookup_lock_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700363
364 friend class OatClass;
Richard Uhler07b3c232015-03-31 15:57:54 -0700365 friend class art::OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800366 friend class OatDumper; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700367 DISALLOW_COPY_AND_ASSIGN(OatFile);
368};
369
Richard Uhler07b3c232015-03-31 15:57:54 -0700370// OatDexFile should be an inner class of OatFile. Unfortunately, C++ doesn't
371// support forward declarations of inner classes, and we want to
372// forward-declare OatDexFile so that we can store an opaque pointer to an
373// OatDexFile in DexFile.
374class OatDexFile FINAL {
375 public:
376 // Opens the DexFile referred to by this OatDexFile from within the containing OatFile.
Igor Murashkina315f5c2015-07-31 17:35:52 -0700377 std::unique_ptr<const DexFile> OpenDexFile(out<std::string> error_msg) const;
Richard Uhler07b3c232015-03-31 15:57:54 -0700378
379 const OatFile* GetOatFile() const {
380 return oat_file_;
381 }
382
383 // Returns the size of the DexFile refered to by this OatDexFile.
384 size_t FileSize() const;
385
386 // Returns original path of DexFile that was the source of this OatDexFile.
387 const std::string& GetDexFileLocation() const {
388 return dex_file_location_;
389 }
390
391 // Returns the canonical location of DexFile that was the source of this OatDexFile.
392 const std::string& GetCanonicalDexFileLocation() const {
393 return canonical_dex_file_location_;
394 }
395
396 // Returns checksum of original DexFile that was the source of this OatDexFile;
397 uint32_t GetDexFileLocationChecksum() const {
398 return dex_file_location_checksum_;
399 }
400
401 // Returns the OatClass for the class specified by the given DexFile class_def_index.
402 OatFile::OatClass GetOatClass(uint16_t class_def_index) const;
403
404 // Returns the offset to the OatClass information. Most callers should use GetOatClass.
405 uint32_t GetOatClassOffset(uint16_t class_def_index) const;
406
407 ~OatDexFile();
408
409 private:
410 OatDexFile(const OatFile* oat_file,
411 const std::string& dex_file_location,
412 const std::string& canonical_dex_file_location,
413 uint32_t dex_file_checksum,
414 const uint8_t* dex_file_pointer,
415 const uint32_t* oat_class_offsets_pointer);
416
417 const OatFile* const oat_file_;
418 const std::string dex_file_location_;
419 const std::string canonical_dex_file_location_;
420 const uint32_t dex_file_location_checksum_;
421 const uint8_t* const dex_file_pointer_;
422 const uint32_t* const oat_class_offsets_pointer_;
423
424 friend class OatFile;
425 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
426};
427
Brian Carlstrome24fa612011-09-29 00:53:55 -0700428} // namespace art
429
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700430#endif // ART_RUNTIME_OAT_FILE_H_