macho-dump: add code to print LC_ID_DYLIB load commands.
[oota-llvm.git] / tools / yaml2obj / yaml2obj.cpp
index cc0fecc3c6384eb1505fcd22b20b49c4de04b1d0..dd41951e7db8924dd8b928be6409ddd12bccd08e 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "yaml2obj.h"
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/FileSystem.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/Signals.h"
+#include "llvm/Support/ToolOutputFile.h"
+#include "llvm/Support/YAMLTraits.h"
 #include "llvm/Support/raw_ostream.h"
-#include "llvm/Support/system_error.h"
+#include <system_error>
 
 using namespace llvm;
 
@@ -49,6 +53,26 @@ cl::opt<YAMLObjectFormat> Format(
     clEnumValN(YOF_ELF, "elf", "ELF object file format"),
   clEnumValEnd));
 
+cl::opt<unsigned>
+DocNum("docnum", cl::init(1),
+       cl::desc("Read specified document from input (default = 1)"));
+
+static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
+                                           cl::value_desc("filename"));
+
+typedef int (*ConvertFuncPtr)(yaml::Input & YIn, raw_ostream &Out);
+
+int convertYAML(yaml::Input & YIn, raw_ostream &Out, ConvertFuncPtr Convert) {
+  unsigned CurDocNum = 0;
+  do {
+    if (++CurDocNum == DocNum)
+      return Convert(YIn, Out);
+  } while (YIn.nextDocument());
+
+  errs() << "yaml2obj: Cannot find the " << DocNum
+         << llvm::getOrdinalSuffix(DocNum) << " document\n";
+  return 1;
+}
 
 int main(int argc, char **argv) {
   cl::ParseCommandLineOptions(argc, argv);
@@ -56,15 +80,36 @@ int main(int argc, char **argv) {
   PrettyStackTraceProgram X(argc, argv);
   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
 
+  if (OutputFilename.empty())
+    OutputFilename = "-";
+
+  std::string ErrorInfo;
+  std::unique_ptr<tool_output_file> Out(
+      new tool_output_file(OutputFilename.c_str(), ErrorInfo, sys::fs::F_None));
+  if (!ErrorInfo.empty()) {
+    errs() << ErrorInfo << '\n';
+    return 1;
+  }
+
   std::unique_ptr<MemoryBuffer> Buf;
   if (MemoryBuffer::getFileOrSTDIN(Input, Buf))
     return 1;
-  if (Format == YOF_COFF) {
-    return yaml2coff(outs(), Buf.get());
-  } else if (Format == YOF_ELF) {
-    return yaml2elf(outs(), Buf.get());
-  } else {
+
+  ConvertFuncPtr Convert = nullptr;
+  if (Format == YOF_COFF)
+    Convert = yaml2coff;
+  else if (Format == YOF_ELF)
+    Convert = yaml2elf;
+  else {
     errs() << "Not yet implemented\n";
     return 1;
   }
+
+  yaml::Input YIn(Buf->getBuffer());
+
+  int Res = convertYAML(YIn, Out->os(), Convert);
+  if (Res == 0)
+    Out->keep();
+
+  return Res;
 }