blob: 9fcba2fde2336f56205c391fe45fddea386789e3 [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 Carlstrome24fa612011-09-29 00:53:55 -070040 byte* requested_base);
41
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,
45 byte* requested_base);
46
Brian Carlstrome24fa612011-09-29 00:53:55 -070047 ~OatFile();
48
49 const std::string& GetLocation() const {
50 return location_;
51 }
52
53 const OatHeader& GetOatHeader() const;
54
55 class OatDexFile;
56
Brian Carlstrom3320cf42011-10-04 14:58:28 -070057 class OatMethod {
58 public:
Brian Carlstromae826982011-11-09 01:33:42 -080059 // Link Method for execution using the contents of this OatMethod
60 void LinkMethodPointers(Method* method) const;
61
62 // Link Method for image writing using the contents of this OatMethod
63 void LinkMethodOffsets(Method* method) const;
64
65 uint32_t GetCodeOffset() const {
66 return code_offset_;
67 }
68 size_t GetFrameSizeInBytes() const {
69 return frame_size_in_bytes_;
70 }
71 uint32_t GetCoreSpillMask() const {
72 return core_spill_mask_;
73 }
74 uint32_t GetFpSpillMask() const {
75 return fp_spill_mask_;
76 }
77 uint32_t GetMappingTableOffset() const {
78 return mapping_table_offset_;
79 }
80 uint32_t GetVmapTableOffset() const {
81 return vmap_table_offset_;
82 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -080083 uint32_t GetGcMapOffset() const {
84 return gc_map_offset_;
85 }
Brian Carlstromae826982011-11-09 01:33:42 -080086 uint32_t GetInvokeStubOffset() const {
87 return invoke_stub_offset_;
88 }
89
90 const void* GetCode() const {
91 return GetOatPointer<const void*>(code_offset_);
92 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -070093 uint32_t GetCodeSize() const {
94 uintptr_t code = reinterpret_cast<uint32_t>(GetCode());
95 if (code == 0) {
96 return 0;
97 }
98 // TODO: make this Thumb2 specific
99 code &= ~0x1;
100 return reinterpret_cast<uint32_t*>(code)[-1];
101 }
Brian Carlstromae826982011-11-09 01:33:42 -0800102 const uint32_t* GetMappingTable() const {
103 return GetOatPointer<const uint32_t*>(mapping_table_offset_);
104 }
105 const uint16_t* GetVmapTable() const {
106 return GetOatPointer<const uint16_t*>(vmap_table_offset_);
107 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800108 const uint8_t* GetGcMap() const {
109 return GetOatPointer<const uint8_t*>(gc_map_offset_);
110 }
Brian Carlstromae826982011-11-09 01:33:42 -0800111 const Method::InvokeStub* GetInvokeStub() const {
112 return GetOatPointer<const Method::InvokeStub*>(invoke_stub_offset_);
113 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700114 uint32_t GetInvokeStubSize() const {
115 uintptr_t code = reinterpret_cast<uint32_t>(GetInvokeStub());
116 if (code == 0) {
117 return 0;
118 }
119 return reinterpret_cast<uint32_t*>(code)[-1];
120 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700121
122 ~OatMethod();
123
Brian Carlstromae826982011-11-09 01:33:42 -0800124 // Create an OatMethod with offsets relative to the given base address
125 OatMethod(const byte* base,
126 const uint32_t code_offset,
127 const size_t frame_size_in_bytes,
128 const uint32_t core_spill_mask,
129 const uint32_t fp_spill_mask,
130 const uint32_t mapping_table_offset,
131 const uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800132 const uint32_t gc_map_offset,
Brian Carlstromae826982011-11-09 01:33:42 -0800133 const uint32_t invoke_stub_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700134
Brian Carlstromae826982011-11-09 01:33:42 -0800135 private:
136 template<class T>
137 T GetOatPointer(uint32_t offset) const {
138 if (offset == 0) {
139 return NULL;
140 }
Ian Rogers30fab402012-01-23 15:43:46 -0800141 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800142 }
143
Ian Rogers30fab402012-01-23 15:43:46 -0800144 const byte* begin_;
Brian Carlstromae826982011-11-09 01:33:42 -0800145
146 uint32_t code_offset_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700147 size_t frame_size_in_bytes_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700148 uint32_t core_spill_mask_;
149 uint32_t fp_spill_mask_;
Brian Carlstromae826982011-11-09 01:33:42 -0800150 uint32_t mapping_table_offset_;
151 uint32_t vmap_table_offset_;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800152 uint32_t gc_map_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800153 uint32_t invoke_stub_offset_;
154
155 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700156 };
157
Brian Carlstrome24fa612011-09-29 00:53:55 -0700158 class OatClass {
159 public:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800160 Class::Status GetStatus() const;
161
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700162 // get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700163 // defintion. direct methods come first, followed by virtual
164 // methods. note that runtime created methods such as miranda
165 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700166 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700167 ~OatClass();
168
169 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800170 OatClass(const OatFile* oat_file,
171 Class::Status status,
172 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700173
Brian Carlstrome24fa612011-09-29 00:53:55 -0700174 const OatFile* oat_file_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800175 const Class::Status status_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700176 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700177
178 friend class OatDexFile;
179 };
180
181 class OatDexFile {
182 public:
Brian Carlstrom89521892011-12-07 22:05:07 -0800183 const DexFile* OpenDexFile() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700184 const OatClass* GetOatClass(uint32_t class_def_index) const;
185
186 const std::string& GetDexFileLocation() const {
187 return dex_file_location_;
188 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700189
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800190 uint32_t GetDexFileLocationChecksum() const {
191 return dex_file_location_checksum_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700192 }
193
Brian Carlstrome24fa612011-09-29 00:53:55 -0700194 ~OatDexFile();
195 private:
196 OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800197 const std::string& dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700198 uint32_t dex_file_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800199 byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800200 const uint32_t* oat_class_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700201
202 const OatFile* oat_file_;
203 std::string dex_file_location_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800204 uint32_t dex_file_location_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800205 const byte* dex_file_pointer_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800206 const uint32_t* oat_class_offsets_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700207
208 friend class OatFile;
209 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
210 };
211
Ian Rogers7fe2c692011-12-06 16:35:59 -0800212 const OatDexFile* GetOatDexFile(const std::string& dex_file_location,
213 bool warn_if_not_found = true) const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700214 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700215
Ian Rogers30fab402012-01-23 15:43:46 -0800216 size_t Size() const {
217 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700218 }
219
220 private:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700221 explicit OatFile(const std::string& filename);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800222 bool Read(File& file, byte* requested_base);
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
232 // backing memory map for oat file
233 UniquePtr<MemMap> mem_map_;
234
235 typedef std::map<std::string, const OatDexFile*> Table;
236 Table oat_dex_files_;
237
238 friend class OatClass;
239 friend class OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800240 friend class OatDumper; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700241 DISALLOW_COPY_AND_ASSIGN(OatFile);
242};
243
244} // namespace art
245
246#endif // ART_SRC_OAT_WRITER_H_