1 //===-- llvm-strings.cpp - Printable String dumping utility ---------------===//
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
7 //===----------------------------------------------------------------------===//
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.
12 //===----------------------------------------------------------------------===//
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"
30 using namespace llvm::object;
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) \
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);
49 static constexpr opt::OptTable::Info InfoTable[] = {
50 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
51 HELPTEXT, METAVAR, VALUES) \
53 PREFIX, NAME, HELPTEXT, \
54 METAVAR, OPT_##ID, opt::Option::KIND##Class, \
55 PARAM, FLAGS, OPT_##GROUP, \
56 OPT_##ALIAS, ALIASARGS, VALUES},
61 class StringsOptTable : public opt::GenericOptTable {
63 StringsOptTable() : GenericOptTable(InfoTable) {
64 setGroupedShortOptions(true);
69 static StringRef ToolName;
71 static cl::list<std::string> InputFileNames(cl::Positional,
72 cl::desc("<input object files>"));
74 static int MinLength = 4;
75 static bool PrintFileName;
77 enum radix { none, octal, hexadecimal, decimal };
80 [[noreturn]] static void reportCmdLineError(const Twine &Message) {
81 WithColor::error(errs(), ToolName) << Message << "\n";
86 static void parseIntArg(const opt::InputArgList &Args, int ID, T &Value) {
87 if (const opt::Arg *A = Args.getLastArg(ID)) {
88 StringRef V(A->getValue());
89 if (!llvm::to_integer(V, Value, 0) || Value <= 0)
90 reportCmdLineError("expected a positive integer, but got '" + V + "'");
94 static void strings(raw_ostream &OS, StringRef FileName, StringRef Contents) {
95 auto print = [&OS, FileName](unsigned Offset, StringRef L) {
96 if (L.size() < static_cast<size_t>(MinLength))
99 OS << FileName << ": ";
104 OS << format("%7o ", Offset);
107 OS << format("%7x ", Offset);
110 OS << format("%7u ", Offset);
116 const char *B = Contents.begin();
117 const char *P = nullptr, *E = nullptr, *S = nullptr;
118 for (P = Contents.begin(), E = Contents.end(); P < E; ++P) {
119 if (isPrint(*P) || *P == '\t') {
123 print(S - B, StringRef(S, P - S));
128 print(S - B, StringRef(S, E - S));
131 int main(int argc, char **argv) {
132 InitLLVM X(argc, argv);
134 StringSaver Saver(A);
137 opt::InputArgList Args =
138 Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver,
139 [&](StringRef Msg) { reportCmdLineError(Msg); });
140 if (Args.hasArg(OPT_help)) {
143 (Twine(ToolName) + " [options] <input object files>").str().c_str(),
144 "llvm string dumper");
145 // TODO Replace this with OptTable API once it adds extrahelp support.
146 outs() << "\nPass @FILE as argument to read options from FILE.\n";
149 if (Args.hasArg(OPT_version)) {
150 outs() << ToolName << '\n';
151 cl::PrintVersionMessage();
155 parseIntArg(Args, OPT_bytes_EQ, MinLength);
156 PrintFileName = Args.hasArg(OPT_print_file_name);
157 StringRef R = Args.getLastArgValue(OPT_radix_EQ);
167 reportCmdLineError("--radix value should be one of: '' (no offset), 'o' "
168 "(octal), 'd' (decimal), 'x' (hexadecimal)");
170 if (MinLength == 0) {
171 errs() << "invalid minimum string length 0\n";
175 std::vector<std::string> InputFileNames = Args.getAllArgValues(OPT_INPUT);
176 if (InputFileNames.empty())
177 InputFileNames.push_back("-");
179 for (const auto &File : InputFileNames) {
180 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
181 MemoryBuffer::getFileOrSTDIN(File);
182 if (std::error_code EC = Buffer.getError())
183 errs() << File << ": " << EC.message() << '\n';
185 strings(llvm::outs(), File == "-" ? "{standard input}" : File,
186 Buffer.get()->getMemBufferRef().getBuffer());