//
//===----------------------------------------------------------------------===//
+#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 {
-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) {
+ if (File != "-" && !sys::fs::exists(File))
+ return obj2yaml_error::file_not_found;
+
+ 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("-"));
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;