1 //===- GSI.cpp - Common Functions for GlobalsStream and PublicsStream ----===//
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 //===----------------------------------------------------------------------===//
12 #include "llvm/DebugInfo/PDB/Native/RawError.h"
13 #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
14 #include "llvm/Support/BinaryStreamArray.h"
15 #include "llvm/Support/BinaryStreamReader.h"
17 #include "llvm/Support/Error.h"
22 static Error checkHashHdrVersion(const GSIHashHeader *HashHdr) {
23 if (HashHdr->VerHdr != GSIHashHeader::HdrVersion)
24 return make_error<RawError>(
25 raw_error_code::feature_unsupported,
26 "Encountered unsupported globals stream version.");
28 return Error::success();
31 Error readGSIHashBuckets(FixedStreamArray<support::ulittle32_t> &HashBuckets,
32 const GSIHashHeader *HashHdr,
33 BinaryStreamReader &Reader) {
34 if (auto EC = checkHashHdrVersion(HashHdr))
37 // Before the actual hash buckets, there is a bitmap of length determined by
39 ArrayRef<uint8_t> Bitmap;
40 size_t BitmapSizeInBits = alignTo(IPHR_HASH + 1, 32);
41 uint32_t NumBitmapEntries = BitmapSizeInBits / 8;
42 if (auto EC = Reader.readBytes(Bitmap, NumBitmapEntries))
43 return joinErrors(std::move(EC),
44 make_error<RawError>(raw_error_code::corrupt_file,
45 "Could not read a bitmap."));
46 uint32_t NumBuckets = 0;
47 for (uint8_t B : Bitmap)
48 NumBuckets += countPopulation(B);
50 // Hash buckets follow.
51 if (auto EC = Reader.readArray(HashBuckets, NumBuckets))
52 return joinErrors(std::move(EC),
53 make_error<RawError>(raw_error_code::corrupt_file,
54 "Hash buckets corrupted."));
56 return Error::success();
59 Error readGSIHashHeader(const GSIHashHeader *&HashHdr,
60 BinaryStreamReader &Reader) {
61 if (Reader.readObject(HashHdr))
62 return make_error<RawError>(raw_error_code::corrupt_file,
63 "Stream does not contain a GSIHashHeader.");
65 if (HashHdr->VerSignature != GSIHashHeader::HdrSignature)
66 return make_error<RawError>(
67 raw_error_code::feature_unsupported,
68 "GSIHashHeader signature (0xffffffff) not found.");
70 return Error::success();
73 Error readGSIHashRecords(FixedStreamArray<PSHashRecord> &HashRecords,
74 const GSIHashHeader *HashHdr,
75 BinaryStreamReader &Reader) {
76 if (auto EC = checkHashHdrVersion(HashHdr))
79 // HashHdr->HrSize specifies the number of bytes of PSHashRecords we have.
80 // Verify that we can read them all.
81 if (HashHdr->HrSize % sizeof(PSHashRecord))
82 return make_error<RawError>(raw_error_code::corrupt_file,
83 "Invalid HR array size.");
84 uint32_t NumHashRecords = HashHdr->HrSize / sizeof(PSHashRecord);
85 if (auto EC = Reader.readArray(HashRecords, NumHashRecords))
86 return joinErrors(std::move(EC),
87 make_error<RawError>(raw_error_code::corrupt_file,
88 "Error reading hash records."));
90 return Error::success();