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