blob: e83b2940da3545624ffce3e1dc906bc029475e2e [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
20 static std::string DexFileToOatFilename(const DexFile& dex_file);
21
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:
40 // Create an OatMethod backed by an OatFile
41 OatMethod(const void* code,
42 const size_t frame_size_in_bytes,
43 const size_t return_pc_offset_in_bytes,
44 const uint32_t core_spill_mask,
45 const uint32_t fp_spill_mask,
46 const uint32_t* mapping_table,
47 const uint16_t* vmap_table,
48 const Method::InvokeStub* invoke_stub);
49
50 ~OatMethod();
51
52 // Link Method using the contents of this OatMethod
Brian Carlstromaded5f72011-10-07 17:15:04 -070053 void LinkMethod(Method* method) const;
Brian Carlstrom3320cf42011-10-04 14:58:28 -070054
55 const void* code_;
56 size_t frame_size_in_bytes_;
57 size_t return_pc_offset_in_bytes_;
58 uint32_t core_spill_mask_;
59 uint32_t fp_spill_mask_;
60 const uint32_t* mapping_table_;
61 const uint16_t* vmap_table_;
62 const Method::InvokeStub* invoke_stub_;
63 };
64
Brian Carlstrome24fa612011-09-29 00:53:55 -070065 class OatClass {
66 public:
Brian Carlstrom3320cf42011-10-04 14:58:28 -070067 // get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -070068 // defintion. direct methods come first, followed by virtual
69 // methods. note that runtime created methods such as miranda
70 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -070071 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -070072 ~OatClass();
73
74 private:
Brian Carlstrom3320cf42011-10-04 14:58:28 -070075 OatClass(const OatFile* oat_file, const OatMethodOffsets* methods_pointer);
76
77 template<class T>
78 T GetOatPointer(uint32_t offset) const {
79 if (offset == 0) {
80 return NULL;
81 }
82 T pointer = reinterpret_cast<T>(oat_file_->GetBase() + offset);
83 CHECK_LT(pointer, reinterpret_cast<T>(oat_file_->GetLimit()));
84 return pointer;
85 }
Brian Carlstrome24fa612011-09-29 00:53:55 -070086
87 const OatFile* oat_file_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -070088 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -070089
90 friend class OatDexFile;
91 };
92
93 class OatDexFile {
94 public:
Brian Carlstromaded5f72011-10-07 17:15:04 -070095 const OatClass* GetOatClass(uint32_t class_def_index) const;
96
97 const std::string& GetDexFileLocation() const {
98 return dex_file_location_;
99 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700100
101 uint32_t GetDexFileChecksum() const {
102 return dex_file_checksum_;
103 }
104
Brian Carlstrome24fa612011-09-29 00:53:55 -0700105 ~OatDexFile();
106 private:
107 OatDexFile(const OatFile* oat_file,
108 std::string dex_file_location,
109 uint32_t dex_file_checksum,
110 uint32_t* classes_pointer);
111
112 const OatFile* oat_file_;
113 std::string dex_file_location_;
114 uint32_t dex_file_checksum_;
115 const uint32_t* classes_pointer_;
116
117 friend class OatFile;
118 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
119 };
120
Brian Carlstromaded5f72011-10-07 17:15:04 -0700121 const OatDexFile* GetOatDexFile(const std::string& dex_file_location) const;
122 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700123
124 size_t GetSize() const {
125 return GetLimit() - GetBase();
126 }
127
128 private:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700129 explicit OatFile(const std::string& filename);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700130 bool Read(const std::string& filename, byte* requested_base);
131
132 const byte* GetBase() const;
133 const byte* GetLimit() const;
134
135 // The oat file name.
136 //
137 // The image will embed this to link its associated oat file.
138 const std::string location_;
139
140 // backing memory map for oat file
141 UniquePtr<MemMap> mem_map_;
142
143 typedef std::map<std::string, const OatDexFile*> Table;
144 Table oat_dex_files_;
145
146 friend class OatClass;
147 friend class OatDexFile;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700148 friend class OatDump; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700149 DISALLOW_COPY_AND_ASSIGN(OatFile);
150};
151
152} // namespace art
153
154#endif // ART_SRC_OAT_WRITER_H_