LLVM  4.0.0
GSI.cpp
Go to the documentation of this file.
1 //===- GSI.cpp - Common Functions for GlobalsStream and PublicsStream ----===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "GSI.h"
11 
16 
17 #include "llvm/Support/Error.h"
18 
19 namespace llvm {
20 namespace pdb {
21 
22 static Error checkHashHdrVersion(const GSIHashHeader *HashHdr) {
23  if (HashHdr->VerHdr != GSIHashHeader::HdrVersion)
24  return make_error<RawError>(
26  "Encountered unsupported globals stream version.");
27 
28  return Error::success();
29 }
30 
33  const GSIHashHeader *HashHdr, msf::StreamReader &Reader) {
34  if (auto EC = checkHashHdrVersion(HashHdr))
35  return EC;
36 
37  // Before the actual hash buckets, there is a bitmap of length determined by
38  // IPHR_HASH.
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);
49 
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."));
55 
56  return Error::success();
57 }
58 
60  msf::StreamReader &Reader) {
61  if (Reader.readObject(HashHdr))
62  return make_error<RawError>(raw_error_code::corrupt_file,
63  "Stream does not contain a GSIHashHeader.");
64 
66  return make_error<RawError>(
68  "GSIHashHeader signature (0xffffffff) not found.");
69 
70  return Error::success();
71 }
72 
74  const GSIHashHeader *HashHdr,
75  msf::StreamReader &Reader) {
76  if (auto EC = checkHashHdrVersion(HashHdr))
77  return EC;
78 
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."));
89 
90  return Error::success();
91 }
92 }
93 }
support::ulittle32_t VerSignature
Definition: GSI.h:53
static const unsigned IPHR_HASH
From https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/dbi/gsi.cpp.
Definition: GSI.h:43
support::ulittle32_t VerHdr
Definition: GSI.h:54
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:664
static Error checkHashHdrVersion(const GSIHashHeader *HashHdr)
Definition: GSI.cpp:22
Error readGSIHashBuckets(msf::FixedStreamArray< support::ulittle32_t > &HashBuckets, const GSIHashHeader *HashHdr, msf::StreamReader &Reader)
Definition: GSI.cpp:31
Header of the hash tables found in the globals and publics sections.
Definition: GSI.h:48
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
Error readBytes(ArrayRef< uint8_t > &Buffer, uint32_t Size)
Error readArray(ArrayRef< T > &Array, uint32_t NumElements)
Definition: StreamReader.h:62
Error readGSIHashHeader(const GSIHashHeader *&HashHdr, msf::StreamReader &Reader)
Definition: GSI.cpp:59
static ErrorSuccess success()
Create a success value.
unsigned countPopulation(T Value)
Count the number of set bits in a value.
Definition: MathExtras.h:494
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Lightweight error class with error context and mandatory checking.
Error readGSIHashRecords(msf::FixedStreamArray< PSHashRecord > &HashRecords, const GSIHashHeader *HashHdr, msf::StreamReader &Reader)
Definition: GSI.cpp:73
support::ulittle32_t HrSize
Definition: GSI.h:55
Error readObject(const T *&Dest)
Definition: StreamReader.h:53