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