blob: 9bb7b8b2e878cfefff1657f9b32746bb8e98a946 [file] [log] [blame]
David Sehr7629f602016-08-07 16:01:51 -07001/*
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>
Vladimir Marko6c702242019-02-07 16:17:33 +000028
29#include <set>
Mathieu Chartier3e0c5172017-11-12 12:58:40 -080030#include <unordered_map>
David Sehr7629f602016-08-07 16:01:51 -070031
David Sehr9e734c72018-01-04 17:56:19 -080032#include "dex/compact_dex_level.h"
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -080033#include "dex_container.h"
David Sehr9e734c72018-01-04 17:56:19 -080034#include "dex/dex_file_layout.h"
Jeff Haoea7c6292016-11-14 18:10:16 -080035#include "dex_ir.h"
Jeff Haoea7c6292016-11-14 18:10:16 -080036
David Sehr7629f602016-08-07 16:01:51 -070037namespace art {
38
Jeff Haoea7c6292016-11-14 18:10:16 -080039class DexFile;
40class Instruction;
David Sehrcdcfde72016-09-26 07:44:04 -070041class ProfileCompilationInfo;
42
David Sehr7629f602016-08-07 16:01:51 -070043/* Supported output formats. */
44enum OutputFormat {
45 kOutputPlain = 0, // default
46 kOutputXml, // XML-style
47};
48
49/* Command-line options. */
Jeff Haoea7c6292016-11-14 18:10:16 -080050class Options {
51 public:
52 Options() = default;
53
54 bool dump_ = false;
55 bool build_dex_ir_ = false;
56 bool checksum_only_ = false;
57 bool disassemble_ = false;
58 bool exports_only_ = false;
59 bool ignore_bad_checksum_ = false;
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -080060 bool output_to_container_ = false;
Jeff Haoea7c6292016-11-14 18:10:16 -080061 bool show_annotations_ = false;
62 bool show_file_headers_ = false;
63 bool show_section_headers_ = false;
David Sehr93357492017-03-09 08:02:44 -080064 bool show_section_statistics_ = false;
Jeff Haoea7c6292016-11-14 18:10:16 -080065 bool verbose_ = false;
Mathieu Chartier2ef3b882017-10-20 19:50:39 -070066 bool verify_output_ = kIsDebugBuild;
Jeff Haoea7c6292016-11-14 18:10:16 -080067 bool visualize_pattern_ = false;
Mathieu Chartier2c4b0842017-12-13 11:49:51 -080068 bool update_checksum_ = false;
Mathieu Chartier603ccab2017-10-20 14:34:28 -070069 CompactDexLevel compact_dex_level_ = CompactDexLevel::kCompactDexLevelNone;
Jeff Haoea7c6292016-11-14 18:10:16 -080070 OutputFormat output_format_ = kOutputPlain;
71 const char* output_dex_directory_ = nullptr;
72 const char* output_file_name_ = nullptr;
73 const char* profile_file_name_ = nullptr;
Mathieu Chartier807b21b2018-01-29 05:18:24 -080074 // Filter that removes classes that don't have a matching descriptor (during IR creation).
75 // This speeds up cases when the output only requires a single class.
Mathieu Chartier75175552018-01-25 11:23:01 -080076 std::set<std::string> class_filter_;
David Sehr7629f602016-08-07 16:01:51 -070077};
78
Mathieu Chartier3e0c5172017-11-12 12:58:40 -080079// Hotness info
80class DexLayoutHotnessInfo {
81 public:
82 // Store layout information so that the offset calculation can specify the section sizes.
83 std::unordered_map<dex_ir::CodeItem*, LayoutType> code_item_layout_;
84};
85
Jeff Haoea7c6292016-11-14 18:10:16 -080086class DexLayout {
87 public:
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -080088 class VectorOutputContainer {
89 public:
90 // Begin is not necessarily aligned (for now).
91 uint8_t* Begin() {
92 return &data_[0];
93 }
94
95 private:
96 std::vector<uint8_t> data_;
97 };
98
99
Mathieu Chartier2c4b0842017-12-13 11:49:51 -0800100 // Setting this to false disables class def layout entirely, which is stronger than strictly
101 // necessary to ensure the partial order w.r.t. class derivation. TODO: Re-enable (b/68317550).
102 static constexpr bool kChangeClassDefOrder = false;
103
Jeff Haoea7c6292016-11-14 18:10:16 -0800104 DexLayout(Options& options,
105 ProfileCompilationInfo* info,
106 FILE* out_file,
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800107 dex_ir::Header* header)
108 : options_(options),
109 info_(info),
110 out_file_(out_file),
111 header_(header) { }
Jeff Haoea7c6292016-11-14 18:10:16 -0800112
113 int ProcessFile(const char* file_name);
Mathieu Chartier05f90d12018-02-07 13:47:17 -0800114 bool ProcessDexFile(const char* file_name,
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800115 const DexFile* dex_file,
116 size_t dex_file_index,
Mathieu Chartier05f90d12018-02-07 13:47:17 -0800117 std::unique_ptr<DexContainer>* dex_container,
118 std::string* error_msg);
Jeff Haoea7c6292016-11-14 18:10:16 -0800119
120 dex_ir::Header* GetHeader() const { return header_; }
121 void SetHeader(dex_ir::Header* header) { header_ = header; }
122
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800123 DexLayoutSections& GetSections() {
Mathieu Chartier120aa282017-08-05 16:03:03 -0700124 return dex_sections_;
125 }
126
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800127 const DexLayoutHotnessInfo& LayoutHotnessInfo() const {
128 return layout_hotness_info_;
129 }
130
Mathieu Chartier2c4b0842017-12-13 11:49:51 -0800131 const Options& GetOptions() const {
132 return options_;
133 }
134
Jeff Haoea7c6292016-11-14 18:10:16 -0800135 private:
136 void DumpAnnotationSetItem(dex_ir::AnnotationSetItem* set_item);
137 void DumpBytecodes(uint32_t idx, const dex_ir::CodeItem* code, uint32_t code_offset);
138 void DumpCatches(const dex_ir::CodeItem* code);
139 void DumpClass(int idx, char** last_package);
140 void DumpClassAnnotations(int idx);
141 void DumpClassDef(int idx);
David Sehraa6abb02017-10-12 08:25:11 -0700142 void DumpCode(uint32_t idx,
143 const dex_ir::CodeItem* code,
144 uint32_t code_offset,
145 const char* declaring_class_descriptor,
146 const char* method_name,
147 bool is_static,
148 const dex_ir::ProtoId* proto);
Jeff Haoea7c6292016-11-14 18:10:16 -0800149 void DumpEncodedAnnotation(dex_ir::EncodedAnnotation* annotation);
150 void DumpEncodedValue(const dex_ir::EncodedValue* data);
151 void DumpFileHeader();
David Brazdil20c765f2018-10-27 21:45:15 +0000152 void DumpIField(uint32_t idx, uint32_t flags, uint32_t hiddenapi_flags, int i);
Jeff Haoea7c6292016-11-14 18:10:16 -0800153 void DumpInstruction(const dex_ir::CodeItem* code,
154 uint32_t code_offset,
155 uint32_t insn_idx,
156 uint32_t insn_width,
157 const Instruction* dec_insn);
158 void DumpInterface(const dex_ir::TypeId* type_item, int i);
159 void DumpLocalInfo(const dex_ir::CodeItem* code);
David Brazdil20c765f2018-10-27 21:45:15 +0000160 void DumpMethod(uint32_t idx,
161 uint32_t flags,
162 uint32_t hiddenapi_flags,
163 const dex_ir::CodeItem* code,
164 int i);
Jeff Haoea7c6292016-11-14 18:10:16 -0800165 void DumpPositionInfo(const dex_ir::CodeItem* code);
David Brazdil20c765f2018-10-27 21:45:15 +0000166 void DumpSField(uint32_t idx,
167 uint32_t flags,
168 uint32_t hiddenapi_flags,
169 int i,
170 dex_ir::EncodedValue* init);
Jeff Haoea7c6292016-11-14 18:10:16 -0800171 void DumpDexFile();
Jeff Hao042e8982016-10-19 11:17:11 -0700172
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800173 void LayoutClassDefsAndClassData(const DexFile* dex_file);
174 void LayoutCodeItems(const DexFile* dex_file);
175 void LayoutStringData(const DexFile* dex_file);
Jeff Hao042e8982016-10-19 11:17:11 -0700176
177 // Creates a new layout for the dex file based on profile info.
178 // Currently reorders ClassDefs, ClassDataItems, and CodeItems.
Jeff Haoea7c6292016-11-14 18:10:16 -0800179 void LayoutOutputFile(const DexFile* dex_file);
Mathieu Chartier05f90d12018-02-07 13:47:17 -0800180 bool OutputDexFile(const DexFile* input_dex_file,
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800181 bool compute_offsets,
Mathieu Chartier05f90d12018-02-07 13:47:17 -0800182 std::unique_ptr<DexContainer>* dex_container,
183 std::string* error_msg);
Jeff Haoea7c6292016-11-14 18:10:16 -0800184
185 void DumpCFG(const DexFile* dex_file, int idx);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800186 void DumpCFG(const DexFile* dex_file, uint32_t dex_method_idx, const dex::CodeItem* code);
Jeff Haoea7c6292016-11-14 18:10:16 -0800187
188 Options& options_;
189 ProfileCompilationInfo* info_;
190 FILE* out_file_;
191 dex_ir::Header* header_;
Mathieu Chartier120aa282017-08-05 16:03:03 -0700192 DexLayoutSections dex_sections_;
Mathieu Chartier3e0c5172017-11-12 12:58:40 -0800193 // Layout hotness information is only calculated when dexlayout is enabled.
194 DexLayoutHotnessInfo layout_hotness_info_;
Jeff Haoea7c6292016-11-14 18:10:16 -0800195
196 DISALLOW_COPY_AND_ASSIGN(DexLayout);
197};
David Sehr7629f602016-08-07 16:01:51 -0700198
199} // namespace art
200
201#endif // ART_DEXLAYOUT_DEXLAYOUT_H_