Marshall Clow | 5d30133 | 2012-06-19 18:02:35 +0000 | [diff] [blame] | 1 | //===------ utils/obj2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===// |
| 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 | #include "obj2yaml.h" |
| 11 | |
| 12 | #include "llvm/ADT/OwningPtr.h" |
| 13 | |
| 14 | #include "llvm/Support/CommandLine.h" |
| 15 | #include "llvm/Support/ManagedStatic.h" |
| 16 | #include "llvm/Support/PrettyStackTrace.h" |
| 17 | #include "llvm/Support/Signals.h" |
| 18 | |
| 19 | #include "llvm/Object/Archive.h" |
| 20 | #include "llvm/Object/COFF.h" |
| 21 | |
| 22 | const char endl = '\n'; |
| 23 | |
| 24 | namespace yaml { // generic yaml-writing specific routines |
| 25 | |
| 26 | unsigned char printable(unsigned char Ch) { |
| 27 | return Ch >= ' ' && Ch <= '~' ? Ch : '.'; |
| 28 | } |
| 29 | |
| 30 | llvm::raw_ostream &writeHexStream(llvm::raw_ostream &Out, |
| 31 | const llvm::ArrayRef<uint8_t> arr) { |
| 32 | const char *hex = "0123456789ABCDEF"; |
| 33 | Out << " !hex \""; |
| 34 | |
| 35 | typedef llvm::ArrayRef<uint8_t>::const_iterator iter_t; |
| 36 | const iter_t end = arr.end(); |
| 37 | for (iter_t iter = arr.begin(); iter != end; ++iter) |
| 38 | Out << hex[(*iter >> 4) & 0x0F] << hex[(*iter & 0x0F)]; |
| 39 | |
| 40 | Out << "\" # |"; |
| 41 | for (iter_t iter = arr.begin(); iter != end; ++iter) |
| 42 | Out << printable(*iter); |
| 43 | Out << "|" << endl; |
| 44 | |
| 45 | return Out; |
| 46 | } |
| 47 | |
| 48 | llvm::raw_ostream &writeHexNumber(llvm::raw_ostream &Out, unsigned long long N) { |
| 49 | if (N >= 10) |
| 50 | Out << "0x"; |
| 51 | Out.write_hex(N); |
| 52 | return Out; |
| 53 | } |
| 54 | |
| 55 | } |
| 56 | |
| 57 | |
| 58 | using namespace llvm; |
| 59 | enum ObjectFileType { coff }; |
| 60 | |
| 61 | cl::opt<ObjectFileType> InputFormat( |
| 62 | cl::desc("Choose input format"), |
| 63 | cl::values( |
| 64 | clEnumVal(coff, "process COFF object files"), |
| 65 | clEnumValEnd)); |
| 66 | |
| 67 | cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-")); |
| 68 | |
| 69 | int main(int argc, char * argv[]) { |
| 70 | cl::ParseCommandLineOptions(argc, argv); |
| 71 | sys::PrintStackTraceOnErrorSignal(); |
| 72 | PrettyStackTraceProgram X(argc, argv); |
| 73 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 74 | |
| 75 | // Process the input file |
| 76 | OwningPtr<MemoryBuffer> buf; |
| 77 | |
| 78 | // TODO: If this is an archive, then burst it and dump each entry |
| 79 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, buf)) |
| 80 | llvm::errs() << "Error: '" << ec.message() << "' opening file '" |
| 81 | << InputFilename << "'" << endl; |
| 82 | else { |
| 83 | ec = coff2yaml(llvm::outs(), buf.take()); |
| 84 | if (ec) |
| 85 | llvm::errs() << "Error: " << ec.message() << " dumping COFF file" << endl; |
| 86 | } |
| 87 | |
| 88 | return 0; |
| 89 | } |