blob: af1476088d22f0306bd0d940e59a7d50143f38a5 [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 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,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070048 bool executable,
49 std::string* error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -070050
Brian Carlstrom700c8d32012-11-05 10:42:02 -080051 // Open an oat file from an already opened File.
Brian Carlstrom265091e2013-01-30 14:08:26 -080052 // Does not use dlopen underneath so cannot be used for runtime use
53 // where relocations may be required. Currently used from
54 // ImageWriter which wants to open a writable version from an existing
55 // file descriptor for patching.
Ian Rogers8d31bbd2013-10-13 10:44:14 -070056 static OatFile* OpenWritable(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
65 const std::string& GetLocation() const {
66 return location_;
67 }
68
69 const OatHeader& GetOatHeader() const;
70
71 class OatDexFile;
72
Brian Carlstrom3320cf42011-10-04 14:58:28 -070073 class OatMethod {
74 public:
Brian Carlstromea46f952013-07-30 01:26:50 -070075 void LinkMethod(mirror::ArtMethod* method) const;
Brian Carlstromae826982011-11-09 01:33:42 -080076
77 uint32_t GetCodeOffset() const {
78 return code_offset_;
79 }
80 size_t GetFrameSizeInBytes() const {
81 return frame_size_in_bytes_;
82 }
83 uint32_t GetCoreSpillMask() const {
84 return core_spill_mask_;
85 }
86 uint32_t GetFpSpillMask() const {
87 return fp_spill_mask_;
88 }
89 uint32_t GetMappingTableOffset() const {
90 return mapping_table_offset_;
91 }
92 uint32_t GetVmapTableOffset() const {
93 return vmap_table_offset_;
94 }
Ian Rogers0c7abda2012-09-19 13:33:42 -070095 uint32_t GetNativeGcMapOffset() const {
96 return native_gc_map_offset_;
Brian Carlstrome7d856b2012-01-11 18:10:55 -080097 }
Brian Carlstromae826982011-11-09 01:33:42 -080098
Logan Chien0c717dd2012-03-28 18:31:07 +080099 const void* GetCode() const;
100 uint32_t GetCodeSize() const;
101
Ian Rogers1809a722013-08-09 22:05:32 -0700102 const uint8_t* GetMappingTable() const {
103 return GetOatPointer<const uint8_t*>(mapping_table_offset_);
Brian Carlstromae826982011-11-09 01:33:42 -0800104 }
Ian Rogers1809a722013-08-09 22:05:32 -0700105 const uint8_t* GetVmapTable() const {
106 return GetOatPointer<const uint8_t*>(vmap_table_offset_);
Brian Carlstromae826982011-11-09 01:33:42 -0800107 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700108 const uint8_t* GetNativeGcMap() const {
109 return GetOatPointer<const uint8_t*>(native_gc_map_offset_);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800110 }
Logan Chien0c717dd2012-03-28 18:31:07 +0800111
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700112 ~OatMethod();
113
Brian Carlstromae826982011-11-09 01:33:42 -0800114 // Create an OatMethod with offsets relative to the given base address
115 OatMethod(const byte* base,
116 const uint32_t code_offset,
117 const size_t frame_size_in_bytes,
118 const uint32_t core_spill_mask,
119 const uint32_t fp_spill_mask,
120 const uint32_t mapping_table_offset,
121 const uint32_t vmap_table_offset,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700122 const uint32_t gc_map_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700123
Brian Carlstromae826982011-11-09 01:33:42 -0800124 private:
125 template<class T>
126 T GetOatPointer(uint32_t offset) const {
127 if (offset == 0) {
128 return NULL;
129 }
Ian Rogers30fab402012-01-23 15:43:46 -0800130 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800131 }
132
Ian Rogers30fab402012-01-23 15:43:46 -0800133 const byte* begin_;
Brian Carlstromae826982011-11-09 01:33:42 -0800134
135 uint32_t code_offset_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700136 size_t frame_size_in_bytes_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700137 uint32_t core_spill_mask_;
138 uint32_t fp_spill_mask_;
Brian Carlstromae826982011-11-09 01:33:42 -0800139 uint32_t mapping_table_offset_;
140 uint32_t vmap_table_offset_;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700141 uint32_t native_gc_map_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800142
143 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700144 };
145
Brian Carlstrome24fa612011-09-29 00:53:55 -0700146 class OatClass {
147 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800148 mirror::Class::Status GetStatus() const;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800149
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700150 // get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700151 // defintion. direct methods come first, followed by virtual
152 // methods. note that runtime created methods such as miranda
153 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700154 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700155 ~OatClass();
156
157 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800158 OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800159 mirror::Class::Status status,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800160 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700161
Brian Carlstrome24fa612011-09-29 00:53:55 -0700162 const OatFile* oat_file_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800163 const mirror::Class::Status status_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700164 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700165
166 friend class OatDexFile;
167 };
168
169 class OatDexFile {
170 public:
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700171 // Opens the DexFile referred to by this OatDexFile from within the containing OatFile.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700172 const DexFile* OpenDexFile(std::string* error_msg) const;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700173
174 // Returns the size of the DexFile refered to by this OatDexFile.
Ian Rogers05f28c62012-10-23 18:12:13 -0700175 size_t FileSize() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700176
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700177 // Returns original path of DexFile that was the source of this OatDexFile.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700178 const std::string& GetDexFileLocation() const {
179 return dex_file_location_;
180 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700181
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700182 // Returns checksum of original DexFile that was the source of this OatDexFile;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800183 uint32_t GetDexFileLocationChecksum() const {
184 return dex_file_location_checksum_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700185 }
186
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700187 // Returns the OatClass for the class specified by the given DexFile class_def_index.
Ian Rogersee39a102013-09-19 02:56:49 -0700188 const OatClass* GetOatClass(uint16_t class_def_index) const;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700189
Brian Carlstrome24fa612011-09-29 00:53:55 -0700190 ~OatDexFile();
Elliott Hughesa21039c2012-06-21 12:09:25 -0700191
Brian Carlstrome24fa612011-09-29 00:53:55 -0700192 private:
193 OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800194 const std::string& dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700195 uint32_t dex_file_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800196 const byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800197 const uint32_t* oat_class_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700198
199 const OatFile* oat_file_;
200 std::string dex_file_location_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800201 uint32_t dex_file_location_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800202 const byte* dex_file_pointer_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800203 const uint32_t* oat_class_offsets_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700204
205 friend class OatFile;
206 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
207 };
208
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700209 const OatDexFile* GetOatDexFile(const char* dex_location,
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700210 const uint32_t* const dex_location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700211 bool exception_if_not_found = true) const;
212
Brian Carlstromaded5f72011-10-07 17:15:04 -0700213 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700214
Ian Rogers30fab402012-01-23 15:43:46 -0800215 size_t Size() const {
216 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700217 }
218
219 private:
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800220 static void CheckLocation(const std::string& location);
221
222 static OatFile* OpenDlopen(const std::string& elf_filename,
223 const std::string& location,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700224 byte* requested_base,
225 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800226
227 static OatFile* OpenElfFile(File* file,
228 const std::string& location,
229 byte* requested_base,
Brian Carlstromf1d34552013-07-12 20:22:23 -0700230 bool writable,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700231 bool executable,
232 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800233
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700234 explicit OatFile(const std::string& filename);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700235 bool Dlopen(const std::string& elf_filename, byte* requested_base, std::string* error_msg);
236 bool ElfFileOpen(File* file, byte* requested_base, bool writable, bool executable,
237 std::string* error_msg);
238 bool Setup(std::string* error_msg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700239
Ian Rogers30fab402012-01-23 15:43:46 -0800240 const byte* Begin() const;
241 const byte* End() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700242
243 // The oat file name.
244 //
245 // The image will embed this to link its associated oat file.
246 const std::string location_;
247
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800248 // Pointer to OatHeader.
249 const byte* begin_;
250
251 // Pointer to end of oat region for bounds checking.
252 const byte* end_;
253
254 // Backing memory map for oat file during when opened by ElfWriter during initial compilation.
Brian Carlstrome24fa612011-09-29 00:53:55 -0700255 UniquePtr<MemMap> mem_map_;
256
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800257 // Backing memory map for oat file during cross compilation.
258 UniquePtr<ElfFile> elf_file_;
259
260 // dlopen handle during runtime.
261 void* dlopen_handle_;
262
Elliott Hughesa0e18062012-04-13 15:59:59 -0700263 typedef SafeMap<std::string, const OatDexFile*> Table;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700264 Table oat_dex_files_;
265
266 friend class OatClass;
267 friend class OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800268 friend class OatDumper; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700269 DISALLOW_COPY_AND_ASSIGN(OatFile);
270};
271
272} // namespace art
273
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700274#endif // ART_RUNTIME_OAT_FILE_H_