blob: 0e22719eb85d95835fcb7da41be58bc9ad12e592 [file] [log] [blame]
Brian Carlstrome24fa612011-09-29 00:53:55 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_OAT_FILE_H_
4#define ART_SRC_OAT_FILE_H_
5
6#include <vector>
7
Brian Carlstrom3320cf42011-10-04 14:58:28 -07008#include "constants.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -07009#include "dex_file.h"
10#include "mem_map.h"
11#include "oat.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070012#include "object.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070013
14namespace art {
15
16class OatFile {
17 public:
18
Brian Carlstromb7bbba42011-10-13 14:58:47 -070019 // Returns an OatFile name based on a DexFile location
jeffhao262bf462011-10-20 18:36:32 -070020 static std::string DexFilenameToOatFilename(const std::string& location);
Brian Carlstromb7bbba42011-10-13 14:58:47 -070021
Brian Carlstrome24fa612011-09-29 00:53:55 -070022 // Open an oat file. Returns NULL on failure. Requested base can
23 // optionally be used to request where the file should be loaded.
24 static OatFile* Open(const std::string& filename,
25 const std::string& strip_location_prefix,
26 byte* requested_base);
27
28 ~OatFile();
29
30 const std::string& GetLocation() const {
31 return location_;
32 }
33
34 const OatHeader& GetOatHeader() const;
35
36 class OatDexFile;
37
Brian Carlstrom3320cf42011-10-04 14:58:28 -070038 class OatMethod {
39 public:
Brian Carlstromae826982011-11-09 01:33:42 -080040 // Link Method for execution using the contents of this OatMethod
41 void LinkMethodPointers(Method* method) const;
42
43 // Link Method for image writing using the contents of this OatMethod
44 void LinkMethodOffsets(Method* method) const;
45
46 uint32_t GetCodeOffset() const {
47 return code_offset_;
48 }
49 size_t GetFrameSizeInBytes() const {
50 return frame_size_in_bytes_;
51 }
52 uint32_t GetCoreSpillMask() const {
53 return core_spill_mask_;
54 }
55 uint32_t GetFpSpillMask() const {
56 return fp_spill_mask_;
57 }
58 uint32_t GetMappingTableOffset() const {
59 return mapping_table_offset_;
60 }
61 uint32_t GetVmapTableOffset() const {
62 return vmap_table_offset_;
63 }
64 uint32_t GetInvokeStubOffset() const {
65 return invoke_stub_offset_;
66 }
67
68 const void* GetCode() const {
69 return GetOatPointer<const void*>(code_offset_);
70 }
71 const uint32_t* GetMappingTable() const {
72 return GetOatPointer<const uint32_t*>(mapping_table_offset_);
73 }
74 const uint16_t* GetVmapTable() const {
75 return GetOatPointer<const uint16_t*>(vmap_table_offset_);
76 }
77 const Method::InvokeStub* GetInvokeStub() const {
78 return GetOatPointer<const Method::InvokeStub*>(invoke_stub_offset_);
79 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -070080
81 ~OatMethod();
82
Brian Carlstromae826982011-11-09 01:33:42 -080083 // Create an OatMethod with offsets relative to the given base address
84 OatMethod(const byte* base,
85 const uint32_t code_offset,
86 const size_t frame_size_in_bytes,
87 const uint32_t core_spill_mask,
88 const uint32_t fp_spill_mask,
89 const uint32_t mapping_table_offset,
90 const uint32_t vmap_table_offset,
91 const uint32_t invoke_stub_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070092
Brian Carlstromae826982011-11-09 01:33:42 -080093 private:
94 template<class T>
95 T GetOatPointer(uint32_t offset) const {
96 if (offset == 0) {
97 return NULL;
98 }
99 return reinterpret_cast<T>(base_ + offset);
100 }
101
102 const byte* base_;
103
104 uint32_t code_offset_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700105 size_t frame_size_in_bytes_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700106 uint32_t core_spill_mask_;
107 uint32_t fp_spill_mask_;
Brian Carlstromae826982011-11-09 01:33:42 -0800108 uint32_t mapping_table_offset_;
109 uint32_t vmap_table_offset_;
110 uint32_t invoke_stub_offset_;
111
112 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700113 };
114
Brian Carlstrome24fa612011-09-29 00:53:55 -0700115 class OatClass {
116 public:
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700117 // get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700118 // defintion. direct methods come first, followed by virtual
119 // methods. note that runtime created methods such as miranda
120 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700121 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700122 ~OatClass();
123
124 private:
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700125 OatClass(const OatFile* oat_file, const OatMethodOffsets* methods_pointer);
126
Brian Carlstrome24fa612011-09-29 00:53:55 -0700127 const OatFile* oat_file_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700128 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700129
130 friend class OatDexFile;
131 };
132
133 class OatDexFile {
134 public:
Brian Carlstromaded5f72011-10-07 17:15:04 -0700135 const OatClass* GetOatClass(uint32_t class_def_index) const;
136
137 const std::string& GetDexFileLocation() const {
138 return dex_file_location_;
139 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700140
141 uint32_t GetDexFileChecksum() const {
142 return dex_file_checksum_;
143 }
144
Brian Carlstrome24fa612011-09-29 00:53:55 -0700145 ~OatDexFile();
146 private:
147 OatDexFile(const OatFile* oat_file,
148 std::string dex_file_location,
149 uint32_t dex_file_checksum,
150 uint32_t* classes_pointer);
151
152 const OatFile* oat_file_;
153 std::string dex_file_location_;
154 uint32_t dex_file_checksum_;
155 const uint32_t* classes_pointer_;
156
157 friend class OatFile;
158 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
159 };
160
Brian Carlstromaded5f72011-10-07 17:15:04 -0700161 const OatDexFile* GetOatDexFile(const std::string& dex_file_location) const;
162 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700163
164 size_t GetSize() const {
165 return GetLimit() - GetBase();
166 }
167
168 private:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700169 explicit OatFile(const std::string& filename);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700170 bool Read(const std::string& filename, byte* requested_base);
171
172 const byte* GetBase() const;
173 const byte* GetLimit() const;
174
175 // The oat file name.
176 //
177 // The image will embed this to link its associated oat file.
178 const std::string location_;
179
180 // backing memory map for oat file
181 UniquePtr<MemMap> mem_map_;
182
183 typedef std::map<std::string, const OatDexFile*> Table;
184 Table oat_dex_files_;
185
186 friend class OatClass;
187 friend class OatDexFile;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700188 friend class OatDump; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700189 DISALLOW_COPY_AND_ASSIGN(OatFile);
190};
191
192} // namespace art
193
194#endif // ART_SRC_OAT_WRITER_H_