Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===- DbiModuleDescriptorBuilder.h - PDB module information ----*- 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 #ifndef LLVM_DEBUGINFO_PDB_NATIVE_DBIMODULEDESCRIPTORBUILDER_H
0010 #define LLVM_DEBUGINFO_PDB_NATIVE_DBIMODULEDESCRIPTORBUILDER_H
0011 
0012 #include "llvm/ADT/ArrayRef.h"
0013 #include "llvm/ADT/StringRef.h"
0014 #include "llvm/DebugInfo/CodeView/CVRecord.h"
0015 #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
0016 #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
0017 #include "llvm/Support/BinaryStreamRef.h"
0018 #include "llvm/Support/Error.h"
0019 #include <cstdint>
0020 #include <string>
0021 #include <vector>
0022 
0023 namespace llvm {
0024 class BinaryStreamWriter;
0025 namespace codeview {
0026 class DebugSubsection;
0027 }
0028 
0029 namespace msf {
0030 class MSFBuilder;
0031 struct MSFLayout;
0032 }
0033 namespace pdb {
0034 
0035 // Represents merged or unmerged symbols. Merged symbols can be written to the
0036 // output file as is, but unmerged symbols must be rewritten first. In either
0037 // case, the size must be known up front.
0038 struct SymbolListWrapper {
0039   explicit SymbolListWrapper(ArrayRef<uint8_t> Syms)
0040       : SymPtr(const_cast<uint8_t *>(Syms.data())), SymSize(Syms.size()),
0041         NeedsToBeMerged(false) {}
0042   explicit SymbolListWrapper(void *SymSrc, uint32_t Length)
0043       : SymPtr(SymSrc), SymSize(Length), NeedsToBeMerged(true) {}
0044 
0045   ArrayRef<uint8_t> asArray() const {
0046     return ArrayRef<uint8_t>(static_cast<const uint8_t *>(SymPtr), SymSize);
0047   }
0048 
0049   uint32_t size() const { return SymSize; }
0050 
0051   void *SymPtr = nullptr;
0052   uint32_t SymSize = 0;
0053   bool NeedsToBeMerged = false;
0054 };
0055 
0056 /// Represents a string table reference at some offset in the module symbol
0057 /// stream.
0058 struct StringTableFixup {
0059   uint32_t StrTabOffset = 0;
0060   uint32_t SymOffsetOfReference = 0;
0061 };
0062 
0063 class DbiModuleDescriptorBuilder {
0064   friend class DbiStreamBuilder;
0065 
0066 public:
0067   DbiModuleDescriptorBuilder(StringRef ModuleName, uint32_t ModIndex,
0068                              msf::MSFBuilder &Msf);
0069   ~DbiModuleDescriptorBuilder();
0070 
0071   DbiModuleDescriptorBuilder(const DbiModuleDescriptorBuilder &) = delete;
0072   DbiModuleDescriptorBuilder &
0073   operator=(const DbiModuleDescriptorBuilder &) = delete;
0074 
0075   void setPdbFilePathNI(uint32_t NI);
0076   void setObjFileName(StringRef Name);
0077 
0078   // Callback to merge one source of unmerged symbols.
0079   using MergeSymbolsCallback = Error (*)(void *Ctx, void *Symbols,
0080                                          BinaryStreamWriter &Writer);
0081 
0082   void setMergeSymbolsCallback(void *Ctx, MergeSymbolsCallback Callback) {
0083     MergeSymsCtx = Ctx;
0084     MergeSymsCallback = Callback;
0085   }
0086 
0087   void setStringTableFixups(std::vector<StringTableFixup> &&Fixups) {
0088     StringTableFixups = std::move(Fixups);
0089   }
0090 
0091   void setFirstSectionContrib(const SectionContrib &SC);
0092   void addSymbol(codeview::CVSymbol Symbol);
0093   void addSymbolsInBulk(ArrayRef<uint8_t> BulkSymbols);
0094 
0095   // Add symbols of known size which will be merged (rewritten) when committing
0096   // the PDB to disk.
0097   void addUnmergedSymbols(void *SymSrc, uint32_t SymLength);
0098 
0099   void
0100   addDebugSubsection(std::shared_ptr<codeview::DebugSubsection> Subsection);
0101 
0102   void
0103   addDebugSubsection(const codeview::DebugSubsectionRecord &SubsectionContents);
0104 
0105   uint16_t getStreamIndex() const;
0106   StringRef getModuleName() const { return ModuleName; }
0107   StringRef getObjFileName() const { return ObjFileName; }
0108 
0109   unsigned getModuleIndex() const { return Layout.Mod; }
0110 
0111   ArrayRef<std::string> source_files() const { return SourceFiles; }
0112 
0113   uint32_t calculateSerializedLength() const;
0114 
0115   /// Return the offset within the module symbol stream of the next symbol
0116   /// record passed to addSymbol. Add four to account for the signature.
0117   uint32_t getNextSymbolOffset() const { return SymbolByteSize + 4; }
0118 
0119   void finalize();
0120   Error finalizeMsfLayout();
0121 
0122   /// Commit the DBI descriptor to the DBI stream.
0123   Error commit(BinaryStreamWriter &ModiWriter);
0124 
0125   /// Commit the accumulated symbols to the module symbol stream. Safe to call
0126   /// in parallel on different DbiModuleDescriptorBuilder objects. Only modifies
0127   /// the pre-allocated stream in question.
0128   Error commitSymbolStream(const msf::MSFLayout &MsfLayout,
0129                            WritableBinaryStreamRef MsfBuffer);
0130 
0131 private:
0132   uint32_t calculateC13DebugInfoSize() const;
0133 
0134   void addSourceFile(StringRef Path);
0135   msf::MSFBuilder &MSF;
0136 
0137   uint32_t SymbolByteSize = 0;
0138   uint32_t PdbFilePathNI = 0;
0139   std::string ModuleName;
0140   std::string ObjFileName;
0141   std::vector<std::string> SourceFiles;
0142   std::vector<SymbolListWrapper> Symbols;
0143 
0144   void *MergeSymsCtx = nullptr;
0145   MergeSymbolsCallback MergeSymsCallback = nullptr;
0146 
0147   std::vector<StringTableFixup> StringTableFixups;
0148 
0149   std::vector<codeview::DebugSubsectionRecordBuilder> C13Builders;
0150 
0151   ModuleInfoHeader Layout;
0152 };
0153 
0154 } // end namespace pdb
0155 
0156 } // end namespace llvm
0157 
0158 #endif // LLVM_DEBUGINFO_PDB_NATIVE_DBIMODULEDESCRIPTORBUILDER_H