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 | * 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 | |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | #include <unistd.h> |
| 28 | |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame^] | 29 | #include "base/logging.h" |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 30 | #include "mem_map.h" |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 31 | |
| 32 | namespace art { |
| 33 | |
| 34 | static const char* kProgramName = "dexlayout"; |
| 35 | |
| 36 | /* |
| 37 | * Shows usage. |
| 38 | */ |
| 39 | static void Usage(void) { |
| 40 | fprintf(stderr, "Copyright (C) 2007 The Android Open Source Project\n\n"); |
| 41 | fprintf(stderr, "%s: [-a] [-c] [-d] [-e] [-f] [-h] [-i] [-l layout] [-o outfile]" |
| 42 | " dexfile...\n\n", kProgramName); |
| 43 | fprintf(stderr, " -a : display annotations\n"); |
| 44 | fprintf(stderr, " -b : build dex_ir\n"); |
| 45 | fprintf(stderr, " -c : verify checksum and exit\n"); |
| 46 | fprintf(stderr, " -d : disassemble code sections\n"); |
| 47 | fprintf(stderr, " -e : display exported items only\n"); |
| 48 | fprintf(stderr, " -f : display summary information from file header\n"); |
| 49 | fprintf(stderr, " -g : display CFG for dex\n"); |
| 50 | fprintf(stderr, " -h : display file header details\n"); |
| 51 | fprintf(stderr, " -i : ignore checksum failures\n"); |
| 52 | fprintf(stderr, " -l : output layout, either 'plain' or 'xml'\n"); |
| 53 | fprintf(stderr, " -o : output file name (defaults to stdout)\n"); |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Main driver of the dexlayout utility. |
| 58 | */ |
| 59 | int DexlayoutDriver(int argc, char** argv) { |
| 60 | // Art specific set up. |
| 61 | InitLogging(argv); |
| 62 | MemMap::Init(); |
| 63 | |
| 64 | // Reset options. |
| 65 | bool want_usage = false; |
| 66 | memset(&options_, 0, sizeof(options_)); |
| 67 | options_.verbose_ = true; |
| 68 | |
| 69 | // Parse all arguments. |
| 70 | while (1) { |
| 71 | const int ic = getopt(argc, argv, "abcdefghil:o:"); |
| 72 | if (ic < 0) { |
| 73 | break; // done |
| 74 | } |
| 75 | switch (ic) { |
| 76 | case 'a': // display annotations |
| 77 | options_.show_annotations_ = true; |
| 78 | break; |
| 79 | case 'b': // build dex_ir |
| 80 | options_.build_dex_ir_ = true; |
| 81 | break; |
| 82 | case 'c': // verify the checksum then exit |
| 83 | options_.checksum_only_ = true; |
| 84 | break; |
| 85 | case 'd': // disassemble Dalvik instructions |
| 86 | options_.disassemble_ = true; |
| 87 | break; |
| 88 | case 'e': // exported items only |
| 89 | options_.exports_only_ = true; |
| 90 | break; |
| 91 | case 'f': // display outer file header |
| 92 | options_.show_file_headers_ = true; |
| 93 | break; |
| 94 | case 'g': // display cfg |
| 95 | options_.show_cfg_ = true; |
| 96 | break; |
| 97 | case 'h': // display section headers, i.e. all meta-data |
| 98 | options_.show_section_headers_ = true; |
| 99 | break; |
| 100 | case 'i': // continue even if checksum is bad |
| 101 | options_.ignore_bad_checksum_ = true; |
| 102 | break; |
| 103 | case 'l': // layout |
| 104 | if (strcmp(optarg, "plain") == 0) { |
| 105 | options_.output_format_ = kOutputPlain; |
| 106 | } else if (strcmp(optarg, "xml") == 0) { |
| 107 | options_.output_format_ = kOutputXml; |
| 108 | options_.verbose_ = false; |
| 109 | } else { |
| 110 | want_usage = true; |
| 111 | } |
| 112 | break; |
| 113 | case 'o': // output file |
| 114 | options_.output_file_name_ = optarg; |
| 115 | break; |
| 116 | default: |
| 117 | want_usage = true; |
| 118 | break; |
| 119 | } // switch |
| 120 | } // while |
| 121 | |
| 122 | // Detect early problems. |
| 123 | if (optind == argc) { |
| 124 | fprintf(stderr, "%s: no file specified\n", kProgramName); |
| 125 | want_usage = true; |
| 126 | } |
| 127 | if (options_.checksum_only_ && options_.ignore_bad_checksum_) { |
| 128 | fprintf(stderr, "Can't specify both -c and -i\n"); |
| 129 | want_usage = true; |
| 130 | } |
| 131 | if (want_usage) { |
| 132 | Usage(); |
| 133 | return 2; |
| 134 | } |
| 135 | |
| 136 | // Open alternative output file. |
| 137 | if (options_.output_file_name_) { |
| 138 | out_file_ = fopen(options_.output_file_name_, "w"); |
| 139 | if (!out_file_) { |
| 140 | fprintf(stderr, "Can't open %s\n", options_.output_file_name_); |
| 141 | return 1; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // Process all files supplied on command line. |
| 146 | int result = 0; |
| 147 | while (optind < argc) { |
| 148 | result |= ProcessFile(argv[optind++]); |
| 149 | } // while |
| 150 | return result != 0; |
| 151 | } |
| 152 | |
| 153 | } // namespace art |
| 154 | |
| 155 | int main(int argc, char** argv) { |
| 156 | return art::DexlayoutDriver(argc, argv); |
| 157 | } |