13add3e9cSRafael Espindola //===- yaml2obj - Convert YAML to a binary object file --------------------===//
23add3e9cSRafael Espindola //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63add3e9cSRafael Espindola //
73add3e9cSRafael Espindola //===----------------------------------------------------------------------===//
83add3e9cSRafael Espindola //
93add3e9cSRafael Espindola // This program takes a YAML description of an object file and outputs the
103add3e9cSRafael Espindola // binary equivalent.
113add3e9cSRafael Espindola //
123add3e9cSRafael Espindola // This is used for writing tests that require binary files.
133add3e9cSRafael Espindola //
143add3e9cSRafael Espindola //===----------------------------------------------------------------------===//
153add3e9cSRafael Espindola 
16c22d9666SAlex Brachet #include "llvm/ObjectYAML/yaml2obj.h"
17f97af8a0SSimon Atanasyan #include "llvm/ADT/StringExtras.h"
188ff0c113SChris Bieneman #include "llvm/ObjectYAML/ObjectYAML.h"
193add3e9cSRafael Espindola #include "llvm/Support/CommandLine.h"
2073e047e5SSimon Atanasyan #include "llvm/Support/FileSystem.h"
21197194b6SRui Ueyama #include "llvm/Support/InitLLVM.h"
223add3e9cSRafael Espindola #include "llvm/Support/MemoryBuffer.h"
2373e047e5SSimon Atanasyan #include "llvm/Support/ToolOutputFile.h"
24c22d9666SAlex Brachet #include "llvm/Support/WithColor.h"
25f97af8a0SSimon Atanasyan #include "llvm/Support/YAMLTraits.h"
26a6e9c3e4SRafael Espindola #include "llvm/Support/raw_ostream.h"
27a6e9c3e4SRafael Espindola #include <system_error>
283add3e9cSRafael Espindola 
293add3e9cSRafael Espindola using namespace llvm;
303add3e9cSRafael Espindola 
31a29a9a34SFangrui Song namespace {
32a29a9a34SFangrui Song cl::OptionCategory Cat("yaml2obj Options");
333add3e9cSRafael Espindola 
34a29a9a34SFangrui Song cl::opt<std::string> Input(cl::Positional, cl::desc("<input file>"),
35a29a9a34SFangrui Song                            cl::init("-"), cl::cat(Cat));
36a29a9a34SFangrui Song 
37*e3951248SFangrui Song cl::list<std::string>
38*e3951248SFangrui Song     D("D", cl::Prefix,
39*e3951248SFangrui Song       cl::desc("Defined the specified macros to their specified "
40*e3951248SFangrui Song                "definition. The syntax is <macro>=<definition>"));
41*e3951248SFangrui Song 
42a29a9a34SFangrui Song cl::opt<unsigned>
43f97af8a0SSimon Atanasyan     DocNum("docnum", cl::init(1),
44a29a9a34SFangrui Song            cl::desc("Read specified document from input (default = 1)"),
45a29a9a34SFangrui Song            cl::cat(Cat));
46f97af8a0SSimon Atanasyan 
47a29a9a34SFangrui Song cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
48a29a9a34SFangrui Song                                     cl::value_desc("filename"), cl::init("-"),
49a29a9a34SFangrui Song                                     cl::Prefix, cl::cat(Cat));
50a29a9a34SFangrui Song } // namespace
51741cebbeSSean Silva 
52*e3951248SFangrui Song static Optional<std::string> preprocess(StringRef Buf,
53*e3951248SFangrui Song                                         yaml::ErrorHandler ErrHandler) {
54*e3951248SFangrui Song   DenseMap<StringRef, StringRef> Defines;
55*e3951248SFangrui Song   for (StringRef Define : D) {
56*e3951248SFangrui Song     StringRef Macro, Definition;
57*e3951248SFangrui Song     std::tie(Macro, Definition) = Define.split('=');
58*e3951248SFangrui Song     if (!Define.count('=') || Macro.empty()) {
59*e3951248SFangrui Song       ErrHandler("invalid syntax for -D: " + Define);
60*e3951248SFangrui Song       return {};
61*e3951248SFangrui Song     }
62*e3951248SFangrui Song     if (!Defines.try_emplace(Macro, Definition).second) {
63*e3951248SFangrui Song       ErrHandler("'" + Macro + "'" + " redefined");
64*e3951248SFangrui Song       return {};
65*e3951248SFangrui Song     }
66*e3951248SFangrui Song   }
67*e3951248SFangrui Song 
68*e3951248SFangrui Song   std::string Preprocessed;
69*e3951248SFangrui Song   while (!Buf.empty()) {
70*e3951248SFangrui Song     if (Buf.startswith("[[")) {
71*e3951248SFangrui Song       size_t I = Buf.find_first_of("[]", 2);
72*e3951248SFangrui Song       if (Buf.substr(I).startswith("]]")) {
73*e3951248SFangrui Song         StringRef Macro = Buf.substr(2, I - 2);
74*e3951248SFangrui Song         auto It = Defines.find(Macro);
75*e3951248SFangrui Song         if (It != Defines.end()) {
76*e3951248SFangrui Song           Preprocessed += It->second;
77*e3951248SFangrui Song           Buf = Buf.substr(I + 2);
78*e3951248SFangrui Song           continue;
79*e3951248SFangrui Song         }
80*e3951248SFangrui Song       }
81*e3951248SFangrui Song     }
82*e3951248SFangrui Song 
83*e3951248SFangrui Song     Preprocessed += Buf[0];
84*e3951248SFangrui Song     Buf = Buf.substr(1);
85*e3951248SFangrui Song   }
86*e3951248SFangrui Song 
87*e3951248SFangrui Song   return Preprocessed;
88*e3951248SFangrui Song }
89*e3951248SFangrui Song 
90741cebbeSSean Silva int main(int argc, char **argv) {
91197194b6SRui Ueyama   InitLLVM X(argc, argv);
92a29a9a34SFangrui Song   cl::HideUnrelatedOptions(Cat);
93a29a9a34SFangrui Song   cl::ParseCommandLineOptions(
94a29a9a34SFangrui Song       argc, argv, "Create an object file from a YAML description", nullptr,
95a29a9a34SFangrui Song       nullptr, /*LongOptionsUseDoubleDash=*/true);
9673e047e5SSimon Atanasyan 
9785011027SGeorge Rimar   auto ErrHandler = [](const Twine &Msg) {
9885011027SGeorge Rimar     WithColor::error(errs(), "yaml2obj") << Msg << "\n";
9985011027SGeorge Rimar   };
10085011027SGeorge Rimar 
1013fd1e993SRafael Espindola   std::error_code EC;
1023fc649cbSReid Kleckner   std::unique_ptr<ToolOutputFile> Out(
103d9b948b6SFangrui Song       new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None));
10485011027SGeorge Rimar   if (EC) {
10585011027SGeorge Rimar     ErrHandler("failed to open '" + OutputFilename + "': " + EC.message());
10685011027SGeorge Rimar     return 1;
10785011027SGeorge Rimar   }
10873e047e5SSimon Atanasyan 
109adf21f2aSRafael Espindola   ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
110adf21f2aSRafael Espindola       MemoryBuffer::getFileOrSTDIN(Input);
111adf21f2aSRafael Espindola   if (!Buf)
112741cebbeSSean Silva     return 1;
11373e047e5SSimon Atanasyan 
114*e3951248SFangrui Song   Optional<std::string> Buffer = preprocess(Buf.get()->getBuffer(), ErrHandler);
115*e3951248SFangrui Song   if (!Buffer)
116*e3951248SFangrui Song     return 1;
117*e3951248SFangrui Song   yaml::Input YIn(*Buffer);
11885011027SGeorge Rimar   if (!convertYAML(YIn, Out->os(), ErrHandler, DocNum))
119c22d9666SAlex Brachet     return 1;
120fc21bb66SDmitri Gribenko 
121c22d9666SAlex Brachet   Out->keep();
122948a5966SZachary Turner   Out->os().flush();
123c22d9666SAlex Brachet   return 0;
1243add3e9cSRafael Espindola }
125