blob: 5d947cfaeaa98ef67540b6f2d9ab7a174535e62f [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_COMPILER_OAT_WRITER_H_
18#define ART_COMPILER_OAT_WRITER_H_
Brian Carlstrom7940e442013-07-12 13:46:57 -070019
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
Brian Carlstromba150c32013-08-27 17:31:03 -070033class BitVector;
Brian Carlstrom7940e442013-07-12 13:46:57 -070034class OutputStream;
35
36// OatHeader variable length with count of D OatDexFiles
37//
38// OatDexFile[0] one variable sized OatDexFile with offsets to Dex and OatClasses
39// OatDexFile[1]
40// ...
41// OatDexFile[D]
42//
43// Dex[0] one variable sized DexFile for each OatDexFile.
44// Dex[1] these are literal copies of the input .dex files.
45// ...
46// Dex[D]
47//
48// OatClass[0] one variable sized OatClass for each of C DexFile::ClassDefs
49// OatClass[1] contains OatClass entries with class status, offsets to code, etc.
50// ...
51// OatClass[C]
52//
53// padding if necessary so that the following code will be page aligned
54//
55// CompiledMethod one variable sized blob with the contents of each CompiledMethod
56// CompiledMethod
57// CompiledMethod
58// CompiledMethod
59// CompiledMethod
60// CompiledMethod
61// ...
62// CompiledMethod
63//
64class OatWriter {
65 public:
Brian Carlstrom7940e442013-07-12 13:46:57 -070066 OatWriter(const std::vector<const DexFile*>& dex_files,
67 uint32_t image_file_location_oat_checksum,
68 uint32_t image_file_location_oat_begin,
69 const std::string& image_file_location,
Ian Rogers3f3d22c2013-08-27 18:11:09 -070070 const CompilerDriver* compiler);
Brian Carlstromc50d8e12013-07-23 22:35:16 -070071
72 const OatHeader& GetOatHeader() const {
73 return *oat_header_;
74 }
75
76 size_t GetSize() const {
77 return size_;
78 }
79
80 bool Write(OutputStream& out);
81
Brian Carlstrom7940e442013-07-12 13:46:57 -070082 ~OatWriter();
83
Brian Carlstromc50d8e12013-07-23 22:35:16 -070084 private:
Brian Carlstrom7940e442013-07-12 13:46:57 -070085 size_t InitOatHeader();
86 size_t InitOatDexFiles(size_t offset);
87 size_t InitDexFiles(size_t offset);
88 size_t InitOatClasses(size_t offset);
89 size_t InitOatCode(size_t offset)
90 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
91 size_t InitOatCodeDexFiles(size_t offset)
92 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
93 size_t InitOatCodeDexFile(size_t offset,
Brian Carlstromba150c32013-08-27 17:31:03 -070094 size_t* oat_class_index,
Brian Carlstrom7940e442013-07-12 13:46:57 -070095 const DexFile& dex_file)
96 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
97 size_t InitOatCodeClassDef(size_t offset,
98 size_t oat_class_index, size_t class_def_index,
99 const DexFile& dex_file,
100 const DexFile::ClassDef& class_def)
101 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
102 size_t InitOatCodeMethod(size_t offset, size_t oat_class_index, size_t class_def_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700103 size_t class_def_method_index, size_t* method_offsets_index,
104 bool is_native, InvokeType type, uint32_t method_idx, const DexFile&)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700105 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
106
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700107 bool WriteTables(OutputStream& out, const size_t file_offset);
108 size_t WriteCode(OutputStream& out, const size_t file_offset);
109 size_t WriteCodeDexFiles(OutputStream& out, const size_t file_offset, size_t relative_offset);
110 size_t WriteCodeDexFile(OutputStream& out, const size_t file_offset, size_t relative_offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700111 size_t* oat_class_index, const DexFile& dex_file);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700112 size_t WriteCodeClassDef(OutputStream& out, const size_t file_offset, size_t relative_offset,
113 size_t oat_class_index, const DexFile& dex_file,
114 const DexFile::ClassDef& class_def);
115 size_t WriteCodeMethod(OutputStream& out, const size_t file_offset, size_t relative_offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700116 size_t oat_class_index, size_t class_def_method_index,
117 size_t* method_offsets_index, bool is_static, uint32_t method_idx,
118 const DexFile& dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119
120 void ReportWriteFailure(const char* what, uint32_t method_idx, const DexFile& dex_file,
121 OutputStream& out) const;
122
123 class OatDexFile {
124 public:
125 explicit OatDexFile(size_t offset, const DexFile& dex_file);
126 size_t SizeOf() const;
127 void UpdateChecksum(OatHeader& oat_header) const;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700128 bool Write(OatWriter* oat_writer, OutputStream& out, const size_t file_offset) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129
130 // Offset of start of OatDexFile from beginning of OatHeader. It is
131 // used to validate file position when writing.
132 size_t offset_;
133
134 // data to write
135 uint32_t dex_file_location_size_;
136 const uint8_t* dex_file_location_data_;
137 uint32_t dex_file_location_checksum_;
138 uint32_t dex_file_offset_;
139 std::vector<uint32_t> methods_offsets_;
140
141 private:
142 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
143 };
144
145 class OatClass {
146 public:
Brian Carlstromba150c32013-08-27 17:31:03 -0700147 explicit OatClass(size_t offset,
148 std::vector<CompiledMethod*>* compiled_methods,
149 uint32_t num_non_null_compiled_methods,
150 mirror::Class::Status status);
151 ~OatClass();
152#if defined(ART_USE_PORTABLE_COMPILER)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700153 size_t GetOatMethodOffsetsOffsetFromOatHeader(size_t class_def_method_index_) const;
154 size_t GetOatMethodOffsetsOffsetFromOatClass(size_t class_def_method_index_) const;
Brian Carlstromba150c32013-08-27 17:31:03 -0700155#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156 size_t SizeOf() const;
157 void UpdateChecksum(OatHeader& oat_header) const;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700158 bool Write(OatWriter* oat_writer, OutputStream& out, const size_t file_offset) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700159
Brian Carlstromba150c32013-08-27 17:31:03 -0700160 CompiledMethod* GetCompiledMethod(size_t class_def_method_index) const {
161 DCHECK(compiled_methods_ != NULL);
162 return (*compiled_methods_)[class_def_method_index];
163 }
164
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 // Offset of start of OatClass from beginning of OatHeader. It is
166 // used to validate file position when writing. For Portable, it
167 // is also used to calculate the position of the OatMethodOffsets
168 // so that code pointers within the OatMethodOffsets can be
169 // patched to point to code in the Portable .o ELF objects.
170 size_t offset_;
171
Brian Carlstromba150c32013-08-27 17:31:03 -0700172 // CompiledMethods for each class_def_method_index, or NULL if no method is available.
173 std::vector<CompiledMethod*>* compiled_methods_;
174
175 // Offset from OatClass::offset_ to the OatMethodOffsets for the
176 // class_def_method_index. If 0, it means the corresponding
177 // CompiledMethod entry in OatClass::compiled_methods_ should be
178 // NULL and that the OatClass::type_ should be kOatClassBitmap.
179 std::vector<uint32_t> oat_method_offsets_offsets_from_oat_class_;
180
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181 // data to write
Brian Carlstromba150c32013-08-27 17:31:03 -0700182
183 COMPILE_ASSERT(mirror::Class::Status::kStatusMax < (2 ^ 16), class_status_wont_fit_in_16bits);
184 int16_t status_;
185
186 COMPILE_ASSERT(OatClassType::kOatClassMax < (2 ^ 16), oat_class_type_wont_fit_in_16bits);
187 uint16_t type_;
188
189 uint32_t method_bitmap_size_;
190
191 // bit vector indexed by ClassDef method index. When
192 // OatClassType::type_ is kOatClassBitmap, a set bit indicates the
193 // method has an OatMethodOffsets in methods_offsets_, otherwise
194 // the entry was ommited to save space. If OatClassType::type_ is
195 // not is kOatClassBitmap, the bitmap will be NULL.
196 BitVector* method_bitmap_;
197
198 // OatMethodOffsets for each CompiledMethod present in the
199 // OatClass. Note that some may be missing if
200 // OatClass::compiled_methods_ contains NULL values (and
201 // oat_method_offsets_offsets_from_oat_class_ should contain 0
202 // values in this case).
Brian Carlstrom7940e442013-07-12 13:46:57 -0700203 std::vector<OatMethodOffsets> method_offsets_;
204
205 private:
206 DISALLOW_COPY_AND_ASSIGN(OatClass);
207 };
208
209 const CompilerDriver* const compiler_driver_;
210
211 // note OatFile does not take ownership of the DexFiles
212 const std::vector<const DexFile*>* dex_files_;
213
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700214 // Size required for Oat data structures.
215 size_t size_;
216
Brian Carlstrom7940e442013-07-12 13:46:57 -0700217 // dependencies on the image.
218 uint32_t image_file_location_oat_checksum_;
219 uint32_t image_file_location_oat_begin_;
220 std::string image_file_location_;
221
222 // data to write
223 OatHeader* oat_header_;
224 std::vector<OatDexFile*> oat_dex_files_;
225 std::vector<OatClass*> oat_classes_;
Ian Rogers468532e2013-08-05 10:56:33 -0700226 UniquePtr<const std::vector<uint8_t> > interpreter_to_interpreter_bridge_;
227 UniquePtr<const std::vector<uint8_t> > interpreter_to_compiled_code_bridge_;
228 UniquePtr<const std::vector<uint8_t> > jni_dlsym_lookup_;
Jeff Hao88474b42013-10-23 16:24:40 -0700229 UniquePtr<const std::vector<uint8_t> > portable_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700230 UniquePtr<const std::vector<uint8_t> > portable_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700231 UniquePtr<const std::vector<uint8_t> > portable_to_interpreter_bridge_;
Jeff Hao88474b42013-10-23 16:24:40 -0700232 UniquePtr<const std::vector<uint8_t> > quick_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700233 UniquePtr<const std::vector<uint8_t> > quick_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700234 UniquePtr<const std::vector<uint8_t> > quick_to_interpreter_bridge_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700235
236 // output stats
237 uint32_t size_dex_file_alignment_;
238 uint32_t size_executable_offset_alignment_;
239 uint32_t size_oat_header_;
240 uint32_t size_oat_header_image_file_location_;
241 uint32_t size_dex_file_;
Ian Rogers468532e2013-08-05 10:56:33 -0700242 uint32_t size_interpreter_to_interpreter_bridge_;
243 uint32_t size_interpreter_to_compiled_code_bridge_;
244 uint32_t size_jni_dlsym_lookup_;
Jeff Hao88474b42013-10-23 16:24:40 -0700245 uint32_t size_portable_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 uint32_t size_portable_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700247 uint32_t size_portable_to_interpreter_bridge_;
Jeff Hao88474b42013-10-23 16:24:40 -0700248 uint32_t size_quick_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700249 uint32_t size_quick_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700250 uint32_t size_quick_to_interpreter_bridge_;
251 uint32_t size_trampoline_alignment_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 uint32_t size_code_size_;
253 uint32_t size_code_;
254 uint32_t size_code_alignment_;
255 uint32_t size_mapping_table_;
256 uint32_t size_vmap_table_;
257 uint32_t size_gc_map_;
258 uint32_t size_oat_dex_file_location_size_;
259 uint32_t size_oat_dex_file_location_data_;
260 uint32_t size_oat_dex_file_location_checksum_;
261 uint32_t size_oat_dex_file_offset_;
262 uint32_t size_oat_dex_file_methods_offsets_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700263 uint32_t size_oat_class_type_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700264 uint32_t size_oat_class_status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700265 uint32_t size_oat_class_method_bitmaps_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 uint32_t size_oat_class_method_offsets_;
267
Mathieu Chartier193bad92013-08-29 18:46:00 -0700268 // Code mappings for deduplication. Deduplication is already done on a pointer basis by the
269 // compiler driver, so we can simply compare the pointers to find out if things are duplicated.
270 SafeMap<const std::vector<uint8_t>*, uint32_t> code_offsets_;
271 SafeMap<const std::vector<uint8_t>*, uint32_t> vmap_table_offsets_;
272 SafeMap<const std::vector<uint8_t>*, uint32_t> mapping_table_offsets_;
273 SafeMap<const std::vector<uint8_t>*, uint32_t> gc_map_offsets_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274
275 DISALLOW_COPY_AND_ASSIGN(OatWriter);
276};
277
278} // namespace art
279
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700280#endif // ART_COMPILER_OAT_WRITER_H_