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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[yaml2obj] Move core yaml2obj code into lib and include for use in unit tests
Reviewers: jhenderson, rupprecht, MaskRay, grimar, labath
Reviewed By: rupprecht
Subscribers: gribozavr, mgrang, seiya, mgorny, sbc100, hiraditya, aheejin, jakehehrlich, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65255
llvm-svn: 368119
2019-08-07 10:44:49 +08:00
|
|
|
#include "llvm/ObjectYAML/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"
|
[yaml2obj] Move core yaml2obj code into lib and include for use in unit tests
Reviewers: jhenderson, rupprecht, MaskRay, grimar, labath
Reviewed By: rupprecht
Subscribers: gribozavr, mgrang, seiya, mgorny, sbc100, hiraditya, aheejin, jakehehrlich, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65255
llvm-svn: 368119
2019-08-07 10:44:49 +08:00
|
|
|
#include "llvm/Support/WithColor.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;
|
|
|
|
|
2020-02-05 02:45:07 +08:00
|
|
|
namespace {
|
|
|
|
cl::OptionCategory Cat("yaml2obj Options");
|
2012-08-03 03:16:56 +08:00
|
|
|
|
2020-02-05 02:45:07 +08:00
|
|
|
cl::opt<std::string> Input(cl::Positional, cl::desc("<input file>"),
|
|
|
|
cl::init("-"), cl::cat(Cat));
|
|
|
|
|
2020-02-04 03:12:04 +08:00
|
|
|
cl::list<std::string>
|
|
|
|
D("D", cl::Prefix,
|
|
|
|
cl::desc("Defined the specified macros to their specified "
|
|
|
|
"definition. The syntax is <macro>=<definition>"));
|
|
|
|
|
2020-02-05 02:45:07 +08:00
|
|
|
cl::opt<unsigned>
|
2020-02-01 02:08:42 +08:00
|
|
|
DocNum("docnum", cl::init(1),
|
2020-02-05 02:45:07 +08:00
|
|
|
cl::desc("Read specified document from input (default = 1)"),
|
|
|
|
cl::cat(Cat));
|
2014-05-31 12:51:07 +08:00
|
|
|
|
2020-06-05 20:50:59 +08:00
|
|
|
static cl::opt<uint64_t> MaxSize(
|
|
|
|
"max-size", cl::init(10 * 1024 * 1024),
|
|
|
|
cl::desc(
|
|
|
|
"Sets the maximum allowed output size (0 means no limit) [ELF only]"));
|
|
|
|
|
2020-02-05 02:45:07 +08:00
|
|
|
cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
|
|
|
|
cl::value_desc("filename"), cl::init("-"),
|
|
|
|
cl::Prefix, cl::cat(Cat));
|
|
|
|
} // namespace
|
2013-06-06 02:51:34 +08:00
|
|
|
|
2020-02-04 03:12:04 +08:00
|
|
|
static Optional<std::string> preprocess(StringRef Buf,
|
|
|
|
yaml::ErrorHandler ErrHandler) {
|
|
|
|
DenseMap<StringRef, StringRef> Defines;
|
|
|
|
for (StringRef Define : D) {
|
|
|
|
StringRef Macro, Definition;
|
|
|
|
std::tie(Macro, Definition) = Define.split('=');
|
|
|
|
if (!Define.count('=') || Macro.empty()) {
|
|
|
|
ErrHandler("invalid syntax for -D: " + Define);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
if (!Defines.try_emplace(Macro, Definition).second) {
|
|
|
|
ErrHandler("'" + Macro + "'" + " redefined");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Preprocessed;
|
|
|
|
while (!Buf.empty()) {
|
|
|
|
if (Buf.startswith("[[")) {
|
|
|
|
size_t I = Buf.find_first_of("[]", 2);
|
|
|
|
if (Buf.substr(I).startswith("]]")) {
|
2020-06-24 20:18:53 +08:00
|
|
|
StringRef MacroExpr = Buf.substr(2, I - 2);
|
|
|
|
StringRef Macro;
|
|
|
|
StringRef Default;
|
|
|
|
std::tie(Macro, Default) = MacroExpr.split('=');
|
|
|
|
|
|
|
|
// When the -D option is requested, we use the provided value.
|
|
|
|
// Otherwise we use a default macro value if present.
|
2020-02-04 03:12:04 +08:00
|
|
|
auto It = Defines.find(Macro);
|
2020-06-24 20:18:53 +08:00
|
|
|
Optional<StringRef> Value;
|
|
|
|
if (It != Defines.end())
|
|
|
|
Value = It->second;
|
|
|
|
else if (!Default.empty() || MacroExpr.endswith("="))
|
|
|
|
Value = Default;
|
|
|
|
|
|
|
|
if (Value) {
|
|
|
|
Preprocessed += *Value;
|
2020-02-04 03:12:04 +08:00
|
|
|
Buf = Buf.substr(I + 2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Preprocessed += Buf[0];
|
|
|
|
Buf = Buf.substr(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Preprocessed;
|
|
|
|
}
|
|
|
|
|
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);
|
2020-02-05 02:45:07 +08:00
|
|
|
cl::HideUnrelatedOptions(Cat);
|
|
|
|
cl::ParseCommandLineOptions(
|
|
|
|
argc, argv, "Create an object file from a YAML description", nullptr,
|
|
|
|
nullptr, /*LongOptionsUseDoubleDash=*/true);
|
2014-05-16 00:14:02 +08:00
|
|
|
|
2019-09-14 00:00:16 +08:00
|
|
|
auto ErrHandler = [](const Twine &Msg) {
|
|
|
|
WithColor::error(errs(), "yaml2obj") << Msg << "\n";
|
|
|
|
};
|
|
|
|
|
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(
|
2019-08-05 13:43:48 +08:00
|
|
|
new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None));
|
2019-09-14 00:00:16 +08:00
|
|
|
if (EC) {
|
|
|
|
ErrHandler("failed to open '" + OutputFilename + "': " + EC.message());
|
|
|
|
return 1;
|
|
|
|
}
|
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
|
|
|
|
2020-02-04 03:12:04 +08:00
|
|
|
Optional<std::string> Buffer = preprocess(Buf.get()->getBuffer(), ErrHandler);
|
|
|
|
if (!Buffer)
|
|
|
|
return 1;
|
|
|
|
yaml::Input YIn(*Buffer);
|
2020-06-05 20:50:59 +08:00
|
|
|
|
|
|
|
if (!convertYAML(YIn, Out->os(), ErrHandler, DocNum,
|
|
|
|
MaxSize == 0 ? UINT64_MAX : MaxSize))
|
[yaml2obj] Move core yaml2obj code into lib and include for use in unit tests
Reviewers: jhenderson, rupprecht, MaskRay, grimar, labath
Reviewed By: rupprecht
Subscribers: gribozavr, mgrang, seiya, mgorny, sbc100, hiraditya, aheejin, jakehehrlich, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65255
llvm-svn: 368119
2019-08-07 10:44:49 +08:00
|
|
|
return 1;
|
2014-05-16 00:14:02 +08:00
|
|
|
|
[yaml2obj] Move core yaml2obj code into lib and include for use in unit tests
Reviewers: jhenderson, rupprecht, MaskRay, grimar, labath
Reviewed By: rupprecht
Subscribers: gribozavr, mgrang, seiya, mgorny, sbc100, hiraditya, aheejin, jakehehrlich, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65255
llvm-svn: 368119
2019-08-07 10:44:49 +08:00
|
|
|
Out->keep();
|
2017-06-16 07:44:19 +08:00
|
|
|
Out->os().flush();
|
[yaml2obj] Move core yaml2obj code into lib and include for use in unit tests
Reviewers: jhenderson, rupprecht, MaskRay, grimar, labath
Reviewed By: rupprecht
Subscribers: gribozavr, mgrang, seiya, mgorny, sbc100, hiraditya, aheejin, jakehehrlich, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65255
llvm-svn: 368119
2019-08-07 10:44:49 +08:00
|
|
|
return 0;
|
2012-08-03 03:16:56 +08:00
|
|
|
}
|