obj2yaml.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #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. #include "llvm/Support/ToolOutputFile.h"
  17. #include "llvm/Support/WithColor.h"
  18. using namespace llvm;
  19. using namespace llvm::object;
  20. static cl::OptionCategory Cat("obj2yaml Options");
  21. static cl::opt<std::string>
  22. InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
  23. static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
  24. cl::value_desc("filename"),
  25. cl::init("-"), cl::Prefix,
  26. cl::cat(Cat));
  27. static cl::bits<RawSegments> RawSegment(
  28. "raw-segment",
  29. cl::desc("Mach-O: dump the raw contents of the listed segments instead of "
  30. "parsing them:"),
  31. cl::values(clEnumVal(data, "__DATA"), clEnumVal(linkedit, "__LINKEDIT")),
  32. cl::cat(Cat));
  33. static Error dumpObject(const ObjectFile &Obj, raw_ostream &OS) {
  34. if (Obj.isCOFF())
  35. return errorCodeToError(coff2yaml(OS, cast<COFFObjectFile>(Obj)));
  36. if (Obj.isXCOFF())
  37. return xcoff2yaml(OS, cast<XCOFFObjectFile>(Obj));
  38. if (Obj.isELF())
  39. return elf2yaml(OS, Obj);
  40. if (Obj.isWasm())
  41. return errorCodeToError(wasm2yaml(OS, cast<WasmObjectFile>(Obj)));
  42. llvm_unreachable("unexpected object file format");
  43. }
  44. static Error dumpInput(StringRef File, raw_ostream &OS) {
  45. ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
  46. MemoryBuffer::getFileOrSTDIN(File, /*IsText=*/false,
  47. /*RequiresNullTerminator=*/false);
  48. if (std::error_code EC = FileOrErr.getError())
  49. return errorCodeToError(EC);
  50. std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
  51. MemoryBufferRef MemBuf = Buffer->getMemBufferRef();
  52. switch (identify_magic(MemBuf.getBuffer())) {
  53. case file_magic::archive:
  54. return archive2yaml(OS, MemBuf);
  55. case file_magic::dxcontainer_object:
  56. return dxcontainer2yaml(OS, MemBuf);
  57. case file_magic::offload_binary:
  58. return offload2yaml(OS, MemBuf);
  59. default:
  60. break;
  61. }
  62. Expected<std::unique_ptr<Binary>> BinOrErr =
  63. createBinary(MemBuf, /*Context=*/nullptr);
  64. if (!BinOrErr)
  65. return BinOrErr.takeError();
  66. Binary &Binary = *BinOrErr->get();
  67. // Universal MachO is not a subclass of ObjectFile, so it needs to be handled
  68. // here with the other binary types.
  69. if (Binary.isMachO() || Binary.isMachOUniversalBinary())
  70. return macho2yaml(OS, Binary, RawSegment.getBits());
  71. if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
  72. return dumpObject(*Obj, OS);
  73. if (MinidumpFile *Minidump = dyn_cast<MinidumpFile>(&Binary))
  74. return minidump2yaml(OS, *Minidump);
  75. return Error::success();
  76. }
  77. static void reportError(StringRef Input, Error Err) {
  78. if (Input == "-")
  79. Input = "<stdin>";
  80. std::string ErrMsg;
  81. raw_string_ostream OS(ErrMsg);
  82. logAllUnhandledErrors(std::move(Err), OS);
  83. OS.flush();
  84. errs() << "Error reading file: " << Input << ": " << ErrMsg;
  85. errs().flush();
  86. }
  87. int main(int argc, char *argv[]) {
  88. InitLLVM X(argc, argv);
  89. cl::HideUnrelatedOptions(Cat);
  90. cl::ParseCommandLineOptions(
  91. argc, argv, "Dump a YAML description from an object file", nullptr,
  92. nullptr, /*LongOptionsUseDoubleDash=*/true);
  93. std::error_code EC;
  94. std::unique_ptr<ToolOutputFile> Out(
  95. new ToolOutputFile(OutputFilename, EC, sys::fs::OF_Text));
  96. if (EC) {
  97. WithColor::error(errs(), "obj2yaml")
  98. << "failed to open '" + OutputFilename + "': " + EC.message() << '\n';
  99. return 1;
  100. }
  101. if (Error Err = dumpInput(InputFilename, Out->os())) {
  102. reportError(InputFilename, std::move(Err));
  103. return 1;
  104. }
  105. Out->keep();
  106. return 0;
  107. }