123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- //===-- TypeStreamMerger.cpp ------------------------------------*- C++ -*-===//
- //
- // The LLVM Compiler Infrastructure
- //
- // This file is distributed under the University of Illinois Open Source
- // License. See LICENSE.TXT for details.
- //
- //===----------------------------------------------------------------------===//
- #include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
- #include "llvm/ADT/SmallString.h"
- #include "llvm/ADT/StringExtras.h"
- #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
- #include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
- #include "llvm/DebugInfo/CodeView/TypeIndex.h"
- #include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h"
- #include "llvm/DebugInfo/CodeView/TypeRecord.h"
- #include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
- #include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h"
- #include "llvm/Support/Error.h"
- #include "llvm/Support/ScopedPrinter.h"
- using namespace llvm;
- using namespace llvm::codeview;
- namespace {
- /// Implementation of CodeView type stream merging.
- ///
- /// A CodeView type stream is a series of records that reference each other
- /// through type indices. A type index is either "simple", meaning it is less
- /// than 0x1000 and refers to a builtin type, or it is complex, meaning it
- /// refers to a prior type record in the current stream. The type index of a
- /// record is equal to the number of records before it in the stream plus
- /// 0x1000.
- ///
- /// Type records are only allowed to use type indices smaller than their own, so
- /// a type stream is effectively a topologically sorted DAG. Cycles occuring in
- /// the type graph of the source program are resolved with forward declarations
- /// of composite types. This class implements the following type stream merging
- /// algorithm, which relies on this DAG structure:
- ///
- /// - Begin with a new empty stream, and a new empty hash table that maps from
- /// type record contents to new type index.
- /// - For each new type stream, maintain a map from source type index to
- /// destination type index.
- /// - For each record, copy it and rewrite its type indices to be valid in the
- /// destination type stream.
- /// - If the new type record is not already present in the destination stream
- /// hash table, append it to the destination type stream, assign it the next
- /// type index, and update the two hash tables.
- /// - If the type record already exists in the destination stream, discard it
- /// and update the type index map to forward the source type index to the
- /// existing destination type index.
- ///
- /// As an additional complication, type stream merging actually produces two
- /// streams: an item (or IPI) stream and a type stream, as this is what is
- /// actually stored in the final PDB. We choose which records go where by
- /// looking at the record kind.
- class TypeStreamMerger : public TypeVisitorCallbacks {
- public:
- explicit TypeStreamMerger(SmallVectorImpl<TypeIndex> &SourceToDest,
- TypeServerHandler *Handler)
- : Handler(Handler), IndexMap(SourceToDest) {
- SourceToDest.clear();
- }
- static const TypeIndex Untranslated;
- Error visitTypeBegin(CVType &Record) override;
- Error visitTypeEnd(CVType &Record) override;
- Error mergeTypesAndIds(TypeTableBuilder &DestIds, TypeTableBuilder &DestTypes,
- const CVTypeArray &IdsAndTypes);
- Error mergeIdRecords(TypeTableBuilder &Dest,
- ArrayRef<TypeIndex> TypeSourceToDest,
- const CVTypeArray &Ids);
- Error mergeTypeRecords(TypeTableBuilder &Dest, const CVTypeArray &Types);
- private:
- Error doit(const CVTypeArray &Types);
- void addMapping(TypeIndex Idx);
- bool remapTypeIndex(TypeIndex &Idx);
- bool remapItemIndex(TypeIndex &Idx);
- bool remapIndices(RemappedType &Record, ArrayRef<TiReference> Refs) {
- auto OriginalData = Record.OriginalRecord.content();
- bool Success = true;
- for (auto &Ref : Refs) {
- uint32_t Offset = Ref.Offset;
- ArrayRef<uint8_t> Bytes =
- OriginalData.slice(Ref.Offset, sizeof(TypeIndex));
- ArrayRef<TypeIndex> TIs(reinterpret_cast<const TypeIndex *>(Bytes.data()),
- Ref.Count);
- for (auto TI : TIs) {
- TypeIndex NewTI = TI;
- bool ThisSuccess = (Ref.Kind == TiRefKind::IndexRef)
- ? remapItemIndex(NewTI)
- : remapTypeIndex(NewTI);
- if (ThisSuccess && NewTI != TI)
- Record.Mappings.emplace_back(Offset, NewTI);
- Offset += sizeof(TypeIndex);
- Success &= ThisSuccess;
- }
- }
- return Success;
- }
- bool remapIndex(TypeIndex &Idx, ArrayRef<TypeIndex> Map);
- size_t slotForIndex(TypeIndex Idx) const {
- assert(!Idx.isSimple() && "simple type indices have no slots");
- return Idx.getIndex() - TypeIndex::FirstNonSimpleIndex;
- }
- Error errorCorruptRecord() const {
- return llvm::make_error<CodeViewError>(cv_error_code::corrupt_record);
- }
- Error writeRecord(TypeTableBuilder &Dest, const RemappedType &Record,
- bool RemapSuccess) {
- TypeIndex DestIdx = Untranslated;
- if (RemapSuccess)
- DestIdx = Dest.writeSerializedRecord(Record);
- addMapping(DestIdx);
- return Error::success();
- }
- Error writeTypeRecord(const CVType &Record) {
- TypeIndex DestIdx =
- DestTypeStream->writeSerializedRecord(Record.RecordData);
- addMapping(DestIdx);
- return Error::success();
- }
- Error writeTypeRecord(const RemappedType &Record, bool RemapSuccess) {
- return writeRecord(*DestTypeStream, Record, RemapSuccess);
- }
- Error writeIdRecord(const RemappedType &Record, bool RemapSuccess) {
- return writeRecord(*DestIdStream, Record, RemapSuccess);
- }
- Optional<Error> LastError;
- bool IsSecondPass = false;
- bool HadUntranslatedMember = false;
- unsigned NumBadIndices = 0;
- TypeIndex CurIndex{TypeIndex::FirstNonSimpleIndex};
- TypeTableBuilder *DestIdStream = nullptr;
- TypeTableBuilder *DestTypeStream = nullptr;
- TypeServerHandler *Handler = nullptr;
- // If we're only mapping id records, this array contains the mapping for
- // type records.
- ArrayRef<TypeIndex> TypeLookup;
- /// Map from source type index to destination type index. Indexed by source
- /// type index minus 0x1000.
- SmallVectorImpl<TypeIndex> &IndexMap;
- };
- } // end anonymous namespace
- const TypeIndex TypeStreamMerger::Untranslated(SimpleTypeKind::NotTranslated);
- Error TypeStreamMerger::visitTypeBegin(CVType &Rec) {
- RemappedType R(Rec);
- SmallVector<TiReference, 32> Refs;
- discoverTypeIndices(Rec.RecordData, Refs);
- bool Success = remapIndices(R, Refs);
- switch (Rec.kind()) {
- case TypeLeafKind::LF_FUNC_ID:
- case TypeLeafKind::LF_MFUNC_ID:
- case TypeLeafKind::LF_STRING_ID:
- case TypeLeafKind::LF_SUBSTR_LIST:
- case TypeLeafKind::LF_BUILDINFO:
- case TypeLeafKind::LF_UDT_SRC_LINE:
- case TypeLeafKind::LF_UDT_MOD_SRC_LINE:
- return writeIdRecord(R, Success);
- default:
- return writeTypeRecord(R, Success);
- }
- return Error::success();
- }
- Error TypeStreamMerger::visitTypeEnd(CVType &Rec) {
- ++CurIndex;
- if (!IsSecondPass)
- assert(IndexMap.size() == slotForIndex(CurIndex) &&
- "visitKnownRecord should add one index map entry");
- return Error::success();
- }
- void TypeStreamMerger::addMapping(TypeIndex Idx) {
- if (!IsSecondPass) {
- assert(IndexMap.size() == slotForIndex(CurIndex) &&
- "visitKnownRecord should add one index map entry");
- IndexMap.push_back(Idx);
- } else {
- assert(slotForIndex(CurIndex) < IndexMap.size());
- IndexMap[slotForIndex(CurIndex)] = Idx;
- }
- }
- bool TypeStreamMerger::remapIndex(TypeIndex &Idx, ArrayRef<TypeIndex> Map) {
- // Simple types are unchanged.
- if (Idx.isSimple())
- return true;
- // Check if this type index refers to a record we've already translated
- // successfully. If it refers to a type later in the stream or a record we
- // had to defer, defer it until later pass.
- unsigned MapPos = slotForIndex(Idx);
- if (MapPos < Map.size() && Map[MapPos] != Untranslated) {
- Idx = Map[MapPos];
- return true;
- }
- // If this is the second pass and this index isn't in the map, then it points
- // outside the current type stream, and this is a corrupt record.
- if (IsSecondPass && MapPos >= Map.size()) {
- // FIXME: Print a more useful error. We can give the current record and the
- // index that we think its pointing to.
- LastError = joinErrors(std::move(*LastError), errorCorruptRecord());
- }
- ++NumBadIndices;
- // This type index is invalid. Remap this to "not translated by cvpack",
- // and return failure.
- Idx = Untranslated;
- return false;
- }
- bool TypeStreamMerger::remapTypeIndex(TypeIndex &Idx) {
- // If we're mapping a pure index stream, then IndexMap only contains mappings
- // from OldIdStream -> NewIdStream, in which case we will need to use the
- // special mapping from OldTypeStream -> NewTypeStream which was computed
- // externally. Regardless, we use this special map if and only if we are
- // doing an id-only mapping.
- if (DestTypeStream == nullptr)
- return remapIndex(Idx, TypeLookup);
- assert(TypeLookup.empty());
- return remapIndex(Idx, IndexMap);
- }
- bool TypeStreamMerger::remapItemIndex(TypeIndex &Idx) {
- assert(DestIdStream);
- return remapIndex(Idx, IndexMap);
- }
- Error TypeStreamMerger::mergeTypeRecords(TypeTableBuilder &Dest,
- const CVTypeArray &Types) {
- DestTypeStream = &Dest;
- return doit(Types);
- }
- Error TypeStreamMerger::mergeIdRecords(TypeTableBuilder &Dest,
- ArrayRef<TypeIndex> TypeSourceToDest,
- const CVTypeArray &Ids) {
- DestIdStream = &Dest;
- TypeLookup = TypeSourceToDest;
- return doit(Ids);
- }
- Error TypeStreamMerger::mergeTypesAndIds(TypeTableBuilder &DestIds,
- TypeTableBuilder &DestTypes,
- const CVTypeArray &IdsAndTypes) {
- DestIdStream = &DestIds;
- DestTypeStream = &DestTypes;
- return doit(IdsAndTypes);
- }
- Error TypeStreamMerger::doit(const CVTypeArray &Types) {
- LastError = Error::success();
- // We don't want to deserialize records. I guess this flag is poorly named,
- // but it really means "Don't deserialize records before switching on the
- // concrete type.
- // FIXME: We can probably get even more speed here if we don't use the visitor
- // pipeline here, but instead write the switch ourselves. I don't think it
- // would buy us much since it's already pretty fast, but it's probably worth
- // a few cycles.
- if (auto EC =
- codeview::visitTypeStream(Types, *this, VDS_BytesExternal, Handler))
- return EC;
- // If we found bad indices but no other errors, try doing another pass and see
- // if we can resolve the indices that weren't in the map on the first pass.
- // This may require multiple passes, but we should always make progress. MASM
- // is the only known CodeView producer that makes type streams that aren't
- // topologically sorted. The standard library contains MASM-produced objects,
- // so this is important to handle correctly, but we don't have to be too
- // efficient. MASM type streams are usually very small.
- while (!*LastError && NumBadIndices > 0) {
- unsigned BadIndicesRemaining = NumBadIndices;
- IsSecondPass = true;
- NumBadIndices = 0;
- CurIndex = TypeIndex(TypeIndex::FirstNonSimpleIndex);
- if (auto EC =
- codeview::visitTypeStream(Types, *this, VDS_BytesExternal, Handler))
- return EC;
- assert(NumBadIndices <= BadIndicesRemaining &&
- "second pass found more bad indices");
- if (!*LastError && NumBadIndices == BadIndicesRemaining) {
- return llvm::make_error<CodeViewError>(
- cv_error_code::corrupt_record, "input type graph contains cycles");
- }
- }
- Error Ret = std::move(*LastError);
- LastError.reset();
- return Ret;
- }
- Error llvm::codeview::mergeTypeRecords(TypeTableBuilder &Dest,
- SmallVectorImpl<TypeIndex> &SourceToDest,
- TypeServerHandler *Handler,
- const CVTypeArray &Types) {
- TypeStreamMerger M(SourceToDest, Handler);
- return M.mergeTypeRecords(Dest, Types);
- }
- Error llvm::codeview::mergeIdRecords(TypeTableBuilder &Dest,
- ArrayRef<TypeIndex> TypeSourceToDest,
- SmallVectorImpl<TypeIndex> &SourceToDest,
- const CVTypeArray &Ids) {
- TypeStreamMerger M(SourceToDest, nullptr);
- return M.mergeIdRecords(Dest, TypeSourceToDest, Ids);
- }
- Error llvm::codeview::mergeTypeAndIdRecords(
- TypeTableBuilder &DestIds, TypeTableBuilder &DestTypes,
- SmallVectorImpl<TypeIndex> &SourceToDest, TypeServerHandler *Handler,
- const CVTypeArray &IdsAndTypes) {
- TypeStreamMerger M(SourceToDest, Handler);
- return M.mergeTypesAndIds(DestIds, DestTypes, IdsAndTypes);
- }
|