13add3e9cSRafael Espindola //===- yaml2obj - Convert YAML to a binary object file --------------------===// 23add3e9cSRafael Espindola // 33add3e9cSRafael Espindola // The LLVM Compiler Infrastructure 43add3e9cSRafael Espindola // 53add3e9cSRafael Espindola // This file is distributed under the University of Illinois Open Source 63add3e9cSRafael Espindola // License. See LICENSE.TXT for details. 73add3e9cSRafael Espindola // 83add3e9cSRafael Espindola //===----------------------------------------------------------------------===// 93add3e9cSRafael Espindola // 103add3e9cSRafael Espindola // This program takes a YAML description of an object file and outputs the 113add3e9cSRafael Espindola // binary equivalent. 123add3e9cSRafael Espindola // 133add3e9cSRafael Espindola // This is used for writing tests that require binary files. 143add3e9cSRafael Espindola // 153add3e9cSRafael Espindola //===----------------------------------------------------------------------===// 163add3e9cSRafael Espindola 173b76e404SSean Silva #include "yaml2obj.h" 18f97af8a0SSimon Atanasyan #include "llvm/ADT/StringExtras.h" 193add3e9cSRafael Espindola #include "llvm/Support/CommandLine.h" 2073e047e5SSimon Atanasyan #include "llvm/Support/FileSystem.h" 213add3e9cSRafael Espindola #include "llvm/Support/ManagedStatic.h" 223add3e9cSRafael Espindola #include "llvm/Support/MemoryBuffer.h" 233add3e9cSRafael Espindola #include "llvm/Support/PrettyStackTrace.h" 243add3e9cSRafael Espindola #include "llvm/Support/Signals.h" 2573e047e5SSimon Atanasyan #include "llvm/Support/ToolOutputFile.h" 26f97af8a0SSimon Atanasyan #include "llvm/Support/YAMLTraits.h" 27a6e9c3e4SRafael Espindola #include "llvm/Support/raw_ostream.h" 28a6e9c3e4SRafael Espindola #include <system_error> 293add3e9cSRafael Espindola 303add3e9cSRafael Espindola using namespace llvm; 313add3e9cSRafael Espindola 323add3e9cSRafael Espindola static cl::opt<std::string> 333add3e9cSRafael Espindola Input(cl::Positional, cl::desc("<input>"), cl::init("-")); 343add3e9cSRafael Espindola 35741cebbeSSean Silva // TODO: The "right" way to tell what kind of object file a given YAML file 36741cebbeSSean Silva // corresponds to is to look at YAML "tags" (e.g. `!Foo`). Then, different 37741cebbeSSean Silva // tags (`!ELF`, `!COFF`, etc.) would be used to discriminate between them. 38741cebbeSSean Silva // Interpreting the tags is needed eventually for when writing test cases, 39741cebbeSSean Silva // so that we can e.g. have `!Archive` contain a sequence of `!ELF`, and 40741cebbeSSean Silva // just Do The Right Thing. However, interpreting these tags and acting on 41741cebbeSSean Silva // them appropriately requires some work in the YAML parser and the YAMLIO 42741cebbeSSean Silva // library. 43741cebbeSSean Silva enum YAMLObjectFormat { 44f99309cbSSean Silva YOF_COFF, 45b83c1871SChris Bieneman YOF_ELF, 46b83c1871SChris Bieneman YOF_MACHO 47741cebbeSSean Silva }; 48741cebbeSSean Silva 49741cebbeSSean Silva cl::opt<YAMLObjectFormat> Format( 50741cebbeSSean Silva "format", 51741cebbeSSean Silva cl::desc("Interpret input as this type of object file"), 52741cebbeSSean Silva cl::values( 53741cebbeSSean Silva clEnumValN(YOF_COFF, "coff", "COFF object file format"), 54f99309cbSSean Silva clEnumValN(YOF_ELF, "elf", "ELF object file format"), 55b83c1871SChris Bieneman clEnumValN(YOF_MACHO, "macho", "Mach-O object file format"), 56741cebbeSSean Silva clEnumValEnd)); 57741cebbeSSean Silva 58f97af8a0SSimon Atanasyan cl::opt<unsigned> 59f97af8a0SSimon Atanasyan DocNum("docnum", cl::init(1), 60f97af8a0SSimon Atanasyan cl::desc("Read specified document from input (default = 1)")); 61f97af8a0SSimon Atanasyan 6273e047e5SSimon Atanasyan static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"), 6373e047e5SSimon Atanasyan cl::value_desc("filename")); 64741cebbeSSean Silva 65f97af8a0SSimon Atanasyan typedef int (*ConvertFuncPtr)(yaml::Input & YIn, raw_ostream &Out); 66f97af8a0SSimon Atanasyan 67f044d3f9SBenjamin Kramer static int convertYAML(yaml::Input &YIn, raw_ostream &Out, 68f044d3f9SBenjamin Kramer ConvertFuncPtr Convert) { 69f97af8a0SSimon Atanasyan unsigned CurDocNum = 0; 70f97af8a0SSimon Atanasyan do { 71f97af8a0SSimon Atanasyan if (++CurDocNum == DocNum) 72f97af8a0SSimon Atanasyan return Convert(YIn, Out); 73f97af8a0SSimon Atanasyan } while (YIn.nextDocument()); 74f97af8a0SSimon Atanasyan 75f97af8a0SSimon Atanasyan errs() << "yaml2obj: Cannot find the " << DocNum 76f97af8a0SSimon Atanasyan << llvm::getOrdinalSuffix(DocNum) << " document\n"; 77f97af8a0SSimon Atanasyan return 1; 78f97af8a0SSimon Atanasyan } 79f97af8a0SSimon Atanasyan 80741cebbeSSean Silva int main(int argc, char **argv) { 81741cebbeSSean Silva cl::ParseCommandLineOptions(argc, argv); 82*2ad6d48bSRichard Smith sys::PrintStackTraceOnErrorSignal(argv[0]); 83741cebbeSSean Silva PrettyStackTraceProgram X(argc, argv); 84741cebbeSSean Silva llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 85741cebbeSSean Silva 8673e047e5SSimon Atanasyan if (OutputFilename.empty()) 8773e047e5SSimon Atanasyan OutputFilename = "-"; 8873e047e5SSimon Atanasyan 893fd1e993SRafael Espindola std::error_code EC; 9073e047e5SSimon Atanasyan std::unique_ptr<tool_output_file> Out( 913fd1e993SRafael Espindola new tool_output_file(OutputFilename, EC, sys::fs::F_None)); 923fd1e993SRafael Espindola if (EC) { 933fd1e993SRafael Espindola errs() << EC.message() << '\n'; 9473e047e5SSimon Atanasyan return 1; 9573e047e5SSimon Atanasyan } 9673e047e5SSimon Atanasyan 97adf21f2aSRafael Espindola ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = 98adf21f2aSRafael Espindola MemoryBuffer::getFileOrSTDIN(Input); 99adf21f2aSRafael Espindola if (!Buf) 100741cebbeSSean Silva return 1; 10173e047e5SSimon Atanasyan 102f97af8a0SSimon Atanasyan ConvertFuncPtr Convert = nullptr; 10373e047e5SSimon Atanasyan if (Format == YOF_COFF) 104f97af8a0SSimon Atanasyan Convert = yaml2coff; 10573e047e5SSimon Atanasyan else if (Format == YOF_ELF) 106f97af8a0SSimon Atanasyan Convert = yaml2elf; 107b83c1871SChris Bieneman else if (Format == YOF_MACHO) 108b83c1871SChris Bieneman Convert = yaml2macho; 109f97af8a0SSimon Atanasyan else { 110741cebbeSSean Silva errs() << "Not yet implemented\n"; 111f97af8a0SSimon Atanasyan return 1; 112f97af8a0SSimon Atanasyan } 11373e047e5SSimon Atanasyan 114adf21f2aSRafael Espindola yaml::Input YIn(Buf.get()->getBuffer()); 115f97af8a0SSimon Atanasyan 116f97af8a0SSimon Atanasyan int Res = convertYAML(YIn, Out->os(), Convert); 11773e047e5SSimon Atanasyan if (Res == 0) 11873e047e5SSimon Atanasyan Out->keep(); 11973e047e5SSimon Atanasyan 12073e047e5SSimon Atanasyan return Res; 1213add3e9cSRafael Espindola } 122