blob: decb7dbc265536b4dbc559183beaa856651b73ff [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 Marko49b0f452015-12-10 13:49:19 +000032#include "utils/array_ref.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070033
34namespace art {
35
Brian Carlstromba150c32013-08-27 17:31:03 -070036class BitVector;
Andreas Gampe79273802014-08-05 20:21:05 -070037class CompiledMethod;
Vladimir Marko20f85592015-03-19 10:07:02 +000038class CompilerDriver;
Vladimir Markof4da6752014-08-01 19:04:18 +010039class ImageWriter;
Brian Carlstrom7940e442013-07-12 13:46:57 -070040class OutputStream;
Vladimir Marko20f85592015-03-19 10:07:02 +000041class TimingLogger;
Artem Udovichenkod9786b02015-10-14 16:36:55 +030042class TypeLookupTable;
Vladimir Marko9bdf1082016-01-21 12:15:52 +000043class ZipEntry;
Brian Carlstrom7940e442013-07-12 13:46:57 -070044
David Srbeckyc5bfa972016-02-05 15:49:10 +000045namespace debug {
Vladimir Marko10c13562015-11-25 14:33:36 +000046struct MethodDebugInfo;
David Srbeckyc5bfa972016-02-05 15:49:10 +000047} // namespace debug
Vladimir Marko10c13562015-11-25 14:33:36 +000048
Vladimir Marko944da602016-02-19 12:27:55 +000049namespace linker {
50class MultiOatRelativePatcher;
51} // namespace linker
52
Brian Carlstrom7940e442013-07-12 13:46:57 -070053// OatHeader variable length with count of D OatDexFiles
54//
55// OatDexFile[0] one variable sized OatDexFile with offsets to Dex and OatClasses
56// OatDexFile[1]
57// ...
58// OatDexFile[D]
59//
60// Dex[0] one variable sized DexFile for each OatDexFile.
61// Dex[1] these are literal copies of the input .dex files.
62// ...
63// Dex[D]
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 Marko96c6ab92014-04-08 14:00:50 +010080// GcMap one variable sized blob with GC map.
81// GcMap GC maps are deduplicated.
82// ...
83// GcMap
84//
85// VmapTable one variable sized VmapTable blob (quick compiler only).
86// VmapTable VmapTables are deduplicated.
87// ...
88// VmapTable
89//
90// MappingTable one variable sized blob with MappingTable (quick compiler only).
91// MappingTable MappingTables are deduplicated.
92// ...
93// MappingTable
94//
Brian Carlstrom7940e442013-07-12 13:46:57 -070095// padding if necessary so that the following code will be page aligned
96//
Vladimir Marko96c6ab92014-04-08 14:00:50 +010097// OatMethodHeader fixed size header for a CompiledMethod including the size of the MethodCode.
98// MethodCode one variable sized blob with the code of a CompiledMethod.
99// OatMethodHeader (OatMethodHeader, MethodCode) pairs are deduplicated.
100// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -0700101// ...
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100102// OatMethodHeader
103// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -0700104//
105class OatWriter {
106 public:
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000107 enum class CreateTypeLookupTable {
108 kCreate,
109 kDontCreate,
110 kDefault = kCreate
111 };
112
113 OatWriter(bool compiling_boot_image, TimingLogger* timings);
114
115 // To produce a valid oat file, the user must first add sources with any combination of
116 // - AddDexFileSource(),
117 // - AddZippedDexFilesSource(),
118 // - AddRawDexFileSource().
119 // Then the user must call in order
120 // - WriteAndOpenDexFiles()
121 // - PrepareLayout(),
122 // - WriteRodata(),
123 // - WriteCode(),
124 // - WriteHeader().
125
126 // Add dex file source(s) from a file, either a plain dex file or
127 // a zip file with one or more dex files.
128 bool AddDexFileSource(
129 const char* filename,
130 const char* location,
131 CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
132 // Add dex file source(s) from a zip file specified by a file handle.
133 bool AddZippedDexFilesSource(
Andreas Gampe43e10b02016-07-15 17:17:34 -0700134 File&& zip_fd,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000135 const char* location,
136 CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
137 // Add dex file source from raw memory.
138 bool AddRawDexFileSource(
139 const ArrayRef<const uint8_t>& data,
140 const char* location,
141 uint32_t location_checksum,
142 CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
143 dchecked_vector<const char*> GetSourceLocations() const;
144
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800145 // Write raw dex files to the .rodata section and open them from the oat file. The verify
146 // setting dictates whether the dex file verifier should check the dex files. This is generally
147 // the case, and should only be false for tests.
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000148 bool WriteAndOpenDexFiles(OutputStream* rodata,
149 File* file,
150 InstructionSet instruction_set,
151 const InstructionSetFeatures* instruction_set_features,
152 SafeMap<std::string, std::string>* key_value_store,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800153 bool verify,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000154 /*out*/ std::unique_ptr<MemMap>* opened_dex_files_map,
155 /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files);
156 // Prepare layout of remaining data.
157 void PrepareLayout(const CompilerDriver* compiler,
158 ImageWriter* image_writer,
Vladimir Marko944da602016-02-19 12:27:55 +0000159 const std::vector<const DexFile*>& dex_files,
160 linker::MultiOatRelativePatcher* relative_patcher);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000161 // Write the rest of .rodata section (ClassOffsets[], OatClass[], maps).
162 bool WriteRodata(OutputStream* out);
163 // Write the code to the .text section.
164 bool WriteCode(OutputStream* out);
165 // Write the oat header. This finalizes the oat file.
166 bool WriteHeader(OutputStream* out,
167 uint32_t image_file_location_oat_checksum,
168 uintptr_t image_file_location_oat_begin,
169 int32_t image_patch_delta);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700170
Vladimir Marko09d09432015-09-08 13:47:48 +0100171 // Returns whether the oat file has an associated image.
172 bool HasImage() const {
173 // Since the image is being created at the same time as the oat file,
174 // check if there's an image writer.
175 return image_writer_ != nullptr;
176 }
177
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800178 bool HasBootImage() const {
179 return compiling_boot_image_;
180 }
181
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700182 const OatHeader& GetOatHeader() const {
183 return *oat_header_;
184 }
185
186 size_t GetSize() const {
187 return size_;
188 }
189
Vladimir Marko5c42c292015-02-25 12:02:49 +0000190 size_t GetBssSize() const {
191 return bss_size_;
192 }
193
Vladimir Marko944da602016-02-19 12:27:55 +0000194 size_t GetOatDataOffset() const {
195 return oat_data_offset_;
196 }
197
Vladimir Marko49b0f452015-12-10 13:49:19 +0000198 ArrayRef<const uintptr_t> GetAbsolutePatchLocations() const {
199 return ArrayRef<const uintptr_t>(absolute_patch_locations_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100200 }
201
Brian Carlstrom7940e442013-07-12 13:46:57 -0700202 ~OatWriter();
203
David Srbecky09c2a6b2016-03-11 17:11:44 +0000204 void AddMethodDebugInfos(const std::vector<debug::MethodDebugInfo>& infos) {
205 method_info_.insert(method_info_.end(), infos.begin(), infos.end());
206 }
207
David Srbeckyc5bfa972016-02-05 15:49:10 +0000208 ArrayRef<const debug::MethodDebugInfo> GetMethodDebugInfo() const {
209 return ArrayRef<const debug::MethodDebugInfo>(method_info_);
Mark Mendellae9fd932014-02-10 16:14:35 -0800210 }
211
Vladimir Markob163bb72015-03-31 21:49:49 +0100212 const CompilerDriver* GetCompilerDriver() {
213 return compiler_driver_;
214 }
215
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700216 private:
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000217 class DexFileSource;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000218 class OatClass;
219 class OatDexFile;
220
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100221 // The function VisitDexMethods() below iterates through all the methods in all
222 // the compiled dex files in order of their definitions. The method visitor
223 // classes provide individual bits of processing for each of the passes we need to
224 // first collect the data we want to write to the oat file and then, in later passes,
225 // to actually write it.
226 class DexMethodVisitor;
227 class OatDexMethodVisitor;
228 class InitOatClassesMethodVisitor;
229 class InitCodeMethodVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100230 class InitMapMethodVisitor;
231 class InitImageMethodVisitor;
232 class WriteCodeMethodVisitor;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100233 class WriteMapMethodVisitor;
234
235 // Visit all the methods in all the compiled dex files in their definition order
236 // with a given DexMethodVisitor.
237 bool VisitDexMethods(DexMethodVisitor* visitor);
238
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000239 size_t InitOatHeader(InstructionSet instruction_set,
240 const InstructionSetFeatures* instruction_set_features,
241 uint32_t num_dex_files,
242 SafeMap<std::string, std::string>* key_value_store);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700243 size_t InitOatDexFiles(size_t offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700244 size_t InitOatClasses(size_t offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100245 size_t InitOatMaps(size_t offset);
Vladimir Marko49b0f452015-12-10 13:49:19 +0000246 size_t InitOatCode(size_t offset);
247 size_t InitOatCodeDexFiles(size_t offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000249 bool WriteClassOffsets(OutputStream* out);
250 bool WriteClasses(OutputStream* out);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100251 size_t WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset);
252 size_t WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset);
Ian Rogers3d504072014-03-01 09:16:49 -0800253 size_t WriteCodeDexFiles(OutputStream* out, const size_t file_offset, size_t relative_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700254
Vladimir Marko944da602016-02-19 12:27:55 +0000255 bool RecordOatDataOffset(OutputStream* out);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000256 bool ReadDexFileHeader(File* file, OatDexFile* oat_dex_file);
257 bool ValidateDexFileHeader(const uint8_t* raw_header, const char* location);
258 bool WriteDexFiles(OutputStream* rodata, File* file);
259 bool WriteDexFile(OutputStream* rodata, File* file, OatDexFile* oat_dex_file);
260 bool SeekToDexFile(OutputStream* rodata, File* file, OatDexFile* oat_dex_file);
261 bool WriteDexFile(OutputStream* rodata, File* file, OatDexFile* oat_dex_file, ZipEntry* dex_file);
262 bool WriteDexFile(OutputStream* rodata, File* file, OatDexFile* oat_dex_file, File* dex_file);
263 bool WriteDexFile(OutputStream* rodata, OatDexFile* oat_dex_file, const uint8_t* dex_file);
264 bool WriteOatDexFiles(OutputStream* rodata);
265 bool ExtendForTypeLookupTables(OutputStream* rodata, File* file, size_t offset);
266 bool OpenDexFiles(File* file,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800267 bool verify,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000268 /*out*/ std::unique_ptr<MemMap>* opened_dex_files_map,
269 /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files);
270 bool WriteTypeLookupTables(MemMap* opened_dex_files_map,
271 const std::vector<std::unique_ptr<const DexFile>>& opened_dex_files);
Vladimir Markof4da6752014-08-01 19:04:18 +0100272 bool WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta);
Vladimir Marko944da602016-02-19 12:27:55 +0000273 void SetMultiOatRelativePatcherAdjustment();
Vladimir Markof4da6752014-08-01 19:04:18 +0100274
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000275 enum class WriteState {
276 kAddingDexFileSources,
277 kPrepareLayout,
278 kWriteRoData,
279 kWriteText,
280 kWriteHeader,
281 kDone
282 };
283
284 WriteState write_state_;
285 TimingLogger* timings_;
286
287 std::vector<std::unique_ptr<File>> raw_dex_files_;
288 std::vector<std::unique_ptr<ZipArchive>> zip_archives_;
289 std::vector<std::unique_ptr<ZipEntry>> zipped_dex_files_;
290
291 // Using std::list<> which doesn't move elements around on push/emplace_back().
292 // We need this because we keep plain pointers to the strings' c_str().
293 std::list<std::string> zipped_dex_file_locations_;
294
David Srbeckyc5bfa972016-02-05 15:49:10 +0000295 dchecked_vector<debug::MethodDebugInfo> method_info_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800296
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000297 const CompilerDriver* compiler_driver_;
298 ImageWriter* image_writer_;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800299 const bool compiling_boot_image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700300
301 // note OatFile does not take ownership of the DexFiles
302 const std::vector<const DexFile*>* dex_files_;
303
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700304 // Size required for Oat data structures.
305 size_t size_;
306
Vladimir Marko5c42c292015-02-25 12:02:49 +0000307 // The size of the required .bss section holding the DexCache data.
308 size_t bss_size_;
309
Vladimir Marko09d09432015-09-08 13:47:48 +0100310 // Offsets of the dex cache arrays for each app dex file. For the
311 // boot image, this information is provided by the ImageWriter.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100312 SafeMap<const DexFile*, size_t> dex_cache_arrays_offsets_; // DexFiles not owned.
Vladimir Marko09d09432015-09-08 13:47:48 +0100313
Vladimir Markof4da6752014-08-01 19:04:18 +0100314 // Offset of the oat data from the start of the mmapped region of the elf file.
315 size_t oat_data_offset_;
316
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317 // data to write
Vladimir Marko49b0f452015-12-10 13:49:19 +0000318 std::unique_ptr<OatHeader> oat_header_;
319 dchecked_vector<OatDexFile> oat_dex_files_;
320 dchecked_vector<OatClass> oat_classes_;
Ian Rogers700a4022014-05-19 16:49:03 -0700321 std::unique_ptr<const std::vector<uint8_t>> jni_dlsym_lookup_;
Ian Rogers700a4022014-05-19 16:49:03 -0700322 std::unique_ptr<const std::vector<uint8_t>> quick_generic_jni_trampoline_;
323 std::unique_ptr<const std::vector<uint8_t>> quick_imt_conflict_trampoline_;
324 std::unique_ptr<const std::vector<uint8_t>> quick_resolution_trampoline_;
325 std::unique_ptr<const std::vector<uint8_t>> quick_to_interpreter_bridge_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326
327 // output stats
328 uint32_t size_dex_file_alignment_;
329 uint32_t size_executable_offset_alignment_;
330 uint32_t size_oat_header_;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700331 uint32_t size_oat_header_key_value_store_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700332 uint32_t size_dex_file_;
Ian Rogers468532e2013-08-05 10:56:33 -0700333 uint32_t size_interpreter_to_interpreter_bridge_;
334 uint32_t size_interpreter_to_compiled_code_bridge_;
335 uint32_t size_jni_dlsym_lookup_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800336 uint32_t size_quick_generic_jni_trampoline_;
Jeff Hao88474b42013-10-23 16:24:40 -0700337 uint32_t size_quick_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700338 uint32_t size_quick_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700339 uint32_t size_quick_to_interpreter_bridge_;
340 uint32_t size_trampoline_alignment_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100341 uint32_t size_method_header_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342 uint32_t size_code_;
343 uint32_t size_code_alignment_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100344 uint32_t size_relative_call_thunks_;
Vladimir Markoc74658b2015-03-31 10:26:41 +0100345 uint32_t size_misc_thunks_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346 uint32_t size_vmap_table_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700347 uint32_t size_oat_dex_file_location_size_;
348 uint32_t size_oat_dex_file_location_data_;
349 uint32_t size_oat_dex_file_location_checksum_;
350 uint32_t size_oat_dex_file_offset_;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000351 uint32_t size_oat_dex_file_class_offsets_offset_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000352 uint32_t size_oat_dex_file_lookup_table_offset_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000353 uint32_t size_oat_lookup_table_alignment_;
354 uint32_t size_oat_lookup_table_;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000355 uint32_t size_oat_class_offsets_alignment_;
356 uint32_t size_oat_class_offsets_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700357 uint32_t size_oat_class_type_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700358 uint32_t size_oat_class_status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700359 uint32_t size_oat_class_method_bitmaps_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700360 uint32_t size_oat_class_method_offsets_;
361
Vladimir Marko944da602016-02-19 12:27:55 +0000362 // The helper for processing relative patches is external so that we can patch across oat files.
363 linker::MultiOatRelativePatcher* relative_patcher_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100364
David Srbeckyf8980872015-05-22 17:04:47 +0100365 // The locations of absolute patches relative to the start of the executable section.
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000366 dchecked_vector<uintptr_t> absolute_patch_locations_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100367
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368 DISALLOW_COPY_AND_ASSIGN(OatWriter);
369};
370
371} // namespace art
372
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700373#endif // ART_COMPILER_OAT_WRITER_H_