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