1 /* 2 * Copyright (C) 2016 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 * Header file of the dexlayout utility. 17 * 18 * This is a tool to read dex files into an internal representation, 19 * reorganize the representation, and emit dex files with a better 20 * file layout. 21 */ 22 23 #ifndef ART_DEXLAYOUT_DEXLAYOUT_H_ 24 #define ART_DEXLAYOUT_DEXLAYOUT_H_ 25 26 #include <stdint.h> 27 #include <stdio.h> 28 29 #include "dex_file_layout.h" 30 #include "dex_ir.h" 31 #include "mem_map.h" 32 33 namespace art { 34 35 class DexFile; 36 class Instruction; 37 class ProfileCompilationInfo; 38 39 /* Supported output formats. */ 40 enum OutputFormat { 41 kOutputPlain = 0, // default 42 kOutputXml, // XML-style 43 }; 44 45 /* Command-line options. */ 46 class Options { 47 public: 48 Options() = default; 49 50 bool dump_ = false; 51 bool build_dex_ir_ = false; 52 bool checksum_only_ = false; 53 bool disassemble_ = false; 54 bool exports_only_ = false; 55 bool ignore_bad_checksum_ = false; 56 bool output_to_memmap_ = false; 57 bool show_annotations_ = false; 58 bool show_file_headers_ = false; 59 bool show_section_headers_ = false; 60 bool show_section_statistics_ = false; 61 bool verbose_ = false; 62 bool verify_output_ = false; 63 bool visualize_pattern_ = false; 64 OutputFormat output_format_ = kOutputPlain; 65 const char* output_dex_directory_ = nullptr; 66 const char* output_file_name_ = nullptr; 67 const char* profile_file_name_ = nullptr; 68 }; 69 70 class DexLayout { 71 public: 72 DexLayout(Options& options, 73 ProfileCompilationInfo* info, 74 FILE* out_file, 75 dex_ir::Header* 76 header = nullptr) options_(options)77 : options_(options), info_(info), out_file_(out_file), header_(header) { } 78 79 int ProcessFile(const char* file_name); 80 void ProcessDexFile(const char* file_name, const DexFile* dex_file, size_t dex_file_index); 81 GetHeader()82 dex_ir::Header* GetHeader() const { return header_; } SetHeader(dex_ir::Header * header)83 void SetHeader(dex_ir::Header* header) { header_ = header; } 84 GetAndReleaseMemMap()85 MemMap* GetAndReleaseMemMap() { return mem_map_.release(); } 86 GetSections()87 const DexLayoutSections& GetSections() const { 88 return dex_sections_; 89 } 90 91 private: 92 void DumpAnnotationSetItem(dex_ir::AnnotationSetItem* set_item); 93 void DumpBytecodes(uint32_t idx, const dex_ir::CodeItem* code, uint32_t code_offset); 94 void DumpCatches(const dex_ir::CodeItem* code); 95 void DumpClass(int idx, char** last_package); 96 void DumpClassAnnotations(int idx); 97 void DumpClassDef(int idx); 98 void DumpCode(uint32_t idx, const dex_ir::CodeItem* code, uint32_t code_offset); 99 void DumpEncodedAnnotation(dex_ir::EncodedAnnotation* annotation); 100 void DumpEncodedValue(const dex_ir::EncodedValue* data); 101 void DumpFileHeader(); 102 void DumpIField(uint32_t idx, uint32_t flags, int i); 103 void DumpInstruction(const dex_ir::CodeItem* code, 104 uint32_t code_offset, 105 uint32_t insn_idx, 106 uint32_t insn_width, 107 const Instruction* dec_insn); 108 void DumpInterface(const dex_ir::TypeId* type_item, int i); 109 void DumpLocalInfo(const dex_ir::CodeItem* code); 110 void DumpMethod(uint32_t idx, uint32_t flags, const dex_ir::CodeItem* code, int i); 111 void DumpPositionInfo(const dex_ir::CodeItem* code); 112 void DumpSField(uint32_t idx, uint32_t flags, int i, dex_ir::EncodedValue* init); 113 void DumpDexFile(); 114 115 std::vector<dex_ir::ClassData*> LayoutClassDefsAndClassData(const DexFile* dex_file); 116 int32_t LayoutCodeItems(const DexFile* dex_file, 117 std::vector<dex_ir::ClassData*> new_class_data_order); 118 void LayoutStringData(const DexFile* dex_file); 119 bool IsNextSectionCodeItemAligned(uint32_t offset); 120 template<class T> void FixupSection(std::map<uint32_t, std::unique_ptr<T>>& map, uint32_t diff); 121 void FixupSections(uint32_t offset, uint32_t diff); 122 123 // Creates a new layout for the dex file based on profile info. 124 // Currently reorders ClassDefs, ClassDataItems, and CodeItems. 125 void LayoutOutputFile(const DexFile* dex_file); 126 void OutputDexFile(const DexFile* dex_file); 127 128 void DumpCFG(const DexFile* dex_file, int idx); 129 void DumpCFG(const DexFile* dex_file, uint32_t dex_method_idx, const DexFile::CodeItem* code); 130 131 Options& options_; 132 ProfileCompilationInfo* info_; 133 FILE* out_file_; 134 dex_ir::Header* header_; 135 std::unique_ptr<MemMap> mem_map_; 136 DexLayoutSections dex_sections_; 137 138 DISALLOW_COPY_AND_ASSIGN(DexLayout); 139 }; 140 141 } // namespace art 142 143 #endif // ART_DEXLAYOUT_DEXLAYOUT_H_ 144