blob: 3e9ed5255af94ce2eac4672b79bde1424b37be05 [file] [log] [blame]
Stephen Hines0164f462011-11-22 19:43:31 -08001/*
Stephen Hines7cd4c492012-03-13 19:57:37 -07002 * Copyright 2011-2012, The Android Open Source Project
Stephen Hines0164f462011-11-22 19:43:31 -08003 *
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
Stephen Hines7cd4c492012-03-13 19:57:37 -070020#include "bcinfo/Wrap/BCHeaderField.h"
21
Stephen Hines0164f462011-11-22 19:43:31 -080022#include <cstddef>
23#include <stdint.h>
24
25namespace bcinfo {
26
Stephen Hines7cd4c492012-03-13 19:57:37 -070027struct AndroidBitcodeWrapper {
Stephen Hines0164f462011-11-22 19:43:31 -080028 uint32_t Magic;
29 uint32_t Version;
30 uint32_t BitcodeOffset;
31 uint32_t BitcodeSize;
32 uint32_t HeaderVersion;
33 uint32_t TargetAPI;
Stephen Hines7cd4c492012-03-13 19:57:37 -070034 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;
Stephen Hines0164f462011-11-22 19:43:31 -080041};
42
43enum BCFileType {
44 BC_NOT_BC = 0,
45 BC_WRAPPER = 1,
46 BC_RAW = 2
47};
48
49class BitcodeWrapper {
50 private:
51 enum BCFileType mFileType;
52 const char *mBitcode;
53 const char *mBitcodeEnd;
54 size_t mBitcodeSize;
55
Stephen Hines7cd4c492012-03-13 19:57:37 -070056 uint32_t mHeaderVersion;
57 uint32_t mTargetAPI;
58 uint32_t mCompilerVersion;
59 uint32_t mOptimizationLevel;
Stephen Hines0164f462011-11-22 19:43:31 -080060
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 /**
Stephen Hines7cd4c492012-03-13 19:57:37 -070073 * Attempt to unwrap the target bitcode. This function is \deprecated.
Stephen Hines0164f462011-11-22 19:43:31 -080074 *
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 /**
Stephen Hines7cd4c492012-03-13 19:57:37 -070087 * \return header version of bitcode wrapper.
Stephen Hines0164f462011-11-22 19:43:31 -080088 */
89 uint32_t getHeaderVersion() const {
Stephen Hines7cd4c492012-03-13 19:57:37 -070090 return mHeaderVersion;
Stephen Hines0164f462011-11-22 19:43:31 -080091 }
92
93 /**
Stephen Hines7cd4c492012-03-13 19:57:37 -070094 * \return target API version for this bitcode.
Stephen Hines0164f462011-11-22 19:43:31 -080095 */
96 uint32_t getTargetAPI() const {
Stephen Hines7cd4c492012-03-13 19:57:37 -070097 return mTargetAPI;
Stephen Hines0164f462011-11-22 19:43:31 -080098 }
Stephen Hines7cd4c492012-03-13 19:57:37 -070099
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
Stephen Hines0164f462011-11-22 19:43:31 -0800114};
115
Stephen Hines7cd4c492012-03-13 19:57:37 -0700116/**
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 */
128static 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
Stephen Hines0164f462011-11-22 19:43:31 -0800152} // namespace bcinfo
153
154#endif // __ANDROID_BCINFO_BITCODEWRAPPER_H__