blob: 7d454a7a48d18b27d4c79e220993ff9028fd44dc [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//
Brian Carlstrom89521892011-12-07 22:05:07 -080023// OatDexFile[0] one variable sized OatDexFile with offsets to Dex and OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -070024// OatDexFile[1]
25// ...
26// OatDexFile[D]
27//
Brian Carlstrom89521892011-12-07 22:05:07 -080028// Dex[0] one variable sized DexFile for each OatDexFile.
29// Dex[1] these are literal copies of the input .dex files.
30// ...
31// Dex[D]
32//
Brian Carlstrome24fa612011-09-29 00:53:55 -070033// OatClasses[0] one variable sized OatClasses for each OatDexFile
34// OatClasses[1] contains DexFile::NumClassDefs offsets to OatMethods for each ClassDef
35// ...
36// OatClasses[D]
37//
38// OatMethods[0] one variable sized OatMethods for each of C DexFile::ClassDefs
Brian Carlstrom3320cf42011-10-04 14:58:28 -070039// OatMethods[1] contains OatMethod entries with offsets to code, method properities, etc.
Brian Carlstrome24fa612011-09-29 00:53:55 -070040// ...
41// OatMethods[C]
42//
43// padding if necessary so that the follow code will be page aligned
44//
Brian Carlstrom3320cf42011-10-04 14:58:28 -070045// CompiledMethod one variable sized blob with the contents of each CompiledMethod
46// CompiledMethod
47// CompiledMethod
48// CompiledMethod
49// CompiledMethod
50// CompiledMethod
Brian Carlstrome24fa612011-09-29 00:53:55 -070051// ...
Brian Carlstrom3320cf42011-10-04 14:58:28 -070052// CompiledMethod
Brian Carlstrome24fa612011-09-29 00:53:55 -070053//
54class OatWriter {
55 public:
56 // Write an oat file. Returns true on success, false on failure.
Elliott Hughes234da572011-11-03 22:13:06 -070057 static bool Create(File* file, const ClassLoader* class_loader, const Compiler& compiler);
Brian Carlstrome24fa612011-09-29 00:53:55 -070058
59 private:
60
Brian Carlstrom3320cf42011-10-04 14:58:28 -070061 OatWriter(const std::vector<const DexFile*>& dex_files,
62 const ClassLoader* class_loader,
63 const Compiler& compiler);
Brian Carlstrome24fa612011-09-29 00:53:55 -070064 ~OatWriter();
65
66 size_t InitOatHeader();
67 size_t InitOatDexFiles(size_t offset);
Brian Carlstrom89521892011-12-07 22:05:07 -080068 size_t InitDexFiles(size_t offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -070069 size_t InitOatClasses(size_t offset);
70 size_t InitOatMethods(size_t offset);
71 size_t InitOatCode(size_t offset);
72 size_t InitOatCodeDexFiles(size_t offset);
73 size_t InitOatCodeDexFile(size_t offset,
74 size_t& oat_class_index,
75 const DexFile& dex_file);
76 size_t InitOatCodeClassDef(size_t offset,
77 size_t oat_class_index,
78 const DexFile& dex_file,
79 const DexFile::ClassDef& class_def);
Ian Rogers0571d352011-11-03 19:51:38 -070080 size_t InitOatCodeMethod(size_t offset, size_t oat_class_index, size_t class_def_method_index,
81 bool is_static, bool is_direct, uint32_t method_idx, const DexFile*);
Brian Carlstrome24fa612011-09-29 00:53:55 -070082
Elliott Hughes234da572011-11-03 22:13:06 -070083 bool Write(File* file);
Brian Carlstrome24fa612011-09-29 00:53:55 -070084 bool WriteTables(File* file);
85 size_t WriteCode(File* file);
Ian Rogers0571d352011-11-03 19:51:38 -070086 size_t WriteCodeDexFiles(File* file, size_t offset);
87 size_t WriteCodeDexFile(File* file, size_t offset, size_t& oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -070088 const DexFile& dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -070089 size_t WriteCodeClassDef(File* file, size_t offset, size_t oat_class_index,
90 const DexFile& dex_file, const DexFile::ClassDef& class_def);
91 size_t WriteCodeMethod(File* file, size_t offset, size_t oat_class_index,
92 size_t class_def_method_index, bool is_static, uint32_t method_idx,
93 const DexFile& dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -070094
Ian Rogers0571d352011-11-03 19:51:38 -070095 void ReportWriteFailure(const char* what, uint32_t method_idx, const DexFile& dex_file,
96 File* f) const;
Elliott Hughes234da572011-11-03 22:13:06 -070097
Brian Carlstrome24fa612011-09-29 00:53:55 -070098 class OatDexFile {
99 public:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700100 explicit OatDexFile(const DexFile& dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700101 size_t SizeOf() const;
102 void UpdateChecksum(OatHeader& oat_header) const;
103 bool Write(File* file) const;
104
105 // data to write
106 uint32_t dex_file_location_size_;
107 const uint8_t* dex_file_location_data_;
108 uint32_t dex_file_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800109 uint32_t dex_file_offset_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700110 uint32_t classes_offset_;
111
112 private:
113 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
114 };
115
116 class OatClasses {
117 public:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700118 explicit OatClasses(const DexFile& dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700119 size_t SizeOf() const;
120 void UpdateChecksum(OatHeader& oat_header) const;
121 bool Write(File* file) const;
122
123 // data to write
124 std::vector<uint32_t> methods_offsets_;
125
126 private:
127 DISALLOW_COPY_AND_ASSIGN(OatClasses);
128 };
129
130 class OatMethods {
131 public:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700132 explicit OatMethods(uint32_t methods_count);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700133 size_t SizeOf() const;
134 void UpdateChecksum(OatHeader& oat_header) const;
135 bool Write(File* file) const;
136
137 // data to write
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700138 std::vector<OatMethodOffsets> method_offsets_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700139
140 private:
141 DISALLOW_COPY_AND_ASSIGN(OatMethods);
142 };
143
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700144 const Compiler* compiler_;
145
Brian Carlstrome24fa612011-09-29 00:53:55 -0700146 // TODO: remove the ClassLoader when the code storage moves out of Method
147 const ClassLoader* class_loader_;
148
149 // note OatFile does not take ownership of the DexFiles
150 const std::vector<const DexFile*>* dex_files_;
151
152 // data to write
153 OatHeader* oat_header_;
154 std::vector<OatDexFile*> oat_dex_files_;
155 std::vector<OatClasses*> oat_classes_;
156 std::vector<OatMethods*> oat_methods_;
157 uint32_t executable_offset_padding_length_;
158
jeffhaof479dcc2011-11-02 15:54:15 -0700159 template <class T> struct MapCompare {
160 public:
161 bool operator() (const T* const &a, const T* const &b) const {
162 return *a < *b;
163 }
164 };
165
jeffhao55d78212011-11-02 11:41:50 -0700166 // code mappings for deduplication
jeffhaof479dcc2011-11-02 15:54:15 -0700167 std::map<const std::vector<uint8_t>*, uint32_t, MapCompare<std::vector<uint8_t> > > code_offsets_;
168 std::map<const std::vector<uint16_t>*, uint32_t, MapCompare<std::vector<uint16_t> > > vmap_table_offsets_;
169 std::map<const std::vector<uint32_t>*, uint32_t, MapCompare<std::vector<uint32_t> > > mapping_table_offsets_;
jeffhao55d78212011-11-02 11:41:50 -0700170
Brian Carlstrome24fa612011-09-29 00:53:55 -0700171 DISALLOW_COPY_AND_ASSIGN(OatWriter);
172};
173
174} // namespace art
175
176#endif // ART_SRC_OAT_WRITER_H_