Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:22

0001 //===- llvm/Bitcode/BitcodeReader.h - Bitcode reader ------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This header defines interfaces to read LLVM bitcode files/streams.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_BITCODE_BITCODEREADER_H
0014 #define LLVM_BITCODE_BITCODEREADER_H
0015 
0016 #include "llvm/ADT/ArrayRef.h"
0017 #include "llvm/ADT/StringRef.h"
0018 #include "llvm/Bitstream/BitCodeEnums.h"
0019 #include "llvm/IR/GlobalValue.h"
0020 #include "llvm/Support/Endian.h"
0021 #include "llvm/Support/Error.h"
0022 #include "llvm/Support/ErrorOr.h"
0023 #include "llvm/Support/MemoryBufferRef.h"
0024 #include <cstdint>
0025 #include <memory>
0026 #include <optional>
0027 #include <string>
0028 #include <system_error>
0029 #include <vector>
0030 namespace llvm {
0031 
0032 class LLVMContext;
0033 class Module;
0034 class MemoryBuffer;
0035 class Metadata;
0036 class ModuleSummaryIndex;
0037 class Type;
0038 class Value;
0039 
0040 // Callback to override the data layout string of an imported bitcode module.
0041 // The first argument is the target triple, the second argument the data layout
0042 // string from the input, or a default string. It will be used if the callback
0043 // returns std::nullopt.
0044 typedef std::function<std::optional<std::string>(StringRef, StringRef)>
0045     DataLayoutCallbackFuncTy;
0046 
0047 typedef std::function<Type *(unsigned)> GetTypeByIDTy;
0048 
0049 typedef std::function<unsigned(unsigned, unsigned)> GetContainedTypeIDTy;
0050 
0051 typedef std::function<void(Value *, unsigned, GetTypeByIDTy,
0052                            GetContainedTypeIDTy)>
0053     ValueTypeCallbackTy;
0054 
0055 typedef std::function<void(Metadata **, unsigned, GetTypeByIDTy,
0056                            GetContainedTypeIDTy)>
0057     MDTypeCallbackTy;
0058 
0059 // These functions are for converting Expected/Error values to
0060 // ErrorOr/std::error_code for compatibility with legacy clients. FIXME:
0061 // Remove these functions once no longer needed by the C and libLTO APIs.
0062 
0063 std::error_code errorToErrorCodeAndEmitErrors(LLVMContext &Ctx, Error Err);
0064 
0065 template <typename T>
0066 ErrorOr<T> expectedToErrorOrAndEmitErrors(LLVMContext &Ctx, Expected<T> Val) {
0067   if (!Val)
0068     return errorToErrorCodeAndEmitErrors(Ctx, Val.takeError());
0069   return std::move(*Val);
0070 }
0071 
0072 struct ParserCallbacks {
0073   std::optional<DataLayoutCallbackFuncTy> DataLayout;
0074   /// The ValueType callback is called for every function definition or
0075   /// declaration and allows accessing the type information, also behind
0076   /// pointers. This can be useful, when the opaque pointer upgrade cleans all
0077   /// type information behind pointers.
0078   /// The second argument to ValueTypeCallback is the type ID of the
0079   /// function, the two passed functions can be used to extract type
0080   /// information.
0081   std::optional<ValueTypeCallbackTy> ValueType;
0082   /// The MDType callback is called for every value in metadata.
0083   std::optional<MDTypeCallbackTy> MDType;
0084 
0085   ParserCallbacks() = default;
0086   explicit ParserCallbacks(DataLayoutCallbackFuncTy DataLayout)
0087       : DataLayout(DataLayout) {}
0088 };
0089 
0090   struct BitcodeFileContents;
0091 
0092   /// Basic information extracted from a bitcode module to be used for LTO.
0093   struct BitcodeLTOInfo {
0094     bool IsThinLTO;
0095     bool HasSummary;
0096     bool EnableSplitLTOUnit;
0097     bool UnifiedLTO;
0098   };
0099 
0100   /// Represents a module in a bitcode file.
0101   class BitcodeModule {
0102     // This covers the identification (if present) and module blocks.
0103     ArrayRef<uint8_t> Buffer;
0104     StringRef ModuleIdentifier;
0105 
0106     // The string table used to interpret this module.
0107     StringRef Strtab;
0108 
0109     // The bitstream location of the IDENTIFICATION_BLOCK.
0110     uint64_t IdentificationBit;
0111 
0112     // The bitstream location of this module's MODULE_BLOCK.
0113     uint64_t ModuleBit;
0114 
0115     BitcodeModule(ArrayRef<uint8_t> Buffer, StringRef ModuleIdentifier,
0116                   uint64_t IdentificationBit, uint64_t ModuleBit)
0117         : Buffer(Buffer), ModuleIdentifier(ModuleIdentifier),
0118           IdentificationBit(IdentificationBit), ModuleBit(ModuleBit) {}
0119 
0120     // Calls the ctor.
0121     friend Expected<BitcodeFileContents>
0122     getBitcodeFileContents(MemoryBufferRef Buffer);
0123 
0124     Expected<std::unique_ptr<Module>>
0125     getModuleImpl(LLVMContext &Context, bool MaterializeAll,
0126                   bool ShouldLazyLoadMetadata, bool IsImporting,
0127                   ParserCallbacks Callbacks = {});
0128 
0129   public:
0130     StringRef getBuffer() const {
0131       return StringRef((const char *)Buffer.begin(), Buffer.size());
0132     }
0133 
0134     StringRef getStrtab() const { return Strtab; }
0135 
0136     StringRef getModuleIdentifier() const { return ModuleIdentifier; }
0137 
0138     /// Read the bitcode module and prepare for lazy deserialization of function
0139     /// bodies. If ShouldLazyLoadMetadata is true, lazily load metadata as well.
0140     /// If IsImporting is true, this module is being parsed for ThinLTO
0141     /// importing into another module.
0142     Expected<std::unique_ptr<Module>>
0143     getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata,
0144                   bool IsImporting, ParserCallbacks Callbacks = {});
0145 
0146     /// Read the entire bitcode module and return it.
0147     Expected<std::unique_ptr<Module>>
0148     parseModule(LLVMContext &Context, ParserCallbacks Callbacks = {});
0149 
0150     /// Returns information about the module to be used for LTO: whether to
0151     /// compile with ThinLTO, and whether it has a summary.
0152     Expected<BitcodeLTOInfo> getLTOInfo();
0153 
0154     /// Parse the specified bitcode buffer, returning the module summary index.
0155     Expected<std::unique_ptr<ModuleSummaryIndex>> getSummary();
0156 
0157     /// Parse the specified bitcode buffer and merge its module summary index
0158     /// into CombinedIndex.
0159     Error
0160     readSummary(ModuleSummaryIndex &CombinedIndex, StringRef ModulePath,
0161                 std::function<bool(GlobalValue::GUID)> IsPrevailing = nullptr);
0162   };
0163 
0164   struct BitcodeFileContents {
0165     std::vector<BitcodeModule> Mods;
0166     StringRef Symtab, StrtabForSymtab;
0167   };
0168 
0169   /// Returns the contents of a bitcode file. This includes the raw contents of
0170   /// the symbol table embedded in the bitcode file. Clients which require a
0171   /// symbol table should prefer to use irsymtab::read instead of this function
0172   /// because it creates a reader for the irsymtab and handles upgrading bitcode
0173   /// files without a symbol table or with an old symbol table.
0174   Expected<BitcodeFileContents> getBitcodeFileContents(MemoryBufferRef Buffer);
0175 
0176   /// Returns a list of modules in the specified bitcode buffer.
0177   Expected<std::vector<BitcodeModule>>
0178   getBitcodeModuleList(MemoryBufferRef Buffer);
0179 
0180   /// Read the header of the specified bitcode buffer and prepare for lazy
0181   /// deserialization of function bodies. If ShouldLazyLoadMetadata is true,
0182   /// lazily load metadata as well. If IsImporting is true, this module is
0183   /// being parsed for ThinLTO importing into another module.
0184   Expected<std::unique_ptr<Module>>
0185   getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context,
0186                        bool ShouldLazyLoadMetadata = false,
0187                        bool IsImporting = false,
0188                        ParserCallbacks Callbacks = {});
0189 
0190   /// Like getLazyBitcodeModule, except that the module takes ownership of
0191   /// the memory buffer if successful. If successful, this moves Buffer. On
0192   /// error, this *does not* move Buffer. If IsImporting is true, this module is
0193   /// being parsed for ThinLTO importing into another module.
0194   Expected<std::unique_ptr<Module>> getOwningLazyBitcodeModule(
0195       std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context,
0196       bool ShouldLazyLoadMetadata = false, bool IsImporting = false,
0197       ParserCallbacks Callbacks = {});
0198 
0199   /// Read the header of the specified bitcode buffer and extract just the
0200   /// triple information. If successful, this returns a string. On error, this
0201   /// returns "".
0202   Expected<std::string> getBitcodeTargetTriple(MemoryBufferRef Buffer);
0203 
0204   /// Return true if \p Buffer contains a bitcode file with ObjC code (category
0205   /// or class) in it.
0206   Expected<bool> isBitcodeContainingObjCCategory(MemoryBufferRef Buffer);
0207 
0208   /// Read the header of the specified bitcode buffer and extract just the
0209   /// producer string information. If successful, this returns a string. On
0210   /// error, this returns "".
0211   Expected<std::string> getBitcodeProducerString(MemoryBufferRef Buffer);
0212 
0213   /// Read the specified bitcode file, returning the module.
0214   Expected<std::unique_ptr<Module>>
0215   parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context,
0216                    ParserCallbacks Callbacks = {});
0217 
0218   /// Returns LTO information for the specified bitcode file.
0219   Expected<BitcodeLTOInfo> getBitcodeLTOInfo(MemoryBufferRef Buffer);
0220 
0221   /// Parse the specified bitcode buffer, returning the module summary index.
0222   Expected<std::unique_ptr<ModuleSummaryIndex>>
0223   getModuleSummaryIndex(MemoryBufferRef Buffer);
0224 
0225   /// Parse the specified bitcode buffer and merge the index into CombinedIndex.
0226   Error readModuleSummaryIndex(MemoryBufferRef Buffer,
0227                                ModuleSummaryIndex &CombinedIndex);
0228 
0229   /// Parse the module summary index out of an IR file and return the module
0230   /// summary index object if found, or an empty summary if not. If Path refers
0231   /// to an empty file and IgnoreEmptyThinLTOIndexFile is true, then
0232   /// this function will return nullptr.
0233   Expected<std::unique_ptr<ModuleSummaryIndex>>
0234   getModuleSummaryIndexForFile(StringRef Path,
0235                                bool IgnoreEmptyThinLTOIndexFile = false);
0236 
0237   /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
0238   /// for an LLVM IR bitcode wrapper.
0239   inline bool isBitcodeWrapper(const unsigned char *BufPtr,
0240                                const unsigned char *BufEnd) {
0241     // See if you can find the hidden message in the magic bytes :-).
0242     // (Hint: it's a little-endian encoding.)
0243     return BufPtr != BufEnd &&
0244            BufPtr[0] == 0xDE &&
0245            BufPtr[1] == 0xC0 &&
0246            BufPtr[2] == 0x17 &&
0247            BufPtr[3] == 0x0B;
0248   }
0249 
0250   /// isRawBitcode - Return true if the given bytes are the magic bytes for
0251   /// raw LLVM IR bitcode (without a wrapper).
0252   inline bool isRawBitcode(const unsigned char *BufPtr,
0253                            const unsigned char *BufEnd) {
0254     // These bytes sort of have a hidden message, but it's not in
0255     // little-endian this time, and it's a little redundant.
0256     return BufPtr != BufEnd &&
0257            BufPtr[0] == 'B' &&
0258            BufPtr[1] == 'C' &&
0259            BufPtr[2] == 0xc0 &&
0260            BufPtr[3] == 0xde;
0261   }
0262 
0263   /// isBitcode - Return true if the given bytes are the magic bytes for
0264   /// LLVM IR bitcode, either with or without a wrapper.
0265   inline bool isBitcode(const unsigned char *BufPtr,
0266                         const unsigned char *BufEnd) {
0267     return isBitcodeWrapper(BufPtr, BufEnd) ||
0268            isRawBitcode(BufPtr, BufEnd);
0269   }
0270 
0271   /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
0272   /// header for padding or other reasons.  The format of this header is:
0273   ///
0274   /// struct bc_header {
0275   ///   uint32_t Magic;         // 0x0B17C0DE
0276   ///   uint32_t Version;       // Version, currently always 0.
0277   ///   uint32_t BitcodeOffset; // Offset to traditional bitcode file.
0278   ///   uint32_t BitcodeSize;   // Size of traditional bitcode file.
0279   ///   ... potentially other gunk ...
0280   /// };
0281   ///
0282   /// This function is called when we find a file with a matching magic number.
0283   /// In this case, skip down to the subsection of the file that is actually a
0284   /// BC file.
0285   /// If 'VerifyBufferSize' is true, check that the buffer is large enough to
0286   /// contain the whole bitcode file.
0287   inline bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr,
0288                                        const unsigned char *&BufEnd,
0289                                        bool VerifyBufferSize) {
0290     // Must contain the offset and size field!
0291     if (unsigned(BufEnd - BufPtr) < BWH_SizeField + 4)
0292       return true;
0293 
0294     unsigned Offset = support::endian::read32le(&BufPtr[BWH_OffsetField]);
0295     unsigned Size = support::endian::read32le(&BufPtr[BWH_SizeField]);
0296     uint64_t BitcodeOffsetEnd = (uint64_t)Offset + (uint64_t)Size;
0297 
0298     // Verify that Offset+Size fits in the file.
0299     if (VerifyBufferSize && BitcodeOffsetEnd > uint64_t(BufEnd-BufPtr))
0300       return true;
0301     BufPtr += Offset;
0302     BufEnd = BufPtr+Size;
0303     return false;
0304   }
0305 
0306   APInt readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits);
0307 
0308   const std::error_category &BitcodeErrorCategory();
0309   enum class BitcodeError { CorruptedBitcode = 1 };
0310   inline std::error_code make_error_code(BitcodeError E) {
0311     return std::error_code(static_cast<int>(E), BitcodeErrorCategory());
0312   }
0313 
0314 } // end namespace llvm
0315 
0316 namespace std {
0317 
0318 template <> struct is_error_code_enum<llvm::BitcodeError> : std::true_type {};
0319 
0320 } // end namespace std
0321 
0322 #endif // LLVM_BITCODE_BITCODEREADER_H