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" 27*a6e9c3e4SRafael Espindola #include "llvm/Support/raw_ostream.h" 28*a6e9c3e4SRafael 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, 45f99309cbSSean Silva YOF_ELF 46741cebbeSSean Silva }; 47741cebbeSSean Silva 48741cebbeSSean Silva cl::opt<YAMLObjectFormat> Format( 49741cebbeSSean Silva "format", 50741cebbeSSean Silva cl::desc("Interpret input as this type of object file"), 51741cebbeSSean Silva cl::values( 52741cebbeSSean Silva clEnumValN(YOF_COFF, "coff", "COFF object file format"), 53f99309cbSSean Silva clEnumValN(YOF_ELF, "elf", "ELF object file format"), 54741cebbeSSean Silva clEnumValEnd)); 55741cebbeSSean Silva 56f97af8a0SSimon Atanasyan cl::opt<unsigned> 57f97af8a0SSimon Atanasyan DocNum("docnum", cl::init(1), 58f97af8a0SSimon Atanasyan cl::desc("Read specified document from input (default = 1)")); 59f97af8a0SSimon Atanasyan 6073e047e5SSimon Atanasyan static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"), 6173e047e5SSimon Atanasyan cl::value_desc("filename")); 62741cebbeSSean Silva 63f97af8a0SSimon Atanasyan typedef int (*ConvertFuncPtr)(yaml::Input & YIn, raw_ostream &Out); 64f97af8a0SSimon Atanasyan 65f97af8a0SSimon Atanasyan int convertYAML(yaml::Input & YIn, raw_ostream &Out, ConvertFuncPtr Convert) { 66f97af8a0SSimon Atanasyan unsigned CurDocNum = 0; 67f97af8a0SSimon Atanasyan do { 68f97af8a0SSimon Atanasyan if (++CurDocNum == DocNum) 69f97af8a0SSimon Atanasyan return Convert(YIn, Out); 70f97af8a0SSimon Atanasyan } while (YIn.nextDocument()); 71f97af8a0SSimon Atanasyan 72f97af8a0SSimon Atanasyan errs() << "yaml2obj: Cannot find the " << DocNum 73f97af8a0SSimon Atanasyan << llvm::getOrdinalSuffix(DocNum) << " document\n"; 74f97af8a0SSimon Atanasyan return 1; 75f97af8a0SSimon Atanasyan } 76f97af8a0SSimon Atanasyan 77741cebbeSSean Silva int main(int argc, char **argv) { 78741cebbeSSean Silva cl::ParseCommandLineOptions(argc, argv); 79741cebbeSSean Silva sys::PrintStackTraceOnErrorSignal(); 80741cebbeSSean Silva PrettyStackTraceProgram X(argc, argv); 81741cebbeSSean Silva llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 82741cebbeSSean Silva 8373e047e5SSimon Atanasyan if (OutputFilename.empty()) 8473e047e5SSimon Atanasyan OutputFilename = "-"; 8573e047e5SSimon Atanasyan 8673e047e5SSimon Atanasyan std::string ErrorInfo; 8773e047e5SSimon Atanasyan std::unique_ptr<tool_output_file> Out( 8873e047e5SSimon Atanasyan new tool_output_file(OutputFilename.c_str(), ErrorInfo, sys::fs::F_None)); 8973e047e5SSimon Atanasyan if (!ErrorInfo.empty()) { 9073e047e5SSimon Atanasyan errs() << ErrorInfo << '\n'; 9173e047e5SSimon Atanasyan return 1; 9273e047e5SSimon Atanasyan } 9373e047e5SSimon Atanasyan 9456440fd8SAhmed Charles std::unique_ptr<MemoryBuffer> Buf; 95741cebbeSSean Silva if (MemoryBuffer::getFileOrSTDIN(Input, Buf)) 96741cebbeSSean Silva return 1; 9773e047e5SSimon Atanasyan 98f97af8a0SSimon Atanasyan ConvertFuncPtr Convert = nullptr; 9973e047e5SSimon Atanasyan if (Format == YOF_COFF) 100f97af8a0SSimon Atanasyan Convert = yaml2coff; 10173e047e5SSimon Atanasyan else if (Format == YOF_ELF) 102f97af8a0SSimon Atanasyan Convert = yaml2elf; 103f97af8a0SSimon Atanasyan else { 104741cebbeSSean Silva errs() << "Not yet implemented\n"; 105f97af8a0SSimon Atanasyan return 1; 106f97af8a0SSimon Atanasyan } 10773e047e5SSimon Atanasyan 108f97af8a0SSimon Atanasyan yaml::Input YIn(Buf->getBuffer()); 109f97af8a0SSimon Atanasyan 110f97af8a0SSimon Atanasyan int Res = convertYAML(YIn, Out->os(), Convert); 11173e047e5SSimon Atanasyan if (Res == 0) 11273e047e5SSimon Atanasyan Out->keep(); 11373e047e5SSimon Atanasyan 11473e047e5SSimon Atanasyan return Res; 1153add3e9cSRafael Espindola } 116