llvm-project
155 строк · 4.7 Кб
1//===- yaml2obj - Convert YAML to a binary object file --------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This program takes a YAML description of an object file and outputs the
10// binary equivalent.
11//
12// This is used for writing tests that require binary files.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/ObjectYAML/yaml2obj.h"17#include "llvm/ADT/StringExtras.h"18#include "llvm/ObjectYAML/ObjectYAML.h"19#include "llvm/Support/CommandLine.h"20#include "llvm/Support/FileSystem.h"21#include "llvm/Support/InitLLVM.h"22#include "llvm/Support/MemoryBuffer.h"23#include "llvm/Support/ToolOutputFile.h"24#include "llvm/Support/WithColor.h"25#include "llvm/Support/YAMLTraits.h"26#include "llvm/Support/raw_ostream.h"27#include <optional>28#include <system_error>29
30using namespace llvm;31
32namespace {33cl::OptionCategory Cat("yaml2obj Options");34
35cl::opt<std::string> Input(cl::Positional, cl::desc("<input file>"),36cl::init("-"), cl::cat(Cat));37
38cl::list<std::string>39D("D", cl::Prefix,40cl::desc("Defined the specified macros to their specified "41"definition. The syntax is <macro>=<definition>"),42cl::cat(Cat));43
44cl::opt<bool> PreprocessOnly("E", cl::desc("Just print the preprocessed file"),45cl::cat(Cat));46
47cl::opt<unsigned>48DocNum("docnum", cl::init(1),49cl::desc("Read specified document from input (default = 1)"),50cl::cat(Cat));51
52static cl::opt<uint64_t> MaxSize(53"max-size", cl::init(10 * 1024 * 1024),54cl::desc(55"Sets the maximum allowed output size (0 means no limit) [ELF only]"),56cl::cat(Cat));57
58cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),59cl::value_desc("filename"), cl::init("-"),60cl::Prefix, cl::cat(Cat));61} // namespace62
63static std::optional<std::string> preprocess(StringRef Buf,64yaml::ErrorHandler ErrHandler) {65DenseMap<StringRef, StringRef> Defines;66for (StringRef Define : D) {67StringRef Macro, Definition;68std::tie(Macro, Definition) = Define.split('=');69if (!Define.count('=') || Macro.empty()) {70ErrHandler("invalid syntax for -D: " + Define);71return {};72}73if (!Defines.try_emplace(Macro, Definition).second) {74ErrHandler("'" + Macro + "'" + " redefined");75return {};76}77}78
79std::string Preprocessed;80while (!Buf.empty()) {81if (Buf.starts_with("[[")) {82size_t I = Buf.find_first_of("[]", 2);83if (Buf.substr(I).starts_with("]]")) {84StringRef MacroExpr = Buf.substr(2, I - 2);85StringRef Macro;86StringRef Default;87std::tie(Macro, Default) = MacroExpr.split('=');88
89// When the -D option is requested, we use the provided value.90// Otherwise we use a default macro value if present.91auto It = Defines.find(Macro);92std::optional<StringRef> Value;93if (It != Defines.end())94Value = It->second;95else if (!Default.empty() || MacroExpr.ends_with("="))96Value = Default;97
98if (Value) {99Preprocessed += *Value;100Buf = Buf.substr(I + 2);101continue;102}103}104}105
106Preprocessed += Buf[0];107Buf = Buf.substr(1);108}109
110return Preprocessed;111}
112
113int main(int argc, char **argv) {114InitLLVM X(argc, argv);115cl::HideUnrelatedOptions(Cat);116cl::ParseCommandLineOptions(117argc, argv, "Create an object file from a YAML description", nullptr,118nullptr, /*LongOptionsUseDoubleDash=*/true);119
120auto ErrHandler = [](const Twine &Msg) {121WithColor::error(errs(), "yaml2obj") << Msg << "\n";122};123
124std::error_code EC;125std::unique_ptr<ToolOutputFile> Out(126new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None));127if (EC) {128ErrHandler("failed to open '" + OutputFilename + "': " + EC.message());129return 1;130}131
132ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =133MemoryBuffer::getFileOrSTDIN(Input, /*IsText=*/true);134if (!Buf)135return 1;136
137std::optional<std::string> Buffer =138preprocess(Buf.get()->getBuffer(), ErrHandler);139if (!Buffer)140return 1;141
142if (PreprocessOnly) {143Out->os() << Buffer;144} else {145yaml::Input YIn(*Buffer);146
147if (!convertYAML(YIn, Out->os(), ErrHandler, DocNum,148MaxSize == 0 ? UINT64_MAX : MaxSize))149return 1;150}151
152Out->keep();153Out->os().flush();154return 0;155}
156