blob: af3d2d026b484056e888a4903209c34e521021d8 [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"
Zachary Turnerd5d37dc2016-05-25 20:37:03 +000028#include "llvm/DebugInfo/CodeView/StreamReader.h"
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000029#include "llvm/DebugInfo/CodeView/TypeRecord.h"
Zachary Turnerd8447992016-06-07 05:28:55 +000030#include "llvm/DebugInfo/PDB/Raw/IndexedStreamData.h"
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000031#include "llvm/DebugInfo/PDB/Raw/MappedBlockStream.h"
Rui Ueyama0fcd8262016-05-20 19:55:17 +000032#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000033#include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
34#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Rui Ueyama0fcd8262016-05-20 19:55:17 +000035#include "llvm/DebugInfo/PDB/Raw/SymbolStream.h"
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000036
37#include "llvm/ADT/BitVector.h"
38#include "llvm/Support/Endian.h"
39#include "llvm/Support/Format.h"
40#include "llvm/Support/MathExtras.h"
41
42using namespace llvm;
43using namespace llvm::support;
44using namespace llvm::pdb;
45
46
47static const unsigned IPHR_HASH = 4096;
48
49// This is PSGSIHDR struct defined in
50// https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/dbi/gsi.h
51struct PublicsStream::HeaderInfo {
52 ulittle32_t SymHash;
53 ulittle32_t AddrMap;
54 ulittle32_t NumThunks;
55 ulittle32_t SizeOfThunk;
56 ulittle16_t ISectThunkTable;
57 char Padding[2];
58 ulittle32_t OffThunkTable;
Rui Ueyama8dc18c52016-05-17 23:07:48 +000059 ulittle32_t NumSections;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000060};
61
Rui Ueyama0fcd8262016-05-20 19:55:17 +000062// This is GSIHashHdr.
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000063struct PublicsStream::GSIHashHeader {
Reid Klecknere1587bc2016-05-19 20:20:22 +000064 enum : unsigned {
65 HdrSignature = ~0U,
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000066 HdrVersion = 0xeffe0000 + 19990810,
67 };
68 ulittle32_t VerSignature;
69 ulittle32_t VerHdr;
70 ulittle32_t HrSize;
71 ulittle32_t NumBuckets;
72};
73
Zachary Turnera1657a92016-06-08 17:26:39 +000074PublicsStream::PublicsStream(PDBFile &File,
75 std::unique_ptr<MappedBlockStream> Stream)
76 : Pdb(File), Stream(std::move(Stream)) {}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000077
78PublicsStream::~PublicsStream() {}
79
80uint32_t PublicsStream::getSymHash() const { return Header->SymHash; }
81uint32_t PublicsStream::getAddrMap() const { return Header->AddrMap; }
82
83// Publics stream contains fixed-size headers and a serialized hash table.
84// This implementation is not complete yet. It reads till the end of the
85// stream so that we verify the stream is at least not corrupted. However,
86// we skip over the hash table which we believe contains information about
87// public symbols.
88Error PublicsStream::reload() {
Zachary Turnera1657a92016-06-08 17:26:39 +000089 codeview::StreamReader Reader(*Stream);
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000090
91 // Check stream size.
92 if (Reader.bytesRemaining() < sizeof(HeaderInfo) + sizeof(GSIHashHeader))
93 return make_error<RawError>(raw_error_code::corrupt_file,
94 "Publics Stream does not contain a header.");
95
96 // Read PSGSIHDR and GSIHashHdr structs.
Zachary Turner8dbe3622016-05-27 01:54:44 +000097 if (Reader.readObject(Header))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000098 return make_error<RawError>(raw_error_code::corrupt_file,
99 "Publics Stream does not contain a header.");
Zachary Turner8dbe3622016-05-27 01:54:44 +0000100
101 if (Reader.readObject(HashHdr))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000102 return make_error<RawError>(raw_error_code::corrupt_file,
103 "Publics Stream does not contain a header.");
104
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000105 // An array of HashRecord follows. Read them.
Zachary Turnerb393d952016-05-27 03:51:53 +0000106 if (HashHdr->HrSize % sizeof(PSHashRecord))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000107 return make_error<RawError>(raw_error_code::corrupt_file,
108 "Invalid HR array size.");
Zachary Turnerb393d952016-05-27 03:51:53 +0000109 uint32_t NumHashRecords = HashHdr->HrSize / sizeof(PSHashRecord);
110 if (auto EC = Reader.readArray(HashRecords, NumHashRecords))
David Majnemer836937e2016-05-27 16:16:56 +0000111 return joinErrors(std::move(EC),
112 make_error<RawError>(raw_error_code::corrupt_file,
113 "Could not read an HR array"));
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000114
115 // A bitmap of a fixed length follows.
116 size_t BitmapSizeInBits = alignTo(IPHR_HASH + 1, 32);
Zachary Turnerb393d952016-05-27 03:51:53 +0000117 uint32_t NumBitmapEntries = BitmapSizeInBits / 8;
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000118 if (auto EC = Reader.readBytes(Bitmap, NumBitmapEntries))
David Majnemer836937e2016-05-27 16:16:56 +0000119 return joinErrors(std::move(EC),
120 make_error<RawError>(raw_error_code::corrupt_file,
121 "Could not read a bitmap."));
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000122 for (uint8_t B : Bitmap)
123 NumBuckets += countPopulation(B);
124
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000125 // We don't yet understand the following data structures completely,
126 // but we at least know the types and sizes. Here we are trying
127 // to read the stream till end so that we at least can detect
128 // corrupted streams.
129
130 // Hash buckets follow.
Zachary Turnerb393d952016-05-27 03:51:53 +0000131 if (auto EC = Reader.readArray(HashBuckets, NumBuckets))
David Majnemer836937e2016-05-27 16:16:56 +0000132 return joinErrors(std::move(EC),
133 make_error<RawError>(raw_error_code::corrupt_file,
134 "Hash buckets corrupted."));
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000135
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000136 // Something called "address map" follows.
Zachary Turnerb393d952016-05-27 03:51:53 +0000137 uint32_t NumAddressMapEntries = Header->AddrMap / sizeof(uint32_t);
138 if (auto EC = Reader.readArray(AddressMap, NumAddressMapEntries))
David Majnemer836937e2016-05-27 16:16:56 +0000139 return joinErrors(std::move(EC),
140 make_error<RawError>(raw_error_code::corrupt_file,
141 "Could not read an address map."));
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000142
143 // Something called "thunk map" follows.
Zachary Turnerb393d952016-05-27 03:51:53 +0000144 if (auto EC = Reader.readArray(ThunkMap, Header->NumThunks))
David Majnemer836937e2016-05-27 16:16:56 +0000145 return joinErrors(std::move(EC),
146 make_error<RawError>(raw_error_code::corrupt_file,
147 "Could not read a thunk map."));
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000148
149 // Something called "section map" follows.
Zachary Turnerb393d952016-05-27 03:51:53 +0000150 if (auto EC = Reader.readArray(SectionOffsets, Header->NumSections))
David Majnemer836937e2016-05-27 16:16:56 +0000151 return joinErrors(std::move(EC),
152 make_error<RawError>(raw_error_code::corrupt_file,
153 "Could not read a section map."));
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000154
155 if (Reader.bytesRemaining() > 0)
156 return make_error<RawError>(raw_error_code::corrupt_file,
157 "Corrupted publics stream.");
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000158 return Error::success();
159}
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000160
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000161iterator_range<codeview::CVSymbolArray::Iterator>
162PublicsStream::getSymbols(bool *HadError) const {
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000163 auto SymbolS = Pdb.getPDBSymbolStream();
Zachary Turner9e33e6f2016-05-24 18:55:14 +0000164 if (SymbolS.takeError()) {
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000165 codeview::CVSymbolArray::Iterator Iter;
166 return llvm::make_range(Iter, Iter);
Zachary Turner9e33e6f2016-05-24 18:55:14 +0000167 }
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000168 SymbolStream &SS = SymbolS.get();
169
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000170 return SS.getSymbols(HadError);
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000171}
Zachary Turner8848a7a2016-07-06 18:05:57 +0000172
173Error PublicsStream::commit() { return Error::success(); }