]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/llvm/tools/llvm-strings/llvm-strings.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm-project / llvm / tools / llvm-strings / llvm-strings.cpp
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 "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
24 using namespace llvm;
25 using namespace llvm::object;
26
27 static cl::list<std::string> InputFileNames(cl::Positional,
28                                             cl::desc("<input object files>"),
29                                             cl::ZeroOrMore);
30
31 static cl::opt<bool>
32     PrintFileName("print-file-name",
33                   cl::desc("Print the name of the file before each string"));
34 static cl::alias PrintFileNameShort("f", cl::desc(""),
35                                     cl::aliasopt(PrintFileName));
36
37 static cl::opt<int>
38     MinLength("bytes", cl::desc("Print sequences of the specified length"),
39               cl::init(4));
40 static cl::alias MinLengthShort("n", cl::desc(""), cl::aliasopt(MinLength));
41
42 static cl::opt<bool>
43     AllSections("all",
44                   cl::desc("Check all sections, not just the data section"));
45 static cl::alias AllSectionsShort("a", cl::desc(""),
46                                     cl::aliasopt(AllSections));
47
48 enum radix { none, octal, hexadecimal, decimal };
49 static cl::opt<radix>
50     Radix("radix", cl::desc("print the offset within the file"),
51           cl::values(clEnumValN(octal, "o", "octal"),
52                      clEnumValN(hexadecimal, "x", "hexadecimal"),
53                      clEnumValN(decimal, "d", "decimal")),
54           cl::init(none));
55 static cl::alias RadixShort("t", cl::desc(""), cl::aliasopt(Radix));
56
57 static cl::extrahelp
58     HelpResponse("\nPass @FILE as argument to read options from FILE.\n");
59
60 static void strings(raw_ostream &OS, StringRef FileName, StringRef Contents) {
61   auto print = [&OS, FileName](unsigned Offset, StringRef L) {
62     if (L.size() < static_cast<size_t>(MinLength))
63       return;
64     if (PrintFileName)
65       OS << FileName << ": ";
66     switch (Radix) {
67     case none:
68       break;
69     case octal:
70       OS << format("%7o ", Offset);
71       break;
72     case hexadecimal:
73       OS << format("%7x ", Offset);
74       break;
75     case decimal:
76       OS << format("%7u ", Offset);
77       break;
78     }
79     OS << L << '\n';
80   };
81
82   const char *B = Contents.begin();
83   const char *P = nullptr, *E = nullptr, *S = nullptr;
84   for (P = Contents.begin(), E = Contents.end(); P < E; ++P) {
85     if (isPrint(*P) || *P == '\t') {
86       if (S == nullptr)
87         S = P;
88     } else if (S) {
89       print(S - B, StringRef(S, P - S));
90       S = nullptr;
91     }
92   }
93   if (S)
94     print(S - B, StringRef(S, E - S));
95 }
96
97 int main(int argc, char **argv) {
98   InitLLVM 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 }