diff options
author | Karl Schimpf <kschimpf@google.com> | 2013-05-01 10:42:30 -0700 |
---|---|---|
committer | Karl Schimpf <kschimpf@google.com> | 2013-05-01 10:42:30 -0700 |
commit | f42b26d0d46034cb2b91df810477fc0df2e67b27 (patch) | |
tree | bc8749b8fa6e36670df3171a27d9db2bfab608a7 /tools/pnacl-thaw | |
parent | 595239b2b7297b62d9f804770f5f43d8bf637a0f (diff) |
Copy LLVM bitcode reader to generate a PNaCl wire format reader.
Copy classes for LLVM BitcodeReader into a NaCl subdirectory, to create a wire
format version. Renames classes/functions to include NaCl prefix, so that
they don't conflict with the LLVM Bitcode reader.
Also implements pnacl-thaw, showing that we can read the PNaCl wire format
files.
BUG= https://code.google.com/p/nativeclient/issues/detail?id=3405
R=jvoung@chromium.org
Review URL: https://codereview.chromium.org/14314016
Diffstat (limited to 'tools/pnacl-thaw')
-rw-r--r-- | tools/pnacl-thaw/CMakeLists.txt | 5 | ||||
-rw-r--r-- | tools/pnacl-thaw/LLVMBuild.txt | 16 | ||||
-rw-r--r-- | tools/pnacl-thaw/Makefile | 17 | ||||
-rw-r--r-- | tools/pnacl-thaw/pnacl-thaw.cpp | 94 |
4 files changed, 132 insertions, 0 deletions
diff --git a/tools/pnacl-thaw/CMakeLists.txt b/tools/pnacl-thaw/CMakeLists.txt new file mode 100644 index 0000000000..91b818efe6 --- /dev/null +++ b/tools/pnacl-thaw/CMakeLists.txt @@ -0,0 +1,5 @@ +set(LLVM_LINK_COMPONENTS bitwriter naclbitreader) + +add_llvm_tool(pnacl-thaw + pnacl-thaw.cpp + ) diff --git a/tools/pnacl-thaw/LLVMBuild.txt b/tools/pnacl-thaw/LLVMBuild.txt new file mode 100644 index 0000000000..864da2cbd5 --- /dev/null +++ b/tools/pnacl-thaw/LLVMBuild.txt @@ -0,0 +1,16 @@ +;===- ./tools/pnacl-thaw/LLVMBuild.txt -----------------------*- Conf -*--===; +;===------------------------------------------------------------------------===; +; +; This is an LLVMBuild description file for the components in this subdirectory. +; +; For more information on the LLVMBuild system, please see: +; +; http://llvm.org/docs/LLVMBuild.html +; +;===------------------------------------------------------------------------===; + +[component_0] +type = Tool +name = pnacl-thaw +parent = Tools +required_libraries = BitWriter NaClBitReader diff --git a/tools/pnacl-thaw/Makefile b/tools/pnacl-thaw/Makefile new file mode 100644 index 0000000000..8e7699e185 --- /dev/null +++ b/tools/pnacl-thaw/Makefile @@ -0,0 +1,17 @@ +##===- tools/pnacl-thaw/Makefile ---------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LEVEL := ../.. +TOOLNAME := pnacl-thaw +LINK_COMPONENTS := bitwriter naclbitreader + +# This tool has no plugins, optimize startup time. +TOOL_NO_EXPORTS := 1 + +include $(LEVEL)/Makefile.common diff --git a/tools/pnacl-thaw/pnacl-thaw.cpp b/tools/pnacl-thaw/pnacl-thaw.cpp new file mode 100644 index 0000000000..b28c357165 --- /dev/null +++ b/tools/pnacl-thaw/pnacl-thaw.cpp @@ -0,0 +1,94 @@ +/* Copyright 2013 The Native Client Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can + * be found in the LICENSE file. + */ + +//===-- pnacl-thaw.cpp - The low-level NaCl bitcode thawer ----------------===// +// +//===----------------------------------------------------------------------===// +// +// Converts NaCl wire format back to LLVM bitcode. +// +//===----------------------------------------------------------------------===// + +#include "llvm/IR/LLVMContext.h" +// Note: We need the following to provide the API for calling the NaCl +// Bitcode Reader to read the frozen file. +#include "llvm/Bitcode/NaCl/NaClReaderWriter.h" +// Note: We need the following to provide the API for calling the (LLVM) +// Bitcode Writer to generate the corresponding LLVM bitcode file. +#include "llvm/Bitcode/ReaderWriter.h" +#include "llvm/IR/Module.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/DataStream.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/ToolOutputFile.h" + +using namespace llvm; + +static cl::opt<std::string> +OutputFilename("o", cl::desc("Specify thawed pexe filename"), + cl::value_desc("filename")); + +static cl::opt<std::string> +InputFilename(cl::Positional, cl::desc("<frozen file>"), cl::Required); + +static void WriteOutputFile(const Module *M) { + + std::string ThawedFilename = + (OutputFilename.size() == 0 ? (InputFilename + ".thawed") : OutputFilename); + + std::string ErrorInfo; + OwningPtr<tool_output_file> Out + (new tool_output_file(ThawedFilename.c_str(), ErrorInfo, + raw_fd_ostream::F_Binary)); + if (!ErrorInfo.empty()) { + errs() << ErrorInfo << '\n'; + exit(1); + } + + WriteBitcodeToFile(M, Out->os()); + + // Declare success. + Out->keep(); +} + +int main(int argc, char **argv) { + // Print a stack trace if we signal out. + sys::PrintStackTraceOnErrorSignal(); + PrettyStackTraceProgram X(argc, argv); + + LLVMContext &Context = getGlobalContext(); + llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. + + cl::ParseCommandLineOptions( + argc, argv, "Converts NaCl pexe wire format into LLVM bitcode format\n"); + + std::string ErrorMessage; + std::auto_ptr<Module> M; + + // Use the bitcode streaming interface + DataStreamer *streamer = getDataFileStreamer(InputFilename, &ErrorMessage); + if (streamer) { + std::string DisplayFilename = InputFilename; + M.reset(getNaClStreamedBitcodeModule(DisplayFilename, streamer, Context, + &ErrorMessage)); + if(M.get() != 0 && M->MaterializeAllPermanently(&ErrorMessage)) { + M.reset(); + } + } + + if (M.get() == 0) { + errs() << argv[0] << ": "; + if (ErrorMessage.size()) + errs() << ErrorMessage << "\n"; + else + errs() << "bitcode didn't read correctly.\n"; + return 1; + } + + WriteOutputFile(M.get()); + return 0; +} |