Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 1 | //===- 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 Silva | 6ed30e0 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 17 | #include "yaml2obj.h" |
Simon Atanasyan | df96c56 | 2014-05-31 04:51:07 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
Chris Bieneman | 9cf90e6 | 2016-06-27 19:53:53 +0000 | [diff] [blame] | 19 | #include "llvm/ObjectYAML/ObjectYAML.h" |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CommandLine.h" |
Simon Atanasyan | 926273d | 2014-05-15 16:14:02 +0000 | [diff] [blame] | 21 | #include "llvm/Support/FileSystem.h" |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 22 | #include "llvm/Support/InitLLVM.h" |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
Simon Atanasyan | 926273d | 2014-05-15 16:14:02 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ToolOutputFile.h" |
Simon Atanasyan | df96c56 | 2014-05-31 04:51:07 +0000 | [diff] [blame] | 25 | #include "llvm/Support/YAMLTraits.h" |
Rafael Espindola | d5132f9 | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | #include <system_error> |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace llvm; |
| 30 | |
| 31 | static cl::opt<std::string> |
| 32 | Input(cl::Positional, cl::desc("<input>"), cl::init("-")); |
| 33 | |
Simon Atanasyan | df96c56 | 2014-05-31 04:51:07 +0000 | [diff] [blame] | 34 | cl::opt<unsigned> |
| 35 | DocNum("docnum", cl::init(1), |
| 36 | cl::desc("Read specified document from input (default = 1)")); |
| 37 | |
Simon Atanasyan | 926273d | 2014-05-15 16:14:02 +0000 | [diff] [blame] | 38 | static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"), |
| 39 | cl::value_desc("filename")); |
Sean Silva | db9dc53 | 2013-06-05 18:51:34 +0000 | [diff] [blame] | 40 | |
Davide Italiano | 6bc97db | 2017-04-05 15:18:16 +0000 | [diff] [blame] | 41 | LLVM_ATTRIBUTE_NORETURN static void error(Twine Message) { |
| 42 | errs() << Message << "\n"; |
| 43 | exit(1); |
| 44 | } |
| 45 | |
Chris Bieneman | 9cf90e6 | 2016-06-27 19:53:53 +0000 | [diff] [blame] | 46 | static int convertYAML(yaml::Input &YIn, raw_ostream &Out) { |
Simon Atanasyan | df96c56 | 2014-05-31 04:51:07 +0000 | [diff] [blame] | 47 | unsigned CurDocNum = 0; |
| 48 | do { |
Chris Bieneman | 9cf90e6 | 2016-06-27 19:53:53 +0000 | [diff] [blame] | 49 | if (++CurDocNum == DocNum) { |
| 50 | yaml::YamlObjectFile Doc; |
| 51 | YIn >> Doc; |
Davide Italiano | 6bc97db | 2017-04-05 15:18:16 +0000 | [diff] [blame] | 52 | if (YIn.error()) |
| 53 | error("yaml2obj: Failed to parse YAML file!"); |
Chris Bieneman | 9cf90e6 | 2016-06-27 19:53:53 +0000 | [diff] [blame] | 54 | if (Doc.Elf) |
| 55 | return yaml2elf(*Doc.Elf, Out); |
| 56 | if (Doc.Coff) |
| 57 | return yaml2coff(*Doc.Coff, Out); |
| 58 | if (Doc.MachO || Doc.FatMachO) |
| 59 | return yaml2macho(Doc, Out); |
Derek Schuff | 349a48f | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 60 | if (Doc.Wasm) |
| 61 | return yaml2wasm(*Doc.Wasm, Out); |
Davide Italiano | 6bc97db | 2017-04-05 15:18:16 +0000 | [diff] [blame] | 62 | error("yaml2obj: Unknown document type!"); |
Chris Bieneman | 9cf90e6 | 2016-06-27 19:53:53 +0000 | [diff] [blame] | 63 | } |
Simon Atanasyan | df96c56 | 2014-05-31 04:51:07 +0000 | [diff] [blame] | 64 | } while (YIn.nextDocument()); |
| 65 | |
Benjamin Kramer | ca5092a | 2017-12-28 16:58:54 +0000 | [diff] [blame] | 66 | error("yaml2obj: Cannot find the " + Twine(DocNum) + |
Davide Italiano | 6bc97db | 2017-04-05 15:18:16 +0000 | [diff] [blame] | 67 | llvm::getOrdinalSuffix(DocNum) + " document"); |
Simon Atanasyan | df96c56 | 2014-05-31 04:51:07 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Sean Silva | db9dc53 | 2013-06-05 18:51:34 +0000 | [diff] [blame] | 70 | int main(int argc, char **argv) { |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 71 | InitLLVM X(argc, argv); |
Sean Silva | db9dc53 | 2013-06-05 18:51:34 +0000 | [diff] [blame] | 72 | cl::ParseCommandLineOptions(argc, argv); |
Sean Silva | db9dc53 | 2013-06-05 18:51:34 +0000 | [diff] [blame] | 73 | |
Simon Atanasyan | 926273d | 2014-05-15 16:14:02 +0000 | [diff] [blame] | 74 | if (OutputFilename.empty()) |
| 75 | OutputFilename = "-"; |
| 76 | |
Rafael Espindola | 8c96862 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 77 | std::error_code EC; |
Reid Kleckner | 97ca964 | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 78 | std::unique_ptr<ToolOutputFile> Out( |
| 79 | new ToolOutputFile(OutputFilename, EC, sys::fs::F_None)); |
Davide Italiano | 6bc97db | 2017-04-05 15:18:16 +0000 | [diff] [blame] | 80 | if (EC) |
| 81 | error("yaml2obj: Error opening '" + OutputFilename + "': " + EC.message()); |
Simon Atanasyan | 926273d | 2014-05-15 16:14:02 +0000 | [diff] [blame] | 82 | |
Rafael Espindola | 7cba2a9 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 83 | ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = |
| 84 | MemoryBuffer::getFileOrSTDIN(Input); |
| 85 | if (!Buf) |
Sean Silva | db9dc53 | 2013-06-05 18:51:34 +0000 | [diff] [blame] | 86 | return 1; |
Simon Atanasyan | 926273d | 2014-05-15 16:14:02 +0000 | [diff] [blame] | 87 | |
Rafael Espindola | 7cba2a9 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 88 | yaml::Input YIn(Buf.get()->getBuffer()); |
Simon Atanasyan | df96c56 | 2014-05-31 04:51:07 +0000 | [diff] [blame] | 89 | |
Chris Bieneman | 9cf90e6 | 2016-06-27 19:53:53 +0000 | [diff] [blame] | 90 | int Res = convertYAML(YIn, Out->os()); |
Simon Atanasyan | 926273d | 2014-05-15 16:14:02 +0000 | [diff] [blame] | 91 | if (Res == 0) |
| 92 | Out->keep(); |
| 93 | |
Zachary Turner | ae75a98 | 2017-06-15 23:44:19 +0000 | [diff] [blame] | 94 | Out->os().flush(); |
Simon Atanasyan | 926273d | 2014-05-15 16:14:02 +0000 | [diff] [blame] | 95 | return Res; |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 96 | } |