blob: 51952f318c2953b5bc4508e3d28ff1a1b767c405 [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"
Vladimir Marko539690a2014-06-05 18:36:42 +010025#include "base/stringpiece.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "dex_file.h"
27#include "invoke_type.h"
28#include "mem_map.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070029#include "mirror/class.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "oat.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080031#include "os.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070032
33namespace art {
34
Brian Carlstromba150c32013-08-27 17:31:03 -070035class BitVector;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080036class ElfFile;
37class MemMap;
38class OatMethodOffsets;
Ian Rogers33e95662013-05-20 20:29:14 -070039class OatHeader;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080040
Brian Carlstrome24fa612011-09-29 00:53:55 -070041class OatFile {
42 public:
Alex Light84d76052014-08-22 17:49:35 -070043 // Opens an oat file contained within the given elf file. This is always opened as
44 // non-executable at the moment.
45 static OatFile* OpenWithElfFile(ElfFile* elf_file, const std::string& location,
Richard Uhlere5fed032015-03-18 08:21:11 -070046 const char* abs_dex_location,
Alex Light84d76052014-08-22 17:49:35 -070047 std::string* error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -070048 // Open an oat file. Returns NULL on failure. Requested base can
49 // optionally be used to request where the file should be loaded.
Richard Uhlere5fed032015-03-18 08:21:11 -070050 // See the ResolveRelativeEncodedDexLocation for a description of how the
51 // abs_dex_location argument is used.
Brian Carlstrome24fa612011-09-29 00:53:55 -070052 static OatFile* Open(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -080053 const std::string& location,
Ian Rogers13735952014-10-08 12:43:28 -070054 uint8_t* requested_base,
Igor Murashkin46774762014-10-22 11:37:02 -070055 uint8_t* oat_file_begin,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070056 bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -070057 const char* abs_dex_location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070058 std::string* error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -070059
Brian Carlstrom700c8d32012-11-05 10:42:02 -080060 // Open an oat file from an already opened File.
Brian Carlstrom265091e2013-01-30 14:08:26 -080061 // Does not use dlopen underneath so cannot be used for runtime use
62 // where relocations may be required. Currently used from
63 // ImageWriter which wants to open a writable version from an existing
64 // file descriptor for patching.
Richard Uhlere5fed032015-03-18 08:21:11 -070065 static OatFile* OpenWritable(File* file, const std::string& location,
66 const char* abs_dex_location,
67 std::string* error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -070068 // Opens an oat file from an already opened File. Maps it PROT_READ, MAP_PRIVATE.
Richard Uhlere5fed032015-03-18 08:21:11 -070069 static OatFile* OpenReadable(File* file, const std::string& location,
70 const char* abs_dex_location,
71 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080072
Brian Carlstrome24fa612011-09-29 00:53:55 -070073 ~OatFile();
74
Alex Light9dcc4572014-08-14 14:16:26 -070075 bool IsExecutable() const {
76 return is_executable_;
77 }
78
Andreas Gampe7ba64962014-10-23 11:37:40 -070079 bool IsPic() const;
80
Alex Light53cb16b2014-06-12 11:26:29 -070081 ElfFile* GetElfFile() const {
82 CHECK_NE(reinterpret_cast<uintptr_t>(elf_file_.get()), reinterpret_cast<uintptr_t>(nullptr))
83 << "Cannot get an elf file from " << GetLocation();
84 return elf_file_.get();
85 }
86
Brian Carlstrome24fa612011-09-29 00:53:55 -070087 const std::string& GetLocation() const {
88 return location_;
89 }
90
91 const OatHeader& GetOatHeader() const;
92
93 class OatDexFile;
94
Brian Carlstrom3320cf42011-10-04 14:58:28 -070095 class OatMethod {
96 public:
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070097 void LinkMethod(mirror::ArtMethod* method) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromae826982011-11-09 01:33:42 -080098
99 uint32_t GetCodeOffset() const {
100 return code_offset_;
101 }
Brian Carlstromae826982011-11-09 01:33:42 -0800102
Ian Rogersef7d42f2014-01-06 12:55:46 -0800103 const void* GetQuickCode() const {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800104 return GetOatPointer<const void*>(code_offset_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800105 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700106
107 // Returns size of quick code.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800108 uint32_t GetQuickCodeSize() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700109 uint32_t GetQuickCodeSizeOffset() const;
110
111 // Returns OatQuickMethodHeader for debugging. Most callers should
112 // use more specific methods such as GetQuickCodeSize.
113 const OatQuickMethodHeader* GetOatQuickMethodHeader() const;
114 uint32_t GetOatQuickMethodHeaderOffset() const;
Logan Chien0c717dd2012-03-28 18:31:07 +0800115
Vladimir Marko7624d252014-05-02 14:40:15 +0100116 size_t GetFrameSizeInBytes() const;
117 uint32_t GetCoreSpillMask() const;
118 uint32_t GetFpSpillMask() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700119
Vladimir Marko8a630572014-04-09 18:45:35 +0100120 const uint8_t* GetMappingTable() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700121 uint32_t GetMappingTableOffset() const;
122 uint32_t GetMappingTableOffsetOffset() const;
123
Vladimir Marko8a630572014-04-09 18:45:35 +0100124 const uint8_t* GetVmapTable() const;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700125 uint32_t GetVmapTableOffset() const;
126 uint32_t GetVmapTableOffsetOffset() const;
Vladimir Marko8a630572014-04-09 18:45:35 +0100127
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800128 const uint8_t* GetGcMap() const;
129 uint32_t GetGcMapOffset() const;
130 uint32_t GetGcMapOffsetOffset() const;
131
Brian Carlstromae826982011-11-09 01:33:42 -0800132 // Create an OatMethod with offsets relative to the given base address
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800133 OatMethod(const uint8_t* base, const uint32_t code_offset)
134 : begin_(base), code_offset_(code_offset) {
Ian Rogers97b52f82014-08-14 11:34:07 -0700135 }
136 ~OatMethod() {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700137
Ian Rogers97b52f82014-08-14 11:34:07 -0700138 // A representation of an invalid OatMethod, used when an OatMethod or OatClass can't be found.
139 // See ClassLinker::FindOatMethodFor.
140 static const OatMethod Invalid() {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800141 return OatMethod(nullptr, -1);
Ian Rogers97b52f82014-08-14 11:34:07 -0700142 }
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100143
Brian Carlstromae826982011-11-09 01:33:42 -0800144 private:
145 template<class T>
146 T GetOatPointer(uint32_t offset) const {
147 if (offset == 0) {
148 return NULL;
149 }
Ian Rogers30fab402012-01-23 15:43:46 -0800150 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800151 }
152
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800153 const uint8_t* begin_;
154 uint32_t code_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800155
156 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700157 };
158
Brian Carlstrome24fa612011-09-29 00:53:55 -0700159 class OatClass {
160 public:
Brian Carlstromba150c32013-08-27 17:31:03 -0700161 mirror::Class::Status GetStatus() const {
162 return status_;
163 }
164
165 OatClassType GetType() const {
166 return type_;
167 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800168
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700169 // Get the OatMethod entry based on its index into the class
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700170 // defintion. Direct methods come first, followed by virtual
171 // methods. Note that runtime created methods such as miranda
Brian Carlstrome24fa612011-09-29 00:53:55 -0700172 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700173 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700174
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700175 // Return a pointer to the OatMethodOffsets for the requested
176 // method_index, or nullptr if none is present. Note that most
177 // callers should use GetOatMethod.
178 const OatMethodOffsets* GetOatMethodOffsets(uint32_t method_index) const;
179
180 // Return the offset from the start of the OatFile to the
181 // OatMethodOffsets for the requested method_index, or 0 if none
182 // is present. Note that most callers should use GetOatMethod.
183 uint32_t GetOatMethodOffsetsOffset(uint32_t method_index) const;
184
Ian Rogers97b52f82014-08-14 11:34:07 -0700185 // A representation of an invalid OatClass, used when an OatClass can't be found.
186 // See ClassLinker::FindOatClass.
187 static OatClass Invalid() {
188 return OatClass(nullptr, mirror::Class::kStatusError, kOatClassNoneCompiled, 0, nullptr,
189 nullptr);
190 }
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100191
Brian Carlstrome24fa612011-09-29 00:53:55 -0700192 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800193 OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800194 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700195 OatClassType type,
196 uint32_t bitmap_size,
197 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800198 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700199
Ian Rogers97b52f82014-08-14 11:34:07 -0700200 const OatFile* const oat_file_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700201
Ian Rogers97b52f82014-08-14 11:34:07 -0700202 const mirror::Class::Status status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700203
Ian Rogers97b52f82014-08-14 11:34:07 -0700204 const OatClassType type_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700205
Ian Rogers97b52f82014-08-14 11:34:07 -0700206 const uint32_t* const bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700207
Ian Rogers97b52f82014-08-14 11:34:07 -0700208 const OatMethodOffsets* const methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700209
210 friend class OatDexFile;
211 };
212
213 class OatDexFile {
214 public:
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700215 // Opens the DexFile referred to by this OatDexFile from within the containing OatFile.
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800216 std::unique_ptr<const DexFile> OpenDexFile(std::string* error_msg) const;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700217
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100218 const OatFile* GetOatFile() const {
219 return oat_file_;
220 }
221
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700222 // Returns the size of the DexFile refered to by this OatDexFile.
Ian Rogers05f28c62012-10-23 18:12:13 -0700223 size_t FileSize() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700224
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700225 // Returns original path of DexFile that was the source of this OatDexFile.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700226 const std::string& GetDexFileLocation() const {
227 return dex_file_location_;
228 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700229
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100230 // Returns the canonical location of DexFile that was the source of this OatDexFile.
231 const std::string& GetCanonicalDexFileLocation() const {
232 return canonical_dex_file_location_;
233 }
234
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700235 // Returns checksum of original DexFile that was the source of this OatDexFile;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800236 uint32_t GetDexFileLocationChecksum() const {
237 return dex_file_location_checksum_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700238 }
239
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700240 // Returns the OatClass for the class specified by the given DexFile class_def_index.
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100241 OatClass GetOatClass(uint16_t class_def_index) const;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700242
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700243 // Returns the offset to the OatClass information. Most callers should use GetOatClass.
244 uint32_t GetOatClassOffset(uint16_t class_def_index) const;
245
Brian Carlstrome24fa612011-09-29 00:53:55 -0700246 ~OatDexFile();
Elliott Hughesa21039c2012-06-21 12:09:25 -0700247
Brian Carlstrome24fa612011-09-29 00:53:55 -0700248 private:
249 OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800250 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100251 const std::string& canonical_dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700252 uint32_t dex_file_checksum,
Ian Rogers13735952014-10-08 12:43:28 -0700253 const uint8_t* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800254 const uint32_t* oat_class_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700255
Vladimir Marko539690a2014-06-05 18:36:42 +0100256 const OatFile* const oat_file_;
257 const std::string dex_file_location_;
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100258 const std::string canonical_dex_file_location_;
Vladimir Marko539690a2014-06-05 18:36:42 +0100259 const uint32_t dex_file_location_checksum_;
Ian Rogers13735952014-10-08 12:43:28 -0700260 const uint8_t* const dex_file_pointer_;
Vladimir Marko539690a2014-06-05 18:36:42 +0100261 const uint32_t* const oat_class_offsets_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700262
263 friend class OatFile;
264 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
265 };
266
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700267 const OatDexFile* GetOatDexFile(const char* dex_location,
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700268 const uint32_t* const dex_location_checksum,
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100269 bool exception_if_not_found = true) const
270 LOCKS_EXCLUDED(secondary_lookup_lock_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700271
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100272 const std::vector<const OatDexFile*>& GetOatDexFiles() const {
273 return oat_dex_files_storage_;
274 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700275
Ian Rogers30fab402012-01-23 15:43:46 -0800276 size_t Size() const {
277 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700278 }
279
Vladimir Marko5c42c292015-02-25 12:02:49 +0000280 size_t BssSize() const {
281 return BssEnd() - BssBegin();
282 }
283
Ian Rogers13735952014-10-08 12:43:28 -0700284 const uint8_t* Begin() const;
285 const uint8_t* End() const;
Alex Light53cb16b2014-06-12 11:26:29 -0700286
Vladimir Marko5c42c292015-02-25 12:02:49 +0000287 const uint8_t* BssBegin() const;
288 const uint8_t* BssEnd() const;
289
Richard Uhlere5fed032015-03-18 08:21:11 -0700290 // Returns the absolute dex location for the encoded relative dex location.
291 //
292 // If not nullptr, abs_dex_location is used to resolve the absolute dex
293 // location of relative dex locations encoded in the oat file.
294 // For example, given absolute location "/data/app/foo/base.apk", encoded
295 // dex locations "base.apk", "base.apk:classes2.dex", etc. would be resolved
296 // to "/data/app/foo/base.apk", "/data/app/foo/base.apk:classes2.dex", etc.
297 // Relative encoded dex locations that don't match the given abs_dex_location
298 // are left unchanged.
299 static std::string ResolveRelativeEncodedDexLocation(
300 const char* abs_dex_location, const std::string& rel_dex_location);
301
Brian Carlstrome24fa612011-09-29 00:53:55 -0700302 private:
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800303 static void CheckLocation(const std::string& location);
304
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800305 static OatFile* OpenElfFile(File* file,
306 const std::string& location,
Ian Rogers13735952014-10-08 12:43:28 -0700307 uint8_t* requested_base,
Igor Murashkin46774762014-10-22 11:37:02 -0700308 uint8_t* oat_file_begin, // Override base if not null
Brian Carlstromf1d34552013-07-12 20:22:23 -0700309 bool writable,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700310 bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700311 const char* abs_dex_location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700312 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800313
Alex Light9dcc4572014-08-14 14:16:26 -0700314 explicit OatFile(const std::string& filename, bool executable);
Igor Murashkin46774762014-10-22 11:37:02 -0700315 bool ElfFileOpen(File* file, uint8_t* requested_base,
316 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
317 bool writable, bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700318 const char* abs_dex_location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700319 std::string* error_msg);
Richard Uhlere5fed032015-03-18 08:21:11 -0700320
321 bool Setup(const char* abs_dex_location, std::string* error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700322
Brian Carlstrome24fa612011-09-29 00:53:55 -0700323 // The oat file name.
324 //
325 // The image will embed this to link its associated oat file.
326 const std::string location_;
327
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800328 // Pointer to OatHeader.
Ian Rogers13735952014-10-08 12:43:28 -0700329 const uint8_t* begin_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800330
331 // Pointer to end of oat region for bounds checking.
Ian Rogers13735952014-10-08 12:43:28 -0700332 const uint8_t* end_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800333
Vladimir Marko5c42c292015-02-25 12:02:49 +0000334 // Pointer to the .bss section, if present, otherwise nullptr.
335 const uint8_t* bss_begin_;
336
337 // Pointer to the end of the .bss section, if present, otherwise nullptr.
338 const uint8_t* bss_end_;
339
Alex Light9dcc4572014-08-14 14:16:26 -0700340 // Was this oat_file loaded executable?
341 const bool is_executable_;
342
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800343 // Backing memory map for oat file during when opened by ElfWriter during initial compilation.
Ian Rogers700a4022014-05-19 16:49:03 -0700344 std::unique_ptr<MemMap> mem_map_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700345
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800346 // Backing memory map for oat file during cross compilation.
Ian Rogers700a4022014-05-19 16:49:03 -0700347 std::unique_ptr<ElfFile> elf_file_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800348
349 // dlopen handle during runtime.
350 void* dlopen_handle_;
351
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100352 // Owning storage for the OatDexFile objects.
353 std::vector<const OatDexFile*> oat_dex_files_storage_;
354
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100355 // NOTE: We use a StringPiece as the key type to avoid a memory allocation on every
356 // lookup with a const char* key. The StringPiece doesn't own its backing storage,
357 // therefore we're using the OatDexFile::dex_file_location_ as the backing storage
358 // for keys in oat_dex_files_ and the string_cache_ entries for the backing storage
359 // of keys in secondary_oat_dex_files_ and oat_dex_files_by_canonical_location_.
Mathieu Chartierbad02672014-08-25 13:08:22 -0700360 typedef AllocationTrackingSafeMap<StringPiece, const OatDexFile*, kAllocatorTagOatFile> Table;
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100361
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100362 // Map each location and canonical location (if different) retrieved from the
363 // oat file to its OatDexFile. This map doesn't change after it's constructed in Setup()
364 // and therefore doesn't need any locking and provides the cheapest dex file lookup
365 // for GetOatDexFile() for a very frequent use case. Never contains a nullptr value.
366 Table oat_dex_files_;
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100367
368 // Lock guarding all members needed for secondary lookup in GetOatDexFile().
369 mutable Mutex secondary_lookup_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
370
371 // If the primary oat_dex_files_ lookup fails, use a secondary map. This map stores
372 // the results of all previous secondary lookups, whether successful (non-null) or
373 // failed (null). If it doesn't contain an entry we need to calculate the canonical
374 // location and use oat_dex_files_by_canonical_location_.
375 mutable Table secondary_oat_dex_files_ GUARDED_BY(secondary_lookup_lock_);
376
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100377 // Cache of strings. Contains the backing storage for keys in the secondary_oat_dex_files_
378 // and the lazily initialized oat_dex_files_by_canonical_location_.
379 // NOTE: We're keeping references to contained strings in form of StringPiece and adding
380 // new strings to the end. The adding of a new element must not touch any previously stored
381 // elements. std::list<> and std::deque<> satisfy this requirement, std::vector<> doesn't.
382 mutable std::list<std::string> string_cache_ GUARDED_BY(secondary_lookup_lock_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700383
384 friend class OatClass;
385 friend class OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800386 friend class OatDumper; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700387 DISALLOW_COPY_AND_ASSIGN(OatFile);
388};
389
390} // namespace art
391
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700392#endif // ART_RUNTIME_OAT_FILE_H_