blob: 10f64cc91ece1942ef842f1666dfb47fe31cc062 [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 Carlstrom30e2ea42013-06-19 23:25:37 -070040 // Returns an .odex file name next adjacent to the dex location.
41 // For example, for "/foo/bar/baz.jar", return "/foo/bar/baz.odex".
42 static std::string DexFilenameToOdexFilename(const std::string& location);
Brian Carlstromb7bbba42011-10-13 14:58:47 -070043
Brian Carlstrome24fa612011-09-29 00:53:55 -070044 // Open an oat file. Returns NULL on failure. Requested base can
45 // optionally be used to request where the file should be loaded.
46 static OatFile* Open(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -080047 const std::string& location,
Brian Carlstromf1d34552013-07-12 20:22:23 -070048 byte* requested_base,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070049 bool executable,
50 std::string* error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -070051
Brian Carlstrom700c8d32012-11-05 10:42:02 -080052 // Open an oat file from an already opened File.
Brian Carlstrom265091e2013-01-30 14:08:26 -080053 // Does not use dlopen underneath so cannot be used for runtime use
54 // where relocations may be required. Currently used from
55 // ImageWriter which wants to open a writable version from an existing
56 // file descriptor for patching.
Ian Rogers8d31bbd2013-10-13 10:44:14 -070057 static OatFile* OpenWritable(File* file, const std::string& location, std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080058
59 // Open an oat file backed by a std::vector with the given location.
Brian Carlstrom265091e2013-01-30 14:08:26 -080060 static OatFile* OpenMemory(std::vector<uint8_t>& oat_contents,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070061 const std::string& location,
62 std::string* error_msg);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080063
Brian Carlstrome24fa612011-09-29 00:53:55 -070064 ~OatFile();
65
66 const std::string& GetLocation() const {
67 return location_;
68 }
69
70 const OatHeader& GetOatHeader() const;
71
72 class OatDexFile;
73
Brian Carlstrom3320cf42011-10-04 14:58:28 -070074 class OatMethod {
75 public:
Brian Carlstromea46f952013-07-30 01:26:50 -070076 void LinkMethod(mirror::ArtMethod* method) const;
Brian Carlstromae826982011-11-09 01:33:42 -080077
78 uint32_t GetCodeOffset() const {
79 return code_offset_;
80 }
81 size_t GetFrameSizeInBytes() const {
82 return frame_size_in_bytes_;
83 }
84 uint32_t GetCoreSpillMask() const {
85 return core_spill_mask_;
86 }
87 uint32_t GetFpSpillMask() const {
88 return fp_spill_mask_;
89 }
90 uint32_t GetMappingTableOffset() const {
91 return mapping_table_offset_;
92 }
93 uint32_t GetVmapTableOffset() const {
94 return vmap_table_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 Rogers1809a722013-08-09 22:05:32 -0700125 const uint8_t* GetMappingTable() const {
126 return GetOatPointer<const uint8_t*>(mapping_table_offset_);
Brian Carlstromae826982011-11-09 01:33:42 -0800127 }
Ian Rogers1809a722013-08-09 22:05:32 -0700128 const uint8_t* GetVmapTable() const {
129 return GetOatPointer<const uint8_t*>(vmap_table_offset_);
Brian Carlstromae826982011-11-09 01:33:42 -0800130 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700131 const uint8_t* GetNativeGcMap() const {
132 return GetOatPointer<const uint8_t*>(native_gc_map_offset_);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800133 }
Logan Chien0c717dd2012-03-28 18:31:07 +0800134
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700135 ~OatMethod();
136
Brian Carlstromae826982011-11-09 01:33:42 -0800137 // Create an OatMethod with offsets relative to the given base address
138 OatMethod(const byte* base,
139 const uint32_t code_offset,
140 const size_t frame_size_in_bytes,
141 const uint32_t core_spill_mask,
142 const uint32_t fp_spill_mask,
143 const uint32_t mapping_table_offset,
144 const uint32_t vmap_table_offset,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700145 const uint32_t gc_map_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700146
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 Rogers30fab402012-01-23 15:43:46 -0800156 const byte* begin_;
Brian Carlstromae826982011-11-09 01:33:42 -0800157
158 uint32_t code_offset_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700159 size_t frame_size_in_bytes_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700160 uint32_t core_spill_mask_;
161 uint32_t fp_spill_mask_;
Brian Carlstromae826982011-11-09 01:33:42 -0800162 uint32_t mapping_table_offset_;
163 uint32_t vmap_table_offset_;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700164 uint32_t native_gc_map_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800165
166 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700167 };
168
Brian Carlstrome24fa612011-09-29 00:53:55 -0700169 class OatClass {
170 public:
Brian Carlstromba150c32013-08-27 17:31:03 -0700171 mirror::Class::Status GetStatus() const {
172 return status_;
173 }
174
175 OatClassType GetType() const {
176 return type_;
177 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800178
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700179 // get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700180 // defintion. direct methods come first, followed by virtual
181 // methods. note that runtime created methods such as miranda
182 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700183 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700184
185 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800186 OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800187 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700188 OatClassType type,
189 uint32_t bitmap_size,
190 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800191 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700192
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100193 const OatFile* const oat_file_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700194
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800195 const mirror::Class::Status status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700196
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100197 const OatClassType type_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700198
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100199 const uint32_t* const bitmap_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700200
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700201 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700202
203 friend class OatDexFile;
204 };
205
206 class OatDexFile {
207 public:
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700208 // Opens the DexFile referred to by this OatDexFile from within the containing OatFile.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700209 const DexFile* OpenDexFile(std::string* error_msg) const;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700210
211 // Returns the size of the DexFile refered to by this OatDexFile.
Ian Rogers05f28c62012-10-23 18:12:13 -0700212 size_t FileSize() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700213
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700214 // Returns original path of DexFile that was the source of this OatDexFile.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700215 const std::string& GetDexFileLocation() const {
216 return dex_file_location_;
217 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700218
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700219 // Returns checksum of original DexFile that was the source of this OatDexFile;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800220 uint32_t GetDexFileLocationChecksum() const {
221 return dex_file_location_checksum_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700222 }
223
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700224 // Returns the OatClass for the class specified by the given DexFile class_def_index.
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100225 OatClass GetOatClass(uint16_t class_def_index) const;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700226
Brian Carlstrome24fa612011-09-29 00:53:55 -0700227 ~OatDexFile();
Elliott Hughesa21039c2012-06-21 12:09:25 -0700228
Brian Carlstrome24fa612011-09-29 00:53:55 -0700229 private:
230 OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800231 const std::string& dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700232 uint32_t dex_file_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800233 const byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800234 const uint32_t* oat_class_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700235
236 const OatFile* oat_file_;
237 std::string dex_file_location_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800238 uint32_t dex_file_location_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800239 const byte* dex_file_pointer_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800240 const uint32_t* oat_class_offsets_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700241
242 friend class OatFile;
243 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
244 };
245
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700246 const OatDexFile* GetOatDexFile(const char* dex_location,
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700247 const uint32_t* const dex_location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700248 bool exception_if_not_found = true) const;
249
Brian Carlstromaded5f72011-10-07 17:15:04 -0700250 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700251
Ian Rogers30fab402012-01-23 15:43:46 -0800252 size_t Size() const {
253 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700254 }
255
256 private:
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800257 static void CheckLocation(const std::string& location);
258
259 static OatFile* OpenDlopen(const std::string& elf_filename,
260 const std::string& location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700261 byte* requested_base,
262 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800263
264 static OatFile* OpenElfFile(File* file,
265 const std::string& location,
266 byte* requested_base,
Brian Carlstromf1d34552013-07-12 20:22:23 -0700267 bool writable,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700268 bool executable,
269 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800270
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700271 explicit OatFile(const std::string& filename);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700272 bool Dlopen(const std::string& elf_filename, byte* requested_base, std::string* error_msg);
273 bool ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable,
274 std::string* error_msg);
275 bool Setup(std::string* error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700276
Ian Rogers30fab402012-01-23 15:43:46 -0800277 const byte* Begin() const;
278 const byte* End() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700279
280 // The oat file name.
281 //
282 // The image will embed this to link its associated oat file.
283 const std::string location_;
284
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800285 // Pointer to OatHeader.
286 const byte* begin_;
287
288 // Pointer to end of oat region for bounds checking.
289 const byte* end_;
290
291 // Backing memory map for oat file during when opened by ElfWriter during initial compilation.
Brian Carlstrome24fa612011-09-29 00:53:55 -0700292 UniquePtr<MemMap> mem_map_;
293
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800294 // Backing memory map for oat file during cross compilation.
295 UniquePtr<ElfFile> elf_file_;
296
297 // dlopen handle during runtime.
298 void* dlopen_handle_;
299
Elliott Hughesa0e18062012-04-13 15:59:59 -0700300 typedef SafeMap<std::string, const OatDexFile*> Table;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700301 Table oat_dex_files_;
302
303 friend class OatClass;
304 friend class OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800305 friend class OatDumper; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700306 DISALLOW_COPY_AND_ASSIGN(OatFile);
307};
308
309} // namespace art
310
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700311#endif // ART_RUNTIME_OAT_FILE_H_