blob: 6a7e1e4fbfe0aa77926b6a461584fadec80983c6 [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 Sehr9e734c72018-01-04 17:56:19 -080027#include "dex/compact_dex_level.h"
David Srbecky32210b92017-12-04 14:39:21 +000028#include "debug/debug_info.h"
Vladimir Marko74527972016-11-29 15:57:32 +000029#include "linker/relative_patcher.h" // For RelativePatcherTargetProvider.
Brian Carlstrom7940e442013-07-12 13:46:57 -070030#include "mem_map.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010031#include "method_reference.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070032#include "mirror/class.h"
Artem Udovichenkod9786b02015-10-14 16:36:55 +030033#include "oat.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000034#include "os.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070035#include "safe_map.h"
Vladimir Markoaad75c62016-10-03 08:46:48 +000036#include "string_reference.h"
Mathieu Chartierdbddc222017-05-24 12:04:13 -070037#include "type_reference.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070038
39namespace art {
40
Brian Carlstromba150c32013-08-27 17:31:03 -070041class BitVector;
Andreas Gampe79273802014-08-05 20:21:05 -070042class CompiledMethod;
Vladimir Marko20f85592015-03-19 10:07:02 +000043class CompilerDriver;
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -080044class DexContainer;
Jeff Hao608f2ce2016-10-19 11:17:11 -070045class ProfileCompilationInfo;
Vladimir Marko20f85592015-03-19 10:07:02 +000046class TimingLogger;
Artem Udovichenkod9786b02015-10-14 16:36:55 +030047class TypeLookupTable;
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000048class VdexFile;
Vladimir Marko9bdf1082016-01-21 12:15:52 +000049class ZipEntry;
Brian Carlstrom7940e442013-07-12 13:46:57 -070050
David Srbeckyc5bfa972016-02-05 15:49:10 +000051namespace debug {
Vladimir Marko10c13562015-11-25 14:33:36 +000052struct MethodDebugInfo;
David Srbeckyc5bfa972016-02-05 15:49:10 +000053} // namespace debug
Vladimir Marko10c13562015-11-25 14:33:36 +000054
David Brazdil5d5a36b2016-09-14 15:34:10 +010055namespace verifier {
Vladimir Marko74527972016-11-29 15:57:32 +000056class VerifierDeps;
David Brazdil5d5a36b2016-09-14 15:34:10 +010057} // namespace verifier
58
Vladimir Marko74527972016-11-29 15:57:32 +000059namespace linker {
60
61class ImageWriter;
62class MultiOatRelativePatcher;
63class OutputStream;
64
Brian Carlstrom7940e442013-07-12 13:46:57 -070065// OatHeader variable length with count of D OatDexFiles
66//
Artem Udovichenkod9786b02015-10-14 16:36:55 +030067// TypeLookupTable[0] one descriptor to class def index hash table for each OatDexFile.
68// TypeLookupTable[1]
69// ...
70// TypeLookupTable[D]
71//
Vladimir Marko9bdf1082016-01-21 12:15:52 +000072// ClassOffsets[0] one table of OatClass offsets for each class def for each OatDexFile.
73// ClassOffsets[1]
74// ...
75// ClassOffsets[D]
76//
Brian Carlstrom7940e442013-07-12 13:46:57 -070077// OatClass[0] one variable sized OatClass for each of C DexFile::ClassDefs
78// OatClass[1] contains OatClass entries with class status, offsets to code, etc.
79// ...
80// OatClass[C]
81//
Vladimir Marko0eb882b2017-05-15 13:39:18 +010082// MethodBssMapping one variable sized MethodBssMapping for each dex file, optional.
83// MethodBssMapping
Vladimir Marko96c6ab92014-04-08 14:00:50 +010084// ...
Vladimir Marko0eb882b2017-05-15 13:39:18 +010085// MethodBssMapping
Vladimir Marko96c6ab92014-04-08 14:00:50 +010086//
Vladimir Marko0eb882b2017-05-15 13:39:18 +010087// VmapTable one variable sized VmapTable blob (CodeInfo or QuickeningInfo).
Vladimir Marko96c6ab92014-04-08 14:00:50 +010088// VmapTable VmapTables are deduplicated.
89// ...
90// VmapTable
91//
Vladimir Marko0eb882b2017-05-15 13:39:18 +010092// MethodInfo one variable sized blob with MethodInfo.
93// MethodInfo MethodInfos are deduplicated.
Vladimir Marko96c6ab92014-04-08 14:00:50 +010094// ...
Vladimir Marko0eb882b2017-05-15 13:39:18 +010095// MethodInfo
96//
97// OatDexFile[0] one variable sized OatDexFile with offsets to Dex and OatClasses
98// OatDexFile[1]
99// ...
100// OatDexFile[D]
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100101//
Brian Carlstrom7940e442013-07-12 13:46:57 -0700102// padding if necessary so that the following code will be page aligned
103//
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100104// OatMethodHeader fixed size header for a CompiledMethod including the size of the MethodCode.
105// MethodCode one variable sized blob with the code of a CompiledMethod.
106// OatMethodHeader (OatMethodHeader, MethodCode) pairs are deduplicated.
107// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108// ...
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100109// OatMethodHeader
110// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111//
112class OatWriter {
113 public:
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000114 enum class CreateTypeLookupTable {
115 kCreate,
116 kDontCreate,
117 kDefault = kCreate
118 };
119
Mathieu Chartier603ccab2017-10-20 14:34:28 -0700120 OatWriter(bool compiling_boot_image,
121 TimingLogger* timings,
122 ProfileCompilationInfo* info,
123 CompactDexLevel compact_dex_level);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000124
125 // To produce a valid oat file, the user must first add sources with any combination of
126 // - AddDexFileSource(),
127 // - AddZippedDexFilesSource(),
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000128 // - AddRawDexFileSource(),
129 // - AddVdexDexFilesSource().
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000130 // Then the user must call in order
131 // - WriteAndOpenDexFiles()
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100132 // - Initialize()
133 // - WriteVerifierDeps()
134 // - WriteQuickeningInfo()
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000135 // - WriteChecksumsAndVdexHeader()
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000136 // - PrepareLayout(),
137 // - WriteRodata(),
138 // - WriteCode(),
139 // - WriteHeader().
140
141 // Add dex file source(s) from a file, either a plain dex file or
142 // a zip file with one or more dex files.
143 bool AddDexFileSource(
144 const char* filename,
145 const char* location,
146 CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
147 // Add dex file source(s) from a zip file specified by a file handle.
148 bool AddZippedDexFilesSource(
Andreas Gampe43e10b02016-07-15 17:17:34 -0700149 File&& zip_fd,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000150 const char* location,
151 CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
152 // Add dex file source from raw memory.
153 bool AddRawDexFileSource(
154 const ArrayRef<const uint8_t>& data,
155 const char* location,
156 uint32_t location_checksum,
157 CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000158 // Add dex file source(s) from a vdex file.
159 bool AddVdexDexFilesSource(
160 const VdexFile& vdex_file,
161 const char* location,
162 CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
Calin Juravle1ce70852017-06-28 10:59:03 -0700163 dchecked_vector<std::string> GetSourceLocations() const;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000164
David Brazdil7b49e6c2016-09-01 11:06:18 +0100165 // Write raw dex files to the vdex file, mmap the file and open the dex files from it.
166 // Supporting data structures are written into the .rodata section of the oat file.
167 // The `verify` setting dictates whether the dex file verifier should check the dex files.
168 // This is generally the case, and should only be false for tests.
Nicolas Geoffray81f57d12016-12-20 13:17:09 +0000169 // If `update_input_vdex` is true, then this method won't actually write the dex files,
170 // and the compiler will just re-use the existing vdex file.
Mathieu Chartier792111c2018-02-15 13:02:15 -0800171 // If `copy_dex_files` is true, copy the input dex files into the vdex file.
David Brazdil7b49e6c2016-09-01 11:06:18 +0100172 bool WriteAndOpenDexFiles(File* vdex_file,
173 OutputStream* oat_rodata,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000174 InstructionSet instruction_set,
175 const InstructionSetFeatures* instruction_set_features,
176 SafeMap<std::string, std::string>* key_value_store,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800177 bool verify,
Nicolas Geoffray81f57d12016-12-20 13:17:09 +0000178 bool update_input_vdex,
Mathieu Chartier792111c2018-02-15 13:02:15 -0800179 bool copy_dex_files,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000180 /*out*/ std::vector<std::unique_ptr<MemMap>>* opened_dex_files_map,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000181 /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100182 bool WriteQuickeningInfo(OutputStream* vdex_out);
David Brazdil5d5a36b2016-09-14 15:34:10 +0100183 bool WriteVerifierDeps(OutputStream* vdex_out, verifier::VerifierDeps* verifier_deps);
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000184 bool WriteChecksumsAndVdexHeader(OutputStream* vdex_out);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100185 // Initialize the writer with the given parameters.
186 void Initialize(const CompilerDriver* compiler,
187 ImageWriter* image_writer,
188 const std::vector<const DexFile*>& dex_files) {
189 compiler_driver_ = compiler;
190 image_writer_ = image_writer;
191 dex_files_ = &dex_files;
192 }
David Brazdil5d5a36b2016-09-14 15:34:10 +0100193
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000194 // Prepare layout of remaining data.
Vladimir Marko74527972016-11-29 15:57:32 +0000195 void PrepareLayout(MultiOatRelativePatcher* relative_patcher);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000196 // Write the rest of .rodata section (ClassOffsets[], OatClass[], maps).
197 bool WriteRodata(OutputStream* out);
198 // Write the code to the .text section.
199 bool WriteCode(OutputStream* out);
200 // Write the oat header. This finalizes the oat file.
201 bool WriteHeader(OutputStream* out,
202 uint32_t image_file_location_oat_checksum,
203 uintptr_t image_file_location_oat_begin,
204 int32_t image_patch_delta);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700205
Vladimir Marko09d09432015-09-08 13:47:48 +0100206 // Returns whether the oat file has an associated image.
207 bool HasImage() const {
208 // Since the image is being created at the same time as the oat file,
209 // check if there's an image writer.
210 return image_writer_ != nullptr;
211 }
212
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800213 bool HasBootImage() const {
214 return compiling_boot_image_;
215 }
216
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700217 const OatHeader& GetOatHeader() const {
218 return *oat_header_;
219 }
220
David Brazdil7b49e6c2016-09-01 11:06:18 +0100221 size_t GetOatSize() const {
222 return oat_size_;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700223 }
224
Vladimir Marko5c42c292015-02-25 12:02:49 +0000225 size_t GetBssSize() const {
226 return bss_size_;
227 }
228
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100229 size_t GetBssMethodsOffset() const {
230 return bss_methods_offset_;
231 }
232
Vladimir Markoaad75c62016-10-03 08:46:48 +0000233 size_t GetBssRootsOffset() const {
234 return bss_roots_offset_;
235 }
236
David Srbeckyec2cdf42017-12-08 16:21:25 +0000237 size_t GetVdexSize() const {
238 return vdex_size_;
239 }
240
Vladimir Marko944da602016-02-19 12:27:55 +0000241 size_t GetOatDataOffset() const {
242 return oat_data_offset_;
243 }
244
Brian Carlstrom7940e442013-07-12 13:46:57 -0700245 ~OatWriter();
246
David Srbecky32210b92017-12-04 14:39:21 +0000247 debug::DebugInfo GetDebugInfo() const;
Mark Mendellae9fd932014-02-10 16:14:35 -0800248
Mathieu Chartier3957bff2017-07-16 13:55:27 -0700249 const CompilerDriver* GetCompilerDriver() const {
Vladimir Markob163bb72015-03-31 21:49:49 +0100250 return compiler_driver_;
251 }
252
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700253 private:
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000254 class DexFileSource;
Mathieu Chartier3957bff2017-07-16 13:55:27 -0700255 class OatClassHeader;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000256 class OatClass;
257 class OatDexFile;
258
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100259 // The function VisitDexMethods() below iterates through all the methods in all
260 // the compiled dex files in order of their definitions. The method visitor
261 // classes provide individual bits of processing for each of the passes we need to
262 // first collect the data we want to write to the oat file and then, in later passes,
263 // to actually write it.
264 class DexMethodVisitor;
265 class OatDexMethodVisitor;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100266 class InitBssLayoutMethodVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100267 class InitOatClassesMethodVisitor;
Igor Murashkin545412b2017-08-17 15:26:54 -0700268 class LayoutCodeMethodVisitor;
269 class LayoutReserveOffsetCodeMethodVisitor;
270 struct OrderedMethodData;
271 class OrderedMethodVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100272 class InitCodeMethodVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100273 class InitMapMethodVisitor;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700274 class InitMethodInfoVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100275 class InitImageMethodVisitor;
276 class WriteCodeMethodVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100277 class WriteMapMethodVisitor;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700278 class WriteMethodInfoVisitor;
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100279 class WriteQuickeningInfoMethodVisitor;
Mathieu Chartier210531f2018-01-12 10:15:51 -0800280 class WriteQuickeningInfoOffsetsMethodVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100281
282 // Visit all the methods in all the compiled dex files in their definition order
283 // with a given DexMethodVisitor.
284 bool VisitDexMethods(DexMethodVisitor* visitor);
285
Nicolas Geoffray81f57d12016-12-20 13:17:09 +0000286 // If `update_input_vdex` is true, then this method won't actually write the dex files,
287 // and the compiler will just re-use the existing vdex file.
Mathieu Chartier792111c2018-02-15 13:02:15 -0800288 bool WriteDexFiles(OutputStream* out,
289 File* file,
290 bool update_input_vdex,
291 bool copy_dex_files);
Nicolas Geoffray81f57d12016-12-20 13:17:09 +0000292 bool WriteDexFile(OutputStream* out,
293 File* file,
294 OatDexFile* oat_dex_file,
295 bool update_input_vdex);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100296 bool SeekToDexFile(OutputStream* out, File* file, OatDexFile* oat_dex_file);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700297 bool LayoutAndWriteDexFile(OutputStream* out, OatDexFile* oat_dex_file);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100298 bool WriteDexFile(OutputStream* out,
299 File* file,
300 OatDexFile* oat_dex_file,
301 ZipEntry* dex_file);
302 bool WriteDexFile(OutputStream* out,
303 File* file,
304 OatDexFile* oat_dex_file,
305 File* dex_file);
Nicolas Geoffray81f57d12016-12-20 13:17:09 +0000306 bool WriteDexFile(OutputStream* out,
307 OatDexFile* oat_dex_file,
308 const uint8_t* dex_file,
309 bool update_input_vdex);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100310 bool OpenDexFiles(File* file,
311 bool verify,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000312 /*out*/ std::vector<std::unique_ptr<MemMap>>* opened_dex_files_map,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100313 /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files);
314
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000315 size_t InitOatHeader(InstructionSet instruction_set,
316 const InstructionSetFeatures* instruction_set_features,
317 uint32_t num_dex_files,
318 SafeMap<std::string, std::string>* key_value_store);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100319 size_t InitClassOffsets(size_t offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320 size_t InitOatClasses(size_t offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100321 size_t InitOatMaps(size_t offset);
Vladimir Markof3c52b42017-11-17 17:32:12 +0000322 size_t InitIndexBssMappings(size_t offset);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100323 size_t InitOatDexFiles(size_t offset);
Vladimir Marko49b0f452015-12-10 13:49:19 +0000324 size_t InitOatCode(size_t offset);
325 size_t InitOatCodeDexFiles(size_t offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000326 void InitBssLayout(InstructionSet instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700327
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100328 size_t WriteClassOffsets(OutputStream* out, size_t file_offset, size_t relative_offset);
329 size_t WriteClasses(OutputStream* out, size_t file_offset, size_t relative_offset);
330 size_t WriteMaps(OutputStream* out, size_t file_offset, size_t relative_offset);
Vladimir Markof3c52b42017-11-17 17:32:12 +0000331 size_t WriteIndexBssMappings(OutputStream* out, size_t file_offset, size_t relative_offset);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100332 size_t WriteOatDexFiles(OutputStream* out, size_t file_offset, size_t relative_offset);
333 size_t WriteCode(OutputStream* out, size_t file_offset, size_t relative_offset);
334 size_t WriteCodeDexFiles(OutputStream* out, size_t file_offset, size_t relative_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700335
Vladimir Marko944da602016-02-19 12:27:55 +0000336 bool RecordOatDataOffset(OutputStream* out);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100337 bool WriteTypeLookupTables(OutputStream* oat_rodata,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000338 const std::vector<std::unique_ptr<const DexFile>>& opened_dex_files);
Mathieu Chartier120aa282017-08-05 16:03:03 -0700339 bool WriteDexLayoutSections(OutputStream* oat_rodata,
340 const std::vector<std::unique_ptr<const DexFile>>& opened_dex_files);
Vladimir Markof4da6752014-08-01 19:04:18 +0100341 bool WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100342 bool WriteUpTo16BytesAlignment(OutputStream* out, uint32_t size, uint32_t* stat);
Vladimir Marko944da602016-02-19 12:27:55 +0000343 void SetMultiOatRelativePatcherAdjustment();
Nicolas Geoffray81f57d12016-12-20 13:17:09 +0000344 void CloseSources();
Vladimir Markof4da6752014-08-01 19:04:18 +0100345
Mathieu Chartier3957bff2017-07-16 13:55:27 -0700346 bool MayHaveCompiledMethods() const;
347
David Srbecky96c84462018-02-05 15:42:40 +0000348 bool VdexWillContainDexFiles() const {
Mathieu Chartier792111c2018-02-15 13:02:15 -0800349 return dex_files_ != nullptr && extract_dex_files_into_vdex_;
David Srbecky96c84462018-02-05 15:42:40 +0000350 }
351
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100352 // Find the address of the GcRoot<String> in the InternTable for a boot image string.
353 const uint8_t* LookupBootImageInternTableSlot(const DexFile& dex_file,
354 dex::StringIndex string_idx);
Vladimir Marko94ec2db2017-09-06 17:21:03 +0100355 // Find the address of the ClassTable::TableSlot for a boot image class.
356 const uint8_t* LookupBootImageClassTableSlot(const DexFile& dex_file, dex::TypeIndex type_idx);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100357
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000358 enum class WriteState {
359 kAddingDexFileSources,
360 kPrepareLayout,
361 kWriteRoData,
362 kWriteText,
363 kWriteHeader,
364 kDone
365 };
366
367 WriteState write_state_;
368 TimingLogger* timings_;
369
370 std::vector<std::unique_ptr<File>> raw_dex_files_;
371 std::vector<std::unique_ptr<ZipArchive>> zip_archives_;
372 std::vector<std::unique_ptr<ZipEntry>> zipped_dex_files_;
373
374 // Using std::list<> which doesn't move elements around on push/emplace_back().
375 // We need this because we keep plain pointers to the strings' c_str().
376 std::list<std::string> zipped_dex_file_locations_;
377
David Srbeckyc5bfa972016-02-05 15:49:10 +0000378 dchecked_vector<debug::MethodDebugInfo> method_info_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800379
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000380 const CompilerDriver* compiler_driver_;
381 ImageWriter* image_writer_;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800382 const bool compiling_boot_image_;
Mathieu Chartier792111c2018-02-15 13:02:15 -0800383 // Whether the dex files being compiled are going to be extracted to the vdex.
384 bool extract_dex_files_into_vdex_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700385
386 // note OatFile does not take ownership of the DexFiles
387 const std::vector<const DexFile*>* dex_files_;
388
David Brazdil7b49e6c2016-09-01 11:06:18 +0100389 // Size required for Vdex data structures.
390 size_t vdex_size_;
391
392 // Offset of section holding Dex files inside Vdex.
393 size_t vdex_dex_files_offset_;
394
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800395 // Offset of section holding shared dex data section in the Vdex.
396 size_t vdex_dex_shared_data_offset_;
397
David Brazdil5d5a36b2016-09-14 15:34:10 +0100398 // Offset of section holding VerifierDeps inside Vdex.
399 size_t vdex_verifier_deps_offset_;
400
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100401 // Offset of section holding quickening info inside Vdex.
402 size_t vdex_quickening_info_offset_;
403
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700404 // Size required for Oat data structures.
David Brazdil7b49e6c2016-09-01 11:06:18 +0100405 size_t oat_size_;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700406
Vladimir Markoaad75c62016-10-03 08:46:48 +0000407 // The start of the required .bss section.
408 size_t bss_start_;
409
410 // The size of the required .bss section holding the DexCache data and GC roots.
Vladimir Marko5c42c292015-02-25 12:02:49 +0000411 size_t bss_size_;
412
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100413 // The offset of the methods in .bss section.
414 size_t bss_methods_offset_;
415
Vladimir Markoaad75c62016-10-03 08:46:48 +0000416 // The offset of the GC roots in .bss section.
417 size_t bss_roots_offset_;
418
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100419 // Map for recording references to ArtMethod entries in .bss.
420 SafeMap<const DexFile*, BitVector> bss_method_entry_references_;
421
Vladimir Markof3c52b42017-11-17 17:32:12 +0000422 // Map for recording references to GcRoot<mirror::Class> entries in .bss.
423 SafeMap<const DexFile*, BitVector> bss_type_entry_references_;
424
425 // Map for recording references to GcRoot<mirror::String> entries in .bss.
426 SafeMap<const DexFile*, BitVector> bss_string_entry_references_;
427
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100428 // Map for allocating ArtMethod entries in .bss. Indexed by MethodReference for the target
429 // method in the dex file with the "method reference value comparator" for deduplication.
430 // The value is the target offset for patching, starting at `bss_start_ + bss_methods_offset_`.
431 SafeMap<MethodReference, size_t, MethodReferenceValueComparator> bss_method_entries_;
432
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000433 // Map for allocating Class entries in .bss. Indexed by TypeReference for the source
434 // type in the dex file with the "type value comparator" for deduplication. The value
435 // is the target offset for patching, starting at `bss_start_ + bss_roots_offset_`.
436 SafeMap<TypeReference, size_t, TypeReferenceValueComparator> bss_type_entries_;
437
Vladimir Markoaad75c62016-10-03 08:46:48 +0000438 // Map for allocating String entries in .bss. Indexed by StringReference for the source
439 // string in the dex file with the "string value comparator" for deduplication. The value
440 // is the target offset for patching, starting at `bss_start_ + bss_roots_offset_`.
441 SafeMap<StringReference, size_t, StringReferenceValueComparator> bss_string_entries_;
442
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100443 // Whether boot image tables should be mapped to the .bss. This is needed for compiled
444 // code that reads from these tables with PC-relative instructions.
445 bool map_boot_image_tables_to_bss_;
446
Vladimir Markof4da6752014-08-01 19:04:18 +0100447 // Offset of the oat data from the start of the mmapped region of the elf file.
448 size_t oat_data_offset_;
449
Mathieu Chartier1b868492016-11-16 16:22:37 -0800450 // Fake OatDexFiles to hold type lookup tables for the compiler.
451 std::vector<std::unique_ptr<art::OatDexFile>> type_lookup_table_oat_dex_files_;
452
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453 // data to write
Vladimir Marko49b0f452015-12-10 13:49:19 +0000454 std::unique_ptr<OatHeader> oat_header_;
455 dchecked_vector<OatDexFile> oat_dex_files_;
Mathieu Chartier3957bff2017-07-16 13:55:27 -0700456 dchecked_vector<OatClassHeader> oat_class_headers_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000457 dchecked_vector<OatClass> oat_classes_;
Ian Rogers700a4022014-05-19 16:49:03 -0700458 std::unique_ptr<const std::vector<uint8_t>> jni_dlsym_lookup_;
Ian Rogers700a4022014-05-19 16:49:03 -0700459 std::unique_ptr<const std::vector<uint8_t>> quick_generic_jni_trampoline_;
460 std::unique_ptr<const std::vector<uint8_t>> quick_imt_conflict_trampoline_;
461 std::unique_ptr<const std::vector<uint8_t>> quick_resolution_trampoline_;
462 std::unique_ptr<const std::vector<uint8_t>> quick_to_interpreter_bridge_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463
464 // output stats
David Brazdil7b49e6c2016-09-01 11:06:18 +0100465 uint32_t size_vdex_header_;
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000466 uint32_t size_vdex_checksums_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700467 uint32_t size_dex_file_alignment_;
468 uint32_t size_executable_offset_alignment_;
469 uint32_t size_oat_header_;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700470 uint32_t size_oat_header_key_value_store_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700471 uint32_t size_dex_file_;
David Brazdil5d5a36b2016-09-14 15:34:10 +0100472 uint32_t size_verifier_deps_;
473 uint32_t size_verifier_deps_alignment_;
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100474 uint32_t size_quickening_info_;
475 uint32_t size_quickening_info_alignment_;
Ian Rogers468532e2013-08-05 10:56:33 -0700476 uint32_t size_interpreter_to_interpreter_bridge_;
477 uint32_t size_interpreter_to_compiled_code_bridge_;
478 uint32_t size_jni_dlsym_lookup_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800479 uint32_t size_quick_generic_jni_trampoline_;
Jeff Hao88474b42013-10-23 16:24:40 -0700480 uint32_t size_quick_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700481 uint32_t size_quick_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700482 uint32_t size_quick_to_interpreter_bridge_;
483 uint32_t size_trampoline_alignment_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100484 uint32_t size_method_header_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700485 uint32_t size_code_;
486 uint32_t size_code_alignment_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100487 uint32_t size_relative_call_thunks_;
Vladimir Markoc74658b2015-03-31 10:26:41 +0100488 uint32_t size_misc_thunks_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700489 uint32_t size_vmap_table_;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700490 uint32_t size_method_info_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491 uint32_t size_oat_dex_file_location_size_;
492 uint32_t size_oat_dex_file_location_data_;
493 uint32_t size_oat_dex_file_location_checksum_;
494 uint32_t size_oat_dex_file_offset_;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000495 uint32_t size_oat_dex_file_class_offsets_offset_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000496 uint32_t size_oat_dex_file_lookup_table_offset_;
Mathieu Chartier120aa282017-08-05 16:03:03 -0700497 uint32_t size_oat_dex_file_dex_layout_sections_offset_;
498 uint32_t size_oat_dex_file_dex_layout_sections_;
499 uint32_t size_oat_dex_file_dex_layout_sections_alignment_;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100500 uint32_t size_oat_dex_file_method_bss_mapping_offset_;
Vladimir Markof3c52b42017-11-17 17:32:12 +0000501 uint32_t size_oat_dex_file_type_bss_mapping_offset_;
502 uint32_t size_oat_dex_file_string_bss_mapping_offset_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000503 uint32_t size_oat_lookup_table_alignment_;
504 uint32_t size_oat_lookup_table_;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000505 uint32_t size_oat_class_offsets_alignment_;
506 uint32_t size_oat_class_offsets_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700507 uint32_t size_oat_class_type_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700508 uint32_t size_oat_class_status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700509 uint32_t size_oat_class_method_bitmaps_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700510 uint32_t size_oat_class_method_offsets_;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100511 uint32_t size_method_bss_mappings_;
Vladimir Markof3c52b42017-11-17 17:32:12 +0000512 uint32_t size_type_bss_mappings_;
513 uint32_t size_string_bss_mappings_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700514
Vladimir Marko944da602016-02-19 12:27:55 +0000515 // The helper for processing relative patches is external so that we can patch across oat files.
Vladimir Marko74527972016-11-29 15:57:32 +0000516 MultiOatRelativePatcher* relative_patcher_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100517
David Srbeckyf8980872015-05-22 17:04:47 +0100518 // The locations of absolute patches relative to the start of the executable section.
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000519 dchecked_vector<uintptr_t> absolute_patch_locations_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100520
Jeff Hao608f2ce2016-10-19 11:17:11 -0700521 // Profile info used to generate new layout of files.
522 ProfileCompilationInfo* profile_compilation_info_;
523
Mathieu Chartier603ccab2017-10-20 14:34:28 -0700524 // Compact dex level that is generated.
525 CompactDexLevel compact_dex_level_;
526
Igor Murashkin545412b2017-08-17 15:26:54 -0700527 using OrderedMethodList = std::vector<OrderedMethodData>;
528
529 // List of compiled methods, sorted by the order defined in OrderedMethodData.
530 // Methods can be inserted more than once in case of duplicated methods.
531 // This pointer is only non-null after InitOatCodeDexFiles succeeds.
532 std::unique_ptr<OrderedMethodList> ordered_methods_;
533
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800534 // Container of shared dex data.
535 std::unique_ptr<DexContainer> dex_container_;
536
Brian Carlstrom7940e442013-07-12 13:46:57 -0700537 DISALLOW_COPY_AND_ASSIGN(OatWriter);
538};
539
Vladimir Marko74527972016-11-29 15:57:32 +0000540} // namespace linker
Brian Carlstrom7940e442013-07-12 13:46:57 -0700541} // namespace art
542
Vladimir Marko74527972016-11-29 15:57:32 +0000543#endif // ART_DEX2OAT_LINKER_OAT_WRITER_H_