blob: dbecb953629662887917fa1758a51e98c936fb9c [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>
Brian Carlstrom7940e442013-07-12 13:46:57 -070021#include <cstddef>
Ian Rogers700a4022014-05-19 16:49:03 -070022#include <memory>
Brian Carlstrom7940e442013-07-12 13:46:57 -070023
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"
Brian Carlstrom7940e442013-07-12 13:46:57 -070029
30namespace art {
31
Brian Carlstromba150c32013-08-27 17:31:03 -070032class BitVector;
Brian Carlstrom7940e442013-07-12 13:46:57 -070033class 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//
Vladimir Marko96c6ab92014-04-08 14:00:50 +010052// GcMap one variable sized blob with GC map.
53// GcMap GC maps are deduplicated.
54// ...
55// GcMap
56//
57// VmapTable one variable sized VmapTable blob (quick compiler only).
58// VmapTable VmapTables are deduplicated.
59// ...
60// VmapTable
61//
62// MappingTable one variable sized blob with MappingTable (quick compiler only).
63// MappingTable MappingTables are deduplicated.
64// ...
65// MappingTable
66//
Brian Carlstrom7940e442013-07-12 13:46:57 -070067// padding if necessary so that the following code will be page aligned
68//
Vladimir Marko96c6ab92014-04-08 14:00:50 +010069// OatMethodHeader fixed size header for a CompiledMethod including the size of the MethodCode.
70// MethodCode one variable sized blob with the code of a CompiledMethod.
71// OatMethodHeader (OatMethodHeader, MethodCode) pairs are deduplicated.
72// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -070073// ...
Vladimir Marko96c6ab92014-04-08 14:00:50 +010074// OatMethodHeader
75// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -070076//
77class OatWriter {
78 public:
Brian Carlstrom7940e442013-07-12 13:46:57 -070079 OatWriter(const std::vector<const DexFile*>& dex_files,
80 uint32_t image_file_location_oat_checksum,
Ian Rogersef7d42f2014-01-06 12:55:46 -080081 uintptr_t image_file_location_oat_begin,
Brian Carlstrom7940e442013-07-12 13:46:57 -070082 const std::string& image_file_location,
Ian Rogersca368cb2013-11-15 15:52:08 -080083 const CompilerDriver* compiler,
84 TimingLogger* timings);
Brian Carlstromc50d8e12013-07-23 22:35:16 -070085
86 const OatHeader& GetOatHeader() const {
87 return *oat_header_;
88 }
89
90 size_t GetSize() const {
91 return size_;
92 }
93
Ian Rogers3d504072014-03-01 09:16:49 -080094 bool Write(OutputStream* out);
Brian Carlstromc50d8e12013-07-23 22:35:16 -070095
Brian Carlstrom7940e442013-07-12 13:46:57 -070096 ~OatWriter();
97
Mark Mendellae9fd932014-02-10 16:14:35 -080098 struct DebugInfo {
99 DebugInfo(const std::string& method_name, uint32_t low_pc, uint32_t high_pc)
100 : method_name_(method_name), low_pc_(low_pc), high_pc_(high_pc) {
101 }
102 std::string method_name_;
103 uint32_t low_pc_;
104 uint32_t high_pc_;
105 };
106
107 const std::vector<DebugInfo>& GetCFIMethodInfo() const {
108 return method_info_;
109 }
110
Alex Light78382fa2014-06-06 15:45:32 -0700111 bool DidAddSymbols() const {
112 return compiler_driver_->DidIncludeDebugSymbols();
113 }
114
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700115 private:
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100116 // The DataAccess classes are helper classes that provide access to members related to
117 // a given map, i.e. GC map, mapping table or vmap table. By abstracting these away
118 // we can share a lot of code for processing the maps with template classes below.
119 struct GcMapDataAccess;
120 struct MappingTableDataAccess;
121 struct VmapTableDataAccess;
122
123 // The function VisitDexMethods() below iterates through all the methods in all
124 // the compiled dex files in order of their definitions. The method visitor
125 // classes provide individual bits of processing for each of the passes we need to
126 // first collect the data we want to write to the oat file and then, in later passes,
127 // to actually write it.
128 class DexMethodVisitor;
129 class OatDexMethodVisitor;
130 class InitOatClassesMethodVisitor;
131 class InitCodeMethodVisitor;
132 template <typename DataAccess>
133 class InitMapMethodVisitor;
134 class InitImageMethodVisitor;
135 class WriteCodeMethodVisitor;
136 template <typename DataAccess>
137 class WriteMapMethodVisitor;
138
139 // Visit all the methods in all the compiled dex files in their definition order
140 // with a given DexMethodVisitor.
141 bool VisitDexMethods(DexMethodVisitor* visitor);
142
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 size_t InitOatHeader();
144 size_t InitOatDexFiles(size_t offset);
145 size_t InitDexFiles(size_t offset);
146 size_t InitOatClasses(size_t offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100147 size_t InitOatMaps(size_t offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 size_t InitOatCode(size_t offset)
149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
150 size_t InitOatCodeDexFiles(size_t offset)
151 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152
Ian Rogers3d504072014-03-01 09:16:49 -0800153 bool WriteTables(OutputStream* out, const size_t file_offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100154 size_t WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset);
155 size_t WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset);
Ian Rogers3d504072014-03-01 09:16:49 -0800156 size_t WriteCodeDexFiles(OutputStream* out, const size_t file_offset, size_t relative_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157
158 class OatDexFile {
159 public:
160 explicit OatDexFile(size_t offset, const DexFile& dex_file);
161 size_t SizeOf() const;
Ian Rogers3d504072014-03-01 09:16:49 -0800162 void UpdateChecksum(OatHeader* oat_header) const;
163 bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700164
165 // Offset of start of OatDexFile from beginning of OatHeader. It is
166 // used to validate file position when writing.
167 size_t offset_;
168
169 // data to write
170 uint32_t dex_file_location_size_;
171 const uint8_t* dex_file_location_data_;
172 uint32_t dex_file_location_checksum_;
173 uint32_t dex_file_offset_;
174 std::vector<uint32_t> methods_offsets_;
175
176 private:
177 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
178 };
179
180 class OatClass {
181 public:
Brian Carlstromba150c32013-08-27 17:31:03 -0700182 explicit OatClass(size_t offset,
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100183 const std::vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -0700184 uint32_t num_non_null_compiled_methods,
185 mirror::Class::Status status);
186 ~OatClass();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700187 size_t GetOatMethodOffsetsOffsetFromOatHeader(size_t class_def_method_index_) const;
188 size_t GetOatMethodOffsetsOffsetFromOatClass(size_t class_def_method_index_) const;
189 size_t SizeOf() const;
Ian Rogers3d504072014-03-01 09:16:49 -0800190 void UpdateChecksum(OatHeader* oat_header) const;
191 bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700192
Brian Carlstromba150c32013-08-27 17:31:03 -0700193 CompiledMethod* GetCompiledMethod(size_t class_def_method_index) const {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100194 DCHECK_LT(class_def_method_index, compiled_methods_.size());
195 return compiled_methods_[class_def_method_index];
Brian Carlstromba150c32013-08-27 17:31:03 -0700196 }
197
Brian Carlstrom7940e442013-07-12 13:46:57 -0700198 // Offset of start of OatClass from beginning of OatHeader. It is
199 // used to validate file position when writing. For Portable, it
200 // is also used to calculate the position of the OatMethodOffsets
201 // so that code pointers within the OatMethodOffsets can be
202 // patched to point to code in the Portable .o ELF objects.
203 size_t offset_;
204
Brian Carlstromba150c32013-08-27 17:31:03 -0700205 // CompiledMethods for each class_def_method_index, or NULL if no method is available.
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100206 std::vector<CompiledMethod*> compiled_methods_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700207
208 // Offset from OatClass::offset_ to the OatMethodOffsets for the
209 // class_def_method_index. If 0, it means the corresponding
210 // CompiledMethod entry in OatClass::compiled_methods_ should be
211 // NULL and that the OatClass::type_ should be kOatClassBitmap.
212 std::vector<uint32_t> oat_method_offsets_offsets_from_oat_class_;
213
Brian Carlstrom7940e442013-07-12 13:46:57 -0700214 // data to write
Brian Carlstromba150c32013-08-27 17:31:03 -0700215
216 COMPILE_ASSERT(mirror::Class::Status::kStatusMax < (2 ^ 16), class_status_wont_fit_in_16bits);
217 int16_t status_;
218
219 COMPILE_ASSERT(OatClassType::kOatClassMax < (2 ^ 16), oat_class_type_wont_fit_in_16bits);
220 uint16_t type_;
221
222 uint32_t method_bitmap_size_;
223
224 // bit vector indexed by ClassDef method index. When
225 // OatClassType::type_ is kOatClassBitmap, a set bit indicates the
226 // method has an OatMethodOffsets in methods_offsets_, otherwise
227 // the entry was ommited to save space. If OatClassType::type_ is
228 // not is kOatClassBitmap, the bitmap will be NULL.
229 BitVector* method_bitmap_;
230
Vladimir Marko8a630572014-04-09 18:45:35 +0100231 // OatMethodOffsets and OatMethodHeaders for each CompiledMethod
232 // present in the OatClass. Note that some may be missing if
Brian Carlstromba150c32013-08-27 17:31:03 -0700233 // OatClass::compiled_methods_ contains NULL values (and
234 // oat_method_offsets_offsets_from_oat_class_ should contain 0
235 // values in this case).
Brian Carlstrom7940e442013-07-12 13:46:57 -0700236 std::vector<OatMethodOffsets> method_offsets_;
Vladimir Marko7624d252014-05-02 14:40:15 +0100237 std::vector<OatQuickMethodHeader> method_headers_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238
239 private:
240 DISALLOW_COPY_AND_ASSIGN(OatClass);
241 };
242
Mark Mendellae9fd932014-02-10 16:14:35 -0800243 std::vector<DebugInfo> method_info_;
244
Brian Carlstrom7940e442013-07-12 13:46:57 -0700245 const CompilerDriver* const compiler_driver_;
246
247 // note OatFile does not take ownership of the DexFiles
248 const std::vector<const DexFile*>* dex_files_;
249
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700250 // Size required for Oat data structures.
251 size_t size_;
252
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253 // dependencies on the image.
254 uint32_t image_file_location_oat_checksum_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800255 uintptr_t image_file_location_oat_begin_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256 std::string image_file_location_;
257
258 // data to write
259 OatHeader* oat_header_;
260 std::vector<OatDexFile*> oat_dex_files_;
261 std::vector<OatClass*> oat_classes_;
Ian Rogers700a4022014-05-19 16:49:03 -0700262 std::unique_ptr<const std::vector<uint8_t>> interpreter_to_interpreter_bridge_;
263 std::unique_ptr<const std::vector<uint8_t>> interpreter_to_compiled_code_bridge_;
264 std::unique_ptr<const std::vector<uint8_t>> jni_dlsym_lookup_;
265 std::unique_ptr<const std::vector<uint8_t>> portable_imt_conflict_trampoline_;
266 std::unique_ptr<const std::vector<uint8_t>> portable_resolution_trampoline_;
267 std::unique_ptr<const std::vector<uint8_t>> portable_to_interpreter_bridge_;
268 std::unique_ptr<const std::vector<uint8_t>> quick_generic_jni_trampoline_;
269 std::unique_ptr<const std::vector<uint8_t>> quick_imt_conflict_trampoline_;
270 std::unique_ptr<const std::vector<uint8_t>> quick_resolution_trampoline_;
271 std::unique_ptr<const std::vector<uint8_t>> quick_to_interpreter_bridge_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272
273 // output stats
274 uint32_t size_dex_file_alignment_;
275 uint32_t size_executable_offset_alignment_;
276 uint32_t size_oat_header_;
277 uint32_t size_oat_header_image_file_location_;
278 uint32_t size_dex_file_;
Ian Rogers468532e2013-08-05 10:56:33 -0700279 uint32_t size_interpreter_to_interpreter_bridge_;
280 uint32_t size_interpreter_to_compiled_code_bridge_;
281 uint32_t size_jni_dlsym_lookup_;
Jeff Hao88474b42013-10-23 16:24:40 -0700282 uint32_t size_portable_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 uint32_t size_portable_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700284 uint32_t size_portable_to_interpreter_bridge_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800285 uint32_t size_quick_generic_jni_trampoline_;
Jeff Hao88474b42013-10-23 16:24:40 -0700286 uint32_t size_quick_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287 uint32_t size_quick_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700288 uint32_t size_quick_to_interpreter_bridge_;
289 uint32_t size_trampoline_alignment_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100290 uint32_t size_method_header_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291 uint32_t size_code_;
292 uint32_t size_code_alignment_;
293 uint32_t size_mapping_table_;
294 uint32_t size_vmap_table_;
295 uint32_t size_gc_map_;
296 uint32_t size_oat_dex_file_location_size_;
297 uint32_t size_oat_dex_file_location_data_;
298 uint32_t size_oat_dex_file_location_checksum_;
299 uint32_t size_oat_dex_file_offset_;
300 uint32_t size_oat_dex_file_methods_offsets_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700301 uint32_t size_oat_class_type_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302 uint32_t size_oat_class_status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700303 uint32_t size_oat_class_method_bitmaps_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700304 uint32_t size_oat_class_method_offsets_;
305
Vladimir Marko8a630572014-04-09 18:45:35 +0100306 struct CodeOffsetsKeyComparator {
307 bool operator()(const CompiledMethod* lhs, const CompiledMethod* rhs) const {
308 if (lhs->GetQuickCode() != rhs->GetQuickCode()) {
309 return lhs->GetQuickCode() < rhs->GetQuickCode();
310 }
311 // If the code is the same, all other fields are likely to be the same as well.
312 if (UNLIKELY(&lhs->GetMappingTable() != &rhs->GetMappingTable())) {
313 return &lhs->GetMappingTable() < &rhs->GetMappingTable();
314 }
315 if (UNLIKELY(&lhs->GetVmapTable() != &rhs->GetVmapTable())) {
316 return &lhs->GetVmapTable() < &rhs->GetVmapTable();
317 }
318 return false;
319 }
320 };
321
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322 DISALLOW_COPY_AND_ASSIGN(OatWriter);
323};
324
325} // namespace art
326
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700327#endif // ART_COMPILER_OAT_WRITER_H_