blob: 91612302b78de833ae8d00d639d58b5b54f4362d [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"
Logan Chien0c717dd2012-03-28 18:31:07 +080023#include "invoke_type.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070024#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:
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
Mathieu Chartier66f19252012-09-18 08:57:04 -070061 void LinkMethodPointers(AbstractMethod* method) const;
Brian Carlstromae826982011-11-09 01:33:42 -080062
63 // Link Method for image writing using the contents of this OatMethod
Mathieu Chartier66f19252012-09-18 08:57:04 -070064 void LinkMethodOffsets(AbstractMethod* method) const;
Brian Carlstromae826982011-11-09 01:33:42 -080065
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 }
Ian Rogers0c7abda2012-09-19 13:33:42 -070084 uint32_t GetNativeGcMapOffset() const {
85 return native_gc_map_offset_;
Brian Carlstrome7d856b2012-01-11 18:10:55 -080086 }
Brian Carlstromae826982011-11-09 01:33:42 -080087 uint32_t GetInvokeStubOffset() const {
88 return invoke_stub_offset_;
89 }
90
Logan Chien0c717dd2012-03-28 18:31:07 +080091 const void* GetCode() const;
92 uint32_t GetCodeSize() const;
93
Brian Carlstromae826982011-11-09 01:33:42 -080094 const uint32_t* GetMappingTable() const {
95 return GetOatPointer<const uint32_t*>(mapping_table_offset_);
96 }
97 const uint16_t* GetVmapTable() const {
98 return GetOatPointer<const uint16_t*>(vmap_table_offset_);
99 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700100 const uint8_t* GetNativeGcMap() const {
101 return GetOatPointer<const uint8_t*>(native_gc_map_offset_);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800102 }
Logan Chien0c717dd2012-03-28 18:31:07 +0800103
Mathieu Chartier66f19252012-09-18 08:57:04 -0700104 AbstractMethod::InvokeStub* GetInvokeStub() const;
Logan Chien0c717dd2012-03-28 18:31:07 +0800105 uint32_t GetInvokeStubSize() const;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700106
TDYa127eead4ac2012-06-03 07:15:25 -0700107#if defined(ART_USE_LLVM_COMPILER)
108 const void* GetProxyStub() const;
109#endif
110
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700111 ~OatMethod();
112
Brian Carlstromae826982011-11-09 01:33:42 -0800113 // Create an OatMethod with offsets relative to the given base address
114 OatMethod(const byte* base,
115 const uint32_t code_offset,
116 const size_t frame_size_in_bytes,
117 const uint32_t core_spill_mask,
118 const uint32_t fp_spill_mask,
119 const uint32_t mapping_table_offset,
120 const uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800121 const uint32_t gc_map_offset,
Logan Chien0c717dd2012-03-28 18:31:07 +0800122 const uint32_t invoke_stub_offset
123#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800124 , const uint32_t proxy_stub_offset
Logan Chien0c717dd2012-03-28 18:31:07 +0800125#endif
126 );
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700127
Brian Carlstromae826982011-11-09 01:33:42 -0800128 private:
129 template<class T>
130 T GetOatPointer(uint32_t offset) const {
131 if (offset == 0) {
132 return NULL;
133 }
Ian Rogers30fab402012-01-23 15:43:46 -0800134 return reinterpret_cast<T>(begin_ + offset);
Brian Carlstromae826982011-11-09 01:33:42 -0800135 }
136
Ian Rogers30fab402012-01-23 15:43:46 -0800137 const byte* begin_;
Brian Carlstromae826982011-11-09 01:33:42 -0800138
139 uint32_t code_offset_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700140 size_t frame_size_in_bytes_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700141 uint32_t core_spill_mask_;
142 uint32_t fp_spill_mask_;
Brian Carlstromae826982011-11-09 01:33:42 -0800143 uint32_t mapping_table_offset_;
144 uint32_t vmap_table_offset_;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700145 uint32_t native_gc_map_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800146 uint32_t invoke_stub_offset_;
147
Logan Chien0c717dd2012-03-28 18:31:07 +0800148#if defined(ART_USE_LLVM_COMPILER)
Logan Chien971bf3f2012-05-01 15:47:55 +0800149 uint32_t proxy_stub_offset_;
Logan Chien0c717dd2012-03-28 18:31:07 +0800150#endif
151
Brian Carlstromae826982011-11-09 01:33:42 -0800152 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700153 };
154
Brian Carlstrome24fa612011-09-29 00:53:55 -0700155 class OatClass {
156 public:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800157 Class::Status GetStatus() const;
158
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700159 // get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700160 // defintion. direct methods come first, followed by virtual
161 // methods. note that runtime created methods such as miranda
162 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700163 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700164 ~OatClass();
165
166 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800167 OatClass(const OatFile* oat_file,
168 Class::Status status,
169 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700170
Brian Carlstrome24fa612011-09-29 00:53:55 -0700171 const OatFile* oat_file_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800172 const Class::Status status_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700173 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700174
175 friend class OatDexFile;
176 };
177
178 class OatDexFile {
179 public:
Brian Carlstrom89521892011-12-07 22:05:07 -0800180 const DexFile* OpenDexFile() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700181 const OatClass* GetOatClass(uint32_t class_def_index) const;
Ian Rogers05f28c62012-10-23 18:12:13 -0700182 size_t FileSize() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700183
184 const std::string& GetDexFileLocation() const {
185 return dex_file_location_;
186 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700187
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800188 uint32_t GetDexFileLocationChecksum() const {
189 return dex_file_location_checksum_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700190 }
191
Brian Carlstrome24fa612011-09-29 00:53:55 -0700192 ~OatDexFile();
Elliott Hughesa21039c2012-06-21 12:09:25 -0700193
Brian Carlstrome24fa612011-09-29 00:53:55 -0700194 private:
195 OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800196 const std::string& dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700197 uint32_t dex_file_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800198 byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800199 const uint32_t* oat_class_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700200
201 const OatFile* oat_file_;
202 std::string dex_file_location_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800203 uint32_t dex_file_location_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800204 const byte* dex_file_pointer_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800205 const uint32_t* oat_class_offsets_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700206
207 friend class OatFile;
208 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
209 };
210
Ian Rogers7fe2c692011-12-06 16:35:59 -0800211 const OatDexFile* GetOatDexFile(const std::string& dex_file_location,
212 bool warn_if_not_found = true) const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700213 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700214
Ian Rogers30fab402012-01-23 15:43:46 -0800215 size_t Size() const {
216 return End() - Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700217 }
218
219 private:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700220 explicit OatFile(const std::string& filename);
Brian Carlstrom1cac3432012-12-12 10:56:22 -0800221 bool Map(File& file, byte* requested_base, bool writable);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700222
Ian Rogers30fab402012-01-23 15:43:46 -0800223 const byte* Begin() const;
224 const byte* End() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700225
226 // The oat file name.
227 //
228 // The image will embed this to link its associated oat file.
229 const std::string location_;
230
231 // backing memory map for oat file
232 UniquePtr<MemMap> mem_map_;
233
Elliott Hughesa0e18062012-04-13 15:59:59 -0700234 typedef SafeMap<std::string, const OatDexFile*> Table;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700235 Table oat_dex_files_;
236
237 friend class OatClass;
238 friend class OatDexFile;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800239 friend class OatDumper; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700240 DISALLOW_COPY_AND_ASSIGN(OatFile);
241};
242
243} // namespace art
244
245#endif // ART_SRC_OAT_WRITER_H_