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