OLD | NEW |
1 /* Copyright 2013 The Native Client Authors. All rights reserved. | 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 | 2 * Use of this source code is governed by a BSD-style license that can |
3 * be found in the LICENSE file. | 3 * be found in the LICENSE file. |
4 */ | 4 */ |
5 | 5 |
6 //===-- pnacl-thaw.cpp - The low-level NaCl bitcode thawer ----------------===// | 6 //===-- pnacl-thaw.cpp - The low-level NaCl bitcode thawer ----------------===// |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // Converts NaCl wire format back to LLVM bitcode. | 10 // Converts NaCl wire format back to LLVM bitcode. |
(...skipping 17 matching lines...) Expand all Loading... |
28 | 28 |
29 namespace { | 29 namespace { |
30 | 30 |
31 static cl::opt<std::string> | 31 static cl::opt<std::string> |
32 OutputFilename("o", cl::desc("Specify thawed pexe filename"), | 32 OutputFilename("o", cl::desc("Specify thawed pexe filename"), |
33 cl::value_desc("filename"), cl::init("-")); | 33 cl::value_desc("filename"), cl::init("-")); |
34 | 34 |
35 static cl::opt<std::string> | 35 static cl::opt<std::string> |
36 InputFilename(cl::Positional, cl::desc("<frozen file>"), cl::init("-")); | 36 InputFilename(cl::Positional, cl::desc("<frozen file>"), cl::init("-")); |
37 | 37 |
38 static cl::opt<bool> | |
39 VerboseErrors( | |
40 "verbose-parse-errors", | |
41 cl::desc("Print out more descriptive PNaCl bitcode parse errors"), | |
42 cl::init(false)); | |
43 | |
44 static void WriteOutputFile(const Module *M) { | 38 static void WriteOutputFile(const Module *M) { |
45 | 39 |
46 std::error_code EC; | 40 std::error_code EC; |
47 std::unique_ptr<tool_output_file> Out( | 41 std::unique_ptr<tool_output_file> Out( |
48 new tool_output_file(OutputFilename, EC, sys::fs::F_None)); | 42 new tool_output_file(OutputFilename, EC, sys::fs::F_None)); |
49 if (EC) { | 43 if (EC) { |
50 errs() << EC.message() << '\n'; | 44 errs() << EC.message() << '\n'; |
51 exit(1); | 45 exit(1); |
52 } | 46 } |
53 | 47 |
54 WriteBitcodeToFile(M, Out->os()); | 48 WriteBitcodeToFile(M, Out->os()); |
55 | 49 |
56 // Declare success. | 50 // Declare success. |
57 Out->keep(); | 51 Out->keep(); |
58 } | 52 } |
59 | 53 |
60 static Module *readBitcode( | 54 static Module *readBitcode(std::string &Filename, LLVMContext &Context, |
61 std::string &Filename, LLVMContext &Context, raw_ostream *Verbose, | 55 std::string &ErrorMessage) { |
62 std::string &ErrorMessage) { | |
63 // Use the bitcode streaming interface | 56 // Use the bitcode streaming interface |
64 DataStreamer *Streamer = getDataFileStreamer(InputFilename, &ErrorMessage); | 57 DataStreamer *Streamer = getDataFileStreamer(InputFilename, &ErrorMessage); |
65 if (Streamer == nullptr) | 58 if (Streamer == nullptr) |
66 return nullptr; | 59 return nullptr; |
67 std::unique_ptr<StreamingMemoryObject> Buffer( | 60 std::unique_ptr<StreamingMemoryObject> Buffer( |
68 new StreamingMemoryObjectImpl(Streamer)); | 61 new StreamingMemoryObjectImpl(Streamer)); |
69 std::string DisplayFilename; | 62 std::string DisplayFilename; |
70 if (Filename == "-") | 63 if (Filename == "-") |
71 DisplayFilename = "<stdin>"; | 64 DisplayFilename = "<stdin>"; |
72 else | 65 else |
73 DisplayFilename = Filename; | 66 DisplayFilename = Filename; |
74 Module *M = getNaClStreamedBitcodeModule(DisplayFilename, Buffer.release(), | 67 DiagnosticHandlerFunction DiagnosticHandler = nullptr; |
75 Context, Verbose, | 68 Module *M = getNaClStreamedBitcodeModule( |
76 &ErrorMessage, | 69 DisplayFilename, Buffer.release(), Context, DiagnosticHandler, |
77 /*AcceptSupportedOnly=*/false); | 70 &ErrorMessage, /*AcceptSupportedOnly=*/false); |
78 if (!M) | 71 if (!M) |
79 return nullptr; | 72 return nullptr; |
80 if (std::error_code EC = M->materializeAllPermanently()) { | 73 if (std::error_code EC = M->materializeAllPermanently()) { |
81 ErrorMessage = EC.message(); | 74 ErrorMessage = EC.message(); |
82 delete M; | 75 delete M; |
83 return nullptr; | 76 return nullptr; |
84 } | 77 } |
85 return M; | 78 return M; |
86 } | 79 } |
87 | 80 |
88 } // end of anonymous namespace | 81 } // end of anonymous namespace |
89 | 82 |
90 int main(int argc, char **argv) { | 83 int main(int argc, char **argv) { |
91 // Print a stack trace if we signal out. | 84 // Print a stack trace if we signal out. |
92 sys::PrintStackTraceOnErrorSignal(); | 85 sys::PrintStackTraceOnErrorSignal(); |
93 PrettyStackTraceProgram X(argc, argv); | 86 PrettyStackTraceProgram X(argc, argv); |
94 | 87 |
95 LLVMContext &Context = getGlobalContext(); | 88 LLVMContext &Context = getGlobalContext(); |
96 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. | 89 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
97 | 90 |
98 cl::ParseCommandLineOptions( | 91 cl::ParseCommandLineOptions( |
99 argc, argv, "Converts NaCl pexe wire format into LLVM bitcode format\n"); | 92 argc, argv, "Converts NaCl pexe wire format into LLVM bitcode format\n"); |
100 | 93 |
101 std::string ErrorMessage; | 94 std::string ErrorMessage; |
102 raw_ostream *Verbose = VerboseErrors ? &errs() : nullptr; | 95 std::unique_ptr<Module> M(readBitcode(InputFilename, Context, ErrorMessage)); |
103 std::unique_ptr<Module> M(readBitcode(InputFilename, Context, | |
104 Verbose, ErrorMessage)); | |
105 | 96 |
106 if (!M.get()) { | 97 if (!M.get()) { |
107 errs() << argv[0] << ": "; | 98 errs() << argv[0] << ": "; |
108 if (ErrorMessage.size()) | 99 if (ErrorMessage.size()) |
109 errs() << ErrorMessage << "\n"; | 100 errs() << ErrorMessage << "\n"; |
110 else | 101 else |
111 errs() << "bitcode didn't read correctly.\n"; | 102 errs() << "bitcode didn't read correctly.\n"; |
112 return 1; | 103 return 1; |
113 } | 104 } |
114 | 105 |
115 WriteOutputFile(M.get()); | 106 WriteOutputFile(M.get()); |
116 return 0; | 107 return 0; |
117 } | 108 } |
OLD | NEW |