yaml2obj.cpp 4.4 KB

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