• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
10 #include "Error.h"
11 #include "obj2yaml.h"
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"
18 
19 using namespace llvm;
20 using namespace llvm::object;
21 
dumpObject(const ObjectFile & Obj)22 static std::error_code dumpObject(const ObjectFile &Obj) {
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;
29 }
30 
dumpInput(StringRef File)31 static std::error_code dumpInput(StringRef File) {
32   Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
33   if (!BinaryOrErr)
34     return errorToErrorCode(BinaryOrErr.takeError());
35 
36   Binary &Binary = *BinaryOrErr.get().getBinary();
37   // Universal MachO is not a subclass of ObjectFile, so it needs to be handled
38   // here with the other binary types.
39   if (Binary.isMachO() || Binary.isMachOUniversalBinary())
40     return macho2yaml(outs(), Binary);
41   // TODO: If this is an archive, then burst it and dump each entry
42   if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
43     return dumpObject(*Obj);
44 
45   return obj2yaml_error::unrecognized_file_format;
46 }
47 
48 cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"),
49                                    cl::init("-"));
50 
main(int argc,char * argv[])51 int main(int argc, char *argv[]) {
52   cl::ParseCommandLineOptions(argc, argv);
53   sys::PrintStackTraceOnErrorSignal(argv[0]);
54   PrettyStackTraceProgram X(argc, argv);
55   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
56 
57   if (std::error_code EC = dumpInput(InputFilename)) {
58     errs() << "Error: '" << EC.message() << "'\n";
59     return 1;
60   }
61 
62   return 0;
63 }
64