Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 1 | /* |
Stephen Hines | 7cd4c49 | 2012-03-13 19:57:37 -0700 | [diff] [blame] | 2 | * Copyright 2011-2012, The Android Open Source Project |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 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 | #include "bcinfo/BitcodeTranslator.h" |
| 18 | |
Stephen Hines | 7cd4c49 | 2012-03-13 19:57:37 -0700 | [diff] [blame] | 19 | #include "bcinfo/BitcodeWrapper.h" |
| 20 | |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 21 | #include "BitReader_2_7/BitReader_2_7.h" |
Logan Chien | c7d67a7 | 2011-12-16 17:06:49 +0800 | [diff] [blame] | 22 | #include "BitReader_3_0/BitReader_3_0.h" |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 23 | |
Stephen Hines | 8dbca8e | 2013-06-21 16:30:26 -0700 | [diff] [blame] | 24 | #include "BitWriter_3_2/ReaderWriter_3_2.h" |
| 25 | |
David Gross | bcb03e5 | 2017-05-11 13:57:07 -0700 | [diff] [blame] | 26 | #include "StripUnkAttr/strip_unknown_attributes.h" |
| 27 | |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 28 | #define LOG_TAG "bcinfo" |
Mark Salyzyn | d5c6901 | 2017-01-10 14:27:14 -0800 | [diff] [blame] | 29 | #include <log/log.h> |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 30 | |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 31 | #include "llvm/Bitcode/BitstreamWriter.h" |
| 32 | #include "llvm/Bitcode/ReaderWriter.h" |
Stephen Hines | b730e23 | 2013-01-09 15:31:36 -0800 | [diff] [blame] | 33 | #include "llvm/IR/LLVMContext.h" |
| 34 | #include "llvm/IR/Module.h" |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 35 | #include "llvm/Support/MemoryBuffer.h" |
Shih-wei Liao | c73b521 | 2012-03-05 09:51:44 -0800 | [diff] [blame] | 36 | #include "llvm/Support/raw_ostream.h" |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 37 | |
| 38 | #include <cstdlib> |
Chris Wailes | 881cda4 | 2014-06-23 11:27:41 -0700 | [diff] [blame] | 39 | #include <climits> |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 40 | |
| 41 | namespace bcinfo { |
| 42 | |
| 43 | /** |
| 44 | * Define minimum and maximum target API versions. These correspond to the |
| 45 | * same API levels used by the standard Android SDK. |
| 46 | * |
Logan Chien | c7d67a7 | 2011-12-16 17:06:49 +0800 | [diff] [blame] | 47 | * LLVM 2.7 |
| 48 | * 11 - Honeycomb |
| 49 | * 12 - Honeycomb MR1 |
| 50 | * 13 - Honeycomb MR2 |
| 51 | * |
| 52 | * LLVM 3.0 |
| 53 | * 14 - Ice Cream Sandwich |
| 54 | * 15 - Ice Cream Sandwich MR1 |
| 55 | * |
| 56 | * LLVM 3.1 |
| 57 | * 16 - Ice Cream Sandwich MR2 |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 58 | */ |
Chris Wailes | 881cda4 | 2014-06-23 11:27:41 -0700 | [diff] [blame] | 59 | static const unsigned int kMinimumAPIVersion = 11; |
| 60 | static const unsigned int kMaximumAPIVersion = RS_VERSION; |
| 61 | static const unsigned int kCurrentAPIVersion = 10000; |
| 62 | static const unsigned int kDevelopmentAPIVersion = UINT_MAX; |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * The minimum version which does not require translation (i.e. is already |
| 66 | * compatible with LLVM's default bitcode reader). |
| 67 | */ |
Logan Chien | c7d67a7 | 2011-12-16 17:06:49 +0800 | [diff] [blame] | 68 | static const unsigned int kMinimumUntranslatedVersion = 16; |
| 69 | static const unsigned int kMinimumCompatibleVersion_LLVM_3_0 = 14; |
| 70 | static const unsigned int kMinimumCompatibleVersion_LLVM_2_7 = 11; |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 71 | |
| 72 | |
David Gross | bcb03e5 | 2017-05-11 13:57:07 -0700 | [diff] [blame] | 73 | static void stripUnknownAttributes(llvm::Module *M) { |
| 74 | for (llvm::Function &F : *M) |
| 75 | slang::stripUnknownAttributes(F); |
| 76 | } |
| 77 | |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 78 | BitcodeTranslator::BitcodeTranslator(const char *bitcode, size_t bitcodeSize, |
| 79 | unsigned int version) |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 80 | : mBitcode(bitcode), mBitcodeSize(bitcodeSize), mTranslatedBitcode(nullptr), |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 81 | mTranslatedBitcodeSize(0), mVersion(version) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | BitcodeTranslator::~BitcodeTranslator() { |
| 87 | if (mVersion < kMinimumUntranslatedVersion) { |
| 88 | // We didn't actually do a translation in the alternate case, so deleting |
| 89 | // the bitcode would be improper. |
| 90 | delete [] mTranslatedBitcode; |
| 91 | } |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 92 | mTranslatedBitcode = nullptr; |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 93 | return; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | bool BitcodeTranslator::translate() { |
| 98 | if (!mBitcode || !mBitcodeSize) { |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 99 | ALOGE("Invalid/empty bitcode"); |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | |
Stephen Hines | 7cd4c49 | 2012-03-13 19:57:37 -0700 | [diff] [blame] | 103 | BitcodeWrapper BCWrapper(mBitcode, mBitcodeSize); |
| 104 | if (BCWrapper.getTargetAPI() != mVersion) { |
| 105 | ALOGE("Bitcode wrapper (%u) and translator (%u) disagree about target API", |
| 106 | BCWrapper.getTargetAPI(), mVersion); |
| 107 | } |
| 108 | |
Chris Wailes | 881cda4 | 2014-06-23 11:27:41 -0700 | [diff] [blame] | 109 | if ((mVersion != kDevelopmentAPIVersion) && |
| 110 | (mVersion != kCurrentAPIVersion) && |
| 111 | ((mVersion < kMinimumAPIVersion) || |
| 112 | (mVersion > kMaximumAPIVersion))) { |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 113 | ALOGE("Invalid API version: %u is out of range ('%u' - '%u')", mVersion, |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 114 | kMinimumAPIVersion, kMaximumAPIVersion); |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | // We currently don't need to transcode any API version higher than 14 or |
| 119 | // the current API version (i.e. 10000) |
| 120 | if (mVersion >= kMinimumUntranslatedVersion) { |
| 121 | mTranslatedBitcode = mBitcode; |
| 122 | mTranslatedBitcodeSize = mBitcodeSize; |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | // Do the actual transcoding by invoking a 2.7-era bitcode reader that can |
| 127 | // then write the bitcode back out in a more modern (acceptable) version. |
Stephen Hines | d0993af | 2014-07-15 16:49:25 -0700 | [diff] [blame] | 128 | std::unique_ptr<llvm::LLVMContext> mContext(new llvm::LLVMContext()); |
| 129 | std::unique_ptr<llvm::MemoryBuffer> MEM( |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 130 | llvm::MemoryBuffer::getMemBuffer( |
Stephen Hines | e708ffe | 2012-05-03 15:18:49 -0700 | [diff] [blame] | 131 | llvm::StringRef(mBitcode, mBitcodeSize), "", false)); |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 132 | std::string error; |
Stephen Hines | 5793613 | 2014-11-25 17:54:59 -0800 | [diff] [blame] | 133 | llvm::ErrorOr<llvm::MemoryBufferRef> MBOrErr = MEM->getMemBufferRef(); |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 134 | |
Stephen Hines | 5793613 | 2014-11-25 17:54:59 -0800 | [diff] [blame] | 135 | llvm::ErrorOr<llvm::Module *> MOrErr(nullptr); |
Logan Chien | c7d67a7 | 2011-12-16 17:06:49 +0800 | [diff] [blame] | 136 | |
| 137 | if (mVersion >= kMinimumCompatibleVersion_LLVM_3_0) { |
Stephen Hines | 5793613 | 2014-11-25 17:54:59 -0800 | [diff] [blame] | 138 | MOrErr = llvm_3_0::parseBitcodeFile(*MBOrErr, *mContext); |
Logan Chien | c7d67a7 | 2011-12-16 17:06:49 +0800 | [diff] [blame] | 139 | } else if (mVersion >= kMinimumCompatibleVersion_LLVM_2_7) { |
Stephen Hines | 5793613 | 2014-11-25 17:54:59 -0800 | [diff] [blame] | 140 | MOrErr = llvm_2_7::parseBitcodeFile(*MBOrErr, *mContext); |
Logan Chien | c7d67a7 | 2011-12-16 17:06:49 +0800 | [diff] [blame] | 141 | } else { |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 142 | ALOGE("No compatible bitcode reader for API version %d", mVersion); |
Logan Chien | c7d67a7 | 2011-12-16 17:06:49 +0800 | [diff] [blame] | 143 | return false; |
| 144 | } |
| 145 | |
Stephen Hines | 5793613 | 2014-11-25 17:54:59 -0800 | [diff] [blame] | 146 | if (std::error_code EC = MOrErr.getError()) { |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 147 | ALOGE("Could not parse bitcode file"); |
Stephen Hines | 5793613 | 2014-11-25 17:54:59 -0800 | [diff] [blame] | 148 | ALOGE("%s", EC.message().c_str()); |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 149 | return false; |
| 150 | } |
| 151 | |
Stephen Hines | 5793613 | 2014-11-25 17:54:59 -0800 | [diff] [blame] | 152 | // Module ownership is handled by the context, so we don't need to free it. |
| 153 | llvm::Module *module = MOrErr.get(); |
| 154 | |
David Gross | bcb03e5 | 2017-05-11 13:57:07 -0700 | [diff] [blame] | 155 | stripUnknownAttributes(module); |
| 156 | |
Shih-wei Liao | c73b521 | 2012-03-05 09:51:44 -0800 | [diff] [blame] | 157 | std::string Buffer; |
| 158 | |
| 159 | llvm::raw_string_ostream OS(Buffer); |
Stephen Hines | 8dbca8e | 2013-06-21 16:30:26 -0700 | [diff] [blame] | 160 | // Use the LLVM 3.2 bitcode writer, instead of the top-of-tree version. |
| 161 | llvm_3_2::WriteBitcodeToFile(module, OS); |
Shih-wei Liao | c73b521 | 2012-03-05 09:51:44 -0800 | [diff] [blame] | 162 | OS.flush(); |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 163 | |
Stephen Hines | 7cd4c49 | 2012-03-13 19:57:37 -0700 | [diff] [blame] | 164 | AndroidBitcodeWrapper wrapper; |
| 165 | size_t actualWrapperLen = writeAndroidBitcodeWrapper( |
Stephen Hines | dc31b1c | 2014-12-17 19:23:28 -0800 | [diff] [blame] | 166 | &wrapper, Buffer.size(), kMinimumUntranslatedVersion, |
Stephen Hines | 7cd4c49 | 2012-03-13 19:57:37 -0700 | [diff] [blame] | 167 | BCWrapper.getCompilerVersion(), BCWrapper.getOptimizationLevel()); |
| 168 | if (!actualWrapperLen) { |
| 169 | ALOGE("Couldn't produce bitcode wrapper!"); |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | mTranslatedBitcodeSize = actualWrapperLen + Buffer.size(); |
| 174 | char *c = new char[mTranslatedBitcodeSize]; |
| 175 | memcpy(c, &wrapper, actualWrapperLen); |
Stephen Hines | e9c850f | 2012-03-21 15:24:25 -0700 | [diff] [blame] | 176 | memcpy(c + actualWrapperLen, Buffer.c_str(), Buffer.size()); |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 177 | |
| 178 | mTranslatedBitcode = c; |
Stephen Hines | 932bc6e | 2011-07-27 16:26:26 -0700 | [diff] [blame] | 179 | |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | } // namespace bcinfo |