xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-strings/llvm-strings.cpp (revision 29332c0dcee1e80c9fb871e06c3160bd5deb1b44)
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 "Opts.inc"
15 #include "llvm/Object/Binary.h"
16 #include "llvm/Option/Arg.h"
17 #include "llvm/Option/ArgList.h"
18 #include "llvm/Option/Option.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/Error.h"
21 #include "llvm/Support/Format.h"
22 #include "llvm/Support/InitLLVM.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/Program.h"
25 #include "llvm/Support/WithColor.h"
26 #include <cctype>
27 #include <string>
28 
29 using namespace llvm;
30 using namespace llvm::object;
31 
32 namespace {
33 enum ID {
34   OPT_INVALID = 0, // This is not an option ID.
35 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
36                HELPTEXT, METAVAR, VALUES)                                      \
37   OPT_##ID,
38 #include "Opts.inc"
39 #undef OPTION
40 };
41 
42 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
43 #include "Opts.inc"
44 #undef PREFIX
45 
46 static const opt::OptTable::Info InfoTable[] = {
47 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
48                HELPTEXT, METAVAR, VALUES)                                      \
49   {                                                                            \
50       PREFIX,      NAME,      HELPTEXT,                                        \
51       METAVAR,     OPT_##ID,  opt::Option::KIND##Class,                        \
52       PARAM,       FLAGS,     OPT_##GROUP,                                     \
53       OPT_##ALIAS, ALIASARGS, VALUES},
54 #include "Opts.inc"
55 #undef OPTION
56 };
57 
58 class StringsOptTable : public opt::OptTable {
59 public:
60   StringsOptTable() : OptTable(InfoTable) { setGroupedShortOptions(true); }
61 };
62 } // namespace
63 
64 const char ToolName[] = "llvm-strings";
65 
66 static cl::list<std::string> InputFileNames(cl::Positional,
67                                             cl::desc("<input object files>"),
68                                             cl::ZeroOrMore);
69 
70 static int MinLength = 4;
71 static bool PrintFileName;
72 
73 enum radix { none, octal, hexadecimal, decimal };
74 static radix Radix;
75 
76 LLVM_ATTRIBUTE_NORETURN static void reportCmdLineError(const Twine &Message) {
77   WithColor::error(errs(), ToolName) << Message << "\n";
78   exit(1);
79 }
80 
81 template <typename T>
82 static void parseIntArg(const opt::InputArgList &Args, int ID, T &Value) {
83   if (const opt::Arg *A = Args.getLastArg(ID)) {
84     StringRef V(A->getValue());
85     if (!llvm::to_integer(V, Value, 0) || Value <= 0)
86       reportCmdLineError("expected a positive integer, but got '" + V + "'");
87   }
88 }
89 
90 static void strings(raw_ostream &OS, StringRef FileName, StringRef Contents) {
91   auto print = [&OS, FileName](unsigned Offset, StringRef L) {
92     if (L.size() < static_cast<size_t>(MinLength))
93       return;
94     if (PrintFileName)
95       OS << FileName << ": ";
96     switch (Radix) {
97     case none:
98       break;
99     case octal:
100       OS << format("%7o ", Offset);
101       break;
102     case hexadecimal:
103       OS << format("%7x ", Offset);
104       break;
105     case decimal:
106       OS << format("%7u ", Offset);
107       break;
108     }
109     OS << L << '\n';
110   };
111 
112   const char *B = Contents.begin();
113   const char *P = nullptr, *E = nullptr, *S = nullptr;
114   for (P = Contents.begin(), E = Contents.end(); P < E; ++P) {
115     if (isPrint(*P) || *P == '\t') {
116       if (S == nullptr)
117         S = P;
118     } else if (S) {
119       print(S - B, StringRef(S, P - S));
120       S = nullptr;
121     }
122   }
123   if (S)
124     print(S - B, StringRef(S, E - S));
125 }
126 
127 int main(int argc, char **argv) {
128   InitLLVM X(argc, argv);
129   BumpPtrAllocator A;
130   StringSaver Saver(A);
131   StringsOptTable Tbl;
132   opt::InputArgList Args =
133       Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver,
134                     [&](StringRef Msg) { reportCmdLineError(Msg); });
135   if (Args.hasArg(OPT_help)) {
136     Tbl.printHelp(
137         outs(),
138         (Twine(ToolName) + " [options] <input object files>").str().c_str(),
139         "llvm string dumper");
140     // TODO Replace this with OptTable API once it adds extrahelp support.
141     outs() << "\nPass @FILE as argument to read options from FILE.\n";
142     return 0;
143   }
144   if (Args.hasArg(OPT_version)) {
145     outs() << ToolName << '\n';
146     cl::PrintVersionMessage();
147     return 0;
148   }
149 
150   parseIntArg(Args, OPT_bytes_EQ, MinLength);
151   PrintFileName = Args.hasArg(OPT_print_file_name);
152   StringRef R = Args.getLastArgValue(OPT_radix_EQ);
153   if (R.empty())
154     Radix = none;
155   else if (R == "o")
156     Radix = octal;
157   else if (R == "d")
158     Radix = decimal;
159   else if (R == "x")
160     Radix = hexadecimal;
161   else
162     reportCmdLineError("--radix value should be one of: '' (no offset), 'o' "
163                        "(octal), 'd' (decimal), 'x' (hexadecimal)");
164 
165   if (MinLength == 0) {
166     errs() << "invalid minimum string length 0\n";
167     return EXIT_FAILURE;
168   }
169 
170   std::vector<std::string> InputFileNames = Args.getAllArgValues(OPT_INPUT);
171   if (InputFileNames.empty())
172     InputFileNames.push_back("-");
173 
174   for (const auto &File : InputFileNames) {
175     ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
176         MemoryBuffer::getFileOrSTDIN(File);
177     if (std::error_code EC = Buffer.getError())
178       errs() << File << ": " << EC.message() << '\n';
179     else
180       strings(llvm::outs(), File == "-" ? "{standard input}" : File,
181               Buffer.get()->getMemBufferRef().getBuffer());
182   }
183 
184   return EXIT_SUCCESS;
185 }
186