llvm-strings.cpp 3.9 KB

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