blob: 65249720d7a28fb2f9681294fa996b9736e561c1 [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
Vladimir Marko74527972016-11-29 15:57:32 +000017#ifndef ART_DEX2OAT_LINKER_OAT_WRITER_H_
18#define ART_DEX2OAT_LINKER_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>
Igor Murashkin545412b2017-08-17 15:26:54 -070023#include <vector>
Brian Carlstrom7940e442013-07-12 13:46:57 -070024
David Brazdild9c90372016-09-14 16:53:55 +010025#include "base/array_ref.h"
Vladimir Marko49b0f452015-12-10 13:49:19 +000026#include "base/dchecked_vector.h"
David Sehr67bf42e2018-02-26 16:43:04 -080027#include "base/safe_map.h"
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +000028#include "compiler.h"
David Sehr9e734c72018-01-04 17:56:19 -080029#include "dex/compact_dex_level.h"
David Srbecky32210b92017-12-04 14:39:21 +000030#include "debug/debug_info.h"
Vladimir Marko74527972016-11-29 15:57:32 +000031#include "linker/relative_patcher.h" // For RelativePatcherTargetProvider.
Brian Carlstrom7940e442013-07-12 13:46:57 -070032#include "mem_map.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010033#include "method_reference.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070034#include "mirror/class.h"
Artem Udovichenkod9786b02015-10-14 16:36:55 +030035#include "oat.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000036#include "os.h"
Vladimir Markoaad75c62016-10-03 08:46:48 +000037#include "string_reference.h"
Mathieu Chartierdbddc222017-05-24 12:04:13 -070038#include "type_reference.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070039
40namespace art {
41
Brian Carlstromba150c32013-08-27 17:31:03 -070042class BitVector;
Andreas Gampe79273802014-08-05 20:21:05 -070043class CompiledMethod;
Vladimir Marko20f85592015-03-19 10:07:02 +000044class CompilerDriver;
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -080045class DexContainer;
Jeff Hao608f2ce2016-10-19 11:17:11 -070046class ProfileCompilationInfo;
Vladimir Marko20f85592015-03-19 10:07:02 +000047class TimingLogger;
Artem Udovichenkod9786b02015-10-14 16:36:55 +030048class TypeLookupTable;
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000049class VdexFile;
Vladimir Marko9bdf1082016-01-21 12:15:52 +000050class ZipEntry;
Brian Carlstrom7940e442013-07-12 13:46:57 -070051
David Srbeckyc5bfa972016-02-05 15:49:10 +000052namespace debug {
Vladimir Marko10c13562015-11-25 14:33:36 +000053struct MethodDebugInfo;
David Srbeckyc5bfa972016-02-05 15:49:10 +000054} // namespace debug
Vladimir Marko10c13562015-11-25 14:33:36 +000055
David Brazdil5d5a36b2016-09-14 15:34:10 +010056namespace verifier {
Vladimir Marko74527972016-11-29 15:57:32 +000057class VerifierDeps;
David Brazdil5d5a36b2016-09-14 15:34:10 +010058} // namespace verifier
59
Vladimir Marko74527972016-11-29 15:57:32 +000060namespace linker {
61
62class ImageWriter;
63class MultiOatRelativePatcher;
64class OutputStream;
65
Brian Carlstrom7940e442013-07-12 13:46:57 -070066// OatHeader variable length with count of D OatDexFiles
67//
Artem Udovichenkod9786b02015-10-14 16:36:55 +030068// TypeLookupTable[0] one descriptor to class def index hash table for each OatDexFile.
69// TypeLookupTable[1]
70// ...
71// TypeLookupTable[D]
72//
Vladimir Marko9bdf1082016-01-21 12:15:52 +000073// ClassOffsets[0] one table of OatClass offsets for each class def for each OatDexFile.
74// ClassOffsets[1]
75// ...
76// ClassOffsets[D]
77//
Brian Carlstrom7940e442013-07-12 13:46:57 -070078// OatClass[0] one variable sized OatClass for each of C DexFile::ClassDefs
79// OatClass[1] contains OatClass entries with class status, offsets to code, etc.
80// ...
81// OatClass[C]
82//
Vladimir Marko0eb882b2017-05-15 13:39:18 +010083// MethodBssMapping one variable sized MethodBssMapping for each dex file, optional.
84// MethodBssMapping
Vladimir Marko96c6ab92014-04-08 14:00:50 +010085// ...
Vladimir Marko0eb882b2017-05-15 13:39:18 +010086// MethodBssMapping
Vladimir Marko96c6ab92014-04-08 14:00:50 +010087//
Vladimir Marko0eb882b2017-05-15 13:39:18 +010088// VmapTable one variable sized VmapTable blob (CodeInfo or QuickeningInfo).
Vladimir Marko96c6ab92014-04-08 14:00:50 +010089// VmapTable VmapTables are deduplicated.
90// ...
91// VmapTable
92//
Vladimir Marko0eb882b2017-05-15 13:39:18 +010093// MethodInfo one variable sized blob with MethodInfo.
94// MethodInfo MethodInfos are deduplicated.
Vladimir Marko96c6ab92014-04-08 14:00:50 +010095// ...
Vladimir Marko0eb882b2017-05-15 13:39:18 +010096// MethodInfo
97//
98// OatDexFile[0] one variable sized OatDexFile with offsets to Dex and OatClasses
99// OatDexFile[1]
100// ...
101// OatDexFile[D]
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100102//
Brian Carlstrom7940e442013-07-12 13:46:57 -0700103// padding if necessary so that the following code will be page aligned
104//
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100105// OatMethodHeader fixed size header for a CompiledMethod including the size of the MethodCode.
106// MethodCode one variable sized blob with the code of a CompiledMethod.
107// OatMethodHeader (OatMethodHeader, MethodCode) pairs are deduplicated.
108// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -0700109// ...
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100110// OatMethodHeader
111// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -0700112//
113class OatWriter {
114 public:
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000115 enum class CreateTypeLookupTable {
116 kCreate,
117 kDontCreate,
118 kDefault = kCreate
119 };
120
Mathieu Chartier603ccab2017-10-20 14:34:28 -0700121 OatWriter(bool compiling_boot_image,
122 TimingLogger* timings,
123 ProfileCompilationInfo* info,
124 CompactDexLevel compact_dex_level);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000125
126 // To produce a valid oat file, the user must first add sources with any combination of
127 // - AddDexFileSource(),
128 // - AddZippedDexFilesSource(),
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000129 // - AddRawDexFileSource(),
130 // - AddVdexDexFilesSource().
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000131 // Then the user must call in order
132 // - WriteAndOpenDexFiles()
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100133 // - Initialize()
134 // - WriteVerifierDeps()
135 // - WriteQuickeningInfo()
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000136 // - WriteChecksumsAndVdexHeader()
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000137 // - PrepareLayout(),
138 // - WriteRodata(),
139 // - WriteCode(),
Vladimir Markob066d432018-01-03 13:14:37 +0000140 // - WriteDataBimgRelRo() iff GetDataBimgRelRoSize() != 0,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000141 // - WriteHeader().
142
143 // Add dex file source(s) from a file, either a plain dex file or
144 // a zip file with one or more dex files.
145 bool AddDexFileSource(
146 const char* filename,
147 const char* location,
148 CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
149 // Add dex file source(s) from a zip file specified by a file handle.
150 bool AddZippedDexFilesSource(
Andreas Gampe43e10b02016-07-15 17:17:34 -0700151 File&& zip_fd,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000152 const char* location,
153 CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
154 // Add dex file source from raw memory.
155 bool AddRawDexFileSource(
156 const ArrayRef<const uint8_t>& data,
157 const char* location,
158 uint32_t location_checksum,
159 CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000160 // Add dex file source(s) from a vdex file.
161 bool AddVdexDexFilesSource(
162 const VdexFile& vdex_file,
163 const char* location,
164 CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
Calin Juravle1ce70852017-06-28 10:59:03 -0700165 dchecked_vector<std::string> GetSourceLocations() const;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000166
David Brazdil7b49e6c2016-09-01 11:06:18 +0100167 // Write raw dex files to the vdex file, mmap the file and open the dex files from it.
168 // Supporting data structures are written into the .rodata section of the oat file.
169 // The `verify` setting dictates whether the dex file verifier should check the dex files.
170 // This is generally the case, and should only be false for tests.
Nicolas Geoffray81f57d12016-12-20 13:17:09 +0000171 // If `update_input_vdex` is true, then this method won't actually write the dex files,
172 // and the compiler will just re-use the existing vdex file.
David Brazdil7b49e6c2016-09-01 11:06:18 +0100173 bool WriteAndOpenDexFiles(File* vdex_file,
174 OutputStream* oat_rodata,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000175 InstructionSet instruction_set,
176 const InstructionSetFeatures* instruction_set_features,
177 SafeMap<std::string, std::string>* key_value_store,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800178 bool verify,
Nicolas Geoffray81f57d12016-12-20 13:17:09 +0000179 bool update_input_vdex,
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000180 CopyOption copy_dex_files,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000181 /*out*/ std::vector<std::unique_ptr<MemMap>>* opened_dex_files_map,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000182 /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100183 bool WriteQuickeningInfo(OutputStream* vdex_out);
David Brazdil5d5a36b2016-09-14 15:34:10 +0100184 bool WriteVerifierDeps(OutputStream* vdex_out, verifier::VerifierDeps* verifier_deps);
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000185 bool WriteChecksumsAndVdexHeader(OutputStream* vdex_out);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100186 // Initialize the writer with the given parameters.
187 void Initialize(const CompilerDriver* compiler,
188 ImageWriter* image_writer,
189 const std::vector<const DexFile*>& dex_files) {
190 compiler_driver_ = compiler;
191 image_writer_ = image_writer;
192 dex_files_ = &dex_files;
193 }
David Brazdil5d5a36b2016-09-14 15:34:10 +0100194
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000195 // Prepare layout of remaining data.
Vladimir Marko74527972016-11-29 15:57:32 +0000196 void PrepareLayout(MultiOatRelativePatcher* relative_patcher);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000197 // Write the rest of .rodata section (ClassOffsets[], OatClass[], maps).
198 bool WriteRodata(OutputStream* out);
199 // Write the code to the .text section.
200 bool WriteCode(OutputStream* out);
Vladimir Markob066d432018-01-03 13:14:37 +0000201 // Write the boot image relocation data to the .data.bimg.rel.ro section.
202 bool WriteDataBimgRelRo(OutputStream* out);
203 // Check the size of the written oat file.
204 bool CheckOatSize(OutputStream* out, size_t file_offset, size_t relative_offset);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000205 // Write the oat header. This finalizes the oat file.
206 bool WriteHeader(OutputStream* out,
207 uint32_t image_file_location_oat_checksum,
208 uintptr_t image_file_location_oat_begin,
209 int32_t image_patch_delta);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700210
Vladimir Marko09d09432015-09-08 13:47:48 +0100211 // Returns whether the oat file has an associated image.
212 bool HasImage() const {
213 // Since the image is being created at the same time as the oat file,
214 // check if there's an image writer.
215 return image_writer_ != nullptr;
216 }
217
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800218 bool HasBootImage() const {
219 return compiling_boot_image_;
220 }
221
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700222 const OatHeader& GetOatHeader() const {
223 return *oat_header_;
224 }
225
Vladimir Markob066d432018-01-03 13:14:37 +0000226 size_t GetCodeSize() const {
227 return code_size_;
228 }
229
David Brazdil7b49e6c2016-09-01 11:06:18 +0100230 size_t GetOatSize() const {
231 return oat_size_;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700232 }
233
Vladimir Markob066d432018-01-03 13:14:37 +0000234 size_t GetDataBimgRelRoSize() const {
235 return data_bimg_rel_ro_size_;
236 }
237
Vladimir Marko5c42c292015-02-25 12:02:49 +0000238 size_t GetBssSize() const {
239 return bss_size_;
240 }
241
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100242 size_t GetBssMethodsOffset() const {
243 return bss_methods_offset_;
244 }
245
Vladimir Markoaad75c62016-10-03 08:46:48 +0000246 size_t GetBssRootsOffset() const {
247 return bss_roots_offset_;
248 }
249
David Srbeckyec2cdf42017-12-08 16:21:25 +0000250 size_t GetVdexSize() const {
251 return vdex_size_;
252 }
253
Vladimir Marko944da602016-02-19 12:27:55 +0000254 size_t GetOatDataOffset() const {
255 return oat_data_offset_;
256 }
257
Brian Carlstrom7940e442013-07-12 13:46:57 -0700258 ~OatWriter();
259
David Srbecky32210b92017-12-04 14:39:21 +0000260 debug::DebugInfo GetDebugInfo() const;
Mark Mendellae9fd932014-02-10 16:14:35 -0800261
Mathieu Chartier3957bff2017-07-16 13:55:27 -0700262 const CompilerDriver* GetCompilerDriver() const {
Vladimir Markob163bb72015-03-31 21:49:49 +0100263 return compiler_driver_;
264 }
265
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700266 private:
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000267 class DexFileSource;
Mathieu Chartier3957bff2017-07-16 13:55:27 -0700268 class OatClassHeader;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000269 class OatClass;
270 class OatDexFile;
271
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100272 // The function VisitDexMethods() below iterates through all the methods in all
273 // the compiled dex files in order of their definitions. The method visitor
274 // classes provide individual bits of processing for each of the passes we need to
275 // first collect the data we want to write to the oat file and then, in later passes,
276 // to actually write it.
277 class DexMethodVisitor;
278 class OatDexMethodVisitor;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100279 class InitBssLayoutMethodVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100280 class InitOatClassesMethodVisitor;
Igor Murashkin545412b2017-08-17 15:26:54 -0700281 class LayoutCodeMethodVisitor;
282 class LayoutReserveOffsetCodeMethodVisitor;
283 struct OrderedMethodData;
284 class OrderedMethodVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100285 class InitCodeMethodVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100286 class InitMapMethodVisitor;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700287 class InitMethodInfoVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100288 class InitImageMethodVisitor;
289 class WriteCodeMethodVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100290 class WriteMapMethodVisitor;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700291 class WriteMethodInfoVisitor;
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100292 class WriteQuickeningInfoMethodVisitor;
Mathieu Chartier210531f2018-01-12 10:15:51 -0800293 class WriteQuickeningInfoOffsetsMethodVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100294
295 // Visit all the methods in all the compiled dex files in their definition order
296 // with a given DexMethodVisitor.
297 bool VisitDexMethods(DexMethodVisitor* visitor);
298
Nicolas Geoffray81f57d12016-12-20 13:17:09 +0000299 // If `update_input_vdex` is true, then this method won't actually write the dex files,
300 // and the compiler will just re-use the existing vdex file.
Mathieu Chartier792111c2018-02-15 13:02:15 -0800301 bool WriteDexFiles(OutputStream* out,
302 File* file,
303 bool update_input_vdex,
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +0000304 CopyOption copy_dex_files);
Nicolas Geoffray81f57d12016-12-20 13:17:09 +0000305 bool WriteDexFile(OutputStream* out,
306 File* file,
307 OatDexFile* oat_dex_file,
308 bool update_input_vdex);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100309 bool SeekToDexFile(OutputStream* out, File* file, OatDexFile* oat_dex_file);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700310 bool LayoutAndWriteDexFile(OutputStream* out, OatDexFile* oat_dex_file);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100311 bool WriteDexFile(OutputStream* out,
312 File* file,
313 OatDexFile* oat_dex_file,
314 ZipEntry* dex_file);
315 bool WriteDexFile(OutputStream* out,
316 File* file,
317 OatDexFile* oat_dex_file,
318 File* dex_file);
Nicolas Geoffray81f57d12016-12-20 13:17:09 +0000319 bool WriteDexFile(OutputStream* out,
320 OatDexFile* oat_dex_file,
321 const uint8_t* dex_file,
322 bool update_input_vdex);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100323 bool OpenDexFiles(File* file,
324 bool verify,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000325 /*out*/ std::vector<std::unique_ptr<MemMap>>* opened_dex_files_map,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100326 /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files);
327
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000328 size_t InitOatHeader(InstructionSet instruction_set,
329 const InstructionSetFeatures* instruction_set_features,
330 uint32_t num_dex_files,
331 SafeMap<std::string, std::string>* key_value_store);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100332 size_t InitClassOffsets(size_t offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700333 size_t InitOatClasses(size_t offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100334 size_t InitOatMaps(size_t offset);
Vladimir Markof3c52b42017-11-17 17:32:12 +0000335 size_t InitIndexBssMappings(size_t offset);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100336 size_t InitOatDexFiles(size_t offset);
Vladimir Marko49b0f452015-12-10 13:49:19 +0000337 size_t InitOatCode(size_t offset);
338 size_t InitOatCodeDexFiles(size_t offset);
Vladimir Markob066d432018-01-03 13:14:37 +0000339 size_t InitDataBimgRelRoLayout(size_t offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000340 void InitBssLayout(InstructionSet instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100342 size_t WriteClassOffsets(OutputStream* out, size_t file_offset, size_t relative_offset);
343 size_t WriteClasses(OutputStream* out, size_t file_offset, size_t relative_offset);
344 size_t WriteMaps(OutputStream* out, size_t file_offset, size_t relative_offset);
Vladimir Markof3c52b42017-11-17 17:32:12 +0000345 size_t WriteIndexBssMappings(OutputStream* out, size_t file_offset, size_t relative_offset);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100346 size_t WriteOatDexFiles(OutputStream* out, size_t file_offset, size_t relative_offset);
347 size_t WriteCode(OutputStream* out, size_t file_offset, size_t relative_offset);
348 size_t WriteCodeDexFiles(OutputStream* out, size_t file_offset, size_t relative_offset);
Vladimir Markob066d432018-01-03 13:14:37 +0000349 size_t WriteDataBimgRelRo(OutputStream* out, size_t file_offset, size_t relative_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700350
Vladimir Marko944da602016-02-19 12:27:55 +0000351 bool RecordOatDataOffset(OutputStream* out);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100352 bool WriteTypeLookupTables(OutputStream* oat_rodata,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000353 const std::vector<std::unique_ptr<const DexFile>>& opened_dex_files);
Mathieu Chartier120aa282017-08-05 16:03:03 -0700354 bool WriteDexLayoutSections(OutputStream* oat_rodata,
355 const std::vector<std::unique_ptr<const DexFile>>& opened_dex_files);
Vladimir Markof4da6752014-08-01 19:04:18 +0100356 bool WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100357 bool WriteUpTo16BytesAlignment(OutputStream* out, uint32_t size, uint32_t* stat);
Vladimir Marko944da602016-02-19 12:27:55 +0000358 void SetMultiOatRelativePatcherAdjustment();
Nicolas Geoffray81f57d12016-12-20 13:17:09 +0000359 void CloseSources();
Vladimir Markof4da6752014-08-01 19:04:18 +0100360
Mathieu Chartier3957bff2017-07-16 13:55:27 -0700361 bool MayHaveCompiledMethods() const;
362
David Srbecky96c84462018-02-05 15:42:40 +0000363 bool VdexWillContainDexFiles() const {
Mathieu Chartier792111c2018-02-15 13:02:15 -0800364 return dex_files_ != nullptr && extract_dex_files_into_vdex_;
David Srbecky96c84462018-02-05 15:42:40 +0000365 }
366
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000367 enum class WriteState {
368 kAddingDexFileSources,
369 kPrepareLayout,
370 kWriteRoData,
371 kWriteText,
Vladimir Markob066d432018-01-03 13:14:37 +0000372 kWriteDataBimgRelRo,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000373 kWriteHeader,
374 kDone
375 };
376
377 WriteState write_state_;
378 TimingLogger* timings_;
379
380 std::vector<std::unique_ptr<File>> raw_dex_files_;
381 std::vector<std::unique_ptr<ZipArchive>> zip_archives_;
382 std::vector<std::unique_ptr<ZipEntry>> zipped_dex_files_;
383
384 // Using std::list<> which doesn't move elements around on push/emplace_back().
385 // We need this because we keep plain pointers to the strings' c_str().
386 std::list<std::string> zipped_dex_file_locations_;
387
David Srbeckyc5bfa972016-02-05 15:49:10 +0000388 dchecked_vector<debug::MethodDebugInfo> method_info_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800389
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000390 const CompilerDriver* compiler_driver_;
391 ImageWriter* image_writer_;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800392 const bool compiling_boot_image_;
Mathieu Chartier792111c2018-02-15 13:02:15 -0800393 // Whether the dex files being compiled are going to be extracted to the vdex.
394 bool extract_dex_files_into_vdex_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700395
396 // note OatFile does not take ownership of the DexFiles
397 const std::vector<const DexFile*>* dex_files_;
398
David Brazdil7b49e6c2016-09-01 11:06:18 +0100399 // Size required for Vdex data structures.
400 size_t vdex_size_;
401
402 // Offset of section holding Dex files inside Vdex.
403 size_t vdex_dex_files_offset_;
404
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800405 // Offset of section holding shared dex data section in the Vdex.
406 size_t vdex_dex_shared_data_offset_;
407
David Brazdil5d5a36b2016-09-14 15:34:10 +0100408 // Offset of section holding VerifierDeps inside Vdex.
409 size_t vdex_verifier_deps_offset_;
410
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100411 // Offset of section holding quickening info inside Vdex.
412 size_t vdex_quickening_info_offset_;
413
Vladimir Markob066d432018-01-03 13:14:37 +0000414 // Size of the .text segment.
415 size_t code_size_;
416
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700417 // Size required for Oat data structures.
David Brazdil7b49e6c2016-09-01 11:06:18 +0100418 size_t oat_size_;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700419
Vladimir Markob066d432018-01-03 13:14:37 +0000420 // The start of the required .data.bimg.rel.ro section.
421 size_t data_bimg_rel_ro_start_;
422
423 // The size of the required .data.bimg.rel.ro section holding the boot image relocations.
424 size_t data_bimg_rel_ro_size_;
425
Vladimir Markoaad75c62016-10-03 08:46:48 +0000426 // The start of the required .bss section.
427 size_t bss_start_;
428
429 // The size of the required .bss section holding the DexCache data and GC roots.
Vladimir Marko5c42c292015-02-25 12:02:49 +0000430 size_t bss_size_;
431
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100432 // The offset of the methods in .bss section.
433 size_t bss_methods_offset_;
434
Vladimir Markoaad75c62016-10-03 08:46:48 +0000435 // The offset of the GC roots in .bss section.
436 size_t bss_roots_offset_;
437
Vladimir Markob066d432018-01-03 13:14:37 +0000438 // Map for allocating .data.bimg.rel.ro entries. Indexed by the boot image offset of the
439 // relocation. The value is the assigned offset within the .data.bimg.rel.ro section.
440 SafeMap<uint32_t, size_t> data_bimg_rel_ro_entries_;
441
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100442 // Map for recording references to ArtMethod entries in .bss.
443 SafeMap<const DexFile*, BitVector> bss_method_entry_references_;
444
Vladimir Markof3c52b42017-11-17 17:32:12 +0000445 // Map for recording references to GcRoot<mirror::Class> entries in .bss.
446 SafeMap<const DexFile*, BitVector> bss_type_entry_references_;
447
448 // Map for recording references to GcRoot<mirror::String> entries in .bss.
449 SafeMap<const DexFile*, BitVector> bss_string_entry_references_;
450
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100451 // Map for allocating ArtMethod entries in .bss. Indexed by MethodReference for the target
452 // method in the dex file with the "method reference value comparator" for deduplication.
453 // The value is the target offset for patching, starting at `bss_start_ + bss_methods_offset_`.
454 SafeMap<MethodReference, size_t, MethodReferenceValueComparator> bss_method_entries_;
455
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000456 // Map for allocating Class entries in .bss. Indexed by TypeReference for the source
457 // type in the dex file with the "type value comparator" for deduplication. The value
458 // is the target offset for patching, starting at `bss_start_ + bss_roots_offset_`.
459 SafeMap<TypeReference, size_t, TypeReferenceValueComparator> bss_type_entries_;
460
Vladimir Markoaad75c62016-10-03 08:46:48 +0000461 // Map for allocating String entries in .bss. Indexed by StringReference for the source
462 // string in the dex file with the "string value comparator" for deduplication. The value
463 // is the target offset for patching, starting at `bss_start_ + bss_roots_offset_`.
464 SafeMap<StringReference, size_t, StringReferenceValueComparator> bss_string_entries_;
465
Vladimir Markof4da6752014-08-01 19:04:18 +0100466 // Offset of the oat data from the start of the mmapped region of the elf file.
467 size_t oat_data_offset_;
468
Mathieu Chartier1b868492016-11-16 16:22:37 -0800469 // Fake OatDexFiles to hold type lookup tables for the compiler.
470 std::vector<std::unique_ptr<art::OatDexFile>> type_lookup_table_oat_dex_files_;
471
Brian Carlstrom7940e442013-07-12 13:46:57 -0700472 // data to write
Vladimir Marko49b0f452015-12-10 13:49:19 +0000473 std::unique_ptr<OatHeader> oat_header_;
474 dchecked_vector<OatDexFile> oat_dex_files_;
Mathieu Chartier3957bff2017-07-16 13:55:27 -0700475 dchecked_vector<OatClassHeader> oat_class_headers_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000476 dchecked_vector<OatClass> oat_classes_;
Ian Rogers700a4022014-05-19 16:49:03 -0700477 std::unique_ptr<const std::vector<uint8_t>> jni_dlsym_lookup_;
Ian Rogers700a4022014-05-19 16:49:03 -0700478 std::unique_ptr<const std::vector<uint8_t>> quick_generic_jni_trampoline_;
479 std::unique_ptr<const std::vector<uint8_t>> quick_imt_conflict_trampoline_;
480 std::unique_ptr<const std::vector<uint8_t>> quick_resolution_trampoline_;
481 std::unique_ptr<const std::vector<uint8_t>> quick_to_interpreter_bridge_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700482
483 // output stats
David Brazdil7b49e6c2016-09-01 11:06:18 +0100484 uint32_t size_vdex_header_;
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000485 uint32_t size_vdex_checksums_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486 uint32_t size_dex_file_alignment_;
487 uint32_t size_executable_offset_alignment_;
488 uint32_t size_oat_header_;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700489 uint32_t size_oat_header_key_value_store_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700490 uint32_t size_dex_file_;
David Brazdil5d5a36b2016-09-14 15:34:10 +0100491 uint32_t size_verifier_deps_;
492 uint32_t size_verifier_deps_alignment_;
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100493 uint32_t size_quickening_info_;
494 uint32_t size_quickening_info_alignment_;
Ian Rogers468532e2013-08-05 10:56:33 -0700495 uint32_t size_interpreter_to_interpreter_bridge_;
496 uint32_t size_interpreter_to_compiled_code_bridge_;
497 uint32_t size_jni_dlsym_lookup_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800498 uint32_t size_quick_generic_jni_trampoline_;
Jeff Hao88474b42013-10-23 16:24:40 -0700499 uint32_t size_quick_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700500 uint32_t size_quick_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700501 uint32_t size_quick_to_interpreter_bridge_;
502 uint32_t size_trampoline_alignment_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100503 uint32_t size_method_header_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700504 uint32_t size_code_;
505 uint32_t size_code_alignment_;
Vladimir Markob066d432018-01-03 13:14:37 +0000506 uint32_t size_data_bimg_rel_ro_;
507 uint32_t size_data_bimg_rel_ro_alignment_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100508 uint32_t size_relative_call_thunks_;
Vladimir Markoc74658b2015-03-31 10:26:41 +0100509 uint32_t size_misc_thunks_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700510 uint32_t size_vmap_table_;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700511 uint32_t size_method_info_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700512 uint32_t size_oat_dex_file_location_size_;
513 uint32_t size_oat_dex_file_location_data_;
514 uint32_t size_oat_dex_file_location_checksum_;
515 uint32_t size_oat_dex_file_offset_;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000516 uint32_t size_oat_dex_file_class_offsets_offset_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000517 uint32_t size_oat_dex_file_lookup_table_offset_;
Mathieu Chartier120aa282017-08-05 16:03:03 -0700518 uint32_t size_oat_dex_file_dex_layout_sections_offset_;
519 uint32_t size_oat_dex_file_dex_layout_sections_;
520 uint32_t size_oat_dex_file_dex_layout_sections_alignment_;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100521 uint32_t size_oat_dex_file_method_bss_mapping_offset_;
Vladimir Markof3c52b42017-11-17 17:32:12 +0000522 uint32_t size_oat_dex_file_type_bss_mapping_offset_;
523 uint32_t size_oat_dex_file_string_bss_mapping_offset_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000524 uint32_t size_oat_lookup_table_alignment_;
525 uint32_t size_oat_lookup_table_;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000526 uint32_t size_oat_class_offsets_alignment_;
527 uint32_t size_oat_class_offsets_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700528 uint32_t size_oat_class_type_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700529 uint32_t size_oat_class_status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700530 uint32_t size_oat_class_method_bitmaps_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700531 uint32_t size_oat_class_method_offsets_;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100532 uint32_t size_method_bss_mappings_;
Vladimir Markof3c52b42017-11-17 17:32:12 +0000533 uint32_t size_type_bss_mappings_;
534 uint32_t size_string_bss_mappings_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700535
Vladimir Marko944da602016-02-19 12:27:55 +0000536 // The helper for processing relative patches is external so that we can patch across oat files.
Vladimir Marko74527972016-11-29 15:57:32 +0000537 MultiOatRelativePatcher* relative_patcher_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100538
David Srbeckyf8980872015-05-22 17:04:47 +0100539 // The locations of absolute patches relative to the start of the executable section.
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000540 dchecked_vector<uintptr_t> absolute_patch_locations_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100541
Jeff Hao608f2ce2016-10-19 11:17:11 -0700542 // Profile info used to generate new layout of files.
543 ProfileCompilationInfo* profile_compilation_info_;
544
Mathieu Chartier603ccab2017-10-20 14:34:28 -0700545 // Compact dex level that is generated.
546 CompactDexLevel compact_dex_level_;
547
Igor Murashkin545412b2017-08-17 15:26:54 -0700548 using OrderedMethodList = std::vector<OrderedMethodData>;
549
550 // List of compiled methods, sorted by the order defined in OrderedMethodData.
551 // Methods can be inserted more than once in case of duplicated methods.
552 // This pointer is only non-null after InitOatCodeDexFiles succeeds.
553 std::unique_ptr<OrderedMethodList> ordered_methods_;
554
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800555 // Container of shared dex data.
556 std::unique_ptr<DexContainer> dex_container_;
557
Brian Carlstrom7940e442013-07-12 13:46:57 -0700558 DISALLOW_COPY_AND_ASSIGN(OatWriter);
559};
560
Vladimir Marko74527972016-11-29 15:57:32 +0000561} // namespace linker
Brian Carlstrom7940e442013-07-12 13:46:57 -0700562} // namespace art
563
Vladimir Marko74527972016-11-29 15:57:32 +0000564#endif // ART_DEX2OAT_LINKER_OAT_WRITER_H_