blob: 8d4c58e6569adf76278e970a43c7f46d866a642d [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))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000109 return make_error<RawError>(raw_error_code::corrupt_file,
110 "Could not read an HR array");
111
112 // A bitmap of a fixed length follows.
113 size_t BitmapSizeInBits = alignTo(IPHR_HASH + 1, 32);
Zachary Turnerb393d952016-05-27 03:51:53 +0000114 uint32_t NumBitmapEntries = BitmapSizeInBits / 8;
115 if (auto EC = Reader.readBytes(NumBitmapEntries, Bitmap))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000116 return make_error<RawError>(raw_error_code::corrupt_file,
117 "Could not read a bitmap.");
118 for (uint8_t B : Bitmap)
119 NumBuckets += countPopulation(B);
120
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000121 // We don't yet understand the following data structures completely,
122 // but we at least know the types and sizes. Here we are trying
123 // to read the stream till end so that we at least can detect
124 // corrupted streams.
125
126 // Hash buckets follow.
Zachary Turnerb393d952016-05-27 03:51:53 +0000127 if (auto EC = Reader.readArray(HashBuckets, NumBuckets))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000128 return make_error<RawError>(raw_error_code::corrupt_file,
129 "Hash buckets corrupted.");
130
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000131 // Something called "address map" follows.
Zachary Turnerb393d952016-05-27 03:51:53 +0000132 uint32_t NumAddressMapEntries = Header->AddrMap / sizeof(uint32_t);
133 if (auto EC = Reader.readArray(AddressMap, NumAddressMapEntries))
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000134 return make_error<RawError>(raw_error_code::corrupt_file,
135 "Could not read an address map.");
136
137 // Something called "thunk map" follows.
Zachary Turnerb393d952016-05-27 03:51:53 +0000138 if (auto EC = Reader.readArray(ThunkMap, Header->NumThunks))
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000139 return make_error<RawError>(raw_error_code::corrupt_file,
140 "Could not read a thunk map.");
141
142 // Something called "section map" follows.
Zachary Turnerb393d952016-05-27 03:51:53 +0000143 if (auto EC = Reader.readArray(SectionOffsets, Header->NumSections))
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000144 return make_error<RawError>(raw_error_code::corrupt_file,
145 "Could not read a section map.");
146
147 if (Reader.bytesRemaining() > 0)
148 return make_error<RawError>(raw_error_code::corrupt_file,
149 "Corrupted publics stream.");
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000150 return Error::success();
151}
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000152
Zachary Turner9e33e6f2016-05-24 18:55:14 +0000153iterator_range<codeview::SymbolIterator> PublicsStream::getSymbols() const {
154 using codeview::SymbolIterator;
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000155 auto SymbolS = Pdb.getPDBSymbolStream();
Zachary Turner9e33e6f2016-05-24 18:55:14 +0000156 if (SymbolS.takeError()) {
157 return llvm::make_range<SymbolIterator>(SymbolIterator(), SymbolIterator());
158 }
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000159 SymbolStream &SS = SymbolS.get();
160
Zachary Turner9e33e6f2016-05-24 18:55:14 +0000161 return SS.getSymbols();
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000162}