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