blob: 5feb5fc516cb1445c6604cca702801a83dba4782 [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 Marko49b0f452015-12-10 13:49:19 +000024#include "base/dchecked_vector.h"
Vladimir Markob163bb72015-03-31 21:49:49 +010025#include "linker/relative_patcher.h" // For linker::RelativePatcherTargetProvider.
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include "mem_map.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010027#include "method_reference.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "mirror/class.h"
Artem Udovichenkod9786b02015-10-14 16:36:55 +030029#include "oat.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030#include "safe_map.h"
Vladimir Marko49b0f452015-12-10 13:49:19 +000031#include "utils/array_ref.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070032
33namespace art {
34
Brian Carlstromba150c32013-08-27 17:31:03 -070035class BitVector;
Andreas Gampe79273802014-08-05 20:21:05 -070036class CompiledMethod;
Vladimir Marko20f85592015-03-19 10:07:02 +000037class CompilerDriver;
Vladimir Markof4da6752014-08-01 19:04:18 +010038class ImageWriter;
Brian Carlstrom7940e442013-07-12 13:46:57 -070039class OutputStream;
Vladimir Marko20f85592015-03-19 10:07:02 +000040class TimingLogger;
Artem Udovichenkod9786b02015-10-14 16:36:55 +030041class TypeLookupTable;
Brian Carlstrom7940e442013-07-12 13:46:57 -070042
Vladimir Marko10c13562015-11-25 14:33:36 +000043namespace dwarf {
44struct MethodDebugInfo;
45} // namespace dwarf
46
Brian Carlstrom7940e442013-07-12 13:46:57 -070047// OatHeader variable length with count of D OatDexFiles
48//
49// OatDexFile[0] one variable sized OatDexFile with offsets to Dex and OatClasses
50// OatDexFile[1]
51// ...
52// OatDexFile[D]
53//
54// Dex[0] one variable sized DexFile for each OatDexFile.
55// Dex[1] these are literal copies of the input .dex files.
56// ...
57// Dex[D]
58//
Artem Udovichenkod9786b02015-10-14 16:36:55 +030059// TypeLookupTable[0] one descriptor to class def index hash table for each OatDexFile.
60// TypeLookupTable[1]
61// ...
62// TypeLookupTable[D]
63//
Brian Carlstrom7940e442013-07-12 13:46:57 -070064// OatClass[0] one variable sized OatClass for each of C DexFile::ClassDefs
65// OatClass[1] contains OatClass entries with class status, offsets to code, etc.
66// ...
67// OatClass[C]
68//
Vladimir Marko96c6ab92014-04-08 14:00:50 +010069// GcMap one variable sized blob with GC map.
70// GcMap GC maps are deduplicated.
71// ...
72// GcMap
73//
74// VmapTable one variable sized VmapTable blob (quick compiler only).
75// VmapTable VmapTables are deduplicated.
76// ...
77// VmapTable
78//
79// MappingTable one variable sized blob with MappingTable (quick compiler only).
80// MappingTable MappingTables are deduplicated.
81// ...
82// MappingTable
83//
Brian Carlstrom7940e442013-07-12 13:46:57 -070084// padding if necessary so that the following code will be page aligned
85//
Vladimir Marko96c6ab92014-04-08 14:00:50 +010086// OatMethodHeader fixed size header for a CompiledMethod including the size of the MethodCode.
87// MethodCode one variable sized blob with the code of a CompiledMethod.
88// OatMethodHeader (OatMethodHeader, MethodCode) pairs are deduplicated.
89// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -070090// ...
Vladimir Marko96c6ab92014-04-08 14:00:50 +010091// OatMethodHeader
92// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -070093//
94class OatWriter {
95 public:
Brian Carlstrom7940e442013-07-12 13:46:57 -070096 OatWriter(const std::vector<const DexFile*>& dex_files,
97 uint32_t image_file_location_oat_checksum,
Ian Rogersef7d42f2014-01-06 12:55:46 -080098 uintptr_t image_file_location_oat_begin,
Alex Lighta59dd802014-07-02 16:28:08 -070099 int32_t image_patch_delta,
Ian Rogersca368cb2013-11-15 15:52:08 -0800100 const CompilerDriver* compiler,
Vladimir Markof4da6752014-08-01 19:04:18 +0100101 ImageWriter* image_writer,
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800102 bool compiling_boot_image,
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700103 TimingLogger* timings,
104 SafeMap<std::string, std::string>* key_value_store);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700105
Vladimir Marko09d09432015-09-08 13:47:48 +0100106 // Returns whether the oat file has an associated image.
107 bool HasImage() const {
108 // Since the image is being created at the same time as the oat file,
109 // check if there's an image writer.
110 return image_writer_ != nullptr;
111 }
112
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800113 bool HasBootImage() const {
114 return compiling_boot_image_;
115 }
116
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700117 const OatHeader& GetOatHeader() const {
118 return *oat_header_;
119 }
120
121 size_t GetSize() const {
122 return size_;
123 }
124
Vladimir Marko5c42c292015-02-25 12:02:49 +0000125 size_t GetBssSize() const {
126 return bss_size_;
127 }
128
Vladimir Marko49b0f452015-12-10 13:49:19 +0000129 ArrayRef<const uintptr_t> GetAbsolutePatchLocations() const {
130 return ArrayRef<const uintptr_t>(absolute_patch_locations_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100131 }
132
David Srbeckybc90fd02015-04-22 19:40:27 +0100133 bool WriteRodata(OutputStream* out);
134 bool WriteCode(OutputStream* out);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700135
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136 ~OatWriter();
137
Vladimir Marko49b0f452015-12-10 13:49:19 +0000138 ArrayRef<const dwarf::MethodDebugInfo> GetMethodDebugInfo() const {
139 return ArrayRef<const dwarf::MethodDebugInfo>(method_info_);
Mark Mendellae9fd932014-02-10 16:14:35 -0800140 }
141
Vladimir Markob163bb72015-03-31 21:49:49 +0100142 const CompilerDriver* GetCompilerDriver() {
143 return compiler_driver_;
144 }
145
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700146 private:
Vladimir Marko49b0f452015-12-10 13:49:19 +0000147 class OatClass;
148 class OatDexFile;
149
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100150 // The DataAccess classes are helper classes that provide access to members related to
151 // a given map, i.e. GC map, mapping table or vmap table. By abstracting these away
152 // we can share a lot of code for processing the maps with template classes below.
153 struct GcMapDataAccess;
154 struct MappingTableDataAccess;
155 struct VmapTableDataAccess;
156
157 // The function VisitDexMethods() below iterates through all the methods in all
158 // the compiled dex files in order of their definitions. The method visitor
159 // classes provide individual bits of processing for each of the passes we need to
160 // first collect the data we want to write to the oat file and then, in later passes,
161 // to actually write it.
162 class DexMethodVisitor;
163 class OatDexMethodVisitor;
164 class InitOatClassesMethodVisitor;
165 class InitCodeMethodVisitor;
166 template <typename DataAccess>
167 class InitMapMethodVisitor;
168 class InitImageMethodVisitor;
169 class WriteCodeMethodVisitor;
170 template <typename DataAccess>
171 class WriteMapMethodVisitor;
172
173 // Visit all the methods in all the compiled dex files in their definition order
174 // with a given DexMethodVisitor.
175 bool VisitDexMethods(DexMethodVisitor* visitor);
176
Brian Carlstrom7940e442013-07-12 13:46:57 -0700177 size_t InitOatHeader();
178 size_t InitOatDexFiles(size_t offset);
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300179 size_t InitLookupTables(size_t offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180 size_t InitDexFiles(size_t offset);
181 size_t InitOatClasses(size_t offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100182 size_t InitOatMaps(size_t offset);
Vladimir Marko49b0f452015-12-10 13:49:19 +0000183 size_t InitOatCode(size_t offset);
184 size_t InitOatCodeDexFiles(size_t offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700185
Ian Rogers3d504072014-03-01 09:16:49 -0800186 bool WriteTables(OutputStream* out, const size_t file_offset);
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300187 bool WriteLookupTables(OutputStream* out, const size_t file_offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100188 size_t WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset);
189 size_t WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset);
Ian Rogers3d504072014-03-01 09:16:49 -0800190 size_t WriteCodeDexFiles(OutputStream* out, const size_t file_offset, size_t relative_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191
Vladimir Marko49b0f452015-12-10 13:49:19 +0000192 bool GetOatDataOffset(OutputStream* out);
Vladimir Markof4da6752014-08-01 19:04:18 +0100193 bool WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta);
Vladimir Marko49b0f452015-12-10 13:49:19 +0000194 bool WriteData(OutputStream* out, const void* data, size_t size);
Vladimir Markof4da6752014-08-01 19:04:18 +0100195
Vladimir Marko49b0f452015-12-10 13:49:19 +0000196 dchecked_vector<dwarf::MethodDebugInfo> method_info_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800197
Brian Carlstrom7940e442013-07-12 13:46:57 -0700198 const CompilerDriver* const compiler_driver_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100199 ImageWriter* const image_writer_;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800200 const bool compiling_boot_image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700201
202 // note OatFile does not take ownership of the DexFiles
203 const std::vector<const DexFile*>* dex_files_;
204
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700205 // Size required for Oat data structures.
206 size_t size_;
207
Vladimir Marko5c42c292015-02-25 12:02:49 +0000208 // The size of the required .bss section holding the DexCache data.
209 size_t bss_size_;
210
Vladimir Marko09d09432015-09-08 13:47:48 +0100211 // Offsets of the dex cache arrays for each app dex file. For the
212 // boot image, this information is provided by the ImageWriter.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100213 SafeMap<const DexFile*, size_t> dex_cache_arrays_offsets_; // DexFiles not owned.
Vladimir Marko09d09432015-09-08 13:47:48 +0100214
Vladimir Markof4da6752014-08-01 19:04:18 +0100215 // Offset of the oat data from the start of the mmapped region of the elf file.
216 size_t oat_data_offset_;
217
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218 // dependencies on the image.
219 uint32_t image_file_location_oat_checksum_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800220 uintptr_t image_file_location_oat_begin_;
Alex Lighta59dd802014-07-02 16:28:08 -0700221 int32_t image_patch_delta_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700222
223 // data to write
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700224 SafeMap<std::string, std::string>* key_value_store_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000225 std::unique_ptr<OatHeader> oat_header_;
226 dchecked_vector<OatDexFile> oat_dex_files_;
227 dchecked_vector<OatClass> oat_classes_;
Ian Rogers700a4022014-05-19 16:49:03 -0700228 std::unique_ptr<const std::vector<uint8_t>> jni_dlsym_lookup_;
Ian Rogers700a4022014-05-19 16:49:03 -0700229 std::unique_ptr<const std::vector<uint8_t>> quick_generic_jni_trampoline_;
230 std::unique_ptr<const std::vector<uint8_t>> quick_imt_conflict_trampoline_;
231 std::unique_ptr<const std::vector<uint8_t>> quick_resolution_trampoline_;
232 std::unique_ptr<const std::vector<uint8_t>> quick_to_interpreter_bridge_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700233
234 // output stats
235 uint32_t size_dex_file_alignment_;
236 uint32_t size_executable_offset_alignment_;
237 uint32_t size_oat_header_;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700238 uint32_t size_oat_header_key_value_store_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700239 uint32_t size_dex_file_;
Ian Rogers468532e2013-08-05 10:56:33 -0700240 uint32_t size_interpreter_to_interpreter_bridge_;
241 uint32_t size_interpreter_to_compiled_code_bridge_;
242 uint32_t size_jni_dlsym_lookup_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800243 uint32_t size_quick_generic_jni_trampoline_;
Jeff Hao88474b42013-10-23 16:24:40 -0700244 uint32_t size_quick_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700245 uint32_t size_quick_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700246 uint32_t size_quick_to_interpreter_bridge_;
247 uint32_t size_trampoline_alignment_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100248 uint32_t size_method_header_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700249 uint32_t size_code_;
250 uint32_t size_code_alignment_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100251 uint32_t size_relative_call_thunks_;
Vladimir Markoc74658b2015-03-31 10:26:41 +0100252 uint32_t size_misc_thunks_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253 uint32_t size_mapping_table_;
254 uint32_t size_vmap_table_;
255 uint32_t size_gc_map_;
256 uint32_t size_oat_dex_file_location_size_;
257 uint32_t size_oat_dex_file_location_data_;
258 uint32_t size_oat_dex_file_location_checksum_;
259 uint32_t size_oat_dex_file_offset_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000260 uint32_t size_oat_dex_file_lookup_table_offset_;
261 uint32_t size_oat_dex_file_class_offsets_;
262 uint32_t size_oat_lookup_table_alignment_;
263 uint32_t size_oat_lookup_table_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700264 uint32_t size_oat_class_type_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 uint32_t size_oat_class_status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700266 uint32_t size_oat_class_method_bitmaps_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267 uint32_t size_oat_class_method_offsets_;
268
Vladimir Markob163bb72015-03-31 21:49:49 +0100269 std::unique_ptr<linker::RelativePatcher> relative_patcher_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100270
David Srbeckyf8980872015-05-22 17:04:47 +0100271 // The locations of absolute patches relative to the start of the executable section.
272 std::vector<uintptr_t> absolute_patch_locations_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100273
Vladimir Markob163bb72015-03-31 21:49:49 +0100274 // Map method reference to assigned offset.
275 // Wrap the map in a class implementing linker::RelativePatcherTargetProvider.
276 class MethodOffsetMap FINAL : public linker::RelativePatcherTargetProvider {
277 public:
278 std::pair<bool, uint32_t> FindMethodOffset(MethodReference ref) OVERRIDE;
279 SafeMap<MethodReference, uint32_t, MethodReferenceComparator> map;
280 };
281 MethodOffsetMap method_offset_map_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100282
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 DISALLOW_COPY_AND_ASSIGN(OatWriter);
284};
285
286} // namespace art
287
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700288#endif // ART_COMPILER_OAT_WRITER_H_