| Marshall Clow | c57b888 | 2012-06-19 18:02:35 +0000 | [diff] [blame] | 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 | |
| Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 10 | #include "Error.h" |
| Marshall Clow | c57b888 | 2012-06-19 18:02:35 +0000 | [diff] [blame] | 11 | #include "obj2yaml.h" |
| Chandler Carruth | 4ffd89f | 2012-12-04 10:37:14 +0000 | [diff] [blame] | 12 | #include "llvm/Object/Archive.h" |
| 13 | #include "llvm/Object/COFF.h" |
| Marshall Clow | c57b888 | 2012-06-19 18:02:35 +0000 | [diff] [blame] | 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 | |
| Chandler Carruth | 97034bb | 2013-04-08 08:55:14 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 20 | using namespace llvm::object; |
| Chandler Carruth | 97034bb | 2013-04-08 08:55:14 +0000 | [diff] [blame] | 21 | |
| Stephen Hines | cd81d94 | 2014-07-21 00:45:20 -0700 | [diff] [blame^] | 22 | static std::error_code dumpObject(const ObjectFile &Obj) { |
| Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 23 | 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 Carruth | 97034bb | 2013-04-08 08:55:14 +0000 | [diff] [blame] | 29 | } |
| Marshall Clow | c57b888 | 2012-06-19 18:02:35 +0000 | [diff] [blame] | 30 | |
| Stephen Hines | cd81d94 | 2014-07-21 00:45:20 -0700 | [diff] [blame^] | 31 | static std::error_code dumpInput(StringRef File) { |
| Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 32 | if (File != "-" && !sys::fs::exists(File)) |
| 33 | return obj2yaml_error::file_not_found; |
| 34 | |
| 35 | ErrorOr<Binary *> BinaryOrErr = createBinary(File); |
| Stephen Hines | cd81d94 | 2014-07-21 00:45:20 -0700 | [diff] [blame^] | 36 | if (std::error_code EC = BinaryOrErr.getError()) |
| Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 37 | 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 Clow | c57b888 | 2012-06-19 18:02:35 +0000 | [diff] [blame] | 46 | |
| Chandler Carruth | 724a7b1 | 2013-04-08 08:39:59 +0000 | [diff] [blame] | 47 | cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"), |
| 48 | cl::init("-")); |
| 49 | |
| 50 | int main(int argc, char *argv[]) { |
| Marshall Clow | c57b888 | 2012-06-19 18:02:35 +0000 | [diff] [blame] | 51 | cl::ParseCommandLineOptions(argc, argv); |
| 52 | sys::PrintStackTraceOnErrorSignal(); |
| 53 | PrettyStackTraceProgram X(argc, argv); |
| Chandler Carruth | 724a7b1 | 2013-04-08 08:39:59 +0000 | [diff] [blame] | 54 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| Marshall Clow | c57b888 | 2012-06-19 18:02:35 +0000 | [diff] [blame] | 55 | |
| Stephen Hines | cd81d94 | 2014-07-21 00:45:20 -0700 | [diff] [blame^] | 56 | if (std::error_code EC = dumpInput(InputFilename)) { |
| Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 57 | errs() << "Error: '" << EC.message() << "'\n"; |
| 58 | return 1; |
| Marshall Clow | c57b888 | 2012-06-19 18:02:35 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | return 0; |
| 62 | } |