blob: d3a7ffd7f93f9336d80f86e5acc754aab43d0276 [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"
30#include "llvm/DebugInfo/PDB/Raw/MappedBlockStream.h"
Rui Ueyama0fcd8262016-05-20 19:55:17 +000031#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000032#include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
33#include "llvm/DebugInfo/PDB/Raw/RawError.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 Ueyama1f6b6e22016-05-13 21:21:53 +000073PublicsStream::PublicsStream(PDBFile &File, uint32_t StreamNum)
Rui Ueyama0fcd8262016-05-20 19:55:17 +000074 : Pdb(File), StreamNum(StreamNum), Stream(StreamNum, File) {}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000075
76PublicsStream::~PublicsStream() {}
77
78uint32_t PublicsStream::getSymHash() const { return Header->SymHash; }
79uint32_t PublicsStream::getAddrMap() const { return Header->AddrMap; }
80
81// Publics stream contains fixed-size headers and a serialized hash table.
82// This implementation is not complete yet. It reads till the end of the
83// stream so that we verify the stream is at least not corrupted. However,
84// we skip over the hash table which we believe contains information about
85// public symbols.
86Error PublicsStream::reload() {
Zachary Turnerd5d37dc2016-05-25 20:37:03 +000087 codeview::StreamReader Reader(Stream);
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000088
89 // Check stream size.
90 if (Reader.bytesRemaining() < sizeof(HeaderInfo) + sizeof(GSIHashHeader))
91 return make_error<RawError>(raw_error_code::corrupt_file,
92 "Publics Stream does not contain a header.");
93
94 // Read PSGSIHDR and GSIHashHdr structs.
Zachary Turner8dbe3622016-05-27 01:54:44 +000095 if (Reader.readObject(Header))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000096 return make_error<RawError>(raw_error_code::corrupt_file,
97 "Publics Stream does not contain a header.");
Zachary Turner8dbe3622016-05-27 01:54:44 +000098
99 if (Reader.readObject(HashHdr))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000100 return make_error<RawError>(raw_error_code::corrupt_file,
101 "Publics Stream does not contain a header.");
102
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000103 // An array of HashRecord follows. Read them.
Zachary Turnerb393d952016-05-27 03:51:53 +0000104 if (HashHdr->HrSize % sizeof(PSHashRecord))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000105 return make_error<RawError>(raw_error_code::corrupt_file,
106 "Invalid HR array size.");
Zachary Turnerb393d952016-05-27 03:51:53 +0000107 uint32_t NumHashRecords = HashHdr->HrSize / sizeof(PSHashRecord);
108 if (auto EC = Reader.readArray(HashRecords, NumHashRecords))
David Majnemer836937e2016-05-27 16:16:56 +0000109 return joinErrors(std::move(EC),
110 make_error<RawError>(raw_error_code::corrupt_file,
111 "Could not read an HR array"));
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000112
113 // A bitmap of a fixed length follows.
114 size_t BitmapSizeInBits = alignTo(IPHR_HASH + 1, 32);
Zachary Turnerb393d952016-05-27 03:51:53 +0000115 uint32_t NumBitmapEntries = BitmapSizeInBits / 8;
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000116 if (auto EC = Reader.readBytes(Bitmap, NumBitmapEntries))
David Majnemer836937e2016-05-27 16:16:56 +0000117 return joinErrors(std::move(EC),
118 make_error<RawError>(raw_error_code::corrupt_file,
119 "Could not read a bitmap."));
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000120 for (uint8_t B : Bitmap)
121 NumBuckets += countPopulation(B);
122
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000123 // We don't yet understand the following data structures completely,
124 // but we at least know the types and sizes. Here we are trying
125 // to read the stream till end so that we at least can detect
126 // corrupted streams.
127
128 // Hash buckets follow.
Zachary Turnerb393d952016-05-27 03:51:53 +0000129 if (auto EC = Reader.readArray(HashBuckets, NumBuckets))
David Majnemer836937e2016-05-27 16:16:56 +0000130 return joinErrors(std::move(EC),
131 make_error<RawError>(raw_error_code::corrupt_file,
132 "Hash buckets corrupted."));
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000133
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000134 // Something called "address map" follows.
Zachary Turnerb393d952016-05-27 03:51:53 +0000135 uint32_t NumAddressMapEntries = Header->AddrMap / sizeof(uint32_t);
136 if (auto EC = Reader.readArray(AddressMap, NumAddressMapEntries))
David Majnemer836937e2016-05-27 16:16:56 +0000137 return joinErrors(std::move(EC),
138 make_error<RawError>(raw_error_code::corrupt_file,
139 "Could not read an address map."));
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000140
141 // Something called "thunk map" follows.
Zachary Turnerb393d952016-05-27 03:51:53 +0000142 if (auto EC = Reader.readArray(ThunkMap, Header->NumThunks))
David Majnemer836937e2016-05-27 16:16:56 +0000143 return joinErrors(std::move(EC),
144 make_error<RawError>(raw_error_code::corrupt_file,
145 "Could not read a thunk map."));
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000146
147 // Something called "section map" follows.
Zachary Turnerb393d952016-05-27 03:51:53 +0000148 if (auto EC = Reader.readArray(SectionOffsets, Header->NumSections))
David Majnemer836937e2016-05-27 16:16:56 +0000149 return joinErrors(std::move(EC),
150 make_error<RawError>(raw_error_code::corrupt_file,
151 "Could not read a section map."));
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000152
153 if (Reader.bytesRemaining() > 0)
154 return make_error<RawError>(raw_error_code::corrupt_file,
155 "Corrupted publics stream.");
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000156 return Error::success();
157}
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000158
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000159iterator_range<codeview::CVSymbolArray::Iterator>
160PublicsStream::getSymbols(bool *HadError) const {
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000161 auto SymbolS = Pdb.getPDBSymbolStream();
Zachary Turner9e33e6f2016-05-24 18:55:14 +0000162 if (SymbolS.takeError()) {
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000163 codeview::CVSymbolArray::Iterator Iter;
164 return llvm::make_range(Iter, Iter);
Zachary Turner9e33e6f2016-05-24 18:55:14 +0000165 }
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000166 SymbolStream &SS = SymbolS.get();
167
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000168 return SS.getSymbols(HadError);
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000169}