blob: fe0bd6768768f5c2a85bc71a75e8d277ebec7a07 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrome24fa612011-09-29 00:53:55 -070016
17#ifndef ART_SRC_OAT_WRITER_H_
18#define ART_SRC_OAT_WRITER_H_
19
20#include <stdint.h>
21
22#include <cstddef>
23
Brian Carlstrom3320cf42011-10-04 14:58:28 -070024#include "compiler.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070025#include "dex_cache.h"
26#include "mem_map.h"
27#include "oat.h"
28#include "object.h"
29#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070030#include "safe_map.h"
31#include "UniquePtr.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070032
33namespace art {
34
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070035// OatHeader variable length with count of D OatDexFiles
Brian Carlstrome24fa612011-09-29 00:53:55 -070036//
Brian Carlstrom389efb02012-01-11 12:06:26 -080037// OatDexFile[0] one variable sized OatDexFile with offsets to Dex and OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -070038// OatDexFile[1]
39// ...
40// OatDexFile[D]
41//
Brian Carlstrom89521892011-12-07 22:05:07 -080042// Dex[0] one variable sized DexFile for each OatDexFile.
43// Dex[1] these are literal copies of the input .dex files.
44// ...
45// Dex[D]
46//
Brian Carlstrom389efb02012-01-11 12:06:26 -080047// OatClass[0] one variable sized OatClass for each of C DexFile::ClassDefs
48// OatClass[1] contains OatClass entries with class status, offsets to code, etc.
Brian Carlstrome24fa612011-09-29 00:53:55 -070049// ...
Brian Carlstrom389efb02012-01-11 12:06:26 -080050// OatClass[C]
Brian Carlstrome24fa612011-09-29 00:53:55 -070051//
Logan Chien25ae6402012-03-20 20:19:26 +080052// OatElfImage[0] one OatElfImage for each ELF image
53// OatElfImage[1] contains the size, checksum, and offset to the ELF image.
54// ...
55// OatElfImage[E]
56//
57// ELF[0]
58// ELF[1]
59// ...
60// ELF[E]
61//
Brian Carlstromf03c2882012-03-05 20:29:06 -080062// padding if necessary so that the following code will be page aligned
Brian Carlstrome24fa612011-09-29 00:53:55 -070063//
Brian Carlstrom3320cf42011-10-04 14:58:28 -070064// CompiledMethod one variable sized blob with the contents of each CompiledMethod
65// CompiledMethod
66// CompiledMethod
67// CompiledMethod
68// CompiledMethod
69// CompiledMethod
Brian Carlstrome24fa612011-09-29 00:53:55 -070070// ...
Brian Carlstrom3320cf42011-10-04 14:58:28 -070071// CompiledMethod
Brian Carlstrome24fa612011-09-29 00:53:55 -070072//
73class OatWriter {
74 public:
75 // Write an oat file. Returns true on success, false on failure.
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070076 static bool Create(File* file,
77 const ClassLoader* class_loader,
78 const std::vector<const DexFile*>& dex_files,
79 uint32_t image_file_location_checksum,
80 const std::string& image_file_location,
81 const Compiler& compiler);
Brian Carlstrome24fa612011-09-29 00:53:55 -070082
83 private:
Brian Carlstrom3320cf42011-10-04 14:58:28 -070084 OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070085 uint32_t image_file_location_checksum,
86 const std::string& image_file_location,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070087 const ClassLoader* class_loader,
88 const Compiler& compiler);
Brian Carlstrome24fa612011-09-29 00:53:55 -070089 ~OatWriter();
90
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070091 size_t InitOatHeader();
Brian Carlstrome24fa612011-09-29 00:53:55 -070092 size_t InitOatDexFiles(size_t offset);
Brian Carlstrom89521892011-12-07 22:05:07 -080093 size_t InitDexFiles(size_t offset);
Brian Carlstrom389efb02012-01-11 12:06:26 -080094 size_t InitOatClasses(size_t offset);
Logan Chien25ae6402012-03-20 20:19:26 +080095 size_t InitOatElfImages(size_t offset);
96 size_t InitElfImages(size_t offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -070097 size_t InitOatCode(size_t offset);
98 size_t InitOatCodeDexFiles(size_t offset);
99 size_t InitOatCodeDexFile(size_t offset,
100 size_t& oat_class_index,
101 const DexFile& dex_file);
102 size_t InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -0800103 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700104 const DexFile& dex_file,
105 const DexFile::ClassDef& class_def);
Ian Rogersc20a83e2012-01-18 18:15:32 -0800106 size_t InitOatCodeMethod(size_t offset, size_t oat_class_index, size_t class_def_index,
107 size_t class_def_method_index, bool is_native, bool is_static,
108 bool is_direct, uint32_t method_idx, const DexFile*);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700109
Elliott Hughes234da572011-11-03 22:13:06 -0700110 bool Write(File* file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700111 bool WriteTables(File* file);
112 size_t WriteCode(File* file);
Ian Rogers0571d352011-11-03 19:51:38 -0700113 size_t WriteCodeDexFiles(File* file, size_t offset);
114 size_t WriteCodeDexFile(File* file, size_t offset, size_t& oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700115 const DexFile& dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -0700116 size_t WriteCodeClassDef(File* file, size_t offset, size_t oat_class_index,
117 const DexFile& dex_file, const DexFile::ClassDef& class_def);
118 size_t WriteCodeMethod(File* file, size_t offset, size_t oat_class_index,
119 size_t class_def_method_index, bool is_static, uint32_t method_idx,
120 const DexFile& dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700121
Ian Rogers0571d352011-11-03 19:51:38 -0700122 void ReportWriteFailure(const char* what, uint32_t method_idx, const DexFile& dex_file,
123 File* f) const;
Elliott Hughes234da572011-11-03 22:13:06 -0700124
Brian Carlstrome24fa612011-09-29 00:53:55 -0700125 class OatDexFile {
126 public:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700127 explicit OatDexFile(const DexFile& dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700128 size_t SizeOf() const;
129 void UpdateChecksum(OatHeader& oat_header) const;
130 bool Write(File* file) const;
131
132 // data to write
133 uint32_t dex_file_location_size_;
134 const uint8_t* dex_file_location_data_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800135 uint32_t dex_file_location_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800136 uint32_t dex_file_offset_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700137 std::vector<uint32_t> methods_offsets_;
138
139 private:
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800140 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700141 };
142
Brian Carlstrom389efb02012-01-11 12:06:26 -0800143 class OatClass {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700144 public:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800145 explicit OatClass(Class::Status status, uint32_t methods_count);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700146 size_t SizeOf() const;
147 void UpdateChecksum(OatHeader& oat_header) const;
148 bool Write(File* file) const;
149
150 // data to write
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800151 Class::Status status_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700152 std::vector<OatMethodOffsets> method_offsets_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700153
154 private:
Brian Carlstrom389efb02012-01-11 12:06:26 -0800155 DISALLOW_COPY_AND_ASSIGN(OatClass);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700156 };
157
Logan Chien25ae6402012-03-20 20:19:26 +0800158 class OatElfImage {
159 public:
160 explicit OatElfImage(const ElfImage& elf_image);
161 size_t SizeOf() const;
162 uint32_t GetElfSize() const;
163 uint32_t GetElfOffset() const;
164 void SetElfOffset(uint32_t offset);
165 bool Write(File* file) const;
166 bool WriteElfImage(File* file) const;
167
168 private:
169 // data to write
170 uint32_t elf_offset_;
171 const uint32_t elf_size_;
172
173 const byte* const elf_addr_;
174 DISALLOW_COPY_AND_ASSIGN(OatElfImage);
175 };
176
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700177 const Compiler* compiler_;
178
Brian Carlstrome24fa612011-09-29 00:53:55 -0700179 // TODO: remove the ClassLoader when the code storage moves out of Method
180 const ClassLoader* class_loader_;
181
182 // note OatFile does not take ownership of the DexFiles
183 const std::vector<const DexFile*>* dex_files_;
184
Logan Chien25ae6402012-03-20 20:19:26 +0800185 std::vector<ElfImage> elf_images_;
186
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700187 // dependency on the image
188 uint32_t image_file_location_checksum_;
189 std::string image_file_location_;
190
Brian Carlstrome24fa612011-09-29 00:53:55 -0700191 // data to write
192 OatHeader* oat_header_;
193 std::vector<OatDexFile*> oat_dex_files_;
Brian Carlstrom389efb02012-01-11 12:06:26 -0800194 std::vector<OatClass*> oat_classes_;
Logan Chien25ae6402012-03-20 20:19:26 +0800195 std::vector<OatElfImage*> oat_elf_images_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700196 uint32_t executable_offset_padding_length_;
197
jeffhaof479dcc2011-11-02 15:54:15 -0700198 template <class T> struct MapCompare {
199 public:
200 bool operator() (const T* const &a, const T* const &b) const {
201 return *a < *b;
202 }
203 };
204
jeffhao55d78212011-11-02 11:41:50 -0700205 // code mappings for deduplication
Elliott Hughesa0e18062012-04-13 15:59:59 -0700206 SafeMap<const std::vector<uint8_t>*, uint32_t, MapCompare<std::vector<uint8_t> > > code_offsets_;
207 SafeMap<const std::vector<uint16_t>*, uint32_t, MapCompare<std::vector<uint16_t> > > vmap_table_offsets_;
208 SafeMap<const std::vector<uint32_t>*, uint32_t, MapCompare<std::vector<uint32_t> > > mapping_table_offsets_;
209 SafeMap<const std::vector<uint8_t>*, uint32_t, MapCompare<std::vector<uint8_t> > > gc_map_offsets_;
jeffhao55d78212011-11-02 11:41:50 -0700210
Brian Carlstrome24fa612011-09-29 00:53:55 -0700211 DISALLOW_COPY_AND_ASSIGN(OatWriter);
212};
213
214} // namespace art
215
216#endif // ART_SRC_OAT_WRITER_H_