yaml2obj.cpp 4.7 KB

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