X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=tools%2Fobj2yaml%2Fobj2yaml.cpp;h=ee6284da6e417229aabb99a849e8b820067319e6;hb=9f25a0678ed9f06088a09649a040a6bef362e6af;hp=615ff863b1fdefa2128069bd3c3d850f6925225e;hpb=724a7b145d480018ddae894ffdd848154cd5478f;p=oota-llvm.git diff --git a/tools/obj2yaml/obj2yaml.cpp b/tools/obj2yaml/obj2yaml.cpp index 615ff863b1f..ee6284da6e4 100644 --- a/tools/obj2yaml/obj2yaml.cpp +++ b/tools/obj2yaml/obj2yaml.cpp @@ -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" @@ -16,48 +16,30 @@ #include "llvm/Support/PrettyStackTrace.h" #include "llvm/Support/Signals.h" -namespace yaml { // generic yaml-writing specific routines - -unsigned char printable(unsigned char Ch) { - return Ch >= ' ' && Ch <= '~' ? Ch : '.'; -} - -llvm::raw_ostream &writeHexStream(llvm::raw_ostream &Out, - const llvm::ArrayRef arr) { - const char *hex = "0123456789ABCDEF"; - Out << " !hex \""; - - typedef llvm::ArrayRef::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)]; +using namespace llvm; +using namespace llvm::object; - Out << "\" # |"; - for (iter_t iter = arr.begin(); iter != end; ++iter) - Out << printable(*iter); - Out << "|\n"; +static std::error_code dumpObject(const ObjectFile &Obj) { + if (Obj.isCOFF()) + return coff2yaml(outs(), cast(Obj)); + if (Obj.isELF()) + return elf2yaml(outs(), Obj); - return Out; + return obj2yaml_error::unsupported_obj_file_format; } -llvm::raw_ostream &writeHexNumber(llvm::raw_ostream &Out, - unsigned long long N) { - if (N >= 10) - Out << "0x"; - Out.write_hex(N); - return Out; -} +static std::error_code dumpInput(StringRef File) { + ErrorOr> BinaryOrErr = createBinary(File); + if (std::error_code EC = BinaryOrErr.getError()) + return EC; -} - -using namespace llvm; -enum ObjectFileType { - coff -}; + Binary &Binary = *BinaryOrErr.get().getBinary(); + // TODO: If this is an archive, then burst it and dump each entry + if (ObjectFile *Obj = dyn_cast(&Binary)) + return dumpObject(*Obj); -cl::opt InputFormat( - cl::desc("Choose input format"), - cl::values(clEnumVal(coff, "process COFF object files"), clEnumValEnd)); + return obj2yaml_error::unrecognized_file_format; +} cl::opt InputFilename(cl::Positional, cl::desc(""), cl::init("-")); @@ -68,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 - OwningPtr buf; - - // 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 << "'\n"; - } else { - ec = coff2yaml(llvm::outs(), buf.take()); - if (ec) - llvm::errs() << "Error: " << ec.message() << " dumping COFF file\n"; + if (std::error_code EC = dumpInput(InputFilename)) { + errs() << "Error: '" << EC.message() << "'\n"; + return 1; } return 0;