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 42fe6060f1SDimitry Andric #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; 43fe6060f1SDimitry Andric #include "Opts.inc" 44fe6060f1SDimitry Andric #undef PREFIX 45fe6060f1SDimitry Andric 46349cc55cSDimitry Andric const opt::OptTable::Info InfoTable[] = { 47fe6060f1SDimitry Andric #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ 48fe6060f1SDimitry Andric HELPTEXT, METAVAR, VALUES) \ 49fe6060f1SDimitry Andric { \ 50fe6060f1SDimitry Andric PREFIX, NAME, HELPTEXT, \ 51fe6060f1SDimitry Andric METAVAR, OPT_##ID, opt::Option::KIND##Class, \ 52fe6060f1SDimitry Andric PARAM, FLAGS, OPT_##GROUP, \ 53fe6060f1SDimitry Andric OPT_##ALIAS, ALIASARGS, VALUES}, 54fe6060f1SDimitry Andric #include "Opts.inc" 55fe6060f1SDimitry Andric #undef OPTION 56fe6060f1SDimitry Andric }; 57fe6060f1SDimitry Andric 58fe6060f1SDimitry Andric class StringsOptTable : public opt::OptTable { 59fe6060f1SDimitry Andric public: 60fe6060f1SDimitry Andric StringsOptTable() : OptTable(InfoTable) { setGroupedShortOptions(true); } 61fe6060f1SDimitry Andric }; 62fe6060f1SDimitry Andric } // namespace 63fe6060f1SDimitry Andric 6404eeddc0SDimitry Andric static StringRef ToolName; 65fe6060f1SDimitry Andric 6662cfcf62SDimitry Andric static cl::list<std::string> InputFileNames(cl::Positional, 67*81ad6265SDimitry Andric cl::desc("<input object files>")); 6862cfcf62SDimitry Andric 69fe6060f1SDimitry Andric static int MinLength = 4; 70fe6060f1SDimitry Andric static bool PrintFileName; 7162cfcf62SDimitry Andric 7262cfcf62SDimitry Andric enum radix { none, octal, hexadecimal, decimal }; 73fe6060f1SDimitry Andric static radix Radix; 7462cfcf62SDimitry Andric 75349cc55cSDimitry Andric [[noreturn]] static void reportCmdLineError(const Twine &Message) { 76fe6060f1SDimitry Andric WithColor::error(errs(), ToolName) << Message << "\n"; 77fe6060f1SDimitry Andric exit(1); 78fe6060f1SDimitry Andric } 79fe6060f1SDimitry Andric 80fe6060f1SDimitry Andric template <typename T> 81fe6060f1SDimitry Andric static void parseIntArg(const opt::InputArgList &Args, int ID, T &Value) { 82fe6060f1SDimitry Andric if (const opt::Arg *A = Args.getLastArg(ID)) { 83fe6060f1SDimitry Andric StringRef V(A->getValue()); 84fe6060f1SDimitry Andric if (!llvm::to_integer(V, Value, 0) || Value <= 0) 85fe6060f1SDimitry Andric reportCmdLineError("expected a positive integer, but got '" + V + "'"); 86fe6060f1SDimitry Andric } 87fe6060f1SDimitry Andric } 8862cfcf62SDimitry Andric 8962cfcf62SDimitry Andric static void strings(raw_ostream &OS, StringRef FileName, StringRef Contents) { 9062cfcf62SDimitry Andric auto print = [&OS, FileName](unsigned Offset, StringRef L) { 9162cfcf62SDimitry Andric if (L.size() < static_cast<size_t>(MinLength)) 9262cfcf62SDimitry Andric return; 9362cfcf62SDimitry Andric if (PrintFileName) 9462cfcf62SDimitry Andric OS << FileName << ": "; 9562cfcf62SDimitry Andric switch (Radix) { 9662cfcf62SDimitry Andric case none: 9762cfcf62SDimitry Andric break; 9862cfcf62SDimitry Andric case octal: 9962cfcf62SDimitry Andric OS << format("%7o ", Offset); 10062cfcf62SDimitry Andric break; 10162cfcf62SDimitry Andric case hexadecimal: 10262cfcf62SDimitry Andric OS << format("%7x ", Offset); 10362cfcf62SDimitry Andric break; 10462cfcf62SDimitry Andric case decimal: 10562cfcf62SDimitry Andric OS << format("%7u ", Offset); 10662cfcf62SDimitry Andric break; 10762cfcf62SDimitry Andric } 10862cfcf62SDimitry Andric OS << L << '\n'; 10962cfcf62SDimitry Andric }; 11062cfcf62SDimitry Andric 11162cfcf62SDimitry Andric const char *B = Contents.begin(); 11262cfcf62SDimitry Andric const char *P = nullptr, *E = nullptr, *S = nullptr; 11362cfcf62SDimitry Andric for (P = Contents.begin(), E = Contents.end(); P < E; ++P) { 11462cfcf62SDimitry Andric if (isPrint(*P) || *P == '\t') { 11562cfcf62SDimitry Andric if (S == nullptr) 11662cfcf62SDimitry Andric S = P; 11762cfcf62SDimitry Andric } else if (S) { 11862cfcf62SDimitry Andric print(S - B, StringRef(S, P - S)); 11962cfcf62SDimitry Andric S = nullptr; 12062cfcf62SDimitry Andric } 12162cfcf62SDimitry Andric } 12262cfcf62SDimitry Andric if (S) 12362cfcf62SDimitry Andric print(S - B, StringRef(S, E - S)); 12462cfcf62SDimitry Andric } 12562cfcf62SDimitry Andric 12662cfcf62SDimitry Andric int main(int argc, char **argv) { 12762cfcf62SDimitry Andric InitLLVM X(argc, argv); 128fe6060f1SDimitry Andric BumpPtrAllocator A; 129fe6060f1SDimitry Andric StringSaver Saver(A); 130fe6060f1SDimitry Andric StringsOptTable Tbl; 13104eeddc0SDimitry Andric ToolName = argv[0]; 132fe6060f1SDimitry Andric opt::InputArgList Args = 133fe6060f1SDimitry Andric Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver, 134fe6060f1SDimitry Andric [&](StringRef Msg) { reportCmdLineError(Msg); }); 135fe6060f1SDimitry Andric if (Args.hasArg(OPT_help)) { 136fe6060f1SDimitry Andric Tbl.printHelp( 137fe6060f1SDimitry Andric outs(), 138fe6060f1SDimitry Andric (Twine(ToolName) + " [options] <input object files>").str().c_str(), 139fe6060f1SDimitry Andric "llvm string dumper"); 140fe6060f1SDimitry Andric // TODO Replace this with OptTable API once it adds extrahelp support. 141fe6060f1SDimitry Andric outs() << "\nPass @FILE as argument to read options from FILE.\n"; 142fe6060f1SDimitry Andric return 0; 143fe6060f1SDimitry Andric } 144fe6060f1SDimitry Andric if (Args.hasArg(OPT_version)) { 145fe6060f1SDimitry Andric outs() << ToolName << '\n'; 146fe6060f1SDimitry Andric cl::PrintVersionMessage(); 147fe6060f1SDimitry Andric return 0; 148fe6060f1SDimitry Andric } 14962cfcf62SDimitry Andric 150fe6060f1SDimitry Andric parseIntArg(Args, OPT_bytes_EQ, MinLength); 151fe6060f1SDimitry Andric PrintFileName = Args.hasArg(OPT_print_file_name); 152fe6060f1SDimitry Andric StringRef R = Args.getLastArgValue(OPT_radix_EQ); 153fe6060f1SDimitry Andric if (R.empty()) 154fe6060f1SDimitry Andric Radix = none; 155fe6060f1SDimitry Andric else if (R == "o") 156fe6060f1SDimitry Andric Radix = octal; 157fe6060f1SDimitry Andric else if (R == "d") 158fe6060f1SDimitry Andric Radix = decimal; 159fe6060f1SDimitry Andric else if (R == "x") 160fe6060f1SDimitry Andric Radix = hexadecimal; 161fe6060f1SDimitry Andric else 162fe6060f1SDimitry Andric reportCmdLineError("--radix value should be one of: '' (no offset), 'o' " 163fe6060f1SDimitry Andric "(octal), 'd' (decimal), 'x' (hexadecimal)"); 164fe6060f1SDimitry Andric 16562cfcf62SDimitry Andric if (MinLength == 0) { 16662cfcf62SDimitry Andric errs() << "invalid minimum string length 0\n"; 16762cfcf62SDimitry Andric return EXIT_FAILURE; 16862cfcf62SDimitry Andric } 16962cfcf62SDimitry Andric 170fe6060f1SDimitry Andric std::vector<std::string> InputFileNames = Args.getAllArgValues(OPT_INPUT); 17162cfcf62SDimitry Andric if (InputFileNames.empty()) 17262cfcf62SDimitry Andric InputFileNames.push_back("-"); 17362cfcf62SDimitry Andric 17462cfcf62SDimitry Andric for (const auto &File : InputFileNames) { 17562cfcf62SDimitry Andric ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = 17662cfcf62SDimitry Andric MemoryBuffer::getFileOrSTDIN(File); 17762cfcf62SDimitry Andric if (std::error_code EC = Buffer.getError()) 17862cfcf62SDimitry Andric errs() << File << ": " << EC.message() << '\n'; 17962cfcf62SDimitry Andric else 18062cfcf62SDimitry Andric strings(llvm::outs(), File == "-" ? "{standard input}" : File, 18162cfcf62SDimitry Andric Buffer.get()->getMemBufferRef().getBuffer()); 18262cfcf62SDimitry Andric } 18362cfcf62SDimitry Andric 18462cfcf62SDimitry Andric return EXIT_SUCCESS; 18562cfcf62SDimitry Andric } 186