blob: f089e0aba5aee6e590ec3a55158128b794a4a27a [file] [log] [blame]
Marshall Clow5d301332012-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 Clow5d301332012-06-19 18:02:35 +000011#include "llvm/ADT/OwningPtr.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000012#include "llvm/Object/Archive.h"
13#include "llvm/Object/COFF.h"
Marshall Clow5d301332012-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
Chandler Carruth18192892013-04-08 08:55:14 +000019using namespace llvm;
20
Marshall Clow5d301332012-06-19 18:02:35 +000021namespace yaml { // generic yaml-writing specific routines
22
23unsigned char printable(unsigned char Ch) {
24 return Ch >= ' ' && Ch <= '~' ? Ch : '.';
25}
Chandler Carruthc224e252013-04-08 08:39:59 +000026
Chandler Carruth18192892013-04-08 08:55:14 +000027raw_ostream &writeHexStream(raw_ostream &Out, const ArrayRef<uint8_t> arr) {
Marshall Clow5d301332012-06-19 18:02:35 +000028 const char *hex = "0123456789ABCDEF";
29 Out << " !hex \"";
30
Chandler Carruth18192892013-04-08 08:55:14 +000031 typedef ArrayRef<uint8_t>::const_iterator iter_t;
Marshall Clow5d301332012-06-19 18:02:35 +000032 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 Carruth741c00d2013-04-08 08:30:47 +000039 Out << "|\n";
Marshall Clow5d301332012-06-19 18:02:35 +000040
41 return Out;
Chandler Carruthc224e252013-04-08 08:39:59 +000042}
Marshall Clow5d301332012-06-19 18:02:35 +000043
Chandler Carruth18192892013-04-08 08:55:14 +000044raw_ostream &writeHexNumber(raw_ostream &Out, unsigned long long N) {
Marshall Clow5d301332012-06-19 18:02:35 +000045 if (N >= 10)
46 Out << "0x";
47 Out.write_hex(N);
48 return Out;
49}
50
Chandler Carruth18192892013-04-08 08:55:14 +000051} // end namespace yaml
Marshall Clow5d301332012-06-19 18:02:35 +000052
Chandler Carruth18192892013-04-08 08:55:14 +000053namespace {
Chandler Carruthc224e252013-04-08 08:39:59 +000054enum ObjectFileType {
55 coff
56};
Chandler Carruth18192892013-04-08 08:55:14 +000057}
Marshall Clow5d301332012-06-19 18:02:35 +000058
59cl::opt<ObjectFileType> InputFormat(
Chandler Carruthc224e252013-04-08 08:39:59 +000060 cl::desc("Choose input format"),
61 cl::values(clEnumVal(coff, "process COFF object files"), clEnumValEnd));
Marshall Clow5d301332012-06-19 18:02:35 +000062
Chandler Carruthc224e252013-04-08 08:39:59 +000063cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"),
64 cl::init("-"));
65
66int main(int argc, char *argv[]) {
Marshall Clow5d301332012-06-19 18:02:35 +000067 cl::ParseCommandLineOptions(argc, argv);
68 sys::PrintStackTraceOnErrorSignal();
69 PrettyStackTraceProgram X(argc, argv);
Chandler Carruthc224e252013-04-08 08:39:59 +000070 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Marshall Clow5d301332012-06-19 18:02:35 +000071
Chandler Carruthc224e252013-04-08 08:39:59 +000072 // Process the input file
Marshall Clow5d301332012-06-19 18:02:35 +000073 OwningPtr<MemoryBuffer> buf;
74
Chandler Carruthc224e252013-04-08 08:39:59 +000075 // TODO: If this is an archive, then burst it and dump each entry
76 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, buf)) {
Chandler Carruth18192892013-04-08 08:55:14 +000077 errs() << "Error: '" << ec.message() << "' opening file '" << InputFilename
78 << "'\n";
Chandler Carruthc224e252013-04-08 08:39:59 +000079 } else {
Chandler Carruth18192892013-04-08 08:55:14 +000080 ec = coff2yaml(outs(), buf.take());
Marshall Clow5d301332012-06-19 18:02:35 +000081 if (ec)
Chandler Carruth18192892013-04-08 08:55:14 +000082 errs() << "Error: " << ec.message() << " dumping COFF file\n";
Marshall Clow5d301332012-06-19 18:02:35 +000083 }
84
85 return 0;
86}