yaml2obj.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //===- yaml2obj - Convert YAML to a binary object file --------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This program takes a YAML description of an object file and outputs the
  11. // binary equivalent.
  12. //
  13. // This is used for writing tests that require binary files.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "yaml2obj.h"
  17. #include "llvm/ADT/StringExtras.h"
  18. #include "llvm/Support/CommandLine.h"
  19. #include "llvm/Support/FileSystem.h"
  20. #include "llvm/Support/ManagedStatic.h"
  21. #include "llvm/Support/MemoryBuffer.h"
  22. #include "llvm/Support/PrettyStackTrace.h"
  23. #include "llvm/Support/Signals.h"
  24. #include "llvm/Support/ToolOutputFile.h"
  25. #include "llvm/Support/YAMLTraits.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. #include <system_error>
  28. using namespace llvm;
  29. static cl::opt<std::string>
  30. Input(cl::Positional, cl::desc("<input>"), cl::init("-"));
  31. // TODO: The "right" way to tell what kind of object file a given YAML file
  32. // corresponds to is to look at YAML "tags" (e.g. `!Foo`). Then, different
  33. // tags (`!ELF`, `!COFF`, etc.) would be used to discriminate between them.
  34. // Interpreting the tags is needed eventually for when writing test cases,
  35. // so that we can e.g. have `!Archive` contain a sequence of `!ELF`, and
  36. // just Do The Right Thing. However, interpreting these tags and acting on
  37. // them appropriately requires some work in the YAML parser and the YAMLIO
  38. // library.
  39. enum YAMLObjectFormat {
  40. YOF_COFF,
  41. YOF_ELF
  42. };
  43. cl::opt<YAMLObjectFormat> Format(
  44. "format",
  45. cl::desc("Interpret input as this type of object file"),
  46. cl::values(
  47. clEnumValN(YOF_COFF, "coff", "COFF object file format"),
  48. clEnumValN(YOF_ELF, "elf", "ELF object file format"),
  49. clEnumValEnd));
  50. cl::opt<unsigned>
  51. DocNum("docnum", cl::init(1),
  52. cl::desc("Read specified document from input (default = 1)"));
  53. static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
  54. cl::value_desc("filename"));
  55. typedef int (*ConvertFuncPtr)(yaml::Input & YIn, raw_ostream &Out);
  56. static int convertYAML(yaml::Input &YIn, raw_ostream &Out,
  57. ConvertFuncPtr Convert) {
  58. unsigned CurDocNum = 0;
  59. do {
  60. if (++CurDocNum == DocNum)
  61. return Convert(YIn, Out);
  62. } while (YIn.nextDocument());
  63. errs() << "yaml2obj: Cannot find the " << DocNum
  64. << llvm::getOrdinalSuffix(DocNum) << " document\n";
  65. return 1;
  66. }
  67. // HLSL Change: changed calling convention to __cdecl
  68. int __cdecl main(int argc, char **argv) {
  69. cl::ParseCommandLineOptions(argc, argv);
  70. sys::PrintStackTraceOnErrorSignal();
  71. PrettyStackTraceProgram X(argc, argv);
  72. llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
  73. if (OutputFilename.empty())
  74. OutputFilename = "-";
  75. std::error_code EC;
  76. std::unique_ptr<tool_output_file> Out(
  77. new tool_output_file(OutputFilename, EC, sys::fs::F_None));
  78. if (EC) {
  79. errs() << EC.message() << '\n';
  80. return 1;
  81. }
  82. ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
  83. MemoryBuffer::getFileOrSTDIN(Input);
  84. if (!Buf)
  85. return 1;
  86. ConvertFuncPtr Convert = nullptr;
  87. if (Format == YOF_COFF)
  88. Convert = yaml2coff;
  89. else if (Format == YOF_ELF)
  90. Convert = yaml2elf;
  91. else {
  92. errs() << "Not yet implemented\n";
  93. return 1;
  94. }
  95. yaml::Input YIn(Buf.get()->getBuffer());
  96. int Res = convertYAML(YIn, Out->os(), Convert);
  97. if (Res == 0)
  98. Out->keep();
  99. return Res;
  100. }