Object, COFF: Cleanup symbol type code, improve binutils compatibility
[oota-llvm.git] / tools / obj2yaml / obj2yaml.cpp
index 821c9ac7d3a1e35f80dfd3c98cbb015f867cc215..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/Signals.h"
 
 using namespace llvm;
+using namespace llvm::object;
 
-namespace objyaml {  // 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;
 }
 
-raw_ostream &writeHexStream(raw_ostream &Out, const ArrayRef<uint8_t> arr) {
-  const char *hex = "0123456789ABCDEF";
-  Out << " !hex \"";
+static std::error_code dumpInput(StringRef File) {
+  if (File != "-" && !sys::fs::exists(File))
+    return obj2yaml_error::file_not_found;
 
-  typedef 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)];
+  ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
+  if (std::error_code EC = BinaryOrErr.getError())
+    return EC;
 
-  Out << "\" # |";
-  for (iter_t iter = arr.begin(); iter != end; ++iter)
-    Out << printable(*iter);
-  Out << "|\n";
-
-  return Out;
-}
-
-raw_ostream &writeHexNumber(raw_ostream &Out, unsigned long long N) {
-  if (N >= 10)
-    Out << "0x";
-  Out.write_hex(N);
-  return Out;
-}
-
-} // end namespace yaml
+  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);
 
-namespace {
-enum ObjectFileType {
-  coff
-};
+  return obj2yaml_error::unrecognized_file_format;
 }
 
-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("-"));
 
@@ -69,17 +53,9 @@ int main(int argc, char *argv[]) {
   PrettyStackTraceProgram X(argc, argv);
   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
 
-  // Process the input file
-  OwningPtr<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.take());
-    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;