blob: 9f95b3b72e2e62714b0d2ac8c2e7ff36bb3517be [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
19 // Open an oat file. Returns NULL on failure. Requested base can
20 // optionally be used to request where the file should be loaded.
21 static OatFile* Open(const std::string& filename,
22 const std::string& strip_location_prefix,
23 byte* requested_base);
24
25 ~OatFile();
26
27 const std::string& GetLocation() const {
28 return location_;
29 }
30
31 const OatHeader& GetOatHeader() const;
32
33 class OatDexFile;
34
Brian Carlstrom3320cf42011-10-04 14:58:28 -070035 class OatMethod {
36 public:
37 // Create an OatMethod backed by an OatFile
38 OatMethod(const void* code,
39 const size_t frame_size_in_bytes,
40 const size_t return_pc_offset_in_bytes,
41 const uint32_t core_spill_mask,
42 const uint32_t fp_spill_mask,
43 const uint32_t* mapping_table,
44 const uint16_t* vmap_table,
45 const Method::InvokeStub* invoke_stub);
46
47 ~OatMethod();
48
49 // Link Method using the contents of this OatMethod
Brian Carlstromaded5f72011-10-07 17:15:04 -070050 void LinkMethod(Method* method) const;
Brian Carlstrom3320cf42011-10-04 14:58:28 -070051
52 const void* code_;
53 size_t frame_size_in_bytes_;
54 size_t return_pc_offset_in_bytes_;
55 uint32_t core_spill_mask_;
56 uint32_t fp_spill_mask_;
57 const uint32_t* mapping_table_;
58 const uint16_t* vmap_table_;
59 const Method::InvokeStub* invoke_stub_;
60 };
61
Brian Carlstrome24fa612011-09-29 00:53:55 -070062 class OatClass {
63 public:
Brian Carlstrom3320cf42011-10-04 14:58:28 -070064 // get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -070065 // defintion. direct methods come first, followed by virtual
66 // methods. note that runtime created methods such as miranda
67 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -070068 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -070069 ~OatClass();
70
71 private:
Brian Carlstrom3320cf42011-10-04 14:58:28 -070072 OatClass(const OatFile* oat_file, const OatMethodOffsets* methods_pointer);
73
74 template<class T>
75 T GetOatPointer(uint32_t offset) const {
76 if (offset == 0) {
77 return NULL;
78 }
79 T pointer = reinterpret_cast<T>(oat_file_->GetBase() + offset);
80 CHECK_LT(pointer, reinterpret_cast<T>(oat_file_->GetLimit()));
81 return pointer;
82 }
Brian Carlstrome24fa612011-09-29 00:53:55 -070083
84 const OatFile* oat_file_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -070085 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -070086
87 friend class OatDexFile;
88 };
89
90 class OatDexFile {
91 public:
Brian Carlstromaded5f72011-10-07 17:15:04 -070092 const OatClass* GetOatClass(uint32_t class_def_index) const;
93
94 const std::string& GetDexFileLocation() const {
95 return dex_file_location_;
96 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -070097
98 uint32_t GetDexFileChecksum() const {
99 return dex_file_checksum_;
100 }
101
Brian Carlstrome24fa612011-09-29 00:53:55 -0700102 ~OatDexFile();
103 private:
104 OatDexFile(const OatFile* oat_file,
105 std::string dex_file_location,
106 uint32_t dex_file_checksum,
107 uint32_t* classes_pointer);
108
109 const OatFile* oat_file_;
110 std::string dex_file_location_;
111 uint32_t dex_file_checksum_;
112 const uint32_t* classes_pointer_;
113
114 friend class OatFile;
115 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
116 };
117
Brian Carlstromaded5f72011-10-07 17:15:04 -0700118 const OatDexFile* GetOatDexFile(const std::string& dex_file_location) const;
119 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700120
121 size_t GetSize() const {
122 return GetLimit() - GetBase();
123 }
124
125 private:
126 OatFile(const std::string& filename);
127 bool Read(const std::string& filename, byte* requested_base);
128
129 const byte* GetBase() const;
130 const byte* GetLimit() const;
131
132 // The oat file name.
133 //
134 // The image will embed this to link its associated oat file.
135 const std::string location_;
136
137 // backing memory map for oat file
138 UniquePtr<MemMap> mem_map_;
139
140 typedef std::map<std::string, const OatDexFile*> Table;
141 Table oat_dex_files_;
142
143 friend class OatClass;
144 friend class OatDexFile;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700145 friend class OatDump; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700146 DISALLOW_COPY_AND_ASSIGN(OatFile);
147};
148
149} // namespace art
150
151#endif // ART_SRC_OAT_WRITER_H_