1 //===- yaml2obj - Convert YAML to a binary object file --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This program takes a YAML description of an object file and outputs the
11 // binary equivalent.
12 //
13 // This is used for writing tests that require binary files.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "yaml2obj.h"
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/ManagedStatic.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/PrettyStackTrace.h"
25 #include "llvm/Support/Signals.h"
26 #include "llvm/Support/ToolOutputFile.h"
27 #include "llvm/Support/YAMLTraits.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include <system_error>
30 
31 using namespace llvm;
32 
33 static cl::opt<std::string>
34   Input(cl::Positional, cl::desc("<input>"), cl::init("-"));
35 
36 cl::opt<unsigned>
37 DocNum("docnum", cl::init(1),
38        cl::desc("Read specified document from input (default = 1)"));
39 
40 static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
41                                            cl::value_desc("filename"));
42 
43 LLVM_ATTRIBUTE_NORETURN static void error(Twine Message) {
44   errs() << Message << "\n";
45   exit(1);
46 }
47 
48 static int convertYAML(yaml::Input &YIn, raw_ostream &Out) {
49   unsigned CurDocNum = 0;
50   do {
51     if (++CurDocNum == DocNum) {
52       yaml::YamlObjectFile Doc;
53       YIn >> Doc;
54       if (YIn.error())
55         error("yaml2obj: Failed to parse YAML file!");
56       if (Doc.Elf)
57         return yaml2elf(*Doc.Elf, Out);
58       if (Doc.Coff)
59         return yaml2coff(*Doc.Coff, Out);
60       if (Doc.MachO || Doc.FatMachO)
61         return yaml2macho(Doc, Out);
62       if (Doc.Wasm)
63         return yaml2wasm(*Doc.Wasm, Out);
64       error("yaml2obj: Unknown document type!");
65     }
66   } while (YIn.nextDocument());
67 
68   error("yaml2obj: Cannot find the " + utostr(DocNum) +
69         llvm::getOrdinalSuffix(DocNum) + " document");
70 }
71 
72 int main(int argc, char **argv) {
73   cl::ParseCommandLineOptions(argc, argv);
74   sys::PrintStackTraceOnErrorSignal(argv[0]);
75   PrettyStackTraceProgram X(argc, argv);
76   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
77 
78   if (OutputFilename.empty())
79     OutputFilename = "-";
80 
81   std::error_code EC;
82   std::unique_ptr<tool_output_file> Out(
83       new tool_output_file(OutputFilename, EC, sys::fs::F_None));
84   if (EC)
85     error("yaml2obj: Error opening '" + OutputFilename + "': " + EC.message());
86 
87   ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
88       MemoryBuffer::getFileOrSTDIN(Input);
89   if (!Buf)
90     return 1;
91 
92   yaml::Input YIn(Buf.get()->getBuffer());
93 
94   int Res = convertYAML(YIn, Out->os());
95   if (Res == 0)
96     Out->keep();
97 
98   Out->os().flush();
99   return Res;
100 }
101