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