| 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 | |
| 20 | using namespace llvm; |
| 21 | using namespace llvm::object; |
| 22 | |
| 23 | static cl::OptionCategory Cat("obj2yaml Options" ); |
| 24 | |
| 25 | static cl::opt<std::string> |
| 26 | InputFilename(cl::Positional, cl::desc("<input file>" ), cl::init(Val: "-" )); |
| 27 | static 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)); |
| 31 | static 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 | |
| 38 | static 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.isWasm()) |
| 49 | return errorCodeToError(EC: wasm2yaml(Out&: OS, Obj: cast<WasmObjectFile>(Val: Obj))); |
| 50 | |
| 51 | llvm_unreachable("unexpected object file format" ); |
| 52 | } |
| 53 | |
| 54 | static Error dumpInput(StringRef File, raw_ostream &OS) { |
| 55 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = |
| 56 | MemoryBuffer::getFileOrSTDIN(Filename: File, /*IsText=*/false, |
| 57 | /*RequiresNullTerminator=*/false); |
| 58 | if (std::error_code EC = FileOrErr.getError()) |
| 59 | return errorCodeToError(EC); |
| 60 | std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get(); |
| 61 | MemoryBufferRef MemBuf = Buffer->getMemBufferRef(); |
| 62 | switch (identify_magic(magic: MemBuf.getBuffer())) { |
| 63 | case file_magic::archive: |
| 64 | return archive2yaml(Out&: OS, Source: MemBuf); |
| 65 | case file_magic::dxcontainer_object: |
| 66 | return dxcontainer2yaml(Out&: OS, Source: MemBuf); |
| 67 | case file_magic::offload_binary: |
| 68 | return offload2yaml(Out&: OS, Source: MemBuf); |
| 69 | default: |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | Expected<std::unique_ptr<Binary>> BinOrErr = |
| 74 | createBinary(Source: MemBuf, /*Context=*/nullptr); |
| 75 | if (!BinOrErr) |
| 76 | return BinOrErr.takeError(); |
| 77 | |
| 78 | Binary &Binary = *BinOrErr->get(); |
| 79 | // Universal MachO is not a subclass of ObjectFile, so it needs to be handled |
| 80 | // here with the other binary types. |
| 81 | if (Binary.isMachO() || Binary.isMachOUniversalBinary()) |
| 82 | return macho2yaml(Out&: OS, Obj: Binary, RawSegments: RawSegment.getBits()); |
| 83 | if (ObjectFile *Obj = dyn_cast<ObjectFile>(Val: &Binary)) |
| 84 | return dumpObject(Obj: *Obj, OS); |
| 85 | if (MinidumpFile *Minidump = dyn_cast<MinidumpFile>(Val: &Binary)) |
| 86 | return minidump2yaml(Out&: OS, Obj: *Minidump); |
| 87 | |
| 88 | return Error::success(); |
| 89 | } |
| 90 | |
| 91 | static void reportError(StringRef Input, Error Err) { |
| 92 | if (Input == "-" ) |
| 93 | Input = "<stdin>" ; |
| 94 | std::string ErrMsg; |
| 95 | raw_string_ostream OS(ErrMsg); |
| 96 | logAllUnhandledErrors(E: std::move(Err), OS); |
| 97 | OS.flush(); |
| 98 | errs() << "Error reading file: " << Input << ": " << ErrMsg; |
| 99 | errs().flush(); |
| 100 | } |
| 101 | |
| 102 | int main(int argc, char *argv[]) { |
| 103 | InitLLVM X(argc, argv); |
| 104 | cl::HideUnrelatedOptions(Category&: Cat); |
| 105 | cl::ParseCommandLineOptions( |
| 106 | argc, argv, Overview: "Dump a YAML description from an object file" , Errs: nullptr, |
| 107 | EnvVar: nullptr, /*LongOptionsUseDoubleDash=*/true); |
| 108 | |
| 109 | std::error_code EC; |
| 110 | std::unique_ptr<ToolOutputFile> Out( |
| 111 | new ToolOutputFile(OutputFilename, EC, sys::fs::OF_Text)); |
| 112 | if (EC) { |
| 113 | WithColor::error(OS&: errs(), Prefix: "obj2yaml" ) |
| 114 | << "failed to open '" + OutputFilename + "': " + EC.message() << '\n'; |
| 115 | return 1; |
| 116 | } |
| 117 | if (Error Err = dumpInput(File: InputFilename, OS&: Out->os())) { |
| 118 | reportError(Input: InputFilename, Err: std::move(Err)); |
| 119 | return 1; |
| 120 | } |
| 121 | Out->keep(); |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |