2012-08-03 03:16:56 +08:00
|
|
|
//===- yaml2obj - Convert YAML to a binary object file --------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-08-03 03:16:56 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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-06 03:56:47 +08:00
|
|
|
#include "yaml2obj.h"
|
2014-05-31 12:51:07 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2016-06-28 03:53:53 +08:00
|
|
|
#include "llvm/ObjectYAML/ObjectYAML.h"
|
2012-08-03 03:16:56 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2014-05-16 00:14:02 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2018-04-14 02:26:06 +08:00
|
|
|
#include "llvm/Support/InitLLVM.h"
|
2012-08-03 03:16:56 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2014-05-16 00:14:02 +08:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2014-05-31 12:51:07 +08:00
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
2014-06-13 01:38:55 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <system_error>
|
2012-08-03 03:16:56 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
Input(cl::Positional, cl::desc("<input>"), cl::init("-"));
|
|
|
|
|
2014-05-31 12:51:07 +08:00
|
|
|
cl::opt<unsigned>
|
|
|
|
DocNum("docnum", cl::init(1),
|
|
|
|
cl::desc("Read specified document from input (default = 1)"));
|
|
|
|
|
2014-05-16 00:14:02 +08:00
|
|
|
static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
|
|
|
|
cl::value_desc("filename"));
|
2013-06-06 02:51:34 +08:00
|
|
|
|
2017-04-05 23:18:16 +08:00
|
|
|
LLVM_ATTRIBUTE_NORETURN static void error(Twine Message) {
|
|
|
|
errs() << Message << "\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-06-28 03:53:53 +08:00
|
|
|
static int convertYAML(yaml::Input &YIn, raw_ostream &Out) {
|
2014-05-31 12:51:07 +08:00
|
|
|
unsigned CurDocNum = 0;
|
|
|
|
do {
|
2016-06-28 03:53:53 +08:00
|
|
|
if (++CurDocNum == DocNum) {
|
|
|
|
yaml::YamlObjectFile Doc;
|
|
|
|
YIn >> Doc;
|
2017-04-05 23:18:16 +08:00
|
|
|
if (YIn.error())
|
|
|
|
error("yaml2obj: Failed to parse YAML file!");
|
2016-06-28 03:53:53 +08:00
|
|
|
if (Doc.Elf)
|
|
|
|
return yaml2elf(*Doc.Elf, Out);
|
|
|
|
if (Doc.Coff)
|
|
|
|
return yaml2coff(*Doc.Coff, Out);
|
|
|
|
if (Doc.MachO || Doc.FatMachO)
|
|
|
|
return yaml2macho(Doc, Out);
|
[ObjectYAML] Add basic minidump generation support
Summary:
This patch adds the ability to read a yaml form of a minidump file and
write it out as binary. Apart from the minidump header and the stream
directory, only three basic stream kinds are supported:
- Text: This kind is used for streams which contain textual data. This
is typically the contents of a /proc file on linux (e.g.
/proc/PID/maps). In this case, we just put the raw stream contents
into the yaml.
- SystemInfo: This stream contains various bits of information about the
host system in binary form. We expose the data in a structured form.
- Raw: This kind is used as a fallback when we don't have any special
knowledge about the stream. In this case, we just print the stream
contents in hex.
For this code to be really useful, more stream kinds will need to be
added (particularly for things like lists of memory regions and loaded
modules). However, these can be added incrementally.
Reviewers: jhenderson, zturner, clayborg, aprantl
Subscribers: mgorny, lemo, llvm-commits, lldb-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59482
llvm-svn: 356753
2019-03-22 22:47:26 +08:00
|
|
|
if (Doc.Minidump)
|
|
|
|
return yaml2minidump(*Doc.Minidump, Out);
|
2017-03-31 03:44:09 +08:00
|
|
|
if (Doc.Wasm)
|
|
|
|
return yaml2wasm(*Doc.Wasm, Out);
|
2017-04-05 23:18:16 +08:00
|
|
|
error("yaml2obj: Unknown document type!");
|
2016-06-28 03:53:53 +08:00
|
|
|
}
|
2014-05-31 12:51:07 +08:00
|
|
|
} while (YIn.nextDocument());
|
|
|
|
|
2017-12-29 00:58:54 +08:00
|
|
|
error("yaml2obj: Cannot find the " + Twine(DocNum) +
|
2017-04-05 23:18:16 +08:00
|
|
|
llvm::getOrdinalSuffix(DocNum) + " document");
|
2014-05-31 12:51:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-06 02:51:34 +08:00
|
|
|
int main(int argc, char **argv) {
|
2018-04-14 02:26:06 +08:00
|
|
|
InitLLVM X(argc, argv);
|
2013-06-06 02:51:34 +08:00
|
|
|
cl::ParseCommandLineOptions(argc, argv);
|
|
|
|
|
2014-05-16 00:14:02 +08:00
|
|
|
if (OutputFilename.empty())
|
|
|
|
OutputFilename = "-";
|
|
|
|
|
2014-08-26 02:16:47 +08:00
|
|
|
std::error_code EC;
|
2017-09-23 09:03:17 +08:00
|
|
|
std::unique_ptr<ToolOutputFile> Out(
|
|
|
|
new ToolOutputFile(OutputFilename, EC, sys::fs::F_None));
|
2017-04-05 23:18:16 +08:00
|
|
|
if (EC)
|
|
|
|
error("yaml2obj: Error opening '" + OutputFilename + "': " + EC.message());
|
2014-05-16 00:14:02 +08:00
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(Input);
|
|
|
|
if (!Buf)
|
2013-06-06 02:51:34 +08:00
|
|
|
return 1;
|
2014-05-16 00:14:02 +08:00
|
|
|
|
2019-04-25 17:59:55 +08:00
|
|
|
yaml::Input YIn(Buf.get()->getBuffer());
|
2016-06-28 03:53:53 +08:00
|
|
|
int Res = convertYAML(YIn, Out->os());
|
2014-05-16 00:14:02 +08:00
|
|
|
if (Res == 0)
|
|
|
|
Out->keep();
|
|
|
|
|
2017-06-16 07:44:19 +08:00
|
|
|
Out->os().flush();
|
2014-05-16 00:14:02 +08:00
|
|
|
return Res;
|
2012-08-03 03:16:56 +08:00
|
|
|
}
|