| 1 | //===- DebugTypes.h ---------------------------------------------*- 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 | #ifndef LLD_COFF_DEBUGTYPES_H |
| 10 | #define LLD_COFF_DEBUGTYPES_H |
| 11 | |
| 12 | #include "lld/Common/LLVM.h" |
| 13 | #include "llvm/ADT/BitVector.h" |
| 14 | #include "llvm/ADT/DenseMap.h" |
| 15 | #include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h" |
| 16 | #include "llvm/DebugInfo/CodeView/TypeRecord.h" |
| 17 | #include "llvm/Support/Error.h" |
| 18 | #include "llvm/Support/MemoryBuffer.h" |
| 19 | |
| 20 | namespace llvm::codeview { |
| 21 | struct GloballyHashedType; |
| 22 | } |
| 23 | namespace llvm::pdb { |
| 24 | class NativeSession; |
| 25 | class TpiStream; |
| 26 | } |
| 27 | |
| 28 | namespace lld::coff { |
| 29 | |
| 30 | using llvm::codeview::GloballyHashedType; |
| 31 | using llvm::codeview::TypeIndex; |
| 32 | |
| 33 | class ObjFile; |
| 34 | class PDBInputFile; |
| 35 | class TypeMerger; |
| 36 | struct GHashState; |
| 37 | class COFFLinkerContext; |
| 38 | |
| 39 | class TpiSource { |
| 40 | public: |
| 41 | enum TpiKind : uint8_t { Regular, PCH, UsingPCH, PDB, PDBIpi, UsingPDB }; |
| 42 | |
| 43 | TpiSource(COFFLinkerContext &ctx, TpiKind k, ObjFile *f); |
| 44 | virtual ~TpiSource(); |
| 45 | |
| 46 | /// Produce a mapping from the type and item indices used in the object |
| 47 | /// file to those in the destination PDB. |
| 48 | /// |
| 49 | /// If the object file uses a type server PDB (compiled with /Zi), merge TPI |
| 50 | /// and IPI from the type server PDB and return a map for it. Each unique type |
| 51 | /// server PDB is merged at most once, so this may return an existing index |
| 52 | /// mapping. |
| 53 | /// |
| 54 | /// If the object does not use a type server PDB (compiled with /Z7), we merge |
| 55 | /// all the type and item records from the .debug$S stream and fill in the |
| 56 | /// caller-provided ObjectIndexMap. |
| 57 | virtual Error mergeDebugT(TypeMerger *m); |
| 58 | |
| 59 | /// Load global hashes, either by hashing types directly, or by loading them |
| 60 | /// from LLVM's .debug$H section. |
| 61 | virtual void loadGHashes(); |
| 62 | |
| 63 | /// Use global hashes to merge type information. |
| 64 | virtual void remapTpiWithGHashes(GHashState *g); |
| 65 | |
| 66 | // Remap a type index in place. |
| 67 | bool remapTypeIndex(TypeIndex &ti, llvm::codeview::TiRefKind refKind) const; |
| 68 | |
| 69 | protected: |
| 70 | void remapRecord(MutableArrayRef<uint8_t> rec, |
| 71 | ArrayRef<llvm::codeview::TiReference> typeRefs); |
| 72 | |
| 73 | void mergeTypeRecord(TypeIndex curIndex, llvm::codeview::CVType ty); |
| 74 | |
| 75 | // Merge the type records listed in uniqueTypes. beginIndex is the TypeIndex |
| 76 | // of the first record in this source, typically 0x1000. When PCHs are |
| 77 | // involved, it may start higher. |
| 78 | void mergeUniqueTypeRecords( |
| 79 | ArrayRef<uint8_t> debugTypes, |
| 80 | TypeIndex beginIndex = TypeIndex(TypeIndex::FirstNonSimpleIndex)); |
| 81 | |
| 82 | // Use the ghash table to construct a map from source type index to |
| 83 | // destination PDB type index. Usable for either TPI or IPI. |
| 84 | void fillMapFromGHashes(GHashState *m); |
| 85 | |
| 86 | // Copies ghashes from a vector into an array. These are long lived, so it's |
| 87 | // worth the time to copy these into an appropriately sized vector to reduce |
| 88 | // memory usage. |
| 89 | void assignGHashesFromVector(std::vector<GloballyHashedType> &&hashVec); |
| 90 | |
| 91 | // Walk over file->debugTypes and fill in the isItemIndex bit vector. |
| 92 | void fillIsItemIndexFromDebugT(); |
| 93 | |
| 94 | COFFLinkerContext &ctx; |
| 95 | |
| 96 | public: |
| 97 | bool remapTypesInSymbolRecord(MutableArrayRef<uint8_t> rec); |
| 98 | |
| 99 | void remapTypesInTypeRecord(MutableArrayRef<uint8_t> rec); |
| 100 | |
| 101 | /// Is this a dependent file that needs to be processed first, before other |
| 102 | /// OBJs? |
| 103 | virtual bool isDependency() const { return false; } |
| 104 | |
| 105 | /// Returns true if this type record should be omitted from the PDB, even if |
| 106 | /// it is unique. This prevents a record from being added to the input ghash |
| 107 | /// table. |
| 108 | bool shouldOmitFromPdb(uint32_t ghashIdx) { |
| 109 | return ghashIdx == endPrecompIdx; |
| 110 | } |
| 111 | |
| 112 | const TpiKind kind; |
| 113 | bool ownedGHashes = true; |
| 114 | uint32_t tpiSrcIdx = 0; |
| 115 | |
| 116 | /// The index (zero based, not 0x1000-based) of the LF_ENDPRECOMP record in |
| 117 | /// this object, if one exists. This is the all ones value otherwise. It is |
| 118 | /// recorded here for validation, and so that it can be omitted from the final |
| 119 | /// ghash table. |
| 120 | uint32_t endPrecompIdx = ~0U; |
| 121 | |
| 122 | public: |
| 123 | ObjFile *file; |
| 124 | |
| 125 | /// An error encountered during type merging, if any. |
| 126 | Error typeMergingError = Error::success(); |
| 127 | |
| 128 | // Storage for tpiMap or ipiMap, depending on the kind of source. |
| 129 | llvm::SmallVector<TypeIndex, 0> indexMapStorage; |
| 130 | |
| 131 | // Source type index to PDB type index mapping for type and item records. |
| 132 | // These mappings will be the same for /Z7 objects, and distinct for /Zi |
| 133 | // objects. |
| 134 | llvm::ArrayRef<TypeIndex> tpiMap; |
| 135 | llvm::ArrayRef<TypeIndex> ipiMap; |
| 136 | |
| 137 | /// Array of global type hashes, indexed by TypeIndex. May be calculated on |
| 138 | /// demand, or present in input object files. |
| 139 | llvm::ArrayRef<llvm::codeview::GloballyHashedType> ghashes; |
| 140 | |
| 141 | /// When ghashing is used, record the mapping from LF_[M]FUNC_ID to function |
| 142 | /// type index here. Both indices are PDB indices, not object type indexes. |
| 143 | std::vector<std::pair<TypeIndex, TypeIndex>> funcIdToType; |
| 144 | |
| 145 | /// Indicates if a type record is an item index or a type index. |
| 146 | llvm::BitVector isItemIndex; |
| 147 | |
| 148 | /// A list of all "unique" type indices which must be merged into the final |
| 149 | /// PDB. GHash type deduplication produces this list, and it should be |
| 150 | /// considerably smaller than the input. |
| 151 | std::vector<uint32_t> uniqueTypes; |
| 152 | |
| 153 | struct MergedInfo { |
| 154 | std::vector<uint8_t> recs; |
| 155 | std::vector<uint16_t> recSizes; |
| 156 | std::vector<uint32_t> recHashes; |
| 157 | }; |
| 158 | |
| 159 | MergedInfo mergedTpi; |
| 160 | MergedInfo mergedIpi; |
| 161 | |
| 162 | uint64_t nbTypeRecords = 0; |
| 163 | uint64_t nbTypeRecordsBytes = 0; |
| 164 | }; |
| 165 | |
| 166 | TpiSource *makeTpiSource(COFFLinkerContext &ctx, ObjFile *f); |
| 167 | TpiSource *makeTypeServerSource(COFFLinkerContext &ctx, |
| 168 | PDBInputFile *pdbInputFile); |
| 169 | TpiSource *makeUseTypeServerSource(COFFLinkerContext &ctx, ObjFile *file, |
| 170 | llvm::codeview::TypeServer2Record ts); |
| 171 | TpiSource *makePrecompSource(COFFLinkerContext &ctx, ObjFile *file); |
| 172 | TpiSource *makeUsePrecompSource(COFFLinkerContext &ctx, ObjFile *file, |
| 173 | llvm::codeview::PrecompRecord ts); |
| 174 | |
| 175 | } // namespace lld::coff |
| 176 | |
| 177 | #endif |
| 178 | |