blob: 615ff863b1fdefa2128069bd3c3d850f6925225e [file] [log] [blame]
Marshall Clowc57b8882012-06-19 18:02:35 +00001//===------ 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 Clowc57b8882012-06-19 18:02:35 +000011#include "llvm/ADT/OwningPtr.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000012#include "llvm/Object/Archive.h"
13#include "llvm/Object/COFF.h"
Marshall Clowc57b8882012-06-19 18:02:35 +000014#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 Clowc57b8882012-06-19 18:02:35 +000019namespace yaml { // generic yaml-writing specific routines
20
21unsigned char printable(unsigned char Ch) {
22 return Ch >= ' ' && Ch <= '~' ? Ch : '.';
23}
Chandler Carruth724a7b12013-04-08 08:39:59 +000024
25llvm::raw_ostream &writeHexStream(llvm::raw_ostream &Out,
26 const llvm::ArrayRef<uint8_t> arr) {
Marshall Clowc57b8882012-06-19 18:02:35 +000027 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 Carruthbcff69a2013-04-08 08:30:47 +000038 Out << "|\n";
Marshall Clowc57b8882012-06-19 18:02:35 +000039
40 return Out;
Chandler Carruth724a7b12013-04-08 08:39:59 +000041}
Marshall Clowc57b8882012-06-19 18:02:35 +000042
Chandler Carruth724a7b12013-04-08 08:39:59 +000043llvm::raw_ostream &writeHexNumber(llvm::raw_ostream &Out,
44 unsigned long long N) {
Marshall Clowc57b8882012-06-19 18:02:35 +000045 if (N >= 10)
46 Out << "0x";
47 Out.write_hex(N);
48 return Out;
49}
50
51}
52
Marshall Clowc57b8882012-06-19 18:02:35 +000053using namespace llvm;
Chandler Carruth724a7b12013-04-08 08:39:59 +000054enum ObjectFileType {
55 coff
56};
Marshall Clowc57b8882012-06-19 18:02:35 +000057
58cl::opt<ObjectFileType> InputFormat(
Chandler Carruth724a7b12013-04-08 08:39:59 +000059 cl::desc("Choose input format"),
60 cl::values(clEnumVal(coff, "process COFF object files"), clEnumValEnd));
Marshall Clowc57b8882012-06-19 18:02:35 +000061
Chandler Carruth724a7b12013-04-08 08:39:59 +000062cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"),
63 cl::init("-"));
64
65int main(int argc, char *argv[]) {
Marshall Clowc57b8882012-06-19 18:02:35 +000066 cl::ParseCommandLineOptions(argc, argv);
67 sys::PrintStackTraceOnErrorSignal();
68 PrettyStackTraceProgram X(argc, argv);
Chandler Carruth724a7b12013-04-08 08:39:59 +000069 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Marshall Clowc57b8882012-06-19 18:02:35 +000070
Chandler Carruth724a7b12013-04-08 08:39:59 +000071 // Process the input file
Marshall Clowc57b8882012-06-19 18:02:35 +000072 OwningPtr<MemoryBuffer> buf;
73
Chandler Carruth724a7b12013-04-08 08:39:59 +000074 // TODO: If this is an archive, then burst it and dump each entry
75 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, buf)) {
76 llvm::errs() << "Error: '" << ec.message() << "' opening file '"
77 << InputFilename << "'\n";
78 } else {
Marshall Clowc57b8882012-06-19 18:02:35 +000079 ec = coff2yaml(llvm::outs(), buf.take());
80 if (ec)
Chandler Carruthbcff69a2013-04-08 08:30:47 +000081 llvm::errs() << "Error: " << ec.message() << " dumping COFF file\n";
Marshall Clowc57b8882012-06-19 18:02:35 +000082 }
83
84 return 0;
85}