aboutsummaryrefslogtreecommitdiff
path: root/thirdparty/raw_pdb/src/PDB_DBIStream.cpp
blob: 5c9bf1512f88cfe7b3a35787f46d99f1d232f9f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright 2011-2022, Molecular Matters GmbH <[email protected]>
// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)

#include "PDB_PCH.h"
#include "PDB_DBIStream.h"
#include "PDB_RawFile.h"


namespace
{
	// the DBI stream always resides at index 3
	static constexpr const uint32_t DBIStreamIndex = 3u;


	// ------------------------------------------------------------------------------------------------
	// ------------------------------------------------------------------------------------------------
	PDB_NO_DISCARD static inline uint32_t GetModuleInfoSubstreamOffset(const PDB::DBI::StreamHeader& /* dbiHeader */) PDB_NO_EXCEPT
	{
		return sizeof(PDB::DBI::StreamHeader);
	}


	// ------------------------------------------------------------------------------------------------
	// ------------------------------------------------------------------------------------------------
	PDB_NO_DISCARD static inline uint32_t GetSectionContributionSubstreamOffset(const PDB::DBI::StreamHeader& dbiHeader) PDB_NO_EXCEPT
	{
		return GetModuleInfoSubstreamOffset(dbiHeader) + dbiHeader.moduleInfoSize;
	}


	// ------------------------------------------------------------------------------------------------
	// ------------------------------------------------------------------------------------------------
	PDB_NO_DISCARD static inline uint32_t GetSectionMapSubstreamOffset(const PDB::DBI::StreamHeader& dbiHeader) PDB_NO_EXCEPT
	{
		return GetSectionContributionSubstreamOffset(dbiHeader) + dbiHeader.sectionContributionSize;
	}


	// ------------------------------------------------------------------------------------------------
	// ------------------------------------------------------------------------------------------------
	PDB_NO_DISCARD static inline uint32_t GetSourceInfoSubstreamOffset(const PDB::DBI::StreamHeader& dbiHeader) PDB_NO_EXCEPT
	{
		return GetSectionMapSubstreamOffset(dbiHeader) + dbiHeader.sectionMapSize;
	}


	// ------------------------------------------------------------------------------------------------
	// ------------------------------------------------------------------------------------------------
	PDB_NO_DISCARD static inline uint32_t GetTypeServerMapSubstreamOffset(const PDB::DBI::StreamHeader& dbiHeader) PDB_NO_EXCEPT
	{
		return GetSourceInfoSubstreamOffset(dbiHeader) + dbiHeader.sourceInfoSize;
	}


	// ------------------------------------------------------------------------------------------------
	// ------------------------------------------------------------------------------------------------
	PDB_NO_DISCARD static inline uint32_t GetECSubstreamOffset(const PDB::DBI::StreamHeader& dbiHeader) PDB_NO_EXCEPT
	{
		return GetTypeServerMapSubstreamOffset(dbiHeader) + dbiHeader.typeServerMapSize;
	}


	// ------------------------------------------------------------------------------------------------
	// ------------------------------------------------------------------------------------------------
	PDB_NO_DISCARD static inline uint32_t GetDebugHeaderSubstreamOffset(const PDB::DBI::StreamHeader& dbiHeader) PDB_NO_EXCEPT
	{
		return GetECSubstreamOffset(dbiHeader) + dbiHeader.ecSize;
	}


	// ------------------------------------------------------------------------------------------------
	// ------------------------------------------------------------------------------------------------
	PDB_NO_DISCARD static inline bool HasDebugHeaderSubstream(const PDB::DBI::StreamHeader& dbiHeader) PDB_NO_EXCEPT
	{
		return dbiHeader.optionalDebugHeaderSize != 0u;
	}
}


// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::DBIStream::DBIStream(void) PDB_NO_EXCEPT
	: m_header()
	, m_stream()
{
}


// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB::DBIStream::DBIStream(const RawFile& file, const DBI::StreamHeader& header) PDB_NO_EXCEPT
	: m_header(header)
	, m_stream(file.CreateMSFStream<DirectMSFStream>(DBIStreamIndex))
{
}


// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::ErrorCode PDB::HasValidDBIStream(const RawFile& file) PDB_NO_EXCEPT
{
	DirectMSFStream stream = file.CreateMSFStream<DirectMSFStream>(DBIStreamIndex);
	if (stream.GetSize() < sizeof(DBI::StreamHeader))
	{
		return ErrorCode::InvalidStream;
	}

	const DBI::StreamHeader header = stream.ReadAtOffset<DBI::StreamHeader>(0u);
	if (header.signature != DBI::StreamHeader::Signature)
	{
		return ErrorCode::InvalidSignature;
	}
	else if (header.version != DBI::StreamHeader::Version::V70)
	{
		return ErrorCode::UnknownVersion;
	}

	return ErrorCode::Success;
}


// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::DBIStream PDB::CreateDBIStream(const RawFile& file) PDB_NO_EXCEPT
{
	DirectMSFStream stream = file.CreateMSFStream<DirectMSFStream>(DBIStreamIndex);
	const DBI::StreamHeader header = stream.ReadAtOffset<DBI::StreamHeader>(0u);

	return DBIStream { file, header };
}


// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::ErrorCode PDB::DBIStream::HasValidSymbolRecordStream(const RawFile& /* file */) const PDB_NO_EXCEPT
{
	return (m_header.symbolRecordStreamIndex != PDB::NilStreamIndex) ? ErrorCode::Success : ErrorCode::InvalidStreamIndex;
}


// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::ErrorCode PDB::DBIStream::HasValidImageSectionStream(const RawFile& /* file */) const PDB_NO_EXCEPT
{
	// the debug header stream is optional. if it's not there, we can't get the image section stream either.
	if (!HasDebugHeaderSubstream(m_header))
	{
		return ErrorCode::InvalidStreamIndex;
	}

	// find the debug header sub-stream
	const uint32_t debugHeaderOffset = GetDebugHeaderSubstreamOffset(m_header);

	// validate that we have enough data to read the debug header
	// (the header field optionalDebugHeaderSize might claim there's a debug header,
	// but the stream might not have enough data - this happens with some .ni.pdb files)
	if (debugHeaderOffset + sizeof(DBI::DebugHeader) > m_stream.GetSize())
	{
		return ErrorCode::InvalidStream;
	}

	const DBI::DebugHeader& debugHeader = m_stream.ReadAtOffset<DBI::DebugHeader>(debugHeaderOffset);

	if (debugHeader.sectionHeaderStreamIndex == DBI::DebugHeader::InvalidStreamIndex)
	{
		return ErrorCode::InvalidStreamIndex;
	}

	return ErrorCode::Success;
}


// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::ErrorCode PDB::DBIStream::HasValidPublicSymbolStream(const RawFile& file) const PDB_NO_EXCEPT
{
	if (m_header.publicStreamIndex == PDB::NilStreamIndex)
	{
		return ErrorCode::InvalidStreamIndex;
	}

	DirectMSFStream publicStream = file.CreateMSFStream<DirectMSFStream>(m_header.publicStreamIndex);

	// the public symbol stream always begins with a header, we are not interested in that.
	// following the public symbol stream header is a hash table header.
	const HashTableHeader hashHeader = publicStream.ReadAtOffset<HashTableHeader>(sizeof(PublicStreamHeader));
	if (hashHeader.signature != HashTableHeader::Signature)
	{
		return ErrorCode::InvalidSignature;
	}
	else if (hashHeader.version != HashTableHeader::Version)
	{
		return ErrorCode::UnknownVersion;
	}

	return ErrorCode::Success;
}


// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::ErrorCode PDB::DBIStream::HasValidGlobalSymbolStream(const RawFile& file) const PDB_NO_EXCEPT
{
	if (m_header.globalStreamIndex == PDB::NilStreamIndex)
	{
		return ErrorCode::InvalidStreamIndex;
	}

	DirectMSFStream globalStream = file.CreateMSFStream<DirectMSFStream>(m_header.globalStreamIndex);

	// the global symbol stream starts with a hash table header
	const HashTableHeader hashHeader = globalStream.ReadAtOffset<HashTableHeader>(0u);
	if (hashHeader.signature != HashTableHeader::Signature)
	{
		return ErrorCode::InvalidSignature;
	}
	else if (hashHeader.version != HashTableHeader::Version)
	{
		return ErrorCode::UnknownVersion;
	}

	return ErrorCode::Success;
}


// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::ErrorCode PDB::DBIStream::HasValidSectionContributionStream(const RawFile& /* file */) const PDB_NO_EXCEPT
{
	if (m_header.sectionContributionSize < sizeof(DBI::SectionContribution::Version))
	{
		return ErrorCode::InvalidStream;
	}

	// find the section contribution sub-stream
	// https://llvm.org/docs/PDB/DbiStream.html#section-contribution-substream
	const uint32_t streamOffset = GetSectionContributionSubstreamOffset(m_header);

	const DBI::SectionContribution::Version version = m_stream.ReadAtOffset<DBI::SectionContribution::Version>(streamOffset);
	if (version != DBI::SectionContribution::Version::Ver60)
	{
		return ErrorCode::UnknownVersion;
	}

	return ErrorCode::Success;
}


// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::CoalescedMSFStream PDB::DBIStream::CreateSymbolRecordStream(const RawFile& file) const PDB_NO_EXCEPT
{
	// the symbol record stream holds the actual CodeView data of the symbols
	return file.CreateMSFStream<CoalescedMSFStream>(m_header.symbolRecordStreamIndex);
}


// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::ImageSectionStream PDB::DBIStream::CreateImageSectionStream(const RawFile& file) const PDB_NO_EXCEPT
{
	// find the debug header sub-stream
	const uint32_t debugHeaderOffset = GetDebugHeaderSubstreamOffset(m_header);
	const DBI::DebugHeader& debugHeader = m_stream.ReadAtOffset<DBI::DebugHeader>(debugHeaderOffset);

	// from there, grab the section header stream
	return ImageSectionStream(file, debugHeader.sectionHeaderStreamIndex);
}


// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::PublicSymbolStream PDB::DBIStream::CreatePublicSymbolStream(const RawFile& file) const PDB_NO_EXCEPT
{
	DirectMSFStream publicStream = file.CreateMSFStream<DirectMSFStream>(m_header.publicStreamIndex);

	// the public symbol stream always begins with a header, we are not interested in that.
	// following the public symbol stream header is a hash table header.
	// we use this to work out how many symbol records are referenced by the public symbol stream.
	const HashTableHeader hashHeader = publicStream.ReadAtOffset<HashTableHeader>(sizeof(PublicStreamHeader));
	const uint32_t recordCount = hashHeader.size / sizeof(HashRecord);

	return PublicSymbolStream(file, m_header.publicStreamIndex, recordCount);
}


// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::GlobalSymbolStream PDB::DBIStream::CreateGlobalSymbolStream(const RawFile& file) const PDB_NO_EXCEPT
{
	DirectMSFStream globalStream = file.CreateMSFStream<DirectMSFStream>(m_header.globalStreamIndex);

	// the global symbol stream starts with a hash table header.
	// we use this to work out how many symbol records are referenced by the global symbol stream.
	const HashTableHeader hashHeader = globalStream.ReadAtOffset<HashTableHeader>(0u);
	const uint32_t recordCount = hashHeader.size / sizeof(HashRecord);

	return GlobalSymbolStream(file, m_header.globalStreamIndex, recordCount);
}


// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::SourceFileStream PDB::DBIStream::CreateSourceFileStream(const RawFile& /* file */) const PDB_NO_EXCEPT
{
	// find the source info sub-stream
	// https://llvm.org/docs/PDB/DbiStream.html#file-info-substream
	const uint32_t streamOffset = GetSourceInfoSubstreamOffset(m_header);

	return SourceFileStream(m_stream, m_header.sourceInfoSize, streamOffset);
}


// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::SectionContributionStream PDB::DBIStream::CreateSectionContributionStream(const RawFile& /* file */) const PDB_NO_EXCEPT
{
	// find the section contribution sub-stream
	// https://llvm.org/docs/PDB/DbiStream.html#section-contribution-substream
	const uint32_t streamOffset = GetSectionContributionSubstreamOffset(m_header);

	return SectionContributionStream(m_stream, m_header.sectionContributionSize - sizeof(DBI::SectionContribution::Version), streamOffset + sizeof(DBI::SectionContribution::Version));
}


// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PDB_NO_DISCARD PDB::ModuleInfoStream PDB::DBIStream::CreateModuleInfoStream(const RawFile& /* file */) const PDB_NO_EXCEPT
{
	// find the module info sub-stream
	// https://llvm.org/docs/PDB/DbiStream.html#module-info-substream
	const uint32_t streamOffset = GetModuleInfoSubstreamOffset(m_header);

	return ModuleInfoStream(m_stream, m_header.moduleInfoSize, streamOffset);
}