blob: 4740ae77ab6503d32f5d3f5af52a41699e524b84 [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,
39 const std::string& strip_location_prefix,
40 byte* requested_base);
41
42 ~OatFile();
43
44 const std::string& GetLocation() const {
45 return location_;
46 }
47
48 const OatHeader& GetOatHeader() const;
49
50 class OatDexFile;
51
Brian Carlstrom3320cf42011-10-04 14:58:28 -070052 class OatMethod {
53 public:
Brian Carlstromae826982011-11-09 01:33:42 -080054 // Link Method for execution using the contents of this OatMethod
55 void LinkMethodPointers(Method* method) const;
56
57 // Link Method for image writing using the contents of this OatMethod
58 void LinkMethodOffsets(Method* method) const;
59
60 uint32_t GetCodeOffset() const {
61 return code_offset_;
62 }
63 size_t GetFrameSizeInBytes() const {
64 return frame_size_in_bytes_;
65 }
66 uint32_t GetCoreSpillMask() const {
67 return core_spill_mask_;
68 }
69 uint32_t GetFpSpillMask() const {
70 return fp_spill_mask_;
71 }
72 uint32_t GetMappingTableOffset() const {
73 return mapping_table_offset_;
74 }
75 uint32_t GetVmapTableOffset() const {
76 return vmap_table_offset_;
77 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -080078 uint32_t GetGcMapOffset() const {
79 return gc_map_offset_;
80 }
Brian Carlstromae826982011-11-09 01:33:42 -080081 uint32_t GetInvokeStubOffset() const {
82 return invoke_stub_offset_;
83 }
84
85 const void* GetCode() const {
86 return GetOatPointer<const void*>(code_offset_);
87 }
88 const uint32_t* GetMappingTable() const {
89 return GetOatPointer<const uint32_t*>(mapping_table_offset_);
90 }
91 const uint16_t* GetVmapTable() const {
92 return GetOatPointer<const uint16_t*>(vmap_table_offset_);
93 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -080094 const uint8_t* GetGcMap() const {
95 return GetOatPointer<const uint8_t*>(gc_map_offset_);
96 }
Brian Carlstromae826982011-11-09 01:33:42 -080097 const Method::InvokeStub* GetInvokeStub() const {
98 return GetOatPointer<const Method::InvokeStub*>(invoke_stub_offset_);
99 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700100
101 ~OatMethod();
102
Brian Carlstromae826982011-11-09 01:33:42 -0800103 // Create an OatMethod with offsets relative to the given base address
104 OatMethod(const byte* base,
105 const uint32_t code_offset,
106 const size_t frame_size_in_bytes,
107 const uint32_t core_spill_mask,
108 const uint32_t fp_spill_mask,
109 const uint32_t mapping_table_offset,
110 const uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800111 const uint32_t gc_map_offset,
Brian Carlstromae826982011-11-09 01:33:42 -0800112 const uint32_t invoke_stub_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700113
Brian Carlstromae826982011-11-09 01:33:42 -0800114 private:
115 template<class T>
116 T GetOatPointer(uint32_t offset) const {
117 if (offset == 0) {
118 return NULL;
119 }
Ian Rogers30fab402012-01-23 15:43:46 -0800120 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800121 }
122
Ian Rogers30fab402012-01-23 15:43:46 -0800123 const byte* begin_;
Brian Carlstromae826982011-11-09 01:33:42 -0800124
125 uint32_t code_offset_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700126 size_t frame_size_in_bytes_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700127 uint32_t core_spill_mask_;
128 uint32_t fp_spill_mask_;
Brian Carlstromae826982011-11-09 01:33:42 -0800129 uint32_t mapping_table_offset_;
130 uint32_t vmap_table_offset_;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800131 uint32_t gc_map_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800132 uint32_t invoke_stub_offset_;
133
134 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700135 };
136
Brian Carlstrome24fa612011-09-29 00:53:55 -0700137 class OatClass {
138 public:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800139 Class::Status GetStatus() const;
140
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700141 // get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700142 // defintion. direct methods come first, followed by virtual
143 // methods. note that runtime created methods such as miranda
144 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700145 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700146 ~OatClass();
147
148 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800149 OatClass(const OatFile* oat_file,
150 Class::Status status,
151 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700152
Brian Carlstrome24fa612011-09-29 00:53:55 -0700153 const OatFile* oat_file_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800154 const Class::Status status_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700155 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700156
157 friend class OatDexFile;
158 };
159
160 class OatDexFile {
161 public:
Brian Carlstrom89521892011-12-07 22:05:07 -0800162 const DexFile* OpenDexFile() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700163 const OatClass* GetOatClass(uint32_t class_def_index) const;
164
165 const std::string& GetDexFileLocation() const {
166 return dex_file_location_;
167 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700168
169 uint32_t GetDexFileChecksum() const {
170 return dex_file_checksum_;
171 }
172
Brian Carlstrome24fa612011-09-29 00:53:55 -0700173 ~OatDexFile();
174 private:
175 OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800176 const std::string& dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700177 uint32_t dex_file_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800178 byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800179 const uint32_t* oat_class_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700180
181 const OatFile* oat_file_;
182 std::string dex_file_location_;
183 uint32_t dex_file_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800184 const byte* dex_file_pointer_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800185 const uint32_t* oat_class_offsets_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700186
187 friend class OatFile;
188 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
189 };
190
Ian Rogers7fe2c692011-12-06 16:35:59 -0800191 const OatDexFile* GetOatDexFile(const std::string& dex_file_location,
192 bool warn_if_not_found = true) const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700193 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700194
Ian Rogers30fab402012-01-23 15:43:46 -0800195 size_t Size() const {
196 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700197 }
198
199 private:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700200 explicit OatFile(const std::string& filename);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700201 bool Read(const std::string& filename, byte* requested_base);
202
Ian Rogers30fab402012-01-23 15:43:46 -0800203 const byte* Begin() const;
204 const byte* End() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700205
206 // The oat file name.
207 //
208 // The image will embed this to link its associated oat file.
209 const std::string location_;
210
211 // backing memory map for oat file
212 UniquePtr<MemMap> mem_map_;
213
214 typedef std::map<std::string, const OatDexFile*> Table;
215 Table oat_dex_files_;
216
217 friend class OatClass;
218 friend class OatDexFile;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700219 friend class OatDump; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700220 DISALLOW_COPY_AND_ASSIGN(OatFile);
221};
222
223} // namespace art
224
225#endif // ART_SRC_OAT_WRITER_H_