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