blob: bab1a26d443dd4e197849191988a38c856f8f9ab [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,
Ian Rogersef7d42f2014-01-06 12:55:46 -080068 uintptr_t image_file_location_oat_begin,
Brian Carlstrom7940e442013-07-12 13:46:57 -070069 const std::string& image_file_location,
Ian Rogersca368cb2013-11-15 15:52:08 -080070 const CompilerDriver* compiler,
71 TimingLogger* timings);
Brian Carlstromc50d8e12013-07-23 22:35:16 -070072
73 const OatHeader& GetOatHeader() const {
74 return *oat_header_;
75 }
76
77 size_t GetSize() const {
78 return size_;
79 }
80
Ian Rogers3d504072014-03-01 09:16:49 -080081 bool Write(OutputStream* out);
Brian Carlstromc50d8e12013-07-23 22:35:16 -070082
Brian Carlstrom7940e442013-07-12 13:46:57 -070083 ~OatWriter();
84
Mark Mendellae9fd932014-02-10 16:14:35 -080085 struct DebugInfo {
86 DebugInfo(const std::string& method_name, uint32_t low_pc, uint32_t high_pc)
87 : method_name_(method_name), low_pc_(low_pc), high_pc_(high_pc) {
88 }
89 std::string method_name_;
90 uint32_t low_pc_;
91 uint32_t high_pc_;
92 };
93
94 const std::vector<DebugInfo>& GetCFIMethodInfo() const {
95 return method_info_;
96 }
97
Brian Carlstromc50d8e12013-07-23 22:35:16 -070098 private:
Brian Carlstrom7940e442013-07-12 13:46:57 -070099 size_t InitOatHeader();
100 size_t InitOatDexFiles(size_t offset);
101 size_t InitDexFiles(size_t offset);
102 size_t InitOatClasses(size_t offset);
103 size_t InitOatCode(size_t offset)
104 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
105 size_t InitOatCodeDexFiles(size_t offset)
106 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
107 size_t InitOatCodeDexFile(size_t offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700108 size_t* oat_class_index,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700109 const DexFile& dex_file)
110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
111 size_t InitOatCodeClassDef(size_t offset,
112 size_t oat_class_index, size_t class_def_index,
113 const DexFile& dex_file,
114 const DexFile::ClassDef& class_def)
115 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
116 size_t InitOatCodeMethod(size_t offset, size_t oat_class_index, size_t class_def_index,
Brian Carlstromba150c32013-08-27 17:31:03 -0700117 size_t class_def_method_index, size_t* method_offsets_index,
118 bool is_native, InvokeType type, uint32_t method_idx, const DexFile&)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
120
Ian Rogers3d504072014-03-01 09:16:49 -0800121 bool WriteTables(OutputStream* out, const size_t file_offset);
122 size_t WriteCode(OutputStream* out, const size_t file_offset);
123 size_t WriteCodeDexFiles(OutputStream* out, const size_t file_offset, size_t relative_offset);
124 size_t WriteCodeDexFile(OutputStream* out, const size_t file_offset, size_t relative_offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700125 size_t* oat_class_index, const DexFile& dex_file);
Ian Rogers3d504072014-03-01 09:16:49 -0800126 size_t WriteCodeClassDef(OutputStream* out, const size_t file_offset, size_t relative_offset,
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700127 size_t oat_class_index, const DexFile& dex_file,
128 const DexFile::ClassDef& class_def);
Ian Rogers3d504072014-03-01 09:16:49 -0800129 size_t WriteCodeMethod(OutputStream* out, const size_t file_offset, size_t relative_offset,
Brian Carlstromba150c32013-08-27 17:31:03 -0700130 size_t oat_class_index, size_t class_def_method_index,
131 size_t* method_offsets_index, bool is_static, uint32_t method_idx,
132 const DexFile& dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133
134 void ReportWriteFailure(const char* what, uint32_t method_idx, const DexFile& dex_file,
Ian Rogers3d504072014-03-01 09:16:49 -0800135 const OutputStream& out) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136
137 class OatDexFile {
138 public:
139 explicit OatDexFile(size_t offset, const DexFile& dex_file);
140 size_t SizeOf() const;
Ian Rogers3d504072014-03-01 09:16:49 -0800141 void UpdateChecksum(OatHeader* oat_header) const;
142 bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143
144 // Offset of start of OatDexFile from beginning of OatHeader. It is
145 // used to validate file position when writing.
146 size_t offset_;
147
148 // data to write
149 uint32_t dex_file_location_size_;
150 const uint8_t* dex_file_location_data_;
151 uint32_t dex_file_location_checksum_;
152 uint32_t dex_file_offset_;
153 std::vector<uint32_t> methods_offsets_;
154
155 private:
156 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
157 };
158
159 class OatClass {
160 public:
Brian Carlstromba150c32013-08-27 17:31:03 -0700161 explicit OatClass(size_t offset,
162 std::vector<CompiledMethod*>* compiled_methods,
163 uint32_t num_non_null_compiled_methods,
164 mirror::Class::Status status);
165 ~OatClass();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166 size_t GetOatMethodOffsetsOffsetFromOatHeader(size_t class_def_method_index_) const;
167 size_t GetOatMethodOffsetsOffsetFromOatClass(size_t class_def_method_index_) const;
168 size_t SizeOf() const;
Ian Rogers3d504072014-03-01 09:16:49 -0800169 void UpdateChecksum(OatHeader* oat_header) const;
170 bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171
Brian Carlstromba150c32013-08-27 17:31:03 -0700172 CompiledMethod* GetCompiledMethod(size_t class_def_method_index) const {
173 DCHECK(compiled_methods_ != NULL);
174 return (*compiled_methods_)[class_def_method_index];
175 }
176
Brian Carlstrom7940e442013-07-12 13:46:57 -0700177 // Offset of start of OatClass from beginning of OatHeader. It is
178 // used to validate file position when writing. For Portable, it
179 // is also used to calculate the position of the OatMethodOffsets
180 // so that code pointers within the OatMethodOffsets can be
181 // patched to point to code in the Portable .o ELF objects.
182 size_t offset_;
183
Brian Carlstromba150c32013-08-27 17:31:03 -0700184 // CompiledMethods for each class_def_method_index, or NULL if no method is available.
185 std::vector<CompiledMethod*>* compiled_methods_;
186
187 // Offset from OatClass::offset_ to the OatMethodOffsets for the
188 // class_def_method_index. If 0, it means the corresponding
189 // CompiledMethod entry in OatClass::compiled_methods_ should be
190 // NULL and that the OatClass::type_ should be kOatClassBitmap.
191 std::vector<uint32_t> oat_method_offsets_offsets_from_oat_class_;
192
Brian Carlstrom7940e442013-07-12 13:46:57 -0700193 // data to write
Brian Carlstromba150c32013-08-27 17:31:03 -0700194
195 COMPILE_ASSERT(mirror::Class::Status::kStatusMax < (2 ^ 16), class_status_wont_fit_in_16bits);
196 int16_t status_;
197
198 COMPILE_ASSERT(OatClassType::kOatClassMax < (2 ^ 16), oat_class_type_wont_fit_in_16bits);
199 uint16_t type_;
200
201 uint32_t method_bitmap_size_;
202
203 // bit vector indexed by ClassDef method index. When
204 // OatClassType::type_ is kOatClassBitmap, a set bit indicates the
205 // method has an OatMethodOffsets in methods_offsets_, otherwise
206 // the entry was ommited to save space. If OatClassType::type_ is
207 // not is kOatClassBitmap, the bitmap will be NULL.
208 BitVector* method_bitmap_;
209
210 // OatMethodOffsets for each CompiledMethod present in the
211 // OatClass. Note that some may be missing if
212 // OatClass::compiled_methods_ contains NULL values (and
213 // oat_method_offsets_offsets_from_oat_class_ should contain 0
214 // values in this case).
Brian Carlstrom7940e442013-07-12 13:46:57 -0700215 std::vector<OatMethodOffsets> method_offsets_;
216
217 private:
218 DISALLOW_COPY_AND_ASSIGN(OatClass);
219 };
220
Mark Mendellae9fd932014-02-10 16:14:35 -0800221 std::vector<DebugInfo> method_info_;
222
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223 const CompilerDriver* const compiler_driver_;
224
225 // note OatFile does not take ownership of the DexFiles
226 const std::vector<const DexFile*>* dex_files_;
227
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700228 // Size required for Oat data structures.
229 size_t size_;
230
Brian Carlstrom7940e442013-07-12 13:46:57 -0700231 // dependencies on the image.
232 uint32_t image_file_location_oat_checksum_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800233 uintptr_t image_file_location_oat_begin_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234 std::string image_file_location_;
235
236 // data to write
237 OatHeader* oat_header_;
238 std::vector<OatDexFile*> oat_dex_files_;
239 std::vector<OatClass*> oat_classes_;
Ian Rogers468532e2013-08-05 10:56:33 -0700240 UniquePtr<const std::vector<uint8_t> > interpreter_to_interpreter_bridge_;
241 UniquePtr<const std::vector<uint8_t> > interpreter_to_compiled_code_bridge_;
242 UniquePtr<const std::vector<uint8_t> > jni_dlsym_lookup_;
Jeff Hao88474b42013-10-23 16:24:40 -0700243 UniquePtr<const std::vector<uint8_t> > portable_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700244 UniquePtr<const std::vector<uint8_t> > portable_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700245 UniquePtr<const std::vector<uint8_t> > portable_to_interpreter_bridge_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800246 UniquePtr<const std::vector<uint8_t> > quick_generic_jni_trampoline_;
Jeff Hao88474b42013-10-23 16:24:40 -0700247 UniquePtr<const std::vector<uint8_t> > quick_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 UniquePtr<const std::vector<uint8_t> > quick_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700249 UniquePtr<const std::vector<uint8_t> > quick_to_interpreter_bridge_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700250
251 // output stats
252 uint32_t size_dex_file_alignment_;
253 uint32_t size_executable_offset_alignment_;
254 uint32_t size_oat_header_;
255 uint32_t size_oat_header_image_file_location_;
256 uint32_t size_dex_file_;
Ian Rogers468532e2013-08-05 10:56:33 -0700257 uint32_t size_interpreter_to_interpreter_bridge_;
258 uint32_t size_interpreter_to_compiled_code_bridge_;
259 uint32_t size_jni_dlsym_lookup_;
Jeff Hao88474b42013-10-23 16:24:40 -0700260 uint32_t size_portable_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261 uint32_t size_portable_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700262 uint32_t size_portable_to_interpreter_bridge_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800263 uint32_t size_quick_generic_jni_trampoline_;
Jeff Hao88474b42013-10-23 16:24:40 -0700264 uint32_t size_quick_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 uint32_t size_quick_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700266 uint32_t size_quick_to_interpreter_bridge_;
267 uint32_t size_trampoline_alignment_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700268 uint32_t size_code_size_;
269 uint32_t size_code_;
270 uint32_t size_code_alignment_;
271 uint32_t size_mapping_table_;
272 uint32_t size_vmap_table_;
273 uint32_t size_gc_map_;
274 uint32_t size_oat_dex_file_location_size_;
275 uint32_t size_oat_dex_file_location_data_;
276 uint32_t size_oat_dex_file_location_checksum_;
277 uint32_t size_oat_dex_file_offset_;
278 uint32_t size_oat_dex_file_methods_offsets_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700279 uint32_t size_oat_class_type_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 uint32_t size_oat_class_status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700281 uint32_t size_oat_class_method_bitmaps_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282 uint32_t size_oat_class_method_offsets_;
283
Mathieu Chartier193bad92013-08-29 18:46:00 -0700284 // Code mappings for deduplication. Deduplication is already done on a pointer basis by the
285 // compiler driver, so we can simply compare the pointers to find out if things are duplicated.
286 SafeMap<const std::vector<uint8_t>*, uint32_t> code_offsets_;
287 SafeMap<const std::vector<uint8_t>*, uint32_t> vmap_table_offsets_;
288 SafeMap<const std::vector<uint8_t>*, uint32_t> mapping_table_offsets_;
289 SafeMap<const std::vector<uint8_t>*, uint32_t> gc_map_offsets_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700290
291 DISALLOW_COPY_AND_ASSIGN(OatWriter);
292};
293
294} // namespace art
295
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700296#endif // ART_COMPILER_OAT_WRITER_H_