blob: 11f8bffd11396ea8f4614a47ea413e2ce945a6e4 [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;
Andreas Gampe79273802014-08-05 20:21:05 -070033class CompiledMethod;
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//
Vladimir Marko96c6ab92014-04-08 14:00:50 +010053// GcMap one variable sized blob with GC map.
54// GcMap GC maps are deduplicated.
55// ...
56// GcMap
57//
58// VmapTable one variable sized VmapTable blob (quick compiler only).
59// VmapTable VmapTables are deduplicated.
60// ...
61// VmapTable
62//
63// MappingTable one variable sized blob with MappingTable (quick compiler only).
64// MappingTable MappingTables are deduplicated.
65// ...
66// MappingTable
67//
Brian Carlstrom7940e442013-07-12 13:46:57 -070068// padding if necessary so that the following code will be page aligned
69//
Vladimir Marko96c6ab92014-04-08 14:00:50 +010070// OatMethodHeader fixed size header for a CompiledMethod including the size of the MethodCode.
71// MethodCode one variable sized blob with the code of a CompiledMethod.
72// OatMethodHeader (OatMethodHeader, MethodCode) pairs are deduplicated.
73// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -070074// ...
Vladimir Marko96c6ab92014-04-08 14:00:50 +010075// OatMethodHeader
76// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -070077//
78class OatWriter {
79 public:
Brian Carlstrom7940e442013-07-12 13:46:57 -070080 OatWriter(const std::vector<const DexFile*>& dex_files,
81 uint32_t image_file_location_oat_checksum,
Ian Rogersef7d42f2014-01-06 12:55:46 -080082 uintptr_t image_file_location_oat_begin,
Alex Lighta59dd802014-07-02 16:28:08 -070083 int32_t image_patch_delta,
Ian Rogersca368cb2013-11-15 15:52:08 -080084 const CompilerDriver* compiler,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070085 TimingLogger* timings,
86 SafeMap<std::string, std::string>* key_value_store);
Brian Carlstromc50d8e12013-07-23 22:35:16 -070087
88 const OatHeader& GetOatHeader() const {
89 return *oat_header_;
90 }
91
92 size_t GetSize() const {
93 return size_;
94 }
95
Ian Rogers3d504072014-03-01 09:16:49 -080096 bool Write(OutputStream* out);
Brian Carlstromc50d8e12013-07-23 22:35:16 -070097
Brian Carlstrom7940e442013-07-12 13:46:57 -070098 ~OatWriter();
99
Mark Mendellae9fd932014-02-10 16:14:35 -0800100 struct DebugInfo {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700101 DebugInfo(const std::string& method_name, const char* src_file_name,
102 uint32_t low_pc, uint32_t high_pc, const uint8_t* dbgstream,
Andreas Gampe79273802014-08-05 20:21:05 -0700103 CompiledMethod* compiled_method)
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700104 : method_name_(method_name), src_file_name_(src_file_name),
105 low_pc_(low_pc), high_pc_(high_pc), dbgstream_(dbgstream),
Andreas Gampe79273802014-08-05 20:21:05 -0700106 compiled_method_(compiled_method) {
Mark Mendellae9fd932014-02-10 16:14:35 -0800107 }
Andreas Gampe79273802014-08-05 20:21:05 -0700108 std::string method_name_; // Note: this name is a pretty-printed name.
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700109 const char* src_file_name_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800110 uint32_t low_pc_;
111 uint32_t high_pc_;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700112 const uint8_t* dbgstream_;
Andreas Gampe79273802014-08-05 20:21:05 -0700113 CompiledMethod* compiled_method_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800114 };
115
116 const std::vector<DebugInfo>& GetCFIMethodInfo() const {
117 return method_info_;
118 }
119
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700120 private:
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100121 // The DataAccess classes are helper classes that provide access to members related to
122 // a given map, i.e. GC map, mapping table or vmap table. By abstracting these away
123 // we can share a lot of code for processing the maps with template classes below.
124 struct GcMapDataAccess;
125 struct MappingTableDataAccess;
126 struct VmapTableDataAccess;
127
128 // The function VisitDexMethods() below iterates through all the methods in all
129 // the compiled dex files in order of their definitions. The method visitor
130 // classes provide individual bits of processing for each of the passes we need to
131 // first collect the data we want to write to the oat file and then, in later passes,
132 // to actually write it.
133 class DexMethodVisitor;
134 class OatDexMethodVisitor;
135 class InitOatClassesMethodVisitor;
136 class InitCodeMethodVisitor;
137 template <typename DataAccess>
138 class InitMapMethodVisitor;
139 class InitImageMethodVisitor;
140 class WriteCodeMethodVisitor;
141 template <typename DataAccess>
142 class WriteMapMethodVisitor;
143
144 // Visit all the methods in all the compiled dex files in their definition order
145 // with a given DexMethodVisitor.
146 bool VisitDexMethods(DexMethodVisitor* visitor);
147
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 size_t InitOatHeader();
149 size_t InitOatDexFiles(size_t offset);
150 size_t InitDexFiles(size_t offset);
151 size_t InitOatClasses(size_t offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100152 size_t InitOatMaps(size_t offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700153 size_t InitOatCode(size_t offset)
154 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
155 size_t InitOatCodeDexFiles(size_t offset)
156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157
Ian Rogers3d504072014-03-01 09:16:49 -0800158 bool WriteTables(OutputStream* out, const size_t file_offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100159 size_t WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset);
160 size_t WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset);
Ian Rogers3d504072014-03-01 09:16:49 -0800161 size_t WriteCodeDexFiles(OutputStream* out, const size_t file_offset, size_t relative_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700162
163 class OatDexFile {
164 public:
165 explicit OatDexFile(size_t offset, const DexFile& dex_file);
166 size_t SizeOf() const;
Ian Rogers3d504072014-03-01 09:16:49 -0800167 void UpdateChecksum(OatHeader* oat_header) const;
168 bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169
170 // Offset of start of OatDexFile from beginning of OatHeader. It is
171 // used to validate file position when writing.
172 size_t offset_;
173
174 // data to write
175 uint32_t dex_file_location_size_;
176 const uint8_t* dex_file_location_data_;
177 uint32_t dex_file_location_checksum_;
178 uint32_t dex_file_offset_;
179 std::vector<uint32_t> methods_offsets_;
180
181 private:
182 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
183 };
184
185 class OatClass {
186 public:
Brian Carlstromba150c32013-08-27 17:31:03 -0700187 explicit OatClass(size_t offset,
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100188 const std::vector<CompiledMethod*>& compiled_methods,
Brian Carlstromba150c32013-08-27 17:31:03 -0700189 uint32_t num_non_null_compiled_methods,
190 mirror::Class::Status status);
191 ~OatClass();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700192 size_t GetOatMethodOffsetsOffsetFromOatHeader(size_t class_def_method_index_) const;
193 size_t GetOatMethodOffsetsOffsetFromOatClass(size_t class_def_method_index_) const;
194 size_t SizeOf() const;
Ian Rogers3d504072014-03-01 09:16:49 -0800195 void UpdateChecksum(OatHeader* oat_header) const;
196 bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700197
Brian Carlstromba150c32013-08-27 17:31:03 -0700198 CompiledMethod* GetCompiledMethod(size_t class_def_method_index) const {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100199 DCHECK_LT(class_def_method_index, compiled_methods_.size());
200 return compiled_methods_[class_def_method_index];
Brian Carlstromba150c32013-08-27 17:31:03 -0700201 }
202
Brian Carlstrom7940e442013-07-12 13:46:57 -0700203 // Offset of start of OatClass from beginning of OatHeader. It is
204 // used to validate file position when writing. For Portable, it
205 // is also used to calculate the position of the OatMethodOffsets
206 // so that code pointers within the OatMethodOffsets can be
207 // patched to point to code in the Portable .o ELF objects.
208 size_t offset_;
209
Brian Carlstromba150c32013-08-27 17:31:03 -0700210 // CompiledMethods for each class_def_method_index, or NULL if no method is available.
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100211 std::vector<CompiledMethod*> compiled_methods_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700212
213 // Offset from OatClass::offset_ to the OatMethodOffsets for the
214 // class_def_method_index. If 0, it means the corresponding
215 // CompiledMethod entry in OatClass::compiled_methods_ should be
216 // NULL and that the OatClass::type_ should be kOatClassBitmap.
217 std::vector<uint32_t> oat_method_offsets_offsets_from_oat_class_;
218
Brian Carlstrom7940e442013-07-12 13:46:57 -0700219 // data to write
Brian Carlstromba150c32013-08-27 17:31:03 -0700220
221 COMPILE_ASSERT(mirror::Class::Status::kStatusMax < (2 ^ 16), class_status_wont_fit_in_16bits);
222 int16_t status_;
223
224 COMPILE_ASSERT(OatClassType::kOatClassMax < (2 ^ 16), oat_class_type_wont_fit_in_16bits);
225 uint16_t type_;
226
227 uint32_t method_bitmap_size_;
228
229 // bit vector indexed by ClassDef method index. When
230 // OatClassType::type_ is kOatClassBitmap, a set bit indicates the
231 // method has an OatMethodOffsets in methods_offsets_, otherwise
232 // the entry was ommited to save space. If OatClassType::type_ is
233 // not is kOatClassBitmap, the bitmap will be NULL.
234 BitVector* method_bitmap_;
235
Vladimir Marko8a630572014-04-09 18:45:35 +0100236 // OatMethodOffsets and OatMethodHeaders for each CompiledMethod
237 // present in the OatClass. Note that some may be missing if
Brian Carlstromba150c32013-08-27 17:31:03 -0700238 // OatClass::compiled_methods_ contains NULL values (and
239 // oat_method_offsets_offsets_from_oat_class_ should contain 0
240 // values in this case).
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241 std::vector<OatMethodOffsets> method_offsets_;
Vladimir Marko7624d252014-05-02 14:40:15 +0100242 std::vector<OatQuickMethodHeader> method_headers_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700243
244 private:
245 DISALLOW_COPY_AND_ASSIGN(OatClass);
246 };
247
Mark Mendellae9fd932014-02-10 16:14:35 -0800248 std::vector<DebugInfo> method_info_;
249
Brian Carlstrom7940e442013-07-12 13:46:57 -0700250 const CompilerDriver* const compiler_driver_;
251
252 // note OatFile does not take ownership of the DexFiles
253 const std::vector<const DexFile*>* dex_files_;
254
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700255 // Size required for Oat data structures.
256 size_t size_;
257
Brian Carlstrom7940e442013-07-12 13:46:57 -0700258 // dependencies on the image.
259 uint32_t image_file_location_oat_checksum_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800260 uintptr_t image_file_location_oat_begin_;
Alex Lighta59dd802014-07-02 16:28:08 -0700261 int32_t image_patch_delta_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262
263 // data to write
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700264 SafeMap<std::string, std::string>* key_value_store_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 OatHeader* oat_header_;
266 std::vector<OatDexFile*> oat_dex_files_;
267 std::vector<OatClass*> oat_classes_;
Ian Rogers700a4022014-05-19 16:49:03 -0700268 std::unique_ptr<const std::vector<uint8_t>> interpreter_to_interpreter_bridge_;
269 std::unique_ptr<const std::vector<uint8_t>> interpreter_to_compiled_code_bridge_;
270 std::unique_ptr<const std::vector<uint8_t>> jni_dlsym_lookup_;
271 std::unique_ptr<const std::vector<uint8_t>> portable_imt_conflict_trampoline_;
272 std::unique_ptr<const std::vector<uint8_t>> portable_resolution_trampoline_;
273 std::unique_ptr<const std::vector<uint8_t>> portable_to_interpreter_bridge_;
274 std::unique_ptr<const std::vector<uint8_t>> quick_generic_jni_trampoline_;
275 std::unique_ptr<const std::vector<uint8_t>> quick_imt_conflict_trampoline_;
276 std::unique_ptr<const std::vector<uint8_t>> quick_resolution_trampoline_;
277 std::unique_ptr<const std::vector<uint8_t>> quick_to_interpreter_bridge_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278
279 // output stats
280 uint32_t size_dex_file_alignment_;
281 uint32_t size_executable_offset_alignment_;
282 uint32_t size_oat_header_;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700283 uint32_t size_oat_header_key_value_store_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284 uint32_t size_dex_file_;
Ian Rogers468532e2013-08-05 10:56:33 -0700285 uint32_t size_interpreter_to_interpreter_bridge_;
286 uint32_t size_interpreter_to_compiled_code_bridge_;
287 uint32_t size_jni_dlsym_lookup_;
Jeff Hao88474b42013-10-23 16:24:40 -0700288 uint32_t size_portable_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700289 uint32_t size_portable_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700290 uint32_t size_portable_to_interpreter_bridge_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800291 uint32_t size_quick_generic_jni_trampoline_;
Jeff Hao88474b42013-10-23 16:24:40 -0700292 uint32_t size_quick_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 uint32_t size_quick_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700294 uint32_t size_quick_to_interpreter_bridge_;
295 uint32_t size_trampoline_alignment_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100296 uint32_t size_method_header_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297 uint32_t size_code_;
298 uint32_t size_code_alignment_;
299 uint32_t size_mapping_table_;
300 uint32_t size_vmap_table_;
301 uint32_t size_gc_map_;
302 uint32_t size_oat_dex_file_location_size_;
303 uint32_t size_oat_dex_file_location_data_;
304 uint32_t size_oat_dex_file_location_checksum_;
305 uint32_t size_oat_dex_file_offset_;
306 uint32_t size_oat_dex_file_methods_offsets_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700307 uint32_t size_oat_class_type_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700308 uint32_t size_oat_class_status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700309 uint32_t size_oat_class_method_bitmaps_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310 uint32_t size_oat_class_method_offsets_;
311
Vladimir Marko8a630572014-04-09 18:45:35 +0100312 struct CodeOffsetsKeyComparator {
313 bool operator()(const CompiledMethod* lhs, const CompiledMethod* rhs) const {
314 if (lhs->GetQuickCode() != rhs->GetQuickCode()) {
315 return lhs->GetQuickCode() < rhs->GetQuickCode();
316 }
317 // If the code is the same, all other fields are likely to be the same as well.
318 if (UNLIKELY(&lhs->GetMappingTable() != &rhs->GetMappingTable())) {
319 return &lhs->GetMappingTable() < &rhs->GetMappingTable();
320 }
321 if (UNLIKELY(&lhs->GetVmapTable() != &rhs->GetVmapTable())) {
322 return &lhs->GetVmapTable() < &rhs->GetVmapTable();
323 }
324 return false;
325 }
326 };
327
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328 DISALLOW_COPY_AND_ASSIGN(OatWriter);
329};
330
331} // namespace art
332
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700333#endif // ART_COMPILER_OAT_WRITER_H_