BitcodeReader.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //===- llvm/Bitcode/BitcodeReader.h - Bitcode reader ------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This header defines interfaces to read LLVM bitcode files/streams.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_BITCODE_BITCODEREADER_H
  13. #define LLVM_BITCODE_BITCODEREADER_H
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/Bitstream/BitCodes.h"
  17. #include "llvm/IR/ModuleSummaryIndex.h"
  18. #include "llvm/Support/Endian.h"
  19. #include "llvm/Support/Error.h"
  20. #include "llvm/Support/ErrorOr.h"
  21. #include "llvm/Support/MemoryBuffer.h"
  22. #include <cstdint>
  23. #include <memory>
  24. #include <string>
  25. #include <system_error>
  26. #include <vector>
  27. namespace llvm {
  28. class LLVMContext;
  29. class Module;
  30. typedef llvm::function_ref<Optional<std::string>(StringRef)>
  31. DataLayoutCallbackTy;
  32. // These functions are for converting Expected/Error values to
  33. // ErrorOr/std::error_code for compatibility with legacy clients. FIXME:
  34. // Remove these functions once no longer needed by the C and libLTO APIs.
  35. std::error_code errorToErrorCodeAndEmitErrors(LLVMContext &Ctx, Error Err);
  36. template <typename T>
  37. ErrorOr<T> expectedToErrorOrAndEmitErrors(LLVMContext &Ctx, Expected<T> Val) {
  38. if (!Val)
  39. return errorToErrorCodeAndEmitErrors(Ctx, Val.takeError());
  40. return std::move(*Val);
  41. }
  42. struct BitcodeFileContents;
  43. /// Basic information extracted from a bitcode module to be used for LTO.
  44. struct BitcodeLTOInfo {
  45. bool IsThinLTO;
  46. bool HasSummary;
  47. bool EnableSplitLTOUnit;
  48. };
  49. /// Represents a module in a bitcode file.
  50. class BitcodeModule {
  51. // This covers the identification (if present) and module blocks.
  52. ArrayRef<uint8_t> Buffer;
  53. StringRef ModuleIdentifier;
  54. // The string table used to interpret this module.
  55. StringRef Strtab;
  56. // The bitstream location of the IDENTIFICATION_BLOCK.
  57. uint64_t IdentificationBit;
  58. // The bitstream location of this module's MODULE_BLOCK.
  59. uint64_t ModuleBit;
  60. BitcodeModule(ArrayRef<uint8_t> Buffer, StringRef ModuleIdentifier,
  61. uint64_t IdentificationBit, uint64_t ModuleBit)
  62. : Buffer(Buffer), ModuleIdentifier(ModuleIdentifier),
  63. IdentificationBit(IdentificationBit), ModuleBit(ModuleBit) {}
  64. // Calls the ctor.
  65. friend Expected<BitcodeFileContents>
  66. getBitcodeFileContents(MemoryBufferRef Buffer);
  67. Expected<std::unique_ptr<Module>>
  68. getModuleImpl(LLVMContext &Context, bool MaterializeAll,
  69. bool ShouldLazyLoadMetadata, bool IsImporting,
  70. DataLayoutCallbackTy DataLayoutCallback);
  71. public:
  72. StringRef getBuffer() const {
  73. return StringRef((const char *)Buffer.begin(), Buffer.size());
  74. }
  75. StringRef getStrtab() const { return Strtab; }
  76. StringRef getModuleIdentifier() const { return ModuleIdentifier; }
  77. /// Read the bitcode module and prepare for lazy deserialization of function
  78. /// bodies. If ShouldLazyLoadMetadata is true, lazily load metadata as well.
  79. /// If IsImporting is true, this module is being parsed for ThinLTO
  80. /// importing into another module.
  81. Expected<std::unique_ptr<Module>> getLazyModule(LLVMContext &Context,
  82. bool ShouldLazyLoadMetadata,
  83. bool IsImporting);
  84. /// Read the entire bitcode module and return it.
  85. Expected<std::unique_ptr<Module>> parseModule(
  86. LLVMContext &Context, DataLayoutCallbackTy DataLayoutCallback =
  87. [](StringRef) { return None; });
  88. /// Returns information about the module to be used for LTO: whether to
  89. /// compile with ThinLTO, and whether it has a summary.
  90. Expected<BitcodeLTOInfo> getLTOInfo();
  91. /// Parse the specified bitcode buffer, returning the module summary index.
  92. Expected<std::unique_ptr<ModuleSummaryIndex>> getSummary();
  93. /// Parse the specified bitcode buffer and merge its module summary index
  94. /// into CombinedIndex.
  95. Error readSummary(ModuleSummaryIndex &CombinedIndex, StringRef ModulePath,
  96. uint64_t ModuleId);
  97. };
  98. struct BitcodeFileContents {
  99. std::vector<BitcodeModule> Mods;
  100. StringRef Symtab, StrtabForSymtab;
  101. };
  102. /// Returns the contents of a bitcode file. This includes the raw contents of
  103. /// the symbol table embedded in the bitcode file. Clients which require a
  104. /// symbol table should prefer to use irsymtab::read instead of this function
  105. /// because it creates a reader for the irsymtab and handles upgrading bitcode
  106. /// files without a symbol table or with an old symbol table.
  107. Expected<BitcodeFileContents> getBitcodeFileContents(MemoryBufferRef Buffer);
  108. /// Returns a list of modules in the specified bitcode buffer.
  109. Expected<std::vector<BitcodeModule>>
  110. getBitcodeModuleList(MemoryBufferRef Buffer);
  111. /// Read the header of the specified bitcode buffer and prepare for lazy
  112. /// deserialization of function bodies. If ShouldLazyLoadMetadata is true,
  113. /// lazily load metadata as well. If IsImporting is true, this module is
  114. /// being parsed for ThinLTO importing into another module.
  115. Expected<std::unique_ptr<Module>>
  116. getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context,
  117. bool ShouldLazyLoadMetadata = false,
  118. bool IsImporting = false);
  119. /// Like getLazyBitcodeModule, except that the module takes ownership of
  120. /// the memory buffer if successful. If successful, this moves Buffer. On
  121. /// error, this *does not* move Buffer. If IsImporting is true, this module is
  122. /// being parsed for ThinLTO importing into another module.
  123. Expected<std::unique_ptr<Module>> getOwningLazyBitcodeModule(
  124. std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context,
  125. bool ShouldLazyLoadMetadata = false, bool IsImporting = false);
  126. /// Read the header of the specified bitcode buffer and extract just the
  127. /// triple information. If successful, this returns a string. On error, this
  128. /// returns "".
  129. Expected<std::string> getBitcodeTargetTriple(MemoryBufferRef Buffer);
  130. /// Return true if \p Buffer contains a bitcode file with ObjC code (category
  131. /// or class) in it.
  132. Expected<bool> isBitcodeContainingObjCCategory(MemoryBufferRef Buffer);
  133. /// Read the header of the specified bitcode buffer and extract just the
  134. /// producer string information. If successful, this returns a string. On
  135. /// error, this returns "".
  136. Expected<std::string> getBitcodeProducerString(MemoryBufferRef Buffer);
  137. /// Read the specified bitcode file, returning the module.
  138. Expected<std::unique_ptr<Module>> parseBitcodeFile(
  139. MemoryBufferRef Buffer, LLVMContext &Context,
  140. DataLayoutCallbackTy DataLayoutCallback = [](StringRef) {
  141. return None;
  142. });
  143. /// Returns LTO information for the specified bitcode file.
  144. Expected<BitcodeLTOInfo> getBitcodeLTOInfo(MemoryBufferRef Buffer);
  145. /// Parse the specified bitcode buffer, returning the module summary index.
  146. Expected<std::unique_ptr<ModuleSummaryIndex>>
  147. getModuleSummaryIndex(MemoryBufferRef Buffer);
  148. /// Parse the specified bitcode buffer and merge the index into CombinedIndex.
  149. Error readModuleSummaryIndex(MemoryBufferRef Buffer,
  150. ModuleSummaryIndex &CombinedIndex,
  151. uint64_t ModuleId);
  152. /// Parse the module summary index out of an IR file and return the module
  153. /// summary index object if found, or an empty summary if not. If Path refers
  154. /// to an empty file and IgnoreEmptyThinLTOIndexFile is true, then
  155. /// this function will return nullptr.
  156. Expected<std::unique_ptr<ModuleSummaryIndex>>
  157. getModuleSummaryIndexForFile(StringRef Path,
  158. bool IgnoreEmptyThinLTOIndexFile = false);
  159. /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
  160. /// for an LLVM IR bitcode wrapper.
  161. inline bool isBitcodeWrapper(const unsigned char *BufPtr,
  162. const unsigned char *BufEnd) {
  163. // See if you can find the hidden message in the magic bytes :-).
  164. // (Hint: it's a little-endian encoding.)
  165. return BufPtr != BufEnd &&
  166. BufPtr[0] == 0xDE &&
  167. BufPtr[1] == 0xC0 &&
  168. BufPtr[2] == 0x17 &&
  169. BufPtr[3] == 0x0B;
  170. }
  171. /// isRawBitcode - Return true if the given bytes are the magic bytes for
  172. /// raw LLVM IR bitcode (without a wrapper).
  173. inline bool isRawBitcode(const unsigned char *BufPtr,
  174. const unsigned char *BufEnd) {
  175. // These bytes sort of have a hidden message, but it's not in
  176. // little-endian this time, and it's a little redundant.
  177. return BufPtr != BufEnd &&
  178. BufPtr[0] == 'B' &&
  179. BufPtr[1] == 'C' &&
  180. BufPtr[2] == 0xc0 &&
  181. BufPtr[3] == 0xde;
  182. }
  183. /// isBitcode - Return true if the given bytes are the magic bytes for
  184. /// LLVM IR bitcode, either with or without a wrapper.
  185. inline bool isBitcode(const unsigned char *BufPtr,
  186. const unsigned char *BufEnd) {
  187. return isBitcodeWrapper(BufPtr, BufEnd) ||
  188. isRawBitcode(BufPtr, BufEnd);
  189. }
  190. /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
  191. /// header for padding or other reasons. The format of this header is:
  192. ///
  193. /// struct bc_header {
  194. /// uint32_t Magic; // 0x0B17C0DE
  195. /// uint32_t Version; // Version, currently always 0.
  196. /// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
  197. /// uint32_t BitcodeSize; // Size of traditional bitcode file.
  198. /// ... potentially other gunk ...
  199. /// };
  200. ///
  201. /// This function is called when we find a file with a matching magic number.
  202. /// In this case, skip down to the subsection of the file that is actually a
  203. /// BC file.
  204. /// If 'VerifyBufferSize' is true, check that the buffer is large enough to
  205. /// contain the whole bitcode file.
  206. inline bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr,
  207. const unsigned char *&BufEnd,
  208. bool VerifyBufferSize) {
  209. // Must contain the offset and size field!
  210. if (unsigned(BufEnd - BufPtr) < BWH_SizeField + 4)
  211. return true;
  212. unsigned Offset = support::endian::read32le(&BufPtr[BWH_OffsetField]);
  213. unsigned Size = support::endian::read32le(&BufPtr[BWH_SizeField]);
  214. uint64_t BitcodeOffsetEnd = (uint64_t)Offset + (uint64_t)Size;
  215. // Verify that Offset+Size fits in the file.
  216. if (VerifyBufferSize && BitcodeOffsetEnd > uint64_t(BufEnd-BufPtr))
  217. return true;
  218. BufPtr += Offset;
  219. BufEnd = BufPtr+Size;
  220. return false;
  221. }
  222. APInt readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits);
  223. const std::error_category &BitcodeErrorCategory();
  224. enum class BitcodeError { CorruptedBitcode = 1 };
  225. inline std::error_code make_error_code(BitcodeError E) {
  226. return std::error_code(static_cast<int>(E), BitcodeErrorCategory());
  227. }
  228. } // end namespace llvm
  229. namespace std {
  230. template <> struct is_error_code_enum<llvm::BitcodeError> : std::true_type {};
  231. } // end namespace std
  232. #endif // LLVM_BITCODE_BITCODEREADER_H