1 //===- yaml2obj - Convert YAML to a binary object file --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This program takes a YAML description of an object file and outputs the
13 // This is used for writing tests that require binary files.
15 //===----------------------------------------------------------------------===//
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>
32 static cl::opt<std::string>
33 Input(cl::Positional, cl::desc("<input>"), cl::init("-"));
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
43 enum YAMLObjectFormat {
48 cl::opt<YAMLObjectFormat> Format(
50 cl::desc("Interpret input as this type of object file"),
52 clEnumValN(YOF_COFF, "coff", "COFF object file format"),
53 clEnumValN(YOF_ELF, "elf", "ELF object file format"),
57 DocNum("docnum", cl::init(1),
58 cl::desc("Read specified document from input (default = 1)"));
60 static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
61 cl::value_desc("filename"));
63 typedef int (*ConvertFuncPtr)(yaml::Input & YIn, raw_ostream &Out);
65 int convertYAML(yaml::Input & YIn, raw_ostream &Out, ConvertFuncPtr Convert) {
66 unsigned CurDocNum = 0;
68 if (++CurDocNum == DocNum)
69 return Convert(YIn, Out);
70 } while (YIn.nextDocument());
72 errs() << "yaml2obj: Cannot find the " << DocNum
73 << llvm::getOrdinalSuffix(DocNum) << " document\n";
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.
83 if (OutputFilename.empty())
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';
94 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
95 MemoryBuffer::getFileOrSTDIN(Input);
99 ConvertFuncPtr Convert = nullptr;
100 if (Format == YOF_COFF)
102 else if (Format == YOF_ELF)
105 errs() << "Not yet implemented\n";
109 yaml::Input YIn(Buf.get()->getBuffer());
111 int Res = convertYAML(YIn, Out->os(), Convert);