blob: 5e7a4a37d19a887d1082637e2facb55d9c1a3e68 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_COMPILER_OAT_WRITER_H_
18#define ART_COMPILER_OAT_WRITER_H_
Brian Carlstrom7940e442013-07-12 13:46:57 -070019
20#include <stdint.h>
Brian Carlstrom7940e442013-07-12 13:46:57 -070021#include <cstddef>
Ian Rogers700a4022014-05-19 16:49:03 -070022#include <memory>
Brian Carlstrom7940e442013-07-12 13:46:57 -070023
Vladimir Marko49b0f452015-12-10 13:49:19 +000024#include "base/dchecked_vector.h"
Vladimir Markob163bb72015-03-31 21:49:49 +010025#include "linker/relative_patcher.h" // For linker::RelativePatcherTargetProvider.
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include "mem_map.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010027#include "method_reference.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "mirror/class.h"
Artem Udovichenkod9786b02015-10-14 16:36:55 +030029#include "oat.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000030#include "os.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070031#include "safe_map.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000032#include "ScopedFd.h"
Vladimir Marko49b0f452015-12-10 13:49:19 +000033#include "utils/array_ref.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070034
35namespace art {
36
Brian Carlstromba150c32013-08-27 17:31:03 -070037class BitVector;
Andreas Gampe79273802014-08-05 20:21:05 -070038class CompiledMethod;
Vladimir Marko20f85592015-03-19 10:07:02 +000039class CompilerDriver;
Vladimir Markof4da6752014-08-01 19:04:18 +010040class ImageWriter;
Brian Carlstrom7940e442013-07-12 13:46:57 -070041class OutputStream;
Vladimir Marko20f85592015-03-19 10:07:02 +000042class TimingLogger;
Artem Udovichenkod9786b02015-10-14 16:36:55 +030043class TypeLookupTable;
Vladimir Marko9bdf1082016-01-21 12:15:52 +000044class ZipEntry;
Brian Carlstrom7940e442013-07-12 13:46:57 -070045
David Srbeckyc5bfa972016-02-05 15:49:10 +000046namespace debug {
Vladimir Marko10c13562015-11-25 14:33:36 +000047struct MethodDebugInfo;
David Srbeckyc5bfa972016-02-05 15:49:10 +000048} // namespace debug
Vladimir Marko10c13562015-11-25 14:33:36 +000049
Vladimir Marko944da602016-02-19 12:27:55 +000050namespace linker {
51class MultiOatRelativePatcher;
52} // namespace linker
53
Brian Carlstrom7940e442013-07-12 13:46:57 -070054// OatHeader variable length with count of D OatDexFiles
55//
56// OatDexFile[0] one variable sized OatDexFile with offsets to Dex and OatClasses
57// OatDexFile[1]
58// ...
59// OatDexFile[D]
60//
61// Dex[0] one variable sized DexFile for each OatDexFile.
62// Dex[1] these are literal copies of the input .dex files.
63// ...
64// Dex[D]
65//
Artem Udovichenkod9786b02015-10-14 16:36:55 +030066// TypeLookupTable[0] one descriptor to class def index hash table for each OatDexFile.
67// TypeLookupTable[1]
68// ...
69// TypeLookupTable[D]
70//
Vladimir Marko9bdf1082016-01-21 12:15:52 +000071// ClassOffsets[0] one table of OatClass offsets for each class def for each OatDexFile.
72// ClassOffsets[1]
73// ...
74// ClassOffsets[D]
75//
Brian Carlstrom7940e442013-07-12 13:46:57 -070076// OatClass[0] one variable sized OatClass for each of C DexFile::ClassDefs
77// OatClass[1] contains OatClass entries with class status, offsets to code, etc.
78// ...
79// OatClass[C]
80//
Vladimir Marko96c6ab92014-04-08 14:00:50 +010081// GcMap one variable sized blob with GC map.
82// GcMap GC maps are deduplicated.
83// ...
84// GcMap
85//
86// VmapTable one variable sized VmapTable blob (quick compiler only).
87// VmapTable VmapTables are deduplicated.
88// ...
89// VmapTable
90//
91// MappingTable one variable sized blob with MappingTable (quick compiler only).
92// MappingTable MappingTables are deduplicated.
93// ...
94// MappingTable
95//
Brian Carlstrom7940e442013-07-12 13:46:57 -070096// padding if necessary so that the following code will be page aligned
97//
Vladimir Marko96c6ab92014-04-08 14:00:50 +010098// OatMethodHeader fixed size header for a CompiledMethod including the size of the MethodCode.
99// MethodCode one variable sized blob with the code of a CompiledMethod.
100// OatMethodHeader (OatMethodHeader, MethodCode) pairs are deduplicated.
101// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -0700102// ...
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100103// OatMethodHeader
104// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -0700105//
106class OatWriter {
107 public:
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000108 enum class CreateTypeLookupTable {
109 kCreate,
110 kDontCreate,
111 kDefault = kCreate
112 };
113
114 OatWriter(bool compiling_boot_image, TimingLogger* timings);
115
116 // To produce a valid oat file, the user must first add sources with any combination of
117 // - AddDexFileSource(),
118 // - AddZippedDexFilesSource(),
119 // - AddRawDexFileSource().
120 // Then the user must call in order
121 // - WriteAndOpenDexFiles()
122 // - PrepareLayout(),
123 // - WriteRodata(),
124 // - WriteCode(),
125 // - WriteHeader().
126
127 // Add dex file source(s) from a file, either a plain dex file or
128 // a zip file with one or more dex files.
129 bool AddDexFileSource(
130 const char* filename,
131 const char* location,
132 CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
133 // Add dex file source(s) from a zip file specified by a file handle.
134 bool AddZippedDexFilesSource(
135 ScopedFd&& zip_fd,
136 const char* location,
137 CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
138 // Add dex file source from raw memory.
139 bool AddRawDexFileSource(
140 const ArrayRef<const uint8_t>& data,
141 const char* location,
142 uint32_t location_checksum,
143 CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
144 dchecked_vector<const char*> GetSourceLocations() const;
145
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800146 // Write raw dex files to the .rodata section and open them from the oat file. The verify
147 // setting dictates whether the dex file verifier should check the dex files. This is generally
148 // the case, and should only be false for tests.
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000149 bool WriteAndOpenDexFiles(OutputStream* rodata,
150 File* file,
151 InstructionSet instruction_set,
152 const InstructionSetFeatures* instruction_set_features,
153 SafeMap<std::string, std::string>* key_value_store,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800154 bool verify,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000155 /*out*/ std::unique_ptr<MemMap>* opened_dex_files_map,
156 /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files);
157 // Prepare layout of remaining data.
158 void PrepareLayout(const CompilerDriver* compiler,
159 ImageWriter* image_writer,
Vladimir Marko944da602016-02-19 12:27:55 +0000160 const std::vector<const DexFile*>& dex_files,
161 linker::MultiOatRelativePatcher* relative_patcher);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000162 // Write the rest of .rodata section (ClassOffsets[], OatClass[], maps).
163 bool WriteRodata(OutputStream* out);
164 // Write the code to the .text section.
165 bool WriteCode(OutputStream* out);
166 // Write the oat header. This finalizes the oat file.
167 bool WriteHeader(OutputStream* out,
168 uint32_t image_file_location_oat_checksum,
169 uintptr_t image_file_location_oat_begin,
170 int32_t image_patch_delta);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700171
Vladimir Marko09d09432015-09-08 13:47:48 +0100172 // Returns whether the oat file has an associated image.
173 bool HasImage() const {
174 // Since the image is being created at the same time as the oat file,
175 // check if there's an image writer.
176 return image_writer_ != nullptr;
177 }
178
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800179 bool HasBootImage() const {
180 return compiling_boot_image_;
181 }
182
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700183 const OatHeader& GetOatHeader() const {
184 return *oat_header_;
185 }
186
187 size_t GetSize() const {
188 return size_;
189 }
190
Vladimir Marko5c42c292015-02-25 12:02:49 +0000191 size_t GetBssSize() const {
192 return bss_size_;
193 }
194
Vladimir Marko944da602016-02-19 12:27:55 +0000195 size_t GetOatDataOffset() const {
196 return oat_data_offset_;
197 }
198
Vladimir Marko49b0f452015-12-10 13:49:19 +0000199 ArrayRef<const uintptr_t> GetAbsolutePatchLocations() const {
200 return ArrayRef<const uintptr_t>(absolute_patch_locations_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100201 }
202
Brian Carlstrom7940e442013-07-12 13:46:57 -0700203 ~OatWriter();
204
David Srbecky09c2a6b2016-03-11 17:11:44 +0000205 void AddMethodDebugInfos(const std::vector<debug::MethodDebugInfo>& infos) {
206 method_info_.insert(method_info_.end(), infos.begin(), infos.end());
207 }
208
David Srbeckyc5bfa972016-02-05 15:49:10 +0000209 ArrayRef<const debug::MethodDebugInfo> GetMethodDebugInfo() const {
210 return ArrayRef<const debug::MethodDebugInfo>(method_info_);
Mark Mendellae9fd932014-02-10 16:14:35 -0800211 }
212
Vladimir Markob163bb72015-03-31 21:49:49 +0100213 const CompilerDriver* GetCompilerDriver() {
214 return compiler_driver_;
215 }
216
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700217 private:
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000218 class DexFileSource;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000219 class OatClass;
220 class OatDexFile;
221
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100222 // The DataAccess classes are helper classes that provide access to members related to
223 // a given map, i.e. GC map, mapping table or vmap table. By abstracting these away
224 // we can share a lot of code for processing the maps with template classes below.
225 struct GcMapDataAccess;
226 struct MappingTableDataAccess;
227 struct VmapTableDataAccess;
228
229 // The function VisitDexMethods() below iterates through all the methods in all
230 // the compiled dex files in order of their definitions. The method visitor
231 // classes provide individual bits of processing for each of the passes we need to
232 // first collect the data we want to write to the oat file and then, in later passes,
233 // to actually write it.
234 class DexMethodVisitor;
235 class OatDexMethodVisitor;
236 class InitOatClassesMethodVisitor;
237 class InitCodeMethodVisitor;
238 template <typename DataAccess>
239 class InitMapMethodVisitor;
240 class InitImageMethodVisitor;
241 class WriteCodeMethodVisitor;
242 template <typename DataAccess>
243 class WriteMapMethodVisitor;
244
245 // Visit all the methods in all the compiled dex files in their definition order
246 // with a given DexMethodVisitor.
247 bool VisitDexMethods(DexMethodVisitor* visitor);
248
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000249 size_t InitOatHeader(InstructionSet instruction_set,
250 const InstructionSetFeatures* instruction_set_features,
251 uint32_t num_dex_files,
252 SafeMap<std::string, std::string>* key_value_store);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253 size_t InitOatDexFiles(size_t offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700254 size_t InitOatClasses(size_t offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100255 size_t InitOatMaps(size_t offset);
Vladimir Marko49b0f452015-12-10 13:49:19 +0000256 size_t InitOatCode(size_t offset);
257 size_t InitOatCodeDexFiles(size_t offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700258
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000259 bool WriteClassOffsets(OutputStream* out);
260 bool WriteClasses(OutputStream* out);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100261 size_t WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset);
262 size_t WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset);
Ian Rogers3d504072014-03-01 09:16:49 -0800263 size_t WriteCodeDexFiles(OutputStream* out, const size_t file_offset, size_t relative_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700264
Vladimir Marko944da602016-02-19 12:27:55 +0000265 bool RecordOatDataOffset(OutputStream* out);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000266 bool ReadDexFileHeader(File* file, OatDexFile* oat_dex_file);
267 bool ValidateDexFileHeader(const uint8_t* raw_header, const char* location);
268 bool WriteDexFiles(OutputStream* rodata, File* file);
269 bool WriteDexFile(OutputStream* rodata, File* file, OatDexFile* oat_dex_file);
270 bool SeekToDexFile(OutputStream* rodata, File* file, OatDexFile* oat_dex_file);
271 bool WriteDexFile(OutputStream* rodata, File* file, OatDexFile* oat_dex_file, ZipEntry* dex_file);
272 bool WriteDexFile(OutputStream* rodata, File* file, OatDexFile* oat_dex_file, File* dex_file);
273 bool WriteDexFile(OutputStream* rodata, OatDexFile* oat_dex_file, const uint8_t* dex_file);
274 bool WriteOatDexFiles(OutputStream* rodata);
275 bool ExtendForTypeLookupTables(OutputStream* rodata, File* file, size_t offset);
276 bool OpenDexFiles(File* file,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800277 bool verify,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000278 /*out*/ std::unique_ptr<MemMap>* opened_dex_files_map,
279 /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files);
280 bool WriteTypeLookupTables(MemMap* opened_dex_files_map,
281 const std::vector<std::unique_ptr<const DexFile>>& opened_dex_files);
Vladimir Markof4da6752014-08-01 19:04:18 +0100282 bool WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta);
Vladimir Marko49b0f452015-12-10 13:49:19 +0000283 bool WriteData(OutputStream* out, const void* data, size_t size);
Vladimir Marko944da602016-02-19 12:27:55 +0000284 void SetMultiOatRelativePatcherAdjustment();
Vladimir Markof4da6752014-08-01 19:04:18 +0100285
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000286 enum class WriteState {
287 kAddingDexFileSources,
288 kPrepareLayout,
289 kWriteRoData,
290 kWriteText,
291 kWriteHeader,
292 kDone
293 };
294
295 WriteState write_state_;
296 TimingLogger* timings_;
297
298 std::vector<std::unique_ptr<File>> raw_dex_files_;
299 std::vector<std::unique_ptr<ZipArchive>> zip_archives_;
300 std::vector<std::unique_ptr<ZipEntry>> zipped_dex_files_;
301
302 // Using std::list<> which doesn't move elements around on push/emplace_back().
303 // We need this because we keep plain pointers to the strings' c_str().
304 std::list<std::string> zipped_dex_file_locations_;
305
David Srbeckyc5bfa972016-02-05 15:49:10 +0000306 dchecked_vector<debug::MethodDebugInfo> method_info_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800307
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000308 const CompilerDriver* compiler_driver_;
309 ImageWriter* image_writer_;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800310 const bool compiling_boot_image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311
312 // note OatFile does not take ownership of the DexFiles
313 const std::vector<const DexFile*>* dex_files_;
314
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700315 // Size required for Oat data structures.
316 size_t size_;
317
Vladimir Marko5c42c292015-02-25 12:02:49 +0000318 // The size of the required .bss section holding the DexCache data.
319 size_t bss_size_;
320
Vladimir Marko09d09432015-09-08 13:47:48 +0100321 // Offsets of the dex cache arrays for each app dex file. For the
322 // boot image, this information is provided by the ImageWriter.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100323 SafeMap<const DexFile*, size_t> dex_cache_arrays_offsets_; // DexFiles not owned.
Vladimir Marko09d09432015-09-08 13:47:48 +0100324
Vladimir Markof4da6752014-08-01 19:04:18 +0100325 // Offset of the oat data from the start of the mmapped region of the elf file.
326 size_t oat_data_offset_;
327
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328 // data to write
Vladimir Marko49b0f452015-12-10 13:49:19 +0000329 std::unique_ptr<OatHeader> oat_header_;
330 dchecked_vector<OatDexFile> oat_dex_files_;
331 dchecked_vector<OatClass> oat_classes_;
Ian Rogers700a4022014-05-19 16:49:03 -0700332 std::unique_ptr<const std::vector<uint8_t>> jni_dlsym_lookup_;
Ian Rogers700a4022014-05-19 16:49:03 -0700333 std::unique_ptr<const std::vector<uint8_t>> quick_generic_jni_trampoline_;
334 std::unique_ptr<const std::vector<uint8_t>> quick_imt_conflict_trampoline_;
335 std::unique_ptr<const std::vector<uint8_t>> quick_resolution_trampoline_;
336 std::unique_ptr<const std::vector<uint8_t>> quick_to_interpreter_bridge_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700337
338 // output stats
339 uint32_t size_dex_file_alignment_;
340 uint32_t size_executable_offset_alignment_;
341 uint32_t size_oat_header_;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700342 uint32_t size_oat_header_key_value_store_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700343 uint32_t size_dex_file_;
Ian Rogers468532e2013-08-05 10:56:33 -0700344 uint32_t size_interpreter_to_interpreter_bridge_;
345 uint32_t size_interpreter_to_compiled_code_bridge_;
346 uint32_t size_jni_dlsym_lookup_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800347 uint32_t size_quick_generic_jni_trampoline_;
Jeff Hao88474b42013-10-23 16:24:40 -0700348 uint32_t size_quick_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700349 uint32_t size_quick_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700350 uint32_t size_quick_to_interpreter_bridge_;
351 uint32_t size_trampoline_alignment_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100352 uint32_t size_method_header_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353 uint32_t size_code_;
354 uint32_t size_code_alignment_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100355 uint32_t size_relative_call_thunks_;
Vladimir Markoc74658b2015-03-31 10:26:41 +0100356 uint32_t size_misc_thunks_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 uint32_t size_mapping_table_;
358 uint32_t size_vmap_table_;
359 uint32_t size_gc_map_;
360 uint32_t size_oat_dex_file_location_size_;
361 uint32_t size_oat_dex_file_location_data_;
362 uint32_t size_oat_dex_file_location_checksum_;
363 uint32_t size_oat_dex_file_offset_;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000364 uint32_t size_oat_dex_file_class_offsets_offset_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000365 uint32_t size_oat_dex_file_lookup_table_offset_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000366 uint32_t size_oat_lookup_table_alignment_;
367 uint32_t size_oat_lookup_table_;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000368 uint32_t size_oat_class_offsets_alignment_;
369 uint32_t size_oat_class_offsets_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700370 uint32_t size_oat_class_type_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 uint32_t size_oat_class_status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700372 uint32_t size_oat_class_method_bitmaps_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373 uint32_t size_oat_class_method_offsets_;
374
Vladimir Marko944da602016-02-19 12:27:55 +0000375 // The helper for processing relative patches is external so that we can patch across oat files.
376 linker::MultiOatRelativePatcher* relative_patcher_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100377
David Srbeckyf8980872015-05-22 17:04:47 +0100378 // The locations of absolute patches relative to the start of the executable section.
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000379 dchecked_vector<uintptr_t> absolute_patch_locations_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100380
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381 DISALLOW_COPY_AND_ASSIGN(OatWriter);
382};
383
384} // namespace art
385
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700386#endif // ART_COMPILER_OAT_WRITER_H_