blob: d6cb65bd64eebd11e4c11147617296dba821b0fa [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
Vladimir Markob163bb72015-03-31 21:49:49 +010024#include "linker/relative_patcher.h" // For linker::RelativePatcherTargetProvider.
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include "mem_map.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010026#include "method_reference.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070027#include "oat.h"
28#include "mirror/class.h"
29#include "safe_map.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030
31namespace art {
32
Brian Carlstromba150c32013-08-27 17:31:03 -070033class BitVector;
Andreas Gampe79273802014-08-05 20:21:05 -070034class CompiledMethod;
Vladimir Marko20f85592015-03-19 10:07:02 +000035class CompilerDriver;
Vladimir Markof4da6752014-08-01 19:04:18 +010036class ImageWriter;
Brian Carlstrom7940e442013-07-12 13:46:57 -070037class OutputStream;
Vladimir Marko20f85592015-03-19 10:07:02 +000038class TimingLogger;
Brian Carlstrom7940e442013-07-12 13:46:57 -070039
40// OatHeader variable length with count of D OatDexFiles
41//
42// OatDexFile[0] one variable sized OatDexFile with offsets to Dex and OatClasses
43// OatDexFile[1]
44// ...
45// OatDexFile[D]
46//
47// Dex[0] one variable sized DexFile for each OatDexFile.
48// Dex[1] these are literal copies of the input .dex files.
49// ...
50// Dex[D]
51//
52// OatClass[0] one variable sized OatClass for each of C DexFile::ClassDefs
53// OatClass[1] contains OatClass entries with class status, offsets to code, etc.
54// ...
55// OatClass[C]
56//
Vladimir Marko96c6ab92014-04-08 14:00:50 +010057// GcMap one variable sized blob with GC map.
58// GcMap GC maps are deduplicated.
59// ...
60// GcMap
61//
62// VmapTable one variable sized VmapTable blob (quick compiler only).
63// VmapTable VmapTables are deduplicated.
64// ...
65// VmapTable
66//
67// MappingTable one variable sized blob with MappingTable (quick compiler only).
68// MappingTable MappingTables are deduplicated.
69// ...
70// MappingTable
71//
Brian Carlstrom7940e442013-07-12 13:46:57 -070072// padding if necessary so that the following code will be page aligned
73//
Vladimir Marko96c6ab92014-04-08 14:00:50 +010074// OatMethodHeader fixed size header for a CompiledMethod including the size of the MethodCode.
75// MethodCode one variable sized blob with the code of a CompiledMethod.
76// OatMethodHeader (OatMethodHeader, MethodCode) pairs are deduplicated.
77// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -070078// ...
Vladimir Marko96c6ab92014-04-08 14:00:50 +010079// OatMethodHeader
80// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -070081//
82class OatWriter {
83 public:
Brian Carlstrom7940e442013-07-12 13:46:57 -070084 OatWriter(const std::vector<const DexFile*>& dex_files,
85 uint32_t image_file_location_oat_checksum,
Ian Rogersef7d42f2014-01-06 12:55:46 -080086 uintptr_t image_file_location_oat_begin,
Alex Lighta59dd802014-07-02 16:28:08 -070087 int32_t image_patch_delta,
Ian Rogersca368cb2013-11-15 15:52:08 -080088 const CompilerDriver* compiler,
Vladimir Markof4da6752014-08-01 19:04:18 +010089 ImageWriter* image_writer,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070090 TimingLogger* timings,
91 SafeMap<std::string, std::string>* key_value_store);
Brian Carlstromc50d8e12013-07-23 22:35:16 -070092
Vladimir Marko09d09432015-09-08 13:47:48 +010093 // Returns whether the oat file has an associated image.
94 bool HasImage() const {
95 // Since the image is being created at the same time as the oat file,
96 // check if there's an image writer.
97 return image_writer_ != nullptr;
98 }
99
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700100 const OatHeader& GetOatHeader() const {
101 return *oat_header_;
102 }
103
104 size_t GetSize() const {
105 return size_;
106 }
107
Vladimir Marko5c42c292015-02-25 12:02:49 +0000108 size_t GetBssSize() const {
109 return bss_size_;
110 }
111
David Srbeckyf8980872015-05-22 17:04:47 +0100112 const std::vector<uintptr_t>& GetAbsolutePatchLocations() const {
Vladimir Markof4da6752014-08-01 19:04:18 +0100113 return absolute_patch_locations_;
114 }
115
David Srbeckybc90fd02015-04-22 19:40:27 +0100116 bool WriteRodata(OutputStream* out);
117 bool WriteCode(OutputStream* out);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700118
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 ~OatWriter();
120
Mark Mendellae9fd932014-02-10 16:14:35 -0800121 struct DebugInfo {
David Srbecky0df9e1f2015-04-07 19:02:58 +0100122 const DexFile* dex_file_;
123 size_t class_def_index_;
124 uint32_t dex_method_index_;
125 uint32_t access_flags_;
126 const DexFile::CodeItem *code_item_;
127 bool deduped_;
128 uint32_t low_pc_;
129 uint32_t high_pc_;
Andreas Gampe79273802014-08-05 20:21:05 -0700130 CompiledMethod* compiled_method_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800131 };
132
David Srbecky3b9d57a2015-04-10 00:22:14 +0100133 const std::vector<DebugInfo>& GetMethodDebugInfo() const {
Mark Mendellae9fd932014-02-10 16:14:35 -0800134 return method_info_;
135 }
136
Vladimir Markob163bb72015-03-31 21:49:49 +0100137 const CompilerDriver* GetCompilerDriver() {
138 return compiler_driver_;
139 }
140
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700141 private:
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100142 // The DataAccess classes are helper classes that provide access to members related to
143 // a given map, i.e. GC map, mapping table or vmap table. By abstracting these away
144 // we can share a lot of code for processing the maps with template classes below.
145 struct GcMapDataAccess;
146 struct MappingTableDataAccess;
147 struct VmapTableDataAccess;
148
149 // The function VisitDexMethods() below iterates through all the methods in all
150 // the compiled dex files in order of their definitions. The method visitor
151 // classes provide individual bits of processing for each of the passes we need to
152 // first collect the data we want to write to the oat file and then, in later passes,
153 // to actually write it.
154 class DexMethodVisitor;
155 class OatDexMethodVisitor;
156 class InitOatClassesMethodVisitor;
157 class InitCodeMethodVisitor;
158 template <typename DataAccess>
159 class InitMapMethodVisitor;
160 class InitImageMethodVisitor;
161 class WriteCodeMethodVisitor;
162 template <typename DataAccess>
163 class WriteMapMethodVisitor;
164
165 // Visit all the methods in all the compiled dex files in their definition order
166 // with a given DexMethodVisitor.
167 bool VisitDexMethods(DexMethodVisitor* visitor);
168
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169 size_t InitOatHeader();
170 size_t InitOatDexFiles(size_t offset);
171 size_t InitDexFiles(size_t offset);
172 size_t InitOatClasses(size_t offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100173 size_t InitOatMaps(size_t offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174 size_t InitOatCode(size_t offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700175 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700176 size_t InitOatCodeDexFiles(size_t offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700177 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178
Ian Rogers3d504072014-03-01 09:16:49 -0800179 bool WriteTables(OutputStream* out, const size_t file_offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100180 size_t WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset);
181 size_t WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset);
Ian Rogers3d504072014-03-01 09:16:49 -0800182 size_t WriteCodeDexFiles(OutputStream* out, const size_t file_offset, size_t relative_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183
Vladimir Markof4da6752014-08-01 19:04:18 +0100184 bool WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta);
185
Brian Carlstrom7940e442013-07-12 13:46:57 -0700186 class OatDexFile {
187 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100188 OatDexFile(size_t offset, const DexFile& dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700189 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
193 // Offset of start of OatDexFile from beginning of OatHeader. It is
194 // used to validate file position when writing.
195 size_t offset_;
196
197 // data to write
198 uint32_t dex_file_location_size_;
199 const uint8_t* dex_file_location_data_;
200 uint32_t dex_file_location_checksum_;
201 uint32_t dex_file_offset_;
202 std::vector<uint32_t> methods_offsets_;
203
204 private:
205 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
206 };
207
208 class OatClass {
209 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100210 OatClass(size_t offset,
211 const std::vector<CompiledMethod*>& compiled_methods,
212 uint32_t num_non_null_compiled_methods,
213 mirror::Class::Status status);
Brian Carlstromba150c32013-08-27 17:31:03 -0700214 ~OatClass();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700215 size_t GetOatMethodOffsetsOffsetFromOatHeader(size_t class_def_method_index_) const;
216 size_t GetOatMethodOffsetsOffsetFromOatClass(size_t class_def_method_index_) const;
217 size_t SizeOf() const;
Ian Rogers3d504072014-03-01 09:16:49 -0800218 void UpdateChecksum(OatHeader* oat_header) const;
219 bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700220
Brian Carlstromba150c32013-08-27 17:31:03 -0700221 CompiledMethod* GetCompiledMethod(size_t class_def_method_index) const {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100222 DCHECK_LT(class_def_method_index, compiled_methods_.size());
223 return compiled_methods_[class_def_method_index];
Brian Carlstromba150c32013-08-27 17:31:03 -0700224 }
225
Brian Carlstrom7940e442013-07-12 13:46:57 -0700226 // Offset of start of OatClass from beginning of OatHeader. It is
Elliott Hughes956af0f2014-12-11 14:34:28 -0800227 // used to validate file position when writing.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700228 size_t offset_;
229
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700230 // CompiledMethods for each class_def_method_index, or null if no method is available.
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100231 std::vector<CompiledMethod*> compiled_methods_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700232
233 // Offset from OatClass::offset_ to the OatMethodOffsets for the
234 // class_def_method_index. If 0, it means the corresponding
235 // CompiledMethod entry in OatClass::compiled_methods_ should be
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700236 // null and that the OatClass::type_ should be kOatClassBitmap.
Brian Carlstromba150c32013-08-27 17:31:03 -0700237 std::vector<uint32_t> oat_method_offsets_offsets_from_oat_class_;
238
Brian Carlstrom7940e442013-07-12 13:46:57 -0700239 // data to write
Brian Carlstromba150c32013-08-27 17:31:03 -0700240
Andreas Gampe785d2f22014-11-03 22:57:30 -0800241 static_assert(mirror::Class::Status::kStatusMax < (2 ^ 16), "class status won't fit in 16bits");
Brian Carlstromba150c32013-08-27 17:31:03 -0700242 int16_t status_;
243
Andreas Gampe785d2f22014-11-03 22:57:30 -0800244 static_assert(OatClassType::kOatClassMax < (2 ^ 16), "oat_class type won't fit in 16bits");
Brian Carlstromba150c32013-08-27 17:31:03 -0700245 uint16_t type_;
246
247 uint32_t method_bitmap_size_;
248
249 // bit vector indexed by ClassDef method index. When
250 // OatClassType::type_ is kOatClassBitmap, a set bit indicates the
251 // method has an OatMethodOffsets in methods_offsets_, otherwise
252 // the entry was ommited to save space. If OatClassType::type_ is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700253 // not is kOatClassBitmap, the bitmap will be null.
Brian Carlstromba150c32013-08-27 17:31:03 -0700254 BitVector* method_bitmap_;
255
Vladimir Marko8a630572014-04-09 18:45:35 +0100256 // OatMethodOffsets and OatMethodHeaders for each CompiledMethod
257 // present in the OatClass. Note that some may be missing if
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700258 // OatClass::compiled_methods_ contains null values (and
Brian Carlstromba150c32013-08-27 17:31:03 -0700259 // oat_method_offsets_offsets_from_oat_class_ should contain 0
260 // values in this case).
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261 std::vector<OatMethodOffsets> method_offsets_;
Vladimir Marko7624d252014-05-02 14:40:15 +0100262 std::vector<OatQuickMethodHeader> method_headers_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700263
264 private:
265 DISALLOW_COPY_AND_ASSIGN(OatClass);
266 };
267
Mark Mendellae9fd932014-02-10 16:14:35 -0800268 std::vector<DebugInfo> method_info_;
269
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270 const CompilerDriver* const compiler_driver_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100271 ImageWriter* const image_writer_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272
273 // note OatFile does not take ownership of the DexFiles
274 const std::vector<const DexFile*>* dex_files_;
275
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700276 // Size required for Oat data structures.
277 size_t size_;
278
Vladimir Marko5c42c292015-02-25 12:02:49 +0000279 // The size of the required .bss section holding the DexCache data.
280 size_t bss_size_;
281
Vladimir Marko09d09432015-09-08 13:47:48 +0100282 // Offsets of the dex cache arrays for each app dex file. For the
283 // boot image, this information is provided by the ImageWriter.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100284 SafeMap<const DexFile*, size_t> dex_cache_arrays_offsets_; // DexFiles not owned.
Vladimir Marko09d09432015-09-08 13:47:48 +0100285
Vladimir Markof4da6752014-08-01 19:04:18 +0100286 // Offset of the oat data from the start of the mmapped region of the elf file.
287 size_t oat_data_offset_;
288
Brian Carlstrom7940e442013-07-12 13:46:57 -0700289 // dependencies on the image.
290 uint32_t image_file_location_oat_checksum_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800291 uintptr_t image_file_location_oat_begin_;
Alex Lighta59dd802014-07-02 16:28:08 -0700292 int32_t image_patch_delta_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293
294 // data to write
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700295 SafeMap<std::string, std::string>* key_value_store_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700296 OatHeader* oat_header_;
297 std::vector<OatDexFile*> oat_dex_files_;
298 std::vector<OatClass*> oat_classes_;
Ian Rogers700a4022014-05-19 16:49:03 -0700299 std::unique_ptr<const std::vector<uint8_t>> jni_dlsym_lookup_;
Ian Rogers700a4022014-05-19 16:49:03 -0700300 std::unique_ptr<const std::vector<uint8_t>> quick_generic_jni_trampoline_;
301 std::unique_ptr<const std::vector<uint8_t>> quick_imt_conflict_trampoline_;
302 std::unique_ptr<const std::vector<uint8_t>> quick_resolution_trampoline_;
303 std::unique_ptr<const std::vector<uint8_t>> quick_to_interpreter_bridge_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700304
305 // output stats
306 uint32_t size_dex_file_alignment_;
307 uint32_t size_executable_offset_alignment_;
308 uint32_t size_oat_header_;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700309 uint32_t size_oat_header_key_value_store_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310 uint32_t size_dex_file_;
Ian Rogers468532e2013-08-05 10:56:33 -0700311 uint32_t size_interpreter_to_interpreter_bridge_;
312 uint32_t size_interpreter_to_compiled_code_bridge_;
313 uint32_t size_jni_dlsym_lookup_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800314 uint32_t size_quick_generic_jni_trampoline_;
Jeff Hao88474b42013-10-23 16:24:40 -0700315 uint32_t size_quick_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700316 uint32_t size_quick_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700317 uint32_t size_quick_to_interpreter_bridge_;
318 uint32_t size_trampoline_alignment_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100319 uint32_t size_method_header_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320 uint32_t size_code_;
321 uint32_t size_code_alignment_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100322 uint32_t size_relative_call_thunks_;
Vladimir Markoc74658b2015-03-31 10:26:41 +0100323 uint32_t size_misc_thunks_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700324 uint32_t size_mapping_table_;
325 uint32_t size_vmap_table_;
326 uint32_t size_gc_map_;
327 uint32_t size_oat_dex_file_location_size_;
328 uint32_t size_oat_dex_file_location_data_;
329 uint32_t size_oat_dex_file_location_checksum_;
330 uint32_t size_oat_dex_file_offset_;
331 uint32_t size_oat_dex_file_methods_offsets_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700332 uint32_t size_oat_class_type_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700333 uint32_t size_oat_class_status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700334 uint32_t size_oat_class_method_bitmaps_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700335 uint32_t size_oat_class_method_offsets_;
336
Vladimir Markob163bb72015-03-31 21:49:49 +0100337 std::unique_ptr<linker::RelativePatcher> relative_patcher_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100338
David Srbeckyf8980872015-05-22 17:04:47 +0100339 // The locations of absolute patches relative to the start of the executable section.
340 std::vector<uintptr_t> absolute_patch_locations_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100341
Vladimir Markob163bb72015-03-31 21:49:49 +0100342 // Map method reference to assigned offset.
343 // Wrap the map in a class implementing linker::RelativePatcherTargetProvider.
344 class MethodOffsetMap FINAL : public linker::RelativePatcherTargetProvider {
345 public:
346 std::pair<bool, uint32_t> FindMethodOffset(MethodReference ref) OVERRIDE;
347 SafeMap<MethodReference, uint32_t, MethodReferenceComparator> map;
348 };
349 MethodOffsetMap method_offset_map_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100350
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 DISALLOW_COPY_AND_ASSIGN(OatWriter);
352};
353
354} // namespace art
355
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700356#endif // ART_COMPILER_OAT_WRITER_H_