blob: 2c39e9d8b5223561df556941860be3d590c31121 [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:
40 // Create an OatMethod backed by an OatFile
41 OatMethod(const void* code,
42 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070043 const uint32_t core_spill_mask,
44 const uint32_t fp_spill_mask,
45 const uint32_t* mapping_table,
46 const uint16_t* vmap_table,
47 const Method::InvokeStub* invoke_stub);
48
49 ~OatMethod();
50
51 // Link Method using the contents of this OatMethod
Brian Carlstromaded5f72011-10-07 17:15:04 -070052 void LinkMethod(Method* method) const;
Brian Carlstrom3320cf42011-10-04 14:58:28 -070053
54 const void* code_;
55 size_t frame_size_in_bytes_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -070056 uint32_t core_spill_mask_;
57 uint32_t fp_spill_mask_;
58 const uint32_t* mapping_table_;
59 const uint16_t* vmap_table_;
60 const Method::InvokeStub* invoke_stub_;
61 };
62
Brian Carlstrome24fa612011-09-29 00:53:55 -070063 class OatClass {
64 public:
Brian Carlstrom3320cf42011-10-04 14:58:28 -070065 // get the OatMethod entry based on its index into the class
Brian Carlstrome24fa612011-09-29 00:53:55 -070066 // defintion. direct methods come first, followed by virtual
67 // methods. note that runtime created methods such as miranda
68 // methods are not included.
Brian Carlstromaded5f72011-10-07 17:15:04 -070069 const OatMethod GetOatMethod(uint32_t method_index) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -070070 ~OatClass();
71
72 private:
Brian Carlstrom3320cf42011-10-04 14:58:28 -070073 OatClass(const OatFile* oat_file, const OatMethodOffsets* methods_pointer);
74
75 template<class T>
76 T GetOatPointer(uint32_t offset) const {
77 if (offset == 0) {
78 return NULL;
79 }
80 T pointer = reinterpret_cast<T>(oat_file_->GetBase() + offset);
81 CHECK_LT(pointer, reinterpret_cast<T>(oat_file_->GetLimit()));
82 return pointer;
83 }
Brian Carlstrome24fa612011-09-29 00:53:55 -070084
85 const OatFile* oat_file_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -070086 const OatMethodOffsets* methods_pointer_;
Brian Carlstrome24fa612011-09-29 00:53:55 -070087
88 friend class OatDexFile;
89 };
90
91 class OatDexFile {
92 public:
Brian Carlstromaded5f72011-10-07 17:15:04 -070093 const OatClass* GetOatClass(uint32_t class_def_index) const;
94
95 const std::string& GetDexFileLocation() const {
96 return dex_file_location_;
97 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -070098
99 uint32_t GetDexFileChecksum() const {
100 return dex_file_checksum_;
101 }
102
Brian Carlstrome24fa612011-09-29 00:53:55 -0700103 ~OatDexFile();
104 private:
105 OatDexFile(const OatFile* oat_file,
106 std::string dex_file_location,
107 uint32_t dex_file_checksum,
108 uint32_t* classes_pointer);
109
110 const OatFile* oat_file_;
111 std::string dex_file_location_;
112 uint32_t dex_file_checksum_;
113 const uint32_t* classes_pointer_;
114
115 friend class OatFile;
116 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
117 };
118
Brian Carlstromaded5f72011-10-07 17:15:04 -0700119 const OatDexFile* GetOatDexFile(const std::string& dex_file_location) const;
120 std::vector<const OatDexFile*> GetOatDexFiles() const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700121
122 size_t GetSize() const {
123 return GetLimit() - GetBase();
124 }
125
126 private:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700127 explicit OatFile(const std::string& filename);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700128 bool Read(const std::string& filename, byte* requested_base);
129
130 const byte* GetBase() const;
131 const byte* GetLimit() const;
132
133 // The oat file name.
134 //
135 // The image will embed this to link its associated oat file.
136 const std::string location_;
137
138 // backing memory map for oat file
139 UniquePtr<MemMap> mem_map_;
140
141 typedef std::map<std::string, const OatDexFile*> Table;
142 Table oat_dex_files_;
143
144 friend class OatClass;
145 friend class OatDexFile;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700146 friend class OatDump; // For GetBase and GetLimit
Brian Carlstrome24fa612011-09-29 00:53:55 -0700147 DISALLOW_COPY_AND_ASSIGN(OatFile);
148};
149
150} // namespace art
151
152#endif // ART_SRC_OAT_WRITER_H_