xref: /freebsd-src/contrib/llvm-project/llvm/tools/llvm-strings/llvm-strings.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
162cfcf62SDimitry Andric //===-- llvm-strings.cpp - Printable String dumping utility ---------------===//
262cfcf62SDimitry Andric //
362cfcf62SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
462cfcf62SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
562cfcf62SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
662cfcf62SDimitry Andric //
762cfcf62SDimitry Andric //===----------------------------------------------------------------------===//
862cfcf62SDimitry Andric //
962cfcf62SDimitry Andric // This program is a utility that works like binutils "strings", that is, it
1062cfcf62SDimitry Andric // prints out printable strings in a binary, objdump, or archive file.
1162cfcf62SDimitry Andric //
1262cfcf62SDimitry Andric //===----------------------------------------------------------------------===//
1362cfcf62SDimitry Andric 
14fe6060f1SDimitry Andric #include "Opts.inc"
1562cfcf62SDimitry Andric #include "llvm/Object/Binary.h"
16fe6060f1SDimitry Andric #include "llvm/Option/Arg.h"
17fe6060f1SDimitry Andric #include "llvm/Option/ArgList.h"
18fe6060f1SDimitry Andric #include "llvm/Option/Option.h"
1962cfcf62SDimitry Andric #include "llvm/Support/CommandLine.h"
2062cfcf62SDimitry Andric #include "llvm/Support/Error.h"
2162cfcf62SDimitry Andric #include "llvm/Support/Format.h"
2262cfcf62SDimitry Andric #include "llvm/Support/InitLLVM.h"
2362cfcf62SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
2462cfcf62SDimitry Andric #include "llvm/Support/Program.h"
25fe6060f1SDimitry Andric #include "llvm/Support/WithColor.h"
2662cfcf62SDimitry Andric #include <cctype>
2762cfcf62SDimitry Andric #include <string>
2862cfcf62SDimitry Andric 
2962cfcf62SDimitry Andric using namespace llvm;
3062cfcf62SDimitry Andric using namespace llvm::object;
3162cfcf62SDimitry Andric 
32fe6060f1SDimitry Andric namespace {
33fe6060f1SDimitry Andric enum ID {
34fe6060f1SDimitry Andric   OPT_INVALID = 0, // This is not an option ID.
35fe6060f1SDimitry Andric #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
36fe6060f1SDimitry Andric                HELPTEXT, METAVAR, VALUES)                                      \
37fe6060f1SDimitry Andric   OPT_##ID,
38fe6060f1SDimitry Andric #include "Opts.inc"
39fe6060f1SDimitry Andric #undef OPTION
40fe6060f1SDimitry Andric };
41fe6060f1SDimitry Andric 
42*bdd1243dSDimitry Andric #define PREFIX(NAME, VALUE)                                                    \
43*bdd1243dSDimitry Andric   static constexpr StringLiteral NAME##_init[] = VALUE;                        \
44*bdd1243dSDimitry Andric   static constexpr ArrayRef<StringLiteral> NAME(NAME##_init,                   \
45*bdd1243dSDimitry Andric                                                 std::size(NAME##_init) - 1);
46fe6060f1SDimitry Andric #include "Opts.inc"
47fe6060f1SDimitry Andric #undef PREFIX
48fe6060f1SDimitry Andric 
49*bdd1243dSDimitry Andric static constexpr opt::OptTable::Info InfoTable[] = {
50fe6060f1SDimitry Andric #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
51fe6060f1SDimitry Andric                HELPTEXT, METAVAR, VALUES)                                      \
52fe6060f1SDimitry Andric   {                                                                            \
53fe6060f1SDimitry Andric       PREFIX,      NAME,      HELPTEXT,                                        \
54fe6060f1SDimitry Andric       METAVAR,     OPT_##ID,  opt::Option::KIND##Class,                        \
55fe6060f1SDimitry Andric       PARAM,       FLAGS,     OPT_##GROUP,                                     \
56fe6060f1SDimitry Andric       OPT_##ALIAS, ALIASARGS, VALUES},
57fe6060f1SDimitry Andric #include "Opts.inc"
58fe6060f1SDimitry Andric #undef OPTION
59fe6060f1SDimitry Andric };
60fe6060f1SDimitry Andric 
61*bdd1243dSDimitry Andric class StringsOptTable : public opt::GenericOptTable {
62fe6060f1SDimitry Andric public:
63*bdd1243dSDimitry Andric   StringsOptTable() : GenericOptTable(InfoTable) {
64*bdd1243dSDimitry Andric     setGroupedShortOptions(true);
65*bdd1243dSDimitry Andric   }
66fe6060f1SDimitry Andric };
67fe6060f1SDimitry Andric } // namespace
68fe6060f1SDimitry Andric 
6904eeddc0SDimitry Andric static StringRef ToolName;
70fe6060f1SDimitry Andric 
7162cfcf62SDimitry Andric static cl::list<std::string> InputFileNames(cl::Positional,
7281ad6265SDimitry Andric                                             cl::desc("<input object files>"));
7362cfcf62SDimitry Andric 
74fe6060f1SDimitry Andric static int MinLength = 4;
75fe6060f1SDimitry Andric static bool PrintFileName;
7662cfcf62SDimitry Andric 
7762cfcf62SDimitry Andric enum radix { none, octal, hexadecimal, decimal };
78fe6060f1SDimitry Andric static radix Radix;
7962cfcf62SDimitry Andric 
80349cc55cSDimitry Andric [[noreturn]] static void reportCmdLineError(const Twine &Message) {
81fe6060f1SDimitry Andric   WithColor::error(errs(), ToolName) << Message << "\n";
82fe6060f1SDimitry Andric   exit(1);
83fe6060f1SDimitry Andric }
84fe6060f1SDimitry Andric 
85fe6060f1SDimitry Andric template <typename T>
86fe6060f1SDimitry Andric static void parseIntArg(const opt::InputArgList &Args, int ID, T &Value) {
87fe6060f1SDimitry Andric   if (const opt::Arg *A = Args.getLastArg(ID)) {
88fe6060f1SDimitry Andric     StringRef V(A->getValue());
89fe6060f1SDimitry Andric     if (!llvm::to_integer(V, Value, 0) || Value <= 0)
90fe6060f1SDimitry Andric       reportCmdLineError("expected a positive integer, but got '" + V + "'");
91fe6060f1SDimitry Andric   }
92fe6060f1SDimitry Andric }
9362cfcf62SDimitry Andric 
9462cfcf62SDimitry Andric static void strings(raw_ostream &OS, StringRef FileName, StringRef Contents) {
9562cfcf62SDimitry Andric   auto print = [&OS, FileName](unsigned Offset, StringRef L) {
9662cfcf62SDimitry Andric     if (L.size() < static_cast<size_t>(MinLength))
9762cfcf62SDimitry Andric       return;
9862cfcf62SDimitry Andric     if (PrintFileName)
9962cfcf62SDimitry Andric       OS << FileName << ": ";
10062cfcf62SDimitry Andric     switch (Radix) {
10162cfcf62SDimitry Andric     case none:
10262cfcf62SDimitry Andric       break;
10362cfcf62SDimitry Andric     case octal:
10462cfcf62SDimitry Andric       OS << format("%7o ", Offset);
10562cfcf62SDimitry Andric       break;
10662cfcf62SDimitry Andric     case hexadecimal:
10762cfcf62SDimitry Andric       OS << format("%7x ", Offset);
10862cfcf62SDimitry Andric       break;
10962cfcf62SDimitry Andric     case decimal:
11062cfcf62SDimitry Andric       OS << format("%7u ", Offset);
11162cfcf62SDimitry Andric       break;
11262cfcf62SDimitry Andric     }
11362cfcf62SDimitry Andric     OS << L << '\n';
11462cfcf62SDimitry Andric   };
11562cfcf62SDimitry Andric 
11662cfcf62SDimitry Andric   const char *B = Contents.begin();
11762cfcf62SDimitry Andric   const char *P = nullptr, *E = nullptr, *S = nullptr;
11862cfcf62SDimitry Andric   for (P = Contents.begin(), E = Contents.end(); P < E; ++P) {
11962cfcf62SDimitry Andric     if (isPrint(*P) || *P == '\t') {
12062cfcf62SDimitry Andric       if (S == nullptr)
12162cfcf62SDimitry Andric         S = P;
12262cfcf62SDimitry Andric     } else if (S) {
12362cfcf62SDimitry Andric       print(S - B, StringRef(S, P - S));
12462cfcf62SDimitry Andric       S = nullptr;
12562cfcf62SDimitry Andric     }
12662cfcf62SDimitry Andric   }
12762cfcf62SDimitry Andric   if (S)
12862cfcf62SDimitry Andric     print(S - B, StringRef(S, E - S));
12962cfcf62SDimitry Andric }
13062cfcf62SDimitry Andric 
13162cfcf62SDimitry Andric int main(int argc, char **argv) {
13262cfcf62SDimitry Andric   InitLLVM X(argc, argv);
133fe6060f1SDimitry Andric   BumpPtrAllocator A;
134fe6060f1SDimitry Andric   StringSaver Saver(A);
135fe6060f1SDimitry Andric   StringsOptTable Tbl;
13604eeddc0SDimitry Andric   ToolName = argv[0];
137fe6060f1SDimitry Andric   opt::InputArgList Args =
138fe6060f1SDimitry Andric       Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver,
139fe6060f1SDimitry Andric                     [&](StringRef Msg) { reportCmdLineError(Msg); });
140fe6060f1SDimitry Andric   if (Args.hasArg(OPT_help)) {
141fe6060f1SDimitry Andric     Tbl.printHelp(
142fe6060f1SDimitry Andric         outs(),
143fe6060f1SDimitry Andric         (Twine(ToolName) + " [options] <input object files>").str().c_str(),
144fe6060f1SDimitry Andric         "llvm string dumper");
145fe6060f1SDimitry Andric     // TODO Replace this with OptTable API once it adds extrahelp support.
146fe6060f1SDimitry Andric     outs() << "\nPass @FILE as argument to read options from FILE.\n";
147fe6060f1SDimitry Andric     return 0;
148fe6060f1SDimitry Andric   }
149fe6060f1SDimitry Andric   if (Args.hasArg(OPT_version)) {
150fe6060f1SDimitry Andric     outs() << ToolName << '\n';
151fe6060f1SDimitry Andric     cl::PrintVersionMessage();
152fe6060f1SDimitry Andric     return 0;
153fe6060f1SDimitry Andric   }
15462cfcf62SDimitry Andric 
155fe6060f1SDimitry Andric   parseIntArg(Args, OPT_bytes_EQ, MinLength);
156fe6060f1SDimitry Andric   PrintFileName = Args.hasArg(OPT_print_file_name);
157fe6060f1SDimitry Andric   StringRef R = Args.getLastArgValue(OPT_radix_EQ);
158fe6060f1SDimitry Andric   if (R.empty())
159fe6060f1SDimitry Andric     Radix = none;
160fe6060f1SDimitry Andric   else if (R == "o")
161fe6060f1SDimitry Andric     Radix = octal;
162fe6060f1SDimitry Andric   else if (R == "d")
163fe6060f1SDimitry Andric     Radix = decimal;
164fe6060f1SDimitry Andric   else if (R == "x")
165fe6060f1SDimitry Andric     Radix = hexadecimal;
166fe6060f1SDimitry Andric   else
167fe6060f1SDimitry Andric     reportCmdLineError("--radix value should be one of: '' (no offset), 'o' "
168fe6060f1SDimitry Andric                        "(octal), 'd' (decimal), 'x' (hexadecimal)");
169fe6060f1SDimitry Andric 
17062cfcf62SDimitry Andric   if (MinLength == 0) {
17162cfcf62SDimitry Andric     errs() << "invalid minimum string length 0\n";
17262cfcf62SDimitry Andric     return EXIT_FAILURE;
17362cfcf62SDimitry Andric   }
17462cfcf62SDimitry Andric 
175fe6060f1SDimitry Andric   std::vector<std::string> InputFileNames = Args.getAllArgValues(OPT_INPUT);
17662cfcf62SDimitry Andric   if (InputFileNames.empty())
17762cfcf62SDimitry Andric     InputFileNames.push_back("-");
17862cfcf62SDimitry Andric 
17962cfcf62SDimitry Andric   for (const auto &File : InputFileNames) {
18062cfcf62SDimitry Andric     ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
18162cfcf62SDimitry Andric         MemoryBuffer::getFileOrSTDIN(File);
18262cfcf62SDimitry Andric     if (std::error_code EC = Buffer.getError())
18362cfcf62SDimitry Andric       errs() << File << ": " << EC.message() << '\n';
18462cfcf62SDimitry Andric     else
18562cfcf62SDimitry Andric       strings(llvm::outs(), File == "-" ? "{standard input}" : File,
18662cfcf62SDimitry Andric               Buffer.get()->getMemBufferRef().getBuffer());
18762cfcf62SDimitry Andric   }
18862cfcf62SDimitry Andric 
18962cfcf62SDimitry Andric   return EXIT_SUCCESS;
19062cfcf62SDimitry Andric }
191