llvm-strings.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include "llvm/Object/Binary.h"
  15. #include "llvm/Support/CommandLine.h"
  16. #include "llvm/Support/Error.h"
  17. #include "llvm/Support/Format.h"
  18. #include "llvm/Support/InitLLVM.h"
  19. #include "llvm/Support/MemoryBuffer.h"
  20. #include "llvm/Support/Program.h"
  21. #include <cctype>
  22. #include <string>
  23. using namespace llvm;
  24. using namespace llvm::object;
  25. static cl::list<std::string> InputFileNames(cl::Positional,
  26. cl::desc("<input object files>"),
  27. cl::ZeroOrMore);
  28. static cl::opt<bool>
  29. PrintFileName("print-file-name",
  30. cl::desc("Print the name of the file before each string"));
  31. static cl::alias PrintFileNameShort("f", cl::desc(""),
  32. cl::aliasopt(PrintFileName));
  33. static cl::opt<int>
  34. MinLength("bytes", cl::desc("Print sequences of the specified length"),
  35. cl::init(4));
  36. static cl::alias MinLengthShort("n", cl::desc(""), cl::aliasopt(MinLength));
  37. static cl::opt<bool>
  38. AllSections("all",
  39. cl::desc("Check all sections, not just the data section"));
  40. static cl::alias AllSectionsShort("a", cl::desc(""),
  41. cl::aliasopt(AllSections));
  42. enum radix { none, octal, hexadecimal, decimal };
  43. static cl::opt<radix>
  44. Radix("radix", cl::desc("print the offset within the file"),
  45. cl::values(clEnumValN(octal, "o", "octal"),
  46. clEnumValN(hexadecimal, "x", "hexadecimal"),
  47. clEnumValN(decimal, "d", "decimal")),
  48. cl::init(none));
  49. static cl::alias RadixShort("t", cl::desc(""), cl::aliasopt(Radix));
  50. static void strings(raw_ostream &OS, StringRef FileName, StringRef Contents) {
  51. auto print = [&OS, FileName](unsigned Offset, StringRef L) {
  52. if (L.size() < static_cast<size_t>(MinLength))
  53. return;
  54. if (PrintFileName)
  55. OS << FileName << ":";
  56. switch (Radix) {
  57. case none:
  58. break;
  59. case octal:
  60. OS << format("%8o", Offset);
  61. break;
  62. case hexadecimal:
  63. OS << format("%8x", Offset);
  64. break;
  65. case decimal:
  66. OS << format("%8u", Offset);
  67. break;
  68. }
  69. OS << " " << L << '\n';
  70. };
  71. const char *B = Contents.begin();
  72. const char *P = nullptr, *E = nullptr, *S = nullptr;
  73. for (P = Contents.begin(), E = Contents.end(); P < E; ++P) {
  74. if (std::isgraph(*P) || std::isblank(*P)) {
  75. if (S == nullptr)
  76. S = P;
  77. } else if (S) {
  78. print(S - B, StringRef(S, P - S));
  79. S = nullptr;
  80. }
  81. }
  82. if (S)
  83. print(S - B, StringRef(S, E - S));
  84. }
  85. int main(int argc, char **argv) {
  86. InitLLVM X(argc, argv);
  87. cl::ParseCommandLineOptions(argc, argv, "llvm string dumper\n");
  88. if (MinLength == 0) {
  89. errs() << "invalid minimum string length 0\n";
  90. return EXIT_FAILURE;
  91. }
  92. if (InputFileNames.empty())
  93. InputFileNames.push_back("-");
  94. for (const auto &File : InputFileNames) {
  95. ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
  96. MemoryBuffer::getFileOrSTDIN(File);
  97. if (std::error_code EC = Buffer.getError())
  98. errs() << File << ": " << EC.message() << '\n';
  99. else
  100. strings(llvm::outs(), File == "-" ? "{standard input}" : File,
  101. Buffer.get()->getMemBufferRef().getBuffer());
  102. }
  103. return EXIT_SUCCESS;
  104. }