Home | History | Annotate | Download | only in bcinfo
      1 /*
      2  * Copyright 2011-2012, The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef __ANDROID_BCINFO_BITCODEWRAPPER_H__
     18 #define __ANDROID_BCINFO_BITCODEWRAPPER_H__
     19 
     20 #include "bcinfo/Wrap/BCHeaderField.h"
     21 
     22 #include <cstddef>
     23 #include <stdint.h>
     24 
     25 namespace bcinfo {
     26 
     27 struct AndroidBitcodeWrapper {
     28   uint32_t Magic;
     29   uint32_t Version;
     30   uint32_t BitcodeOffset;
     31   uint32_t BitcodeSize;
     32   uint32_t HeaderVersion;
     33   uint32_t TargetAPI;
     34   uint32_t PNaClVersion;
     35   uint16_t CompilerVersionTag;
     36   uint16_t CompilerVersionLen;
     37   uint32_t CompilerVersion;
     38   uint16_t OptimizationLevelTag;
     39   uint16_t OptimizationLevelLen;
     40   uint32_t OptimizationLevel;
     41 };
     42 
     43 enum BCFileType {
     44   BC_NOT_BC = 0,
     45   BC_WRAPPER = 1,
     46   BC_RAW = 2
     47 };
     48 
     49 class BitcodeWrapper {
     50  private:
     51   enum BCFileType mFileType;
     52   const char *mBitcode;
     53   const char *mBitcodeEnd;
     54   size_t mBitcodeSize;
     55 
     56   uint32_t mHeaderVersion;
     57   uint32_t mTargetAPI;
     58   uint32_t mCompilerVersion;
     59   uint32_t mOptimizationLevel;
     60 
     61  public:
     62   /**
     63    * Reads wrapper information from \p bitcode.
     64    *
     65    * \param bitcode - input bitcode string.
     66    * \param bitcodeSize - length of \p bitcode string (in bytes).
     67    */
     68   BitcodeWrapper(const char *bitcode, size_t bitcodeSize);
     69 
     70   ~BitcodeWrapper();
     71 
     72   /**
     73    * Attempt to unwrap the target bitcode. This function is \deprecated.
     74    *
     75    * \return true on success and false if an error occurred.
     76    */
     77   bool unwrap();
     78 
     79   /**
     80    * \return type of bitcode file.
     81    */
     82   enum BCFileType getBCFileType() const {
     83     return mFileType;
     84   }
     85 
     86   /**
     87    * \return header version of bitcode wrapper.
     88    */
     89   uint32_t getHeaderVersion() const {
     90     return mHeaderVersion;
     91   }
     92 
     93   /**
     94    * \return target API version for this bitcode.
     95    */
     96   uint32_t getTargetAPI() const {
     97     return mTargetAPI;
     98   }
     99 
    100   /**
    101    * \return compiler version that generated this bitcode.
    102    */
    103   uint32_t getCompilerVersion() const {
    104     return mCompilerVersion;
    105   }
    106 
    107   /**
    108    * \return compiler optimization level for this bitcode.
    109    */
    110   uint32_t getOptimizationLevel() const {
    111     return mOptimizationLevel;
    112   }
    113 
    114 };
    115 
    116 /**
    117  * Helper function to emit just the bitcode wrapper returning the number of
    118  * bytes that were written.
    119  *
    120  * \param wrapper - where to write header information into.
    121  * \param bitcodeSize - size of bitcode in bytes.
    122  * \param targetAPI - target API version for this bitcode.
    123  * \param compilerVersion - compiler version that generated this bitcode.
    124  * \param optimizationLevel - compiler optimization level for this bitcode.
    125  *
    126  * \return number of wrapper bytes written into the \p buffer.
    127  */
    128 static inline size_t writeAndroidBitcodeWrapper(AndroidBitcodeWrapper *wrapper,
    129     size_t bitcodeSize, uint32_t targetAPI, uint32_t compilerVersion,
    130     uint32_t optimizationLevel) {
    131   if (!wrapper) {
    132     return 0;
    133   }
    134 
    135   wrapper->Magic = 0x0B17C0DE;
    136   wrapper->Version = 0;
    137   wrapper->BitcodeOffset = sizeof(*wrapper);
    138   wrapper->BitcodeSize = bitcodeSize;
    139   wrapper->HeaderVersion = 0;
    140   wrapper->TargetAPI = targetAPI;
    141   wrapper->PNaClVersion = 0;
    142   wrapper->CompilerVersionTag = BCHeaderField::kAndroidCompilerVersion;
    143   wrapper->CompilerVersionLen = 4;
    144   wrapper->CompilerVersion = compilerVersion;
    145   wrapper->OptimizationLevelTag = BCHeaderField::kAndroidOptimizationLevel;
    146   wrapper->OptimizationLevelLen = 4;
    147   wrapper->OptimizationLevel = optimizationLevel;
    148 
    149   return sizeof(*wrapper);
    150 }
    151 
    152 }  // namespace bcinfo
    153 
    154 #endif  // __ANDROID_BCINFO_BITCODEWRAPPER_H__
    155