Jeff Hao | ec7f1a9 | 2017-03-13 16:24:24 -0700 | [diff] [blame] | 1 | /* |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 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 | * Main driver 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 | #include "dexlayout.h" |
| 24 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 25 | #include <fcntl.h> |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <string.h> |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 28 | #include <sys/stat.h> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <unistd.h> |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 31 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 32 | #include <android-base/logging.h> |
| 33 | |
| 34 | #include "base/logging.h" // For InitLogging. |
David Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 35 | #include "base/mem_map.h" |
David Sehr | 82d046e | 2018-04-23 08:14:19 -0700 | [diff] [blame] | 36 | #include "profile/profile_compilation_info.h" |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 37 | |
| 38 | namespace art { |
| 39 | |
| 40 | static const char* kProgramName = "dexlayout"; |
| 41 | |
| 42 | /* |
| 43 | * Shows usage. |
| 44 | */ |
| 45 | static void Usage(void) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 46 | LOG(ERROR) << "Copyright (C) 2016 The Android Open Source Project\n"; |
| 47 | LOG(ERROR) << kProgramName |
| 48 | << ": [-a] [-c] [-d] [-e] [-f] [-h] [-i] [-l layout] [-o outfile] [-p profile]" |
| 49 | " [-s] [-t] [-v] [-w directory] dexfile...\n"; |
| 50 | LOG(ERROR) << " -a : display annotations"; |
| 51 | LOG(ERROR) << " -b : build dex_ir"; |
| 52 | LOG(ERROR) << " -c : verify checksum and exit"; |
| 53 | LOG(ERROR) << " -d : disassemble code sections"; |
| 54 | LOG(ERROR) << " -e : display exported items only"; |
| 55 | LOG(ERROR) << " -f : display summary information from file header"; |
| 56 | LOG(ERROR) << " -h : display file header details"; |
| 57 | LOG(ERROR) << " -i : ignore checksum failures"; |
| 58 | LOG(ERROR) << " -l : output layout, either 'plain' or 'xml'"; |
| 59 | LOG(ERROR) << " -o : output file name (defaults to stdout)"; |
| 60 | LOG(ERROR) << " -p : profile file name (defaults to no profile)"; |
| 61 | LOG(ERROR) << " -s : visualize reference pattern"; |
| 62 | LOG(ERROR) << " -t : display file section sizes"; |
Mathieu Chartier | 36f7df5 | 2018-08-28 17:59:53 -0700 | [diff] [blame] | 63 | LOG(ERROR) << " -u : update dex checksums"; |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 64 | LOG(ERROR) << " -v : verify output file is canonical to input (IR level comparison)"; |
| 65 | LOG(ERROR) << " -w : output dex directory"; |
| 66 | LOG(ERROR) << " -x : compact dex generation level, either 'none' or 'fast'"; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 67 | } |
| 68 | |
David Sehr | 671af6c | 2018-05-17 11:00:35 -0700 | [diff] [blame] | 69 | NO_RETURN static void Abort(const char* msg) { |
| 70 | LOG(ERROR) << msg; |
| 71 | exit(1); |
| 72 | } |
| 73 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 74 | /* |
| 75 | * Main driver of the dexlayout utility. |
| 76 | */ |
| 77 | int DexlayoutDriver(int argc, char** argv) { |
| 78 | // Art specific set up. |
David Sehr | 671af6c | 2018-05-17 11:00:35 -0700 | [diff] [blame] | 79 | InitLogging(argv, Abort); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 80 | MemMap::Init(); |
| 81 | |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 82 | Options options; |
| 83 | options.dump_ = true; |
| 84 | options.verbose_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 85 | bool want_usage = false; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 86 | |
| 87 | // Parse all arguments. |
| 88 | while (1) { |
Mathieu Chartier | 36f7df5 | 2018-08-28 17:59:53 -0700 | [diff] [blame] | 89 | const int ic = getopt(argc, argv, "abcdefghil:o:p:stuvw:x:"); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 90 | if (ic < 0) { |
| 91 | break; // done |
| 92 | } |
| 93 | switch (ic) { |
| 94 | case 'a': // display annotations |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 95 | options.show_annotations_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 96 | break; |
| 97 | case 'b': // build dex_ir |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 98 | options.build_dex_ir_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 99 | break; |
| 100 | case 'c': // verify the checksum then exit |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 101 | options.checksum_only_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 102 | break; |
| 103 | case 'd': // disassemble Dalvik instructions |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 104 | options.disassemble_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 105 | break; |
| 106 | case 'e': // exported items only |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 107 | options.exports_only_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 108 | break; |
| 109 | case 'f': // display outer file header |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 110 | options.show_file_headers_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 111 | break; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 112 | case 'h': // display section headers, i.e. all meta-data |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 113 | options.show_section_headers_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 114 | break; |
| 115 | case 'i': // continue even if checksum is bad |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 116 | options.ignore_bad_checksum_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 117 | break; |
| 118 | case 'l': // layout |
| 119 | if (strcmp(optarg, "plain") == 0) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 120 | options.output_format_ = kOutputPlain; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 121 | } else if (strcmp(optarg, "xml") == 0) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 122 | options.output_format_ = kOutputXml; |
| 123 | options.verbose_ = false; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 124 | } else { |
| 125 | want_usage = true; |
| 126 | } |
| 127 | break; |
| 128 | case 'o': // output file |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 129 | options.output_file_name_ = optarg; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 130 | break; |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 131 | case 'p': // profile file |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 132 | options.profile_file_name_ = optarg; |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 133 | break; |
| 134 | case 's': // visualize access pattern |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 135 | options.visualize_pattern_ = true; |
| 136 | options.verbose_ = false; |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 137 | break; |
David Sehr | 9335749 | 2017-03-09 08:02:44 -0800 | [diff] [blame] | 138 | case 't': // display section statistics |
| 139 | options.show_section_statistics_ = true; |
| 140 | options.verbose_ = false; |
| 141 | break; |
Mathieu Chartier | 36f7df5 | 2018-08-28 17:59:53 -0700 | [diff] [blame] | 142 | case 'u': // update checksum |
| 143 | options.update_checksum_ = true; |
| 144 | break; |
Jeff Hao | ec7f1a9 | 2017-03-13 16:24:24 -0700 | [diff] [blame] | 145 | case 'v': // verify output |
| 146 | options.verify_output_ = true; |
| 147 | break; |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 148 | case 'w': // output dex files directory |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 149 | options.output_dex_directory_ = optarg; |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 150 | break; |
Mathieu Chartier | 5c36220 | 2018-01-17 14:52:55 -0800 | [diff] [blame] | 151 | case 'x': // compact dex level |
| 152 | if (strcmp(optarg, "none") == 0) { |
| 153 | options.compact_dex_level_ = CompactDexLevel::kCompactDexLevelNone; |
| 154 | } else if (strcmp(optarg, "fast") == 0) { |
| 155 | options.compact_dex_level_ = CompactDexLevel::kCompactDexLevelFast; |
| 156 | } else { |
| 157 | want_usage = true; |
| 158 | } |
| 159 | break; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 160 | default: |
| 161 | want_usage = true; |
| 162 | break; |
| 163 | } // switch |
| 164 | } // while |
| 165 | |
| 166 | // Detect early problems. |
| 167 | if (optind == argc) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 168 | LOG(ERROR) << "no file specified"; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 169 | want_usage = true; |
| 170 | } |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 171 | if (options.checksum_only_ && options.ignore_bad_checksum_) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 172 | LOG(ERROR) << "Can't specify both -c and -i"; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 173 | want_usage = true; |
| 174 | } |
| 175 | if (want_usage) { |
| 176 | Usage(); |
| 177 | return 2; |
| 178 | } |
| 179 | |
| 180 | // Open alternative output file. |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 181 | FILE* out_file = stdout; |
| 182 | if (options.output_file_name_) { |
| 183 | out_file = fopen(options.output_file_name_, "w"); |
| 184 | if (!out_file) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 185 | PLOG(ERROR) << "Can't open " << options.output_file_name_; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 186 | return 1; |
| 187 | } |
| 188 | } |
| 189 | |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 190 | // Open profile file. |
Andreas Gampe | 08ae77f | 2017-04-26 22:02:33 -0700 | [diff] [blame] | 191 | std::unique_ptr<ProfileCompilationInfo> profile_info; |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 192 | if (options.profile_file_name_) { |
| 193 | int profile_fd = open(options.profile_file_name_, O_RDONLY); |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 194 | if (profile_fd < 0) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 195 | PLOG(ERROR) << "Can't open " << options.profile_file_name_; |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 196 | return 1; |
| 197 | } |
Andreas Gampe | 08ae77f | 2017-04-26 22:02:33 -0700 | [diff] [blame] | 198 | profile_info.reset(new ProfileCompilationInfo()); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 199 | if (!profile_info->Load(profile_fd)) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 200 | LOG(ERROR) << "Can't read profile info from " << options.profile_file_name_; |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 201 | return 1; |
| 202 | } |
| 203 | } |
| 204 | |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 205 | // Create DexLayout instance. |
Mathieu Chartier | e6b6ff8 | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 206 | DexLayout dex_layout(options, profile_info.get(), out_file, /*header*/ nullptr); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 207 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 208 | // Process all files supplied on command line. |
| 209 | int result = 0; |
| 210 | while (optind < argc) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 211 | result |= dex_layout.ProcessFile(argv[optind++]); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 212 | } // while |
Andreas Gampe | 08ae77f | 2017-04-26 22:02:33 -0700 | [diff] [blame] | 213 | |
| 214 | if (options.output_file_name_) { |
| 215 | CHECK(out_file != nullptr && out_file != stdout); |
| 216 | fclose(out_file); |
| 217 | } |
| 218 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 219 | return result != 0; |
| 220 | } |
| 221 | |
| 222 | } // namespace art |
| 223 | |
| 224 | int main(int argc, char** argv) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 225 | // Output all logging to stderr. |
| 226 | android::base::SetLogger(android::base::StderrLogger); |
| 227 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 228 | return art::DexlayoutDriver(argc, argv); |
| 229 | } |