yaml2obj.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //===- yaml2obj - Convert YAML to a binary object file --------------------===//
  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. // This program takes a YAML description of an object file and outputs the
  10. // binary equivalent.
  11. //
  12. // This is used for writing tests that require binary files.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/ObjectYAML/yaml2obj.h"
  16. #include "llvm/ADT/StringExtras.h"
  17. #include "llvm/ObjectYAML/ObjectYAML.h"
  18. #include "llvm/Support/CommandLine.h"
  19. #include "llvm/Support/FileSystem.h"
  20. #include "llvm/Support/InitLLVM.h"
  21. #include "llvm/Support/MemoryBuffer.h"
  22. #include "llvm/Support/ToolOutputFile.h"
  23. #include "llvm/Support/WithColor.h"
  24. #include "llvm/Support/YAMLTraits.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include <system_error>
  27. using namespace llvm;
  28. namespace {
  29. cl::OptionCategory Cat("yaml2obj Options");
  30. cl::opt<std::string> Input(cl::Positional, cl::desc("<input file>"),
  31. cl::init("-"), cl::cat(Cat));
  32. cl::list<std::string>
  33. D("D", cl::Prefix,
  34. cl::desc("Defined the specified macros to their specified "
  35. "definition. The syntax is <macro>=<definition>"));
  36. cl::opt<unsigned>
  37. DocNum("docnum", cl::init(1),
  38. cl::desc("Read specified document from input (default = 1)"),
  39. cl::cat(Cat));
  40. static cl::opt<uint64_t> MaxSize(
  41. "max-size", cl::init(10 * 1024 * 1024),
  42. cl::desc(
  43. "Sets the maximum allowed output size (0 means no limit) [ELF only]"));
  44. cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
  45. cl::value_desc("filename"), cl::init("-"),
  46. cl::Prefix, cl::cat(Cat));
  47. } // namespace
  48. static Optional<std::string> preprocess(StringRef Buf,
  49. yaml::ErrorHandler ErrHandler) {
  50. DenseMap<StringRef, StringRef> Defines;
  51. for (StringRef Define : D) {
  52. StringRef Macro, Definition;
  53. std::tie(Macro, Definition) = Define.split('=');
  54. if (!Define.count('=') || Macro.empty()) {
  55. ErrHandler("invalid syntax for -D: " + Define);
  56. return {};
  57. }
  58. if (!Defines.try_emplace(Macro, Definition).second) {
  59. ErrHandler("'" + Macro + "'" + " redefined");
  60. return {};
  61. }
  62. }
  63. std::string Preprocessed;
  64. while (!Buf.empty()) {
  65. if (Buf.startswith("[[")) {
  66. size_t I = Buf.find_first_of("[]", 2);
  67. if (Buf.substr(I).startswith("]]")) {
  68. StringRef MacroExpr = Buf.substr(2, I - 2);
  69. StringRef Macro;
  70. StringRef Default;
  71. std::tie(Macro, Default) = MacroExpr.split('=');
  72. // When the -D option is requested, we use the provided value.
  73. // Otherwise we use a default macro value if present.
  74. auto It = Defines.find(Macro);
  75. Optional<StringRef> Value;
  76. if (It != Defines.end())
  77. Value = It->second;
  78. else if (!Default.empty() || MacroExpr.endswith("="))
  79. Value = Default;
  80. if (Value) {
  81. Preprocessed += *Value;
  82. Buf = Buf.substr(I + 2);
  83. continue;
  84. }
  85. }
  86. }
  87. Preprocessed += Buf[0];
  88. Buf = Buf.substr(1);
  89. }
  90. return Preprocessed;
  91. }
  92. int main(int argc, char **argv) {
  93. InitLLVM X(argc, argv);
  94. cl::HideUnrelatedOptions(Cat);
  95. cl::ParseCommandLineOptions(
  96. argc, argv, "Create an object file from a YAML description", nullptr,
  97. nullptr, /*LongOptionsUseDoubleDash=*/true);
  98. auto ErrHandler = [](const Twine &Msg) {
  99. WithColor::error(errs(), "yaml2obj") << Msg << "\n";
  100. };
  101. std::error_code EC;
  102. std::unique_ptr<ToolOutputFile> Out(
  103. new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None));
  104. if (EC) {
  105. ErrHandler("failed to open '" + OutputFilename + "': " + EC.message());
  106. return 1;
  107. }
  108. ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
  109. MemoryBuffer::getFileOrSTDIN(Input);
  110. if (!Buf)
  111. return 1;
  112. Optional<std::string> Buffer = preprocess(Buf.get()->getBuffer(), ErrHandler);
  113. if (!Buffer)
  114. return 1;
  115. yaml::Input YIn(*Buffer);
  116. if (!convertYAML(YIn, Out->os(), ErrHandler, DocNum,
  117. MaxSize == 0 ? UINT64_MAX : MaxSize))
  118. return 1;
  119. Out->keep();
  120. Out->os().flush();
  121. return 0;
  122. }