diff options
author | Derek Schuff <dschuff@chromium.org> | 2013-10-23 08:35:49 -0700 |
---|---|---|
committer | Derek Schuff <dschuff@chromium.org> | 2013-10-23 08:35:49 -0700 |
commit | 99a581677ff9a785f77e9d9d171b87d61f2da25e (patch) | |
tree | 68028c90a26c775aee1bd2834a4d3ec2953f140e | |
parent | 7287c45c13dc887cebe3db6abfa2f1080186bb97 (diff) |
Remove obsolete bitcode wrapper code
We are using our own bitcode reader now, and no longer need this.
R=jvoung@chromium.org, kschimpf@google.com
BUG=cleanup
Review URL: https://codereview.chromium.org/32943005
-rw-r--r-- | include/llvm/Wrap/BCHeaderField.h | 106 | ||||
-rw-r--r-- | include/llvm/Wrap/bitcode_wrapperer.h | 192 | ||||
-rw-r--r-- | include/llvm/Wrap/file_wrapper_input.h | 48 | ||||
-rw-r--r-- | include/llvm/Wrap/file_wrapper_output.h | 34 | ||||
-rw-r--r-- | include/llvm/Wrap/wrapper_input.h | 38 | ||||
-rw-r--r-- | include/llvm/Wrap/wrapper_output.h | 34 | ||||
-rw-r--r-- | lib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | lib/Makefile | 2 | ||||
-rw-r--r-- | lib/Wrap/CMakeLists.txt | 6 | ||||
-rw-r--r-- | lib/Wrap/LLVMBuild.txt | 21 | ||||
-rw-r--r-- | lib/Wrap/Makefile | 14 | ||||
-rw-r--r-- | lib/Wrap/bitcode_wrapperer.cpp | 355 | ||||
-rw-r--r-- | lib/Wrap/file_wrapper_input.cpp | 53 | ||||
-rw-r--r-- | lib/Wrap/file_wrapper_output.cpp | 37 | ||||
-rw-r--r-- | lib/Wrap/wrapper_output.cpp | 9 | ||||
-rw-r--r-- | tools/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tools/Makefile | 2 | ||||
-rw-r--r-- | tools/bc-wrap/CMakeLists.txt | 5 | ||||
-rw-r--r-- | tools/bc-wrap/LLVMBuild.txt | 22 | ||||
-rw-r--r-- | tools/bc-wrap/Makefile | 20 | ||||
-rw-r--r-- | tools/bc-wrap/bc_wrap.cpp | 123 |
21 files changed, 2 insertions, 1121 deletions
diff --git a/include/llvm/Wrap/BCHeaderField.h b/include/llvm/Wrap/BCHeaderField.h deleted file mode 100644 index 40a3714c9f..0000000000 --- a/include/llvm/Wrap/BCHeaderField.h +++ /dev/null @@ -1,106 +0,0 @@ -/* Copyright 2012 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. - */ - -#ifndef LLVM_WRAP_BCHEADERFIELD_H -#define LLVM_WRAP_BCHEADERFIELD_H -#include <limits> -#include <stdint.h> -#include <stdio.h> -#include <string.h> - -// Class representing a variable-size metadata field in the bitcode header. -// Also contains the list of known Tag IDs. -// Contains a pointer to the data but does not own the data, so it can be -// copied with the trivial copy constructor/assignment operator. - -// The serialized format has 2 fixed subfields (ID and length) and the -// variable-length data subfield -class BCHeaderField { - public: - typedef enum { - kInvalid = 0, - kBitcodeHash = 1, - kAndroidCompilerVersion = 0x4001, - kAndroidOptimizationLevel = 0x4002 - } Tag; - typedef uint16_t FixedSubfield; - - BCHeaderField(Tag ID, size_t len, uint8_t* data) : - ID_(ID), len_(len), data_(data) {} - size_t GetTotalSize() { - // Round up to 4 byte alignment - return (kTagLenSize + len_ + 3) & ~3; - } - - bool Write(uint8_t* buf, size_t buf_len) { - size_t fields_len = kTagLenSize + len_; - size_t pad_len = (4 - (fields_len & 3)) & 3; - // Ensure buffer is large enough and that length can be represented - // in 16 bits - if (buf_len < fields_len + pad_len || - len_ > std::numeric_limits<FixedSubfield>::max()) return false; - - WriteFixedSubfield(static_cast<FixedSubfield>(ID_), buf); - WriteFixedSubfield(static_cast<FixedSubfield>(len_), - buf + sizeof(FixedSubfield)); - memcpy(buf + kTagLenSize, data_, len_); - // Pad out to 4 byte alignment - if (pad_len) { - memset(buf + fields_len, 0, pad_len); - } - return true; - } - - bool Read(const uint8_t* buf, size_t buf_len) { - if (buf_len < kTagLenSize) return false; - FixedSubfield field; - ReadFixedSubfield(&field, buf); - ID_ = static_cast<Tag>(field); - ReadFixedSubfield(&field, buf + sizeof(FixedSubfield)); - len_ = static_cast<size_t>(field); - if (buf_len < kTagLenSize + len_) return false; - memcpy(data_, buf + kTagLenSize, len_); - return true; - } - - void Print() { - fprintf(stderr, "Field ID: %d, data length %d, total length %d\n", - ID_, static_cast<int>(len_), static_cast<int>(GetTotalSize())); - fprintf(stderr, "Data: "); - for (size_t i = 0; i < len_; i++) fprintf(stderr, "%02x", data_[i]); - fprintf(stderr, "\n"); - } - - // Get the data size from a serialized field to allow allocation - static size_t GetDataSizeFromSerialized(const uint8_t* buf) { - FixedSubfield len; - ReadFixedSubfield(&len, buf + sizeof(FixedSubfield)); - return len; - } - - Tag getID() const { - return ID_; - } - - size_t getLen() const { - return len_; - } - - private: - // Combined size of the fixed subfields - const static size_t kTagLenSize = 2 * sizeof(FixedSubfield); - static void WriteFixedSubfield(FixedSubfield value, uint8_t* buf) { - buf[0] = value & 0xFF; - buf[1] = (value >> 8) & 0xFF; - } - static void ReadFixedSubfield(FixedSubfield* value, const uint8_t* buf) { - *value = buf[0] | buf[1] << 8; - } - Tag ID_; - size_t len_; - uint8_t *data_; -}; - -#endif diff --git a/include/llvm/Wrap/bitcode_wrapperer.h b/include/llvm/Wrap/bitcode_wrapperer.h deleted file mode 100644 index 89f2a4cbcc..0000000000 --- a/include/llvm/Wrap/bitcode_wrapperer.h +++ /dev/null @@ -1,192 +0,0 @@ -/* Copyright 2012 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. - */ - -// Define utility class to wrap/unwrap bitcode files. Does wrapping/unwrapping -// in such a way that the wrappered bitcode file is still a bitcode file. - -#ifndef LLVM_WRAP_BITCODE_WRAPPERER_H__ -#define LLVM_WRAP_BITCODE_WRAPPERER_H__ - -#include <stdint.h> -#include <stddef.h> -#include <vector> - -#include "llvm/Support/support_macros.h" -#include "llvm/Wrap/BCHeaderField.h" -#include "llvm/Wrap/wrapper_input.h" -#include "llvm/Wrap/wrapper_output.h" - -// The bitcode wrapper header is the following 7 fixed 4-byte fields: -// 1) 0B17C0DE - The magic number expected by llvm for wrapped bitcodes -// 2) Version # 0 - The current version of wrapped bitcode files -// 3) (raw) bitcode offset -// 4) (raw) bitcode size -// 5) Android header version -// 6) Android target API -// 7) PNaCl Bitcode version -// plus 0 or more variable-length fields (consisting of ID, length, data) - -// Initial buffer size. It is expanded if needed to hold large variable-size -// fields. -static const size_t kBitcodeWrappererBufferSize = 1024; - -// Support class for outputting a wrapped bitcode file from a raw bitcode -// file (and optionally additional header fields), or for outputting a raw -// bitcode file from a wrapped one. -class BitcodeWrapperer { - public: - // Create a bitcode wrapperer using the following - // input and output files. - BitcodeWrapperer(WrapperInput* infile, WrapperOutput* outfile); - - // Returns true if the input file begins with a bitcode - // wrapper magic number. As a side effect, _wrapper_ fields are set. - bool IsInputBitcodeWrapper(); - - // Returns true if the input file begins with a bitcode - // file magic number. - bool IsInputBitcodeFile(); - - // Add a variable-length field to the header. The caller is responsible - // for freeing the data pointed to by the BCHeaderField. - void AddHeaderField(BCHeaderField* field); - - // Generate a wrapped bitcode file from the input bitcode file - // and the current header data. Return true on success. - bool GenerateWrappedBitcodeFile(); - - // Unwrap the wrapped bitcode file, to the corresponding - // outfile. Return true on success. - bool GenerateRawBitcodeFile(); - - // Print current wrapper header fields to stderr for debugging. - void PrintWrapperHeader(); - - ~BitcodeWrapperer(); - - private: - DISALLOW_CLASS_COPY_AND_ASSIGN(BitcodeWrapperer); - - // Refills the buffer with more bytes. Does this in a way - // such that it is maximally filled. - void FillBuffer(); - - // Returns the number of bytes in infile. - off_t GetInFileSize() { - if (infile_ != NULL) { - return infile_->Size(); - } else { - return 0; - } - } - - // Returns the offset of bitcode (i.e. the size of the wrapper header) - // if the output file were to be written now. - size_t BitcodeOffset(); - - // Returns true if we can read a word. If necessary, fills the buffer - // with enough characters so that there are at least a 32-bit value - // in the buffer. Returns false if there isn't a 32-bit value - // to read from the input file. - bool CanReadWord(); - - // Read a (32-bit) word from the input. Return true - // if able to read the word. - bool ReadWord(uint32_t& word); - - // Write a (32-bit) word to the output. Return true if successful - bool WriteWord(uint32_t word); - - // Write all variable-sized header fields to the output. Return true - // if successful. - bool WriteVariableFields(); - - // Parse the bitcode wrapper header in the infile, if any. Return true - // if successful. - bool ParseWrapperHeader(); - - // Returns the i-th character in front of the cursor in the buffer. - uint8_t BufferLookahead(int i) { return buffer_[cursor_ + i]; } - - // Returns how many unread bytes are in the buffer. - size_t GetBufferUnreadBytes() { return buffer_size_ - cursor_; } - - - // Backs up the read cursor to the beginning of the input buffer. - void ResetCursor() { - cursor_ = 0; - } - - // Generates the header sequence for the wrapped bitcode being - // generated. - bool WriteBitcodeWrapperHeader(); - - // Copies size bytes of infile to outfile, using the buffer. - bool BufferCopyInToOut(uint32_t size); - - // Discards the old infile and replaces it with the given file. - void ReplaceInFile(WrapperInput* new_infile); - - // Discards the old outfile and replaces it with the given file. - void ReplaceOutFile(WrapperOutput* new_outfile); - - // Moves to the given position in the input file. Returns false - // if unsuccessful. - bool Seek(uint32_t pos); - - // Clear the buffer of all contents. - void ClearBuffer(); - - // The input file being processed. Can be either - // a bitcode file, a wrappered bitcode file, or a secondary - // file to be wrapped. - WrapperInput* infile_; - - // The output file being generated. Can be either - // a bitcode file, a wrappered bitcode file, or a secondary - // unwrapped file. - WrapperOutput* outfile_; - - // A buffer of bytes read from the input file. - std::vector<uint8_t> buffer_; - - // The number of bytes that were read from the input file - // into the buffer. - size_t buffer_size_; - - // The index to the current read point within the buffer. - size_t cursor_; - - // True when eof of input is reached. - bool infile_at_eof_; - - // The 32-bit value defining the offset of the raw bitcode in the input file. - uint32_t infile_bc_offset_; - - // The 32-bit value defining the generated offset of the wrapped bitcode. - // This value changes as new fields are added with AddHeaderField - uint32_t wrapper_bc_offset_; - - // The 32-bit value defining the size of the raw wrapped bitcode. - uint32_t wrapper_bc_size_; - - // Android header version and target API - uint32_t android_header_version_; - uint32_t android_target_api_; - - // PNaCl bitcode version - uint32_t pnacl_bc_version_; - - // Vector of variable header fields - std::vector<BCHeaderField> header_fields_; - // If any bufferdata from header fields is owned, it is stored here and - // freed on destruction. - std::vector<uint8_t*> variable_field_data_; - - // True if there was an error condition (e.g. the file is not bitcode) - bool error_; -}; - -#endif // LLVM_WRAP_BITCODE_WRAPPERER_H__ diff --git a/include/llvm/Wrap/file_wrapper_input.h b/include/llvm/Wrap/file_wrapper_input.h deleted file mode 100644 index 9f3de004c4..0000000000 --- a/include/llvm/Wrap/file_wrapper_input.h +++ /dev/null @@ -1,48 +0,0 @@ -/* Copyright 2012 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. - */ - -// Defines utility allowing files for bitcode input wrapping. - -#ifndef FILE_WRAPPER_INPUT_H__ -#define FILE_WRAPPER_INPUT_H__ - -#include "llvm/Support/support_macros.h" -#include "llvm/Wrap/wrapper_input.h" - -#include <stdio.h> -#include <string> - -// Define a class to wrap named files. -class FileWrapperInput : public WrapperInput { - public: - FileWrapperInput(const std::string& name); - ~FileWrapperInput(); - // Tries to read the requested number of bytes into the buffer. Returns the - // actual number of bytes read. - virtual size_t Read(uint8_t* buffer, size_t wanted); - // Returns true if at end of file. Note: May return false - // until Read is called, and returns 0. - virtual bool AtEof(); - // Returns the size of the file (in bytes). - virtual off_t Size(); - // Moves to the given offset within the file. Returns - // false if unable to move to that position. - virtual bool Seek(uint32_t pos); - private: - // The name of the file. - std::string _name; - // True once eof has been encountered. - bool _at_eof; - // True if size has been computed. - bool _size_found; - // The size of the file. - off_t _size; - // The corresponding (opened) file. - FILE* _file; - private: - DISALLOW_CLASS_COPY_AND_ASSIGN(FileWrapperInput); -}; - -#endif // FILE_WRAPPER_INPUT_H__ diff --git a/include/llvm/Wrap/file_wrapper_output.h b/include/llvm/Wrap/file_wrapper_output.h deleted file mode 100644 index 714bd36a75..0000000000 --- a/include/llvm/Wrap/file_wrapper_output.h +++ /dev/null @@ -1,34 +0,0 @@ -/* Copyright 2012 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. - */ - -// Defines utility allowing files for bitcode output wrapping. - -#ifndef FILE_WRAPPER_OUTPUT_H__ -#define FILE_WRAPPER_OUTPUT_H__ - -#include "llvm/Support/support_macros.h" -#include "llvm/Wrap/wrapper_output.h" -#include <stdio.h> -#include <string> - -// Define a class to wrap named files. */ -class FileWrapperOutput : public WrapperOutput { - public: - FileWrapperOutput(const std::string& name); - ~FileWrapperOutput(); - // Writes a single byte, returning false if unable to write. - virtual bool Write(uint8_t byte); - // Writes the specified number of bytes in the buffer to - // output. Returns false if unable to write. - virtual bool Write(const uint8_t* buffer, size_t buffer_size); - private: - // The name of the file - std::string _name; - // The corresponding (opened) file. - FILE* _file; - private: - DISALLOW_CLASS_COPY_AND_ASSIGN(FileWrapperOutput); -}; -#endif // FILE_WRAPPER_OUTPUT_H__ diff --git a/include/llvm/Wrap/wrapper_input.h b/include/llvm/Wrap/wrapper_input.h deleted file mode 100644 index cde918083a..0000000000 --- a/include/llvm/Wrap/wrapper_input.h +++ /dev/null @@ -1,38 +0,0 @@ -/* Copyright 2012 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. - */ - -// Define a generic interface to a file/memory region that contains -// a bitcode file, a wrapped bitcode file, or a data file to wrap. - -#ifndef LLVM_WRAP_WRAPPER_INPUT_H__ -#define LLVM_WRAP_WRAPPER_INPUT_H__ - -#include <stdint.h> -#include <sys/types.h> - -#include "llvm/Support/support_macros.h" - -// The following is a generic interface to a file/memory region that contains -// a bitcode file, a wrapped bitcode file, or data file to wrap. -class WrapperInput { - public: - WrapperInput() {} - virtual ~WrapperInput() {} - // Tries to read the requested number of bytes into the buffer. Returns the - // actual number of bytes read. - virtual size_t Read(uint8_t* buffer, size_t wanted) = 0; - // Returns true if at end of input. Note: May return false until - // Read is called, and returns 0. - virtual bool AtEof() = 0; - // Returns the size of the input (in bytes). - virtual off_t Size() = 0; - // Moves to the given offset within the input region. Returns false - // if unable to move to that position. - virtual bool Seek(uint32_t pos) = 0; - private: - DISALLOW_CLASS_COPY_AND_ASSIGN(WrapperInput); -}; - -#endif // LLVM_WRAP_WRAPPER_INPUT_H__ diff --git a/include/llvm/Wrap/wrapper_output.h b/include/llvm/Wrap/wrapper_output.h deleted file mode 100644 index 7045705991..0000000000 --- a/include/llvm/Wrap/wrapper_output.h +++ /dev/null @@ -1,34 +0,0 @@ -/* Copyright 2012 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. - */ - -// Defines a generic interface to a file/memory region that -// contains a generated wrapped bitcode file, bitcode file, -// or data file. - -#ifndef LLVM_WRAP_WRAPPER_OUTPUT_H__ -#define LLVM_WRAP_WRAPPER_OUTPUT_H__ - -#include <stdint.h> -#include <stddef.h> - -#include "llvm/Support/support_macros.h" - -// The following is a generic interface to a file/memory region -// that contains a generated bitcode file, wrapped bitcode file, -// or a data file. -class WrapperOutput { - public: - WrapperOutput() {} - virtual ~WrapperOutput() {} - // Writes a single byte, returning false if unable to write. - virtual bool Write(uint8_t byte) = 0; - // Writes the specified number of bytes in the buffer to - // output. Returns false if unable to write. - virtual bool Write(const uint8_t* buffer, size_t buffer_size); - private: - DISALLOW_CLASS_COPY_AND_ASSIGN(WrapperOutput); -}; - -#endif // LLVM_WRAP_WRAPPER_OUTPUT_H__ diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index e2a5c54756..76ebe9aca9 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -15,4 +15,3 @@ add_subdirectory(ExecutionEngine) add_subdirectory(Target) add_subdirectory(AsmParser) add_subdirectory(Archive) -add_subdirectory(Wrap) diff --git a/lib/Makefile b/lib/Makefile index ac9050db7c..3a699adc8d 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -11,7 +11,7 @@ LEVEL = .. include $(LEVEL)/Makefile.config PARALLEL_DIRS := IR IRReader AsmParser Bitcode Archive Analysis Transforms CodeGen \ - Target ExecutionEngine Linker MC Object Option Wrap DebugInfo + Target ExecutionEngine Linker MC Object Option DebugInfo ifeq ($(NACL_SANDBOX),1) PARALLEL_DIRS := $(filter-out Archive Linker, \ diff --git a/lib/Wrap/CMakeLists.txt b/lib/Wrap/CMakeLists.txt deleted file mode 100644 index da44a6bb56..0000000000 --- a/lib/Wrap/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -add_llvm_library(LLVMWrap - bitcode_wrapperer.cpp - file_wrapper_input.cpp - file_wrapper_output.cpp - wrapper_output.cpp -)
\ No newline at end of file diff --git a/lib/Wrap/LLVMBuild.txt b/lib/Wrap/LLVMBuild.txt deleted file mode 100644 index 8750711338..0000000000 --- a/lib/Wrap/LLVMBuild.txt +++ /dev/null @@ -1,21 +0,0 @@ -;===- ./lib/Wrap/LLVMBuild.txt ------------------------------*- Conf -*--===; -; -; The LLVM Compiler Infrastructure -; -; This file is distributed under the University of Illinois Open Source -; License. See LICENSE.TXT for details. -; -;===------------------------------------------------------------------------===; -; -; 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 = Library -name = Wrap -parent = Libraries diff --git a/lib/Wrap/Makefile b/lib/Wrap/Makefile deleted file mode 100644 index 79aa2b3531..0000000000 --- a/lib/Wrap/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -##===- lib/Linker/Makefile ---------------------------------*- Makefile -*-===## -# -# The LLVM Compiler Infrastructure -# -# This file is distributed under the University of Illinois Open Source -# License. See LICENSE.TXT for details. -# -##===----------------------------------------------------------------------===## - -LEVEL = ../.. -LIBRARYNAME = LLVMWrap -BUILD_ARCHIVE := 1 - -include $(LEVEL)/Makefile.common diff --git a/lib/Wrap/bitcode_wrapperer.cpp b/lib/Wrap/bitcode_wrapperer.cpp deleted file mode 100644 index eeb2825793..0000000000 --- a/lib/Wrap/bitcode_wrapperer.cpp +++ /dev/null @@ -1,355 +0,0 @@ -/* Copyright 2012 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. - */ - -#include "llvm/Wrap/bitcode_wrapperer.h" - -#include <stdio.h> -#include <sys/stat.h> - -using std::vector; - -// The number of bytes in a 32 bit integer. -static const uint32_t kWordSize = 4; - -// Number of LLVM-defined fixed fields in the header. -static const uint32_t kLLVMFields = 4; - -// Total number of fixed fields in the header. -static const uint32_t kFixedFields = 7; - -// The magic number that must exist for bitcode wrappers. -static const uint32_t kWrapperMagicNumber = 0x0B17C0DE; - -// The version number associated with a wrapper file. -// Note: llvm currently only allows the value 0. When this changes, -// we should consider making this a command line option. -static const uint32_t kLLVMVersionNumber = 0; - -// Fields defined by Android bitcode header. -static const uint32_t kAndroidHeaderVersion = 0; -static const uint32_t kAndroidTargetAPI = 0; -static const uint32_t kAndroidDefaultCompilerVersion = 0; -static const uint32_t kAndroidDefaultOptimizationLevel = 3; - -// PNaCl bitcode version number. -static const uint32_t kPnaclBitcodeVersion = 0; - -// Max size for variable fields. Currently only used for writing them -// out to files (the parsing works for arbitrary sizes). -static const size_t kMaxVariableFieldSize = 256; - -BitcodeWrapperer::BitcodeWrapperer(WrapperInput* infile, WrapperOutput* outfile) - : infile_(infile), - outfile_(outfile), - buffer_size_(0), - cursor_(0), - infile_at_eof_(false), - infile_bc_offset_(0), - wrapper_bc_offset_(0), - wrapper_bc_size_(0), - android_header_version_(kAndroidHeaderVersion), - android_target_api_(kAndroidTargetAPI), - pnacl_bc_version_(0), - error_(false) { - buffer_.resize(kBitcodeWrappererBufferSize); - if (IsInputBitcodeWrapper()) { - ParseWrapperHeader(); - } else if (IsInputBitcodeFile()) { - wrapper_bc_offset_ = kWordSize * kFixedFields; - wrapper_bc_size_ = GetInFileSize(); - } else { - fprintf(stderr, "Error: input file is not a bitcode file.\n"); - error_ = true; - } -} - -BitcodeWrapperer::~BitcodeWrapperer() { - for(size_t i = 0; i < variable_field_data_.size(); i++) { - delete [] variable_field_data_[i]; - } -} - - -void BitcodeWrapperer::ClearBuffer() { - buffer_size_ = 0; - cursor_ = 0; - infile_at_eof_ = false; -} - -bool BitcodeWrapperer::Seek(uint32_t pos) { - if (infile_ != NULL && infile_->Seek(pos)) { - ClearBuffer(); - return true; - } - return false; -} - -bool BitcodeWrapperer::CanReadWord() { - if (GetBufferUnreadBytes() < kWordSize) { - FillBuffer(); - return GetBufferUnreadBytes() >= kWordSize; - } else { - return true; - } -} - -void BitcodeWrapperer::FillBuffer() { - if (cursor_ > 0) { - // Before filling, move any remaining bytes to the - // front of the buffer. This allows us to assume - // that after the call to FillBuffer, readable - // text is contiguous. - if (cursor_ < buffer_size_) { - size_t i = 0; - while (cursor_ < buffer_size_) { - buffer_[i++] = buffer_[cursor_++]; - } - cursor_ = 0; - buffer_size_ = i; - } - } else { - // Assume the buffer contents have been used, - // and we want to completely refill it. - buffer_size_ = 0; - } - - // If we don't have an input, we can't refill the buffer at all. - if (infile_ == NULL) { - return; - } - - // Now fill in remaining space. - size_t needed = buffer_.size() - buffer_size_; - - while (buffer_.size() > buffer_size_) { - int actually_read = infile_->Read(&buffer_[buffer_size_], needed); - if (infile_->AtEof()) { - infile_at_eof_ = true; - } - if (actually_read) { - buffer_size_ += actually_read; - needed -= actually_read; - } else if (infile_at_eof_) { - break; - } - } -} - -bool BitcodeWrapperer::ReadWord(uint32_t& word) { - if (!CanReadWord()) return false; - word = (((uint32_t) BufferLookahead(0)) << 0) - | (((uint32_t) BufferLookahead(1)) << 8) - | (((uint32_t) BufferLookahead(2)) << 16) - | (((uint32_t) BufferLookahead(3)) << 24); - cursor_ += kWordSize; - return true; -} - -bool BitcodeWrapperer::WriteWord(uint32_t value) { - uint8_t buffer[kWordSize]; - buffer[3] = (value >> 24) & 0xFF; - buffer[2] = (value >> 16) & 0xFF; - buffer[1] = (value >> 8) & 0xFF; - buffer[0] = (value >> 0) & 0xFF; - return outfile_->Write(buffer, kWordSize); -} - -bool BitcodeWrapperer::WriteVariableFields() { - // This buffer may have to be bigger if we start using the fields - // for larger things. - uint8_t buffer[kMaxVariableFieldSize]; - for (vector<BCHeaderField>::iterator it = header_fields_.begin(); - it != header_fields_.end(); ++it) { - if (!it->Write(buffer, kMaxVariableFieldSize) || - !outfile_->Write(buffer, it->GetTotalSize())) { - return false; - } - } - return true; -} - -bool BitcodeWrapperer::ParseWrapperHeader() { - // Make sure LLVM-defined fields have been parsed - if (!IsInputBitcodeWrapper()) return false; - // Check the android/pnacl fields - if (!ReadWord(android_header_version_) || - !ReadWord(android_target_api_) || !ReadWord(pnacl_bc_version_)) { - fprintf(stderr, "Error: file not long enough to contain header\n"); - return false; - } - if (pnacl_bc_version_ != kPnaclBitcodeVersion) { - fprintf(stderr, "Error: bad PNaCl Bitcode version\n"); - return false; - } - int field_data_total = wrapper_bc_offset_ - kWordSize * kFixedFields; - if (field_data_total > 0) { - // Read in the variable fields. We need to allocate space for the data. - int field_data_read = 0; - - while (field_data_read < field_data_total) { - FillBuffer(); - size_t buffer_needed = BCHeaderField::GetDataSizeFromSerialized( - &buffer_[cursor_]); - if (buffer_needed > buffer_.size()) { - buffer_.resize(buffer_needed + - sizeof(BCHeaderField::FixedSubfield) * 2); - FillBuffer(); - } - variable_field_data_.push_back(new uint8_t[buffer_needed]); - - BCHeaderField field(BCHeaderField::kInvalid, 0, - variable_field_data_.back()); - field.Read(&buffer_[cursor_], buffer_size_); - header_fields_.push_back(field); - size_t field_size = field.GetTotalSize(); - cursor_ += field_size; - field_data_read += field_size; - if (field_data_read > field_data_total) { - // We read too much data, the header is corrupted - fprintf(stderr, "Error: raw bitcode offset inconsistent with " - "variable field data\n"); - return false; - } - } - Seek(0); - } - return true; -} - -bool BitcodeWrapperer::IsInputBitcodeWrapper() { - ResetCursor(); - // First make sure that there are enough words (LLVM header) - // to peek at. - if (GetBufferUnreadBytes() < kLLVMFields * kWordSize) { - FillBuffer(); - if (GetBufferUnreadBytes() < kLLVMFields * kWordSize) return false; - } - - // Now make sure the magic number is right. - uint32_t first_word; - if ((!ReadWord(first_word)) || - (kWrapperMagicNumber != first_word)) return false; - - // Make sure the version is right. - uint32_t second_word; - if ((!ReadWord(second_word)) || - (kLLVMVersionNumber != second_word)) return false; - - // Make sure that the offset and size (for llvm) is defined. - uint32_t bc_offset; - uint32_t bc_size; - if (ReadWord(bc_offset) && - ReadWord(bc_size)) { - // Before returning, save the extracted values. - wrapper_bc_offset_ = bc_offset; - infile_bc_offset_ = bc_offset; - wrapper_bc_size_ = bc_size; - return true; - } - // If reached, unable to read wrapped header. - return false; -} - -bool BitcodeWrapperer::IsInputBitcodeFile() { - ResetCursor(); - // First make sure that there are four bytes to peek at. - if (GetBufferUnreadBytes() < kWordSize) { - FillBuffer(); - if (GetBufferUnreadBytes() < kWordSize) return false; - } - // If reached, Check if first 4 bytes match bitcode - // file magic number. - return (BufferLookahead(0) == 'B') && - (BufferLookahead(1) == 'C') && - (BufferLookahead(2) == 0xc0) && - (BufferLookahead(3) == 0xde); -} - -bool BitcodeWrapperer::BufferCopyInToOut(uint32_t size) { - while (size > 0) { - // Be sure buffer is non-empty before writing. - if (0 == buffer_size_) { - FillBuffer(); - if (0 == buffer_size_) { - return false; - } - } - // copy the buffer to the output file. - size_t block = (buffer_size_ < size) ? buffer_size_ : size; - if (!outfile_->Write(&buffer_[cursor_], block)) return false; - size -= block; - buffer_size_ = 0; - } - // Be sure that there isn't more bytes on the input stream. - FillBuffer(); - return buffer_size_ == 0; -} - -void BitcodeWrapperer::AddHeaderField(BCHeaderField* field) { - vector<BCHeaderField>::iterator it = header_fields_.begin(); - for (; it != header_fields_.end(); ++it) { - // If this field is the same as an existing one, overwrite it. - if (it->getID() == field->getID()) { - wrapper_bc_offset_ += (field->GetTotalSize() - it->GetTotalSize()); - *it = *field; - break; - } - } - if (it == header_fields_.end()) { // there was no match, add a new field - header_fields_.push_back(*field); - wrapper_bc_offset_ += field->GetTotalSize(); - } -} - -bool BitcodeWrapperer::WriteBitcodeWrapperHeader() { - return - // Note: This writes out the 4 word header required by llvm wrapped - // bitcode. - WriteWord(kWrapperMagicNumber) && - WriteWord(kLLVMVersionNumber) && - WriteWord(wrapper_bc_offset_) && - WriteWord(wrapper_bc_size_) && - // 2 fixed fields defined by Android - WriteWord(android_header_version_) && - WriteWord(android_target_api_) && - // PNaClBitcode version - WriteWord(kPnaclBitcodeVersion) && - // Common variable-length fields - WriteVariableFields(); -} - -void BitcodeWrapperer::PrintWrapperHeader() { - if (error_) { - fprintf(stderr, "Error condition exists: the following" - "data may not be reliable\n"); - } - fprintf(stderr, "Wrapper magic:\t\t%x\n", kWrapperMagicNumber); - fprintf(stderr, "LLVM Bitcode version:\t%d\n", kLLVMVersionNumber); - fprintf(stderr, "Raw bitcode offset:\t%d\n", wrapper_bc_offset_); - fprintf(stderr, "Raw bitcode size:\t%d\n", wrapper_bc_size_); - fprintf(stderr, "Android header version:\t%d\n", android_header_version_); - fprintf(stderr, "Android target API:\t%d\n", android_target_api_); - fprintf(stderr, "PNaCl bitcode version:\t%d\n", kPnaclBitcodeVersion); - for (size_t i = 0; i < header_fields_.size( |