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