summaryrefslogtreecommitdiff
path: root/tools/yaml2obj/yaml2obj.cpp
blob: dd41951e7db8924dd8b928be6409ddd12bccd08e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//===- yaml2obj - Convert YAML to a binary object file --------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This program takes a YAML description of an object file and outputs the
// binary equivalent.
//
// This is used for writing tests that require binary files.
//
//===----------------------------------------------------------------------===//

#include "yaml2obj.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/raw_ostream.h"
#include <system_error>

using namespace llvm;

static cl::opt<std::string>
  Input(cl::Positional, cl::desc("<input>"), cl::init("-"));

// TODO: The "right" way to tell what kind of object file a given YAML file
// corresponds to is to look at YAML "tags" (e.g. `!Foo`). Then, different
// tags (`!ELF`, `!COFF`, etc.) would be used to discriminate between them.
// Interpreting the tags is needed eventually for when writing test cases,
// so that we can e.g. have `!Archive` contain a sequence of `!ELF`, and
// just Do The Right Thing. However, interpreting these tags and acting on
// them appropriately requires some work in the YAML parser and the YAMLIO
// library.
enum YAMLObjectFormat {
  YOF_COFF,
  YOF_ELF
};

cl::opt<YAMLObjectFormat> Format(
  "format",
  cl::desc("Interpret input as this type of object file"),
  cl::values(
    clEnumValN(YOF_COFF, "coff", "COFF object file format"),
    clEnumValN(YOF_ELF, "elf", "ELF object file format"),
  clEnumValEnd));

cl::opt<unsigned>
DocNum("docnum", cl::init(1),
       cl::desc("Read specified document from input (default = 1)"));

static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
                                           cl::value_desc("filename"));

typedef int (*ConvertFuncPtr)(yaml::Input & YIn, raw_ostream &Out);

int convertYAML(yaml::Input & YIn, raw_ostream &Out, ConvertFuncPtr Convert) {
  unsigned CurDocNum = 0;
  do {
    if (++CurDocNum == DocNum)
      return Convert(YIn, Out);
  } while (YIn.nextDocument());

  errs() << "yaml2obj: Cannot find the " << DocNum
         << llvm::getOrdinalSuffix(DocNum) << " document\n";
  return 1;
}

int main(int argc, char **argv) {
  cl::ParseCommandLineOptions(argc, argv);
  sys::PrintStackTraceOnErrorSignal();
  PrettyStackTraceProgram X(argc, argv);
  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.

  if (OutputFilename.empty())
    OutputFilename = "-";

  std::string ErrorInfo;
  std::unique_ptr<tool_output_file> Out(
      new tool_output_file(OutputFilename.c_str(), ErrorInfo, sys::fs::F_None));
  if (!ErrorInfo.empty()) {
    errs() << ErrorInfo << '\n';
    return 1;
  }

  std::unique_ptr<MemoryBuffer> Buf;
  if (MemoryBuffer::getFileOrSTDIN(Input, Buf))
    return 1;

  ConvertFuncPtr Convert = nullptr;
  if (Format == YOF_COFF)
    Convert = yaml2coff;
  else if (Format == YOF_ELF)
    Convert = yaml2elf;
  else {
    errs() << "Not yet implemented\n";
    return 1;
  }

  yaml::Input YIn(Buf->getBuffer());

  int Res = convertYAML(YIn, Out->os(), Convert);
  if (Res == 0)
    Out->keep();

  return Res;
}