Merging r258611:
[oota-llvm.git] / tools / obj2yaml / obj2yaml.cpp
index 38779fe45656bb310812f479a39400e7e5c325ed..ee6284da6e417229aabb99a849e8b820067319e6 100644 (file)
@@ -7,6 +7,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "Error.h"
 #include "obj2yaml.h"
 #include "llvm/Object/Archive.h"
 #include "llvm/Object/COFF.h"
 #include "llvm/Support/Signals.h"
 
 using namespace llvm;
+using namespace llvm::object;
 
-namespace {
-enum ObjectFileType {
-  coff
-};
+static std::error_code dumpObject(const ObjectFile &Obj) {
+  if (Obj.isCOFF())
+    return coff2yaml(outs(), cast<COFFObjectFile>(Obj));
+  if (Obj.isELF())
+    return elf2yaml(outs(), Obj);
+
+  return obj2yaml_error::unsupported_obj_file_format;
 }
 
-cl::opt<ObjectFileType> InputFormat(
-    cl::desc("Choose input format"),
-    cl::values(clEnumVal(coff, "process COFF object files"), clEnumValEnd));
+static std::error_code dumpInput(StringRef File) {
+  ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
+  if (std::error_code EC = BinaryOrErr.getError())
+    return EC;
+
+  Binary &Binary = *BinaryOrErr.get().getBinary();
+  // TODO: If this is an archive, then burst it and dump each entry
+  if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
+    return dumpObject(*Obj);
+
+  return obj2yaml_error::unrecognized_file_format;
+}
 
 cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"),
                                    cl::init("-"));
@@ -36,17 +50,9 @@ int main(int argc, char *argv[]) {
   PrettyStackTraceProgram X(argc, argv);
   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
 
-  // Process the input file
-  std::unique_ptr<MemoryBuffer> buf;
-
-  // TODO: If this is an archive, then burst it and dump each entry
-  if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, buf)) {
-    errs() << "Error: '" << ec.message() << "' opening file '" << InputFilename
-           << "'\n";
-  } else {
-    ec = coff2yaml(outs(), buf.release());
-    if (ec)
-      errs() << "Error: " << ec.message() << " dumping COFF file\n";
+  if (std::error_code EC = dumpInput(InputFilename)) {
+    errs() << "Error: '" << EC.message() << "'\n";
+    return 1;
   }
 
   return 0;