1 //===- yaml2obj - Convert YAML to a binary object file --------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This program takes a YAML description of an object file and outputs the
12 // This is used for writing tests that require binary files.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/ObjectYAML/yaml2obj.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ObjectYAML/ObjectYAML.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/InitLLVM.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "llvm/Support/ToolOutputFile.h"
24 #include "llvm/Support/WithColor.h"
25 #include "llvm/Support/YAMLTraits.h"
26 #include "llvm/Support/raw_ostream.h"
28 #include <system_error>
33 cl::OptionCategory
Cat("yaml2obj Options");
35 cl::opt
<std::string
> Input(cl::Positional
, cl::desc("<input file>"),
36 cl::init("-"), cl::cat(Cat
));
40 cl::desc("Defined the specified macros to their specified "
41 "definition. The syntax is <macro>=<definition>"),
44 cl::opt
<bool> PreprocessOnly("E", cl::desc("Just print the preprocessed file"),
48 DocNum("docnum", cl::init(1),
49 cl::desc("Read specified document from input (default = 1)"),
52 static cl::opt
<uint64_t> MaxSize(
53 "max-size", cl::init(10 * 1024 * 1024),
55 "Sets the maximum allowed output size (0 means no limit) [ELF only]"),
58 cl::opt
<std::string
> OutputFilename("o", cl::desc("Output filename"),
59 cl::value_desc("filename"), cl::init("-"),
60 cl::Prefix
, cl::cat(Cat
));
63 static std::optional
<std::string
> preprocess(StringRef Buf
,
64 yaml::ErrorHandler ErrHandler
) {
65 DenseMap
<StringRef
, StringRef
> Defines
;
66 for (StringRef Define
: D
) {
67 StringRef Macro
, Definition
;
68 std::tie(Macro
, Definition
) = Define
.split('=');
69 if (!Define
.count('=') || Macro
.empty()) {
70 ErrHandler("invalid syntax for -D: " + Define
);
73 if (!Defines
.try_emplace(Macro
, Definition
).second
) {
74 ErrHandler("'" + Macro
+ "'" + " redefined");
79 std::string Preprocessed
;
80 while (!Buf
.empty()) {
81 if (Buf
.startswith("[[")) {
82 size_t I
= Buf
.find_first_of("[]", 2);
83 if (Buf
.substr(I
).startswith("]]")) {
84 StringRef MacroExpr
= Buf
.substr(2, I
- 2);
87 std::tie(Macro
, Default
) = MacroExpr
.split('=');
89 // When the -D option is requested, we use the provided value.
90 // Otherwise we use a default macro value if present.
91 auto It
= Defines
.find(Macro
);
92 std::optional
<StringRef
> Value
;
93 if (It
!= Defines
.end())
95 else if (!Default
.empty() || MacroExpr
.endswith("="))
99 Preprocessed
+= *Value
;
100 Buf
= Buf
.substr(I
+ 2);
106 Preprocessed
+= Buf
[0];
113 int main(int argc
, char **argv
) {
114 InitLLVM
X(argc
, argv
);
115 cl::HideUnrelatedOptions(Cat
);
116 cl::ParseCommandLineOptions(
117 argc
, argv
, "Create an object file from a YAML description", nullptr,
118 nullptr, /*LongOptionsUseDoubleDash=*/true);
120 auto ErrHandler
= [](const Twine
&Msg
) {
121 WithColor::error(errs(), "yaml2obj") << Msg
<< "\n";
125 std::unique_ptr
<ToolOutputFile
> Out(
126 new ToolOutputFile(OutputFilename
, EC
, sys::fs::OF_None
));
128 ErrHandler("failed to open '" + OutputFilename
+ "': " + EC
.message());
132 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> Buf
=
133 MemoryBuffer::getFileOrSTDIN(Input
);
137 std::optional
<std::string
> Buffer
=
138 preprocess(Buf
.get()->getBuffer(), ErrHandler
);
142 if (PreprocessOnly
) {
145 yaml::Input
YIn(*Buffer
);
147 if (!convertYAML(YIn
, Out
->os(), ErrHandler
, DocNum
,
148 MaxSize
== 0 ? UINT64_MAX
: MaxSize
))