1 //===-- llvm/Bitcode/BitcodeReader.h - Bitcode reader ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header defines interfaces to read LLVM bitcode files/streams.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_BITCODE_BITCODEREADER_H
15 #define LLVM_BITCODE_BITCODEREADER_H
17 #include "llvm/Bitcode/BitCodes.h"
18 #include "llvm/IR/DiagnosticInfo.h"
19 #include "llvm/IR/ModuleSummaryIndex.h"
20 #include "llvm/Support/Endian.h"
21 #include "llvm/Support/Error.h"
22 #include "llvm/Support/ErrorOr.h"
23 #include "llvm/Support/MemoryBuffer.h"
30 // These functions are for converting Expected/Error values to
31 // ErrorOr/std::error_code for compatibility with legacy clients. FIXME:
32 // Remove these functions once no longer needed by the C and libLTO APIs.
34 std::error_code errorToErrorCodeAndEmitErrors(LLVMContext &Ctx, Error Err);
37 ErrorOr<T> expectedToErrorOrAndEmitErrors(LLVMContext &Ctx, Expected<T> Val) {
39 return errorToErrorCodeAndEmitErrors(Ctx, Val.takeError());
40 return std::move(*Val);
43 /// Represents a module in a bitcode file.
45 // This covers the identification (if present) and module blocks.
46 ArrayRef<uint8_t> Buffer;
47 StringRef ModuleIdentifier;
49 // The bitstream location of the IDENTIFICATION_BLOCK.
50 uint64_t IdentificationBit;
52 // The bitstream location of this module's MODULE_BLOCK.
55 BitcodeModule(ArrayRef<uint8_t> Buffer, StringRef ModuleIdentifier,
56 uint64_t IdentificationBit, uint64_t ModuleBit)
57 : Buffer(Buffer), ModuleIdentifier(ModuleIdentifier),
58 IdentificationBit(IdentificationBit), ModuleBit(ModuleBit) {}
61 friend Expected<std::vector<BitcodeModule>>
62 getBitcodeModuleList(MemoryBufferRef Buffer);
64 Expected<std::unique_ptr<Module>> getModuleImpl(LLVMContext &Context,
66 bool ShouldLazyLoadMetadata,
70 StringRef getBuffer() const {
71 return StringRef((const char *)Buffer.begin(), Buffer.size());
74 StringRef getModuleIdentifier() const { return ModuleIdentifier; }
76 /// Read the bitcode module and prepare for lazy deserialization of function
77 /// bodies. If ShouldLazyLoadMetadata is true, lazily load metadata as well.
78 /// If IsImporting is true, this module is being parsed for ThinLTO
79 /// importing into another module.
80 Expected<std::unique_ptr<Module>> getLazyModule(LLVMContext &Context,
81 bool ShouldLazyLoadMetadata,
84 /// Read the entire bitcode module and return it.
85 Expected<std::unique_ptr<Module>> parseModule(LLVMContext &Context);
87 /// Check if the given bitcode buffer contains a summary block.
88 Expected<bool> hasSummary();
90 /// Parse the specified bitcode buffer, returning the module summary index.
91 Expected<std::unique_ptr<ModuleSummaryIndex>> getSummary();
94 /// Returns a list of modules in the specified bitcode buffer.
95 Expected<std::vector<BitcodeModule>>
96 getBitcodeModuleList(MemoryBufferRef Buffer);
98 /// Read the header of the specified bitcode buffer and prepare for lazy
99 /// deserialization of function bodies. If ShouldLazyLoadMetadata is true,
100 /// lazily load metadata as well. If IsImporting is true, this module is
101 /// being parsed for ThinLTO importing into another module.
102 Expected<std::unique_ptr<Module>>
103 getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context,
104 bool ShouldLazyLoadMetadata = false,
105 bool IsImporting = false);
107 /// Like getLazyBitcodeModule, except that the module takes ownership of
108 /// the memory buffer if successful. If successful, this moves Buffer. On
109 /// error, this *does not* move Buffer. If IsImporting is true, this module is
110 /// being parsed for ThinLTO importing into another module.
111 Expected<std::unique_ptr<Module>> getOwningLazyBitcodeModule(
112 std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context,
113 bool ShouldLazyLoadMetadata = false, bool IsImporting = false);
115 /// Read the header of the specified bitcode buffer and extract just the
116 /// triple information. If successful, this returns a string. On error, this
118 Expected<std::string> getBitcodeTargetTriple(MemoryBufferRef Buffer);
120 /// Return true if \p Buffer contains a bitcode file with ObjC code (category
122 Expected<bool> isBitcodeContainingObjCCategory(MemoryBufferRef Buffer);
124 /// Read the header of the specified bitcode buffer and extract just the
125 /// producer string information. If successful, this returns a string. On
126 /// error, this returns "".
127 Expected<std::string> getBitcodeProducerString(MemoryBufferRef Buffer);
129 /// Read the specified bitcode file, returning the module.
130 Expected<std::unique_ptr<Module>> parseBitcodeFile(MemoryBufferRef Buffer,
131 LLVMContext &Context);
133 /// Check if the given bitcode buffer contains a summary block.
134 Expected<bool> hasGlobalValueSummary(MemoryBufferRef Buffer);
136 /// Parse the specified bitcode buffer, returning the module summary index.
137 Expected<std::unique_ptr<ModuleSummaryIndex>>
138 getModuleSummaryIndex(MemoryBufferRef Buffer);
140 /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
141 /// for an LLVM IR bitcode wrapper.
143 inline bool isBitcodeWrapper(const unsigned char *BufPtr,
144 const unsigned char *BufEnd) {
145 // See if you can find the hidden message in the magic bytes :-).
146 // (Hint: it's a little-endian encoding.)
147 return BufPtr != BufEnd &&
154 /// isRawBitcode - Return true if the given bytes are the magic bytes for
155 /// raw LLVM IR bitcode (without a wrapper).
157 inline bool isRawBitcode(const unsigned char *BufPtr,
158 const unsigned char *BufEnd) {
159 // These bytes sort of have a hidden message, but it's not in
160 // little-endian this time, and it's a little redundant.
161 return BufPtr != BufEnd &&
168 /// isBitcode - Return true if the given bytes are the magic bytes for
169 /// LLVM IR bitcode, either with or without a wrapper.
171 inline bool isBitcode(const unsigned char *BufPtr,
172 const unsigned char *BufEnd) {
173 return isBitcodeWrapper(BufPtr, BufEnd) ||
174 isRawBitcode(BufPtr, BufEnd);
177 /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
178 /// header for padding or other reasons. The format of this header is:
180 /// struct bc_header {
181 /// uint32_t Magic; // 0x0B17C0DE
182 /// uint32_t Version; // Version, currently always 0.
183 /// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
184 /// uint32_t BitcodeSize; // Size of traditional bitcode file.
185 /// ... potentially other gunk ...
188 /// This function is called when we find a file with a matching magic number.
189 /// In this case, skip down to the subsection of the file that is actually a
191 /// If 'VerifyBufferSize' is true, check that the buffer is large enough to
192 /// contain the whole bitcode file.
193 inline bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr,
194 const unsigned char *&BufEnd,
195 bool VerifyBufferSize) {
196 // Must contain the offset and size field!
197 if (unsigned(BufEnd - BufPtr) < BWH_SizeField + 4)
200 unsigned Offset = support::endian::read32le(&BufPtr[BWH_OffsetField]);
201 unsigned Size = support::endian::read32le(&BufPtr[BWH_SizeField]);
202 uint64_t BitcodeOffsetEnd = (uint64_t)Offset + (uint64_t)Size;
204 // Verify that Offset+Size fits in the file.
205 if (VerifyBufferSize && BitcodeOffsetEnd > uint64_t(BufEnd-BufPtr))
208 BufEnd = BufPtr+Size;
212 const std::error_category &BitcodeErrorCategory();
213 enum class BitcodeError { CorruptedBitcode = 1 };
214 inline std::error_code make_error_code(BitcodeError E) {
215 return std::error_code(static_cast<int>(E), BitcodeErrorCategory());
218 } // End llvm namespace
221 template <> struct is_error_code_enum<llvm::BitcodeError> : std::true_type {};