yaml2obj.cpp revision 36b56886974eae4f9c5ebc96befd3e7bfe5de338
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/Support/CommandLine.h"
19#include "llvm/Support/ManagedStatic.h"
20#include "llvm/Support/MemoryBuffer.h"
21#include "llvm/Support/PrettyStackTrace.h"
22#include "llvm/Support/Signals.h"
23#include "llvm/Support/raw_ostream.h"
24#include "llvm/Support/system_error.h"
25
26using namespace llvm;
27
28static cl::opt<std::string>
29  Input(cl::Positional, cl::desc("<input>"), cl::init("-"));
30
31// TODO: The "right" way to tell what kind of object file a given YAML file
32// corresponds to is to look at YAML "tags" (e.g. `!Foo`). Then, different
33// tags (`!ELF`, `!COFF`, etc.) would be used to discriminate between them.
34// Interpreting the tags is needed eventually for when writing test cases,
35// so that we can e.g. have `!Archive` contain a sequence of `!ELF`, and
36// just Do The Right Thing. However, interpreting these tags and acting on
37// them appropriately requires some work in the YAML parser and the YAMLIO
38// library.
39enum YAMLObjectFormat {
40  YOF_COFF,
41  YOF_ELF
42};
43
44cl::opt<YAMLObjectFormat> Format(
45  "format",
46  cl::desc("Interpret input as this type of object file"),
47  cl::values(
48    clEnumValN(YOF_COFF, "coff", "COFF object file format"),
49    clEnumValN(YOF_ELF, "elf", "ELF object file format"),
50  clEnumValEnd));
51
52
53int main(int argc, char **argv) {
54  cl::ParseCommandLineOptions(argc, argv);
55  sys::PrintStackTraceOnErrorSignal();
56  PrettyStackTraceProgram X(argc, argv);
57  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
58
59  std::unique_ptr<MemoryBuffer> Buf;
60  if (MemoryBuffer::getFileOrSTDIN(Input, Buf))
61    return 1;
62  if (Format == YOF_COFF) {
63    return yaml2coff(outs(), Buf.get());
64  } else if (Format == YOF_ELF) {
65    return yaml2elf(outs(), Buf.get());
66  } else {
67    errs() << "Not yet implemented\n";
68    return 1;
69  }
70}
71