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/Support/CommandLine.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/ManagedStatic.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/PrettyStackTrace.h"
23 #include "llvm/Support/Signals.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Support/system_error.h"
26 #include "llvm/Support/ToolOutputFile.h"
30 static cl::opt<std::string>
31 Input(cl::Positional, cl::desc("<input>"), cl::init("-"));
33 // TODO: The "right" way to tell what kind of object file a given YAML file
34 // corresponds to is to look at YAML "tags" (e.g. `!Foo`). Then, different
35 // tags (`!ELF`, `!COFF`, etc.) would be used to discriminate between them.
36 // Interpreting the tags is needed eventually for when writing test cases,
37 // so that we can e.g. have `!Archive` contain a sequence of `!ELF`, and
38 // just Do The Right Thing. However, interpreting these tags and acting on
39 // them appropriately requires some work in the YAML parser and the YAMLIO
41 enum YAMLObjectFormat {
46 cl::opt<YAMLObjectFormat> Format(
48 cl::desc("Interpret input as this type of object file"),
50 clEnumValN(YOF_COFF, "coff", "COFF object file format"),
51 clEnumValN(YOF_ELF, "elf", "ELF object file format"),
54 static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
55 cl::value_desc("filename"));
57 int main(int argc, char **argv) {
58 cl::ParseCommandLineOptions(argc, argv);
59 sys::PrintStackTraceOnErrorSignal();
60 PrettyStackTraceProgram X(argc, argv);
61 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
63 if (OutputFilename.empty())
66 std::string ErrorInfo;
67 std::unique_ptr<tool_output_file> Out(
68 new tool_output_file(OutputFilename.c_str(), ErrorInfo, sys::fs::F_None));
69 if (!ErrorInfo.empty()) {
70 errs() << ErrorInfo << '\n';
74 std::unique_ptr<MemoryBuffer> Buf;
75 if (MemoryBuffer::getFileOrSTDIN(Input, Buf))
79 if (Format == YOF_COFF)
80 Res = yaml2coff(Out->os(), Buf.get());
81 else if (Format == YOF_ELF)
82 Res = yaml2elf(Out->os(), Buf.get());
84 errs() << "Not yet implemented\n";