obj2yaml.cpp 3.1 KB

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