xref: /freebsd-src/contrib/llvm-project/llvm/tools/llvm-strings/llvm-strings.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
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"
15*06c3fb27SDimitry Andric #include "llvm/ADT/StringExtras.h"
1662cfcf62SDimitry Andric #include "llvm/Object/Binary.h"
17fe6060f1SDimitry Andric #include "llvm/Option/Arg.h"
18fe6060f1SDimitry Andric #include "llvm/Option/ArgList.h"
19fe6060f1SDimitry Andric #include "llvm/Option/Option.h"
2062cfcf62SDimitry Andric #include "llvm/Support/CommandLine.h"
2162cfcf62SDimitry Andric #include "llvm/Support/Error.h"
2262cfcf62SDimitry Andric #include "llvm/Support/Format.h"
2362cfcf62SDimitry Andric #include "llvm/Support/InitLLVM.h"
2462cfcf62SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
2562cfcf62SDimitry Andric #include "llvm/Support/Program.h"
26fe6060f1SDimitry Andric #include "llvm/Support/WithColor.h"
2762cfcf62SDimitry Andric #include <cctype>
2862cfcf62SDimitry Andric #include <string>
2962cfcf62SDimitry Andric 
3062cfcf62SDimitry Andric using namespace llvm;
3162cfcf62SDimitry Andric using namespace llvm::object;
3262cfcf62SDimitry Andric 
33fe6060f1SDimitry Andric namespace {
34fe6060f1SDimitry Andric enum ID {
35fe6060f1SDimitry Andric   OPT_INVALID = 0, // This is not an option ID.
36fe6060f1SDimitry Andric #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
37fe6060f1SDimitry Andric                HELPTEXT, METAVAR, VALUES)                                      \
38fe6060f1SDimitry Andric   OPT_##ID,
39fe6060f1SDimitry Andric #include "Opts.inc"
40fe6060f1SDimitry Andric #undef OPTION
41fe6060f1SDimitry Andric };
42fe6060f1SDimitry Andric 
43bdd1243dSDimitry Andric #define PREFIX(NAME, VALUE)                                                    \
44bdd1243dSDimitry Andric   static constexpr StringLiteral NAME##_init[] = VALUE;                        \
45bdd1243dSDimitry Andric   static constexpr ArrayRef<StringLiteral> NAME(NAME##_init,                   \
46bdd1243dSDimitry Andric                                                 std::size(NAME##_init) - 1);
47fe6060f1SDimitry Andric #include "Opts.inc"
48fe6060f1SDimitry Andric #undef PREFIX
49fe6060f1SDimitry Andric 
50bdd1243dSDimitry Andric static constexpr opt::OptTable::Info InfoTable[] = {
51fe6060f1SDimitry Andric #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
52fe6060f1SDimitry Andric                HELPTEXT, METAVAR, VALUES)                                      \
53fe6060f1SDimitry Andric   {                                                                            \
54fe6060f1SDimitry Andric       PREFIX,      NAME,      HELPTEXT,                                        \
55fe6060f1SDimitry Andric       METAVAR,     OPT_##ID,  opt::Option::KIND##Class,                        \
56fe6060f1SDimitry Andric       PARAM,       FLAGS,     OPT_##GROUP,                                     \
57fe6060f1SDimitry Andric       OPT_##ALIAS, ALIASARGS, VALUES},
58fe6060f1SDimitry Andric #include "Opts.inc"
59fe6060f1SDimitry Andric #undef OPTION
60fe6060f1SDimitry Andric };
61fe6060f1SDimitry Andric 
62bdd1243dSDimitry Andric class StringsOptTable : public opt::GenericOptTable {
63fe6060f1SDimitry Andric public:
64bdd1243dSDimitry Andric   StringsOptTable() : GenericOptTable(InfoTable) {
65bdd1243dSDimitry Andric     setGroupedShortOptions(true);
66*06c3fb27SDimitry Andric     setDashDashParsing(true);
67bdd1243dSDimitry Andric   }
68fe6060f1SDimitry Andric };
69fe6060f1SDimitry Andric } // namespace
70fe6060f1SDimitry Andric 
7104eeddc0SDimitry Andric static StringRef ToolName;
72fe6060f1SDimitry Andric 
7362cfcf62SDimitry Andric static cl::list<std::string> InputFileNames(cl::Positional,
7481ad6265SDimitry Andric                                             cl::desc("<input object files>"));
7562cfcf62SDimitry Andric 
76fe6060f1SDimitry Andric static int MinLength = 4;
77fe6060f1SDimitry Andric static bool PrintFileName;
7862cfcf62SDimitry Andric 
7962cfcf62SDimitry Andric enum radix { none, octal, hexadecimal, decimal };
80fe6060f1SDimitry Andric static radix Radix;
8162cfcf62SDimitry Andric 
82349cc55cSDimitry Andric [[noreturn]] static void reportCmdLineError(const Twine &Message) {
83fe6060f1SDimitry Andric   WithColor::error(errs(), ToolName) << Message << "\n";
84fe6060f1SDimitry Andric   exit(1);
85fe6060f1SDimitry Andric }
86fe6060f1SDimitry Andric 
87fe6060f1SDimitry Andric template <typename T>
88fe6060f1SDimitry Andric static void parseIntArg(const opt::InputArgList &Args, int ID, T &Value) {
89fe6060f1SDimitry Andric   if (const opt::Arg *A = Args.getLastArg(ID)) {
90fe6060f1SDimitry Andric     StringRef V(A->getValue());
91fe6060f1SDimitry Andric     if (!llvm::to_integer(V, Value, 0) || Value <= 0)
92fe6060f1SDimitry Andric       reportCmdLineError("expected a positive integer, but got '" + V + "'");
93fe6060f1SDimitry Andric   }
94fe6060f1SDimitry Andric }
9562cfcf62SDimitry Andric 
9662cfcf62SDimitry Andric static void strings(raw_ostream &OS, StringRef FileName, StringRef Contents) {
9762cfcf62SDimitry Andric   auto print = [&OS, FileName](unsigned Offset, StringRef L) {
9862cfcf62SDimitry Andric     if (L.size() < static_cast<size_t>(MinLength))
9962cfcf62SDimitry Andric       return;
10062cfcf62SDimitry Andric     if (PrintFileName)
10162cfcf62SDimitry Andric       OS << FileName << ": ";
10262cfcf62SDimitry Andric     switch (Radix) {
10362cfcf62SDimitry Andric     case none:
10462cfcf62SDimitry Andric       break;
10562cfcf62SDimitry Andric     case octal:
10662cfcf62SDimitry Andric       OS << format("%7o ", Offset);
10762cfcf62SDimitry Andric       break;
10862cfcf62SDimitry Andric     case hexadecimal:
10962cfcf62SDimitry Andric       OS << format("%7x ", Offset);
11062cfcf62SDimitry Andric       break;
11162cfcf62SDimitry Andric     case decimal:
11262cfcf62SDimitry Andric       OS << format("%7u ", Offset);
11362cfcf62SDimitry Andric       break;
11462cfcf62SDimitry Andric     }
11562cfcf62SDimitry Andric     OS << L << '\n';
11662cfcf62SDimitry Andric   };
11762cfcf62SDimitry Andric 
11862cfcf62SDimitry Andric   const char *B = Contents.begin();
11962cfcf62SDimitry Andric   const char *P = nullptr, *E = nullptr, *S = nullptr;
12062cfcf62SDimitry Andric   for (P = Contents.begin(), E = Contents.end(); P < E; ++P) {
12162cfcf62SDimitry Andric     if (isPrint(*P) || *P == '\t') {
12262cfcf62SDimitry Andric       if (S == nullptr)
12362cfcf62SDimitry Andric         S = P;
12462cfcf62SDimitry Andric     } else if (S) {
12562cfcf62SDimitry Andric       print(S - B, StringRef(S, P - S));
12662cfcf62SDimitry Andric       S = nullptr;
12762cfcf62SDimitry Andric     }
12862cfcf62SDimitry Andric   }
12962cfcf62SDimitry Andric   if (S)
13062cfcf62SDimitry Andric     print(S - B, StringRef(S, E - S));
13162cfcf62SDimitry Andric }
13262cfcf62SDimitry Andric 
13362cfcf62SDimitry Andric int main(int argc, char **argv) {
13462cfcf62SDimitry Andric   InitLLVM X(argc, argv);
135fe6060f1SDimitry Andric   BumpPtrAllocator A;
136fe6060f1SDimitry Andric   StringSaver Saver(A);
137fe6060f1SDimitry Andric   StringsOptTable Tbl;
13804eeddc0SDimitry Andric   ToolName = argv[0];
139fe6060f1SDimitry Andric   opt::InputArgList Args =
140fe6060f1SDimitry Andric       Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver,
141fe6060f1SDimitry Andric                     [&](StringRef Msg) { reportCmdLineError(Msg); });
142fe6060f1SDimitry Andric   if (Args.hasArg(OPT_help)) {
143fe6060f1SDimitry Andric     Tbl.printHelp(
144fe6060f1SDimitry Andric         outs(),
145fe6060f1SDimitry Andric         (Twine(ToolName) + " [options] <input object files>").str().c_str(),
146fe6060f1SDimitry Andric         "llvm string dumper");
147fe6060f1SDimitry Andric     // TODO Replace this with OptTable API once it adds extrahelp support.
148fe6060f1SDimitry Andric     outs() << "\nPass @FILE as argument to read options from FILE.\n";
149fe6060f1SDimitry Andric     return 0;
150fe6060f1SDimitry Andric   }
151fe6060f1SDimitry Andric   if (Args.hasArg(OPT_version)) {
152fe6060f1SDimitry Andric     outs() << ToolName << '\n';
153fe6060f1SDimitry Andric     cl::PrintVersionMessage();
154fe6060f1SDimitry Andric     return 0;
155fe6060f1SDimitry Andric   }
15662cfcf62SDimitry Andric 
157fe6060f1SDimitry Andric   parseIntArg(Args, OPT_bytes_EQ, MinLength);
158fe6060f1SDimitry Andric   PrintFileName = Args.hasArg(OPT_print_file_name);
159fe6060f1SDimitry Andric   StringRef R = Args.getLastArgValue(OPT_radix_EQ);
160fe6060f1SDimitry Andric   if (R.empty())
161fe6060f1SDimitry Andric     Radix = none;
162fe6060f1SDimitry Andric   else if (R == "o")
163fe6060f1SDimitry Andric     Radix = octal;
164fe6060f1SDimitry Andric   else if (R == "d")
165fe6060f1SDimitry Andric     Radix = decimal;
166fe6060f1SDimitry Andric   else if (R == "x")
167fe6060f1SDimitry Andric     Radix = hexadecimal;
168fe6060f1SDimitry Andric   else
169fe6060f1SDimitry Andric     reportCmdLineError("--radix value should be one of: '' (no offset), 'o' "
170fe6060f1SDimitry Andric                        "(octal), 'd' (decimal), 'x' (hexadecimal)");
171fe6060f1SDimitry Andric 
17262cfcf62SDimitry Andric   if (MinLength == 0) {
17362cfcf62SDimitry Andric     errs() << "invalid minimum string length 0\n";
17462cfcf62SDimitry Andric     return EXIT_FAILURE;
17562cfcf62SDimitry Andric   }
17662cfcf62SDimitry Andric 
177fe6060f1SDimitry Andric   std::vector<std::string> InputFileNames = Args.getAllArgValues(OPT_INPUT);
17862cfcf62SDimitry Andric   if (InputFileNames.empty())
17962cfcf62SDimitry Andric     InputFileNames.push_back("-");
18062cfcf62SDimitry Andric 
18162cfcf62SDimitry Andric   for (const auto &File : InputFileNames) {
18262cfcf62SDimitry Andric     ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
18362cfcf62SDimitry Andric         MemoryBuffer::getFileOrSTDIN(File);
18462cfcf62SDimitry Andric     if (std::error_code EC = Buffer.getError())
18562cfcf62SDimitry Andric       errs() << File << ": " << EC.message() << '\n';
18662cfcf62SDimitry Andric     else
18762cfcf62SDimitry Andric       strings(llvm::outs(), File == "-" ? "{standard input}" : File,
18862cfcf62SDimitry Andric               Buffer.get()->getMemBufferRef().getBuffer());
18962cfcf62SDimitry Andric   }
19062cfcf62SDimitry Andric 
19162cfcf62SDimitry Andric   return EXIT_SUCCESS;
19262cfcf62SDimitry Andric }
193