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