178 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLD_COFF_DEBUGTYPES_H10#define LLD_COFF_DEBUGTYPES_H11 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 20namespace llvm::codeview {21struct GloballyHashedType;22}23namespace llvm::pdb {24class NativeSession;25class TpiStream;26}27 28namespace lld::coff {29 30using llvm::codeview::GloballyHashedType;31using llvm::codeview::TypeIndex;32 33class ObjFile;34class PDBInputFile;35class TypeMerger;36struct GHashState;37class COFFLinkerContext;38 39class TpiSource {40public: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 object47 /// file to those in the destination PDB.48 ///49 /// If the object file uses a type server PDB (compiled with /Zi), merge TPI50 /// and IPI from the type server PDB and return a map for it. Each unique type51 /// server PDB is merged at most once, so this may return an existing index52 /// mapping.53 ///54 /// If the object does not use a type server PDB (compiled with /Z7), we merge55 /// all the type and item records from the .debug$S stream and fill in the56 /// caller-provided ObjectIndexMap.57 virtual Error mergeDebugT(TypeMerger *m);58 59 /// Load global hashes, either by hashing types directly, or by loading them60 /// 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 69protected: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 TypeIndex76 // of the first record in this source, typically 0x1000. When PCHs are77 // 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 to83 // 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's87 // worth the time to copy these into an appropriately sized vector to reduce88 // 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 96public: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 other102 /// OBJs?103 virtual bool isDependency() const { return false; }104 105 /// Returns true if this type record should be omitted from the PDB, even if106 /// it is unique. This prevents a record from being added to the input ghash107 /// 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 in117 /// this object, if one exists. This is the all ones value otherwise. It is118 /// recorded here for validation, and so that it can be omitted from the final119 /// ghash table.120 uint32_t endPrecompIdx = ~0U;121 122public: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 /Zi133 // objects.134 llvm::ArrayRef<TypeIndex> tpiMap;135 llvm::ArrayRef<TypeIndex> ipiMap;136 137 /// Array of global type hashes, indexed by TypeIndex. May be calculated on138 /// 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 function142 /// 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 final149 /// PDB. GHash type deduplication produces this list, and it should be150 /// 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 166TpiSource *makeTpiSource(COFFLinkerContext &ctx, ObjFile *f);167TpiSource *makeTypeServerSource(COFFLinkerContext &ctx,168 PDBInputFile *pdbInputFile);169TpiSource *makeUseTypeServerSource(COFFLinkerContext &ctx, ObjFile *file,170 llvm::codeview::TypeServer2Record ts);171TpiSource *makePrecompSource(COFFLinkerContext &ctx, ObjFile *file);172TpiSource *makeUsePrecompSource(COFFLinkerContext &ctx, ObjFile *file,173 llvm::codeview::PrecompRecord ts);174 175} // namespace lld::coff176 177#endif178