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/InitLLVM.h"
18 using namespace llvm::object
;
20 static std::error_code
dumpObject(const ObjectFile
&Obj
) {
22 return coff2yaml(outs(), cast
<COFFObjectFile
>(Obj
));
24 return elf2yaml(outs(), Obj
);
26 return wasm2yaml(outs(), cast
<WasmObjectFile
>(Obj
));
28 return obj2yaml_error::unsupported_obj_file_format
;
31 static Error
dumpInput(StringRef File
) {
32 Expected
<OwningBinary
<Binary
>> BinaryOrErr
= createBinary(File
);
34 return BinaryOrErr
.takeError();
36 Binary
&Binary
= *BinaryOrErr
.get().getBinary();
37 // Universal MachO is not a subclass of ObjectFile, so it needs to be handled
38 // here with the other binary types.
39 if (Binary
.isMachO() || Binary
.isMachOUniversalBinary())
40 return errorCodeToError(macho2yaml(outs(), Binary
));
41 // TODO: If this is an archive, then burst it and dump each entry
42 if (ObjectFile
*Obj
= dyn_cast
<ObjectFile
>(&Binary
))
43 return errorCodeToError(dumpObject(*Obj
));
45 return Error::success();
48 static void reportError(StringRef Input
, Error Err
) {
52 raw_string_ostream
OS(ErrMsg
);
53 logAllUnhandledErrors(std::move(Err
), OS
);
55 errs() << "Error reading file: " << Input
<< ": " << ErrMsg
;
59 cl::opt
<std::string
> InputFilename(cl::Positional
, cl::desc("<input file>"),
62 int main(int argc
, char *argv
[]) {
63 InitLLVM
X(argc
, argv
);
64 cl::ParseCommandLineOptions(argc
, argv
);
66 if (Error Err
= dumpInput(InputFilename
)) {
67 reportError(InputFilename
, std::move(Err
));