2012-06-20 02:02:35 +08:00
|
|
|
//===------ utils/obj2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-05-07 13:18:51 +08:00
|
|
|
#include "Error.h"
|
2012-06-20 02:02:35 +08:00
|
|
|
#include "obj2yaml.h"
|
2012-12-04 18:37:14 +08:00
|
|
|
#include "llvm/Object/Archive.h"
|
|
|
|
#include "llvm/Object/COFF.h"
|
2012-06-20 02:02:35 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
|
|
|
|
2013-04-08 16:55:14 +08:00
|
|
|
using namespace llvm;
|
2014-05-07 13:18:51 +08:00
|
|
|
using namespace llvm::object;
|
2013-04-08 16:55:14 +08:00
|
|
|
|
2014-06-13 11:07:50 +08:00
|
|
|
static std::error_code dumpObject(const ObjectFile &Obj) {
|
2014-05-07 13:18:51 +08:00
|
|
|
if (Obj.isCOFF())
|
|
|
|
return coff2yaml(outs(), cast<COFFObjectFile>(Obj));
|
2014-05-14 13:07:47 +08:00
|
|
|
if (Obj.isELF())
|
|
|
|
return elf2yaml(outs(), Obj);
|
2014-05-07 13:18:51 +08:00
|
|
|
|
|
|
|
return obj2yaml_error::unsupported_obj_file_format;
|
2013-04-08 16:55:14 +08:00
|
|
|
}
|
2012-06-20 02:02:35 +08:00
|
|
|
|
2014-06-13 11:07:50 +08:00
|
|
|
static std::error_code dumpInput(StringRef File) {
|
2016-04-07 06:14:09 +08:00
|
|
|
Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
|
|
|
|
if (!BinaryOrErr)
|
|
|
|
return errorToErrorCode(BinaryOrErr.takeError());
|
2014-05-07 13:18:51 +08:00
|
|
|
|
2014-08-20 02:44:46 +08:00
|
|
|
Binary &Binary = *BinaryOrErr.get().getBinary();
|
2016-06-25 04:42:28 +08:00
|
|
|
// Universal MachO is not a subclass of ObjectFile, so it needs to be handled
|
|
|
|
// here with the other binary types.
|
|
|
|
if (Binary.isMachO() || Binary.isMachOUniversalBinary())
|
|
|
|
return macho2yaml(outs(), Binary);
|
2014-05-07 13:18:51 +08:00
|
|
|
// TODO: If this is an archive, then burst it and dump each entry
|
2014-08-01 22:31:55 +08:00
|
|
|
if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
|
2014-05-07 13:18:51 +08:00
|
|
|
return dumpObject(*Obj);
|
|
|
|
|
|
|
|
return obj2yaml_error::unrecognized_file_format;
|
|
|
|
}
|
2013-04-08 16:39:59 +08:00
|
|
|
|
|
|
|
cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"),
|
|
|
|
cl::init("-"));
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2012-06-20 02:02:35 +08:00
|
|
|
cl::ParseCommandLineOptions(argc, argv);
|
2016-06-09 08:53:21 +08:00
|
|
|
sys::PrintStackTraceOnErrorSignal(argv[0]);
|
2012-06-20 02:02:35 +08:00
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
2013-04-08 16:39:59 +08:00
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
2012-06-20 02:02:35 +08:00
|
|
|
|
2014-06-13 11:07:50 +08:00
|
|
|
if (std::error_code EC = dumpInput(InputFilename)) {
|
2014-05-07 13:18:51 +08:00
|
|
|
errs() << "Error: '" << EC.message() << "'\n";
|
|
|
|
return 1;
|
2012-06-20 02:02:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|