blob: 944314a923f77456017f5796648cabd49dfaff34 [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
Stephen Hinesdce4a402014-05-29 02:49:00 -070010#include "Error.h"
Marshall Clowc57b8882012-06-19 18:02:35 +000011#include "obj2yaml.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
Chandler Carruth97034bb2013-04-08 08:55:14 +000019using namespace llvm;
Stephen Hinesdce4a402014-05-29 02:49:00 -070020using namespace llvm::object;
Chandler Carruth97034bb2013-04-08 08:55:14 +000021
Stephen Hinescd81d942014-07-21 00:45:20 -070022static std::error_code dumpObject(const ObjectFile &Obj) {
Stephen Hinesdce4a402014-05-29 02:49:00 -070023 if (Obj.isCOFF())
24 return coff2yaml(outs(), cast<COFFObjectFile>(Obj));
25 if (Obj.isELF())
26 return elf2yaml(outs(), Obj);
27
28 return obj2yaml_error::unsupported_obj_file_format;
Chandler Carruth97034bb2013-04-08 08:55:14 +000029}
Marshall Clowc57b8882012-06-19 18:02:35 +000030
Stephen Hinescd81d942014-07-21 00:45:20 -070031static std::error_code dumpInput(StringRef File) {
Stephen Hinesdce4a402014-05-29 02:49:00 -070032 if (File != "-" && !sys::fs::exists(File))
33 return obj2yaml_error::file_not_found;
34
35 ErrorOr<Binary *> BinaryOrErr = createBinary(File);
Stephen Hinescd81d942014-07-21 00:45:20 -070036 if (std::error_code EC = BinaryOrErr.getError())
Stephen Hinesdce4a402014-05-29 02:49:00 -070037 return EC;
38
39 std::unique_ptr<Binary> Binary(BinaryOrErr.get());
40 // TODO: If this is an archive, then burst it and dump each entry
41 if (ObjectFile *Obj = dyn_cast<ObjectFile>(Binary.get()))
42 return dumpObject(*Obj);
43
44 return obj2yaml_error::unrecognized_file_format;
45}
Marshall Clowc57b8882012-06-19 18:02:35 +000046
Chandler Carruth724a7b12013-04-08 08:39:59 +000047cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"),
48 cl::init("-"));
49
50int main(int argc, char *argv[]) {
Marshall Clowc57b8882012-06-19 18:02:35 +000051 cl::ParseCommandLineOptions(argc, argv);
52 sys::PrintStackTraceOnErrorSignal();
53 PrettyStackTraceProgram X(argc, argv);
Chandler Carruth724a7b12013-04-08 08:39:59 +000054 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Marshall Clowc57b8882012-06-19 18:02:35 +000055
Stephen Hinescd81d942014-07-21 00:45:20 -070056 if (std::error_code EC = dumpInput(InputFilename)) {
Stephen Hinesdce4a402014-05-29 02:49:00 -070057 errs() << "Error: '" << EC.message() << "'\n";
58 return 1;
Marshall Clowc57b8882012-06-19 18:02:35 +000059 }
60
61 return 0;
62}