blob: 07e235b9f1a3120186b5cdf18b1bb9556bef7148 [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 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -080064 uint32_t GetGcMapOffset() const {
65 return gc_map_offset_;
66 }
Brian Carlstromae826982011-11-09 01:33:42 -080067 uint32_t GetInvokeStubOffset() const {
68 return invoke_stub_offset_;
69 }
70
71 const void* GetCode() const {
72 return GetOatPointer<const void*>(code_offset_);
73 }
74 const uint32_t* GetMappingTable() const {
75 return GetOatPointer<const uint32_t*>(mapping_table_offset_);
76 }
77 const uint16_t* GetVmapTable() const {
78 return GetOatPointer<const uint16_t*>(vmap_table_offset_);
79 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -080080 const uint8_t* GetGcMap() const {
81 return GetOatPointer<const uint8_t*>(gc_map_offset_);
82 }
Brian Carlstromae826982011-11-09 01:33:42 -080083 const Method::InvokeStub* GetInvokeStub() const {
84 return GetOatPointer<const Method::InvokeStub*>(invoke_stub_offset_);
85 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -070086
87 ~OatMethod();
88
Brian Carlstromae826982011-11-09 01:33:42 -080089 // Create an OatMethod with offsets relative to the given base address
90 OatMethod(const byte* base,
91 const uint32_t code_offset,
92 const size_t frame_size_in_bytes,
93 const uint32_t core_spill_mask,
94 const uint32_t fp_spill_mask,
95 const uint32_t mapping_table_offset,
96 const uint32_t vmap_table_offset,
Brian Carlstrome7d856b2012-01-11 18:10:55 -080097 const uint32_t gc_map_offset,
Brian Carlstromae826982011-11-09 01:33:42 -080098 const uint32_t invoke_stub_offset);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070099
Brian Carlstromae826982011-11-09 01:33:42 -0800100 private:
101 template<class T>
102 T GetOatPointer(uint32_t offset) const {
103 if (offset == 0) {
104 return NULL;
105 }
106 return reinterpret_cast<T>(base_ + offset);
107 }
108
109 const byte* base_;
110
111 uint32_t code_offset_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700112 size_t frame_size_in_bytes_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700113 uint32_t core_spill_mask_;
114 uint32_t fp_spill_mask_;
Brian Carlstromae826982011-11-09 01:33:42 -0800115 uint32_t mapping_table_offset_;
116 uint32_t vmap_table_offset_;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800117 uint32_t gc_map_offset_;
Brian Carlstromae826982011-11-09 01:33:42 -0800118 uint32_t invoke_stub_offset_;
119
120 friend class OatClass;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700121 };
122
Brian Carlstrome24fa612011-09-29 00:53:55 -0700123 class OatClass {
124 public:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800125 Class::Status GetStatus() const;
126
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700127 // get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -0700128 // defintion. direct methods come first, followed by virtual
129 // methods. note that runtime created methods such as miranda
130 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700131 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700132 ~OatClass();
133
134 private:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800135 OatClass(const OatFile* oat_file,
136 Class::Status status,
137 const OatMethodOffsets* methods_pointer);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700138
Brian Carlstrome24fa612011-09-29 00:53:55 -0700139 const OatFile* oat_file_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800140 const Class::Status status_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700141 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700142
143 friend class OatDexFile;
144 };
145
146 class OatDexFile {
147 public:
Brian Carlstrom89521892011-12-07 22:05:07 -0800148 const DexFile* OpenDexFile() const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700149 const OatClass* GetOatClass(uint32_t class_def_index) const;
150
151 const std::string& GetDexFileLocation() const {
152 return dex_file_location_;
153 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700154
155 uint32_t GetDexFileChecksum() const {
156 return dex_file_checksum_;
157 }
158
Brian Carlstrome24fa612011-09-29 00:53:55 -0700159 ~OatDexFile();
160 private:
161 OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800162 const std::string& dex_file_location,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700163 uint32_t dex_file_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800164 byte* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800165 const uint32_t* oat_class_offsets_pointer);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700166
167 const OatFile* oat_file_;
168 std::string dex_file_location_;
169 uint32_t dex_file_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800170 const byte* dex_file_pointer_;
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800171 const uint32_t* oat_class_offsets_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700172
173 friend class OatFile;
174 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
175 };
176
Ian Rogers7fe2c692011-12-06 16:35:59 -0800177 const OatDexFile* GetOatDexFile(const std::string& dex_file_location,
178 bool warn_if_not_found = true) const;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700179 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700180
181 size_t GetSize() const {
182 return GetLimit() - GetBase();
183 }
184
185 private:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700186 explicit OatFile(const std::string& filename);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700187 bool Read(const std::string& filename, byte* requested_base);
188
189 const byte* GetBase() const;
190 const byte* GetLimit() const;
191
192 // The oat file name.
193 //
194 // The image will embed this to link its associated oat file.
195 const std::string location_;
196
197 // backing memory map for oat file
198 UniquePtr<MemMap> mem_map_;
199
200 typedef std::map<std::string, const OatDexFile*> Table;
201 Table oat_dex_files_;
202
203 friend class OatClass;
204 friend class OatDexFile;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700205 friend class OatDump; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700206 DISALLOW_COPY_AND_ASSIGN(OatFile);
207};
208
209} // namespace art
210
211#endif // ART_SRC_OAT_WRITER_H_