blob: af4d8689067240d3a65ce3fceb41ad03ddf0fe73 [file] [log] [blame]
Michael J. Spencer1ffd9de2012-08-02 19:16:56 +00001//===- 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
Sean Silva3b76e402013-06-05 19:56:47 +000017#include "yaml2obj.h"
Simon Atanasyanf97af8a2014-05-31 04:51:07 +000018#include "llvm/ADT/StringExtras.h"
Michael J. Spencer1ffd9de2012-08-02 19:16:56 +000019#include "llvm/Support/CommandLine.h"
Simon Atanasyan73e047e2014-05-15 16:14:02 +000020#include "llvm/Support/FileSystem.h"
Michael J. Spencer1ffd9de2012-08-02 19:16:56 +000021#include "llvm/Support/ManagedStatic.h"
22#include "llvm/Support/MemoryBuffer.h"
23#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer1ffd9de2012-08-02 19:16:56 +000024#include "llvm/Support/Signals.h"
Simon Atanasyan73e047e2014-05-15 16:14:02 +000025#include "llvm/Support/ToolOutputFile.h"
Simon Atanasyanf97af8a2014-05-31 04:51:07 +000026#include "llvm/Support/YAMLTraits.h"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000027#include "llvm/Support/raw_ostream.h"
28#include <system_error>
Michael J. Spencer1ffd9de2012-08-02 19:16:56 +000029
30using namespace llvm;
31
32static cl::opt<std::string>
33 Input(cl::Positional, cl::desc("<input>"), cl::init("-"));
34
Sean Silva741cebb2013-06-05 18:51:34 +000035// 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
42// library.
43enum YAMLObjectFormat {
Sean Silvaf99309c2013-06-10 23:44:15 +000044 YOF_COFF,
45 YOF_ELF
Sean Silva741cebb2013-06-05 18:51:34 +000046};
47
48cl::opt<YAMLObjectFormat> Format(
49 "format",
50 cl::desc("Interpret input as this type of object file"),
51 cl::values(
52 clEnumValN(YOF_COFF, "coff", "COFF object file format"),
Sean Silvaf99309c2013-06-10 23:44:15 +000053 clEnumValN(YOF_ELF, "elf", "ELF object file format"),
Sean Silva741cebb2013-06-05 18:51:34 +000054 clEnumValEnd));
55
Simon Atanasyanf97af8a2014-05-31 04:51:07 +000056cl::opt<unsigned>
57DocNum("docnum", cl::init(1),
58 cl::desc("Read specified document from input (default = 1)"));
59
Simon Atanasyan73e047e2014-05-15 16:14:02 +000060static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
61 cl::value_desc("filename"));
Sean Silva741cebb2013-06-05 18:51:34 +000062
Simon Atanasyanf97af8a2014-05-31 04:51:07 +000063typedef int (*ConvertFuncPtr)(yaml::Input & YIn, raw_ostream &Out);
64
Benjamin Kramerf044d3f2015-03-09 16:23:46 +000065static int convertYAML(yaml::Input &YIn, raw_ostream &Out,
66 ConvertFuncPtr Convert) {
Simon Atanasyanf97af8a2014-05-31 04:51:07 +000067 unsigned CurDocNum = 0;
68 do {
69 if (++CurDocNum == DocNum)
70 return Convert(YIn, Out);
71 } while (YIn.nextDocument());
72
73 errs() << "yaml2obj: Cannot find the " << DocNum
74 << llvm::getOrdinalSuffix(DocNum) << " document\n";
75 return 1;
76}
77
Sean Silva741cebb2013-06-05 18:51:34 +000078int main(int argc, char **argv) {
79 cl::ParseCommandLineOptions(argc, argv);
80 sys::PrintStackTraceOnErrorSignal();
81 PrettyStackTraceProgram X(argc, argv);
82 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
83
Simon Atanasyan73e047e2014-05-15 16:14:02 +000084 if (OutputFilename.empty())
85 OutputFilename = "-";
86
Rafael Espindola3fd1e992014-08-25 18:16:47 +000087 std::error_code EC;
Simon Atanasyan73e047e2014-05-15 16:14:02 +000088 std::unique_ptr<tool_output_file> Out(
Rafael Espindola3fd1e992014-08-25 18:16:47 +000089 new tool_output_file(OutputFilename, EC, sys::fs::F_None));
90 if (EC) {
91 errs() << EC.message() << '\n';
Simon Atanasyan73e047e2014-05-15 16:14:02 +000092 return 1;
93 }
94
Rafael Espindolaadf21f22014-07-06 17:43:13 +000095 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
96 MemoryBuffer::getFileOrSTDIN(Input);
97 if (!Buf)
Sean Silva741cebb2013-06-05 18:51:34 +000098 return 1;
Simon Atanasyan73e047e2014-05-15 16:14:02 +000099
Simon Atanasyanf97af8a2014-05-31 04:51:07 +0000100 ConvertFuncPtr Convert = nullptr;
Simon Atanasyan73e047e2014-05-15 16:14:02 +0000101 if (Format == YOF_COFF)
Simon Atanasyanf97af8a2014-05-31 04:51:07 +0000102 Convert = yaml2coff;
Simon Atanasyan73e047e2014-05-15 16:14:02 +0000103 else if (Format == YOF_ELF)
Simon Atanasyanf97af8a2014-05-31 04:51:07 +0000104 Convert = yaml2elf;
105 else {
Sean Silva741cebb2013-06-05 18:51:34 +0000106 errs() << "Not yet implemented\n";
Simon Atanasyanf97af8a2014-05-31 04:51:07 +0000107 return 1;
108 }
Simon Atanasyan73e047e2014-05-15 16:14:02 +0000109
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000110 yaml::Input YIn(Buf.get()->getBuffer());
Simon Atanasyanf97af8a2014-05-31 04:51:07 +0000111
112 int Res = convertYAML(YIn, Out->os(), Convert);
Simon Atanasyan73e047e2014-05-15 16:14:02 +0000113 if (Res == 0)
114 Out->keep();
115
116 return Res;
Michael J. Spencer1ffd9de2012-08-02 19:16:56 +0000117}