[llvm] [cmake] Add possibility to use ChooseMSVCCRT.cmake when include LLVM library
[llvm-core.git] / tools / obj2yaml / obj2yaml.cpp
blobf03b1ef4bade38b08dc41fe1648ed214525d9aed
1 //===------ utils/obj2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "obj2yaml.h"
10 #include "Error.h"
11 #include "llvm/Object/Archive.h"
12 #include "llvm/Object/COFF.h"
13 #include "llvm/Object/Minidump.h"
14 #include "llvm/Support/CommandLine.h"
15 #include "llvm/Support/InitLLVM.h"
17 using namespace llvm;
18 using namespace llvm::object;
20 static Error dumpObject(const ObjectFile &Obj) {
21 if (Obj.isCOFF())
22 return errorCodeToError(coff2yaml(outs(), cast<COFFObjectFile>(Obj)));
24 if (Obj.isXCOFF())
25 return errorCodeToError(xcoff2yaml(outs(), cast<XCOFFObjectFile>(Obj)));
27 if (Obj.isELF())
28 return elf2yaml(outs(), Obj);
30 if (Obj.isWasm())
31 return errorCodeToError(wasm2yaml(outs(), cast<WasmObjectFile>(Obj)));
33 return errorCodeToError(obj2yaml_error::unsupported_obj_file_format);
36 static Error dumpInput(StringRef File) {
37 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
38 if (!BinaryOrErr)
39 return BinaryOrErr.takeError();
41 Binary &Binary = *BinaryOrErr.get().getBinary();
42 // Universal MachO is not a subclass of ObjectFile, so it needs to be handled
43 // here with the other binary types.
44 if (Binary.isMachO() || Binary.isMachOUniversalBinary())
45 return errorCodeToError(macho2yaml(outs(), Binary));
46 // TODO: If this is an archive, then burst it and dump each entry
47 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
48 return dumpObject(*Obj);
49 if (MinidumpFile *Minidump = dyn_cast<MinidumpFile>(&Binary))
50 return minidump2yaml(outs(), *Minidump);
52 return Error::success();
55 static void reportError(StringRef Input, Error Err) {
56 if (Input == "-")
57 Input = "<stdin>";
58 std::string ErrMsg;
59 raw_string_ostream OS(ErrMsg);
60 logAllUnhandledErrors(std::move(Err), OS);
61 OS.flush();
62 errs() << "Error reading file: " << Input << ": " << ErrMsg;
63 errs().flush();
66 cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"),
67 cl::init("-"));
69 int main(int argc, char *argv[]) {
70 InitLLVM X(argc, argv);
71 cl::ParseCommandLineOptions(argc, argv);
73 if (Error Err = dumpInput(InputFilename)) {
74 reportError(InputFilename, std::move(Err));
75 return 1;
78 return 0;