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 18 using namespace llvm; 19 using namespace llvm::object; 20 21 static cl::opt<std::string> 22 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-")); 23 static cl::bits<RawSegments> RawSegment( 24 "raw-segment", 25 cl::desc("Mach-O: dump the raw contents of the listed segments instead of " 26 "parsing them:"), 27 cl::values(clEnumVal(data, "__DATA"), clEnumVal(linkedit, "__LINKEDIT"))); 28 29 static Error dumpObject(const ObjectFile &Obj) { 30 if (Obj.isCOFF()) 31 return errorCodeToError(coff2yaml(outs(), cast<COFFObjectFile>(Obj))); 32 33 if (Obj.isXCOFF()) 34 return xcoff2yaml(outs(), cast<XCOFFObjectFile>(Obj)); 35 36 if (Obj.isELF()) 37 return elf2yaml(outs(), Obj); 38 39 if (Obj.isWasm()) 40 return errorCodeToError(wasm2yaml(outs(), cast<WasmObjectFile>(Obj))); 41 42 llvm_unreachable("unexpected object file format"); 43 } 44 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 switch (identify_magic(MemBuf.getBuffer())) { 54 case file_magic::archive: 55 return archive2yaml(outs(), MemBuf); 56 case file_magic::dxcontainer_object: 57 return dxcontainer2yaml(outs(), MemBuf); 58 default: 59 break; 60 } 61 62 Expected<std::unique_ptr<Binary>> BinOrErr = 63 createBinary(MemBuf, /*Context=*/nullptr); 64 if (!BinOrErr) 65 return BinOrErr.takeError(); 66 67 Binary &Binary = *BinOrErr->get(); 68 // Universal MachO is not a subclass of ObjectFile, so it needs to be handled 69 // here with the other binary types. 70 if (Binary.isMachO() || Binary.isMachOUniversalBinary()) 71 return macho2yaml(outs(), Binary, RawSegment.getBits()); 72 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary)) 73 return dumpObject(*Obj); 74 if (MinidumpFile *Minidump = dyn_cast<MinidumpFile>(&Binary)) 75 return minidump2yaml(outs(), *Minidump); 76 77 return Error::success(); 78 } 79 80 static void reportError(StringRef Input, Error Err) { 81 if (Input == "-") 82 Input = "<stdin>"; 83 std::string ErrMsg; 84 raw_string_ostream OS(ErrMsg); 85 logAllUnhandledErrors(std::move(Err), OS); 86 OS.flush(); 87 errs() << "Error reading file: " << Input << ": " << ErrMsg; 88 errs().flush(); 89 } 90 91 int main(int argc, char *argv[]) { 92 InitLLVM X(argc, argv); 93 cl::ParseCommandLineOptions(argc, argv); 94 95 if (Error Err = dumpInput(InputFilename)) { 96 reportError(InputFilename, std::move(Err)); 97 return 1; 98 } 99 100 return 0; 101 } 102