blob: 2fd4f4c98ad35f53c4c88fe7730f7719158c2e1e [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,
46 std::string* error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -070047 // Open an oat file. Returns NULL on failure. Requested base can
48 // optionally be used to request where the file should be loaded.
49 static OatFile* Open(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -080050 const std::string& location,
Brian Carlstromf1d34552013-07-12 20:22:23 -070051 byte* requested_base,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070052 bool executable,
53 std::string* error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -070054
Brian Carlstrom700c8d32012-11-05 10:42:02 -080055 // Open an oat file from an already opened File.
Brian Carlstrom265091e2013-01-30 14:08:26 -080056 // Does not use dlopen underneath so cannot be used for runtime use
57 // where relocations may be required. Currently used from
58 // ImageWriter which wants to open a writable version from an existing
59 // file descriptor for patching.
Ian Rogers8d31bbd2013-10-13 10:44:14 -070060 static OatFile* OpenWritable(File* file, const std::string& location, std::string* error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -070061 // Opens an oat file from an already opened File. Maps it PROT_READ, MAP_PRIVATE.
62 static OatFile* OpenReadable(File* file, const std::string& location, std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080063
64 // Open an oat file backed by a std::vector with the given location.
Brian Carlstrom265091e2013-01-30 14:08:26 -080065 static OatFile* OpenMemory(std::vector<uint8_t>& oat_contents,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070066 const std::string& location,
67 std::string* error_msg);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080068
Brian Carlstrome24fa612011-09-29 00:53:55 -070069 ~OatFile();
70
Alex Light9dcc4572014-08-14 14:16:26 -070071 bool IsExecutable() const {
72 return is_executable_;
73 }
74
Alex Light53cb16b2014-06-12 11:26:29 -070075 ElfFile* GetElfFile() const {
76 CHECK_NE(reinterpret_cast<uintptr_t>(elf_file_.get()), reinterpret_cast<uintptr_t>(nullptr))
77 << "Cannot get an elf file from " << GetLocation();
78 return elf_file_.get();
79 }
80
Brian Carlstrome24fa612011-09-29 00:53:55 -070081 const std::string& GetLocation() const {
82 return location_;
83 }
84
85 const OatHeader& GetOatHeader() const;
86
87 class OatDexFile;
88
Brian Carlstrom3320cf42011-10-04 14:58:28 -070089 class OatMethod {
90 public:
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070091 void LinkMethod(mirror::ArtMethod* method) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromae826982011-11-09 01:33:42 -080092
93 uint32_t GetCodeOffset() const {
94 return code_offset_;
95 }
Ian Rogers0c7abda2012-09-19 13:33:42 -070096 uint32_t GetNativeGcMapOffset() const {
97 return native_gc_map_offset_;
Brian Carlstrome7d856b2012-01-11 18:10:55 -080098 }
Brian Carlstromae826982011-11-09 01:33:42 -080099
Ian Rogersef7d42f2014-01-06 12:55:46 -0800100 const void* GetPortableCode() const {
101 // TODO: encode whether code is portable/quick in flags within OatMethod.
102 if (kUsePortableCompiler) {
103 return GetOatPointer<const void*>(code_offset_);
104 } else {
105 return nullptr;
106 }
107 }
108
109 const void* GetQuickCode() const {
110 if (kUsePortableCompiler) {
111 return nullptr;
112 } else {
113 return GetOatPointer<const void*>(code_offset_);
114 }
115 }
116
117 uint32_t GetPortableCodeSize() const {
118 // TODO: With Quick, we store the size before the code. With Portable, the code is in a .o
119 // file we don't manage ourselves. ELF symbols do have a concept of size, so we could capture
120 // that and store it somewhere, such as the OatMethod.
121 return 0;
122 }
123 uint32_t GetQuickCodeSize() const;
Logan Chien0c717dd2012-03-28 18:31:07 +0800124
Ian Rogers0c7abda2012-09-19 13:33:42 -0700125 const uint8_t* GetNativeGcMap() const {
126 return GetOatPointer<const uint8_t*>(native_gc_map_offset_);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800127 }
Logan Chien0c717dd2012-03-28 18:31:07 +0800128
Vladimir Marko7624d252014-05-02 14:40:15 +0100129 size_t GetFrameSizeInBytes() const;
130 uint32_t GetCoreSpillMask() const;
131 uint32_t GetFpSpillMask() const;
Vladimir Marko8a630572014-04-09 18:45:35 +0100132 uint32_t GetMappingTableOffset() const;
133 uint32_t GetVmapTableOffset() const;
134 const uint8_t* GetMappingTable() const;
135 const uint8_t* GetVmapTable() const;
136
Brian Carlstromae826982011-11-09 01:33:42 -0800137 // Create an OatMethod with offsets relative to the given base address
Ian Rogers97b52f82014-08-14 11:34:07 -0700138 OatMethod(const byte* base, const uint32_t code_offset, const uint32_t gc_map_offset)
139 : begin_(base),
140 code_offset_(code_offset),
141 native_gc_map_offset_(gc_map_offset) {
142 }
143 ~OatMethod() {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700144
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() {
148 return OatMethod(nullptr, -1, -1);
149 }
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) {
155 return NULL;
156 }
Ian Rogers30fab402012-01-23 15:43:46 -0800157 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800158 }
159
Ian Rogers97b52f82014-08-14 11:34:07 -0700160 const byte* const begin_;
Brian Carlstromae826982011-11-09 01:33:42 -0800161
Ian Rogers97b52f82014-08-14 11:34:07 -0700162 const uint32_t code_offset_;
163 const uint32_t native_gc_map_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800164
165 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700166 };
167
Brian Carlstrome24fa612011-09-29 00:53:55 -0700168 class OatClass {
169 public:
Brian Carlstromba150c32013-08-27 17:31:03 -0700170 mirror::Class::Status GetStatus() const {
171 return status_;
172 }
173
174 OatClassType GetType() const {
175 return type_;
176 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800177
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700178 // Get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700179 // defintion. direct methods come first, followed by virtual
180 // methods. note that runtime created methods such as miranda
181 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700182 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700183
Ian Rogers97b52f82014-08-14 11:34:07 -0700184 // A representation of an invalid OatClass, used when an OatClass can't be found.
185 // See ClassLinker::FindOatClass.
186 static OatClass Invalid() {
187 return OatClass(nullptr, mirror::Class::kStatusError, kOatClassNoneCompiled, 0, nullptr,
188 nullptr);
189 }
Nicolas Geoffray4fcdc942014-07-22 10:48:00 +0100190
Brian Carlstrome24fa612011-09-29 00:53:55 -0700191 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800192 OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800193 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700194 OatClassType type,
195 uint32_t bitmap_size,
196 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800197 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700198
Ian Rogers97b52f82014-08-14 11:34:07 -0700199 const OatFile* const oat_file_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700200
Ian Rogers97b52f82014-08-14 11:34:07 -0700201 const mirror::Class::Status status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700202
Ian Rogers97b52f82014-08-14 11:34:07 -0700203 const OatClassType type_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700204
Ian Rogers97b52f82014-08-14 11:34:07 -0700205 const uint32_t* const bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700206
Ian Rogers97b52f82014-08-14 11:34:07 -0700207 const OatMethodOffsets* const methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700208
209 friend class OatDexFile;
210 };
211
212 class OatDexFile {
213 public:
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700214 // Opens the DexFile referred to by this OatDexFile from within the containing OatFile.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700215 const DexFile* OpenDexFile(std::string* error_msg) const;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700216
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100217 const OatFile* GetOatFile() const {
218 return oat_file_;
219 }
220
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700221 // Returns the size of the DexFile refered to by this OatDexFile.
Ian Rogers05f28c62012-10-23 18:12:13 -0700222 size_t FileSize() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700223
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700224 // Returns original path of DexFile that was the source of this OatDexFile.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700225 const std::string& GetDexFileLocation() const {
226 return dex_file_location_;
227 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700228
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100229 // Returns the canonical location of DexFile that was the source of this OatDexFile.
230 const std::string& GetCanonicalDexFileLocation() const {
231 return canonical_dex_file_location_;
232 }
233
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700234 // Returns checksum of original DexFile that was the source of this OatDexFile;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800235 uint32_t GetDexFileLocationChecksum() const {
236 return dex_file_location_checksum_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700237 }
238
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700239 // Returns the OatClass for the class specified by the given DexFile class_def_index.
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100240 OatClass GetOatClass(uint16_t class_def_index) const;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700241
Brian Carlstrome24fa612011-09-29 00:53:55 -0700242 ~OatDexFile();
Elliott Hughesa21039c2012-06-21 12:09:25 -0700243
Brian Carlstrome24fa612011-09-29 00:53:55 -0700244 private:
245 OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800246 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100247 const std::string& canonical_dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700248 uint32_t dex_file_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800249 const byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800250 const uint32_t* oat_class_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700251
Vladimir Marko539690a2014-06-05 18:36:42 +0100252 const OatFile* const oat_file_;
253 const std::string dex_file_location_;
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100254 const std::string canonical_dex_file_location_;
Vladimir Marko539690a2014-06-05 18:36:42 +0100255 const uint32_t dex_file_location_checksum_;
256 const byte* const dex_file_pointer_;
257 const uint32_t* const oat_class_offsets_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700258
259 friend class OatFile;
260 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
261 };
262
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700263 const OatDexFile* GetOatDexFile(const char* dex_location,
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700264 const uint32_t* const dex_location_checksum,
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100265 bool exception_if_not_found = true) const
266 LOCKS_EXCLUDED(secondary_lookup_lock_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700267
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100268 const std::vector<const OatDexFile*>& GetOatDexFiles() const {
269 return oat_dex_files_storage_;
270 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700271
Ian Rogers30fab402012-01-23 15:43:46 -0800272 size_t Size() const {
273 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700274 }
275
Alex Light53cb16b2014-06-12 11:26:29 -0700276 const byte* Begin() const;
277 const byte* End() const;
278
Brian Carlstrome24fa612011-09-29 00:53:55 -0700279 private:
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800280 static void CheckLocation(const std::string& location);
281
282 static OatFile* OpenDlopen(const std::string& elf_filename,
283 const std::string& location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700284 byte* requested_base,
285 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800286
287 static OatFile* OpenElfFile(File* file,
288 const std::string& location,
289 byte* requested_base,
Brian Carlstromf1d34552013-07-12 20:22:23 -0700290 bool writable,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700291 bool executable,
292 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800293
Alex Light9dcc4572014-08-14 14:16:26 -0700294 explicit OatFile(const std::string& filename, bool executable);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700295 bool Dlopen(const std::string& elf_filename, byte* requested_base, std::string* error_msg);
296 bool ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable,
297 std::string* error_msg);
298 bool Setup(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.
306 const byte* begin_;
307
308 // Pointer to end of oat region for bounds checking.
309 const byte* end_;
310
Alex Light9dcc4572014-08-14 14:16:26 -0700311 // Was this oat_file loaded executable?
312 const bool is_executable_;
313
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800314 // Backing memory map for oat file during when opened by ElfWriter during initial compilation.
Ian Rogers700a4022014-05-19 16:49:03 -0700315 std::unique_ptr<MemMap> mem_map_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700316
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800317 // Backing memory map for oat file during cross compilation.
Ian Rogers700a4022014-05-19 16:49:03 -0700318 std::unique_ptr<ElfFile> elf_file_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800319
320 // dlopen handle during runtime.
321 void* dlopen_handle_;
322
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100323 // Owning storage for the OatDexFile objects.
324 std::vector<const OatDexFile*> oat_dex_files_storage_;
325
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100326 // NOTE: We use a StringPiece as the key type to avoid a memory allocation on every
327 // lookup with a const char* key. The StringPiece doesn't own its backing storage,
328 // therefore we're using the OatDexFile::dex_file_location_ as the backing storage
329 // for keys in oat_dex_files_ and the string_cache_ entries for the backing storage
330 // of keys in secondary_oat_dex_files_ and oat_dex_files_by_canonical_location_.
Mathieu Chartierbad02672014-08-25 13:08:22 -0700331 typedef AllocationTrackingSafeMap<StringPiece, const OatDexFile*, kAllocatorTagOatFile> Table;
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100332
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100333 // Map each location and canonical location (if different) retrieved from the
334 // oat file to its OatDexFile. This map doesn't change after it's constructed in Setup()
335 // and therefore doesn't need any locking and provides the cheapest dex file lookup
336 // for GetOatDexFile() for a very frequent use case. Never contains a nullptr value.
337 Table oat_dex_files_;
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100338
339 // Lock guarding all members needed for secondary lookup in GetOatDexFile().
340 mutable Mutex secondary_lookup_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
341
342 // If the primary oat_dex_files_ lookup fails, use a secondary map. This map stores
343 // the results of all previous secondary lookups, whether successful (non-null) or
344 // failed (null). If it doesn't contain an entry we need to calculate the canonical
345 // location and use oat_dex_files_by_canonical_location_.
346 mutable Table secondary_oat_dex_files_ GUARDED_BY(secondary_lookup_lock_);
347
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100348 // Cache of strings. Contains the backing storage for keys in the secondary_oat_dex_files_
349 // and the lazily initialized oat_dex_files_by_canonical_location_.
350 // NOTE: We're keeping references to contained strings in form of StringPiece and adding
351 // new strings to the end. The adding of a new element must not touch any previously stored
352 // elements. std::list<> and std::deque<> satisfy this requirement, std::vector<> doesn't.
353 mutable std::list<std::string> string_cache_ GUARDED_BY(secondary_lookup_lock_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700354
355 friend class OatClass;
356 friend class OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800357 friend class OatDumper; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700358 DISALLOW_COPY_AND_ASSIGN(OatFile);
359};
360
361} // namespace art
362
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700363#endif // ART_RUNTIME_OAT_FILE_H_