blob: 0bf79a9c03d740c28742c75d2f0f1359969a16f3 [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
17#ifndef ART_SRC_OAT_FILE_H_
18#define ART_SRC_OAT_FILE_H_
19
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 Carlstrom700c8d32012-11-05 10:42:02 -080047 byte* requested_base);
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.
54 static OatFile* OpenWritable(File* file, const std::string& location);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080055
56 // Open an oat file backed by a std::vector with the given location.
Brian Carlstrom265091e2013-01-30 14:08:26 -080057 static OatFile* OpenMemory(std::vector<uint8_t>& oat_contents,
58 const std::string& location);
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:
Brian Carlstrom265091e2013-01-30 14:08:26 -080072 void LinkMethod(mirror::AbstractMethod* method) const;
Brian Carlstromae826982011-11-09 01:33:42 -080073
74 uint32_t GetCodeOffset() const {
75 return code_offset_;
76 }
77 size_t GetFrameSizeInBytes() const {
78 return frame_size_in_bytes_;
79 }
80 uint32_t GetCoreSpillMask() const {
81 return core_spill_mask_;
82 }
83 uint32_t GetFpSpillMask() const {
84 return fp_spill_mask_;
85 }
86 uint32_t GetMappingTableOffset() const {
87 return mapping_table_offset_;
88 }
89 uint32_t GetVmapTableOffset() const {
90 return vmap_table_offset_;
91 }
Ian Rogers0c7abda2012-09-19 13:33:42 -070092 uint32_t GetNativeGcMapOffset() const {
93 return native_gc_map_offset_;
Brian Carlstrome7d856b2012-01-11 18:10:55 -080094 }
Brian Carlstromae826982011-11-09 01:33:42 -080095
Logan Chien0c717dd2012-03-28 18:31:07 +080096 const void* GetCode() const;
97 uint32_t GetCodeSize() const;
98
Brian Carlstromae826982011-11-09 01:33:42 -080099 const uint32_t* GetMappingTable() const {
100 return GetOatPointer<const uint32_t*>(mapping_table_offset_);
101 }
102 const uint16_t* GetVmapTable() const {
103 return GetOatPointer<const uint16_t*>(vmap_table_offset_);
104 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700105 const uint8_t* GetNativeGcMap() const {
106 return GetOatPointer<const uint8_t*>(native_gc_map_offset_);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800107 }
Logan Chien0c717dd2012-03-28 18:31:07 +0800108
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700109 ~OatMethod();
110
Brian Carlstromae826982011-11-09 01:33:42 -0800111 // Create an OatMethod with offsets relative to the given base address
112 OatMethod(const byte* base,
113 const uint32_t code_offset,
114 const size_t frame_size_in_bytes,
115 const uint32_t core_spill_mask,
116 const uint32_t fp_spill_mask,
117 const uint32_t mapping_table_offset,
118 const uint32_t vmap_table_offset,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700119 const uint32_t gc_map_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700120
Brian Carlstromae826982011-11-09 01:33:42 -0800121 private:
122 template<class T>
123 T GetOatPointer(uint32_t offset) const {
124 if (offset == 0) {
125 return NULL;
126 }
Ian Rogers30fab402012-01-23 15:43:46 -0800127 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800128 }
129
Ian Rogers30fab402012-01-23 15:43:46 -0800130 const byte* begin_;
Brian Carlstromae826982011-11-09 01:33:42 -0800131
132 uint32_t code_offset_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700133 size_t frame_size_in_bytes_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700134 uint32_t core_spill_mask_;
135 uint32_t fp_spill_mask_;
Brian Carlstromae826982011-11-09 01:33:42 -0800136 uint32_t mapping_table_offset_;
137 uint32_t vmap_table_offset_;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700138 uint32_t native_gc_map_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800139
140 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700141 };
142
Brian Carlstrome24fa612011-09-29 00:53:55 -0700143 class OatClass {
144 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145 mirror::Class::Status GetStatus() const;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800146
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700147 // get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700148 // defintion. direct methods come first, followed by virtual
149 // methods. note that runtime created methods such as miranda
150 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700151 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700152 ~OatClass();
153
154 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800155 OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800156 mirror::Class::Status status,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800157 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700158
Brian Carlstrome24fa612011-09-29 00:53:55 -0700159 const OatFile* oat_file_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800160 const mirror::Class::Status status_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700161 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700162
163 friend class OatDexFile;
164 };
165
166 class OatDexFile {
167 public:
Brian Carlstrom89521892011-12-07 22:05:07 -0800168 const DexFile* OpenDexFile() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700169 const OatClass* GetOatClass(uint32_t class_def_index) const;
Ian Rogers05f28c62012-10-23 18:12:13 -0700170 size_t FileSize() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700171
172 const std::string& GetDexFileLocation() const {
173 return dex_file_location_;
174 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700175
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800176 uint32_t GetDexFileLocationChecksum() const {
177 return dex_file_location_checksum_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700178 }
179
Brian Carlstrome24fa612011-09-29 00:53:55 -0700180 ~OatDexFile();
Elliott Hughesa21039c2012-06-21 12:09:25 -0700181
Brian Carlstrome24fa612011-09-29 00:53:55 -0700182 private:
183 OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800184 const std::string& dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700185 uint32_t dex_file_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800186 const byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800187 const uint32_t* oat_class_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700188
189 const OatFile* oat_file_;
190 std::string dex_file_location_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800191 uint32_t dex_file_location_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800192 const byte* dex_file_pointer_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800193 const uint32_t* oat_class_offsets_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700194
195 friend class OatFile;
196 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
197 };
198
Ian Rogers7fe2c692011-12-06 16:35:59 -0800199 const OatDexFile* GetOatDexFile(const std::string& dex_file_location,
Ian Rogers33e95662013-05-20 20:29:14 -0700200 bool exception_if_not_found = true) const
201 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700202 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700203
Ian Rogers30fab402012-01-23 15:43:46 -0800204 size_t Size() const {
205 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700206 }
207
208 private:
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800209 static void CheckLocation(const std::string& location);
210
211 static OatFile* OpenDlopen(const std::string& elf_filename,
212 const std::string& location,
213 byte* requested_base);
214
215 static OatFile* OpenElfFile(File* file,
216 const std::string& location,
217 byte* requested_base,
218 bool writable);
219
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700220 explicit OatFile(const std::string& filename);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800221 bool Dlopen(const std::string& elf_filename, byte* requested_base);
222 bool ElfFileOpen(File* file, byte* requested_base, bool writable);
Brian Carlstromf1b30302013-03-28 10:35:32 -0700223 bool Setup();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700224
Ian Rogers30fab402012-01-23 15:43:46 -0800225 const byte* Begin() const;
226 const byte* End() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700227
228 // The oat file name.
229 //
230 // The image will embed this to link its associated oat file.
231 const std::string location_;
232
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800233 // Pointer to OatHeader.
234 const byte* begin_;
235
236 // Pointer to end of oat region for bounds checking.
237 const byte* end_;
238
239 // Backing memory map for oat file during when opened by ElfWriter during initial compilation.
Brian Carlstrome24fa612011-09-29 00:53:55 -0700240 UniquePtr<MemMap> mem_map_;
241
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800242 // Backing memory map for oat file during cross compilation.
243 UniquePtr<ElfFile> elf_file_;
244
245 // dlopen handle during runtime.
246 void* dlopen_handle_;
247
Elliott Hughesa0e18062012-04-13 15:59:59 -0700248 typedef SafeMap<std::string, const OatDexFile*> Table;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700249 Table oat_dex_files_;
250
251 friend class OatClass;
252 friend class OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800253 friend class OatDumper; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700254 DISALLOW_COPY_AND_ASSIGN(OatFile);
255};
256
257} // namespace art
258
259#endif // ART_SRC_OAT_WRITER_H_