OLD | NEW |
(Empty) | |
| 1 /* Copyright 2013 The Native Client Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can |
| 3 * be found in the LICENSE file. |
| 4 */ |
| 5 |
| 6 //===-- pnacl-thaw.cpp - The low-level NaCl bitcode thawer ----------------===// |
| 7 // |
| 8 //===----------------------------------------------------------------------===// |
| 9 // |
| 10 // Converts NaCl wire format back to LLVM bitcode. |
| 11 // |
| 12 //===----------------------------------------------------------------------===// |
| 13 |
| 14 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" |
| 15 #include "llvm/Bitcode/ReaderWriter.h" |
| 16 #include "llvm/IR/LLVMContext.h" |
| 17 #include "llvm/IR/Module.h" |
| 18 #include "llvm/Support/CommandLine.h" |
| 19 #include "llvm/Support/DataStream.h" |
| 20 #include "llvm/Support/FileSystem.h" |
| 21 #include "llvm/Support/ManagedStatic.h" |
| 22 #include "llvm/Support/PrettyStackTrace.h" |
| 23 #include "llvm/Support/Signals.h" |
| 24 #include "llvm/Support/StreamingMemoryObject.h" |
| 25 #include "llvm/Support/ToolOutputFile.h" |
| 26 |
| 27 using namespace llvm; |
| 28 |
| 29 static cl::opt<std::string> |
| 30 OutputFilename("o", cl::desc("Specify thawed pexe filename"), |
| 31 cl::value_desc("filename"), cl::init("-")); |
| 32 |
| 33 static cl::opt<std::string> |
| 34 InputFilename(cl::Positional, cl::desc("<frozen file>"), cl::init("-")); |
| 35 |
| 36 static cl::opt<bool> |
| 37 VerboseErrors( |
| 38 "verbose-parse-errors", |
| 39 cl::desc("Print out more descriptive PNaCl bitcode parse errors"), |
| 40 cl::init(false)); |
| 41 |
| 42 static void WriteOutputFile(const Module *M) { |
| 43 |
| 44 std::error_code EC; |
| 45 std::unique_ptr<tool_output_file> Out( |
| 46 new tool_output_file(OutputFilename, EC, sys::fs::F_None)); |
| 47 if (EC) { |
| 48 errs() << EC.message() << '\n'; |
| 49 exit(1); |
| 50 } |
| 51 |
| 52 WriteBitcodeToFile(M, Out->os()); |
| 53 |
| 54 // Declare success. |
| 55 Out->keep(); |
| 56 } |
| 57 |
| 58 int main(int argc, char **argv) { |
| 59 // Print a stack trace if we signal out. |
| 60 sys::PrintStackTraceOnErrorSignal(); |
| 61 PrettyStackTraceProgram X(argc, argv); |
| 62 |
| 63 LLVMContext &Context = getGlobalContext(); |
| 64 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 65 |
| 66 cl::ParseCommandLineOptions( |
| 67 argc, argv, "Converts NaCl pexe wire format into LLVM bitcode format\n"); |
| 68 |
| 69 std::string ErrorMessage; |
| 70 std::auto_ptr<Module> M; |
| 71 |
| 72 // Use the bitcode streaming interface |
| 73 DataStreamer *streamer = getDataFileStreamer(InputFilename, &ErrorMessage); |
| 74 std::unique_ptr<StreamingMemoryObject> Buffer( |
| 75 new StreamingMemoryObjectImpl(streamer)); |
| 76 if (streamer) { |
| 77 std::string DisplayFilename; |
| 78 if (InputFilename == "-") |
| 79 DisplayFilename = "<stdin>"; |
| 80 else |
| 81 DisplayFilename = InputFilename; |
| 82 raw_ostream *Verbose = VerboseErrors ? &errs() : nullptr; |
| 83 M.reset(getNaClStreamedBitcodeModule(DisplayFilename, Buffer.release(), |
| 84 Context, Verbose, |
| 85 &ErrorMessage, |
| 86 /*AcceptSupportedOnly=*/false)); |
| 87 if (M.get()) |
| 88 if (std::error_code EC = M->materializeAllPermanently()) { |
| 89 ErrorMessage = EC.message(); |
| 90 M.reset(); |
| 91 } |
| 92 } |
| 93 |
| 94 if (!M.get()) { |
| 95 errs() << argv[0] << ": "; |
| 96 if (ErrorMessage.size()) |
| 97 errs() << ErrorMessage << "\n"; |
| 98 else |
| 99 errs() << "bitcode didn't read correctly.\n"; |
| 100 return 1; |
| 101 } |
| 102 |
| 103 WriteOutputFile(M.get()); |
| 104 return 0; |
| 105 } |
OLD | NEW |