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/FunctionInfo.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 /// Read the header of the specified bitcode buffer and prepare for lazy 34 /// deserialization of function bodies. If ShouldLazyLoadMetadata is true, 35 /// lazily load metadata as well. If successful, this moves Buffer. On 36 /// error, this *does not* move Buffer. 37 ErrorOr<std::unique_ptr<Module>> 38 getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer, 39 LLVMContext &Context, 40 bool ShouldLazyLoadMetadata = false); 41 42 /// Read the header of the specified stream and prepare for lazy 43 /// deserialization and streaming of function bodies. 44 ErrorOr<std::unique_ptr<Module>> 45 getStreamedBitcodeModule(StringRef Name, 46 std::unique_ptr<DataStreamer> Streamer, 47 LLVMContext &Context); 48 49 /// Read the header of the specified bitcode buffer and extract just the 50 /// triple information. If successful, this returns a string. On error, this 51 /// returns "". 52 std::string getBitcodeTargetTriple(MemoryBufferRef Buffer, 53 LLVMContext &Context); 54 55 /// Read the header of the specified bitcode buffer and extract just the 56 /// producer string information. If successful, this returns a string. On 57 /// error, this returns "". 58 std::string getBitcodeProducerString(MemoryBufferRef Buffer, 59 LLVMContext &Context); 60 61 /// Read the specified bitcode file, returning the module. 62 ErrorOr<std::unique_ptr<Module>> parseBitcodeFile(MemoryBufferRef Buffer, 63 LLVMContext &Context); 64 65 /// Check if the given bitcode buffer contains a function summary block. 66 bool hasFunctionSummary(MemoryBufferRef Buffer, 67 DiagnosticHandlerFunction DiagnosticHandler); 68 69 /// Parse the specified bitcode buffer, returning the function info index. 70 /// If IsLazy is true, parse the entire function summary into 71 /// the index. Otherwise skip the function summary section, and only create 72 /// an index object with a map from function name to function summary offset. 73 /// The index is used to perform lazy function summary reading later. 74 ErrorOr<std::unique_ptr<FunctionInfoIndex>> 75 getFunctionInfoIndex(MemoryBufferRef Buffer, 76 DiagnosticHandlerFunction DiagnosticHandler, 77 bool IsLazy = false); 78 79 /// This method supports lazy reading of function summary data from the 80 /// combined index during function importing. When reading the combined index 81 /// file, getFunctionInfoIndex is first invoked with IsLazy=true. 82 /// Then this method is called for each function considered for importing, 83 /// to parse the summary information for the given function name into 84 /// the index. 85 std::error_code readFunctionSummary( 86 MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler, 87 StringRef FunctionName, std::unique_ptr<FunctionInfoIndex> Index); 88 89 /// \brief Write the specified module to the specified raw output stream. 90 /// 91 /// For streams where it matters, the given stream should be in "binary" 92 /// mode. 93 /// 94 /// If \c ShouldPreserveUseListOrder, encode the use-list order for each \a 95 /// Value in \c M. These will be reconstructed exactly when \a M is 96 /// deserialized. 97 /// 98 /// If \c EmitFunctionSummary, emit the function summary index (currently 99 /// for use in ThinLTO optimization). 100 void WriteBitcodeToFile(const Module *M, raw_ostream &Out, 101 bool ShouldPreserveUseListOrder = false, 102 bool EmitFunctionSummary = false); 103 104 /// Write the specified function 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. 107 void WriteFunctionSummaryToFile(const FunctionInfoIndex &Index, 108 raw_ostream &Out); 109 110 /// isBitcodeWrapper - Return true if the given bytes are the magic bytes 111 /// for an LLVM IR bitcode wrapper. 112 /// isBitcodeWrapper(const unsigned char * BufPtr,const unsigned char * BufEnd)113 inline bool isBitcodeWrapper(const unsigned char *BufPtr, 114 const unsigned char *BufEnd) { 115 // See if you can find the hidden message in the magic bytes :-). 116 // (Hint: it's a little-endian encoding.) 117 return BufPtr != BufEnd && 118 BufPtr[0] == 0xDE && 119 BufPtr[1] == 0xC0 && 120 BufPtr[2] == 0x17 && 121 BufPtr[3] == 0x0B; 122 } 123 124 /// isRawBitcode - Return true if the given bytes are the magic bytes for 125 /// raw LLVM IR bitcode (without a wrapper). 126 /// isRawBitcode(const unsigned char * BufPtr,const unsigned char * BufEnd)127 inline bool isRawBitcode(const unsigned char *BufPtr, 128 const unsigned char *BufEnd) { 129 // These bytes sort of have a hidden message, but it's not in 130 // little-endian this time, and it's a little redundant. 131 return BufPtr != BufEnd && 132 BufPtr[0] == 'B' && 133 BufPtr[1] == 'C' && 134 BufPtr[2] == 0xc0 && 135 BufPtr[3] == 0xde; 136 } 137 138 /// isBitcode - Return true if the given bytes are the magic bytes for 139 /// LLVM IR bitcode, either with or without a wrapper. 140 /// isBitcode(const unsigned char * BufPtr,const unsigned char * BufEnd)141 inline bool isBitcode(const unsigned char *BufPtr, 142 const unsigned char *BufEnd) { 143 return isBitcodeWrapper(BufPtr, BufEnd) || 144 isRawBitcode(BufPtr, BufEnd); 145 } 146 147 /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special 148 /// header for padding or other reasons. The format of this header is: 149 /// 150 /// struct bc_header { 151 /// uint32_t Magic; // 0x0B17C0DE 152 /// uint32_t Version; // Version, currently always 0. 153 /// uint32_t BitcodeOffset; // Offset to traditional bitcode file. 154 /// uint32_t BitcodeSize; // Size of traditional bitcode file. 155 /// ... potentially other gunk ... 156 /// }; 157 /// 158 /// This function is called when we find a file with a matching magic number. 159 /// In this case, skip down to the subsection of the file that is actually a 160 /// BC file. 161 /// If 'VerifyBufferSize' is true, check that the buffer is large enough to 162 /// contain the whole bitcode file. SkipBitcodeWrapperHeader(const unsigned char * & BufPtr,const unsigned char * & BufEnd,bool VerifyBufferSize)163 inline bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr, 164 const unsigned char *&BufEnd, 165 bool VerifyBufferSize) { 166 enum { 167 KnownHeaderSize = 4*4, // Size of header we read. 168 OffsetField = 2*4, // Offset in bytes to Offset field. 169 SizeField = 3*4 // Offset in bytes to Size field. 170 }; 171 172 // Must contain the header! 173 if (BufEnd-BufPtr < KnownHeaderSize) return true; 174 175 unsigned Offset = support::endian::read32le(&BufPtr[OffsetField]); 176 unsigned Size = support::endian::read32le(&BufPtr[SizeField]); 177 178 // Verify that Offset+Size fits in the file. 179 if (VerifyBufferSize && Offset+Size > unsigned(BufEnd-BufPtr)) 180 return true; 181 BufPtr += Offset; 182 BufEnd = BufPtr+Size; 183 return false; 184 } 185 186 const std::error_category &BitcodeErrorCategory(); 187 enum class BitcodeError { InvalidBitcodeSignature = 1, CorruptedBitcode }; make_error_code(BitcodeError E)188 inline std::error_code make_error_code(BitcodeError E) { 189 return std::error_code(static_cast<int>(E), BitcodeErrorCategory()); 190 } 191 192 class BitcodeDiagnosticInfo : public DiagnosticInfo { 193 const Twine &Msg; 194 std::error_code EC; 195 196 public: 197 BitcodeDiagnosticInfo(std::error_code EC, DiagnosticSeverity Severity, 198 const Twine &Msg); 199 void print(DiagnosticPrinter &DP) const override; getError()200 std::error_code getError() const { return EC; } 201 classof(const DiagnosticInfo * DI)202 static bool classof(const DiagnosticInfo *DI) { 203 return DI->getKind() == DK_Bitcode; 204 } 205 }; 206 207 } // End llvm namespace 208 209 namespace std { 210 template <> struct is_error_code_enum<llvm::BitcodeError> : std::true_type {}; 211 } 212 213 #endif 214