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