blob: 8535bf413337e73bbf27b8b7d82644df581e2cc4 [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:
Brian Carlstrome24fa612011-09-29 00:53:55 -070043 // Open an oat file. Returns NULL on failure. Requested base can
44 // optionally be used to request where the file should be loaded.
45 static OatFile* Open(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -080046 const std::string& location,
Brian Carlstromf1d34552013-07-12 20:22:23 -070047 byte* requested_base,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070048 bool executable,
49 std::string* error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -070050
Brian Carlstrom700c8d32012-11-05 10:42:02 -080051 // Open an oat file from an already opened File.
Brian Carlstrom265091e2013-01-30 14:08:26 -080052 // Does not use dlopen underneath so cannot be used for runtime use
53 // where relocations may be required. Currently used from
54 // ImageWriter which wants to open a writable version from an existing
55 // file descriptor for patching.
Ian Rogers8d31bbd2013-10-13 10:44:14 -070056 static OatFile* OpenWritable(File* file, const std::string& location, std::string* error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -070057 // Opens an oat file from an already opened File. Maps it PROT_READ, MAP_PRIVATE.
58 static OatFile* OpenReadable(File* file, const std::string& location, std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080059
60 // Open an oat file backed by a std::vector with the given location.
Brian Carlstrom265091e2013-01-30 14:08:26 -080061 static OatFile* OpenMemory(std::vector<uint8_t>& oat_contents,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070062 const std::string& location,
63 std::string* error_msg);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080064
Brian Carlstrome24fa612011-09-29 00:53:55 -070065 ~OatFile();
66
Alex Light9dcc4572014-08-14 14:16:26 -070067 bool IsExecutable() const {
68 return is_executable_;
69 }
70
Alex Light53cb16b2014-06-12 11:26:29 -070071 ElfFile* GetElfFile() const {
72 CHECK_NE(reinterpret_cast<uintptr_t>(elf_file_.get()), reinterpret_cast<uintptr_t>(nullptr))
73 << "Cannot get an elf file from " << GetLocation();
74 return elf_file_.get();
75 }
76
Brian Carlstrome24fa612011-09-29 00:53:55 -070077 const std::string& GetLocation() const {
78 return location_;
79 }
80
81 const OatHeader& GetOatHeader() const;
82
83 class OatDexFile;
84
Brian Carlstrom3320cf42011-10-04 14:58:28 -070085 class OatMethod {
86 public:
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070087 void LinkMethod(mirror::ArtMethod* method) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromae826982011-11-09 01:33:42 -080088
89 uint32_t GetCodeOffset() const {
90 return code_offset_;
91 }
Ian Rogers0c7abda2012-09-19 13:33:42 -070092 uint32_t GetNativeGcMapOffset() const {
93 return native_gc_map_offset_;
Brian Carlstrome7d856b2012-01-11 18:10:55 -080094 }
Brian Carlstromae826982011-11-09 01:33:42 -080095
Ian Rogersef7d42f2014-01-06 12:55:46 -080096 const void* GetPortableCode() const {
97 // TODO: encode whether code is portable/quick in flags within OatMethod.
98 if (kUsePortableCompiler) {
99 return GetOatPointer<const void*>(code_offset_);
100 } else {
101 return nullptr;
102 }
103 }
104
105 const void* GetQuickCode() const {
106 if (kUsePortableCompiler) {
107 return nullptr;
108 } else {
109 return GetOatPointer<const void*>(code_offset_);
110 }
111 }
112
113 uint32_t GetPortableCodeSize() const {
114 // TODO: With Quick, we store the size before the code. With Portable, the code is in a .o
115 // file we don't manage ourselves. ELF symbols do have a concept of size, so we could capture
116 // that and store it somewhere, such as the OatMethod.
117 return 0;
118 }
119 uint32_t GetQuickCodeSize() const;
Logan Chien0c717dd2012-03-28 18:31:07 +0800120
Ian Rogers0c7abda2012-09-19 13:33:42 -0700121 const uint8_t* GetNativeGcMap() const {
122 return GetOatPointer<const uint8_t*>(native_gc_map_offset_);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800123 }
Logan Chien0c717dd2012-03-28 18:31:07 +0800124
Vladimir Marko7624d252014-05-02 14:40:15 +0100125 size_t GetFrameSizeInBytes() const;
126 uint32_t GetCoreSpillMask() const;
127 uint32_t GetFpSpillMask() const;
Vladimir Marko8a630572014-04-09 18:45:35 +0100128 uint32_t GetMappingTableOffset() const;
129 uint32_t GetVmapTableOffset() const;
130 const uint8_t* GetMappingTable() const;
131 const uint8_t* GetVmapTable() const;
132
Brian Carlstromae826982011-11-09 01:33:42 -0800133 // Create an OatMethod with offsets relative to the given base address
Ian Rogers97b52f82014-08-14 11:34:07 -0700134 OatMethod(const byte* base, const uint32_t code_offset, const uint32_t gc_map_offset)
135 : begin_(base),
136 code_offset_(code_offset),
137 native_gc_map_offset_(gc_map_offset) {
138 }
139 ~OatMethod() {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700140
Ian Rogers97b52f82014-08-14 11:34:07 -0700141 // A representation of an invalid OatMethod, used when an OatMethod or OatClass can't be found.
142 // See ClassLinker::FindOatMethodFor.
143 static const OatMethod Invalid() {
144 return OatMethod(nullptr, -1, -1);
145 }
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100146
Brian Carlstromae826982011-11-09 01:33:42 -0800147 private:
148 template<class T>
149 T GetOatPointer(uint32_t offset) const {
150 if (offset == 0) {
151 return NULL;
152 }
Ian Rogers30fab402012-01-23 15:43:46 -0800153 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800154 }
155
Ian Rogers97b52f82014-08-14 11:34:07 -0700156 const byte* const begin_;
Brian Carlstromae826982011-11-09 01:33:42 -0800157
Ian Rogers97b52f82014-08-14 11:34:07 -0700158 const uint32_t code_offset_;
159 const uint32_t native_gc_map_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800160
161 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700162 };
163
Brian Carlstrome24fa612011-09-29 00:53:55 -0700164 class OatClass {
165 public:
Brian Carlstromba150c32013-08-27 17:31:03 -0700166 mirror::Class::Status GetStatus() const {
167 return status_;
168 }
169
170 OatClassType GetType() const {
171 return type_;
172 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800173
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700174 // Get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700175 // defintion. direct methods come first, followed by virtual
176 // methods. note that runtime created methods such as miranda
177 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700178 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700179
Ian Rogers97b52f82014-08-14 11:34:07 -0700180 // A representation of an invalid OatClass, used when an OatClass can't be found.
181 // See ClassLinker::FindOatClass.
182 static OatClass Invalid() {
183 return OatClass(nullptr, mirror::Class::kStatusError, kOatClassNoneCompiled, 0, nullptr,
184 nullptr);
185 }
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100186
Brian Carlstrome24fa612011-09-29 00:53:55 -0700187 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800188 OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800189 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700190 OatClassType type,
191 uint32_t bitmap_size,
192 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800193 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700194
Ian Rogers97b52f82014-08-14 11:34:07 -0700195 const OatFile* const oat_file_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700196
Ian Rogers97b52f82014-08-14 11:34:07 -0700197 const mirror::Class::Status status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700198
Ian Rogers97b52f82014-08-14 11:34:07 -0700199 const OatClassType type_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700200
Ian Rogers97b52f82014-08-14 11:34:07 -0700201 const uint32_t* const bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700202
Ian Rogers97b52f82014-08-14 11:34:07 -0700203 const OatMethodOffsets* const methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700204
205 friend class OatDexFile;
206 };
207
208 class OatDexFile {
209 public:
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700210 // Opens the DexFile referred to by this OatDexFile from within the containing OatFile.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700211 const DexFile* OpenDexFile(std::string* error_msg) const;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700212
213 // Returns the size of the DexFile refered to by this OatDexFile.
Ian Rogers05f28c62012-10-23 18:12:13 -0700214 size_t FileSize() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700215
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700216 // Returns original path of DexFile that was the source of this OatDexFile.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700217 const std::string& GetDexFileLocation() const {
218 return dex_file_location_;
219 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700220
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700221 // Returns checksum of original DexFile that was the source of this OatDexFile;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800222 uint32_t GetDexFileLocationChecksum() const {
223 return dex_file_location_checksum_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700224 }
225
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700226 // Returns the OatClass for the class specified by the given DexFile class_def_index.
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100227 OatClass GetOatClass(uint16_t class_def_index) const;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700228
Brian Carlstrome24fa612011-09-29 00:53:55 -0700229 ~OatDexFile();
Elliott Hughesa21039c2012-06-21 12:09:25 -0700230
Brian Carlstrome24fa612011-09-29 00:53:55 -0700231 private:
232 OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800233 const std::string& dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700234 uint32_t dex_file_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800235 const byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800236 const uint32_t* oat_class_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700237
Vladimir Marko539690a2014-06-05 18:36:42 +0100238 const OatFile* const oat_file_;
239 const std::string dex_file_location_;
240 const uint32_t dex_file_location_checksum_;
241 const byte* const dex_file_pointer_;
242 const uint32_t* const oat_class_offsets_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700243
244 friend class OatFile;
245 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
246 };
247
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700248 const OatDexFile* GetOatDexFile(const char* dex_location,
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700249 const uint32_t* const dex_location_checksum,
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100250 bool exception_if_not_found = true) const
251 LOCKS_EXCLUDED(secondary_lookup_lock_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700252
Brian Carlstromaded5f72011-10-07 17:15:04 -0700253 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700254
Ian Rogers30fab402012-01-23 15:43:46 -0800255 size_t Size() const {
256 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700257 }
258
Alex Light53cb16b2014-06-12 11:26:29 -0700259 const byte* Begin() const;
260 const byte* End() const;
261
Brian Carlstrome24fa612011-09-29 00:53:55 -0700262 private:
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800263 static void CheckLocation(const std::string& location);
264
265 static OatFile* OpenDlopen(const std::string& elf_filename,
266 const std::string& location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700267 byte* requested_base,
268 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800269
270 static OatFile* OpenElfFile(File* file,
271 const std::string& location,
272 byte* requested_base,
Brian Carlstromf1d34552013-07-12 20:22:23 -0700273 bool writable,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700274 bool executable,
275 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800276
Alex Light9dcc4572014-08-14 14:16:26 -0700277 explicit OatFile(const std::string& filename, bool executable);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700278 bool Dlopen(const std::string& elf_filename, byte* requested_base, std::string* error_msg);
279 bool ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable,
280 std::string* error_msg);
281 bool Setup(std::string* error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700282
Brian Carlstrome24fa612011-09-29 00:53:55 -0700283 // The oat file name.
284 //
285 // The image will embed this to link its associated oat file.
286 const std::string location_;
287
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800288 // Pointer to OatHeader.
289 const byte* begin_;
290
291 // Pointer to end of oat region for bounds checking.
292 const byte* end_;
293
Alex Light9dcc4572014-08-14 14:16:26 -0700294 // Was this oat_file loaded executable?
295 const bool is_executable_;
296
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800297 // Backing memory map for oat file during when opened by ElfWriter during initial compilation.
Ian Rogers700a4022014-05-19 16:49:03 -0700298 std::unique_ptr<MemMap> mem_map_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700299
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800300 // Backing memory map for oat file during cross compilation.
Ian Rogers700a4022014-05-19 16:49:03 -0700301 std::unique_ptr<ElfFile> elf_file_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800302
303 // dlopen handle during runtime.
304 void* dlopen_handle_;
305
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100306 // NOTE: We use a StringPiece as the key type to avoid a memory allocation on every
307 // lookup with a const char* key. The StringPiece doesn't own its backing storage,
308 // therefore we're using the OatDexFile::dex_file_location_ as the backing storage
309 // for keys in oat_dex_files_ and the string_cache_ entries for the backing storage
310 // of keys in secondary_oat_dex_files_ and oat_dex_files_by_canonical_location_.
Vladimir Marko539690a2014-06-05 18:36:42 +0100311 typedef SafeMap<StringPiece, const OatDexFile*> Table;
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100312
313 // Map each plain dex file location retrieved from the oat file to its OatDexFile.
314 // This map doesn't change after it's constructed in Setup() and therefore doesn't
315 // need any locking and provides the cheapest dex file lookup for GetOatDexFile()
316 // for a very frequent use case. Never contains a nullptr value.
317 Table oat_dex_files_; // Owns the OatDexFile* values.
318
319 // Lock guarding all members needed for secondary lookup in GetOatDexFile().
320 mutable Mutex secondary_lookup_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
321
322 // If the primary oat_dex_files_ lookup fails, use a secondary map. This map stores
323 // the results of all previous secondary lookups, whether successful (non-null) or
324 // failed (null). If it doesn't contain an entry we need to calculate the canonical
325 // location and use oat_dex_files_by_canonical_location_.
326 mutable Table secondary_oat_dex_files_ GUARDED_BY(secondary_lookup_lock_);
327
328 // Map the canonical location to an OatDexFile. This lazily constructed map is used
329 // when we're doing the secondary lookup for a given location for the first time.
330 mutable Table oat_dex_files_by_canonical_location_ GUARDED_BY(secondary_lookup_lock_);
331
332 // Cache of strings. Contains the backing storage for keys in the secondary_oat_dex_files_
333 // and the lazily initialized oat_dex_files_by_canonical_location_.
334 // NOTE: We're keeping references to contained strings in form of StringPiece and adding
335 // new strings to the end. The adding of a new element must not touch any previously stored
336 // elements. std::list<> and std::deque<> satisfy this requirement, std::vector<> doesn't.
337 mutable std::list<std::string> string_cache_ GUARDED_BY(secondary_lookup_lock_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700338
339 friend class OatClass;
340 friend class OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800341 friend class OatDumper; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700342 DISALLOW_COPY_AND_ASSIGN(OatFile);
343};
344
345} // namespace art
346
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700347#endif // ART_RUNTIME_OAT_FILE_H_