1 //===- yaml2obj - Convert YAML to a binary object file --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This program takes a YAML description of an object file and outputs the
13 // This is used for writing tests that require binary files.
15 //===----------------------------------------------------------------------===//
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ObjectYAML/ObjectYAML.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/FileSystem.h"
22 #include "llvm/Support/InitLLVM.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/ToolOutputFile.h"
25 #include "llvm/Support/YAMLTraits.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <system_error>
31 static cl::opt
<std::string
>
32 Input(cl::Positional
, cl::desc("<input>"), cl::init("-"));
35 DocNum("docnum", cl::init(1),
36 cl::desc("Read specified document from input (default = 1)"));
38 static cl::opt
<std::string
> OutputFilename("o", cl::desc("Output filename"),
39 cl::value_desc("filename"));
41 LLVM_ATTRIBUTE_NORETURN
static void error(Twine Message
) {
42 errs() << Message
<< "\n";
46 static int convertYAML(yaml::Input
&YIn
, raw_ostream
&Out
) {
47 unsigned CurDocNum
= 0;
49 if (++CurDocNum
== DocNum
) {
50 yaml::YamlObjectFile Doc
;
53 error("yaml2obj: Failed to parse YAML file!");
55 return yaml2elf(*Doc
.Elf
, Out
);
57 return yaml2coff(*Doc
.Coff
, Out
);
58 if (Doc
.MachO
|| Doc
.FatMachO
)
59 return yaml2macho(Doc
, Out
);
61 return yaml2wasm(*Doc
.Wasm
, Out
);
62 error("yaml2obj: Unknown document type!");
64 } while (YIn
.nextDocument());
66 error("yaml2obj: Cannot find the " + Twine(DocNum
) +
67 llvm::getOrdinalSuffix(DocNum
) + " document");
70 int main(int argc
, char **argv
) {
71 InitLLVM
X(argc
, argv
);
72 cl::ParseCommandLineOptions(argc
, argv
);
74 if (OutputFilename
.empty())
78 std::unique_ptr
<ToolOutputFile
> Out(
79 new ToolOutputFile(OutputFilename
, EC
, sys::fs::F_None
));
81 error("yaml2obj: Error opening '" + OutputFilename
+ "': " + EC
.message());
83 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> Buf
=
84 MemoryBuffer::getFileOrSTDIN(Input
);
88 yaml::Input
YIn(Buf
.get()->getBuffer());
90 int Res
= convertYAML(YIn
, Out
->os());