Saleem Abdulrasool | 030ff0f | 2016-11-11 03:44:12 +0000 | [diff] [blame] | 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" |
Saleem Abdulrasool | 2dcea63 | 2016-11-11 04:00:59 +0000 | [diff] [blame^] | 25 | #include <cctype> |
Saleem Abdulrasool | 030ff0f | 2016-11-11 03:44:12 +0000 | [diff] [blame] | 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 *S = nullptr; |
| 37 | for (const char *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 | } |
| 48 | |
| 49 | namespace { |
| 50 | class Strings { |
| 51 | LLVMContext Context; |
| 52 | raw_ostream &OS; |
| 53 | |
| 54 | void dump(const ObjectFile *O) { |
| 55 | for (const auto &S : O->sections()) { |
| 56 | StringRef Contents; |
| 57 | if (!S.getContents(Contents)) |
| 58 | ::dump(OS, Contents); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void dump(const Archive *A) { |
| 63 | Error E; |
| 64 | for (auto &Element : A->children(E)) { |
| 65 | if (Expected<std::unique_ptr<Binary>> Child = |
| 66 | Element.getAsBinary(&Context)) { |
| 67 | dump(dyn_cast<ObjectFile>(&**Child)); |
| 68 | } else { |
| 69 | if (auto E = isNotObjectErrorInvalidFileType(Child.takeError())) { |
| 70 | errs() << A->getFileName(); |
| 71 | if (Expected<StringRef> Name = Element.getName()) |
| 72 | errs() << '(' << *Name << ')'; |
| 73 | logAllUnhandledErrors(std::move(E), errs(), ""); |
| 74 | errs() << '\n'; |
| 75 | } |
| 76 | } |
| 77 | } |
Saleem Abdulrasool | 2dcea63 | 2016-11-11 04:00:59 +0000 | [diff] [blame^] | 78 | (void)static_cast<bool>(E); |
Saleem Abdulrasool | 030ff0f | 2016-11-11 03:44:12 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | public: |
| 82 | Strings(raw_ostream &S) : OS(S) {} |
| 83 | |
| 84 | void scan(StringRef File) { |
| 85 | ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = |
| 86 | MemoryBuffer::getFileOrSTDIN(File); |
| 87 | if (std::error_code EC = Buffer.getError()) { |
| 88 | errs() << File << ": " << EC.message() << '\n'; |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | if (Expected<std::unique_ptr<Binary>> B = |
| 93 | createBinary(Buffer.get()->getMemBufferRef(), &Context)) { |
| 94 | if (auto *A = dyn_cast<Archive>(&**B)) |
| 95 | return dump(A); |
| 96 | if (auto *O = dyn_cast<ObjectFile>(&**B)) |
| 97 | return dump(O); |
| 98 | ::dump(OS, Buffer.get()->getMemBufferRef().getBuffer()); |
| 99 | } else { |
| 100 | consumeError(B.takeError()); |
| 101 | ::dump(OS, Buffer.get()->getMemBufferRef().getBuffer()); |
| 102 | } |
| 103 | } |
| 104 | }; |
| 105 | } |
| 106 | |
| 107 | int main(int argc, char **argv) { |
| 108 | sys::PrintStackTraceOnErrorSignal(argv[0]); |
| 109 | PrettyStackTraceProgram X(argc, argv); |
| 110 | |
| 111 | cl::ParseCommandLineOptions(argc, argv, "llvm string dumper\n"); |
| 112 | |
| 113 | if (InputFileNames.empty()) |
| 114 | InputFileNames.push_back("-"); |
| 115 | |
| 116 | Strings S(llvm::outs()); |
| 117 | std::for_each(InputFileNames.begin(), InputFileNames.end(), |
| 118 | [&S](StringRef F) { S.scan(F); }); |
| 119 | return EXIT_SUCCESS; |
| 120 | } |
| 121 | |