1 //===-- TypeStreamMerger.cpp ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/StringExtras.h"
13 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
14 #include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
15 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
16 #include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h"
17 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
18 #include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
19 #include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h"
20 #include "llvm/Support/Error.h"
21 #include "llvm/Support/ScopedPrinter.h"
24 using namespace llvm::codeview;
28 /// Implementation of CodeView type stream merging.
30 /// A CodeView type stream is a series of records that reference each other
31 /// through type indices. A type index is either "simple", meaning it is less
32 /// than 0x1000 and refers to a builtin type, or it is complex, meaning it
33 /// refers to a prior type record in the current stream. The type index of a
34 /// record is equal to the number of records before it in the stream plus
37 /// Type records are only allowed to use type indices smaller than their own, so
38 /// a type stream is effectively a topologically sorted DAG. Cycles occuring in
39 /// the type graph of the source program are resolved with forward declarations
40 /// of composite types. This class implements the following type stream merging
41 /// algorithm, which relies on this DAG structure:
43 /// - Begin with a new empty stream, and a new empty hash table that maps from
44 /// type record contents to new type index.
45 /// - For each new type stream, maintain a map from source type index to
46 /// destination type index.
47 /// - For each record, copy it and rewrite its type indices to be valid in the
48 /// destination type stream.
49 /// - If the new type record is not already present in the destination stream
50 /// hash table, append it to the destination type stream, assign it the next
51 /// type index, and update the two hash tables.
52 /// - If the type record already exists in the destination stream, discard it
53 /// and update the type index map to forward the source type index to the
54 /// existing destination type index.
56 /// As an additional complication, type stream merging actually produces two
57 /// streams: an item (or IPI) stream and a type stream, as this is what is
58 /// actually stored in the final PDB. We choose which records go where by
59 /// looking at the record kind.
60 class TypeStreamMerger : public TypeVisitorCallbacks {
62 explicit TypeStreamMerger(SmallVectorImpl<TypeIndex> &SourceToDest,
63 TypeServerHandler *Handler)
64 : Handler(Handler), IndexMap(SourceToDest) {
68 static const TypeIndex Untranslated;
70 Error visitTypeBegin(CVType &Record) override;
71 Error visitTypeEnd(CVType &Record) override;
73 Error mergeTypesAndIds(TypeTableBuilder &DestIds, TypeTableBuilder &DestTypes,
74 const CVTypeArray &IdsAndTypes);
75 Error mergeIdRecords(TypeTableBuilder &Dest,
76 ArrayRef<TypeIndex> TypeSourceToDest,
77 const CVTypeArray &Ids);
78 Error mergeTypeRecords(TypeTableBuilder &Dest, const CVTypeArray &Types);
81 Error doit(const CVTypeArray &Types);
83 void addMapping(TypeIndex Idx);
85 bool remapTypeIndex(TypeIndex &Idx);
86 bool remapItemIndex(TypeIndex &Idx);
88 bool remapIndices(RemappedType &Record, ArrayRef<TiReference> Refs) {
89 auto OriginalData = Record.OriginalRecord.content();
91 for (auto &Ref : Refs) {
92 uint32_t Offset = Ref.Offset;
93 ArrayRef<uint8_t> Bytes =
94 OriginalData.slice(Ref.Offset, sizeof(TypeIndex));
95 ArrayRef<TypeIndex> TIs(reinterpret_cast<const TypeIndex *>(Bytes.data()),
99 bool ThisSuccess = (Ref.Kind == TiRefKind::IndexRef)
100 ? remapItemIndex(NewTI)
101 : remapTypeIndex(NewTI);
102 if (ThisSuccess && NewTI != TI)
103 Record.Mappings.emplace_back(Offset, NewTI);
104 Offset += sizeof(TypeIndex);
105 Success &= ThisSuccess;
111 bool remapIndex(TypeIndex &Idx, ArrayRef<TypeIndex> Map);
113 size_t slotForIndex(TypeIndex Idx) const {
114 assert(!Idx.isSimple() && "simple type indices have no slots");
115 return Idx.getIndex() - TypeIndex::FirstNonSimpleIndex;
118 Error errorCorruptRecord() const {
119 return llvm::make_error<CodeViewError>(cv_error_code::corrupt_record);
122 Error writeRecord(TypeTableBuilder &Dest, const RemappedType &Record,
124 TypeIndex DestIdx = Untranslated;
126 DestIdx = Dest.writeSerializedRecord(Record);
128 return Error::success();
131 Error writeTypeRecord(const CVType &Record) {
133 DestTypeStream->writeSerializedRecord(Record.RecordData);
135 return Error::success();
138 Error writeTypeRecord(const RemappedType &Record, bool RemapSuccess) {
139 return writeRecord(*DestTypeStream, Record, RemapSuccess);
142 Error writeIdRecord(const RemappedType &Record, bool RemapSuccess) {
143 return writeRecord(*DestIdStream, Record, RemapSuccess);
146 Optional<Error> LastError;
148 bool IsSecondPass = false;
150 unsigned NumBadIndices = 0;
152 TypeIndex CurIndex{TypeIndex::FirstNonSimpleIndex};
154 TypeTableBuilder *DestIdStream = nullptr;
155 TypeTableBuilder *DestTypeStream = nullptr;
156 TypeServerHandler *Handler = nullptr;
158 // If we're only mapping id records, this array contains the mapping for
160 ArrayRef<TypeIndex> TypeLookup;
162 /// Map from source type index to destination type index. Indexed by source
163 /// type index minus 0x1000.
164 SmallVectorImpl<TypeIndex> &IndexMap;
167 } // end anonymous namespace
169 const TypeIndex TypeStreamMerger::Untranslated(SimpleTypeKind::NotTranslated);
171 Error TypeStreamMerger::visitTypeBegin(CVType &Rec) {
173 SmallVector<TiReference, 32> Refs;
174 discoverTypeIndices(Rec.RecordData, Refs);
175 bool Success = remapIndices(R, Refs);
176 switch (Rec.kind()) {
177 case TypeLeafKind::LF_FUNC_ID:
178 case TypeLeafKind::LF_MFUNC_ID:
179 case TypeLeafKind::LF_STRING_ID:
180 case TypeLeafKind::LF_SUBSTR_LIST:
181 case TypeLeafKind::LF_BUILDINFO:
182 case TypeLeafKind::LF_UDT_SRC_LINE:
183 case TypeLeafKind::LF_UDT_MOD_SRC_LINE:
184 return writeIdRecord(R, Success);
186 return writeTypeRecord(R, Success);
188 return Error::success();
191 Error TypeStreamMerger::visitTypeEnd(CVType &Rec) {
194 assert(IndexMap.size() == slotForIndex(CurIndex) &&
195 "visitKnownRecord should add one index map entry");
196 return Error::success();
199 void TypeStreamMerger::addMapping(TypeIndex Idx) {
201 assert(IndexMap.size() == slotForIndex(CurIndex) &&
202 "visitKnownRecord should add one index map entry");
203 IndexMap.push_back(Idx);
205 assert(slotForIndex(CurIndex) < IndexMap.size());
206 IndexMap[slotForIndex(CurIndex)] = Idx;
210 bool TypeStreamMerger::remapIndex(TypeIndex &Idx, ArrayRef<TypeIndex> Map) {
211 // Simple types are unchanged.
215 // Check if this type index refers to a record we've already translated
216 // successfully. If it refers to a type later in the stream or a record we
217 // had to defer, defer it until later pass.
218 unsigned MapPos = slotForIndex(Idx);
219 if (MapPos < Map.size() && Map[MapPos] != Untranslated) {
224 // If this is the second pass and this index isn't in the map, then it points
225 // outside the current type stream, and this is a corrupt record.
226 if (IsSecondPass && MapPos >= Map.size()) {
227 // FIXME: Print a more useful error. We can give the current record and the
228 // index that we think its pointing to.
229 LastError = joinErrors(std::move(*LastError), errorCorruptRecord());
234 // This type index is invalid. Remap this to "not translated by cvpack",
235 // and return failure.
240 bool TypeStreamMerger::remapTypeIndex(TypeIndex &Idx) {
241 // If we're mapping a pure index stream, then IndexMap only contains mappings
242 // from OldIdStream -> NewIdStream, in which case we will need to use the
243 // special mapping from OldTypeStream -> NewTypeStream which was computed
244 // externally. Regardless, we use this special map if and only if we are
245 // doing an id-only mapping.
246 if (DestTypeStream == nullptr)
247 return remapIndex(Idx, TypeLookup);
249 assert(TypeLookup.empty());
250 return remapIndex(Idx, IndexMap);
253 bool TypeStreamMerger::remapItemIndex(TypeIndex &Idx) {
254 assert(DestIdStream);
255 return remapIndex(Idx, IndexMap);
258 Error TypeStreamMerger::mergeTypeRecords(TypeTableBuilder &Dest,
259 const CVTypeArray &Types) {
260 DestTypeStream = &Dest;
265 Error TypeStreamMerger::mergeIdRecords(TypeTableBuilder &Dest,
266 ArrayRef<TypeIndex> TypeSourceToDest,
267 const CVTypeArray &Ids) {
268 DestIdStream = &Dest;
269 TypeLookup = TypeSourceToDest;
274 Error TypeStreamMerger::mergeTypesAndIds(TypeTableBuilder &DestIds,
275 TypeTableBuilder &DestTypes,
276 const CVTypeArray &IdsAndTypes) {
277 DestIdStream = &DestIds;
278 DestTypeStream = &DestTypes;
280 return doit(IdsAndTypes);
283 Error TypeStreamMerger::doit(const CVTypeArray &Types) {
284 LastError = Error::success();
286 // We don't want to deserialize records. I guess this flag is poorly named,
287 // but it really means "Don't deserialize records before switching on the
289 // FIXME: We can probably get even more speed here if we don't use the visitor
290 // pipeline here, but instead write the switch ourselves. I don't think it
291 // would buy us much since it's already pretty fast, but it's probably worth
294 codeview::visitTypeStream(Types, *this, VDS_BytesExternal, Handler))
297 // If we found bad indices but no other errors, try doing another pass and see
298 // if we can resolve the indices that weren't in the map on the first pass.
299 // This may require multiple passes, but we should always make progress. MASM
300 // is the only known CodeView producer that makes type streams that aren't
301 // topologically sorted. The standard library contains MASM-produced objects,
302 // so this is important to handle correctly, but we don't have to be too
303 // efficient. MASM type streams are usually very small.
304 while (!*LastError && NumBadIndices > 0) {
305 unsigned BadIndicesRemaining = NumBadIndices;
308 CurIndex = TypeIndex(TypeIndex::FirstNonSimpleIndex);
311 codeview::visitTypeStream(Types, *this, VDS_BytesExternal, Handler))
314 assert(NumBadIndices <= BadIndicesRemaining &&
315 "second pass found more bad indices");
316 if (!*LastError && NumBadIndices == BadIndicesRemaining) {
317 return llvm::make_error<CodeViewError>(
318 cv_error_code::corrupt_record, "input type graph contains cycles");
322 Error Ret = std::move(*LastError);
327 Error llvm::codeview::mergeTypeRecords(TypeTableBuilder &Dest,
328 SmallVectorImpl<TypeIndex> &SourceToDest,
329 TypeServerHandler *Handler,
330 const CVTypeArray &Types) {
331 TypeStreamMerger M(SourceToDest, Handler);
332 return M.mergeTypeRecords(Dest, Types);
335 Error llvm::codeview::mergeIdRecords(TypeTableBuilder &Dest,
336 ArrayRef<TypeIndex> TypeSourceToDest,
337 SmallVectorImpl<TypeIndex> &SourceToDest,
338 const CVTypeArray &Ids) {
339 TypeStreamMerger M(SourceToDest, nullptr);
340 return M.mergeIdRecords(Dest, TypeSourceToDest, Ids);
343 Error llvm::codeview::mergeTypeAndIdRecords(
344 TypeTableBuilder &DestIds, TypeTableBuilder &DestTypes,
345 SmallVectorImpl<TypeIndex> &SourceToDest, TypeServerHandler *Handler,
346 const CVTypeArray &IdsAndTypes) {
348 TypeStreamMerger M(SourceToDest, Handler);
349 return M.mergeTypesAndIds(DestIds, DestTypes, IdsAndTypes);