blob: d70373172edbdf34ca740f138611756b9b30bbaa [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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "dex_file.h"
24#include "invoke_type.h"
25#include "mem_map.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070026#include "mirror/art_method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "oat.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080028#include "os.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070029
30namespace art {
31
Brian Carlstromba150c32013-08-27 17:31:03 -070032class BitVector;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080033class ElfFile;
34class MemMap;
35class OatMethodOffsets;
Ian Rogers33e95662013-05-20 20:29:14 -070036class OatHeader;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080037
Brian Carlstrome24fa612011-09-29 00:53:55 -070038class OatFile {
39 public:
Brian Carlstrome24fa612011-09-29 00:53:55 -070040 // Open an oat file. Returns NULL on failure. Requested base can
41 // optionally be used to request where the file should be loaded.
42 static OatFile* Open(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -080043 const std::string& location,
Brian Carlstromf1d34552013-07-12 20:22:23 -070044 byte* requested_base,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070045 bool executable,
46 std::string* error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -070047
Brian Carlstrom700c8d32012-11-05 10:42:02 -080048 // Open an oat file from an already opened File.
Brian Carlstrom265091e2013-01-30 14:08:26 -080049 // Does not use dlopen underneath so cannot be used for runtime use
50 // where relocations may be required. Currently used from
51 // ImageWriter which wants to open a writable version from an existing
52 // file descriptor for patching.
Ian Rogers8d31bbd2013-10-13 10:44:14 -070053 static OatFile* OpenWritable(File* file, const std::string& location, std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080054
55 // Open an oat file backed by a std::vector with the given location.
Brian Carlstrom265091e2013-01-30 14:08:26 -080056 static OatFile* OpenMemory(std::vector<uint8_t>& oat_contents,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070057 const std::string& location,
58 std::string* error_msg);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080059
Brian Carlstrome24fa612011-09-29 00:53:55 -070060 ~OatFile();
61
62 const std::string& GetLocation() const {
63 return location_;
64 }
65
66 const OatHeader& GetOatHeader() const;
67
68 class OatDexFile;
69
Brian Carlstrom3320cf42011-10-04 14:58:28 -070070 class OatMethod {
71 public:
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070072 void LinkMethod(mirror::ArtMethod* method) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromae826982011-11-09 01:33:42 -080073
74 uint32_t GetCodeOffset() const {
75 return code_offset_;
76 }
Ian Rogers0c7abda2012-09-19 13:33:42 -070077 uint32_t GetNativeGcMapOffset() const {
78 return native_gc_map_offset_;
Brian Carlstrome7d856b2012-01-11 18:10:55 -080079 }
Brian Carlstromae826982011-11-09 01:33:42 -080080
Ian Rogersef7d42f2014-01-06 12:55:46 -080081 const void* GetPortableCode() const {
82 // TODO: encode whether code is portable/quick in flags within OatMethod.
83 if (kUsePortableCompiler) {
84 return GetOatPointer<const void*>(code_offset_);
85 } else {
86 return nullptr;
87 }
88 }
89
90 const void* GetQuickCode() const {
91 if (kUsePortableCompiler) {
92 return nullptr;
93 } else {
94 return GetOatPointer<const void*>(code_offset_);
95 }
96 }
97
98 uint32_t GetPortableCodeSize() const {
99 // TODO: With Quick, we store the size before the code. With Portable, the code is in a .o
100 // file we don't manage ourselves. ELF symbols do have a concept of size, so we could capture
101 // that and store it somewhere, such as the OatMethod.
102 return 0;
103 }
104 uint32_t GetQuickCodeSize() const;
Logan Chien0c717dd2012-03-28 18:31:07 +0800105
Ian Rogers0c7abda2012-09-19 13:33:42 -0700106 const uint8_t* GetNativeGcMap() const {
107 return GetOatPointer<const uint8_t*>(native_gc_map_offset_);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800108 }
Logan Chien0c717dd2012-03-28 18:31:07 +0800109
Vladimir Marko7624d252014-05-02 14:40:15 +0100110 size_t GetFrameSizeInBytes() const;
111 uint32_t GetCoreSpillMask() const;
112 uint32_t GetFpSpillMask() const;
Vladimir Marko8a630572014-04-09 18:45:35 +0100113 uint32_t GetMappingTableOffset() const;
114 uint32_t GetVmapTableOffset() const;
115 const uint8_t* GetMappingTable() const;
116 const uint8_t* GetVmapTable() const;
117
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700118 ~OatMethod();
119
Brian Carlstromae826982011-11-09 01:33:42 -0800120 // Create an OatMethod with offsets relative to the given base address
121 OatMethod(const byte* base,
122 const uint32_t code_offset,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700123 const uint32_t gc_map_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700124
Brian Carlstromae826982011-11-09 01:33:42 -0800125 private:
126 template<class T>
127 T GetOatPointer(uint32_t offset) const {
128 if (offset == 0) {
129 return NULL;
130 }
Ian Rogers30fab402012-01-23 15:43:46 -0800131 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800132 }
133
Ian Rogers30fab402012-01-23 15:43:46 -0800134 const byte* begin_;
Brian Carlstromae826982011-11-09 01:33:42 -0800135
136 uint32_t code_offset_;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700137 uint32_t native_gc_map_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800138
139 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700140 };
141
Brian Carlstrome24fa612011-09-29 00:53:55 -0700142 class OatClass {
143 public:
Brian Carlstromba150c32013-08-27 17:31:03 -0700144 mirror::Class::Status GetStatus() const {
145 return status_;
146 }
147
148 OatClassType GetType() const {
149 return type_;
150 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800151
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700152 // Get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700153 // defintion. direct methods come first, followed by virtual
154 // methods. note that runtime created methods such as miranda
155 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700156 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700157
158 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800159 OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800160 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700161 OatClassType type,
162 uint32_t bitmap_size,
163 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800164 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700165
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100166 const OatFile* const oat_file_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700167
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800168 const mirror::Class::Status status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700169
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100170 const OatClassType type_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700171
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100172 const uint32_t* const bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700173
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700174 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700175
176 friend class OatDexFile;
177 };
178
179 class OatDexFile {
180 public:
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700181 // Opens the DexFile referred to by this OatDexFile from within the containing OatFile.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700182 const DexFile* OpenDexFile(std::string* error_msg) const;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700183
184 // Returns the size of the DexFile refered to by this OatDexFile.
Ian Rogers05f28c62012-10-23 18:12:13 -0700185 size_t FileSize() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700186
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700187 // Returns original path of DexFile that was the source of this OatDexFile.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700188 const std::string& GetDexFileLocation() const {
189 return dex_file_location_;
190 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700191
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700192 // Returns checksum of original DexFile that was the source of this OatDexFile;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800193 uint32_t GetDexFileLocationChecksum() const {
194 return dex_file_location_checksum_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700195 }
196
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700197 // Returns the OatClass for the class specified by the given DexFile class_def_index.
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100198 OatClass GetOatClass(uint16_t class_def_index) const;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700199
Brian Carlstrome24fa612011-09-29 00:53:55 -0700200 ~OatDexFile();
Elliott Hughesa21039c2012-06-21 12:09:25 -0700201
Brian Carlstrome24fa612011-09-29 00:53:55 -0700202 private:
203 OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800204 const std::string& dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700205 uint32_t dex_file_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800206 const byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800207 const uint32_t* oat_class_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700208
209 const OatFile* oat_file_;
210 std::string dex_file_location_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800211 uint32_t dex_file_location_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800212 const byte* dex_file_pointer_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800213 const uint32_t* oat_class_offsets_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700214
215 friend class OatFile;
216 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
217 };
218
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,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700221 bool exception_if_not_found = true) const;
222
Brian Carlstromaded5f72011-10-07 17:15:04 -0700223 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700224
Ian Rogers30fab402012-01-23 15:43:46 -0800225 size_t Size() const {
226 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700227 }
228
229 private:
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800230 static void CheckLocation(const std::string& location);
231
232 static OatFile* OpenDlopen(const std::string& elf_filename,
233 const std::string& location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700234 byte* requested_base,
235 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800236
237 static OatFile* OpenElfFile(File* file,
238 const std::string& location,
239 byte* requested_base,
Brian Carlstromf1d34552013-07-12 20:22:23 -0700240 bool writable,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700241 bool executable,
242 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800243
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700244 explicit OatFile(const std::string& filename);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700245 bool Dlopen(const std::string& elf_filename, byte* requested_base, std::string* error_msg);
246 bool ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable,
247 std::string* error_msg);
248 bool Setup(std::string* error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700249
Ian Rogers30fab402012-01-23 15:43:46 -0800250 const byte* Begin() const;
251 const byte* End() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700252
253 // The oat file name.
254 //
255 // The image will embed this to link its associated oat file.
256 const std::string location_;
257
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800258 // Pointer to OatHeader.
259 const byte* begin_;
260
261 // Pointer to end of oat region for bounds checking.
262 const byte* end_;
263
264 // Backing memory map for oat file during when opened by ElfWriter during initial compilation.
Ian Rogers700a4022014-05-19 16:49:03 -0700265 std::unique_ptr<MemMap> mem_map_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700266
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800267 // Backing memory map for oat file during cross compilation.
Ian Rogers700a4022014-05-19 16:49:03 -0700268 std::unique_ptr<ElfFile> elf_file_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800269
270 // dlopen handle during runtime.
271 void* dlopen_handle_;
272
Elliott Hughesa0e18062012-04-13 15:59:59 -0700273 typedef SafeMap<std::string, const OatDexFile*> Table;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700274 Table oat_dex_files_;
275
276 friend class OatClass;
277 friend class OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800278 friend class OatDumper; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700279 DISALLOW_COPY_AND_ASSIGN(OatFile);
280};
281
282} // namespace art
283
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700284#endif // ART_RUNTIME_OAT_FILE_H_