blob: b201d6b4ee19dde3ad0526359186ddef32dcb759 [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
Ian Rogers1212a022013-03-04 10:48:41 -080024#include "compiler/driver/compiler_driver.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070025#include "mem_map.h"
26#include "oat.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/class.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070028#include "safe_map.h"
29#include "UniquePtr.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070030
31namespace art {
32
Brian Carlstromcd60ac72013-01-20 17:09:51 -080033class OutputStream;
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//
Brian Carlstromf03c2882012-03-05 20:29:06 -080052// padding if necessary so that the following code will be page aligned
Brian Carlstrome24fa612011-09-29 00:53:55 -070053//
Brian Carlstrom3320cf42011-10-04 14:58:28 -070054// CompiledMethod one variable sized blob with the contents of each CompiledMethod
55// CompiledMethod
56// CompiledMethod
57// CompiledMethod
58// CompiledMethod
59// CompiledMethod
Brian Carlstrome24fa612011-09-29 00:53:55 -070060// ...
Brian Carlstrom3320cf42011-10-04 14:58:28 -070061// CompiledMethod
Brian Carlstrome24fa612011-09-29 00:53:55 -070062//
63class OatWriter {
64 public:
65 // Write an oat file. Returns true on success, false on failure.
Brian Carlstromcd60ac72013-01-20 17:09:51 -080066 static bool Create(OutputStream& out,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070067 const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070068 uint32_t image_file_location_oat_checksum,
69 uint32_t image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070070 const std::string& image_file_location,
Ian Rogers1212a022013-03-04 10:48:41 -080071 const CompilerDriver& compiler)
Ian Rogersb726dcb2012-09-05 08:57:23 -070072 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070073
74 private:
Brian Carlstrom3320cf42011-10-04 14:58:28 -070075 OatWriter(const std::vector<const DexFile*>& dex_files,
Brian Carlstrom28db0122012-10-18 16:20:41 -070076 uint32_t image_file_location_oat_checksum,
77 uint32_t image_file_location_oat_begin,
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070078 const std::string& image_file_location,
Ian Rogers1212a022013-03-04 10:48:41 -080079 const CompilerDriver* compiler) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070080 ~OatWriter();
81
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070082 size_t InitOatHeader();
Brian Carlstrome24fa612011-09-29 00:53:55 -070083 size_t InitOatDexFiles(size_t offset);
Brian Carlstrom89521892011-12-07 22:05:07 -080084 size_t InitDexFiles(size_t offset);
Brian Carlstrom389efb02012-01-11 12:06:26 -080085 size_t InitOatClasses(size_t offset);
Jeff Hao0aba0ba2013-06-03 14:49:28 -070086 size_t InitOatCode(size_t offset)
87 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070088 size_t InitOatCodeDexFiles(size_t offset)
Ian Rogersb726dcb2012-09-05 08:57:23 -070089 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070090 size_t InitOatCodeDexFile(size_t offset,
91 size_t& oat_class_index,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070092 const DexFile& dex_file)
Ian Rogersb726dcb2012-09-05 08:57:23 -070093 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrome24fa612011-09-29 00:53:55 -070094 size_t InitOatCodeClassDef(size_t offset,
Ian Rogersc20a83e2012-01-18 18:15:32 -080095 size_t oat_class_index, size_t class_def_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -070096 const DexFile& dex_file,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070097 const DexFile::ClassDef& class_def)
Ian Rogersb726dcb2012-09-05 08:57:23 -070098 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersc20a83e2012-01-18 18:15:32 -080099 size_t InitOatCodeMethod(size_t offset, size_t oat_class_index, size_t class_def_index,
Ian Rogers08f753d2012-08-24 14:35:25 -0700100 size_t class_def_method_index, bool is_native, InvokeType type,
101 uint32_t method_idx, const DexFile*)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700102 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700103
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800104 bool Write(OutputStream& out);
105 bool WriteTables(OutputStream& out);
106 size_t WriteCode(OutputStream& out);
107 size_t WriteCodeDexFiles(OutputStream& out, size_t offset);
108 size_t WriteCodeDexFile(OutputStream& out, size_t offset, size_t& oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -0700109 const DexFile& dex_file);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800110 size_t WriteCodeClassDef(OutputStream& out, size_t offset, size_t oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700111 const DexFile& dex_file, const DexFile::ClassDef& class_def);
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800112 size_t WriteCodeMethod(OutputStream& out, size_t offset, size_t oat_class_index,
Ian Rogers0571d352011-11-03 19:51:38 -0700113 size_t class_def_method_index, bool is_static, uint32_t method_idx,
114 const DexFile& dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700115
Ian Rogers0571d352011-11-03 19:51:38 -0700116 void ReportWriteFailure(const char* what, uint32_t method_idx, const DexFile& dex_file,
Brian Carlstromcd60ac72013-01-20 17:09:51 -0800117 OutputStream& out) const;
Elliott Hughes234da572011-11-03 22:13:06 -0700118
Brian Carlstrome24fa612011-09-29 00:53:55 -0700119 class OatDexFile {
120 public:
Brian Carlstrom265091e2013-01-30 14:08:26 -0800121 explicit OatDexFile(size_t offset, const DexFile& dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700122 size_t SizeOf() const;
123 void UpdateChecksum(OatHeader& oat_header) const;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700124 bool Write(OatWriter* oat_writer, OutputStream& out) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700125
Brian Carlstrom265091e2013-01-30 14:08:26 -0800126 // Offset of start of OatDexFile from beginning of OatHeader. It is
127 // used to validate file position when writing.
128 size_t offset_;
129
Brian Carlstrome24fa612011-09-29 00:53:55 -0700130 // data to write
131 uint32_t dex_file_location_size_;
132 const uint8_t* dex_file_location_data_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800133 uint32_t dex_file_location_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800134 uint32_t dex_file_offset_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700135 std::vector<uint32_t> methods_offsets_;
136
137 private:
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800138 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700139 };
140
Brian Carlstrom389efb02012-01-11 12:06:26 -0800141 class OatClass {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700142 public:
Brian Carlstrom265091e2013-01-30 14:08:26 -0800143 explicit OatClass(size_t offset, mirror::Class::Status status, uint32_t methods_count);
144 size_t GetOatMethodOffsetsOffsetFromOatHeader(size_t class_def_method_index_) const;
145 size_t GetOatMethodOffsetsOffsetFromOatClass(size_t class_def_method_index_) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700146 size_t SizeOf() const;
147 void UpdateChecksum(OatHeader& oat_header) const;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700148 bool Write(OatWriter* oat_writer, OutputStream& out) const;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700149
Brian Carlstrom265091e2013-01-30 14:08:26 -0800150 // Offset of start of OatClass from beginning of OatHeader. It is
151 // used to validate file position when writing. For Portable, it
152 // is also used to calculate the position of the OatMethodOffsets
153 // so that code pointers within the OatMethodOffsets can be
154 // patched to point to code in the Portable .o ELF objects.
155 size_t offset_;
156
Brian Carlstrome24fa612011-09-29 00:53:55 -0700157 // data to write
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158 mirror::Class::Status status_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700159 std::vector<OatMethodOffsets> method_offsets_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700160
161 private:
Brian Carlstrom389efb02012-01-11 12:06:26 -0800162 DISALLOW_COPY_AND_ASSIGN(OatClass);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700163 };
164
Ian Rogers1212a022013-03-04 10:48:41 -0800165 const CompilerDriver* const compiler_driver_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700166
Brian Carlstrome24fa612011-09-29 00:53:55 -0700167 // note OatFile does not take ownership of the DexFiles
168 const std::vector<const DexFile*>* dex_files_;
169
Brian Carlstrom28db0122012-10-18 16:20:41 -0700170 // dependencies on the image.
171 uint32_t image_file_location_oat_checksum_;
172 uint32_t image_file_location_oat_begin_;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700173 std::string image_file_location_;
174
Brian Carlstrome24fa612011-09-29 00:53:55 -0700175 // data to write
176 OatHeader* oat_header_;
177 std::vector<OatDexFile*> oat_dex_files_;
Brian Carlstrom389efb02012-01-11 12:06:26 -0800178 std::vector<OatClass*> oat_classes_;
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700179 UniquePtr<const std::vector<uint8_t> > interpreter_to_interpreter_entry_;
180 UniquePtr<const std::vector<uint8_t> > interpreter_to_quick_entry_;
181 UniquePtr<const std::vector<uint8_t> > portable_resolution_trampoline_;
182 UniquePtr<const std::vector<uint8_t> > quick_resolution_trampoline_;
183
184 // output stats
185 uint32_t size_dex_file_alignment_;
186 uint32_t size_executable_offset_alignment_;
187 uint32_t size_oat_header_;
188 uint32_t size_oat_header_image_file_location_;
189 uint32_t size_dex_file_;
190 uint32_t size_interpreter_to_interpreter_entry_;
191 uint32_t size_interpreter_to_quick_entry_;
192 uint32_t size_portable_resolution_trampoline_;
193 uint32_t size_quick_resolution_trampoline_;
194 uint32_t size_stubs_alignment_;
195 uint32_t size_code_size_;
196 uint32_t size_code_;
197 uint32_t size_code_alignment_;
198 uint32_t size_mapping_table_;
199 uint32_t size_vmap_table_;
200 uint32_t size_gc_map_;
201 uint32_t size_oat_dex_file_location_size_;
202 uint32_t size_oat_dex_file_location_data_;
203 uint32_t size_oat_dex_file_location_checksum_;
204 uint32_t size_oat_dex_file_offset_;
205 uint32_t size_oat_dex_file_methods_offsets_;
206 uint32_t size_oat_class_status_;
207 uint32_t size_oat_class_method_offsets_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700208
jeffhaof479dcc2011-11-02 15:54:15 -0700209 template <class T> struct MapCompare {
210 public:
211 bool operator() (const T* const &a, const T* const &b) const {
212 return *a < *b;
213 }
214 };
215
jeffhao55d78212011-11-02 11:41:50 -0700216 // code mappings for deduplication
Elliott Hughesa0e18062012-04-13 15:59:59 -0700217 SafeMap<const std::vector<uint8_t>*, uint32_t, MapCompare<std::vector<uint8_t> > > code_offsets_;
218 SafeMap<const std::vector<uint16_t>*, uint32_t, MapCompare<std::vector<uint16_t> > > vmap_table_offsets_;
219 SafeMap<const std::vector<uint32_t>*, uint32_t, MapCompare<std::vector<uint32_t> > > mapping_table_offsets_;
220 SafeMap<const std::vector<uint8_t>*, uint32_t, MapCompare<std::vector<uint8_t> > > gc_map_offsets_;
jeffhao55d78212011-11-02 11:41:50 -0700221
Brian Carlstrome24fa612011-09-29 00:53:55 -0700222 DISALLOW_COPY_AND_ASSIGN(OatWriter);
223};
224
225} // namespace art
226
227#endif // ART_SRC_OAT_WRITER_H_