1//===------ utils/obj2yaml.cpp - obj2yaml conversion tool -----------------===//
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//===----------------------------------------------------------------------===//
8
9#include "obj2yaml.h"
10#include "llvm/BinaryFormat/Magic.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/Errc.h"
16#include "llvm/Support/InitLLVM.h"
17#include "llvm/Support/ToolOutputFile.h"
18#include "llvm/Support/WithColor.h"
19
20using namespace llvm;
21using namespace llvm::object;
22
23static cl::OptionCategory Cat("obj2yaml Options");
24
25static cl::opt<std::string>
26 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init(Val: "-"));
27static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
28 cl::value_desc("filename"),
29 cl::init(Val: "-"), cl::Prefix,
30 cl::cat(Cat));
31static cl::bits<RawSegments> RawSegment(
32 "raw-segment",
33 cl::desc("Mach-O: dump the raw contents of the listed segments instead of "
34 "parsing them:"),
35 cl::values(clEnumVal(data, "__DATA"), clEnumVal(linkedit, "__LINKEDIT")),
36 cl::cat(Cat));
37
38static Error dumpObject(const ObjectFile &Obj, raw_ostream &OS) {
39 if (Obj.isCOFF())
40 return errorCodeToError(EC: coff2yaml(Out&: OS, Obj: cast<COFFObjectFile>(Val: Obj)));
41
42 if (Obj.isXCOFF())
43 return xcoff2yaml(Out&: OS, Obj: cast<XCOFFObjectFile>(Val: Obj));
44
45 if (Obj.isELF())
46 return elf2yaml(Out&: OS, Obj);
47
48 if (Obj.isGOFF())
49 return goff2yaml(Out&: OS, Obj: cast<GOFFObjectFile>(Val: Obj));
50
51 if (Obj.isWasm())
52 return errorCodeToError(EC: wasm2yaml(Out&: OS, Obj: cast<WasmObjectFile>(Val: Obj)));
53
54 llvm_unreachable("unexpected object file format");
55}
56
57static Error dumpInput(StringRef File, raw_ostream &OS) {
58 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
59 MemoryBuffer::getFileOrSTDIN(Filename: File, /*IsText=*/false,
60 /*RequiresNullTerminator=*/false);
61 if (std::error_code EC = FileOrErr.getError())
62 return errorCodeToError(EC);
63 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
64 MemoryBufferRef MemBuf = Buffer->getMemBufferRef();
65 switch (identify_magic(magic: MemBuf.getBuffer())) {
66 case file_magic::archive:
67 return archive2yaml(Out&: OS, Source: MemBuf);
68 case file_magic::dxcontainer_object:
69 return dxcontainer2yaml(Out&: OS, Source: MemBuf);
70 case file_magic::offload_binary:
71 return offload2yaml(Out&: OS, Source: MemBuf);
72 default:
73 break;
74 }
75
76 Expected<std::unique_ptr<Binary>> BinOrErr =
77 createBinary(Source: MemBuf, /*Context=*/nullptr);
78 if (!BinOrErr)
79 return BinOrErr.takeError();
80
81 Binary &Binary = *BinOrErr->get();
82 // Universal MachO is not a subclass of ObjectFile, so it needs to be handled
83 // here with the other binary types.
84 if (Binary.isMachO() || Binary.isMachOUniversalBinary())
85 return macho2yaml(Out&: OS, Obj: Binary, RawSegments: RawSegment.getBits());
86 if (ObjectFile *Obj = dyn_cast<ObjectFile>(Val: &Binary))
87 return dumpObject(Obj: *Obj, OS);
88 if (MinidumpFile *Minidump = dyn_cast<MinidumpFile>(Val: &Binary))
89 return minidump2yaml(Out&: OS, Obj: *Minidump);
90
91 return Error::success();
92}
93
94static void reportError(StringRef Input, Error Err) {
95 if (Input == "-")
96 Input = "<stdin>";
97 std::string ErrMsg;
98 raw_string_ostream OS(ErrMsg);
99 logAllUnhandledErrors(E: std::move(Err), OS);
100 errs() << "Error reading file: " << Input << ": " << ErrMsg;
101 errs().flush();
102}
103
104int main(int argc, char *argv[]) {
105 InitLLVM X(argc, argv);
106 cl::HideUnrelatedOptions(Category&: Cat);
107 cl::ParseCommandLineOptions(
108 argc, argv, Overview: "Dump a YAML description from an object file", Errs: nullptr,
109 VFS: nullptr, EnvVar: nullptr, /*LongOptionsUseDoubleDash=*/true);
110
111 std::error_code EC;
112 std::unique_ptr<ToolOutputFile> Out(
113 new ToolOutputFile(OutputFilename, EC, sys::fs::OF_Text));
114 if (EC) {
115 WithColor::error(OS&: errs(), Prefix: "obj2yaml")
116 << "failed to open '" + OutputFilename + "': " + EC.message() << '\n';
117 return 1;
118 }
119 if (Error Err = dumpInput(File: InputFilename, OS&: Out->os())) {
120 reportError(Input: InputFilename, Err: std::move(Err));
121 return 1;
122 }
123 Out->keep();
124
125 return 0;
126}
127