1 //===-- llvm/Bitcode/ReaderWriter.h - Bitcode reader/writers ----*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This header defines interfaces to read and write LLVM bitcode files/streams. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_BITCODE_READERWRITER_H 15 #define LLVM_BITCODE_READERWRITER_H 16 17 #include "llvm/IR/DiagnosticInfo.h" 18 #include "llvm/IR/ModuleSummaryIndex.h" 19 #include "llvm/Support/Endian.h" 20 #include "llvm/Support/ErrorOr.h" 21 #include "llvm/Support/MemoryBuffer.h" 22 #include <memory> 23 #include <string> 24 25 namespace llvm { 26 class BitstreamWriter; 27 class DataStreamer; 28 class LLVMContext; 29 class Module; 30 class ModulePass; 31 class raw_ostream; 32 33 /// Offsets of the 32-bit fields of bitcode wrapper header. 34 static const unsigned BWH_MagicField = 0*4; 35 static const unsigned BWH_VersionField = 1*4; 36 static const unsigned BWH_OffsetField = 2*4; 37 static const unsigned BWH_SizeField = 3*4; 38 static const unsigned BWH_CPUTypeField = 4*4; 39 static const unsigned BWH_HeaderSize = 5*4; 40 41 /// Read the header of the specified bitcode buffer and prepare for lazy 42 /// deserialization of function bodies. If ShouldLazyLoadMetadata is true, 43 /// lazily load metadata as well. If successful, this moves Buffer. On 44 /// error, this *does not* move Buffer. 45 ErrorOr<std::unique_ptr<Module>> 46 getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer, 47 LLVMContext &Context, 48 bool ShouldLazyLoadMetadata = false); 49 50 /// Read the header of the specified stream and prepare for lazy 51 /// deserialization and streaming of function bodies. 52 ErrorOr<std::unique_ptr<Module>> 53 getStreamedBitcodeModule(StringRef Name, 54 std::unique_ptr<DataStreamer> Streamer, 55 LLVMContext &Context); 56 57 /// Read the header of the specified bitcode buffer and extract just the 58 /// triple information. If successful, this returns a string. On error, this 59 /// returns "". 60 std::string getBitcodeTargetTriple(MemoryBufferRef Buffer, 61 LLVMContext &Context); 62 63 /// Return true if \p Buffer contains a bitcode file with ObjC code (category 64 /// or class) in it. 65 bool isBitcodeContainingObjCCategory(MemoryBufferRef Buffer, 66 LLVMContext &Context); 67 68 /// Read the header of the specified bitcode buffer and extract just the 69 /// producer string information. If successful, this returns a string. On 70 /// error, this returns "". 71 std::string getBitcodeProducerString(MemoryBufferRef Buffer, 72 LLVMContext &Context); 73 74 /// Read the specified bitcode file, returning the module. 75 ErrorOr<std::unique_ptr<Module>> parseBitcodeFile(MemoryBufferRef Buffer, 76 LLVMContext &Context); 77 78 /// Check if the given bitcode buffer contains a summary block. 79 bool 80 hasGlobalValueSummary(MemoryBufferRef Buffer, 81 const DiagnosticHandlerFunction &DiagnosticHandler); 82 83 /// Parse the specified bitcode buffer, returning the module summary index. 84 ErrorOr<std::unique_ptr<ModuleSummaryIndex>> 85 getModuleSummaryIndex(MemoryBufferRef Buffer, 86 const DiagnosticHandlerFunction &DiagnosticHandler); 87 88 /// \brief Write the specified module to the specified raw output stream. 89 /// 90 /// For streams where it matters, the given stream should be in "binary" 91 /// mode. 92 /// 93 /// If \c ShouldPreserveUseListOrder, encode the use-list order for each \a 94 /// Value in \c M. These will be reconstructed exactly when \a M is 95 /// deserialized. 96 /// 97 /// If \c EmitSummaryIndex, emit the module's summary index (currently 98 /// for use in ThinLTO optimization). 99 void WriteBitcodeToFile(const Module *M, raw_ostream &Out, 100 bool ShouldPreserveUseListOrder = false, 101 const ModuleSummaryIndex *Index = nullptr, 102 bool GenerateHash = false); 103 104 /// Write the specified module summary index to the given raw output stream, 105 /// where it will be written in a new bitcode block. This is used when 106 /// writing the combined index file for ThinLTO. When writing a subset of the 107 /// index for a distributed backend, provide the \p ModuleToSummariesForIndex 108 /// map. 109 void WriteIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out, 110 std::map<std::string, GVSummaryMapTy> 111 *ModuleToSummariesForIndex = nullptr); 112 113 /// isBitcodeWrapper - Return true if the given bytes are the magic bytes 114 /// for an LLVM IR bitcode wrapper. 115 /// isBitcodeWrapper(const unsigned char * BufPtr,const unsigned char * BufEnd)116 inline bool isBitcodeWrapper(const unsigned char *BufPtr, 117 const unsigned char *BufEnd) { 118 // See if you can find the hidden message in the magic bytes :-). 119 // (Hint: it's a little-endian encoding.) 120 return BufPtr != BufEnd && 121 BufPtr[0] == 0xDE && 122 BufPtr[1] == 0xC0 && 123 BufPtr[2] == 0x17 && 124 BufPtr[3] == 0x0B; 125 } 126 127 /// isRawBitcode - Return true if the given bytes are the magic bytes for 128 /// raw LLVM IR bitcode (without a wrapper). 129 /// isRawBitcode(const unsigned char * BufPtr,const unsigned char * BufEnd)130 inline bool isRawBitcode(const unsigned char *BufPtr, 131 const unsigned char *BufEnd) { 132 // These bytes sort of have a hidden message, but it's not in 133 // little-endian this time, and it's a little redundant. 134 return BufPtr != BufEnd && 135 BufPtr[0] == 'B' && 136 BufPtr[1] == 'C' && 137 BufPtr[2] == 0xc0 && 138 BufPtr[3] == 0xde; 139 } 140 141 /// isBitcode - Return true if the given bytes are the magic bytes for 142 /// LLVM IR bitcode, either with or without a wrapper. 143 /// isBitcode(const unsigned char * BufPtr,const unsigned char * BufEnd)144 inline bool isBitcode(const unsigned char *BufPtr, 145 const unsigned char *BufEnd) { 146 return isBitcodeWrapper(BufPtr, BufEnd) || 147 isRawBitcode(BufPtr, BufEnd); 148 } 149 150 /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special 151 /// header for padding or other reasons. The format of this header is: 152 /// 153 /// struct bc_header { 154 /// uint32_t Magic; // 0x0B17C0DE 155 /// uint32_t Version; // Version, currently always 0. 156 /// uint32_t BitcodeOffset; // Offset to traditional bitcode file. 157 /// uint32_t BitcodeSize; // Size of traditional bitcode file. 158 /// ... potentially other gunk ... 159 /// }; 160 /// 161 /// This function is called when we find a file with a matching magic number. 162 /// In this case, skip down to the subsection of the file that is actually a 163 /// BC file. 164 /// If 'VerifyBufferSize' is true, check that the buffer is large enough to 165 /// contain the whole bitcode file. SkipBitcodeWrapperHeader(const unsigned char * & BufPtr,const unsigned char * & BufEnd,bool VerifyBufferSize)166 inline bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr, 167 const unsigned char *&BufEnd, 168 bool VerifyBufferSize) { 169 // Must contain the offset and size field! 170 if (unsigned(BufEnd - BufPtr) < BWH_SizeField + 4) 171 return true; 172 173 unsigned Offset = support::endian::read32le(&BufPtr[BWH_OffsetField]); 174 unsigned Size = support::endian::read32le(&BufPtr[BWH_SizeField]); 175 uint64_t BitcodeOffsetEnd = (uint64_t)Offset + (uint64_t)Size; 176 177 // Verify that Offset+Size fits in the file. 178 if (VerifyBufferSize && BitcodeOffsetEnd > uint64_t(BufEnd-BufPtr)) 179 return true; 180 BufPtr += Offset; 181 BufEnd = BufPtr+Size; 182 return false; 183 } 184 185 const std::error_category &BitcodeErrorCategory(); 186 enum class BitcodeError { InvalidBitcodeSignature = 1, CorruptedBitcode }; make_error_code(BitcodeError E)187 inline std::error_code make_error_code(BitcodeError E) { 188 return std::error_code(static_cast<int>(E), BitcodeErrorCategory()); 189 } 190 191 class BitcodeDiagnosticInfo : public DiagnosticInfo { 192 const Twine &Msg; 193 std::error_code EC; 194 195 public: 196 BitcodeDiagnosticInfo(std::error_code EC, DiagnosticSeverity Severity, 197 const Twine &Msg); 198 void print(DiagnosticPrinter &DP) const override; getError()199 std::error_code getError() const { return EC; } 200 classof(const DiagnosticInfo * DI)201 static bool classof(const DiagnosticInfo *DI) { 202 return DI->getKind() == DK_Bitcode; 203 } 204 }; 205 206 } // End llvm namespace 207 208 namespace std { 209 template <> struct is_error_code_enum<llvm::BitcodeError> : std::true_type {}; 210 } 211 212 #endif 213