1 //===------ utils/obj2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "llvm/Object/Archive.h"
13 #include "llvm/Object/COFF.h"
14 #include "llvm/Support/CommandLine.h"
15 #include "llvm/Support/ManagedStatic.h"
16 #include "llvm/Support/PrettyStackTrace.h"
17 #include "llvm/Support/Signals.h"
20 using namespace llvm::object
;
22 static std::error_code
dumpObject(const ObjectFile
&Obj
) {
24 return coff2yaml(outs(), cast
<COFFObjectFile
>(Obj
));
26 return elf2yaml(outs(), Obj
);
28 return wasm2yaml(outs(), cast
<WasmObjectFile
>(Obj
));
30 return obj2yaml_error::unsupported_obj_file_format
;
33 static Error
dumpInput(StringRef File
) {
34 Expected
<OwningBinary
<Binary
>> BinaryOrErr
= createBinary(File
);
36 return BinaryOrErr
.takeError();
38 Binary
&Binary
= *BinaryOrErr
.get().getBinary();
39 // Universal MachO is not a subclass of ObjectFile, so it needs to be handled
40 // here with the other binary types.
41 if (Binary
.isMachO() || Binary
.isMachOUniversalBinary())
42 return errorCodeToError(macho2yaml(outs(), Binary
));
43 // TODO: If this is an archive, then burst it and dump each entry
44 if (ObjectFile
*Obj
= dyn_cast
<ObjectFile
>(&Binary
))
45 return errorCodeToError(dumpObject(*Obj
));
47 return Error::success();
50 static void reportError(StringRef Input
, Error Err
) {
54 raw_string_ostream
OS(ErrMsg
);
55 logAllUnhandledErrors(std::move(Err
), OS
, "");
57 errs() << "Error reading file: " << Input
<< ": " << ErrMsg
;
61 cl::opt
<std::string
> InputFilename(cl::Positional
, cl::desc("<input file>"),
64 int main(int argc
, char *argv
[]) {
65 cl::ParseCommandLineOptions(argc
, argv
);
66 sys::PrintStackTraceOnErrorSignal(argv
[0]);
67 PrettyStackTraceProgram
X(argc
, argv
);
68 llvm_shutdown_obj Y
; // Call llvm_shutdown() on exit.
70 if (Error Err
= dumpInput(InputFilename
)) {
71 reportError(InputFilename
, std::move(Err
));