blob: 46aad4ffd11f0a50b7c788840981a084d9bb5597 [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 Carlstromb7bbba42011-10-13 14:58:47 -070039 // Returns an OatFile name based on a DexFile location
jeffhao262bf462011-10-20 18:36:32 -070040 static std::string DexFilenameToOatFilename(const std::string& location);
Brian Carlstromb7bbba42011-10-13 14:58:47 -070041
Brian Carlstrome24fa612011-09-29 00:53:55 -070042 // Open an oat file. Returns NULL on failure. Requested base can
43 // optionally be used to request where the file should be loaded.
44 static OatFile* Open(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -080045 const std::string& location,
Brian Carlstrom700c8d32012-11-05 10:42:02 -080046 byte* requested_base);
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.
53 static OatFile* OpenWritable(File* file, const std::string& location);
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,
57 const std::string& location);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080058
Brian Carlstrome24fa612011-09-29 00:53:55 -070059 ~OatFile();
60
61 const std::string& GetLocation() const {
62 return location_;
63 }
64
65 const OatHeader& GetOatHeader() const;
66
67 class OatDexFile;
68
Brian Carlstrom3320cf42011-10-04 14:58:28 -070069 class OatMethod {
70 public:
Brian Carlstrom265091e2013-01-30 14:08:26 -080071 void LinkMethod(mirror::AbstractMethod* method) const;
Brian Carlstromae826982011-11-09 01:33:42 -080072
73 uint32_t GetCodeOffset() const {
74 return code_offset_;
75 }
76 size_t GetFrameSizeInBytes() const {
77 return frame_size_in_bytes_;
78 }
79 uint32_t GetCoreSpillMask() const {
80 return core_spill_mask_;
81 }
82 uint32_t GetFpSpillMask() const {
83 return fp_spill_mask_;
84 }
85 uint32_t GetMappingTableOffset() const {
86 return mapping_table_offset_;
87 }
88 uint32_t GetVmapTableOffset() const {
89 return vmap_table_offset_;
90 }
Ian Rogers0c7abda2012-09-19 13:33:42 -070091 uint32_t GetNativeGcMapOffset() const {
92 return native_gc_map_offset_;
Brian Carlstrome7d856b2012-01-11 18:10:55 -080093 }
Brian Carlstromae826982011-11-09 01:33:42 -080094
Logan Chien0c717dd2012-03-28 18:31:07 +080095 const void* GetCode() const;
96 uint32_t GetCodeSize() const;
97
Brian Carlstromae826982011-11-09 01:33:42 -080098 const uint32_t* GetMappingTable() const {
99 return GetOatPointer<const uint32_t*>(mapping_table_offset_);
100 }
101 const uint16_t* GetVmapTable() const {
102 return GetOatPointer<const uint16_t*>(vmap_table_offset_);
103 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700104 const uint8_t* GetNativeGcMap() const {
105 return GetOatPointer<const uint8_t*>(native_gc_map_offset_);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800106 }
Logan Chien0c717dd2012-03-28 18:31:07 +0800107
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700108 ~OatMethod();
109
Brian Carlstromae826982011-11-09 01:33:42 -0800110 // Create an OatMethod with offsets relative to the given base address
111 OatMethod(const byte* base,
112 const uint32_t code_offset,
113 const size_t frame_size_in_bytes,
114 const uint32_t core_spill_mask,
115 const uint32_t fp_spill_mask,
116 const uint32_t mapping_table_offset,
117 const uint32_t vmap_table_offset,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700118 const uint32_t gc_map_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700119
Brian Carlstromae826982011-11-09 01:33:42 -0800120 private:
121 template<class T>
122 T GetOatPointer(uint32_t offset) const {
123 if (offset == 0) {
124 return NULL;
125 }
Ian Rogers30fab402012-01-23 15:43:46 -0800126 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800127 }
128
Ian Rogers30fab402012-01-23 15:43:46 -0800129 const byte* begin_;
Brian Carlstromae826982011-11-09 01:33:42 -0800130
131 uint32_t code_offset_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700132 size_t frame_size_in_bytes_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700133 uint32_t core_spill_mask_;
134 uint32_t fp_spill_mask_;
Brian Carlstromae826982011-11-09 01:33:42 -0800135 uint32_t mapping_table_offset_;
136 uint32_t vmap_table_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:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800144 mirror::Class::Status GetStatus() const;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800145
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700146 // get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700147 // defintion. direct methods come first, followed by virtual
148 // methods. note that runtime created methods such as miranda
149 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700150 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700151 ~OatClass();
152
153 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800154 OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155 mirror::Class::Status status,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800156 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700157
Brian Carlstrome24fa612011-09-29 00:53:55 -0700158 const OatFile* oat_file_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800159 const mirror::Class::Status status_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700160 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700161
162 friend class OatDexFile;
163 };
164
165 class OatDexFile {
166 public:
Brian Carlstrom89521892011-12-07 22:05:07 -0800167 const DexFile* OpenDexFile() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700168 const OatClass* GetOatClass(uint32_t class_def_index) const;
Ian Rogers05f28c62012-10-23 18:12:13 -0700169 size_t FileSize() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700170
171 const std::string& GetDexFileLocation() const {
172 return dex_file_location_;
173 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700174
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800175 uint32_t GetDexFileLocationChecksum() const {
176 return dex_file_location_checksum_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700177 }
178
Brian Carlstrome24fa612011-09-29 00:53:55 -0700179 ~OatDexFile();
Elliott Hughesa21039c2012-06-21 12:09:25 -0700180
Brian Carlstrome24fa612011-09-29 00:53:55 -0700181 private:
182 OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800183 const std::string& dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700184 uint32_t dex_file_checksum,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800185 const byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800186 const uint32_t* oat_class_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700187
188 const OatFile* oat_file_;
189 std::string dex_file_location_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800190 uint32_t dex_file_location_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800191 const byte* dex_file_pointer_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800192 const uint32_t* oat_class_offsets_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700193
194 friend class OatFile;
195 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
196 };
197
Ian Rogers7fe2c692011-12-06 16:35:59 -0800198 const OatDexFile* GetOatDexFile(const std::string& dex_file_location,
Ian Rogers33e95662013-05-20 20:29:14 -0700199 bool exception_if_not_found = true) const
200 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700201 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700202
Ian Rogers30fab402012-01-23 15:43:46 -0800203 size_t Size() const {
204 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700205 }
206
207 private:
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800208 static void CheckLocation(const std::string& location);
209
210 static OatFile* OpenDlopen(const std::string& elf_filename,
211 const std::string& location,
212 byte* requested_base);
213
214 static OatFile* OpenElfFile(File* file,
215 const std::string& location,
216 byte* requested_base,
217 bool writable);
218
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700219 explicit OatFile(const std::string& filename);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800220 bool Dlopen(const std::string& elf_filename, byte* requested_base);
221 bool ElfFileOpen(File* file, byte* requested_base, bool writable);
Brian Carlstromf1b30302013-03-28 10:35:32 -0700222 bool Setup();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700223
Ian Rogers30fab402012-01-23 15:43:46 -0800224 const byte* Begin() const;
225 const byte* End() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700226
227 // The oat file name.
228 //
229 // The image will embed this to link its associated oat file.
230 const std::string location_;
231
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800232 // Pointer to OatHeader.
233 const byte* begin_;
234
235 // Pointer to end of oat region for bounds checking.
236 const byte* end_;
237
238 // Backing memory map for oat file during when opened by ElfWriter during initial compilation.
Brian Carlstrome24fa612011-09-29 00:53:55 -0700239 UniquePtr<MemMap> mem_map_;
240
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800241 // Backing memory map for oat file during cross compilation.
242 UniquePtr<ElfFile> elf_file_;
243
244 // dlopen handle during runtime.
245 void* dlopen_handle_;
246
Elliott Hughesa0e18062012-04-13 15:59:59 -0700247 typedef SafeMap<std::string, const OatDexFile*> Table;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700248 Table oat_dex_files_;
249
250 friend class OatClass;
251 friend class OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800252 friend class OatDumper; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700253 DISALLOW_COPY_AND_ASSIGN(OatFile);
254};
255
256} // namespace art
257
258#endif // ART_SRC_OAT_WRITER_H_