David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 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 | |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 29 | #include "dex_file_layout.h" |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 30 | #include "dex_ir.h" |
| 31 | #include "mem_map.h" |
| 32 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 33 | namespace art { |
| 34 | |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 35 | class DexFile; |
| 36 | class Instruction; |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 37 | class ProfileCompilationInfo; |
| 38 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 39 | /* Supported output formats. */ |
| 40 | enum OutputFormat { |
| 41 | kOutputPlain = 0, // default |
| 42 | kOutputXml, // XML-style |
| 43 | }; |
| 44 | |
| 45 | /* Command-line options. */ |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 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; |
David Sehr | 9335749 | 2017-03-09 08:02:44 -0800 | [diff] [blame] | 60 | bool show_section_statistics_ = false; |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 61 | bool verbose_ = false; |
Jeff Hao | 0f15300 | 2017-07-06 10:27:15 -0700 | [diff] [blame] | 62 | // TODO: Set verify_output_ back to false by default. Was set to true for debugging b/62840842. |
| 63 | bool verify_output_ = true; |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 64 | bool visualize_pattern_ = false; |
| 65 | OutputFormat output_format_ = kOutputPlain; |
| 66 | const char* output_dex_directory_ = nullptr; |
| 67 | const char* output_file_name_ = nullptr; |
| 68 | const char* profile_file_name_ = nullptr; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 71 | class DexLayout { |
| 72 | public: |
| 73 | DexLayout(Options& options, |
| 74 | ProfileCompilationInfo* info, |
| 75 | FILE* out_file, |
| 76 | dex_ir::Header* |
| 77 | header = nullptr) |
| 78 | : options_(options), info_(info), out_file_(out_file), header_(header) { } |
| 79 | |
| 80 | int ProcessFile(const char* file_name); |
| 81 | void ProcessDexFile(const char* file_name, const DexFile* dex_file, size_t dex_file_index); |
| 82 | |
| 83 | dex_ir::Header* GetHeader() const { return header_; } |
| 84 | void SetHeader(dex_ir::Header* header) { header_ = header; } |
| 85 | |
| 86 | MemMap* GetAndReleaseMemMap() { return mem_map_.release(); } |
| 87 | |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 88 | const DexLayoutSections& GetSections() const { |
| 89 | return dex_sections_; |
| 90 | } |
| 91 | |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 92 | private: |
| 93 | void DumpAnnotationSetItem(dex_ir::AnnotationSetItem* set_item); |
| 94 | void DumpBytecodes(uint32_t idx, const dex_ir::CodeItem* code, uint32_t code_offset); |
| 95 | void DumpCatches(const dex_ir::CodeItem* code); |
| 96 | void DumpClass(int idx, char** last_package); |
| 97 | void DumpClassAnnotations(int idx); |
| 98 | void DumpClassDef(int idx); |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame^] | 99 | void DumpCode(uint32_t idx, |
| 100 | const dex_ir::CodeItem* code, |
| 101 | uint32_t code_offset, |
| 102 | const char* declaring_class_descriptor, |
| 103 | const char* method_name, |
| 104 | bool is_static, |
| 105 | const dex_ir::ProtoId* proto); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 106 | void DumpEncodedAnnotation(dex_ir::EncodedAnnotation* annotation); |
| 107 | void DumpEncodedValue(const dex_ir::EncodedValue* data); |
| 108 | void DumpFileHeader(); |
| 109 | void DumpIField(uint32_t idx, uint32_t flags, int i); |
| 110 | void DumpInstruction(const dex_ir::CodeItem* code, |
| 111 | uint32_t code_offset, |
| 112 | uint32_t insn_idx, |
| 113 | uint32_t insn_width, |
| 114 | const Instruction* dec_insn); |
| 115 | void DumpInterface(const dex_ir::TypeId* type_item, int i); |
| 116 | void DumpLocalInfo(const dex_ir::CodeItem* code); |
| 117 | void DumpMethod(uint32_t idx, uint32_t flags, const dex_ir::CodeItem* code, int i); |
| 118 | void DumpPositionInfo(const dex_ir::CodeItem* code); |
| 119 | void DumpSField(uint32_t idx, uint32_t flags, int i, dex_ir::EncodedValue* init); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 120 | void DumpDexFile(); |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 121 | |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 122 | std::vector<dex_ir::ClassData*> LayoutClassDefsAndClassData(const DexFile* dex_file); |
Shubham Ajmera | 36a282b | 2017-04-03 10:04:28 -0700 | [diff] [blame] | 123 | int32_t LayoutCodeItems(const DexFile* dex_file, |
| 124 | std::vector<dex_ir::ClassData*> new_class_data_order); |
Mathieu Chartier | fa0aa09 | 2017-03-27 15:43:54 -0700 | [diff] [blame] | 125 | void LayoutStringData(const DexFile* dex_file); |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 126 | bool IsNextSectionCodeItemAligned(uint32_t offset); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 127 | template<class T> void FixupSection(std::map<uint32_t, std::unique_ptr<T>>& map, uint32_t diff); |
| 128 | void FixupSections(uint32_t offset, uint32_t diff); |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 129 | |
| 130 | // Creates a new layout for the dex file based on profile info. |
| 131 | // Currently reorders ClassDefs, ClassDataItems, and CodeItems. |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 132 | void LayoutOutputFile(const DexFile* dex_file); |
Jeff Hao | ec7f1a9 | 2017-03-13 16:24:24 -0700 | [diff] [blame] | 133 | void OutputDexFile(const DexFile* dex_file); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 134 | |
| 135 | void DumpCFG(const DexFile* dex_file, int idx); |
| 136 | void DumpCFG(const DexFile* dex_file, uint32_t dex_method_idx, const DexFile::CodeItem* code); |
| 137 | |
| 138 | Options& options_; |
| 139 | ProfileCompilationInfo* info_; |
| 140 | FILE* out_file_; |
| 141 | dex_ir::Header* header_; |
| 142 | std::unique_ptr<MemMap> mem_map_; |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 143 | DexLayoutSections dex_sections_; |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 144 | |
| 145 | DISALLOW_COPY_AND_ASSIGN(DexLayout); |
| 146 | }; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 147 | |
| 148 | } // namespace art |
| 149 | |
| 150 | #endif // ART_DEXLAYOUT_DEXLAYOUT_H_ |