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