blob: 2e0b8971f915bda372ee733684ac785f9cec08f2 [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"
Rui Ueyama0fcd8262016-05-20 19:55:17 +000030#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000031#include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
32#include "llvm/DebugInfo/PDB/Raw/RawError.h"
33#include "llvm/DebugInfo/PDB/Raw/StreamReader.h"
Rui Ueyama0fcd8262016-05-20 19:55:17 +000034#include "llvm/DebugInfo/PDB/Raw/SymbolStream.h"
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000035
36#include "llvm/ADT/BitVector.h"
37#include "llvm/Support/Endian.h"
38#include "llvm/Support/Format.h"
39#include "llvm/Support/MathExtras.h"
40
41using namespace llvm;
42using namespace llvm::support;
43using namespace llvm::pdb;
44
45
46static const unsigned IPHR_HASH = 4096;
47
48// This is PSGSIHDR struct defined in
49// https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/dbi/gsi.h
50struct PublicsStream::HeaderInfo {
51 ulittle32_t SymHash;
52 ulittle32_t AddrMap;
53 ulittle32_t NumThunks;
54 ulittle32_t SizeOfThunk;
55 ulittle16_t ISectThunkTable;
56 char Padding[2];
57 ulittle32_t OffThunkTable;
Rui Ueyama8dc18c52016-05-17 23:07:48 +000058 ulittle32_t NumSections;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000059};
60
Rui Ueyama0fcd8262016-05-20 19:55:17 +000061// This is GSIHashHdr.
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000062struct PublicsStream::GSIHashHeader {
Reid Klecknere1587bc2016-05-19 20:20:22 +000063 enum : unsigned {
64 HdrSignature = ~0U,
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000065 HdrVersion = 0xeffe0000 + 19990810,
66 };
67 ulittle32_t VerSignature;
68 ulittle32_t VerHdr;
69 ulittle32_t HrSize;
70 ulittle32_t NumBuckets;
71};
72
Rui Ueyama0fcd8262016-05-20 19:55:17 +000073// This is HRFile.
74struct PublicsStream::HashRecord {
75 ulittle32_t Off; // Offset in the symbol record stream
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000076 ulittle32_t CRef;
77};
78
Rui Ueyama8dc18c52016-05-17 23:07:48 +000079// This struct is defined as "SO" in langapi/include/pdb.h.
80namespace {
81struct SectionOffset {
82 ulittle32_t Off;
83 ulittle16_t Isect;
84 char Padding[2];
85};
86}
87
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000088PublicsStream::PublicsStream(PDBFile &File, uint32_t StreamNum)
Rui Ueyama0fcd8262016-05-20 19:55:17 +000089 : Pdb(File), StreamNum(StreamNum), Stream(StreamNum, File) {}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000090
91PublicsStream::~PublicsStream() {}
92
93uint32_t PublicsStream::getSymHash() const { return Header->SymHash; }
94uint32_t PublicsStream::getAddrMap() const { return Header->AddrMap; }
95
96// Publics stream contains fixed-size headers and a serialized hash table.
97// This implementation is not complete yet. It reads till the end of the
98// stream so that we verify the stream is at least not corrupted. However,
99// we skip over the hash table which we believe contains information about
100// public symbols.
101Error PublicsStream::reload() {
102 StreamReader Reader(Stream);
103
104 // Check stream size.
105 if (Reader.bytesRemaining() < sizeof(HeaderInfo) + sizeof(GSIHashHeader))
106 return make_error<RawError>(raw_error_code::corrupt_file,
107 "Publics Stream does not contain a header.");
108
109 // Read PSGSIHDR and GSIHashHdr structs.
110 Header.reset(new HeaderInfo());
111 if (Reader.readObject(Header.get()))
112 return make_error<RawError>(raw_error_code::corrupt_file,
113 "Publics Stream does not contain a header.");
114 HashHdr.reset(new GSIHashHeader());
115 if (Reader.readObject(HashHdr.get()))
116 return make_error<RawError>(raw_error_code::corrupt_file,
117 "Publics Stream does not contain a header.");
118
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000119 // An array of HashRecord follows. Read them.
120 if (HashHdr->HrSize % sizeof(HashRecord))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000121 return make_error<RawError>(raw_error_code::corrupt_file,
122 "Invalid HR array size.");
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000123 HashRecords.resize(HashHdr->HrSize / sizeof(HashRecord));
124 if (auto EC = Reader.readArray<HashRecord>(HashRecords))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000125 return make_error<RawError>(raw_error_code::corrupt_file,
126 "Could not read an HR array");
127
128 // A bitmap of a fixed length follows.
129 size_t BitmapSizeInBits = alignTo(IPHR_HASH + 1, 32);
130 std::vector<uint8_t> Bitmap(BitmapSizeInBits / 8);
131 if (auto EC = Reader.readArray<uint8_t>(Bitmap))
132 return make_error<RawError>(raw_error_code::corrupt_file,
133 "Could not read a bitmap.");
134 for (uint8_t B : Bitmap)
135 NumBuckets += countPopulation(B);
136
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000137 // We don't yet understand the following data structures completely,
138 // but we at least know the types and sizes. Here we are trying
139 // to read the stream till end so that we at least can detect
140 // corrupted streams.
141
142 // Hash buckets follow.
Daniel Sanders016e6c42016-05-18 12:36:25 +0000143 std::vector<ulittle32_t> TempHashBuckets(NumBuckets);
Daniel Sandersc819d902016-05-18 09:59:14 +0000144 if (auto EC = Reader.readArray<ulittle32_t>(TempHashBuckets))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000145 return make_error<RawError>(raw_error_code::corrupt_file,
146 "Hash buckets corrupted.");
Daniel Sandersc819d902016-05-18 09:59:14 +0000147 HashBuckets.resize(NumBuckets);
148 std::copy(TempHashBuckets.begin(), TempHashBuckets.end(),
149 HashBuckets.begin());
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000150
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000151 // Something called "address map" follows.
Daniel Sanders016e6c42016-05-18 12:36:25 +0000152 std::vector<ulittle32_t> TempAddressMap(Header->AddrMap / sizeof(uint32_t));
153 if (auto EC = Reader.readArray<ulittle32_t>(TempAddressMap))
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000154 return make_error<RawError>(raw_error_code::corrupt_file,
155 "Could not read an address map.");
Daniel Sanders016e6c42016-05-18 12:36:25 +0000156 AddressMap.resize(Header->AddrMap / sizeof(uint32_t));
157 std::copy(TempAddressMap.begin(), TempAddressMap.end(), AddressMap.begin());
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000158
159 // Something called "thunk map" follows.
Daniel Sanders016e6c42016-05-18 12:36:25 +0000160 std::vector<ulittle32_t> TempThunkMap(Header->NumThunks);
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000161 ThunkMap.resize(Header->NumThunks);
Daniel Sanders016e6c42016-05-18 12:36:25 +0000162 if (auto EC = Reader.readArray<ulittle32_t>(TempThunkMap))
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000163 return make_error<RawError>(raw_error_code::corrupt_file,
164 "Could not read a thunk map.");
Daniel Sanders016e6c42016-05-18 12:36:25 +0000165 ThunkMap.resize(Header->NumThunks);
166 std::copy(TempThunkMap.begin(), TempThunkMap.end(), ThunkMap.begin());
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000167
168 // Something called "section map" follows.
Rui Ueyama350b2982016-05-18 16:24:16 +0000169 std::vector<SectionOffset> Offsets(Header->NumSections);
170 if (auto EC = Reader.readArray<SectionOffset>(Offsets))
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000171 return make_error<RawError>(raw_error_code::corrupt_file,
172 "Could not read a section map.");
Rui Ueyama350b2982016-05-18 16:24:16 +0000173 for (auto &SO : Offsets) {
174 SectionOffsets.push_back(SO.Off);
175 SectionOffsets.push_back(SO.Isect);
176 }
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000177
178 if (Reader.bytesRemaining() > 0)
179 return make_error<RawError>(raw_error_code::corrupt_file,
180 "Corrupted publics stream.");
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000181 return Error::success();
182}
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000183
184std::vector<std::string> PublicsStream::getSymbols() const {
185 auto SymbolS = Pdb.getPDBSymbolStream();
186 if (SymbolS.takeError())
187 return {};
188 SymbolStream &SS = SymbolS.get();
189
190 std::vector<std::string> Ret;
191 for (const HashRecord &HR : HashRecords) {
192 // For some reason, symbol offset is biased by one.
193 Expected<std::string> Name = SS.getSymbolName(HR.Off - 1);
194 if (Name.takeError())
195 return Ret;
196 Ret.push_back(std::move(Name.get()));
197 }
198 return Ret;
199}