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