blob: 5c011fdcd3dbc71da0abc372005d46a2e4318ab0 [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 Carlstrom0755ec52012-01-11 15:19:46 -0800117 Class::Status GetStatus() const;
118
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700119 // get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700120 // defintion. direct methods come first, followed by virtual
121 // methods. note that runtime created methods such as miranda
122 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700123 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700124 ~OatClass();
125
126 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800127 OatClass(const OatFile* oat_file,
128 Class::Status status,
129 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700130
Brian Carlstrome24fa612011-09-29 00:53:55 -0700131 const OatFile* oat_file_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800132 const Class::Status status_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700133 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700134
135 friend class OatDexFile;
136 };
137
138 class OatDexFile {
139 public:
Brian Carlstrom89521892011-12-07 22:05:07 -0800140 const DexFile* OpenDexFile() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700141 const OatClass* GetOatClass(uint32_t class_def_index) const;
142
143 const std::string& GetDexFileLocation() const {
144 return dex_file_location_;
145 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700146
147 uint32_t GetDexFileChecksum() const {
148 return dex_file_checksum_;
149 }
150
Brian Carlstrome24fa612011-09-29 00:53:55 -0700151 ~OatDexFile();
152 private:
153 OatDexFile(const OatFile* oat_file,
154 std::string dex_file_location,
155 uint32_t dex_file_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800156 byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800157 const uint32_t* oat_class_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700158
159 const OatFile* oat_file_;
160 std::string dex_file_location_;
161 uint32_t dex_file_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800162 const byte* dex_file_pointer_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800163 const uint32_t* oat_class_offsets_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700164
165 friend class OatFile;
166 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
167 };
168
Ian Rogers7fe2c692011-12-06 16:35:59 -0800169 const OatDexFile* GetOatDexFile(const std::string& dex_file_location,
170 bool warn_if_not_found = true) const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700171 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700172
173 size_t GetSize() const {
174 return GetLimit() - GetBase();
175 }
176
177 private:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700178 explicit OatFile(const std::string& filename);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700179 bool Read(const std::string& filename, byte* requested_base);
180
181 const byte* GetBase() const;
182 const byte* GetLimit() const;
183
184 // The oat file name.
185 //
186 // The image will embed this to link its associated oat file.
187 const std::string location_;
188
189 // backing memory map for oat file
190 UniquePtr<MemMap> mem_map_;
191
192 typedef std::map<std::string, const OatDexFile*> Table;
193 Table oat_dex_files_;
194
195 friend class OatClass;
196 friend class OatDexFile;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700197 friend class OatDump; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700198 DISALLOW_COPY_AND_ASSIGN(OatFile);
199};
200
201} // namespace art
202
203#endif // ART_SRC_OAT_WRITER_H_