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