Don't own the buffer in object::Binary.
[oota-llvm.git] / tools / obj2yaml / obj2yaml.cpp
index bdc461a9471530c4e3e6c0b6c1aee7d12179a543..b64096d75fa4d3f0ab4ce890debc1c495d4da25f 100644 (file)
@@ -7,8 +7,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "Error.h"
 #include "obj2yaml.h"
-#include "llvm/ADT/OwningPtr.h"
 #include "llvm/Object/Archive.h"
 #include "llvm/Object/COFF.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/Signals.h"
 
-const char endl = '\n';
+using namespace llvm;
+using namespace llvm::object;
 
-namespace yaml {  // generic yaml-writing specific routines
+static std::error_code dumpObject(const ObjectFile &Obj) {
+  if (Obj.isCOFF())
+    return coff2yaml(outs(), cast<COFFObjectFile>(Obj));
+  if (Obj.isELF())
+    return elf2yaml(outs(), Obj);
 
-unsigned char printable(unsigned char Ch) {
-  return Ch >= ' ' && Ch <= '~' ? Ch : '.';
+  return obj2yaml_error::unsupported_obj_file_format;
 }
-  
-llvm::raw_ostream &writeHexStream(llvm::raw_ostream &Out, 
-                                     const llvm::ArrayRef<uint8_t> arr) {
-  const char *hex = "0123456789ABCDEF";
-  Out << " !hex \"";
-
-  typedef llvm::ArrayRef<uint8_t>::const_iterator iter_t;
-  const iter_t end = arr.end();
-  for (iter_t iter = arr.begin(); iter != end; ++iter)
-    Out << hex[(*iter >> 4) & 0x0F] << hex[(*iter & 0x0F)];
 
-  Out << "\" # |";
-  for (iter_t iter = arr.begin(); iter != end; ++iter)
-    Out << printable(*iter);
-  Out << "|" << endl;
+static std::error_code dumpInput(StringRef File) {
+  if (File != "-" && !sys::fs::exists(File))
+    return obj2yaml_error::file_not_found;
 
-  return Out;
-  }
+  ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
+  if (std::error_code EC = BinaryOrErr.getError())
+    return EC;
 
-llvm::raw_ostream &writeHexNumber(llvm::raw_ostream &Out, unsigned long long N) {
-  if (N >= 10)
-    Out << "0x";
-  Out.write_hex(N);
-  return Out;
-}
+  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("-"));
 
-using namespace llvm;
-enum ObjectFileType { coff };
-
-cl::opt<ObjectFileType> InputFormat(
-  cl::desc("Choose input format"),
-    cl::values(
-      clEnumVal(coff, "process COFF object files"),
-    clEnumValEnd));
-    
-cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
-
-int main(int argc, char * argv[]) {
+int main(int argc, char *argv[]) {
   cl::ParseCommandLineOptions(argc, argv);
   sys::PrintStackTraceOnErrorSignal();
   PrettyStackTraceProgram X(argc, argv);
-  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
-
-// Process the input file  
-  OwningPtr<MemoryBuffer> buf;
+  llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
 
-// TODO: If this is an archive, then burst it and dump each entry
-  if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, buf))
-    llvm::errs() << "Error: '" << ec.message() << "' opening file '" 
-              << InputFilename << "'" << endl;
-  else {
-    ec = coff2yaml(llvm::outs(), buf.take());
-    if (ec)
-      llvm::errs() << "Error: " << ec.message() << " dumping COFF file" << endl;
+  if (std::error_code EC = dumpInput(InputFilename)) {
+    errs() << "Error: '" << EC.message() << "'\n";
+    return 1;
   }
 
   return 0;