blob: 6c46ebc4ec4261f8455c80cba7c4519a9c8070d6 [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 "mirror/class.h"
Artem Udovichenkod9786b02015-10-14 16:36:55 +030028#include "oat.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070029#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;
Artem Udovichenkod9786b02015-10-14 16:36:55 +030039class TypeLookupTable;
Brian Carlstrom7940e442013-07-12 13:46:57 -070040
Vladimir Marko10c13562015-11-25 14:33:36 +000041namespace dwarf {
42struct MethodDebugInfo;
43} // namespace dwarf
44
Brian Carlstrom7940e442013-07-12 13:46:57 -070045// OatHeader variable length with count of D OatDexFiles
46//
47// OatDexFile[0] one variable sized OatDexFile with offsets to Dex and OatClasses
48// OatDexFile[1]
49// ...
50// OatDexFile[D]
51//
52// Dex[0] one variable sized DexFile for each OatDexFile.
53// Dex[1] these are literal copies of the input .dex files.
54// ...
55// Dex[D]
56//
Artem Udovichenkod9786b02015-10-14 16:36:55 +030057// TypeLookupTable[0] one descriptor to class def index hash table for each OatDexFile.
58// TypeLookupTable[1]
59// ...
60// TypeLookupTable[D]
61//
Brian Carlstrom7940e442013-07-12 13:46:57 -070062// OatClass[0] one variable sized OatClass for each of C DexFile::ClassDefs
63// OatClass[1] contains OatClass entries with class status, offsets to code, etc.
64// ...
65// OatClass[C]
66//
Vladimir Marko96c6ab92014-04-08 14:00:50 +010067// GcMap one variable sized blob with GC map.
68// GcMap GC maps are deduplicated.
69// ...
70// GcMap
71//
72// VmapTable one variable sized VmapTable blob (quick compiler only).
73// VmapTable VmapTables are deduplicated.
74// ...
75// VmapTable
76//
77// MappingTable one variable sized blob with MappingTable (quick compiler only).
78// MappingTable MappingTables are deduplicated.
79// ...
80// MappingTable
81//
Brian Carlstrom7940e442013-07-12 13:46:57 -070082// padding if necessary so that the following code will be page aligned
83//
Vladimir Marko96c6ab92014-04-08 14:00:50 +010084// OatMethodHeader fixed size header for a CompiledMethod including the size of the MethodCode.
85// MethodCode one variable sized blob with the code of a CompiledMethod.
86// OatMethodHeader (OatMethodHeader, MethodCode) pairs are deduplicated.
87// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -070088// ...
Vladimir Marko96c6ab92014-04-08 14:00:50 +010089// OatMethodHeader
90// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -070091//
92class OatWriter {
93 public:
Brian Carlstrom7940e442013-07-12 13:46:57 -070094 OatWriter(const std::vector<const DexFile*>& dex_files,
95 uint32_t image_file_location_oat_checksum,
Ian Rogersef7d42f2014-01-06 12:55:46 -080096 uintptr_t image_file_location_oat_begin,
Alex Lighta59dd802014-07-02 16:28:08 -070097 int32_t image_patch_delta,
Ian Rogersca368cb2013-11-15 15:52:08 -080098 const CompilerDriver* compiler,
Vladimir Markof4da6752014-08-01 19:04:18 +010099 ImageWriter* image_writer,
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800100 bool compiling_boot_image,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700101 TimingLogger* timings,
102 SafeMap<std::string, std::string>* key_value_store);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700103
Vladimir Marko09d09432015-09-08 13:47:48 +0100104 // Returns whether the oat file has an associated image.
105 bool HasImage() const {
106 // Since the image is being created at the same time as the oat file,
107 // check if there's an image writer.
108 return image_writer_ != nullptr;
109 }
110
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800111 bool HasBootImage() const {
112 return compiling_boot_image_;
113 }
114
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700115 const OatHeader& GetOatHeader() const {
116 return *oat_header_;
117 }
118
119 size_t GetSize() const {
120 return size_;
121 }
122
Vladimir Marko5c42c292015-02-25 12:02:49 +0000123 size_t GetBssSize() const {
124 return bss_size_;
125 }
126
David Srbeckyf8980872015-05-22 17:04:47 +0100127 const std::vector<uintptr_t>& GetAbsolutePatchLocations() const {
Vladimir Markof4da6752014-08-01 19:04:18 +0100128 return absolute_patch_locations_;
129 }
130
David Srbeckybc90fd02015-04-22 19:40:27 +0100131 bool WriteRodata(OutputStream* out);
132 bool WriteCode(OutputStream* out);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700133
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 ~OatWriter();
135
Vladimir Marko10c13562015-11-25 14:33:36 +0000136 const std::vector<dwarf::MethodDebugInfo>& GetMethodDebugInfo() const {
Mark Mendellae9fd932014-02-10 16:14:35 -0800137 return method_info_;
138 }
139
Vladimir Markob163bb72015-03-31 21:49:49 +0100140 const CompilerDriver* GetCompilerDriver() {
141 return compiler_driver_;
142 }
143
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700144 private:
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100145 // The DataAccess classes are helper classes that provide access to members related to
146 // a given map, i.e. GC map, mapping table or vmap table. By abstracting these away
147 // we can share a lot of code for processing the maps with template classes below.
148 struct GcMapDataAccess;
149 struct MappingTableDataAccess;
150 struct VmapTableDataAccess;
151
152 // The function VisitDexMethods() below iterates through all the methods in all
153 // the compiled dex files in order of their definitions. The method visitor
154 // classes provide individual bits of processing for each of the passes we need to
155 // first collect the data we want to write to the oat file and then, in later passes,
156 // to actually write it.
157 class DexMethodVisitor;
158 class OatDexMethodVisitor;
159 class InitOatClassesMethodVisitor;
160 class InitCodeMethodVisitor;
161 template <typename DataAccess>
162 class InitMapMethodVisitor;
163 class InitImageMethodVisitor;
164 class WriteCodeMethodVisitor;
165 template <typename DataAccess>
166 class WriteMapMethodVisitor;
167
168 // Visit all the methods in all the compiled dex files in their definition order
169 // with a given DexMethodVisitor.
170 bool VisitDexMethods(DexMethodVisitor* visitor);
171
Brian Carlstrom7940e442013-07-12 13:46:57 -0700172 size_t InitOatHeader();
173 size_t InitOatDexFiles(size_t offset);
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300174 size_t InitLookupTables(size_t offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700175 size_t InitDexFiles(size_t offset);
176 size_t InitOatClasses(size_t offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100177 size_t InitOatMaps(size_t offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178 size_t InitOatCode(size_t offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700179 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180 size_t InitOatCodeDexFiles(size_t offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700181 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182
Ian Rogers3d504072014-03-01 09:16:49 -0800183 bool WriteTables(OutputStream* out, const size_t file_offset);
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300184 bool WriteLookupTables(OutputStream* out, const size_t file_offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100185 size_t WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset);
186 size_t WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset);
Ian Rogers3d504072014-03-01 09:16:49 -0800187 size_t WriteCodeDexFiles(OutputStream* out, const size_t file_offset, size_t relative_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188
Vladimir Markof4da6752014-08-01 19:04:18 +0100189 bool WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta);
190
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191 class OatDexFile {
192 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100193 OatDexFile(size_t offset, const DexFile& dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700194 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
198 // Offset of start of OatDexFile from beginning of OatHeader. It is
199 // used to validate file position when writing.
200 size_t offset_;
201
202 // data to write
203 uint32_t dex_file_location_size_;
204 const uint8_t* dex_file_location_data_;
205 uint32_t dex_file_location_checksum_;
206 uint32_t dex_file_offset_;
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300207 uint32_t lookup_table_offset_;
208 TypeLookupTable* lookup_table_; // Owned by the dex file.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 std::vector<uint32_t> methods_offsets_;
210
211 private:
212 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
213 };
214
215 class OatClass {
216 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100217 OatClass(size_t offset,
218 const std::vector<CompiledMethod*>& compiled_methods,
219 uint32_t num_non_null_compiled_methods,
220 mirror::Class::Status status);
Brian Carlstromba150c32013-08-27 17:31:03 -0700221 ~OatClass();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700222 size_t GetOatMethodOffsetsOffsetFromOatHeader(size_t class_def_method_index_) const;
223 size_t GetOatMethodOffsetsOffsetFromOatClass(size_t class_def_method_index_) const;
224 size_t SizeOf() const;
Ian Rogers3d504072014-03-01 09:16:49 -0800225 void UpdateChecksum(OatHeader* oat_header) const;
226 bool Write(OatWriter* oat_writer, OutputStream* out, const size_t file_offset) const;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700227
Brian Carlstromba150c32013-08-27 17:31:03 -0700228 CompiledMethod* GetCompiledMethod(size_t class_def_method_index) const {
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100229 DCHECK_LT(class_def_method_index, compiled_methods_.size());
230 return compiled_methods_[class_def_method_index];
Brian Carlstromba150c32013-08-27 17:31:03 -0700231 }
232
Brian Carlstrom7940e442013-07-12 13:46:57 -0700233 // Offset of start of OatClass from beginning of OatHeader. It is
Elliott Hughes956af0f2014-12-11 14:34:28 -0800234 // used to validate file position when writing.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700235 size_t offset_;
236
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700237 // CompiledMethods for each class_def_method_index, or null if no method is available.
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100238 std::vector<CompiledMethod*> compiled_methods_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700239
240 // Offset from OatClass::offset_ to the OatMethodOffsets for the
241 // class_def_method_index. If 0, it means the corresponding
242 // CompiledMethod entry in OatClass::compiled_methods_ should be
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700243 // null and that the OatClass::type_ should be kOatClassBitmap.
Brian Carlstromba150c32013-08-27 17:31:03 -0700244 std::vector<uint32_t> oat_method_offsets_offsets_from_oat_class_;
245
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 // data to write
Brian Carlstromba150c32013-08-27 17:31:03 -0700247
Andreas Gampe785d2f22014-11-03 22:57:30 -0800248 static_assert(mirror::Class::Status::kStatusMax < (2 ^ 16), "class status won't fit in 16bits");
Brian Carlstromba150c32013-08-27 17:31:03 -0700249 int16_t status_;
250
Andreas Gampe785d2f22014-11-03 22:57:30 -0800251 static_assert(OatClassType::kOatClassMax < (2 ^ 16), "oat_class type won't fit in 16bits");
Brian Carlstromba150c32013-08-27 17:31:03 -0700252 uint16_t type_;
253
254 uint32_t method_bitmap_size_;
255
256 // bit vector indexed by ClassDef method index. When
257 // OatClassType::type_ is kOatClassBitmap, a set bit indicates the
258 // method has an OatMethodOffsets in methods_offsets_, otherwise
259 // the entry was ommited to save space. If OatClassType::type_ is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700260 // not is kOatClassBitmap, the bitmap will be null.
Brian Carlstromba150c32013-08-27 17:31:03 -0700261 BitVector* method_bitmap_;
262
Vladimir Marko8a630572014-04-09 18:45:35 +0100263 // OatMethodOffsets and OatMethodHeaders for each CompiledMethod
264 // present in the OatClass. Note that some may be missing if
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700265 // OatClass::compiled_methods_ contains null values (and
Brian Carlstromba150c32013-08-27 17:31:03 -0700266 // oat_method_offsets_offsets_from_oat_class_ should contain 0
267 // values in this case).
Brian Carlstrom7940e442013-07-12 13:46:57 -0700268 std::vector<OatMethodOffsets> method_offsets_;
Vladimir Marko7624d252014-05-02 14:40:15 +0100269 std::vector<OatQuickMethodHeader> method_headers_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270
271 private:
272 DISALLOW_COPY_AND_ASSIGN(OatClass);
273 };
274
Vladimir Marko10c13562015-11-25 14:33:36 +0000275 std::vector<dwarf::MethodDebugInfo> method_info_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800276
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277 const CompilerDriver* const compiler_driver_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100278 ImageWriter* const image_writer_;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800279 const bool compiling_boot_image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280
281 // note OatFile does not take ownership of the DexFiles
282 const std::vector<const DexFile*>* dex_files_;
283
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700284 // Size required for Oat data structures.
285 size_t size_;
286
Vladimir Marko5c42c292015-02-25 12:02:49 +0000287 // The size of the required .bss section holding the DexCache data.
288 size_t bss_size_;
289
Vladimir Marko09d09432015-09-08 13:47:48 +0100290 // Offsets of the dex cache arrays for each app dex file. For the
291 // boot image, this information is provided by the ImageWriter.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100292 SafeMap<const DexFile*, size_t> dex_cache_arrays_offsets_; // DexFiles not owned.
Vladimir Marko09d09432015-09-08 13:47:48 +0100293
Vladimir Markof4da6752014-08-01 19:04:18 +0100294 // Offset of the oat data from the start of the mmapped region of the elf file.
295 size_t oat_data_offset_;
296
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297 // dependencies on the image.
298 uint32_t image_file_location_oat_checksum_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800299 uintptr_t image_file_location_oat_begin_;
Alex Lighta59dd802014-07-02 16:28:08 -0700300 int32_t image_patch_delta_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700301
302 // data to write
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700303 SafeMap<std::string, std::string>* key_value_store_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700304 OatHeader* oat_header_;
305 std::vector<OatDexFile*> oat_dex_files_;
306 std::vector<OatClass*> oat_classes_;
Ian Rogers700a4022014-05-19 16:49:03 -0700307 std::unique_ptr<const std::vector<uint8_t>> jni_dlsym_lookup_;
Ian Rogers700a4022014-05-19 16:49:03 -0700308 std::unique_ptr<const std::vector<uint8_t>> quick_generic_jni_trampoline_;
309 std::unique_ptr<const std::vector<uint8_t>> quick_imt_conflict_trampoline_;
310 std::unique_ptr<const std::vector<uint8_t>> quick_resolution_trampoline_;
311 std::unique_ptr<const std::vector<uint8_t>> quick_to_interpreter_bridge_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312
313 // output stats
314 uint32_t size_dex_file_alignment_;
315 uint32_t size_executable_offset_alignment_;
316 uint32_t size_oat_header_;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700317 uint32_t size_oat_header_key_value_store_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318 uint32_t size_dex_file_;
Ian Rogers468532e2013-08-05 10:56:33 -0700319 uint32_t size_interpreter_to_interpreter_bridge_;
320 uint32_t size_interpreter_to_compiled_code_bridge_;
321 uint32_t size_jni_dlsym_lookup_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800322 uint32_t size_quick_generic_jni_trampoline_;
Jeff Hao88474b42013-10-23 16:24:40 -0700323 uint32_t size_quick_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700324 uint32_t size_quick_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700325 uint32_t size_quick_to_interpreter_bridge_;
326 uint32_t size_trampoline_alignment_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100327 uint32_t size_method_header_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328 uint32_t size_code_;
329 uint32_t size_code_alignment_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100330 uint32_t size_relative_call_thunks_;
Vladimir Markoc74658b2015-03-31 10:26:41 +0100331 uint32_t size_misc_thunks_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700332 uint32_t size_mapping_table_;
333 uint32_t size_vmap_table_;
334 uint32_t size_gc_map_;
335 uint32_t size_oat_dex_file_location_size_;
336 uint32_t size_oat_dex_file_location_data_;
337 uint32_t size_oat_dex_file_location_checksum_;
338 uint32_t size_oat_dex_file_offset_;
339 uint32_t size_oat_dex_file_methods_offsets_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700340 uint32_t size_oat_class_type_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341 uint32_t size_oat_class_status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700342 uint32_t size_oat_class_method_bitmaps_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700343 uint32_t size_oat_class_method_offsets_;
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300344 uint32_t size_oat_lookup_table_alignment_;
345 uint32_t size_oat_lookup_table_offset_;
346 uint32_t size_oat_lookup_table_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700347
Vladimir Markob163bb72015-03-31 21:49:49 +0100348 std::unique_ptr<linker::RelativePatcher> relative_patcher_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100349
David Srbeckyf8980872015-05-22 17:04:47 +0100350 // The locations of absolute patches relative to the start of the executable section.
351 std::vector<uintptr_t> absolute_patch_locations_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100352
Vladimir Markob163bb72015-03-31 21:49:49 +0100353 // Map method reference to assigned offset.
354 // Wrap the map in a class implementing linker::RelativePatcherTargetProvider.
355 class MethodOffsetMap FINAL : public linker::RelativePatcherTargetProvider {
356 public:
357 std::pair<bool, uint32_t> FindMethodOffset(MethodReference ref) OVERRIDE;
358 SafeMap<MethodReference, uint32_t, MethodReferenceComparator> map;
359 };
360 MethodOffsetMap method_offset_map_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100361
Brian Carlstrom7940e442013-07-12 13:46:57 -0700362 DISALLOW_COPY_AND_ASSIGN(OatWriter);
363};
364
365} // namespace art
366
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700367#endif // ART_COMPILER_OAT_WRITER_H_