blob: 281ef78ec2c78f764d842f598f12bdb979e3cc3b [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
20#include <vector>
21
22#include "dex_file.h"
23#include "mem_map.h"
24#include "oat.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070025#include "object.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070026
27namespace art {
28
29class OatFile {
30 public:
31
Brian Carlstromb7bbba42011-10-13 14:58:47 -070032 // Returns an OatFile name based on a DexFile location
jeffhao262bf462011-10-20 18:36:32 -070033 static std::string DexFilenameToOatFilename(const std::string& location);
Brian Carlstromb7bbba42011-10-13 14:58:47 -070034
Brian Carlstrome24fa612011-09-29 00:53:55 -070035 // Open an oat file. Returns NULL on failure. Requested base can
36 // optionally be used to request where the file should be loaded.
37 static OatFile* Open(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -080038 const std::string& location,
Brian Carlstromf5822582012-03-19 22:34:31 -070039 byte* requested_base,
Elliott Hughesb25c3f62012-03-26 16:35:06 -070040 bool writable = false);
Brian Carlstrome24fa612011-09-29 00:53:55 -070041
Brian Carlstrom5b332c82012-02-01 15:02:31 -080042 // Open an oat file from an already opened File with the given location.
43 static OatFile* Open(File& file,
44 const std::string& location,
Brian Carlstromf5822582012-03-19 22:34:31 -070045 byte* requested_base,
Elliott Hughesb25c3f62012-03-26 16:35:06 -070046 bool writable = false);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080047
Brian Carlstrome24fa612011-09-29 00:53:55 -070048 ~OatFile();
49
50 const std::string& GetLocation() const {
51 return location_;
52 }
53
54 const OatHeader& GetOatHeader() const;
55
56 class OatDexFile;
57
Brian Carlstrom3320cf42011-10-04 14:58:28 -070058 class OatMethod {
59 public:
Brian Carlstromae826982011-11-09 01:33:42 -080060 // Link Method for execution using the contents of this OatMethod
61 void LinkMethodPointers(Method* method) const;
62
63 // Link Method for image writing using the contents of this OatMethod
64 void LinkMethodOffsets(Method* method) const;
65
66 uint32_t GetCodeOffset() const {
67 return code_offset_;
68 }
69 size_t GetFrameSizeInBytes() const {
70 return frame_size_in_bytes_;
71 }
72 uint32_t GetCoreSpillMask() const {
73 return core_spill_mask_;
74 }
75 uint32_t GetFpSpillMask() const {
76 return fp_spill_mask_;
77 }
78 uint32_t GetMappingTableOffset() const {
79 return mapping_table_offset_;
80 }
81 uint32_t GetVmapTableOffset() const {
82 return vmap_table_offset_;
83 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -080084 uint32_t GetGcMapOffset() const {
85 return gc_map_offset_;
86 }
Brian Carlstromae826982011-11-09 01:33:42 -080087 uint32_t GetInvokeStubOffset() const {
88 return invoke_stub_offset_;
89 }
90
91 const void* GetCode() const {
92 return GetOatPointer<const void*>(code_offset_);
93 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -070094 uint32_t GetCodeSize() const {
95 uintptr_t code = reinterpret_cast<uint32_t>(GetCode());
96 if (code == 0) {
97 return 0;
98 }
99 // TODO: make this Thumb2 specific
100 code &= ~0x1;
101 return reinterpret_cast<uint32_t*>(code)[-1];
102 }
Brian Carlstromae826982011-11-09 01:33:42 -0800103 const uint32_t* GetMappingTable() const {
104 return GetOatPointer<const uint32_t*>(mapping_table_offset_);
105 }
106 const uint16_t* GetVmapTable() const {
107 return GetOatPointer<const uint16_t*>(vmap_table_offset_);
108 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800109 const uint8_t* GetGcMap() const {
110 return GetOatPointer<const uint8_t*>(gc_map_offset_);
111 }
Brian Carlstromae826982011-11-09 01:33:42 -0800112 const Method::InvokeStub* GetInvokeStub() const {
113 return GetOatPointer<const Method::InvokeStub*>(invoke_stub_offset_);
114 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700115 uint32_t GetInvokeStubSize() const {
116 uintptr_t code = reinterpret_cast<uint32_t>(GetInvokeStub());
117 if (code == 0) {
118 return 0;
119 }
120 return reinterpret_cast<uint32_t*>(code)[-1];
121 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700122
123 ~OatMethod();
124
Brian Carlstromae826982011-11-09 01:33:42 -0800125 // Create an OatMethod with offsets relative to the given base address
126 OatMethod(const byte* base,
127 const uint32_t code_offset,
128 const size_t frame_size_in_bytes,
129 const uint32_t core_spill_mask,
130 const uint32_t fp_spill_mask,
131 const uint32_t mapping_table_offset,
132 const uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800133 const uint32_t gc_map_offset,
Brian Carlstromae826982011-11-09 01:33:42 -0800134 const uint32_t invoke_stub_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700135
Brian Carlstromae826982011-11-09 01:33:42 -0800136 private:
137 template<class T>
138 T GetOatPointer(uint32_t offset) const {
139 if (offset == 0) {
140 return NULL;
141 }
Ian Rogers30fab402012-01-23 15:43:46 -0800142 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800143 }
144
Ian Rogers30fab402012-01-23 15:43:46 -0800145 const byte* begin_;
Brian Carlstromae826982011-11-09 01:33:42 -0800146
147 uint32_t code_offset_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700148 size_t frame_size_in_bytes_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700149 uint32_t core_spill_mask_;
150 uint32_t fp_spill_mask_;
Brian Carlstromae826982011-11-09 01:33:42 -0800151 uint32_t mapping_table_offset_;
152 uint32_t vmap_table_offset_;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800153 uint32_t gc_map_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800154 uint32_t invoke_stub_offset_;
155
156 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700157 };
158
Brian Carlstrome24fa612011-09-29 00:53:55 -0700159 class OatClass {
160 public:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800161 Class::Status GetStatus() const;
162
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700163 // get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700164 // defintion. direct methods come first, followed by virtual
165 // methods. note that runtime created methods such as miranda
166 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700167 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700168 ~OatClass();
169
170 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800171 OatClass(const OatFile* oat_file,
172 Class::Status status,
173 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700174
Brian Carlstrome24fa612011-09-29 00:53:55 -0700175 const OatFile* oat_file_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800176 const Class::Status status_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700177 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700178
179 friend class OatDexFile;
180 };
181
182 class OatDexFile {
183 public:
Brian Carlstrom89521892011-12-07 22:05:07 -0800184 const DexFile* OpenDexFile() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700185 const OatClass* GetOatClass(uint32_t class_def_index) const;
186
187 const std::string& GetDexFileLocation() const {
188 return dex_file_location_;
189 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700190
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800191 uint32_t GetDexFileLocationChecksum() const {
192 return dex_file_location_checksum_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700193 }
194
Brian Carlstrome24fa612011-09-29 00:53:55 -0700195 ~OatDexFile();
196 private:
197 OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800198 const std::string& dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700199 uint32_t dex_file_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800200 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 const OatFile* oat_file_;
204 std::string dex_file_location_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800205 uint32_t dex_file_location_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800206 const byte* dex_file_pointer_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800207 const uint32_t* oat_class_offsets_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700208
209 friend class OatFile;
210 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
211 };
212
Logan Chien0cc6ab62012-03-20 22:57:52 +0800213 class OatElfImage {
214 public:
215 const byte* begin() const {
216 return elf_addr_;
217 }
218
219 const byte* end() const {
220 return (elf_addr_ + elf_size_);
221 }
222
223 size_t size() const {
224 return elf_size_;
225 }
226
227 private:
228 OatElfImage(const OatFile* oat_file, const byte* addr, uint32_t size);
229
230 const OatFile* oat_file_;
231 const byte* elf_addr_;
232 uint32_t elf_size_;
233
234 friend class OatFile;
235 DISALLOW_COPY_AND_ASSIGN(OatElfImage);
236 };
237
Ian Rogers7fe2c692011-12-06 16:35:59 -0800238 const OatDexFile* GetOatDexFile(const std::string& dex_file_location,
239 bool warn_if_not_found = true) const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700240 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700241
Logan Chien0cc6ab62012-03-20 22:57:52 +0800242 const OatElfImage* GetOatElfImage(size_t i) const {
243 return oat_elf_images_[i];
244 }
245
Ian Rogers30fab402012-01-23 15:43:46 -0800246 size_t Size() const {
247 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700248 }
249
250 private:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700251 explicit OatFile(const std::string& filename);
Brian Carlstromf5822582012-03-19 22:34:31 -0700252 bool Map(File& file, byte* requested_base, bool writable);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700253
Ian Rogers30fab402012-01-23 15:43:46 -0800254 const byte* Begin() const;
255 const byte* End() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700256
257 // The oat file name.
258 //
259 // The image will embed this to link its associated oat file.
260 const std::string location_;
261
262 // backing memory map for oat file
263 UniquePtr<MemMap> mem_map_;
264
265 typedef std::map<std::string, const OatDexFile*> Table;
266 Table oat_dex_files_;
267
Logan Chien0cc6ab62012-03-20 22:57:52 +0800268 std::vector<OatElfImage*> oat_elf_images_;
269
Brian Carlstrome24fa612011-09-29 00:53:55 -0700270 friend class OatClass;
271 friend class OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800272 friend class OatDumper; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700273 DISALLOW_COPY_AND_ASSIGN(OatFile);
274};
275
276} // namespace art
277
278#endif // ART_SRC_OAT_WRITER_H_