1 //===-- llvm-strings.cpp - Printable String dumping utility ---------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This program is a utility that works like binutils "strings", that is, it 10 // prints out printable strings in a binary, objdump, or archive file. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "Opts.inc" 15 #include "llvm/Object/Binary.h" 16 #include "llvm/Option/Arg.h" 17 #include "llvm/Option/ArgList.h" 18 #include "llvm/Option/Option.h" 19 #include "llvm/Support/CommandLine.h" 20 #include "llvm/Support/Error.h" 21 #include "llvm/Support/Format.h" 22 #include "llvm/Support/InitLLVM.h" 23 #include "llvm/Support/MemoryBuffer.h" 24 #include "llvm/Support/Program.h" 25 #include "llvm/Support/WithColor.h" 26 #include <cctype> 27 #include <string> 28 29 using namespace llvm; 30 using namespace llvm::object; 31 32 namespace { 33 enum ID { 34 OPT_INVALID = 0, // This is not an option ID. 35 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ 36 HELPTEXT, METAVAR, VALUES) \ 37 OPT_##ID, 38 #include "Opts.inc" 39 #undef OPTION 40 }; 41 42 #define PREFIX(NAME, VALUE) \ 43 static constexpr StringLiteral NAME##_init[] = VALUE; \ 44 static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ 45 std::size(NAME##_init) - 1); 46 #include "Opts.inc" 47 #undef PREFIX 48 49 static constexpr opt::OptTable::Info InfoTable[] = { 50 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ 51 HELPTEXT, METAVAR, VALUES) \ 52 { \ 53 PREFIX, NAME, HELPTEXT, \ 54 METAVAR, OPT_##ID, opt::Option::KIND##Class, \ 55 PARAM, FLAGS, OPT_##GROUP, \ 56 OPT_##ALIAS, ALIASARGS, VALUES}, 57 #include "Opts.inc" 58 #undef OPTION 59 }; 60 61 class StringsOptTable : public opt::OptTable { 62 public: 63 StringsOptTable() : OptTable(InfoTable) { setGroupedShortOptions(true); } 64 }; 65 } // namespace 66 67 static StringRef ToolName; 68 69 static cl::list<std::string> InputFileNames(cl::Positional, 70 cl::desc("<input object files>")); 71 72 static int MinLength = 4; 73 static bool PrintFileName; 74 75 enum radix { none, octal, hexadecimal, decimal }; 76 static radix Radix; 77 78 [[noreturn]] static void reportCmdLineError(const Twine &Message) { 79 WithColor::error(errs(), ToolName) << Message << "\n"; 80 exit(1); 81 } 82 83 template <typename T> 84 static void parseIntArg(const opt::InputArgList &Args, int ID, T &Value) { 85 if (const opt::Arg *A = Args.getLastArg(ID)) { 86 StringRef V(A->getValue()); 87 if (!llvm::to_integer(V, Value, 0) || Value <= 0) 88 reportCmdLineError("expected a positive integer, but got '" + V + "'"); 89 } 90 } 91 92 static void strings(raw_ostream &OS, StringRef FileName, StringRef Contents) { 93 auto print = [&OS, FileName](unsigned Offset, StringRef L) { 94 if (L.size() < static_cast<size_t>(MinLength)) 95 return; 96 if (PrintFileName) 97 OS << FileName << ": "; 98 switch (Radix) { 99 case none: 100 break; 101 case octal: 102 OS << format("%7o ", Offset); 103 break; 104 case hexadecimal: 105 OS << format("%7x ", Offset); 106 break; 107 case decimal: 108 OS << format("%7u ", Offset); 109 break; 110 } 111 OS << L << '\n'; 112 }; 113 114 const char *B = Contents.begin(); 115 const char *P = nullptr, *E = nullptr, *S = nullptr; 116 for (P = Contents.begin(), E = Contents.end(); P < E; ++P) { 117 if (isPrint(*P) || *P == '\t') { 118 if (S == nullptr) 119 S = P; 120 } else if (S) { 121 print(S - B, StringRef(S, P - S)); 122 S = nullptr; 123 } 124 } 125 if (S) 126 print(S - B, StringRef(S, E - S)); 127 } 128 129 int main(int argc, char **argv) { 130 InitLLVM X(argc, argv); 131 BumpPtrAllocator A; 132 StringSaver Saver(A); 133 StringsOptTable Tbl; 134 ToolName = argv[0]; 135 opt::InputArgList Args = 136 Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver, 137 [&](StringRef Msg) { reportCmdLineError(Msg); }); 138 if (Args.hasArg(OPT_help)) { 139 Tbl.printHelp( 140 outs(), 141 (Twine(ToolName) + " [options] <input object files>").str().c_str(), 142 "llvm string dumper"); 143 // TODO Replace this with OptTable API once it adds extrahelp support. 144 outs() << "\nPass @FILE as argument to read options from FILE.\n"; 145 return 0; 146 } 147 if (Args.hasArg(OPT_version)) { 148 outs() << ToolName << '\n'; 149 cl::PrintVersionMessage(); 150 return 0; 151 } 152 153 parseIntArg(Args, OPT_bytes_EQ, MinLength); 154 PrintFileName = Args.hasArg(OPT_print_file_name); 155 StringRef R = Args.getLastArgValue(OPT_radix_EQ); 156 if (R.empty()) 157 Radix = none; 158 else if (R == "o") 159 Radix = octal; 160 else if (R == "d") 161 Radix = decimal; 162 else if (R == "x") 163 Radix = hexadecimal; 164 else 165 reportCmdLineError("--radix value should be one of: '' (no offset), 'o' " 166 "(octal), 'd' (decimal), 'x' (hexadecimal)"); 167 168 if (MinLength == 0) { 169 errs() << "invalid minimum string length 0\n"; 170 return EXIT_FAILURE; 171 } 172 173 std::vector<std::string> InputFileNames = Args.getAllArgValues(OPT_INPUT); 174 if (InputFileNames.empty()) 175 InputFileNames.push_back("-"); 176 177 for (const auto &File : InputFileNames) { 178 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = 179 MemoryBuffer::getFileOrSTDIN(File); 180 if (std::error_code EC = Buffer.getError()) 181 errs() << File << ": " << EC.message() << '\n'; 182 else 183 strings(llvm::outs(), File == "-" ? "{standard input}" : File, 184 Buffer.get()->getMemBufferRef().getBuffer()); 185 } 186 187 return EXIT_SUCCESS; 188 } 189