blob: d681998774694706dd1bdc25012cbfd31dade28d [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
Vladimir Marko10c13562015-11-25 14:33:36 +000046namespace dwarf {
47struct MethodDebugInfo;
48} // namespace dwarf
49
Brian Carlstrom7940e442013-07-12 13:46:57 -070050// OatHeader variable length with count of D OatDexFiles
51//
52// OatDexFile[0] one variable sized OatDexFile with offsets to Dex and OatClasses
53// OatDexFile[1]
54// ...
55// OatDexFile[D]
56//
57// Dex[0] one variable sized DexFile for each OatDexFile.
58// Dex[1] these are literal copies of the input .dex files.
59// ...
60// Dex[D]
61//
Artem Udovichenkod9786b02015-10-14 16:36:55 +030062// TypeLookupTable[0] one descriptor to class def index hash table for each OatDexFile.
63// TypeLookupTable[1]
64// ...
65// TypeLookupTable[D]
66//
Vladimir Marko9bdf1082016-01-21 12:15:52 +000067// ClassOffsets[0] one table of OatClass offsets for each class def for each OatDexFile.
68// ClassOffsets[1]
69// ...
70// ClassOffsets[D]
71//
Brian Carlstrom7940e442013-07-12 13:46:57 -070072// OatClass[0] one variable sized OatClass for each of C DexFile::ClassDefs
73// OatClass[1] contains OatClass entries with class status, offsets to code, etc.
74// ...
75// OatClass[C]
76//
Vladimir Marko96c6ab92014-04-08 14:00:50 +010077// GcMap one variable sized blob with GC map.
78// GcMap GC maps are deduplicated.
79// ...
80// GcMap
81//
82// VmapTable one variable sized VmapTable blob (quick compiler only).
83// VmapTable VmapTables are deduplicated.
84// ...
85// VmapTable
86//
87// MappingTable one variable sized blob with MappingTable (quick compiler only).
88// MappingTable MappingTables are deduplicated.
89// ...
90// MappingTable
91//
Brian Carlstrom7940e442013-07-12 13:46:57 -070092// padding if necessary so that the following code will be page aligned
93//
Vladimir Marko96c6ab92014-04-08 14:00:50 +010094// OatMethodHeader fixed size header for a CompiledMethod including the size of the MethodCode.
95// MethodCode one variable sized blob with the code of a CompiledMethod.
96// OatMethodHeader (OatMethodHeader, MethodCode) pairs are deduplicated.
97// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -070098// ...
Vladimir Marko96c6ab92014-04-08 14:00:50 +010099// OatMethodHeader
100// MethodCode
Brian Carlstrom7940e442013-07-12 13:46:57 -0700101//
102class OatWriter {
103 public:
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000104 enum class CreateTypeLookupTable {
105 kCreate,
106 kDontCreate,
107 kDefault = kCreate
108 };
109
110 OatWriter(bool compiling_boot_image, TimingLogger* timings);
111
112 // To produce a valid oat file, the user must first add sources with any combination of
113 // - AddDexFileSource(),
114 // - AddZippedDexFilesSource(),
115 // - AddRawDexFileSource().
116 // Then the user must call in order
117 // - WriteAndOpenDexFiles()
118 // - PrepareLayout(),
119 // - WriteRodata(),
120 // - WriteCode(),
121 // - WriteHeader().
122
123 // Add dex file source(s) from a file, either a plain dex file or
124 // a zip file with one or more dex files.
125 bool AddDexFileSource(
126 const char* filename,
127 const char* location,
128 CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
129 // Add dex file source(s) from a zip file specified by a file handle.
130 bool AddZippedDexFilesSource(
131 ScopedFd&& zip_fd,
132 const char* location,
133 CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
134 // Add dex file source from raw memory.
135 bool AddRawDexFileSource(
136 const ArrayRef<const uint8_t>& data,
137 const char* location,
138 uint32_t location_checksum,
139 CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
140 dchecked_vector<const char*> GetSourceLocations() const;
141
142 // Write raw dex files to the .rodata section and open them from the oat file.
143 bool WriteAndOpenDexFiles(OutputStream* rodata,
144 File* file,
145 InstructionSet instruction_set,
146 const InstructionSetFeatures* instruction_set_features,
147 SafeMap<std::string, std::string>* key_value_store,
148 /*out*/ std::unique_ptr<MemMap>* opened_dex_files_map,
149 /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files);
150 // Prepare layout of remaining data.
151 void PrepareLayout(const CompilerDriver* compiler,
152 ImageWriter* image_writer,
153 const std::vector<const DexFile*>& dex_files);
154 // Write the rest of .rodata section (ClassOffsets[], OatClass[], maps).
155 bool WriteRodata(OutputStream* out);
156 // Write the code to the .text section.
157 bool WriteCode(OutputStream* out);
158 // Write the oat header. This finalizes the oat file.
159 bool WriteHeader(OutputStream* out,
160 uint32_t image_file_location_oat_checksum,
161 uintptr_t image_file_location_oat_begin,
162 int32_t image_patch_delta);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700163
Vladimir Marko09d09432015-09-08 13:47:48 +0100164 // Returns whether the oat file has an associated image.
165 bool HasImage() const {
166 // Since the image is being created at the same time as the oat file,
167 // check if there's an image writer.
168 return image_writer_ != nullptr;
169 }
170
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800171 bool HasBootImage() const {
172 return compiling_boot_image_;
173 }
174
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700175 const OatHeader& GetOatHeader() const {
176 return *oat_header_;
177 }
178
179 size_t GetSize() const {
180 return size_;
181 }
182
Vladimir Marko5c42c292015-02-25 12:02:49 +0000183 size_t GetBssSize() const {
184 return bss_size_;
185 }
186
Vladimir Marko49b0f452015-12-10 13:49:19 +0000187 ArrayRef<const uintptr_t> GetAbsolutePatchLocations() const {
188 return ArrayRef<const uintptr_t>(absolute_patch_locations_);
Vladimir Markof4da6752014-08-01 19:04:18 +0100189 }
190
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191 ~OatWriter();
192
Vladimir Marko49b0f452015-12-10 13:49:19 +0000193 ArrayRef<const dwarf::MethodDebugInfo> GetMethodDebugInfo() const {
194 return ArrayRef<const dwarf::MethodDebugInfo>(method_info_);
Mark Mendellae9fd932014-02-10 16:14:35 -0800195 }
196
Vladimir Markob163bb72015-03-31 21:49:49 +0100197 const CompilerDriver* GetCompilerDriver() {
198 return compiler_driver_;
199 }
200
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700201 private:
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000202 class DexFileSource;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000203 class OatClass;
204 class OatDexFile;
205
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100206 // The DataAccess classes are helper classes that provide access to members related to
207 // a given map, i.e. GC map, mapping table or vmap table. By abstracting these away
208 // we can share a lot of code for processing the maps with template classes below.
209 struct GcMapDataAccess;
210 struct MappingTableDataAccess;
211 struct VmapTableDataAccess;
212
213 // The function VisitDexMethods() below iterates through all the methods in all
214 // the compiled dex files in order of their definitions. The method visitor
215 // classes provide individual bits of processing for each of the passes we need to
216 // first collect the data we want to write to the oat file and then, in later passes,
217 // to actually write it.
218 class DexMethodVisitor;
219 class OatDexMethodVisitor;
220 class InitOatClassesMethodVisitor;
221 class InitCodeMethodVisitor;
222 template <typename DataAccess>
223 class InitMapMethodVisitor;
224 class InitImageMethodVisitor;
225 class WriteCodeMethodVisitor;
226 template <typename DataAccess>
227 class WriteMapMethodVisitor;
228
229 // Visit all the methods in all the compiled dex files in their definition order
230 // with a given DexMethodVisitor.
231 bool VisitDexMethods(DexMethodVisitor* visitor);
232
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000233 size_t InitOatHeader(InstructionSet instruction_set,
234 const InstructionSetFeatures* instruction_set_features,
235 uint32_t num_dex_files,
236 SafeMap<std::string, std::string>* key_value_store);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700237 size_t InitOatDexFiles(size_t offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238 size_t InitOatClasses(size_t offset);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100239 size_t InitOatMaps(size_t offset);
Vladimir Marko49b0f452015-12-10 13:49:19 +0000240 size_t InitOatCode(size_t offset);
241 size_t InitOatCodeDexFiles(size_t offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000243 bool WriteClassOffsets(OutputStream* out);
244 bool WriteClasses(OutputStream* out);
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100245 size_t WriteMaps(OutputStream* out, const size_t file_offset, size_t relative_offset);
246 size_t WriteCode(OutputStream* out, const size_t file_offset, size_t relative_offset);
Ian Rogers3d504072014-03-01 09:16:49 -0800247 size_t WriteCodeDexFiles(OutputStream* out, const size_t file_offset, size_t relative_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248
Vladimir Marko49b0f452015-12-10 13:49:19 +0000249 bool GetOatDataOffset(OutputStream* out);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000250 bool ReadDexFileHeader(File* file, OatDexFile* oat_dex_file);
251 bool ValidateDexFileHeader(const uint8_t* raw_header, const char* location);
252 bool WriteDexFiles(OutputStream* rodata, File* file);
253 bool WriteDexFile(OutputStream* rodata, File* file, OatDexFile* oat_dex_file);
254 bool SeekToDexFile(OutputStream* rodata, File* file, OatDexFile* oat_dex_file);
255 bool WriteDexFile(OutputStream* rodata, File* file, OatDexFile* oat_dex_file, ZipEntry* dex_file);
256 bool WriteDexFile(OutputStream* rodata, File* file, OatDexFile* oat_dex_file, File* dex_file);
257 bool WriteDexFile(OutputStream* rodata, OatDexFile* oat_dex_file, const uint8_t* dex_file);
258 bool WriteOatDexFiles(OutputStream* rodata);
259 bool ExtendForTypeLookupTables(OutputStream* rodata, File* file, size_t offset);
260 bool OpenDexFiles(File* file,
261 /*out*/ std::unique_ptr<MemMap>* opened_dex_files_map,
262 /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files);
263 bool WriteTypeLookupTables(MemMap* opened_dex_files_map,
264 const std::vector<std::unique_ptr<const DexFile>>& opened_dex_files);
Vladimir Markof4da6752014-08-01 19:04:18 +0100265 bool WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta);
Vladimir Marko49b0f452015-12-10 13:49:19 +0000266 bool WriteData(OutputStream* out, const void* data, size_t size);
Vladimir Markof4da6752014-08-01 19:04:18 +0100267
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000268 enum class WriteState {
269 kAddingDexFileSources,
270 kPrepareLayout,
271 kWriteRoData,
272 kWriteText,
273 kWriteHeader,
274 kDone
275 };
276
277 WriteState write_state_;
278 TimingLogger* timings_;
279
280 std::vector<std::unique_ptr<File>> raw_dex_files_;
281 std::vector<std::unique_ptr<ZipArchive>> zip_archives_;
282 std::vector<std::unique_ptr<ZipEntry>> zipped_dex_files_;
283
284 // Using std::list<> which doesn't move elements around on push/emplace_back().
285 // We need this because we keep plain pointers to the strings' c_str().
286 std::list<std::string> zipped_dex_file_locations_;
287
Vladimir Marko49b0f452015-12-10 13:49:19 +0000288 dchecked_vector<dwarf::MethodDebugInfo> method_info_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800289
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000290 const CompilerDriver* compiler_driver_;
291 ImageWriter* image_writer_;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -0800292 const bool compiling_boot_image_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293
294 // note OatFile does not take ownership of the DexFiles
295 const std::vector<const DexFile*>* dex_files_;
296
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700297 // Size required for Oat data structures.
298 size_t size_;
299
Vladimir Marko5c42c292015-02-25 12:02:49 +0000300 // The size of the required .bss section holding the DexCache data.
301 size_t bss_size_;
302
Vladimir Marko09d09432015-09-08 13:47:48 +0100303 // Offsets of the dex cache arrays for each app dex file. For the
304 // boot image, this information is provided by the ImageWriter.
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100305 SafeMap<const DexFile*, size_t> dex_cache_arrays_offsets_; // DexFiles not owned.
Vladimir Marko09d09432015-09-08 13:47:48 +0100306
Vladimir Markof4da6752014-08-01 19:04:18 +0100307 // Offset of the oat data from the start of the mmapped region of the elf file.
308 size_t oat_data_offset_;
309
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310 // data to write
Vladimir Marko49b0f452015-12-10 13:49:19 +0000311 std::unique_ptr<OatHeader> oat_header_;
312 dchecked_vector<OatDexFile> oat_dex_files_;
313 dchecked_vector<OatClass> oat_classes_;
Ian Rogers700a4022014-05-19 16:49:03 -0700314 std::unique_ptr<const std::vector<uint8_t>> jni_dlsym_lookup_;
Ian Rogers700a4022014-05-19 16:49:03 -0700315 std::unique_ptr<const std::vector<uint8_t>> quick_generic_jni_trampoline_;
316 std::unique_ptr<const std::vector<uint8_t>> quick_imt_conflict_trampoline_;
317 std::unique_ptr<const std::vector<uint8_t>> quick_resolution_trampoline_;
318 std::unique_ptr<const std::vector<uint8_t>> quick_to_interpreter_bridge_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700319
320 // output stats
321 uint32_t size_dex_file_alignment_;
322 uint32_t size_executable_offset_alignment_;
323 uint32_t size_oat_header_;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700324 uint32_t size_oat_header_key_value_store_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700325 uint32_t size_dex_file_;
Ian Rogers468532e2013-08-05 10:56:33 -0700326 uint32_t size_interpreter_to_interpreter_bridge_;
327 uint32_t size_interpreter_to_compiled_code_bridge_;
328 uint32_t size_jni_dlsym_lookup_;
Andreas Gampe2da88232014-02-27 12:26:20 -0800329 uint32_t size_quick_generic_jni_trampoline_;
Jeff Hao88474b42013-10-23 16:24:40 -0700330 uint32_t size_quick_imt_conflict_trampoline_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700331 uint32_t size_quick_resolution_trampoline_;
Ian Rogers468532e2013-08-05 10:56:33 -0700332 uint32_t size_quick_to_interpreter_bridge_;
333 uint32_t size_trampoline_alignment_;
Vladimir Marko96c6ab92014-04-08 14:00:50 +0100334 uint32_t size_method_header_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700335 uint32_t size_code_;
336 uint32_t size_code_alignment_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100337 uint32_t size_relative_call_thunks_;
Vladimir Markoc74658b2015-03-31 10:26:41 +0100338 uint32_t size_misc_thunks_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 uint32_t size_mapping_table_;
340 uint32_t size_vmap_table_;
341 uint32_t size_gc_map_;
342 uint32_t size_oat_dex_file_location_size_;
343 uint32_t size_oat_dex_file_location_data_;
344 uint32_t size_oat_dex_file_location_checksum_;
345 uint32_t size_oat_dex_file_offset_;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000346 uint32_t size_oat_dex_file_class_offsets_offset_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000347 uint32_t size_oat_dex_file_lookup_table_offset_;
Vladimir Marko49b0f452015-12-10 13:49:19 +0000348 uint32_t size_oat_lookup_table_alignment_;
349 uint32_t size_oat_lookup_table_;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000350 uint32_t size_oat_class_offsets_alignment_;
351 uint32_t size_oat_class_offsets_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700352 uint32_t size_oat_class_type_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353 uint32_t size_oat_class_status_;
Brian Carlstromba150c32013-08-27 17:31:03 -0700354 uint32_t size_oat_class_method_bitmaps_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700355 uint32_t size_oat_class_method_offsets_;
356
Vladimir Markob163bb72015-03-31 21:49:49 +0100357 std::unique_ptr<linker::RelativePatcher> relative_patcher_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100358
David Srbeckyf8980872015-05-22 17:04:47 +0100359 // The locations of absolute patches relative to the start of the executable section.
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000360 dchecked_vector<uintptr_t> absolute_patch_locations_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100361
Vladimir Markob163bb72015-03-31 21:49:49 +0100362 // Map method reference to assigned offset.
363 // Wrap the map in a class implementing linker::RelativePatcherTargetProvider.
364 class MethodOffsetMap FINAL : public linker::RelativePatcherTargetProvider {
365 public:
366 std::pair<bool, uint32_t> FindMethodOffset(MethodReference ref) OVERRIDE;
367 SafeMap<MethodReference, uint32_t, MethodReferenceComparator> map;
368 };
369 MethodOffsetMap method_offset_map_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100370
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 DISALLOW_COPY_AND_ASSIGN(OatWriter);
372};
373
374} // namespace art
375
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700376#endif // ART_COMPILER_OAT_WRITER_H_