1 //===------ utils/obj2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===// 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 "Error.h" 11 #include "llvm/Object/Archive.h" 12 #include "llvm/Object/COFF.h" 13 #include "llvm/Support/CommandLine.h" 14 #include "llvm/Support/InitLLVM.h" 15 16 using namespace llvm; 17 using namespace llvm::object; 18 19 static std::error_code dumpObject(const ObjectFile &Obj) { 20 if (Obj.isCOFF()) 21 return coff2yaml(outs(), cast<COFFObjectFile>(Obj)); 22 if (Obj.isELF()) 23 return elf2yaml(outs(), Obj); 24 if (Obj.isWasm()) 25 return wasm2yaml(outs(), cast<WasmObjectFile>(Obj)); 26 27 return obj2yaml_error::unsupported_obj_file_format; 28 } 29 30 static Error dumpInput(StringRef File) { 31 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File); 32 if (!BinaryOrErr) 33 return BinaryOrErr.takeError(); 34 35 Binary &Binary = *BinaryOrErr.get().getBinary(); 36 // Universal MachO is not a subclass of ObjectFile, so it needs to be handled 37 // here with the other binary types. 38 if (Binary.isMachO() || Binary.isMachOUniversalBinary()) 39 return errorCodeToError(macho2yaml(outs(), Binary)); 40 // TODO: If this is an archive, then burst it and dump each entry 41 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary)) 42 return errorCodeToError(dumpObject(*Obj)); 43 44 return Error::success(); 45 } 46 47 static void reportError(StringRef Input, Error Err) { 48 if (Input == "-") 49 Input = "<stdin>"; 50 std::string ErrMsg; 51 raw_string_ostream OS(ErrMsg); 52 logAllUnhandledErrors(std::move(Err), OS); 53 OS.flush(); 54 errs() << "Error reading file: " << Input << ": " << ErrMsg; 55 errs().flush(); 56 } 57 58 cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"), 59 cl::init("-")); 60 61 int main(int argc, char *argv[]) { 62 InitLLVM X(argc, argv); 63 cl::ParseCommandLineOptions(argc, argv); 64 65 if (Error Err = dumpInput(InputFilename)) { 66 reportError(InputFilename, std::move(Err)); 67 return 1; 68 } 69 70 return 0; 71 } 72