17330f729Sjoerg //===------ utils/obj2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg
97330f729Sjoerg #include "obj2yaml.h"
10*82d56013Sjoerg #include "llvm/BinaryFormat/Magic.h"
117330f729Sjoerg #include "llvm/Object/Archive.h"
127330f729Sjoerg #include "llvm/Object/COFF.h"
137330f729Sjoerg #include "llvm/Object/Minidump.h"
147330f729Sjoerg #include "llvm/Support/CommandLine.h"
15*82d56013Sjoerg #include "llvm/Support/Errc.h"
167330f729Sjoerg #include "llvm/Support/InitLLVM.h"
177330f729Sjoerg
187330f729Sjoerg using namespace llvm;
197330f729Sjoerg using namespace llvm::object;
207330f729Sjoerg
dumpObject(const ObjectFile & Obj)217330f729Sjoerg static Error dumpObject(const ObjectFile &Obj) {
227330f729Sjoerg if (Obj.isCOFF())
237330f729Sjoerg return errorCodeToError(coff2yaml(outs(), cast<COFFObjectFile>(Obj)));
247330f729Sjoerg
257330f729Sjoerg if (Obj.isXCOFF())
267330f729Sjoerg return errorCodeToError(xcoff2yaml(outs(), cast<XCOFFObjectFile>(Obj)));
277330f729Sjoerg
287330f729Sjoerg if (Obj.isELF())
297330f729Sjoerg return elf2yaml(outs(), Obj);
307330f729Sjoerg
317330f729Sjoerg if (Obj.isWasm())
327330f729Sjoerg return errorCodeToError(wasm2yaml(outs(), cast<WasmObjectFile>(Obj)));
337330f729Sjoerg
34*82d56013Sjoerg llvm_unreachable("unexpected object file format");
357330f729Sjoerg }
367330f729Sjoerg
dumpInput(StringRef File)377330f729Sjoerg static Error dumpInput(StringRef File) {
38*82d56013Sjoerg ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
39*82d56013Sjoerg MemoryBuffer::getFileOrSTDIN(File, /*IsText=*/false,
40*82d56013Sjoerg /*RequiresNullTerminator=*/false);
41*82d56013Sjoerg if (std::error_code EC = FileOrErr.getError())
42*82d56013Sjoerg return errorCodeToError(EC);
43*82d56013Sjoerg std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
44*82d56013Sjoerg MemoryBufferRef MemBuf = Buffer->getMemBufferRef();
45*82d56013Sjoerg if (file_magic::archive == identify_magic(MemBuf.getBuffer()))
46*82d56013Sjoerg return archive2yaml(outs(), MemBuf);
477330f729Sjoerg
48*82d56013Sjoerg Expected<std::unique_ptr<Binary>> BinOrErr =
49*82d56013Sjoerg createBinary(MemBuf, /*Context=*/nullptr);
50*82d56013Sjoerg if (!BinOrErr)
51*82d56013Sjoerg return BinOrErr.takeError();
52*82d56013Sjoerg
53*82d56013Sjoerg Binary &Binary = *BinOrErr->get();
547330f729Sjoerg // Universal MachO is not a subclass of ObjectFile, so it needs to be handled
557330f729Sjoerg // here with the other binary types.
567330f729Sjoerg if (Binary.isMachO() || Binary.isMachOUniversalBinary())
57*82d56013Sjoerg return macho2yaml(outs(), Binary);
587330f729Sjoerg if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
597330f729Sjoerg return dumpObject(*Obj);
607330f729Sjoerg if (MinidumpFile *Minidump = dyn_cast<MinidumpFile>(&Binary))
617330f729Sjoerg return minidump2yaml(outs(), *Minidump);
627330f729Sjoerg
637330f729Sjoerg return Error::success();
647330f729Sjoerg }
657330f729Sjoerg
reportError(StringRef Input,Error Err)667330f729Sjoerg static void reportError(StringRef Input, Error Err) {
677330f729Sjoerg if (Input == "-")
687330f729Sjoerg Input = "<stdin>";
697330f729Sjoerg std::string ErrMsg;
707330f729Sjoerg raw_string_ostream OS(ErrMsg);
717330f729Sjoerg logAllUnhandledErrors(std::move(Err), OS);
727330f729Sjoerg OS.flush();
737330f729Sjoerg errs() << "Error reading file: " << Input << ": " << ErrMsg;
747330f729Sjoerg errs().flush();
757330f729Sjoerg }
767330f729Sjoerg
777330f729Sjoerg cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"),
787330f729Sjoerg cl::init("-"));
797330f729Sjoerg
main(int argc,char * argv[])807330f729Sjoerg int main(int argc, char *argv[]) {
817330f729Sjoerg InitLLVM X(argc, argv);
827330f729Sjoerg cl::ParseCommandLineOptions(argc, argv);
837330f729Sjoerg
847330f729Sjoerg if (Error Err = dumpInput(InputFilename)) {
857330f729Sjoerg reportError(InputFilename, std::move(Err));
867330f729Sjoerg return 1;
877330f729Sjoerg }
887330f729Sjoerg
897330f729Sjoerg return 0;
907330f729Sjoerg }
91