blob: 824b395b027d6e1004f58bc61d367c3e513ab6ab [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"
Vladimir Marko74527972016-11-29 15:57:32 +000028#include "linker/relative_patcher.h" // For RelativePatcherTargetProvider.
Brian Carlstrom7940e442013-07-12 13:46:57 -070029#include "mem_map.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010030#include "method_reference.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070031#include "mirror/class.h"
Artem Udovichenkod9786b02015-10-14 16:36:55 +030032#include "oat.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000033#include "os.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070034#include "safe_map.h"
Vladimir Markoaad75c62016-10-03 08:46:48 +000035#include "string_reference.h"
Mathieu Chartierdbddc222017-05-24 12:04:13 -070036#include "type_reference.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070037
38namespace art {
39
Brian Carlstromba150c32013-08-27 17:31:03 -070040class BitVector;
Andreas Gampe79273802014-08-05 20:21:05 -070041class CompiledMethod;
Vladimir Marko20f85592015-03-19 10:07:02 +000042class CompilerDriver;
Jeff Hao608f2ce2016-10-19 11:17:11 -070043class ProfileCompilationInfo;
Vladimir Marko20f85592015-03-19 10:07:02 +000044class TimingLogger;
Artem Udovichenkod9786b02015-10-14 16:36:55 +030045class TypeLookupTable;
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000046class VdexFile;
Vladimir Marko9bdf1082016-01-21 12:15:52 +000047class ZipEntry;
Brian Carlstrom7940e442013-07-12 13:46:57 -070048
David Srbeckyc5bfa972016-02-05 15:49:10 +000049namespace debug {
Vladimir Marko10c13562015-11-25 14:33:36 +000050struct MethodDebugInfo;
David Srbeckyc5bfa972016-02-05 15:49:10 +000051} // namespace debug
Vladimir Marko10c13562015-11-25 14:33:36 +000052
David Brazdil5d5a36b2016-09-14 15:34:10 +010053namespace verifier {
Vladimir Marko74527972016-11-29 15:57:32 +000054class VerifierDeps;
David Brazdil5d5a36b2016-09-14 15:34:10 +010055} // namespace verifier
56
Vladimir Marko74527972016-11-29 15:57:32 +000057namespace linker {
58
59class ImageWriter;
60class MultiOatRelativePatcher;
61class OutputStream;
62
Brian Carlstrom7940e442013-07-12 13:46:57 -070063// OatHeader variable length with count of D OatDexFiles
64//
Artem Udovichenkod9786b02015-10-14 16:36:55 +030065// TypeLookupTable[0] one descriptor to class def index hash table for each OatDexFile.
66// TypeLookupTable[1]
67// ...
68// TypeLookupTable[D]
69//
Vladimir Marko9bdf1082016-01-21 12:15:52 +000070// ClassOffsets[0] one table of OatClass offsets for each class def for each OatDexFile.
71// ClassOffsets[1]
72// ...
73// ClassOffsets[D]
74//
Brian Carlstrom7940e442013-07-12 13:46:57 -070075// OatClass[0] one variable sized OatClass for each of C DexFile::ClassDefs
76// OatClass[1] contains OatClass entries with class status, offsets to code, etc.
77// ...
78// OatClass[C]
79//
Vladimir Marko0eb882b2017-05-15 13:39:18 +010080// MethodBssMapping one variable sized MethodBssMapping for each dex file, optional.
81// MethodBssMapping
Vladimir Marko96c6ab92014-04-08 14:00:50 +010082// ...
Vladimir Marko0eb882b2017-05-15 13:39:18 +010083// MethodBssMapping
Vladimir Marko96c6ab92014-04-08 14:00:50 +010084//
Vladimir Marko0eb882b2017-05-15 13:39:18 +010085// VmapTable one variable sized VmapTable blob (CodeInfo or QuickeningInfo).
Vladimir Marko96c6ab92014-04-08 14:00:50 +010086// VmapTable VmapTables are deduplicated.
87// ...
88// VmapTable
89//
Vladimir Marko0eb882b2017-05-15 13:39:18 +010090// MethodInfo one variable sized blob with MethodInfo.
91// MethodInfo MethodInfos are deduplicated.
Vladimir Marko96c6ab92014-04-08 14:00:50 +010092// ...
Vladimir Marko0eb882b2017-05-15 13:39:18 +010093// MethodInfo
94//
95// OatDexFile[0] one variable sized OatDexFile with offsets to Dex and OatClasses
96// OatDexFile[1]
97// ...
98// OatDexFile[D]
Vladimir Marko96c6ab92014-04-08 14:00:50 +010099//
Brian Carlstrom7940e442013-07-12 13:46:57 -0700100// padding if necessary so that the following code will be page aligned
101//
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100102// OatMethodHeader fixed size header for a CompiledMethod including the size of the MethodCode.
103// MethodCode one variable sized blob with the code of a CompiledMethod.
104// OatMethodHeader (OatMethodHeader, MethodCode) pairs are deduplicated.
105// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -0700106// ...
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100107// OatMethodHeader
108// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -0700109//
110class OatWriter {
111 public:
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000112 enum class CreateTypeLookupTable {
113 kCreate,
114 kDontCreate,
115 kDefault = kCreate
116 };
117
Mathieu Chartier603ccab2017-10-20 14:34:28 -0700118 OatWriter(bool compiling_boot_image,
119 TimingLogger* timings,
120 ProfileCompilationInfo* info,
121 CompactDexLevel compact_dex_level);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000122
123 // To produce a valid oat file, the user must first add sources with any combination of
124 // - AddDexFileSource(),
125 // - AddZippedDexFilesSource(),
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000126 // - AddRawDexFileSource(),
127 // - AddVdexDexFilesSource().
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000128 // Then the user must call in order
129 // - WriteAndOpenDexFiles()
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100130 // - Initialize()
131 // - WriteVerifierDeps()
132 // - WriteQuickeningInfo()
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000133 // - WriteChecksumsAndVdexHeader()
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000134 // - PrepareLayout(),
135 // - WriteRodata(),
136 // - WriteCode(),
137 // - WriteHeader().
138
139 // Add dex file source(s) from a file, either a plain dex file or
140 // a zip file with one or more dex files.
141 bool AddDexFileSource(
142 const char* filename,
143 const char* location,
144 CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
145 // Add dex file source(s) from a zip file specified by a file handle.
146 bool AddZippedDexFilesSource(
Andreas Gampe43e10b02016-07-15 17:17:34 -0700147 File&& zip_fd,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000148 const char* location,
149 CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
150 // Add dex file source from raw memory.
151 bool AddRawDexFileSource(
152 const ArrayRef<const uint8_t>& data,
153 const char* location,
154 uint32_t location_checksum,
155 CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000156 // Add dex file source(s) from a vdex file.
157 bool AddVdexDexFilesSource(
158 const VdexFile& vdex_file,
159 const char* location,
160 CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
Calin Juravle1ce70852017-06-28 10:59:03 -0700161 dchecked_vector<std::string> GetSourceLocations() const;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000162
David Brazdil7b49e6c2016-09-01 11:06:18 +0100163 // Write raw dex files to the vdex file, mmap the file and open the dex files from it.
164 // Supporting data structures are written into the .rodata section of the oat file.
165 // The `verify` setting dictates whether the dex file verifier should check the dex files.
166 // This is generally the case, and should only be false for tests.
Nicolas Geoffray81f57d12016-12-20 13:17:09 +0000167 // If `update_input_vdex` is true, then this method won't actually write the dex files,
168 // and the compiler will just re-use the existing vdex file.
David Brazdil7b49e6c2016-09-01 11:06:18 +0100169 bool WriteAndOpenDexFiles(File* vdex_file,
170 OutputStream* oat_rodata,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000171 InstructionSet instruction_set,
172 const InstructionSetFeatures* instruction_set_features,
173 SafeMap<std::string, std::string>* key_value_store,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800174 bool verify,
Nicolas Geoffray81f57d12016-12-20 13:17:09 +0000175 bool update_input_vdex,
Andreas Gampee166e672017-12-19 18:59:29 +0000176 /*out*/ std::unique_ptr<MemMap>* opened_dex_files_map,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000177 /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100178 bool WriteQuickeningInfo(OutputStream* vdex_out);
David Brazdil5d5a36b2016-09-14 15:34:10 +0100179 bool WriteVerifierDeps(OutputStream* vdex_out, verifier::VerifierDeps* verifier_deps);
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000180 bool WriteChecksumsAndVdexHeader(OutputStream* vdex_out);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100181 // Initialize the writer with the given parameters.
182 void Initialize(const CompilerDriver* compiler,
183 ImageWriter* image_writer,
184 const std::vector<const DexFile*>& dex_files) {
185 compiler_driver_ = compiler;
186 image_writer_ = image_writer;
187 dex_files_ = &dex_files;
188 }
David Brazdil5d5a36b2016-09-14 15:34:10 +0100189
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000190 // Prepare layout of remaining data.
Vladimir Marko74527972016-11-29 15:57:32 +0000191 void PrepareLayout(MultiOatRelativePatcher* relative_patcher);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000192 // Write the rest of .rodata section (ClassOffsets[], OatClass[], maps).
193 bool WriteRodata(OutputStream* out);
194 // Write the code to the .text section.
195 bool WriteCode(OutputStream* out);
196 // Write the oat header. This finalizes the oat file.
197 bool WriteHeader(OutputStream* out,
198 uint32_t image_file_location_oat_checksum,
199 uintptr_t image_file_location_oat_begin,
200 int32_t image_patch_delta);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700201
Vladimir Marko09d09432015-09-08 13:47:48 +0100202 // Returns whether the oat file has an associated image.
203 bool HasImage() const {
204 // Since the image is being created at the same time as the oat file,
205 // check if there's an image writer.
206 return image_writer_ != nullptr;
207 }
208
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800209 bool HasBootImage() const {
210 return compiling_boot_image_;
211 }
212
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700213 const OatHeader& GetOatHeader() const {
214 return *oat_header_;
215 }
216
David Brazdil7b49e6c2016-09-01 11:06:18 +0100217 size_t GetOatSize() const {
218 return oat_size_;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700219 }
220
Vladimir Marko5c42c292015-02-25 12:02:49 +0000221 size_t GetBssSize() const {
222 return bss_size_;
223 }
224
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100225 size_t GetBssMethodsOffset() const {
226 return bss_methods_offset_;
227 }
228
Vladimir Markoaad75c62016-10-03 08:46:48 +0000229 size_t GetBssRootsOffset() const {
230 return bss_roots_offset_;
231 }
232
David Srbeckyec2cdf42017-12-08 16:21:25 +0000233 size_t GetVdexSize() const {
234 return vdex_size_;
235 }
236
Vladimir Marko944da602016-02-19 12:27:55 +0000237 size_t GetOatDataOffset() const {
238 return oat_data_offset_;
239 }
240
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241 ~OatWriter();
242
David Srbeckyc5bfa972016-02-05 15:49:10 +0000243 ArrayRef<const debug::MethodDebugInfo> GetMethodDebugInfo() const {
244 return ArrayRef<const debug::MethodDebugInfo>(method_info_);
Mark Mendellae9fd932014-02-10 16:14:35 -0800245 }
246
Mathieu Chartier3957bff2017-07-16 13:55:27 -0700247 const CompilerDriver* GetCompilerDriver() const {
Vladimir Markob163bb72015-03-31 21:49:49 +0100248 return compiler_driver_;
249 }
250
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700251 private:
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000252 class DexFileSource;
Mathieu Chartier3957bff2017-07-16 13:55:27 -0700253 class OatClassHeader;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000254 class OatClass;
255 class OatDexFile;
256
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100257 // The function VisitDexMethods() below iterates through all the methods in all
258 // the compiled dex files in order of their definitions. The method visitor
259 // classes provide individual bits of processing for each of the passes we need to
260 // first collect the data we want to write to the oat file and then, in later passes,
261 // to actually write it.
262 class DexMethodVisitor;
263 class OatDexMethodVisitor;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100264 class InitBssLayoutMethodVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100265 class InitOatClassesMethodVisitor;
Igor Murashkin545412b2017-08-17 15:26:54 -0700266 class LayoutCodeMethodVisitor;
267 class LayoutReserveOffsetCodeMethodVisitor;
268 struct OrderedMethodData;
269 class OrderedMethodVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100270 class InitCodeMethodVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100271 class InitMapMethodVisitor;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700272 class InitMethodInfoVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100273 class InitImageMethodVisitor;
274 class WriteCodeMethodVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100275 class WriteMapMethodVisitor;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700276 class WriteMethodInfoVisitor;
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100277 class WriteQuickeningInfoMethodVisitor;
Mathieu Chartier210531f2018-01-12 10:15:51 -0800278 class WriteQuickeningInfoOffsetsMethodVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100279
280 // Visit all the methods in all the compiled dex files in their definition order
281 // with a given DexMethodVisitor.
282 bool VisitDexMethods(DexMethodVisitor* visitor);
283
Nicolas Geoffray81f57d12016-12-20 13:17:09 +0000284 // If `update_input_vdex` is true, then this method won't actually write the dex files,
285 // and the compiler will just re-use the existing vdex file.
286 bool WriteDexFiles(OutputStream* out, File* file, bool update_input_vdex);
287 bool WriteDexFile(OutputStream* out,
288 File* file,
289 OatDexFile* oat_dex_file,
290 bool update_input_vdex);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100291 bool SeekToDexFile(OutputStream* out, File* file, OatDexFile* oat_dex_file);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700292 bool LayoutAndWriteDexFile(OutputStream* out, OatDexFile* oat_dex_file);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100293 bool WriteDexFile(OutputStream* out,
294 File* file,
295 OatDexFile* oat_dex_file,
296 ZipEntry* dex_file);
297 bool WriteDexFile(OutputStream* out,
298 File* file,
299 OatDexFile* oat_dex_file,
300 File* dex_file);
Nicolas Geoffray81f57d12016-12-20 13:17:09 +0000301 bool WriteDexFile(OutputStream* out,
302 OatDexFile* oat_dex_file,
303 const uint8_t* dex_file,
304 bool update_input_vdex);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100305 bool OpenDexFiles(File* file,
306 bool verify,
Andreas Gampee166e672017-12-19 18:59:29 +0000307 /*out*/ std::unique_ptr<MemMap>* opened_dex_files_map,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100308 /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files);
309
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000310 size_t InitOatHeader(InstructionSet instruction_set,
311 const InstructionSetFeatures* instruction_set_features,
312 uint32_t num_dex_files,
313 SafeMap<std::string, std::string>* key_value_store);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100314 size_t InitClassOffsets(size_t offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315 size_t InitOatClasses(size_t offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100316 size_t InitOatMaps(size_t offset);
Vladimir Markof3c52b42017-11-17 17:32:12 +0000317 size_t InitIndexBssMappings(size_t offset);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100318 size_t InitOatDexFiles(size_t offset);
Vladimir Marko49b0f452015-12-10 13:49:19 +0000319 size_t InitOatCode(size_t offset);
320 size_t InitOatCodeDexFiles(size_t offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000321 void InitBssLayout(InstructionSet instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100323 size_t WriteClassOffsets(OutputStream* out, size_t file_offset, size_t relative_offset);
324 size_t WriteClasses(OutputStream* out, size_t file_offset, size_t relative_offset);
325 size_t WriteMaps(OutputStream* out, size_t file_offset, size_t relative_offset);
Vladimir Markof3c52b42017-11-17 17:32:12 +0000326 size_t WriteIndexBssMappings(OutputStream* out, size_t file_offset, size_t relative_offset);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100327 size_t WriteOatDexFiles(OutputStream* out, size_t file_offset, size_t relative_offset);
328 size_t WriteCode(OutputStream* out, size_t file_offset, size_t relative_offset);
329 size_t WriteCodeDexFiles(OutputStream* out, size_t file_offset, size_t relative_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700330
Vladimir Marko944da602016-02-19 12:27:55 +0000331 bool RecordOatDataOffset(OutputStream* out);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100332 bool WriteTypeLookupTables(OutputStream* oat_rodata,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000333 const std::vector<std::unique_ptr<const DexFile>>& opened_dex_files);
Mathieu Chartier120aa282017-08-05 16:03:03 -0700334 bool WriteDexLayoutSections(OutputStream* oat_rodata,
335 const std::vector<std::unique_ptr<const DexFile>>& opened_dex_files);
Vladimir Markof4da6752014-08-01 19:04:18 +0100336 bool WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100337 bool WriteUpTo16BytesAlignment(OutputStream* out, uint32_t size, uint32_t* stat);
Vladimir Marko944da602016-02-19 12:27:55 +0000338 void SetMultiOatRelativePatcherAdjustment();
Nicolas Geoffray81f57d12016-12-20 13:17:09 +0000339 void CloseSources();
Vladimir Markof4da6752014-08-01 19:04:18 +0100340
Mathieu Chartier3957bff2017-07-16 13:55:27 -0700341 bool MayHaveCompiledMethods() const;
342
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100343 // Find the address of the GcRoot<String> in the InternTable for a boot image string.
344 const uint8_t* LookupBootImageInternTableSlot(const DexFile& dex_file,
345 dex::StringIndex string_idx);
Vladimir Marko94ec2db2017-09-06 17:21:03 +0100346 // Find the address of the ClassTable::TableSlot for a boot image class.
347 const uint8_t* LookupBootImageClassTableSlot(const DexFile& dex_file, dex::TypeIndex type_idx);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100348
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000349 enum class WriteState {
350 kAddingDexFileSources,
351 kPrepareLayout,
352 kWriteRoData,
353 kWriteText,
354 kWriteHeader,
355 kDone
356 };
357
358 WriteState write_state_;
359 TimingLogger* timings_;
360
361 std::vector<std::unique_ptr<File>> raw_dex_files_;
362 std::vector<std::unique_ptr<ZipArchive>> zip_archives_;
363 std::vector<std::unique_ptr<ZipEntry>> zipped_dex_files_;
364
365 // Using std::list<> which doesn't move elements around on push/emplace_back().
366 // We need this because we keep plain pointers to the strings' c_str().
367 std::list<std::string> zipped_dex_file_locations_;
368
David Srbeckyc5bfa972016-02-05 15:49:10 +0000369 dchecked_vector<debug::MethodDebugInfo> method_info_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800370
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000371 const CompilerDriver* compiler_driver_;
372 ImageWriter* image_writer_;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800373 const bool compiling_boot_image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700374
375 // note OatFile does not take ownership of the DexFiles
376 const std::vector<const DexFile*>* dex_files_;
377
David Brazdil7b49e6c2016-09-01 11:06:18 +0100378 // Size required for Vdex data structures.
379 size_t vdex_size_;
380
381 // Offset of section holding Dex files inside Vdex.
382 size_t vdex_dex_files_offset_;
383
David Brazdil5d5a36b2016-09-14 15:34:10 +0100384 // Offset of section holding VerifierDeps inside Vdex.
385 size_t vdex_verifier_deps_offset_;
386
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100387 // Offset of section holding quickening info inside Vdex.
388 size_t vdex_quickening_info_offset_;
389
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700390 // Size required for Oat data structures.
David Brazdil7b49e6c2016-09-01 11:06:18 +0100391 size_t oat_size_;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700392
Vladimir Markoaad75c62016-10-03 08:46:48 +0000393 // The start of the required .bss section.
394 size_t bss_start_;
395
396 // The size of the required .bss section holding the DexCache data and GC roots.
Vladimir Marko5c42c292015-02-25 12:02:49 +0000397 size_t bss_size_;
398
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100399 // The offset of the methods in .bss section.
400 size_t bss_methods_offset_;
401
Vladimir Markoaad75c62016-10-03 08:46:48 +0000402 // The offset of the GC roots in .bss section.
403 size_t bss_roots_offset_;
404
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100405 // Map for recording references to ArtMethod entries in .bss.
406 SafeMap<const DexFile*, BitVector> bss_method_entry_references_;
407
Vladimir Markof3c52b42017-11-17 17:32:12 +0000408 // Map for recording references to GcRoot<mirror::Class> entries in .bss.
409 SafeMap<const DexFile*, BitVector> bss_type_entry_references_;
410
411 // Map for recording references to GcRoot<mirror::String> entries in .bss.
412 SafeMap<const DexFile*, BitVector> bss_string_entry_references_;
413
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100414 // Map for allocating ArtMethod entries in .bss. Indexed by MethodReference for the target
415 // method in the dex file with the "method reference value comparator" for deduplication.
416 // The value is the target offset for patching, starting at `bss_start_ + bss_methods_offset_`.
417 SafeMap<MethodReference, size_t, MethodReferenceValueComparator> bss_method_entries_;
418
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000419 // Map for allocating Class entries in .bss. Indexed by TypeReference for the source
420 // type in the dex file with the "type value comparator" for deduplication. The value
421 // is the target offset for patching, starting at `bss_start_ + bss_roots_offset_`.
422 SafeMap<TypeReference, size_t, TypeReferenceValueComparator> bss_type_entries_;
423
Vladimir Markoaad75c62016-10-03 08:46:48 +0000424 // Map for allocating String entries in .bss. Indexed by StringReference for the source
425 // string in the dex file with the "string value comparator" for deduplication. The value
426 // is the target offset for patching, starting at `bss_start_ + bss_roots_offset_`.
427 SafeMap<StringReference, size_t, StringReferenceValueComparator> bss_string_entries_;
428
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100429 // Whether boot image tables should be mapped to the .bss. This is needed for compiled
430 // code that reads from these tables with PC-relative instructions.
431 bool map_boot_image_tables_to_bss_;
432
Vladimir Markof4da6752014-08-01 19:04:18 +0100433 // Offset of the oat data from the start of the mmapped region of the elf file.
434 size_t oat_data_offset_;
435
Mathieu Chartier1b868492016-11-16 16:22:37 -0800436 // Fake OatDexFiles to hold type lookup tables for the compiler.
437 std::vector<std::unique_ptr<art::OatDexFile>> type_lookup_table_oat_dex_files_;
438
Brian Carlstrom7940e442013-07-12 13:46:57 -0700439 // data to write
Vladimir Marko49b0f452015-12-10 13:49:19 +0000440 std::unique_ptr<OatHeader> oat_header_;
441 dchecked_vector<OatDexFile> oat_dex_files_;
Mathieu Chartier3957bff2017-07-16 13:55:27 -0700442 dchecked_vector<OatClassHeader> oat_class_headers_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000443 dchecked_vector<OatClass> oat_classes_;
Ian Rogers700a4022014-05-19 16:49:03 -0700444 std::unique_ptr<const std::vector<uint8_t>> jni_dlsym_lookup_;
Ian Rogers700a4022014-05-19 16:49:03 -0700445 std::unique_ptr<const std::vector<uint8_t>> quick_generic_jni_trampoline_;
446 std::unique_ptr<const std::vector<uint8_t>> quick_imt_conflict_trampoline_;
447 std::unique_ptr<const std::vector<uint8_t>> quick_resolution_trampoline_;
448 std::unique_ptr<const std::vector<uint8_t>> quick_to_interpreter_bridge_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700449
450 // output stats
David Brazdil7b49e6c2016-09-01 11:06:18 +0100451 uint32_t size_vdex_header_;
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000452 uint32_t size_vdex_checksums_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453 uint32_t size_dex_file_alignment_;
454 uint32_t size_executable_offset_alignment_;
455 uint32_t size_oat_header_;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700456 uint32_t size_oat_header_key_value_store_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457 uint32_t size_dex_file_;
David Brazdil5d5a36b2016-09-14 15:34:10 +0100458 uint32_t size_verifier_deps_;
459 uint32_t size_verifier_deps_alignment_;
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100460 uint32_t size_quickening_info_;
461 uint32_t size_quickening_info_alignment_;
Ian Rogers468532e2013-08-05 10:56:33 -0700462 uint32_t size_interpreter_to_interpreter_bridge_;
463 uint32_t size_interpreter_to_compiled_code_bridge_;
464 uint32_t size_jni_dlsym_lookup_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800465 uint32_t size_quick_generic_jni_trampoline_;
Jeff Hao88474b42013-10-23 16:24:40 -0700466 uint32_t size_quick_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700467 uint32_t size_quick_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700468 uint32_t size_quick_to_interpreter_bridge_;
469 uint32_t size_trampoline_alignment_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100470 uint32_t size_method_header_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700471 uint32_t size_code_;
472 uint32_t size_code_alignment_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100473 uint32_t size_relative_call_thunks_;
Vladimir Markoc74658b2015-03-31 10:26:41 +0100474 uint32_t size_misc_thunks_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700475 uint32_t size_vmap_table_;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700476 uint32_t size_method_info_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700477 uint32_t size_oat_dex_file_location_size_;
478 uint32_t size_oat_dex_file_location_data_;
479 uint32_t size_oat_dex_file_location_checksum_;
480 uint32_t size_oat_dex_file_offset_;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000481 uint32_t size_oat_dex_file_class_offsets_offset_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000482 uint32_t size_oat_dex_file_lookup_table_offset_;
Mathieu Chartier120aa282017-08-05 16:03:03 -0700483 uint32_t size_oat_dex_file_dex_layout_sections_offset_;
484 uint32_t size_oat_dex_file_dex_layout_sections_;
485 uint32_t size_oat_dex_file_dex_layout_sections_alignment_;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100486 uint32_t size_oat_dex_file_method_bss_mapping_offset_;
Vladimir Markof3c52b42017-11-17 17:32:12 +0000487 uint32_t size_oat_dex_file_type_bss_mapping_offset_;
488 uint32_t size_oat_dex_file_string_bss_mapping_offset_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000489 uint32_t size_oat_lookup_table_alignment_;
490 uint32_t size_oat_lookup_table_;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000491 uint32_t size_oat_class_offsets_alignment_;
492 uint32_t size_oat_class_offsets_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700493 uint32_t size_oat_class_type_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700494 uint32_t size_oat_class_status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700495 uint32_t size_oat_class_method_bitmaps_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700496 uint32_t size_oat_class_method_offsets_;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100497 uint32_t size_method_bss_mappings_;
Vladimir Markof3c52b42017-11-17 17:32:12 +0000498 uint32_t size_type_bss_mappings_;
499 uint32_t size_string_bss_mappings_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700500
Vladimir Marko944da602016-02-19 12:27:55 +0000501 // The helper for processing relative patches is external so that we can patch across oat files.
Vladimir Marko74527972016-11-29 15:57:32 +0000502 MultiOatRelativePatcher* relative_patcher_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100503
David Srbeckyf8980872015-05-22 17:04:47 +0100504 // The locations of absolute patches relative to the start of the executable section.
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000505 dchecked_vector<uintptr_t> absolute_patch_locations_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100506
Jeff Hao608f2ce2016-10-19 11:17:11 -0700507 // Profile info used to generate new layout of files.
508 ProfileCompilationInfo* profile_compilation_info_;
509
Mathieu Chartier603ccab2017-10-20 14:34:28 -0700510 // Compact dex level that is generated.
511 CompactDexLevel compact_dex_level_;
512
Igor Murashkin545412b2017-08-17 15:26:54 -0700513 using OrderedMethodList = std::vector<OrderedMethodData>;
514
515 // List of compiled methods, sorted by the order defined in OrderedMethodData.
516 // Methods can be inserted more than once in case of duplicated methods.
517 // This pointer is only non-null after InitOatCodeDexFiles succeeds.
518 std::unique_ptr<OrderedMethodList> ordered_methods_;
519
Brian Carlstrom7940e442013-07-12 13:46:57 -0700520 DISALLOW_COPY_AND_ASSIGN(OatWriter);
521};
522
Vladimir Marko74527972016-11-29 15:57:32 +0000523} // namespace linker
Brian Carlstrom7940e442013-07-12 13:46:57 -0700524} // namespace art
525
Vladimir Marko74527972016-11-29 15:57:32 +0000526#endif // ART_DEX2OAT_LINKER_OAT_WRITER_H_