blob: 0642fa4d5c5619e2b7cbe19041d4d3e95894acc0 [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.
Elliott Hughes234da572011-11-03 22:13:06 -070052 static bool Create(File* file, const ClassLoader* class_loader, const Compiler& compiler);
Brian Carlstrome24fa612011-09-29 00:53:55 -070053
54 private:
55
Brian Carlstrom3320cf42011-10-04 14:58:28 -070056 OatWriter(const std::vector<const DexFile*>& dex_files,
57 const ClassLoader* class_loader,
58 const Compiler& compiler);
Brian Carlstrome24fa612011-09-29 00:53:55 -070059 ~OatWriter();
60
61 size_t InitOatHeader();
62 size_t InitOatDexFiles(size_t offset);
63 size_t InitOatClasses(size_t offset);
64 size_t InitOatMethods(size_t offset);
65 size_t InitOatCode(size_t offset);
66 size_t InitOatCodeDexFiles(size_t offset);
67 size_t InitOatCodeDexFile(size_t offset,
68 size_t& oat_class_index,
69 const DexFile& dex_file);
70 size_t InitOatCodeClassDef(size_t offset,
71 size_t oat_class_index,
72 const DexFile& dex_file,
73 const DexFile::ClassDef& class_def);
74 size_t InitOatCodeMethod(size_t offset,
75 size_t oat_class_index,
76 size_t class_def_method_index,
77 Method* method);
78
Elliott Hughes234da572011-11-03 22:13:06 -070079 bool Write(File* file);
Brian Carlstrome24fa612011-09-29 00:53:55 -070080 bool WriteTables(File* file);
81 size_t WriteCode(File* file);
82 size_t WriteCodeDexFiles(File* file,
83 size_t offset);
84 size_t WriteCodeDexFile(File* file,
85 size_t offset,
86 const DexFile& dex_file);
87 size_t WriteCodeClassDef(File* file,
88 size_t offset,
89 const DexFile& dex_file,
90 const DexFile::ClassDef& class_def);
91 size_t WriteCodeMethod(File* file,
92 size_t offset,
93 Method* method);
94
Elliott Hughes234da572011-11-03 22:13:06 -070095 void ReportWriteFailure(const char* what, Method* m, File* f);
96
Brian Carlstrome24fa612011-09-29 00:53:55 -070097 class OatDexFile {
98 public:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -070099 explicit OatDexFile(const DexFile& dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700100 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:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700116 explicit OatClasses(const DexFile& dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700117 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:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700130 explicit OatMethods(uint32_t methods_count);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700131 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
jeffhaof479dcc2011-11-02 15:54:15 -0700157 template <class T> struct MapCompare {
158 public:
159 bool operator() (const T* const &a, const T* const &b) const {
160 return *a < *b;
161 }
162 };
163
jeffhao55d78212011-11-02 11:41:50 -0700164 // code mappings for deduplication
jeffhaof479dcc2011-11-02 15:54:15 -0700165 std::map<const std::vector<uint8_t>*, uint32_t, MapCompare<std::vector<uint8_t> > > code_offsets_;
166 std::map<const std::vector<uint16_t>*, uint32_t, MapCompare<std::vector<uint16_t> > > vmap_table_offsets_;
167 std::map<const std::vector<uint32_t>*, uint32_t, MapCompare<std::vector<uint32_t> > > mapping_table_offsets_;
jeffhao55d78212011-11-02 11:41:50 -0700168
Brian Carlstrome24fa612011-09-29 00:53:55 -0700169 DISALLOW_COPY_AND_ASSIGN(OatWriter);
170};
171
172} // namespace art
173
174#endif // ART_SRC_OAT_WRITER_H_