blob: 1f97bf853c63765d36155da33327b5a23fe40113 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
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 */
16
17#ifndef ART_SRC_OAT_WRITER_H_
18#define ART_SRC_OAT_WRITER_H_
19
20#include <stdint.h>
21
22#include <cstddef>
23
24#include "driver/compiler_driver.h"
25#include "mem_map.h"
26#include "oat.h"
27#include "mirror/class.h"
28#include "safe_map.h"
29#include "UniquePtr.h"
30
31namespace art {
32
33class OutputStream;
34
35// OatHeader variable length with count of D OatDexFiles
36//
37// OatDexFile[0] one variable sized OatDexFile with offsets to Dex and OatClasses
38// OatDexFile[1]
39// ...
40// OatDexFile[D]
41//
42// 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//
47// 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.
49// ...
50// OatClass[C]
51//
52// padding if necessary so that the following code will be page aligned
53//
54// CompiledMethod one variable sized blob with the contents of each CompiledMethod
55// CompiledMethod
56// CompiledMethod
57// CompiledMethod
58// CompiledMethod
59// CompiledMethod
60// ...
61// CompiledMethod
62//
63class OatWriter {
64 public:
65 // Write an oat file. Returns true on success, false on failure.
66 static bool Create(OutputStream& out,
67 const std::vector<const DexFile*>& dex_files,
68 uint32_t image_file_location_oat_checksum,
69 uint32_t image_file_location_oat_begin,
70 const std::string& image_file_location,
71 const CompilerDriver& compiler)
72 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
73
74 private:
75 OatWriter(const std::vector<const DexFile*>& dex_files,
76 uint32_t image_file_location_oat_checksum,
77 uint32_t image_file_location_oat_begin,
78 const std::string& image_file_location,
79 const CompilerDriver* compiler) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
80 ~OatWriter();
81
82 size_t InitOatHeader();
83 size_t InitOatDexFiles(size_t offset);
84 size_t InitDexFiles(size_t offset);
85 size_t InitOatClasses(size_t offset);
86 size_t InitOatCode(size_t offset)
87 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
88 size_t InitOatCodeDexFiles(size_t offset)
89 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
90 size_t InitOatCodeDexFile(size_t offset,
91 size_t& oat_class_index,
92 const DexFile& dex_file)
93 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
94 size_t InitOatCodeClassDef(size_t offset,
95 size_t oat_class_index, size_t class_def_index,
96 const DexFile& dex_file,
97 const DexFile::ClassDef& class_def)
98 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
99 size_t InitOatCodeMethod(size_t offset, size_t oat_class_index, size_t class_def_index,
100 size_t class_def_method_index, bool is_native, InvokeType type,
101 uint32_t method_idx, const DexFile*)
102 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
103
104 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,
109 const DexFile& dex_file);
110 size_t WriteCodeClassDef(OutputStream& out, size_t offset, size_t oat_class_index,
111 const DexFile& dex_file, const DexFile::ClassDef& class_def);
112 size_t WriteCodeMethod(OutputStream& out, size_t offset, size_t oat_class_index,
113 size_t class_def_method_index, bool is_static, uint32_t method_idx,
114 const DexFile& dex_file);
115
116 void ReportWriteFailure(const char* what, uint32_t method_idx, const DexFile& dex_file,
117 OutputStream& out) const;
118
119 class OatDexFile {
120 public:
121 explicit OatDexFile(size_t offset, const DexFile& dex_file);
122 size_t SizeOf() const;
123 void UpdateChecksum(OatHeader& oat_header) const;
124 bool Write(OatWriter* oat_writer, OutputStream& out) const;
125
126 // Offset of start of OatDexFile from beginning of OatHeader. It is
127 // used to validate file position when writing.
128 size_t offset_;
129
130 // data to write
131 uint32_t dex_file_location_size_;
132 const uint8_t* dex_file_location_data_;
133 uint32_t dex_file_location_checksum_;
134 uint32_t dex_file_offset_;
135 std::vector<uint32_t> methods_offsets_;
136
137 private:
138 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
139 };
140
141 class OatClass {
142 public:
143 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;
146 size_t SizeOf() const;
147 void UpdateChecksum(OatHeader& oat_header) const;
148 bool Write(OatWriter* oat_writer, OutputStream& out) const;
149
150 // 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
157 // data to write
158 mirror::Class::Status status_;
159 std::vector<OatMethodOffsets> method_offsets_;
160
161 private:
162 DISALLOW_COPY_AND_ASSIGN(OatClass);
163 };
164
165 const CompilerDriver* const compiler_driver_;
166
167 // note OatFile does not take ownership of the DexFiles
168 const std::vector<const DexFile*>* dex_files_;
169
170 // dependencies on the image.
171 uint32_t image_file_location_oat_checksum_;
172 uint32_t image_file_location_oat_begin_;
173 std::string image_file_location_;
174
175 // data to write
176 OatHeader* oat_header_;
177 std::vector<OatDexFile*> oat_dex_files_;
178 std::vector<OatClass*> oat_classes_;
179 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_;
208
209 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
216 // code mappings for deduplication
217 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_;
221
222 DISALLOW_COPY_AND_ASSIGN(OatWriter);
223};
224
225} // namespace art
226
227#endif // ART_SRC_OAT_WRITER_H_