xref: /llvm-project/llvm/tools/llvm-strings/llvm-strings.cpp (revision 2729786ffff8e74acbc179155364a30897d1affd)
1 //===-- llvm-strings.cpp - Printable String dumping utility ---------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This program is a utility that works like binutils "strings", that is, it
11 // prints out printable strings in a binary, objdump, or archive file.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/Object/Archive.h"
17 #include "llvm/Object/Binary.h"
18 #include "llvm/Object/ObjectFile.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/Error.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/PrettyStackTrace.h"
23 #include "llvm/Support/Program.h"
24 #include "llvm/Support/Signals.h"
25 #include <cctype>
26 #include <string>
27 
28 using namespace llvm;
29 using namespace llvm::object;
30 
31 static cl::list<std::string> InputFileNames(cl::Positional,
32                                             cl::desc("<input object files>"),
33                                             cl::ZeroOrMore);
34 
35 static void dump(raw_ostream &OS, StringRef Contents) {
36   const char *P = nullptr, *E = nullptr, *S = nullptr;
37   for (P = Contents.begin(), E = Contents.end(); P < E; ++P) {
38     if (std::isgraph(*P) || std::isblank(*P)) {
39       if (S == nullptr)
40         S = P;
41     } else if (S) {
42       if (P - S > 3)
43         OS << StringRef(S, P - S) << '\n';
44       S = nullptr;
45     }
46   }
47   if (S && E - S > 3)
48     OS << StringRef(S, E - S) << '\n';
49 }
50 
51 namespace {
52 class Strings {
53   LLVMContext Context;
54   raw_ostream &OS;
55 
56   void dump(const ObjectFile *O) {
57     for (const auto &S : O->sections()) {
58       StringRef Contents;
59       if (!S.getContents(Contents))
60         ::dump(OS, Contents);
61     }
62   }
63 
64   void dump(const Archive *A) {
65     Error E = Error::success();
66     for (auto &Element : A->children(E)) {
67       if (Expected<std::unique_ptr<Binary>> Child =
68               Element.getAsBinary(&Context)) {
69         dump(dyn_cast<ObjectFile>(&**Child));
70       } else {
71         if (auto E = isNotObjectErrorInvalidFileType(Child.takeError())) {
72           errs() << A->getFileName();
73           if (Expected<StringRef> Name = Element.getName())
74             errs() << '(' << *Name << ')';
75           logAllUnhandledErrors(std::move(E), errs(), "");
76           errs() << '\n';
77         }
78       }
79     }
80     (void)static_cast<bool>(E);
81   }
82 
83 public:
84   Strings(raw_ostream &S) : OS(S) {}
85 
86   void scan(StringRef File) {
87     ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
88         MemoryBuffer::getFileOrSTDIN(File);
89     if (std::error_code EC = Buffer.getError()) {
90       errs() << File << ": " << EC.message() << '\n';
91       return;
92     }
93 
94     if (Expected<std::unique_ptr<Binary>> B =
95             createBinary(Buffer.get()->getMemBufferRef(), &Context)) {
96       if (auto *A = dyn_cast<Archive>(&**B))
97         return dump(A);
98       if (auto *O = dyn_cast<ObjectFile>(&**B))
99         return dump(O);
100       ::dump(OS, Buffer.get()->getMemBufferRef().getBuffer());
101     } else {
102       consumeError(B.takeError());
103       ::dump(OS, Buffer.get()->getMemBufferRef().getBuffer());
104     }
105   }
106 };
107 }
108 
109 int main(int argc, char **argv) {
110   sys::PrintStackTraceOnErrorSignal(argv[0]);
111   PrettyStackTraceProgram X(argc, argv);
112 
113   cl::ParseCommandLineOptions(argc, argv, "llvm string dumper\n");
114 
115   if (InputFileNames.empty())
116     InputFileNames.push_back("-");
117 
118   Strings S(llvm::outs());
119   std::for_each(InputFileNames.begin(), InputFileNames.end(),
120                 [&S](StringRef F) { S.scan(F); });
121   return EXIT_SUCCESS;
122 }
123 
124