X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=blobdiff_plain;f=tools%2Fobj2yaml%2Fobj2yaml.cpp;h=b64096d75fa4d3f0ab4ce890debc1c495d4da25f;hp=bdc461a9471530c4e3e6c0b6c1aee7d12179a543;hb=548f2b6e8fc5499fa8c9394fe7d110f50c487802;hpb=46af06799732192ec7c8d0cc76ea151874b51129 diff --git a/tools/obj2yaml/obj2yaml.cpp b/tools/obj2yaml/obj2yaml.cpp index bdc461a9471..b64096d75fa 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,70 +16,46 @@ #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(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 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)]; - 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> 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(&Binary)) + return dumpObject(*Obj); + return obj2yaml_error::unrecognized_file_format; } +cl::opt InputFilename(cl::Positional, cl::desc(""), + cl::init("-")); -using namespace llvm; -enum ObjectFileType { coff }; - -cl::opt InputFormat( - cl::desc("Choose input format"), - cl::values( - clEnumVal(coff, "process COFF object files"), - clEnumValEnd)); - -cl::opt InputFilename(cl::Positional, cl::desc(""), 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 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;