obj2yaml.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include "obj2yaml.h"
  9. #include "llvm/BinaryFormat/Magic.h"
  10. #include "llvm/Object/Archive.h"
  11. #include "llvm/Object/COFF.h"
  12. #include "llvm/Object/Minidump.h"
  13. #include "llvm/Support/CommandLine.h"
  14. #include "llvm/Support/Errc.h"
  15. #include "llvm/Support/InitLLVM.h"
  16. using namespace llvm;
  17. using namespace llvm::object;
  18. static Error dumpObject(const ObjectFile &Obj) {
  19. if (Obj.isCOFF())
  20. return errorCodeToError(coff2yaml(outs(), cast<COFFObjectFile>(Obj)));
  21. if (Obj.isXCOFF())
  22. return errorCodeToError(xcoff2yaml(outs(), cast<XCOFFObjectFile>(Obj)));
  23. if (Obj.isELF())
  24. return elf2yaml(outs(), Obj);
  25. if (Obj.isWasm())
  26. return errorCodeToError(wasm2yaml(outs(), cast<WasmObjectFile>(Obj)));
  27. llvm_unreachable("unexpected object file format");
  28. }
  29. static Error dumpInput(StringRef File) {
  30. ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
  31. MemoryBuffer::getFileOrSTDIN(File, /*FileSize=*/-1,
  32. /*RequiresNullTerminator=*/false);
  33. if (std::error_code EC = FileOrErr.getError())
  34. return errorCodeToError(EC);
  35. std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
  36. MemoryBufferRef MemBuf = Buffer->getMemBufferRef();
  37. if (file_magic::archive == identify_magic(MemBuf.getBuffer()))
  38. return archive2yaml(outs(), MemBuf);
  39. Expected<std::unique_ptr<Binary>> BinOrErr =
  40. createBinary(MemBuf, /*Context=*/nullptr);
  41. if (!BinOrErr)
  42. return BinOrErr.takeError();
  43. Binary &Binary = *BinOrErr->get();
  44. // Universal MachO is not a subclass of ObjectFile, so it needs to be handled
  45. // here with the other binary types.
  46. if (Binary.isMachO() || Binary.isMachOUniversalBinary())
  47. return macho2yaml(outs(), Binary);
  48. if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
  49. return dumpObject(*Obj);
  50. if (MinidumpFile *Minidump = dyn_cast<MinidumpFile>(&Binary))
  51. return minidump2yaml(outs(), *Minidump);
  52. return Error::success();
  53. }
  54. static void reportError(StringRef Input, Error Err) {
  55. if (Input == "-")
  56. Input = "<stdin>";
  57. std::string ErrMsg;
  58. raw_string_ostream OS(ErrMsg);
  59. logAllUnhandledErrors(std::move(Err), OS);
  60. OS.flush();
  61. errs() << "Error reading file: " << Input << ": " << ErrMsg;
  62. errs().flush();
  63. }
  64. cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"),
  65. cl::init("-"));
  66. int main(int argc, char *argv[]) {
  67. InitLLVM X(argc, argv);
  68. cl::ParseCommandLineOptions(argc, argv);
  69. if (Error Err = dumpInput(InputFilename)) {
  70. reportError(InputFilename, std::move(Err));
  71. return 1;
  72. }
  73. return 0;
  74. }