1 //===- DebugTypes.h ---------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLD_COFF_DEBUGTYPES_H
10 #define LLD_COFF_DEBUGTYPES_H
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"
20 namespace llvm::codeview
{
21 struct GloballyHashedType
;
30 using llvm::codeview::GloballyHashedType
;
31 using llvm::codeview::TypeIndex
;
37 class COFFLinkerContext
;
41 enum TpiKind
: uint8_t { Regular
, PCH
, UsingPCH
, PDB
, PDBIpi
, UsingPDB
};
43 TpiSource(COFFLinkerContext
&ctx
, TpiKind k
, ObjFile
*f
);
46 /// Produce a mapping from the type and item indices used in the object
47 /// file to those in the destination PDB.
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
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
);
59 /// Load global hashes, either by hashing types directly, or by loading them
60 /// from LLVM's .debug$H section.
61 virtual void loadGHashes();
63 /// Use global hashes to merge type information.
64 virtual void remapTpiWithGHashes(GHashState
*g
);
66 // Remap a type index in place.
67 bool remapTypeIndex(TypeIndex
&ti
, llvm::codeview::TiRefKind refKind
) const;
70 void remapRecord(MutableArrayRef
<uint8_t> rec
,
71 ArrayRef
<llvm::codeview::TiReference
> typeRefs
);
73 void mergeTypeRecord(TypeIndex curIndex
, llvm::codeview::CVType ty
);
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
));
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
);
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
89 void assignGHashesFromVector(std::vector
<GloballyHashedType
> &&hashVec
);
91 // Walk over file->debugTypes and fill in the isItemIndex bit vector.
92 void fillIsItemIndexFromDebugT();
94 COFFLinkerContext
&ctx
;
97 bool remapTypesInSymbolRecord(MutableArrayRef
<uint8_t> rec
);
99 void remapTypesInTypeRecord(MutableArrayRef
<uint8_t> rec
);
101 /// Is this a dependent file that needs to be processed first, before other
103 virtual bool isDependency() const { return false; }
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
108 bool shouldOmitFromPdb(uint32_t ghashIdx
) {
109 return ghashIdx
== endPrecompIdx
;
113 bool ownedGHashes
= true;
114 uint32_t tpiSrcIdx
= 0;
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
120 uint32_t endPrecompIdx
= ~0U;
125 /// An error encountered during type merging, if any.
126 Error typeMergingError
= Error::success();
128 // Storage for tpiMap or ipiMap, depending on the kind of source.
129 llvm::SmallVector
<TypeIndex
, 0> indexMapStorage
;
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
134 llvm::ArrayRef
<TypeIndex
> tpiMap
;
135 llvm::ArrayRef
<TypeIndex
> ipiMap
;
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
;
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
;
145 /// Indicates if a type record is an item index or a type index.
146 llvm::BitVector isItemIndex
;
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
;
154 std::vector
<uint8_t> recs
;
155 std::vector
<uint16_t> recSizes
;
156 std::vector
<uint32_t> recHashes
;
159 MergedInfo mergedTpi
;
160 MergedInfo mergedIpi
;
162 uint64_t nbTypeRecords
= 0;
163 uint64_t nbTypeRecordsBytes
= 0;
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
);
175 } // namespace lld::coff