1 //===------ utils/obj2yaml.cpp - obj2yaml conversion tool -----------------===//
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
7 //===----------------------------------------------------------------------===//
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"
19 using namespace llvm::object
;
21 static cl::opt
<std::string
>
22 InputFilename(cl::Positional
, cl::desc("<input file>"), cl::init("-"));
23 static cl::bits
<RawSegments
> RawSegment(
25 cl::desc("Mach-O: dump the raw contents of the listed segments instead of "
27 cl::values(clEnumVal(data
, "__DATA"), clEnumVal(linkedit
, "__LINKEDIT")));
29 static Error
dumpObject(const ObjectFile
&Obj
) {
31 return errorCodeToError(coff2yaml(outs(), cast
<COFFObjectFile
>(Obj
)));
34 return xcoff2yaml(outs(), cast
<XCOFFObjectFile
>(Obj
));
37 return elf2yaml(outs(), Obj
);
40 return errorCodeToError(wasm2yaml(outs(), cast
<WasmObjectFile
>(Obj
)));
42 llvm_unreachable("unexpected object file format");
45 static Error
dumpInput(StringRef File
) {
46 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> FileOrErr
=
47 MemoryBuffer::getFileOrSTDIN(File
, /*IsText=*/false,
48 /*RequiresNullTerminator=*/false);
49 if (std::error_code EC
= FileOrErr
.getError())
50 return errorCodeToError(EC
);
51 std::unique_ptr
<MemoryBuffer
> &Buffer
= FileOrErr
.get();
52 MemoryBufferRef MemBuf
= Buffer
->getMemBufferRef();
53 if (file_magic::archive
== identify_magic(MemBuf
.getBuffer()))
54 return archive2yaml(outs(), MemBuf
);
56 Expected
<std::unique_ptr
<Binary
>> BinOrErr
=
57 createBinary(MemBuf
, /*Context=*/nullptr);
59 return BinOrErr
.takeError();
61 Binary
&Binary
= *BinOrErr
->get();
62 // Universal MachO is not a subclass of ObjectFile, so it needs to be handled
63 // here with the other binary types.
64 if (Binary
.isMachO() || Binary
.isMachOUniversalBinary())
65 return macho2yaml(outs(), Binary
, RawSegment
.getBits());
66 if (ObjectFile
*Obj
= dyn_cast
<ObjectFile
>(&Binary
))
67 return dumpObject(*Obj
);
68 if (MinidumpFile
*Minidump
= dyn_cast
<MinidumpFile
>(&Binary
))
69 return minidump2yaml(outs(), *Minidump
);
71 return Error::success();
74 static void reportError(StringRef Input
, Error Err
) {
78 raw_string_ostream
OS(ErrMsg
);
79 logAllUnhandledErrors(std::move(Err
), OS
);
81 errs() << "Error reading file: " << Input
<< ": " << ErrMsg
;
85 int main(int argc
, char *argv
[]) {
86 InitLLVM
X(argc
, argv
);
87 cl::ParseCommandLineOptions(argc
, argv
);
89 if (Error Err
= dumpInput(InputFilename
)) {
90 reportError(InputFilename
, std::move(Err
));