1 //===-- llvm-strings.cpp - Printable String dumping utility ---------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This program is a utility that works like binutils "strings", that is, it 11 // prints out printable strings in a binary, objdump, or archive file. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/Object/Binary.h" 16 #include "llvm/Support/CommandLine.h" 17 #include "llvm/Support/Error.h" 18 #include "llvm/Support/Format.h" 19 #include "llvm/Support/MemoryBuffer.h" 20 #include "llvm/Support/PrettyStackTrace.h" 21 #include "llvm/Support/Program.h" 22 #include "llvm/Support/Signals.h" 23 #include <cctype> 24 #include <string> 25 26 using namespace llvm; 27 using namespace llvm::object; 28 29 static cl::list<std::string> InputFileNames(cl::Positional, 30 cl::desc("<input object files>"), 31 cl::ZeroOrMore); 32 33 static cl::opt<bool> 34 PrintFileName("print-file-name", 35 cl::desc("Print the name of the file before each string")); 36 static cl::alias PrintFileNameShort("f", cl::desc(""), 37 cl::aliasopt(PrintFileName)); 38 39 static cl::opt<int> 40 MinLength("bytes", cl::desc("Print sequences of the specified length"), 41 cl::init(4)); 42 static cl::alias MinLengthShort("n", cl::desc(""), cl::aliasopt(MinLength)); 43 44 static cl::opt<bool> 45 AllSections("all", 46 cl::desc("Check all sections, not just the data section")); 47 static cl::alias AllSectionsShort("a", cl::desc(""), 48 cl::aliasopt(AllSections)); 49 50 enum radix { none, octal, hexadecimal, decimal }; 51 static cl::opt<radix> 52 Radix("radix", cl::desc("print the offset within the file"), 53 cl::values(clEnumValN(octal, "o", "octal"), 54 clEnumValN(hexadecimal, "x", "hexadecimal"), 55 clEnumValN(decimal, "d", "decimal")), 56 cl::init(none)); 57 static cl::alias RadixShort("t", cl::desc(""), cl::aliasopt(Radix)); 58 59 static void strings(raw_ostream &OS, StringRef FileName, StringRef Contents) { 60 auto print = [&OS, FileName](unsigned Offset, StringRef L) { 61 if (L.size() < static_cast<size_t>(MinLength)) 62 return; 63 if (PrintFileName) 64 OS << FileName << ":"; 65 switch (Radix) { 66 case none: 67 break; 68 case octal: 69 OS << format("%8o", Offset); 70 break; 71 case hexadecimal: 72 OS << format("%8x", Offset); 73 break; 74 case decimal: 75 OS << format("%8u", Offset); 76 break; 77 } 78 OS << " " << L << '\n'; 79 }; 80 81 const char *B = Contents.begin(); 82 const char *P = nullptr, *E = nullptr, *S = nullptr; 83 for (P = Contents.begin(), E = Contents.end(); P < E; ++P) { 84 if (std::isgraph(*P) || std::isblank(*P)) { 85 if (S == nullptr) 86 S = P; 87 } else if (S) { 88 print(S - B, StringRef(S, P - S)); 89 S = nullptr; 90 } 91 } 92 if (S) 93 print(S - B, StringRef(S, E - S)); 94 } 95 96 int main(int argc, char **argv) { 97 sys::PrintStackTraceOnErrorSignal(argv[0]); 98 PrettyStackTraceProgram X(argc, argv); 99 100 cl::ParseCommandLineOptions(argc, argv, "llvm string dumper\n"); 101 if (MinLength == 0) { 102 errs() << "invalid minimum string length 0\n"; 103 return EXIT_FAILURE; 104 } 105 106 if (InputFileNames.empty()) 107 InputFileNames.push_back("-"); 108 109 for (const auto &File : InputFileNames) { 110 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = 111 MemoryBuffer::getFileOrSTDIN(File); 112 if (std::error_code EC = Buffer.getError()) 113 errs() << File << ": " << EC.message() << '\n'; 114 else 115 strings(llvm::outs(), File == "-" ? "{standard input}" : File, 116 Buffer.get()->getMemBufferRef().getBuffer()); 117 } 118 119 return EXIT_SUCCESS; 120 } 121