Remove system_error.h.
[oota-llvm.git] / tools / yaml2obj / yaml2obj.cpp
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/Support/CommandLine.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/ManagedStatic.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "llvm/Support/PrettyStackTrace.h"
24 #include "llvm/Support/Signals.h"
25 #include "llvm/Support/ToolOutputFile.h"
26 #include "llvm/Support/YAMLTraits.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include <system_error>
29
30 using namespace llvm;
31
32 static cl::opt<std::string>
33   Input(cl::Positional, cl::desc("<input>"), cl::init("-"));
34
35 // TODO: The "right" way to tell what kind of object file a given YAML file
36 // corresponds to is to look at YAML "tags" (e.g. `!Foo`). Then, different
37 // tags (`!ELF`, `!COFF`, etc.) would be used to discriminate between them.
38 // Interpreting the tags is needed eventually for when writing test cases,
39 // so that we can e.g. have `!Archive` contain a sequence of `!ELF`, and
40 // just Do The Right Thing. However, interpreting these tags and acting on
41 // them appropriately requires some work in the YAML parser and the YAMLIO
42 // library.
43 enum YAMLObjectFormat {
44   YOF_COFF,
45   YOF_ELF
46 };
47
48 cl::opt<YAMLObjectFormat> Format(
49   "format",
50   cl::desc("Interpret input as this type of object file"),
51   cl::values(
52     clEnumValN(YOF_COFF, "coff", "COFF object file format"),
53     clEnumValN(YOF_ELF, "elf", "ELF object file format"),
54   clEnumValEnd));
55
56 cl::opt<unsigned>
57 DocNum("docnum", cl::init(1),
58        cl::desc("Read specified document from input (default = 1)"));
59
60 static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
61                                            cl::value_desc("filename"));
62
63 typedef int (*ConvertFuncPtr)(yaml::Input & YIn, raw_ostream &Out);
64
65 int convertYAML(yaml::Input & YIn, raw_ostream &Out, ConvertFuncPtr Convert) {
66   unsigned CurDocNum = 0;
67   do {
68     if (++CurDocNum == DocNum)
69       return Convert(YIn, Out);
70   } while (YIn.nextDocument());
71
72   errs() << "yaml2obj: Cannot find the " << DocNum
73          << llvm::getOrdinalSuffix(DocNum) << " document\n";
74   return 1;
75 }
76
77 int main(int argc, char **argv) {
78   cl::ParseCommandLineOptions(argc, argv);
79   sys::PrintStackTraceOnErrorSignal();
80   PrettyStackTraceProgram X(argc, argv);
81   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
82
83   if (OutputFilename.empty())
84     OutputFilename = "-";
85
86   std::string ErrorInfo;
87   std::unique_ptr<tool_output_file> Out(
88       new tool_output_file(OutputFilename.c_str(), ErrorInfo, sys::fs::F_None));
89   if (!ErrorInfo.empty()) {
90     errs() << ErrorInfo << '\n';
91     return 1;
92   }
93
94   std::unique_ptr<MemoryBuffer> Buf;
95   if (MemoryBuffer::getFileOrSTDIN(Input, Buf))
96     return 1;
97
98   ConvertFuncPtr Convert = nullptr;
99   if (Format == YOF_COFF)
100     Convert = yaml2coff;
101   else if (Format == YOF_ELF)
102     Convert = yaml2elf;
103   else {
104     errs() << "Not yet implemented\n";
105     return 1;
106   }
107
108   yaml::Input YIn(Buf->getBuffer());
109
110   int Res = convertYAML(YIn, Out->os(), Convert);
111   if (Res == 0)
112     Out->keep();
113
114   return Res;
115 }