blob: d75c7c7e6f3712bb8fc57e23800676f8a41c8834 [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
Brian Carlstrom3320cf42011-10-04 14:58:28 -070022#include "constants.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070023#include "dex_file.h"
24#include "mem_map.h"
25#include "oat.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070026#include "object.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070027
28namespace art {
29
30class OatFile {
31 public:
32
Brian Carlstromb7bbba42011-10-13 14:58:47 -070033 // Returns an OatFile name based on a DexFile location
jeffhao262bf462011-10-20 18:36:32 -070034 static std::string DexFilenameToOatFilename(const std::string& location);
Brian Carlstromb7bbba42011-10-13 14:58:47 -070035
Brian Carlstrome24fa612011-09-29 00:53:55 -070036 // Open an oat file. Returns NULL on failure. Requested base can
37 // optionally be used to request where the file should be loaded.
38 static OatFile* Open(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -080039 const std::string& location,
Brian Carlstromf5822582012-03-19 22:34:31 -070040 byte* requested_base,
41 bool writable=false);
Brian Carlstrome24fa612011-09-29 00:53:55 -070042
Brian Carlstrom5b332c82012-02-01 15:02:31 -080043 // Open an oat file from an already opened File with the given location.
44 static OatFile* Open(File& file,
45 const std::string& location,
Brian Carlstromf5822582012-03-19 22:34:31 -070046 byte* requested_base,
47 bool writable=false);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080048
Brian Carlstrome24fa612011-09-29 00:53:55 -070049 ~OatFile();
50
51 const std::string& GetLocation() const {
52 return location_;
53 }
54
55 const OatHeader& GetOatHeader() const;
56
57 class OatDexFile;
58
Brian Carlstrom3320cf42011-10-04 14:58:28 -070059 class OatMethod {
60 public:
Brian Carlstromae826982011-11-09 01:33:42 -080061 // Link Method for execution using the contents of this OatMethod
62 void LinkMethodPointers(Method* method) const;
63
64 // Link Method for image writing using the contents of this OatMethod
65 void LinkMethodOffsets(Method* method) const;
66
67 uint32_t GetCodeOffset() const {
68 return code_offset_;
69 }
70 size_t GetFrameSizeInBytes() const {
71 return frame_size_in_bytes_;
72 }
73 uint32_t GetCoreSpillMask() const {
74 return core_spill_mask_;
75 }
76 uint32_t GetFpSpillMask() const {
77 return fp_spill_mask_;
78 }
79 uint32_t GetMappingTableOffset() const {
80 return mapping_table_offset_;
81 }
82 uint32_t GetVmapTableOffset() const {
83 return vmap_table_offset_;
84 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -080085 uint32_t GetGcMapOffset() const {
86 return gc_map_offset_;
87 }
Brian Carlstromae826982011-11-09 01:33:42 -080088 uint32_t GetInvokeStubOffset() const {
89 return invoke_stub_offset_;
90 }
91
92 const void* GetCode() const {
93 return GetOatPointer<const void*>(code_offset_);
94 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -070095 uint32_t GetCodeSize() const {
96 uintptr_t code = reinterpret_cast<uint32_t>(GetCode());
97 if (code == 0) {
98 return 0;
99 }
100 // TODO: make this Thumb2 specific
101 code &= ~0x1;
102 return reinterpret_cast<uint32_t*>(code)[-1];
103 }
Brian Carlstromae826982011-11-09 01:33:42 -0800104 const uint32_t* GetMappingTable() const {
105 return GetOatPointer<const uint32_t*>(mapping_table_offset_);
106 }
107 const uint16_t* GetVmapTable() const {
108 return GetOatPointer<const uint16_t*>(vmap_table_offset_);
109 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800110 const uint8_t* GetGcMap() const {
111 return GetOatPointer<const uint8_t*>(gc_map_offset_);
112 }
Brian Carlstromae826982011-11-09 01:33:42 -0800113 const Method::InvokeStub* GetInvokeStub() const {
114 return GetOatPointer<const Method::InvokeStub*>(invoke_stub_offset_);
115 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700116 uint32_t GetInvokeStubSize() const {
117 uintptr_t code = reinterpret_cast<uint32_t>(GetInvokeStub());
118 if (code == 0) {
119 return 0;
120 }
121 return reinterpret_cast<uint32_t*>(code)[-1];
122 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700123
124 ~OatMethod();
125
Brian Carlstromae826982011-11-09 01:33:42 -0800126 // Create an OatMethod with offsets relative to the given base address
127 OatMethod(const byte* base,
128 const uint32_t code_offset,
129 const size_t frame_size_in_bytes,
130 const uint32_t core_spill_mask,
131 const uint32_t fp_spill_mask,
132 const uint32_t mapping_table_offset,
133 const uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800134 const uint32_t gc_map_offset,
Brian Carlstromae826982011-11-09 01:33:42 -0800135 const uint32_t invoke_stub_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700136
Brian Carlstromae826982011-11-09 01:33:42 -0800137 private:
138 template<class T>
139 T GetOatPointer(uint32_t offset) const {
140 if (offset == 0) {
141 return NULL;
142 }
Ian Rogers30fab402012-01-23 15:43:46 -0800143 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800144 }
145
Ian Rogers30fab402012-01-23 15:43:46 -0800146 const byte* begin_;
Brian Carlstromae826982011-11-09 01:33:42 -0800147
148 uint32_t code_offset_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700149 size_t frame_size_in_bytes_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700150 uint32_t core_spill_mask_;
151 uint32_t fp_spill_mask_;
Brian Carlstromae826982011-11-09 01:33:42 -0800152 uint32_t mapping_table_offset_;
153 uint32_t vmap_table_offset_;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800154 uint32_t gc_map_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800155 uint32_t invoke_stub_offset_;
156
157 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700158 };
159
Brian Carlstrome24fa612011-09-29 00:53:55 -0700160 class OatClass {
161 public:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800162 Class::Status GetStatus() const;
163
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700164 // get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700165 // defintion. direct methods come first, followed by virtual
166 // methods. note that runtime created methods such as miranda
167 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700168 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700169 ~OatClass();
170
171 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800172 OatClass(const OatFile* oat_file,
173 Class::Status status,
174 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700175
Brian Carlstrome24fa612011-09-29 00:53:55 -0700176 const OatFile* oat_file_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800177 const Class::Status status_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700178 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700179
180 friend class OatDexFile;
181 };
182
183 class OatDexFile {
184 public:
Brian Carlstrom89521892011-12-07 22:05:07 -0800185 const DexFile* OpenDexFile() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700186 const OatClass* GetOatClass(uint32_t class_def_index) const;
187
188 const std::string& GetDexFileLocation() const {
189 return dex_file_location_;
190 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700191
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800192 uint32_t GetDexFileLocationChecksum() const {
193 return dex_file_location_checksum_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700194 }
195
Brian Carlstrome24fa612011-09-29 00:53:55 -0700196 ~OatDexFile();
197 private:
198 OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800199 const std::string& dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700200 uint32_t dex_file_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800201 byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800202 const uint32_t* oat_class_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700203
204 const OatFile* oat_file_;
205 std::string dex_file_location_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800206 uint32_t dex_file_location_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800207 const byte* dex_file_pointer_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800208 const uint32_t* oat_class_offsets_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700209
210 friend class OatFile;
211 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
212 };
213
Ian Rogers7fe2c692011-12-06 16:35:59 -0800214 const OatDexFile* GetOatDexFile(const std::string& dex_file_location,
215 bool warn_if_not_found = true) const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700216 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700217
Ian Rogers30fab402012-01-23 15:43:46 -0800218 size_t Size() const {
219 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700220 }
221
222 private:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700223 explicit OatFile(const std::string& filename);
Brian Carlstromf5822582012-03-19 22:34:31 -0700224 bool Map(File& file, byte* requested_base, bool writable);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700225
Ian Rogers30fab402012-01-23 15:43:46 -0800226 const byte* Begin() const;
227 const byte* End() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700228
229 // The oat file name.
230 //
231 // The image will embed this to link its associated oat file.
232 const std::string location_;
233
234 // backing memory map for oat file
235 UniquePtr<MemMap> mem_map_;
236
237 typedef std::map<std::string, const OatDexFile*> Table;
238 Table oat_dex_files_;
239
240 friend class OatClass;
241 friend class OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800242 friend class OatDumper; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700243 DISALLOW_COPY_AND_ASSIGN(OatFile);
244};
245
246} // namespace art
247
248#endif // ART_SRC_OAT_WRITER_H_