1 /*
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 
17 #ifndef ART_DEX2OAT_LINKER_OAT_WRITER_H_
18 #define ART_DEX2OAT_LINKER_OAT_WRITER_H_
19 
20 #include <stdint.h>
21 #include <cstddef>
22 #include <list>
23 #include <memory>
24 #include <vector>
25 
26 #include "base/array_ref.h"
27 #include "base/dchecked_vector.h"
28 #include "base/os.h"
29 #include "base/mem_map.h"
30 #include "base/safe_map.h"
31 #include "debug/debug_info.h"
32 #include "dex/compact_dex_level.h"
33 #include "dex/method_reference.h"
34 #include "dex/string_reference.h"
35 #include "dex/type_reference.h"
36 #include "linker/relative_patcher.h"  // For RelativePatcherTargetProvider.
37 #include "mirror/class.h"
38 #include "oat.h"
39 
40 namespace art {
41 
42 class BitVector;
43 class CompiledMethod;
44 class CompilerDriver;
45 class CompilerOptions;
46 class DexContainer;
47 class OutputStream;
48 class ProfileCompilationInfo;
49 class TimingLogger;
50 class TypeLookupTable;
51 class VdexFile;
52 class ZipEntry;
53 
54 namespace debug {
55 struct MethodDebugInfo;
56 }  // namespace debug
57 
58 namespace verifier {
59 class VerifierDeps;
60 }  // namespace verifier
61 
62 namespace linker {
63 
64 class ImageWriter;
65 class MultiOatRelativePatcher;
66 
67 enum class CopyOption {
68   kNever,
69   kAlways,
70   kOnlyIfCompressed
71 };
72 
73 // OatHeader         variable length with count of D OatDexFiles
74 //
75 // TypeLookupTable[0] one descriptor to class def index hash table for each OatDexFile.
76 // TypeLookupTable[1]
77 // ...
78 // TypeLookupTable[D]
79 //
80 // ClassOffsets[0]   one table of OatClass offsets for each class def for each OatDexFile.
81 // ClassOffsets[1]
82 // ...
83 // ClassOffsets[D]
84 //
85 // OatClass[0]       one variable sized OatClass for each of C DexFile::ClassDefs
86 // OatClass[1]       contains OatClass entries with class status, offsets to code, etc.
87 // ...
88 // OatClass[C]
89 //
90 // MethodBssMapping  one variable sized MethodBssMapping for each dex file, optional.
91 // MethodBssMapping
92 // ...
93 // MethodBssMapping
94 //
95 // VmapTable         one variable sized VmapTable blob (CodeInfo or QuickeningInfo).
96 // VmapTable         VmapTables are deduplicated.
97 // ...
98 // VmapTable
99 //
100 // OatDexFile[0]     one variable sized OatDexFile with offsets to Dex and OatClasses
101 // OatDexFile[1]
102 // ...
103 // OatDexFile[D]
104 //
105 // padding           if necessary so that the following code will be page aligned
106 //
107 // OatMethodHeader   fixed size header for a CompiledMethod including the size of the MethodCode.
108 // MethodCode        one variable sized blob with the code of a CompiledMethod.
109 // OatMethodHeader   (OatMethodHeader, MethodCode) pairs are deduplicated.
110 // MethodCode
111 // ...
112 // OatMethodHeader
113 // MethodCode
114 //
115 class OatWriter {
116  public:
117   enum class CreateTypeLookupTable {
118     kCreate,
119     kDontCreate,
120     kDefault = kCreate
121   };
122 
123   OatWriter(const CompilerOptions& compiler_options,
124             TimingLogger* timings,
125             ProfileCompilationInfo* info,
126             CompactDexLevel compact_dex_level);
127 
128   // To produce a valid oat file, the user must first add sources with any combination of
129   //   - AddDexFileSource(),
130   //   - AddZippedDexFilesSource(),
131   //   - AddRawDexFileSource(),
132   //   - AddVdexDexFilesSource().
133   // Then the user must call in order
134   //   - WriteAndOpenDexFiles()
135   //   - Initialize()
136   //   - WriteVerifierDeps()
137   //   - WriteQuickeningInfo()
138   //   - WriteChecksumsAndVdexHeader()
139   //   - PrepareLayout(),
140   //   - WriteRodata(),
141   //   - WriteCode(),
142   //   - WriteDataBimgRelRo() iff GetDataBimgRelRoSize() != 0,
143   //   - WriteHeader().
144 
145   // Add dex file source(s) from a file, either a plain dex file or
146   // a zip file with one or more dex files.
147   bool AddDexFileSource(
148       const char* filename,
149       const char* location,
150       CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
151   // Add dex file source(s) from a zip file specified by a file handle.
152   bool AddZippedDexFilesSource(
153       File&& zip_fd,
154       const char* location,
155       CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
156   // Add dex file source from raw memory.
157   bool AddRawDexFileSource(
158       const ArrayRef<const uint8_t>& data,
159       const char* location,
160       uint32_t location_checksum,
161       CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
162   // Add dex file source(s) from a vdex file.
163   bool AddVdexDexFilesSource(
164       const VdexFile& vdex_file,
165       const char* location,
166       CreateTypeLookupTable create_type_lookup_table = CreateTypeLookupTable::kDefault);
167   dchecked_vector<std::string> GetSourceLocations() const;
168 
169   // Write raw dex files to the vdex file, mmap the file and open the dex files from it.
170   // Supporting data structures are written into the .rodata section of the oat file.
171   // The `verify` setting dictates whether the dex file verifier should check the dex files.
172   // This is generally the case, and should only be false for tests.
173   // If `update_input_vdex` is true, then this method won't actually write the dex files,
174   // and the compiler will just re-use the existing vdex file.
175   bool WriteAndOpenDexFiles(File* vdex_file,
176                             OutputStream* oat_rodata,
177                             SafeMap<std::string, std::string>* key_value_store,
178                             bool verify,
179                             bool update_input_vdex,
180                             CopyOption copy_dex_files,
181                             /*out*/ std::vector<MemMap>* opened_dex_files_map,
182                             /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files);
183   // Initialize the writer with the given parameters.
184   void Initialize(const CompilerDriver* compiler_driver,
185                   ImageWriter* image_writer,
186                   const std::vector<const DexFile*>& dex_files);
187   bool WriteQuickeningInfo(OutputStream* vdex_out);
188   bool WriteVerifierDeps(OutputStream* vdex_out, verifier::VerifierDeps* verifier_deps);
189   bool WriteChecksumsAndVdexHeader(OutputStream* vdex_out);
190 
191   // Prepare layout of remaining data.
192   void PrepareLayout(MultiOatRelativePatcher* relative_patcher);
193   // Write the rest of .rodata section (ClassOffsets[], OatClass[], maps).
194   bool WriteRodata(OutputStream* out);
195   // Write the code to the .text section.
196   bool WriteCode(OutputStream* out);
197   // Write the boot image relocation data to the .data.bimg.rel.ro section.
198   bool WriteDataBimgRelRo(OutputStream* out);
199   // Check the size of the written oat file.
200   bool CheckOatSize(OutputStream* out, size_t file_offset, size_t relative_offset);
201   // Write the oat header. This finalizes the oat file.
202   bool WriteHeader(OutputStream* out);
203 
204   // Returns whether the oat file has an associated image.
HasImage()205   bool HasImage() const {
206     // Since the image is being created at the same time as the oat file,
207     // check if there's an image writer.
208     return image_writer_ != nullptr;
209   }
210 
GetOatHeader()211   const OatHeader& GetOatHeader() const {
212     return *oat_header_;
213   }
214 
GetCodeSize()215   size_t GetCodeSize() const {
216     return code_size_;
217   }
218 
GetOatSize()219   size_t GetOatSize() const {
220     return oat_size_;
221   }
222 
GetDataBimgRelRoSize()223   size_t GetDataBimgRelRoSize() const {
224     return data_bimg_rel_ro_size_;
225   }
226 
GetBssSize()227   size_t GetBssSize() const {
228     return bss_size_;
229   }
230 
GetBssMethodsOffset()231   size_t GetBssMethodsOffset() const {
232     return bss_methods_offset_;
233   }
234 
GetBssRootsOffset()235   size_t GetBssRootsOffset() const {
236     return bss_roots_offset_;
237   }
238 
GetVdexSize()239   size_t GetVdexSize() const {
240     return vdex_size_;
241   }
242 
GetOatDataOffset()243   size_t GetOatDataOffset() const {
244     return oat_data_offset_;
245   }
246 
247   ~OatWriter();
248 
249   debug::DebugInfo GetDebugInfo() const;
250 
GetCompilerDriver()251   const CompilerDriver* GetCompilerDriver() const {
252     return compiler_driver_;
253   }
254 
GetCompilerOptions()255   const CompilerOptions& GetCompilerOptions() const {
256     return compiler_options_;
257   }
258 
259  private:
260   class ChecksumUpdatingOutputStream;
261   class DexFileSource;
262   class OatClassHeader;
263   class OatClass;
264   class OatDexFile;
265 
266   // The function VisitDexMethods() below iterates through all the methods in all
267   // the compiled dex files in order of their definitions. The method visitor
268   // classes provide individual bits of processing for each of the passes we need to
269   // first collect the data we want to write to the oat file and then, in later passes,
270   // to actually write it.
271   class DexMethodVisitor;
272   class OatDexMethodVisitor;
273   class InitBssLayoutMethodVisitor;
274   class InitOatClassesMethodVisitor;
275   class LayoutCodeMethodVisitor;
276   class LayoutReserveOffsetCodeMethodVisitor;
277   struct OrderedMethodData;
278   class OrderedMethodVisitor;
279   class InitCodeMethodVisitor;
280   class InitMapMethodVisitor;
281   class InitImageMethodVisitor;
282   class WriteCodeMethodVisitor;
283   class WriteMapMethodVisitor;
284   class WriteQuickeningInfoMethodVisitor;
285   class WriteQuickeningInfoOffsetsMethodVisitor;
286 
287   // Visit all the methods in all the compiled dex files in their definition order
288   // with a given DexMethodVisitor.
289   bool VisitDexMethods(DexMethodVisitor* visitor);
290 
291   // If `update_input_vdex` is true, then this method won't actually write the dex files,
292   // and the compiler will just re-use the existing vdex file.
293   bool WriteDexFiles(OutputStream* out,
294                      File* file,
295                      bool update_input_vdex,
296                      CopyOption copy_dex_files);
297   bool WriteDexFile(OutputStream* out,
298                     File* file,
299                     OatDexFile* oat_dex_file,
300                     bool update_input_vdex);
301   bool SeekToDexFile(OutputStream* out, File* file, OatDexFile* oat_dex_file);
302   bool LayoutAndWriteDexFile(OutputStream* out, OatDexFile* oat_dex_file);
303   bool WriteDexFile(OutputStream* out,
304                     File* file,
305                     OatDexFile* oat_dex_file,
306                     ZipEntry* dex_file);
307   bool WriteDexFile(OutputStream* out,
308                     File* file,
309                     OatDexFile* oat_dex_file,
310                     File* dex_file);
311   bool WriteDexFile(OutputStream* out,
312                     OatDexFile* oat_dex_file,
313                     const uint8_t* dex_file,
314                     bool update_input_vdex);
315   bool OpenDexFiles(File* file,
316                     bool verify,
317                     /*out*/ std::vector<MemMap>* opened_dex_files_map,
318                     /*out*/ std::vector<std::unique_ptr<const DexFile>>* opened_dex_files);
319 
320   size_t InitOatHeader(uint32_t num_dex_files, SafeMap<std::string, std::string>* key_value_store);
321   size_t InitClassOffsets(size_t offset);
322   size_t InitOatClasses(size_t offset);
323   size_t InitOatMaps(size_t offset);
324   size_t InitIndexBssMappings(size_t offset);
325   size_t InitOatDexFiles(size_t offset);
326   size_t InitOatCode(size_t offset);
327   size_t InitOatCodeDexFiles(size_t offset);
328   size_t InitDataBimgRelRoLayout(size_t offset);
329   void InitBssLayout(InstructionSet instruction_set);
330 
331   size_t WriteClassOffsets(OutputStream* out, size_t file_offset, size_t relative_offset);
332   size_t WriteClasses(OutputStream* out, size_t file_offset, size_t relative_offset);
333   size_t WriteMaps(OutputStream* out, size_t file_offset, size_t relative_offset);
334   size_t WriteIndexBssMappings(OutputStream* out, size_t file_offset, size_t relative_offset);
335   size_t WriteOatDexFiles(OutputStream* out, size_t file_offset, size_t relative_offset);
336   size_t WriteCode(OutputStream* out, size_t file_offset, size_t relative_offset);
337   size_t WriteCodeDexFiles(OutputStream* out, size_t file_offset, size_t relative_offset);
338   size_t WriteDataBimgRelRo(OutputStream* out, size_t file_offset, size_t relative_offset);
339 
340   bool RecordOatDataOffset(OutputStream* out);
341   bool WriteTypeLookupTables(OutputStream* oat_rodata,
342                              const std::vector<std::unique_ptr<const DexFile>>& opened_dex_files);
343   bool WriteDexLayoutSections(OutputStream* oat_rodata,
344                               const std::vector<std::unique_ptr<const DexFile>>& opened_dex_files);
345   bool WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta);
346   bool WriteUpTo16BytesAlignment(OutputStream* out, uint32_t size, uint32_t* stat);
347   void SetMultiOatRelativePatcherAdjustment();
348   void CloseSources();
349 
350   bool MayHaveCompiledMethods() const;
351 
VdexWillContainDexFiles()352   bool VdexWillContainDexFiles() const {
353     return dex_files_ != nullptr && extract_dex_files_into_vdex_;
354   }
355 
356   enum class WriteState {
357     kAddingDexFileSources,
358     kPrepareLayout,
359     kWriteRoData,
360     kWriteText,
361     kWriteDataBimgRelRo,
362     kWriteHeader,
363     kDone
364   };
365 
366   WriteState write_state_;
367   TimingLogger* timings_;
368 
369   std::vector<std::unique_ptr<File>> raw_dex_files_;
370   std::vector<std::unique_ptr<ZipArchive>> zip_archives_;
371   std::vector<std::unique_ptr<ZipEntry>> zipped_dex_files_;
372 
373   // Using std::list<> which doesn't move elements around on push/emplace_back().
374   // We need this because we keep plain pointers to the strings' c_str().
375   std::list<std::string> zipped_dex_file_locations_;
376 
377   dchecked_vector<debug::MethodDebugInfo> method_info_;
378 
379   std::vector<uint8_t> code_info_data_;
380 
381   const CompilerDriver* compiler_driver_;
382   const CompilerOptions& compiler_options_;
383   ImageWriter* image_writer_;
384   // Whether the dex files being compiled are going to be extracted to the vdex.
385   bool extract_dex_files_into_vdex_;
386 
387   // note OatFile does not take ownership of the DexFiles
388   const std::vector<const DexFile*>* dex_files_;
389 
390   // Whether this is the primary oat file.
391   bool primary_oat_file_;
392 
393   // Size required for Vdex data structures.
394   size_t vdex_size_;
395 
396   // Offset of section holding Dex files inside Vdex.
397   size_t vdex_dex_files_offset_;
398 
399   // Offset of section holding shared dex data section in the Vdex.
400   size_t vdex_dex_shared_data_offset_;
401 
402   // Offset of section holding VerifierDeps inside Vdex.
403   size_t vdex_verifier_deps_offset_;
404 
405   // Offset of section holding quickening info inside Vdex.
406   size_t vdex_quickening_info_offset_;
407 
408   // OAT checksum.
409   uint32_t oat_checksum_;
410 
411   // Size of the .text segment.
412   size_t code_size_;
413 
414   // Size required for Oat data structures.
415   size_t oat_size_;
416 
417   // The start of the required .data.bimg.rel.ro section.
418   size_t data_bimg_rel_ro_start_;
419 
420   // The size of the required .data.bimg.rel.ro section holding the boot image relocations.
421   size_t data_bimg_rel_ro_size_;
422 
423   // The start of the required .bss section.
424   size_t bss_start_;
425 
426   // The size of the required .bss section holding the DexCache data and GC roots.
427   size_t bss_size_;
428 
429   // The offset of the methods in .bss section.
430   size_t bss_methods_offset_;
431 
432   // The offset of the GC roots in .bss section.
433   size_t bss_roots_offset_;
434 
435   // Map for allocating .data.bimg.rel.ro entries. Indexed by the boot image offset of the
436   // relocation. The value is the assigned offset within the .data.bimg.rel.ro section.
437   SafeMap<uint32_t, size_t> data_bimg_rel_ro_entries_;
438 
439   // Map for recording references to ArtMethod entries in .bss.
440   SafeMap<const DexFile*, BitVector> bss_method_entry_references_;
441 
442   // Map for recording references to GcRoot<mirror::Class> entries in .bss.
443   SafeMap<const DexFile*, BitVector> bss_type_entry_references_;
444 
445   // Map for recording references to GcRoot<mirror::String> entries in .bss.
446   SafeMap<const DexFile*, BitVector> bss_string_entry_references_;
447 
448   // Map for allocating ArtMethod entries in .bss. Indexed by MethodReference for the target
449   // method in the dex file with the "method reference value comparator" for deduplication.
450   // The value is the target offset for patching, starting at `bss_start_ + bss_methods_offset_`.
451   SafeMap<MethodReference, size_t, MethodReferenceValueComparator> bss_method_entries_;
452 
453   // Map for allocating Class entries in .bss. Indexed by TypeReference for the source
454   // type in the dex file with the "type value comparator" for deduplication. The value
455   // is the target offset for patching, starting at `bss_start_ + bss_roots_offset_`.
456   SafeMap<TypeReference, size_t, TypeReferenceValueComparator> bss_type_entries_;
457 
458   // Map for allocating String entries in .bss. Indexed by StringReference for the source
459   // string in the dex file with the "string value comparator" for deduplication. The value
460   // is the target offset for patching, starting at `bss_start_ + bss_roots_offset_`.
461   SafeMap<StringReference, size_t, StringReferenceValueComparator> bss_string_entries_;
462 
463   // Offset of the oat data from the start of the mmapped region of the elf file.
464   size_t oat_data_offset_;
465 
466   // Fake OatDexFiles to hold type lookup tables for the compiler.
467   std::vector<std::unique_ptr<art::OatDexFile>> type_lookup_table_oat_dex_files_;
468 
469   // data to write
470   std::unique_ptr<OatHeader> oat_header_;
471   dchecked_vector<OatDexFile> oat_dex_files_;
472   dchecked_vector<OatClassHeader> oat_class_headers_;
473   dchecked_vector<OatClass> oat_classes_;
474   std::unique_ptr<const std::vector<uint8_t>> jni_dlsym_lookup_;
475   std::unique_ptr<const std::vector<uint8_t>> quick_generic_jni_trampoline_;
476   std::unique_ptr<const std::vector<uint8_t>> quick_imt_conflict_trampoline_;
477   std::unique_ptr<const std::vector<uint8_t>> quick_resolution_trampoline_;
478   std::unique_ptr<const std::vector<uint8_t>> quick_to_interpreter_bridge_;
479 
480   // output stats
481   uint32_t size_vdex_header_;
482   uint32_t size_vdex_checksums_;
483   uint32_t size_dex_file_alignment_;
484   uint32_t size_executable_offset_alignment_;
485   uint32_t size_oat_header_;
486   uint32_t size_oat_header_key_value_store_;
487   uint32_t size_dex_file_;
488   uint32_t size_verifier_deps_;
489   uint32_t size_verifier_deps_alignment_;
490   uint32_t size_quickening_info_;
491   uint32_t size_quickening_info_alignment_;
492   uint32_t size_interpreter_to_interpreter_bridge_;
493   uint32_t size_interpreter_to_compiled_code_bridge_;
494   uint32_t size_jni_dlsym_lookup_;
495   uint32_t size_quick_generic_jni_trampoline_;
496   uint32_t size_quick_imt_conflict_trampoline_;
497   uint32_t size_quick_resolution_trampoline_;
498   uint32_t size_quick_to_interpreter_bridge_;
499   uint32_t size_trampoline_alignment_;
500   uint32_t size_method_header_;
501   uint32_t size_code_;
502   uint32_t size_code_alignment_;
503   uint32_t size_data_bimg_rel_ro_;
504   uint32_t size_data_bimg_rel_ro_alignment_;
505   uint32_t size_relative_call_thunks_;
506   uint32_t size_misc_thunks_;
507   uint32_t size_vmap_table_;
508   uint32_t size_method_info_;
509   uint32_t size_oat_dex_file_location_size_;
510   uint32_t size_oat_dex_file_location_data_;
511   uint32_t size_oat_dex_file_location_checksum_;
512   uint32_t size_oat_dex_file_offset_;
513   uint32_t size_oat_dex_file_class_offsets_offset_;
514   uint32_t size_oat_dex_file_lookup_table_offset_;
515   uint32_t size_oat_dex_file_dex_layout_sections_offset_;
516   uint32_t size_oat_dex_file_dex_layout_sections_;
517   uint32_t size_oat_dex_file_dex_layout_sections_alignment_;
518   uint32_t size_oat_dex_file_method_bss_mapping_offset_;
519   uint32_t size_oat_dex_file_type_bss_mapping_offset_;
520   uint32_t size_oat_dex_file_string_bss_mapping_offset_;
521   uint32_t size_oat_lookup_table_alignment_;
522   uint32_t size_oat_lookup_table_;
523   uint32_t size_oat_class_offsets_alignment_;
524   uint32_t size_oat_class_offsets_;
525   uint32_t size_oat_class_type_;
526   uint32_t size_oat_class_status_;
527   uint32_t size_oat_class_method_bitmaps_;
528   uint32_t size_oat_class_method_offsets_;
529   uint32_t size_method_bss_mappings_;
530   uint32_t size_type_bss_mappings_;
531   uint32_t size_string_bss_mappings_;
532 
533   // The helper for processing relative patches is external so that we can patch across oat files.
534   MultiOatRelativePatcher* relative_patcher_;
535 
536   // Profile info used to generate new layout of files.
537   ProfileCompilationInfo* profile_compilation_info_;
538 
539   // Compact dex level that is generated.
540   CompactDexLevel compact_dex_level_;
541 
542   using OrderedMethodList = std::vector<OrderedMethodData>;
543 
544   // List of compiled methods, sorted by the order defined in OrderedMethodData.
545   // Methods can be inserted more than once in case of duplicated methods.
546   // This pointer is only non-null after InitOatCodeDexFiles succeeds.
547   std::unique_ptr<OrderedMethodList> ordered_methods_;
548 
549   // Container of shared dex data.
550   std::unique_ptr<DexContainer> dex_container_;
551 
552   DISALLOW_COPY_AND_ASSIGN(OatWriter);
553 };
554 
555 }  // namespace linker
556 }  // namespace art
557 
558 #endif  // ART_DEX2OAT_LINKER_OAT_WRITER_H_
559