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