blob: 3ec2e8434592f04db9c4e7af362e8602bf7fd22f [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
Brian Carlstrom700c8d32012-11-05 10:42:02 -080020#include <string>
Brian Carlstrome24fa612011-09-29 00:53:55 -070021#include <vector>
22
Vladimir Marko539690a2014-06-05 18:36:42 +010023#include "base/stringpiece.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "dex_file.h"
25#include "invoke_type.h"
26#include "mem_map.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070027#include "mirror/class.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "oat.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080029#include "os.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070030
31namespace art {
32
Brian Carlstromba150c32013-08-27 17:31:03 -070033class BitVector;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080034class ElfFile;
35class MemMap;
36class OatMethodOffsets;
Ian Rogers33e95662013-05-20 20:29:14 -070037class OatHeader;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080038
Brian Carlstrome24fa612011-09-29 00:53:55 -070039class OatFile {
40 public:
Brian Carlstrome24fa612011-09-29 00:53:55 -070041 // Open an oat file. Returns NULL on failure. Requested base can
42 // optionally be used to request where the file should be loaded.
43 static OatFile* Open(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -080044 const std::string& location,
Brian Carlstromf1d34552013-07-12 20:22:23 -070045 byte* requested_base,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070046 bool executable,
47 std::string* error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -070048
Brian Carlstrom700c8d32012-11-05 10:42:02 -080049 // Open an oat file from an already opened File.
Brian Carlstrom265091e2013-01-30 14:08:26 -080050 // Does not use dlopen underneath so cannot be used for runtime use
51 // where relocations may be required. Currently used from
52 // ImageWriter which wants to open a writable version from an existing
53 // file descriptor for patching.
Ian Rogers8d31bbd2013-10-13 10:44:14 -070054 static OatFile* OpenWritable(File* file, const std::string& location, std::string* error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -070055 // Opens an oat file from an already opened File. Maps it PROT_READ, MAP_PRIVATE.
56 static OatFile* OpenReadable(File* file, const std::string& location, std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080057
58 // Open an oat file backed by a std::vector with the given location.
Brian Carlstrom265091e2013-01-30 14:08:26 -080059 static OatFile* OpenMemory(std::vector<uint8_t>& oat_contents,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070060 const std::string& location,
61 std::string* error_msg);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080062
Brian Carlstrome24fa612011-09-29 00:53:55 -070063 ~OatFile();
64
Alex Light53cb16b2014-06-12 11:26:29 -070065 ElfFile* GetElfFile() const {
66 CHECK_NE(reinterpret_cast<uintptr_t>(elf_file_.get()), reinterpret_cast<uintptr_t>(nullptr))
67 << "Cannot get an elf file from " << GetLocation();
68 return elf_file_.get();
69 }
70
Brian Carlstrome24fa612011-09-29 00:53:55 -070071 const std::string& GetLocation() const {
72 return location_;
73 }
74
75 const OatHeader& GetOatHeader() const;
76
77 class OatDexFile;
78
Brian Carlstrom3320cf42011-10-04 14:58:28 -070079 class OatMethod {
80 public:
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070081 void LinkMethod(mirror::ArtMethod* method) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromae826982011-11-09 01:33:42 -080082
83 uint32_t GetCodeOffset() const {
84 return code_offset_;
85 }
Ian Rogers0c7abda2012-09-19 13:33:42 -070086 uint32_t GetNativeGcMapOffset() const {
87 return native_gc_map_offset_;
Brian Carlstrome7d856b2012-01-11 18:10:55 -080088 }
Brian Carlstromae826982011-11-09 01:33:42 -080089
Ian Rogersef7d42f2014-01-06 12:55:46 -080090 const void* GetPortableCode() const {
91 // TODO: encode whether code is portable/quick in flags within OatMethod.
92 if (kUsePortableCompiler) {
93 return GetOatPointer<const void*>(code_offset_);
94 } else {
95 return nullptr;
96 }
97 }
98
99 const void* GetQuickCode() const {
100 if (kUsePortableCompiler) {
101 return nullptr;
102 } else {
103 return GetOatPointer<const void*>(code_offset_);
104 }
105 }
106
107 uint32_t GetPortableCodeSize() const {
108 // TODO: With Quick, we store the size before the code. With Portable, the code is in a .o
109 // file we don't manage ourselves. ELF symbols do have a concept of size, so we could capture
110 // that and store it somewhere, such as the OatMethod.
111 return 0;
112 }
113 uint32_t GetQuickCodeSize() const;
Logan Chien0c717dd2012-03-28 18:31:07 +0800114
Ian Rogers0c7abda2012-09-19 13:33:42 -0700115 const uint8_t* GetNativeGcMap() const {
116 return GetOatPointer<const uint8_t*>(native_gc_map_offset_);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800117 }
Logan Chien0c717dd2012-03-28 18:31:07 +0800118
Vladimir Marko7624d252014-05-02 14:40:15 +0100119 size_t GetFrameSizeInBytes() const;
120 uint32_t GetCoreSpillMask() const;
121 uint32_t GetFpSpillMask() const;
Vladimir Marko8a630572014-04-09 18:45:35 +0100122 uint32_t GetMappingTableOffset() const;
123 uint32_t GetVmapTableOffset() const;
124 const uint8_t* GetMappingTable() const;
125 const uint8_t* GetVmapTable() const;
126
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700127 ~OatMethod();
128
Brian Carlstromae826982011-11-09 01:33:42 -0800129 // Create an OatMethod with offsets relative to the given base address
130 OatMethod(const byte* base,
131 const uint32_t code_offset,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700132 const uint32_t gc_map_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700133
Brian Carlstromae826982011-11-09 01:33:42 -0800134 private:
135 template<class T>
136 T GetOatPointer(uint32_t offset) const {
137 if (offset == 0) {
138 return NULL;
139 }
Ian Rogers30fab402012-01-23 15:43:46 -0800140 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800141 }
142
Ian Rogers30fab402012-01-23 15:43:46 -0800143 const byte* begin_;
Brian Carlstromae826982011-11-09 01:33:42 -0800144
145 uint32_t code_offset_;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700146 uint32_t native_gc_map_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800147
148 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700149 };
150
Brian Carlstrome24fa612011-09-29 00:53:55 -0700151 class OatClass {
152 public:
Brian Carlstromba150c32013-08-27 17:31:03 -0700153 mirror::Class::Status GetStatus() const {
154 return status_;
155 }
156
157 OatClassType GetType() const {
158 return type_;
159 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800160
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700161 // Get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700162 // defintion. direct methods come first, followed by virtual
163 // methods. note that runtime created methods such as miranda
164 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700165 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700166
167 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800168 OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700170 OatClassType type,
171 uint32_t bitmap_size,
172 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800173 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700174
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100175 const OatFile* const oat_file_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700176
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800177 const mirror::Class::Status status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700178
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100179 const OatClassType type_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700180
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100181 const uint32_t* const bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700182
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700183 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700184
185 friend class OatDexFile;
186 };
187
188 class OatDexFile {
189 public:
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700190 // Opens the DexFile referred to by this OatDexFile from within the containing OatFile.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700191 const DexFile* OpenDexFile(std::string* error_msg) const;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700192
193 // Returns the size of the DexFile refered to by this OatDexFile.
Ian Rogers05f28c62012-10-23 18:12:13 -0700194 size_t FileSize() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700195
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700196 // Returns original path of DexFile that was the source of this OatDexFile.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700197 const std::string& GetDexFileLocation() const {
198 return dex_file_location_;
199 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700200
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700201 // Returns checksum of original DexFile that was the source of this OatDexFile;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800202 uint32_t GetDexFileLocationChecksum() const {
203 return dex_file_location_checksum_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700204 }
205
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700206 // Returns the OatClass for the class specified by the given DexFile class_def_index.
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100207 OatClass GetOatClass(uint16_t class_def_index) const;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700208
Brian Carlstrome24fa612011-09-29 00:53:55 -0700209 ~OatDexFile();
Elliott Hughesa21039c2012-06-21 12:09:25 -0700210
Brian Carlstrome24fa612011-09-29 00:53:55 -0700211 private:
212 OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800213 const std::string& dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700214 uint32_t dex_file_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800215 const byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800216 const uint32_t* oat_class_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700217
Vladimir Marko539690a2014-06-05 18:36:42 +0100218 const OatFile* const oat_file_;
219 const std::string dex_file_location_;
220 const uint32_t dex_file_location_checksum_;
221 const byte* const dex_file_pointer_;
222 const uint32_t* const oat_class_offsets_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700223
224 friend class OatFile;
225 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
226 };
227
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700228 const OatDexFile* GetOatDexFile(const char* dex_location,
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700229 const uint32_t* const dex_location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700230 bool exception_if_not_found = true) const;
231
Brian Carlstromaded5f72011-10-07 17:15:04 -0700232 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700233
Ian Rogers30fab402012-01-23 15:43:46 -0800234 size_t Size() const {
235 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700236 }
237
Alex Light53cb16b2014-06-12 11:26:29 -0700238 const byte* Begin() const;
239 const byte* End() const;
240
Brian Carlstrome24fa612011-09-29 00:53:55 -0700241 private:
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800242 static void CheckLocation(const std::string& location);
243
244 static OatFile* OpenDlopen(const std::string& elf_filename,
245 const std::string& location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700246 byte* requested_base,
247 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800248
249 static OatFile* OpenElfFile(File* file,
250 const std::string& location,
251 byte* requested_base,
Brian Carlstromf1d34552013-07-12 20:22:23 -0700252 bool writable,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700253 bool executable,
254 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800255
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700256 explicit OatFile(const std::string& filename);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700257 bool Dlopen(const std::string& elf_filename, byte* requested_base, std::string* error_msg);
258 bool ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable,
259 std::string* error_msg);
260 bool Setup(std::string* error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700261
Brian Carlstrome24fa612011-09-29 00:53:55 -0700262 // The oat file name.
263 //
264 // The image will embed this to link its associated oat file.
265 const std::string location_;
266
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800267 // Pointer to OatHeader.
268 const byte* begin_;
269
270 // Pointer to end of oat region for bounds checking.
271 const byte* end_;
272
273 // Backing memory map for oat file during when opened by ElfWriter during initial compilation.
Ian Rogers700a4022014-05-19 16:49:03 -0700274 std::unique_ptr<MemMap> mem_map_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700275
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800276 // Backing memory map for oat file during cross compilation.
Ian Rogers700a4022014-05-19 16:49:03 -0700277 std::unique_ptr<ElfFile> elf_file_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800278
279 // dlopen handle during runtime.
280 void* dlopen_handle_;
281
Vladimir Marko539690a2014-06-05 18:36:42 +0100282 // NOTE: We use a StringPiece as the key type to avoid a memory allocation on every lookup
283 // with a const char* key.
284 typedef SafeMap<StringPiece, const OatDexFile*> Table;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700285 Table oat_dex_files_;
286
287 friend class OatClass;
288 friend class OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800289 friend class OatDumper; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700290 DISALLOW_COPY_AND_ASSIGN(OatFile);
291};
292
293} // namespace art
294
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700295#endif // ART_RUNTIME_OAT_FILE_H_