blob: 650301465b62d1551583924895b1ce40c2c2437e [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"
26#include "mirror/abstract_method.h"
27#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 Carlstrom700c8d32012-11-05 10:42:02 -080032class ElfFile;
33class MemMap;
34class OatMethodOffsets;
Ian Rogers33e95662013-05-20 20:29:14 -070035class OatHeader;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080036
Brian Carlstrome24fa612011-09-29 00:53:55 -070037class OatFile {
38 public:
Brian Carlstrom30e2ea42013-06-19 23:25:37 -070039 // Returns an .odex file name next adjacent to the dex location.
40 // For example, for "/foo/bar/baz.jar", return "/foo/bar/baz.odex".
41 static std::string DexFilenameToOdexFilename(const std::string& location);
Brian Carlstromb7bbba42011-10-13 14:58:47 -070042
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,
48 bool executable);
Brian Carlstrome24fa612011-09-29 00:53:55 -070049
Brian Carlstrom700c8d32012-11-05 10:42:02 -080050 // Open an oat file from an already opened File.
Brian Carlstrom265091e2013-01-30 14:08:26 -080051 // Does not use dlopen underneath so cannot be used for runtime use
52 // where relocations may be required. Currently used from
53 // ImageWriter which wants to open a writable version from an existing
54 // file descriptor for patching.
55 static OatFile* OpenWritable(File* file, const std::string& location);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080056
57 // Open an oat file backed by a std::vector with the given location.
Brian Carlstrom265091e2013-01-30 14:08:26 -080058 static OatFile* OpenMemory(std::vector<uint8_t>& oat_contents,
59 const std::string& location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080060
Brian Carlstrome24fa612011-09-29 00:53:55 -070061 ~OatFile();
62
63 const std::string& GetLocation() const {
64 return location_;
65 }
66
67 const OatHeader& GetOatHeader() const;
68
69 class OatDexFile;
70
Brian Carlstrom3320cf42011-10-04 14:58:28 -070071 class OatMethod {
72 public:
Brian Carlstrom265091e2013-01-30 14:08:26 -080073 void LinkMethod(mirror::AbstractMethod* method) const;
Brian Carlstromae826982011-11-09 01:33:42 -080074
75 uint32_t GetCodeOffset() const {
76 return code_offset_;
77 }
78 size_t GetFrameSizeInBytes() const {
79 return frame_size_in_bytes_;
80 }
81 uint32_t GetCoreSpillMask() const {
82 return core_spill_mask_;
83 }
84 uint32_t GetFpSpillMask() const {
85 return fp_spill_mask_;
86 }
87 uint32_t GetMappingTableOffset() const {
88 return mapping_table_offset_;
89 }
90 uint32_t GetVmapTableOffset() const {
91 return vmap_table_offset_;
92 }
Ian Rogers0c7abda2012-09-19 13:33:42 -070093 uint32_t GetNativeGcMapOffset() const {
94 return native_gc_map_offset_;
Brian Carlstrome7d856b2012-01-11 18:10:55 -080095 }
Brian Carlstromae826982011-11-09 01:33:42 -080096
Logan Chien0c717dd2012-03-28 18:31:07 +080097 const void* GetCode() const;
98 uint32_t GetCodeSize() const;
99
Ian Rogers1809a722013-08-09 22:05:32 -0700100 const uint8_t* GetMappingTable() const {
101 return GetOatPointer<const uint8_t*>(mapping_table_offset_);
Brian Carlstromae826982011-11-09 01:33:42 -0800102 }
Ian Rogers1809a722013-08-09 22:05:32 -0700103 const uint8_t* GetVmapTable() const {
104 return GetOatPointer<const uint8_t*>(vmap_table_offset_);
Brian Carlstromae826982011-11-09 01:33:42 -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
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700110 ~OatMethod();
111
Brian Carlstromae826982011-11-09 01:33:42 -0800112 // Create an OatMethod with offsets relative to the given base address
113 OatMethod(const byte* base,
114 const uint32_t code_offset,
115 const size_t frame_size_in_bytes,
116 const uint32_t core_spill_mask,
117 const uint32_t fp_spill_mask,
118 const uint32_t mapping_table_offset,
119 const uint32_t vmap_table_offset,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700120 const uint32_t gc_map_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700121
Brian Carlstromae826982011-11-09 01:33:42 -0800122 private:
123 template<class T>
124 T GetOatPointer(uint32_t offset) const {
125 if (offset == 0) {
126 return NULL;
127 }
Ian Rogers30fab402012-01-23 15:43:46 -0800128 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800129 }
130
Ian Rogers30fab402012-01-23 15:43:46 -0800131 const byte* begin_;
Brian Carlstromae826982011-11-09 01:33:42 -0800132
133 uint32_t code_offset_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700134 size_t frame_size_in_bytes_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700135 uint32_t core_spill_mask_;
136 uint32_t fp_spill_mask_;
Brian Carlstromae826982011-11-09 01:33:42 -0800137 uint32_t mapping_table_offset_;
138 uint32_t vmap_table_offset_;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700139 uint32_t native_gc_map_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800140
141 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700142 };
143
Brian Carlstrome24fa612011-09-29 00:53:55 -0700144 class OatClass {
145 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146 mirror::Class::Status GetStatus() const;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800147
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700148 // get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700149 // defintion. direct methods come first, followed by virtual
150 // methods. note that runtime created methods such as miranda
151 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700152 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700153 ~OatClass();
154
155 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800156 OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800157 mirror::Class::Status status,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800158 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700159
Brian Carlstrome24fa612011-09-29 00:53:55 -0700160 const OatFile* oat_file_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161 const mirror::Class::Status status_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700162 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700163
164 friend class OatDexFile;
165 };
166
167 class OatDexFile {
168 public:
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700169 // Opens the DexFile referred to by this OatDexFile from within the containing OatFile.
Brian Carlstrom89521892011-12-07 22:05:07 -0800170 const DexFile* OpenDexFile() const;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700171
172 // Returns the size of the DexFile refered to by this OatDexFile.
Ian Rogers05f28c62012-10-23 18:12:13 -0700173 size_t FileSize() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700174
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700175 // Returns original path of DexFile that was the source of this OatDexFile.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700176 const std::string& GetDexFileLocation() const {
177 return dex_file_location_;
178 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700179
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700180 // Returns checksum of original DexFile that was the source of this OatDexFile;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800181 uint32_t GetDexFileLocationChecksum() const {
182 return dex_file_location_checksum_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700183 }
184
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700185 // Returns the OatClass for the class specified by the given DexFile class_def_index.
186 const OatClass* GetOatClass(uint32_t class_def_index) const;
187
Brian Carlstrome24fa612011-09-29 00:53:55 -0700188 ~OatDexFile();
Elliott Hughesa21039c2012-06-21 12:09:25 -0700189
Brian Carlstrome24fa612011-09-29 00:53:55 -0700190 private:
191 OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800192 const std::string& dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700193 uint32_t dex_file_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800194 const byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800195 const uint32_t* oat_class_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700196
197 const OatFile* oat_file_;
198 std::string dex_file_location_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800199 uint32_t dex_file_location_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800200 const byte* dex_file_pointer_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800201 const uint32_t* oat_class_offsets_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700202
203 friend class OatFile;
204 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
205 };
206
Ian Rogers7fe2c692011-12-06 16:35:59 -0800207 const OatDexFile* GetOatDexFile(const std::string& dex_file_location,
Ian Rogers33e95662013-05-20 20:29:14 -0700208 bool exception_if_not_found = true) const
209 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700210 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700211
Ian Rogers30fab402012-01-23 15:43:46 -0800212 size_t Size() const {
213 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700214 }
215
216 private:
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800217 static void CheckLocation(const std::string& location);
218
219 static OatFile* OpenDlopen(const std::string& elf_filename,
220 const std::string& location,
221 byte* requested_base);
222
223 static OatFile* OpenElfFile(File* file,
224 const std::string& location,
225 byte* requested_base,
Brian Carlstromf1d34552013-07-12 20:22:23 -0700226 bool writable,
227 bool executable);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800228
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700229 explicit OatFile(const std::string& filename);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800230 bool Dlopen(const std::string& elf_filename, byte* requested_base);
Brian Carlstromf1d34552013-07-12 20:22:23 -0700231 bool ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable);
Brian Carlstromf1b30302013-03-28 10:35:32 -0700232 bool Setup();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700233
Ian Rogers30fab402012-01-23 15:43:46 -0800234 const byte* Begin() const;
235 const byte* End() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700236
237 // The oat file name.
238 //
239 // The image will embed this to link its associated oat file.
240 const std::string location_;
241
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800242 // Pointer to OatHeader.
243 const byte* begin_;
244
245 // Pointer to end of oat region for bounds checking.
246 const byte* end_;
247
248 // Backing memory map for oat file during when opened by ElfWriter during initial compilation.
Brian Carlstrome24fa612011-09-29 00:53:55 -0700249 UniquePtr<MemMap> mem_map_;
250
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800251 // Backing memory map for oat file during cross compilation.
252 UniquePtr<ElfFile> elf_file_;
253
254 // dlopen handle during runtime.
255 void* dlopen_handle_;
256
Elliott Hughesa0e18062012-04-13 15:59:59 -0700257 typedef SafeMap<std::string, const OatDexFile*> Table;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700258 Table oat_dex_files_;
259
260 friend class OatClass;
261 friend class OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800262 friend class OatDumper; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700263 DISALLOW_COPY_AND_ASSIGN(OatFile);
264};
265
266} // namespace art
267
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700268#endif // ART_RUNTIME_OAT_FILE_H_