1 //===------ utils/obj2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
20 using namespace llvm::object;
21 using std::error_code;
23 static error_code dumpObject(const ObjectFile &Obj) {
25 return coff2yaml(outs(), cast<COFFObjectFile>(Obj));
27 return elf2yaml(outs(), Obj);
29 return obj2yaml_error::unsupported_obj_file_format;
32 static error_code dumpInput(StringRef File) {
33 if (File != "-" && !sys::fs::exists(File))
34 return obj2yaml_error::file_not_found;
36 ErrorOr<Binary *> BinaryOrErr = createBinary(File);
37 if (error_code EC = BinaryOrErr.getError())
40 std::unique_ptr<Binary> Binary(BinaryOrErr.get());
41 // TODO: If this is an archive, then burst it and dump each entry
42 if (ObjectFile *Obj = dyn_cast<ObjectFile>(Binary.get()))
43 return dumpObject(*Obj);
45 return obj2yaml_error::unrecognized_file_format;
48 cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"),
51 int main(int argc, char *argv[]) {
52 cl::ParseCommandLineOptions(argc, argv);
53 sys::PrintStackTraceOnErrorSignal();
54 PrettyStackTraceProgram X(argc, argv);
55 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
57 if (error_code EC = dumpInput(InputFilename)) {
58 errs() << "Error: '" << EC.message() << "'\n";