blob: eef5b1d79b2a2e24eb0c19d982af9e2109302cb0 [file] [log] [blame]
Rui Ueyama1f6b6e22016-05-13 21:21:53 +00001//===- PublicsStream.cpp - PDB Public Symbol Stream -----------------------===//
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// The data structures defined in this file are based on the reference
11// implementation which is available at
12// https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/dbi/gsi.h
13//
14// When you are reading the reference source code, you'd find the
15// information below useful.
16//
17// - ppdb1->m_fMinimalDbgInfo seems to be always true.
18// - SMALLBUCKETS macro is defined.
19//
20// The reference doesn't compile, so I learned just by reading code.
21// It's not guaranteed to be correct.
22//
23//===----------------------------------------------------------------------===//
24
25#include "llvm/DebugInfo/PDB/Raw/PublicsStream.h"
26
27#include "llvm/DebugInfo/CodeView/CodeView.h"
28#include "llvm/DebugInfo/CodeView/TypeRecord.h"
29#include "llvm/DebugInfo/PDB/Raw/MappedBlockStream.h"
30#include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
31#include "llvm/DebugInfo/PDB/Raw/RawError.h"
32#include "llvm/DebugInfo/PDB/Raw/StreamReader.h"
33
34#include "llvm/ADT/BitVector.h"
35#include "llvm/Support/Endian.h"
36#include "llvm/Support/Format.h"
37#include "llvm/Support/MathExtras.h"
38
39using namespace llvm;
40using namespace llvm::support;
41using namespace llvm::pdb;
42
43
44static const unsigned IPHR_HASH = 4096;
45
46// This is PSGSIHDR struct defined in
47// https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/dbi/gsi.h
48struct PublicsStream::HeaderInfo {
49 ulittle32_t SymHash;
50 ulittle32_t AddrMap;
51 ulittle32_t NumThunks;
52 ulittle32_t SizeOfThunk;
53 ulittle16_t ISectThunkTable;
54 char Padding[2];
55 ulittle32_t OffThunkTable;
Rui Ueyama8dc18c52016-05-17 23:07:48 +000056 ulittle32_t NumSections;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000057};
58
59
60// This is GSIHashHdr struct defined in
61struct PublicsStream::GSIHashHeader {
62 enum {
63 HdrSignature = -1,
64 HdrVersion = 0xeffe0000 + 19990810,
65 };
66 ulittle32_t VerSignature;
67 ulittle32_t VerHdr;
68 ulittle32_t HrSize;
69 ulittle32_t NumBuckets;
70};
71
72struct PublicsStream::HRFile {
73 ulittle32_t Off;
74 ulittle32_t CRef;
75};
76
Rui Ueyama8dc18c52016-05-17 23:07:48 +000077// This struct is defined as "SO" in langapi/include/pdb.h.
78namespace {
79struct SectionOffset {
80 ulittle32_t Off;
81 ulittle16_t Isect;
82 char Padding[2];
83};
84}
85
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000086PublicsStream::PublicsStream(PDBFile &File, uint32_t StreamNum)
87 : StreamNum(StreamNum), Stream(StreamNum, File) {}
88
89PublicsStream::~PublicsStream() {}
90
91uint32_t PublicsStream::getSymHash() const { return Header->SymHash; }
92uint32_t PublicsStream::getAddrMap() const { return Header->AddrMap; }
93
94// Publics stream contains fixed-size headers and a serialized hash table.
95// This implementation is not complete yet. It reads till the end of the
96// stream so that we verify the stream is at least not corrupted. However,
97// we skip over the hash table which we believe contains information about
98// public symbols.
99Error PublicsStream::reload() {
100 StreamReader Reader(Stream);
101
102 // Check stream size.
103 if (Reader.bytesRemaining() < sizeof(HeaderInfo) + sizeof(GSIHashHeader))
104 return make_error<RawError>(raw_error_code::corrupt_file,
105 "Publics Stream does not contain a header.");
106
107 // Read PSGSIHDR and GSIHashHdr structs.
108 Header.reset(new HeaderInfo());
109 if (Reader.readObject(Header.get()))
110 return make_error<RawError>(raw_error_code::corrupt_file,
111 "Publics Stream does not contain a header.");
112 HashHdr.reset(new GSIHashHeader());
113 if (Reader.readObject(HashHdr.get()))
114 return make_error<RawError>(raw_error_code::corrupt_file,
115 "Publics Stream does not contain a header.");
116
117 // An array of HRFile follows. Read them.
118 if (HashHdr->HrSize % sizeof(HRFile))
119 return make_error<RawError>(raw_error_code::corrupt_file,
120 "Invalid HR array size.");
121 std::vector<HRFile> HRs(HashHdr->HrSize / sizeof(HRFile));
122 if (auto EC = Reader.readArray<HRFile>(HRs))
123 return make_error<RawError>(raw_error_code::corrupt_file,
124 "Could not read an HR array");
125
126 // A bitmap of a fixed length follows.
127 size_t BitmapSizeInBits = alignTo(IPHR_HASH + 1, 32);
128 std::vector<uint8_t> Bitmap(BitmapSizeInBits / 8);
129 if (auto EC = Reader.readArray<uint8_t>(Bitmap))
130 return make_error<RawError>(raw_error_code::corrupt_file,
131 "Could not read a bitmap.");
132 for (uint8_t B : Bitmap)
133 NumBuckets += countPopulation(B);
134
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000135 // We don't yet understand the following data structures completely,
136 // but we at least know the types and sizes. Here we are trying
137 // to read the stream till end so that we at least can detect
138 // corrupted streams.
139
140 // Hash buckets follow.
Daniel Sanders016e6c42016-05-18 12:36:25 +0000141 std::vector<ulittle32_t> TempHashBuckets(NumBuckets);
Daniel Sandersc819d902016-05-18 09:59:14 +0000142 if (auto EC = Reader.readArray<ulittle32_t>(TempHashBuckets))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000143 return make_error<RawError>(raw_error_code::corrupt_file,
144 "Hash buckets corrupted.");
Daniel Sandersc819d902016-05-18 09:59:14 +0000145 HashBuckets.resize(NumBuckets);
146 std::copy(TempHashBuckets.begin(), TempHashBuckets.end(),
147 HashBuckets.begin());
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000148
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000149 // Something called "address map" follows.
Daniel Sanders016e6c42016-05-18 12:36:25 +0000150 std::vector<ulittle32_t> TempAddressMap(Header->AddrMap / sizeof(uint32_t));
151 if (auto EC = Reader.readArray<ulittle32_t>(TempAddressMap))
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000152 return make_error<RawError>(raw_error_code::corrupt_file,
153 "Could not read an address map.");
Daniel Sanders016e6c42016-05-18 12:36:25 +0000154 AddressMap.resize(Header->AddrMap / sizeof(uint32_t));
155 std::copy(TempAddressMap.begin(), TempAddressMap.end(), AddressMap.begin());
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000156
157 // Something called "thunk map" follows.
Daniel Sanders016e6c42016-05-18 12:36:25 +0000158 std::vector<ulittle32_t> TempThunkMap(Header->NumThunks);
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000159 ThunkMap.resize(Header->NumThunks);
Daniel Sanders016e6c42016-05-18 12:36:25 +0000160 if (auto EC = Reader.readArray<ulittle32_t>(TempThunkMap))
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000161 return make_error<RawError>(raw_error_code::corrupt_file,
162 "Could not read a thunk map.");
Daniel Sanders016e6c42016-05-18 12:36:25 +0000163 ThunkMap.resize(Header->NumThunks);
164 std::copy(TempThunkMap.begin(), TempThunkMap.end(), ThunkMap.begin());
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000165
166 // Something called "section map" follows.
Rui Ueyama350b2982016-05-18 16:24:16 +0000167 std::vector<SectionOffset> Offsets(Header->NumSections);
168 if (auto EC = Reader.readArray<SectionOffset>(Offsets))
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000169 return make_error<RawError>(raw_error_code::corrupt_file,
170 "Could not read a section map.");
Rui Ueyama350b2982016-05-18 16:24:16 +0000171 for (auto &SO : Offsets) {
172 SectionOffsets.push_back(SO.Off);
173 SectionOffsets.push_back(SO.Isect);
174 }
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000175
176 if (Reader.bytesRemaining() > 0)
177 return make_error<RawError>(raw_error_code::corrupt_file,
178 "Corrupted publics stream.");
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000179 return Error::success();
180}