2012-08-02 21:16:56 +02:00
|
|
|
//===- 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-06-05 21:56:47 +02:00
|
|
|
#include "yaml2obj.h"
|
2014-05-31 06:51:07 +02:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2012-08-02 21:16:56 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2014-05-15 18:14:02 +02:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-08-02 21:16:56 +02:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
2014-05-15 18:14:02 +02:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2014-05-31 06:51:07 +02:00
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
2014-06-12 19:38:55 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <system_error>
|
2012-08-02 21:16:56 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
Input(cl::Positional, cl::desc("<input>"), cl::init("-"));
|
|
|
|
|
2013-06-05 20:51:34 +02:00
|
|
|
// 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 {
|
2013-06-11 01:44:15 +02:00
|
|
|
YOF_COFF,
|
|
|
|
YOF_ELF
|
2013-06-05 20:51:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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"),
|
2013-06-11 01:44:15 +02:00
|
|
|
clEnumValN(YOF_ELF, "elf", "ELF object file format"),
|
2013-06-05 20:51:34 +02:00
|
|
|
clEnumValEnd));
|
|
|
|
|
2014-05-31 06:51:07 +02:00
|
|
|
cl::opt<unsigned>
|
|
|
|
DocNum("docnum", cl::init(1),
|
|
|
|
cl::desc("Read specified document from input (default = 1)"));
|
|
|
|
|
2014-05-15 18:14:02 +02:00
|
|
|
static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
|
|
|
|
cl::value_desc("filename"));
|
2013-06-05 20:51:34 +02:00
|
|
|
|
2014-05-31 06:51:07 +02:00
|
|
|
typedef int (*ConvertFuncPtr)(yaml::Input & YIn, raw_ostream &Out);
|
|
|
|
|
2015-03-09 17:23:46 +01:00
|
|
|
static int convertYAML(yaml::Input &YIn, raw_ostream &Out,
|
|
|
|
ConvertFuncPtr Convert) {
|
2014-05-31 06:51:07 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-06-05 20:51:34 +02:00
|
|
|
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.
|
|
|
|
|
2014-05-15 18:14:02 +02:00
|
|
|
if (OutputFilename.empty())
|
|
|
|
OutputFilename = "-";
|
|
|
|
|
2014-08-25 20:16:47 +02:00
|
|
|
std::error_code EC;
|
2014-05-15 18:14:02 +02:00
|
|
|
std::unique_ptr<tool_output_file> Out(
|
2014-08-25 20:16:47 +02:00
|
|
|
new tool_output_file(OutputFilename, EC, sys::fs::F_None));
|
|
|
|
if (EC) {
|
|
|
|
errs() << EC.message() << '\n';
|
2014-05-15 18:14:02 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-07-06 19:43:13 +02:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(Input);
|
|
|
|
if (!Buf)
|
2013-06-05 20:51:34 +02:00
|
|
|
return 1;
|
2014-05-15 18:14:02 +02:00
|
|
|
|
2014-05-31 06:51:07 +02:00
|
|
|
ConvertFuncPtr Convert = nullptr;
|
2014-05-15 18:14:02 +02:00
|
|
|
if (Format == YOF_COFF)
|
2014-05-31 06:51:07 +02:00
|
|
|
Convert = yaml2coff;
|
2014-05-15 18:14:02 +02:00
|
|
|
else if (Format == YOF_ELF)
|
2014-05-31 06:51:07 +02:00
|
|
|
Convert = yaml2elf;
|
|
|
|
else {
|
2013-06-05 20:51:34 +02:00
|
|
|
errs() << "Not yet implemented\n";
|
2014-05-31 06:51:07 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-07-06 19:43:13 +02:00
|
|
|
yaml::Input YIn(Buf.get()->getBuffer());
|
2014-05-15 18:14:02 +02:00
|
|
|
|
2014-05-31 06:51:07 +02:00
|
|
|
int Res = convertYAML(YIn, Out->os(), Convert);
|
2014-05-15 18:14:02 +02:00
|
|
|
if (Res == 0)
|
|
|
|
Out->keep();
|
|
|
|
|
|
|
|
return Res;
|
2012-08-02 21:16:56 +02:00
|
|
|
}
|