blob: a33ffc7e97d9c11e0649a79cf989be484480beec [file] [log] [blame]
Brian Carlstrome24fa612011-09-29 00:53:55 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_OAT_WRITER_H_
4#define ART_SRC_OAT_WRITER_H_
5
6#include <stdint.h>
7
8#include <cstddef>
9
10#include "UniquePtr.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070011#include "compiler.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070012#include "dex_cache.h"
13#include "mem_map.h"
14#include "oat.h"
15#include "object.h"
16#include "os.h"
17#include "space.h"
18
19namespace art {
20
21// OatHeader fixed length with count of D OatDexFiles
22//
23// OatDexFile[0] each fixed length with offset to variable sized OatClasses
24// OatDexFile[1]
25// ...
26// OatDexFile[D]
27//
28// OatClasses[0] one variable sized OatClasses for each OatDexFile
29// OatClasses[1] contains DexFile::NumClassDefs offsets to OatMethods for each ClassDef
30// ...
31// OatClasses[D]
32//
33// OatMethods[0] one variable sized OatMethods for each of C DexFile::ClassDefs
Brian Carlstrom3320cf42011-10-04 14:58:28 -070034// OatMethods[1] contains OatMethod entries with offsets to code, method properities, etc.
Brian Carlstrome24fa612011-09-29 00:53:55 -070035// ...
36// OatMethods[C]
37//
38// padding if necessary so that the follow code will be page aligned
39//
Brian Carlstrom3320cf42011-10-04 14:58:28 -070040// CompiledMethod one variable sized blob with the contents of each CompiledMethod
41// CompiledMethod
42// CompiledMethod
43// CompiledMethod
44// CompiledMethod
45// CompiledMethod
Brian Carlstrome24fa612011-09-29 00:53:55 -070046// ...
Brian Carlstrom3320cf42011-10-04 14:58:28 -070047// CompiledMethod
Brian Carlstrome24fa612011-09-29 00:53:55 -070048//
49class OatWriter {
50 public:
51 // Write an oat file. Returns true on success, false on failure.
Brian Carlstrom3320cf42011-10-04 14:58:28 -070052 static bool Create(const std::string& filename,
53 const ClassLoader* class_loader,
54 const Compiler& compiler);
Brian Carlstrome24fa612011-09-29 00:53:55 -070055
56 private:
57
Brian Carlstrom3320cf42011-10-04 14:58:28 -070058 OatWriter(const std::vector<const DexFile*>& dex_files,
59 const ClassLoader* class_loader,
60 const Compiler& compiler);
Brian Carlstrome24fa612011-09-29 00:53:55 -070061 ~OatWriter();
62
63 size_t InitOatHeader();
64 size_t InitOatDexFiles(size_t offset);
65 size_t InitOatClasses(size_t offset);
66 size_t InitOatMethods(size_t offset);
67 size_t InitOatCode(size_t offset);
68 size_t InitOatCodeDexFiles(size_t offset);
69 size_t InitOatCodeDexFile(size_t offset,
70 size_t& oat_class_index,
71 const DexFile& dex_file);
72 size_t InitOatCodeClassDef(size_t offset,
73 size_t oat_class_index,
74 const DexFile& dex_file,
75 const DexFile::ClassDef& class_def);
76 size_t InitOatCodeMethod(size_t offset,
77 size_t oat_class_index,
78 size_t class_def_method_index,
79 Method* method);
80
81 bool Write(const std::string& filename);
82 bool WriteTables(File* file);
83 size_t WriteCode(File* file);
84 size_t WriteCodeDexFiles(File* file,
85 size_t offset);
86 size_t WriteCodeDexFile(File* file,
87 size_t offset,
88 const DexFile& dex_file);
89 size_t WriteCodeClassDef(File* file,
90 size_t offset,
91 const DexFile& dex_file,
92 const DexFile::ClassDef& class_def);
93 size_t WriteCodeMethod(File* file,
94 size_t offset,
95 Method* method);
96
97 class OatDexFile {
98 public:
99 OatDexFile(const DexFile& dex_file);
100 size_t SizeOf() const;
101 void UpdateChecksum(OatHeader& oat_header) const;
102 bool Write(File* file) const;
103
104 // data to write
105 uint32_t dex_file_location_size_;
106 const uint8_t* dex_file_location_data_;
107 uint32_t dex_file_checksum_;
108 uint32_t classes_offset_;
109
110 private:
111 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
112 };
113
114 class OatClasses {
115 public:
116 OatClasses(const DexFile& dex_file);
117 size_t SizeOf() const;
118 void UpdateChecksum(OatHeader& oat_header) const;
119 bool Write(File* file) const;
120
121 // data to write
122 std::vector<uint32_t> methods_offsets_;
123
124 private:
125 DISALLOW_COPY_AND_ASSIGN(OatClasses);
126 };
127
128 class OatMethods {
129 public:
130 OatMethods(uint32_t methods_count);
131 size_t SizeOf() const;
132 void UpdateChecksum(OatHeader& oat_header) const;
133 bool Write(File* file) const;
134
135 // data to write
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700136 std::vector<OatMethodOffsets> method_offsets_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700137
138 private:
139 DISALLOW_COPY_AND_ASSIGN(OatMethods);
140 };
141
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700142 const Compiler* compiler_;
143
Brian Carlstrome24fa612011-09-29 00:53:55 -0700144 // TODO: remove the ClassLoader when the code storage moves out of Method
145 const ClassLoader* class_loader_;
146
147 // note OatFile does not take ownership of the DexFiles
148 const std::vector<const DexFile*>* dex_files_;
149
150 // data to write
151 OatHeader* oat_header_;
152 std::vector<OatDexFile*> oat_dex_files_;
153 std::vector<OatClasses*> oat_classes_;
154 std::vector<OatMethods*> oat_methods_;
155 uint32_t executable_offset_padding_length_;
156
157 DISALLOW_COPY_AND_ASSIGN(OatWriter);
158};
159
160} // namespace art
161
162#endif // ART_SRC_OAT_WRITER_H_