blob: 4055878b55d46169a26e941acc050341037da84a [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"
Mathieu Chartier603ccab2017-10-20 14:34:28 -070027#include "cdex/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
Vladimir Marko944da602016-02-19 12:27:55 +0000233 size_t GetOatDataOffset() const {
234 return oat_data_offset_;
235 }
236
Brian Carlstrom7940e442013-07-12 13:46:57 -0700237 ~OatWriter();
238
David Srbeckyc5bfa972016-02-05 15:49:10 +0000239 ArrayRef<const debug::MethodDebugInfo> GetMethodDebugInfo() const {
240 return ArrayRef<const debug::MethodDebugInfo>(method_info_);
Mark Mendellae9fd932014-02-10 16:14:35 -0800241 }
242
Mathieu Chartier3957bff2017-07-16 13:55:27 -0700243 const CompilerDriver* GetCompilerDriver() const {
Vladimir Markob163bb72015-03-31 21:49:49 +0100244 return compiler_driver_;
245 }
246
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700247 private:
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000248 class DexFileSource;
Mathieu Chartier3957bff2017-07-16 13:55:27 -0700249 class OatClassHeader;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000250 class OatClass;
251 class OatDexFile;
252
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100253 // The function VisitDexMethods() below iterates through all the methods in all
254 // the compiled dex files in order of their definitions. The method visitor
255 // classes provide individual bits of processing for each of the passes we need to
256 // first collect the data we want to write to the oat file and then, in later passes,
257 // to actually write it.
258 class DexMethodVisitor;
259 class OatDexMethodVisitor;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100260 class InitBssLayoutMethodVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100261 class InitOatClassesMethodVisitor;
Igor Murashkin545412b2017-08-17 15:26:54 -0700262 class LayoutCodeMethodVisitor;
263 class LayoutReserveOffsetCodeMethodVisitor;
264 struct OrderedMethodData;
265 class OrderedMethodVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100266 class InitCodeMethodVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100267 class InitMapMethodVisitor;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700268 class InitMethodInfoVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100269 class InitImageMethodVisitor;
270 class WriteCodeMethodVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100271 class WriteMapMethodVisitor;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700272 class WriteMethodInfoVisitor;
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100273 class WriteQuickeningInfoMethodVisitor;
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100274 class WriteQuickeningIndicesMethodVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100275
276 // Visit all the methods in all the compiled dex files in their definition order
277 // with a given DexMethodVisitor.
278 bool VisitDexMethods(DexMethodVisitor* visitor);
279
Nicolas Geoffray81f57d12016-12-20 13:17:09 +0000280 // If `update_input_vdex` is true, then this method won't actually write the dex files,
281 // and the compiler will just re-use the existing vdex file.
282 bool WriteDexFiles(OutputStream* out, File* file, bool update_input_vdex);
283 bool WriteDexFile(OutputStream* out,
284 File* file,
285 OatDexFile* oat_dex_file,
286 bool update_input_vdex);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100287 bool SeekToDexFile(OutputStream* out, File* file, OatDexFile* oat_dex_file);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700288 bool LayoutAndWriteDexFile(OutputStream* out, OatDexFile* oat_dex_file);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100289 bool WriteDexFile(OutputStream* out,
290 File* file,
291 OatDexFile* oat_dex_file,
292 ZipEntry* dex_file);
293 bool WriteDexFile(OutputStream* out,
294 File* file,
295 OatDexFile* oat_dex_file,
296 File* dex_file);
Nicolas Geoffray81f57d12016-12-20 13:17:09 +0000297 bool WriteDexFile(OutputStream* out,
298 OatDexFile* oat_dex_file,
299 const uint8_t* dex_file,
300 bool update_input_vdex);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100301 bool OpenDexFiles(File* file,
302 bool verify,
Andreas Gampee166e672017-12-19 18:59:29 +0000303 /*out*/ std::unique_ptr<MemMap>* opened_dex_files_map,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100304 /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files);
305
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000306 size_t InitOatHeader(InstructionSet instruction_set,
307 const InstructionSetFeatures* instruction_set_features,
308 uint32_t num_dex_files,
309 SafeMap<std::string, std::string>* key_value_store);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100310 size_t InitClassOffsets(size_t offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311 size_t InitOatClasses(size_t offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100312 size_t InitOatMaps(size_t offset);
Vladimir Markof3c52b42017-11-17 17:32:12 +0000313 size_t InitIndexBssMappings(size_t offset);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100314 size_t InitOatDexFiles(size_t offset);
Vladimir Marko49b0f452015-12-10 13:49:19 +0000315 size_t InitOatCode(size_t offset);
316 size_t InitOatCodeDexFiles(size_t offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000317 void InitBssLayout(InstructionSet instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100319 size_t WriteClassOffsets(OutputStream* out, size_t file_offset, size_t relative_offset);
320 size_t WriteClasses(OutputStream* out, size_t file_offset, size_t relative_offset);
321 size_t WriteMaps(OutputStream* out, size_t file_offset, size_t relative_offset);
Vladimir Markof3c52b42017-11-17 17:32:12 +0000322 size_t WriteIndexBssMappings(OutputStream* out, size_t file_offset, size_t relative_offset);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100323 size_t WriteOatDexFiles(OutputStream* out, size_t file_offset, size_t relative_offset);
324 size_t WriteCode(OutputStream* out, size_t file_offset, size_t relative_offset);
325 size_t WriteCodeDexFiles(OutputStream* out, size_t file_offset, size_t relative_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326
Vladimir Marko944da602016-02-19 12:27:55 +0000327 bool RecordOatDataOffset(OutputStream* out);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100328 bool WriteTypeLookupTables(OutputStream* oat_rodata,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000329 const std::vector<std::unique_ptr<const DexFile>>& opened_dex_files);
Mathieu Chartier120aa282017-08-05 16:03:03 -0700330 bool WriteDexLayoutSections(OutputStream* oat_rodata,
331 const std::vector<std::unique_ptr<const DexFile>>& opened_dex_files);
Vladimir Markof4da6752014-08-01 19:04:18 +0100332 bool WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100333 bool WriteUpTo16BytesAlignment(OutputStream* out, uint32_t size, uint32_t* stat);
Vladimir Marko944da602016-02-19 12:27:55 +0000334 void SetMultiOatRelativePatcherAdjustment();
Nicolas Geoffray81f57d12016-12-20 13:17:09 +0000335 void CloseSources();
Vladimir Markof4da6752014-08-01 19:04:18 +0100336
Mathieu Chartier3957bff2017-07-16 13:55:27 -0700337 bool MayHaveCompiledMethods() const;
338
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100339 // Find the address of the GcRoot<String> in the InternTable for a boot image string.
340 const uint8_t* LookupBootImageInternTableSlot(const DexFile& dex_file,
341 dex::StringIndex string_idx);
Vladimir Marko94ec2db2017-09-06 17:21:03 +0100342 // Find the address of the ClassTable::TableSlot for a boot image class.
343 const uint8_t* LookupBootImageClassTableSlot(const DexFile& dex_file, dex::TypeIndex type_idx);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100344
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000345 enum class WriteState {
346 kAddingDexFileSources,
347 kPrepareLayout,
348 kWriteRoData,
349 kWriteText,
350 kWriteHeader,
351 kDone
352 };
353
354 WriteState write_state_;
355 TimingLogger* timings_;
356
357 std::vector<std::unique_ptr<File>> raw_dex_files_;
358 std::vector<std::unique_ptr<ZipArchive>> zip_archives_;
359 std::vector<std::unique_ptr<ZipEntry>> zipped_dex_files_;
360
361 // Using std::list<> which doesn't move elements around on push/emplace_back().
362 // We need this because we keep plain pointers to the strings' c_str().
363 std::list<std::string> zipped_dex_file_locations_;
364
David Srbeckyc5bfa972016-02-05 15:49:10 +0000365 dchecked_vector<debug::MethodDebugInfo> method_info_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800366
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000367 const CompilerDriver* compiler_driver_;
368 ImageWriter* image_writer_;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800369 const bool compiling_boot_image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700370
371 // note OatFile does not take ownership of the DexFiles
372 const std::vector<const DexFile*>* dex_files_;
373
David Brazdil7b49e6c2016-09-01 11:06:18 +0100374 // Size required for Vdex data structures.
375 size_t vdex_size_;
376
377 // Offset of section holding Dex files inside Vdex.
378 size_t vdex_dex_files_offset_;
379
David Brazdil5d5a36b2016-09-14 15:34:10 +0100380 // Offset of section holding VerifierDeps inside Vdex.
381 size_t vdex_verifier_deps_offset_;
382
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100383 // Offset of section holding quickening info inside Vdex.
384 size_t vdex_quickening_info_offset_;
385
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700386 // Size required for Oat data structures.
David Brazdil7b49e6c2016-09-01 11:06:18 +0100387 size_t oat_size_;
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700388
Vladimir Markoaad75c62016-10-03 08:46:48 +0000389 // The start of the required .bss section.
390 size_t bss_start_;
391
392 // The size of the required .bss section holding the DexCache data and GC roots.
Vladimir Marko5c42c292015-02-25 12:02:49 +0000393 size_t bss_size_;
394
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100395 // The offset of the methods in .bss section.
396 size_t bss_methods_offset_;
397
Vladimir Markoaad75c62016-10-03 08:46:48 +0000398 // The offset of the GC roots in .bss section.
399 size_t bss_roots_offset_;
400
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100401 // Map for recording references to ArtMethod entries in .bss.
402 SafeMap<const DexFile*, BitVector> bss_method_entry_references_;
403
Vladimir Markof3c52b42017-11-17 17:32:12 +0000404 // Map for recording references to GcRoot<mirror::Class> entries in .bss.
405 SafeMap<const DexFile*, BitVector> bss_type_entry_references_;
406
407 // Map for recording references to GcRoot<mirror::String> entries in .bss.
408 SafeMap<const DexFile*, BitVector> bss_string_entry_references_;
409
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100410 // Map for allocating ArtMethod entries in .bss. Indexed by MethodReference for the target
411 // method in the dex file with the "method reference value comparator" for deduplication.
412 // The value is the target offset for patching, starting at `bss_start_ + bss_methods_offset_`.
413 SafeMap<MethodReference, size_t, MethodReferenceValueComparator> bss_method_entries_;
414
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000415 // Map for allocating Class entries in .bss. Indexed by TypeReference for the source
416 // type in the dex file with the "type value comparator" for deduplication. The value
417 // is the target offset for patching, starting at `bss_start_ + bss_roots_offset_`.
418 SafeMap<TypeReference, size_t, TypeReferenceValueComparator> bss_type_entries_;
419
Vladimir Markoaad75c62016-10-03 08:46:48 +0000420 // Map for allocating String entries in .bss. Indexed by StringReference for the source
421 // string in the dex file with the "string value comparator" for deduplication. The value
422 // is the target offset for patching, starting at `bss_start_ + bss_roots_offset_`.
423 SafeMap<StringReference, size_t, StringReferenceValueComparator> bss_string_entries_;
424
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100425 // Whether boot image tables should be mapped to the .bss. This is needed for compiled
426 // code that reads from these tables with PC-relative instructions.
427 bool map_boot_image_tables_to_bss_;
428
Vladimir Markof4da6752014-08-01 19:04:18 +0100429 // Offset of the oat data from the start of the mmapped region of the elf file.
430 size_t oat_data_offset_;
431
Mathieu Chartier1b868492016-11-16 16:22:37 -0800432 // Fake OatDexFiles to hold type lookup tables for the compiler.
433 std::vector<std::unique_ptr<art::OatDexFile>> type_lookup_table_oat_dex_files_;
434
Brian Carlstrom7940e442013-07-12 13:46:57 -0700435 // data to write
Vladimir Marko49b0f452015-12-10 13:49:19 +0000436 std::unique_ptr<OatHeader> oat_header_;
437 dchecked_vector<OatDexFile> oat_dex_files_;
Mathieu Chartier3957bff2017-07-16 13:55:27 -0700438 dchecked_vector<OatClassHeader> oat_class_headers_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000439 dchecked_vector<OatClass> oat_classes_;
Ian Rogers700a4022014-05-19 16:49:03 -0700440 std::unique_ptr<const std::vector<uint8_t>> jni_dlsym_lookup_;
Ian Rogers700a4022014-05-19 16:49:03 -0700441 std::unique_ptr<const std::vector<uint8_t>> quick_generic_jni_trampoline_;
442 std::unique_ptr<const std::vector<uint8_t>> quick_imt_conflict_trampoline_;
443 std::unique_ptr<const std::vector<uint8_t>> quick_resolution_trampoline_;
444 std::unique_ptr<const std::vector<uint8_t>> quick_to_interpreter_bridge_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700445
446 // output stats
David Brazdil7b49e6c2016-09-01 11:06:18 +0100447 uint32_t size_vdex_header_;
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000448 uint32_t size_vdex_checksums_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700449 uint32_t size_dex_file_alignment_;
450 uint32_t size_executable_offset_alignment_;
451 uint32_t size_oat_header_;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700452 uint32_t size_oat_header_key_value_store_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453 uint32_t size_dex_file_;
David Brazdil5d5a36b2016-09-14 15:34:10 +0100454 uint32_t size_verifier_deps_;
455 uint32_t size_verifier_deps_alignment_;
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100456 uint32_t size_quickening_info_;
457 uint32_t size_quickening_info_alignment_;
Ian Rogers468532e2013-08-05 10:56:33 -0700458 uint32_t size_interpreter_to_interpreter_bridge_;
459 uint32_t size_interpreter_to_compiled_code_bridge_;
460 uint32_t size_jni_dlsym_lookup_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800461 uint32_t size_quick_generic_jni_trampoline_;
Jeff Hao88474b42013-10-23 16:24:40 -0700462 uint32_t size_quick_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463 uint32_t size_quick_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700464 uint32_t size_quick_to_interpreter_bridge_;
465 uint32_t size_trampoline_alignment_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100466 uint32_t size_method_header_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700467 uint32_t size_code_;
468 uint32_t size_code_alignment_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100469 uint32_t size_relative_call_thunks_;
Vladimir Markoc74658b2015-03-31 10:26:41 +0100470 uint32_t size_misc_thunks_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700471 uint32_t size_vmap_table_;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700472 uint32_t size_method_info_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700473 uint32_t size_oat_dex_file_location_size_;
474 uint32_t size_oat_dex_file_location_data_;
475 uint32_t size_oat_dex_file_location_checksum_;
476 uint32_t size_oat_dex_file_offset_;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000477 uint32_t size_oat_dex_file_class_offsets_offset_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000478 uint32_t size_oat_dex_file_lookup_table_offset_;
Mathieu Chartier120aa282017-08-05 16:03:03 -0700479 uint32_t size_oat_dex_file_dex_layout_sections_offset_;
480 uint32_t size_oat_dex_file_dex_layout_sections_;
481 uint32_t size_oat_dex_file_dex_layout_sections_alignment_;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100482 uint32_t size_oat_dex_file_method_bss_mapping_offset_;
Vladimir Markof3c52b42017-11-17 17:32:12 +0000483 uint32_t size_oat_dex_file_type_bss_mapping_offset_;
484 uint32_t size_oat_dex_file_string_bss_mapping_offset_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000485 uint32_t size_oat_lookup_table_alignment_;
486 uint32_t size_oat_lookup_table_;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000487 uint32_t size_oat_class_offsets_alignment_;
488 uint32_t size_oat_class_offsets_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700489 uint32_t size_oat_class_type_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700490 uint32_t size_oat_class_status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700491 uint32_t size_oat_class_method_bitmaps_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 uint32_t size_oat_class_method_offsets_;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100493 uint32_t size_method_bss_mappings_;
Vladimir Markof3c52b42017-11-17 17:32:12 +0000494 uint32_t size_type_bss_mappings_;
495 uint32_t size_string_bss_mappings_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700496
Vladimir Marko944da602016-02-19 12:27:55 +0000497 // The helper for processing relative patches is external so that we can patch across oat files.
Vladimir Marko74527972016-11-29 15:57:32 +0000498 MultiOatRelativePatcher* relative_patcher_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100499
David Srbeckyf8980872015-05-22 17:04:47 +0100500 // The locations of absolute patches relative to the start of the executable section.
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000501 dchecked_vector<uintptr_t> absolute_patch_locations_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100502
Jeff Hao608f2ce2016-10-19 11:17:11 -0700503 // Profile info used to generate new layout of files.
504 ProfileCompilationInfo* profile_compilation_info_;
505
Mathieu Chartier603ccab2017-10-20 14:34:28 -0700506 // Compact dex level that is generated.
507 CompactDexLevel compact_dex_level_;
508
Igor Murashkin545412b2017-08-17 15:26:54 -0700509 using OrderedMethodList = std::vector<OrderedMethodData>;
510
511 // List of compiled methods, sorted by the order defined in OrderedMethodData.
512 // Methods can be inserted more than once in case of duplicated methods.
513 // This pointer is only non-null after InitOatCodeDexFiles succeeds.
514 std::unique_ptr<OrderedMethodList> ordered_methods_;
515
Brian Carlstrom7940e442013-07-12 13:46:57 -0700516 DISALLOW_COPY_AND_ASSIGN(OatWriter);
517};
518
Vladimir Marko74527972016-11-29 15:57:32 +0000519} // namespace linker
Brian Carlstrom7940e442013-07-12 13:46:57 -0700520} // namespace art
521
Vladimir Marko74527972016-11-29 15:57:32 +0000522#endif // ART_DEX2OAT_LINKER_OAT_WRITER_H_