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 
16 #include "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/YAMLTraits.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include <system_error>
27 
28 using namespace llvm;
29 
30 static cl::opt<std::string>
31   Input(cl::Positional, cl::desc("<input>"), cl::init("-"));
32 
33 cl::opt<unsigned>
34 DocNum("docnum", cl::init(1),
35        cl::desc("Read specified document from input (default = 1)"));
36 
37 static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
38                                            cl::value_desc("filename"));
39 
error(Twine Message)40 LLVM_ATTRIBUTE_NORETURN static void error(Twine Message) {
41   errs() << Message << "\n";
42   exit(1);
43 }
44 
convertYAML(yaml::Input & YIn,raw_ostream & Out)45 static int convertYAML(yaml::Input &YIn, raw_ostream &Out) {
46   unsigned CurDocNum = 0;
47   do {
48     if (++CurDocNum == DocNum) {
49       yaml::YamlObjectFile Doc;
50       YIn >> Doc;
51       if (YIn.error())
52         error("yaml2obj: Failed to parse YAML file!");
53       if (Doc.Elf)
54         return yaml2elf(*Doc.Elf, Out);
55       if (Doc.Coff)
56         return yaml2coff(*Doc.Coff, Out);
57       if (Doc.MachO || Doc.FatMachO)
58         return yaml2macho(Doc, Out);
59       if (Doc.Minidump)
60         return yaml2minidump(*Doc.Minidump, Out);
61       if (Doc.Wasm)
62         return yaml2wasm(*Doc.Wasm, Out);
63       error("yaml2obj: Unknown document type!");
64     }
65   } while (YIn.nextDocument());
66 
67   error("yaml2obj: Cannot find the " + Twine(DocNum) +
68         llvm::getOrdinalSuffix(DocNum) + " document");
69 }
70 
main(int argc,char ** argv)71 int main(int argc, char **argv) {
72   InitLLVM X(argc, argv);
73   cl::ParseCommandLineOptions(argc, argv);
74 
75   if (OutputFilename.empty())
76     OutputFilename = "-";
77 
78   std::error_code EC;
79   std::unique_ptr<ToolOutputFile> Out(
80       new ToolOutputFile(OutputFilename, EC, sys::fs::F_None));
81   if (EC)
82     error("yaml2obj: Error opening '" + OutputFilename + "': " + EC.message());
83 
84   ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
85       MemoryBuffer::getFileOrSTDIN(Input);
86   if (!Buf)
87     return 1;
88 
89   yaml::Input YIn(Buf.get()->getBuffer());
90   int Res = convertYAML(YIn, Out->os());
91   if (Res == 0)
92     Out->keep();
93 
94   Out->os().flush();
95   return Res;
96 }
97